@cocreate/events 1.4.0 → 1.5.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,22 @@
1
+ # [1.5.0](https://github.com/CoCreate-app/CoCreate-events/compare/v1.4.1...v1.5.0) (2023-03-15)
2
+
3
+
4
+ ### Bug Fixes
5
+
6
+ * add events per element and observer to detect newely added elements ([dcbaa38](https://github.com/CoCreate-app/CoCreate-events/commit/dcbaa38a09fa75ddcf3c969c708fa1dbcf2e3ce4))
7
+
8
+
9
+ ### Features
10
+
11
+ * attribute for targeting parent and sibling elements ([842be58](https://github.com/CoCreate-app/CoCreate-events/commit/842be58541401574cd4d093d08468abe39decf9b))
12
+
13
+ ## [1.4.1](https://github.com/CoCreate-app/CoCreate-events/compare/v1.4.0...v1.4.1) (2023-02-21)
14
+
15
+
16
+ ### Bug Fixes
17
+
18
+ * improved key usage ([a92e1f2](https://github.com/CoCreate-app/CoCreate-events/commit/a92e1f25d7b4c83dfa0beb9a7526c9ca3dc184fe))
19
+
1
20
  # [1.4.0](https://github.com/CoCreate-app/CoCreate-events/compare/v1.3.0...v1.4.0) (2023-02-19)
2
21
 
3
22
 
package/demo/index.html CHANGED
@@ -71,6 +71,11 @@
71
71
  <input input-target="#testing" input-value="" input-key="ty">
72
72
  <span id="testing" class="{{ty}}">other content</span>
73
73
  </div>
74
+
75
+ <span id="keyname" clone-data='{"attributes": {"key": "{{keyname}}", "value": ""}}' {{keyname}}="test" style="margin-right:10px; padding:5px">
76
+ <svg class="svg" width="12" height="12" viewBox="0 0 12 12" xmlns="http://www.w3.org/2000/svg"><path d="M5 5V0h1v5h5v1H6v5H5V6H0V5h5z" fill-rule="nonzero" fill-opacity="1" fill="#fff" stroke="none"></path></svg>
77
+ </span>
78
+ <input placeholder="Search all attributes" input-value input-key="keyname" input-target="#keyname">
74
79
 
75
80
  <!--<script src="../dist/CoCreate-events.js"></script>-->
76
81
  <script src="../../../CoCreateJS/dist/CoCreate.js"></script>
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cocreate/events",
3
- "version": "1.4.0",
3
+ "version": "1.5.0",
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
@@ -1,5 +1,7 @@
1
1
  import {queryDocumentSelectorAll} from '@cocreate/utils';
2
2
  import action from '@cocreate/actions';
3
+ import observer from '@cocreate/observer';
4
+
3
5
 
4
6
  const CoCreateEvents = {
5
7
  // ToDo update to support config, ability to add custom prefix, for loop each defualt and custom prefix to support action
@@ -9,9 +11,9 @@ const CoCreateEvents = {
9
11
  else {
10
12
  this.initElement(document, 'toggle', ['click']);
11
13
  this.initElement(document, 'click', ['click']);
12
- // this.initElement(document, 'hover', ['mouseover', 'mouseout');
13
- // this.initElement(document, 'mouseover', ['mouseover');
14
- // this.initElement(document, 'mouseout', ['mouseout');
14
+ this.initElement(document, 'hover', ['mouseover', 'mouseout']);
15
+ this.initElement(document, 'mouseover', ['mouseover']);
16
+ this.initElement(document, 'mouseout', ['mouseout']);
15
17
  this.initElement(document, 'input', ['input']);
16
18
  this.initElement(document, 'change', ['change']);
17
19
  this.initElement(document, 'selected', ['click']);
@@ -24,15 +26,45 @@ const CoCreateEvents = {
24
26
 
25
27
  __initElementEvent: function(element, prefix, events) {
26
28
  const self = this;
27
-
29
+
30
+ let selector = `[${prefix}], [${prefix}-value]`
31
+ let elements = document.querySelectorAll(selector)
32
+ for (const el of elements) {
33
+ this.addEvent(el, prefix, events)
34
+ }
35
+
36
+ action.init({
37
+ name: prefix,
38
+ endEvent: `${prefix}End`,
39
+ callback: (btn, data) => {
40
+ this.__updateElements(btn, prefix)
41
+ }
42
+ });
43
+
44
+ observer.init({
45
+ name: 'CoCreateEventInit',
46
+ observe: ['addedNodes'],
47
+ target: `[${prefix}], [${prefix}-value]`,
48
+ callback: function(mutation) {
49
+ self.addEvent(mutation.target, prefix, events);
50
+ }
51
+ });
52
+
53
+
54
+ },
55
+
56
+ addEvent: function(element, prefix, events) {
57
+ const self = this;
28
58
  events.forEach((eventName) => {
29
59
  let debounce;
30
60
  element.addEventListener(eventName, function(event) {
31
61
  // ToDo: apply debounce
32
- clearTimeout(debounce);
33
- debounce = setTimeout(function() {
34
- const target = event.target.closest(`[${prefix}], [${prefix}-value]`);
62
+ // clearTimeout(debounce);
63
+ // debounce = setTimeout(function() {
64
+ const target = event.currentTarget
35
65
  if (target) {
66
+ if (target.id == "testtemplate1" || target.id == "testtemplate2")
67
+ console.log(target.id)
36
68
  let attribute = target.getAttribute('actions') || ""
37
69
  if (attribute.includes(prefix))
38
70
  return;
@@ -44,31 +76,26 @@ const CoCreateEvents = {
44
76
  if (parentElement) {
45
77
  do {
46
78
  parentElement = parentElement.closest(`[${prefix}], [${prefix}-value]`)
47
- if (parentElement)
79
+ if (parentElement) {
48
80
  self.__updateElements(parentElement, prefix);
81
+ parentElement = parentElement.parentElement
82
+ }
49
83
  }
50
84
  while (parentElement)
51
85
 
52
86
  }
53
87
 
54
88
  }
55
- }, 500);
56
-
89
+ // }, 500);
90
+
57
91
  });
92
+
58
93
  });
59
-
60
- action.init({
61
- name: prefix,
62
- endEvent: `${prefix}End`,
63
- callback: (btn, data) => {
64
- this.__updateElements(btn, prefix)
65
- }
66
- });
67
-
68
94
  },
69
-
95
+
70
96
  __updateElements: function(element, prefix) {
71
97
  const self = this;
98
+ // ToDo: support empty value when prefix-attribute defined, add and remove the attribute
72
99
  let targetValue = element.getAttribute(`${prefix}-value`) || element.getAttribute(prefix);
73
100
  if (!targetValue && element.value)
74
101
  targetValue = element.getValue()
@@ -82,6 +109,10 @@ const CoCreateEvents = {
82
109
  let targetAttribute = element.getAttribute(`${prefix}-attribute`) || 'class';
83
110
  let targetSelector = element.getAttribute(`${prefix}-target`);
84
111
  let targetClosest = element.getAttribute(`${prefix}-closest`);
112
+ let targetParent = element.getAttribute(`${prefix}-parent`);
113
+ let targetNext = element.getAttribute(`${prefix}-next`);
114
+ let targetPrevious = element.getAttribute(`${prefix}-previous`);
115
+
85
116
  let targetKey = element.getAttribute(`${prefix}-key`);
86
117
 
87
118
  let targetGroup = element.getAttribute(`${prefix}-group`);
@@ -97,6 +128,9 @@ const CoCreateEvents = {
97
128
 
98
129
  let groupTarget = el.getAttribute(`${prefix}-target`);
99
130
  let groupClosest = el.getAttribute(`${prefix}-closest`);
131
+ let groupParent = el.getAttribute(`${prefix}-parent`);
132
+ let groupNext = el.getAttribute(`${prefix}-next`);
133
+ let groupPrevious = el.getAttribute(`${prefix}-previous`);
100
134
  let groupAttribute = el.getAttribute(`${prefix}-attribute`) || 'class';
101
135
  let groupKey = el.getAttribute(`${prefix}-key`)
102
136
 
@@ -111,6 +145,19 @@ const CoCreateEvents = {
111
145
  if (element)
112
146
  self.setValue(prefix, element, groupAttribute, groupValues, groupKey, 'deactivate');
113
147
  }
148
+ else if (groupParent)
149
+ el.parentElement.querySelectorAll(groupParent).forEach((el) =>
150
+ self.setValue(prefix, el, groupAttribute, groupValues, groupKey, 'deactivate')
151
+ );
152
+ else if (groupNext)
153
+ el.nextElementSibling.querySelectorAll(groupNext).forEach((el) =>
154
+ self.setValue(prefix, el, groupAttribute, groupValues, groupKey, 'deactivate')
155
+ );
156
+ else if (groupPrevious)
157
+ el.previousElementSibling.querySelectorAll(groupPrevious).forEach((el) =>
158
+ self.setValue(prefix, el, groupAttribute, groupValues, groupKey, 'deactivate')
159
+ );
160
+
114
161
  });
115
162
  }
116
163
 
@@ -123,7 +170,20 @@ const CoCreateEvents = {
123
170
  if (/{{\s*([\w\W]+)\s*}}/g.test(targetSelector)) return;
124
171
  targetElements = queryDocumentSelectorAll(targetSelector);
125
172
  targetElements.forEach((el) => self.setValue(prefix, el, targetAttribute, values, targetKey));
126
- } else
173
+ }
174
+ else if (targetParent)
175
+ element.parentElement.querySelectorAll(targetParent).forEach((el) =>
176
+ self.setValue(prefix, el, targetAttribute, values, targetKey)
177
+ );
178
+ else if (targetNext)
179
+ el.nextElementSibling.querySelectorAll(targetNext).forEach((el) =>
180
+ self.setValue(prefix, el, targetAttribute, values, targetKey)
181
+ );
182
+ else if (targetPrevious)
183
+ el.previousElementSibling.querySelectorAll(targetPrevious).forEach((el) =>
184
+ self.setValue(prefix, el, targetAttribute, values, targetKey)
185
+ );
186
+ else
127
187
  self.setValue(prefix, element, targetAttribute, values, targetKey);
128
188
 
129
189
  document.dispatchEvent(new CustomEvent(`${prefix}End`, {
@@ -174,14 +234,14 @@ const CoCreateEvents = {
174
234
  newValue = ""
175
235
 
176
236
  if (attrName === 'class') {
177
- if (oldValue != '') {
237
+ if (oldValue) {
178
238
  element.classList.remove(oldValue);
179
239
  if (values.length === 1) {
180
240
  return;
181
241
  }
182
242
  }
183
243
 
184
- if (newValue != '') {
244
+ if (newValue) {
185
245
  element.classList.add(newValue);
186
246
  }
187
247
  } else if (attrName === 'value') {
@@ -205,39 +265,102 @@ const CoCreateEvents = {
205
265
  };
206
266
 
207
267
  const eventElements = new Map();
208
- const elementPrefix = new Map();
268
+ const eventKeys = new Map();
269
+ const elementAttributes = new Map();
270
+
209
271
  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
272
+ let currentValue = key
273
+
211
274
  if (eventElements.has(element)) {
212
- key = eventElements.get(element).get(prefix);
275
+ currentValue = eventElements.get(element).get(key);
276
+ }
277
+ let value = CoCreateEvents.__getNextValue(values, currentValue);
278
+ eventKeys.set(key, value)
279
+ eventElements.set(element, eventKeys)
280
+
281
+ let attributes = elementAttributes.get(element)
282
+ if (!attributes) {
283
+ attributes = {}
284
+ elementAttributes.set(element, attributes)
213
285
  }
214
- let oldValue = key
215
- let value = CoCreateEvents.__getNextValue(values, oldValue);
216
286
 
217
- const regex = new RegExp(key, "g");
287
+
218
288
  for (let attribute of element.attributes){
289
+ let newName = attribute.name;
290
+ let newValue = attribute.value;
219
291
  let attrName = attribute.name;
220
292
  let attrValue = attribute.value;
221
293
  let setAttr = false;
222
- if (attrValue.includes(key)){
223
- attrValue = attrValue.replace(regex, value);
224
- setAttr = true;
225
- }
226
- if (attrName.includes(key)){
227
- element.removeAttribute(key);
228
- attrName = attrName.replace(regex, value);
229
- setAttr = true;
294
+
295
+ if(attrName === 'clone-data')
296
+ console.log('clone-data')
297
+ if (attrValue.includes(currentValue) || attributes[attrName] && attributes[attrName].valueKeys && attributes[attrName].valueKeys[key]){
298
+ if (!attributes[attrName])
299
+ attributes[attrName] = { originalValue: attrValue }
300
+ else if (!attributes[attrName].originalValue)
301
+ attributes[attrName].originalValue = attrValue
302
+ if (!attributes[attrName].valueKeys)
303
+ attributes[attrName].valueKeys = {[key]: value}
304
+ else
305
+ attributes[attrName].valueKeys[key] = value;
306
+
307
+ newValue = attributes[attrName].originalValue
308
+ for (let valueKey of Object.keys(attributes[attrName].valueKeys)) {
309
+ const valueKeyRegex = new RegExp(valueKey, "g");
310
+ newValue = newValue.replace(valueKeyRegex, value);
311
+ }
312
+ setAttr = true;
230
313
  }
314
+ // if (attrName.includes(currentValue) || attributes[attrName] && attributes[attrName].nameKeys && attributes[attrName].nameKeys[key]){
315
+ // if (!attributes[attrName])
316
+ // attributes[attrName] = { originalName: attrName }
317
+ // else if (!attributes[attrName].originalName)
318
+ // attributes[attrName].originalName = attrName
319
+
320
+ // if (!attributes[attrName].nameKeys)
321
+ // attributes[attrName].nameKeys = {[key]: value}
322
+
323
+ // newName = attributes[attrName].originalName
324
+ // for (let nameKey of Object.keys(attributes[attrName].nameKeys)) {
325
+ // let r
326
+ // if (attrName === nameKey)
327
+ // r = nameKey
328
+ // else
329
+ // r = attributes[attrName].nameKeys[nameKey]
330
+
331
+ // const nameKeyRegex = new RegExp(r, "g");
332
+ // newName = newName.replace(nameKeyRegex, value);
333
+ // }
334
+
335
+ // attributes[attrName].nameKeys[key] = value;
336
+
337
+
338
+ // element.removeAttribute(attrName);
339
+ // setAttr = true;
340
+
341
+ // }
231
342
  if (setAttr)
232
- element.setAttribute(attrName, attrValue);
343
+ element.setAttribute(newName, newValue);
233
344
  }
234
345
  let html = element.innerHTML;
235
- if (html.indexOf(key) !== -1){
236
- html = html.replace(regex, value);
237
- element.innerHTML = html;
346
+ if (html.indexOf(currentValue) !== -1 || element.htmlKeys && element.htmlKeys[key]) {
347
+ if (!element.originalHtml)
348
+ element.originalHtml = html
349
+ if (!element.htmlKeys)
350
+ element.htmlKeys = {[key]: value}
351
+ else
352
+ element.htmlKeys[key] = value;
353
+
354
+ let newHTML = element.originalHTML
355
+ if (newHTML) {
356
+ for (let elementKey of Object.keys(element.htmlKeys)) {
357
+ const htmlKeyRegex = new RegExp(elementKey, "g");
358
+ newHTML = newHTML.replace(htmlKeyRegex, value);
359
+ }
360
+
361
+ element.innerHTML = newHTML;
362
+ }
238
363
  }
239
- elementPrefix.set(prefix, value)
240
- eventElements.set(element, elementPrefix)
241
364
 
242
365
  }
243
366