@wernfried/daterangepicker 5.7.1 → 5.8.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.
@@ -1,4 +1,4 @@
1
- import { DateTime, Duration, Info, Settings } from "luxon";
1
+ import { DateTime, Duration, Info, Settings, Interval } from "luxon";
2
2
  class DateRangePicker {
3
3
  #startDate = null;
4
4
  #endDate = null;
@@ -412,27 +412,33 @@ class DateRangePicker {
412
412
  }
413
413
  }
414
414
  }
415
- if (this.singleDatePicker) {
416
- if (typeof options.altInput === "string") {
415
+ if (["function", "string", "object"].includes(typeof options.altFormat))
416
+ this.altFormat = options.altFormat;
417
+ if (typeof options.altInput === "string") {
418
+ if (this.singleDatePicker) {
417
419
  const el = document.querySelector(options.altInput);
418
- this.altInput = el instanceof HTMLInputElement && ["text", "hidden"].includes(el.type) ? el : null;
419
- } else if (options.altInput instanceof HTMLElement) {
420
- this.altInput = options.altInput instanceof HTMLInputElement && ["text", "hidden"].includes(options.altInput.type) ? options.altInput : null;
421
- }
422
- } else if (!this.singleDatePicker && Array.isArray(options.altInput) && options.altInput.length === 2) {
423
- this.altInput = [];
424
- for (let item of options.altInput) {
425
- const el = typeof item === "string" ? document.querySelector(item) : item;
426
420
  if (el instanceof HTMLInputElement && ["text", "hidden"].includes(el.type))
427
- this.altInput.push(el);
428
- }
429
- if (this.altInput.length !== 2)
430
- this.altInput = null;
431
- } else if (options.altInput != null) {
432
- console.warn(`Option 'altInput' ${JSON.stringify(options.altInput)} is not valid`);
433
- }
434
- if (options.altInput && ["function", "string"].includes(typeof options.altFormat))
435
- this.altFormat = options.altFormat;
421
+ this.altInput = el;
422
+ } else {
423
+ const el = Array.from(document.querySelectorAll(options.altInput));
424
+ if ([1, 2].includes(el.length) && el.every((x) => x instanceof HTMLInputElement && ["text", "hidden"].includes(x.type)))
425
+ this.altInput = el.length === 1 ? el[0] : el;
426
+ }
427
+ } else if (options.altInput instanceof HTMLElement && ["text", "hidden"].includes(options.altInput.type)) {
428
+ this.altInput = options.altInput;
429
+ } else if (Array.isArray(options.altInput) || options.altInput instanceof HTMLCollection || options.altInput instanceof NodeList) {
430
+ let el = Array.isArray(options.altInput) ? options.altInput : Array.from(options.altInput);
431
+ if (!this.singleDatePicker && el.length === 2) {
432
+ if (el.every((x) => typeof x === "string"))
433
+ el = el.map((x) => {
434
+ return document.querySelector(x);
435
+ });
436
+ if (el.every((x) => x instanceof HTMLInputElement && ["text", "hidden"].includes(x.type)))
437
+ this.altInput = el;
438
+ }
439
+ }
440
+ if (options.altInput != null && this.altInput == null)
441
+ console.error(`Option 'altInput' ${JSON.stringify(options.altInput)} is not valid`);
436
442
  if (typeof options.opens === "string") {
437
443
  if (["left", "right", "center"].includes(options.opens))
438
444
  this.opens = options.opens;
@@ -875,24 +881,41 @@ class DateRangePicker {
875
881
  * @param {sting|external:DateTime|Date} month - The month to be shown, if null then current month is selected.
876
882
  */
877
883
  clearDate(month) {
878
- const initialMonth = this.parseDate(month ?? this.#startDate.startOf("month"));
884
+ if (this.#startDate == null && this.#endDate == null)
885
+ return;
886
+ const monthChange = month !== void 0 && !this.parseDate(month).hasSame(this.#startDate, "month");
887
+ if (monthChange)
888
+ this.#startDate = this.parseDate(month);
889
+ this.updateMonthsInView();
879
890
  this.oldStartDate = this.#startDate;
880
891
  this.oldEndDate = this.#endDate;
881
892
  this.#startDate = null;
882
893
  this.#endDate = null;
894
+ this.updateCalendars(monthChange);
895
+ this.setApplyBtnState();
896
+ if (this.timePicker) {
897
+ this.triggerEvent(this.#events.onBeforeRenderTimePicker);
898
+ this.renderTimePicker("start");
899
+ this.renderTimePicker("end");
900
+ this.container.querySelectorAll(".calendar-time.end-time select").forEach((el) => {
901
+ el.disabled = !this.#endDate;
902
+ });
903
+ this.container.querySelectorAll(".calendar-time.end-time select").forEach((el) => {
904
+ el.classList.toggle("disabled", !this.#endDate);
905
+ });
906
+ }
883
907
  if (this.isInputText)
884
908
  this.element.value = "";
885
909
  if (!this.singleDatePicker)
886
910
  this.chosenLabel = null;
887
911
  if (this.altInput) {
888
- if (this.singleDatePicker) {
912
+ if (this.singleDatePicker || !Array.isArray(this.altInput)) {
889
913
  this.altInput.value = "";
890
914
  } else {
891
915
  this.altInput[0].value = "";
892
916
  this.altInput[1].value = "";
893
917
  }
894
918
  }
895
- this.updateView(!initialMonth.hasSame(this.oldStartDate, "month"));
896
919
  }
897
920
  /**
898
921
  * Parse date value
@@ -2299,7 +2322,7 @@ class DateRangePicker {
2299
2322
  updateAltInput() {
2300
2323
  if (this.altInput == null)
2301
2324
  return;
2302
- if (this.altFormat == null) {
2325
+ if (this.altFormat == null || typeof this.altFormat === "object") {
2303
2326
  let precision = "day";
2304
2327
  if (this.timePicker) {
2305
2328
  if (this.timePickerOpts.showSeconds) {
@@ -2310,18 +2333,37 @@ class DateRangePicker {
2310
2333
  precision = "hour";
2311
2334
  }
2312
2335
  }
2313
- const startDate = this.#startDate.toISO({ format: "basic", precision, includeOffset: false });
2314
- (this.singleDatePicker ? this.altInput : this.altInput[0]).value = startDate;
2315
- if (!this.singleDatePicker && this.#endDate) {
2316
- const endDate = this.#endDate.toISO({ format: "basic", precision, includeOffset: false });
2317
- this.altInput[1].value = endDate;
2336
+ const isoOptions = this.altFormat ?? { format: "basic", precision, includeOffset: false };
2337
+ const startDate = this.#startDate.toISO(isoOptions);
2338
+ if (this.singleDatePicker) {
2339
+ this.altInput.value = startDate;
2340
+ } else if (this.#endDate) {
2341
+ const endDate = this.#endDate.toISO(isoOptions);
2342
+ if (Array.isArray(this.altInput)) {
2343
+ this.altInput[0].value = startDate;
2344
+ this.altInput[1].value = endDate;
2345
+ } else {
2346
+ const interval = Interval.fromDateTimes(this.#startDate, this.#endDate);
2347
+ this.altInput.value = interval.toISO(isoOptions);
2348
+ }
2318
2349
  }
2319
2350
  } else {
2320
- const startDate = typeof this.altFormat === "function" ? this.altFormat(this.#startDate) : this.formatDate(this.#startDate, this.altFormat);
2321
- (this.singleDatePicker ? this.altInput : this.altInput[0]).value = startDate;
2322
- if (!this.singleDatePicker && this.#endDate) {
2323
- const endDate = typeof this.altFormat === "function" ? this.altFormat(this.#endDate) : this.formatDate(this.#endDate, this.altFormat);
2324
- this.altInput[1].value = endDate;
2351
+ const startDate = typeof this.altFormat === "function" ? this.altFormat(this.#startDate) : this.toFormat(this.#startDate, this.altFormat);
2352
+ if (this.singleDatePicker) {
2353
+ this.altInput.value = startDate;
2354
+ } else if (this.#endDate) {
2355
+ const endDate = typeof this.altFormat === "function" ? this.altFormat(this.#endDate) : this.toFormat(this.#endDate, this.altFormat);
2356
+ if (Array.isArray(this.altInput)) {
2357
+ this.altInput[0].value = startDate;
2358
+ this.altInput[1].value = endDate;
2359
+ } else {
2360
+ const interval = Interval.fromDateTimes(this.#startDate, this.#endDate);
2361
+ if (typeof this.altFormat === "function") {
2362
+ this.altInput.value = this.altFormat(interval);
2363
+ } else {
2364
+ this.altInput.value = this.toFormat(interval, this.altFormat, { separator: "/" });
2365
+ }
2366
+ }
2325
2367
  }
2326
2368
  }
2327
2369
  }
@@ -2383,6 +2425,7 @@ class DateRangePicker {
2383
2425
  * @param {boolean|object} [options] - Optional event options:
2384
2426
  * - `boolean capture`: Whether to listen in the capture phase (default: `false`)
2385
2427
  * - `object signal`: An `AbortSignal` for later removal of the listener (default: `undefined`)
2428
+ * @returns {DateRangePicker} - The DateRangePicker instance, usefull for chaining
2386
2429
  *
2387
2430
  * @example
2388
2431
  * picker.addEventListener('hide', (e) => {
@@ -2392,6 +2435,7 @@ class DateRangePicker {
2392
2435
  */
2393
2436
  addEventListener(type, listener, options) {
2394
2437
  this.element.addEventListener(type, listener, options);
2438
+ return this;
2395
2439
  }
2396
2440
  /**
2397
2441
  * Counterpart to `addEventListener()`
@@ -2402,9 +2446,11 @@ class DateRangePicker {
2402
2446
  * @param {boolean|object} [options] - Optional event options:
2403
2447
  * - `boolean capture`: Whether to listen in the capture phase (default: `false`)
2404
2448
  * - `object signal`: An `AbortSignal` for later removal of the listener (default: `undefined`)
2405
- */
2449
+ * @returns {DateRangePicker} - The DateRangePicker instance, usefull for chaining
2450
+ */
2406
2451
  removeEventListener(type, listener, options) {
2407
2452
  this.element.removeEventListener(type, listener, options);
2453
+ return this;
2408
2454
  }
2409
2455
  }
2410
2456
  function createElementFromHTML(html) {