@logimaxx/kgrid 0.4.0 → 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.
package/src/dom.js CHANGED
@@ -30,35 +30,6 @@
30
30
  return Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15);
31
31
  };
32
32
 
33
- /**
34
- * Whether the table needs a trailing row-actions column (header, filters, data rows, colspan).
35
- * @param {Object} options table options with features
36
- * @returns {boolean}
37
- */
38
- CT.hasActionColumn = function (options) {
39
- const f = options && options.features;
40
- if (!f) {
41
- return false;
42
- }
43
- return !!(f.delete || f.update || f.create || f.clone);
44
- };
45
-
46
- /**
47
- * Compact width for the row-actions column under table-layout:fixed
48
- * (fixed layout ignores content; 1% caused overflow). Sized from max buttons
49
- * shown in any mode (idle clone/delete vs editing save/cancel).
50
- * @param {Object} [options]
51
- * @returns {string} CSS width
52
- */
53
- CT.actionColumnWidth = function (options) {
54
- const f = (options && options.features) || {};
55
- const idle = (f.clone ? 1 : 0) + (f.delete ? 1 : 0);
56
- const editing = f.update ? 2 : 0;
57
- const insert = f.create ? 1 : 0;
58
- const n = Math.max(idle, editing, insert, 1);
59
- return (2.5 * n + 0.75).toFixed(2) + "rem";
60
- };
61
-
62
33
  /**
63
34
  * Sync <colgroup> so row-actions width can collapse in view (table-layout: fixed).
64
35
  * @param {JQuery} $table
package/src/events.js CHANGED
@@ -86,8 +86,12 @@
86
86
  });
87
87
  }
88
88
 
89
- if(options.features.clone) {
90
- view.el.find("button.clone-item").off("click").on("click",(event)=>{
89
+ const ra = CT.resolveRowActions(options);
90
+ const hasClone = ra.menuItems.some((it) => it.action === "clone");
91
+ const hasDelete = ra.menuItems.some((it) => it.action === "delete");
92
+
93
+ if (hasClone) {
94
+ view.el.find(".clone-item").off("click").on("click", (event) => {
91
95
  event.preventDefault();
92
96
  if (typeof options.onClone === "function") {
93
97
  options.onClone(item, view, event);
@@ -95,8 +99,8 @@
95
99
  });
96
100
  }
97
101
 
98
- if(options.features.delete) {
99
- view.el.find("button.delete-item").off("click").on("click",(event)=>{
102
+ if (hasDelete) {
103
+ view.el.find(".delete-item").off("click").on("click", (event) => {
100
104
  event.preventDefault();
101
105
  view.el.addClass("confirm-delete");
102
106
  const clearConfirmState = () => view.el.removeClass("confirm-delete");
@@ -110,6 +114,27 @@
110
114
  });
111
115
  }
112
116
 
117
+ const customById = new Map();
118
+ ra.menuItems.forEach((it) => {
119
+ if (it.id && typeof it.callback === "function") {
120
+ customById.set(it.id, it.callback);
121
+ }
122
+ });
123
+ if (customById.size) {
124
+ view.el
125
+ .find("[data-kgrid-action]")
126
+ .off("click.kgridAction")
127
+ .on("click.kgridAction", (event) => {
128
+ const id = event.currentTarget.getAttribute("data-kgrid-action");
129
+ const cb = customById.get(id);
130
+ if (!cb) {
131
+ return;
132
+ }
133
+ event.preventDefault();
134
+ cb(event, item, view);
135
+ });
136
+ }
137
+
113
138
  if (typeof options.onRowFields === "function") {
114
139
  options.onRowFields(item, view, table);
115
140
  }
@@ -2,16 +2,307 @@
2
2
  * Built-in field types with no external library dependency (plain HTML / jQuery DOM).
3
3
  */
