powerpagestoolkit 2.5.2 → 2.5.301

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.
@@ -38,8 +38,28 @@ export declare const _init: unique symbol;
38
38
  */
39
39
  /******/ /******/ constructor(target: HTMLElement | string);
40
40
  [_init](): Promise<void>;
41
+ /**
42
+ * Initializes value synchronization with appropriate event listeners
43
+ * based on element type.
44
+ * @private
45
+ */
41
46
  private _initValueSync;
47
+ /**
48
+ * Updates the value and checked state based on element type
49
+ * @public
50
+ */
42
51
  updateValue(): void;
52
+ /**
53
+ * Gets the current value of the element based on its type
54
+ * @private
55
+ * @returns {ElementValue} Object containing value and optional checked state
56
+ */
57
+ private getElementValue;
58
+ /**
59
+ * Updates related radio buttons if this is part of a radio group
60
+ * @private
61
+ */
62
+ private updateRadioGroup;
43
63
  private _attachVisibilityController;
44
64
  private _attachRadioButtons;
45
65
  /**
@@ -81,6 +101,14 @@ export declare const _init: unique symbol;
81
101
  * @returns - Instance of this
82
102
  */
83
103
  disable(): DOMNodeReference;
104
+ /**
105
+ * Clears all values and states of the element.
106
+ * Handles different input types appropriately.
107
+ *
108
+ * @returns {DOMNodeReference} Instance of this for method chaining
109
+ * @throws {Error} If clearing values fails
110
+ */
111
+ clearValues(): Promise<DOMNodeReference>;
84
112
  /**
85
113
  * Enables the element so that users can input data
86
114
  * @returns - Instance of this
@@ -156,18 +184,18 @@ export declare const _init: unique symbol;
156
184
  */
157
185
  uncheckRadios(): DOMNodeReference;
158
186
  /**
159
- * Configures conditional rendering for the target element based on a condition
160
- * and the visibility of one or more trigger elements.
161
- *
162
- * @param {() => boolean} condition - A function that returns a boolean to determine
163
- * the visibility of the target element. If `condition()` returns true, the element is shown;
164
- * otherwise, it is hidden.
165
- * @param {Array<DOMNodeReference>} [dependencies] - An array of `DOMNodeReference` instances. Event listeners are
166
- * registered on each to toggle the visibility of the target element based on the `condition` and the visibility of
167
- * the target node.
168
- * @throws {ConditionalRenderingError} When there's an error in setting up conditional rendering
169
- * @returns {DOMNodeReference} - Instance of this
170
- */
187
+ * Configures conditional rendering for the target element based on a condition
188
+ * and the visibility of one or more trigger elements.
189
+ *
190
+ * @param {() => boolean} condition - A function that returns a boolean to determine
191
+ * the visibility of the target element. If `condition()` returns true, the element is shown;
192
+ * otherwise, it is hidden.
193
+ * @param {Array<DOMNodeReference>} [dependencies] - An array of `DOMNodeReference` instances. Event listeners are
194
+ * registered on each to toggle the visibility of the target element based on the `condition` and the visibility of
195
+ * the target node.
196
+ * @throws {ConditionalRenderingError} When there's an error in setting up conditional rendering
197
+ * @returns {DOMNodeReference} - Instance of this
198
+ */
171
199
  configureConditionalRendering(condition: () => boolean, dependencies?: Array<DOMNodeReference>): DOMNodeReference;
172
200
  /**
173
201
  * Sets up validation and requirement rules for the field with enhanced error handling and dynamic updates.
package/dist/bundle.js CHANGED
@@ -252,47 +252,80 @@ var DOMNodeReference = class _DOMNodeReference {
252
252
  throw new DOMNodeInitializationError(this, e);
253
253
  }
254
254
  }
255
- // Function to update this.value based on element type
255
+ /**
256
+ * Initializes value synchronization with appropriate event listeners
257
+ * based on element type.
258
+ * @private
259
+ */
256
260
  _initValueSync() {
257
261
  this.updateValue();
258
- const elementType = this.element.type;
259
- if (elementType === "checkbox" || elementType === "radio") {
260
- this.element.addEventListener("click", this.updateValue.bind(this));
261
- } else if (elementType === "select-one" || elementType === "select" || elementType === "select-multiple") {
262
- this.element.addEventListener("change", this.updateValue.bind(this));
263
- } else {
264
- this.element.addEventListener("input", this.updateValue.bind(this));
265
- }
262
+ const input = this.element;
263
+ const eventMapping = {
264
+ checkbox: "click",
265
+ radio: "click",
266
+ "select-one": "change",
267
+ select: "change",
268
+ "select-multiple": "change"
269
+ };
270
+ const boundUpdateValue = this.updateValue.bind(this);
271
+ const eventType = eventMapping[input.type] || "input";
272
+ this.element.addEventListener(eventType, boundUpdateValue);
266
273
  }
