@cocreate/events 1.1.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 CHANGED
@@ -1,3 +1,17 @@
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
+
8
+ # [1.2.0](https://github.com/CoCreate-app/CoCreate-events/compare/v1.1.0...v1.2.0) (2023-02-02)
9
+
10
+
11
+ ### Features
12
+
13
+ * add @cocreate/actions, events bubble up and triggers parent events ([9946b30](https://github.com/CoCreate-app/CoCreate-events/commit/9946b302b5ed19e3f20d850f0734dde6e28852fe))
14
+
1
15
  # [1.1.0](https://github.com/CoCreate-app/CoCreate-events/compare/v1.0.0...v1.1.0) (2023-02-02)
2
16
 
3
17
 
package/demo/click.html CHANGED
@@ -63,7 +63,7 @@
63
63
 
64
64
 
65
65
  <!-- <script src="../dist/CoCreate-events.js"></script> -->
66
- <!-- <script src="https://cdn.cocreate.app/latest/CoCreate.min.js" ></script> -->
67
- <script src="../../../CoCreateJS/dist/CoCreate.js" ></script>
66
+ <script src="https://cdn.cocreate.app/latest/CoCreate.min.js" ></script>
67
+ <!-- <script src="../../../CoCreateJS/dist/CoCreate.js" ></script> -->
68
68
  </body>
69
69
  </html>
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
- <script src="https://cdn.cocreate.app/latest/CoCreate.min.js" ></script>
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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cocreate/events",
3
- "version": "1.1.0",
3
+ "version": "1.3.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",
@@ -60,6 +60,7 @@
60
60
  "webpack-log": "^3.0.1"
61
61
  },
62
62
  "dependencies": {
63
+ "@cocreate/actions": "^1.5.59",
63
64
  "@cocreate/docs": "^1.5.12",
64
65
  "@cocreate/utils": "^1.18.0"
65
66
  }
package/src/index.js CHANGED
@@ -3,38 +3,50 @@ 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
- this.initElement(document, 'events');
8
- this.initElement(document, 'click');
9
- this.initElement(document, 'hover');
10
- this.initElement(document, 'mouseover');
11
- this.initElement(document, 'mouseout');
12
- this.initElement(document, 'input');
13
- this.initElement(document, 'change');
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(container, prefix) {
17
- this.__initElementEvent(container || document, prefix);
21
+ initElement: function(element, prefix, events) {
22
+ this.__initElementEvent(element || document, prefix, events);
18
23
  },
19
24
 
20
- __initElementEvent: function(mainContainer, prefix) {
25
+ __initElementEvent: function(element, prefix, events) {
21
26
  const self = this;
22
- let eventNames = [];
23
-
24
- if (prefix === 'toggle') eventNames = ['click'];
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
- if (target.closest(`[actions=*"${prefix}"]`)) return;
32
+ let attribute = target.getAttribute('actions') || ""
33
+ if (attribute.includes(prefix))
34
+ return;
35
+ // if (target.closest(`[actions*="${prefix}"]`))
36
+ // return;
37
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);
45
+ }
46
+ while (parentElement)
47
+
48
+ }
49
+
38
50
  }
39
51
  });
40
52
  });