4
4
  (function (CT) {
5
+ const MULTI_SELECT_VALUE_CLASS = "kgrid-multi-select-value";
6
+ const MULTI_SELECT_SEP = ";";
7
+
8
+ /**
9
+ * Normalize multi_select values to a semicolon-joined string (dbAPI `><` / IN).
10
+ * @param {*} value
11
+ * @param {string} [sep]
12
+ * @returns {string}
13
+ */
14
+ CT.normalizeMultiSelectValue = function (value, sep) {
15
+ const separator = sep || MULTI_SELECT_SEP;
16
+ if (value == null || value === "") {
17
+ return "";
18
+ }
19
+ if (Array.isArray(value)) {
20
+ return value
21
+ .filter(function (v) {
22
+ return v != null && v !== "";
23
+ })
24
+ .map(String)
25
+ .join(separator);
26
+ }
27
+ return String(value);
28
+ };
29
+
30
+ CT.parseMultiSelectValue = function (value, sep) {
31
+ const str = CT.normalizeMultiSelectValue(value, sep);
32
+ if (!str) {
33
+ return [];
34
+ }
35
+ return str.split(sep || MULTI_SELECT_SEP).filter(Boolean);
36
+ };
37
+
38
+ (function installMultiSelectValHooks() {
39
+ const prev = $.valHooks.hidden || {};
40
+ $.valHooks.hidden = {
41
+ get: function (elem) {
42
+ if (elem && elem.classList && elem.classList.contains(MULTI_SELECT_VALUE_CLASS)) {
43
+ return elem.value;
44
+ }
45
+ return prev.get ? prev.get(elem) : undefined;
46
+ },
47
+ set: function (elem, value) {
48
+ if (elem && elem.classList && elem.classList.contains(MULTI_SELECT_VALUE_CLASS)) {
49
+ const sep = $(elem).data("kgridMultiSep") || MULTI_SELECT_SEP;
50
+ elem.value = CT.normalizeMultiSelectValue(value, sep);
51
+ $(elem).triggerHandler("kgrid:multiselect:set");
52
+ return true;
53
+ }
54
+ return prev.set ? prev.set(elem, value) : undefined;
55
+ },
56
+ };
57
+ })();
58
+
59
+ function assertMultiSelectOptions(config, mode) {
60
+ if (!Array.isArray(config.options)) {
61
+ throw new Error(
62
+ "multi_select requires an options array (" +
63
+ mode +
64
+ "): " +
65
+ JSON.stringify(config, null, 2)
66
+ );
67
+ }
68
+ config.options.forEach(function (opt) {
69
+ if (!opt || typeof opt.label !== "string" || typeof opt.value !== "string") {
70
+ throw new Error(
71
+ "multi_select options need string label and value: " +
72
+ JSON.stringify(config, null, 2)
73
+ );
74
+ }
75
+ });
76
+ }
77
+
78
+ function multiSelectEmptyLabel(config) {
79
+ if (config.placeholder != null && config.placeholder !== "") {
80
+ return String(config.placeholder);
81
+ }
82
+ return "All";
83
+ }
84
+
85
+ function multiSelectSummary(selected, options, emptyLabel) {
86
+ if (!selected.length) {
87
+ return emptyLabel;
88
+ }
89
+ if (selected.length === 1) {
90
+ const match = options.find(function (opt) {
91
+ return opt.value === selected[0];
92
+ });
93
+ return match ? match.label : selected[0];
94
+ }
95
+ return selected.length + " selected";
96
+ }
97
+
98
+ function buildMultiSelectUi($input, config, mode) {
99
+ const sep = config.separator || MULTI_SELECT_SEP;
100
+ const options = config.options || [];
101
+ const emptyLabel = multiSelectEmptyLabel(config);
102
+ const compact = mode === "filter";
103
+
104
+ $input.data("kgridMultiSep", sep);
105
+ $input.addClass(MULTI_SELECT_VALUE_CLASS);
106
+
107
+ const $root = $("<div>")
108
+ .addClass("kgrid-multi-select")
109
+ .attr("data-mode", mode);
110
+ const $panel = $("<div>").addClass("kgrid-multi-select-panel");
111
+ let $toggle = null;
112
+
113
+ if (compact) {
114
+ $toggle = $("<button>")
115
+ .attr({ type: "button", "aria-expanded": "false" })
116
+ .addClass("kgrid-multi-select-toggle form-select form-select-sm");
117
+ $panel.attr("hidden", "hidden");
118
+ $root.append($toggle, $panel);
119
+ } else {
120
+ $root.addClass("kgrid-multi-select-open");
121
+ $root.append($panel);
122
+ }
123
+
124
+ options.forEach(function (opt, index) {
125
+ const uid = "kgrid_ms_" + CT.uuid() + "_" + index;
126
+ const $label = $("<label>")
127
+ .addClass("kgrid-multi-select-option")
128
+ .attr("for", uid);
129
+ $("<input>")
130
+ .attr({
131
+ type: "checkbox",
132
+ id: uid,
133
+ value: opt.value,
134
+ class: "kgrid-multi-select-check",
135
+ })
136
+ .appendTo($label);
137
+ $("<span>").addClass("kgrid-multi-select-label").text(opt.label).appendTo($label);
138
+ $panel.append($label);
139
+ });
140
+
141
+ $input.before($root);
142
+ $root.append($input);
143
+
144
+ function selectedValues() {
145
+ return CT.parseMultiSelectValue($input[0].value, sep);
146
+ }
147
+
148
+ function syncCheckboxesFromInput() {
149
+ const selected = selectedValues();
150
+ $panel.find("input[type='checkbox']").each(function () {
151
+ this.checked = selected.indexOf(this.value) !== -1;
152
+ });
153
+ if ($toggle) {
154
+ $toggle.text(multiSelectSummary(selected, options, emptyLabel));
155
+ }
156
+ }
157
+
158
+ function syncInputFromCheckboxes(triggerChange) {
159
+ const vals = [];
160
+ $panel.find("input[type='checkbox']:checked").each(function () {
161
+ vals.push(this.value);
162
+ });
163
+ const next = vals.join(sep);
164
+ if ($input[0].value !== next) {
165
+ $input[0].value = next;
166
+ if (triggerChange) {
167
+ $input.trigger("change");
168
+ }
169
+ }
170
+ if ($toggle) {
171
+ $toggle.text(multiSelectSummary(vals, options, emptyLabel));
172
+ }
173
+ }
174
+
175
+ function setOpen(open) {
176
+ if (!$toggle) {
177
+ return;
178
+ }
179
+ if (open) {
180
+ // Leave the table/card overflow context so the list is fully visible.
181
+ $panel.appendTo(document.body);
182
+ $panel.addClass("kgrid-multi-select-panel-floating");
183
+ $panel.removeAttr("hidden");
184
+ $root.addClass("kgrid-multi-select-open");
185
+ $toggle.attr("aria-expanded", "true");
186
+ positionFilterPanel();
187
+ } else {
188
+ $panel.attr("hidden", "hidden");
189
+ $root.removeClass("kgrid-multi-select-open");
190
+ $toggle.attr("aria-expanded", "false");
191
+ clearFilterPanelPosition();
192
+ $panel.removeClass("kgrid-multi-select-panel-floating");
193
+ $panel.insertAfter($toggle);
194
+ }
195
+ }
196
+
197
+ function positionFilterPanel() {
198
+ if (!$toggle) {
199
+ return;
200
+ }
201
+ const rect = $toggle[0].getBoundingClientRect();
202
+ const width = Math.max(rect.width, 12 * 16);
203
+ let left = rect.left;
204
+ const maxLeft = window.innerWidth - width - 8;
205
+ if (left > maxLeft) {
206
+ left = Math.max(8, maxLeft);
207
+ }
208
+ let top = rect.bottom + 2;
209
+ const approxHeight = Math.min(14 * 16, options.length * 28 + 16);
210
+ if (top + approxHeight > window.innerHeight - 8 && rect.top > approxHeight + 8) {
211
+ top = rect.top - approxHeight - 2;
212
+ }
213
+ $panel.css({
214
+ position: "fixed",
215
+ top: top + "px",
216
+ left: left + "px",
217
+ width: width + "px",
218
+ minWidth: width + "px",
219
+ maxWidth: "min(18rem, calc(100vw - 16px))",
220
+ zIndex: 1080,
221
+ });
222
+ }
223
+
224
+ function clearFilterPanelPosition() {
225
+ $panel.css({
226
+ position: "",
227
+ top: "",
228
+ left: "",
229
+ width: "",
230
+ minWidth: "",
231
+ maxWidth: "",
232
+ zIndex: "",
233
+ });
234
+ }
235
+
236
+ const ns = ".kgridMultiSelect_" + CT.uuid();
237
+
238
+ $panel.on("change", "input[type='checkbox']", function () {
239
+ syncInputFromCheckboxes(true);
240
+ });
241
+
242
+ $input.on("kgrid:multiselect:set", function () {
243
+ syncCheckboxesFromInput();
244
+ });
245
+
246
+ if ($toggle) {
247
+ $toggle.on("click", function (e) {
248
+ e.preventDefault();
249
+ e.stopPropagation();
250
+ setOpen(!$root.hasClass("kgrid-multi-select-open"));
251
+ });
252
+ $(document).on("mousedown" + ns, function (e) {
253
+ if (!$root.hasClass("kgrid-multi-select-open")) {
254
+ return;
255
+ }
256
+ if ($(e.target).closest($root).length || $(e.target).closest($panel).length) {
257
+ return;
258
+ }
259
+ setOpen(false);
260
+ });
261
+ $(window).on("resize" + ns + " scroll" + ns, function () {
262
+ if ($root.hasClass("kgrid-multi-select-open")) {
263
+ positionFilterPanel();
264
+ }
265
+ });
266
+ }
267
+
268
+ const formBind = function () {
269
+ const formEl = $input.prop("form") || ($input[0] && $input[0].form);
270
+ if (!formEl) {
271
+ return;
272
+ }
273
+ $(formEl).off("reset" + ns).on("reset" + ns, function () {
274
+ setTimeout(syncCheckboxesFromInput, 0);
275
+ });
276
+ };
277
+ formBind();
278
+ setTimeout(formBind, 0);
279
+
280
+ syncCheckboxesFromInput();
281
+ return $root;
282
+ }
283
+
5
284
  CT.registerFieldType("multi_select", {
285
+ filterEvents: "change",
286
+ filterDebounceMs: 0,
287
+ validate(config, mode) {
288
+ assertMultiSelectOptions(config, mode);
289
+ },
6
290
  create({ config }) {
7
- const $input = $("<select class='form-select form-select-sm' multiple>");
8
- if (Array.isArray(config.options)) {
9
- config.options.forEach((opt) => {
10
- $("<option>").text(opt.label).attr("value", opt.value).appendTo($input);
11
- });
291
+ const $input = $("<input type='hidden'>").addClass(MULTI_SELECT_VALUE_CLASS);
292
+ if (config.separator) {
293
+ $input.data("kgridMultiSep", config.separator);
12
294
  }
13
295
  return { $input, skipValueAttr: true };
14
296
  },
297
+ mount(ctx) {
298
+ buildMultiSelectUi(ctx.$input, ctx.config || {}, ctx.mode);
299
+ if (ctx.mode === "update" && ctx.item && ctx.col && ctx.col.name) {
300
+ const raw = ctx.item.attributes ? ctx.item.attributes[ctx.col.name] : null;
301
+ if (raw != null && raw !== "") {
302
+ ctx.$input.val(raw);
303
+ }
304
+ }
305
+ },
15
306
  });
16
307
 
17
308
  CT.registerFieldType("date_range", {
package/src/filters.js CHANGED
@@ -324,16 +324,25 @@
324
324
  }
325
325
  }
326
326
 
327
- input.attr("data-operator", filter.operator);
327
+ const operator =
328
+ filter.operator ||
329
+ (filter.type === "multi_select" ? "><" : undefined);
330
+ if (operator != null) {
331
+ input.attr("data-operator", operator);
332
+ }
328
333
  input.attr("form", filterFormId);
329
334
  input.attr("name", col.name);
330
335
 
331
336
  const defVal = CT.filterDefaultValue(filter.default);
332
337
  if (defVal != null && defVal !== "" && !input.val()) {
333
- input.val(String(defVal));
334
- input.attr("data-default", String(defVal));
338
+ const normalized =
339
+ filter.type === "multi_select"
340
+ ? CT.normalizeMultiSelectValue(defVal, filter.separator)
341
+ : String(defVal);
342
+ input.val(normalized);
343
+ input.attr("data-default", normalized);
335
344
  if (input[0]) {
336
- input[0].defaultValue = String(defVal);
345
+ input[0].defaultValue = normalized;
337
346
  }
338
347
  }
339
348
 
package/src/init.js CHANGED
@@ -105,6 +105,14 @@
105
105
  }
106
106
  options.onClone = fn;
107
107
  }
108
+ CT.resolveRowActions(options, handlers);
109
+ {
110
+ const ra = options._resolvedRowActions;
111
+ CT.getTableInteractionHost($host).attr(
112
+ "data-has-row-menu",
113
+ ra && ra.menuItems && ra.menuItems.length ? "true" : "false"
114
+ );
115
+ }
108
116
  options.columns.forEach((col) => {
109
117
  ["insert", "update", "display"].forEach((mode) => {
110
118
  const events = col[mode].events;
package/src/insert-row.js CHANGED
@@ -36,7 +36,7 @@
36
36
  grid.instance.newItem(data).then(()=>{
37
37
  event.target.reset();
38
38
  if(typeof options.onNewItemCreated=="function") {
39
- options.onNewItemCreated(data);
39
+ options.onNewItemCreated(data, event.target);
40
40
  }
41
41
  }).catch(CT.onError);
42
42
  });
@@ -112,10 +112,14 @@
112
112
  ? insertConfig.default.value
113
113
  : insertConfig.default;
114
114
  if (insertType === "checkbox") {
115
- input.prop("checked", CT.isFlagOn(rawDefault));
115
+ const on = CT.isFlagOn(rawDefault);
116
+ input.prop("checked", on).prop("defaultChecked", on);
116
117
  } else if(insertConfig.default != null && insertConfig.default !== "" && !input.val()) {
117
118
  if(String(rawDefault).trim()) {
118
119
  input.val(rawDefault).trigger("change");
120
+ if (input[0] && "defaultValue" in input[0]) {
121
+ input[0].defaultValue = input[0].value;
122
+ }
119
123
  }
120
124
  }
121
125
 
@@ -264,11 +264,15 @@
264
264
  return result;
265
265
  };
266
266
 
267
+ CT.columnDefaultUserHidden = function (col) {
268
+ return !!(col && col.defaultHidden && !col.locked && !col.hidden);
269
+ };
270
+
267
271
  CT.mergeLayoutIntoColumns = function (columns, layout) {
268
272
  const list = (columns || []).slice();
269
273
  list.forEach(function (col) {
270
274
  if (col && !col.hidden) {
271
- col.userHidden = false;
275
+ col.userHidden = CT.columnDefaultUserHidden(col);
272
276
  }
273
277
  });
274
278
  const visible = CT.chooserColumns(list);