@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/src/dom.js CHANGED
@@ -40,7 +40,23 @@
40
40
  if (!f) {
41
41
  return false;
42
42
  }
43
- return !!(f.delete || f.update || f.create);
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";
44
60
  };
45
61
 
46
62
  /**
@@ -48,18 +64,37 @@
48
64
  * @param {JQuery} $table
49
65
  * @param {number} dataColumnCount visible data columns (no row-actions)
50
66
  * @param {boolean} hasActions
67
+ * @param {Object} [options] table options (for action column width)
51
68
  */
52
- CT.syncActionColumnColgroup = function ($table, dataColumnCount, hasActions) {
69
+ CT.syncActionColumnColgroup = function ($table, dataColumnCount, hasActions, options, layoutColumns) {
53
70
  let $colgroup = $table.children("colgroup.kgrid-colgroup");
54
71
  if (!$colgroup.length) {
55
72
  $colgroup = $("<colgroup>").addClass("kgrid-colgroup").prependTo($table);
56
73
  }
57
74
  $colgroup.empty();
58
- for (let i = 0; i < dataColumnCount; i++) {
59
- $colgroup.append($("<col>"));
75
+ const named = Array.isArray(layoutColumns) ? layoutColumns : null;
76
+ if (named && named.length) {
77
+ named.forEach(function (col) {
78
+ const $col = $("<col>");
79
+ if (col && col.name) {
80
+ $col.attr("data-name", col.name);
81
+ }
82
+ if (col && col.userHidden) {
83
+ $col.addClass("kgrid-user-hidden");
84
+ }
85
+ $colgroup.append($col);
86
+ });
87
+ } else {
88
+ for (let i = 0; i < dataColumnCount; i++) {
89
+ $colgroup.append($("<col>"));
90
+ }
60
91
  }
61
92
  if (hasActions) {
62
- $colgroup.append($("<col>").addClass("kgrid-row-actions-col"));
93
+ $colgroup.append(
94
+ $("<col>")
95
+ .addClass("kgrid-row-actions-col")
96
+ .css("width", CT.actionColumnWidth(options))
97
+ );
63
98
  }
64
99
  };
65
100
 
@@ -118,6 +153,7 @@
118
153
  if (cls) {
119
154
  $el.addClass(cls);
120
155
  }
156
+ $el.toggleClass("kgrid-user-hidden", !!col.userHidden);
121
157
  return $el;
122
158
  };
123
159
  })(window.KGrid);
package/src/events.js CHANGED
@@ -64,6 +64,14 @@
64
64
  $(input).val(typeof val === "boolean" ? String(val) : val);
65
65
  });
66
66
 
67
+ view.el.find("input[type='checkbox']").each((index, input) => {
68
+ const col = colMap.get(input.name);
69
+ if (!col) {
70
+ return;
71
+ }
72
+ input.checked = CT.isFlagOn(item.attributes[col.name]);
73
+ });
74
+
67
75
  view.el.find("form.edit-form").off("submit").on("submit",(event)=>{
68
76
  const form = event.target;
69
77
  event.preventDefault();
@@ -78,6 +86,15 @@
78
86
  });
79
87
  }
80
88
 
