@logimaxx/kgrid 0.7.2 → 0.7.4

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/constants.js CHANGED
@@ -16,8 +16,16 @@
16
16
  "delete": false,
17
17
  /** @deprecated use rowActions items [{ action: "clone" }] */
18
18
  "clone": false,
19
- "columnChooser": false
19
+ "columnChooser": false,
20
+ /** dbAPI CSV from the column panel: all fields, or the visible columns. */
21
+ "csvExport": false,
22
+ /** Leading checkbox column. Header checkbox toggles the current page. */
23
+ "selection": false
20
24
  },
25
+ /** Optional { meta(item, $row) } copied onto each selected record (id is always set). */
26
+ selection: null,
27
+ selectionAllLabel: "Toggle all",
28
+ selectionRowLabel: "Select row",
21
29
  /**
22
30
  * Row action chrome: { display: "buttons"|"dropdown", items: [...] }.
23
31
  * Built-ins: delete, clone. Save/cancel come from features.update.
@@ -32,6 +40,10 @@
32
40
  filterStorageScope: null,
33
41
  columnChooserLabel: "Columns",
34
42
  columnChooserResetLabel: "Reset columns",
43
+ csvExportVisibleLabel: "Export CSV — visible fields",
44
+ csvExportAllLabel: "Export CSV — all fields",
45
+ /** Row cap sent as page[type][limit] on the CSV request. */
46
+ csvExportLimit: 10000,
35
47
  "defaultInteraction": "view",
