@logimaxx/kgrid 0.2.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,113 @@
1
+ /**
2
+ * Default table/column config prototypes and allowed input types.
3
+ */
4
+ (function (CT) {
5
+ CT.protoOptions = {
6
+ "url": null,
7
+ "type": null,
8
+ filter: null,
9
+ "features": {
10
+ "filtering": false,
11
+ "sorting": false,
12
+ "paging": false,
13
+ "create": false,
14
+ "update": false,
15
+ "delete": false
16
+ },
17
+ "defaultInteraction": "view",
18
+ "insertFormRow": {
19
+ "position": "top"
20
+ },
21
+ "tableAttrs": {
22
+ "class": ""
23
+ },
24
+ "labelsRowAttrs": {
25
+
26
+ },
27
+ "dataRowAttrs": {
28
+
29
+ },
30
+ "filtersRowAttrs": null,
31
+ /** Page size choices in paging footer <select> */
32
+ pagingPageSizes: [10, 25, 50, 75],
33
+ pagingDefaultSize: 10,
34
+ /** Initial text in .no-data-tbody (overridden by noDataTemplate after init) */
35
+ emptyRowMessage: null,
36
+ pagingFooterLabel: "records per page. Total",
37
+ };
38
+
39
+ CT.protoColumnConfig = {
40
+ label: null,
41
+ name: null,
42
+ hidden: false,
43
+ /** CSS class(es) on header/filter/data/insert cells (alias: columnClass) */
44
+ class: null,
45
+ columnClass: null,
46
+ attrs: {},
47
+ features: {
48
+ create: false,
49
+ update: false,
50
+ filter: false,
51
+ sort: false
52
+ },
53
+ display: {
54
+ template: null,
55
+ events: []
56
+ },
57
+ /**
58
+ * Shared insert+update defaults. Explicit insert/update win on conflict.
59
+ * Example: input: { type: "number", required: true }
60
+ */
61
+ input: null,
62
+ insert: {
63
+ type: "text",
64
+ default: null,
65
+ value: null,
66
+ placeholder: null,
67
+ disabled: false,
68
+ required: false,
69
+ options: null,
70
+ events: []
71
+ },
72
+ update: {
73
+ type: "text",
74
+ default: null,
75
+ value: null,
76
+ placeholder: null,
77
+ disabled: false,
78
+ required: false,
79
+ options: null,
80
+ template: null,
81
+ events: []
82
+ },
83
+ filter: {
84
+ type: "text",
85
+ operator: "~=~",
86
+ default: null,
87
+ placeholder: "",
88
+ options: null,
89
+ /** Keep default across filter form reset / re-submit */
90
+ persist: false,
91
+ /** Override KGrid.configure({ filterDebounceMs }); 0 = submit immediately */
92
+ debounceMs: null,
93
+ }
94
+ };
95
+
96
+ CT.VALID_NATIVE_INPUT_TYPES = ["displayonly","text","textarea","number","date","datetime","time","checkbox","radio","file","password","email","url","search","tel","select","hidden"];
97
+ /** @deprecated use VALID_NATIVE_INPUT_TYPES or isValidInputType() */
98
+ CT.VALID_INPUT_TYPES = CT.VALID_NATIVE_INPUT_TYPES.slice();
99
+ CT.VALID_NATIVE_FILTER_TYPES = CT.VALID_NATIVE_INPUT_TYPES.filter((t) => t !== "displayonly");
100
+ /** @deprecated use isValidFilterType() */
101
+ CT.VALID_FILTER_TYPES = CT.VALID_NATIVE_FILTER_TYPES.concat(["multi_select","date_range"]);
102
+
103
+ CT.isValidInputType = function (type) {
104
+ return CT.VALID_NATIVE_INPUT_TYPES.includes(type) || CT.isPluggableFieldType(type);
105
+ };
106
+
107
+ CT.isValidFilterType = function (type) {
108
+ if (type === "displayonly") {
109
+ return false;
110
+ }
111
+ return CT.VALID_NATIVE_FILTER_TYPES.includes(type) || CT.isPluggableFieldType(type);
112
+ };
113
+ })(window.KGrid = window.KGrid || {});
@@ -0,0 +1,99 @@
1
+ (function (CT) {
2
+ CT.setupNoDataTbody = function (noDataTbody, options, noVisibleCols) {
3
+ const td = noDataTbody.find("td")
4
+ .attr("colspan", noVisibleCols);
5
+
6
+ if(options.noDataTemplate) {
7
+ td.html(options.noDataTemplate);
8
+ }
9
+ return td;
10
+ };
11
+
12
+ CT.setupDataBody = function (dataBody, options, labelsRow, filterForm, pagingFooter, noDataTbody) {
13
+ const columns = [...options.columns];
14
+ const dataRow = $("<tr>").appendTo(dataBody);
15
+ if(options.dataRowAttrs && typeof options.dataRowAttrs!=="object") {
16
+ throw new Error("options.dataRowAttrs must be an object");
17
+ }
18
+ const dataRowAttrs = {...(options.dataRowAttrs ?? {})};
19
+ Object.keys(dataRowAttrs).forEach(attr => dataRow.attr(attr,dataRowAttrs[attr]));
20
+
21
+ {
22
+ dataBody.data("emptyview",noDataTbody);
23
+
24
+ if(options?.features?.sorting) {
25
+ dataBody.data("sort",labelsRow);
26
+ }
27
+ if(pagingFooter) {
28
+ dataBody.data("paging",pagingFooter.find(".pages"))
29
+ .data("pagesizeinp",pagingFooter.find(".pagesize"))
30
+ .data("totalrecscount",pagingFooter.find(".totalrecscount"));
31
+ }
32
+
33
+ if(filterForm) {
34
+ dataBody.data("filter",filterForm);
35
+ }
36
+
37
+ if(options.type) {
38
+ dataBody.data("type",options.type);
39
+ }
40
+ }
41
+
42
+ const dataRowFormId = "data_row_form_"+CT.uuid()+"_{{this.id}}";
43
+ const editForm = options.features.update
44
+ ? $("<form class='edit-form table-row-form'>").attr("id", dataRowFormId)
45
+ : null;
46
+
47
+ columns.forEach(col => {
48
+ const dataCell = CT.setupCell(col,editForm);
49
+ if(!col.hidden) {
50
+ dataCell.appendTo(dataRow);
51
+ }
52
+ });
53
+
54
+ if (CT.hasActionColumn(options)) {
55
+ const buttonColumn = $("<td>").addClass("kgrid-row-actions").appendTo(dataRow);
56
+ if(options.features.delete) {
57
+ $("<div>").addClass("btn-group delete-item-grp").appendTo(buttonColumn).append(
58
+ $("<button>").addClass("btn btn-sm btn-danger delete-item")
59
+ .attr("type","button")
60
+ .attr("title","Delete item")
61
+ .append("<i class='fas fa-trash'></i>"));
62
+ }
63
+
64
+ if(options.features.update) {
65
+ const grp = $("<div>").addClass("btn-group edit-item-grp").appendTo(buttonColumn);
66
+ $("<button>").addClass("btn btn-sm btn-success save-item")
67
+ .attr("type","submit")
68
+ .attr("name","save")
69
+ .attr("title","Save item")
70
+ .attr("form",dataRowFormId)
71
+ .html("<i class='fas fa-save'></i>")
72
+ .appendTo(grp);
73
+ $("<button>").addClass("btn btn-sm btn-secondary cancel-edit")
74
+ .attr("type","button")
75
+ .attr("name","cancel")
76
+ .attr("title","Cancel edit")
77
+ .attr("form",dataRowFormId)
78
+ .attr("onclick","$(this).parents('[data-type=item]').data().instance.loadFromRemote()")
79
+ .html("<i class='fas fa-undo'></i>")
80
+ .appendTo(grp);
81
+ }
82
+ }
83
+ if (editForm) {
84
+ CT.anchorRowForm(editForm, dataRow);
85
+ }
86
+
87
+ return dataBody;
88
+ };
89
+
90
+ CT.setupPagingFooter = function (footer, options, noVisibleCols) {
91
+ CT.log("setupPagingFooter",footer,options,noVisibleCols);
92
+ if(options.pagingFooterAttrs && CT.isPlainObject(options.pagingFooterAttrs)) {
93
+ Object.keys(options.pagingFooterAttrs).forEach(att => footer.attr(att,options.pagingFooterAttrs[att]));
94
+ }
95
+ CT.log("noVisibleCols",noVisibleCols);
96
+ footer.find("td").attr("colspan",noVisibleCols);
97
+ return footer;
98
+ };
99
+ })(window.KGrid);
package/src/dom.js ADDED
@@ -0,0 +1,123 @@
1
+ (function (CT) {
2
+ /**
3
+ * Normalize init host: native DOM element or jQuery collection.
4
+ * @param {Element|JQuery} host
5
+ * @returns {JQuery}
6
+ */
7
+ CT.resolveHostElement = function (host) {
8
+ if (host == null) {
9
+ throw new TypeError("KGrid.init(host, opts): host is required");
10
+ }
11
+ if (typeof host === "object" && host.jquery) {
12
+ if (!host.length) {
13
+ throw new Error("KGrid.init(host, opts): empty jQuery selection");
14
+ }
15
+ return host;
16
+ }
17
+ if (
18
+ typeof host === "object" &&
19
+ host.nodeType === 1 &&
20
+ typeof host.nodeName === "string"
21
+ ) {
22
+ return $(host);
23
+ }
24
+ throw new TypeError(
25
+ "KGrid.init(host, opts): host must be a DOM Element or jQuery object"
26
+ );
27
+ };
28
+
29
+ CT.uuid = function () {
30
+ return Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15);
31
+ };
32
+
33
+ /**
34
+ * Whether the table needs a trailing row-actions column (header, filters, data rows, colspan).
35
+ * @param {Object} options table options with features
36
+ * @returns {boolean}
37
+ */
38
+ CT.hasActionColumn = function (options) {
39
+ const f = options && options.features;
40
+ if (!f) {
41
+ return false;
42
+ }
43
+ return !!(f.delete || f.update || f.create);
44
+ };
45
+
46
+ /**
47
+ * Sync <colgroup> so row-actions width can collapse in view (table-layout: fixed).
48
+ * @param {JQuery} $table
49
+ * @param {number} dataColumnCount visible data columns (no row-actions)
50
+ * @param {boolean} hasActions
51
+ */
52
+ CT.syncActionColumnColgroup = function ($table, dataColumnCount, hasActions) {
53
+ let $colgroup = $table.children("colgroup.kgrid-colgroup");
54
+ if (!$colgroup.length) {
55
+ $colgroup = $("<colgroup>").addClass("kgrid-colgroup").prependTo($table);
56
+ }
57
+ $colgroup.empty();
58
+ for (let i = 0; i < dataColumnCount; i++) {
59
+ $colgroup.append($("<col>"));
60
+ }
61
+ if (hasActions) {
62
+ $colgroup.append($("<col>").addClass("kgrid-row-actions-col"));
63
+ }
64
+ };
65
+
66
+ /**
67
+ * Place a hidden <form> anchor on a table row template.
68
+ * Row controls use form="id" (cannot wrap <tr> in one <form>).
69
+ */
70
+ CT.anchorRowForm = function ($form, $row) {
71
+ if (!$form || !$form.length || !$row || !$row.length) {
72
+ return $form;
73
+ }
74
+ $form.attr("hidden", "hidden").attr("aria-hidden", "true");
75
+ const $firstTd = $row.children("td").first();
76
+ if ($firstTd.length) {
77
+ $form.prependTo($firstTd);
78
+ }
79
+ return $form;
80
+ };
81
+
82
+ /** Field in filter row (associated via form="" on control, not nested inside <form>). */
83
+ CT.filterFormField = function (formEl, name) {
84
+ if (!formEl || !name) {
85
+ return $();
86
+ }
87
+ const el = formEl.elements && formEl.elements.namedItem(name);
88
+ if (el) {
89
+ return $(el);
90
+ }
91
+ if (formEl.id) {
92
+ const associated = document.querySelector(`[form="${formEl.id}"][name="${name}"]`);
93
+ if (associated) {
94
+ return $(associated);
95
+ }
96
+ }
97
+ return $();
98
+ };
99
+
100
+ /** Resolved CSS class for a column (`class` or alias `columnClass`). */
101
+ CT.columnClassName = function (col) {
102
+ if (!col) {
103
+ return null;
104
+ }
105
+ const cls = col.class != null && col.class !== "" ? col.class : col.columnClass;
106
+ return cls != null && cls !== "" ? String(cls) : null;
107
+ };
108
+
109
+ /** Add column class + data-name to a header/cell element. */
110
+ CT.applyColumnCellMeta = function ($el, col) {
111
+ if (!$el || !$el.length || !col) {
112
+ return $el;
113
+ }
114
+ if (col.name) {
115
+ $el.attr("data-name", col.name);
116
+ }
117
+ const cls = CT.columnClassName(col);
118
+ if (cls) {
119
+ $el.addClass(cls);
120
+ }
121
+ return $el;
122
+ };
123
+ })(window.KGrid);
package/src/events.js ADDED
@@ -0,0 +1,100 @@
1
+ (function (CT) {
2
+ /**
3
+ * Setup events for item after render
4
+ */
5
+ CT.setupEvents = function (item, table, options, colMap) {
6
+ const view = item.views[0];
7
+
8
+ options.columns.forEach(col=>{
9
+ col.display.events.forEach(event=>{
10
+ if(!event.selector || !event.event || !event.callback)
11
+ throw new Error("Invalid event: "+JSON.stringify(event,null,2));
12
+
13
+ view.el.find(event.selector).off(event.event).on(event.event,(e)=>{
14
+ if(typeof event.callback!=="function")
15
+ throw new Error("Event callback must be a function: "+JSON.stringify(event,null,2));
16
+ event.callback(e,item,view);
17
+ });
18
+ });
19
+
20
+ if(options.features.update) {
21
+ col.update.events.forEach((event)=>{
22
+ if(!event.event || !event.callback)
23
+ throw new Error("Invalid event: "+JSON.stringify(event,null,2));
24
+
25
+ view.el.find("[name='"+col.name+"']").off(event.event).on(event.event,function(e,...args){
26
+ CT.log("event",event,e,this,args);
27
+ if(typeof event.callback!=="function")
28
+ throw new Error("Event callback must be a function: "+JSON.stringify(event,null,2));
29
+ event.callback(e,item,view,...args);
30
+ });
31
+ });
32
+ }
33
+ });
34
+
35
+ if(options.features.update) {
36
+ view.el.find("[data-type]").each((index, el) => {
37
+ const type = el.getAttribute("data-type");
38
+ if (!CT.isPluggableFieldType(type)) {
39
+ return;
40
+ }
41
+ const col = colMap.get(el.name);
42
+ if (!col?.update) {
43
+ return;
44
+ }
45
+ CT.mountField({
46
+ mode: "update",
47
+ $input: $(el),
48
+ col,
49
+ config: col.update,
50
+ item,
51
+ view,
52
+ });
53
+ });
54
+
55
+ view.el.find("select[data-type='select']").each((index, input) => {
56
+ const col = colMap.get(input.name);
57
+ if (!col) {
58
+ return;
59
+ }
60
+ const val = item.attributes[col.name];
61
+ if (val == null || val === "") {
62
+ return;
63
+ }
64
+ $(input).val(typeof val === "boolean" ? String(val) : val);
65
+ });
66
+
67
+ view.el.find("form.edit-form").off("submit").on("submit",(event)=>{
68
+ const form = event.target;
69
+ event.preventDefault();
70
+ const instance = $(form).parents("[data-type=item]").data().instance;
71
+ const data = CT.serializeForm(form);
72
+ Object.keys(data).forEach(key => {
73
+ if(options.columns.find(col => col.name === key)?.update?.dontsave) {
74
+ delete data[key];
75
+ }
76
+ });
77
+ instance.update(data).catch(CT.onError);
78
+ });
79
+ }
80
+
81
+ if(options.features.delete) {
82
+ view.el.find("button.delete-item").off("click").on("click",(event)=>{
83
+ event.preventDefault();
84
+ view.el.addClass("confirm-delete");
85
+ const clearConfirmState = () => view.el.removeClass("confirm-delete");
86
+ CT.runDeleteConfirm(
87
+ { item, view, options },
88
+ () => {
89
+ item.delete().catch(CT.onError).finally(clearConfirmState);
90
+ },
91
+ clearConfirmState
92
+ );
93
+ });
94
+ }
95
+
96
+ if (typeof options.onRowFields === "function") {
97
+ options.onRowFields(item, view, table);
98
+ }
99
+ };
100
+ })(window.KGrid);
@@ -0,0 +1,25 @@
1
+ /**
2
+ * Built-in field types with no external library dependency (plain HTML / jQuery DOM).
3
+ */
4
+ (function (CT) {
5
+ CT.registerFieldType("multi_select", {
6
+ create({ config }) {
7
+ const $input = $("<select class='form-select form-select-sm' multiple>");
8
+ if (Array.isArray(config.options)) {
9
+ config.options.forEach((opt) => {
10
+ $("<option>").text(opt.label).attr("value", opt.value).appendTo($input);
11
+ });
12
+ }
13
+ return { $input, skipValueAttr: true };
14
+ },
15
+ });
16
+
17
+ CT.registerFieldType("date_range", {
18
+ create() {
19
+ const $input = $(
20
+ "<input autocomplete='off' type='date' class='form-control form-control-sm'/>"
21
+ );
22
+ return { $input };
23
+ },
24
+ });
25
+ })(window.KGrid);
@@ -0,0 +1,228 @@
1
+ /**
2
+ * Pluggable field types for filter / insert / update contexts.
3
+ *
4
+ * KGrid.configure({ customInputTypes: { myType: { create, mount?, … } } })
5
+ * KGrid.registerFieldType(name, plugin)
6
+ * KGrid.inputType(mount, { element }) — simple widget helper
7
+ *
8
+ * Plugin shape:
9
+ * create({ mode, col, config }) — required; returns { $input, skipValueAttr?, filterEvents? }
10
+ * filterEvents? — jQuery event names for filter submit (e.g. "change", "input"); false = none (use bindFilterSubmit)
11
+ * filterDebounceMs? — override configure/filter.debounceMs for this type
12
+ * mount?, validate?, bindFilterSubmit? — extra widget-specific filter events
13
+ */
14
+ (function (CT) {
15
+ CT._fieldTypes = Object.create(null);
16
+ CT._configuredCustomInputTypeNames = [];
17
+
18
+ CT.registerFieldType = function (name, plugin, options) {
19
+ if (!name || typeof name !== "string") {
20
+ throw new TypeError("registerFieldType(name, plugin): name must be a string");
21
+ }
22
+ if (!plugin || typeof plugin.create !== "function") {
23
+ throw new TypeError("registerFieldType(" + name + "): plugin.create is required");
24
+ }
25
+ if (CT._fieldTypes[name] && !(options && options.overwrite)) {
26
+ throw new Error("Field type already registered: " + name);
27
+ }
28
+ CT._fieldTypes[name] = Object.assign({ name }, plugin);
29
+ return CT;
30
+ };
31
+
32
+ CT.unregisterFieldType = function (name) {
33
+ if (name && CT._fieldTypes[name]) {
34
+ delete CT._fieldTypes[name];
35
+ }
36
+ return CT;
37
+ };
38
+
39
+ CT.getFieldType = function (name) {
40
+ return name ? CT._fieldTypes[name] || null : null;
41
+ };
42
+
43
+ CT.listFieldTypes = function () {
44
+ return Object.keys(CT._fieldTypes);
45
+ };
46
+
47
+ CT.createFieldInput = function ({ mode, col, config }) {
48
+ const type = config.type ?? "text";
49
+ const plugin = CT.getFieldType(type);
50
+ if (!plugin) {
51
+ return null;
52
+ }
53
+ if (typeof plugin.validate === "function") {
54
+ plugin.validate(config, mode, col);
55
+ }
56
+ const result = plugin.create({ mode, col, config });
57
+ if (!result || !result.$input || !result.$input.length) {
58
+ throw new Error("Field type \"" + type + "\" create() must return { $input: jQuery }");
59
+ }
60
+ result.$input.attr("data-type", type);
61
+ return result;
62
+ };
63
+
64
+ CT.mountField = function (opts) {
65
+ const type = opts.config?.type ?? opts.type;
66
+ const plugin = CT.getFieldType(type);
67
+ if (!plugin || typeof plugin.mount !== "function") {
68
+ return;
69
+ }
70
+ plugin.mount(opts);
71
+ };
72
+
73
+ /**
74
+ * Which DOM events on a filter control should submit the hidden filter form.
75
+ * Priority: create() return filterEvents → plugin.filterEvents → default ("change" for select, else "input").
76
+ * @returns {string|null} jQuery event string, or null to skip (bindFilterSubmit only)
77
+ */
78
+ CT.resolveFilterEvents = function ({ plugin, $input, createResult }) {
79
+ let events;
80
+ if (createResult && Object.prototype.hasOwnProperty.call(createResult, "filterEvents")) {
81
+ events = createResult.filterEvents;
82
+ } else if (plugin && Object.prototype.hasOwnProperty.call(plugin, "filterEvents")) {
83
+ events = plugin.filterEvents;
84
+ }
85
+ if (events === false || events === null) {
86
+ return null;
87
+ }
88
+ if (typeof events === "string" && events.trim()) {
89
+ return events.trim();
90
+ }
91
+ return $input.is("select") ? "change" : "input";
92
+ };
93
+
94
+ CT.normalizeFilterDebounceMs = function (value, fallback) {
95
+ if (value === false) {
96
+ return 0;
97
+ }
98
+ if (value === undefined) {
99
+ return fallback;
100
+ }
101
+ if (value === null) {
102
+ return fallback;
103
+ }
104
+ const n = Number(value);
105
+ if (!Number.isFinite(n) || n < 0) {
106
+ return 0;
107
+ }
108
+ return n;
109
+ };
110
+
111
+ /**
112
+ * Resolve debounce delay: column filter.debounceMs → plugin.filterDebounceMs → configure.filterDebounceMs.
113
+ */
114
+ CT.resolveFilterDebounceMs = function ({ filterConfig, plugin }) {
115
+ if (filterConfig && Object.prototype.hasOwnProperty.call(filterConfig, "debounceMs")) {
116
+ // Note: protoColumnConfig.filter.debounceMs defaults to `null`.
117
+ // Treat `null` as "not specified" so we can still fall back to plugin/global.
118
+ if (filterConfig.debounceMs !== null) {
119
+ return CT.normalizeFilterDebounceMs(filterConfig.debounceMs, 0);
120
+ }
121
+ }
122
+ if (plugin && Object.prototype.hasOwnProperty.call(plugin, "filterDebounceMs")) {
123
+ // Same semantics as column debounceMs: null means "unset".
124
+ if (plugin.filterDebounceMs !== null) {
125
+ return CT.normalizeFilterDebounceMs(plugin.filterDebounceMs, 0);
126
+ }
127
+ }
128
+ return CT.normalizeFilterDebounceMs(CT._config.filterDebounceMs, 0);
129
+ };
130
+
131
+ /** Debounce filter submit events when debounceMs > 0. */
132
+ CT.shouldDebounceFilterSubmit = function (events, debounceMs) {
133
+ if (!debounceMs || debounceMs <= 0) {
134
+ return false;
135
+ }
136
+ if (!events) {
137
+ return false;
138
+ }
139
+ return true;
140
+ };
141
+
142
+ CT.bindFieldFilterSubmit = function (type, $input, onSubmit) {
143
+ const plugin = CT.getFieldType(type);
144
+ if (plugin && typeof plugin.bindFilterSubmit === "function") {
145
+ plugin.bindFilterSubmit($input, onSubmit);
146
+ }
147
+ };
148
+
149
+ CT.bindFilterInputEvents = function ({ type, $input, onSubmit, createResult, filterConfig }) {
150
+ const plugin = type ? CT.getFieldType(type) : null;
151
+ const events = CT.resolveFilterEvents({ plugin, $input, createResult });
152
+ const debounceMs = CT.resolveFilterDebounceMs({ filterConfig, plugin });
153
+ const handler = CT.wrapFilterSubmitHandler($input, onSubmit, events, debounceMs);
154
+ if (events) {
155
+ $input.on(events, handler);
156
+ }
157
+ CT.bindFieldFilterSubmit(type, $input, handler);
158
+ };
159
+
160
+ CT.isPluggableFieldType = function (type) {
161
+ return !!CT.getFieldType(type);
162
+ };
163
+
164
+ /**
165
+ * Simple custom type: fixed HTML element + your mount() to init a widget.
166
+ * @param {( $input: JQuery, options: object, ctx: object) => void} mount
167
+ * @param {{ element: string, formInputClass?: string, skipValueAttr?: boolean, filterEvents?, filterDebounceMs?, validate?, bindFilterSubmit? }} opts
168
+ */
169
+ CT.inputType = function (mount, opts) {
170
+ if (typeof mount !== "function") {
171
+ throw new TypeError("inputType(mount, opts): mount must be a function");
172
+ }
173
+ if (!opts || typeof opts.element !== "string") {
174
+ throw new TypeError("inputType(mount, opts): opts.element is required (HTML string)");
175
+ }
176
+ return {
177
+ filterEvents: opts.filterEvents,
178
+ filterDebounceMs: opts.filterDebounceMs,
179
+ validate: opts.validate,
180
+ create({ mode }) {
181
+ const $input = $(opts.element);
182
+ if (opts.formInputClass && (mode === "insert" || mode === "update")) {
183
+ $input.addClass(opts.formInputClass);
184
+ }
185
+ return { $input, skipValueAttr: !!opts.skipValueAttr };
186
+ },
187
+ mount(ctx) {
188
+ mount(ctx.$input, ctx.config?.options ?? ctx.config, ctx);
189
+ },
190
+ bindFilterSubmit: opts.bindFilterSubmit,
191
+ };
192
+ };
193
+
194
+ CT._getCustomInputTypesConfig = function () {
195
+ return CT._config.customInputTypes ?? CT._config.fieldTypes ?? null;
196
+ };
197
+
198
+ CT._syncCustomInputTypes = function () {
199
+ const types = CT._getCustomInputTypesConfig();
200
+ const activeNames =
201
+ types && typeof types === "object"
202
+ ? Object.keys(types).filter((name) => types[name] != null)
203
+ : [];
204
+
205
+ CT._configuredCustomInputTypeNames.forEach((name) => {
206
+ if (!activeNames.includes(name)) {
207
+ CT.unregisterFieldType(name);
208
+ }
209
+ });
210
+
211
+ activeNames.forEach((name) => {
212
+ const spec = types[name];
213
+ if (!spec || typeof spec !== "object" || typeof spec.create !== "function") {
214
+ if (typeof spec === "function") {
215
+ throw new Error(
216
+ "customInputTypes." +
217
+ name +
218
+ " is a function — use a plugin, e.g. KGrid.select2(fn) or KGrid.inputType(fn, { element: \"...\" })"
219
+ );
220
+ }
221
+ throw new TypeError("customInputTypes." + name + " must be a plugin with create()");
222
+ }
223
+ CT.registerFieldType(name, spec, { overwrite: true });
224
+ });
225
+
226
+ CT._configuredCustomInputTypeNames = activeNames.slice();
227
+ };
228
+ })(window.KGrid);