89
+ if(options.features.clone) {
90
+ view.el.find("button.clone-item").off("click").on("click",(event)=>{
91
+ event.preventDefault();
92
+ if (typeof options.onClone === "function") {
93
+ options.onClone(item, view, event);
94
+ }
95
+ });
96
+ }
97
+
81
98
  if(options.features.delete) {
82
99
  view.el.find("button.delete-item").off("click").on("click",(event)=>{
83
100
  event.preventDefault();
package/src/filters.js CHANGED
@@ -144,8 +144,9 @@
144
144
  * @param {HTMLFormElement|JQuery} filterFormEl
145
145
  * @param {Object} options
146
146
  * @param {Object} [collection] KViews collection
147
+ * @param {{ skipInitSubmit?: boolean }} [extra]
147
148
  */
148
- CT.setupDefaultFilters = function (filterFormEl, options, collection) {
149
+ CT.setupDefaultFilters = function (filterFormEl, options, collection, extra) {
149
150
  const form = $(filterFormEl)[0];
150
151
  if (!form || !options || !options.features || !options.features.filtering) {
151
152
  return;
@@ -206,7 +207,7 @@
206
207
  const missing = persisted.some(function (col) {
207
208
  return urlFilter.indexOf(col.name + "=") === -1;
208
209
  });
209
- if (missing) {
210
+ if (missing && !(extra && extra.skipInitSubmit)) {
210
211
  collection.filtering.handleSubmit(form);
211
212
  }
212
213
  };
@@ -361,6 +362,40 @@
361
362
  return filterForm;
362
363
  };
363
364
 
365
+ /** Write current form fields into collection.url.parameters.filter (same rules as KViews Filtering). */
366
+ CT.syncFormFiltersToCollectionUrl = function (form, collection) {
367
+ if (!form || !collection || !collection.url) {
368
+ return;
369
+ }
370
+ if (!collection.url.parameters) {
371
+ collection.url.parameters = {};
372
+ }
373
+ const filter = [];
374
+ const els = form.elements;
375
+ if (!els) {
376
+ return;
377
+ }
378
+ for (let i = 0; i < els.length; i++) {
379
+ const el = els[i];
380
+ if (!el || !el.name) {
381
+ continue;
382
+ }
383
+ const $el = $(el);
384
+ const value = $el.val();
385
+ if (value == null || value === "") {
386
+ continue;
387
+ }
388
+ const operator = $el.attr("data-operator") || $el.data("operator") || "=";
389
+ filter.push(el.name + operator + value);
390
+ }
391
+ collection.offset = 0;
392
+ if (filter.length) {
393
+ collection.url.parameters.filter = filter.join(",");
394
+ } else {
395
+ delete collection.url.parameters.filter;
396
+ }
397
+ };
398
+
364
399
  CT.FilterForm = function (form) {
365
400
  this.form = $(form)[0];
366
401
  this.filter = (name, value, operator = "~=~") => {
package/src/init.js CHANGED
@@ -57,12 +57,54 @@
57
57
  }
58
58
  return api.setInteraction(api.getInteraction() === "edit" ? "view" : "edit");
59
59
  },
60
+ getLayout() {
61
+ return CT.layoutFromColumns(options.columns);
62
+ },
63
+ setLayout(layout) {
64
+ CT.applyColumnLayout(api, options, layout);
65
+ CT.preferencesSaveLayout(options, api.getLayout());
66
+ return api.getLayout();
67
+ },
68
+ resetLayout() {
69
+ const byName = new Map();
70
+ options.columns.forEach(function (col) {
71
+ if (col && col.name) {
72
+ byName.set(col.name, col);
73
+ }
74
+ });
75
+ options.columns = schemaOrder
76
+ .map(function (name) {
77
+ return byName.get(name);
78
+ })
79
+ .filter(Boolean);
80
+ CT.preferencesSaveLayout(options, null);
81
+ CT.applyColumnLayout(api, options, null);
82
+ return api.getLayout();
83
+ },
60
84
  };
61
85
 
62
- options.columns = options.columns.map((col) => CT.normalizeColumnConfig(col));
86
+ options.columns = (options.columns || []).map((col) => CT.normalizeColumnConfig(col));
87
+ const schemaOrder = options.columns.map(function (col) {
88
+ return col.name;
89
+ });
90
+ options.columns = CT.mergeLayoutIntoColumns(
91
+ options.columns,
92
+ CT.preferencesLoadLayout(options)
93
+ );
63
94
 
64
95
  const handlers = options.handlers ?? {};
65
96
  delete options.handlers;
97
+ if (typeof options.onClone === "string") {
98
+ const fn =
99
+ handlers[options.onClone] ||
100
+ (options.functions && options.functions[options.onClone]);
101
+ if (!fn || typeof fn !== "function") {
102
+ throw new Error(
103
+ "onClone handler " + options.onClone + " not found or is not a function"
104
+ );
105
+ }
106
+ options.onClone = fn;
107
+ }
66
108
  options.columns.forEach((col) => {
67
109
  ["insert", "update", "display"].forEach((mode) => {
68
110
  const events = col[mode].events;
@@ -116,7 +158,13 @@
116
158
  const hasActionColumn = CT.hasActionColumn(options);
117
159
  const visibleColumnsCount = labelsRow.find("th").length;
118
160
  const dataColumnCount = visibleColumnsCount - (hasActionColumn ? 1 : 0);
119
- CT.syncActionColumnColgroup(table, dataColumnCount, hasActionColumn);
161
+ CT.syncActionColumnColgroup(
162
+ table,
163
+ dataColumnCount,
164
+ hasActionColumn,
165
+ options,
166
+ CT.chooserColumns(options.columns)
167
+ );
120
168
 
121
169
  const filterForm = options.filterForm ?? CT.setupFilterHeader(table, options);
122
170
 
@@ -180,7 +228,24 @@
180
228
  if (options.insertUrl) {
181
229
  api.instance.setUrl(options.insertUrl, "insert");
182
230
  }
231
+ }
232
+
233
+ CT.setupDefaultFilters(filterForm, options, api.instance, { skipInitSubmit: true });
234
+ const filterFormEl = filterForm && (filterForm.jquery ? filterForm[0] : filterForm);
235
+ CT.applyUserFilters(
236
+ filterFormEl,
237
+ options.columns,
238
+ CT.preferencesLoadFilters(options)
239
+ );
240
+ CT.ensureUrlFiltersOnForm(filterFormEl, api.instance);
241
+ CT.setupFilterPersistence(filterFormEl, options);
242
+ CT.setupColumnChooser($host, options, api);
243
+
244
+ if (options.url) {
183
245
  try {
246
+ if (options.features && options.features.filtering && filterFormEl) {
247
+ CT.syncFormFiltersToCollectionUrl(filterFormEl, api.instance);
248
+ }
184
249
  await api.instance.loadFromRemote();
185
250
  } catch (error) {
186
251
  CT.onError(error);
@@ -192,8 +257,6 @@
192
257
  throw new Error("Invalid data: missing datasource url or data for table");
193
258
  }
194
259
 
195
- CT.setupDefaultFilters(filterForm, options, api.instance);
196
-
197
260
  return api;
198
261
  };
199
262
  })(window.KGrid);
package/src/insert-row.js CHANGED
@@ -94,6 +94,10 @@
94
94
  case "hidden":
95
95
  input = $(`<input type='hidden' class='form-input form-control form-control-sm'/>`);
96
96
  break;
97
+ case "checkbox":
98
+ input = $("<input autocomplete='off' type='checkbox' class='form-check-input'/>");
99
+ input.attr("data-type", insertType);
100
+ break;
97
101
  default:
98
102
  if(CT.isValidInputType(insertType)) {
99
103
  input = $(`<input autocomplete='off' type='${insertType}' class='form-input form-control form-control-sm'/>`);
@@ -104,12 +108,14 @@
104
108
  }
105
109
  }
106
110
 
107
- if(insertConfig.default != null && insertConfig.default !== "" && !input.val()) {
108
- const defVal = (typeof insertConfig.default === "object" && insertConfig.default.value != null)
109
- ? insertConfig.default.value
110
- : insertConfig.default;
111
- if(String(defVal).trim()) {
112
- input.val(defVal).trigger("change");
111
+ const rawDefault = (typeof insertConfig.default === "object" && insertConfig.default && insertConfig.default.value != null)
112
+ ? insertConfig.default.value
113
+ : insertConfig.default;
114
+ if (insertType === "checkbox") {
115
+ input.prop("checked", CT.isFlagOn(rawDefault));
116
+ } else if(insertConfig.default != null && insertConfig.default !== "" && !input.val()) {
117
+ if(String(rawDefault).trim()) {
118
+ input.val(rawDefault).trigger("change");
113
119
  }
114
120
  }
115
121
 
@@ -132,7 +138,8 @@
132
138
  }
133
139
 
134
140
  if(insertConfig.type!=="hidden") {
135
- const $td = $("<td>").append(input).appendTo(newRecordRow).attr("data-label", col.label);
141
+ const $control = insertType === "checkbox" ? CT.wrapFlagSwitch(input) : input;
142
+ const $td = $("<td>").append($control).appendTo(newRecordRow).attr("data-label", col.label);
136
143
  CT.applyColumnCellMeta($td, col);
137
144
  }
138
145
  CT.mountField({
@@ -36,8 +36,11 @@
36
36
  if (overrides.delete !== undefined) {
37
37
  $shell.attr("data-allow-delete", overrides.delete ? "true" : "false");
38
38
  }
39
+ if (overrides.clone !== undefined) {
40
+ $shell.attr("data-allow-clone", overrides.clone ? "true" : "false");
41
+ }
39
42
  } else if (interaction === "edit") {
40
- $shell.removeAttr("data-allow-insert data-allow-update data-allow-delete");
43
+ $shell.removeAttr("data-allow-insert data-allow-update data-allow-delete data-allow-clone");
41
44
  }
42
45
  };
43
46
  })(window.KGrid);