@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.
package/dist/kgrid.js CHANGED
@@ -1,4 +1,4 @@
1
- /*! @logimaxx/kgrid | (c) Logimaxx System SRL — proprietary | https://logimaxx.ro | built 2026-10-27T05:18:09.983Z */
1
+ /*! @logimaxx/kgrid | (c) Logimaxx System SRL — proprietary | https://logimaxx.ro | built 2026-10-28T14:23:09.350Z */
2
2
 
3
3
  /* --- configure.js --- */
4
4
  /**
@@ -670,20 +670,33 @@
670
670
 
671
671
  /** Field in filter row (associated via form="" on control, not nested inside <form>). */
672
672
  CT.filterFormField = function (formEl, name) {
673
+ const $fields = CT.filterFormFields(formEl, name);
674
+ return $fields.length ? $fields.eq(0) : $();
675
+ };
676
+
677
+ /** Every control with this name. Two date-range bounds share a name and differ by operator. */
678
+ CT.filterFormFields = function (formEl, name) {
673
679
  if (!formEl || !name) {
674
680
  return $();
675
681
  }
676
- const el = formEl.elements && formEl.elements.namedItem(name);
677
- if (el) {
678
- return $(el);
682
+ const list = [];
683
+ const els = formEl.elements;
684
+ if (els) {
685
+ for (let i = 0; i < els.length; i++) {
686
+ if (els[i] && els[i].name === name) {
687
+ list.push(els[i]);
688
+ }
689
+ }
679
690
  }
680
- if (formEl.id) {
681
- const associated = document.querySelector(`[form="${formEl.id}"][name="${name}"]`);
682
- if (associated) {
683
- return $(associated);
691
+ if (!list.length && formEl.id) {
692
+ const nodes = document.querySelectorAll(
693
+ '[form="' + formEl.id + '"][name="' + name + '"]'
694
+ );
695
+ for (let i = 0; i < nodes.length; i++) {
696
+ list.push(nodes[i]);
684
697
  }
685
698
  }
686
- return $();
699
+ return $(list);
687
700
  };
688
701
 
689
702
  /** Resolved CSS class for a column (`class` or alias `columnClass`). */
@@ -1900,12 +1913,223 @@
1900
1913
  },
1901
1914
  });
1902
1915
 
