@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.
package/dist/kgrid.js CHANGED
@@ -1,4 +1,4 @@
1
- /*! @logimaxx/kgrid | (c) Logimaxx System SRL — proprietary | https://logimaxx.ro | built 2026-10-27T13:09:34.515Z */
1
+ /*! @logimaxx/kgrid | (c) Logimaxx System SRL — proprietary | https://logimaxx.ro | built 2026-11-03T07:47:27.707Z */
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,371 @@
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 doc = hidden.ownerDocument;
1959
+ const panel = doc.getElementById(id + "_panel");
1960
+ function boundInput(selector) {
1961
+ return root.querySelector(selector) || (panel && panel.querySelector(selector));
1962
+ }
1963
+ const bound = hidden.getAttribute("data-kgrid-date-bound");
1964
+ const visible = boundInput(
1965
+ bound === "to" ? ".kgrid-date-range-to" : ".kgrid-date-range-from"
1966
+ );
1967
+ if (!visible) {
1968
+ return;
1969
+ }
1970
+ const date = CT.dateRangeCalendarDate(hidden.value);
1971
+ if (visible.value !== date) {
1972
+ visible.value = date;
1973
+ }
1974
+ const display = root.querySelector(".kgrid-date-range-display");
1975
+ if (!display) {
1976
+ return;
1977
+ }
1978
+ const fromEl = boundInput(".kgrid-date-range-from");
1979
+ const toEl = boundInput(".kgrid-date-range-to");
1980
+ const text = formatDateRangeDisplay(fromEl && fromEl.value, toEl && toEl.value);
1981
+ if (display.value !== text) {
1982
+ display.value = text;
1983
+ }
1984
+ const clear = root.querySelector(".kgrid-date-range-clear");
1985
+ if (clear) {
1986
+ if (text) {
1987
+ clear.removeAttribute("hidden");
1988
+ root.classList.add("kgrid-date-range-has-value");
1989
+ } else {
1990
+ clear.setAttribute("hidden", "hidden");
1991
+ root.classList.remove("kgrid-date-range-has-value");
1992
+ }
1993
+ }
1994
+ };
1995
+
1996
+ /** Fill empty date-range hiddens from `{ from, to }`. Does not replace a value already set. */
1997
+ CT.applyDateRangeDefault = function (formEl, name, spec) {
1998
+ const range = CT.normalizeDateRangeDefault(spec);
1999
+ if (!range.from && !range.to) {
2000
+ return;
2001
+ }
2002
+ const $fields = CT.filterFormFields(formEl, name);
2003
+ function fill(operator, date) {
2004
+ if (!date) {
2005
+ return;
2006
+ }
2007
+ const $match = $fields.filter(function () {
2008
+ return $(this).attr("data-operator") === operator;
2009
+ });
2010
+ if (!$match.length || $match.val()) {
2011
+ return;
2012
+ }
2013
+ $match.val(date);
2014
+ if ($match[0]) {
2015
+ $match[0].defaultValue = $match[0].value;
2016
+ }
2017
+ }
2018
+ fill(">=", range.from);
2019
+ fill("<=", range.to);
2020
+ };
2021
+
2022
+ /*
2023
+ * type=text, not hidden: for type=hidden the value property IS defaultValue,
2024
+ * so form.reset() cannot restore the original bound.
2025
+ */
2026
+ (function installDateRangeValHooks() {
2027
+ const prev = $.valHooks.text || {};
2028
+ $.valHooks.text = {
2029
+ get: function (elem) {
2030
+ if (elem && elem.classList && elem.classList.contains(DATE_RANGE_VALUE_CLASS)) {
2031
+ return elem.value;
2032
+ }
2033
+ return prev.get ? prev.get(elem) : undefined;
2034
+ },
2035
+ set: function (elem, value) {
2036
+ if (elem && elem.classList && elem.classList.contains(DATE_RANGE_VALUE_CLASS)) {
2037
+ const date = CT.dateRangeCalendarDate(value);
2038
+ const bound = elem.getAttribute("data-kgrid-date-bound");
2039
+ elem.value = bound === "to" ? CT.dateRangeEndValue(date) : date;
2040
+ CT.syncDateRangeVisible(elem);
2041
+ return true;
2042
+ }
2043
+ return prev.set ? prev.set(elem, value) : undefined;
2044
+ },
2045
+ };
2046
+ })();
2047
+
2048
+ /** Closed field shows `DD.MM.YYYY`. One side omitted stays blank around the dash. */
2049
+ function formatDateRangeDisplay(from, to) {
2050
+ function day(iso) {
2051
+ const match = /^(\d{4})-(\d{2})-(\d{2})$/.exec(iso || "");
2052
+ return match ? match[3] + "." + match[2] + "." + match[1] : "";
2053
+ }
2054
+ const start = day(from);
2055
+ const end = day(to);
2056
+ if (start && end) {
2057
+ return start + " – " + end;
2058
+ }
2059
+ if (start) {
2060
+ return start + " –";
2061
+ }
2062
+ if (end) {
2063
+ return "– " + end;
2064
+ }
2065
+ return "";
2066
+ }
2067
+
2068
+ function dateRangeLabel(config, key, fallback) {
2069
+ const value = config && config[key];
2070
+ return value != null && value !== "" ? String(value) : fallback;
2071
+ }
2072
+
2073
+ function buildDateRangeUi($from, config) {
2074
+ const $to = $from.data("kgridDateRangeTo");
2075
+ const rootId = "kgrid_dr_" + CT.uuid();
2076
+ const fromLabel = dateRangeLabel(config, "fromLabel", "From");
2077
+ const toLabel = dateRangeLabel(config, "toLabel", "To");
2078
+ const placeholder =
2079
+ config && config.placeholder != null && config.placeholder !== ""
2080
+ ? String(config.placeholder)
2081
+ : fromLabel + " – " + toLabel;
2082
+ const $root = $("<div>", { class: "kgrid-date-range", id: rootId });
2083
+ const panelId = rootId + "_panel";
2084
+ const $display = $(
2085
+ "<input type='text' readonly autocomplete='off' class='form-control form-control-sm kgrid-date-range-display'/>"
2086
+ )
2087
+ .attr("placeholder", placeholder)
2088
+ .attr("role", "button")
2089
+ .attr("aria-haspopup", "dialog")
2090
+ .attr("aria-expanded", "false")
2091
+ .attr("aria-controls", panelId);
2092
+ const $clear = $("<button>", {
2093
+ type: "button",
2094
+ class: "kgrid-date-range-clear",
2095
+ hidden: "hidden",
2096
+ "aria-label": "Clear",
2097
+ text: "×",
2098
+ });
2099
+ const $fromVisible = $(
2100
+ "<input autocomplete='off' type='date' class='form-control form-control-sm kgrid-date-range-from'/>"
2101
+ );
2102
+ const $toVisible = $(
2103
+ "<input autocomplete='off' type='date' class='form-control form-control-sm kgrid-date-range-to'/>"
2104
+ );
2105
+ const $panel = $("<div>", {
2106
+ class: "kgrid-date-range-panel",
2107
+ id: panelId,
2108
+ hidden: "hidden",
2109
+ role: "dialog",
2110
+ "aria-label": placeholder,
2111
+ });
2112
+ $panel.append(
2113
+ $("<label>", { class: "kgrid-date-range-bound" }).append(
2114
+ $("<span>", { class: "kgrid-date-range-label", text: fromLabel }),
2115
+ $fromVisible
2116
+ ),
2117
+ $("<label>", { class: "kgrid-date-range-bound" }).append(
2118
+ $("<span>", { class: "kgrid-date-range-label", text: toLabel }),
2119
+ $toVisible
2120
+ )
2121
+ );
2122
+ $root.append($display, $clear, $panel);
2123
+ $from.before($root);
2124
+ $from.attr("data-kgrid-date-range", rootId);
2125
+ $to.attr("data-kgrid-date-range", rootId);
2126
+ $root.append($from, $to);
2127
+
2128
+ function writePair(from, to, asDefault) {
2129
+ if (from && to && to < from) {
2130
+ to = from;
2131
+ }
2132
+ $fromVisible.val(from || "");
2133
+ $toVisible.val(to || "");
2134
+ $from.val(from || "");
2135
+ $to.val(to || "");
2136
+ if (asDefault) {
2137
+ if ($from[0]) {
2138
+ $from[0].defaultValue = $from[0].value;
2139
+ }
2140
+ if ($to[0]) {
2141
+ $to[0].defaultValue = $to[0].value;
2142
+ }
2143
+ }
2144
+ }
2145
+
2146
+ function setOpen(open) {
2147
+ if (open) {
2148
+ $panel.appendTo(document.body);
2149
+ $panel.addClass("kgrid-date-range-panel-floating");
2150
+ $panel.removeAttr("hidden");
2151
+ $root.addClass("kgrid-date-range-open");
2152
+ $display.attr("aria-expanded", "true");
2153
+ positionPanel();
2154
+ } else {
2155
+ $panel.attr("hidden", "hidden");
2156
+ $root.removeClass("kgrid-date-range-open");
2157
+ $display.attr("aria-expanded", "false");
2158
+ $panel.css({ position: "", top: "", left: "", width: "", zIndex: "" });
2159
+ $panel.removeClass("kgrid-date-range-panel-floating");
2160
+ $panel.insertAfter($clear);
2161
+ }
2162
+ }
2163
+
2164
+ function positionPanel() {
2165
+ const rect = $display[0].getBoundingClientRect();
2166
+ const width = Math.min(Math.max(rect.width, 20 * 16), window.innerWidth - 16);
2167
+ let left = rect.left;
2168
+ const maxLeft = window.innerWidth - width - 8;
2169
+ if (left > maxLeft) {
2170
+ left = Math.max(8, maxLeft);
2171
+ }
2172
+ $panel.css({
2173
+ position: "fixed",
2174
+ top: rect.bottom + 2 + "px",
2175
+ left: left + "px",
2176
+ width: width + "px",
2177
+ zIndex: 1080,
2178
+ });
2179
+ const height = $panel[0].offsetHeight;
2180
+ let top = rect.bottom + 2;
2181
+ if (top + height > window.innerHeight - 8 && rect.top > height + 8) {
2182
+ top = rect.top - height - 2;
2183
+ }
2184
+ $panel.css("top", top + "px");
2185
+ }
2186
+
2187
+ $fromVisible.add($toVisible).on("change", function () {
2188
+ writePair($fromVisible.val() || "", $toVisible.val() || "", false);
2189
+ $from.trigger("change");
2190
+ });
2191
+
2192
+ $display.on("click", function (e) {
2193
+ e.preventDefault();
2194
+ setOpen(!$root.hasClass("kgrid-date-range-open"));
2195
+ });
2196
+ $display.on("keydown", function (e) {
2197
+ if (e.key === "Enter" || e.key === " ") {
2198
+ e.preventDefault();
2199
+ setOpen(!$root.hasClass("kgrid-date-range-open"));
2200
+ }
2201
+ });
2202
+ $clear.on("click", function (e) {
2203
+ e.preventDefault();
2204
+ e.stopPropagation();
2205
+ writePair("", "", false);
2206
+ $from.trigger("change");
2207
+ setOpen(false);
2208
+ });
2209
+
2210
+ const ns = ".kgridDateRange_" + CT.uuid();
2211
+ $(document).on("mousedown" + ns, function (e) {
2212
+ if (!$root.hasClass("kgrid-date-range-open")) {
2213
+ return;
2214
+ }
2215
+ if ($(e.target).closest($root).length || $(e.target).closest($panel).length) {
2216
+ return;
2217
+ }
2218
+ setOpen(false);
2219
+ });
2220
+ $(document).on("keydown" + ns, function (e) {
2221
+ if (e.key === "Escape" && $root.hasClass("kgrid-date-range-open")) {
2222
+ setOpen(false);
2223
+ }
2224
+ });
2225
+ $(window).on("resize" + ns + " scroll" + ns, function () {
2226
+ if ($root.hasClass("kgrid-date-range-open")) {
2227
+ positionPanel();
2228
+ }
2229
+ });
2230
+
2231
+ const formBind = function () {
2232
+ const formEl = $from.prop("form") || ($from[0] && $from[0].form);
2233
+ if (!formEl) {
2234
+ return;
2235
+ }
2236
+ $(formEl).off("reset" + ns).on("reset" + ns, function () {
2237
+ setTimeout(function () {
2238
+ CT.syncDateRangeVisible($from[0]);
2239
+ CT.syncDateRangeVisible($to[0]);
2240
+ }, 0);
2241
+ });
2242
+ };
2243
+ $from.data("kgridDateRangeBindReset", formBind);
2244
+ formBind();
2245
+ setTimeout(formBind, 0);
2246
+
2247
+ const range = CT.normalizeDateRangeDefault(
2248
+ CT.filterDefaultValue ? CT.filterDefaultValue(config && config.default) : config && config.default
2249
+ );
2250
+ if (range.from || range.to) {
2251
+ writePair(range.from, range.to, true);
2252
+ }
2253
+ }
2254
+
1903
2255
  CT.registerFieldType("date_range", {
2256
+ filterEvents: "change",
2257
+ filterDebounceMs: 0,
2258
+ validate(_config, mode) {
2259
+ if (mode !== "filter") {
2260
+ throw new Error("date_range is only valid as a filter");
2261
+ }
2262
+ },
1904
2263
  create() {
1905
- const $input = $(
1906
- "<input autocomplete='off' type='date' class='form-control form-control-sm'/>"
1907
- );
1908
- return { $input };
2264
+ const $from = $("<input type='text' autocomplete='off'>")
2265
+ .addClass(DATE_RANGE_VALUE_CLASS)
2266
+ .attr("data-operator", ">=")
2267
+ .attr("data-kgrid-date-bound", "from")
2268
+ .attr("tabindex", "-1")
2269
+ .attr("aria-hidden", "true");
2270
+ const $to = $("<input type='text' autocomplete='off'>")
2271
+ .addClass(DATE_RANGE_VALUE_CLASS)
2272
+ .attr("data-operator", "<=")
2273
+ .attr("data-kgrid-date-bound", "to")
2274
+ .attr("tabindex", "-1")
2275
+ .attr("aria-hidden", "true");
2276
+ $from.data("kgridDateRangeTo", $to);
2277
+ return { $input: $from, skipValueAttr: true };
2278
+ },
2279
+ mount(ctx) {
2280
+ buildDateRangeUi(ctx.$input, ctx.config || {});
1909
2281
  },
1910
2282
  });
