@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/dist/kgrid.js CHANGED
@@ -1,4 +1,4 @@
1
- /*! @logimaxx/kgrid | (c) Logimaxx System SRL — proprietary | https://logimaxx.ro | built 2026-11-03T08:05:10.178Z */
1
+ /*! @logimaxx/kgrid | (c) Logimaxx System SRL — proprietary | https://logimaxx.ro | built 2026-11-17T14:32:51.460Z */
2
2
 
3
3
  /* --- configure.js --- */
4
4
  /**
@@ -209,8 +209,16 @@
209
209
  "delete": false,
210
210
  /** @deprecated use rowActions items [{ action: "clone" }] */
211
211
  "clone": false,
212
- "columnChooser": false
212
+ "columnChooser": false,
213
+ /** dbAPI CSV from the column panel: all fields, or the visible columns. */
214
+ "csvExport": false,
215
+ /** Leading checkbox column. Header checkbox toggles the current page. */
216
+ "selection": false
213
217
  },
218
+ /** Optional { meta(item, $row) } copied onto each selected record (id is always set). */
219
+ selection: null,
220
+ selectionAllLabel: "Toggle all",
221
+ selectionRowLabel: "Select row",
214
222
  /**
215
223
  * Row action chrome: { display: "buttons"|"dropdown", items: [...] }.
216
224
  * Built-ins: delete, clone. Save/cancel come from features.update.
@@ -225,6 +233,10 @@
225
233
  filterStorageScope: null,
226
234
  columnChooserLabel: "Columns",
227
235
  columnChooserResetLabel: "Reset columns",
236
+ csvExportVisibleLabel: "Export CSV — visible fields",
237
+ csvExportAllLabel: "Export CSV — all fields",
238
+ /** Row cap sent as page[type][limit] on the CSV request. */
239
+ csvExportLimit: 10000,
228
240
  "defaultInteraction": "view",
