@logimaxx/kgrid 0.6.2 → 0.7.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.
@@ -305,12 +305,223 @@
305
305
  },
306
306
  });
307
307
 
308
+ const DATE_RANGE_VALUE_CLASS = "kgrid-date-range-value";
309
+
310
+ /** Calendar day from a date or `YYYY-MM-DD HH:MM:SS` filter value. */
311
+ CT.dateRangeCalendarDate = function (value) {
312
+ if (value == null || value === "") {
313
+ return "";
314
+ }
315
+ const match = /^(\d{4}-\d{2}-\d{2})/.exec(String(value).trim());
316
+ return match ? match[1] : "";
317
+ };
318
+
319
+ /** Inclusive end of a calendar day for DATETIME columns. */
320
+ CT.dateRangeEndValue = function (date) {
321
+ return date ? date + " 23:59:59" : "";
322
+ };
323
+
324
+ /**
325
+ * `{ from, to }` calendar dates. If `to` is before `from`, `to` becomes `from`.
326
+ * @param {*} spec
327
+ * @returns {{ from: string, to: string }}
328
+ */
329
+ CT.normalizeDateRangeDefault = function (spec) {
330
+ if (spec == null || typeof spec !== "object" || Array.isArray(spec)) {
331
+ return { from: "", to: "" };
332
+ }
333
+ let from = CT.dateRangeCalendarDate(spec.from);
334
+ let to = CT.dateRangeCalendarDate(spec.to);
335
+ if (from && to && to < from) {
336
+ to = from;
337
+ }
338
+ return { from: from, to: to };
339
+ };
340
+
341
+ CT.syncDateRangeVisible = function (hidden) {
342
+ if (!hidden || !hidden.getAttribute) {
343
+ return;
344
+ }
345
+ const id = hidden.getAttribute("data-kgrid-date-range");
346
+ const root = id && hidden.ownerDocument.getElementById(id);
347
+ if (!root) {
348
+ return;
349
+ }
350
+ const bound = hidden.getAttribute("data-kgrid-date-bound");
351
+ const visible = root.querySelector(
352
+ bound === "to" ? ".kgrid-date-range-to" : ".kgrid-date-range-from"
353
+ );
354
+ if (!visible) {
355
+ return;
356
+ }
357
+ const date = CT.dateRangeCalendarDate(hidden.value);
358
+ if (visible.value !== date) {
359
+ visible.value = date;
360
+ }
361
+ };
362
+
363
+ /** Fill empty date-range hiddens from `{ from, to }`. Does not replace a value already set. */
364
+ CT.applyDateRangeDefault = function (formEl, name, spec) {
365
+ const range = CT.normalizeDateRangeDefault(spec);
366
+ if (!range.from && !range.to) {
367
+ return;
368
+ }
369
+ const $fields = CT.filterFormFields(formEl, name);
370
+ function fill(operator, date) {
371
+ if (!date) {
372
+ return;
373
+ }
374
+ const $match = $fields.filter(function () {
375
+ return $(this).attr("data-operator") === operator;
376
+ });
377
+ if (!$match.length || $match.val()) {
378
+ return;
379
+ }
380
+ $match.val(date);
381
+ if ($match[0]) {
382
+ $match[0].defaultValue = $match[0].value;
383
+ }
384
+ }
385
+ fill(">=", range.from);
386
+ fill("<=", range.to);
387
+ };
388
+
389
+ /*
390
+ * type=text, not hidden: for type=hidden the value property IS defaultValue,
391
+ * so form.reset() cannot restore the original bound.
392
+ */
393
+ (function installDateRangeValHooks() {
394
+ const prev = $.valHooks.text || {};
395
+ $.valHooks.text = {
396
+ get: function (elem) {
397
+ if (elem && elem.classList && elem.classList.contains(DATE_RANGE_VALUE_CLASS)) {
398
+ return elem.value;
399
+ }
400
+ return prev.get ? prev.get(elem) : undefined;
401
+ },
402
+ set: function (elem, value) {
403
+ if (elem && elem.classList && elem.classList.contains(DATE_RANGE_VALUE_CLASS)) {
404
+ const date = CT.dateRangeCalendarDate(value);
405
+ const bound = elem.getAttribute("data-kgrid-date-bound");
406
+ elem.value = bound === "to" ? CT.dateRangeEndValue(date) : date;
407
+ CT.syncDateRangeVisible(elem);
408
+ return true;
409
+ }
410
+ return prev.set ? prev.set(elem, value) : undefined;
411
+ },
412
+ };
413
+ })();
414
+
415
+ function dateRangeLabel(config, key, fallback) {
416
+ const value = config && config[key];
417
+ return value != null && value !== "" ? String(value) : fallback;
418
+ }
419
+
420
+ function buildDateRangeUi($from, config) {
421
+ const $to = $from.data("kgridDateRangeTo");
422
+ const rootId = "kgrid_dr_" + CT.uuid();
423
+ const $root = $("<div>", { class: "kgrid-date-range", id: rootId });
424
+ const $fromVisible = $(
425
+ "<input autocomplete='off' type='date' class='form-control form-control-sm kgrid-date-range-from'/>"
426
+ );
427
+ const $toVisible = $(
428
+ "<input autocomplete='off' type='date' class='form-control form-control-sm kgrid-date-range-to'/>"
429
+ );
430
+ $root.append(
431
+ $("<label>", { class: "kgrid-date-range-bound" }).append(
432
+ $("<span>", {
433
+ class: "kgrid-date-range-label",
434
+ text: dateRangeLabel(config, "fromLabel", "From"),
435
+ }),
436
+ $fromVisible
437
+ ),
438
+ $("<label>", { class: "kgrid-date-range-bound" }).append(
439
+ $("<span>", {
440
+ class: "kgrid-date-range-label",
441
+ text: dateRangeLabel(config, "toLabel", "To"),
442
+ }),
443
+ $toVisible
444
+ )
445
+ );
446
+ $from.before($root);
447
+ $from.attr("data-kgrid-date-range", rootId);
448
+ $to.attr("data-kgrid-date-range", rootId);
449
+ $root.append($from, $to);
450
+
451
+ function writePair(from, to, asDefault) {
452
+ if (from && to && to < from) {
453
+ to = from;
454
+ }
455
+ $fromVisible.val(from || "");
456
+ $toVisible.val(to || "");
457
+ $from.val(from || "");
458
+ $to.val(to || "");
459
+ if (asDefault) {
460
+ if ($from[0]) {
461
+ $from[0].defaultValue = $from[0].value;
462
+ }
463
+ if ($to[0]) {
464
+ $to[0].defaultValue = $to[0].value;
465
+ }
466
+ }
467
+ }
468
+
469
+ $fromVisible.add($toVisible).on("change", function () {
470
+ writePair($fromVisible.val() || "", $toVisible.val() || "", false);
471
+ $from.trigger("change");
472
+ });
473
+
474
+ const ns = ".kgridDateRange_" + CT.uuid();
475
+ const formBind = function () {
476
+ const formEl = $from.prop("form") || ($from[0] && $from[0].form);
477
+ if (!formEl) {
478
+ return;
479
+ }
480
+ $(formEl).off("reset" + ns).on("reset" + ns, function () {
481
+ setTimeout(function () {
482
+ CT.syncDateRangeVisible($from[0]);
483
+ CT.syncDateRangeVisible($to[0]);
484
+ }, 0);
485
+ });
486
+ };
487
+ $from.data("kgridDateRangeBindReset", formBind);
488
+ formBind();
489
+ setTimeout(formBind, 0);
490
+
491
+ const range = CT.normalizeDateRangeDefault(
492
+ CT.filterDefaultValue ? CT.filterDefaultValue(config && config.default) : config && config.default
493
+ );
494
+ if (range.from || range.to) {
495
+ writePair(range.from, range.to, true);
496
+ }
497
+ }
498
+
308
499
  CT.registerFieldType("date_range", {
500
+ filterEvents: "change",
501
+ filterDebounceMs: 0,
502
+ validate(_config, mode) {
503
+ if (mode !== "filter") {
504
+ throw new Error("date_range is only valid as a filter");
505
+ }
506
+ },
309
507
  create() {
310
- const $input = $(
311
- "<input autocomplete='off' type='date' class='form-control form-control-sm'/>"
312
- );
313
- return { $input };
508
+ const $from = $("<input type='text' autocomplete='off'>")
509
+ .addClass(DATE_RANGE_VALUE_CLASS)
510
+ .attr("data-operator", ">=")
511
+ .attr("data-kgrid-date-bound", "from")
512
+ .attr("tabindex", "-1")
513
+ .attr("aria-hidden", "true");
514
+ const $to = $("<input type='text' autocomplete='off'>")
515
+ .addClass(DATE_RANGE_VALUE_CLASS)
516
+ .attr("data-operator", "<=")
517
+ .attr("data-kgrid-date-bound", "to")
518
+ .attr("tabindex", "-1")
519
+ .attr("aria-hidden", "true");
520
+ $from.data("kgridDateRangeTo", $to);
521
+ return { $input: $from, skipValueAttr: true };
522
+ },
523
+ mount(ctx) {
524
+ buildDateRangeUi(ctx.$input, ctx.config || {});
314
525
  },
315
526
  });