1911
2283
  })(window.KGrid);
@@ -2145,6 +2517,10 @@
2145
2517
  if (value == null || value === "") {
2146
2518
  return;
2147
2519
  }
2520
+ if (filter.type === "date_range") {
2521
+ CT.applyDateRangeDefault(formEl, col.name, value);
2522
+ return;
2523
+ }
2148
2524
  const operator =
2149
2525
  filter.operator || (CT.columnFilterPersisted(col) ? "=" : "~=~");
2150
2526
  const $existing = CT.filterFormField(formEl, col.name);
@@ -2225,6 +2601,18 @@
2225
2601
  ""
2226
2602
  );
2227
2603
  const missing = persisted.some(function (col) {
2604
+ if (col.filter && col.filter.type === "date_range") {
2605
+ const range = CT.normalizeDateRangeDefault(
2606
+ CT.filterDefaultValue(col.filter.default)
2607
+ );
2608
+ if (range.from && urlFilter.indexOf(col.name + ">=") === -1) {
2609
+ return true;
2610
+ }
2611
+ if (range.to && urlFilter.indexOf(col.name + "<=") === -1) {
2612
+ return true;
2613
+ }
2614
+ return false;
2615
+ }
2228
2616
  return urlFilter.indexOf(col.name + "=") === -1;
2229
2617
  });
