@wernfried/daterangepicker 5.7.1 → 5.8.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.
@@ -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;
@@ -885,7 +891,7 @@ class DateRangePicker {
885
891
  if (!this.singleDatePicker)
886
892
  this.chosenLabel = null;
887
893
  if (this.altInput) {
888
- if (this.singleDatePicker) {
894
+ if (this.singleDatePicker || !Array.isArray(this.altInput)) {
889
895
  this.altInput.value = "";
890
896
  } else {
891
897
  this.altInput[0].value = "";
@@ -2299,7 +2305,7 @@ class DateRangePicker {
2299
2305
  updateAltInput() {
2300
2306
  if (this.altInput == null)
2301
2307
  return;
2302
- if (this.altFormat == null) {
2308
+ if (this.altFormat == null || typeof this.altFormat === "object") {
2303
2309
  let precision = "day";
2304
2310
  if (this.timePicker) {
2305
2311
  if (this.timePickerOpts.showSeconds) {
@@ -2310,18 +2316,37 @@ class DateRangePicker {
2310
2316
  precision = "hour";
2311
2317
  }
2312
2318
  }
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;
2319
+ const isoOptions = this.altFormat ?? { format: "basic", precision, includeOffset: false };
2320
+ const startDate = this.#startDate.toISO(isoOptions);
2321
+ if (this.singleDatePicker) {
2322
+ this.altInput.value = startDate;
2323
+ } else if (this.#endDate) {
2324
+ const endDate = this.#endDate.toISO(isoOptions);
2325
+ if (Array.isArray(this.altInput)) {
2326
+ this.altInput[0].value = startDate;
2327
+ this.altInput[1].value = endDate;
2328
+ } else {
2329
+ const interval = Interval.fromDateTimes(this.#startDate, this.#endDate);
2330
+ this.altInput.value = interval.toISO(isoOptions);
2331
+ }
2318
2332
  }
2319
2333
  } 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;
2334
+ const startDate = typeof this.altFormat === "function" ? this.altFormat(this.#startDate) : this.toFormat(this.#startDate, this.altFormat);
2335
+ if (this.singleDatePicker) {
2336
+ this.altInput.value = startDate;
2337
+ } else if (this.#endDate) {
2338
+ const endDate = typeof this.altFormat === "function" ? this.altFormat(this.#endDate) : this.toFormat(this.#endDate, this.altFormat);
2339
+ if (Array.isArray(this.altInput)) {
2340
+ this.altInput[0].value = startDate;
2341
+ this.altInput[1].value = endDate;
2342
+ } else {
2343
+ const interval = Interval.fromDateTimes(this.#startDate, this.#endDate);
2344
+ if (typeof this.altFormat === "function") {
2345
+ this.altInput.value = this.altFormat(interval);
2346
+ } else {
2347
+ this.altInput.value = this.toFormat(interval, this.altFormat, { separator: "/" });
2348
+ }
2349
+ }
2325
2350
  }
2326
2351
  }
2327
2352
  }
@@ -2383,6 +2408,7 @@ class DateRangePicker {
2383
2408
  * @param {boolean|object} [options] - Optional event options:
2384
2409
  * - `boolean capture`: Whether to listen in the capture phase (default: `false`)
2385
2410
  * - `object signal`: An `AbortSignal` for later removal of the listener (default: `undefined`)
2411
+ * @returns {DateRangePicker} - The DateRangePicker instance, usefull for chaining
2386
2412
  *
2387
2413
  * @example
2388
2414
  * picker.addEventListener('hide', (e) => {
@@ -2392,6 +2418,7 @@ class DateRangePicker {
2392
2418
  */
2393
2419
  addEventListener(type, listener, options) {
2394
2420
  this.element.addEventListener(type, listener, options);
2421
+ return this;
2395
2422
  }
2396
2423
  /**
2397
2424
  * Counterpart to `addEventListener()`
@@ -2402,9 +2429,11 @@ class DateRangePicker {
2402
2429
  * @param {boolean|object} [options] - Optional event options:
2403
2430
  * - `boolean capture`: Whether to listen in the capture phase (default: `false`)
2404
2431
  * - `object signal`: An `AbortSignal` for later removal of the listener (default: `undefined`)
2405
- */
2432
+ * @returns {DateRangePicker} - The DateRangePicker instance, usefull for chaining
2433
+ */
2406
2434
  removeEventListener(type, listener, options) {
2407
2435
  this.element.removeEventListener(type, listener, options);
2436
+ return this;
2408
2437
  }
2409
2438
  }
2410
2439
  function createElementFromHTML(html) {