@wernfried/daterangepicker 5.2.18 → 5.3.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.
@@ -8,6 +8,8 @@ class DateRangePicker {
8
8
  this.parentEl = "body";
9
9
  this.element = element instanceof HTMLElement ? element : document.querySelector(element);
10
10
  this.isInputText = this.element instanceof HTMLInputElement && this.element.type === "text";
11
+ this.button = null;
12
+ this.showOnClick = true;
11
13
  this.#startDate = DateTime.now().startOf("day");
12
14
  this.#endDate = DateTime.now().plus({ day: 1 }).startOf("day");
13
15
  this.minDate = null;
@@ -84,6 +86,16 @@ class DateRangePicker {
84
86
  dataOptions[name] = ts.isValid && isDate ? ts : JSON.parse(item.value);
85
87
  }
86
88
  options = { ...dataOptions, ...options };
89
+ if (["string", "object"].includes(typeof options.button)) {
90
+ let button = options.button;
91
+ if (typeof button === "string" && document.querySelectorAll(button).length === 1)
92
+ button = document.querySelector(button);
93
+ if (button instanceof HTMLButtonElement) {
94
+ this.button = button;
95
+ } else {
96
+ console.error(`Option 'button' cannot resolved to a HTMLButtonElement`);
97
+ }
98
+ }
87
99
  if (typeof options.singleDatePicker === "boolean")
88
100
  this.singleDatePicker = options.singleDatePicker;
89
101
  if (!this.singleDatePicker && typeof options.singleMonthView === "boolean") {
@@ -163,13 +175,13 @@ class DateRangePicker {
163
175
  if (["rtl", "ltr"].includes(options.locale.direction))
164
176
  this.locale.direction = options.locale.direction;
165
177
  else
166
- console.error(`Option 'options.locale.direction' must be 'rtl' or 'ltr'`);
178
+ console.error(`Option 'locale.direction' must be 'rtl' or 'ltr'`);
167
179
  }
168
180
  if (["string", "object"].includes(typeof options.locale.format))
169
181
  this.locale.format = options.locale.format;
170
182
  if (Array.isArray(options.locale.daysOfWeek)) {
171
183
  if (options.locale.daysOfWeek.some((x) => typeof x !== "string"))
172
- console.error(`Option 'options.locale.daysOfWeek' must be an array of strings`);
184
+ console.error(`Option 'locale.daysOfWeek' must be an array of strings`);
173
185
  else
174
186
  this.locale.daysOfWeek = options.locale.daysOfWeek.slice();
175
187
  }
@@ -201,7 +213,8 @@ class DateRangePicker {
201
213
  "alwaysShowCalendars",
202
214
  "autoApply",
203
215
  "autoUpdateInput",
204
- "showLabel"
216
+ "showLabel",
217
+ "showOnClick"
205
218
  ]) {
206
219
  if (typeof options[key2] === "boolean")
207
220
  this[key2] = options[key2];
@@ -418,13 +431,13 @@ class DateRangePicker {
418
431
  if (["left", "right", "center"].includes(options.opens))
419
432
  this.opens = options.opens;
420
433
  else
421
- console.error(`Option 'options.opens' must be 'left', 'right' or 'center'`);
434
+ console.error(`Option 'opens' must be 'left', 'right' or 'center'`);
422
435
  }
423
436
  if (typeof options.drops === "string") {
424
437
  if (["up", "down", "auto"].includes(options.drops))
425
438
  this.drops = options.drops;
426
439
  else
427
- console.error(`Option 'options.drops' must be 'up', 'down' or 'auto'`);
440
+ console.error(`Option 'drops' must be 'up', 'down' or 'auto'`);
428
441
  }
429
442
  if (Array.isArray(options.buttonClasses)) {
430
443
  this.buttonClasses = options.buttonClasses.join(" ");
@@ -435,7 +448,7 @@ class DateRangePicker {
435
448
  if (["cancel", "apply"].includes(options.onOutsideClick))
436
449
  this.onOutsideClick = options.onOutsideClick;
437
450
  else
438
- console.error(`Option 'options.onOutsideClick' must be 'cancel' or 'apply'`);
451
+ console.error(`Option 'onOutsideClick' must be 'cancel' or 'apply'`);
439
452
  }
440
453
  if (this.locale.firstDay != 1) {
441
454
  let iterator = this.locale.firstDay;
@@ -526,14 +539,18 @@ class DateRangePicker {
526
539
  this.addListener(".drp-buttons", "click", "button.applyBtn", this.clickApply.bind(this));
527
540
  this.addListener(".drp-buttons", "click", "button.cancelBtn", this.clickCancel.bind(this));
528
541
  if (this.element.matches("input") || this.element.matches("button")) {
529
- this.element.addEventListener("click", this.#showProxy);
530
- this.element.addEventListener("focus", this.#showProxy);
542
+ if (this.showOnClick) {
543
+ this.element.addEventListener("click", this.#showProxy);
544
+ this.element.addEventListener("focus", this.#showProxy);
545
+ }
531
546
  this.element.addEventListener("keyup", this.#elementChangedProxy);
532
547
  this.element.addEventListener("keydown", this.#keydownProxy);
533
- } else {
548
+ } else if (this.showOnClick) {
534
549
  this.element.addEventListener("click", this.#toggleProxy);
535
550
  this.element.addEventListener("keydown", this.#toggleProxy);
536
551
  }
552
+ if (this.button)
553
+ this.button.addEventListener("click", this.#showProxy);
537
554
  this.updateElement();
538
555
  }
539
556
  /**
@@ -1731,7 +1748,9 @@ class DateRangePicker {
1731
1748
  outsideClick(e) {
1732
1749
  const target = e.target;
1733
1750
  function closest2(el, selector) {
1734
- let parent = el.parentElement;
1751
+ if (selector == null)
1752
+ return null;
1753
+ let parent = el;
1735
1754
  while (parent) {
1736
1755
  if (parent == selector)
1737
1756
  return parent;
@@ -1741,7 +1760,7 @@ class DateRangePicker {
1741
1760
  }
1742
1761
  if (
1743
1762
  // ie modal dialog fix
1744
- e.type === "focusin" || closest2(target, this.element) || closest2(target, this.container) || target.closest(".calendar-table")
1763
+ e.type === "focusin" || closest2(target, this.element) || closest2(target, this.container) || closest2(target, this.button) || target.closest(".calendar-table")
1745
1764
  ) return;
1746
1765
  const event = this.triggerEvent(this.#events.onOutsideClick);
1747
1766
  if (event.defaultPrevented)