@cocreate/element-prototype 1.18.0 → 1.20.0

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/CHANGELOG.md CHANGED
@@ -1,3 +1,30 @@
1
+ # [1.20.0](https://github.com/CoCreate-app/CoCreate-element-prototype/compare/v1.19.0...v1.20.0) (2024-02-03)
2
+
3
+
4
+ ### Bug Fixes
5
+
6
+ * distpatchEvents if innerHtml ([9d55c26](https://github.com/CoCreate-app/CoCreate-element-prototype/commit/9d55c26492014af2750a38ea7ea7e939b3df6577))
7
+ * remove api typo ([2d7728b](https://github.com/CoCreate-app/CoCreate-element-prototype/commit/2d7728b8e52d1d0d290ebfac1c35308cfb9c200a))
8
+
9
+
10
+ ### Features
11
+
12
+ * value-types to handle date and time ([3a72fbc](https://github.com/CoCreate-app/CoCreate-element-prototype/commit/3a72fbc23d9b8008a0bc488ac8230f5dfc2f86ea))
13
+
14
+ # [1.19.0](https://github.com/CoCreate-app/CoCreate-element-prototype/compare/v1.18.0...v1.19.0) (2024-01-30)
15
+
16
+
17
+ ### Bug Fixes
18
+
19
+ * improved input true and false value ([b788abb](https://github.com/CoCreate-app/CoCreate-element-prototype/commit/b788abba4ea09c774af693f89bf6da96a5721665))
20
+ * value must be a string to test for url operators ([de1ed9d](https://github.com/CoCreate-app/CoCreate-element-prototype/commit/de1ed9d64164b08098c34c0ceb5b51918d201f0e))
21
+
22
+
23
+ ### Features
24
+
25
+ * handle url search params ([2bc548e](https://github.com/CoCreate-app/CoCreate-element-prototype/commit/2bc548ed1adf74dfa795161e8d7959f79d75482d))
26
+ * special values and regex support ([083d56a](https://github.com/CoCreate-app/CoCreate-element-prototype/commit/083d56a4ab63aa6a8d347770dcfda5ddafedc866))
27
+
1
28
  # [1.18.0](https://github.com/CoCreate-app/CoCreate-element-prototype/compare/v1.17.0...v1.18.0) (2024-01-17)
2
29
 
3
30
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cocreate/element-prototype",
3
- "version": "1.18.0",
3
+ "version": "1.20.0",
4
4
  "description": "A simple element-prototype component in vanilla javascript. Easily configured using HTML5 data-attributes and/or JavaScript API.",
5
5
  "keywords": [
6
6
  "element-prototype",
package/src/getValue.js CHANGED
@@ -17,7 +17,7 @@ HTMLHeadingElement.prototype.getValue = function () {
17
17
 
18
18
  // TODO: check if using a a switch case will provide better performance
19
19
  const getValue = (element) => {
20
- let value = element.value || element.getAttribute('value')
20
+ let value = element.value || element.getAttribute('value') || "";
21
21
  if (element.hasAttribute('component') || element.hasAttribute('plugin')) {
22
22
  value = storage.get(element)
23
23
  storage.delete(element)
@@ -26,6 +26,7 @@ const getValue = (element) => {
26
26
 
27
27
  let prefix = element.getAttribute('value-prefix') || "";
28
28
  let suffix = element.getAttribute('value-suffix') || "";
29
+ let valueType = element.getAttribute('value-type');
29
30
 
30
31
  if (element.type === "checkbox") {
31
32
  let inputs = [element]
@@ -64,6 +65,11 @@ const getValue = (element) => {
64
65
  value = [Number(element.min), Number(element.value)];
65
66
  } else if (element.type === "password") {
66
67
  value = btoa(value || '');
68
+ } else if (element.type === "email") {
69
+ value = value.toLowerCase();
70
+ } else if (element.type === "url") {
71
+ // TODO: define attributes to return url parts
72
+ // return as a string or an object of url parts
67
73
  } else if (element.tagName == "SELECT" && element.hasAttribute('multiple')) {
68
74
  let options = element.selectedOptions;
69
75
  value = [];
@@ -73,10 +79,40 @@ const getValue = (element) => {
73
79
  optionValue = prefix + optionValue + suffix;
74
80
  value.push(optionValue);
75
81
  }
76
- } else if (["time", "datetime", "datetime-local"].includes(element.type)) {
77
- value = new Date(value).toISOString();
78
- if (el.type === 'time')
79
- value = value.substring(11, 8) + 'Z';
82
+ } else if (["time", "date", "datetime", "datetime-local"].includes(element.type)) {
83
+ if (value === '$now')
84
+ value = new Date()
85
+ else if (value)
86
+ value = new Date(value)
87
+
88
+ if (value) {
89
+ if (!valueType)
90
+ value = value.toISOString()
91
+
92
+ if (element.type === 'time')
93
+ // value = value.substring(11, 8) + 'Z';
94
+ value = value.substring(11, 19) + 'Z';
95
+
96
+ switch (valueType) {
97
+ case 'getDayName':
98
+ const days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
99
+ value = days[value.getDay()];
100
+ break;
101
+ case 'getMonthName':
102
+ const months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
103
+ value = months[value.getMonth()];
104
+ break;
105
+ case 'toUnixTimestamp':
106
+ value = Math.floor(value.getTime() / 1000);
107
+ break;
108
+ default:
109
+ if (typeof value[valueType] === 'function') {
110
+ value = value[valueType]();
111
+ } else {
112
+ console.warn(`The method ${valueType} is not a function of Date object.`);
113
+ }
114
+ }
115
+ }
80
116
  } else if (element.tagName == 'INPUT' || element.tagName == 'SELECT') {
81
117
  value = element.value;
82
118
  } else if (element.tagName == 'TEXTAREA') {
@@ -92,7 +128,6 @@ const getValue = (element) => {
92
128
  value = element.innerHTML;
93
129
  }
94
130
 
95
- let valueType = element.getAttribute('value-type');
96
131
  if (!Array.isArray(value)) {
97
132
  if (prefix || suffix)
98
133
  value = prefix + value + suffix;
@@ -109,13 +144,76 @@ const getValue = (element) => {
109
144
  }
110
145
  }
111
146
 
112
- if (value === '$user_id')
113
- value = localStorage.getItem('user_id')
114
- else if (value === '$organization_id')
147
+ if (value === '$organization_id')
115
148
  value = localStorage.getItem('organization_id')
149
+ else if (value === '$user_id')
150
+ value = localStorage.getItem('user_id')
116
151
  else if (value === '$clientId')
117
152
  value = localStorage.getItem('clientId')
153
+ else if (value === '$session_id')
154
+ value = localStorage.getItem('session_id')
155
+ else if (typeof value === 'string') {
156
+ if (value.startsWith('$search')) {
157
+ const searchParams = new URLSearchParams(window.location.search);
158
+ if (value.includes('.')) {
159
+ value = searchParams.get(value.split('.')[1]);
160
+ } else {
161
+ const paramsObject = {};
162
+
163
+ // Iterate over all key-value pairs and add them to the object
164
+ for (const [key, value] of searchParams) {
165
+ paramsObject[key] = value;
166
+ }
167
+ value = paramsObject
168
+ }
169
+
170
+ } else if ([
171
+ '$href',
172
+ '$origin',
173
+ '$protocol',
174
+ '$host',
175
+ '$hostname',
176
+ '$port',
177
+ '$pathname',
178
+ '$hash'
179
+ ].includes(value)) {
180
+ value = window.location[value.substring(1)]
181
+ }
182
+ }
183
+
184
+ let replace = element.getAttribute('value-replace');
185
+ let replaceAll = element.getAttribute('value-replaceall');
186
+
187
+ if (replace || replaceAll) {
188
+ let replaceWith = element.getAttribute('value-replace-with') || "";
189
+ let replaceRegex = element.getAttribute('value-replace-regex');
190
+
191
+ if (replaceRegex) {
192
+ try {
193
+ if (replace)
194
+ value = value.replace(replaceRegex, replaceWith);
195
+ else
196
+ value = value.replaceAll(replaceRegex, replaceWith);
197
+ } catch (error) {
198
+ console.error('getValue() Regex error:', error, element);
199
+ }
200
+ } else {
201
+ if (replace)
202
+ value = value.replace(replace, replaceWith);
203
+ else
204
+ value = value.replaceAll(replaceAll, replaceWith);
205
+ }
206
+ }
207
+
208
+ let lowercase = element.getAttribute('value-lowercase')
209
+ if (lowercase || lowercase === '')
210
+ value = value.toLowerCase()
211
+ let uppercase = element.getAttribute('value-uppercase');
212
+ if (uppercase || uppercase === '')
213
+ value = value.toUpperCase()
214
+
118
215
  return value;
216
+
119
217
  };
120
218
 
121
219
  export { getValue, storage };
package/src/setValue.js CHANGED
@@ -47,7 +47,9 @@ const setValue = (el, value, dispatch) => {
47
47
 
48
48
  for (let i = 0; i < inputs.length; i++) {
49
49
  if (inputs[i].value) {
50
- if (value === true || value.includes(inputs[i].value))
50
+ if (value === true || value === false)
51
+ inputs[i].checked = value;
52
+ else if (value.includes(inputs[i].value))
51
53
  inputs[i].checked = true;
52
54
  else
53
55
  inputs[i].checked = false;
@@ -130,6 +132,10 @@ const setValue = (el, value, dispatch) => {
130
132
  if (el.hasAttribute("value")) {
131
133
  el.setAttribute("value", value);
132
134
  }
135
+
136
+
137
+ dispatchEvents(el, dispatch);
138
+
133
139
  }
134
140
 
135
141
  if (el.getAttribute('contenteditable'))