1916
+ const DATE_RANGE_VALUE_CLASS = "kgrid-date-range-value";
1917
+
1918
+ /** Calendar day from a date or `YYYY-MM-DD HH:MM:SS` filter value. */
1919
+ CT.dateRangeCalendarDate = function (value) {
1920
+ if (value == null || value === "") {
1921
+ return "";
1922
+ }
1923
+ const match = /^(\d{4}-\d{2}-\d{2})/.exec(String(value).trim());
1924
+ return match ? match[1] : "";
1925
+ };
1926
+
1927
+ /** Inclusive end of a calendar day for DATETIME columns. */
1928
+ CT.dateRangeEndValue = function (date) {
1929
+ return date ? date + " 23:59:59" : "";
1930
+ };
1931
+
1932
+ /**
1933
+ * `{ from, to }` calendar dates. If `to` is before `from`, `to` becomes `from`.
1934
+ * @param {*} spec
1935
+ * @returns {{ from: string, to: string }}
1936
+ */
1937
+ CT.normalizeDateRangeDefault = function (spec) {
1938
+ if (spec == null || typeof spec !== "object" || Array.isArray(spec)) {
1939
+ return { from: "", to: "" };
1940
+ }
1941
+ let from = CT.dateRangeCalendarDate(spec.from);
1942
+ let to = CT.dateRangeCalendarDate(spec.to);
1943
+ if (from && to && to < from) {
1944
+ to = from;
1945
+ }
1946
+ return { from: from, to: to };
1947
+ };
1948
+
1949
+ CT.syncDateRangeVisible = function (hidden) {
1950
+ if (!hidden || !hidden.getAttribute) {
1951
+ return;
1952
+ }
1953
+ const id = hidden.getAttribute("data-kgrid-date-range");
1954
+ const root = id && hidden.ownerDocument.getElementById(id);
1955
+ if (!root) {
1956
+ return;
1957
+ }
1958
+ const bound = hidden.getAttribute("data-kgrid-date-bound");
1959
+ const visible = root.querySelector(
1960
+ bound === "to" ? ".kgrid-date-range-to" : ".kgrid-date-range-from"
1961
+ );
1962
+ if (!visible) {
1963
+ return;
1964
+ }
1965
+ const date = CT.dateRangeCalendarDate(hidden.value);
1966
+ if (visible.value !== date) {
1967
+ visible.value = date;
1968
+ }
1969
+ };
1970
+
1971
+ /** Fill empty date-range hiddens from `{ from, to }`. Does not replace a value already set. */
1972
+ CT.applyDateRangeDefault = function (formEl, name, spec) {
1973
+ const range = CT.normalizeDateRangeDefault(spec);
1974
+ if (!range.from && !range.to) {
1975
+ return;
1976
+ }
1977
+ const $fields = CT.filterFormFields(formEl, name);
1978
+ function fill(operator, date) {
1979
+ if (!date) {
1980
+ return;
1981
+ }
1982
+ const $match = $fields.filter(function () {
1983
+ return $(this).attr("data-operator") === operator;
1984
+ });
1985
+ if (!$match.length || $match.val()) {
1986
+ return;
1987
+ }
1988
+ $match.val(date);
1989
+ if ($match[0]) {
1990
+ $match[0].defaultValue = $match[0].value;
1991
+ }
1992
+ }
1993
+ fill(">=", range.from);
1994
+ fill("<=", range.to);
1995
+ };
1996
+
1997
+ /*
1998
+ * type=text, not hidden: for type=hidden the value property IS defaultValue,
1999
+ * so form.reset() cannot restore the original bound.
2000
+ */
2001
+ (function installDateRangeValHooks() {
2002
+ const prev = $.valHooks.text || {};
2003
+ $.valHooks.text = {
2004
+ get: function (elem) {
2005
+ if (elem && elem.classList && elem.classList.contains(DATE_RANGE_VALUE_CLASS)) {
2006
+ return elem.value;
2007
+ }
2008
+ return prev.get ? prev.get(elem) : undefined;
2009
+ },
2010
+ set: function (elem, value) {
2011
+ if (elem && elem.classList && elem.classList.contains(DATE_RANGE_VALUE_CLASS)) {
2012
+ const date = CT.dateRangeCalendarDate(value);
2013
+ const bound = elem.getAttribute("data-kgrid-date-bound");
2014
+ elem.value = bound === "to" ? CT.dateRangeEndValue(date) : date;
2015
+ CT.syncDateRangeVisible(elem);
2016
+ return true;
2017
+ }
2018
+ return prev.set ? prev.set(elem, value) : undefined;
2019
+ },
2020
+ };
2021
+ })();
2022
+
2023
+ function dateRangeLabel(config, key, fallback) {
2024
+ const value = config && config[key];
2025
+ return value != null && value !== "" ? String(value) : fallback;
2026
+ }
2027
+
2028
+ function buildDateRangeUi($from, config) {
2029
+ const $to = $from.data("kgridDateRangeTo");
2030
+ const rootId = "kgrid_dr_" + CT.uuid();
2031
+ const $root = $("<div>", { class: "kgrid-date-range", id: rootId });
2032
+ const $fromVisible = $(
2033
+ "<input autocomplete='off' type='date' class='form-control form-control-sm kgrid-date-range-from'/>"
2034
+ );
2035
+ const $toVisible = $(
2036
+ "<input autocomplete='off' type='date' class='form-control form-control-sm kgrid-date-range-to'/>"
2037
+ );
2038
+ $root.append(
2039
+ $("<label>", { class: "kgrid-date-range-bound" }).append(
2040
+ $("<span>", {
2041
+ class: "kgrid-date-range-label",
2042
+ text: dateRangeLabel(config, "fromLabel", "From"),
2043
+ }),
2044
+ $fromVisible
2045
+ ),
2046
+ $("<label>", { class: "kgrid-date-range-bound" }).append(
2047
+ $("<span>", {
2048
+ class: "kgrid-date-range-label",
2049
+ text: dateRangeLabel(config, "toLabel", "To"),
2050
+ }),
2051
+ $toVisible
2052
+ )
2053
+ );
2054
+ $from.before($root);
2055
+ $from.attr("data-kgrid-date-range", rootId);
2056
+ $to.attr("data-kgrid-date-range", rootId);
2057
+ $root.append($from, $to);
2058
+
2059
+ function writePair(from, to, asDefault) {
2060
+ if (from && to && to < from) {
2061
+ to = from;
2062
+ }
2063
+ $fromVisible.val(from || "");
2064
+ $toVisible.val(to || "");
2065
+ $from.val(from || "");
2066
+ $to.val(to || "");
2067
+ if (asDefault) {
2068
+ if ($from[0]) {
2069
+ $from[0].defaultValue = $from[0].value;
2070
+ }
2071
+ if ($to[0]) {
2072
+ $to[0].defaultValue = $to[0].value;
2073
+ }
2074
+ }
2075
+ }
2076
+
2077
+ $fromVisible.add($toVisible).on("change", function () {
2078
+ writePair($fromVisible.val() || "", $toVisible.val() || "", false);
2079
+ $from.trigger("change");
2080
+ });
2081
+
2082
+ const ns = ".kgridDateRange_" + CT.uuid();
2083
+ const formBind = function () {
2084
+ const formEl = $from.prop("form") || ($from[0] && $from[0].form);
2085
+ if (!formEl) {
2086
+ return;
2087
+ }
2088
+ $(formEl).off("reset" + ns).on("reset" + ns, function () {
2089
+ setTimeout(function () {
2090
+ CT.syncDateRangeVisible($from[0]);
2091
+ CT.syncDateRangeVisible($to[0]);
2092
+ }, 0);
2093
+ });
2094
+ };
2095
+ $from.data("kgridDateRangeBindReset", formBind);
2096
+ formBind();
2097
+ setTimeout(formBind, 0);
2098
+
2099
+ const range = CT.normalizeDateRangeDefault(
2100
+ CT.filterDefaultValue ? CT.filterDefaultValue(config && config.default) : config && config.default
2101
+ );
2102
+ if (range.from || range.to) {
2103
+ writePair(range.from, range.to, true);
2104
+ }
2105
+ }
2106
+
1903
2107
  CT.registerFieldType("date_range", {
2108
+ filterEvents: "change",
2109
+ filterDebounceMs: 0,
2110
+ validate(_config, mode) {
2111
+ if (mode !== "filter") {
2112
+ throw new Error("date_range is only valid as a filter");
2113
+ }
2114
+ },
1904
2115
  create() {
1905
- const $input = $(
1906
- "<input autocomplete='off' type='date' class='form-control form-control-sm'/>"
1907
- );
1908
- return { $input };
2116
+ const $from = $("<input type='text' autocomplete='off'>")
2117
+ .addClass(DATE_RANGE_VALUE_CLASS)
2118
+ .attr("data-operator", ">=")
2119
+ .attr("data-kgrid-date-bound", "from")
2120
+ .attr("tabindex", "-1")
2121
+ .attr("aria-hidden", "true");
2122
+ const $to = $("<input type='text' autocomplete='off'>")
2123
+ .addClass(DATE_RANGE_VALUE_CLASS)
2124
+ .attr("data-operator", "<=")
2125
+ .attr("data-kgrid-date-bound", "to")
2126
+ .attr("tabindex", "-1")
2127
+ .attr("aria-hidden", "true");
2128
+ $from.data("kgridDateRangeTo", $to);
2129
+ return { $input: $from, skipValueAttr: true };
2130
+ },
2131
+ mount(ctx) {
2132
+ buildDateRangeUi(ctx.$input, ctx.config || {});
1909
2133
  },
1910
2134
  });
