powerpagestoolkit 1.2.202 → 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.
Files changed (4) hide show
  1. package/README.md +275 -1
  2. package/dist/index.bundle.js +444 -334
  3. package/index.d.ts +272 -181
  4. package/package.json +61 -40
package/README.md CHANGED
@@ -1 +1,275 @@
1
- # PowerPagesToolKit
1
+ # PowerPages Tool Kit
2
+
3
+ This package provides utilities for managing and interacting with the DOM and making AJAX requests to a DataVerse API. It includes the `API` module for handling CRUD operations and the `DOMNodeReference` class for seamless DOM manipulations.
4
+
5
+ ## Installation
6
+
7
+ After installing
8
+
9
+ `npm install powerpagestoolkit`
10
+
11
+ You can then import into your JavaScript files as follows:
12
+
13
+ ```javascript
14
+ import {
15
+ API,
16
+ createDOMNodeReference,
17
+ createMultipleDOMNodeReferences,
18
+ } from "powerpagestoolkit";
19
+ ```
20
+
21
+ # Modules
22
+
23
+ ### `DOMNodereference`
24
+
25
+ The `DOMNodeReference` module simplifies DOM element management. It provides functionalities for creating and interacting with DOM elements:
26
+
27
+ #### Usage
28
+
29
+ - **`createDOMNodeReference(selector)`**: Creates a `DOMNodeReference` instance for a single DOM element specified by a CSS selector or HTMLElement. Returns a `DOMNodeReference` instance.
30
+
31
+ - **`createMultipleDOMNodeReferences(selector)`**: Creates multiple `DOMNodeReference` instances for all elements matching the specified CSS selector. Returns an array of `DOMNodeReference` instances.
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
+
35
+ ```javascript
36
+ // single instance of DOMNodeReference
37
+ const node = await createDOMNodeReference("#my-element");
38
+
39
+ node.onceLoaded(() => {
40
+ console.log("Element is loaded: ", node.element);
41
+ });
42
+
43
+ // to imitate 'querySelectorAll', and return an array of DOMNodeReferences
44
+ const nodeArray = await createMultipleDOMNodeReferences('div[class="row"]');
45
+
46
+ nodeArray.forEach((node) => {
47
+ node.oneLoaded(() => {
48
+ console.log("Element loaded: ", node.element")
49
+ })
50
+ })
51
+ ```
52
+
53
+ ##### Available Properties
54
+
55
+ These properties are public and can be used in any custom logic/configurations
56
+
57
+ ```typescript
58
+ target: HTMLElement | string;
59
+ element: HTMLElement;
60
+ isLoaded: boolean;
61
+ value: any;
62
+ yesRadio: DOMNodeReference;
63
+ noRadio: DOMNodeReference;
64
+ ```
65
+
66
+ ##### Methods
67
+
68
+ Here are the key methods you can use with a DOMNodeReference instance:
69
+
70
+ ```javascript
71
+
72
+ /********/
73
+ // VISIBILITY / ACCESSIBILITY
74
+
75
+ // Hides the associated DOM element.
76
+ hide()
77
+
78
+ // Shows the associated DOM element.
79
+ show()
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
+
155
+ // Sets the value of the associated HTML element.
156
+ setValue(value: any)
157
+
158
+ // Sets the inner HTML content of the associated HTML element.
159
+ setTextContent(text: string)
160
+
161
+ // Appends child elements to the associated HTML element.
162
+ append(...elements: HTMLElement[])
163
+
164
+ // Inserts elements after the associated HTML element.
165
+ after(...elements: HTMLElement[])
166
+
167
+ // Retrieves the label associated with the HTML element.
168
+ getLabel(): HTMLElement | null
169
+
170
+ // Appends child elements to the label associated with the HTML element.
171
+ appendToLabel(...elements: HTMLElement[])
172
+
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)
176
+
177
+ // Unchecks both yes and no radio buttons if they exist.
178
+ uncheckRadios()
179
+
180
+ //Creates a validation instance for the field.
181
+ createValidation(evaluationFunction: () => boolean, fieldDisplayName: string)
182
+
183
+ // Adds a tooltip to the label associated with the HTML element.
184
+ addLabelTooltip(text: string)
185
+
186
+ // Adds a tooltip with the specified text to the element
187
+ addTooltip(text: string)
188
+
189
+ // Executes a callback function once the element is fully loaded.
190
+ onceLoaded(callback: (instance: DOMNodeReference) => void)
191
+
192
+ ```
193
+
194
+ ### `API`
195
+
196
+ The `API` module provides functions for creating and retrieving records from a DataVerse. It includes the following methods:
197
+
198
+ - **`createRecord(schema)`**: Creates a new record in the DataVerse using the provided schema instance. Returns a Promise that resolves with the record ID or rejects with an error.
199
+ - **`getRecord(tableSetName, recordID, selectColumns)`**: Retrieves a specific record from the DataVerse. Returns a Promise that resolves with the retrieved record or rejects with an error.
200
+
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.
202
+
203
+ #### Usage
204
+
205
+ ###### 1. Creating a Record
206
+
207
+ To create a new record in the DataVerse, you can use the `createRecord` method. This method takes an instance of a schema class containing the data for the record.
208
+
209
+ ```javascript
210
+ // Assuming you have a schema class defined
211
+ const schema = new YourSchemaClass({
212
+ name: "Sample Record",
213
+ description: "This is a sample record for demonstration.",
214
+ });
215
+
216
+ API.createRecord(schema)
217
+ .then((recordId) => {
218
+ console.log("Record created successfully with ID:", recordId);
219
+ })
220
+ .catch((error) => {
221
+ console.error("Error creating record:", error);
222
+ });
223
+ ```
224
+
225
+ ###### 2. Getting a Single Record
226
+
227
+ To retrieve a specific record from the DataVerse, use the `getRecord` method. You need to provide the table set name and the record ID.
228
+
229
+ ```javascript
230
+ const tableSetName = "accounts"; // The DataVerse table set name
231
+ const recordID = "your-record-id"; // The GUID of the record to retrieve
232
+
233
+ API.getRecord(tableSetName, recordID)
234
+ .then((record) => {
235
+ console.log("Retrieved record:", record);
236
+ })
237
+ .catch((error) => {
238
+ console.error("Error retrieving record:", error);
239
+ });
240
+ ```
241
+
242
+ ###### 3. Getting Multiple Records
243
+
244
+ If you need to retrieve multiple records with specific query parameters, you can use the `getMultiple` method. This method accepts the table set name and optional query parameters for filtering.
245
+
246
+ ```javascript
247
+ const tableSetName = "contacts"; // The DataVerse table set name
248
+ const queryParameters =
249
+ "$filter=firstName eq 'John'&$select=firstName,lastName"; // OData query parameters
250
+
251
+ API.getMultiple(tableSetName, queryParameters)
252
+ .then((records) => {
253
+ console.log("Retrieved records:", records);
254
+ })
255
+ .catch((error) => {
256
+ console.error("Error retrieving records:", error);
257
+ });
258
+ ```
259
+
260
+ ##### Example Schema Class
261
+
262
+ Here's a simple example of a schema class that you might use with the createRecord method:
263
+
264
+ ```javascript
265
+ class YourSchemaClass {
266
+ constructor(tableSetName, data) {
267
+ this.setName = tableSetName;
268
+ this.data = data;
269
+ }
270
+
271
+ value() {
272
+ return JSON.stringify(this.data); // Convert data to JSON format for the API
273
+ }
274
+ }
275
+ ```