@logimaxx/kgrid 0.4.0 → 0.5.1

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,356 @@
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
+ "data-bs-popper-config": JSON.stringify({ strategy: "fixed" }),
295
+ "aria-expanded": "false",
296
+ title: "Actions",
297
+ })
298
+ .html("<i class='fas fa-ellipsis-v'></i>")
299
+ .appendTo($menuWrap);
300
+ const $menu = $("<ul>")
301
+ .addClass("dropdown-menu dropdown-menu-end")
302
+ .appendTo($menuWrap);
303
+ idleItems.forEach(function (item) {
304
+ const $li = $("<li>").appendTo($menu);
305
+ const $a = $("<button>")
306
+ .addClass("dropdown-item")
307
+ .attr("type", "button")
308
+ .appendTo($li);
309
+ if (item.action === "clone") {
310
+ $a.addClass("clone-item");
311
+ } else if (item.action === "delete") {
312
+ $a.addClass("delete-item text-danger");
313
+ }
314
+ if (item.id) {
315
+ $a.attr("data-kgrid-action", item.id);
316
+ }
317
+ if (item.action) {
318
+ $a.attr("data-kgrid-builtin", item.action);
319
+ }
320
+ if (item.class) {
321
+ $a.addClass(item.class);
322
+ }
323
+ if (item.btnClass && !item.action) {
324
+ // Custom items may pass btnClass meant for button mode; keep danger etc. as text color hints.
325
+ if (/\bbtn-danger\b|\btext-danger\b|\boutline-danger\b/.test(item.btnClass)) {
326
+ $a.addClass("text-danger");
327
+ }
328
+ }
329
+ const label = item.label || item.title || item.action || item.id;
330
+ if (item.icon) {
331
+ $a.html(iconHtml(item.icon) + " " + CT.escapeHtml(label));
332
+ } else {
333
+ $a.text(label);
334
+ }
335
+ });
336
+ } else {
337
+ idleItems.forEach(function (item) {
338
+ const $grp = $("<div>")
339
+ .addClass(item.groupClass || "btn-group kgrid-custom-action-grp")
340
+ .appendTo($buttonColumn);
341
+ if (item.when === "always") {
342
+ $grp.addClass("kgrid-action-always");
343
+ }
344
+ appendButton($grp, item, dataRowFormId);
345
+ });
346
+ }
347
+
348
+ const editingAll = ra.editingItems.concat(editingExtras);
349
+ if (editingAll.length) {
350
+ const $grp = $("<div>").addClass("btn-group edit-item-grp").appendTo($buttonColumn);
351
+ editingAll.forEach(function (item) {
352
+ appendButton($grp, item, dataRowFormId);
353
+ });
354
+ }
355
+ };
356
+ })(window.KGrid);
package/styles/table.css CHANGED
@@ -156,13 +156,14 @@ div.save-item{
156
156
  display: inline-flex;
157
157
  }
158
158
 
159
- /* Row-actions: collapse column in view so fixed layout gives full width to data cols */
160
- .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 {
161
162
  visibility: collapse;
162
163
  width: 0 !important;
163
164
  }
