powerpagestoolkit 2.7.102 → 2.7.104

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
@@ -150,7 +150,7 @@ _Method Signature:_
150
150
 
151
151
  ```typescript
152
152
  applyBusinessRule(
153
- rule: IBusinessRule,
153
+ rule: BusinessRule,
154
154
  dependencies: DOMNodeReference[]
155
155
  ): DOMNodeReference; /* Instance of this is returned for optional
156
156
  method chaining */
@@ -159,7 +159,7 @@ applyBusinessRule(
159
159
  **BusinessRule Definition**
160
160
 
161
161
  ```typescript
162
- interface IBusinessRule {
162
+ interface BusinessRule {
163
163
  setVisibility?: [
164
164
  condition: () => boolean,
165
165
  clearValuesOnHide?: boolean = true
package/dist/bundle.js CHANGED
@@ -285,13 +285,12 @@ var ValidationConfigError = class extends Error {
285
285
  };
286
286
 
287
287
  // src/core/DOMNodeReference.ts
288
- var eventMapping = {
289
- checkbox: "click",
290
- radio: "click",
291
- select: "change",
292
- "select-multiple": "change",
293
- textarea: "keyup"
294
- // Add other input types as needed
288
+ var EventTypes = {
289
+ CHECKBOX: "click",
290
+ RADIO: "click",
291
+ SELECT: "change",
292
+ TEXTAREA: "keyup",
293
+ DEFAULT: "input"
295
294
  };
296
295
  var DOMNodeReference = class _DOMNodeReference {
297
296
  // properties initialized in the constructor
@@ -320,18 +319,7 @@ var DOMNodeReference = class _DOMNodeReference {
320
319
  /******/
321
320
  constructor(target, root = document.body, debounceTime2) {
322
321
  this.target = target;
323
- if (typeof target === "string") {
324
- let lName = null;
325
- const bracketMatch = target.match(/\[([^\]]+)\]/);
326
- if (bracketMatch) {
327
- lName = bracketMatch[1];
328
- const quoteMatch = lName.match(/["']([^"']+)["']/);
329
- if (quoteMatch) {
330
- lName = quoteMatch[1];
331
- }
332
- }
333
- this.logicalName = (lName || target).replace(/[#\[\]]/g, "");
334
- }
322
+ this.logicalName = this.extractLogicalName(target);
335
323
  this.root = root;
336
324
  this[debounceTime] = debounceTime2;
337
325
  this.isLoaded = false;
@@ -339,6 +327,14 @@ var DOMNodeReference = class _DOMNodeReference {
339
327
  this.value = null;
340
328
  this[bindMethods]();
341
329
  }
330
+ extractLogicalName(target) {
331
+ if (typeof target !== "string") return "";
332
+ const bracketMatch = target.match(/\[([^\]]+)\]/);
333
+ if (!bracketMatch) return target.replace(/[#\[\]]/g, "");
334
+ const content = bracketMatch[1];
335
+ const quoteMatch = content.match(/["']([^"']+)["']/);
336
+ return (quoteMatch?.[1] || content).replace(/[#\[\]]/g, "");
337
+ }
342
338
  async [init]() {
343
339
  try {
344
340
  if (this.target instanceof HTMLElement) {
@@ -385,31 +381,23 @@ var DOMNodeReference = class _DOMNodeReference {
385
381
  * Initializes value synchronization with appropriate event listeners
386
382
  * based on element type.
387
383
  */
388
- async [valueSync]() {
389
- try {
390
- if (!this[isValidFormElement](this.element)) return;
391
- this.updateValue();
392
- let eventType;
393
- if (this.element instanceof HTMLSelectElement) {
394
- eventType = "change";
395
- } else if (this.element instanceof HTMLInputElement) {
396
- eventType = eventMapping[this.element.type] ?? "input";
397
- } else if (this.element instanceof HTMLTextAreaElement) {
398
- eventType = eventMapping[this.element.type] ?? "input";
399
- } else {
400
- eventType = "input";
401
- }
402
- this[registerEventListener](this.element, eventType, this.updateValue);
403
- if (this.element instanceof HTMLInputElement && this.element.dataset.type === "date") {
404
- await this[dateSync](this.element);
405
- }
406
- } catch (error) {
407
- throw new DOMNodeInitializationError(
408
- this,
409
- `Failed to initialize value sync: ${error}`
410
- );
384
+ [valueSync]() {
385
+ if (!this[isValidFormElement](this.element)) return;
386
+ this.updateValue();
387
+ const eventType = this.determineEventType();
388
+ this[registerEventListener](this.element, eventType, this.updateValue);
389
+ if (this.isDateInput()) {
390
+ this[dateSync](this.element);
411
391
  }
412
392
  }
393
+ determineEventType() {
394
+ if (this.element instanceof HTMLSelectElement) return "change";
395
+ if (!(this.element instanceof HTMLInputElement)) return EventTypes.DEFAULT;
396
+ return EventTypes[this.element.type.toUpperCase()] || EventTypes.DEFAULT;
397
+ }
398
+ isDateInput() {
399
+ return this.element instanceof HTMLInputElement && this.element.dataset.type === "date";
400
+ }
413
401
  [isValidFormElement](element) {
414
402
  return element instanceof HTMLInputElement || element instanceof HTMLSelectElement || element instanceof HTMLTextAreaElement || element instanceof HTMLSpanElement || element instanceof HTMLButtonElement || element instanceof HTMLFieldSetElement;
415
403
  }
@@ -623,16 +611,7 @@ var DOMNodeReference = class _DOMNodeReference {
623
611
  if (value instanceof Function) {
624
612
  value = value();
625
613
  }
626
- let eventType;
627
- if (this.element instanceof HTMLSelectElement) {
628
- eventType = "change";
629
- } else if (this.element instanceof HTMLInputElement) {
630
- eventType = eventMapping[this.element.type] ?? "input";
631
- } else if (this.element instanceof HTMLTextAreaElement) {
632
- eventType = eventMapping[this.element.type] ?? "input";
633
- } else {
634
- eventType = "input";
635
- }
614
+ const eventType = this.determineEventType();
636
615
  this.element.dispatchEvent(new Event(eventType, { bubbles: false }));
637
616
  if (this.element.classList.contains("boolean-radio") && this.yesRadio instanceof _DOMNodeReference && this.noRadio instanceof _DOMNodeReference) {
638
617
  this.yesRadio.element.checked = value;
@@ -706,9 +685,9 @@ var DOMNodeReference = class _DOMNodeReference {
706
685
  );
707
686
  if (childInputs.length > 0) {
708
687
  const promises = childInputs.map(async (input) => {
709
- const inputRef = await createDOMNodeReference(input, {
710
- multiple: false
711
- });
688
+ const inputRef = await createDOMNodeReference(
689
+ input
690
+ );
712
691
  return inputRef.clearValue();
713
692
  });
714
693
  await Promise.all(promises);
@@ -860,7 +839,7 @@ var DOMNodeReference = class _DOMNodeReference {
860
839
  }
861
840
  /**
862
841
  * Applies a business rule to manage visibility, required state, value, and disabled state dynamically.
863
- *
842
+ * @see {@link BusinessRule}
864
843
  * @param rule The business rule containing conditions for various actions.
865
844
  * @param dependencies For re-evaluation conditions when the state of the dependencies change
866
845
  * @returns Instance of this for method chaining.
@@ -1222,7 +1201,7 @@ function enhanceArray(array) {
1222
1201
  async function createDOMNodeReference(target, options = {
1223
1202
  multiple: false,
1224
1203
  root: document.body,
1225
- timeout: 0
1204
+ timeoutMs: 0
1226
1205
  }) {
1227
1206
  try {
1228
1207
  if (typeof options !== "object") {
@@ -1231,7 +1210,7 @@ async function createDOMNodeReference(target, options = {
1231
1210
  );
1232
1211
  }
1233
1212
  validateOptions(options);
1234
- const { multiple = false, root = document.body, timeout = 0 } = options;
1213
+ const { multiple = false, root = document.body, timeoutMs = 0 } = options;
1235
1214
  const isMultiple = typeof multiple === "function" ? multiple() : multiple;
1236
1215
  if (isMultiple) {
1237
1216
  if (typeof target !== "string") {
@@ -1239,17 +1218,17 @@ async function createDOMNodeReference(target, options = {
1239
1218
  `'target' must be of type 'string' if 'multiple' is set to 'true'. Received type: '${typeof target}'`
1240
1219
  );
1241
1220
  }
1242
- const elements = await waitFor(target, root, true, timeout);
1221
+ const elements = await waitFor(target, root, true, timeoutMs);
1243
1222
  const initializedElements = await Promise.all(
1244
1223
  elements.map(async (element) => {
1245
- const instance2 = new DOMNodeReference(element, root, timeout);
1224
+ const instance2 = new DOMNodeReference(element, root, timeoutMs);
1246
1225
  await instance2[init]();
1247
1226
  return new Proxy(instance2, createProxyHandler());
1248
1227
  })
1249
1228
  );
1250
1229
  return enhanceArray(initializedElements);
1251
1230
  }
1252
- const instance = new DOMNodeReference(target, root, timeout);
1231
+ const instance = new DOMNodeReference(target, root, timeoutMs);
1253
1232
  await instance[init]();
1254
1233
  return new Proxy(instance, createProxyHandler());
1255
1234
  } catch (e) {
@@ -1257,7 +1236,7 @@ async function createDOMNodeReference(target, options = {
1257
1236
  }
1258
1237
  }
1259
1238
  function validateOptions(options) {
1260
- const { multiple = false, root = document.body, timeout = 0 } = options;
1239
+ const { multiple = false, root = document.body, timeoutMs = 0 } = options;
1261
1240
  if (typeof multiple !== "boolean" && typeof multiple !== "function") {
1262
1241
  throw new Error(
1263
1242
  `'multiple' must be of type 'boolean' or 'function'. Received type: '${typeof multiple}'`
@@ -1276,9 +1255,9 @@ function validateOptions(options) {
1276
1255
  `'root' must be of type 'HTMLElement'. Received type: '${typeof root}'`
1277
1256
  );
1278
1257
  }
1279
- if (typeof timeout !== "number") {
1258
+ if (typeof timeoutMs !== "number") {
1280
1259
  throw new Error(
1281
- `'timeout' must be of type 'number'. Received type: '${typeof timeout}'`
1260
+ `'timeout' must be of type 'number'. Received type: '${typeof timeoutMs}'`
1282
1261
  );
1283
1262
  }
1284
1263
  return;
@@ -7,7 +7,7 @@ export default class DOMNodeReference {
7
7
  protected isLoaded: boolean;
8
8
  protected defaultDisplay: string;
9
9
  protected [s.observers]: Array<MutationObserver>;
10
- protected [s.boundEventListeners]: Array<IBoundEventListener>;
10
+ protected [s.boundEventListeners]: Array<BoundEventListener>;
11
11
  protected isRadio: boolean;
12
12
  protected radioType: RadioType | null;
13
13
  /**
@@ -42,12 +42,15 @@ export default class DOMNodeReference {
42
42
  * Defaults to 'document.body'
43
43
  */
44
44
  /******/ /******/ constructor(target: Element | string, root: Element | undefined, debounceTime: number);
45
+ private extractLogicalName;
45
46
  [s.init](): Promise<void>;
46
47
  /**
47
48
  * Initializes value synchronization with appropriate event listeners
48
49
  * based on element type.
49
50
  */
50
- protected [s.valueSync](): Promise<void>;
51
+ protected [s.valueSync](): void;
52
+ private determineEventType;
53
+ private isDateInput;
51
54
  protected [s.isValidFormElement](element: Element): element is FormElement;
52
55
  protected [s.registerEventListener](element: Element, eventType: keyof HTMLElementEventMap, handler: (e: Event) => unknown): void;
53
56
  protected [s.dateSync](element: HTMLInputElement): Promise<void>;
@@ -186,12 +189,12 @@ export default class DOMNodeReference {
186
189
  uncheckRadios(): DOMNodeReference;
187
190
  /**
188
191
  * Applies a business rule to manage visibility, required state, value, and disabled state dynamically.
189
- *
192
+ * @see {@link BusinessRule}
190
193
  * @param rule The business rule containing conditions for various actions.
191
194
  * @param dependencies For re-evaluation conditions when the state of the dependencies change
192
195
  * @returns Instance of this for method chaining.
193
196
  */
194
- applyBusinessRule(rule: IBusinessRule, dependencies: DOMNodeReference[]): DOMNodeReference;
197
+ applyBusinessRule(rule: BusinessRule, dependencies: DOMNodeReference[]): DOMNodeReference;
195
198
  /**
196
199
  * Configures conditional rendering for the target element based on a condition
197
200
  * and the visibility of one or more trigger elements.
@@ -1,21 +1,55 @@
1
1
  import DOMNodeReference from "./DOMNodeReference.js";
2
2
  import DOMNodeReferenceArray from "./DOMNodeReferenceArray.js";
3
- export default function createDOMNodeReference<T extends string>(target: Element, options?: ICreationOptions): Promise<DOMNodeReference>;
4
- export default function createDOMNodeReference(target: string, options?: Omit<ICreationOptions, "multiple"> & {
3
+ export default function createDOMNodeReference(target: string, options?: {
5
4
  /**
6
- * Should this call return an array of instantiated references, or just a single instance?
5
+ * Should this call return an array of instantiated references, or just a single?
7
6
  * Defaults to false, returning a single instance.
8
7
  */
9
8
  multiple?: false;
9
+ /**
10
+ * Optionally specify the element within which to search for the element targeted by 'target'.
11
+ * Defaults to 'document.body'.
12
+ */
13
+ root?: HTMLElement;
14
+ /**
15
+ * Optionally specify the amount of time that should be waited to find the targeted element before throwing an error.
16
+ * Useful for async DOM loading. Relies on MutationObserver.
17
+ * WARNING: Implementing multiple references with timeout can result in infinite loading.
18
+ */
19
+ timeoutMs?: number;
10
20
  }): Promise<DOMNodeReference>;
11
- export default function createDOMNodeReference<T extends string>(target: string, options?: Omit<ICreationOptions, "multiple"> & {
21
+ export default function createDOMNodeReference(target: Element, options?: {
12
22
  /**
13
- * Should this call return an array of instantiated references, or just a single instance?
23
+ * Optionally specify the element within which to search for the element targeted by 'target'.
24
+ * Defaults to 'document.body'.
25
+ */
26
+ root?: HTMLElement;
27
+ /**
28
+ * Optionally specify the amount of time that should be waited to find the targeted element before throwing an error.
29
+ * Useful for async DOM loading. Relies on MutationObserver.
30
+ * WARNING: Implementing multiple references with timeout can result in infinite loading.
31
+ */
32
+ timeoutMs?: number;
33
+ }): Promise<DOMNodeReference>;
34
+ export default function createDOMNodeReference(target: string, options?: {
35
+ /**
36
+ * Should this call return an array of instantiated references, or just a single?
14
37
  * Defaults to false, returning a single instance.
15
38
  */
16
39
  multiple?: true;
40
+ /**
41
+ * Optionally specify the element within which to search for the element targeted by 'target'.
42
+ * Defaults to 'document.body'.
43
+ */
44
+ root?: HTMLElement;
45
+ /**
46
+ * Optionally specify the amount of time that should be waited to find the targeted element before throwing an error.
47
+ * Useful for async DOM loading. Relies on MutationObserver.
48
+ * WARNING: Implementing multiple references with timeout can result in infinite loading.
49
+ */
50
+ timeoutMs?: number;
17
51
  }): Promise<DOMNodeReferenceArray>;
18
- export declare function validateOptions(options: any): void;
52
+ export declare function validateOptions(options: Partial<CreationOptions>): void;
19
53
  export declare function createProxyHandler(): {
20
54
  get: (target: DOMNodeReference, prop: string | symbol) => any;
21
55
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "powerpagestoolkit",
3
- "version": "2.7.102",
3
+ "version": "2.7.104",
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.js",
6
6
  "types": "./dist/index.d.ts",
@@ -67,7 +67,7 @@
67
67
  "exports": {
68
68
  ".": {
69
69
  "import": "./dist/bundle.js",
70
- "types": "./dist/index.d.ts"
70
+ "types": "./@types/index.d.ts"
71
71
  },
72
72
  "./createDOMNodeReference": {
73
73
  "import": "./dist/createDOMNodeReference.js",