2230
2618
  if (missing && !(extra && extra.skipInitSubmit)) {
@@ -2344,25 +2732,37 @@
2344
2732
  }
2345
2733
  }
2346
2734
 
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;
2735
+ if (filter.type === "date_range") {
2736
+ filterCell.find("input.kgrid-date-range-value").each(function () {
2737
+ this.setAttribute("form", filterFormId);
2738
+ this.setAttribute("name", col.name);
2739
+ filterForm[0].appendChild(this);
2740
+ });
2741
+ const bindReset = input.data("kgridDateRangeBindReset");
2742
+ if (typeof bindReset === "function") {
2743
+ bindReset();
2744
+ }
2745
+ } else {
2746
+ const operator =
2747
+ filter.operator ||
2748
+ (filter.type === "multi_select" ? "><" : undefined);
2749
+ if (operator != null) {
2750
+ input.attr("data-operator", operator);
2751
+ }
2752
+ input.attr("form", filterFormId);
2753
+ input.attr("name", col.name);
2754
+
2755
+ const defVal = CT.filterDefaultValue(filter.default);
2756
+ if (defVal != null && defVal !== "" && !input.val()) {
2757
+ const normalized =
2758
+ filter.type === "multi_select"
2759
+ ? CT.normalizeMultiSelectValue(defVal, filter.separator)
2760
+ : String(defVal);
2761
+ input.val(normalized);
2762
+ input.attr("data-default", normalized);
2763
+ if (input[0]) {
2764
+ input[0].defaultValue = normalized;
2765
+ }
2366
2766
  }
2367
2767
  }