274
+ /**
275
+ * Updates the value and checked state based on element type
276
+ * @public
277
+ */
267
278
  updateValue() {
268
- switch (this.element.type) {
279
+ const elementValue = this.getElementValue();
280
+ this.value = elementValue.value;
281
+ if (elementValue.checked !== void 0) {
282
+ this.checked = elementValue.checked;
283
+ }
284
+ this.updateRadioGroup();
285
+ }
286
+ /**
287
+ * Gets the current value of the element based on its type
288
+ * @private
289
+ * @returns {ElementValue} Object containing value and optional checked state
290
+ */
291
+ getElementValue() {
292
+ const input = this.element;
293
+ const select = this.element;
294
+ switch (input.type) {
269
295
  case "checkbox":
270
296
  case "radio":
271
- this.value = this.element.checked;
272
- this.checked = this.element.checked;
273
- break;
297
+ return {
298
+ value: input.checked,
299
+ checked: input.checked
300
+ };
274
301
  case "select-multiple":
275
- this.value = Array.from(
276
- this.element.selectedOptions
277
- ).map((option) => option.value);
278
- break;
302
+ return {
303
+ value: Array.from(select.selectedOptions).map(
304
+ (option) => option.value
305
+ )
306
+ };
279
307
  case "select-one":
280
- this.value = this.element.value;
281
- break;
308
+ return {
309
+ value: select.value
310
+ };
282
311
  case "number":
283
- this.value = this.element.value !== "" ? Number(this.element.value) : null;
284
- break;
312
+ return {
313
+ value: input.value !== "" ? Number(input.value) : null
314
+ };
285
315
  default:
286
- if (this.element.classList.contains("decimal")) {
287
- this.value = parseFloat(this.element.value);
288
- } else {
289
- this.value = this.element.value;
290
- }
291
- break;
316
+ return {
317
+ value: this.element.classList.contains("decimal") ? parseFloat(input.value) : input.value
318
+ };
292
319
  }
293
- if (this.yesRadio instanceof _DOMNodeReference) {
320
+ }
321
+ /**
322
+ * Updates related radio buttons if this is part of a radio group
323
+ * @private
324
+ */
325
+ updateRadioGroup() {
326
+ if (this.yesRadio instanceof _DOMNodeReference && this.noRadio instanceof _DOMNodeReference) {
294
327
  this.yesRadio.updateValue();
295
- this.noRadio.updateValue();
328
+ this.noRadio?.updateValue();
296
329
  this.checked = this.yesRadio.checked;
297
330
  this.value = this.yesRadio.checked;
298
331
  }
@@ -399,6 +432,75 @@ var DOMNodeReference = class _DOMNodeReference {
399
432
  }
400
433
  return this;
401
434
  }
435
+ /**
436
+ * Clears all values and states of the element.
437
+ * Handles different input types appropriately.
438
+ *
439
+ * @returns {DOMNodeReference} Instance of this for method chaining
440
+ * @throws {Error} If clearing values fails
441
+ */
442
+ async clearValues() {
443
+ try {
444
+ const element = this.element;
445
+ if (element instanceof HTMLInputElement) {
446
+ switch (element.type.toLowerCase()) {
447
+ case "checkbox":
448
+ case "radio":
449
+ element.checked = false;
450
+ this.checked = false;
451
+ this.value = false;
452
+ break;
453
+ case "number":
454
+ element.value = "";
455
+ this.value = "";
456
+ break;
457
+ default:
458
+ element.value = "";
459
+ this.value = "";
460
+ break;
461
+ }
462
+ } else if (element instanceof HTMLSelectElement) {
463
+ if (element.multiple) {
464
+ Array.from(element.options).forEach(
465
+ (option) => option.selected = false
466
+ );
467
+ this.value = [];
468
+ } else {
469
+ element.selectedIndex = -1;
470
+ this.value = "";
471
+ }
472
+ } else if (element instanceof HTMLTextAreaElement) {
473
+ element.value = "";
474
+ this.value = "";
475
+ } else {
476
+ this.value = "";
477
+ const childInputs = Array.from(
478
+ this.element.querySelectorAll("input, select, textarea")
479
+ );
480
+ if (childInputs.length > 0) {
481
+ const clearPromises = childInputs.map(async (input) => {
482
+ const inputRef = await createDOMNodeReference(input);
483
+ return inputRef.clearValues();
484
+ });
485
+ await Promise.all(clearPromises);
486
+ }
487
+ }
488
+ if (this.yesRadio instanceof _DOMNodeReference && this.noRadio instanceof _DOMNodeReference) {
489
+ await this.yesRadio.clearValues();
490
+ await this.noRadio.clearValues();
491
+ }
492
+ const events = [
493
+ new Event("input", { bubbles: true }),
494
+ new Event("change", { bubbles: true }),
495
+ new Event("click", { bubbles: true })
496
+ ];
497
+ events.forEach((event) => this.element.dispatchEvent(event));
498
+ return this;
499
+ } catch (error) {
500
+ const errorMessage = `Failed to clear values for element with target "${this.target}": ${error instanceof Error ? error.message : String(error)}`;
501
+ throw new Error(errorMessage);
502
+ }
503
+ }
402
504
  /**
403
505
  * Enables the element so that users can input data
404
506
  * @returns - Instance of this
@@ -535,18 +637,18 @@ var DOMNodeReference = class _DOMNodeReference {
535
637
  return this;
536
638
  }
537
639
  /**
538
- * Configures conditional rendering for the target element based on a condition
539
- * and the visibility of one or more trigger elements.
540
- *
541
- * @param {() => boolean} condition - A function that returns a boolean to determine
542
- * the visibility of the target element. If `condition()` returns true, the element is shown;
543
- * otherwise, it is hidden.
544
- * @param {Array<DOMNodeReference>} [dependencies] - An array of `DOMNodeReference` instances. Event listeners are
545
- * registered on each to toggle the visibility of the target element based on the `condition` and the visibility of
546
- * the target node.
547
- * @throws {ConditionalRenderingError} When there's an error in setting up conditional rendering
548
- * @returns {DOMNodeReference} - Instance of this
549
- */
640
+ * Configures conditional rendering for the target element based on a condition
641
+ * and the visibility of one or more trigger elements.
642
+ *
643
+ * @param {() => boolean} condition - A function that returns a boolean to determine
644
+ * the visibility of the target element. If `condition()` returns true, the element is shown;
645
+ * otherwise, it is hidden.
646
+ * @param {Array<DOMNodeReference>} [dependencies] - An array of `DOMNodeReference` instances. Event listeners are
647
+ * registered on each to toggle the visibility of the target element based on the `condition` and the visibility of
648
+ * the target node.
649
+ * @throws {ConditionalRenderingError} When there's an error in setting up conditional rendering
650
+ * @returns {DOMNodeReference} - Instance of this
651
+ */
550
652
  configureConditionalRendering(condition, dependencies) {
551
653
  try {
552
654
  if (typeof condition !== "function") {
@@ -561,14 +663,18 @@ var DOMNodeReference = class _DOMNodeReference {
561
663
  );
562
664
  return this;
563
665
  }
564
- const observers = [];
565
666
  dependencies.forEach((node) => {
566
667
  if (!node || !(node instanceof _DOMNodeReference)) {
567
- throw new TypeError("Each dependency must be a valid DOMNodeReference instance");
668
+ throw new TypeError(
669
+ "Each dependency must be a valid DOMNodeReference instance"
670
+ );
568
671
  }
569
672
  const handleChange = () => {
570
673
  try {
571
674
  this.toggleVisibility(condition());
675
+ if (condition() === false) {
676
+ this.clearValues();
677
+ }
572
678
  } catch (error) {
573
679
  console.error("Error in change handler:", error);
574
680
  }
@@ -576,7 +682,9 @@ var DOMNodeReference = class _DOMNodeReference {
576
682
  node.on("change", handleChange);
577
683
  const observer = new MutationObserver(() => {
578
684
  try {
579
- const display = window.getComputedStyle(node.visibilityController).display;
685
+ const display = window.getComputedStyle(
686
+ node.visibilityController
687
+ ).display;
580
688
  this.toggleVisibility(display !== "none" && condition());
581
689
  } catch (error) {
582
690
  0;
@@ -588,7 +696,6 @@ var DOMNodeReference = class _DOMNodeReference {
588
696
  attributes: true,
589
697
  attributeFilter: ["style"]
590
698
  });
591
- observers.push(observer);
592
699
  });
593
700
  return this;
594
701
  } catch (error) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "powerpagestoolkit",
3
- "version": "2.5.2",
3
+ "version": "2.5.301",
4
4
  "description": "Reference, manipulate, and engage with Power Pages sites through the nodes in the DOM; use a variety of custom methods that allow customizing your power pages site quicker and easier. ",
5
5
  "main": "./dist/bundle.js",
6
6
  "types": "./dist/index.d.ts",