316
527
  })(window.KGrid);
package/src/filters.js CHANGED
@@ -125,6 +125,10 @@
125
125
  if (value == null || value === "") {
126
126
  return;
127
127
  }
128
+ if (filter.type === "date_range") {
129
+ CT.applyDateRangeDefault(formEl, col.name, value);
130
+ return;
131
+ }
128
132
  const operator =
129
133
  filter.operator || (CT.columnFilterPersisted(col) ? "=" : "~=~");
130
134
  const $existing = CT.filterFormField(formEl, col.name);
@@ -205,6 +209,18 @@
205
209
  ""
206
210
  );
207
211
  const missing = persisted.some(function (col) {
212
+ if (col.filter && col.filter.type === "date_range") {
213
+ const range = CT.normalizeDateRangeDefault(
214
+ CT.filterDefaultValue(col.filter.default)
215
+ );
216
+ if (range.from && urlFilter.indexOf(col.name + ">=") === -1) {
217
+ return true;
218
+ }
219
+ if (range.to && urlFilter.indexOf(col.name + "<=") === -1) {
220
+ return true;
221
+ }
222
+ return false;
223
+ }
208
224
  return urlFilter.indexOf(col.name + "=") === -1;
209
225
  });
210
226
  if (missing && !(extra && extra.skipInitSubmit)) {
@@ -324,25 +340,37 @@
324
340
  }
