@logimaxx/kgrid 0.6.3 → 0.7.1

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,371 @@
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 doc = hidden.ownerDocument;
351
+ const panel = doc.getElementById(id + "_panel");
352
+ function boundInput(selector) {
353
+ return root.querySelector(selector) || (panel && panel.querySelector(selector));
354
+ }
355
+ const bound = hidden.getAttribute("data-kgrid-date-bound");
356
+ const visible = boundInput(
357
+ bound === "to" ? ".kgrid-date-range-to" : ".kgrid-date-range-from"
358
+ );
359
+ if (!visible) {
360
+ return;
361
+ }
362
+ const date = CT.dateRangeCalendarDate(hidden.value);
363
+ if (visible.value !== date) {
364
+ visible.value = date;
365
+ }
366
+ const display = root.querySelector(".kgrid-date-range-display");
367
+ if (!display) {
368
+ return;
369
+ }
370
+ const fromEl = boundInput(".kgrid-date-range-from");
371
+ const toEl = boundInput(".kgrid-date-range-to");
372
+ const text = formatDateRangeDisplay(fromEl && fromEl.value, toEl && toEl.value);
373
+ if (display.value !== text) {
374
+ display.value = text;
375
+ }
376
+ const clear = root.querySelector(".kgrid-date-range-clear");
377
+ if (clear) {
378
+ if (text) {
379
+ clear.removeAttribute("hidden");
380
+ root.classList.add("kgrid-date-range-has-value");
381
+ } else {
382
+ clear.setAttribute("hidden", "hidden");
383
+ root.classList.remove("kgrid-date-range-has-value");
384
+ }
385
+ }
386
+ };
387
+
388
+ /** Fill empty date-range hiddens from `{ from, to }`. Does not replace a value already set. */
389
+ CT.applyDateRangeDefault = function (formEl, name, spec) {
390
+ const range = CT.normalizeDateRangeDefault(spec);
391
+ if (!range.from && !range.to) {
392
+ return;
393
+ }
394
+ const $fields = CT.filterFormFields(formEl, name);
395
+ function fill(operator, date) {
396
+ if (!date) {
397
+ return;
398
+ }
399
+ const $match = $fields.filter(function () {
400
+ return $(this).attr("data-operator") === operator;
401
+ });
402
+ if (!$match.length || $match.val()) {
403
+ return;
404
+ }
405
+ $match.val(date);
406
+ if ($match[0]) {
407
+ $match[0].defaultValue = $match[0].value;
408
+ }
409
+ }
410
+ fill(">=", range.from);
411
+ fill("<=", range.to);
412
+ };
413
+
414
+ /*
415
+ * type=text, not hidden: for type=hidden the value property IS defaultValue,
416
+ * so form.reset() cannot restore the original bound.
417
+ */
418
+ (function installDateRangeValHooks() {
419
+ const prev = $.valHooks.text || {};
420
+ $.valHooks.text = {
421
+ get: function (elem) {
422
+ if (elem && elem.classList && elem.classList.contains(DATE_RANGE_VALUE_CLASS)) {
423
+ return elem.value;
424
+ }
425
+ return prev.get ? prev.get(elem) : undefined;
426
+ },
427
+ set: function (elem, value) {
428
+ if (elem && elem.classList && elem.classList.contains(DATE_RANGE_VALUE_CLASS)) {
429
+ const date = CT.dateRangeCalendarDate(value);
430
+ const bound = elem.getAttribute("data-kgrid-date-bound");
431
+ elem.value = bound === "to" ? CT.dateRangeEndValue(date) : date;
432
+ CT.syncDateRangeVisible(elem);
433
+ return true;
434
+ }
435
+ return prev.set ? prev.set(elem, value) : undefined;
436
+ },
437
+ };
438
+ })();
439
+
440
+ /** Closed field shows `DD.MM.YYYY`. One side omitted stays blank around the dash. */
441
+ function formatDateRangeDisplay(from, to) {
442
+ function day(iso) {
443
+ const match = /^(\d{4})-(\d{2})-(\d{2})$/.exec(iso || "");
444
+ return match ? match[3] + "." + match[2] + "." + match[1] : "";
445
+ }
446
+ const start = day(from);
447
+ const end = day(to);
448
+ if (start && end) {
449
+ return start + " – " + end;
450
+ }
451
+ if (start) {
452
+ return start + " –";
453
+ }
454
+ if (end) {
455
+ return "– " + end;
456
+ }
457
+ return "";
458
+ }
459
+
460
+ function dateRangeLabel(config, key, fallback) {
461
+ const value = config && config[key];
462
+ return value != null && value !== "" ? String(value) : fallback;
463
+ }
464
+
465
+ function buildDateRangeUi($from, config) {
466
+ const $to = $from.data("kgridDateRangeTo");
467
+ const rootId = "kgrid_dr_" + CT.uuid();
468
+ const fromLabel = dateRangeLabel(config, "fromLabel", "From");
469
+ const toLabel = dateRangeLabel(config, "toLabel", "To");
470
+ const placeholder =
471
+ config && config.placeholder != null && config.placeholder !== ""
472
+ ? String(config.placeholder)
473
+ : fromLabel + " – " + toLabel;
474
+ const $root = $("<div>", { class: "kgrid-date-range", id: rootId });
475
+ const panelId = rootId + "_panel";
476
+ const $display = $(
477
+ "<input type='text' readonly autocomplete='off' class='form-control form-control-sm kgrid-date-range-display'/>"
478
+ )
479
+ .attr("placeholder", placeholder)
480
+ .attr("role", "button")
481
+ .attr("aria-haspopup", "dialog")
482
+ .attr("aria-expanded", "false")
483
+ .attr("aria-controls", panelId);
484
+ const $clear = $("<button>", {
485
+ type: "button",
486
+ class: "kgrid-date-range-clear",
487
+ hidden: "hidden",
488
+ "aria-label": "Clear",
489
+ text: "×",
490
+ });
491
+ const $fromVisible = $(
492
+ "<input autocomplete='off' type='date' class='form-control form-control-sm kgrid-date-range-from'/>"
493
+ );
494
+ const $toVisible = $(
495
+ "<input autocomplete='off' type='date' class='form-control form-control-sm kgrid-date-range-to'/>"
496
+ );
497
+ const $panel = $("<div>", {
498
+ class: "kgrid-date-range-panel",
499
+ id: panelId,
500
+ hidden: "hidden",
501
+ role: "dialog",
502
+ "aria-label": placeholder,
503
+ });
504
+ $panel.append(
505
+ $("<label>", { class: "kgrid-date-range-bound" }).append(
506
+ $("<span>", { class: "kgrid-date-range-label", text: fromLabel }),
507
+ $fromVisible
508
+ ),
509
+ $("<label>", { class: "kgrid-date-range-bound" }).append(
510
+ $("<span>", { class: "kgrid-date-range-label", text: toLabel }),
511
+ $toVisible
512
+ )
513
+ );
514
+ $root.append($display, $clear, $panel);
515
+ $from.before($root);
516
+ $from.attr("data-kgrid-date-range", rootId);
517
+ $to.attr("data-kgrid-date-range", rootId);
518
+ $root.append($from, $to);
519
+
520
+ function writePair(from, to, asDefault) {
521
+ if (from && to && to < from) {
522
+ to = from;
523
+ }
524
+ $fromVisible.val(from || "");
525
+ $toVisible.val(to || "");
526
+ $from.val(from || "");
527
+ $to.val(to || "");
528
+ if (asDefault) {
529
+ if ($from[0]) {
530
+ $from[0].defaultValue = $from[0].value;
531
+ }
532
+ if ($to[0]) {
533
+ $to[0].defaultValue = $to[0].value;
534
+ }
535
+ }
536
+ }
537
+
538
+ function setOpen(open) {
539
+ if (open) {
540
+ $panel.appendTo(document.body);
541
+ $panel.addClass("kgrid-date-range-panel-floating");
542
+ $panel.removeAttr("hidden");
543
+ $root.addClass("kgrid-date-range-open");
544
+ $display.attr("aria-expanded", "true");
545
+ positionPanel();
546
+ } else {
547
+ $panel.attr("hidden", "hidden");
548
+ $root.removeClass("kgrid-date-range-open");
549
+ $display.attr("aria-expanded", "false");
550
+ $panel.css({ position: "", top: "", left: "", width: "", zIndex: "" });
551
+ $panel.removeClass("kgrid-date-range-panel-floating");
552
+ $panel.insertAfter($clear);
553
+ }
554
+ }
555
+
556
+ function positionPanel() {
557
+ const rect = $display[0].getBoundingClientRect();
558
+ const width = Math.min(Math.max(rect.width, 20 * 16), window.innerWidth - 16);
559
+ let left = rect.left;
560
+ const maxLeft = window.innerWidth - width - 8;
561
+ if (left > maxLeft) {
562
+ left = Math.max(8, maxLeft);
563
+ }
564
+ $panel.css({
565
+ position: "fixed",
566
+ top: rect.bottom + 2 + "px",
567
+ left: left + "px",
568
+ width: width + "px",
569
+ zIndex: 1080,
570
+ });
571
+ const height = $panel[0].offsetHeight;
572
+ let top = rect.bottom + 2;
573
+ if (top + height > window.innerHeight - 8 && rect.top > height + 8) {
574
+ top = rect.top - height - 2;
575
+ }
576
+ $panel.css("top", top + "px");
577
+ }
578
+
579
+ $fromVisible.add($toVisible).on("change", function () {
580
+ writePair($fromVisible.val() || "", $toVisible.val() || "", false);
581
+ $from.trigger("change");
582
+ });
583
+
584
+ $display.on("click", function (e) {
585
+ e.preventDefault();
586
+ setOpen(!$root.hasClass("kgrid-date-range-open"));
587
+ });
588
+ $display.on("keydown", function (e) {
589
+ if (e.key === "Enter" || e.key === " ") {
590
+ e.preventDefault();
591
+ setOpen(!$root.hasClass("kgrid-date-range-open"));
592
+ }
593
+ });
594
+ $clear.on("click", function (e) {
595
+ e.preventDefault();
596
+ e.stopPropagation();
597
+ writePair("", "", false);
598
+ $from.trigger("change");
599
+ setOpen(false);
600
+ });
601
+
602
+ const ns = ".kgridDateRange_" + CT.uuid();
603
+ $(document).on("mousedown" + ns, function (e) {
604
+ if (!$root.hasClass("kgrid-date-range-open")) {
605
+ return;
606
+ }
607
+ if ($(e.target).closest($root).length || $(e.target).closest($panel).length) {
608
+ return;
609
+ }
610
+ setOpen(false);
611
+ });
612
+ $(document).on("keydown" + ns, function (e) {
613
+ if (e.key === "Escape" && $root.hasClass("kgrid-date-range-open")) {
614
+ setOpen(false);
615
+ }
616
+ });
617
+ $(window).on("resize" + ns + " scroll" + ns, function () {
618
+ if ($root.hasClass("kgrid-date-range-open")) {
619
+ positionPanel();
620
+ }
621
+ });
622
+
623
+ const formBind = function () {
624
+ const formEl = $from.prop("form") || ($from[0] && $from[0].form);
625
+ if (!formEl) {
626
+ return;
627
+ }
628
+ $(formEl).off("reset" + ns).on("reset" + ns, function () {
629
+ setTimeout(function () {
630
+ CT.syncDateRangeVisible($from[0]);
631
+ CT.syncDateRangeVisible($to[0]);
632
+ }, 0);
633
+ });
634
+ };
635
+ $from.data("kgridDateRangeBindReset", formBind);
636
+ formBind();
637
+ setTimeout(formBind, 0);
638
+
639
+ const range = CT.normalizeDateRangeDefault(
640
+ CT.filterDefaultValue ? CT.filterDefaultValue(config && config.default) : config && config.default
641
+ );
642
+ if (range.from || range.to) {
643
+ writePair(range.from, range.to, true);
644
+ }
645
+ }
646
+
308
647
  CT.registerFieldType("date_range", {
648
+ filterEvents: "change",
649
+ filterDebounceMs: 0,
650
+ validate(_config, mode) {
651
+ if (mode !== "filter") {
652
+ throw new Error("date_range is only valid as a filter");
653
+ }
654
+ },
309
655
  create() {
310
- const $input = $(
311
- "<input autocomplete='off' type='date' class='form-control form-control-sm'/>"
312
- );
313
- return { $input };
656
+ const $from = $("<input type='text' autocomplete='off'>")
657
+ .addClass(DATE_RANGE_VALUE_CLASS)
658
+ .attr("data-operator", ">=")
659
+ .attr("data-kgrid-date-bound", "from")
660
+ .attr("tabindex", "-1")
661
+ .attr("aria-hidden", "true");
662
+ const $to = $("<input type='text' autocomplete='off'>")
663
+ .addClass(DATE_RANGE_VALUE_CLASS)
664
+ .attr("data-operator", "<=")
665
+ .attr("data-kgrid-date-bound", "to")
666
+ .attr("tabindex", "-1")
667
+ .attr("aria-hidden", "true");
668
+ $from.data("kgridDateRangeTo", $to);
669
+ return { $input: $from, skipValueAttr: true };
670
+ },
671
+ mount(ctx) {
672
+ buildDateRangeUi(ctx.$input, ctx.config || {});
314
673
  },
315
674
  });
