@cocreate/events 1.4.0 → 1.4.1

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,10 @@
1
+ ## [1.4.1](https://github.com/CoCreate-app/CoCreate-events/compare/v1.4.0...v1.4.1) (2023-02-21)
2
+
3
+
4
+ ### Bug Fixes
5
+
6
+ * improved key usage ([a92e1f2](https://github.com/CoCreate-app/CoCreate-events/commit/a92e1f25d7b4c83dfa0beb9a7526c9ca3dc184fe))
7
+
1
8
  # [1.4.0](https://github.com/CoCreate-app/CoCreate-events/compare/v1.3.0...v1.4.0) (2023-02-19)
2
9
 
3
10
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cocreate/events",
3
- "version": "1.4.0",
3
+ "version": "1.4.1",
4
4
  "description": "A simple events component in vanilla javascript. Easily configured using HTML5 attributes and/or JavaScript API.",
5
5
  "keywords": [
6
6
  "events",
package/src/index.js CHANGED
@@ -9,9 +9,9 @@ const CoCreateEvents = {
9
9
  else {
10
10
  this.initElement(document, 'toggle', ['click']);
11
11
  this.initElement(document, 'click', ['click']);
12
- // this.initElement(document, 'hover', ['mouseover', 'mouseout');
13
- // this.initElement(document, 'mouseover', ['mouseover');
14
- // this.initElement(document, 'mouseout', ['mouseout');
12
+ this.initElement(document, 'hover', ['mouseover', 'mouseout']);
13
+ this.initElement(document, 'mouseover', ['mouseover']);
14
+ this.initElement(document, 'mouseout', ['mouseout']);
15
15
  this.initElement(document, 'input', ['input']);
16
16
  this.initElement(document, 'change', ['change']);
17
17
  this.initElement(document, 'selected', ['click']);
@@ -29,8 +29,8 @@ const CoCreateEvents = {
29
29
  let debounce;
30
30
  element.addEventListener(eventName, function(event) {
31
31
  // ToDo: apply debounce
32
- clearTimeout(debounce);
33
- debounce = setTimeout(function() {
32
+ // clearTimeout(debounce);
33
+ // debounce = setTimeout(function() {
34
34
  const target = event.target.closest(`[${prefix}], [${prefix}-value]`);
35
35
  if (target) {
36
36
  let attribute = target.getAttribute('actions') || ""
@@ -52,7 +52,7 @@ const CoCreateEvents = {
52
52
  }
53
53
 
54
54
  }
55
- }, 500);
55
+ // }, 500);
56
56
 
57
57
  });
58
58
  });
@@ -205,39 +205,77 @@ const CoCreateEvents = {
205
205
  };
206
206
 
207
207
  const eventElements = new Map();
208
- const elementPrefix = new Map();
208
+ const eventKeys = new Map();
209
+
209
210
  function replaceKey(prefix, element, key, values) {
210
- // ToDo: improve updating key to prevent unwanted match by storing each attribute that originally had a key to know which attributes to update
211
+ let currentValue = key
212
+
211
213
  if (eventElements.has(element)) {
212
- key = eventElements.get(element).get(prefix);
214
+ currentValue = eventElements.get(element).get(key);
213
215
  }
214
- let oldValue = key
215
- let value = CoCreateEvents.__getNextValue(values, oldValue);
216
+ let value = CoCreateEvents.__getNextValue(values, currentValue);
217
+ eventKeys.set(key, value)
218
+ eventElements.set(element, eventKeys)
216
219
 
217
- const regex = new RegExp(key, "g");
218
220
  for (let attribute of element.attributes){
221
+ let newName = attribute.name;
222
+ let newValue = attribute.value;
219
223
  let attrName = attribute.name;
220
224
  let attrValue = attribute.value;
221
225
  let setAttr = false;
222
- if (attrValue.includes(key)){
223
- attrValue = attrValue.replace(regex, value);
224
- setAttr = true;
226
+ if (attrValue.includes(currentValue) || attribute.valueKeys && attribute.valueKeys[key]){
227
+ if (!attribute.originalValue)
228
+ attribute.originalValue = attrValue
229
+ if (!attribute.valueKeys)
230
+ attribute.valueKeys = {[key]: value}
231
+ else
232
+ attribute.valueKeys[key] = value;
233
+
234
+ newValue = attribute.originalValue
235
+ for (let valueKey of Object.keys(attribute.valueKeys)) {
236
+ const valueKeyRegex = new RegExp(valueKey, "g");
237
+ newValue = newValue.replace(valueKeyRegex, value);
238
+ }
239
+ setAttr = true;
225
240
  }
226
- if (attrName.includes(key)){
227
- element.removeAttribute(key);
228
- attrName = attrName.replace(regex, value);
229
- setAttr = true;
241
+ if (attrName.includes(currentValue) || attribute.nameKeys && attribute.nameKeys[key]){
242
+ if (!attribute.originalName)
243
+ attribute.originalName = attrName
244
+ if (!attribute.nameKeys)
245
+ attribute.nameKeys = {[key]: value}
246
+ else
247
+ attribute.nameKeys[key] = value;
248
+
249
+ newName = attribute.originalName
250
+ for (let nameKey of Object.keys(attribute.nameKeys)) {
251
+ const nameKeyRegex = new RegExp(nameKey, "g");
252
+ newName = newName.replace(nameKeyRegex, value);
253
+ }
254
+
255
+ element.removeAttribute(attrName);
256
+ setAttr = true;
257
+
230
258
  }
231
259
  if (setAttr)
232
- element.setAttribute(attrName, attrValue);
260
+ element.setAttribute(newName, newValue);
233
261
  }
234
262
  let html = element.innerHTML;
235
- if (html.indexOf(key) !== -1){
236
- html = html.replace(regex, value);
237
- element.innerHTML = html;
263
+ if (html.indexOf(currentValue) !== -1 || element.htmlKeys && element.htmlKeys[key]) {
264
+ if (!element.originalHtml)
265
+ element.originalHtml = html
266
+ if (!element.htmlKeys)
267
+ element.htmlKeys = {[key]: value}
268
+ else
269
+ element.htmlKeys[key] = value;
270
+
271
+ let newHTML = element.originalHTML
272
+ for (let elementKey of Object.keys(element.htmlKeys)) {
273
+ const htmlKeyRegex = new RegExp(elementKey, "g");
274
+ newHTML = newHTML.replace(htmlKeyRegex, value);
275
+ }
276
+
277
+ element.innerHTML = newHTML;
238
278
  }
239
- elementPrefix.set(prefix, value)
240
- eventElements.set(element, elementPrefix)
241
279
 
242
280
  }
243
281