164
- .custom-table-shell[data-interaction="view"] th.kgrid-row-actions,
165
- .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 {
166
167
  visibility: collapse;
167
168
  width: 0 !important;
168
169
  max-width: 0;
@@ -175,15 +176,31 @@ div.save-item{
175
176
  width: 5.75rem;
176
177
  }
177
178
  .custom-table-shell[data-interaction="edit"] th.kgrid-row-actions,
178
- .custom-table-shell[data-interaction="edit"] td.kgrid-row-actions {
179
+ .custom-table-shell[data-interaction="edit"] td.kgrid-row-actions,
180
+ .custom-table-shell[data-has-row-menu="true"] th.kgrid-row-actions,
181
+ .custom-table-shell[data-has-row-menu="true"] td.kgrid-row-actions {
179
182
  white-space: nowrap;
183
+ overflow: visible;
184
+ }
185
+ /* Dropdown menus must escape table stacking (rows paint over absolute menus). */
186
+ .kgrid-row-actions-menu .dropdown-menu {
187
+ z-index: 1055;
188
+ }
189
+ .custom-table-shell tr:has(.kgrid-row-actions-menu .dropdown-toggle.show) {
190
+ position: relative;
191
+ z-index: 6;
180
192
  }
181
193
  .editing .delete-item-grp,
182
- .editing .clone-item-grp {
194
+ .editing .clone-item-grp,
195
+ .editing .kgrid-row-actions-menu,
196
+ .editing .kgrid-custom-action-grp:not(.kgrid-action-always) {
183
197
  display: none;
184
198
  }
185
199
 
186
- /* User-hidden columns (chooser): collapse like row-actions; cells stay in DOM for filters/forms */
200
+ /* User-hidden columns (chooser): collapse like row-actions; cells stay in DOM for filters/forms.
201
+ Do not use display:none — under table-layout:fixed it leaves width gaps.
202
+ Chrome + border-collapse:separate treats collapse as hidden; clip text so 0-width
203
+ headers do not wrap glyph-by-glyph and stretch thead. */
187
204
  col.kgrid-user-hidden,
188
205
  th.kgrid-user-hidden,
189
206
  td.kgrid-user-hidden {
@@ -193,6 +210,9 @@ td.kgrid-user-hidden {
193
210
  padding: 0 !important;
194
211
  border-width: 0 !important;
195
212
  overflow: hidden;
213
+ white-space: nowrap;
214
+ line-height: 0;
215
+ font-size: 0;
196
216
  }
197
217
 
198
218
  .kgrid-column-chooser {
@@ -245,6 +265,62 @@ td.kgrid-user-hidden {
245
265
  width: 100%;
246
266
  }
247
267
 
268
+ /* multi_select: checkbox list (dropdown in filter row) */
269
+ .kgrid-multi-select {
270
+ position: relative;
271
+ min-width: 0;
272
+ }
273
+ .kgrid-multi-select-toggle {
274
+ width: 100%;
275
+ text-align: left;
276
+ overflow: hidden;
277
+ text-overflow: ellipsis;
278
+ white-space: nowrap;
279
+ cursor: pointer;
280
+ }
281
+ .kgrid-multi-select-panel {
282
+ max-height: 14rem;
283
+ overflow-x: hidden;
284
+ overflow-y: auto;
285
+ padding: 0.4rem 0.55rem;
286
+ background: var(--bs-body-bg, #fff);
287
+ color: var(--bs-body-color, #212529);
288
+ border: 1px solid var(--bs-border-color, #dee2e6);
289
+ border-radius: 0.375rem;
290
+ box-sizing: border-box;
291
+ }
292
+ .kgrid-multi-select-panel.kgrid-multi-select-panel-floating {
293
+ box-shadow: 0 0.5rem 1rem rgba(0, 0, 0, 0.15);
294
+ }
295
+ .kgrid-multi-select[data-mode="insert"] .kgrid-multi-select-panel,
296
+ .kgrid-multi-select[data-mode="update"] .kgrid-multi-select-panel {
297
+ max-height: 10rem;
298
+ }
299
+ .kgrid-multi-select-option {
300
+ display: flex;
301
+ align-items: flex-start;
302
+ gap: 0.5rem;
303
+ margin: 0;
304
+ padding: 0.3rem 0.15rem;
305
+ cursor: pointer;
306
+ min-height: 1.5rem;
307
+ }
308
+ .kgrid-multi-select-check {
309
+ flex: 0 0 auto;
310
+ width: 1.05em;
311
+ height: 1.05em;
312
+ margin: 0.15rem 0 0;
313
+ accent-color: var(--bs-primary, #0d6efd);
314
+ cursor: pointer;
315
+ }
316
+ .kgrid-multi-select-label {
317
+ flex: 1 1 auto;
318
+ min-width: 0;
319
+ font-weight: 400;
320
+ line-height: 1.35;
321
+ user-select: none;
322
+ }
323
+
248
324
 
249
325
  .custom-table .thead-labels th{
250
326
  background-color: #2c3e50;
@@ -255,6 +331,7 @@ td.kgrid-user-hidden {
255
331
  }
256
332
  .custom-table .thead-filters th{
257
333
  background-color: #f1f3f5;
334
+ overflow: visible;
258
335
  }
259
336
 
260
337
  .custom-table .thead-labels th a{