316
675
  })(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
@@ -181,6 +181,7 @@
181
181
  .custom-table-shell .thead-filters th .form-control,
182
182
  .custom-table-shell .thead-filters th .form-select,
183
183
  .custom-table-shell .thead-filters th .kgrid-multi-select,
184
+ .custom-table-shell .thead-filters th .kgrid-date-range,
184
185
  .custom-table-shell .thead-filters th .select2-container {
185
186
  width: 100%;
186
187
  max-width: 100%;
@@ -494,6 +495,76 @@ th.kgrid-row-actions {
494
495
  user-select: none;
495
496
  }
496
497
 
498
+ /* date_range: one field in the filter cell; the two dates open in a panel */
499
+ .kgrid-date-range {
500
+ position: relative;
501
+ min-width: 0;
502
+ width: 100%;
503
+ }
504
+ .kgrid-date-range-display {
505
+ width: 100%;
506
+ max-width: 100%;
507
+ min-width: 0;
508
+ cursor: pointer;
509
+ text-overflow: ellipsis;
510
+ }
511
+ .kgrid-date-range-display[readonly] {
512
+ background-color: var(--bs-body-bg, #fff);
513
+ }
514
+ .kgrid-date-range-has-value .kgrid-date-range-display {
515
+ padding-right: 1.6rem;
516
+ }
517
+ .kgrid-date-range-clear {
518
+ position: absolute;
519
+ top: 50%;
520
+ right: 0.3rem;
521
+ transform: translateY(-50%);
522
+ border: 0;
523
+ background: transparent;
524
+ padding: 0 0.2rem;
525
+ line-height: 1;
526
+ font-size: 1rem;
527
+ color: var(--kgrid-muted, var(--bs-secondary-color, #6c757d));
528
+ cursor: pointer;
529
+ }
530
+ .kgrid-date-range-panel {
531
+ display: flex;
532
+ flex-wrap: wrap;
533
+ gap: 0.5rem;
534
+ padding: 0.5rem;
535
+ background: var(--bs-body-bg, #fff);
536
+ color: var(--bs-body-color, #212529);
537
+ border: 1px solid var(--bs-border-color, #dee2e6);
538
+ border-radius: 0.375rem;
539
+ box-sizing: border-box;
540
+ }
541
+ .kgrid-date-range-panel[hidden] {
542
+ display: none !important;
543
+ }
544
+ .kgrid-date-range-panel.kgrid-date-range-panel-floating {
545
+ box-shadow: 0 0.5rem 1rem rgba(0, 0, 0, 0.15);
546
+ }
547
+ .kgrid-date-range-bound {
548
+ display: flex;
549
+ flex: 1 1 9rem;
550
+ flex-direction: column;
551
+ gap: 0.15rem;
552
+ margin: 0;
553
+ min-width: 0;
554
+ }
555
+ .kgrid-date-range-label {
556
+ font-size: 0.75rem;
557
+ font-weight: 600;
558
+ line-height: 1.2;
559
+ color: var(--kgrid-muted, var(--bs-secondary-color, #6c757d));
560
+ }
561
+ .kgrid-date-range input[type="date"] {
562
+ width: 100%;
563
+ max-width: 100%;
564
+ min-width: 0;
565
+ box-sizing: border-box;
566
+ }
567
+
497
568
  /* —— Visual chrome (Linear / Notion) —— */
498
569
  .custom-table {
499
570
  font-size: var(--kgrid-font-size);