@logimaxx/kgrid 0.3.1 → 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.
@@ -0,0 +1,404 @@
1
+ /**
2
+ * User preferences: column layout (order + hide) and filter values.
3
+ * Layout is keyed by storageKey; filters also use filterStorageScope (e.g. company).
4
+ */
5
+ (function (CT) {
6
+ CT.PREFERENCES_VERSION = 1;
7
+
8
+ CT.localStoragePreferences = {
9
+ get: function (key) {
10
+ try {
11
+ const root = typeof window !== "undefined" ? window : globalThis;
12
+ const ls = root && root.localStorage;
13
+ if (!ls) {
14
+ return null;
15
+ }
16
+ const raw = ls.getItem(key);
17
+ return raw ? JSON.parse(raw) : null;
18
+ } catch (err) {
19
+ return null;
20
+ }
21
+ },
22
+ set: function (key, value) {
23
+ try {
24
+ const root = typeof window !== "undefined" ? window : globalThis;
25
+ const ls = root && root.localStorage;
26
+ if (!ls) {
27
+ return;
28
+ }
29
+ if (value == null) {
30
+ ls.removeItem(key);
31
+ return;
32
+ }
33
+ ls.setItem(key, JSON.stringify(value));
34
+ } catch (err) {
35
+ /* quota / private mode */
36
+ }
37
+ },
38
+ };
39
+
40
+ CT.preferencesStorageFor = function (options) {
41
+ if (options && options.preferencesStorage) {
42
+ return options.preferencesStorage;
43
+ }
44
+ if (CT._config && CT._config.preferencesStorage) {
45
+ return CT._config.preferencesStorage;
46
+ }
47
+ return CT.localStoragePreferences;
48
+ };
49
+
50
+ CT.layoutStorageKey = function (storageKey) {
51
+ return "kgrid:" + storageKey + ":layout";
52
+ };
53
+
54
+ CT.filtersStorageKey = function (storageKey, scope) {
55
+ let key = "kgrid:" + storageKey + ":filters";
56
+ if (scope != null && String(scope) !== "") {
57
+ key += ":" + String(scope);
58
+ }
59
+ return key;
60
+ };
61
+
62
+ CT.chooserColumns = function (columns) {
63
+ return (columns || []).filter(function (col) {
64
+ return col && col.name && !col.hidden;
65
+ });
66
+ };
67
+
68
+ CT.layoutFromColumns = function (columns) {
69
+ return {
70
+ v: CT.PREFERENCES_VERSION,
71
+ columns: CT.chooserColumns(columns).map(function (col) {
72
+ return { name: col.name, hidden: !!col.userHidden };
73
+ }),
74
+ };
75
+ };
76
+
77
+ CT.parseFilterExpression = function (part) {
78
+ const s = String(part || "");
79
+ const ops = ["~=~", ">=", "<=", "!=", "=", ">", "<"];
80
+ for (let i = 0; i < ops.length; i++) {
81
+ const op = ops[i];
82
+ const idx = s.indexOf(op);
83
+ if (idx > 0) {
84
+ return {
85
+ name: s.slice(0, idx),
86
+ operator: op,
87
+ value: s.slice(idx + op.length),
88
+ };
89
+ }
90
+ }
91
+ return null;
92
+ };
93
+
94
+ CT.isUserFilterColumn = function (col) {
95
+ return !!(
96
+ col &&
97
+ col.name &&
98
+ !col.hidden &&
99
+ (!col.features || col.features.filter !== false)
100
+ );
101
+ };
102
+
103
+ CT.readUserFilters = function (form, columns) {
104
+ if (!form) {
105
+ return [];
106
+ }
107
+ const out = [];
108
+ (columns || []).forEach(function (col) {
109
+ if (!CT.isUserFilterColumn(col)) {
110
+ return;
111
+ }
112
+ const $field = CT.filterFormField(form, col.name);
113
+ if (!$field.length) {
114
+ return;
115
+ }
116
+ const value = $field.val();
117
+ if (value == null || value === "" || (Array.isArray(value) && !value.length)) {
118
+ return;
119
+ }
120
+ const operator =
121
+ $field.attr("data-operator") ||
122
+ (col.filter && col.filter.operator) ||
123
+ "~=~";
124
+ out.push({
125
+ name: col.name,
126
+ value: Array.isArray(value) ? value : String(value),
127
+ operator: operator,
128
+ });
129
+ });
130
+ return out;
131
+ };
132
+
133
+ CT.applyUserFilters = function (form, columns, saved) {
134
+ if (!form || !saved || !Array.isArray(saved.filters)) {
135
+ return;
136
+ }
137
+ saved.filters.forEach(function (entry) {
138
+ if (!entry || !entry.name) {
139
+ return;
140
+ }
141
+ const col = (columns || []).find(function (c) {
142
+ return c && c.name === entry.name;
143
+ });
144
+ if (!CT.isUserFilterColumn(col)) {
145
+ return;
146
+ }
147
+ if (entry.value == null || entry.value === "") {
148
+ return;
149
+ }
150
+ let $field = CT.filterFormField(form, entry.name);
151
+ if (!$field.length) {
152
+ CT.ensureFilterField(form, entry.name, entry.value, entry.operator);
153
+ $field = CT.filterFormField(form, entry.name);
154
+ }
155
+ if (!$field.length) {
156
+ return;
157
+ }
158
+ $field.val(entry.value);
159
+ if (entry.operator) {
160
+ $field.attr("data-operator", entry.operator);
161
+ }
162
+ });
163
+ };
164
+
165
+ /** Copy URL filter parts onto the form when the field is missing or empty. */
166
+ CT.ensureUrlFiltersOnForm = function (form, collection) {
167
+ if (!form || !collection || !collection.url || !collection.url.parameters) {
168
+ return;
169
+ }
170
+ const urlFilter = String(collection.url.parameters.filter || "");
171
+ if (!urlFilter) {
172
+ return;
173
+ }
174
+ urlFilter.split(",").forEach(function (part) {
175
+ const parsed = CT.parseFilterExpression(part.trim());
176
+ if (!parsed || !parsed.name || parsed.value === "") {
177
+ return;
178
+ }
179
+ const $existing = CT.filterFormField(form, parsed.name);
180
+ if ($existing.length && $existing.val()) {
181
+ return;
182
+ }
183
+ if ($existing.length) {
184
+ $existing.val(parsed.value);
185
+ $existing.attr("data-operator", parsed.operator);
186
+ return;
187
+ }
188
+ CT.ensureFilterField(form, parsed.name, parsed.value, parsed.operator);
189
+ });
190
+ };
191
+
192
+ CT.preferencesLoadLayout = function (options) {
193
+ if (!options || !options.storageKey) {
194
+ return null;
195
+ }
196
+ const stored = CT.preferencesStorageFor(options).get(
197
+ CT.layoutStorageKey(options.storageKey)
198
+ );
199
+ if (!stored || stored.v !== CT.PREFERENCES_VERSION || !Array.isArray(stored.columns)) {
200
+ return null;
201
+ }
202
+ return stored;
203
+ };
204
+
205
+ CT.preferencesSaveLayout = function (options, layout) {
206
+ if (!options || !options.storageKey) {
207
+ return;
208
+ }
209
+ CT.preferencesStorageFor(options).set(
210
+ CT.layoutStorageKey(options.storageKey),
211
+ layout
212
+ );
213
+ };
214
+
215
+ CT.preferencesLoadFilters = function (options) {
216
+ if (!options || !options.storageKey) {
217
+ return null;
218
+ }
219
+ const stored = CT.preferencesStorageFor(options).get(
220
+ CT.filtersStorageKey(options.storageKey, options.filterStorageScope)
221
+ );
222
+ if (!stored || stored.v !== CT.PREFERENCES_VERSION || !Array.isArray(stored.filters)) {
223
+ return null;
224
+ }
225
+ return stored;
226
+ };
227
+
228
+ CT.preferencesSaveFilters = function (options, filters) {
229
+ if (!options || !options.storageKey) {
230
+ return;
231
+ }
232
+ CT.preferencesStorageFor(options).set(
233
+ CT.filtersStorageKey(options.storageKey, options.filterStorageScope),
234
+ { v: CT.PREFERENCES_VERSION, filters: filters || [] }
235
+ );
236
+ };
237
+
238
+ CT.reorderColumns = function (columns, orderedVisibleNames) {
239
+ const byName = new Map();
240
+ const schemaHidden = [];
241
+ (columns || []).forEach(function (col, i) {
242
+ if (col && col.hidden) {
243
+ schemaHidden.push({ col: col, index: i });
244
+ return;
245
+ }
246
+ if (col && col.name) {
247
+ byName.set(col.name, col);
248
+ }
249
+ });
250
+ const visible = [];
251
+ (orderedVisibleNames || []).forEach(function (name) {
252
+ if (byName.has(name)) {
253
+ visible.push(byName.get(name));
254
+ byName.delete(name);
255
+ }
256
+ });
257
+ byName.forEach(function (col) {
258
+ visible.push(col);
259
+ });
260
+ const result = visible.slice();
261
+ schemaHidden.forEach(function (entry) {
262
+ result.splice(Math.min(entry.index, result.length), 0, entry.col);
263
+ });
264
+ return result;
265
+ };
266
+
267
+ CT.mergeLayoutIntoColumns = function (columns, layout) {
268
+ const list = (columns || []).slice();
269
+ list.forEach(function (col) {
270
+ if (col && !col.hidden) {
271
+ col.userHidden = false;
272
+ }
273
+ });
274
+ const visible = CT.chooserColumns(list);
275
+ const byName = new Map(
276
+ visible.map(function (col) {
277
+ return [col.name, col];
278
+ })
279
+ );
280
+ const orderedNames = [];
281
+ const used = new Set();
282
+ const saved = layout && Array.isArray(layout.columns) ? layout.columns : [];
283
+ saved.forEach(function (entry) {
284
+ if (!entry || !entry.name || !byName.has(entry.name)) {
285
+ return;
286
+ }
287
+ const col = byName.get(entry.name);
288
+ col.userHidden = !!entry.hidden && !col.locked;
289
+ orderedNames.push(entry.name);
290
+ used.add(entry.name);
291
+ });
292
+ visible.forEach(function (col) {
293
+ if (!used.has(col.name)) {
294
+ orderedNames.push(col.name);
295
+ }
296
+ });
297
+ const merged = CT.reorderColumns(list, orderedNames);
298
+ const chooser = CT.chooserColumns(merged);
299
+ if (chooser.length && chooser.every(function (col) { return col.userHidden; })) {
300
+ const unlock = chooser.find(function (col) { return !col.locked; }) || chooser[0];
301
+ unlock.userHidden = false;
302
+ }
303
+ return merged;
304
+ };
305
+
306
+ CT.applyLayoutToDom = function ($table, columns, options) {
307
+ if (!$table || !$table.length) {
308
+ return;
309
+ }
310
+ const order = CT.chooserColumns(columns).map(function (col) {
311
+ return col.name;
312
+ });
313
+ const hidden = {};
314
+ CT.chooserColumns(columns).forEach(function (col) {
315
+ if (col.userHidden) {
316
+ hidden[col.name] = true;
317
+ }
318
+ });
319
+ const $rows = $table.find(
320
+ ".thead-labels tr, .thead-filters tr, .before-main-tbody tr, .main-tbody tr, .after-main-tbody tr"
321
+ );
322
+ $rows.each(function () {
323
+ const $row = $(this);
324
+ const $action = $row.children(".kgrid-row-actions");
325
+ const byName = {};
326
+ $row.children("[data-name]").each(function () {
327
+ byName[this.getAttribute("data-name")] = this;
328
+ });
329
+ order.forEach(function (name) {
330
+ const el = byName[name];
331
+ if (!el) {
332
+ return;
333
+ }
334
+ if ($action.length) {
335
+ $(el).insertBefore($action);
336
+ } else {
337
+ $row.append(el);
338
+ }
339
+ el.classList.toggle("kgrid-user-hidden", !!hidden[name]);
340
+ });
341
+ });
342
+ const hasActions = $table.find(".kgrid-row-actions").length > 0;
343
+ CT.syncActionColumnColgroup(
344
+ $table,
345
+ order.length,
346
+ hasActions,
347
+ options,
348
+ CT.chooserColumns(columns)
349
+ );
350
+ };
351
+
352
+ CT.refreshCollectionTemplate = function (api, options) {
353
+ if (!api || !api.instance || typeof CT.fillDataRow !== "function") {
354
+ return;
355
+ }
356
+ const KViews = CT.getKViews(options && options.kviews);
357
+ if (!KViews || typeof KViews.template !== "function") {
358
+ return;
359
+ }
360
+ const $tr = $("<tr>");
361
+ if (options.dataRowAttrs && CT.isPlainObject(options.dataRowAttrs)) {
362
+ Object.keys(options.dataRowAttrs).forEach(function (attr) {
363
+ $tr.attr(attr, options.dataRowAttrs[attr]);
364
+ });
365
+ }
366
+ CT.fillDataRow($tr, options);
367
+ const html = $("<div>").append($tr).html();
368
+ const compiled = KViews.template(html);
369
+ api.instance.template = compiled;
370
+ (api.instance.items || []).forEach(function (item) {
371
+ (item.views || []).forEach(function (view) {
372
+ view.template = compiled;
373
+ });
374
+ });
375
+ };
376
+
377
+ CT.setupFilterPersistence = function (form, options) {
378
+ if (!form || !options || !options.storageKey) {
379
+ return;
380
+ }
381
+ if (!options.features || !options.features.filtering) {
382
+ return;
383
+ }
384
+ $(form)
385
+ .off("submit.kgridFilterPrefs")
386
+ .on("submit.kgridFilterPrefs", function () {
387
+ CT.preferencesSaveFilters(options, CT.readUserFilters(form, options.columns));
388
+ });
389
+ };
390
+
391
+ CT.applyColumnLayout = function (api, options, layout) {
392
+ options.columns = CT.mergeLayoutIntoColumns(options.columns, layout);
393
+ const $table = api.$host.find("table").first();
394
+ CT.applyLayoutToDom($table, options.columns, options);
395
+ CT.refreshCollectionTemplate(api, options);
396
+ if (typeof CT.renderColumnChooserPanel === "function") {
397
+ const $panel = api.$host.find(".kgrid-column-chooser-panel");
398
+ if ($panel.length) {
399
+ CT.renderColumnChooserPanel($panel, options, api);
400
+ }
401
+ }
402
+ return CT.layoutFromColumns(options.columns);
403
+ };
404
+ })(window.KGrid);
package/styles/table.css CHANGED
@@ -54,6 +54,14 @@
54
54
  max-width: 100%;
