@cocreate/events 1.3.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,17 @@
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
+
8
+ # [1.4.0](https://github.com/CoCreate-app/CoCreate-events/compare/v1.3.0...v1.4.0) (2023-02-19)
9
+
10
+
11
+ ### Features
12
+
13
+ * debonce ([43748c4](https://github.com/CoCreate-app/CoCreate-events/commit/43748c45b9e76f58aaf1ad11d16964422c028bbc))
14
+
1
15
  # [1.3.0](https://github.com/CoCreate-app/CoCreate-events/compare/v1.2.0...v1.3.0) (2023-02-11)
2
16
 
3
17
 
package/demo/index.html CHANGED
@@ -62,7 +62,13 @@
62
62
  other content
63
63
  </div>
64
64
  <!-- defining key example -->
65
- <div click-target="#testing" click-value="hello, bye" click-key="ty">
65
+ <!-- <div click-target="#testing" click-value="hello, bye" click-key="ty">
66
+ <span id="testing" class="{{ty}}">other content</span>
67
+ </div> -->
68
+
69
+ <!-- defining key example -->
70
+ <div >
71
+ <input input-target="#testing" input-value="" input-key="ty">
66
72
  <span id="testing" class="{{ty}}">other content</span>
67
73
  </div>
68
74
 
@@ -0,0 +1,18 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>Value | CoCreateJS</title>
5
+
6
+ <!-- CoCreate Favicon -->
7
+ <link rel="icon" href="https://cdn.cocreate.app/favicon.ico" type="image/ico" sizes="16x16">
8
+ </head>
9
+ <body>
10
+ <div click-target="#testing" click-value="hello, bye" click-key="ty">
11
+ <span id="testing" class="{{ty}}">other content</span>
12
+ </div>
13
+
14
+ <script src="../../../CoCreateJS/dist/CoCreate.js"></script>
15
+ <!-- <script src="https://cdn.cocreate.app/latest/CoCreate.min.js" ></script> -->
16
+
17
+ </body>
18
+ </html>
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cocreate/events",
3
- "version": "1.3.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
@@ -2,16 +2,16 @@ import {queryDocumentSelectorAll} from '@cocreate/utils';
2
2
  import action from '@cocreate/actions';
3
3
 
4
4
  const CoCreateEvents = {
5
- // ToDo update to support config, ability to add custom prefix, for loop each deafualt and custom prefix to support action
5
+ // ToDo update to support config, ability to add custom prefix, for loop each defualt and custom prefix to support action
6
6
  init: function(prefix, events) {
7
7
  if (prefix && events)
8
8
  this.initElement(document, prefix, events);
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']);
@@ -26,28 +26,34 @@ const CoCreateEvents = {
26
26
  const self = this;
27
27
 
28
28
  events.forEach((eventName) => {
29
+ let debounce;
29
30
  element.addEventListener(eventName, function(event) {
30
- const target = event.target.closest(`[${prefix}], [${prefix}-value]`);
31
- if (target) {
32
- let attribute = target.getAttribute('actions') || ""
33
- if (attribute.includes(prefix))
34
- return;
35
- // if (target.closest(`[actions*="${prefix}"]`))
36
- // return;
37
- self.__updateElements(target, prefix);
38
-
39
- let parentElement = target.parentElement;
40
- if (parentElement) {
41
- do {
42
- parentElement = parentElement.closest(`[${prefix}], [${prefix}-value]`)
43
- if (parentElement)
44
- self.__updateElements(parentElement, prefix);
31
+ // ToDo: apply debounce
32
+ // clearTimeout(debounce);
33
+ // debounce = setTimeout(function() {
34
+ const target = event.target.closest(`[${prefix}], [${prefix}-value]`);
35
+ if (target) {
36
+ let attribute = target.getAttribute('actions') || ""
37
+ if (attribute.includes(prefix))
38
+ return;
39
+ // if (target.closest(`[actions*="${prefix}"]`))
40
+ // return;
41
+ self.__updateElements(target, prefix);
42
+
43
+ let parentElement = target.parentElement;
44
+ if (parentElement) {
45
+ do {
46
+ parentElement = parentElement.closest(`[${prefix}], [${prefix}-value]`)
47
+ if (parentElement)
48
+ self.__updateElements(parentElement, prefix);
49
+ }
50
+ while (parentElement)
51
+
45
52
  }
46
- while (parentElement)
47
-
53
+
48
54
  }
55
+ // }, 500);
49
56
 
50
- }
51
57
  });
52
58
  });
53
59
 
@@ -64,6 +70,8 @@ const CoCreateEvents = {
64
70
  __updateElements: function(element, prefix) {
65
71
  const self = this;
66
72
  let targetValue = element.getAttribute(`${prefix}-value`) || element.getAttribute(prefix);
73
+ if (!targetValue && element.value)
74
+ targetValue = element.getValue()
67
75
  if (!targetValue) return
68
76
 
69
77
  let values = targetValue.split(',');
@@ -197,38 +205,77 @@ const CoCreateEvents = {
197
205
  };
198
206
 
199
207
  const eventElements = new Map();
200
- const elementPrefix = new Map();
201
- function replaceKey(prefix, element, key, values){
208
+ const eventKeys = new Map();
209
+
210
+ function replaceKey(prefix, element, key, values) {
211
+ let currentValue = key
212
+
202
213
  if (eventElements.has(element)) {
203
- key = eventElements.get(element).get(prefix);
214
+ currentValue = eventElements.get(element).get(key);
204
215
  }
205
- let oldValue = key
206
- let value = CoCreateEvents.__getNextValue(values, oldValue);
216
+ let value = CoCreateEvents.__getNextValue(values, currentValue);
217
+ eventKeys.set(key, value)
218
+ eventElements.set(element, eventKeys)
207
219
 
208
- const regex = new RegExp(key, "g");
209
220
  for (let attribute of element.attributes){
221
+ let newName = attribute.name;
222
+ let newValue = attribute.value;
210
223
  let attrName = attribute.name;
211
224
  let attrValue = attribute.value;
212
225
  let setAttr = false;
213
- if (attrValue.includes(key)){
214
- attrValue = attrValue.replace(regex, value);
215
- 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;
216
240
  }
217
- if (attrName.includes(key)){
218
- element.removeAttribute(key);
219
- attrName = attrName.replace(regex, value);
220
- 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
+
221
258
  }
222
259
  if (setAttr)
223
- element.setAttribute(attrName, attrValue);
260
+ element.setAttribute(newName, newValue);
224
261
  }
225
262
  let html = element.innerHTML;
226
- if (html.indexOf(key) !== -1){
227
- html = html.replace(regex, value);
228
- 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;
229
278
  }
230
- elementPrefix.set(prefix, value)
231
- eventElements.set(element, elementPrefix)
232
279
 
233
280
  }
234
281