2368
2768
 
@@ -3147,10 +3547,32 @@
3147
3547
  if (!CT.isUserFilterColumn(col)) {
3148
3548
  return;
3149
3549
  }
3150
- const $field = CT.filterFormField(form, col.name);
3151
- if (!$field.length) {
3550
+ let $fields = CT.filterFormFields(form, col.name);
3551
+ if (!$fields.length) {
3552
+ $fields = CT.filterFormField(form, col.name);
3553
+ }
3554
+ if (!$fields.length) {
3152
3555
  return;
3153
3556
  }
3557
+ if ($fields.length > 1) {
3558
+ $fields.each(function () {
3559
+ const $el = $(this);
3560
+ const bound = $el.val();
3561
+ if (bound == null || bound === "" || (Array.isArray(bound) && !bound.length)) {
3562
+ return;
3563
+ }
3564
+ out.push({
3565
+ name: col.name,
3566
+ value: Array.isArray(bound) ? bound : String(bound),
3567
+ operator:
3568
+ $el.attr("data-operator") ||
3569
+ (col.filter && col.filter.operator) ||
3570
+ ">=",
3571
+ });
3572
+ });
3573
+ return;
3574
+ }
3575
+ const $field = $fields;
3154
3576
  const value = $field.val();
3155
3577
  if (value == null || value === "" || (Array.isArray(value) && !value.length)) {
3156
3578
  return;
@@ -3185,6 +3607,19 @@
3185
3607
  if (entry.value == null || entry.value === "") {
3186
3608
  return;
3187
3609
  }
3610
+ const $bounds = CT.filterFormFields(form, entry.name);
3611
+ if ($bounds.length > 1) {
3612
+ if (!entry.operator) {
3613
+ return;
3614
+ }
3615
+ const $match = $bounds.filter(function () {
3616
+ return $(this).attr("data-operator") === entry.operator;
3617
+ });
3618
+ if ($match.length) {
3619
+ $match.val(entry.value);
3620
+ }
3621
+ return;
3622
+ }
3188
3623
  let $field = CT.filterFormField(form, entry.name);
3189
3624
  if (!$field.length) {
3190
3625
  CT.ensureFilterField(form, entry.name, entry.value, entry.operator);
@@ -3214,6 +3649,16 @@
3214
3649
  if (!parsed || !parsed.name || parsed.value === "") {
3215
3650
  return;
3216
3651
  }
3652
+ const $bounds = CT.filterFormFields(form, parsed.name);
3653
+ if ($bounds.length > 1) {
3654
+ const $match = $bounds.filter(function () {
3655
+ return $(this).attr("data-operator") === parsed.operator;
3656
+ });
3657
+ if ($match.length && !$match.val()) {
3658
+ $match.val(parsed.value);
3659
+ }
3660
+ return;
3661
+ }
3217
3662
  const $existing = CT.filterFormField(form, parsed.name);
3218
3663
  if ($existing.length && $existing.val()) {
3219
3664
  return;