@cocreate/events 1.4.1 → 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 +12 -0
- package/demo/index.html +5 -0
- package/package.json +1 -1
- package/src/index.js +130 -45
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,15 @@
|
|
|
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
|
+
|
|
1
13
|
## [1.4.1](https://github.com/CoCreate-app/CoCreate-events/compare/v1.4.0...v1.4.1) (2023-02-21)
|
|
2
14
|
|
|
3
15
|
|
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
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
|
|
@@ -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
62
|
// clearTimeout(debounce);
|
|
33
63
|
// debounce = setTimeout(function() {
|
|
34
|
-
const target = event.
|
|
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,8 +76,10 @@ 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
|
|
|
@@ -53,22 +87,15 @@ const CoCreateEvents = {
|
|
|
53
87
|
|
|
54
88
|
}
|
|
55
89
|
// }, 500);
|
|
56
|
-
|
|
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
|
-
}
|
|
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') {
|
|
@@ -206,6 +266,7 @@ const CoCreateEvents = {
|
|
|
206
266
|
|
|
207
267
|
const eventElements = new Map();
|
|
208
268
|
const eventKeys = new Map();
|
|
269
|
+
const elementAttributes = new Map();
|
|
209
270
|
|
|
210
271
|
function replaceKey(prefix, element, key, values) {
|
|
211
272
|
let currentValue = key
|
|
@@ -217,45 +278,67 @@ function replaceKey(prefix, element, key, values) {
|
|
|
217
278
|
eventKeys.set(key, value)
|
|
218
279
|
eventElements.set(element, eventKeys)
|
|
219
280
|
|
|
281
|
+
let attributes = elementAttributes.get(element)
|
|
282
|
+
if (!attributes) {
|
|
283
|
+
attributes = {}
|
|
284
|
+
elementAttributes.set(element, attributes)
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
|
|
220
288
|
for (let attribute of element.attributes){
|
|
221
289
|
let newName = attribute.name;
|
|
222
290
|
let newValue = attribute.value;
|
|
223
291
|
let attrName = attribute.name;
|
|
224
292
|
let attrValue = attribute.value;
|
|
225
293
|
let setAttr = false;
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
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}
|
|
231
304
|
else
|
|
232
|
-
|
|
305
|
+
attributes[attrName].valueKeys[key] = value;
|
|
233
306
|
|
|
234
|
-
newValue =
|
|
235
|
-
for (let valueKey of Object.keys(
|
|
307
|
+
newValue = attributes[attrName].originalValue
|
|
308
|
+
for (let valueKey of Object.keys(attributes[attrName].valueKeys)) {
|
|
236
309
|
const valueKeyRegex = new RegExp(valueKey, "g");
|
|
237
310
|
newValue = newValue.replace(valueKeyRegex, value);
|
|
238
311
|
}
|
|
239
312
|
setAttr = true;
|
|
240
313
|
}
|
|
241
|
-
if (attrName.includes(currentValue) ||
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
else
|
|
247
|
-
attribute.nameKeys[key] = value;
|
|
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
|
|
248
319
|
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
const nameKeyRegex = new RegExp(nameKey, "g");
|
|
252
|
-
newName = newName.replace(nameKeyRegex, value);
|
|
253
|
-
}
|
|
320
|
+
// if (!attributes[attrName].nameKeys)
|
|
321
|
+
// attributes[attrName].nameKeys = {[key]: value}
|
|
254
322
|
|
|
255
|
-
|
|
256
|
-
|
|
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]
|
|
257
330
|
|
|
258
|
-
|
|
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
|
+
// }
|
|
259
342
|
if (setAttr)
|
|
260
343
|
element.setAttribute(newName, newValue);
|
|
261
344
|
}
|
|
@@ -269,12 +352,14 @@ function replaceKey(prefix, element, key, values) {
|
|
|
269
352
|
element.htmlKeys[key] = value;
|
|
270
353
|
|
|
271
354
|
let newHTML = element.originalHTML
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
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
|
+
}
|
|
276
360
|
|
|
277
|
-
|
|
361
|
+
element.innerHTML = newHTML;
|
|
362
|
+
}
|
|
278
363
|
}
|
|
279
364
|
|
|
280
365
|
}
|