@cocreate/events 1.2.0 → 1.3.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 +7 -0
- package/demo/index.html +11 -2
- package/demo/selected.html +54 -0
- package/package.json +1 -1
- package/src/index.js +112 -71
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,10 @@
|
|
|
1
|
+
# [1.3.0](https://github.com/CoCreate-app/CoCreate-events/compare/v1.2.0...v1.3.0) (2023-02-11)
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
### Features
|
|
5
|
+
|
|
6
|
+
* define and replace a key ([f321bba](https://github.com/CoCreate-app/CoCreate-events/commit/f321bbad58ed361b5d7185cf5fe33e6c4ed79d2b))
|
|
7
|
+
|
|
1
8
|
# [1.2.0](https://github.com/CoCreate-app/CoCreate-events/compare/v1.1.0...v1.2.0) (2023-02-02)
|
|
2
9
|
|
|
3
10
|
|
package/demo/index.html
CHANGED
|
@@ -57,9 +57,18 @@
|
|
|
57
57
|
some content
|
|
58
58
|
</div>
|
|
59
59
|
|
|
60
|
+
<!-- custom event example -->
|
|
61
|
+
<div test-value="active" test-events="click" event>
|
|
62
|
+
other content
|
|
63
|
+
</div>
|
|
64
|
+
<!-- defining key example -->
|
|
65
|
+
<div click-target="#testing" click-value="hello, bye" click-key="ty">
|
|
66
|
+
<span id="testing" class="{{ty}}">other content</span>
|
|
67
|
+
</div>
|
|
60
68
|
|
|
61
|
-
|
|
62
69
|
<!--<script src="../dist/CoCreate-events.js"></script>-->
|
|
63
|
-
|
|
70
|
+
<script src="../../../CoCreateJS/dist/CoCreate.js"></script>
|
|
71
|
+
<!-- <script src="https://cdn.cocreate.app/latest/CoCreate.min.js"></script> -->
|
|
72
|
+
|
|
64
73
|
</body>
|
|
65
74
|
</html>
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
<!DOCTYPE html>
|
|
2
|
+
<html>
|
|
3
|
+
<head>
|
|
4
|
+
<title>Selected | CoCreateJS</title>
|
|
5
|
+
|
|
6
|
+
<!-- CoCreate Favicon -->
|
|
7
|
+
<link rel="icon" href="https://cdn.cocreate.app/favicon.ico" type="image/ico" sizes="16x16">
|
|
8
|
+
<!-- Custom CSS -->
|
|
9
|
+
<style type="text/css">
|
|
10
|
+
.test{background:red}
|
|
11
|
+
.green{background:green}
|
|
12
|
+
.yellow{background:yellow}
|
|
13
|
+
.blue{background:blue}
|
|
14
|
+
.red{background:red}
|
|
15
|
+
.active{
|
|
16
|
+
background-color:blue;
|
|
17
|
+
|
|
18
|
+
}
|
|
19
|
+
.hfhfh[selected]{}
|
|
20
|
+
|
|
21
|
+
</style>
|
|
22
|
+
</head>
|
|
23
|
+
<body>
|
|
24
|
+
|
|
25
|
+
<!--<article>-->
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
<div id="yyy" selected-target=".tester" selected-value="active," selected-attribute="data-test" selected-group="uno" data-test="" class="test">1 tab</div>
|
|
31
|
+
<div selected-value="active" selected-group="uno" class="test">2 tab</div>
|
|
32
|
+
<div selected-value="active" selected-group="uno" class="test">3 tab</div>
|
|
33
|
+
|
|
34
|
+
<hr/>
|
|
35
|
+
|
|
36
|
+
<div selected-value="yellow" selected-group="2" class="green">1 tab</div>
|
|
37
|
+
<div selected-value="blue" selected-group="2" class="green">2 tab</div>
|
|
38
|
+
<div selected-value="red" selected-group="2" class="green">3 tab</div>
|
|
39
|
+
|
|
40
|
+
<hr/>
|
|
41
|
+
<div class="tester">
|
|
42
|
+
<div selected-value="active" selected-closest="div.tester" class="test">4 tab</div>
|
|
43
|
+
<div selected-value="active" class="test">4 tab</div>
|
|
44
|
+
|
|
45
|
+
<div selected-value="color:black; background:grey" selected-attribute="style" style="">color tab</div>
|
|
46
|
+
|
|
47
|
+
<div id="yyyy" data-test="" class="test">1 tab</div>
|
|
48
|
+
</div>
|
|
49
|
+
|
|
50
|
+
<!-- <script src="../dist/CoCreate-selected.js"></script> -->
|
|
51
|
+
<script src="../../../CoCreateJS/dist/CoCreate.js"></script>
|
|
52
|
+
<!-- <script src="https://cdn.cocreate.app/latest/CoCreate.min.js"></script> -->
|
|
53
|
+
</body>
|
|
54
|
+
</html>
|
package/package.json
CHANGED
package/src/index.js
CHANGED
|
@@ -3,34 +3,30 @@ import action from '@cocreate/actions';
|
|
|
3
3
|
|
|
4
4
|
const CoCreateEvents = {
|
|
5
5
|
// ToDo update to support config, ability to add custom prefix, for loop each deafualt and custom prefix to support action
|
|
6
|
-
init: function() {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
6
|
+
init: function(prefix, events) {
|
|
7
|
+
if (prefix && events)
|
|
8
|
+
this.initElement(document, prefix, events);
|
|
9
|
+
else {
|
|
10
|
+
this.initElement(document, 'toggle', ['click']);
|
|
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');
|
|
15
|
+
this.initElement(document, 'input', ['input']);
|
|
16
|
+
this.initElement(document, 'change', ['change']);
|
|
17
|
+
this.initElement(document, 'selected', ['click']);
|
|
18
|
+
}
|
|
14
19
|
},
|
|
15
20
|
|
|
16
|
-
initElement: function(
|
|
17
|
-
this.__initElementEvent(
|
|
21
|
+
initElement: function(element, prefix, events) {
|
|
22
|
+
this.__initElementEvent(element || document, prefix, events);
|
|
18
23
|
},
|
|
19
24
|
|
|
20
|
-
__initElementEvent: function(
|
|
25
|
+
__initElementEvent: function(element, prefix, events) {
|
|
21
26
|
const self = this;
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
if (prefix === 'click') eventNames = ['click'];
|
|
26
|
-
if (prefix === 'hover') eventNames = ['mouseover', 'mouseout'];
|
|
27
|
-
if (prefix === 'mouseover') eventNames = ['mouseover'];
|
|
28
|
-
if (prefix === 'mouseout') eventNames = ['mouseout'];
|
|
29
|
-
if (prefix === 'input') eventNames = ['input'];
|
|
30
|
-
if (prefix === 'change') eventNames = ['change'];
|
|
31
|
-
|
|
32
|
-
eventNames.forEach((event_name) => {
|
|
33
|
-
mainContainer.addEventListener(event_name, function(event) {
|
|
27
|
+
|
|
28
|
+
events.forEach((eventName) => {
|
|
29
|
+
element.addEventListener(eventName, function(event) {
|
|
34
30
|
const target = event.target.closest(`[${prefix}], [${prefix}-value]`);
|
|
35
31
|
if (target) {
|
|
36
32
|
let attribute = target.getAttribute('actions') || ""
|
|
@@ -75,10 +71,11 @@ const CoCreateEvents = {
|
|
|
75
71
|
return;
|
|
76
72
|
}
|
|
77
73
|
|
|
78
|
-
|
|
79
74
|
let targetAttribute = element.getAttribute(`${prefix}-attribute`) || 'class';
|
|
80
75
|
let targetSelector = element.getAttribute(`${prefix}-target`);
|
|
81
76
|
let targetClosest = element.getAttribute(`${prefix}-closest`);
|
|
77
|
+
let targetKey = element.getAttribute(`${prefix}-key`);
|
|
78
|
+
|
|
82
79
|
let targetGroup = element.getAttribute(`${prefix}-group`);
|
|
83
80
|
if (targetGroup) {
|
|
84
81
|
document.querySelectorAll(`[${prefix}-group="${targetGroup}"]`).forEach((el) => {
|
|
@@ -87,22 +84,24 @@ const CoCreateEvents = {
|
|
|
87
84
|
if (!groupValues || groupValues.length == 0) {
|
|
88
85
|
return;
|
|
89
86
|
}
|
|
87
|
+
|
|
90
88
|
groupValues = groupValues.map(x => x.trim());
|
|
91
89
|
|
|
92
90
|
let groupTarget = el.getAttribute(`${prefix}-target`);
|
|
93
91
|
let groupClosest = el.getAttribute(`${prefix}-closest`);
|
|
94
92
|
let groupAttribute = el.getAttribute(`${prefix}-attribute`) || 'class';
|
|
93
|
+
let groupKey = el.getAttribute(`${prefix}-key`)
|
|
95
94
|
|
|
96
95
|
// el.removeAttribute(prefix)
|
|
97
|
-
self.setValue(el, groupAttribute,
|
|
96
|
+
self.setValue(prefix, el, groupAttribute, groupValues, groupKey, 'deactivate')
|
|
98
97
|
if (groupTarget)
|
|
99
98
|
document.querySelectorAll(groupTarget).forEach((el) =>
|
|
100
|
-
self.setValue(el, groupAttribute,
|
|
99
|
+
self.setValue(prefix, el, groupAttribute, groupValues, groupKey, 'deactivate')
|
|
101
100
|
);
|
|
102
101
|
else if (groupClosest) {
|
|
103
102
|
let element = el.closest(groupClosest)
|
|
104
103
|
if (element)
|
|
105
|
-
self.setValue(element, groupAttribute,
|
|
104
|
+
self.setValue(prefix, element, groupAttribute, groupValues, groupKey, 'deactivate');
|
|
106
105
|
}
|
|
107
106
|
});
|
|
108
107
|
}
|
|
@@ -115,9 +114,9 @@ const CoCreateEvents = {
|
|
|
115
114
|
if (targetSelector) {
|
|
116
115
|
if (/{{\s*([\w\W]+)\s*}}/g.test(targetSelector)) return;
|
|
117
116
|
targetElements = queryDocumentSelectorAll(targetSelector);
|
|
118
|
-
targetElements.forEach((el) => self.setValue(el, targetAttribute, values));
|
|
117
|
+
targetElements.forEach((el) => self.setValue(prefix, el, targetAttribute, values, targetKey));
|
|
119
118
|
} else
|
|
120
|
-
self.setValue(element, targetAttribute, values);
|
|
119
|
+
self.setValue(prefix, element, targetAttribute, values, targetKey);
|
|
121
120
|
|
|
122
121
|
document.dispatchEvent(new CustomEvent(`${prefix}End`, {
|
|
123
122
|
detail: {}
|
|
@@ -125,56 +124,63 @@ const CoCreateEvents = {
|
|
|
125
124
|
|
|
126
125
|
},
|
|
127
126
|
|
|
128
|
-
setValue: function(element, attrName, values, deactivate) {
|
|
127
|
+
setValue: function(prefix, element, attrName, values, key, deactivate) {
|
|
129
128
|
let attrValues, oldValue;
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
attrValues = element.getAttribute(attrName).split(';').map(x => x.trim());
|
|
135
|
-
attrValues = attrValues.map(function(item, index){
|
|
136
|
-
return item.replace(' ','');
|
|
137
|
-
});
|
|
138
|
-
oldValue = values.filter(x => attrValues.includes(x))[0] || '';
|
|
139
|
-
}
|
|
140
|
-
let newValue = this.__getNextValue(values, oldValue);
|
|
141
|
-
|
|
142
|
-
if (oldValue) {
|
|
143
|
-
let [property, value] = oldValue.split(":");
|
|
144
|
-
element.style[property] = '';
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
if (newValue != '') {
|
|
148
|
-
let [property, value] = newValue.split(":");
|
|
149
|
-
element.style[property] = value;
|
|
150
|
-
}
|
|
129
|
+
if (key) {
|
|
130
|
+
key = `{{${key}}}`;
|
|
131
|
+
// let value = values[0]
|
|
132
|
+
replaceKey(prefix, element, key, values)
|
|
151
133
|
} else {
|
|
152
|
-
if (attrName === '
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
134
|
+
if (attrName === 'style') {
|
|
135
|
+
if (element.getAttribute(attrName)) {
|
|
136
|
+
attrValues = element.getAttribute(attrName).split(';').map(x => x.trim());
|
|
137
|
+
attrValues = attrValues.map(function(item, index){
|
|
138
|
+
return item.replace(' ','');
|
|
139
|
+
});
|
|
156
140
|
oldValue = values.filter(x => attrValues.includes(x))[0] || '';
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
if (attrName === 'class') {
|
|
164
|
-
if (oldValue != '') {
|
|
165
|
-
element.classList.remove(oldValue);
|
|
166
|
-
if (values.length === 1) {
|
|
167
|
-
return;
|
|
168
|
-
}
|
|
141
|
+
}
|
|
142
|
+
let newValue = this.__getNextValue(values, oldValue);
|
|
143
|
+
|
|
144
|
+
if (oldValue) {
|
|
145
|
+
let [property, value] = oldValue.split(":");
|
|
146
|
+
element.style[property] = '';
|
|
169
147
|
}
|
|
170
148
|
|
|
171
149
|
if (newValue != '') {
|
|
172
|
-
|
|
150
|
+
newValue = newValue.split(';').map(x => x.trim())
|
|
151
|
+
for (let style of newValue) {
|
|
152
|
+
let [property, value] = style.split(":");
|
|
153
|
+
element.style[property] = value;
|
|
154
|
+
}
|
|
173
155
|
}
|
|
174
|
-
} else if (attrName === 'value') {
|
|
175
|
-
element.setValue(newValue);
|
|
176
156
|
} else {
|
|
177
|
-
|
|
157
|
+
if (attrName === 'value') {
|
|
158
|
+
oldValue = element.getValue()
|
|
159
|
+
} else if (element.getAttribute(attrName)) {
|
|
160
|
+
attrValues = element.getAttribute(attrName).split(' ').map(x => x.trim());
|
|
161
|
+
oldValue = values.filter(x => attrValues.includes(x))[0] || '';
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
let newValue = this.__getNextValue(values, oldValue);
|
|
165
|
+
if (deactivate)
|
|
166
|
+
newValue = ""
|
|
167
|
+
|
|
168
|
+
if (attrName === 'class') {
|
|
169
|
+
if (oldValue != '') {
|
|
170
|
+
element.classList.remove(oldValue);
|
|
171
|
+
if (values.length === 1) {
|
|
172
|
+
return;
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
if (newValue != '') {
|
|
177
|
+
element.classList.add(newValue);
|
|
178
|
+
}
|
|
179
|
+
} else if (attrName === 'value') {
|
|
180
|
+
element.setValue(newValue);
|
|
181
|
+
} else {
|
|
182
|
+
element.setAttribute(attrName, newValue);
|
|
183
|
+
}
|
|
178
184
|
}
|
|
179
185
|
}
|
|
180
186
|
},
|
|
@@ -190,6 +196,41 @@ const CoCreateEvents = {
|
|
|
190
196
|
}
|
|
191
197
|
};
|
|
192
198
|
|
|
199
|
+
const eventElements = new Map();
|
|
200
|
+
const elementPrefix = new Map();
|
|
201
|
+
function replaceKey(prefix, element, key, values){
|
|
202
|
+
if (eventElements.has(element)) {
|
|
203
|
+
key = eventElements.get(element).get(prefix);
|
|
204
|
+
}
|
|
205
|
+
let oldValue = key
|
|
206
|
+
let value = CoCreateEvents.__getNextValue(values, oldValue);
|
|
207
|
+
|
|
208
|
+
const regex = new RegExp(key, "g");
|
|
209
|
+
for (let attribute of element.attributes){
|
|
210
|
+
let attrName = attribute.name;
|
|
211
|
+
let attrValue = attribute.value;
|
|
212
|
+
let setAttr = false;
|
|
213
|
+
if (attrValue.includes(key)){
|
|
214
|
+
attrValue = attrValue.replace(regex, value);
|
|
215
|
+
setAttr = true;
|
|
216
|
+
}
|
|
217
|
+
if (attrName.includes(key)){
|
|
218
|
+
element.removeAttribute(key);
|
|
219
|
+
attrName = attrName.replace(regex, value);
|
|
220
|
+
setAttr = true;
|
|
221
|
+
}
|
|
222
|
+
if (setAttr)
|
|
223
|
+
element.setAttribute(attrName, attrValue);
|
|
224
|
+
}
|
|
225
|
+
let html = element.innerHTML;
|
|
226
|
+
if (html.indexOf(key) !== -1){
|
|
227
|
+
html = html.replace(regex, value);
|
|
228
|
+
element.innerHTML = html;
|
|
229
|
+
}
|
|
230
|
+
elementPrefix.set(prefix, value)
|
|
231
|
+
eventElements.set(element, elementPrefix)
|
|
232
|
+
|
|
233
|
+
}
|
|
193
234
|
|
|
194
235
|
CoCreateEvents.init();
|
|
195
236
|
|