1911
2135
  })(window.KGrid);
@@ -2145,6 +2369,10 @@
2145
2369
  if (value == null || value === "") {
2146
2370
  return;
2147
2371
  }
2372
+ if (filter.type === "date_range") {
2373
+ CT.applyDateRangeDefault(formEl, col.name, value);
2374
+ return;
2375
+ }
2148
2376
  const operator =
2149
2377
  filter.operator || (CT.columnFilterPersisted(col) ? "=" : "~=~");
2150
2378
  const $existing = CT.filterFormField(formEl, col.name);
@@ -2225,6 +2453,18 @@
2225
2453
  ""
2226
2454
  );
2227
2455
  const missing = persisted.some(function (col) {
2456
+ if (col.filter && col.filter.type === "date_range") {
2457
+ const range = CT.normalizeDateRangeDefault(
2458
+ CT.filterDefaultValue(col.filter.default)
2459
+ );
2460
+ if (range.from && urlFilter.indexOf(col.name + ">=") === -1) {
2461
+ return true;
2462
+ }
2463
+ if (range.to && urlFilter.indexOf(col.name + "<=") === -1) {
2464
+ return true;
2465
+ }
2466
+ return false;
2467
+ }
2228
2468
  return urlFilter.indexOf(col.name + "=") === -1;
2229
2469
  });
2230
2470
  if (missing && !(extra && extra.skipInitSubmit)) {
@@ -2344,25 +2584,37 @@
2344
2584
  }
2345
2585
  }
2346
2586
 
