powerpagestoolkit 2.5.0 → 2.5.2

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/README.md CHANGED
@@ -154,14 +154,14 @@ Type-safe wrapper for DataVerse API operations.
154
154
  #### Create Record
155
155
 
156
156
  ```typescript
157
- const recordId = await API.createRecord("accounts", {
158
- name: "New Account",
159
- type: "Customer",
157
+ await API.createRecord("accounts", {
158
+ name: "Gypsum LLC",
159
+ type: "Vendor",
160
160
  })
161
- .then(() => {
161
+ .then((recordId) => {
162
162
  console.log("Created record:", recordId);
163
163
  })
164
- .catch(() => {
164
+ .catch((error) => {
165
165
  console.error("Creation failed:", error);
166
166
  });
167
167
  ```
@@ -222,11 +222,11 @@ node.onceLoaded((instance) => {
222
222
  5. Use proper error handling with API operations:
223
223
 
224
224
  ```typescript
225
- try {
226
- await API.createRecord(/*...*/);
227
- } catch (error) {
228
- // Handle error appropriately
229
- }
225
+ await API.createRecord(/*...*/)
226
+ .then((recordId) => {})
227
+ .catch((error) => {
228
+ // handle your errors appropriately
229
+ });
230
230
  ```
231
231
 
232
232
  ## TypeScript Support
@@ -239,4 +239,4 @@ Contributions are welcome, feel free to create a pull request with enhancements.
239
239
 
240
240
  ## License
241
241
 
242
- This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
242
+ This project is licensed under the AGPL-3.0 License - see the [LICENSE](LICENSE) file for details.
@@ -46,7 +46,7 @@ export declare const _init: unique symbol;
46
46
  * Sets up an event listener based on the specified event type, executing the specified
47
47
  * event handler
48
48
  * @param {string} eventType - The DOM event to watch for
49
- * @param {(this: DOMNodeReference, e: Event) => void} eventHandler - The callback function that runs when the
49
+ * @param {(e: Event) => void} eventHandler - The callback function that runs when the
50
50
  * specified event occurs
51
51
  * @returns - Instance of this
52
52
  */
@@ -63,11 +63,11 @@ export declare const _init: unique symbol;
63
63
  show(): DOMNodeReference;
64
64
  /**
65
65
  *
66
- * @param {function(this: DOMNodeReference): boolean | boolean} shouldShow - Either a function that returns true or false,
66
+ * @param {function(instance: DOMNodeReference): boolean | boolean} shouldShow - Either a function that returns true or false,
67
67
  * or a natural boolean to determine the visibility of this
68
68
  * @returns - Instance of this
69
69
  */
70
- toggleVisibility(shouldShow: Function | boolean): DOMNodeReference;
70
+ toggleVisibility(shouldShow: ((instance: DOMNodeReference) => boolean) | boolean): DOMNodeReference;
71
71
  /**
72
72
  * Sets the value of the HTML element.
73
73
  * @param {(() => any) | any} value - The value to set for the HTML element.
@@ -156,28 +156,35 @@ export declare const _init: unique symbol;
156
156
  */
157
157
  uncheckRadios(): DOMNodeReference;
158
158
  /**
159
- * Configures conditional rendering for the target element based on a condition
160
- * and the visibility of one or more trigger elements.
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
+ */
171
+ configureConditionalRendering(condition: () => boolean, dependencies?: Array<DOMNodeReference>): DOMNodeReference;
172
+ /**
173
+ * Sets up validation and requirement rules for the field with enhanced error handling and dynamic updates.
161
174
  *
162
- * @param {(this: DOMNodeReference) => 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
- * @returns - Instance of this
175
+ * @param {() => boolean} isRequired - Function determining if field is required
176
+ * @param {() => boolean} isValid - Function validating field input
177
+ * @param {string} fieldDisplayName - Display name for error messages
178
+ * @param {Array<DOMNodeReference>} dependencies - Fields that trigger requirement/validation updates
179
+ * @returns {DOMNodeReference} Instance of this for method chaining
180
+ * @throws {ValidationConfigError} If validation setup fails
169
181
  */
170
- configureConditionalRendering(condition: () => boolean, dependencies: Array<DOMNodeReference>): DOMNodeReference;
182
+ configureValidationAndRequirements(isRequired: () => boolean, isValid: () => boolean, fieldDisplayName: string, dependencies: Array<DOMNodeReference>): DOMNodeReference;
171
183
  /**
172
- * Sets up validation and requirement rules for the field. This function dynamically updates the field's required status and validates its input based on the specified conditions.
173
- *
174
- * @param {function(this: DOMNodeReference): boolean} isRequired - A function that determines whether the field should be required. Returns `true` if required, `false` otherwise.
175
- * @param {function(this: DOMNodeReference): boolean} isValid - A function that checks if the field's input is valid. Returns `true` if valid, `false` otherwise.
176
- * @param {string} fieldDisplayName - The name of the field, used in error messages if validation fails.
177
- * @param {Array<DOMNodeReference>} [dependencies] Other fields that this field’s requirement depends on. When these fields change, the required status of this field is re-evaluated. Make sure any DOMNodeReference used in `isRequired` or `isValid` is included in this array.
178
- * @returns - Instance of this
184
+ * Sets up tracking for dependent fields using both event listeners and mutation observers.
185
+ * @private
179
186
  */
180
- configureValidationAndRequirements(isRequired: (instance: DOMNodeReference) => boolean, isValid: (instance: DOMNodeReference) => boolean, fieldDisplayName: string, dependencies: Array<DOMNodeReference>): DOMNodeReference;
187
+ private _setupDependencyTracking;
181
188
  /**
182
189
  * Sets the required level for the field by adding or removing the "required-field" class on the label.
183
190
  *
package/dist/bundle.js CHANGED
@@ -202,6 +202,12 @@ var ConditionalRenderingError = class extends Error {
202
202
  );
203
203
  }
204
204
  };
205
+ var ValidationConfigError = class extends Error {
206
+ constructor(node, message) {
207
+ super(`Validation configuration error for ${node.target}: ${message}`);
208
+ this.name = "ValidationConfigError";
209
+ }
210
+ };
205
211
 
206
212
  // src/DOMNodeReference.ts
207
213
  var _init = Symbol("_init");
@@ -322,7 +328,7 @@ var DOMNodeReference = class _DOMNodeReference {
322
328
  * Sets up an event listener based on the specified event type, executing the specified
323
329
  * event handler
324
330
  * @param {string} eventType - The DOM event to watch for
325
- * @param {(this: DOMNodeReference, e: Event) => void} eventHandler - The callback function that runs when the
331
+ * @param {(e: Event) => void} eventHandler - The callback function that runs when the
326
332
  * specified event occurs
327
333
  * @returns - Instance of this
328
334
  */
@@ -348,7 +354,7 @@ var DOMNodeReference = class _DOMNodeReference {
348
354
  }
349
355
  /**
350
356
  *
351
- * @param {function(this: DOMNodeReference): boolean | boolean} shouldShow - Either a function that returns true or false,
357
+ * @param {function(instance: DOMNodeReference): boolean | boolean} shouldShow - Either a function that returns true or false,
352
358
  * or a natural boolean to determine the visibility of this
353
359
  * @returns - Instance of this
354
360
  */
@@ -529,81 +535,151 @@ var DOMNodeReference = class _DOMNodeReference {
529
535
  return this;
530
536
  }
531
537
  /**
532
- * Configures conditional rendering for the target element based on a condition
533
- * and the visibility of one or more trigger elements.
534
- *
535
- * @param {(this: DOMNodeReference) => boolean} condition - A function that returns a boolean to determine
536
- * the visibility of the target element. If `condition()` returns true, the element is shown;
537
- * otherwise, it is hidden.
538
- * @param {Array<DOMNodeReference>} [dependencies] - An array of `DOMNodeReference` instances. Event listeners are
539
- * registered on each to toggle the visibility of the target element based on the `condition` and the visibility of
540
- * the target node.
541
- * @returns - Instance of this
542
- */
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
+ */
543
550
  configureConditionalRendering(condition, dependencies) {
544
551
  try {
545
- this.toggleVisibility(condition());
546
- if (!dependencies) {
552
+ if (typeof condition !== "function") {
553
+ throw new TypeError("Condition must be a function");
554
+ }
555
+ condition = condition.bind(this);
556
+ const initialState = condition();
557
+ this.toggleVisibility(initialState);
558
+ if (!dependencies?.length) {
547
559
  console.warn(
548
- `powerpagestoolkit: No dependencies were found when configuring conditional rendering for ${this}. Be sure that if you are referencing other nodes in your rendering logic, that you include those nodes in the dependency array`
560
+ `powerpagestoolkit: No dependencies provided for conditional rendering of ${this}. Include referenced nodes in the dependency array if using them in rendering logic.`
549
561
  );
550
562
  return this;
551
563
  }
564
+ const observers = [];
552
565
  dependencies.forEach((node) => {
553
- node.on("change", () => this.toggleVisibility(condition()));
566
+ if (!node || !(node instanceof _DOMNodeReference)) {
567
+ throw new TypeError("Each dependency must be a valid DOMNodeReference instance");
568
+ }
569
+ const handleChange = () => {
570
+ try {
571
+ this.toggleVisibility(condition());
572
+ } catch (error) {
573
+ console.error("Error in change handler:", error);
574
+ }
575
+ };
576
+ node.on("change", handleChange);
554
577
  const observer = new MutationObserver(() => {
555
- const display = window.getComputedStyle(
556
- node.visibilityController
557
- ).display;
558
- this.toggleVisibility(display !== "none" && condition());
578
+ try {
579
+ const display = window.getComputedStyle(node.visibilityController).display;
580
+ this.toggleVisibility(display !== "none" && condition());
581
+ } catch (error) {
582
+ 0;
583
+ console.error("Error in mutation observer:", error);
584
+ observer.disconnect();
585
+ }
559
586
  });
560
587
  observer.observe(node.visibilityController, {
561
588
  attributes: true,
562
589
  attributeFilter: ["style"]
563
590
  });
591
+ observers.push(observer);
564
592
  });
565
593
  return this;
566
- } catch (e) {
567
- throw new ConditionalRenderingError(this, e);
594
+ } catch (error) {
595
+ const errorMessage = error instanceof Error ? error.message : String(error);
596
+ throw new ConditionalRenderingError(this, errorMessage);
568
597
  }
569
598
  }
570
599
  /**
571
- * Sets up validation and requirement rules for the field. This function dynamically updates the field's required status and validates its input based on the specified conditions.
600
+ * Sets up validation and requirement rules for the field with enhanced error handling and dynamic updates.
572
601
  *
573
- * @param {function(this: DOMNodeReference): boolean} isRequired - A function that determines whether the field should be required. Returns `true` if required, `false` otherwise.
574
- * @param {function(this: DOMNodeReference): boolean} isValid - A function that checks if the field's input is valid. Returns `true` if valid, `false` otherwise.
575
- * @param {string} fieldDisplayName - The name of the field, used in error messages if validation fails.
576
- * @param {Array<DOMNodeReference>} [dependencies] Other fields that this field’s requirement depends on. When these fields change, the required status of this field is re-evaluated. Make sure any DOMNodeReference used in `isRequired` or `isValid` is included in this array.
577
- * @returns - Instance of this
602
+ * @param {() => boolean} isRequired - Function determining if field is required
603
+ * @param {() => boolean} isValid - Function validating field input
604
+ * @param {string} fieldDisplayName - Display name for error messages
605
+ * @param {Array<DOMNodeReference>} dependencies - Fields that trigger requirement/validation updates
606
+ * @returns {DOMNodeReference} Instance of this for method chaining
607
+ * @throws {ValidationConfigError} If validation setup fails
578
608
  */
579
609
  configureValidationAndRequirements(isRequired, isValid, fieldDisplayName, dependencies) {
580
- if (typeof Page_Validators !== "undefined") {
610
+ if (!fieldDisplayName?.trim()) {
611
+ throw new ValidationConfigError(this, "Field display name is required");
612
+ }
613
+ if (!Array.isArray(dependencies)) {
614
+ throw new ValidationConfigError(this, "Dependencies must be an array");
615
+ }
616
+ try {
617
+ isRequired = isRequired.bind(this);
618
+ isValid = isValid.bind(this);
619
+ if (typeof Page_Validators === "undefined") {
620
+ throw new ValidationConfigError(this, "Page_Validators not found");
621
+ }
622
+ const validatorId = `${this.element.id}Validator`;
581
623
  const newValidator = document.createElement("span");
582
624
  newValidator.style.display = "none";
583
- newValidator.id = `${this.element.id}Validator`;
584
- newValidator.controltovalidate = this.element.id;
585
- newValidator.errormessage = `<a href='#${this.element.id}_label'>${fieldDisplayName} is a required field</a>`;
586
- newValidator.evaluationfunction = isValid.bind(this);
625
+ newValidator.id = validatorId;
626
+ const validatorConfig = {
627
+ controltovalidate: this.element.id,
628
+ errormessage: `<a href='#${this.element.id}_label'>${fieldDisplayName} is a required field</a>`,
629
+ evaluationfunction: () => {
630
+ const isFieldRequired = isRequired();
631
+ const isFieldVisible = window.getComputedStyle(this.visibilityController).display !== "none";
632
+ if (!isFieldRequired || !isFieldVisible) {
633
+ return true;
634
+ }
635
+ return isValid();
636
+ }
637
+ };
638
+ Object.assign(newValidator, validatorConfig);
587
639
  Page_Validators.push(newValidator);
588
- } else {
589
- throw new Error(
590
- "Attempted to add to Validator where Page_Validators do not exist"
640
+ this.setRequiredLevel(isRequired());
641
+ this._setupDependencyTracking(isRequired, dependencies);
642
+ } catch (error) {
643
+ throw new ValidationConfigError(
644
+ this,
645
+ `Failed to configure validation: ${error}`
591
646
  );
592
647
  }
593
- this.setRequiredLevel(isRequired(this));
594
- if (!dependencies) {
648
+ return this;
649
+ }
650
+ /**
651
+ * Sets up tracking for dependent fields using both event listeners and mutation observers.
652
+ * @private
653
+ */
654
+ _setupDependencyTracking(isRequired, dependencies) {
655
+ if (dependencies.length === 0) {
595
656
  console.warn(
596
- `powerpagestoolkit: No dependencies were found when configuring requirement and validation for ${this}. Be sure that if you are referencing other nodes in your requirement or validation logic, that you include those nodes in the dependency array`
657
+ `powerpagestoolkit: No dependencies specified for ${this.element.id}. Include all referenced nodes in the dependency array for proper validation.`
597
658
  );
598
- return this;
659
+ return;
599
660
  }
600
661
  dependencies.forEach((dep) => {
601
- dep.element.addEventListener(
602
- "change",
603
- () => this.setRequiredLevel(isRequired(this))
604
- );
662
+ dep.on("change", () => this.setRequiredLevel(isRequired(this)));
663
+ dep.on("input", () => this.setRequiredLevel(isRequired(this)));
664
+ const observer = new MutationObserver(() => {
665
+ const display = window.getComputedStyle(
666
+ dep.visibilityController
667
+ ).display;
668
+ if (display !== "none") {
669
+ this.setRequiredLevel(isRequired(this));
670
+ }
671
+ });
672
+ observer.observe(dep.visibilityController, {
673
+ attributes: true,
674
+ attributeFilter: ["style"],
675
+ subtree: false
676
+ });
677
+ if (dep.yesRadio || dep.noRadio) {
678
+ [dep.yesRadio, dep.noRadio].forEach((radio) => {
679
+ radio?.on("change", () => this.setRequiredLevel(isRequired(this)));
680
+ });
681
+ }
605
682
  });
606
- return this;
607
683
  }
608
684
  /**
609
685
  * Sets the required level for the field by adding or removing the "required-field" class on the label.
package/dist/errors.d.ts CHANGED
@@ -8,3 +8,6 @@ export declare class DOMNodeNotFoundError extends Error {
8
8
  export declare class ConditionalRenderingError extends Error {
9
9
  constructor(instance: DOMNodeReference, error: string);
10
10
  }
11
+ export declare class ValidationConfigError extends Error {
12
+ constructor(node: DOMNodeReference, message: string);
13
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "powerpagestoolkit",
3
- "version": "2.5.0",
3
+ "version": "2.5.2",
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",
@@ -26,7 +26,7 @@
26
26
  "typescript-eslint": "^8.12.2"
27
27
  },
28
28
  "author": "KeatonBrewster",
29
- "license": "SSPL-1.0",
29
+ "license": "AGPL-3.0-only",
30
30
  "type": "module",
31
31
  "repository": {
32
32
  "type": "git",