powerpagestoolkit 2.221.12 → 2.701.3
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 +421 -230
- package/dist/import_map.json +6 -0
- package/dist/src/constants/eventMapping.d.ts +7 -0
- package/dist/src/constants/symbols.d.ts +14 -0
- package/dist/{API.d.ts → src/core/API.d.ts} +16 -8
- package/dist/src/core/DOMNodeReference.d.ts +227 -0
- package/dist/src/core/DOMNodeReferenceArray.d.ts +12 -0
- package/dist/src/core/List.d.ts +13 -0
- package/dist/src/core/bindForm.d.ts +28 -0
- package/dist/src/core/createDOMNodeReferences.d.ts +88 -0
- package/dist/src/core/waitFor.d.ts +9 -0
- package/dist/{errors.d.ts → src/errors/errors.d.ts} +5 -1
- package/dist/src/globals.d.ts +152 -0
- package/dist/src/index.d.ts +5 -0
- package/dist/src/index.js +1250 -0
- package/dist/src/index.js.LEGAL.txt +0 -0
- package/dist/src/index.js.map +7 -0
- package/dist/src/managers/ReferenceManager.d.ts +6 -0
- package/dist/src/utils/createInfoElement.d.ts +8 -0
- package/dist/src/utils/enhanceArray.d.ts +13 -0
- package/dist/src/utils/safeAjax.d.ts +5 -0
- package/package.json +42 -29
- package/dist/DOMNodeReference.d.ts +0 -197
- package/dist/bundle.js +0 -704
- package/dist/createDOMNodeReferences.d.ts +0 -15
- package/dist/createInfoElement.d.ts +0 -1
- package/dist/index.d.ts +0 -4
- package/dist/safeAjax.d.ts +0 -1
- package/dist/waitFor.d.ts +0 -1
package/dist/bundle.js
DELETED
|
@@ -1,704 +0,0 @@
|
|
|
1
|
-
// src/safeAjax.ts
|
|
2
|
-
function safeAjax(ajaxOptions) {
|
|
3
|
-
const deferredAjax = $.Deferred();
|
|
4
|
-
shell.getTokenDeferred().done(function(token) {
|
|
5
|
-
if (!ajaxOptions.headers) {
|
|
6
|
-
$.extend(ajaxOptions, {
|
|
7
|
-
headers: {
|
|
8
|
-
__RequestVerificationToken: token
|
|
9
|
-
}
|
|
10
|
-
});
|
|
11
|
-
} else {
|
|
12
|
-
ajaxOptions.headers["__RequestVerificationToken"] = token;
|
|
13
|
-
}
|
|
14
|
-
$.ajax(ajaxOptions).done(function(data, textStatus, jqXHR) {
|
|
15
|
-
validateLoginSession(data, textStatus, jqXHR, deferredAjax.resolve);
|
|
16
|
-
}).fail(deferredAjax.reject);
|
|
17
|
-
}).fail(function() {
|
|
18
|
-
deferredAjax.rejectWith(this, arguments);
|
|
19
|
-
});
|
|
20
|
-
return deferredAjax.promise();
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
// src/API.ts
|
|
24
|
-
var API = {
|
|
25
|
-
/**
|
|
26
|
-
*
|
|
27
|
-
* @param {Schema} schema an instance of a schema class, containing the desired information for the POST request
|
|
28
|
-
* @returns a Promise resolving the successful results *[record id]* of the POST request, or rejecting the failed results *[error]* of the POST request.
|
|
29
|
-
*/
|
|
30
|
-
createRecord(schema) {
|
|
31
|
-
return new Promise((resolve, reject) => {
|
|
32
|
-
safeAjax({
|
|
33
|
-
type: "POST",
|
|
34
|
-
url: `/_api/${schema.logicalName()}`,
|
|
35
|
-
data: schema.value(),
|
|
36
|
-
contentType: "application/json",
|
|
37
|
-
success: function(response, status, xhr) {
|
|
38
|
-
resolve(xhr.getResponseHeader("entityid"));
|
|
39
|
-
},
|
|
40
|
-
error: (error) => {
|
|
41
|
-
reject(error);
|
|
42
|
-
}
|
|
43
|
-
});
|
|
44
|
-
});
|
|
45
|
-
},
|
|
46
|
-
/**
|
|
47
|
-
*
|
|
48
|
-
* @param tableSetName The DataVerse SET name of the table being queried
|
|
49
|
-
* @param recordID the GUID of the records to be retrieved
|
|
50
|
-
* @param selectColumns *OPTIONAL* if desired, enter your own custom OData query for advanced GET results. Format = select=column1,column2,column3...
|
|
51
|
-
* @returns a Promise resolving the successful results of the GET request, or rejecting the failed results of the GET request
|
|
52
|
-
*/
|
|
53
|
-
getRecord(tableSetName, recordID, selectColumns) {
|
|
54
|
-
return new Promise((resolve, reject) => {
|
|
55
|
-
const url = `/_api/${tableSetName}(${recordID})${selectColumns ? `?$${selectColumns}` : ""}`;
|
|
56
|
-
safeAjax({
|
|
57
|
-
type: "GET",
|
|
58
|
-
url,
|
|
59
|
-
success: resolve,
|
|
60
|
-
error: reject
|
|
61
|
-
});
|
|
62
|
-
});
|
|
63
|
-
},
|
|
64
|
-
/**
|
|
65
|
-
*
|
|
66
|
-
* @param tableSetName The dataverse set name of the table being queried
|
|
67
|
-
* @param queryParameters *OPTIONAL* the OData query parameters for refining search results: *format = $filter=filters&$select=columns*
|
|
68
|
-
* @returns a Promise resolving the successful results of the GET request, or rejecting the failed results of the GET request
|
|
69
|
-
*/
|
|
70
|
-
getMultiple(tableSetName, queryParameters) {
|
|
71
|
-
return new Promise((resolve, reject) => {
|
|
72
|
-
const url = `/_api/${tableSetName}${queryParameters ? `?${queryParameters}` : ""}`;
|
|
73
|
-
safeAjax({
|
|
74
|
-
type: "GET",
|
|
75
|
-
url,
|
|
76
|
-
success: function(response) {
|
|
77
|
-
resolve(response.value);
|
|
78
|
-
},
|
|
79
|
-
error: reject
|
|
80
|
-
});
|
|
81
|
-
});
|
|
82
|
-
},
|
|
83
|
-
/**
|
|
84
|
-
*
|
|
85
|
-
* @param tableSetName The dataverse set name for the table that you are updating a record in
|
|
86
|
-
* @param recordId The GUID of the record that is being updated
|
|
87
|
-
* @param data The JSON of the fields and data that are to be updated on the targeted record
|
|
88
|
-
* @returns A Promise with the results of the API execution
|
|
89
|
-
*/
|
|
90
|
-
updateRecord(tableSetName, recordId, data) {
|
|
91
|
-
return new Promise((resolve, reject) => {
|
|
92
|
-
const url = `/_api/${tableSetName}(${recordId})`;
|
|
93
|
-
safeAjax({
|
|
94
|
-
type: "PATCH",
|
|
95
|
-
url,
|
|
96
|
-
data: JSON.stringify(data),
|
|
97
|
-
success: resolve,
|
|
98
|
-
error: reject
|
|
99
|
-
});
|
|
100
|
-
});
|
|
101
|
-
}
|
|
102
|
-
};
|
|
103
|
-
var API_default = API;
|
|
104
|
-
|
|
105
|
-
// src/waitFor.ts
|
|
106
|
-
function waitFor(target) {
|
|
107
|
-
return new Promise((resolve, reject) => {
|
|
108
|
-
const observer = new MutationObserver(() => {
|
|
109
|
-
const observedElement = document.querySelector(target);
|
|
110
|
-
if (observedElement) {
|
|
111
|
-
clearTimeout(timeout);
|
|
112
|
-
observer.disconnect();
|
|
113
|
-
resolve(observedElement);
|
|
114
|
-
}
|
|
115
|
-
});
|
|
116
|
-
const timeout = setTimeout(() => {
|
|
117
|
-
observer.disconnect();
|
|
118
|
-
reject(new Error(`Element not found: ${target} within 5 seconds`));
|
|
119
|
-
}, 5e3);
|
|
120
|
-
if (target instanceof HTMLElement) {
|
|
121
|
-
clearTimeout(timeout);
|
|
122
|
-
return resolve(target);
|
|
123
|
-
}
|
|
124
|
-
const element = document.querySelector(target);
|
|
125
|
-
if (element) {
|
|
126
|
-
clearTimeout(timeout);
|
|
127
|
-
return resolve(element);
|
|
128
|
-
}
|
|
129
|
-
observer.observe(document.body, {
|
|
130
|
-
subtree: true,
|
|
131
|
-
attributes: true,
|
|
132
|
-
childList: true
|
|
133
|
-
// Detects added/removed child elements
|
|
134
|
-
});
|
|
135
|
-
});
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
// src/createInfoElement.ts
|
|
139
|
-
function CreateInfoEl(titleString) {
|
|
140
|
-
const span = document.createElement("span");
|
|
141
|
-
span.classList.add("info-icon");
|
|
142
|
-
const icon = document.createElement("i");
|
|
143
|
-
icon.classList.add("fa", "fa-solid", "fa-info-circle");
|
|
144
|
-
icon.setAttribute("aria-label", "Info");
|
|
145
|
-
icon.style.cursor = "pointer";
|
|
146
|
-
const flyoutContent = document.createElement("div");
|
|
147
|
-
flyoutContent.innerHTML = titleString;
|
|
148
|
-
flyoutContent.classList.add("flyout-content");
|
|
149
|
-
span.appendChild(icon);
|
|
150
|
-
span.appendChild(flyoutContent);
|
|
151
|
-
const positionFlyout = () => {
|
|
152
|
-
flyoutContent.style.display = "block";
|
|
153
|
-
const flyoutRect = flyoutContent.getBoundingClientRect();
|
|
154
|
-
const viewportWidth = window.innerWidth;
|
|
155
|
-
if (flyoutRect.right > viewportWidth) {
|
|
156
|
-
const overflowAmount = flyoutRect.right - viewportWidth;
|
|
157
|
-
flyoutContent.style.left = `calc(50% - ${overflowAmount}px)`;
|
|
158
|
-
}
|
|
159
|
-
if (flyoutRect.left < 0) {
|
|
160
|
-
const overflowAmount = Math.abs(flyoutRect.left);
|
|
161
|
-
flyoutContent.style.left = `calc(50% + ${overflowAmount}px)`;
|
|
162
|
-
}
|
|
163
|
-
};
|
|
164
|
-
icon.addEventListener("mouseenter", positionFlyout);
|
|
165
|
-
icon.addEventListener("mouseleave", () => {
|
|
166
|
-
flyoutContent.style.display = "none";
|
|
167
|
-
});
|
|
168
|
-
icon.addEventListener("touchstart", (event) => {
|
|
169
|
-
event.preventDefault();
|
|
170
|
-
flyoutContent.style.display = flyoutContent.style.display === "block" ? "none" : "block";
|
|
171
|
-
if (flyoutContent.style.display === "block") {
|
|
172
|
-
positionFlyout();
|
|
173
|
-
}
|
|
174
|
-
});
|
|
175
|
-
document.body.addEventListener("click", (event) => {
|
|
176
|
-
if (!span.contains(event.target)) {
|
|
177
|
-
flyoutContent.style.display = "none";
|
|
178
|
-
}
|
|
179
|
-
});
|
|
180
|
-
flyoutContent.style.display = "none";
|
|
181
|
-
return span;
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
// src/errors.ts
|
|
185
|
-
var DOMNodeInitializationError = class extends Error {
|
|
186
|
-
constructor(instance, error) {
|
|
187
|
-
super(
|
|
188
|
-
`There was an error initializing a DOMNodeReference for target: ${instance.target}, :: ${error}`
|
|
189
|
-
);
|
|
190
|
-
this.name = "DOMNodeInitializationError";
|
|
191
|
-
}
|
|
192
|
-
};
|
|
193
|
-
var DOMNodeNotFoundError = class extends Error {
|
|
194
|
-
constructor(instance) {
|
|
195
|
-
super(`The targeted DOM element was not found: ${instance.target}`);
|
|
196
|
-
}
|
|
197
|
-
};
|
|
198
|
-
var ConditionalRenderingError = class extends Error {
|
|
199
|
-
constructor(instance, error) {
|
|
200
|
-
super(
|
|
201
|
-
`There was an error condiguring conditional rendering for target: ${instance.target} :: ${error}`
|
|
202
|
-
);
|
|
203
|
-
}
|
|
204
|
-
};
|
|
205
|
-
|
|
206
|
-
// src/DOMNodeReference.ts
|
|
207
|
-
var _init = Symbol("_init");
|
|
208
|
-
var DOMNodeReference = class _DOMNodeReference {
|
|
209
|
-
// properties initialized in the constructor
|
|
210
|
-
target;
|
|
211
|
-
isLoaded;
|
|
212
|
-
defaultDisplay;
|
|
213
|
-
/**
|
|
214
|
-
* The value of the element that this node represents
|
|
215
|
-
* stays in syncs with the live DOM elements?.,m via event handler
|
|
216
|
-
* @type {any}
|
|
217
|
-
*/
|
|
218
|
-
value;
|
|
219
|
-
/**
|
|
220
|
-
* Creates an instance of DOMNodeReference.
|
|
221
|
-
* @param {string} target - The CSS selector to find the desired DOM element.
|
|
222
|
-
*/
|
|
223
|
-
/******/
|
|
224
|
-
/******/
|
|
225
|
-
constructor(target) {
|
|
226
|
-
this.target = target;
|
|
227
|
-
this.isLoaded = false;
|
|
228
|
-
this.defaultDisplay = "";
|
|
229
|
-
this.value = null;
|
|
230
|
-
}
|
|
231
|
-
async [_init]() {
|
|
232
|
-
try {
|
|
233
|
-
const element = await waitFor(this.target);
|
|
234
|
-
this.element = element;
|
|
235
|
-
if (!this.element) {
|
|
236
|
-
throw new DOMNodeNotFoundError(this);
|
|
237
|
-
}
|
|
238
|
-
if (this.element.classList.contains("boolean-radio")) {
|
|
239
|
-
await this._attachRadioButtons();
|
|
240
|
-
}
|
|
241
|
-
this._initValueSync();
|
|
242
|
-
this._attachVisibilityController();
|
|
243
|
-
this.defaultDisplay = this.visibilityController.style.display;
|
|
244
|
-
this.isLoaded = true;
|
|
245
|
-
} catch (e) {
|
|
246
|
-
throw new DOMNodeInitializationError(this, e);
|
|
247
|
-
}
|
|
248
|
-
}
|
|
249
|
-
// Function to update this.value based on element type
|
|
250
|
-
_initValueSync() {
|
|
251
|
-
this.updateValue();
|
|
252
|
-
const elementType = this.element.type;
|
|
253
|
-
if (elementType === "checkbox" || elementType === "radio") {
|
|
254
|
-
this.element.addEventListener("click", this.updateValue.bind(this));
|
|
255
|
-
} else if (elementType === "select-one" || elementType === "select" || elementType === "select-multiple") {
|
|
256
|
-
this.element.addEventListener("change", this.updateValue.bind(this));
|
|
257
|
-
} else {
|
|
258
|
-
this.element.addEventListener("input", this.updateValue.bind(this));
|
|
259
|
-
}
|
|
260
|
-
}
|
|
261
|
-
updateValue() {
|
|
262
|
-
switch (this.element.type) {
|
|
263
|
-
case "checkbox":
|
|
264
|
-
this.value = +this.element.checked;
|
|
265
|
-
this.checked = this.element.checked;
|
|
266
|
-
case "radio":
|
|
267
|
-
this.value = this.element.checked;
|
|
268
|
-
this.checked = this.element.checked;
|
|
269
|
-
break;
|
|
270
|
-
case "select-multiple":
|
|
271
|
-
this.value = Array.from(
|
|
272
|
-
this.element.selectedOptions
|
|
273
|
-
).map((option) => option.value);
|
|
274
|
-
break;
|
|
275
|
-
case "select-one":
|
|
276
|
-
this.value = this.element.value;
|
|
277
|
-
case "number":
|
|
278
|
-
this.value = this.element.value !== "" ? Number(this.element.value) : null;
|
|
279
|
-
break;
|
|
280
|
-
default:
|
|
281
|
-
this.value = this.element.value;
|
|
282
|
-
break;
|
|
283
|
-
}
|
|
284
|
-
if (this.yesRadio instanceof _DOMNodeReference) {
|
|
285
|
-
this.yesRadio.updateValue();
|
|
286
|
-
this.noRadio.updateValue();
|
|
287
|
-
this.checked = this.yesRadio.checked;
|
|
288
|
-
this.value = +this.yesRadio.checked;
|
|
289
|
-
}
|
|
290
|
-
this._observeValueChanges();
|
|
291
|
-
}
|
|
292
|
-
// Add a method to observe value changes using MutationObserver
|
|
293
|
-
_observeValueChanges() {
|
|
294
|
-
const observer = new MutationObserver(() => {
|
|
295
|
-
this.updateValue();
|
|
296
|
-
});
|
|
297
|
-
observer.observe(this.element, {
|
|
298
|
-
attributes: true,
|
|
299
|
-
attributeFilter: ["value"]
|
|
300
|
-
});
|
|
301
|
-
}
|
|
302
|
-
_attachVisibilityController() {
|
|
303
|
-
this.visibilityController = this.element;
|
|
304
|
-
if (this.element.tagName === "TABLE") {
|
|
305
|
-
const fieldset = this.element.closest("fieldset");
|
|
306
|
-
if (fieldset) {
|
|
307
|
-
this.visibilityController = fieldset;
|
|
308
|
-
}
|
|
309
|
-
return;
|
|
310
|
-
}
|
|
311
|
-
const tagsRequiringTdParent = [
|
|
312
|
-
"SPAN",
|
|
313
|
-
"INPUT",
|
|
314
|
-
"TEXTAREA",
|
|
315
|
-
"SELECT",
|
|
316
|
-
"TABLE"
|
|
317
|
-
];
|
|
318
|
-
if (tagsRequiringTdParent.includes(this.element.tagName)) {
|
|
319
|
-
const tdParent = this.element.closest("td");
|
|
320
|
-
if (tdParent) {
|
|
321
|
-
this.visibilityController = tdParent;
|
|
322
|
-
}
|
|
323
|
-
}
|
|
324
|
-
}
|
|
325
|
-
async _attachRadioButtons() {
|
|
326
|
-
this.yesRadio = await createDOMNodeReference(`#${this.element.id}_1`);
|
|
327
|
-
this.noRadio = await createDOMNodeReference(`#${this.element.id}_0`);
|
|
328
|
-
}
|
|
329
|
-
/**
|
|
330
|
-
* Sets up an event listener based on the specified event type, executing the specified
|
|
331
|
-
* event handler
|
|
332
|
-
* @param {string} eventType - The DOM event to watch for
|
|
333
|
-
* @param {(this: DOMNodeReference, e: Event) => void} eventHandler - The callback function that runs when the
|
|
334
|
-
* specified event occurs
|
|
335
|
-
* @returns - Instance of this
|
|
336
|
-
*/
|
|
337
|
-
on(eventType, eventHandler) {
|
|
338
|
-
this.element.addEventListener(eventType, eventHandler.bind(this));
|
|
339
|
-
return this;
|
|
340
|
-
}
|
|
341
|
-
/**
|
|
342
|
-
* Hides the element by setting its display style to "none".
|
|
343
|
-
* @returns - Instance of this
|
|
344
|
-
*/
|
|
345
|
-
hide() {
|
|
346
|
-
this.visibilityController.style.display = "none";
|
|
347
|
-
return this;
|
|
348
|
-
}
|
|
349
|
-
/**
|
|
350
|
-
* Shows the element by restoring its default display style.
|
|
351
|
-
* @returns - Instance of this
|
|
352
|
-
*/
|
|
353
|
-
show() {
|
|
354
|
-
this.visibilityController.style.display = this.defaultDisplay;
|
|
355
|
-
return this;
|
|
356
|
-
}
|
|
357
|
-
/**
|
|
358
|
-
*
|
|
359
|
-
* @param {function(this: DOMNodeReference): boolean | boolean} shouldShow - Either a function that returns true or false,
|
|
360
|
-
* or a natural boolean to determine the visibility of this
|
|
361
|
-
* @returns - Instance of this
|
|
362
|
-
*/
|
|
363
|
-
toggleVisibility(shouldShow) {
|
|
364
|
-
if (shouldShow instanceof Function) {
|
|
365
|
-
shouldShow(this) ? this.show() : this.hide();
|
|
366
|
-
} else {
|
|
367
|
-
shouldShow ? this.show() : this.hide();
|
|
368
|
-
}
|
|
369
|
-
return this;
|
|
370
|
-
}
|
|
371
|
-
/**
|
|
372
|
-
* Sets the value of the HTML element.
|
|
373
|
-
* @param {() => any} value - The value to set for the HTML element.
|
|
374
|
-
* for parents of boolean radios, pass true or false as value, or
|
|
375
|
-
* an expression returning a boolean
|
|
376
|
-
* @returns - Instance of this
|
|
377
|
-
*/
|
|
378
|
-
setValue(value) {
|
|
379
|
-
if (this.element.classList.contains("boolean-radio")) {
|
|
380
|
-
this.yesRadio.element.checked = value;
|
|
381
|
-
this.noRadio.element.checked = !value;
|
|
382
|
-
} else {
|
|
383
|
-
this.element.value = value;
|
|
384
|
-
}
|
|
385
|
-
return this;
|
|
386
|
-
}
|
|
387
|
-
/**
|
|
388
|
-
* Disables the element so that users cannot input any data
|
|
389
|
-
* @returns - Instance of this
|
|
390
|
-
*/
|
|
391
|
-
disable() {
|
|
392
|
-
try {
|
|
393
|
-
this.element.disabled = true;
|
|
394
|
-
} catch (e) {
|
|
395
|
-
throw new Error(
|
|
396
|
-
`There was an error trying to disable the target: ${this.target}`
|
|
397
|
-
);
|
|
398
|
-
}
|
|
399
|
-
return this;
|
|
400
|
-
}
|
|
401
|
-
/**
|
|
402
|
-
* Enables the element so that users can input data
|
|
403
|
-
* @returns - Instance of this
|
|
404
|
-
*/
|
|
405
|
-
enable() {
|
|
406
|
-
try {
|
|
407
|
-
this.element.disabled = false;
|
|
408
|
-
} catch (e) {
|
|
409
|
-
throw new Error(
|
|
410
|
-
`There was an error trying to disable the target: ${this.target}`
|
|
411
|
-
);
|
|
412
|
-
}
|
|
413
|
-
return this;
|
|
414
|
-
}
|
|
415
|
-
/**
|
|
416
|
-
*
|
|
417
|
-
* @param {...HTMLElement} elements - The elements to prepend to the element targeted by this.
|
|
418
|
-
* @returns - Instance of this
|
|
419
|
-
*/
|
|
420
|
-
prepend(...elements) {
|
|
421
|
-
this.element.prepend(...elements);
|
|
422
|
-
return this;
|
|
423
|
-
}
|
|
424
|
-
/**
|
|
425
|
-
* Appends child elements to the HTML element.
|
|
426
|
-
* @param {...HTMLElement} elements - The elements to append to the element targeted by this.
|
|
427
|
-
* @returns - Instance of this
|
|
428
|
-
*/
|
|
429
|
-
append(...elements) {
|
|
430
|
-
this.element.append(...elements);
|
|
431
|
-
return this;
|
|
432
|
-
}
|
|
433
|
-
/**
|
|
434
|
-
* Inserts elements before the HTML element.
|
|
435
|
-
* @param {...HTMLElement} elements - The elements to insert before the HTML element.
|
|
436
|
-
* @returns - Instance of this
|
|
437
|
-
*/
|
|
438
|
-
before(...elements) {
|
|
439
|
-
this.element.before(...elements);
|
|
440
|
-
return this;
|
|
441
|
-
}
|
|
442
|
-
/**
|
|
443
|
-
* Inserts elements after the HTML element.
|
|
444
|
-
* @param {...HTMLElement} elements - The elements to insert after the HTML element.
|
|
445
|
-
* @returns - Instance of this
|
|
446
|
-
*/
|
|
447
|
-
after(...elements) {
|
|
448
|
-
this.element.after(...elements);
|
|
449
|
-
return this;
|
|
450
|
-
}
|
|
451
|
-
/**
|
|
452
|
-
* Retrieves the label associated with the HTML element.
|
|
453
|
-
* @returns {HTMLElement} The label element associated with this element.
|
|
454
|
-
*/
|
|
455
|
-
getLabel() {
|
|
456
|
-
return document.querySelector(`#${this.element.id}_label`) || null;
|
|
457
|
-
}
|
|
458
|
-
/**
|
|
459
|
-
* Appends child elements to the label associated with the HTML element.
|
|
460
|
-
* @param {...HTMLElement} elements - The elements to append to the label.
|
|
461
|
-
* @returns - Instance of this
|
|
462
|
-
*/
|
|
463
|
-
appendToLabel(...elements) {
|
|
464
|
-
const label = this.getLabel();
|
|
465
|
-
if (label) {
|
|
466
|
-
label.append(" ", ...elements);
|
|
467
|
-
}
|
|
468
|
-
return this;
|
|
469
|
-
}
|
|
470
|
-
/**
|
|
471
|
-
* Adds a tooltip with specified text to the label associated with the HTML element.
|
|
472
|
-
* @param {string} text - The text to display in the tooltip.
|
|
473
|
-
* @returns - Instance of this
|
|
474
|
-
*/
|
|
475
|
-
addLabelTooltip(text) {
|
|
476
|
-
this.appendToLabel(CreateInfoEl(text));
|
|
477
|
-
return this;
|
|
478
|
-
}
|
|
479
|
-
/**
|
|
480
|
-
* Adds a tooltip with the specified text to the element
|
|
481
|
-
* @param {string} text - The text to display in the tooltip
|
|
482
|
-
* @returns - Instance of this
|
|
483
|
-
*/
|
|
484
|
-
addTooltip(text) {
|
|
485
|
-
this.append(CreateInfoEl(text));
|
|
486
|
-
return this;
|
|
487
|
-
}
|
|
488
|
-
/**
|
|
489
|
-
* Sets the inner HTML content of the HTML element.
|
|
490
|
-
* @param {string} string - The text to set as the inner HTML of the element.
|
|
491
|
-
* @returns - Instance of this
|
|
492
|
-
*/
|
|
493
|
-
setInnerHTML(string) {
|
|
494
|
-
this.element.innerHTML = string;
|
|
495
|
-
return this;
|
|
496
|
-
}
|
|
497
|
-
/**
|
|
498
|
-
* Removes this element from the DOM
|
|
499
|
-
* @returns - Instance of this
|
|
500
|
-
*/
|
|
501
|
-
remove() {
|
|
502
|
-
this.element.remove();
|
|
503
|
-
return this;
|
|
504
|
-
}
|
|
505
|
-
/**
|
|
506
|
-
*
|
|
507
|
-
* @param {Partial<CSSStyleDeclaration} options and object containing the styles you want to set : {key: value} e.g.: {'display': 'block'}
|
|
508
|
-
* @returns - Instance of this
|
|
509
|
-
*/
|
|
510
|
-
setStyle(options) {
|
|
511
|
-
if (Object.prototype.toString.call(options) !== "[object Object]") {
|
|
512
|
-
throw new Error(
|
|
513
|
-
`powerpagestoolkit: 'DOMNodeReference.setStyle' required options to be in the form of an object. Argument passed was of type: ${typeof options}`
|
|
514
|
-
);
|
|
515
|
-
}
|
|
516
|
-
for (const key in options) {
|
|
517
|
-
this.element.style[key] = options[key];
|
|
518
|
-
}
|
|
519
|
-
return this;
|
|
520
|
-
}
|
|
521
|
-
/**
|
|
522
|
-
* Unchecks both the yes and no radio buttons if they exist.
|
|
523
|
-
* @returns - Instance of this
|
|
524
|
-
*/
|
|
525
|
-
uncheckRadios() {
|
|
526
|
-
if (this.yesRadio && this.noRadio) {
|
|
527
|
-
this.yesRadio.element.checked = false;
|
|
528
|
-
this.noRadio.element.checked = false;
|
|
529
|
-
} else {
|
|
530
|
-
console.error(
|
|
531
|
-
"[SYNACT] Attempted to uncheck radios for an element that has no radios"
|
|
532
|
-
);
|
|
533
|
-
}
|
|
534
|
-
return this;
|
|
535
|
-
}
|
|
536
|
-
/**
|
|
537
|
-
* Configures conditional rendering for the target element based on a condition
|
|
538
|
-
* and the visibility of one or more trigger elements.
|
|
539
|
-
*
|
|
540
|
-
* @param {(this: DOMNodeReference) => boolean} condition - A function that returns a boolean to determine
|
|
541
|
-
* the visibility of the target element. If `condition()` returns true, the element is shown;
|
|
542
|
-
* otherwise, it is hidden.
|
|
543
|
-
* @param {Array<DOMNodeReference>} [dependencies] - An array of `DOMNodeReference` instances. Event listeners are
|
|
544
|
-
* registered on each to toggle the visibility of the target element based on the `condition` and the visibility of
|
|
545
|
-
* the target node.
|
|
546
|
-
* @returns - Instance of this
|
|
547
|
-
*/
|
|
548
|
-
configureConditionalRendering(condition, dependencies) {
|
|
549
|
-
try {
|
|
550
|
-
this.toggleVisibility(condition());
|
|
551
|
-
if (!dependencies) {
|
|
552
|
-
console.warn(
|
|
553
|
-
`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`
|
|
554
|
-
);
|
|
555
|
-
return this;
|
|
556
|
-
}
|
|
557
|
-
dependencies.forEach((node) => {
|
|
558
|
-
node.on("change", () => this.toggleVisibility(condition()));
|
|
559
|
-
const observer = new MutationObserver(() => {
|
|
560
|
-
const display = window.getComputedStyle(
|
|
561
|
-
node.visibilityController
|
|
562
|
-
).display;
|
|
563
|
-
this.toggleVisibility(display !== "none" && condition());
|
|
564
|
-
});
|
|
565
|
-
observer.observe(node.visibilityController, {
|
|
566
|
-
attributes: true,
|
|
567
|
-
attributeFilter: ["style"]
|
|
568
|
-
});
|
|
569
|
-
});
|
|
570
|
-
return this;
|
|
571
|
-
} catch (e) {
|
|
572
|
-
throw new ConditionalRenderingError(this, e);
|
|
573
|
-
}
|
|
574
|
-
}
|
|
575
|
-
/**
|
|
576
|
-
* 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.
|
|
577
|
-
*
|
|
578
|
-
* @param {function(this: DOMNodeReference): boolean} isRequired - A function that determines whether the field should be required. Returns `true` if required, `false` otherwise.
|
|
579
|
-
* @param {function(this: DOMNodeReference): boolean} isValid - A function that checks if the field's input is valid. Returns `true` if valid, `false` otherwise.
|
|
580
|
-
* @param {string} fieldDisplayName - The name of the field, used in error messages if validation fails.
|
|
581
|
-
* @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.
|
|
582
|
-
* @returns - Instance of this
|
|
583
|
-
*/
|
|
584
|
-
configureValidationAndRequirements(isRequired, isValid, fieldDisplayName, dependencies) {
|
|
585
|
-
if (typeof Page_Validators !== "undefined") {
|
|
586
|
-
const newValidator = document.createElement("span");
|
|
587
|
-
newValidator.style.display = "none";
|
|
588
|
-
newValidator.id = `${this.element.id}Validator`;
|
|
589
|
-
newValidator.controltovalidate = this.element.id;
|
|
590
|
-
newValidator.errormessage = `<a href='#${this.element.id}_label'>${fieldDisplayName} is a required field</a>`;
|
|
591
|
-
newValidator.evaluationfunction = isValid.bind(this);
|
|
592
|
-
Page_Validators.push(newValidator);
|
|
593
|
-
} else {
|
|
594
|
-
throw new Error(
|
|
595
|
-
"Attempted to add to Validator where Page_Validators do not exist"
|
|
596
|
-
);
|
|
597
|
-
}
|
|
598
|
-
this.setRequiredLevel(isRequired(this));
|
|
599
|
-
if (!dependencies) {
|
|
600
|
-
console.warn(
|
|
601
|
-
`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`
|
|
602
|
-
);
|
|
603
|
-
return this;
|
|
604
|
-
}
|
|
605
|
-
dependencies.forEach((dep) => {
|
|
606
|
-
dep.element.addEventListener(
|
|
607
|
-
"change",
|
|
608
|
-
() => this.setRequiredLevel(isRequired(this))
|
|
609
|
-
);
|
|
610
|
-
});
|
|
611
|
-
return this;
|
|
612
|
-
}
|
|
613
|
-
/**
|
|
614
|
-
* Sets the required level for the field by adding or removing the "required-field" class on the label.
|
|
615
|
-
*
|
|
616
|
-
* @param {boolean} isRequired - Determines whether the field should be marked as required.
|
|
617
|
-
* If true, the "required-field" class is added to the label; if false, it is removed.
|
|
618
|
-
* @returns - Instance of this
|
|
619
|
-
*/
|
|
620
|
-
setRequiredLevel(isRequired) {
|
|
621
|
-
if (isRequired instanceof Function) {
|
|
622
|
-
isRequired() ? this.getLabel()?.classList.add("required-field") : this.getLabel()?.classList.remove("required-field");
|
|
623
|
-
return this;
|
|
624
|
-
} else {
|
|
625
|
-
isRequired ? this.getLabel()?.classList.add("required-field") : this.getLabel()?.classList.remove("required-field");
|
|
626
|
-
return this;
|
|
627
|
-
}
|
|
628
|
-
}
|
|
629
|
-
/**
|
|
630
|
-
* Executes a callback function once the element is fully loaded.
|
|
631
|
-
* If the element is already loaded, the callback is called immediately.
|
|
632
|
-
* Otherwise, a MutationObserver is used to detect when the element is added to the DOM.
|
|
633
|
-
* @param {Function} callback - A callback function to execute once the element is loaded.
|
|
634
|
-
*/
|
|
635
|
-
onceLoaded(callback) {
|
|
636
|
-
if (this.isLoaded) {
|
|
637
|
-
callback(this);
|
|
638
|
-
return;
|
|
639
|
-
}
|
|
640
|
-
if (this.target instanceof HTMLElement) {
|
|
641
|
-
callback(this);
|
|
642
|
-
return;
|
|
643
|
-
}
|
|
644
|
-
const observer = new MutationObserver(() => {
|
|
645
|
-
if (document.querySelector(this.target)) {
|
|
646
|
-
observer.disconnect();
|
|
647
|
-
this.isLoaded = true;
|
|
648
|
-
callback(this);
|
|
649
|
-
}
|
|
650
|
-
});
|
|
651
|
-
observer.observe(document.body, {
|
|
652
|
-
subtree: true,
|
|
653
|
-
childList: true
|
|
654
|
-
});
|
|
655
|
-
}
|
|
656
|
-
};
|
|
657
|
-
|
|
658
|
-
// src/createDOMNodeReferences.ts
|
|
659
|
-
async function createDOMNodeReference(target) {
|
|
660
|
-
try {
|
|
661
|
-
const instance = new DOMNodeReference(target);
|
|
662
|
-
await instance[_init]();
|
|
663
|
-
return new Proxy(instance, {
|
|
664
|
-
get: (target2, prop) => {
|
|
665
|
-
if (prop.toString().startsWith("_")) return void 0;
|
|
666
|
-
const value = target2[prop];
|
|
667
|
-
if (typeof value === "function" && prop !== "onceLoaded") {
|
|
668
|
-
return (...args) => {
|
|
669
|
-
target2.onceLoaded(() => value.apply(target2, args));
|
|
670
|
-
return target2;
|
|
671
|
-
};
|
|
672
|
-
}
|
|
673
|
-
return value;
|
|
674
|
-
}
|
|
675
|
-
});
|
|
676
|
-
} catch (e) {
|
|
677
|
-
throw new Error(e);
|
|
678
|
-
}
|
|
679
|
-
}
|
|
680
|
-
async function createMultipleDOMNodeReferences(querySelector) {
|
|
681
|
-
try {
|
|
682
|
-
const elements = Array.from(
|
|
683
|
-
document.querySelectorAll(querySelector)
|
|
684
|
-
);
|
|
685
|
-
const initializedElements = await Promise.all(
|
|
686
|
-
elements.map((element) => createDOMNodeReference(element))
|
|
687
|
-
);
|
|
688
|
-
const domNodeArray = initializedElements;
|
|
689
|
-
domNodeArray.hideAll = () => domNodeArray.forEach((instance) => instance.hide());
|
|
690
|
-
domNodeArray.showAll = () => domNodeArray.forEach((instance) => instance.show());
|
|
691
|
-
return domNodeArray;
|
|
692
|
-
} catch (e) {
|
|
693
|
-
console.error(
|
|
694
|
-
`There was an error creating multiple DOMNodeReferences: ${e}`
|
|
695
|
-
);
|
|
696
|
-
throw new Error(e);
|
|
697
|
-
}
|
|
698
|
-
}
|
|
699
|
-
export {
|
|
700
|
-
API_default as API,
|
|
701
|
-
createDOMNodeReference,
|
|
702
|
-
createMultipleDOMNodeReferences
|
|
703
|
-
};
|
|
704
|
-
//# sourceMappingURL=bundle.js.map
|