55
55
  box-sizing: border-box;
56
56
  }
57
+ .custom-table-shell td .kgrid-flag-switch .form-check-input {
58
+ width: 2em;
59
+ max-width: 2em;
60
+ }
61
+ .custom-table-shell th.kgrid-user-hidden,
62
+ .custom-table-shell td.kgrid-user-hidden {
63
+ display: none;
64
+ }
57
65
  }
58
66
 
59
67
  tr.new-record-row{
@@ -93,6 +101,16 @@ div.save-item{
93
101
  .cell-input{
94
102
  display: none;
95
103
  }
104
+ .kgrid-flag-switch {
105
+ display: flex;
106
+ align-items: center;
107
+ min-height: 1.5rem;
108
+ }
109
+ .custom-table-shell td .kgrid-flag-switch .form-check-input {
110
+ width: 2em;
111
+ max-width: 2em;
112
+ float: none;
113
+ }
96
114
  .custom-table-shell[data-interaction="view"] .cell-input,
97
115
  .custom-table-shell[data-interaction="view"] .delete-item,
98
116
  .custom-table-shell[data-interaction="view"] .clone-item{
@@ -152,17 +170,81 @@ div.save-item{
152
170
  border-width: 0 !important;
153
171
  overflow: hidden;
154
172
  }
173
+ /* Width set inline via actionColumnWidth(); fallback if colgroup sync skipped */
155
174
  .custom-table-shell[data-interaction="edit"] col.kgrid-row-actions-col {
156
- width: 130px;
175
+ width: 5.75rem;
157
176
  }
158
- .custom-table-shell[data-interaction="edit"] th.kgrid-row-actions {
159
- width: 130px;
177
+ .custom-table-shell[data-interaction="edit"] th.kgrid-row-actions,
178
+ .custom-table-shell[data-interaction="edit"] td.kgrid-row-actions {
179
+ white-space: nowrap;
160
180
  }
161
181
  .editing .delete-item-grp,
162
182
  .editing .clone-item-grp {
163
183
  display: none;
164
184
  }
165
185
 
186
+ /* User-hidden columns (chooser): collapse like row-actions; cells stay in DOM for filters/forms */
187
+ col.kgrid-user-hidden,
188
+ th.kgrid-user-hidden,
189
+ td.kgrid-user-hidden {
190
+ visibility: collapse;
191
+ width: 0 !important;
192
+ max-width: 0;
193
+ padding: 0 !important;
194
+ border-width: 0 !important;
195
+ overflow: hidden;
196
+ }
197
+
198
+ .kgrid-column-chooser {
199
+ position: relative;
200
+ display: flex;
201
+ justify-content: flex-end;
202
+ padding: 0.25rem 0.5rem;
203
+ }
204
+ .kgrid-column-chooser-panel {
205
+ position: absolute;
206
+ right: 0.5rem;
207
+ top: 100%;
208
+ z-index: 20;
209
+ min-width: 16rem;
210
+ max-height: 20rem;
211
+ overflow: auto;
212
+ background: var(--bs-body-bg, #fff);
213
+ color: var(--bs-body-color, #212529);
214
+ border: 1px solid var(--bs-border-color, #dee2e6);
215
+ border-radius: 0.375rem;
216
+ box-shadow: 0 0.5rem 1rem rgba(0, 0, 0, 0.15);
217
+ padding: 0.5rem;
218
+ }
219
+ .kgrid-column-chooser-list {
220
+ list-style: none;
221
+ margin: 0 0 0.5rem;
222
+ padding: 0;
223
+ }
224
+ .kgrid-column-chooser-list li {
225
+ display: flex;
226
+ align-items: center;
227
+ gap: 0.35rem;
228
+ padding: 0.2rem 0.25rem;
229
+ cursor: grab;
230
+ }
231
+ .kgrid-column-chooser-list li.kgrid-column-chooser-dragging {
232
+ opacity: 0.5;
233
+ }
234
+ .kgrid-column-chooser-handle {
235
+ color: #6c757d;
236
+ user-select: none;
237
+ letter-spacing: -0.15em;
238
+ }
239
+ .kgrid-column-chooser-list label {
240
+ margin: 0;
241
+ cursor: pointer;
242
+ flex: 1;
243
+ }
244
+ .kgrid-column-chooser-reset {
245
+ width: 100%;
246
+ }
247
+
166
248
 
167
249
  .custom-table .thead-labels th{
168
250
  background-color: #2c3e50;