powerpagestoolkit 1.3.103 → 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)