@@ -52,14 +64,18 @@ const CoCreateEvents = {
52
64
  __updateElements: function(element, prefix) {
53
65
  const self = this;
54
66
  let targetValue = element.getAttribute(`${prefix}-value`) || element.getAttribute(prefix);
67
+ if (!targetValue) return
68
+
55
69
  let values = targetValue.split(',');
56
70
  if (!values || values.length == 0) {
57
71
  return;
58
72
  }
59
-
73
+
60
74
  let targetAttribute = element.getAttribute(`${prefix}-attribute`) || 'class';
61
75
  let targetSelector = element.getAttribute(`${prefix}-target`);
62
76
  let targetClosest = element.getAttribute(`${prefix}-closest`);
77
+ let targetKey = element.getAttribute(`${prefix}-key`);
78
+
63
79
  let targetGroup = element.getAttribute(`${prefix}-group`);
64
80
  if (targetGroup) {
65
81
  document.querySelectorAll(`[${prefix}-group="${targetGroup}"]`).forEach((el) => {
@@ -68,22 +84,24 @@ const CoCreateEvents = {
68
84
  if (!groupValues || groupValues.length == 0) {
69
85
  return;
70
86
  }
87
+
71
88
  groupValues = groupValues.map(x => x.trim());
72
89
 
73
90
  let groupTarget = el.getAttribute(`${prefix}-target`);
74
91
  let groupClosest = el.getAttribute(`${prefix}-closest`);
75
92
  let groupAttribute = el.getAttribute(`${prefix}-attribute`) || 'class';
93
+ let groupKey = el.getAttribute(`${prefix}-key`)
76
94
 
77
95
  // el.removeAttribute(prefix)
78
- self.setValue(el, groupAttribute, groupValue, 'deactivate')
96
+ self.setValue(prefix, el, groupAttribute, groupValues, groupKey, 'deactivate')
79
97
  if (groupTarget)
80
98
  document.querySelectorAll(groupTarget).forEach((el) =>
81
- self.setValue(el, groupAttribute, groupValue, 'deactivate')
99
+ self.setValue(prefix, el, groupAttribute, groupValues, groupKey, 'deactivate')
82
100
  );
83
101
  else if (groupClosest) {
84
102
  let element = el.closest(groupClosest)
85
103
  if (element)
86
- self.setValue(element, groupAttribute, groupValue, 'deactivate');
104
+ self.setValue(prefix, element, groupAttribute, groupValues, groupKey, 'deactivate');
87
105
  }
88
106
  });
89
107
  }
@@ -96,9 +114,9 @@ const CoCreateEvents = {
96
114
  if (targetSelector) {
97
115
  if (/{{\s*([\w\W]+)\s*}}/g.test(targetSelector)) return;
98
116
  targetElements = queryDocumentSelectorAll(targetSelector);
99
- targetElements.forEach((el) => self.setValue(el, targetAttribute, values));
117
+ targetElements.forEach((el) => self.setValue(prefix, el, targetAttribute, values, targetKey));
100
118
  } else
101
- self.setValue(element, targetAttribute, values);
119
+ self.setValue(prefix, element, targetAttribute, values, targetKey);
102
120
 
103
121
  document.dispatchEvent(new CustomEvent(`${prefix}End`, {
104
122
  detail: {}
@@ -106,56 +124,63 @@ const CoCreateEvents = {
106
124
 
107
125
  },
108
126
 
109
- setValue: function(element, attrName, values, deactivate) {
127
+ setValue: function(prefix, element, attrName, values, key, deactivate) {
110
128
  let attrValues, oldValue;
111
-
112
- if (attrName === 'style') {
113
- if (element.getAttribute(attrName)) {
114
- attrValues = element.getAttribute(attrName).split(';').map(x => x.trim());
115
- attrValues = element.getAttribute(attrName).split(';').map(x => x.trim());
116
- attrValues = attrValues.map(function(item, index){
117
- return item.replace(' ','');
118
- });
119
- oldValue = values.filter(x => attrValues.includes(x))[0] || '';
120
- }
121
- let newValue = this.__getNextValue(values, oldValue);
122
-
123
- if (oldValue) {
124
- let [property, value] = oldValue.split(":");
125
- element.style[property] = '';
126
- }
127
-
128
- if (newValue != '') {
129
- let [property, value] = newValue.split(":");
130
- element.style[property] = value;
131
- }
129
+ if (key) {
130
+ key = `{{${key}}}`;
131
+ // let value = values[0]
132
+ replaceKey(prefix, element, key, values)
132
133
  } else {
133
- if (attrName === 'value') {
134
- oldValue = element.getValue()
135
- } else if (element.getAttribute(attrName)) {
136
- attrValues = element.getAttribute(attrName).split(' ').map(x => x.trim());
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
+ });
137
140
  oldValue = values.filter(x => attrValues.includes(x))[0] || '';
138
- }
139
-
140
- let newValue = this.__getNextValue(values, oldValue);
141
- if (deactivate)
142
- newValue = ""
143
-
144
- if (attrName === 'class') {
145
- if (oldValue != '') {
146
- element.classList.remove(oldValue);
147
- if (values.length === 1) {
148
- return;
149
- }
141
+ }
142
+ let newValue = this.__getNextValue(values, oldValue);
143
+
144
+ if (oldValue) {
145
+ let [property, value] = oldValue.split(":");
146
+ element.style[property] = '';
150
147
  }
151
148
 
152
149
  if (newValue != '') {
153
- element.classList.add(newValue);
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
+ }
154
155
  }
155
- } else if (attrName === 'value') {
156
- element.setValue(newValue);
157
156
  } else {
158
- element.setAttribute(attrName, newValue);
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
+ }
159
184
  }
160
185
  }
161
186
  },
@@ -171,6 +196,41 @@ const CoCreateEvents = {
171
196
  }
172
197
  };
173
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
+ }
174
234
 
175
235
  CoCreateEvents.init();
176
236