-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathSimpleTableCellEditor.es6.min.js
1 lines (1 loc) · 7.6 KB
/
SimpleTableCellEditor.es6.min.js
1
"use strict"; function _defineProperties(e, t) { for (var i = 0; i < t.length; i++) { var l = t[i]; l.enumerable = l.enumerable || !1, l.configurable = !0, "value" in l && (l.writable = !0), Object.defineProperty(e, l.key, l) } } function _createClass(e, t, i) { return t && _defineProperties(e.prototype, t), i && _defineProperties(e, i), e } function _instanceof(e, t) { return null != t && "undefined" != typeof Symbol && t[Symbol.hasInstance] ? !!t[Symbol.hasInstance](e) : e instanceof t } function _classCallCheck(e, t) { if (!_instanceof(e, t)) throw new TypeError("Cannot call a class as a function") } window.onload = function () { if (!window.jQuery) throw "jQuery is not loaded" }; var SimpleTableCellEdition = function e(t, i) { _classCallCheck(this, e), this.Elem = t, this.oldContent = $(t).html(), this.oldValue = i.internals.extractValue(t), this.cellParams = i }, SimpleTableCellEditor = function () { function e(t, i) { _classCallCheck(this, e); var l = this; l.EditionEndOrigin = { OutsideTable: 1, AnotherCell: 2 }, l.editableClasses = [], void 0 === t && (t = "table"), this.active = !0, this.tableId = t, this.params = l._GetExtendedEditorParams(i), this.CellEdition = null, this._TryHandleDataTableReloadEvent(), $(document).mouseup(function (e) { var t = $("#".concat(l.tableId)); t.is(e.target) || 0 !== t.has(e.target).length || l._FreeCurrentCell(l.EditionEndOrigin.OutsideTable) }) } return _createClass(e, [{ key: "SetEditable", value: function (e, t) { var i = this; if (i._isValidElem(e)) { var l = i._GetExtendedCellParams(t); $(e).on("click", function (e) { i.active && ($(this).hasClass(i.params.inEditClass) || i._EditCell(this, l)) }), $(e).on("keydown", function (e) { i.active && $(this).hasClass(i.params.inEditClass) && i._HandleKeyPressed(e, this, l) }) } } }, { key: "SetEditableClass", value: function (e, t) { var i = this, l = i._GetExtendedCellParams(t); i.editableClasses.push(".".concat(e)), $("#".concat(i.tableId)).on("click", "td.".concat(e, ":not(.").concat(i.params.inEditClass, ")"), function () { i.active && i._EditCell(this, l) }), $("#".concat(i.tableId)).on("keydown", "td.".concat(e, ".").concat(i.params.inEditClass), function (e) { i.active && i._HandleKeyPressed(e, this, l) }) } }, { key: "Toggle", value: function (e) { void 0 === e && (e = !this.active), this.active = e } }, { key: "_HandleKeyPressed", value: function (e, t, i) { var l = e.which, n = e.shiftKey, a = $(t).closest("td").index(), r = ($(t).closest("tr").index(), !1), s = !1, o = !1, d = !1; if (this.params.navigation && 0 !== this.editableClasses.length && (i.behaviour.arrowKeyCauseCursorMove && n && (39 === l ? r = !0 : 37 === l ? s = !0 : 40 === l ? o = !0 : 38 === l && (d = !0)), i.behaviour.tabKeyCauseCursorMove && 9 === l && (n ? s = !0 : r = !0), r || s || o || d)) { e.preventDefault(); var u = $(t).closest("table").find(this.editableClasses.join(",")), c = []; $.each(u, function () { $(this).is(":visible") && c.push(this) }); var h, E = $(c), C = E.index($(t)); if (r)++C === E.length && (C = 0); else if (s)--C < 0 && (C = E.length - 1); else if (o || d) { var v = []; $.each(E, function () { $(this).closest("td").index() === a && v.push(this) }); var f = $(v), _ = f.index($(t)); o ? ++_ === f.length && (_ = 0) : d && --_ < 0 && (_ = f.length - 1); C = E.index($(f[_])) } return h = $(E[C]), this._FreeCell(t, i, !0), void h.click() } i.keys.validation.includes(l) ? this._FreeCell(t, i, !0) : i.keys.cancellation.includes(l) && this._FreeCell(t, i, !1) } }, { key: "_EditCell", value: function (e, t) { if (!this._FireOnEditEnterEvent(e).isDefaultPrevented()) { this._FreeCurrentCell(this.EditionEndOrigin.AnotherCell), this.CellEdition = new SimpleTableCellEdition(e, t), this.isDataTable && (this.CellEdition.cellIndex = $("#".concat(this.tableId)).DataTable().cell($(e)).index()); var i = t.internals.extractValue(e); $(e).addClass(this.params.inEditClass), t.internals.renderEditor(e, i), this._FireOnEditEnteredEvent(e, i) } } }, { key: "_EndEditCell", value: function (e, t) { this._FreeCell(e, t, !0) } }, { key: "_CancelEditCell", value: function (e, t) { this._FreeCell(e, t, !1) } }, { key: "_FreeCell", value: function (e, t, i) { if (this._isValidElem(e) && null !== this.CellEdition && !this._FireOnEditExitEvent(e, this.CellEdition.oldValue).isDefaultPrevented()) { var l = t.internals.extractEditorValue(e); $(e).removeClass(this.params.inEditClass), $(e).html(""), t.validation(l) && this.CellEdition.oldValue !== l || (i = !1); var n = t.formatter(l); this._FireOnEditExitedEvent(e, this.CellEdition.oldValue, n, i), i ? (t.internals.renderValue(e, n), this._FireEditedEvent(e, this.CellEdition.oldValue, n)) : $(e).html(this.CellEdition.oldContent), this.CellEdition = null } } }, { key: "_FreeCurrentCell", value: function (e) { var t = this._GetCurrentEdition(); if (null !== t) { var i = !0; e === this.EditionEndOrigin.OutsideTable && t.cellParams.behaviour.outsideTableClickCauseCancellation && (i = !1), e === this.EditionEndOrigin.AnotherCell && t.cellParams.behaviour.anotherCellClickCauseCancellation && (i = !1), this._FreeCell(t.Elem, t.cellParams, i) } } }, { key: "_GetCurrentEdition", value: function () { return null === this.CellEdition ? null : this.CellEdition } }, { key: "_GetExtendedEditorParams", value: function (e) { return $.extend(!0, {}, this._GetDefaultEditorParams(), e) } }, { key: "_GetExtendedCellParams", value: function (e) { return $.extend(!0, {}, this._GetDefaultCellParams(), e) } }, { key: "_FireOnEditEnterEvent", value: function (e) { var t = jQuery.Event("cell:onEditEnter", { element: e }); return $("#".concat(this.tableId)).trigger(t), t } }, { key: "_FireOnEditEnteredEvent", value: function (e, t) { $("#".concat(this.tableId)).trigger({ type: "cell:onEditEntered", element: e, oldValue: t }) } }, { key: "_FireOnEditExitEvent", value: function (e, t) { var i = jQuery.Event("cell:onEditExit", { element: e, oldValue: t }); return $("#".concat(this.tableId)).trigger(i), i } }, { key: "_FireOnEditExitedEvent", value: function (e, t, i, l) { $("#".concat(this.tableId)).trigger({ type: "cell:onEditExited", element: e, newValue: i, oldValue: t, applied: l }) } }, { key: "_FireEditedEvent", value: function (e, t, i) { $("#".concat(this.tableId)).trigger({ type: "cell:edited", element: e, newValue: i, oldValue: t }) } }, { key: "_TryHandleDataTableReloadEvent", value: function () { var e = this; this.isDataTable = !1; try { $.fn.DataTable.isDataTable("#".concat(e.tableId)) && (e.isDataTable = !0) } catch (e) { return } e.isDataTable && $("#".concat(e.tableId)).on("draw.dt", function () { if (null !== e.CellEdition && null !== e.CellEdition.Elem) { var t = $("#".concat(e.tableId)).DataTable().cell(e.CellEdition.cellIndex).node(); e._EditCell(t, e.CellEdition.cellParams) } }) } }, { key: "_GetDefaultEditorParams", value: function () { return { inEditClass: "inEdit", navigation: !0 } } }, { key: "_GetDefaultCellParams", value: function () { return { validation: function (e) { return !0 }, formatter: function (e) { return e }, keys: { validation: [13], cancellation: [27] }, behaviour: { tabKeyCauseCursorMove: !0, arrowKeyCauseCursorMove: !0, outsideTableClickCauseCancellation: !1, anotherCellClickCauseCancellation: !1 }, internals: this._GetDefaultInternals() } } }, { key: "_GetDefaultInternals", value: function () { return { renderValue: function (e, t) { $(e).text(t) }, renderEditor: function (e, t) { $(e).html("<input type='text' style=\"width:100%; max-width:none\">"); var i = $(e).find("input"); i.focus(), i.val(t) }, extractEditorValue: function (e) { return $(e).find("input").val() }, extractValue: function (e) { return $(e).text() } } } }, { key: "_isValidElem", value: function (e) { return null != e && $(e).length > 0 } }]), e }();