powerpagestoolkit 1.3.104 → 1.3.201

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
@@ -30,6 +30,8 @@ The `DOMNodeReference` module simplifies DOM element management. It provides fun
30
30
 
31
31
  - **`createMultipleDOMNodeReferences(selector)`**: Creates multiple `DOMNodeReference` instances for all elements matching the specified CSS selector. Returns an array of `DOMNodeReference` instances.
32
32
 
33
+ `selector` uses standard ED6 `document.querySelector()` syntax. For more information, read [here](https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelector)
34
+
33
35
  ```javascript
34
36
  // single instance of DOMNodeReference
35
37
  const node = await createDOMNodeReference("#my-element");
@@ -48,22 +50,27 @@ nodeArray.forEach((node) => {
48
50
  })
49
51
  ```
50
52
 
51
- ##### Properties
53
+ ##### Available Properties
54
+
55
+ These properties are public and can be used in any custom logic/configurations
52
56
 
53
57
  ```typescript
54
- target: string;
55
- element: HTMLElement | null;
58
+ target: HTMLElement | string;
59
+ element: HTMLElement;
56
60
  isLoaded: boolean;
57
- visibilityController: HTMLElement | null;
58
- defaultDisplay: string;
59
- value: string | null;
61
+ value: any;
62
+ yesRadio: DOMNodeReference;
63
+ noRadio: DOMNodeReference;
60
64
  ```
61
65
 
62
66
  ##### Methods
63
67
 
64
68
  Here are the key methods you can use with a DOMNodeReference instance:
65
69
 
66
- ```typescript
70
+ ```javascript
71
+
72
+ /********/
73
+ // VISIBILITY / ACCESSIBILITY
67
74
 
68
75
  // Hides the associated DOM element.
69
76
  hide()
@@ -71,8 +78,96 @@ hide()
71
78
  // Shows the associated DOM element.
72
79
  show()
73
80
 
81
+ /**
82
+ * advanced visibility control in the case you need to apply
83
+ * custom logic to the visibility of an element
84
+ */
85
+ toggleVisibility(shouldShow: boolean | () => boolean)
86
+
87
+ /**
88
+ * Configures conditional rendering for the target element based on a condition
89
+ * and the visibility of one or more trigger elements.
90
+ *
91
+ * @param {(this: DOMNodeReference) => boolean} condition - A function that returns a boolean to determine
92
+ * the visibility of the target element. If `condition()` returns true, the element is shown;
93
+ * otherwise, it is hidden.
94
+ * @param {DOMNodeReference[]} triggerNodes - An array of `DOMNodeReference` instances. Event listeners are
95
+ * registered on each to toggle the visibility of the target element based on the `condition` and the visibility of
96
+ * the target node.
97
+ */
98
+ configureConditionalRendering(
99
+ condition: (this: DOMNodeReference) => boolean,
100
+ triggerNodes: DOMNodeReference[]
101
+ )
102
+
103
+
104
+ // EXAMPLE:
105
+ const your_node = await createDOMNodeReference("#element_id")
106
+ const other_node = await createDOMNodeReference(".element_class")
107
+
108
+ your_node.configureConditionalRendering(() =>
109
+ other_node.value == "3", // your_node will only be visible when the value of other_node is "3"
110
+ [other_node] // and we have to include any DOMNodeReferences used in the evaluation logic, so that changes to them can be watched and the condition evaluated again
111
+ );
112
+
113
+
114
+ /**
115
+ * 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.
116
+ *
117
+ * @param {function(this: DOMNodeReference): boolean} isRequired - A function that determines
118
+ * whether the field should be required. Returns `true` if required, `false` otherwise.
119
+ * @param {function(this: DOMNodeReference): boolean} isValid - A function that checks if the field's
120
+ * input is valid. Returns `true` if valid, `false` otherwise.
121
+ * @param {string} fieldDisplayName - The name of the field, used in error messages if validation fails.
122
+ * @param {Array<DOMNodeReference>} [dependencies] Other fields that this field’s requirement depends on.
123
+ * When these Nodes or their values change, the required status of this field is re-evaluated.
124
+ * Make sure any DOMNodeReference used in `isRequired` or `isValid` is included in this array.
125
+ */
126
+ configureValidationAndRequirements(
127
+ isRequired: (this: this) => boolean,
128
+ isValid: (this: this) => boolean,
129
+ fieldDisplayName: string,
130
+ dependencies: Array<DOMNodeReference>
131
+ )
132
+
133
+ // EXAMPLE:
134
+ const your_node = await createDOMNodeReference("#element_id")
135
+ const other_node = await createDOMNodeReference(".element_class")
136
+
137
+ your_node.configureValidationAndRequirements(
138
+ () => other_node.yesRadio.checked, /* if 'yes' is checked for this other node,
139
+ this function will evaluate to true, meaning that 'your_node' will be required */
140
+
141
+ function () { /* important to use standard 'function' declaration, instead of
142
+ arrow function when needing to access 'this' (the instance of 'your_node') */
143
+
144
+ if (other_node.yesRadio.checked) { // when other_node radio is checked 'yes'
145
+ return this.value; // this is only 'valid' if it has a value
146
+ } else return true;
147
+ },
148
+ "Your Field Name",
149
+ [other_node] /* since our conditions depend on 'other_node' it must be included in the
150
+ dependency array so that the requirement conditions can be re-evaluated when
151
+ the value of 'other_node' changes */
152
+ );
153
+
154
+
155
+ /* sets the elements 'disabled' to true - useful for inputs
156
+ that need to be enabled/disabled conditionally */
157
+ disable()
158
+
159
+ // Sets the element 'disabled' to false
160
+ enable()
161
+ ```
162
+
163
+ ```javascript
164
+ // OTHER METHODS
165
+
74
166
  // Sets the value of the associated HTML element.
