@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.
- package/AGENTS.md +9 -0
- package/LICENSE +34 -0
- package/NOTICE.md +24 -0
- package/README.md +151 -0
- package/dist/kgrid.js +2051 -0
- package/dist/kgrid.min.js +2 -0
- package/docs/ai-guide.md +273 -0
- package/docs/api.md +191 -0
- package/docs/configuration.md +337 -0
- package/docs/field-types.md +92 -0
- package/docs/integration.md +81 -0
- package/docs/table-shell.md +110 -0
- package/integrations/kgrid-plugin-demo-select.js +99 -0
- package/integrations/kgrid-widgets.js +136 -0
- package/integrations/maxxops.example.js +16 -0
- package/package.json +57 -0
- package/src/cells.js +113 -0
- package/src/config.js +59 -0
- package/src/configure.js +136 -0
- package/src/constants.js +113 -0
- package/src/data-body.js +99 -0
- package/src/dom.js +123 -0
- package/src/events.js +100 -0
- package/src/field-types-builtins.js +25 -0
- package/src/field-types.js +228 -0
- package/src/filters.js +394 -0
- package/src/init.js +199 -0
- package/src/insert-row.js +172 -0
- package/src/interaction.js +43 -0
- package/src/labels.js +49 -0
- package/src/table-shell.js +152 -0
- package/styles/table.css +201 -0
package/dist/kgrid.js
ADDED
|
@@ -0,0 +1,2051 @@
|
|
|
1
|
+
/*! @logimaxx/kgrid | (c) Logimaxx System SRL — proprietary | https://logimaxx.ro | built 2026-07-30T15:53:29.364Z */
|
|
2
|
+
|
|
3
|
+
/* --- configure.js --- */
|
|
4
|
+
/**
|
|
5
|
+
* Host-app integration: call KGrid.configure({ ... }) before mounting tables.
|
|
6
|
+
*/
|
|
7
|
+
(function (CT) {
|
|
8
|
+
const defaultConfig = {
|
|
9
|
+
log: function () {},
|
|
10
|
+
onError: function (err) {
|
|
11
|
+
console.error(err);
|
|
12
|
+
},
|
|
13
|
+
confirm: function (message, onConfirm, onCancel) {
|
|
14
|
+
if (typeof window !== "undefined" && window.confirm(message)) {
|
|
15
|
+
onConfirm();
|
|
16
|
+
} else if (onCancel) {
|
|
17
|
+
onCancel();
|
|
18
|
+
}
|
|
19
|
+
},
|
|
20
|
+
/** @type {((context: object, onConfirm: Function, onCancel?: Function) => void)|null} */
|
|
21
|
+
deleteConfirm: null,
|
|
22
|
+
serializeForm: function (form, columns) {
|
|
23
|
+
const fd = new FormData(form);
|
|
24
|
+
const out = {};
|
|
25
|
+
for (const [key, value] of fd.entries()) {
|
|
26
|
+
if (Object.prototype.hasOwnProperty.call(out, key)) {
|
|
27
|
+
if (!Array.isArray(out[key])) {
|
|
28
|
+
out[key] = [out[key]];
|
|
29
|
+
}
|
|
30
|
+
out[key].push(value);
|
|
31
|
+
} else {
|
|
32
|
+
out[key] = value;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
return out;
|
|
36
|
+
},
|
|
37
|
+
/** @type {Record<string, Function|object>|null} name → wrapper fn or field type plugin */
|
|
38
|
+
customInputTypes: null,
|
|
39
|
+
/** @deprecated use customInputTypes */
|
|
40
|
+
fieldTypes: null,
|
|
41
|
+
/** @type {typeof globalThis.KViews|null} Set via configure or use window.KViews */
|
|
42
|
+
kviews: null,
|
|
43
|
+
/** Default delay (ms) before filter submit; 0 = immediate. Applies to the resolved filter-submit event. */
|
|
44
|
+
filterDebounceMs: 300,
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
CT._config = Object.assign({}, defaultConfig);
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* @param {Object} overrides
|
|
51
|
+
* @param {Function} [overrides.log]
|
|
52
|
+
* @param {Function} [overrides.onError]
|
|
53
|
+
* @param {Function} [overrides.confirm] (message, onConfirm, onCancel?)
|
|
54
|
+
* @param {Function} [overrides.deleteConfirm] (context, onConfirm, onCancel?) — row delete UX
|
|
55
|
+
* @param {Function} [overrides.serializeForm] (form, columns?)
|
|
56
|
+
* @param {Object} [overrides.customInputTypes] map of name → wrapper fn or field type plugin
|
|
57
|
+
* @param {Object} [overrides.fieldTypes] deprecated alias of customInputTypes
|
|
58
|
+
* @param {Object} [overrides.kviews] KViews module (createCollectionInstance)
|
|
59
|
+
* @param {number} [overrides.filterDebounceMs] default filter submit debounce (ms); 0 = off
|
|
60
|
+
*/
|
|
61
|
+
CT.configure = function (overrides) {
|
|
62
|
+
if (overrides && typeof overrides === "object") {
|
|
63
|
+
Object.assign(CT._config, overrides);
|
|
64
|
+
}
|
|
65
|
+
if (typeof CT._syncCustomInputTypes === "function") {
|
|
66
|
+
CT._syncCustomInputTypes();
|
|
67
|
+
}
|
|
68
|
+
return CT;
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
CT.log = function () {
|
|
72
|
+
CT._config.log.apply(null, arguments);
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
CT.onError = function (err) {
|
|
76
|
+
return CT._config.onError(err);
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
CT.confirm = function (message, onConfirm, onCancel) {
|
|
80
|
+
return CT._config.confirm(message, onConfirm, onCancel);
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
CT.DEFAULT_DELETE_CONFIRM_MESSAGE = "Delete this record?";
|
|
84
|
+
|
|
85
|
+
CT._defaultDeleteConfirm = function (context, onConfirm, onCancel) {
|
|
86
|
+
CT.confirm(CT.DEFAULT_DELETE_CONFIRM_MESSAGE, onConfirm, onCancel);
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Ask the host to confirm row delete. Per-table `options.deleteConfirm` wins over configure.
|
|
91
|
+
* @param {{ item: object, view: object, options?: object }} context
|
|
92
|
+
* @param {() => void} onConfirm run delete (e.g. item.delete())
|
|
93
|
+
* @param {() => void} [onCancel]
|
|
94
|
+
*/
|
|
95
|
+
CT.runDeleteConfirm = function (context, onConfirm, onCancel) {
|
|
96
|
+
const perTable =
|
|
97
|
+
context.options && typeof context.options.deleteConfirm === "function"
|
|
98
|
+
? context.options.deleteConfirm
|
|
99
|
+
: null;
|
|
100
|
+
const configured =
|
|
101
|
+
typeof CT._config.deleteConfirm === "function"
|
|
102
|
+
? CT._config.deleteConfirm
|
|
103
|
+
: null;
|
|
104
|
+
const fn = perTable || configured || CT._defaultDeleteConfirm;
|
|
105
|
+
return fn(context, onConfirm, onCancel);
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
CT.serializeForm = function (form, columns) {
|
|
109
|
+
return CT._config.serializeForm(form, columns);
|
|
110
|
+
};
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* Resolve KViews from init override, configure({ kviews }), or window.KViews.
|
|
114
|
+
* @param {Object} [override] per-init kviews (opts.kviews)
|
|
115
|
+
* @returns {Object|null}
|
|
116
|
+
*/
|
|
117
|
+
CT.getKViews = function (override) {
|
|
118
|
+
const candidates = [override, CT._config.kviews];
|
|
119
|
+
for (let i = 0; i < candidates.length; i++) {
|
|
120
|
+
const kv = candidates[i];
|
|
121
|
+
if (kv && typeof kv.createCollectionInstance === "function") {
|
|
122
|
+
return kv;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
const root = typeof window !== "undefined"
|
|
126
|
+
? window
|
|
127
|
+
: (typeof globalThis !== "undefined" ? globalThis : {});
|
|
128
|
+
const globalKv = root.KViews;
|
|
129
|
+
if (globalKv && typeof globalKv.createCollectionInstance === "function") {
|
|
130
|
+
return globalKv;
|
|
131
|
+
}
|
|
132
|
+
return null;
|
|
133
|
+
};
|
|
134
|
+
|
|
135
|
+
CT.KVIEWS_MISSING_MSG =
|
|
136
|
+
"KGrid requires KViews: install peer @logimaxx/kviews, load it before kgrid.js " +
|
|
137
|
+
"(window.KViews), or call KGrid.configure({ kviews: KViews }). " +
|
|
138
|
+
"You can also pass kviews in table options: KGrid.init(host, { kviews, ... }).";
|
|
139
|
+
})(window.KGrid = window.KGrid || {});
|
|
140
|
+
|
|
141
|
+
|
|
142
|
+
/* --- constants.js --- */
|
|
143
|
+
/**
|
|
144
|
+
* Default table/column config prototypes and allowed input types.
|
|
145
|
+
*/
|
|
146
|
+
(function (CT) {
|
|
147
|
+
CT.protoOptions = {
|
|
148
|
+
"url": null,
|
|
149
|
+
"type": null,
|
|
150
|
+
filter: null,
|
|
151
|
+
"features": {
|
|
152
|
+
"filtering": false,
|
|
153
|
+
"sorting": false,
|
|
154
|
+
"paging": false,
|
|
155
|
+
"create": false,
|
|
156
|
+
"update": false,
|
|
157
|
+
"delete": false
|
|
158
|
+
},
|
|
159
|
+
"defaultInteraction": "view",
|
|
160
|
+
"insertFormRow": {
|
|
161
|
+
"position": "top"
|
|
162
|
+
},
|
|
163
|
+
"tableAttrs": {
|
|
164
|
+
"class": ""
|
|
165
|
+
},
|
|
166
|
+
"labelsRowAttrs": {
|
|
167
|
+
|
|
168
|
+
},
|
|
169
|
+
"dataRowAttrs": {
|
|
170
|
+
|
|
171
|
+
},
|
|
172
|
+
"filtersRowAttrs": null,
|
|
173
|
+
/** Page size choices in paging footer <select> */
|
|
174
|
+
pagingPageSizes: [10, 25, 50, 75],
|
|
175
|
+
pagingDefaultSize: 10,
|
|
176
|
+
/** Initial text in .no-data-tbody (overridden by noDataTemplate after init) */
|
|
177
|
+
emptyRowMessage: null,
|
|
178
|
+
pagingFooterLabel: "records per page. Total",
|
|
179
|
+
};
|
|
180
|
+
|
|
181
|
+
CT.protoColumnConfig = {
|
|
182
|
+
label: null,
|
|
183
|
+
name: null,
|
|
184
|
+
hidden: false,
|
|
185
|
+
/** CSS class(es) on header/filter/data/insert cells (alias: columnClass) */
|
|
186
|
+
class: null,
|
|
187
|
+
columnClass: null,
|
|
188
|
+
attrs: {},
|
|
189
|
+
features: {
|
|
190
|
+
create: false,
|
|
191
|
+
update: false,
|
|
192
|
+
filter: false,
|
|
193
|
+
sort: false
|
|
194
|
+
},
|
|
195
|
+
display: {
|
|
196
|
+
template: null,
|
|
197
|
+
events: []
|
|
198
|
+
},
|
|
199
|
+
/**
|
|
200
|
+
* Shared insert+update defaults. Explicit insert/update win on conflict.
|
|
201
|
+
* Example: input: { type: "number", required: true }
|
|
202
|
+
*/
|
|
203
|
+
input: null,
|
|
204
|
+
insert: {
|
|
205
|
+
type: "text",
|
|
206
|
+
default: null,
|
|
207
|
+
value: null,
|
|
208
|
+
placeholder: null,
|
|
209
|
+
disabled: false,
|
|
210
|
+
required: false,
|
|
211
|
+
options: null,
|
|
212
|
+
events: []
|
|
213
|
+
},
|
|
214
|
+
update: {
|
|
215
|
+
type: "text",
|
|
216
|
+
default: null,
|
|
217
|
+
value: null,
|
|
218
|
+
placeholder: null,
|
|
219
|
+
disabled: false,
|
|
220
|
+
required: false,
|
|
221
|
+
options: null,
|
|
222
|
+
template: null,
|
|
223
|
+
events: []
|
|
224
|
+
},
|
|
225
|
+
filter: {
|
|
226
|
+
type: "text",
|
|
227
|
+
operator: "~=~",
|
|
228
|
+
default: null,
|
|
229
|
+
placeholder: "",
|
|
230
|
+
options: null,
|
|
231
|
+
/** Keep default across filter form reset / re-submit */
|
|
232
|
+
persist: false,
|
|
233
|
+
/** Override KGrid.configure({ filterDebounceMs }); 0 = submit immediately */
|
|
234
|
+
debounceMs: null,
|
|
235
|
+
}
|
|
236
|
+
};
|
|
237
|
+
|
|
238
|
+
CT.VALID_NATIVE_INPUT_TYPES = ["displayonly","text","textarea","number","date","datetime","time","checkbox","radio","file","password","email","url","search","tel","select","hidden"];
|
|
239
|
+
/** @deprecated use VALID_NATIVE_INPUT_TYPES or isValidInputType() */
|
|
240
|
+
CT.VALID_INPUT_TYPES = CT.VALID_NATIVE_INPUT_TYPES.slice();
|
|
241
|
+
CT.VALID_NATIVE_FILTER_TYPES = CT.VALID_NATIVE_INPUT_TYPES.filter((t) => t !== "displayonly");
|
|
242
|
+
/** @deprecated use isValidFilterType() */
|
|
243
|
+
CT.VALID_FILTER_TYPES = CT.VALID_NATIVE_FILTER_TYPES.concat(["multi_select","date_range"]);
|
|
244
|
+
|
|
245
|
+
CT.isValidInputType = function (type) {
|
|
246
|
+
return CT.VALID_NATIVE_INPUT_TYPES.includes(type) || CT.isPluggableFieldType(type);
|
|
247
|
+
};
|
|
248
|
+
|
|
249
|
+
CT.isValidFilterType = function (type) {
|
|
250
|
+
if (type === "displayonly") {
|
|
251
|
+
return false;
|
|
252
|
+
}
|
|
253
|
+
return CT.VALID_NATIVE_FILTER_TYPES.includes(type) || CT.isPluggableFieldType(type);
|
|
254
|
+
};
|
|
255
|
+
})(window.KGrid = window.KGrid || {});
|
|
256
|
+
|
|
257
|
+
|
|
258
|
+
/* --- config.js --- */
|
|
259
|
+
(function (CT) {
|
|
260
|
+
/** Plain object (not array / null) — realm-safe vs `constructor === Object`. */
|
|
261
|
+
CT.isPlainObject = function (value) {
|
|
262
|
+
return value != null && typeof value === "object" && !Array.isArray(value);
|
|
263
|
+
};
|
|
264
|
+
|
|
265
|
+
CT.setDefaultValues = function (proto, col) {
|
|
266
|
+
const newCol = { ...(proto || {}) };
|
|
267
|
+
Object.keys(col || {}).forEach((key) => {
|
|
268
|
+
if (CT.isPlainObject(col[key]) && CT.isPlainObject(newCol[key])) {
|
|
269
|
+
newCol[key] = CT.setDefaultValues(newCol[key], col[key]);
|
|
270
|
+
return;
|
|
271
|
+
}
|
|
272
|
+
if (CT.isPlainObject(col[key]) && newCol[key] == null) {
|
|
273
|
+
newCol[key] = CT.setDefaultValues({}, col[key]);
|
|
274
|
+
return;
|
|
275
|
+
}
|
|
276
|
+
newCol[key] = col[key];
|
|
277
|
+
});
|
|
278
|
+
return newCol;
|
|
279
|
+
};
|
|
280
|
+
|
|
281
|
+
/**
|
|
282
|
+
* Normalize a column: expand `input` shorthand, merge proto, ensure events[].
|
|
283
|
+
* @param {Object} col raw column from table options
|
|
284
|
+
* @returns {Object}
|
|
285
|
+
*/
|
|
286
|
+
CT.normalizeColumnConfig = function (col) {
|
|
287
|
+
let partial = col && typeof col === "object" ? { ...col } : {};
|
|
288
|
+
if (CT.isPlainObject(partial.input)) {
|
|
289
|
+
const shared = { ...partial.input };
|
|
290
|
+
delete partial.input;
|
|
291
|
+
partial.insert = CT.setDefaultValues(shared, partial.insert || {});
|
|
292
|
+
partial.update = CT.setDefaultValues(shared, partial.update || {});
|
|
293
|
+
}
|
|
294
|
+
const merged = CT.setDefaultValues(CT.protoColumnConfig, partial);
|
|
295
|
+
if (merged.class == null && merged.columnClass) {
|
|
296
|
+
merged.class = merged.columnClass;
|
|
297
|
+
}
|
|
298
|
+
if (
|
|
299
|
+
merged.update &&
|
|
300
|
+
merged.update.type === "displayonly" &&
|
|
301
|
+
!merged.update.template &&
|
|
302
|
+
merged.display &&
|
|
303
|
+
merged.display.template
|
|
304
|
+
) {
|
|
305
|
+
merged.update.template = merged.display.template;
|
|
306
|
+
}
|
|
307
|
+
["display", "insert", "update"].forEach((mode) => {
|
|
308
|
+
if (!merged[mode] || typeof merged[mode] !== "object") {
|
|
309
|
+
return;
|
|
310
|
+
}
|
|
311
|
+
if (!Array.isArray(merged[mode].events)) {
|
|
312
|
+
merged[mode].events = [];
|
|
313
|
+
}
|
|
314
|
+
});
|
|
315
|
+
return merged;
|
|
316
|
+
};
|
|
317
|
+
})(window.KGrid);
|
|
318
|
+
|
|
319
|
+
|
|
320
|
+
/* --- dom.js --- */
|
|
321
|
+
(function (CT) {
|
|
322
|
+
/**
|
|
323
|
+
* Normalize init host: native DOM element or jQuery collection.
|
|
324
|
+
* @param {Element|JQuery} host
|
|
325
|
+
* @returns {JQuery}
|
|
326
|
+
*/
|
|
327
|
+
CT.resolveHostElement = function (host) {
|
|
328
|
+
if (host == null) {
|
|
329
|
+
throw new TypeError("KGrid.init(host, opts): host is required");
|
|
330
|
+
}
|
|
331
|
+
if (typeof host === "object" && host.jquery) {
|
|
332
|
+
if (!host.length) {
|
|
333
|
+
throw new Error("KGrid.init(host, opts): empty jQuery selection");
|
|
334
|
+
}
|
|
335
|
+
return host;
|
|
336
|
+
}
|
|
337
|
+
if (
|
|
338
|
+
typeof host === "object" &&
|
|
339
|
+
host.nodeType === 1 &&
|
|
340
|
+
typeof host.nodeName === "string"
|
|
341
|
+
) {
|
|
342
|
+
return $(host);
|
|
343
|
+
}
|
|
344
|
+
throw new TypeError(
|
|
345
|
+
"KGrid.init(host, opts): host must be a DOM Element or jQuery object"
|
|
346
|
+
);
|
|
347
|
+
};
|
|
348
|
+
|
|
349
|
+
CT.uuid = function () {
|
|
350
|
+
return Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15);
|
|
351
|
+
};
|
|
352
|
+
|
|
353
|
+
/**
|
|
354
|
+
* Whether the table needs a trailing row-actions column (header, filters, data rows, colspan).
|
|
355
|
+
* @param {Object} options table options with features
|
|
356
|
+
* @returns {boolean}
|
|
357
|
+
*/
|
|
358
|
+
CT.hasActionColumn = function (options) {
|
|
359
|
+
const f = options && options.features;
|
|
360
|
+
if (!f) {
|
|
361
|
+
return false;
|
|
362
|
+
}
|
|
363
|
+
return !!(f.delete || f.update || f.create);
|
|
364
|
+
};
|
|
365
|
+
|
|
366
|
+
/**
|
|
367
|
+
* Sync <colgroup> so row-actions width can collapse in view (table-layout: fixed).
|
|
368
|
+
* @param {JQuery} $table
|
|
369
|
+
* @param {number} dataColumnCount visible data columns (no row-actions)
|
|
370
|
+
* @param {boolean} hasActions
|
|
371
|
+
*/
|
|
372
|
+
CT.syncActionColumnColgroup = function ($table, dataColumnCount, hasActions) {
|
|
373
|
+
let $colgroup = $table.children("colgroup.kgrid-colgroup");
|
|
374
|
+
if (!$colgroup.length) {
|
|
375
|
+
$colgroup = $("<colgroup>").addClass("kgrid-colgroup").prependTo($table);
|
|
376
|
+
}
|
|
377
|
+
$colgroup.empty();
|
|
378
|
+
for (let i = 0; i < dataColumnCount; i++) {
|
|
379
|
+
$colgroup.append($("<col>"));
|
|
380
|
+
}
|
|
381
|
+
if (hasActions) {
|
|
382
|
+
$colgroup.append($("<col>").addClass("kgrid-row-actions-col"));
|
|
383
|
+
}
|
|
384
|
+
};
|
|
385
|
+
|
|
386
|
+
/**
|
|
387
|
+
* Place a hidden <form> anchor on a table row template.
|
|
388
|
+
* Row controls use form="id" (cannot wrap <tr> in one <form>).
|
|
389
|
+
*/
|
|
390
|
+
CT.anchorRowForm = function ($form, $row) {
|
|
391
|
+
if (!$form || !$form.length || !$row || !$row.length) {
|
|
392
|
+
return $form;
|
|
393
|
+
}
|
|
394
|
+
$form.attr("hidden", "hidden").attr("aria-hidden", "true");
|
|
395
|
+
const $firstTd = $row.children("td").first();
|
|
396
|
+
if ($firstTd.length) {
|
|
397
|
+
$form.prependTo($firstTd);
|
|
398
|
+
}
|
|
399
|
+
return $form;
|
|
400
|
+
};
|
|
401
|
+
|
|
402
|
+
/** Field in filter row (associated via form="" on control, not nested inside <form>). */
|
|
403
|
+
CT.filterFormField = function (formEl, name) {
|
|
404
|
+
if (!formEl || !name) {
|
|
405
|
+
return $();
|
|
406
|
+
}
|
|
407
|
+
const el = formEl.elements && formEl.elements.namedItem(name);
|
|
408
|
+
if (el) {
|
|
409
|
+
return $(el);
|
|
410
|
+
}
|
|
411
|
+
if (formEl.id) {
|
|
412
|
+
const associated = document.querySelector(`[form="${formEl.id}"][name="${name}"]`);
|
|
413
|
+
if (associated) {
|
|
414
|
+
return $(associated);
|
|
415
|
+
}
|
|
416
|
+
}
|
|
417
|
+
return $();
|
|
418
|
+
};
|
|
419
|
+
|
|
420
|
+
/** Resolved CSS class for a column (`class` or alias `columnClass`). */
|
|
421
|
+
CT.columnClassName = function (col) {
|
|
422
|
+
if (!col) {
|
|
423
|
+
return null;
|
|
424
|
+
}
|
|
425
|
+
const cls = col.class != null && col.class !== "" ? col.class : col.columnClass;
|
|
426
|
+
return cls != null && cls !== "" ? String(cls) : null;
|
|
427
|
+
};
|
|
428
|
+
|
|
429
|
+
/** Add column class + data-name to a header/cell element. */
|
|
430
|
+
CT.applyColumnCellMeta = function ($el, col) {
|
|
431
|
+
if (!$el || !$el.length || !col) {
|
|
432
|
+
return $el;
|
|
433
|
+
}
|
|
434
|
+
if (col.name) {
|
|
435
|
+
$el.attr("data-name", col.name);
|
|
436
|
+
}
|
|
437
|
+
const cls = CT.columnClassName(col);
|
|
438
|
+
if (cls) {
|
|
439
|
+
$el.addClass(cls);
|
|
440
|
+
}
|
|
441
|
+
return $el;
|
|
442
|
+
};
|
|
443
|
+
})(window.KGrid);
|
|
444
|
+
|
|
445
|
+
|
|
446
|
+
/* --- table-shell.js --- */
|
|
447
|
+
/**
|
|
448
|
+
* Build the KGrid table DOM skeleton (self-contained; no external HTML file).
|
|
449
|
+
*
|
|
450
|
+
* Edit TABLE_SHELL_TEMPLATE below to change markup. Dynamic slots:
|
|
451
|
+
* {{EMPTY_ROW_MESSAGE}} — text in .no-data-tbody
|
|
452
|
+
* {{PAGES_DATA_PAGESIZE}} — value for .pages data-pagesize
|
|
453
|
+
* {{PAGE_SIZE_OPTIONS}} — <option> elements for .pagesize
|
|
454
|
+
* {{PAGING_FOOTER_LABEL}} — text after page-size select
|
|
455
|
+
*/
|
|
456
|
+
(function (CT) {
|
|
457
|
+
CT.DEFAULT_EMPTY_ROW_MESSAGE = "No records match your search.";
|
|
458
|
+
|
|
459
|
+
/**
|
|
460
|
+
* Static table skeleton (one sortable header cell template; labels.js clones it per column).
|
|
461
|
+
* @type {string}
|
|
462
|
+
*/
|
|
463
|
+
CT.TABLE_SHELL_TEMPLATE = `
|
|
464
|
+
<table class="custom-table" style="table-layout: fixed;">
|
|
465
|
+
<thead class="thead-labels">
|
|
466
|
+
<tr>
|
|
467
|
+
<th class="align-top">
|
|
468
|
+
<a class="sort" data-sortfld="" style="cursor: pointer;">
|
|
469
|
+
<span class="column-label"></span>
|
|
470
|
+
<i class="fas fa-sort-up sort-up" style="display: none"></i>
|
|
471
|
+
<i class="fas fa-sort-down sort-down" style="display: none"></i>
|
|
472
|
+
<i class="fas fa-sort sort-default"></i>
|
|
473
|
+
</a>
|
|
474
|
+
</th>
|
|
475
|
+
</tr>
|
|
476
|
+
</thead>
|
|
477
|
+
<thead class="thead-filters"></thead>
|
|
478
|
+
<tbody class="before-main-tbody"></tbody>
|
|
479
|
+
<tbody class="main-tbody"></tbody>
|
|
480
|
+
<tbody class="after-main-tbody"></tbody>
|
|
481
|
+
<tbody class="no-data-tbody">
|
|
482
|
+
<tr><td>{{EMPTY_ROW_MESSAGE}}</td></tr>
|
|
483
|
+
</tbody>
|
|
484
|
+
<tfoot class="paging-footer">
|
|
485
|
+
<tr>
|
|
486
|
+
<td>
|
|
487
|
+
<div class="btn-group pages" data-pagesize="{{PAGES_DATA_PAGESIZE}}">
|
|
488
|
+
<button type="button" name="first" class="btn btn-sm btn-outline-secondary"><<</button>
|
|
489
|
+
<button type="button" name="prev" class="btn btn-sm btn-outline-secondary"><</button>
|
|
490
|
+
<button type="button" name="page" class="btn btn-sm btn-outline-secondary">1</button>
|
|
491
|
+
<button type="button" name="next" class="btn btn-sm btn-outline-secondary">></button>
|
|
492
|
+
<button type="button" name="last" class="btn btn-sm btn-outline-secondary">>></button>
|
|
493
|
+
</div>
|
|
494
|
+
<select class="pagesize">
|
|
495
|
+
{{PAGE_SIZE_OPTIONS}}
|
|
496
|
+
</select>
|
|
497
|
+
{{PAGING_FOOTER_LABEL}} <span class="totalrecscount"></span>
|
|
498
|
+
</td>
|
|
499
|
+
</tr>
|
|
500
|
+
</tfoot>
|
|
501
|
+
</table>`.trim();
|
|
502
|
+
|
|
503
|
+
CT._escapeTableShellText = function (text) {
|
|
504
|
+
return String(text)
|
|
505
|
+
.replace(/&/g, "&")
|
|
506
|
+
.replace(/</g, "<")
|
|
507
|
+
.replace(/>/g, ">")
|
|
508
|
+
.replace(/"/g, """);
|
|
509
|
+
};
|
|
510
|
+
|
|
511
|
+
CT._renderPageSizeOptions = function (pageSizes, defaultSize) {
|
|
512
|
+
return pageSizes
|
|
513
|
+
.map((size) => {
|
|
514
|
+
const value = String(size);
|
|
515
|
+
const selected = value === String(defaultSize) ? " selected" : "";
|
|
516
|
+
return ` <option value="${value}"${selected}>${value}</option>`;
|
|
517
|
+
})
|
|
518
|
+
.join("\n");
|
|
519
|
+
};
|
|
520
|
+
|
|
521
|
+
/**
|
|
522
|
+
* Fill template placeholders from table options.
|
|
523
|
+
* @param {Object} options
|
|
524
|
+
* @param {string} [template]
|
|
525
|
+
* @returns {string}
|
|
526
|
+
*/
|
|
527
|
+
CT.renderTableShellHtml = function (options, template) {
|
|
528
|
+
const pageSizes = Array.isArray(options.pagingPageSizes)
|
|
529
|
+
? options.pagingPageSizes
|
|
530
|
+
: [10, 25, 50, 75];
|
|
531
|
+
const defaultSize =
|
|
532
|
+
options.pagingDefaultSize != null ? options.pagingDefaultSize : pageSizes[0];
|
|
533
|
+
const emptyMsg = CT._escapeTableShellText(
|
|
534
|
+
options.emptyRowMessage || CT.DEFAULT_EMPTY_ROW_MESSAGE
|
|
535
|
+
);
|
|
536
|
+
const pagingLabel = CT._escapeTableShellText(
|
|
537
|
+
options.pagingFooterLabel || "records per page. Total"
|
|
538
|
+
);
|
|
539
|
+
|
|
540
|
+
return (template || CT.TABLE_SHELL_TEMPLATE)
|
|
541
|
+
.replace(/\{\{EMPTY_ROW_MESSAGE\}\}/g, emptyMsg)
|
|
542
|
+
.replace(/\{\{PAGES_DATA_PAGESIZE\}\}/g, String(defaultSize))
|
|
543
|
+
.replace(
|
|
544
|
+
/\{\{PAGE_SIZE_OPTIONS\}\}/g,
|
|
545
|
+
CT._renderPageSizeOptions(pageSizes, defaultSize)
|
|
546
|
+
)
|
|
547
|
+
.replace(/\{\{PAGING_FOOTER_LABEL\}\}/g, pagingLabel);
|
|
548
|
+
};
|
|
549
|
+
|
|
550
|
+
/**
|
|
551
|
+
* Create the table element (not yet attached to document).
|
|
552
|
+
* @param {Object} options merged table options
|
|
553
|
+
* @returns {JQuery} table.custom-table
|
|
554
|
+
*/
|
|
555
|
+
CT.createTableShell = function (options) {
|
|
556
|
+
const html = CT.renderTableShellHtml(options);
|
|
557
|
+
const nodes = $.parseHTML(html, document, true);
|
|
558
|
+
const $table = $(nodes).filter("table").first();
|
|
559
|
+
if (!$table.length) {
|
|
560
|
+
throw new Error("KGrid TABLE_SHELL_TEMPLATE must contain a root <table> element");
|
|
561
|
+
}
|
|
562
|
+
|
|
563
|
+
if (options.tableAttrs && CT.isPlainObject(options.tableAttrs)) {
|
|
564
|
+
Object.keys(options.tableAttrs).forEach((att) => {
|
|
565
|
+
if (att !== "class") {
|
|
566
|
+
$table.attr(att, options.tableAttrs[att]);
|
|
567
|
+
}
|
|
568
|
+
});
|
|
569
|
+
if (typeof options.tableAttrs.class === "string" && options.tableAttrs.class) {
|
|
570
|
+
$table.addClass(options.tableAttrs.class);
|
|
571
|
+
}
|
|
572
|
+
}
|
|
573
|
+
|
|
574
|
+
return $table;
|
|
575
|
+
};
|
|
576
|
+
|
|
577
|
+
/**
|
|
578
|
+
* Ensure host contains a KGrid table; build one if missing.
|
|
579
|
+
* @param {JQuery} $host
|
|
580
|
+
* @param {Object} options
|
|
581
|
+
* @returns {JQuery} table element
|
|
582
|
+
*/
|
|
583
|
+
CT.mountTableShell = function ($host, options) {
|
|
584
|
+
const $existing = $host.find("table.custom-table, table").first();
|
|
585
|
+
if ($existing.length) {
|
|
586
|
+
return $existing;
|
|
587
|
+
}
|
|
588
|
+
|
|
589
|
+
const $shell = CT.getTableInteractionHost($host);
|
|
590
|
+
if (!$shell.hasClass("custom-table-shell")) {
|
|
591
|
+
$shell.addClass("custom-table-shell");
|
|
592
|
+
}
|
|
593
|
+
|
|
594
|
+
const $table = CT.createTableShell(options);
|
|
595
|
+
$shell.append($table);
|
|
596
|
+
return $table;
|
|
597
|
+
};
|
|
598
|
+
})(window.KGrid);
|
|
599
|
+
|
|
600
|
+
|
|
601
|
+
/* --- field-types.js --- */
|
|
602
|
+
/**
|
|
603
|
+
* Pluggable field types for filter / insert / update contexts.
|
|
604
|
+
*
|
|
605
|
+
* KGrid.configure({ customInputTypes: { myType: { create, mount?, … } } })
|
|
606
|
+
* KGrid.registerFieldType(name, plugin)
|
|
607
|
+
* KGrid.inputType(mount, { element }) — simple widget helper
|
|
608
|
+
*
|
|
609
|
+
* Plugin shape:
|
|
610
|
+
* create({ mode, col, config }) — required; returns { $input, skipValueAttr?, filterEvents? }
|
|
611
|
+
* filterEvents? — jQuery event names for filter submit (e.g. "change", "input"); false = none (use bindFilterSubmit)
|
|
612
|
+
* filterDebounceMs? — override configure/filter.debounceMs for this type
|
|
613
|
+
* mount?, validate?, bindFilterSubmit? — extra widget-specific filter events
|
|
614
|
+
*/
|
|
615
|
+
(function (CT) {
|
|
616
|
+
CT._fieldTypes = Object.create(null);
|
|
617
|
+
CT._configuredCustomInputTypeNames = [];
|
|
618
|
+
|
|
619
|
+
CT.registerFieldType = function (name, plugin, options) {
|
|
620
|
+
if (!name || typeof name !== "string") {
|
|
621
|
+
throw new TypeError("registerFieldType(name, plugin): name must be a string");
|
|
622
|
+
}
|
|
623
|
+
if (!plugin || typeof plugin.create !== "function") {
|
|
624
|
+
throw new TypeError("registerFieldType(" + name + "): plugin.create is required");
|
|
625
|
+
}
|
|
626
|
+
if (CT._fieldTypes[name] && !(options && options.overwrite)) {
|
|
627
|
+
throw new Error("Field type already registered: " + name);
|
|
628
|
+
}
|
|
629
|
+
CT._fieldTypes[name] = Object.assign({ name }, plugin);
|
|
630
|
+
return CT;
|
|
631
|
+
};
|
|
632
|
+
|
|
633
|
+
CT.unregisterFieldType = function (name) {
|
|
634
|
+
if (name && CT._fieldTypes[name]) {
|
|
635
|
+
delete CT._fieldTypes[name];
|
|
636
|
+
}
|
|
637
|
+
return CT;
|
|
638
|
+
};
|
|
639
|
+
|
|
640
|
+
CT.getFieldType = function (name) {
|
|
641
|
+
return name ? CT._fieldTypes[name] || null : null;
|
|
642
|
+
};
|
|
643
|
+
|
|
644
|
+
CT.listFieldTypes = function () {
|
|
645
|
+
return Object.keys(CT._fieldTypes);
|
|
646
|
+
};
|
|
647
|
+
|
|
648
|
+
CT.createFieldInput = function ({ mode, col, config }) {
|
|
649
|
+
const type = config.type ?? "text";
|
|
650
|
+
const plugin = CT.getFieldType(type);
|
|
651
|
+
if (!plugin) {
|
|
652
|
+
return null;
|
|
653
|
+
}
|
|
654
|
+
if (typeof plugin.validate === "function") {
|
|
655
|
+
plugin.validate(config, mode, col);
|
|
656
|
+
}
|
|
657
|
+
const result = plugin.create({ mode, col, config });
|
|
658
|
+
if (!result || !result.$input || !result.$input.length) {
|
|
659
|
+
throw new Error("Field type \"" + type + "\" create() must return { $input: jQuery }");
|
|
660
|
+
}
|
|
661
|
+
result.$input.attr("data-type", type);
|
|
662
|
+
return result;
|
|
663
|
+
};
|
|
664
|
+
|
|
665
|
+
CT.mountField = function (opts) {
|
|
666
|
+
const type = opts.config?.type ?? opts.type;
|
|
667
|
+
const plugin = CT.getFieldType(type);
|
|
668
|
+
if (!plugin || typeof plugin.mount !== "function") {
|
|
669
|
+
return;
|
|
670
|
+
}
|
|
671
|
+
plugin.mount(opts);
|
|
672
|
+
};
|
|
673
|
+
|
|
674
|
+
/**
|
|
675
|
+
* Which DOM events on a filter control should submit the hidden filter form.
|
|
676
|
+
* Priority: create() return filterEvents → plugin.filterEvents → default ("change" for select, else "input").
|
|
677
|
+
* @returns {string|null} jQuery event string, or null to skip (bindFilterSubmit only)
|
|
678
|
+
*/
|
|
679
|
+
CT.resolveFilterEvents = function ({ plugin, $input, createResult }) {
|
|
680
|
+
let events;
|
|
681
|
+
if (createResult && Object.prototype.hasOwnProperty.call(createResult, "filterEvents")) {
|
|
682
|
+
events = createResult.filterEvents;
|
|
683
|
+
} else if (plugin && Object.prototype.hasOwnProperty.call(plugin, "filterEvents")) {
|
|
684
|
+
events = plugin.filterEvents;
|
|
685
|
+
}
|
|
686
|
+
if (events === false || events === null) {
|
|
687
|
+
return null;
|
|
688
|
+
}
|
|
689
|
+
if (typeof events === "string" && events.trim()) {
|
|
690
|
+
return events.trim();
|
|
691
|
+
}
|
|
692
|
+
return $input.is("select") ? "change" : "input";
|
|
693
|
+
};
|
|
694
|
+
|
|
695
|
+
CT.normalizeFilterDebounceMs = function (value, fallback) {
|
|
696
|
+
if (value === false) {
|
|
697
|
+
return 0;
|
|
698
|
+
}
|
|
699
|
+
if (value === undefined) {
|
|
700
|
+
return fallback;
|
|
701
|
+
}
|
|
702
|
+
if (value === null) {
|
|
703
|
+
return fallback;
|
|
704
|
+
}
|
|
705
|
+
const n = Number(value);
|
|
706
|
+
if (!Number.isFinite(n) || n < 0) {
|
|
707
|
+
return 0;
|
|
708
|
+
}
|
|
709
|
+
return n;
|
|
710
|
+
};
|
|
711
|
+
|
|
712
|
+
/**
|
|
713
|
+
* Resolve debounce delay: column filter.debounceMs → plugin.filterDebounceMs → configure.filterDebounceMs.
|
|
714
|
+
*/
|
|
715
|
+
CT.resolveFilterDebounceMs = function ({ filterConfig, plugin }) {
|
|
716
|
+
if (filterConfig && Object.prototype.hasOwnProperty.call(filterConfig, "debounceMs")) {
|
|
717
|
+
// Note: protoColumnConfig.filter.debounceMs defaults to `null`.
|
|
718
|
+
// Treat `null` as "not specified" so we can still fall back to plugin/global.
|
|
719
|
+
if (filterConfig.debounceMs !== null) {
|
|
720
|
+
return CT.normalizeFilterDebounceMs(filterConfig.debounceMs, 0);
|
|
721
|
+
}
|
|
722
|
+
}
|
|
723
|
+
if (plugin && Object.prototype.hasOwnProperty.call(plugin, "filterDebounceMs")) {
|
|
724
|
+
// Same semantics as column debounceMs: null means "unset".
|
|
725
|
+
if (plugin.filterDebounceMs !== null) {
|
|
726
|
+
return CT.normalizeFilterDebounceMs(plugin.filterDebounceMs, 0);
|
|
727
|
+
}
|
|
728
|
+
}
|
|
729
|
+
return CT.normalizeFilterDebounceMs(CT._config.filterDebounceMs, 0);
|
|
730
|
+
};
|
|
731
|
+
|
|
732
|
+
/** Debounce filter submit events when debounceMs > 0. */
|
|
733
|
+
CT.shouldDebounceFilterSubmit = function (events, debounceMs) {
|
|
734
|
+
if (!debounceMs || debounceMs <= 0) {
|
|
735
|
+
return false;
|
|
736
|
+
}
|
|
737
|
+
if (!events) {
|
|
738
|
+
return false;
|
|
739
|
+
}
|
|
740
|
+
return true;
|
|
741
|
+
};
|
|
742
|
+
|
|
743
|
+
CT.bindFieldFilterSubmit = function (type, $input, onSubmit) {
|
|
744
|
+
const plugin = CT.getFieldType(type);
|
|
745
|
+
if (plugin && typeof plugin.bindFilterSubmit === "function") {
|
|
746
|
+
plugin.bindFilterSubmit($input, onSubmit);
|
|
747
|
+
}
|
|
748
|
+
};
|
|
749
|
+
|
|
750
|
+
CT.bindFilterInputEvents = function ({ type, $input, onSubmit, createResult, filterConfig }) {
|
|
751
|
+
const plugin = type ? CT.getFieldType(type) : null;
|
|
752
|
+
const events = CT.resolveFilterEvents({ plugin, $input, createResult });
|
|
753
|
+
const debounceMs = CT.resolveFilterDebounceMs({ filterConfig, plugin });
|
|
754
|
+
const handler = CT.wrapFilterSubmitHandler($input, onSubmit, events, debounceMs);
|
|
755
|
+
if (events) {
|
|
756
|
+
$input.on(events, handler);
|
|
757
|
+
}
|
|
758
|
+
CT.bindFieldFilterSubmit(type, $input, handler);
|
|
759
|
+
};
|
|
760
|
+
|
|
761
|
+
CT.isPluggableFieldType = function (type) {
|
|
762
|
+
return !!CT.getFieldType(type);
|
|
763
|
+
};
|
|
764
|
+
|
|
765
|
+
/**
|
|
766
|
+
* Simple custom type: fixed HTML element + your mount() to init a widget.
|
|
767
|
+
* @param {( $input: JQuery, options: object, ctx: object) => void} mount
|
|
768
|
+
* @param {{ element: string, formInputClass?: string, skipValueAttr?: boolean, filterEvents?, filterDebounceMs?, validate?, bindFilterSubmit? }} opts
|
|
769
|
+
*/
|
|
770
|
+
CT.inputType = function (mount, opts) {
|
|
771
|
+
if (typeof mount !== "function") {
|
|
772
|
+
throw new TypeError("inputType(mount, opts): mount must be a function");
|
|
773
|
+
}
|
|
774
|
+
if (!opts || typeof opts.element !== "string") {
|
|
775
|
+
throw new TypeError("inputType(mount, opts): opts.element is required (HTML string)");
|
|
776
|
+
}
|
|
777
|
+
return {
|
|
778
|
+
filterEvents: opts.filterEvents,
|
|
779
|
+
filterDebounceMs: opts.filterDebounceMs,
|
|
780
|
+
validate: opts.validate,
|
|
781
|
+
create({ mode }) {
|
|
782
|
+
const $input = $(opts.element);
|
|
783
|
+
if (opts.formInputClass && (mode === "insert" || mode === "update")) {
|
|
784
|
+
$input.addClass(opts.formInputClass);
|
|
785
|
+
}
|
|
786
|
+
return { $input, skipValueAttr: !!opts.skipValueAttr };
|
|
787
|
+
},
|
|
788
|
+
mount(ctx) {
|
|
789
|
+
mount(ctx.$input, ctx.config?.options ?? ctx.config, ctx);
|
|
790
|
+
},
|
|
791
|
+
bindFilterSubmit: opts.bindFilterSubmit,
|
|
792
|
+
};
|
|
793
|
+
};
|
|
794
|
+
|
|
795
|
+
CT._getCustomInputTypesConfig = function () {
|
|
796
|
+
return CT._config.customInputTypes ?? CT._config.fieldTypes ?? null;
|
|
797
|
+
};
|
|
798
|
+
|
|
799
|
+
CT._syncCustomInputTypes = function () {
|
|
800
|
+
const types = CT._getCustomInputTypesConfig();
|
|
801
|
+
const activeNames =
|
|
802
|
+
types && typeof types === "object"
|
|
803
|
+
? Object.keys(types).filter((name) => types[name] != null)
|
|
804
|
+
: [];
|
|
805
|
+
|
|
806
|
+
CT._configuredCustomInputTypeNames.forEach((name) => {
|
|
807
|
+
if (!activeNames.includes(name)) {
|
|
808
|
+
CT.unregisterFieldType(name);
|
|
809
|
+
}
|
|
810
|
+
});
|
|
811
|
+
|
|
812
|
+
activeNames.forEach((name) => {
|
|
813
|
+
const spec = types[name];
|
|
814
|
+
if (!spec || typeof spec !== "object" || typeof spec.create !== "function") {
|
|
815
|
+
if (typeof spec === "function") {
|
|
816
|
+
throw new Error(
|
|
817
|
+
"customInputTypes." +
|
|
818
|
+
name +
|
|
819
|
+
" is a function — use a plugin, e.g. KGrid.select2(fn) or KGrid.inputType(fn, { element: \"...\" })"
|
|
820
|
+
);
|
|
821
|
+
}
|
|
822
|
+
throw new TypeError("customInputTypes." + name + " must be a plugin with create()");
|
|
823
|
+
}
|
|
824
|
+
CT.registerFieldType(name, spec, { overwrite: true });
|
|
825
|
+
});
|
|
826
|
+
|
|
827
|
+
CT._configuredCustomInputTypeNames = activeNames.slice();
|
|
828
|
+
};
|
|
829
|
+
})(window.KGrid);
|
|
830
|
+
|
|
831
|
+
|
|
832
|
+
/* --- field-types-builtins.js --- */
|
|
833
|
+
/**
|
|
834
|
+
* Built-in field types with no external library dependency (plain HTML / jQuery DOM).
|
|
835
|
+
*/
|
|
836
|
+
(function (CT) {
|
|
837
|
+
CT.registerFieldType("multi_select", {
|
|
838
|
+
create({ config }) {
|
|
839
|
+
const $input = $("<select class='form-select form-select-sm' multiple>");
|
|
840
|
+
if (Array.isArray(config.options)) {
|
|
841
|
+
config.options.forEach((opt) => {
|
|
842
|
+
$("<option>").text(opt.label).attr("value", opt.value).appendTo($input);
|
|
843
|
+
});
|
|
844
|
+
}
|
|
845
|
+
return { $input, skipValueAttr: true };
|
|
846
|
+
},
|
|
847
|
+
});
|
|
848
|
+
|
|
849
|
+
CT.registerFieldType("date_range", {
|
|
850
|
+
create() {
|
|
851
|
+
const $input = $(
|
|
852
|
+
"<input autocomplete='off' type='date' class='form-control form-control-sm'/>"
|
|
853
|
+
);
|
|
854
|
+
return { $input };
|
|
855
|
+
},
|
|
856
|
+
});
|
|
857
|
+
})(window.KGrid);
|
|
858
|
+
|
|
859
|
+
|
|
860
|
+
/* --- interaction.js --- */
|
|
861
|
+
(function (CT) {
|
|
862
|
+
/** @returns {"view"|"edit"} */
|
|
863
|
+
CT.resolveDefaultInteraction = function (options) {
|
|
864
|
+
if (options.defaultInteraction === "edit" || options.defaultInteraction === "view") {
|
|
865
|
+
return options.defaultInteraction;
|
|
866
|
+
}
|
|
867
|
+
if (options.editmode === true) {
|
|
868
|
+
return "edit";
|
|
869
|
+
}
|
|
870
|
+
if (options.editmode === false) {
|
|
871
|
+
return "view";
|
|
872
|
+
}
|
|
873
|
+
return "view";
|
|
874
|
+
};
|
|
875
|
+
|
|
876
|
+
/** Host element that owns interaction CSS (.custom-table-shell). */
|
|
877
|
+
CT.getTableInteractionHost = function ($host) {
|
|
878
|
+
if ($host.hasClass("custom-table-shell")) {
|
|
879
|
+
return $host;
|
|
880
|
+
}
|
|
881
|
+
const $shell = $host.find(".custom-table-shell").first();
|
|
882
|
+
return $shell.length ? $shell : $host;
|
|
883
|
+
};
|
|
884
|
+
|
|
885
|
+
CT.applyInteraction = function ($host, mode, overrides) {
|
|
886
|
+
const $shell = CT.getTableInteractionHost($host);
|
|
887
|
+
const interaction = mode === "edit" ? "edit" : "view";
|
|
888
|
+
$shell.attr("data-interaction", interaction);
|
|
889
|
+
if (overrides && typeof overrides === "object") {
|
|
890
|
+
if (overrides.create !== undefined) {
|
|
891
|
+
$shell.attr("data-allow-insert", overrides.create ? "true" : "false");
|
|
892
|
+
}
|
|
893
|
+
if (overrides.update !== undefined) {
|
|
894
|
+
$shell.attr("data-allow-update", overrides.update ? "true" : "false");
|
|
895
|
+
}
|
|
896
|
+
if (overrides.delete !== undefined) {
|
|
897
|
+
$shell.attr("data-allow-delete", overrides.delete ? "true" : "false");
|
|
898
|
+
}
|
|
899
|
+
} else if (interaction === "edit") {
|
|
900
|
+
$shell.removeAttr("data-allow-insert data-allow-update data-allow-delete");
|
|
901
|
+
}
|
|
902
|
+
};
|
|
903
|
+
})(window.KGrid);
|
|
904
|
+
|
|
905
|
+
|
|
906
|
+
/* --- labels.js --- */
|
|
907
|
+
(function (CT) {
|
|
908
|
+
/**
|
|
909
|
+
* Setup labels row
|
|
910
|
+
* @returns {jQuery}
|
|
911
|
+
*/
|
|
912
|
+
CT.setupLabelsHeader = function (labelsThead, options) {
|
|
913
|
+
const labelsRow = labelsThead.children("tr");
|
|
914
|
+
|
|
915
|
+
const existingTh = labelsRow.children();
|
|
916
|
+
const labelTemplate = existingTh.first().clone(true);
|
|
917
|
+
existingTh.remove();
|
|
918
|
+
const columns = [...options.columns];
|
|
919
|
+
columns.forEach((col) => {
|
|
920
|
+
if (col.hidden) {
|
|
921
|
+
return;
|
|
922
|
+
}
|
|
923
|
+
|
|
924
|
+
let cell = labelTemplate.clone(true);
|
|
925
|
+
if (col.attrs && typeof col.attrs === "object") {
|
|
926
|
+
Object.keys(col.attrs).forEach((attr) => cell.attr(attr, col.attrs[attr]));
|
|
927
|
+
}
|
|
928
|
+
CT.applyColumnCellMeta(cell, col);
|
|
929
|
+
|
|
930
|
+
if (!col.features?.sort) {
|
|
931
|
+
const tmp = cell.empty().html(col.label ?? "");
|
|
932
|
+
CT.applyColumnCellMeta(tmp, col);
|
|
933
|
+
labelsRow.append(tmp);
|
|
934
|
+
return;
|
|
935
|
+
}
|
|
936
|
+
if (!col.name) {
|
|
937
|
+
throw new Error(
|
|
938
|
+
"Column must have a name when column.sortable is true: \n" +
|
|
939
|
+
JSON.stringify(col, null, 2)
|
|
940
|
+
);
|
|
941
|
+
}
|
|
942
|
+
cell.find("span.column-label").text(col.label ?? "");
|
|
943
|
+
cell.children("a").attr("data-sortfld", col.name);
|
|
944
|
+
cell.appendTo(labelsRow);
|
|
945
|
+
});
|
|
946
|
+
if (CT.hasActionColumn(options)) {
|
|
947
|
+
$("<th>")
|
|
948
|
+
.addClass("kgrid-row-actions")
|
|
949
|
+
.attr("aria-label", "Actions")
|
|
950
|
+
.appendTo(labelsRow);
|
|
951
|
+
}
|
|
952
|
+
|
|
953
|
+
return labelsRow;
|
|
954
|
+
};
|
|
955
|
+
})(window.KGrid);
|
|
956
|
+
|
|
957
|
+
|
|
958
|
+
/* --- filters.js --- */
|
|
959
|
+
(function (CT) {
|
|
960
|
+
const FILTER_DEBOUNCE_TIMER_KEY = "kgridFilterDebounceTimer";
|
|
961
|
+
|
|
962
|
+
/**
|
|
963
|
+
* Cancel a pending debounced filter submit on one control.
|
|
964
|
+
* @param {JQuery} $input
|
|
965
|
+
*/
|
|
966
|
+
CT.cancelFilterSubmit = function ($input) {
|
|
967
|
+
const timer = $input.data(FILTER_DEBOUNCE_TIMER_KEY);
|
|
968
|
+
if (timer) {
|
|
969
|
+
clearTimeout(timer);
|
|
970
|
+
$input.removeData(FILTER_DEBOUNCE_TIMER_KEY);
|
|
971
|
+
}
|
|
972
|
+
};
|
|
973
|
+
|
|
974
|
+
/**
|
|
975
|
+
* Cancel debounced submits for all fields associated with a filter form.
|
|
976
|
+
* @param {HTMLFormElement|JQuery} form
|
|
977
|
+
*/
|
|
978
|
+
CT.cancelFilterDebounces = function (form) {
|
|
979
|
+
const formId = $(form).attr("id");
|
|
980
|
+
if (!formId) {
|
|
981
|
+
return;
|
|
982
|
+
}
|
|
983
|
+
$(document)
|
|
984
|
+
.find("[form='" + formId + "']")
|
|
985
|
+
.each(function () {
|
|
986
|
+
CT.cancelFilterSubmit($(this));
|
|
987
|
+
});
|
|
988
|
+
};
|
|
989
|
+
|
|
990
|
+
/**
|
|
991
|
+
* Run a pending debounced submit immediately, if any.
|
|
992
|
+
* @param {JQuery} $input
|
|
993
|
+
*/
|
|
994
|
+
CT.flushFilterSubmit = function ($input) {
|
|
995
|
+
const timer = $input.data(FILTER_DEBOUNCE_TIMER_KEY);
|
|
996
|
+
if (!timer) {
|
|
997
|
+
return;
|
|
998
|
+
}
|
|
999
|
+
clearTimeout(timer);
|
|
1000
|
+
$input.removeData(FILTER_DEBOUNCE_TIMER_KEY);
|
|
1001
|
+
const form = $input[0] && $input[0].form;
|
|
1002
|
+
if (form) {
|
|
1003
|
+
$(form).trigger("submit");
|
|
1004
|
+
}
|
|
1005
|
+
};
|
|
1006
|
+
|
|
1007
|
+
/**
|
|
1008
|
+
* Optionally debounce filter submit (see resolveFilterDebounceMs / shouldDebounceFilterSubmit).
|
|
1009
|
+
* @param {JQuery} $input
|
|
1010
|
+
* @param {Function} onSubmit bound with control as `this`
|
|
1011
|
+
* @param {string|null} events resolved filter events
|
|
1012
|
+
* @param {number} debounceMs
|
|
1013
|
+
* @returns {Function}
|
|
1014
|
+
*/
|
|
1015
|
+
CT.wrapFilterSubmitHandler = function ($input, onSubmit, events, debounceMs) {
|
|
1016
|
+
if (!CT.shouldDebounceFilterSubmit(events, debounceMs)) {
|
|
1017
|
+
return onSubmit;
|
|
1018
|
+
}
|
|
1019
|
+
const delay = debounceMs;
|
|
1020
|
+
return function filterSubmitDebounced() {
|
|
1021
|
+
CT.cancelFilterSubmit($input);
|
|
1022
|
+
const timer = setTimeout(
|
|
1023
|
+
function () {
|
|
1024
|
+
$input.removeData(FILTER_DEBOUNCE_TIMER_KEY);
|
|
1025
|
+
onSubmit.call(this);
|
|
1026
|
+
}.bind(this),
|
|
1027
|
+
delay
|
|
1028
|
+
);
|
|
1029
|
+
$input.data(FILTER_DEBOUNCE_TIMER_KEY, timer);
|
|
1030
|
+
};
|
|
1031
|
+
};
|
|
1032
|
+
|
|
1033
|
+
CT.filterDefaultValue = function (defaultSpec) {
|
|
1034
|
+
if (defaultSpec != null && typeof defaultSpec === "object" && defaultSpec.value != null) {
|
|
1035
|
+
return defaultSpec.value;
|
|
1036
|
+
}
|
|
1037
|
+
return defaultSpec;
|
|
1038
|
+
};
|
|
1039
|
+
|
|
1040
|
+
/** Hidden / persist filters survive reset and stay out of the visible filter row when appropriate. */
|
|
1041
|
+
CT.columnFilterPersisted = function (col) {
|
|
1042
|
+
const filter = col && col.filter ? col.filter : {};
|
|
1043
|
+
return !!(col && (col.hidden || filter.type === "hidden" || filter.persist));
|
|
1044
|
+
};
|
|
1045
|
+
|
|
1046
|
+
/**
|
|
1047
|
+
* Ensure a named filter field exists on the hidden form (creates hidden input if needed).
|
|
1048
|
+
* @returns {HTMLInputElement|null}
|
|
1049
|
+
*/
|
|
1050
|
+
CT.ensureFilterField = function (formEl, name, value, operator) {
|
|
1051
|
+
if (!formEl || !name || value == null || value === "") {
|
|
1052
|
+
return null;
|
|
1053
|
+
}
|
|
1054
|
+
let field = formEl.elements && formEl.elements.namedItem(name);
|
|
1055
|
+
if (field && field.length != null && !field.tagName) {
|
|
1056
|
+
field = field[0];
|
|
1057
|
+
}
|
|
1058
|
+
if (!field || !field.tagName) {
|
|
1059
|
+
field = document.createElement("input");
|
|
1060
|
+
field.type = "hidden";
|
|
1061
|
+
field.name = name;
|
|
1062
|
+
formEl.appendChild(field);
|
|
1063
|
+
}
|
|
1064
|
+
field.setAttribute("data-operator", operator || "=");
|
|
1065
|
+
field.value = String(value);
|
|
1066
|
+
field.defaultValue = String(value);
|
|
1067
|
+
return field;
|
|
1068
|
+
};
|
|
1069
|
+
|
|
1070
|
+
CT.applyColumnDefaultFilters = function (formEl, columns) {
|
|
1071
|
+
if (!formEl || !columns) {
|
|
1072
|
+
return;
|
|
1073
|
+
}
|
|
1074
|
+
columns.forEach(function (col) {
|
|
1075
|
+
if (!col.name || (col.features && col.features.filter === false)) {
|
|
1076
|
+
return;
|
|
1077
|
+
}
|
|
1078
|
+
const filter = col.filter;
|
|
1079
|
+
if (!filter || filter.default == null || filter.default === "") {
|
|
1080
|
+
return;
|
|
1081
|
+
}
|
|
1082
|
+
const value = CT.filterDefaultValue(filter.default);
|
|
1083
|
+
if (value == null || value === "") {
|
|
1084
|
+
return;
|
|
1085
|
+
}
|
|
1086
|
+
const operator =
|
|
1087
|
+
filter.operator || (CT.columnFilterPersisted(col) ? "=" : "~=~");
|
|
1088
|
+
const $existing = CT.filterFormField(formEl, col.name);
|
|
1089
|
+
if (col.hidden || filter.type === "hidden" || !$existing.length) {
|
|
1090
|
+
CT.ensureFilterField(formEl, col.name, value, operator);
|
|
1091
|
+
return;
|
|
1092
|
+
}
|
|
1093
|
+
if (!$existing.val()) {
|
|
1094
|
+
$existing.val(String(value));
|
|
1095
|
+
$existing.attr("data-operator", operator);
|
|
1096
|
+
}
|
|
1097
|
+
});
|
|
1098
|
+
};
|
|
1099
|
+
|
|
1100
|
+
/**
|
|
1101
|
+
* Re-apply persisted defaults on reset; optionally re-submit when URL lacks them.
|
|
1102
|
+
* @param {HTMLFormElement|JQuery} filterFormEl
|
|
1103
|
+
* @param {Object} options
|
|
1104
|
+
* @param {Object} [collection] KViews collection
|
|
1105
|
+
*/
|
|
1106
|
+
CT.setupDefaultFilters = function (filterFormEl, options, collection) {
|
|
1107
|
+
const form = $(filterFormEl)[0];
|
|
1108
|
+
if (!form || !options || !options.features || !options.features.filtering) {
|
|
1109
|
+
return;
|
|
1110
|
+
}
|
|
1111
|
+
const columns = options.columns || [];
|
|
1112
|
+
CT.applyColumnDefaultFilters(form, columns);
|
|
1113
|
+
|
|
1114
|
+
if (columns.some(CT.columnFilterPersisted)) {
|
|
1115
|
+
let resetting = false;
|
|
1116
|
+
$(form)
|
|
1117
|
+
.off("reset.kgridDefaultFilters")
|
|
1118
|
+
.on("reset.kgridDefaultFilters", function (e) {
|
|
1119
|
+
if (resetting) {
|
|
1120
|
+
return;
|
|
1121
|
+
}
|
|
1122
|
+
e.preventDefault();
|
|
1123
|
+
CT.cancelFilterDebounces(form);
|
|
1124
|
+
resetting = true;
|
|
1125
|
+
try {
|
|
1126
|
+
form.reset();
|
|
1127
|
+
CT.applyColumnDefaultFilters(form, columns);
|
|
1128
|
+
if (
|
|
1129
|
+
collection &&
|
|
1130
|
+
collection.filtering &&
|
|
1131
|
+
typeof collection.filtering.handleSubmit === "function"
|
|
1132
|
+
) {
|
|
1133
|
+
collection.filtering.handleSubmit(form);
|
|
1134
|
+
} else {
|
|
1135
|
+
$(form).trigger("submit");
|
|
1136
|
+
}
|
|
1137
|
+
} finally {
|
|
1138
|
+
resetting = false;
|
|
1139
|
+
}
|
|
1140
|
+
});
|
|
1141
|
+
}
|
|
1142
|
+
|
|
1143
|
+
if (!collection || !collection.filtering) {
|
|
1144
|
+
return;
|
|
1145
|
+
}
|
|
1146
|
+
const persisted = columns.filter(function (col) {
|
|
1147
|
+
return (
|
|
1148
|
+
col.name &&
|
|
1149
|
+
col.filter &&
|
|
1150
|
+
col.filter.default != null &&
|
|
1151
|
+
col.filter.default !== "" &&
|
|
1152
|
+
CT.columnFilterPersisted(col)
|
|
1153
|
+
);
|
|
1154
|
+
});
|
|
1155
|
+
if (!persisted.length) {
|
|
1156
|
+
return;
|
|
1157
|
+
}
|
|
1158
|
+
const urlFilter = String(
|
|
1159
|
+
(collection.url &&
|
|
1160
|
+
collection.url.parameters &&
|
|
1161
|
+
collection.url.parameters.filter) ||
|
|
1162
|
+
""
|
|
1163
|
+
);
|
|
1164
|
+
const missing = persisted.some(function (col) {
|
|
1165
|
+
return urlFilter.indexOf(col.name + "=") === -1;
|
|
1166
|
+
});
|
|
1167
|
+
if (missing) {
|
|
1168
|
+
collection.filtering.handleSubmit(form);
|
|
1169
|
+
}
|
|
1170
|
+
};
|
|
1171
|
+
|
|
1172
|
+
/**
|
|
1173
|
+
* Setup filtering row.
|
|
1174
|
+
* Filter inputs live in <th> cells; they cannot sit inside one <form> in a <tr>.
|
|
1175
|
+
* Each control uses the HTML form="" attribute pointing at a hidden <form id="…"> (see setupFilterHeader).
|
|
1176
|
+
* @returns {jQuery|null} hidden filter form element (for FilterForm / KViews)
|
|
1177
|
+
*/
|
|
1178
|
+
CT.setupFilterHeader = function (table, options) {
|
|
1179
|
+
if (!options.features || !options.features.filtering) {
|
|
1180
|
+
return null;
|
|
1181
|
+
}
|
|
1182
|
+
const theadFilters = table.find(".thead-filters");
|
|
1183
|
+
theadFilters.empty();
|
|
1184
|
+
|
|
1185
|
+
const columns = [...options.columns];
|
|
1186
|
+
const filtersRow = $("<tr>");
|
|
1187
|
+
filtersRow.appendTo(theadFilters);
|
|
1188
|
+
const filterFormId = "filter_form_" + CT.uuid();
|
|
1189
|
+
const filterForm = $("<form>")
|
|
1190
|
+
.attr("id", filterFormId)
|
|
1191
|
+
.attr("hidden", "hidden")
|
|
1192
|
+
.attr("aria-hidden", "true")
|
|
1193
|
+
.addClass("table-filter-form")
|
|
1194
|
+
.insertBefore(table);
|
|
1195
|
+
|
|
1196
|
+
columns.forEach((col) => {
|
|
1197
|
+
const filter = col.filter || {};
|
|
1198
|
+
const skipVisible = col.hidden || filter.type === "hidden";
|
|
1199
|
+
|
|
1200
|
+
if (skipVisible) {
|
|
1201
|
+
if (col.features && col.features.filter === false) {
|
|
1202
|
+
return;
|
|
1203
|
+
}
|
|
1204
|
+
if (!col.name) {
|
|
1205
|
+
return;
|
|
1206
|
+
}
|
|
1207
|
+
const defVal = CT.filterDefaultValue(filter.default);
|
|
1208
|
+
if (defVal != null && defVal !== "") {
|
|
1209
|
+
CT.ensureFilterField(
|
|
1210
|
+
filterForm[0],
|
|
1211
|
+
col.name,
|
|
1212
|
+
defVal,
|
|
1213
|
+
filter.operator || "="
|
|
1214
|
+
);
|
|
1215
|
+
} else if (CT.columnFilterPersisted(col)) {
|
|
1216
|
+
CT.ensureFilterField(filterForm[0], col.name, "", filter.operator || "=");
|
|
1217
|
+
}
|
|
1218
|
+
return;
|
|
1219
|
+
}
|
|
1220
|
+
|
|
1221
|
+
let filterCell = $("<th>").appendTo(filtersRow).attr("data-label", col.label);
|
|
1222
|
+
CT.applyColumnCellMeta(filterCell, col);
|
|
1223
|
+
|
|
1224
|
+
if (col.features.filter === false) {
|
|
1225
|
+
return;
|
|
1226
|
+
}
|
|
1227
|
+
if (!col.name) {
|
|
1228
|
+
throw new Error(
|
|
1229
|
+
"Column must have a name when column.features.filter is true: \n" +
|
|
1230
|
+
JSON.stringify(col, null, 2)
|
|
1231
|
+
);
|
|
1232
|
+
}
|
|
1233
|
+
|
|
1234
|
+
let input;
|
|
1235
|
+
let pluggableResult = null;
|
|
1236
|
+
const pluggable = CT.createFieldInput({ mode: "filter", col, config: filter });
|
|
1237
|
+
if (pluggable) {
|
|
1238
|
+
pluggableResult = pluggable;
|
|
1239
|
+
input = pluggable.$input;
|
|
1240
|
+
input.appendTo(filterCell);
|
|
1241
|
+
CT.mountField({ mode: "filter", $input: input, col, config: filter });
|
|
1242
|
+
} else {
|
|
1243
|
+
switch (filter.type) {
|
|
1244
|
+
case "select":
|
|
1245
|
+
if (!filter.options) {
|
|
1246
|
+
throw new Error(
|
|
1247
|
+
"Column must have a filter.options array when column.filter.type is select: \n" +
|
|
1248
|
+
JSON.stringify(col, null, 2)
|
|
1249
|
+
);
|
|
1250
|
+
}
|
|
1251
|
+
input = $("<select>").addClass("form-select form-select-sm");
|
|
1252
|
+
if (Array.isArray(filter.options)) {
|
|
1253
|
+
filter.options.forEach((opt) => {
|
|
1254
|
+
if (!opt.label || typeof opt.value !== "string") {
|
|
1255
|
+
throw new Error(
|
|
1256
|
+
"Column must have an filter.options object with label and value when column.filter.type is select: \n" +
|
|
1257
|
+
JSON.stringify(col, null, 2)
|
|
1258
|
+
);
|
|
1259
|
+
}
|
|
1260
|
+
$("<option>")
|
|
1261
|
+
.text(opt.label)
|
|
1262
|
+
.attr("value", opt.value)
|
|
1263
|
+
.appendTo(input);
|
|
1264
|
+
});
|
|
1265
|
+
} else {
|
|
1266
|
+
throw new Error(
|
|
1267
|
+
"Column must have a filter.options array when column.filter.type is select: \n" +
|
|
1268
|
+
JSON.stringify(col, null, 2)
|
|
1269
|
+
);
|
|
1270
|
+
}
|
|
1271
|
+
input.appendTo(filterCell);
|
|
1272
|
+
break;
|
|
1273
|
+
default:
|
|
1274
|
+
if (!CT.isValidFilterType(filter.type)) {
|
|
1275
|
+
throw new Error("Unknown filter type: " + filter.type);
|
|
1276
|
+
}
|
|
1277
|
+
input = $(
|
|
1278
|
+
`<input autocomplete='off' type='${filter.type}' class='form-control form-control-sm'/>`
|
|
1279
|
+
);
|
|
1280
|
+
input.appendTo(filterCell);
|
|
1281
|
+
}
|
|
1282
|
+
}
|
|
1283
|
+
|
|
1284
|
+
input.attr("data-operator", filter.operator);
|
|
1285
|
+
input.attr("form", filterFormId);
|
|
1286
|
+
input.attr("name", col.name);
|
|
1287
|
+
|
|
1288
|
+
const defVal = CT.filterDefaultValue(filter.default);
|
|
1289
|
+
if (defVal != null && defVal !== "" && !input.val()) {
|
|
1290
|
+
input.val(String(defVal));
|
|
1291
|
+
input.attr("data-default", String(defVal));
|
|
1292
|
+
if (input[0]) {
|
|
1293
|
+
input[0].defaultValue = String(defVal);
|
|
1294
|
+
}
|
|
1295
|
+
}
|
|
1296
|
+
|
|
1297
|
+
const submitFilterForm = function () {
|
|
1298
|
+
const form = this.form;
|
|
1299
|
+
if (form) {
|
|
1300
|
+
$(form).trigger("submit");
|
|
1301
|
+
}
|
|
1302
|
+
};
|
|
1303
|
+
CT.bindFilterInputEvents({
|
|
1304
|
+
type: filter.type,
|
|
1305
|
+
$input: input,
|
|
1306
|
+
onSubmit: submitFilterForm,
|
|
1307
|
+
createResult: pluggableResult,
|
|
1308
|
+
filterConfig: filter,
|
|
1309
|
+
});
|
|
1310
|
+
});
|
|
1311
|
+
|
|
1312
|
+
if (CT.hasActionColumn(options)) {
|
|
1313
|
+
$("<th>")
|
|
1314
|
+
.addClass("kgrid-row-actions")
|
|
1315
|
+
.attr("data-label", "Actions")
|
|
1316
|
+
.appendTo(filtersRow);
|
|
1317
|
+
}
|
|
1318
|
+
|
|
1319
|
+
return filterForm;
|
|
1320
|
+
};
|
|
1321
|
+
|
|
1322
|
+
CT.FilterForm = function (form) {
|
|
1323
|
+
this.form = $(form)[0];
|
|
1324
|
+
this.filter = (name, value, operator = "~=~") => {
|
|
1325
|
+
if (!this.form) return this;
|
|
1326
|
+
let $field = CT.filterFormField(this.form, name);
|
|
1327
|
+
if (!$field.length) {
|
|
1328
|
+
CT.ensureFilterField(this.form, name, value, operator);
|
|
1329
|
+
$field = CT.filterFormField(this.form, name);
|
|
1330
|
+
}
|
|
1331
|
+
if (!$field.length) return this;
|
|
1332
|
+
CT.flushFilterSubmit($field);
|
|
1333
|
+
$field.val(value);
|
|
1334
|
+
const oldOperator = $field.attr("data-operator");
|
|
1335
|
+
$field.attr("data-operator", operator);
|
|
1336
|
+
$(this.form).trigger("submit");
|
|
1337
|
+
$field.attr("data-operator", oldOperator);
|
|
1338
|
+
return this;
|
|
1339
|
+
};
|
|
1340
|
+
this.ensure = (name, value, operator = "=") => {
|
|
1341
|
+
CT.ensureFilterField(this.form, name, value, operator);
|
|
1342
|
+
return this;
|
|
1343
|
+
};
|
|
1344
|
+
this.reset = () => {
|
|
1345
|
+
if (this.form) {
|
|
1346
|
+
CT.cancelFilterDebounces(this.form);
|
|
1347
|
+
this.form.reset();
|
|
1348
|
+
}
|
|
1349
|
+
return this;
|
|
1350
|
+
};
|
|
1351
|
+
};
|
|
1352
|
+
})(window.KGrid);
|
|
1353
|
+
|
|
1354
|
+
|
|
1355
|
+
/* --- cells.js --- */
|
|
1356
|
+
(function (CT) {
|
|
1357
|
+
/**
|
|
1358
|
+
* Setup data cell
|
|
1359
|
+
* @param {Object} col
|
|
1360
|
+
* @param {jQuery} editForm hidden anchor form (fields use form="id")
|
|
1361
|
+
* @returns {jQuery}
|
|
1362
|
+
*/
|
|
1363
|
+
CT.setupCell = function (col, editForm) {
|
|
1364
|
+
|
|
1365
|
+
const formId = editForm ? editForm.attr("id") : null;
|
|
1366
|
+
|
|
1367
|
+
const c = {...col };
|
|
1368
|
+
const $cell = $("<td>").attr("data-label", col.label);
|
|
1369
|
+
CT.applyColumnCellMeta($cell, col);
|
|
1370
|
+
|
|
1371
|
+
const attrs = (c.attrs && typeof c.attrs==="object") ? c.attrs : {};
|
|
1372
|
+
Object.keys(attrs).forEach(attr => $cell.attr(attr, attrs[attr]));
|
|
1373
|
+
|
|
1374
|
+
let cellContent = $("<div>").addClass("cell-content");
|
|
1375
|
+
cellContent.html(c.display.template ?? `{{${c.name}}}`);
|
|
1376
|
+
cellContent.appendTo($cell);
|
|
1377
|
+
|
|
1378
|
+
if (editForm==null || !col.features.update) {
|
|
1379
|
+
cellContent.clone(true).removeClass('cell-content').addClass('cell-input').appendTo($cell);
|
|
1380
|
+
return $cell;
|
|
1381
|
+
}
|
|
1382
|
+
|
|
1383
|
+
if(!c.name) {
|
|
1384
|
+
throw new Error("Column must have a name when column.features.update is true: \n"+JSON.stringify(col,null,2));
|
|
1385
|
+
}
|
|
1386
|
+
|
|
1387
|
+
const updateConfig = c.update;
|
|
1388
|
+
const updateType = updateConfig.type ?? "text";
|
|
1389
|
+
let input;
|
|
1390
|
+
let skipValueAttr = false;
|
|
1391
|
+
|
|
1392
|
+
const pluggable = CT.createFieldInput({ mode: "update", col, config: updateConfig });
|
|
1393
|
+
if (pluggable) {
|
|
1394
|
+
input = pluggable.$input;
|
|
1395
|
+
skipValueAttr = !!pluggable.skipValueAttr;
|
|
1396
|
+
} else {
|
|
1397
|
+
switch(updateType) {
|
|
1398
|
+
case "textarea":
|
|
1399
|
+
input = $(`<textarea class='form-input form-control form-control-sm'>{{${updateConfig.value??c.name}}}</textarea>`);
|
|
1400
|
+
skipValueAttr = true;
|
|
1401
|
+
break;
|
|
1402
|
+
case "select":
|
|
1403
|
+
input = $(`<select class='form-input form-control form-control-sm'/>`);
|
|
1404
|
+
if(!Array.isArray(updateConfig.options)) {
|
|
1405
|
+
throw new Error("Select column must have an update.options array when column.features.update is true: \n"+JSON.stringify(col,null,2));
|
|
1406
|
+
}
|
|
1407
|
+
updateConfig.options.forEach((opt) => {
|
|
1408
|
+
if(typeof opt.label !== "string" || typeof opt.value === "undefined") {
|
|
1409
|
+
throw new Error("Select update.options entries need label and value: \n"+JSON.stringify(col,null,2));
|
|
1410
|
+
}
|
|
1411
|
+
$("<option>").text(opt.label).attr("value", opt.value).appendTo(input);
|
|
1412
|
+
});
|
|
1413
|
+
skipValueAttr = true;
|
|
1414
|
+
break;
|
|
1415
|
+
case "hidden":
|
|
1416
|
+
input = $(`<input type='hidden' class='form-input form-control form-control-sm'/>`);
|
|
1417
|
+
break;
|
|
1418
|
+
case "displayonly": {
|
|
1419
|
+
const tpl =
|
|
1420
|
+
updateConfig.template ||
|
|
1421
|
+
c.display.template ||
|
|
1422
|
+
`{{${c.name}}}`;
|
|
1423
|
+
$("<div>").addClass("cell-input").append(tpl).appendTo($cell);
|
|
1424
|
+
return $cell;
|
|
1425
|
+
}
|
|
1426
|
+
default:
|
|
1427
|
+
if(CT.isValidInputType(updateType)) {
|
|
1428
|
+
input = $(`<input autocomplete='off' type='${updateType}' class='form-input form-control form-control-sm'/>`);
|
|
1429
|
+
} else {
|
|
1430
|
+
throw new Error("Invalid update updateConfig type: "+JSON.stringify(updateConfig,null,2));
|
|
1431
|
+
}
|
|
1432
|
+
}
|
|
1433
|
+
input.attr("data-type", updateType);
|
|
1434
|
+
}
|
|
1435
|
+
|
|
1436
|
+
if(updateConfig.attrs && typeof updateConfig.attrs === 'object') {
|
|
1437
|
+
Object.keys(updateConfig.attrs).forEach(k => input.attr(k, updateConfig.attrs[k]));
|
|
1438
|
+
}
|
|
1439
|
+
|
|
1440
|
+
input.attr("form",formId);
|
|
1441
|
+
input.attr("name",c.name);
|
|
1442
|
+
if (!skipValueAttr && updateType !== "textarea") {
|
|
1443
|
+
const rawValue = updateConfig.value ?? `{{${c.name}}}`;
|
|
1444
|
+
if (rawValue != null && typeof rawValue !== "object") {
|
|
1445
|
+
input.attr("value", rawValue);
|
|
1446
|
+
}
|
|
1447
|
+
}
|
|
1448
|
+
|
|
1449
|
+
if(c.hidden) {
|
|
1450
|
+
input.attr("type","hidden").appendTo(editForm);
|
|
1451
|
+
return $cell;
|
|
1452
|
+
}
|
|
1453
|
+
|
|
1454
|
+
|
|
1455
|
+
if(updateConfig.disabled) input.attr("disabled",true);
|
|
1456
|
+
if(updateConfig.readonly) input.attr("readonly",true);
|
|
1457
|
+
if(updateConfig.required) input.attr("required",true);
|
|
1458
|
+
|
|
1459
|
+
if(updateConfig.attrs && typeof updateConfig.attrs === 'object') {
|
|
1460
|
+
Object.keys(updateConfig.attrs).forEach(k => input.attr(k, updateConfig.attrs[k]));
|
|
1461
|
+
}
|
|
1462
|
+
if(!updateConfig.disabled && !updateConfig.readonly)
|
|
1463
|
+
input.attr("onchange","$(this).parents('tr').addClass('editing')");
|
|
1464
|
+
|
|
1465
|
+
$("<div>").addClass("cell-input").append(input).appendTo($cell);
|
|
1466
|
+
return $cell;
|
|
1467
|
+
};
|
|
1468
|
+
})(window.KGrid);
|
|
1469
|
+
|
|
1470
|
+
|
|
1471
|
+
/* --- data-body.js --- */
|
|
1472
|
+
(function (CT) {
|
|
1473
|
+
CT.setupNoDataTbody = function (noDataTbody, options, noVisibleCols) {
|
|
1474
|
+
const td = noDataTbody.find("td")
|
|
1475
|
+
.attr("colspan", noVisibleCols);
|
|
1476
|
+
|
|
1477
|
+
if(options.noDataTemplate) {
|
|
1478
|
+
td.html(options.noDataTemplate);
|
|
1479
|
+
}
|
|
1480
|
+
return td;
|
|
1481
|
+
};
|
|
1482
|
+
|
|
1483
|
+
CT.setupDataBody = function (dataBody, options, labelsRow, filterForm, pagingFooter, noDataTbody) {
|
|
1484
|
+
const columns = [...options.columns];
|
|
1485
|
+
const dataRow = $("<tr>").appendTo(dataBody);
|
|
1486
|
+
if(options.dataRowAttrs && typeof options.dataRowAttrs!=="object") {
|
|
1487
|
+
throw new Error("options.dataRowAttrs must be an object");
|
|
1488
|
+
}
|
|
1489
|
+
const dataRowAttrs = {...(options.dataRowAttrs ?? {})};
|
|
1490
|
+
Object.keys(dataRowAttrs).forEach(attr => dataRow.attr(attr,dataRowAttrs[attr]));
|
|
1491
|
+
|
|
1492
|
+
{
|
|
1493
|
+
dataBody.data("emptyview",noDataTbody);
|
|
1494
|
+
|
|
1495
|
+
if(options?.features?.sorting) {
|
|
1496
|
+
dataBody.data("sort",labelsRow);
|
|
1497
|
+
}
|
|
1498
|
+
if(pagingFooter) {
|
|
1499
|
+
dataBody.data("paging",pagingFooter.find(".pages"))
|
|
1500
|
+
.data("pagesizeinp",pagingFooter.find(".pagesize"))
|
|
1501
|
+
.data("totalrecscount",pagingFooter.find(".totalrecscount"));
|
|
1502
|
+
}
|
|
1503
|
+
|
|
1504
|
+
if(filterForm) {
|
|
1505
|
+
dataBody.data("filter",filterForm);
|
|
1506
|
+
}
|
|
1507
|
+
|
|
1508
|
+
if(options.type) {
|
|
1509
|
+
dataBody.data("type",options.type);
|
|
1510
|
+
}
|
|
1511
|
+
}
|
|
1512
|
+
|
|
1513
|
+
const dataRowFormId = "data_row_form_"+CT.uuid()+"_{{this.id}}";
|
|
1514
|
+
const editForm = options.features.update
|
|
1515
|
+
? $("<form class='edit-form table-row-form'>").attr("id", dataRowFormId)
|
|
1516
|
+
: null;
|
|
1517
|
+
|
|
1518
|
+
columns.forEach(col => {
|
|
1519
|
+
const dataCell = CT.setupCell(col,editForm);
|
|
1520
|
+
if(!col.hidden) {
|
|
1521
|
+
dataCell.appendTo(dataRow);
|
|
1522
|
+
}
|
|
1523
|
+
});
|
|
1524
|
+
|
|
1525
|
+
if (CT.hasActionColumn(options)) {
|
|
1526
|
+
const buttonColumn = $("<td>").addClass("kgrid-row-actions").appendTo(dataRow);
|
|
1527
|
+
if(options.features.delete) {
|
|
1528
|
+
$("<div>").addClass("btn-group delete-item-grp").appendTo(buttonColumn).append(
|
|
1529
|
+
$("<button>").addClass("btn btn-sm btn-danger delete-item")
|
|
1530
|
+
.attr("type","button")
|
|
1531
|
+
.attr("title","Delete item")
|
|
1532
|
+
.append("<i class='fas fa-trash'></i>"));
|
|
1533
|
+
}
|
|
1534
|
+
|
|
1535
|
+
if(options.features.update) {
|
|
1536
|
+
const grp = $("<div>").addClass("btn-group edit-item-grp").appendTo(buttonColumn);
|
|
1537
|
+
$("<button>").addClass("btn btn-sm btn-success save-item")
|
|
1538
|
+
.attr("type","submit")
|
|
1539
|
+
.attr("name","save")
|
|
1540
|
+
.attr("title","Save item")
|
|
1541
|
+
.attr("form",dataRowFormId)
|
|
1542
|
+
.html("<i class='fas fa-save'></i>")
|
|
1543
|
+
.appendTo(grp);
|
|
1544
|
+
$("<button>").addClass("btn btn-sm btn-secondary cancel-edit")
|
|
1545
|
+
.attr("type","button")
|
|
1546
|
+
.attr("name","cancel")
|
|
1547
|
+
.attr("title","Cancel edit")
|
|
1548
|
+
.attr("form",dataRowFormId)
|
|
1549
|
+
.attr("onclick","$(this).parents('[data-type=item]').data().instance.loadFromRemote()")
|
|
1550
|
+
.html("<i class='fas fa-undo'></i>")
|
|
1551
|
+
.appendTo(grp);
|
|
1552
|
+
}
|
|
1553
|
+
}
|
|
1554
|
+
if (editForm) {
|
|
1555
|
+
CT.anchorRowForm(editForm, dataRow);
|
|
1556
|
+
}
|
|
1557
|
+
|
|
1558
|
+
return dataBody;
|
|
1559
|
+
};
|
|
1560
|
+
|
|
1561
|
+
CT.setupPagingFooter = function (footer, options, noVisibleCols) {
|
|
1562
|
+
CT.log("setupPagingFooter",footer,options,noVisibleCols);
|
|
1563
|
+
if(options.pagingFooterAttrs && CT.isPlainObject(options.pagingFooterAttrs)) {
|
|
1564
|
+
Object.keys(options.pagingFooterAttrs).forEach(att => footer.attr(att,options.pagingFooterAttrs[att]));
|
|
1565
|
+
}
|
|
1566
|
+
CT.log("noVisibleCols",noVisibleCols);
|
|
1567
|
+
footer.find("td").attr("colspan",noVisibleCols);
|
|
1568
|
+
return footer;
|
|
1569
|
+
};
|
|
1570
|
+
})(window.KGrid);
|
|
1571
|
+
|
|
1572
|
+
|
|
1573
|
+
/* --- insert-row.js --- */
|
|
1574
|
+
(function (CT) {
|
|
1575
|
+
/**
|
|
1576
|
+
* Setup new record row
|
|
1577
|
+
* @returns {jQuery}
|
|
1578
|
+
*/
|
|
1579
|
+
CT.setupNewRecordForm = function (table, options, grid) {
|
|
1580
|
+
if(!options.insertFormRow || !CT.isPlainObject(options.insertFormRow)) {
|
|
1581
|
+
table.find(".before-main-tbody").remove();
|
|
1582
|
+
table.find(".after-main-tbody").remove();
|
|
1583
|
+
return;
|
|
1584
|
+
}
|
|
1585
|
+
|
|
1586
|
+
const columns = [...options.columns];
|
|
1587
|
+
|
|
1588
|
+
const newRecordRow = $("<tr>").addClass("new-record-row");
|
|
1589
|
+
const position = options.insertFormRow?.position ?? "top";
|
|
1590
|
+
if(position==="top") {
|
|
1591
|
+
table.find(".after-main-tbody").remove();
|
|
1592
|
+
newRecordRow.appendTo(table.find(".before-main-tbody"));
|
|
1593
|
+
}
|
|
1594
|
+
else {
|
|
1595
|
+
table.find(".before-main-tbody").remove();
|
|
1596
|
+
newRecordRow.appendTo(table.find(".after-main-tbody"));
|
|
1597
|
+
}
|
|
1598
|
+
|
|
1599
|
+
const newRecordFormId = "new_record_row_form_"+CT.uuid();
|
|
1600
|
+
const newRecordForm = $("<form class='table-row-form'>").attr("id",newRecordFormId)
|
|
1601
|
+
.off("submit").on("submit",(event)=>{
|
|
1602
|
+
event.preventDefault();
|
|
1603
|
+
const data = CT.serializeForm(event.target, columns);
|
|
1604
|
+
Object.keys(data).forEach(key => {
|
|
1605
|
+
if(columns.find(col => col.name === key)?.insert?.dontsave) {
|
|
1606
|
+
delete data[key];
|
|
1607
|
+
}
|
|
1608
|
+
});
|
|
1609
|
+
grid.instance.newItem(data).then(()=>{
|
|
1610
|
+
event.target.reset();
|
|
1611
|
+
if(typeof options.onNewItemCreated=="function") {
|
|
1612
|
+
options.onNewItemCreated(data);
|
|
1613
|
+
}
|
|
1614
|
+
}).catch(CT.onError);
|
|
1615
|
+
});
|
|
1616
|
+
|
|
1617
|
+
columns.forEach(col => {
|
|
1618
|
+
|
|
1619
|
+
if(col.hidden) {
|
|
1620
|
+
if(options.features?.create && col.features?.create) {
|
|
1621
|
+
const input = $("<input type='hidden'>")
|
|
1622
|
+
.attr("name",col.name)
|
|
1623
|
+
.attr("form",newRecordFormId).appendTo(newRecordForm);
|
|
1624
|
+
if(col.insert.default) {
|
|
1625
|
+
input.val(col.insert.default);
|
|
1626
|
+
}
|
|
1627
|
+
}
|
|
1628
|
+
return;
|
|
1629
|
+
}
|
|
1630
|
+
if(!col.features.create){
|
|
1631
|
+
const $empty = $("<td>").appendTo(newRecordRow).attr("data-label", col.label);
|
|
1632
|
+
CT.applyColumnCellMeta($empty, col);
|
|
1633
|
+
return;
|
|
1634
|
+
}
|
|
1635
|
+
|
|
1636
|
+
const insertConfig = col.insert;
|
|
1637
|
+
if(!insertConfig || !CT.isPlainObject(insertConfig)) {
|
|
1638
|
+
throw new Error("Column must have an insert config object when column.features.create is true: \n"+JSON.stringify(col,null,2));
|
|
1639
|
+
}
|
|
1640
|
+
|
|
1641
|
+
if(!insertConfig.type) {
|
|
1642
|
+
throw new Error("Column must have an insert.type when column.features.insert is true: \n"+JSON.stringify(col,null,2));
|
|
1643
|
+
}
|
|
1644
|
+
|
|
1645
|
+
let input;
|
|
1646
|
+
const insertType = insertConfig.type ?? "text";
|
|
1647
|
+
const pluggable = CT.createFieldInput({ mode: "insert", col, config: insertConfig });
|
|
1648
|
+
if (pluggable) {
|
|
1649
|
+
input = pluggable.$input;
|
|
1650
|
+
} else {
|
|
1651
|
+
switch(insertType) {
|
|
1652
|
+
case "textarea":
|
|
1653
|
+
input = $(`<textarea class='form-input form-control form-control-sm'>`);
|
|
1654
|
+
break;
|
|
1655
|
+
case "select":
|
|
1656
|
+
input = $(`<select class='form-input form-control form-control-sm'/>`);
|
|
1657
|
+
if(!insertConfig.options || !Array.isArray(insertConfig.options)) {
|
|
1658
|
+
throw new Error("Column must have an insert.options array when column.features.insert is true: \n"+JSON.stringify(col,null,2));
|
|
1659
|
+
}
|
|
1660
|
+
insertConfig.options.forEach((opt)=>{
|
|
1661
|
+
if(typeof opt.label!="string" || typeof opt.value=="undefined") {
|
|
1662
|
+
throw new Error("Column must have an insert.options object with label and value when column.features.insert is true: \n"+JSON.stringify(col,null,2));
|
|
1663
|
+
}
|
|
1664
|
+
$("<option>").text(opt.label).attr("value",opt.value).appendTo(input);
|
|
1665
|
+
});
|
|
1666
|
+
break;
|
|
1667
|
+
case "hidden":
|
|
1668
|
+
input = $(`<input type='hidden' class='form-input form-control form-control-sm'/>`);
|
|
1669
|
+
break;
|
|
1670
|
+
default:
|
|
1671
|
+
if(CT.isValidInputType(insertType)) {
|
|
1672
|
+
input = $(`<input autocomplete='off' type='${insertType}' class='form-input form-control form-control-sm'/>`);
|
|
1673
|
+
} else {
|
|
1674
|
+
throw new Error("Invalid type: "+JSON.stringify(insertConfig,null,2));
|
|
1675
|
+
}
|
|
1676
|
+
input.attr("data-type", insertType);
|
|
1677
|
+
}
|
|
1678
|
+
}
|
|
1679
|
+
|
|
1680
|
+
if(insertConfig.default != null && insertConfig.default !== "" && !input.val()) {
|
|
1681
|
+
const defVal = (typeof insertConfig.default === "object" && insertConfig.default.value != null)
|
|
1682
|
+
? insertConfig.default.value
|
|
1683
|
+
: insertConfig.default;
|
|
1684
|
+
if(String(defVal).trim()) {
|
|
1685
|
+
input.val(defVal).trigger("change");
|
|
1686
|
+
}
|
|
1687
|
+
}
|
|
1688
|
+
|
|
1689
|
+
if(insertConfig.attrs && typeof insertConfig.attrs === 'object') {
|
|
1690
|
+
Object.keys(insertConfig.attrs).forEach(k => input.attr(k, insertConfig.attrs[k]));
|
|
1691
|
+
}
|
|
1692
|
+
|
|
1693
|
+
input.attr("form",newRecordFormId).attr("name",col.name);
|
|
1694
|
+
if(insertConfig.disabled) {
|
|
1695
|
+
input.attr("disabled",true);
|
|
1696
|
+
}
|
|
1697
|
+
if(insertConfig.readonly) {
|
|
1698
|
+
input.attr("readonly",true);
|
|
1699
|
+
}
|
|
1700
|
+
if(insertConfig.required) {
|
|
1701
|
+
input.attr("required",true);
|
|
1702
|
+
}
|
|
1703
|
+
if(insertConfig.attrs && typeof insertConfig.attrs === 'object') {
|
|
1704
|
+
Object.keys(insertConfig.attrs).forEach(k => input.attr(k, insertConfig.attrs[k]));
|
|
1705
|
+
}
|
|
1706
|
+
|
|
1707
|
+
if(insertConfig.type!=="hidden") {
|
|
1708
|
+
const $td = $("<td>").append(input).appendTo(newRecordRow).attr("data-label", col.label);
|
|
1709
|
+
CT.applyColumnCellMeta($td, col);
|
|
1710
|
+
}
|
|
1711
|
+
CT.mountField({
|
|
1712
|
+
mode: "insert",
|
|
1713
|
+
$input: input,
|
|
1714
|
+
col,
|
|
1715
|
+
config: insertConfig,
|
|
1716
|
+
formEl: newRecordForm[0],
|
|
1717
|
+
rowEl: newRecordRow[0],
|
|
1718
|
+
});
|
|
1719
|
+
if(insertConfig.events && Array.isArray(insertConfig.events)) {
|
|
1720
|
+
insertConfig.events.forEach(ev=>{
|
|
1721
|
+
input.off(ev.event).on(ev.event, function(e, ...args) {
|
|
1722
|
+
if (typeof ev.callback === "function") ev.callback(e, newRecordForm[0],newRecordRow[0], ...args);
|
|
1723
|
+
});
|
|
1724
|
+
});
|
|
1725
|
+
}
|
|
1726
|
+
});
|
|
1727
|
+
CT.anchorRowForm(newRecordForm, newRecordRow);
|
|
1728
|
+
const actionColumn = $("<td>")
|
|
1729
|
+
.addClass("kgrid-row-actions")
|
|
1730
|
+
.appendTo(newRecordRow)
|
|
1731
|
+
.attr("data-label", "action");
|
|
1732
|
+
const grp = $("<div>").addClass("btn-group").appendTo(actionColumn);
|
|
1733
|
+
$("<button>").addClass("btn btn-sm btn-primary new-item-btn")
|
|
1734
|
+
.html("<i class='fas fa-plus-square'></i>")
|
|
1735
|
+
.attr("form",newRecordFormId)
|
|
1736
|
+
.attr("type","submit")
|
|
1737
|
+
.appendTo(grp);
|
|
1738
|
+
|
|
1739
|
+
if (typeof options.onInsertRowReady === "function") {
|
|
1740
|
+
options.onInsertRowReady(newRecordForm[0], newRecordRow[0]);
|
|
1741
|
+
}
|
|
1742
|
+
|
|
1743
|
+
return newRecordRow;
|
|
1744
|
+
};
|
|
1745
|
+
})(window.KGrid);
|
|
1746
|
+
|
|
1747
|
+
|
|
1748
|
+
/* --- events.js --- */
|
|
1749
|
+
(function (CT) {
|
|
1750
|
+
/**
|
|
1751
|
+
* Setup events for item after render
|
|
1752
|
+
*/
|
|
1753
|
+
CT.setupEvents = function (item, table, options, colMap) {
|
|
1754
|
+
const view = item.views[0];
|
|
1755
|
+
|
|
1756
|
+
options.columns.forEach(col=>{
|
|
1757
|
+
col.display.events.forEach(event=>{
|
|
1758
|
+
if(!event.selector || !event.event || !event.callback)
|
|
1759
|
+
throw new Error("Invalid event: "+JSON.stringify(event,null,2));
|
|
1760
|
+
|
|
1761
|
+
view.el.find(event.selector).off(event.event).on(event.event,(e)=>{
|
|
1762
|
+
if(typeof event.callback!=="function")
|
|
1763
|
+
throw new Error("Event callback must be a function: "+JSON.stringify(event,null,2));
|
|
1764
|
+
event.callback(e,item,view);
|
|
1765
|
+
});
|
|
1766
|
+
});
|
|
1767
|
+
|
|
1768
|
+
if(options.features.update) {
|
|
1769
|
+
col.update.events.forEach((event)=>{
|
|
1770
|
+
if(!event.event || !event.callback)
|
|
1771
|
+
throw new Error("Invalid event: "+JSON.stringify(event,null,2));
|
|
1772
|
+
|
|
1773
|
+
view.el.find("[name='"+col.name+"']").off(event.event).on(event.event,function(e,...args){
|
|
1774
|
+
CT.log("event",event,e,this,args);
|
|
1775
|
+
if(typeof event.callback!=="function")
|
|
1776
|
+
throw new Error("Event callback must be a function: "+JSON.stringify(event,null,2));
|
|
1777
|
+
event.callback(e,item,view,...args);
|
|
1778
|
+
});
|
|
1779
|
+
});
|
|
1780
|
+
}
|
|
1781
|
+
});
|
|
1782
|
+
|
|
1783
|
+
if(options.features.update) {
|
|
1784
|
+
view.el.find("[data-type]").each((index, el) => {
|
|
1785
|
+
const type = el.getAttribute("data-type");
|
|
1786
|
+
if (!CT.isPluggableFieldType(type)) {
|
|
1787
|
+
return;
|
|
1788
|
+
}
|
|
1789
|
+
const col = colMap.get(el.name);
|
|
1790
|
+
if (!col?.update) {
|
|
1791
|
+
return;
|
|
1792
|
+
}
|
|
1793
|
+
CT.mountField({
|
|
1794
|
+
mode: "update",
|
|
1795
|
+
$input: $(el),
|
|
1796
|
+
col,
|
|
1797
|
+
config: col.update,
|
|
1798
|
+
item,
|
|
1799
|
+
view,
|
|
1800
|
+
});
|
|
1801
|
+
});
|
|
1802
|
+
|
|
1803
|
+
view.el.find("select[data-type='select']").each((index, input) => {
|
|
1804
|
+
const col = colMap.get(input.name);
|
|
1805
|
+
if (!col) {
|
|
1806
|
+
return;
|
|
1807
|
+
}
|
|
1808
|
+
const val = item.attributes[col.name];
|
|
1809
|
+
if (val == null || val === "") {
|
|
1810
|
+
return;
|
|
1811
|
+
}
|
|
1812
|
+
$(input).val(typeof val === "boolean" ? String(val) : val);
|
|
1813
|
+
});
|
|
1814
|
+
|
|
1815
|
+
view.el.find("form.edit-form").off("submit").on("submit",(event)=>{
|
|
1816
|
+
const form = event.target;
|
|
1817
|
+
event.preventDefault();
|
|
1818
|
+
const instance = $(form).parents("[data-type=item]").data().instance;
|
|
1819
|
+
const data = CT.serializeForm(form);
|
|
1820
|
+
Object.keys(data).forEach(key => {
|
|
1821
|
+
if(options.columns.find(col => col.name === key)?.update?.dontsave) {
|
|
1822
|
+
delete data[key];
|
|
1823
|
+
}
|
|
1824
|
+
});
|
|
1825
|
+
instance.update(data).catch(CT.onError);
|
|
1826
|
+
});
|
|
1827
|
+
}
|
|
1828
|
+
|
|
1829
|
+
if(options.features.delete) {
|
|
1830
|
+
view.el.find("button.delete-item").off("click").on("click",(event)=>{
|
|
1831
|
+
event.preventDefault();
|
|
1832
|
+
view.el.addClass("confirm-delete");
|
|
1833
|
+
const clearConfirmState = () => view.el.removeClass("confirm-delete");
|
|
1834
|
+
CT.runDeleteConfirm(
|
|
1835
|
+
{ item, view, options },
|
|
1836
|
+
() => {
|
|
1837
|
+
item.delete().catch(CT.onError).finally(clearConfirmState);
|
|
1838
|
+
},
|
|
1839
|
+
clearConfirmState
|
|
1840
|
+
);
|
|
1841
|
+
});
|
|
1842
|
+
}
|
|
1843
|
+
|
|
1844
|
+
if (typeof options.onRowFields === "function") {
|
|
1845
|
+
options.onRowFields(item, view, table);
|
|
1846
|
+
}
|
|
1847
|
+
};
|
|
1848
|
+
})(window.KGrid);
|
|
1849
|
+
|
|
1850
|
+
|
|
1851
|
+
/* --- init.js --- */
|
|
1852
|
+
(function (CT) {
|
|
1853
|
+
/**
|
|
1854
|
+
* Initialize a KGrid table inside a host element (DOM node or jQuery).
|
|
1855
|
+
* The host may be an empty element; KGrid builds the table DOM when none is present.
|
|
1856
|
+
*
|
|
1857
|
+
* @param {Element|JQuery} host
|
|
1858
|
+
* @param {Object} opts table configuration
|
|
1859
|
+
* @returns {Promise<KGridTable>} API: instance, filterForm, setInteraction, …
|
|
1860
|
+
*/
|
|
1861
|
+
CT.init = async function (host, opts) {
|
|
1862
|
+
const $host = CT.resolveHostElement(host);
|
|
1863
|
+
const options = { ...CT.protoOptions, ...opts };
|
|
1864
|
+
const table = CT.mountTableShell($host, options);
|
|
1865
|
+
CT.log("table config options", options);
|
|
1866
|
+
|
|
1867
|
+
const initialInteraction = CT.resolveDefaultInteraction(options);
|
|
1868
|
+
CT.applyInteraction($host, initialInteraction);
|
|
1869
|
+
|
|
1870
|
+
const api = {
|
|
1871
|
+
$host,
|
|
1872
|
+
instance: null,
|
|
1873
|
+
filterForm: null,
|
|
1874
|
+
find(sel) {
|
|
1875
|
+
return $host.find(sel);
|
|
1876
|
+
},
|
|
1877
|
+
setInteraction(mode, overrides) {
|
|
1878
|
+
CT.applyInteraction($host, mode, overrides);
|
|
1879
|
+
return api;
|
|
1880
|
+
},
|
|
1881
|
+
getInteraction() {
|
|
1882
|
+
const mode = CT.getTableInteractionHost($host).attr("data-interaction");
|
|
1883
|
+
return mode === "edit" ? "edit" : "view";
|
|
1884
|
+
},
|
|
1885
|
+
/** @deprecated prefer setInteraction('edit'|'view') */
|
|
1886
|
+
setEditMode(editMode) {
|
|
1887
|
+
if (typeof editMode !== "boolean") {
|
|
1888
|
+
throw new TypeError(
|
|
1889
|
+
"setEditMode(editMode) expects a boolean (true = edit, false = view)"
|
|
1890
|
+
);
|
|
1891
|
+
}
|
|
1892
|
+
return api.setInteraction(editMode ? "edit" : "view");
|
|
1893
|
+
},
|
|
1894
|
+
/**
|
|
1895
|
+
* @deprecated prefer setInteraction('edit'|'view')
|
|
1896
|
+
* Boolean: true = edit, false = view. No arg / Event: toggle.
|
|
1897
|
+
*/
|
|
1898
|
+
toggleEditMode(editMode) {
|
|
1899
|
+
if (
|
|
1900
|
+
editMode != null &&
|
|
1901
|
+
typeof editMode === "object" &&
|
|
1902
|
+
typeof editMode.preventDefault === "function"
|
|
1903
|
+
) {
|
|
1904
|
+
editMode = undefined;
|
|
1905
|
+
}
|
|
1906
|
+
if (typeof editMode === "boolean") {
|
|
1907
|
+
return api.setInteraction(editMode ? "edit" : "view");
|
|
1908
|
+
}
|
|
1909
|
+
return api.setInteraction(api.getInteraction() === "edit" ? "view" : "edit");
|
|
1910
|
+
},
|
|
1911
|
+
};
|
|
1912
|
+
|
|
1913
|
+
options.columns = options.columns.map((col) => CT.normalizeColumnConfig(col));
|
|
1914
|
+
|
|
1915
|
+
const handlers = options.handlers ?? {};
|
|
1916
|
+
delete options.handlers;
|
|
1917
|
+
options.columns.forEach((col) => {
|
|
1918
|
+
["insert", "update", "display"].forEach((mode) => {
|
|
1919
|
+
const events = col[mode].events;
|
|
1920
|
+
if (!events || !Array.isArray(events)) {
|
|
1921
|
+
throw new Error("Column " + col.name + " events are not an array");
|
|
1922
|
+
}
|
|
1923
|
+
events.forEach((event) => {
|
|
1924
|
+
if (typeof event.callback === "string") {
|
|
1925
|
+
const fn =
|
|
1926
|
+
handlers[event.callback] ||
|
|
1927
|
+
(options.functions && options.functions[event.callback]);
|
|
1928
|
+
if (!fn || typeof fn !== "function") {
|
|
1929
|
+
throw new Error(
|
|
1930
|
+
"Event callback function " +
|
|
1931
|
+
event.callback +
|
|
1932
|
+
" not found for column " +
|
|
1933
|
+
col.name +
|
|
1934
|
+
" or is not a function"
|
|
1935
|
+
);
|
|
1936
|
+
}
|
|
1937
|
+
event.callback = fn;
|
|
1938
|
+
} else if (typeof event.callback !== "function") {
|
|
1939
|
+
throw new Error(
|
|
1940
|
+
"Event callback must be a function for column " + col.name
|
|
1941
|
+
);
|
|
1942
|
+
}
|
|
1943
|
+
});
|
|
1944
|
+
});
|
|
1945
|
+
const display = col.display;
|
|
1946
|
+
if (display && display.events && Array.isArray(display.events)) {
|
|
1947
|
+
display.events.forEach((event) => {
|
|
1948
|
+
if (typeof event.callback === "string") {
|
|
1949
|
+
const fn =
|
|
1950
|
+
handlers[event.callback] ||
|
|
1951
|
+
(options.functions && options.functions[event.callback]);
|
|
1952
|
+
if (fn && typeof fn === "function") {
|
|
1953
|
+
event.callback = fn;
|
|
1954
|
+
}
|
|
1955
|
+
}
|
|
1956
|
+
});
|
|
1957
|
+
}
|
|
1958
|
+
});
|
|
1959
|
+
|
|
1960
|
+
const colMap = new Map();
|
|
1961
|
+
options.columns.forEach((col) => {
|
|
1962
|
+
colMap.set(col.name, col);
|
|
1963
|
+
});
|
|
1964
|
+
|
|
1965
|
+
const labelsRow = CT.setupLabelsHeader(table.find(".thead-labels"), options);
|
|
1966
|
+
|
|
1967
|
+
const hasActionColumn = CT.hasActionColumn(options);
|
|
1968
|
+
const visibleColumnsCount = labelsRow.find("th").length;
|
|
1969
|
+
const dataColumnCount = visibleColumnsCount - (hasActionColumn ? 1 : 0);
|
|
1970
|
+
CT.syncActionColumnColgroup(table, dataColumnCount, hasActionColumn);
|
|
1971
|
+
|
|
1972
|
+
const filterForm = options.filterForm ?? CT.setupFilterHeader(table, options);
|
|
1973
|
+
|
|
1974
|
+
let pagingFooter;
|
|
1975
|
+
if (options.features && options.features.paging) {
|
|
1976
|
+
pagingFooter = CT.setupPagingFooter(
|
|
1977
|
+
table.find(".paging-footer"),
|
|
1978
|
+
options,
|
|
1979
|
+
visibleColumnsCount
|
|
1980
|
+
);
|
|
1981
|
+
} else {
|
|
1982
|
+
pagingFooter = null;
|
|
1983
|
+
table.find(".paging-footer").remove();
|
|
1984
|
+
}
|
|
1985
|
+
|
|
1986
|
+
const noDataTbody = CT.setupNoDataTbody(
|
|
1987
|
+
table.find(".no-data-tbody"),
|
|
1988
|
+
options,
|
|
1989
|
+
visibleColumnsCount
|
|
1990
|
+
);
|
|
1991
|
+
|
|
1992
|
+
if (options.features && options.features.create) {
|
|
1993
|
+
CT.setupNewRecordForm(table, options, api);
|
|
1994
|
+
}
|
|
1995
|
+
|
|
1996
|
+
api.filterForm = new CT.FilterForm(filterForm);
|
|
1997
|
+
const dataBody = CT.setupDataBody(
|
|
1998
|
+
table.find(".main-tbody"),
|
|
1999
|
+
options,
|
|
2000
|
+
labelsRow,
|
|
2001
|
+
filterForm,
|
|
2002
|
+
pagingFooter,
|
|
2003
|
+
noDataTbody
|
|
2004
|
+
);
|
|
2005
|
+
|
|
2006
|
+
const KViewOptions = {
|
|
2007
|
+
dontload: true,
|
|
2008
|
+
setAttrAsId: options.setAttrAsId ?? false,
|
|
2009
|
+
itemListeners: {
|
|
2010
|
+
afterrender: (item) =>
|
|
2011
|
+
CT.setupEvents(item, api.find("table"), options, colMap),
|
|
2012
|
+
},
|
|
2013
|
+
};
|
|
2014
|
+
|
|
2015
|
+
const KViews = CT.getKViews(options.kviews);
|
|
2016
|
+
if (!KViews) {
|
|
2017
|
+
throw new Error(CT.KVIEWS_MISSING_MSG);
|
|
2018
|
+
}
|
|
2019
|
+
api.instance = await KViews.createCollectionInstance(dataBody, KViewOptions);
|
|
2020
|
+
|
|
2021
|
+
if (options.url) {
|
|
2022
|
+
api.instance.setUrl(options.url);
|
|
2023
|
+
if (options.deleteUrl) {
|
|
2024
|
+
CT.log("set deleteUrl", options.deleteUrl);
|
|
2025
|
+
api.instance.setUrl(options.deleteUrl, "delete");
|
|
2026
|
+
}
|
|
2027
|
+
if (options.updateUrl) {
|
|
2028
|
+
CT.log("set updateUrl", options.updateUrl);
|
|
2029
|
+
api.instance.setUrl(options.updateUrl, "update");
|
|
2030
|
+
}
|
|
2031
|
+
if (options.insertUrl) {
|
|
2032
|
+
api.instance.setUrl(options.insertUrl, "insert");
|
|
2033
|
+
}
|
|
2034
|
+
try {
|
|
2035
|
+
await api.instance.loadFromRemote();
|
|
2036
|
+
} catch (error) {
|
|
2037
|
+
CT.onError(error);
|
|
2038
|
+
}
|
|
2039
|
+
} else if (options.data && options.data.constructor === Array) {
|
|
2040
|
+
const dataCopy = options.data.map((item) => ({ attributes: item }));
|
|
2041
|
+
api.instance.loadFromData(dataCopy);
|
|
2042
|
+
} else {
|
|
2043
|
+
throw new Error("Invalid data: missing datasource url or data for table");
|
|
2044
|
+
}
|
|
2045
|
+
|
|
2046
|
+
CT.setupDefaultFilters(filterForm, options, api.instance);
|
|
2047
|
+
|
|
2048
|
+
return api;
|
|
2049
|
+
};
|
|
2050
|
+
})(window.KGrid);
|
|
2051
|
+
|