powerpagestoolkit 1.3.1041 → 1.3.3001

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,34 @@ 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
+ /**
63
+ * If the element targeted is the main input for a yes/no radio control,
64
+ * yesRadio and noRadio will be available as properties of 'this'
65
+ */
66
+ yesRadio: DOMNodeReference;
67
+ noRadio: DOMNodeReference;
68
+ // and if 'this' is the instance of a yesRadio or noRadio
69
+ // checked will represent wether the radio has been checked or not
70
+ checked: boolean
60
71
  ```
61
72
 
62
73
  ##### Methods
63
74
 
64
75
  Here are the key methods you can use with a DOMNodeReference instance:
65
76
 
66
- ```typescript
77
+ ```javascript
78
+
79
+ /********/
80
+ // VISIBILITY / ACCESSIBILITY
67
81
 
68
82
  // Hides the associated DOM element.
69
83
  hide()
@@ -71,8 +85,120 @@ hide()
71
85
  // Shows the associated DOM element.
72
86
  show()
73
87
 
88
+ /**
89
+ * advanced visibility control in the case you need to apply
90
+ * custom logic to the visibility of an element
91
+ */
92
+ toggleVisibility(shouldShow: boolean | () => boolean)
93
+
94
+ /**
95
+ * Configures conditional rendering for the target element
96
+ * based on a condition and the visibility of one or more trigger elements.
97
+ *
98
+ * @param {(this: DOMNodeReference) => boolean} condition -
99
+ * A function that returns a boolean to determine the visibility
100
+ * of the target element. If `condition()` returns true, the
101
+ * element is shown; otherwise, it is hidden.
102
+ * @param {Array<DOMNodeReference>} dependencies - An array
103
+ * of `DOMNodeReference` instances. Event listeners are
104
+ * registered on each to toggle the visibility of the
105
+ * target element based on the `condition` and the
106
+ * visibility of the target node.
107
+ */
108
+ configureConditionalRendering(
109
+ condition: (this: DOMNodeReference) => boolean,
110
+ dependencies: Array<DOMNodeReference>
111
+ )
112
+
113
+
114
+ // EXAMPLE:
115
+ const your_node = await createDOMNodeReference("#element_id")
116
+ const other_node = await createDOMNodeReference(".element_class")
117
+
118
+ your_node.configureConditionalRendering(() =>
119
+ other_node.value == "3",
120
+ /* your_node will only be
121
+ visible when the value of other_node is "3"
122
+ */
123
+ [other_node]
124
+ /* and we have to include any DOMNodeReferences used
125
+ in the evaluation logic, so that changes to them can
126
+ be watched and the condition evaluated again
127
+ */
128
+ );
129
+
130
+
131
+ /**
132
+ * Sets up validation and requirement rules for the field.
133
+ * This function dynamically updates the field's required status
134
+ * and validates its input based on the specified conditions.
135
+ *
136
+ * @param {function(this: DOMNodeReference): boolean} isRequired
137
+ * A function that determines whether the field should be required.
138
+ * Return `true` if required, `false` to not be required.
139
+ * @param {function(this: DOMNodeReference): boolean} isValid
140
+ * A function that checks if the field's input is valid.
141
+ * Return `true` if validation satisfied, `false` if not.
142
+ * @param {string} fieldDisplayName - The name of the field, used
143
+ * in error messages if validation fails.
144
+ * @param {Array<DOMNodeReference>} [dependencies]
145
+ * Other fields that this field’s requirement depends on. When
146
+ * these Nodes or their values change, the required status
147
+ * of this field is re-evaluated. Make sure any DOMNodeReference
148
+ * used in `isRequired` or `isValid` is included in this array.
149
+ */
150
+ configureValidationAndRequirements(
151
+ isRequired: (this: this) => boolean,
152
+ isValid: (this: this) => boolean,
153
+ fieldDisplayName: string,
154
+ dependencies: Array<DOMNodeReference>
155
+ )
156
+
157
+ // EXAMPLE:
158
+ const your_node = await createDOMNodeReference("#element_id")
159
+ const other_node = await createDOMNodeReference(".element_class")
160
+
161
+ your_node.configureValidationAndRequirements(
162
+ () => other_node.yesRadio.checked,
163
+ /* if 'yes' is checked for this other node,
164
+ this function will evaluate to true,
165
+ meaning that 'your_node' will be required */
166
+
167
+ function () {
168
+ /* important to use standard 'function' declaration,
169
+ instead of arrow function when needing to
170
+ access 'this' (the instance of 'your_node') */
171
+
172
+ if (other_node.yesRadio.checked) {
173
+ // when other_node radio is checked 'yes'
174
+ return this.value; // this is only 'valid' if it has a value
175
+ } else return true;
176
+ },
177
+ "Your Field Name",
178
+ [other_node]
179
+ /* since our conditions depend on
180
+ 'other_node' it must be included in the dependency
181
+ array so that the requirement conditions can be
182
+ re-evaluated when the value of 'other_node' changes */
183
+ );
184
+
185
+
186
+ /* sets the elements 'disabled' to true - useful for inputs
187
+ that need to be enabled/disabled conditionally */
188
+ disable()
189
+
190
+ // Sets the element 'disabled' to false
191
+ enable()
192
+ ```
193
+
194
+ ```javascript
195
+ // OTHER METHODS
196
+
74
197
  // Sets the value of the associated HTML element.
75
- setValue(value: string)
198
+ setValue(value: any)
199
+
200
+ // Sets the inner HTML content of the associated HTML element.
201
+ setTextContent(text: string)
76
202
 
77
203
  // Appends child elements to the associated HTML element.
78
204
  append(...elements: HTMLElement[])
@@ -86,33 +212,18 @@ getLabel(): HTMLElement | null
86
212
  // Appends child elements to the label associated with the HTML element.
87
213
  appendToLabel(...elements: HTMLElement[])
88
214
 
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)
215
+ // Create an event listener on the target element. Provide access to 'this'
216
+ // in the event handler function
217
+ on(eventType: string, eventHandler: (this: DOMNodeReference) => void)
94
218
 
95
219
  // Unchecks both yes and no radio buttons if they exist.
96
220
  uncheckRadios()
97
221
 
98
- //Creates a validation instance for the field.
99
- createValidation(evaluationFunction: () => boolean, fieldDisplayName: string)
100
-
101
222
  // Adds a tooltip to the label associated with the HTML element.
102
223
  addLabelTooltip(text: string)
103
224
 
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)
225
+ // Adds a tooltip with the specified text to the element
226
+ addTooltip(text: string)
116
227
 
117
228
  // Executes a callback function once the element is fully loaded.
118
229
  onceLoaded(callback: (instance: DOMNodeReference) => void)
@@ -815,7 +815,7 @@ var DOMNodeReference = /*#__PURE__*/function () {
815
815
  case 10:
816
816
  this._initValueSync();
817
817
  this._attachVisibilityController();
818
- this.defaultDisplay = this.visibilityController.style.display;
818
+ this.defaultDisplay = this.visibilityController.style.display || "inline-block";
819
819
  this.isLoaded = true;
820
820
  _context.next = 19;
821
821
  break;
@@ -983,17 +983,65 @@ var DOMNodeReference = /*#__PURE__*/function () {
983
983
  throw new Error("There was an error trying to disable the target: ".concat(this.target));
984
984
  }
985
985
  }
986
+ }, {
987
+ key: "prepend",
988
+ value: function prepend() {
989
+ var _this = this;
990
+ for (var _len = arguments.length, nodes = new Array(_len), _key = 0; _key < _len; _key++) {
991
+ nodes[_key] = arguments[_key];
992
+ }
993
+ nodes.forEach(function (node) {
994
+ if (node instanceof DOMNodeReference) {
995
+ _this.element.prepend(node.element);
996
+ } else {
997
+ _this.element.prepend(node);
998
+ }
999
+ });
1000
+ }
986
1001
  }, {
987
1002
  key: "append",
988
1003
  value: function append() {
989
- var _this$element;
990
- (_this$element = this.element).append.apply(_this$element, arguments);
1004
+ var _this2 = this;
1005
+ for (var _len2 = arguments.length, nodes = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
1006
+ nodes[_key2] = arguments[_key2];
1007
+ }
1008
+ nodes.forEach(function (node) {
1009
+ if (node instanceof DOMNodeReference) {
1010
+ _this2.element.append(node.element);
1011
+ } else {
1012
+ _this2.element.append(node);
1013
+ }
1014
+ });
1015
+ }
1016
+ }, {
1017
+ key: "before",
1018
+ value: function before() {
1019
+ var _this3 = this;
1020
+ for (var _len3 = arguments.length, nodes = new Array(_len3), _key3 = 0; _key3 < _len3; _key3++) {
1021
+ nodes[_key3] = arguments[_key3];
1022
+ }
1023
+ nodes.forEach(function (node) {
1024
+ if (node instanceof DOMNodeReference) {
1025
+ _this3.element.before(node.element);
1026
+ } else {
1027
+ _this3.element.before(node);
1028
+ }
1029
+ });
991
1030
  }
992
1031
  }, {
993
1032
  key: "after",
994
1033
  value: function after() {
995
- var _this$element2;
996
- (_this$element2 = this.element).after.apply(_this$element2, arguments);
1034
+ var _this4 = this;
1035
+ for (var _len4 = arguments.length, nodes = new Array(_len4), _key4 = 0; _key4 < _len4; _key4++) {
1036
+ nodes[_key4] = arguments[_key4];
1037
+ }
1038
+ nodes.forEach(function (node) {
1039
+ if (node instanceof DOMNodeReference) {
1040
+ _this4.element.after(node.element);
1041
+ } else {
1042
+ _this4.element.after(node);
1043
+ }
1044
+ });
997
1045
  }
998
1046
  }, {
999
1047
  key: "getLabel",
@@ -1005,8 +1053,8 @@ var DOMNodeReference = /*#__PURE__*/function () {
1005
1053
  value: function appendToLabel() {
1006
1054
  var label = this.getLabel();
1007
1055
  if (label) {
1008
- for (var _len = arguments.length, elements = new Array(_len), _key = 0; _key < _len; _key++) {
1009
- elements[_key] = arguments[_key];
1056
+ for (var _len5 = arguments.length, elements = new Array(_len5), _key5 = 0; _key5 < _len5; _key5++) {
1057
+ elements[_key5] = arguments[_key5];
1010
1058
  }
1011
1059
  label.append.apply(label, [" "].concat(elements));
1012
1060
  }
@@ -1017,8 +1065,8 @@ var DOMNodeReference = /*#__PURE__*/function () {
1017
1065
  this.appendToLabel(CreateInfoEl(text));
1018
1066
  }
1019
1067
  }, {
1020
- key: "addToolTip",
1021
- value: function addToolTip(text) {
1068
+ key: "addTooltip",
1069
+ value: function addTooltip(text) {
1022
1070
  this.append(CreateInfoEl(text));
1023
1071
  }
1024
1072
  }, {
@@ -1026,6 +1074,17 @@ var DOMNodeReference = /*#__PURE__*/function () {
1026
1074
  value: function setTextContent(text) {
1027
1075
  this.element.innerHTML = text;
1028
1076
  }
1077
+ }, {
1078
+ key: "setStyle",
1079
+ value: function setStyle(options) {
1080
+ var _this5 = this;
1081
+ if (!Object.prototype.toString.call(options) !== "[object Object]") {
1082
+ throw new Error("powerpagestoolkit: 'DOMNodeReference.setStyle' required options to be in the form of an object. Argument passed was of type: ".concat(DOMNodeReferences_typeof(options)));
1083
+ }
1084
+ Object.keys(options).forEach(function (key) {
1085
+ _this5.element.style[key] = options[key];
1086
+ });
1087
+ }
1029
1088
  }, {
1030
1089
  key: "uncheckRadios",
1031
1090
  value: function uncheckRadios() {
@@ -1039,18 +1098,18 @@ var DOMNodeReference = /*#__PURE__*/function () {
1039
1098
  }, {
1040
1099
  key: "configureConditionalRendering",
1041
1100
  value: function configureConditionalRendering(condition, triggerNodes) {
1042
- var _this = this;
1101
+ var _this6 = this;
1043
1102
  try {
1044
- this.toggleVisibility(condition());
1103
+ this.toggleVisibility(condition(this));
1045
1104
  if (triggerNodes) {
1046
1105
  var nodes = Array.isArray(triggerNodes) ? triggerNodes : [triggerNodes];
1047
1106
  nodes.forEach(function (node) {
1048
1107
  node.on("change", function () {
1049
- return _this.toggleVisibility(condition());
1108
+ return _this6.toggleVisibility(condition(_this6));
1050
1109
  });
1051
1110
  var observer = new MutationObserver(function () {
1052
1111
  var display = window.getComputedStyle(node.visibilityController).display;
1053
- _this.toggleVisibility(display !== "none" && condition());
1112
+ _this6.toggleVisibility(display !== "none" && condition(_this6));
1054
1113
  });
1055
1114
  observer.observe(node.visibilityController, {
1056
1115
  attributes: true,
@@ -1065,7 +1124,7 @@ var DOMNodeReference = /*#__PURE__*/function () {
1065
1124
  }, {
1066
1125
  key: "configureValidationAndRequirements",
1067
1126
  value: function configureValidationAndRequirements(isRequired, isValid, fieldDisplayName) {
1068
- var _this2 = this;
1127
+ var _this7 = this;
1069
1128
  var dependencies = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : [];
1070
1129
  if (typeof Page_Validators !== "undefined") {
1071
1130
  var newValidator = document.createElement("span");
@@ -1084,7 +1143,7 @@ var DOMNodeReference = /*#__PURE__*/function () {
1084
1143
  dependencies = Array.isArray(dependencies) ? dependencies : [dependencies];
1085
1144
  dependencies.forEach(function (dep) {
1086
1145
  dep.element.addEventListener("change", function () {
1087
- return _this2.setRequiredLevel(isRequired(_this2));
1146
+ return _this7.setRequiredLevel(isRequired(_this7));
1088
1147
  });
1089
1148
  });
1090
1149
  }
@@ -1100,15 +1159,15 @@ var DOMNodeReference = /*#__PURE__*/function () {
1100
1159
  }, {
1101
1160
  key: "onceLoaded",
1102
1161
  value: function onceLoaded(callback) {
1103
- var _this3 = this;
1162
+ var _this8 = this;
1104
1163
  if (this.isLoaded) {
1105
1164
  callback(this);
1106
1165
  } else {
1107
1166
  var observer = new MutationObserver(function () {
1108
- if (document.querySelector(_this3.target)) {
1167
+ if (document.querySelector(_this8.target)) {
1109
1168
  observer.disconnect(); // Stop observing once loaded
1110
- _this3.isLoaded = true;
1111
- callback(_this3); // Call the provided callback
1169
+ _this8.isLoaded = true;
1170
+ callback(_this8); // Call the provided callback
1112
1171
  }
1113
1172
  });
1114
1173
  observer.observe(document.body, {
@@ -1159,8 +1218,8 @@ function _createDOMNodeReference() {
1159
1218
  var value = target[prop];
1160
1219
  if (typeof value === "function" && prop !== "onceLoaded") {
1161
1220
  return function () {
1162
- for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
1163
- args[_key2] = arguments[_key2];
1221
+ for (var _len6 = arguments.length, args = new Array(_len6), _key6 = 0; _key6 < _len6; _key6++) {
1222
+ args[_key6] = arguments[_key6];
1164
1223
  }
1165
1224
  return target.onceLoaded(function () {
1166
1225
  return value.apply(target, args);
package/index.d.ts CHANGED
@@ -8,7 +8,6 @@ class DOMNodeReference {
8
8
  */
9
9
  constructor(target: string): DOMNodeReference;
10
10
 
11
- target: string;
12
11
  /**
13
12
  * The element targeted when instantiating DOMNodeReference.
14
13
  * Made available in order to perform normal DOM traversal,
@@ -17,8 +16,6 @@ class DOMNodeReference {
17
16
  */
18
17
  element: HTMLElement | null;
19
18
  isLoaded: boolean;
20
- visibilityController: HTMLElement | null;
21
- defaultDisplay: string;
22
19
  /**
23
20
  * The value of the element that this node represents
24
21
  * stays in syncs with the live DOM elements via event handler
@@ -78,27 +75,27 @@ class DOMNodeReference {
78
75
 
79
76
  /**
80
77
  * Prepends elements to the target
81
- * @param {...HTMLElement} elements - The elements to prepend to the HTML element
78
+ * @param {HTMLElement[] | DOMNodeReference[]} nodes - The elements to prepend to the HTML element
82
79
  */
83
- prepend(...elements: HTMLElement[]): void;
80
+ prepend(...nodes: HTMLElement[] | DOMNodeReference[]): void;
84
81
 
85
82
  /**
86
83
  * Appends child elements to the HTML element.
87
- * @param {...HTMLElement} elements - The elements to append to the HTML element.
84
+ * @param {HTMLElement[] | DOMNodeReference[]} nodes - The elements to append to the HTML element.
88
85
  */
89
- append(...elements: HTMLElement[]): void;
86
+ append(...nodes: HTMLElement[] | DOMNodeReference[]): void;
90
87
 
91
88
  /**
92
89
  * Inserts elements before the HTML element.
93
- * @param {...HTMLElement} elements - The elements to insert before the HTML element.
90
+ * @param {HTMLElement[] | DOMNodeReference[]} nodes - The elements to insert before the HTML element.
94
91
  */
95
- before(...elements: HTMLElement[]): void;
92
+ before(...nodes: HTMLElement[] | DOMNodeReference[]): void;
96
93
 
97
94
  /**
98
95
  * Inserts elements after the HTML element.
99
- * @param {...HTMLElement} elements - The elements to insert after the HTML element.
96
+ * @param {HTMLElement[] | DOMNodeReference[]} nodes - The elements to insert after the HTML element.
100
97
  */
101
- after(...elements: HTMLElement[]): void;
98
+ after(...nodes: HTMLElement[] | DOMNodeReference[]): void;
102
99
 
103
100
  /**
104
101
  * Retrieves the label associated with the HTML element.
@@ -127,18 +124,18 @@ class DOMNodeReference {
127
124
  uncheckRadios(): void;
128
125
 
129
126
  /**
130
- * Configures validation and requirement conditions for the field based on the provided logic functions and dependencies.
131
- * Creates a validator and sets a required level dynamically based on dependency changes.
132
- * @param {(this: DOMNodeReference) => boolean} isRequired - Function to determine if the field is required.
133
- * @param {(this: DOMNodeReference) => boolean} isValid - Function to evaluate the field's validity.
134
- * @param {string} fieldDisplayName - Display name used in error messages if validation fails.
135
- * @param {Array<DOMNodeReference>} [dependencies] - Optional dependencies for setting requirement conditions dynamically.
127
+ * 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.
128
+ *
129
+ * @param {function(this: DOMNodeReference): boolean} isRequired - A function that determines whether the field should be required. Returns `true` if required, `false` otherwise.
130
+ * @param {function(this: DOMNodeReference): boolean} isValid - A function that checks if the field's input is valid. Returns `true` if valid, `false` otherwise.
131
+ * @param {string} fieldDisplayName - The name of the field, used in error messages if validation fails.
132
+ * @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.
136
133
  */
137
134
  configureValidationAndRequirements(
138
135
  isRequired: (this: this) => boolean,
139
136
  isValid: (this: this) => boolean,
140
137
  fieldDisplayName: string,
141
- dependencies?: DOMNodeReference[]
138
+ dependencies: Array<DOMNodeReference>
142
139
  ): void;
143
140
 
144
141
  /**
@@ -155,12 +152,25 @@ class DOMNodeReference {
155
152
  */
156
153
  addLabelTooltip(text: string): void;
157
154
 
155
+ /**
156
+ * Adds a tooltip with the specified text to the element
157
+ * @param {string} text - The text to display in the tooltip
158
+ */
159
+ addTooltip(text: string): void;
160
+
158
161
  /**
159
162
  * Sets the inner HTML content of the HTML element.
160
163
  * @param {string} text - The text to set as the inner HTML of the element.
161
164
  */
162
165
  setTextContent(text: string): void;
163
166
 
167
+ /**
168
+ *
169
+ * @param {Partial<CSSStyleDeclaration>} options - An object with the style properties (keys) and updated styles (values)
170
+ * to apply to the this. {"key": "value"}
171
+ */
172
+ setStyle(options: Partial<CSSStyleDeclaration>): void;
173
+
164
174
  /**
165
175
  *
166
176
  * @param {boolean} shouldShow shows or hides the target
@@ -172,16 +182,16 @@ class DOMNodeReference {
172
182
  * Configures conditional rendering for the target element based on a condition
173
183
  * and the visibility of one or more trigger elements.
174
184
  *
175
- * @param {Function} condition - A function that returns a boolean to determine
185
+ * @param {(this: DOMNodeReference) => boolean} condition - A function that returns a boolean to determine
176
186
  * the visibility of the target element. If `condition()` returns true, the element is shown;
177
187
  * otherwise, it is hidden.
178
- * @param {DOMNodeReference | DOMNodeReference[]} triggerNodes - A single `DOMNodeReference`
179
- * or an array of `DOMNodeReference` instances. Event listeners are registered on each
180
- * `triggerNode` to toggle the visibility of the target element based on the `condition`.
188
+ * @param {Array<DOMNodeReference>} dependencies - An array of `DOMNodeReference` instances. Event listeners are
189
+ * registered on each to toggle the visibility of the target element based on the `condition` and the visibility of
190
+ * the target node.
181
191
  */
182
192
  configureConditionalRendering(
183
- condition: () => boolean,
184
- triggerNodes?: DOMNodeReference | DOMNodeReference[]
193
+ condition: (this: DOMNodeReference) => boolean,
194
+ dependencies: DOMNodeReference[]
185
195
  ): void;
186
196
 
187
197
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "powerpagestoolkit",
3
- "version": "1.3.1041",
3
+ "version": "1.3.3001",
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.bundle.js",
6
6
  "types": "index.d.ts",