75
- setValue(value: string)
167
+ setValue(value: any)
168
+
169
+ // Sets the inner HTML content of the associated HTML element.
170
+ setTextContent(text: string)
76
171
 
77
172
  // Appends child elements to the associated HTML element.
78
173
  append(...elements: HTMLElement[])
@@ -86,11 +181,9 @@ getLabel(): HTMLElement | null
86
181
  // Appends child elements to the label associated with the HTML element.
87
182
  appendToLabel(...elements: HTMLElement[])
88
183
 
89
- // Adds a click event listener to the associated HTML element.
90
- addClickListener(eventHandler: () => void)
91
-
92
- // Adds a change event listener to the associated HTML element.
93
- addChangeListener(eventHandler: () => void)
184
+ // Create an event listener on the target element. Provide access to 'this'
185
+ // in the event handler function
186
+ on(eventType: string, eventHandler: (this: DOMNodeReference) => void)
94
187
 
95
188
  // Unchecks both yes and no radio buttons if they exist.
96
189
  uncheckRadios()
@@ -101,18 +194,8 @@ createValidation(evaluationFunction: () => boolean, fieldDisplayName: string)
101
194
  // Adds a tooltip to the label associated with the HTML element.
102
195
  addLabelTooltip(text: string)
103
196
 
104
- // Adds a tooltip to the associated HTML element.
105
-
106
- addToolTip(text: string)
107
-
108
- // Sets the inner HTML content of the associated HTML element.
109
- setTextContent(text: string)
110
-
111
- // Toggles visibility based on the provided boolean value.
112
- toggleVisibility(shouldShow: boolean)
113
-
114
- // Sets the visibility of the element based on a condition and binds it to another DOMNodeReference.
115
- configureConditionalRendering(condition: () => boolean, triggerNode?: DOMNodeReference)
197
+ // Adds a tooltip with the specified text to the element
198
+ addTooltip(text: string)
116
199
 
117
200
  // Executes a callback function once the element is fully loaded.
118
201
  onceLoaded(callback: (instance: DOMNodeReference) => void)
