@logimaxx/kgrid 0.3.1 → 0.5.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,347 @@
1
+ /**
2
+ * Declarative row actions: menu items (delete/clone/custom) + auto save/cancel from features.update.
3
+ */
4
+ (function (CT) {
5
+ CT.BUILTIN_ROW_ACTIONS = {
6
+ clone: {
7
+ action: "clone",
8
+ icon: "fa-regular fa-copy",
9
+ label: "Clone",
10
+ title: "Clone item",
11
+ when: "idle",
12
+ btnClass: "btn btn-sm btn-outline-secondary clone-item",
13
+ groupClass: "clone-item-grp",
14
+ },
15
+ delete: {
16
+ action: "delete",
17
+ icon: "fas fa-trash",
18
+ label: "Delete",
19
+ title: "Delete item",
20
+ when: "idle",
21
+ btnClass: "btn btn-sm btn-danger delete-item",
22
+ groupClass: "delete-item-grp",
23
+ },
24
+ save: {
25
+ action: "save",
26
+ icon: "fas fa-save",
27
+ label: "Save",
28
+ title: "Save item",
29
+ when: "editing",
30
+ btnClass: "btn btn-sm btn-success save-item",
31
+ groupClass: "edit-item-grp",
32
+ },
33
+ cancel: {
34
+ action: "cancel",
35
+ icon: "fas fa-undo",
36
+ label: "Cancel",
37
+ title: "Cancel edit",
38
+ when: "editing",
39
+ btnClass: "btn btn-sm btn-secondary cancel-edit",
40
+ groupClass: "edit-item-grp",
41
+ },
42
+ };
43
+
44
+ function warn(msg) {
45
+ if (typeof console !== "undefined" && typeof console.warn === "function") {
46
+ console.warn(msg);
47
+ }
48
+ }
49
+
50
+ function resolveHandler(name, handlers, options) {
51
+ if (!name || typeof name !== "string") {
52
+ return null;
53
+ }
54
+ const fn =
55
+ (handlers && handlers[name]) ||
56
+ (options.functions && options.functions[name]);
57
+ return typeof fn === "function" ? fn : null;
58
+ }
59
+
60
+ /**
61
+ * Merge a raw item with builtin defaults (or treat as custom).
62
+ * @param {Object} raw
63
+ * @param {Object} [handlers]
64
+ * @param {Object} options
65
+ * @returns {Object|null}
66
+ */
67
+ CT.normalizeRowActionItem = function (raw, handlers, options) {
68
+ if (!raw || typeof raw !== "object") {
69
+ return null;
70
+ }
71
+ const action = raw.action;
72
+ if (action === "save" || action === "cancel") {
73
+ warn(
74
+ "KGrid: rowActions items should not list '" +
75
+ action +
76
+ "' — it is added automatically when features.update is true"
77
+ );
78
+ return null;
79
+ }
80
+ let base = {};
81
+ if (action && CT.BUILTIN_ROW_ACTIONS[action]) {
82
+ base = { ...CT.BUILTIN_ROW_ACTIONS[action] };
83
+ } else if (!raw.id) {
84
+ warn("KGrid: custom rowAction requires id: " + JSON.stringify(raw));
85
+ return null;
86
+ } else {
87
+ base = {
88
+ id: raw.id,
89
+ when: "idle",
90
+ btnClass: "btn btn-sm btn-outline-secondary",
91
+ groupClass: "kgrid-custom-action-grp",
92
+ };
93
+ }
94
+
95
+ const item = { ...base, ...raw };
96
+ if (action && CT.BUILTIN_ROW_ACTIONS[action]) {
97
+ item.action = action;
98
+ }
99
+ if (!item.when) {
100
+ item.when = item.action ? base.when || "idle" : "idle";
101
+ }
102
+ if (!item.title && item.label) {
103
+ item.title = item.label;
104
+ }
105
+ if (!item.label && item.title) {
106
+ item.label = item.title;
107
+ }
108
+ if (typeof item.callback === "string") {
109
+ const fn = resolveHandler(item.callback, handlers, options);
110
+ if (!fn) {
111
+ throw new Error(
112
+ "rowAction callback " + item.callback + " not found or is not a function"
113
+ );
114
+ }
115
+ item.callback = fn;
116
+ }
117
+ if (item.id && typeof item.callback !== "function" && !item.action) {
118
+ throw new Error("Custom rowAction '" + item.id + "' requires a callback");
119
+ }
120
+ return item;
121
+ };
122
+
123
+ /**
124
+ * Resolve and cache rowActions on options.
125
+ * @param {Object} options
126
+ * @param {Object} [handlers]
127
+ * @returns {{ display: string, menuItems: Object[], editingItems: Object[] }}
128
+ */
129
+ CT.resolveRowActions = function (options, handlers) {
130
+ if (options && options._resolvedRowActions && arguments.length < 2) {
131
+ return options._resolvedRowActions;
132
+ }
133
+ const f = (options && options.features) || {};
134
+ let display = "buttons";
135
+ let rawItems = [];
136
+
137
+ if (options && options.rowActions != null) {
138
+ const ra = options.rowActions;
139
+ if (Array.isArray(ra)) {
140
+ rawItems = ra;
141
+ } else if (CT.isPlainObject(ra)) {
142
+ display = ra.display === "dropdown" ? "dropdown" : "buttons";
143
+ rawItems = Array.isArray(ra.items) ? ra.items : [];
144
+ }
145
+ if (f.delete || f.clone) {
146
+ warn(
147
+ "KGrid: features.delete / features.clone are ignored when rowActions is set"
148
+ );
149
+ }
150
+ } else {
151
+ if (f.clone) {
152
+ rawItems.push({ action: "clone" });
153
+ }
154
+ if (f.delete) {
155
+ rawItems.push({ action: "delete" });
156
+ }
157
+ }
158
+
159
+ const menuItems = [];
160
+ rawItems.forEach(function (raw) {
161
+ const item = CT.normalizeRowActionItem(raw, handlers, options || {});
162
+ if (item) {
163
+ menuItems.push(item);
164
+ }
165
+ });
166
+
167
+ const editingItems = [];
168
+ if (f.update) {
169
+ editingItems.push({ ...CT.BUILTIN_ROW_ACTIONS.save });
170
+ editingItems.push({ ...CT.BUILTIN_ROW_ACTIONS.cancel });
171
+ }
172
+
173
+ const resolved = { display: display, menuItems: menuItems, editingItems: editingItems };
174
+ if (options) {
175
+ options._resolvedRowActions = resolved;
176
+ }
177
+ return resolved;
178
+ };
179
+
180
+ /**
181
+ * Whether the table needs a trailing row-actions column.
182
+ * @param {Object} options
183
+ * @returns {boolean}
184
+ */
185
+ CT.hasActionColumn = function (options) {
186
+ const f = options && options.features;
187
+ if (f && f.create) {
188
+ return true;
189
+ }
190
+ const ra = CT.resolveRowActions(options);
191
+ return ra.menuItems.length > 0 || ra.editingItems.length > 0;
192
+ };
193
+
194
+ /**
195
+ * Compact width for the row-actions column under table-layout:fixed.
196
+ * @param {Object} [options]
197
+ * @returns {string} CSS width
198
+ */
199
+ CT.actionColumnWidth = function (options) {
200
+ const ra = CT.resolveRowActions(options);
201
+ const f = (options && options.features) || {};
202
+ const editing = ra.editingItems.length;
203
+ const insert = f.create ? 1 : 0;
204
+ let idle;
205
+ if (ra.display === "dropdown" && ra.menuItems.length > 0) {
206
+ idle = 1;
207
+ } else {
208
+ idle = ra.menuItems.filter(function (it) {
209
+ return it.when !== "editing";
210
+ }).length;
211
+ }
212
+ const n = Math.max(idle, editing, insert, 1);
213
+ return (2.5 * n + 0.75).toFixed(2) + "rem";
214
+ };
215
+
216
+ function iconHtml(icon) {
217
+ if (!icon) {
218
+ return "";
219
+ }
220
+ return "<i class='" + icon + "'></i>";
221
+ }
222
+
223
+ function appendButton($parent, item, dataRowFormId) {
224
+ const $btn = $("<button>")
225
+ .addClass(item.btnClass || "btn btn-sm btn-outline-secondary")
226
+ .attr("type", item.action === "save" ? "submit" : "button")
227
+ .attr("title", item.title || item.label || "");
228
+ if (item.class) {
229
+ $btn.addClass(item.class);
230
+ }
231
+ if (item.action === "save" || item.action === "cancel") {
232
+ $btn.attr("name", item.action === "save" ? "save" : "cancel");
233
+ if (dataRowFormId) {
234
+ $btn.attr("form", dataRowFormId);
235
+ }
236
+ }
237
+ if (item.action === "cancel") {
238
+ $btn.attr(
239
+ "onclick",
240
+ "$(this).parents('[data-type=item]').data().instance.loadFromRemote()"
241
+ );
242
+ }
243
+ if (item.id) {
244
+ $btn.attr("data-kgrid-action", item.id);
245
+ }
246
+ if (item.action) {
247
+ $btn.attr("data-kgrid-builtin", item.action);
248
+ }
249
+ if (item.icon) {
250
+ $btn.html(iconHtml(item.icon));
251
+ if (item.showLabel && item.label) {
252
+ $btn.append(document.createTextNode(" " + item.label));
253
+ }
254
+ } else if (item.label) {
255
+ $btn.text(item.label);
256
+ }
257
+ $btn.appendTo($parent);
258
+ return $btn;
259
+ }
260
+
261
+ /** Minimal escape for button text (labels). */
262
+ CT.escapeHtml = CT.escapeHtml || function (str) {
263
+ return String(str)
264
+ .replace(/&/g, "&amp;")
265
+ .replace(/</g, "&lt;")
266
+ .replace(/>/g, "&gt;")
267
+ .replace(/"/g, "&quot;");
268
+ };
269
+
270
+ /**
271
+ * Fill .kgrid-row-actions cell for a data row template.
272
+ * @param {JQuery} $buttonColumn
273
+ * @param {Object} options
274
+ * @param {string|null} dataRowFormId
275
+ */
276
+ CT.renderRowActions = function ($buttonColumn, options, dataRowFormId) {
277
+ const ra = CT.resolveRowActions(options);
278
+ const idleItems = ra.menuItems.filter(function (it) {
279
+ return it.when !== "editing";
280
+ });
281
+ const editingExtras = ra.menuItems.filter(function (it) {
282
+ return it.when === "editing";
283
+ });
284
+
285
+ if (ra.display === "dropdown" && idleItems.length > 0) {
286
+ const $menuWrap = $("<div>")
287
+ .addClass("btn-group kgrid-row-actions-menu")
288
+ .appendTo($buttonColumn);
289
+ $("<button>")
290
+ .addClass("btn btn-sm btn-outline-secondary dropdown-toggle")
291
+ .attr({
292
+ type: "button",
293
+ "data-bs-toggle": "dropdown",
294
+ "aria-expanded": "false",
295
+ title: "Actions",
296
+ })
297
+ .html("<i class='fas fa-ellipsis-v'></i>")
298
+ .appendTo($menuWrap);
299
+ const $menu = $("<ul>").addClass("dropdown-menu dropdown-menu-end").appendTo($menuWrap);
300
+ idleItems.forEach(function (item) {
301
+ const $li = $("<li>").appendTo($menu);
302
+ const $a = $("<button>")
303
+ .addClass("dropdown-item")
304
+ .attr("type", "button")
305
+ .appendTo($li);
306
+ if (item.action === "clone") {
307
+ $a.addClass("clone-item");
308
+ } else if (item.action === "delete") {
309
+ $a.addClass("delete-item text-danger");
310
+ }
311
+ if (item.id) {
312
+ $a.attr("data-kgrid-action", item.id);
313
+ }
314
+ if (item.action) {
315
+ $a.attr("data-kgrid-builtin", item.action);
316
+ }
317
+ if (item.class) {
318
+ $a.addClass(item.class);
319
+ }
320
+ const label = item.label || item.title || item.action || item.id;
321
+ if (item.icon) {
322
+ $a.html(iconHtml(item.icon) + " " + CT.escapeHtml(label));
323
+ } else {
324
+ $a.text(label);
325
+ }
326
+ });
327
+ } else {
328
+ idleItems.forEach(function (item) {
329
+ const $grp = $("<div>")
330
+ .addClass(item.groupClass || "btn-group kgrid-custom-action-grp")
331
+ .appendTo($buttonColumn);
332
+ if (item.when === "always") {
333
+ $grp.addClass("kgrid-action-always");
334
+ }
335
+ appendButton($grp, item, dataRowFormId);
336
+ });
337
+ }
338
+
339
+ const editingAll = ra.editingItems.concat(editingExtras);
340
+ if (editingAll.length) {
341
+ const $grp = $("<div>").addClass("btn-group edit-item-grp").appendTo($buttonColumn);
342
+ editingAll.forEach(function (item) {
343
+ appendButton($grp, item, dataRowFormId);
344
+ });
345
+ }
346
+ };
347
+ })(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{
@@ -138,13 +156,14 @@ div.save-item{
138
156
  display: inline-flex;
139
157
  }
140
158
 
141
- /* Row-actions: collapse column in view so fixed layout gives full width to data cols */
142
- .custom-table-shell[data-interaction="view"] col.kgrid-row-actions-col {
159
+ /* Row-actions: collapse in view only when there is no idle menu (delete/clone/custom).
160
+ Menu-only tables stay usable in view; save/cancel remain hidden until .editing. */
161
+ .custom-table-shell[data-interaction="view"]:not([data-has-row-menu="true"]) col.kgrid-row-actions-col {
143
162
  visibility: collapse;
144
163
  width: 0 !important;
145
164
  }
146
- .custom-table-shell[data-interaction="view"] th.kgrid-row-actions,
147
- .custom-table-shell[data-interaction="view"] td.kgrid-row-actions {
165
+ .custom-table-shell[data-interaction="view"]:not([data-has-row-menu="true"]) th.kgrid-row-actions,
166
+ .custom-table-shell[data-interaction="view"]:not([data-has-row-menu="true"]) td.kgrid-row-actions {
148
167
  visibility: collapse;
149
168
  width: 0 !important;
150
169
  max-width: 0;
@@ -152,17 +171,145 @@ div.save-item{
152
171
  border-width: 0 !important;
153
172
  overflow: hidden;
154
173
  }
174
+ /* Width set inline via actionColumnWidth(); fallback if colgroup sync skipped */
155
175
  .custom-table-shell[data-interaction="edit"] col.kgrid-row-actions-col {
156
- width: 130px;
176
+ width: 5.75rem;
157
177
  }
158
- .custom-table-shell[data-interaction="edit"] th.kgrid-row-actions {
159
- width: 130px;
178
+ .custom-table-shell[data-interaction="edit"] th.kgrid-row-actions,
179
+ .custom-table-shell[data-interaction="edit"] td.kgrid-row-actions {
180
+ white-space: nowrap;
160
181
  }
161
182
  .editing .delete-item-grp,
162
- .editing .clone-item-grp {
183
+ .editing .clone-item-grp,
184
+ .editing .kgrid-row-actions-menu,
185
+ .editing .kgrid-custom-action-grp:not(.kgrid-action-always) {
163
186
  display: none;
164
187
  }
165
188
 
189
+ /* User-hidden columns (chooser): collapse like row-actions; cells stay in DOM for filters/forms.
190
+ Do not use display:none — under table-layout:fixed it leaves width gaps.
191
+ Chrome + border-collapse:separate treats collapse as hidden; clip text so 0-width
192
+ headers do not wrap glyph-by-glyph and stretch thead. */
193
+ col.kgrid-user-hidden,
194
+ th.kgrid-user-hidden,
195
+ td.kgrid-user-hidden {
196
+ visibility: collapse;
197
+ width: 0 !important;
198
+ max-width: 0;
199
+ padding: 0 !important;
200
+ border-width: 0 !important;
201
+ overflow: hidden;
202
+ white-space: nowrap;
203
+ line-height: 0;
204
+ font-size: 0;
205
+ }
206
+
207
+ .kgrid-column-chooser {
208
+ position: relative;
209
+ display: flex;
210
+ justify-content: flex-end;
211
+ padding: 0.25rem 0.5rem;
212
+ }
213
+ .kgrid-column-chooser-panel {
214
+ position: absolute;
215
+ right: 0.5rem;
216
+ top: 100%;
217
+ z-index: 20;
218
+ min-width: 16rem;
219
+ max-height: 20rem;
220
+ overflow: auto;
221
+ background: var(--bs-body-bg, #fff);
222
+ color: var(--bs-body-color, #212529);
223
+ border: 1px solid var(--bs-border-color, #dee2e6);
224
+ border-radius: 0.375rem;
225
+ box-shadow: 0 0.5rem 1rem rgba(0, 0, 0, 0.15);
226
+ padding: 0.5rem;
227
+ }
228
+ .kgrid-column-chooser-list {
229
+ list-style: none;
230
+ margin: 0 0 0.5rem;
231
+ padding: 0;
232
+ }
233
+ .kgrid-column-chooser-list li {
234
+ display: flex;
235
+ align-items: center;
236
+ gap: 0.35rem;
237
+ padding: 0.2rem 0.25rem;
238
+ cursor: grab;
239
+ }
240
+ .kgrid-column-chooser-list li.kgrid-column-chooser-dragging {
241
+ opacity: 0.5;
242
+ }
243
+ .kgrid-column-chooser-handle {
244
+ color: #6c757d;
245
+ user-select: none;
246
+ letter-spacing: -0.15em;
247
+ }
248
+ .kgrid-column-chooser-list label {
249
+ margin: 0;
250
+ cursor: pointer;
251
+ flex: 1;
252
+ }
253
+ .kgrid-column-chooser-reset {
254
+ width: 100%;
255
+ }
256
+
257
+ /* multi_select: checkbox list (dropdown in filter row) */
258
+ .kgrid-multi-select {
259
+ position: relative;
260
+ min-width: 0;
261
+ }
262
+ .kgrid-multi-select-toggle {
263
+ width: 100%;
264
+ text-align: left;
265
+ overflow: hidden;
266
+ text-overflow: ellipsis;
267
+ white-space: nowrap;
268
+ cursor: pointer;
269
+ }
270
+ .kgrid-multi-select-panel {
271
+ max-height: 14rem;
272
+ overflow-x: hidden;
273
+ overflow-y: auto;
274
+ padding: 0.4rem 0.55rem;
275
+ background: var(--bs-body-bg, #fff);
276
+ color: var(--bs-body-color, #212529);
277
+ border: 1px solid var(--bs-border-color, #dee2e6);
278
+ border-radius: 0.375rem;
279
+ box-sizing: border-box;
280
+ }
281
+ .kgrid-multi-select-panel.kgrid-multi-select-panel-floating {
282
+ box-shadow: 0 0.5rem 1rem rgba(0, 0, 0, 0.15);
283
+ }
284
+ .kgrid-multi-select[data-mode="insert"] .kgrid-multi-select-panel,
285
+ .kgrid-multi-select[data-mode="update"] .kgrid-multi-select-panel {
286
+ max-height: 10rem;
287
+ }
288
+ .kgrid-multi-select-option {
289
+ display: flex;
290
+ align-items: flex-start;
291
+ gap: 0.5rem;
292
+ margin: 0;
293
+ padding: 0.3rem 0.15rem;
294
+ cursor: pointer;
295
+ min-height: 1.5rem;
296
+ }
297
+ .kgrid-multi-select-check {
298
+ flex: 0 0 auto;
299
+ width: 1.05em;
300
+ height: 1.05em;
301
+ margin: 0.15rem 0 0;
302
+ accent-color: var(--bs-primary, #0d6efd);
303
+ cursor: pointer;
304
+ }
305
+ .kgrid-multi-select-label {
306
+ flex: 1 1 auto;
307
+ min-width: 0;
308
+ font-weight: 400;
309
+ line-height: 1.35;
310
+ user-select: none;
311
+ }
312
+
166
313
 
167
314
  .custom-table .thead-labels th{
168
315
  background-color: #2c3e50;
@@ -173,6 +320,7 @@ div.save-item{
173
320
  }
174
321
  .custom-table .thead-filters th{
175
322
  background-color: #f1f3f5;
323
+ overflow: visible;
176
324
  }
177
325
 
178
326
  .custom-table .thead-labels th a{