@logimaxx/kgrid 0.6.3 → 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 +331 -34
- package/dist/kgrid.min.js +2 -2
- package/docs/configuration.md +5 -3
- package/docs/field-types.md +30 -0
- package/package.json +1 -1
- package/src/dom.js +21 -8
- package/src/field-types-builtins.js +215 -4
- package/src/filters.js +46 -18
- package/src/preferences.js +47 -2
- package/styles/table.css +29 -0
|
@@ -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 $
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
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
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
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
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
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
|
|
package/src/preferences.js
CHANGED
|
@@ -117,10 +117,32 @@
|
|
|
117
117
|
if (!CT.isUserFilterColumn(col)) {
|
|
118
118
|
return;
|
|
119
119
|
}
|
|
120
|
-
|
|
121
|
-
if (!$
|
|
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,34 @@ th.kgrid-row-actions {
|
|
|
494
495
|
user-select: none;
|
|
495
496
|
}
|
|
496
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
|
+
|
|
497
526
|
/* —— Visual chrome (Linear / Notion) —— */
|
|
498
527
|
.custom-table {
|
|
499
528
|
font-size: var(--kgrid-font-size);
|