325
341
  }
326
342
 
327
- const operator =
328
- filter.operator ||
329
- (filter.type === "multi_select" ? "><" : undefined);
330
- if (operator != null) {
331
- input.attr("data-operator", operator);
332
- }
333
- input.attr("form", filterFormId);
334
- input.attr("name", col.name);
343
+ if (filter.type === "date_range") {
344
+ filterCell.find("input.kgrid-date-range-value").each(function () {
345
+ this.setAttribute("form", filterFormId);
346
+ this.setAttribute("name", col.name);
347
+ filterForm[0].appendChild(this);
348
+ });
349
+ const bindReset = input.data("kgridDateRangeBindReset");
350
+ if (typeof bindReset === "function") {
351
+ bindReset();
352
+ }
353
+ } else {
354
+ const operator =
355
+ filter.operator ||
356
+ (filter.type === "multi_select" ? "><" : undefined);
357
+ if (operator != null) {
358
+ input.attr("data-operator", operator);
359
+ }
360
+ input.attr("form", filterFormId);
361
+ input.attr("name", col.name);
335
362
 
336
- const defVal = CT.filterDefaultValue(filter.default);
337
- if (defVal != null && defVal !== "" && !input.val()) {
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);
344
- if (input[0]) {
345
- input[0].defaultValue = normalized;
363
+ const defVal = CT.filterDefaultValue(filter.default);
364
+ if (defVal != null && defVal !== "" && !input.val()) {
365
+ const normalized =
366
+ filter.type === "multi_select"
367
+ ? CT.normalizeMultiSelectValue(defVal, filter.separator)
368
+ : String(defVal);
369
+ input.val(normalized);
370
+ input.attr("data-default", normalized);
371
+ if (input[0]) {
372
+ input[0].defaultValue = normalized;
373
+ }
346
374
  }
347
375
  }
348
376
 
@@ -117,10 +117,32 @@
117
117
  if (!CT.isUserFilterColumn(col)) {
118
118
  return;
119
119
  }
120
- const $field = CT.filterFormField(form, col.name);
121
- if (!$field.length) {
120
+ let $fields = CT.filterFormFields(form, col.name);
121
+ if (!$fields.length) {
122
+ $fields = CT.filterFormField(form, col.name);
123
+ }
124
+ if (!$fields.length) {
125
+ return;
126
+ }
127
+ if ($fields.length > 1) {
128
+ $fields.each(function () {
129
+ const $el = $(this);
130
+ const bound = $el.val();
131
+ if (bound == null || bound === "" || (Array.isArray(bound) && !bound.length)) {
132
+ return;
133
+ }
134
+ out.push({
135
+ name: col.name,
136
+ value: Array.isArray(bound) ? bound : String(bound),
137
+ operator:
138
+ $el.attr("data-operator") ||
139
+ (col.filter && col.filter.operator) ||
140
+ ">=",
141
+ });
142
+ });
122
143
  return;
123
144
  }
145
+ const $field = $fields;
124
146
  const value = $field.val();
125
147
  if (value == null || value === "" || (Array.isArray(value) && !value.length)) {
126
148
  return;
@@ -155,6 +177,19 @@
155
177
  if (entry.value == null || entry.value === "") {
156
178
  return;
157
179
  }
180
+ const $bounds = CT.filterFormFields(form, entry.name);
181
+ if ($bounds.length > 1) {
182
+ if (!entry.operator) {
183
+ return;
184
+ }
185
+ const $match = $bounds.filter(function () {
186
+ return $(this).attr("data-operator") === entry.operator;
187
+ });
188
+ if ($match.length) {
189
+ $match.val(entry.value);
190
+ }
191
+ return;
192
+ }
158
193
  let $field = CT.filterFormField(form, entry.name);
159
194
  if (!$field.length) {
160
195
  CT.ensureFilterField(form, entry.name, entry.value, entry.operator);
@@ -184,6 +219,16 @@
184
219
  if (!parsed || !parsed.name || parsed.value === "") {
185
220
  return;
186
221
  }
222
+ const $bounds = CT.filterFormFields(form, parsed.name);
223
+ if ($bounds.length > 1) {
224
+ const $match = $bounds.filter(function () {
225
+ return $(this).attr("data-operator") === parsed.operator;
226
+ });
227
+ if ($match.length && !$match.val()) {
228
+ $match.val(parsed.value);
229
+ }
230
+ return;
231
+ }
187
232
  const $existing = CT.filterFormField(form, parsed.name);
188
233
  if ($existing.length && $existing.val()) {
189
234
  return;
package/styles/table.css CHANGED
@@ -44,10 +44,6 @@
44
44
  display: block;
45
45
  }
46
46
 
47
- .custom-table-shell thead {
48
- display: none;
49
- }
50
-
51
47
  .custom-table-shell tr {
52
48
  margin-top: 0.5rem;
53
49
  border: 1px solid var(--kgrid-row-border);
@@ -116,6 +112,81 @@
116
112
  .custom-table-shell td.kgrid-user-hidden {
117
113
  display: none;
118
114
  }
115
+
116
+ /* Sort links and filter controls stay usable. Column-name headers that
117
+ are not sortable stay hidden — each data cell already shows data-label.
118
+ Empty filter cells (no control) stay hidden so the card is only fields. */
119
+ .custom-table-shell .thead-labels tr,
120
+ .custom-table-shell .thead-filters tr {
121
+ display: flex;
122
+ flex-direction: column;
123
+ padding: 0.35rem 0.85rem 0.55rem;
124
+ }
125
+ .custom-table-shell .thead-labels th,
126
+ .custom-table-shell .thead-filters th {
127
+ padding: 0.4rem 0;
128
+ border: 0;
129
+ background: transparent;
130
+ white-space: normal;
131
+ overflow: visible;
132
+ }
133
+ .custom-table-shell .thead-labels th:not(:has(a.sort)):not(.kgrid-row-actions) {
134
+ display: none;
135
+ }
136
+ .custom-table-shell .thead-labels:not(:has(th:not(.kgrid-user-hidden) a.sort)):not(:has(.kgrid-column-chooser)) {
137
+ display: none;
138
+ }
139
+ .custom-table-shell .thead-labels th a.sort {
140
+ display: flex;
141
+ align-items: center;
142
+ justify-content: space-between;
143
+ gap: 0.5rem;
144
+ min-height: 2rem;
145
+ }
146
+ .custom-table-shell .thead-labels th a.sort .column-label {
147
+ min-width: 0;
148
+ overflow: hidden;
149
+ text-overflow: ellipsis;
150
+ white-space: nowrap;
151
+ }
152
+ .custom-table-shell .thead-labels th a.sort i {
153
+ flex: 0 0 auto;
154
+ }
155
+ .custom-table-shell .thead-labels th.kgrid-row-actions {
156
+ display: flex;
157
+ justify-content: flex-end;
158
+ order: -1;
159
+ padding-bottom: 0.15rem;
160
+ }
161
+ .custom-table-shell .thead-labels th.kgrid-row-actions:not(:has(.kgrid-column-chooser)) {
162
+ display: none;
163
+ }
164
+ .custom-table-shell .thead-filters th:not(:has(input, select, textarea, button)) {
165
+ display: none;
166
+ }
167
+ .custom-table-shell .thead-filters:not(:has(th:not(.kgrid-user-hidden) :is(input, select, textarea, button))) {
168
+ display: none;
169
+ }
170
+ .custom-table-shell .thead-filters th::before {
171
+ content: attr(data-label);
172
+ display: block;
173
+ font-weight: 600;
174
+ font-size: 0.78rem;
175
+ color: var(--kgrid-muted);
176
+ margin-bottom: 0.35rem;
177
+ }
178
+ .custom-table-shell .thead-filters th input:not([type="checkbox"]):not([type="radio"]):not([type="hidden"]),
179
+ .custom-table-shell .thead-filters th select,
180
+ .custom-table-shell .thead-filters th textarea,
181
+ .custom-table-shell .thead-filters th .form-control,
182
+ .custom-table-shell .thead-filters th .form-select,
183
+ .custom-table-shell .thead-filters th .kgrid-multi-select,
184
+ .custom-table-shell .thead-filters th .kgrid-date-range,
185
+ .custom-table-shell .thead-filters th .select2-container {
186
+ width: 100%;
187
+ max-width: 100%;
188
+ box-sizing: border-box;
189
+ }
119
190
  }
120
191
 
121
192
  tr.new-record-row {
@@ -424,6 +495,34 @@ th.kgrid-row-actions {
424
495
  user-select: none;
425
496
  }
426
497
 
498
+ /* date_range: from / to stacked in the filter cell */
499
+ .kgrid-date-range {
500
+ display: flex;
501
+ flex-direction: column;
502
+ gap: 0.35rem;
503
+ min-width: 0;
504
+ width: 100%;
505
+ }
506
+ .kgrid-date-range-bound {
507
+ display: flex;
508
+ flex-direction: column;
509
+ gap: 0.15rem;
510
+ margin: 0;
511
+ min-width: 0;
512
+ }
513
+ .kgrid-date-range-label {
514
+ font-size: 0.75rem;
515
+ font-weight: 600;
516
+ line-height: 1.2;
517
+ color: var(--kgrid-muted, var(--bs-secondary-color, #6c757d));
518
+ }
519
+ .kgrid-date-range input[type="date"] {
520
+ width: 100%;
521
+ max-width: 100%;
522
+ min-width: 0;
523
+ box-sizing: border-box;
524
+ }
525
+
427
526
  /* —— Visual chrome (Linear / Notion) —— */
428
527
  .custom-table {
429
528
  font-size: var(--kgrid-font-size);
@@ -441,6 +540,17 @@ th.kgrid-row-actions {
441
540
  overflow: hidden;
442
541
  text-overflow: ellipsis;
443
542
  }
543
+ /* Card mode is declared earlier; these header rules would otherwise
544
+ paint a desktop header bar and clip the sort icon. */
545
+ @container kgrid (max-width: 640px) {
546
+ .custom-table-shell .custom-table .thead-labels th,
547
+ .custom-table-shell .custom-table .thead-filters th {
548
+ background-color: transparent;
549
+ white-space: normal;
550
+ overflow: visible;
551
+ text-overflow: unset;
552
+ }
553
+ }
444
554
  .custom-table .thead-labels th a {
445
555
  color: var(--kgrid-header-link);
446
556
  text-decoration: none;