@@ -1041,16 +1041,16 @@ var DOMNodeReference = /*#__PURE__*/function () {
1041
1041
  value: function configureConditionalRendering(condition, triggerNodes) {
1042
1042
  var _this = this;
1043
1043
  try {
1044
- this.toggleVisibility(condition());
1044
+ this.toggleVisibility(condition(this));
1045
1045
  if (triggerNodes) {
1046
1046
  var nodes = Array.isArray(triggerNodes) ? triggerNodes : [triggerNodes];
1047
1047
  nodes.forEach(function (node) {
1048
1048
  node.on("change", function () {
1049
- return _this.toggleVisibility(condition());
1049
+ return _this.toggleVisibility(condition(_this));
1050
1050
  });
1051
1051
  var observer = new MutationObserver(function () {
1052
1052
  var display = window.getComputedStyle(node.visibilityController).display;
1053
- _this.toggleVisibility(display !== "none" && condition());
1053
+ _this.toggleVisibility(display !== "none" && condition(_this));
1054
1054
  });
1055
1055
  observer.observe(node.visibilityController, {
1056
1056
  attributes: true,
@@ -1064,29 +1064,27 @@ var DOMNodeReference = /*#__PURE__*/function () {
1064
1064
  }
1065
1065
  }, {
1066
1066
  key: "configureValidationAndRequirements",
1067
- value: function configureValidationAndRequirements(_ref, fieldDisplayName) {
1067
+ value: function configureValidationAndRequirements(isRequired, isValid, fieldDisplayName) {
1068
1068
  var _this2 = this;
1069
- var requirementLogic = _ref.requirementLogic,
1070
- validationLogic = _ref.validationLogic;
1071
- var dependencies = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : [];
1069
+ var dependencies = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : [];
1072
1070
  if (typeof Page_Validators !== "undefined") {
1073
1071
  var newValidator = document.createElement("span");
1074
1072
  newValidator.style.display = "none";
1075
1073
  newValidator.id = "".concat(this.id, "Validator");
1076
1074
  newValidator.controltovalidate = this.id;
1077
1075
  newValidator.errormessage = "<a href='#".concat(this.element.id, "_label'>").concat(fieldDisplayName, " is a required field</a>");
1078
- newValidator.evaluationfunction = validationLogic.bind(this);
1076
+ newValidator.evaluationfunction = isValid.bind(this);
1079
1077
  //eslint-disable-next-line
1080
1078
  Page_Validators.push(newValidator);
1081
1079
  } else {
1082
1080
  throw new Error("Attempted to add to Validator where Page_Validators do not exist");
1083
1081
  }
1084
- this.setRequiredLevel(requirementLogic(this));
1082
+ this.setRequiredLevel(isRequired(this));
1085
1083
  if (!dependencies) return;
1086
1084
  dependencies = Array.isArray(dependencies) ? dependencies : [dependencies];
1087
1085
  dependencies.forEach(function (dep) {
1088
1086
  dep.element.addEventListener("change", function () {
1089
- return _this2.setRequiredLevel(requirementLogic(_this2));
1087
+ return _this2.setRequiredLevel(isRequired(_this2));
1090
1088
  });
1091
1089
  });
1092
1090
  }
package/index.d.ts CHANGED
@@ -127,21 +127,18 @@ class DOMNodeReference {
127
127
  uncheckRadios(): void;
128
128
 
129
129
  /**
130
- * Configures validation and requirement conditions for the field based on the provided logic functions and dependencies.
131
- * Creates a validator and sets a required level dynamically based on dependency changes.
132
- * @param {Object} config An object containing the requirement and validation logical functions
133
- * @param {(this: DOMNodeReference) => boolean} config.requirementLogic - Function to determine if the field is required.
134
- * @param {(this: DOMNodeReference) => boolean} config.validationLogic - Function to evaluate the field's validity.
135
- * @param {string} fieldDisplayName - Display name used in error messages if validation fails.
136
- * @param {Array<DOMNodeReference>} [dependencies] - Optional dependencies for setting requirement conditions dynamically.
130
+ * 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.
131
+ *
132
+ * @param {function(this: DOMNodeReference): boolean} isRequired - A function that determines whether the field should be required. Returns `true` if required, `false` otherwise.
133
+ * @param {function(this: DOMNodeReference): boolean} isValid - A function that checks if the field's input is valid. Returns `true` if valid, `false` otherwise.
134
+ * @param {string} fieldDisplayName - The name of the field, used in error messages if validation fails.
135
+ * @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.
137
136
  */
138
137
  configureValidationAndRequirements(
139
- config: {
140
- requirementLogic: (this: this) => boolean;
141
- validationLogic: (this: this) => boolean;
142
- },
138
+ isRequired: (this: this) => boolean,
139
+ isValid: (this: this) => boolean,
143
140
  fieldDisplayName: string,
144
- dependencies?: DOMNodeReference[]
141
+ dependencies: Array<DOMNodeReference>
145
142
  ): void;
146
143
 
147
144
  /**
@@ -158,6 +155,12 @@ class DOMNodeReference {
158
155
  */
159
156
  addLabelTooltip(text: string): void;
160
157
 
158
+ /**
159
+ * Adds a tooltip with the specified text to the element
160
+ * @param {string} text - The text to display in the tooltip
161
+ */
162
+ addTooltip(text: string): void;
163
+
161
164
  /**
162
165
  * Sets the inner HTML content of the HTML element.
163
166
  * @param {string} text - The text to set as the inner HTML of the element.
@@ -175,16 +178,16 @@ class DOMNodeReference {
175
178
  * Configures conditional rendering for the target element based on a condition
176
179
  * and the visibility of one or more trigger elements.
177
180
  *
178
- * @param {Function} condition - A function that returns a boolean to determine
181
+ * @param {(this: DOMNodeReference) => boolean} condition - A function that returns a boolean to determine
179
182
  * the visibility of the target element. If `condition()` returns true, the element is shown;
180
183
  * otherwise, it is hidden.
181
- * @param {DOMNodeReference | DOMNodeReference[]} triggerNodes - A single `DOMNodeReference`
182
- * or an array of `DOMNodeReference` instances. Event listeners are registered on each
183
- * `triggerNode` to toggle the visibility of the target element based on the `condition`.
184
+ * @param {DOMNodeReference[]} triggerNodes - An array of `DOMNodeReference` instances. Event listeners are
185
+ * registered on each to toggle the visibility of the target element based on the `condition` and the visibility of
186
+ * the target node.
184
187
  */
185
188
  configureConditionalRendering(
186
- condition: () => boolean,
187
- triggerNodes?: DOMNodeReference | DOMNodeReference[]
189
+ condition: (this: DOMNodeReference) => boolean,
190
+ triggerNodes: DOMNodeReference[]
188
191
  ): void;
189
192
 
190
193
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "powerpagestoolkit",
3
- "version": "1.3.104",
3
+ "version": "1.3.201",
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/index.bundle.js",
6
6
  "types": "index.d.ts",