@logimaxx/kgrid 0.2.3 → 0.4.0
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/README.md +4 -1
- package/dist/kgrid.js +891 -67
- package/dist/kgrid.min.js +2 -2
- package/docs/ai-guide.md +15 -6
- package/docs/api.md +20 -4
- package/docs/configuration.md +32 -4
- package/docs/field-types.md +22 -0
- package/docs/release.md +180 -0
- package/docs/table-shell.md +1 -0
- package/integrations/maxxops.example.js +0 -1
- package/package.json +1 -1
- package/src/cells.js +19 -1
- package/src/column-chooser.js +155 -0
- package/src/configure.js +66 -15
- package/src/constants.js +13 -1
- package/src/data-body.js +47 -30
- package/src/dom.js +41 -5
- package/src/events.js +17 -0
- package/src/filters.js +37 -2
- package/src/init.js +67 -4
- package/src/insert-row.js +14 -7
- package/src/interaction.js +4 -1
- package/src/preferences.js +404 -0
- package/styles/table.css +94 -6
package/dist/kgrid.js
CHANGED
|
@@ -1,10 +1,73 @@
|
|
|
1
|
-
/*! @logimaxx/kgrid | (c) Logimaxx System SRL — proprietary | https://logimaxx.ro | built 2026-
|
|
1
|
+
/*! @logimaxx/kgrid | (c) Logimaxx System SRL — proprietary | https://logimaxx.ro | built 2026-09-02T08:17:18.911Z */
|
|
2
2
|
|
|
3
3
|
/* --- configure.js --- */
|
|
4
4
|
/**
|
|
5
5
|
* Host-app integration: call KGrid.configure({ ... }) before mounting tables.
|
|
6
6
|
*/
|
|
7
7
|
(function (CT) {
|
|
8
|
+
/**
|
|
9
|
+
* Flag is on when the API/form value is true, 1, or "1".
|
|
10
|
+
* @param {*} v
|
|
11
|
+
* @returns {boolean}
|
|
12
|
+
*/
|
|
13
|
+
CT.isFlagOn = function (v) {
|
|
14
|
+
return v === true || v === 1 || v === "1";
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Walk form.elements (includes fields associated via form="id").
|
|
19
|
+
* Checkboxes always emit "1" or "0" — FormData omits unchecked boxes.
|
|
20
|
+
* @param {HTMLFormElement} form
|
|
21
|
+
* @returns {Record<string, *>}
|
|
22
|
+
*/
|
|
23
|
+
function defaultSerializeForm(form) {
|
|
24
|
+
const out = {};
|
|
25
|
+
const els = form && form.elements;
|
|
26
|
+
if (!els) {
|
|
27
|
+
return out;
|
|
28
|
+
}
|
|
29
|
+
for (let i = 0; i < els.length; i++) {
|
|
30
|
+
const el = els[i];
|
|
31
|
+
const name = el.name;
|
|
32
|
+
if (!name || el.disabled) {
|
|
33
|
+
continue;
|
|
34
|
+
}
|
|
35
|
+
const type = el.type;
|
|
36
|
+
if (type === "submit" || type === "button" || type === "reset" || type === "file") {
|
|
37
|
+
continue;
|
|
38
|
+
}
|
|
39
|
+
if (type === "checkbox") {
|
|
40
|
+
out[name] = el.checked ? "1" : "0";
|
|
41
|
+
continue;
|
|
42
|
+
}
|
|
43
|
+
if (type === "radio") {
|
|
44
|
+
if (!el.checked) {
|
|
45
|
+
continue;
|
|
46
|
+
}
|
|
47
|
+
out[name] = el.value;
|
|
48
|
+
continue;
|
|
49
|
+
}
|
|
50
|
+
if (el.tagName === "SELECT" && el.multiple) {
|
|
51
|
+
out[name] = Array.from(el.selectedOptions).map(function (o) {
|
|
52
|
+
return o.value;
|
|
53
|
+
});
|
|
54
|
+
continue;
|
|
55
|
+
}
|
|
56
|
+
const value = el.value;
|
|
57
|
+
if (Object.prototype.hasOwnProperty.call(out, name)) {
|
|
58
|
+
if (!Array.isArray(out[name])) {
|
|
59
|
+
out[name] = [out[name]];
|
|
60
|
+
}
|
|
61
|
+
out[name].push(value);
|
|
62
|
+
} else {
|
|
63
|
+
out[name] = value;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
return out;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
CT.serializeFormDefault = defaultSerializeForm;
|
|
70
|
+
|
|
8
71
|
const defaultConfig = {
|
|
9
72
|
log: function () {},
|
|
10
73
|
onError: function (err) {
|
|
@@ -19,21 +82,7 @@
|
|
|
19
82
|
},
|
|
20
83
|
/** @type {((context: object, onConfirm: Function, onCancel?: Function) => void)|null} */
|
|
21
84
|
deleteConfirm: null,
|
|
22
|
-
serializeForm:
|
|
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
|
-
},
|
|
85
|
+
serializeForm: defaultSerializeForm,
|
|
37
86
|
/** @type {Record<string, Function|object>|null} name → wrapper fn or field type plugin */
|
|
38
87
|
customInputTypes: null,
|
|
39
88
|
/** @deprecated use customInputTypes */
|
|
@@ -42,6 +91,8 @@
|
|
|
42
91
|
kviews: null,
|
|
43
92
|
/** Default delay (ms) before filter submit; 0 = immediate. Applies to the resolved filter-submit event. */
|
|
44
93
|
filterDebounceMs: 300,
|
|
94
|
+
/** { get(key), set(key, value|null) } — null uses localStorage */
|
|
95
|
+
preferencesStorage: null,
|
|
45
96
|
};
|
|
46
97
|
|
|
47
98
|
CT._config = Object.assign({}, defaultConfig);
|
|
@@ -154,8 +205,16 @@
|
|
|
154
205
|
"paging": false,
|
|
155
206
|
"create": false,
|
|
156
207
|
"update": false,
|
|
157
|
-
"delete": false
|
|
208
|
+
"delete": false,
|
|
209
|
+
"clone": false,
|
|
210
|
+
"columnChooser": false
|
|
158
211
|
},
|
|
212
|
+
/** Persist key for layout (and filters). Required for localStorage. */
|
|
213
|
+
storageKey: null,
|
|
214
|
+
/** Extra suffix for saved filters only (e.g. company id). Layout ignores this. */
|
|
215
|
+
filterStorageScope: null,
|
|
216
|
+
columnChooserLabel: "Columns",
|
|
217
|
+
columnChooserResetLabel: "Reset columns",
|
|
159
218
|
"defaultInteraction": "view",
|
|
160
219
|
"insertFormRow": {
|
|
161
220
|
"position": "top"
|
|
@@ -182,6 +241,10 @@
|
|
|
182
241
|
label: null,
|
|
183
242
|
name: null,
|
|
184
243
|
hidden: false,
|
|
244
|
+
/** When true, column chooser cannot hide this column (still reorderable). */
|
|
245
|
+
locked: false,
|
|
246
|
+
/** Runtime: user hid this column via chooser. Not a schema flag. */
|
|
247
|
+
userHidden: false,
|
|
185
248
|
/** CSS class(es) on header/filter/data/insert cells (alias: columnClass) */
|
|
186
249
|
class: null,
|
|
187
250
|
columnClass: null,
|
|
@@ -360,7 +423,23 @@
|
|
|
360
423
|
if (!f) {
|
|
361
424
|
return false;
|
|
362
425
|
}
|
|
363
|
-
return !!(f.delete || f.update || f.create);
|
|
426
|
+
return !!(f.delete || f.update || f.create || f.clone);
|
|
427
|
+
};
|
|
428
|
+
|
|
429
|
+
/**
|
|
430
|
+
* Compact width for the row-actions column under table-layout:fixed
|
|
431
|
+
* (fixed layout ignores content; 1% caused overflow). Sized from max buttons
|
|
432
|
+
* shown in any mode (idle clone/delete vs editing save/cancel).
|
|
433
|
+
* @param {Object} [options]
|
|
434
|
+
* @returns {string} CSS width
|
|
435
|
+
*/
|
|
436
|
+
CT.actionColumnWidth = function (options) {
|
|
437
|
+
const f = (options && options.features) || {};
|
|
438
|
+
const idle = (f.clone ? 1 : 0) + (f.delete ? 1 : 0);
|
|
439
|
+
const editing = f.update ? 2 : 0;
|
|
440
|
+
const insert = f.create ? 1 : 0;
|
|
441
|
+
const n = Math.max(idle, editing, insert, 1);
|
|
442
|
+
return (2.5 * n + 0.75).toFixed(2) + "rem";
|
|
364
443
|
};
|
|
365
444
|
|
|
366
445
|
/**
|
|
@@ -368,18 +447,37 @@
|
|
|
368
447
|
* @param {JQuery} $table
|
|
369
448
|
* @param {number} dataColumnCount visible data columns (no row-actions)
|
|
370
449
|
* @param {boolean} hasActions
|
|
450
|
+
* @param {Object} [options] table options (for action column width)
|
|
371
451
|
*/
|
|
372
|
-
CT.syncActionColumnColgroup = function ($table, dataColumnCount, hasActions) {
|
|
452
|
+
CT.syncActionColumnColgroup = function ($table, dataColumnCount, hasActions, options, layoutColumns) {
|
|
373
453
|
let $colgroup = $table.children("colgroup.kgrid-colgroup");
|
|
374
454
|
if (!$colgroup.length) {
|
|
375
455
|
$colgroup = $("<colgroup>").addClass("kgrid-colgroup").prependTo($table);
|
|
376
456
|
}
|
|
377
457
|
$colgroup.empty();
|
|
378
|
-
|
|
379
|
-
|
|
458
|
+
const named = Array.isArray(layoutColumns) ? layoutColumns : null;
|
|
459
|
+
if (named && named.length) {
|
|
460
|
+
named.forEach(function (col) {
|
|
461
|
+
const $col = $("<col>");
|
|
462
|
+
if (col && col.name) {
|
|
463
|
+
$col.attr("data-name", col.name);
|
|
464
|
+
}
|
|
465
|
+
if (col && col.userHidden) {
|
|
466
|
+
$col.addClass("kgrid-user-hidden");
|
|
467
|
+
}
|
|
468
|
+
$colgroup.append($col);
|
|
469
|
+
});
|
|
470
|
+
} else {
|
|
471
|
+
for (let i = 0; i < dataColumnCount; i++) {
|
|
472
|
+
$colgroup.append($("<col>"));
|
|
473
|
+
}
|
|
380
474
|
}
|
|
381
475
|
if (hasActions) {
|
|
382
|
-
$colgroup.append(
|
|
476
|
+
$colgroup.append(
|
|
477
|
+
$("<col>")
|
|
478
|
+
.addClass("kgrid-row-actions-col")
|
|
479
|
+
.css("width", CT.actionColumnWidth(options))
|
|
480
|
+
);
|
|
383
481
|
}
|
|
384
482
|
};
|
|
385
483
|
|
|
@@ -438,6 +536,7 @@
|
|
|
438
536
|
if (cls) {
|
|
439
537
|
$el.addClass(cls);
|
|
440
538
|
}
|
|
539
|
+
$el.toggleClass("kgrid-user-hidden", !!col.userHidden);
|
|
441
540
|
return $el;
|
|
442
541
|
};
|
|
443
542
|
})(window.KGrid);
|
|
@@ -896,8 +995,11 @@
|
|
|
896
995
|
if (overrides.delete !== undefined) {
|
|
897
996
|
$shell.attr("data-allow-delete", overrides.delete ? "true" : "false");
|
|
898
997
|
}
|
|
998
|
+
if (overrides.clone !== undefined) {
|
|
999
|
+
$shell.attr("data-allow-clone", overrides.clone ? "true" : "false");
|
|
1000
|
+
}
|
|
899
1001
|
} else if (interaction === "edit") {
|
|
900
|
-
$shell.removeAttr("data-allow-insert data-allow-update data-allow-delete");
|
|
1002
|
+
$shell.removeAttr("data-allow-insert data-allow-update data-allow-delete data-allow-clone");
|
|
901
1003
|
}
|
|
902
1004
|
};
|
|
903
1005
|
})(window.KGrid);
|
|
@@ -1102,8 +1204,9 @@
|
|
|
1102
1204
|
* @param {HTMLFormElement|JQuery} filterFormEl
|
|
1103
1205
|
* @param {Object} options
|
|
1104
1206
|
* @param {Object} [collection] KViews collection
|
|
1207
|
+
* @param {{ skipInitSubmit?: boolean }} [extra]
|
|
1105
1208
|
*/
|
|
1106
|
-
CT.setupDefaultFilters = function (filterFormEl, options, collection) {
|
|
1209
|
+
CT.setupDefaultFilters = function (filterFormEl, options, collection, extra) {
|
|
1107
1210
|
const form = $(filterFormEl)[0];
|
|
1108
1211
|
if (!form || !options || !options.features || !options.features.filtering) {
|
|
1109
1212
|
return;
|
|
@@ -1164,7 +1267,7 @@
|
|
|
1164
1267
|
const missing = persisted.some(function (col) {
|
|
1165
1268
|
return urlFilter.indexOf(col.name + "=") === -1;
|
|
1166
1269
|
});
|
|
1167
|
-
if (missing) {
|
|
1270
|
+
if (missing && !(extra && extra.skipInitSubmit)) {
|
|
1168
1271
|
collection.filtering.handleSubmit(form);
|
|
1169
1272
|
}
|
|
1170
1273
|
};
|
|
@@ -1319,6 +1422,40 @@
|
|
|
1319
1422
|
return filterForm;
|
|
1320
1423
|
};
|
|
1321
1424
|
|
|
1425
|
+
/** Write current form fields into collection.url.parameters.filter (same rules as KViews Filtering). */
|
|
1426
|
+
CT.syncFormFiltersToCollectionUrl = function (form, collection) {
|
|
1427
|
+
if (!form || !collection || !collection.url) {
|
|
1428
|
+
return;
|
|
1429
|
+
}
|
|
1430
|
+
if (!collection.url.parameters) {
|
|
1431
|
+
collection.url.parameters = {};
|
|
1432
|
+
}
|
|
1433
|
+
const filter = [];
|
|
1434
|
+
const els = form.elements;
|
|
1435
|
+
if (!els) {
|
|
1436
|
+
return;
|
|
1437
|
+
}
|
|
1438
|
+
for (let i = 0; i < els.length; i++) {
|
|
1439
|
+
const el = els[i];
|
|
1440
|
+
if (!el || !el.name) {
|
|
1441
|
+
continue;
|
|
1442
|
+
}
|
|
1443
|
+
const $el = $(el);
|
|
1444
|
+
const value = $el.val();
|
|
1445
|
+
if (value == null || value === "") {
|
|
1446
|
+
continue;
|
|
1447
|
+
}
|
|
1448
|
+
const operator = $el.attr("data-operator") || $el.data("operator") || "=";
|
|
1449
|
+
filter.push(el.name + operator + value);
|
|
1450
|
+
}
|
|
1451
|
+
collection.offset = 0;
|
|
1452
|
+
if (filter.length) {
|
|
1453
|
+
collection.url.parameters.filter = filter.join(",");
|
|
1454
|
+
} else {
|
|
1455
|
+
delete collection.url.parameters.filter;
|
|
1456
|
+
}
|
|
1457
|
+
};
|
|
1458
|
+
|
|
1322
1459
|
CT.FilterForm = function (form) {
|
|
1323
1460
|
this.form = $(form)[0];
|
|
1324
1461
|
this.filter = (name, value, operator = "~=~") => {
|
|
@@ -1354,6 +1491,19 @@
|
|
|
1354
1491
|
|
|
1355
1492
|
/* --- cells.js --- */
|
|
1356
1493
|
(function (CT) {
|
|
1494
|
+
/**
|
|
1495
|
+
* Bootstrap switch wrapper around a checkbox used as a boolean flag.
|
|
1496
|
+
* @param {JQuery} $input
|
|
1497
|
+
* @returns {JQuery} wrapper
|
|
1498
|
+
*/
|
|
1499
|
+
CT.wrapFlagSwitch = function ($input) {
|
|
1500
|
+
$input
|
|
1501
|
+
.attr({ type: "checkbox", role: "switch", value: "1" })
|
|
1502
|
+
.removeClass("form-control form-input form-control-sm")
|
|
1503
|
+
.addClass("form-check-input");
|
|
1504
|
+
return $("<div class='form-check form-switch kgrid-flag-switch mb-0'>").append($input);
|
|
1505
|
+
};
|
|
1506
|
+
|
|
1357
1507
|
/**
|
|
1358
1508
|
* Setup data cell
|
|
1359
1509
|
* @param {Object} col
|
|
@@ -1423,6 +1573,10 @@
|
|
|
1423
1573
|
$("<div>").addClass("cell-input").append(tpl).appendTo($cell);
|
|
1424
1574
|
return $cell;
|
|
1425
1575
|
}
|
|
1576
|
+
case "checkbox":
|
|
1577
|
+
input = $("<input autocomplete='off' type='checkbox' class='form-check-input'/>");
|
|
1578
|
+
skipValueAttr = true;
|
|
1579
|
+
break;
|
|
1426
1580
|
default:
|
|
1427
1581
|
if(CT.isValidInputType(updateType)) {
|
|
1428
1582
|
input = $(`<input autocomplete='off' type='${updateType}' class='form-input form-control form-control-sm'/>`);
|
|
@@ -1462,7 +1616,8 @@
|
|
|
1462
1616
|
if(!updateConfig.disabled && !updateConfig.readonly)
|
|
1463
1617
|
input.attr("onchange","$(this).parents('tr').addClass('editing')");
|
|
1464
1618
|
|
|
1465
|
-
$
|
|
1619
|
+
const $control = updateType === "checkbox" ? CT.wrapFlagSwitch(input) : input;
|
|
1620
|
+
$("<div>").addClass("cell-input").append($control).appendTo($cell);
|
|
1466
1621
|
return $cell;
|
|
1467
1622
|
};
|
|
1468
1623
|
})(window.KGrid);
|
|
@@ -1480,38 +1635,16 @@
|
|
|
1480
1635
|
return td;
|
|
1481
1636
|
};
|
|
1482
1637
|
|
|
1483
|
-
|
|
1638
|
+
/**
|
|
1639
|
+
* Fill a data <tr> with cells + optional row-actions (KViews item template).
|
|
1640
|
+
* @param {JQuery} dataRow
|
|
1641
|
+
* @param {Object} options
|
|
1642
|
+
* @returns {JQuery} dataRow
|
|
1643
|
+
*/
|
|
1644
|
+
CT.fillDataRow = function (dataRow, options) {
|
|
1484
1645
|
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
1646
|
const dataRowFormId = "data_row_form_"+CT.uuid()+"_{{this.id}}";
|
|
1514
|
-
const editForm = options.features.update
|
|
1647
|
+
const editForm = options.features && options.features.update
|
|
1515
1648
|
? $("<form class='edit-form table-row-form'>").attr("id", dataRowFormId)
|
|
1516
1649
|
: null;
|
|
1517
1650
|
|
|
@@ -1524,6 +1657,13 @@
|
|
|
1524
1657
|
|
|
1525
1658
|
if (CT.hasActionColumn(options)) {
|
|
1526
1659
|
const buttonColumn = $("<td>").addClass("kgrid-row-actions").appendTo(dataRow);
|
|
1660
|
+
if(options.features.clone) {
|
|
1661
|
+
$("<div>").addClass("btn-group clone-item-grp").appendTo(buttonColumn).append(
|
|
1662
|
+
$("<button>").addClass("btn btn-sm btn-outline-secondary clone-item")
|
|
1663
|
+
.attr("type","button")
|
|
1664
|
+
.attr("title","Clone item")
|
|
1665
|
+
.append("<i class='fa-regular fa-copy'></i>"));
|
|
1666
|
+
}
|
|
1527
1667
|
if(options.features.delete) {
|
|
1528
1668
|
$("<div>").addClass("btn-group delete-item-grp").appendTo(buttonColumn).append(
|
|
1529
1669
|
$("<button>").addClass("btn btn-sm btn-danger delete-item")
|
|
@@ -1554,7 +1694,39 @@
|
|
|
1554
1694
|
if (editForm) {
|
|
1555
1695
|
CT.anchorRowForm(editForm, dataRow);
|
|
1556
1696
|
}
|
|
1697
|
+
return dataRow;
|
|
1698
|
+
};
|
|
1699
|
+
|
|
1700
|
+
CT.setupDataBody = function (dataBody, options, labelsRow, filterForm, pagingFooter, noDataTbody) {
|
|
1701
|
+
const dataRow = $("<tr>").appendTo(dataBody);
|
|
1702
|
+
if(options.dataRowAttrs && typeof options.dataRowAttrs!=="object") {
|
|
1703
|
+
throw new Error("options.dataRowAttrs must be an object");
|
|
1704
|
+
}
|
|
1705
|
+
const dataRowAttrs = {...(options.dataRowAttrs ?? {})};
|
|
1706
|
+
Object.keys(dataRowAttrs).forEach(attr => dataRow.attr(attr,dataRowAttrs[attr]));
|
|
1707
|
+
|
|
1708
|
+
{
|
|
1709
|
+
dataBody.data("emptyview",noDataTbody);
|
|
1710
|
+
|
|
1711
|
+
if(options?.features?.sorting) {
|
|
1712
|
+
dataBody.data("sort",labelsRow);
|
|
1713
|
+
}
|
|
1714
|
+
if(pagingFooter) {
|
|
1715
|
+
dataBody.data("paging",pagingFooter.find(".pages"))
|
|
1716
|
+
.data("pagesizeinp",pagingFooter.find(".pagesize"))
|
|
1717
|
+
.data("totalrecscount",pagingFooter.find(".totalrecscount"));
|
|
1718
|
+
}
|
|
1719
|
+
|
|
1720
|
+
if(filterForm) {
|
|
1721
|
+
dataBody.data("filter",filterForm);
|
|
1722
|
+
}
|
|
1723
|
+
|
|
1724
|
+
if(options.type) {
|
|
1725
|
+
dataBody.data("type",options.type);
|
|
1726
|
+
}
|
|
1727
|
+
}
|
|
1557
1728
|
|
|
1729
|
+
CT.fillDataRow(dataRow, options);
|
|
1558
1730
|
return dataBody;
|
|
1559
1731
|
};
|
|
1560
1732
|
|
|
@@ -1667,6 +1839,10 @@
|
|
|
1667
1839
|
case "hidden":
|
|
1668
1840
|
input = $(`<input type='hidden' class='form-input form-control form-control-sm'/>`);
|
|
1669
1841
|
break;
|
|
1842
|
+
case "checkbox":
|
|
1843
|
+
input = $("<input autocomplete='off' type='checkbox' class='form-check-input'/>");
|
|
1844
|
+
input.attr("data-type", insertType);
|
|
1845
|
+
break;
|
|
1670
1846
|
default:
|
|
1671
1847
|
if(CT.isValidInputType(insertType)) {
|
|
1672
1848
|
input = $(`<input autocomplete='off' type='${insertType}' class='form-input form-control form-control-sm'/>`);
|
|
@@ -1677,12 +1853,14 @@
|
|
|
1677
1853
|
}
|
|
1678
1854
|
}
|
|
1679
1855
|
|
|
1680
|
-
|
|
1681
|
-
|
|
1682
|
-
|
|
1683
|
-
|
|
1684
|
-
|
|
1685
|
-
|
|
1856
|
+
const rawDefault = (typeof insertConfig.default === "object" && insertConfig.default && insertConfig.default.value != null)
|
|
1857
|
+
? insertConfig.default.value
|
|
1858
|
+
: insertConfig.default;
|
|
1859
|
+
if (insertType === "checkbox") {
|
|
1860
|
+
input.prop("checked", CT.isFlagOn(rawDefault));
|
|
1861
|
+
} else if(insertConfig.default != null && insertConfig.default !== "" && !input.val()) {
|
|
1862
|
+
if(String(rawDefault).trim()) {
|
|
1863
|
+
input.val(rawDefault).trigger("change");
|
|
1686
1864
|
}
|
|
1687
1865
|
}
|
|
1688
1866
|
|
|
@@ -1705,7 +1883,8 @@
|
|
|
1705
1883
|
}
|
|
1706
1884
|
|
|
1707
1885
|
if(insertConfig.type!=="hidden") {
|
|
1708
|
-
const $
|
|
1886
|
+
const $control = insertType === "checkbox" ? CT.wrapFlagSwitch(input) : input;
|
|
1887
|
+
const $td = $("<td>").append($control).appendTo(newRecordRow).attr("data-label", col.label);
|
|
1709
1888
|
CT.applyColumnCellMeta($td, col);
|
|
1710
1889
|
}
|
|
1711
1890
|
CT.mountField({
|
|
@@ -1812,6 +1991,14 @@
|
|
|
1812
1991
|
$(input).val(typeof val === "boolean" ? String(val) : val);
|
|
1813
1992
|
});
|
|
1814
1993
|
|
|
1994
|
+
view.el.find("input[type='checkbox']").each((index, input) => {
|
|
1995
|
+
const col = colMap.get(input.name);
|
|
1996
|
+
if (!col) {
|
|
1997
|
+
return;
|
|
1998
|
+
}
|
|
1999
|
+
input.checked = CT.isFlagOn(item.attributes[col.name]);
|
|
2000
|
+
});
|
|
2001
|
+
|
|
1815
2002
|
view.el.find("form.edit-form").off("submit").on("submit",(event)=>{
|
|
1816
2003
|
const form = event.target;
|
|
1817
2004
|
event.preventDefault();
|
|
@@ -1826,6 +2013,15 @@
|
|
|
1826
2013
|
});
|
|
1827
2014
|
}
|
|
1828
2015
|
|
|
2016
|
+
if(options.features.clone) {
|
|
2017
|
+
view.el.find("button.clone-item").off("click").on("click",(event)=>{
|
|
2018
|
+
event.preventDefault();
|
|
2019
|
+
if (typeof options.onClone === "function") {
|
|
2020
|
+
options.onClone(item, view, event);
|
|
2021
|
+
}
|
|
2022
|
+
});
|
|
2023
|
+
}
|
|
2024
|
+
|
|
1829
2025
|
if(options.features.delete) {
|
|
1830
2026
|
view.el.find("button.delete-item").off("click").on("click",(event)=>{
|
|
1831
2027
|
event.preventDefault();
|
|
@@ -1848,6 +2044,571 @@
|
|
|
1848
2044
|
})(window.KGrid);
|
|
1849
2045
|
|
|
1850
2046
|
|
|
2047
|
+
/* --- preferences.js --- */
|
|
2048
|
+
/**
|
|
2049
|
+
* User preferences: column layout (order + hide) and filter values.
|
|
2050
|
+
* Layout is keyed by storageKey; filters also use filterStorageScope (e.g. company).
|
|
2051
|
+
*/
|
|
2052
|
+
(function (CT) {
|
|
2053
|
+
CT.PREFERENCES_VERSION = 1;
|
|
2054
|
+
|
|
2055
|
+
CT.localStoragePreferences = {
|
|
2056
|
+
get: function (key) {
|
|
2057
|
+
try {
|
|
2058
|
+
const root = typeof window !== "undefined" ? window : globalThis;
|
|
2059
|
+
const ls = root && root.localStorage;
|
|
2060
|
+
if (!ls) {
|
|
2061
|
+
return null;
|
|
2062
|
+
}
|
|
2063
|
+
const raw = ls.getItem(key);
|
|
2064
|
+
return raw ? JSON.parse(raw) : null;
|
|
2065
|
+
} catch (err) {
|
|
2066
|
+
return null;
|
|
2067
|
+
}
|
|
2068
|
+
},
|
|
2069
|
+
set: function (key, value) {
|
|
2070
|
+
try {
|
|
2071
|
+
const root = typeof window !== "undefined" ? window : globalThis;
|
|
2072
|
+
const ls = root && root.localStorage;
|
|
2073
|
+
if (!ls) {
|
|
2074
|
+
return;
|
|
2075
|
+
}
|
|
2076
|
+
if (value == null) {
|
|
2077
|
+
ls.removeItem(key);
|
|
2078
|
+
return;
|
|
2079
|
+
}
|
|
2080
|
+
ls.setItem(key, JSON.stringify(value));
|
|
2081
|
+
} catch (err) {
|
|
2082
|
+
/* quota / private mode */
|
|
2083
|
+
}
|
|
2084
|
+
},
|
|
2085
|
+
};
|
|
2086
|
+
|
|
2087
|
+
CT.preferencesStorageFor = function (options) {
|
|
2088
|
+
if (options && options.preferencesStorage) {
|
|
2089
|
+
return options.preferencesStorage;
|
|
2090
|
+
}
|
|
2091
|
+
if (CT._config && CT._config.preferencesStorage) {
|
|
2092
|
+
return CT._config.preferencesStorage;
|
|
2093
|
+
}
|
|
2094
|
+
return CT.localStoragePreferences;
|
|
2095
|
+
};
|
|
2096
|
+
|
|
2097
|
+
CT.layoutStorageKey = function (storageKey) {
|
|
2098
|
+
return "kgrid:" + storageKey + ":layout";
|
|
2099
|
+
};
|
|
2100
|
+
|
|
2101
|
+
CT.filtersStorageKey = function (storageKey, scope) {
|
|
2102
|
+
let key = "kgrid:" + storageKey + ":filters";
|
|
2103
|
+
if (scope != null && String(scope) !== "") {
|
|
2104
|
+
key += ":" + String(scope);
|
|
2105
|
+
}
|
|
2106
|
+
return key;
|
|
2107
|
+
};
|
|
2108
|
+
|
|
2109
|
+
CT.chooserColumns = function (columns) {
|
|
2110
|
+
return (columns || []).filter(function (col) {
|
|
2111
|
+
return col && col.name && !col.hidden;
|
|
2112
|
+
});
|
|
2113
|
+
};
|
|
2114
|
+
|
|
2115
|
+
CT.layoutFromColumns = function (columns) {
|
|
2116
|
+
return {
|
|
2117
|
+
v: CT.PREFERENCES_VERSION,
|
|
2118
|
+
columns: CT.chooserColumns(columns).map(function (col) {
|
|
2119
|
+
return { name: col.name, hidden: !!col.userHidden };
|
|
2120
|
+
}),
|
|
2121
|
+
};
|
|
2122
|
+
};
|
|
2123
|
+
|
|
2124
|
+
CT.parseFilterExpression = function (part) {
|
|
2125
|
+
const s = String(part || "");
|
|
2126
|
+
const ops = ["~=~", ">=", "<=", "!=", "=", ">", "<"];
|
|
2127
|
+
for (let i = 0; i < ops.length; i++) {
|
|
2128
|
+
const op = ops[i];
|
|
2129
|
+
const idx = s.indexOf(op);
|
|
2130
|
+
if (idx > 0) {
|
|
2131
|
+
return {
|
|
2132
|
+
name: s.slice(0, idx),
|
|
2133
|
+
operator: op,
|
|
2134
|
+
value: s.slice(idx + op.length),
|
|
2135
|
+
};
|
|
2136
|
+
}
|
|
2137
|
+
}
|
|
2138
|
+
return null;
|
|
2139
|
+
};
|
|
2140
|
+
|
|
2141
|
+
CT.isUserFilterColumn = function (col) {
|
|
2142
|
+
return !!(
|
|
2143
|
+
col &&
|
|
2144
|
+
col.name &&
|
|
2145
|
+
!col.hidden &&
|
|
2146
|
+
(!col.features || col.features.filter !== false)
|
|
2147
|
+
);
|
|
2148
|
+
};
|
|
2149
|
+
|
|
2150
|
+
CT.readUserFilters = function (form, columns) {
|
|
2151
|
+
if (!form) {
|
|
2152
|
+
return [];
|
|
2153
|
+
}
|
|
2154
|
+
const out = [];
|
|
2155
|
+
(columns || []).forEach(function (col) {
|
|
2156
|
+
if (!CT.isUserFilterColumn(col)) {
|
|
2157
|
+
return;
|
|
2158
|
+
}
|
|
2159
|
+
const $field = CT.filterFormField(form, col.name);
|
|
2160
|
+
if (!$field.length) {
|
|
2161
|
+
return;
|
|
2162
|
+
}
|
|
2163
|
+
const value = $field.val();
|
|
2164
|
+
if (value == null || value === "" || (Array.isArray(value) && !value.length)) {
|
|
2165
|
+
return;
|
|
2166
|
+
}
|
|
2167
|
+
const operator =
|
|
2168
|
+
$field.attr("data-operator") ||
|
|
2169
|
+
(col.filter && col.filter.operator) ||
|
|
2170
|
+
"~=~";
|
|
2171
|
+
out.push({
|
|
2172
|
+
name: col.name,
|
|
2173
|
+
value: Array.isArray(value) ? value : String(value),
|
|
2174
|
+
operator: operator,
|
|
2175
|
+
});
|
|
2176
|
+
});
|
|
2177
|
+
return out;
|
|
2178
|
+
};
|
|
2179
|
+
|
|
2180
|
+
CT.applyUserFilters = function (form, columns, saved) {
|
|
2181
|
+
if (!form || !saved || !Array.isArray(saved.filters)) {
|
|
2182
|
+
return;
|
|
2183
|
+
}
|
|
2184
|
+
saved.filters.forEach(function (entry) {
|
|
2185
|
+
if (!entry || !entry.name) {
|
|
2186
|
+
return;
|
|
2187
|
+
}
|
|
2188
|
+
const col = (columns || []).find(function (c) {
|
|
2189
|
+
return c && c.name === entry.name;
|
|
2190
|
+
});
|
|
2191
|
+
if (!CT.isUserFilterColumn(col)) {
|
|
2192
|
+
return;
|
|
2193
|
+
}
|
|
2194
|
+
if (entry.value == null || entry.value === "") {
|
|
2195
|
+
return;
|
|
2196
|
+
}
|
|
2197
|
+
let $field = CT.filterFormField(form, entry.name);
|
|
2198
|
+
if (!$field.length) {
|
|
2199
|
+
CT.ensureFilterField(form, entry.name, entry.value, entry.operator);
|
|
2200
|
+
$field = CT.filterFormField(form, entry.name);
|
|
2201
|
+
}
|
|
2202
|
+
if (!$field.length) {
|
|
2203
|
+
return;
|
|
2204
|
+
}
|
|
2205
|
+
$field.val(entry.value);
|
|
2206
|
+
if (entry.operator) {
|
|
2207
|
+
$field.attr("data-operator", entry.operator);
|
|
2208
|
+
}
|
|
2209
|
+
});
|
|
2210
|
+
};
|
|
2211
|
+
|
|
2212
|
+
/** Copy URL filter parts onto the form when the field is missing or empty. */
|
|
2213
|
+
CT.ensureUrlFiltersOnForm = function (form, collection) {
|
|
2214
|
+
if (!form || !collection || !collection.url || !collection.url.parameters) {
|
|
2215
|
+
return;
|
|
2216
|
+
}
|
|
2217
|
+
const urlFilter = String(collection.url.parameters.filter || "");
|
|
2218
|
+
if (!urlFilter) {
|
|
2219
|
+
return;
|
|
2220
|
+
}
|
|
2221
|
+
urlFilter.split(",").forEach(function (part) {
|
|
2222
|
+
const parsed = CT.parseFilterExpression(part.trim());
|
|
2223
|
+
if (!parsed || !parsed.name || parsed.value === "") {
|
|
2224
|
+
return;
|
|
2225
|
+
}
|
|
2226
|
+
const $existing = CT.filterFormField(form, parsed.name);
|
|
2227
|
+
if ($existing.length && $existing.val()) {
|
|
2228
|
+
return;
|
|
2229
|
+
}
|
|
2230
|
+
if ($existing.length) {
|
|
2231
|
+
$existing.val(parsed.value);
|
|
2232
|
+
$existing.attr("data-operator", parsed.operator);
|
|
2233
|
+
return;
|
|
2234
|
+
}
|
|
2235
|
+
CT.ensureFilterField(form, parsed.name, parsed.value, parsed.operator);
|
|
2236
|
+
});
|
|
2237
|
+
};
|
|
2238
|
+
|
|
2239
|
+
CT.preferencesLoadLayout = function (options) {
|
|
2240
|
+
if (!options || !options.storageKey) {
|
|
2241
|
+
return null;
|
|
2242
|
+
}
|
|
2243
|
+
const stored = CT.preferencesStorageFor(options).get(
|
|
2244
|
+
CT.layoutStorageKey(options.storageKey)
|
|
2245
|
+
);
|
|
2246
|
+
if (!stored || stored.v !== CT.PREFERENCES_VERSION || !Array.isArray(stored.columns)) {
|
|
2247
|
+
return null;
|
|
2248
|
+
}
|
|
2249
|
+
return stored;
|
|
2250
|
+
};
|
|
2251
|
+
|
|
2252
|
+
CT.preferencesSaveLayout = function (options, layout) {
|
|
2253
|
+
if (!options || !options.storageKey) {
|
|
2254
|
+
return;
|
|
2255
|
+
}
|
|
2256
|
+
CT.preferencesStorageFor(options).set(
|
|
2257
|
+
CT.layoutStorageKey(options.storageKey),
|
|
2258
|
+
layout
|
|
2259
|
+
);
|
|
2260
|
+
};
|
|
2261
|
+
|
|
2262
|
+
CT.preferencesLoadFilters = function (options) {
|
|
2263
|
+
if (!options || !options.storageKey) {
|
|
2264
|
+
return null;
|
|
2265
|
+
}
|
|
2266
|
+
const stored = CT.preferencesStorageFor(options).get(
|
|
2267
|
+
CT.filtersStorageKey(options.storageKey, options.filterStorageScope)
|
|
2268
|
+
);
|
|
2269
|
+
if (!stored || stored.v !== CT.PREFERENCES_VERSION || !Array.isArray(stored.filters)) {
|
|
2270
|
+
return null;
|
|
2271
|
+
}
|
|
2272
|
+
return stored;
|
|
2273
|
+
};
|
|
2274
|
+
|
|
2275
|
+
CT.preferencesSaveFilters = function (options, filters) {
|
|
2276
|
+
if (!options || !options.storageKey) {
|
|
2277
|
+
return;
|
|
2278
|
+
}
|
|
2279
|
+
CT.preferencesStorageFor(options).set(
|
|
2280
|
+
CT.filtersStorageKey(options.storageKey, options.filterStorageScope),
|
|
2281
|
+
{ v: CT.PREFERENCES_VERSION, filters: filters || [] }
|
|
2282
|
+
);
|
|
2283
|
+
};
|
|
2284
|
+
|
|
2285
|
+
CT.reorderColumns = function (columns, orderedVisibleNames) {
|
|
2286
|
+
const byName = new Map();
|
|
2287
|
+
const schemaHidden = [];
|
|
2288
|
+
(columns || []).forEach(function (col, i) {
|
|
2289
|
+
if (col && col.hidden) {
|
|
2290
|
+
schemaHidden.push({ col: col, index: i });
|
|
2291
|
+
return;
|
|
2292
|
+
}
|
|
2293
|
+
if (col && col.name) {
|
|
2294
|
+
byName.set(col.name, col);
|
|
2295
|
+
}
|
|
2296
|
+
});
|
|
2297
|
+
const visible = [];
|
|
2298
|
+
(orderedVisibleNames || []).forEach(function (name) {
|
|
2299
|
+
if (byName.has(name)) {
|
|
2300
|
+
visible.push(byName.get(name));
|
|
2301
|
+
byName.delete(name);
|
|
2302
|
+
}
|
|
2303
|
+
});
|
|
2304
|
+
byName.forEach(function (col) {
|
|
2305
|
+
visible.push(col);
|
|
2306
|
+
});
|
|
2307
|
+
const result = visible.slice();
|
|
2308
|
+
schemaHidden.forEach(function (entry) {
|
|
2309
|
+
result.splice(Math.min(entry.index, result.length), 0, entry.col);
|
|
2310
|
+
});
|
|
2311
|
+
return result;
|
|
2312
|
+
};
|
|
2313
|
+
|
|
2314
|
+
CT.mergeLayoutIntoColumns = function (columns, layout) {
|
|
2315
|
+
const list = (columns || []).slice();
|
|
2316
|
+
list.forEach(function (col) {
|
|
2317
|
+
if (col && !col.hidden) {
|
|
2318
|
+
col.userHidden = false;
|
|
2319
|
+
}
|
|
2320
|
+
});
|
|
2321
|
+
const visible = CT.chooserColumns(list);
|
|
2322
|
+
const byName = new Map(
|
|
2323
|
+
visible.map(function (col) {
|
|
2324
|
+
return [col.name, col];
|
|
2325
|
+
})
|
|
2326
|
+
);
|
|
2327
|
+
const orderedNames = [];
|
|
2328
|
+
const used = new Set();
|
|
2329
|
+
const saved = layout && Array.isArray(layout.columns) ? layout.columns : [];
|
|
2330
|
+
saved.forEach(function (entry) {
|
|
2331
|
+
if (!entry || !entry.name || !byName.has(entry.name)) {
|
|
2332
|
+
return;
|
|
2333
|
+
}
|
|
2334
|
+
const col = byName.get(entry.name);
|
|
2335
|
+
col.userHidden = !!entry.hidden && !col.locked;
|
|
2336
|
+
orderedNames.push(entry.name);
|
|
2337
|
+
used.add(entry.name);
|
|
2338
|
+
});
|
|
2339
|
+
visible.forEach(function (col) {
|
|
2340
|
+
if (!used.has(col.name)) {
|
|
2341
|
+
orderedNames.push(col.name);
|
|
2342
|
+
}
|
|
2343
|
+
});
|
|
2344
|
+
const merged = CT.reorderColumns(list, orderedNames);
|
|
2345
|
+
const chooser = CT.chooserColumns(merged);
|
|
2346
|
+
if (chooser.length && chooser.every(function (col) { return col.userHidden; })) {
|
|
2347
|
+
const unlock = chooser.find(function (col) { return !col.locked; }) || chooser[0];
|
|
2348
|
+
unlock.userHidden = false;
|
|
2349
|
+
}
|
|
2350
|
+
return merged;
|
|
2351
|
+
};
|
|
2352
|
+
|
|
2353
|
+
CT.applyLayoutToDom = function ($table, columns, options) {
|
|
2354
|
+
if (!$table || !$table.length) {
|
|
2355
|
+
return;
|
|
2356
|
+
}
|
|
2357
|
+
const order = CT.chooserColumns(columns).map(function (col) {
|
|
2358
|
+
return col.name;
|
|
2359
|
+
});
|
|
2360
|
+
const hidden = {};
|
|
2361
|
+
CT.chooserColumns(columns).forEach(function (col) {
|
|
2362
|
+
if (col.userHidden) {
|
|
2363
|
+
hidden[col.name] = true;
|
|
2364
|
+
}
|
|
2365
|
+
});
|
|
2366
|
+
const $rows = $table.find(
|
|
2367
|
+
".thead-labels tr, .thead-filters tr, .before-main-tbody tr, .main-tbody tr, .after-main-tbody tr"
|
|
2368
|
+
);
|
|
2369
|
+
$rows.each(function () {
|
|
2370
|
+
const $row = $(this);
|
|
2371
|
+
const $action = $row.children(".kgrid-row-actions");
|
|
2372
|
+
const byName = {};
|
|
2373
|
+
$row.children("[data-name]").each(function () {
|
|
2374
|
+
byName[this.getAttribute("data-name")] = this;
|
|
2375
|
+
});
|
|
2376
|
+
order.forEach(function (name) {
|
|
2377
|
+
const el = byName[name];
|
|
2378
|
+
if (!el) {
|
|
2379
|
+
return;
|
|
2380
|
+
}
|
|
2381
|
+
if ($action.length) {
|
|
2382
|
+
$(el).insertBefore($action);
|
|
2383
|
+
} else {
|
|
2384
|
+
$row.append(el);
|
|
2385
|
+
}
|
|
2386
|
+
el.classList.toggle("kgrid-user-hidden", !!hidden[name]);
|
|
2387
|
+
});
|
|
2388
|
+
});
|
|
2389
|
+
const hasActions = $table.find(".kgrid-row-actions").length > 0;
|
|
2390
|
+
CT.syncActionColumnColgroup(
|
|
2391
|
+
$table,
|
|
2392
|
+
order.length,
|
|
2393
|
+
hasActions,
|
|
2394
|
+
options,
|
|
2395
|
+
CT.chooserColumns(columns)
|
|
2396
|
+
);
|
|
2397
|
+
};
|
|
2398
|
+
|
|
2399
|
+
CT.refreshCollectionTemplate = function (api, options) {
|
|
2400
|
+
if (!api || !api.instance || typeof CT.fillDataRow !== "function") {
|
|
2401
|
+
return;
|
|
2402
|
+
}
|
|
2403
|
+
const KViews = CT.getKViews(options && options.kviews);
|
|
2404
|
+
if (!KViews || typeof KViews.template !== "function") {
|
|
2405
|
+
return;
|
|
2406
|
+
}
|
|
2407
|
+
const $tr = $("<tr>");
|
|
2408
|
+
if (options.dataRowAttrs && CT.isPlainObject(options.dataRowAttrs)) {
|
|
2409
|
+
Object.keys(options.dataRowAttrs).forEach(function (attr) {
|
|
2410
|
+
$tr.attr(attr, options.dataRowAttrs[attr]);
|
|
2411
|
+
});
|
|
2412
|
+
}
|
|
2413
|
+
CT.fillDataRow($tr, options);
|
|
2414
|
+
const html = $("<div>").append($tr).html();
|
|
2415
|
+
const compiled = KViews.template(html);
|
|
2416
|
+
api.instance.template = compiled;
|
|
2417
|
+
(api.instance.items || []).forEach(function (item) {
|
|
2418
|
+
(item.views || []).forEach(function (view) {
|
|
2419
|
+
view.template = compiled;
|
|
2420
|
+
});
|
|
2421
|
+
});
|
|
2422
|
+
};
|
|
2423
|
+
|
|
2424
|
+
CT.setupFilterPersistence = function (form, options) {
|
|
2425
|
+
if (!form || !options || !options.storageKey) {
|
|
2426
|
+
return;
|
|
2427
|
+
}
|
|
2428
|
+
if (!options.features || !options.features.filtering) {
|
|
2429
|
+
return;
|
|
2430
|
+
}
|
|
2431
|
+
$(form)
|
|
2432
|
+
.off("submit.kgridFilterPrefs")
|
|
2433
|
+
.on("submit.kgridFilterPrefs", function () {
|
|
2434
|
+
CT.preferencesSaveFilters(options, CT.readUserFilters(form, options.columns));
|
|
2435
|
+
});
|
|
2436
|
+
};
|
|
2437
|
+
|
|
2438
|
+
CT.applyColumnLayout = function (api, options, layout) {
|
|
2439
|
+
options.columns = CT.mergeLayoutIntoColumns(options.columns, layout);
|
|
2440
|
+
const $table = api.$host.find("table").first();
|
|
2441
|
+
CT.applyLayoutToDom($table, options.columns, options);
|
|
2442
|
+
CT.refreshCollectionTemplate(api, options);
|
|
2443
|
+
if (typeof CT.renderColumnChooserPanel === "function") {
|
|
2444
|
+
const $panel = api.$host.find(".kgrid-column-chooser-panel");
|
|
2445
|
+
if ($panel.length) {
|
|
2446
|
+
CT.renderColumnChooserPanel($panel, options, api);
|
|
2447
|
+
}
|
|
2448
|
+
}
|
|
2449
|
+
return CT.layoutFromColumns(options.columns);
|
|
2450
|
+
};
|
|
2451
|
+
})(window.KGrid);
|
|
2452
|
+
|
|
2453
|
+
|
|
2454
|
+
/* --- column-chooser.js --- */
|
|
2455
|
+
/**
|
|
2456
|
+
* Column chooser: checkbox visibility + HTML5 drag reorder.
|
|
2457
|
+
*/
|
|
2458
|
+
(function (CT) {
|
|
2459
|
+
function layoutFromPanel($panel) {
|
|
2460
|
+
const columns = [];
|
|
2461
|
+
$panel.find(".kgrid-column-chooser-list li[data-name]").each(function () {
|
|
2462
|
+
const name = this.getAttribute("data-name");
|
|
2463
|
+
const checked = $(this).find("input[type='checkbox']").prop("checked");
|
|
2464
|
+
columns.push({ name: name, hidden: !checked });
|
|
2465
|
+
});
|
|
2466
|
+
return { v: CT.PREFERENCES_VERSION, columns: columns };
|
|
2467
|
+
}
|
|
2468
|
+
|
|
2469
|
+
function visibleCount(layout) {
|
|
2470
|
+
return layout.columns.filter(function (c) {
|
|
2471
|
+
return !c.hidden;
|
|
2472
|
+
}).length;
|
|
2473
|
+
}
|
|
2474
|
+
|
|
2475
|
+
CT.renderColumnChooserPanel = function ($panel, options, api) {
|
|
2476
|
+
$panel.empty();
|
|
2477
|
+
const $list = $("<ul>").addClass("kgrid-column-chooser-list").appendTo($panel);
|
|
2478
|
+
CT.chooserColumns(options.columns).forEach(function (col) {
|
|
2479
|
+
const $li = $("<li>")
|
|
2480
|
+
.attr("draggable", "true")
|
|
2481
|
+
.attr("data-name", col.name)
|
|
2482
|
+
.appendTo($list);
|
|
2483
|
+
$("<span>")
|
|
2484
|
+
.addClass("kgrid-column-chooser-handle")
|
|
2485
|
+
.attr("aria-hidden", "true")
|
|
2486
|
+
.text("⋮⋮")
|
|
2487
|
+
.appendTo($li);
|
|
2488
|
+
const $label = $("<label>").appendTo($li);
|
|
2489
|
+
const $cb = $("<input>")
|
|
2490
|
+
.attr("type", "checkbox")
|
|
2491
|
+
.prop("checked", !col.userHidden)
|
|
2492
|
+
.appendTo($label);
|
|
2493
|
+
if (col.locked) {
|
|
2494
|
+
$cb.prop("disabled", true).attr("title", "This column cannot be hidden");
|
|
2495
|
+
}
|
|
2496
|
+
$label.append(document.createTextNode(" " + (col.label || col.name)));
|
|
2497
|
+
});
|
|
2498
|
+
$("<button>")
|
|
2499
|
+
.attr("type", "button")
|
|
2500
|
+
.addClass("btn btn-sm btn-outline-secondary kgrid-column-chooser-reset")
|
|
2501
|
+
.text(options.columnChooserResetLabel || "Reset columns")
|
|
2502
|
+
.appendTo($panel);
|
|
2503
|
+
|
|
2504
|
+
$panel
|
|
2505
|
+
.off("change.kgridChooser")
|
|
2506
|
+
.on("change.kgridChooser", "input[type='checkbox']", function () {
|
|
2507
|
+
const layout = layoutFromPanel($panel);
|
|
2508
|
+
if (visibleCount(layout) < 1) {
|
|
2509
|
+
this.checked = true;
|
|
2510
|
+
return;
|
|
2511
|
+
}
|
|
2512
|
+
api.setLayout(layout);
|
|
2513
|
+
});
|
|
2514
|
+
|
|
2515
|
+
$panel
|
|
2516
|
+
.off("click.kgridChooserReset")
|
|
2517
|
+
.on("click.kgridChooserReset", ".kgrid-column-chooser-reset", function () {
|
|
2518
|
+
api.resetLayout();
|
|
2519
|
+
});
|
|
2520
|
+
|
|
2521
|
+
let dragEl = null;
|
|
2522
|
+
$list.off(".kgridChooserDrag");
|
|
2523
|
+
$list.on("dragstart.kgridChooserDrag", "li", function (e) {
|
|
2524
|
+
dragEl = this;
|
|
2525
|
+
$(this).addClass("kgrid-column-chooser-dragging");
|
|
2526
|
+
if (e.originalEvent && e.originalEvent.dataTransfer) {
|
|
2527
|
+
e.originalEvent.dataTransfer.effectAllowed = "move";
|
|
2528
|
+
e.originalEvent.dataTransfer.setData("text/plain", this.getAttribute("data-name"));
|
|
2529
|
+
}
|
|
2530
|
+
});
|
|
2531
|
+
$list.on("dragend.kgridChooserDrag", "li", function () {
|
|
2532
|
+
$(this).removeClass("kgrid-column-chooser-dragging");
|
|
2533
|
+
dragEl = null;
|
|
2534
|
+
});
|
|
2535
|
+
$list.on("dragover.kgridChooserDrag", "li", function (e) {
|
|
2536
|
+
e.preventDefault();
|
|
2537
|
+
if (!dragEl || dragEl === this) {
|
|
2538
|
+
return;
|
|
2539
|
+
}
|
|
2540
|
+
const rect = this.getBoundingClientRect();
|
|
2541
|
+
const mid = rect.top + rect.height / 2;
|
|
2542
|
+
const clientY = e.originalEvent ? e.originalEvent.clientY : 0;
|
|
2543
|
+
if (clientY < mid) {
|
|
2544
|
+
this.parentNode.insertBefore(dragEl, this);
|
|
2545
|
+
} else {
|
|
2546
|
+
this.parentNode.insertBefore(dragEl, this.nextSibling);
|
|
2547
|
+
}
|
|
2548
|
+
});
|
|
2549
|
+
$list.on("drop.kgridChooserDrag", "li", function (e) {
|
|
2550
|
+
e.preventDefault();
|
|
2551
|
+
api.setLayout(layoutFromPanel($panel));
|
|
2552
|
+
});
|
|
2553
|
+
};
|
|
2554
|
+
|
|
2555
|
+
CT.setupColumnChooser = function ($host, options, api) {
|
|
2556
|
+
if (!options.features || !options.features.columnChooser) {
|
|
2557
|
+
return;
|
|
2558
|
+
}
|
|
2559
|
+
const $shell = CT.getTableInteractionHost($host);
|
|
2560
|
+
$shell.find(".kgrid-column-chooser").remove();
|
|
2561
|
+
const label = options.columnChooserLabel || "Columns";
|
|
2562
|
+
const $wrap = $("<div>").addClass("kgrid-column-chooser");
|
|
2563
|
+
const $btn = $("<button>")
|
|
2564
|
+
.attr("type", "button")
|
|
2565
|
+
.addClass("kgrid-column-chooser-toggle btn btn-sm btn-outline-secondary")
|
|
2566
|
+
.attr("title", label)
|
|
2567
|
+
.attr("aria-expanded", "false")
|
|
2568
|
+
.attr("aria-haspopup", "true")
|
|
2569
|
+
.html(
|
|
2570
|
+
'<i class="fas fa-table-columns" aria-hidden="true"></i>' +
|
|
2571
|
+
'<span class="kgrid-column-chooser-toggle-label"> ' +
|
|
2572
|
+
$("<div>").text(label).html() +
|
|
2573
|
+
"</span>"
|
|
2574
|
+
);
|
|
2575
|
+
const $panel = $("<div>")
|
|
2576
|
+
.addClass("kgrid-column-chooser-panel")
|
|
2577
|
+
.attr("hidden", "hidden");
|
|
2578
|
+
$wrap.append($btn, $panel);
|
|
2579
|
+
$shell.prepend($wrap);
|
|
2580
|
+
CT.renderColumnChooserPanel($panel, options, api);
|
|
2581
|
+
|
|
2582
|
+
function close() {
|
|
2583
|
+
$panel.attr("hidden", "hidden");
|
|
2584
|
+
$btn.attr("aria-expanded", "false");
|
|
2585
|
+
}
|
|
2586
|
+
|
|
2587
|
+
$btn.on("click", function (e) {
|
|
2588
|
+
e.stopPropagation();
|
|
2589
|
+
const open = !$panel.attr("hidden");
|
|
2590
|
+
if (open) {
|
|
2591
|
+
close();
|
|
2592
|
+
} else {
|
|
2593
|
+
$panel.removeAttr("hidden");
|
|
2594
|
+
$btn.attr("aria-expanded", "true");
|
|
2595
|
+
}
|
|
2596
|
+
});
|
|
2597
|
+
|
|
2598
|
+
const ns = "kgridChooser" + CT.uuid();
|
|
2599
|
+
$(document).on("pointerdown." + ns, function (e) {
|
|
2600
|
+
if (!document.body.contains($wrap[0])) {
|
|
2601
|
+
$(document).off("pointerdown." + ns);
|
|
2602
|
+
return;
|
|
2603
|
+
}
|
|
2604
|
+
if (!$wrap[0].contains(e.target)) {
|
|
2605
|
+
close();
|
|
2606
|
+
}
|
|
2607
|
+
});
|
|
2608
|
+
};
|
|
2609
|
+
})(window.KGrid);
|
|
2610
|
+
|
|
2611
|
+
|
|
1851
2612
|
/* --- init.js --- */
|
|
1852
2613
|
(function (CT) {
|
|
1853
2614
|
/**
|
|
@@ -1908,12 +2669,54 @@
|
|
|
1908
2669
|
}
|
|
1909
2670
|
return api.setInteraction(api.getInteraction() === "edit" ? "view" : "edit");
|
|
1910
2671
|
},
|
|
2672
|
+
getLayout() {
|
|
2673
|
+
return CT.layoutFromColumns(options.columns);
|
|
2674
|
+
},
|
|
2675
|
+
setLayout(layout) {
|
|
2676
|
+
CT.applyColumnLayout(api, options, layout);
|
|
2677
|
+
CT.preferencesSaveLayout(options, api.getLayout());
|
|
2678
|
+
return api.getLayout();
|
|
2679
|
+
},
|
|
2680
|
+
resetLayout() {
|
|
2681
|
+
const byName = new Map();
|
|
2682
|
+
options.columns.forEach(function (col) {
|
|
2683
|
+
if (col && col.name) {
|
|
2684
|
+
byName.set(col.name, col);
|
|
2685
|
+
}
|
|
2686
|
+
});
|
|
2687
|
+
options.columns = schemaOrder
|
|
2688
|
+
.map(function (name) {
|
|
2689
|
+
return byName.get(name);
|
|
2690
|
+
})
|
|
2691
|
+
.filter(Boolean);
|
|
2692
|
+
CT.preferencesSaveLayout(options, null);
|
|
2693
|
+
CT.applyColumnLayout(api, options, null);
|
|
2694
|
+
return api.getLayout();
|
|
2695
|
+
},
|
|
1911
2696
|
};
|
|
1912
2697
|
|
|
1913
|
-
options.columns = options.columns.map((col) => CT.normalizeColumnConfig(col));
|
|
2698
|
+
options.columns = (options.columns || []).map((col) => CT.normalizeColumnConfig(col));
|
|
2699
|
+
const schemaOrder = options.columns.map(function (col) {
|
|
2700
|
+
return col.name;
|
|
2701
|
+
});
|
|
2702
|
+
options.columns = CT.mergeLayoutIntoColumns(
|
|
2703
|
+
options.columns,
|
|
2704
|
+
CT.preferencesLoadLayout(options)
|
|
2705
|
+
);
|
|
1914
2706
|
|
|
1915
2707
|
const handlers = options.handlers ?? {};
|
|
1916
2708
|
delete options.handlers;
|
|
2709
|
+
if (typeof options.onClone === "string") {
|
|
2710
|
+
const fn =
|
|
2711
|
+
handlers[options.onClone] ||
|
|
2712
|
+
(options.functions && options.functions[options.onClone]);
|
|
2713
|
+
if (!fn || typeof fn !== "function") {
|
|
2714
|
+
throw new Error(
|
|
2715
|
+
"onClone handler " + options.onClone + " not found or is not a function"
|
|
2716
|
+
);
|
|
2717
|
+
}
|
|
2718
|
+
options.onClone = fn;
|
|
2719
|
+
}
|
|
1917
2720
|
options.columns.forEach((col) => {
|
|
1918
2721
|
["insert", "update", "display"].forEach((mode) => {
|
|
1919
2722
|
const events = col[mode].events;
|
|
@@ -1967,7 +2770,13 @@
|
|
|
1967
2770
|
const hasActionColumn = CT.hasActionColumn(options);
|
|
1968
2771
|
const visibleColumnsCount = labelsRow.find("th").length;
|
|
1969
2772
|
const dataColumnCount = visibleColumnsCount - (hasActionColumn ? 1 : 0);
|
|
1970
|
-
CT.syncActionColumnColgroup(
|
|
2773
|
+
CT.syncActionColumnColgroup(
|
|
2774
|
+
table,
|
|
2775
|
+
dataColumnCount,
|
|
2776
|
+
hasActionColumn,
|
|
2777
|
+
options,
|
|
2778
|
+
CT.chooserColumns(options.columns)
|
|
2779
|
+
);
|
|
1971
2780
|
|
|
1972
2781
|
const filterForm = options.filterForm ?? CT.setupFilterHeader(table, options);
|
|
1973
2782
|
|
|
@@ -2031,7 +2840,24 @@
|
|
|
2031
2840
|
if (options.insertUrl) {
|
|
2032
2841
|
api.instance.setUrl(options.insertUrl, "insert");
|
|
2033
2842
|
}
|
|
2843
|
+
}
|
|
2844
|
+
|
|
2845
|
+
CT.setupDefaultFilters(filterForm, options, api.instance, { skipInitSubmit: true });
|
|
2846
|
+
const filterFormEl = filterForm && (filterForm.jquery ? filterForm[0] : filterForm);
|
|
2847
|
+
CT.applyUserFilters(
|
|
2848
|
+
filterFormEl,
|
|
2849
|
+
options.columns,
|
|
2850
|
+
CT.preferencesLoadFilters(options)
|
|
2851
|
+
);
|
|
2852
|
+
CT.ensureUrlFiltersOnForm(filterFormEl, api.instance);
|
|
2853
|
+
CT.setupFilterPersistence(filterFormEl, options);
|
|
2854
|
+
CT.setupColumnChooser($host, options, api);
|
|
2855
|
+
|
|
2856
|
+
if (options.url) {
|
|
2034
2857
|
try {
|
|
2858
|
+
if (options.features && options.features.filtering && filterFormEl) {
|
|
2859
|
+
CT.syncFormFiltersToCollectionUrl(filterFormEl, api.instance);
|
|
2860
|
+
}
|
|
2035
2861
|
await api.instance.loadFromRemote();
|
|
2036
2862
|
} catch (error) {
|
|
2037
2863
|
CT.onError(error);
|
|
@@ -2043,8 +2869,6 @@
|
|
|
2043
2869
|
throw new Error("Invalid data: missing datasource url or data for table");
|
|
2044
2870
|
}
|
|
2045
2871
|
|
|
2046
|
-
CT.setupDefaultFilters(filterForm, options, api.instance);
|
|
2047
|
-
|
|
2048
2872
|
return api;
|
|
2049
2873
|
};
|
|
2050
2874
|
})(window.KGrid);
|