36
48
  "insertFormRow": {
37
49
  "position": "top"
@@ -74,6 +86,11 @@
74
86
  defaultHidden: false,
75
87
  /** Runtime: user hid this column via chooser. Not a schema flag. */
76
88
  userHidden: false,
89
+ /**
90
+ * Sparse CSV: false omits the column. A string exports that field name
91
+ * instead of `name` (the column name is still the chooser key).
92
+ */
93
+ export: true,
77
94
  /** CSS class(es) on header/filter/data/insert cells (alias: columnClass) */
78
95
  class: null,
79
96
  columnClass: null,
@@ -0,0 +1,134 @@
1
+ /**
2
+ * dbAPI CSV export from the column chooser.
3
+ * Full export keeps the collection query (filters, sort, include).
4
+ * Visible export sends fields[type] for the columns on screen and drops include,
5
+ * so related resources are not appended in full.
6
+ */
7
+ (function (CT) {
8
+ CT.csvExportFields = function (columns) {
9
+ return CT.layoutVisibleColumns(columns)
10
+ .filter(function (col) {
11
+ return (
12
+ col &&
13
+ col.name &&
14
+ !col.selection &&
15
+ col.name !== CT.SELECTION_COLUMN_NAME &&
16
+ col.export !== false
17
+ );
18
+ })
19
+ .map(function (col) {
20
+ return typeof col.exportField === "string" && col.exportField
21
+ ? col.exportField
22
+ : col.name;
23
+ });
24
+ };
25
+
26
+ /**
27
+ * @param {string} href collection URL (relative or absolute)
28
+ * @param {{ type: string, mode: "all"|"visible", fields?: string[], limit?: number }} spec
29
+ * @returns {string} same shape as href (path+query, or absolute)
30
+ */
31
+ CT.buildCsvExportUrl = function (href, spec) {
32
+ const parsed = new window.URL(href, "http://kgrid.local");
33
+ const type = spec && spec.type;
34
+ const limit = spec && spec.limit != null ? spec.limit : 10000;
35
+ parsed.searchParams.set("format", "csv");
36
+ parsed.searchParams.set("includetablehead", "true");
37
+ if (type) {
38
+ parsed.searchParams.set("page[" + type + "][limit]", String(limit));
39
+ parsed.searchParams.set("page[" + type + "][offset]", "0");
40
+ }
41
+ const dropFields = [];
42
+ parsed.searchParams.forEach(function (_value, key) {
43
+ if (key.indexOf("fields[") === 0) {
44
+ dropFields.push(key);
45
+ }
46
+ });
47
+ dropFields.forEach(function (key) {
48
+ parsed.searchParams.delete(key);
49
+ });
50
+ if (spec && spec.mode === "visible") {
51
+ parsed.searchParams.delete("include");
52
+ if (type && spec.fields && spec.fields.length) {
53
+ parsed.searchParams.set("fields[" + type + "]", spec.fields.join(","));
54
+ }
55
+ }
56
+ if (/^https?:\/\//i.test(href)) {
57
+ return parsed.toString();
58
+ }
59
+ return parsed.pathname + parsed.search;
60
+ };
61
+
62
+ CT.resolveApiUrl = function (href, kviews) {
63
+ if (href == null || href === "") {
64
+ return href;
65
+ }
66
+ const s = String(href);
67
+ if (/^https?:\/\//i.test(s) || s.startsWith("//")) {
68
+ return s;
69
+ }
70
+ const base = (kviews && (kviews.baseUrl || kviews.basePath)) || "";
71
+ if (!base) {
72
+ return s;
73
+ }
74
+ const baseNorm = String(base).replace(/\/+$/, "");
75
+ const pathNorm = s.replace(/^\/+/, "");
76
+ return pathNorm ? baseNorm + "/" + pathNorm : baseNorm + "/";
77
+ };
78
+
79
+ function csvErrorMessage(response, data) {
80
+ if (data && data.errors && data.errors[0]) {
81
+ return data.errors[0].detail || data.errors[0].title || "";
82
+ }
83
+ if (data && data.error) {
84
+ return String(data.error);
85
+ }
86
+ return "CSV export failed (" + response.status + ")";
87
+ }
88
+
89
+ CT.downloadCsv = function (options, api, mode) {
90
+ const instance = api && api.instance;
91
+ const href =
92
+ instance && instance.url && instance.url.toString ? instance.url.toString() : "";
93
+ if (!href) {
94
+ return Promise.reject(new Error("CSV export requires a collection URL"));
95
+ }
96
+ const type = options && options.type;
97
+ if (!type) {
98
+ return Promise.reject(new Error("CSV export requires a resource type"));
99
+ }
100
+ const fields = mode === "visible" ? CT.csvExportFields(options.columns) : null;
101
+ if (mode === "visible" && (!fields || !fields.length)) {
102
+ return Promise.reject(new Error("No visible columns to export"));
103
+ }
104
+ const built = CT.buildCsvExportUrl(href, {
105
+ type: type,
106
+ mode: mode === "visible" ? "visible" : "all",
107
+ fields: fields,
108
+ limit: options.csvExportLimit,
109
+ });
110
+ const requestUrl = CT.resolveApiUrl(built, CT.getKViews(options.kviews));
111
+ return window.fetch(requestUrl).then(function (response) {
112
+ if (!response.ok) {
113
+ return response
114
+ .json()
115
+ .catch(function () {
116
+ return null;
117
+ })
118
+ .then(function (data) {
119
+ throw new Error(csvErrorMessage(response, data));
120
+ });
121
+ }
122
+ return response.blob().then(function (blob) {
123
+ const link = document.createElement("a");
124
+ const objectUrl = window.URL.createObjectURL(blob);
125
+ link.href = objectUrl;
126
+ link.download = options.csvFileName || type + ".csv";
127
+ document.body.appendChild(link);
128
+ link.click();
129
+ link.remove();
130
+ window.URL.revokeObjectURL(objectUrl);
131
+ });
132
+ });
133
+ };
134
+ })(window.KGrid);
package/src/events.js CHANGED
@@ -7,6 +7,9 @@
7
7
  if (view && view.el && typeof CT.applyLayoutToRow === "function") {
8
8
  CT.applyLayoutToRow(view.el, options.columns);
9
9
  }
10
+ if (typeof CT.bindSelectionRow === "function") {
11
+ CT.bindSelectionRow(item, view, table, options);
12
+ }
10
13
 
11
14
  options.columns.forEach(col=>{
12
15
  col.display.events.forEach(event=>{
package/src/init.js CHANGED
@@ -83,6 +83,7 @@
83
83
  },
84
84
  };
85
85
 
86
+ CT.installSelectionColumn(options);
86
87
  options.columns = (options.columns || []).map((col) => CT.normalizeColumnConfig(col));
87
88
  const schemaOrder = options.columns.map(function (col) {
88
89
  return col.name;
@@ -271,6 +272,9 @@
271
272
  CT.ensureUrlFiltersOnForm(filterFormEl, api.instance);
272
273
  CT.setupFilterPersistence(filterFormEl, options);
273
274
  CT.setupColumnChooser($host, options, api);
275
+ if (CT.selectionEnabled(options)) {
276
+ CT.bindSelection(api, options, table);
277
+ }
274
278
 
275
279
  if (options.url) {
276
280
  try {
package/src/labels.js CHANGED
@@ -18,6 +18,17 @@
18
18
  let cell = labelTemplate.clone(true);
19
19
  CT.applyColumnDomAttrs(cell, col);
20
20
  CT.applyColumnCellMeta(cell, col);
21
+ if (col.selection) {
22
+ const $th = cell.empty();
23
+ $("<input>", {
24
+ type: "checkbox",
25
+ class: "form-check-input kgrid-select-all",
26
+ "aria-label": options.selectionAllLabel || "Toggle all",
27
+ }).appendTo($th);
28
+ CT.applyColumnCellMeta($th, col);
29
+ labelsRow.append($th);
30
+ return;
31
+ }
21
32
  const label = col.label ?? "";
22
33
 
23
34
  if (!col.features?.sort) {
@@ -69,7 +69,7 @@
69
69
 
70
70
  CT.chooserColumns = function (columns) {
71
71
  return (columns || []).filter(function (col) {
72
- return col && col.name && !col.hidden;
72
+ return col && col.name && !col.hidden && !col.selection;
73
73
  });
74
74
  };
75
75
 
@@ -505,11 +505,16 @@
505
505
  CT.reorderColumns = function (columns, orderedVisibleNames) {
506
506
  const byName = new Map();
507
507
  const schemaHidden = [];
508
+ const pinned = [];
508
509
  (columns || []).forEach(function (col, i) {
509
510
  if (col && col.hidden) {
510
511
  schemaHidden.push({ col: col, index: i });
511
512
  return;
512
513
  }
514
+ if (col && col.selection) {
515
+ pinned.push(col);
516
+ return;
517
+ }
513
518
  if (col && col.name) {
514
519
  byName.set(col.name, col);
515
520
  }
@@ -524,7 +529,7 @@
524
529
  byName.forEach(function (col) {
525
530
  visible.push(col);
526
531
  });
527
- const result = visible.slice();
532
+ const result = pinned.concat(visible);
528
533
  schemaHidden.forEach(function (entry) {
529
534
  result.splice(Math.min(entry.index, result.length), 0, entry.col);
530
535
  });
@@ -216,7 +216,7 @@
216
216
  */
217
217
  CT.hasActionColumn = function (options) {
218
218
  const f = options && options.features;
219
- if (f && (f.create || f.columnChooser)) {
219
+ if (f && (f.create || f.columnChooser || f.csvExport)) {
220
220
  return true;
221
221
  }
222
222
  const ra = CT.resolveRowActions(options);
@@ -0,0 +1,199 @@
1
+ /**
2
+ * Row selection. features.selection adds a leading checkbox column.
3
+ * The header checkbox toggles every row on the current page.
4
+ * Ids stay selected across filter, sort, and paging.
5
+ */
6
+ (function (CT) {
7
+ CT.SELECTION_COLUMN_NAME = "__select";
8
+
9
+ CT.selectionEnabled = function (options) {
10
+ return !!(options && options.features && options.features.selection);
11
+ };
12
+
13
+ CT.installSelectionColumn = function (options) {
14
+ if (!CT.selectionEnabled(options)) {
15
+ return options;
16
+ }
17
+ options.columns = options.columns || [];
18
+ if (options.columns.some(function (col) { return col && col.selection; })) {
19
+ return options;
20
+ }
21
+ const rowLabel = options.selectionRowLabel || "Select row";
22
+ options.columns.unshift({
23
+ name: CT.SELECTION_COLUMN_NAME,
24
+ label: "",
25
+ selection: true,
26
+ locked: true,
27
+ class: "kgrid-select",
28
+ attrs: { width: 40 },
29
+ features: { create: false, update: false, filter: false, sort: false },
30
+ display: {
31
+ template:
32
+ '<input type="checkbox" class="form-check-input kgrid-select-row" aria-label="' +
33
+ rowLabel +
34
+ '">',
35
+ events: [],
36
+ },
37
+ });
38
+ if (!options.selectedRows) {
39
+ options.selectedRows = new Map();
40
+ }
41
+ return options;
42
+ };
43
+
44
+ CT.selectionId = function (item) {
45
+ if (!item) {
46
+ return "";
47
+ }
48
+ if (item.id != null && item.id !== "") {
49
+ return String(item.id);
50
+ }
51
+ if (item.attributes && item.attributes.id != null && item.attributes.id !== "") {
52
+ return String(item.attributes.id);
53
+ }
54
+ return "";
55
+ };
56
+
57
+ CT.selectionItem = function ($row) {
58
+ if (!$row || !$row.length) {
59
+ return null;
60
+ }
61
+ const direct = $row.data("instance");
62
+ if (direct && (direct.id != null || direct.attributes)) {
63
+ return direct;
64
+ }
65
+ const nested = $row.find("[data-type=item]").first().data("instance");
66
+ return nested || null;
67
+ };
68
+
69
+ CT.visibleSelectionRows = function ($table) {
70
+ return $table.find("tbody.main-tbody > tr").filter(function () {
71
+ return $(this).find(".cell-content .kgrid-select-row").length > 0;
72
+ });
73
+ };
74
+
75
+ CT.selectionRecord = function (options, item, $row) {
76
+ const id = CT.selectionId(item);
77
+ const record = { id: id };
78
+ const metaFn = options.selection && options.selection.meta;
79
+ if (typeof metaFn === "function") {
80
+ const extra = metaFn(item, $row);
81
+ if (extra && typeof extra === "object") {
82
+ Object.keys(extra).forEach(function (key) {
83
+ if (key !== "id") {
84
+ record[key] = extra[key];
85
+ }
86
+ });
87
+ }
88
+ }
89
+ return record;
90
+ };
91
+
92
+ CT.selectionApply = function (options, $row, on, item) {
93
+ const rowItem = item || CT.selectionItem($row);
94
+ const id = CT.selectionId(rowItem);
95
+ if (!id) {
96
+ return;
97
+ }
98
+ const store = options.selectedRows;
99
+ if (on) {
100
+ store.set(id, CT.selectionRecord(options, rowItem, $row));
101
+ } else {
102
+ store.delete(id);
103
+ }
104
+ $row.find(".kgrid-select-row").prop("checked", !!on);
105
+ };
106
+
107
+ CT.paintSelectionToggle = function ($table) {
108
+ const $rows = CT.visibleSelectionRows($table);
109
+ let total = 0;
110
+ let on = 0;
111
+ $rows.each(function () {
112
+ total += 1;
113
+ if ($(this).find(".cell-content .kgrid-select-row").prop("checked")) {
114
+ on += 1;
115
+ }
116
+ });
117
+ $table.find(".kgrid-select-all").each(function () {
118
+ this.checked = total > 0 && on === total;
119
+ this.indeterminate = on > 0 && on < total;
120
+ });
121
+ };
122
+
123
+ CT.syncSelectionDom = function (options, $table) {
124
+ const store = options.selectedRows;
125
+ CT.visibleSelectionRows($table).each(function () {
126
+ const $row = $(this);
127
+ const id = CT.selectionId(CT.selectionItem($row));
128
+ $row.find(".kgrid-select-row").prop("checked", !!(id && store.has(id)));
129
+ });
130
+ CT.paintSelectionToggle($table);
131
+ };
132
+
133
+ CT.toggleSelection = function (options, $table, force) {
134
+ const $rows = CT.visibleSelectionRows($table);
135
+ let on = force;
136
+ if (typeof on !== "boolean") {
137
+ let total = 0;
138
+ let checked = 0;
139
+ $rows.each(function () {
140
+ total += 1;
141
+ if ($(this).find(".cell-content .kgrid-select-row").prop("checked")) {
142
+ checked += 1;
143
+ }
144
+ });
145
+ on = !(total > 0 && checked === total);
146
+ }
147
+ $rows.each(function () {
148
+ CT.selectionApply(options, $(this), on);
149
+ });
150
+ CT.paintSelectionToggle($table);
151
+ return on;
152
+ };
153
+
154
+ CT.bindSelectionRow = function (item, view, table, options) {
155
+ if (!CT.selectionEnabled(options) || !view || !view.el) {
156
+ return;
157
+ }
158
+ const $row = view.el;
159
+ const $table = $(table);
160
+ $row.find(".kgrid-select-row").off("change.kgridSelect").on("change.kgridSelect", function () {
161
+ CT.selectionApply(options, $row, this.checked, item);
162
+ CT.paintSelectionToggle($table);
163
+ });
164
+ const id = CT.selectionId(item);
165
+ const on = !!(id && options.selectedRows && options.selectedRows.has(id));
166
+ $row.find(".kgrid-select-row").prop("checked", on);
167
+ if (on) {
168
+ options.selectedRows.set(id, CT.selectionRecord(options, item, $row));
169
+ }
170
+ CT.paintSelectionToggle($table);
171
+ };
172
+
173
+ CT.bindSelection = function (api, options, $table) {
174
+ if (!options.selectedRows) {
175
+ options.selectedRows = new Map();
176
+ }
177
+ api.selected = options.selectedRows;
178
+ api.getSelection = function () {
179
+ return Array.from(options.selectedRows.values());
180
+ };
181
+ api.clearSelection = function () {
182
+ options.selectedRows.clear();
183
+ CT.syncSelectionDom(options, $table);
184
+ return api;
185
+ };
186
+ api.toggleAll = function (force) {
187
+ CT.toggleSelection(options, $table, force);
188
+ return api.getSelection();
189
+ };
190
+ api.syncSelection = function () {
191
+ CT.syncSelectionDom(options, $table);
192
+ return api;
193
+ };
194
+ $table.find(".kgrid-select-all").off("change.kgridSelect").on("change.kgridSelect", function () {
195
+ CT.toggleSelection(options, $table, this.checked);
196
+ });
197
+ return api;
198
+ };
199
+ })(window.KGrid);
package/styles/table.css CHANGED
@@ -438,6 +438,15 @@ th.kgrid-row-actions {
438
438
  .kgrid-column-chooser-reset {
439
439
  width: 100%;
440
440
  }
441
+ .kgrid-column-chooser-export {
442
+ display: flex;
443
+ flex-direction: column;
444
+ gap: 0.35rem;
445
+ margin-bottom: 0.5rem;
446
+ }
447
+ .kgrid-column-chooser-export .btn {
448
+ width: 100%;
449
+ }
441
450
 
442
451
  /* multi_select: checkbox list (dropdown in filter row) */
443
452
  .kgrid-multi-select {
@@ -663,3 +672,15 @@ th.kgrid-row-actions {
663
672
  background-color: transparent;
664
673
  }
665
674
  }
675
+
676
+ .custom-table-shell th.kgrid-select,
677
+ .custom-table-shell td.kgrid-select {
678
+ width: 2.25rem;
679
+ text-align: center;
680
+ vertical-align: middle;
681
+ }
682
+ .custom-table-shell .kgrid-select-all,
683
+ .custom-table-shell .kgrid-select-row {
684
+ margin: 0;
685
+ vertical-align: middle;
686
+ }