powerpagestoolkit 1.3.0 → 1.3.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
@@ -20,16 +20,18 @@ import {
20
20
 
21
21
  # Modules
22
22
 
23
- ## `DOMNodereference`
23
+ ### `DOMNodereference`
24
24
 
25
25
  The `DOMNodeReference` module simplifies DOM element management. It provides functionalities for creating and interacting with DOM elements:
26
26
 
27
- ### Usage
27
+ #### Usage
28
28
 
29
29
  - **`createDOMNodeReference(selector)`**: Creates a `DOMNodeReference` instance for a single DOM element specified by a CSS selector or HTMLElement. Returns a `DOMNodeReference` instance.
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,85 @@ 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 whether the field should be required. Returns `true` if required, `false` otherwise.
118
+ * @param {function(this: DOMNodeReference): boolean} isValid - A function that checks if the field's input is valid. Returns `true` if valid, `false` otherwise.
119
+ * @param {string} fieldDisplayName - The name of the field, used in error messages if validation fails.
120
+ * @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.
121
+ */
122
+ configureValidationAndRequirements(
123
+ isRequired: (this: this) => boolean,
124
+ isValid: (this: this) => boolean,
125
+ fieldDisplayName: string,
126
+ dependencies: Array<DOMNodeReference>
127
+ )
128
+
129
+ // EXAMPLE:
130
+ const your_node = await createDOMNodeReference("#element_id")
131
+ const other_node = await createDOMNodeReference(".element_class")
132
+
133
+ your_node.configureValidationAndRequirements(
134
+ () => other_node.yesRadio.checked, // if 'yes' is checked for this other node, this function will evaluate to true, meaning that 'your_node' will be required
135
+ function () { // important to use standard 'function' declaration, instead of arrow function when needing to access 'this' (the instance of 'your_node')
136
+ if (other_node.yesRadio.checked) { // when other_node radio is checked 'yes'
137
+ return this.value; // this is only 'valid' if it has a value
138
+ } else return true;
139
+ },
140
+ "Your Field Name",
141
+ [other_node] // since our conditions depend on 'other_node' it must be included in the dependency array so that the requirement conditions can be re-evaluated when the value of 'other_node' changes
142
+ );
143
+
144
+
145
+ // sets the elements 'disabled' to true - useful for inputs that need to be enabled/disabled conditionally
146
+ disable()
147
+
148
+ // Sets the element 'disabled' to false
149
+ enable()
150
+ ```
151
+
152
+ ```javascript
153
+ // OTHER METHODS
154
+
74
155
  // Sets the value of the associated HTML element.
75
- setValue(value: string)
156
+ setValue(value: any)
157
+
158
+ // Sets the inner HTML content of the associated HTML element.
159
+ setTextContent(text: string)
76
160
 
77
161
  // Appends child elements to the associated HTML element.
78
162
  append(...elements: HTMLElement[])
@@ -86,11 +170,9 @@ getLabel(): HTMLElement | null
86
170
  // Appends child elements to the label associated with the HTML element.
87
171
  appendToLabel(...elements: HTMLElement[])
88
172
 
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)
173
+ // Create an event listener on the target element. Provide access to 'this'
174
+ // in the event handler function
175
+ on(eventType: string, eventHandler: (this: DOMNodeReference) => void)
94
176
 
95
177
  // Unchecks both yes and no radio buttons if they exist.
96
178
  uncheckRadios()
@@ -101,25 +183,15 @@ createValidation(evaluationFunction: () => boolean, fieldDisplayName: string)
101
183
  // Adds a tooltip to the label associated with the HTML element.
102
184
  addLabelTooltip(text: string)
103
185
 
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)
186
+ // Adds a tooltip with the specified text to the element
187
+ addTooltip(text: string)
116
188
 
117
189
  // Executes a callback function once the element is fully loaded.
118
190
  onceLoaded(callback: (instance: DOMNodeReference) => void)
119
191
 
120
192
  ```
121
193
 
122
- ## `API`
194
+ ### `API`
123
195
 
124
196
  The `API` module provides functions for creating and retrieving records from a DataVerse. It includes the following methods:
125
197
 
@@ -128,7 +200,7 @@ The `API` module provides functions for creating and retrieving records from a D
128
200
 
129
201
  - **`getMultiple(tableSetName, queryParameters)`**: Retrieves multiple records from the DataVerse based on specified query parameters. Returns a Promise that resolves with the list of retrieved records or rejects with an error.
130
202
 
131
- ### Usage
203
+ #### Usage
132
204
 
133
205
  ###### 1. Creating a Record
134
206