2347
- const operator =
2348
- filter.operator ||
2349
- (filter.type === "multi_select" ? "><" : undefined);
2350
- if (operator != null) {
2351
- input.attr("data-operator", operator);
2352
- }
2353
- input.attr("form", filterFormId);
2354
- input.attr("name", col.name);
2355
-
2356
- const defVal = CT.filterDefaultValue(filter.default);
2357
- if (defVal != null && defVal !== "" && !input.val()) {
2358
- const normalized =
2359
- filter.type === "multi_select"
2360
- ? CT.normalizeMultiSelectValue(defVal, filter.separator)
2361
- : String(defVal);
2362
- input.val(normalized);
2363
- input.attr("data-default", normalized);
2364
- if (input[0]) {
2365
- input[0].defaultValue = normalized;
2587
+ if (filter.type === "date_range") {
2588
+ filterCell.find("input.kgrid-date-range-value").each(function () {
2589
+ this.setAttribute("form", filterFormId);
2590
+ this.setAttribute("name", col.name);
2591
+ filterForm[0].appendChild(this);
2592
+ });
2593
+ const bindReset = input.data("kgridDateRangeBindReset");
2594
+ if (typeof bindReset === "function") {
2595
+ bindReset();
2596
+ }
2597
+ } else {
2598
+ const operator =
2599
+ filter.operator ||
2600
+ (filter.type === "multi_select" ? "><" : undefined);
2601
+ if (operator != null) {
2602
+ input.attr("data-operator", operator);
2603
+ }
2604
+ input.attr("form", filterFormId);
2605
+ input.attr("name", col.name);
2606
+
2607
+ const defVal = CT.filterDefaultValue(filter.default);
2608
+ if (defVal != null && defVal !== "" && !input.val()) {
2609
+ const normalized =
2610
+ filter.type === "multi_select"
2611
+ ? CT.normalizeMultiSelectValue(defVal, filter.separator)
2612
+ : String(defVal);
2613
+ input.val(normalized);
2614
+ input.attr("data-default", normalized);
2615
+ if (input[0]) {
2616
+ input[0].defaultValue = normalized;
2617
+ }
2366
2618
  }
2367
2619
  }
2368
2620
 
@@ -3147,10 +3399,32 @@
3147
3399
  if (!CT.isUserFilterColumn(col)) {
3148
3400
  return;
3149
3401
  }
3150
- const $field = CT.filterFormField(form, col.name);
3151
- if (!$field.length) {
3402
+ let $fields = CT.filterFormFields(form, col.name);
3403
+ if (!$fields.length) {
3404
+ $fields = CT.filterFormField(form, col.name);
3405
+ }
3406
+ if (!$fields.length) {
3152
3407
  return;
3153
3408
  }
3409
+ if ($fields.length > 1) {
3410
+ $fields.each(function () {
3411
+ const $el = $(this);
3412
+ const bound = $el.val();
3413
+ if (bound == null || bound === "" || (Array.isArray(bound) && !bound.length)) {
3414
+ return;
3415
+ }
3416
+ out.push({
3417
+ name: col.name,
3418
+ value: Array.isArray(bound) ? bound : String(bound),
3419
+ operator:
3420
+ $el.attr("data-operator") ||
3421
+ (col.filter && col.filter.operator) ||
3422
+ ">=",
3423
+ });
3424
+ });
3425
+ return;
3426
+ }
3427
+ const $field = $fields;
3154
3428
  const value = $field.val();
3155
3429
  if (value == null || value === "" || (Array.isArray(value) && !value.length)) {
3156
3430
  return;
@@ -3185,6 +3459,19 @@
3185
3459
  if (entry.value == null || entry.value === "") {
3186
3460
  return;
3187
3461
  }
3462
+ const $bounds = CT.filterFormFields(form, entry.name);
3463
+ if ($bounds.length > 1) {
3464
+ if (!entry.operator) {
3465
+ return;
3466
+ }
3467
+ const $match = $bounds.filter(function () {
3468
+ return $(this).attr("data-operator") === entry.operator;
3469
+ });
3470
+ if ($match.length) {
3471
+ $match.val(entry.value);
3472
+ }
3473
+ return;
3474
+ }
3188
3475
  let $field = CT.filterFormField(form, entry.name);
3189
3476
  if (!$field.length) {
3190
3477
  CT.ensureFilterField(form, entry.name, entry.value, entry.operator);
@@ -3214,6 +3501,16 @@
3214
3501
  if (!parsed || !parsed.name || parsed.value === "") {
3215
3502
  return;
3216
3503
  }
3504
+ const $bounds = CT.filterFormFields(form, parsed.name);
3505
+ if ($bounds.length > 1) {
3506
+ const $match = $bounds.filter(function () {
3507
+ return $(this).attr("data-operator") === parsed.operator;
3508
+ });
3509
+ if ($match.length && !$match.val()) {
3510
+ $match.val(parsed.value);
3511
+ }
3512
+ return;
3513
+ }
3217
3514
  const $existing = CT.filterFormField(form, parsed.name);
3218
3515
  if ($existing.length && $existing.val()) {
3219
3516
  return;