229
241
  "insertFormRow": {
230
242
  "position": "top"
@@ -267,6 +279,11 @@
267
279
  defaultHidden: false,
268
280
  /** Runtime: user hid this column via chooser. Not a schema flag. */
269
281
  userHidden: false,
282
+ /**
283
+ * Sparse CSV: false omits the column. A string exports that field name
284
+ * instead of `name` (the column name is still the chooser key).
285
+ */
286
+ export: true,
270
287
  /** CSS class(es) on header/filter/data/insert cells (alias: columnClass) */
271
288
  class: null,
272
289
  columnClass: null,
@@ -945,7 +962,7 @@
945
962
  */
946
963
  CT.hasActionColumn = function (options) {
947
964
  const f = options && options.features;
948
- if (f && (f.create || f.columnChooser)) {
965
+ if (f && (f.create || f.columnChooser || f.csvExport)) {
949
966
  return true;
950
967
  }
951
968
  const ra = CT.resolveRowActions(options);
@@ -2353,6 +2370,17 @@
2353
2370
  let cell = labelTemplate.clone(true);
2354
2371
  CT.applyColumnDomAttrs(cell, col);
2355
2372
  CT.applyColumnCellMeta(cell, col);
2373
+ if (col.selection) {
2374
+ const $th = cell.empty();
2375
+ $("<input>", {
2376
+ type: "checkbox",
2377
+ class: "form-check-input kgrid-select-all",
2378
+ "aria-label": options.selectionAllLabel || "Toggle all",
2379
+ }).appendTo($th);
2380
+ CT.applyColumnCellMeta($th, col);
2381
+ labelsRow.append($th);
2382
+ return;
2383
+ }
2356
2384
  const label = col.label ?? "";
2357
2385
 
2358
2386
  if (!col.features?.sort) {
@@ -3274,6 +3302,9 @@
3274
3302
  if (view && view.el && typeof CT.applyLayoutToRow === "function") {
3275
3303
  CT.applyLayoutToRow(view.el, options.columns);
3276
3304
  }
3305
+ if (typeof CT.bindSelectionRow === "function") {
3306
+ CT.bindSelectionRow(item, view, table, options);
3307
+ }
3277
3308
 
3278
3309
  options.columns.forEach(col=>{
3279
3310
  col.display.events.forEach(event=>{
@@ -3499,7 +3530,7 @@
3499
3530
 
3500
3531
  CT.chooserColumns = function (columns) {
3501
3532
  return (columns || []).filter(function (col) {
3502
- return col && col.name && !col.hidden;
3533
+ return col && col.name && !col.hidden && !col.selection;
3503
3534
  });
3504
3535
  };
3505
3536
 
@@ -3935,11 +3966,16 @@
3935
3966
  CT.reorderColumns = function (columns, orderedVisibleNames) {
3936
3967
  const byName = new Map();
3937
3968
  const schemaHidden = [];
3969
+ const pinned = [];
3938
3970
  (columns || []).forEach(function (col, i) {
3939
3971
  if (col && col.hidden) {
3940
3972
  schemaHidden.push({ col: col, index: i });
3941
3973
  return;
3942
3974
  }
3975
+ if (col && col.selection) {
3976
+ pinned.push(col);
3977
+ return;
3978
+ }
3943
3979
  if (col && col.name) {
3944
3980
  byName.set(col.name, col);
3945
3981
  }
@@ -3954,7 +3990,7 @@
3954
3990
  byName.forEach(function (col) {
3955
3991
  visible.push(col);
3956
3992
  });
3957
- const result = visible.slice();
3993
+ const result = pinned.concat(visible);
3958
3994
  schemaHidden.forEach(function (entry) {
3959
3995
  result.splice(Math.min(entry.index, result.length), 0, entry.col);
3960
3996
  });
@@ -4108,6 +4144,143 @@
4108
4144
  })(window.KGrid);
4109
4145
 
4110
4146
 
4147
+ /* --- csv-export.js --- */
4148
+ /**
4149
+ * dbAPI CSV export from the column chooser.
4150
+ * Full export keeps the collection query (filters, sort, include).
4151
+ * Visible export sends fields[type] for the columns on screen and drops include,
4152
+ * so related resources are not appended in full.
4153
+ */
4154
+ (function (CT) {
4155
+ CT.csvExportFields = function (columns) {
4156
+ return CT.layoutVisibleColumns(columns)
4157
+ .filter(function (col) {
4158
+ return (
4159
+ col &&
4160
+ col.name &&
4161
+ !col.selection &&
4162
+ col.name !== CT.SELECTION_COLUMN_NAME &&
4163
+ col.export !== false
4164
+ );
4165
+ })
4166
+ .map(function (col) {
4167
+ return typeof col.exportField === "string" && col.exportField
4168
+ ? col.exportField
4169
+ : col.name;
4170
+ });
4171
+ };
4172
+
4173
+ /**
4174
+ * @param {string} href collection URL (relative or absolute)
4175
+ * @param {{ type: string, mode: "all"|"visible", fields?: string[], limit?: number }} spec
4176
+ * @returns {string} same shape as href (path+query, or absolute)
4177
+ */
4178
+ CT.buildCsvExportUrl = function (href, spec) {
4179
+ const parsed = new window.URL(href, "http://kgrid.local");
4180
+ const type = spec && spec.type;
4181
+ const limit = spec && spec.limit != null ? spec.limit : 10000;
4182
+ parsed.searchParams.set("format", "csv");
4183
+ parsed.searchParams.set("includetablehead", "true");
4184
+ if (type) {
4185
+ parsed.searchParams.set("page[" + type + "][limit]", String(limit));
4186
+ parsed.searchParams.set("page[" + type + "][offset]", "0");
4187
+ }
4188
+ const dropFields = [];
4189
+ parsed.searchParams.forEach(function (_value, key) {
4190
+ if (key.indexOf("fields[") === 0) {
4191
+ dropFields.push(key);
4192
+ }
4193
+ });
4194
+ dropFields.forEach(function (key) {
4195
+ parsed.searchParams.delete(key);
4196
+ });
4197
+ if (spec && spec.mode === "visible") {
4198
+ parsed.searchParams.delete("include");
4199
+ if (type && spec.fields && spec.fields.length) {
4200
+ parsed.searchParams.set("fields[" + type + "]", spec.fields.join(","));
4201
+ }
4202
+ }
4203
+ if (/^https?:\/\//i.test(href)) {
4204
+ return parsed.toString();
4205
+ }
4206
+ return parsed.pathname + parsed.search;
4207
+ };
4208
+
4209
+ CT.resolveApiUrl = function (href, kviews) {
4210
+ if (href == null || href === "") {
4211
+ return href;
4212
+ }
4213
+ const s = String(href);
4214
+ if (/^https?:\/\//i.test(s) || s.startsWith("//")) {
4215
+ return s;
4216
+ }
4217
+ const base = (kviews && (kviews.baseUrl || kviews.basePath)) || "";
4218
+ if (!base) {
4219
+ return s;
4220
+ }
4221
+ const baseNorm = String(base).replace(/\/+$/, "");
4222
+ const pathNorm = s.replace(/^\/+/, "");
4223
+ return pathNorm ? baseNorm + "/" + pathNorm : baseNorm + "/";
4224
+ };
4225
+
4226
+ function csvErrorMessage(response, data) {
4227
+ if (data && data.errors && data.errors[0]) {
4228
+ return data.errors[0].detail || data.errors[0].title || "";
4229
+ }
4230
+ if (data && data.error) {
4231
+ return String(data.error);
4232
+ }
4233
+ return "CSV export failed (" + response.status + ")";
4234
+ }
4235
+
4236
+ CT.downloadCsv = function (options, api, mode) {
4237
+ const instance = api && api.instance;
4238
+ const href =
4239
+ instance && instance.url && instance.url.toString ? instance.url.toString() : "";
4240
+ if (!href) {
4241
+ return Promise.reject(new Error("CSV export requires a collection URL"));
4242
+ }
4243
+ const type = options && options.type;
4244
+ if (!type) {
4245
+ return Promise.reject(new Error("CSV export requires a resource type"));
4246
+ }
4247
+ const fields = mode === "visible" ? CT.csvExportFields(options.columns) : null;
4248
+ if (mode === "visible" && (!fields || !fields.length)) {
4249
+ return Promise.reject(new Error("No visible columns to export"));
4250
+ }
4251
+ const built = CT.buildCsvExportUrl(href, {
4252
+ type: type,
4253
+ mode: mode === "visible" ? "visible" : "all",
4254
+ fields: fields,
4255
+ limit: options.csvExportLimit,
4256
+ });
4257
+ const requestUrl = CT.resolveApiUrl(built, CT.getKViews(options.kviews));
4258
+ return window.fetch(requestUrl).then(function (response) {
4259
+ if (!response.ok) {
4260
+ return response
4261
+ .json()
4262
+ .catch(function () {
4263
+ return null;
4264
+ })
4265
+ .then(function (data) {
4266
+ throw new Error(csvErrorMessage(response, data));
4267
+ });
4268
+ }
4269
+ return response.blob().then(function (blob) {
4270
+ const link = document.createElement("a");
4271
+ const objectUrl = window.URL.createObjectURL(blob);
4272
+ link.href = objectUrl;
4273
+ link.download = options.csvFileName || type + ".csv";
4274
+ document.body.appendChild(link);
4275
+ link.click();
4276
+ link.remove();
4277
+ window.URL.revokeObjectURL(objectUrl);
4278
+ });
4279
+ });
4280
+ };
4281
+ })(window.KGrid);
4282
+
4283
+
4111
4284
  /* --- column-chooser.js --- */
4112
4285
  /**
4113
4286
  * Column chooser: checkbox visibility + HTML5 drag reorder.
@@ -4129,8 +4302,44 @@
4129
4302
  }).length;
4130
4303
  }
4131
4304
 
4305
+ function bindCsvExport($panel, options, api) {
4306
+ $panel
4307
+ .off("click.kgridCsv")
4308
+ .on("click.kgridCsv", ".kgrid-csv-export-visible, .kgrid-csv-export-all", function () {
4309
+ const button = this;
4310
+ const mode = button.classList.contains("kgrid-csv-export-visible") ? "visible" : "all";
4311
+ button.disabled = true;
4312
+ Promise.resolve(CT.downloadCsv(options, api, mode))
4313
+ .catch(function (err) {
4314
+ CT.onError(err);
4315
+ })
4316
+ .finally(function () {
4317
+ button.disabled = false;
4318
+ });
4319
+ });
4320
+ }
4321
+
4132
4322
  CT.renderColumnChooserPanel = function ($panel, options, api) {
4133
4323
  $panel.empty();
4324
+ const chooser = !!(options.features && options.features.columnChooser);
4325
+ const csv = !!(options.features && options.features.csvExport);
4326
+ if (csv) {
4327
+ const $export = $("<div>").addClass("kgrid-column-chooser-export").appendTo($panel);
4328
+ $("<button>")
4329
+ .attr("type", "button")
4330
+ .addClass("btn btn-sm btn-outline-secondary kgrid-csv-export-visible")
4331
+ .text(options.csvExportVisibleLabel || "Export CSV — visible fields")
4332
+ .appendTo($export);
4333
+ $("<button>")
4334
+ .attr("type", "button")
4335
+ .addClass("btn btn-sm btn-outline-secondary kgrid-csv-export-all")
4336
+ .text(options.csvExportAllLabel || "Export CSV — all fields")
4337
+ .appendTo($export);
4338
+ bindCsvExport($panel, options, api);
4339
+ }
4340
+ if (!chooser) {
4341
+ return;
4342
+ }
4134
4343
  const $list = $("<ul>").addClass("kgrid-column-chooser-list").appendTo($panel);
4135
4344
  CT.chooserColumns(options.columns).forEach(function (col) {
4136
4345
  const $li = $("<li>")
@@ -4210,7 +4419,9 @@
4210
4419
  };
4211
4420
 
4212
4421
  CT.setupColumnChooser = function ($host, options, api) {
4213
- if (!options.features || !options.features.columnChooser) {
4422
+ const chooser = !!(options.features && options.features.columnChooser);
4423
+ const csv = !!(options.features && options.features.csvExport);
4424
+ if (!chooser && !csv) {
4214
4425
  return;
4215
4426
  }
4216
4427
  const $shell = CT.getTableInteractionHost($host);
@@ -4296,6 +4507,208 @@
4296
4507
  })(window.KGrid);
4297
4508
 
4298
4509
 
4510
+ /* --- selection.js --- */
4511
+ /**
4512
+ * Row selection. features.selection adds a leading checkbox column.
4513
+ * The header checkbox toggles every row on the current page.
4514
+ * Ids stay selected across filter, sort, and paging.
4515
+ */
4516
+ (function (CT) {
4517
+ CT.SELECTION_COLUMN_NAME = "__select";
4518
+
4519
+ CT.selectionEnabled = function (options) {
4520
+ return !!(options && options.features && options.features.selection);
4521
+ };
4522
+
4523
+ CT.installSelectionColumn = function (options) {
4524
+ if (!CT.selectionEnabled(options)) {
4525
+ return options;
4526
+ }
4527
+ options.columns = options.columns || [];
4528
+ if (options.columns.some(function (col) { return col && col.selection; })) {
4529
+ return options;
4530
+ }
4531
+ const rowLabel = options.selectionRowLabel || "Select row";
4532
+ options.columns.unshift({
4533
+ name: CT.SELECTION_COLUMN_NAME,
4534
+ label: "",
4535
+ selection: true,
4536
+ locked: true,
4537
+ class: "kgrid-select",
4538
+ attrs: { width: 40 },
4539
+ features: { create: false, update: false, filter: false, sort: false },
4540
+ display: {
4541
+ template:
4542
+ '<input type="checkbox" class="form-check-input kgrid-select-row" aria-label="' +
4543
+ rowLabel +
4544
+ '">',
4545
+ events: [],
4546
+ },
4547
+ });
4548
+ if (!options.selectedRows) {
4549
+ options.selectedRows = new Map();
4550
+ }
4551
+ return options;
4552
+ };
4553
+
4554
+ CT.selectionId = function (item) {
4555
+ if (!item) {
4556
+ return "";
4557
+ }
4558
+ if (item.id != null && item.id !== "") {
4559
+ return String(item.id);
4560
+ }
4561
+ if (item.attributes && item.attributes.id != null && item.attributes.id !== "") {
4562
+ return String(item.attributes.id);
4563
+ }
4564
+ return "";
4565
+ };
4566
+
4567
+ CT.selectionItem = function ($row) {
4568
+ if (!$row || !$row.length) {
4569
+ return null;
4570
+ }
4571
+ const direct = $row.data("instance");
4572
+ if (direct && (direct.id != null || direct.attributes)) {
4573
+ return direct;
4574
+ }
4575
+ const nested = $row.find("[data-type=item]").first().data("instance");
4576
+ return nested || null;
4577
+ };
4578
+
4579
+ CT.visibleSelectionRows = function ($table) {
4580
+ return $table.find("tbody.main-tbody > tr").filter(function () {
4581
+ return $(this).find(".cell-content .kgrid-select-row").length > 0;
4582
+ });
4583
+ };
4584
+
4585
+ CT.selectionRecord = function (options, item, $row) {
4586
+ const id = CT.selectionId(item);
4587
+ const record = { id: id };
4588
+ const metaFn = options.selection && options.selection.meta;
4589
+ if (typeof metaFn === "function") {
4590
+ const extra = metaFn(item, $row);
4591
+ if (extra && typeof extra === "object") {
4592
+ Object.keys(extra).forEach(function (key) {
4593
+ if (key !== "id") {
4594
+ record[key] = extra[key];
4595
+ }
4596
+ });
4597
+ }
4598
+ }
4599
+ return record;
4600
+ };
4601
+
4602
+ CT.selectionApply = function (options, $row, on, item) {
4603
+ const rowItem = item || CT.selectionItem($row);
4604
+ const id = CT.selectionId(rowItem);
4605
+ if (!id) {
4606
+ return;
4607
+ }
4608
+ const store = options.selectedRows;
4609
+ if (on) {
4610
+ store.set(id, CT.selectionRecord(options, rowItem, $row));
4611
+ } else {
4612
+ store.delete(id);
4613
+ }
4614
+ $row.find(".kgrid-select-row").prop("checked", !!on);
4615
+ };
4616
+
4617
+ CT.paintSelectionToggle = function ($table) {
4618
+ const $rows = CT.visibleSelectionRows($table);
4619
+ let total = 0;
4620
+ let on = 0;
4621
+ $rows.each(function () {
4622
+ total += 1;
4623
+ if ($(this).find(".cell-content .kgrid-select-row").prop("checked")) {
4624
+ on += 1;
4625
+ }
4626
+ });
4627
+ $table.find(".kgrid-select-all").each(function () {
4628
+ this.checked = total > 0 && on === total;
4629
+ this.indeterminate = on > 0 && on < total;
4630
+ });
4631
+ };
4632
+
4633
+ CT.syncSelectionDom = function (options, $table) {
4634
+ const store = options.selectedRows;
4635
+ CT.visibleSelectionRows($table).each(function () {
4636
+ const $row = $(this);
4637
+ const id = CT.selectionId(CT.selectionItem($row));
4638
+ $row.find(".kgrid-select-row").prop("checked", !!(id && store.has(id)));
4639
+ });
4640
+ CT.paintSelectionToggle($table);
4641
+ };
4642
+
4643
+ CT.toggleSelection = function (options, $table, force) {
4644
+ const $rows = CT.visibleSelectionRows($table);
4645
+ let on = force;
4646
+ if (typeof on !== "boolean") {
4647
+ let total = 0;
4648
+ let checked = 0;
4649
+ $rows.each(function () {
4650
+ total += 1;
4651
+ if ($(this).find(".cell-content .kgrid-select-row").prop("checked")) {
4652
+ checked += 1;
4653
+ }
4654
+ });
4655
+ on = !(total > 0 && checked === total);
4656
+ }
4657
+ $rows.each(function () {
4658
+ CT.selectionApply(options, $(this), on);
4659
+ });
4660
+ CT.paintSelectionToggle($table);
4661
+ return on;
4662
+ };
4663
+
4664
+ CT.bindSelectionRow = function (item, view, table, options) {
4665
+ if (!CT.selectionEnabled(options) || !view || !view.el) {
4666
+ return;
4667
+ }
4668
+ const $row = view.el;
4669
+ const $table = $(table);
4670
+ $row.find(".kgrid-select-row").off("change.kgridSelect").on("change.kgridSelect", function () {
4671
+ CT.selectionApply(options, $row, this.checked, item);
4672
+ CT.paintSelectionToggle($table);
4673
+ });
4674
+ const id = CT.selectionId(item);
4675
+ const on = !!(id && options.selectedRows && options.selectedRows.has(id));
4676
+ $row.find(".kgrid-select-row").prop("checked", on);
4677
+ if (on) {
4678
+ options.selectedRows.set(id, CT.selectionRecord(options, item, $row));
4679
+ }
4680
+ CT.paintSelectionToggle($table);
4681
+ };
4682
+
4683
+ CT.bindSelection = function (api, options, $table) {
4684
+ if (!options.selectedRows) {
4685
+ options.selectedRows = new Map();
4686
+ }
4687
+ api.selected = options.selectedRows;
4688
+ api.getSelection = function () {
4689
+ return Array.from(options.selectedRows.values());
4690
+ };
4691
+ api.clearSelection = function () {
4692
+ options.selectedRows.clear();
4693
+ CT.syncSelectionDom(options, $table);
4694
+ return api;
4695
+ };
4696
+ api.toggleAll = function (force) {
4697
+ CT.toggleSelection(options, $table, force);
4698
+ return api.getSelection();
4699
+ };
4700
+ api.syncSelection = function () {
4701
+ CT.syncSelectionDom(options, $table);
4702
+ return api;
4703
+ };
4704
+ $table.find(".kgrid-select-all").off("change.kgridSelect").on("change.kgridSelect", function () {
4705
+ CT.toggleSelection(options, $table, this.checked);
4706
+ });
4707
+ return api;
4708
+ };
4709
+ })(window.KGrid);
4710
+
4711
+
4299
4712
  /* --- init.js --- */
4300
4713
  (function (CT) {
4301
4714
  /**
@@ -4382,6 +4795,7 @@
4382
4795
  },
4383
4796
  };
4384
4797
 
4798
+ CT.installSelectionColumn(options);
4385
4799
  options.columns = (options.columns || []).map((col) => CT.normalizeColumnConfig(col));
4386
4800
  const schemaOrder = options.columns.map(function (col) {
4387
4801
  return col.name;
@@ -4570,6 +4984,9 @@
4570
4984
  CT.ensureUrlFiltersOnForm(filterFormEl, api.instance);
4571
4985
  CT.setupFilterPersistence(filterFormEl, options);
4572
4986
  CT.setupColumnChooser($host, options, api);
4987
+ if (CT.selectionEnabled(options)) {
4988
+ CT.bindSelection(api, options, table);
4989
+ }
4573
4990
 
4574
4991
  if (options.url) {
4575
4992
  try {