@alexgyver/component 1.1.4 → 1.2.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/Component.js +61 -60
- package/README.md +21 -16
- package/example/script.js +3 -17
- package/package.json +1 -1
package/Component.js
CHANGED
|
@@ -30,8 +30,8 @@ export class Component {
|
|
|
30
30
|
* events {object} - добавляет addEventListener'ы {event: handler}
|
|
31
31
|
* parent - {Element} добавляет компонент к указанному элементу (имеет смысл только для корневого компонента)
|
|
32
32
|
* style {string | object} - объект в виде { padding: '0px', ... } или строка css стилей
|
|
33
|
-
* children - массив DOM, Component, object, html string
|
|
34
|
-
* child - DOM, Component, object, html string
|
|
33
|
+
* children/children_r - массив DOM, Component, object, html string. _r - заменить имеющиеся
|
|
34
|
+
* child/child_r - DOM, Component, object, html string. _r - заменить имеющиеся
|
|
35
35
|
* всё остальное будет добавлено как property
|
|
36
36
|
*/
|
|
37
37
|
static make(tag, data = {}, svg = false) {
|
|
@@ -44,36 +44,6 @@ export class Component {
|
|
|
44
44
|
return Component.make(tag, data, true);
|
|
45
45
|
}
|
|
46
46
|
|
|
47
|
-
/**
|
|
48
|
-
* Создать теневой компонент от указанного tag, дети подключатся к нему в shadowRoot
|
|
49
|
-
* @param {string|Node} host html tag теневого элемента или Node
|
|
50
|
-
* @param {object} data параметры внешнего элемента
|
|
51
|
-
* @param {string} sheet css стили
|
|
52
|
-
* @returns {Node} host
|
|
53
|
-
*/
|
|
54
|
-
static makeShadow(host, data = {}, sheet = null) {
|
|
55
|
-
if (!host || typeof data !== 'object') return null;
|
|
56
|
-
|
|
57
|
-
let $host = (host instanceof Node) ? host : document.createElement(host);
|
|
58
|
-
$host.attachShadow({ mode: 'open' });
|
|
59
|
-
|
|
60
|
-
Component.config($host.shadowRoot, {
|
|
61
|
-
context: data.context,
|
|
62
|
-
children: [
|
|
63
|
-
{
|
|
64
|
-
tag: 'style',
|
|
65
|
-
textContent: sheet ?? '',
|
|
66
|
-
},
|
|
67
|
-
data.child ?? {},
|
|
68
|
-
...(data.children ?? []),
|
|
69
|
-
]
|
|
70
|
-
});
|
|
71
|
-
delete data.children;
|
|
72
|
-
delete data.child;
|
|
73
|
-
Component.config($host, data);
|
|
74
|
-
return $host;
|
|
75
|
-
}
|
|
76
|
-
|
|
77
47
|
/**
|
|
78
48
|
* Настроить элемент
|
|
79
49
|
* @param {Node} el элемент
|
|
@@ -120,7 +90,7 @@ export class Component {
|
|
|
120
90
|
case 'var': if (context) context['$' + val] = el; break;
|
|
121
91
|
case 'events': for (let ev in val) if (val[ev]) el.addEventListener(ev, val[ev].bind(context)); break;
|
|
122
92
|
case 'parent': if (val instanceof Node || val instanceof DocumentFragment) val.append(el); break;
|
|
123
|
-
case 'attrs': for (let attr in val) el.setAttribute(attr, val[attr]); break;
|
|
93
|
+
case 'attrs': for (let attr in val) svg ? el.setAttributeNS(null, attr, val[attr]) : el.setAttribute(attr, val[attr]); break;
|
|
124
94
|
case 'props': for (let prop in val) el[prop] = val[prop]; break;
|
|
125
95
|
case 'child_r': el.replaceChildren();
|
|
126
96
|
case 'child': addChild(val); break;
|
|
@@ -153,12 +123,42 @@ export class Component {
|
|
|
153
123
|
static makeArraySVG(arr) {
|
|
154
124
|
return Component.makeArray(arr, true);
|
|
155
125
|
}
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* Создать теневой компонент от указанного tag, дети подключатся к нему в shadowRoot
|
|
129
|
+
* @param {string|Node} host html tag теневого элемента или Node
|
|
130
|
+
* @param {object} data параметры внешнего элемента
|
|
131
|
+
* @param {string} sheet css стили
|
|
132
|
+
* @returns {Node} host
|
|
133
|
+
*/
|
|
134
|
+
static makeShadow(host, data = {}, sheet = null) {
|
|
135
|
+
if (!host || typeof data !== 'object') return null;
|
|
136
|
+
|
|
137
|
+
let $host = (host instanceof Node) ? host : document.createElement(host);
|
|
138
|
+
$host.attachShadow({ mode: 'open' });
|
|
139
|
+
|
|
140
|
+
Component.config($host.shadowRoot, {
|
|
141
|
+
context: data.context,
|
|
142
|
+
children: [
|
|
143
|
+
{
|
|
144
|
+
tag: 'style',
|
|
145
|
+
textContent: sheet ?? '',
|
|
146
|
+
},
|
|
147
|
+
data.child ?? {},
|
|
148
|
+
...(data.children ?? []),
|
|
149
|
+
]
|
|
150
|
+
});
|
|
151
|
+
delete data.children;
|
|
152
|
+
delete data.child;
|
|
153
|
+
Component.config($host, data);
|
|
154
|
+
return $host;
|
|
155
|
+
}
|
|
156
156
|
}
|
|
157
157
|
|
|
158
158
|
export class Sheet {
|
|
159
159
|
/**
|
|
160
160
|
* Добавить стиль с уникальным id в head. ext - стиль можно будет удалить по id
|
|
161
|
-
* @param {string|array} style стили в виде css строки
|
|
161
|
+
* @param {string|array} style стили в виде css строки
|
|
162
162
|
* @param {string|this} id уникальный id стиля. При передаче this будет именем класса
|
|
163
163
|
* @param {boolean} ext внешний стиль - может быть удалён по id
|
|
164
164
|
*/
|
|
@@ -166,51 +166,52 @@ export class Sheet {
|
|
|
166
166
|
if (!style || !id) return;
|
|
167
167
|
if (typeof id === 'object') id = id.constructor.name;
|
|
168
168
|
|
|
169
|
-
if (!Sheet.#
|
|
170
|
-
if (typeof style === 'object') {
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
}
|
|
169
|
+
if (!Sheet.#int.has(id) && !Sheet.#ext.has(id)) {
|
|
170
|
+
// if (typeof style === 'object') {
|
|
171
|
+
// let str = '';
|
|
172
|
+
// let f = 0;
|
|
173
|
+
// for (const v of style) {
|
|
174
|
+
// if (f = !f) {
|
|
175
|
+
// str += v;
|
|
176
|
+
// } else {
|
|
177
|
+
// str += '{';
|
|
178
|
+
// for (const rule of v) str += rule + ';';
|
|
179
|
+
// str += '}';
|
|
180
|
+
// }
|
|
181
|
+
// }
|
|
182
|
+
// style = str;
|
|
183
|
+
// }
|
|
184
184
|
|
|
185
185
|
if (ext) {
|
|
186
186
|
let sheet = document.createElement('style');
|
|
187
187
|
document.head.appendChild(sheet);
|
|
188
188
|
sheet.sheet.insertRule(style);
|
|
189
|
-
Sheet.#
|
|
189
|
+
Sheet.#ext.set(id, sheet);
|
|
190
190
|
} else {
|
|
191
191
|
if (!Sheet.#sheet) {
|
|
192
192
|
Sheet.#sheet = document.head.appendChild(document.createElement('style')).sheet;
|
|
193
193
|
}
|
|
194
194
|
Sheet.#sheet.insertRule(style);
|
|
195
|
-
Sheet.#
|
|
195
|
+
Sheet.#int.add(id);
|
|
196
196
|
}
|
|
197
197
|
}
|
|
198
198
|
}
|
|
199
199
|
|
|
200
200
|
/**
|
|
201
201
|
* Удалить ext стиль по его id
|
|
202
|
-
* @param {string} id id
|
|
202
|
+
* @param {string} id id стиля. При передаче this будет именем класса
|
|
203
203
|
*/
|
|
204
204
|
static removeStyle(id) {
|
|
205
|
-
if (
|
|
206
|
-
|
|
207
|
-
Sheet.#
|
|
205
|
+
if (typeof id === 'object') id = id.constructor.name;
|
|
206
|
+
if (Sheet.#ext.has(id)) {
|
|
207
|
+
Sheet.#ext.get(id).remove();
|
|
208
|
+
Sheet.#ext.delete(id);
|
|
208
209
|
}
|
|
209
210
|
}
|
|
210
211
|
|
|
211
|
-
static #sheet;
|
|
212
|
-
static #
|
|
213
|
-
static #
|
|
212
|
+
static #sheet = null;
|
|
213
|
+
static #int = new Set();
|
|
214
|
+
static #ext = new Map();
|
|
214
215
|
}
|
|
215
216
|
|
|
216
217
|
export class StyledComponent extends Component {
|
|
@@ -218,7 +219,7 @@ export class StyledComponent extends Component {
|
|
|
218
219
|
* Создать компонент и поместить его в переменную $root
|
|
219
220
|
* @param {string} tag html tag элемента
|
|
220
221
|
* @param {object} data параметры
|
|
221
|
-
* @param {string|array} style стили в виде css строки
|
|
222
|
+
* @param {string|array} style стили в виде css строки
|
|
222
223
|
* @param {string|this} id уникальный id стиля. При передаче this будет именем класса
|
|
223
224
|
* @param {boolean} ext внешний стиль - может быть удалён по id
|
|
224
225
|
*/
|
|
@@ -231,7 +232,7 @@ export class StyledComponent extends Component {
|
|
|
231
232
|
* Создать компонент
|
|
232
233
|
* @param {string} tag html tag элемента
|
|
233
234
|
* @param {object} data параметры
|
|
234
|
-
* @param {string|array} style стили в виде css строки
|
|
235
|
+
* @param {string|array} style стили в виде css строки
|
|
235
236
|
* @param {string|this} id уникальный id стиля. При передаче this будет именем класса
|
|
236
237
|
* @param {boolean} ext внешний стиль - может быть удалён по id
|
|
237
238
|
*/
|
package/README.md
CHANGED
|
@@ -11,31 +11,34 @@
|
|
|
11
11
|
* @param {string} tag html tag элемента
|
|
12
12
|
* @param {object} data параметры
|
|
13
13
|
*/
|
|
14
|
-
Component(tag, data = {});
|
|
14
|
+
Component(tag, data = {}, svg = false);
|
|
15
15
|
|
|
16
16
|
/**
|
|
17
17
|
* Создать компонент
|
|
18
18
|
* @param {string} tag html tag элемента
|
|
19
19
|
* @param {object} data параметры
|
|
20
20
|
* @returns {Node}
|
|
21
|
-
* tag {string} тег html элемента (для указания в children например)
|
|
22
|
-
*
|
|
23
|
-
*
|
|
24
|
-
*
|
|
25
|
-
*
|
|
26
|
-
*
|
|
27
|
-
*
|
|
21
|
+
* tag {string} - тег html элемента (для указания в children например)
|
|
22
|
+
* svg {boolean} - создавать как SVG элемент
|
|
23
|
+
* context {object} - контекст для параметра 'var' и вызовов 'also'
|
|
24
|
+
* text {string} - добавить в textContent
|
|
25
|
+
* html {string} - добавить в innerHTML
|
|
26
|
+
* attrs {object} - добавить аттрибуты
|
|
27
|
+
* props {object} - добавить свойства
|
|
28
|
+
* class {string} - добавить в className
|
|
28
29
|
* also {function} - вызвать с текущим компонентом: { ... , also(el) { console.log(el); }, }
|
|
29
30
|
* export {array} - положить в 0 ячейку указанного массива
|
|
30
|
-
*
|
|
31
|
-
*
|
|
31
|
+
* push {array} - добавить к массиву
|
|
32
|
+
* var {string} - создаёт переменную $имя в указанном контексте
|
|
33
|
+
* events {object} - добавляет addEventListener'ы {event: handler}
|
|
32
34
|
* parent - {Element} добавляет компонент к указанному элементу (имеет смысл только для корневого компонента)
|
|
33
|
-
* style {string | object} объект в виде { padding: '0px', ... } или строка css стилей
|
|
34
|
-
* children - массив DOM, Component, object, html string
|
|
35
|
-
* child - DOM, Component, object, html string
|
|
35
|
+
* style {string | object} - объект в виде { padding: '0px', ... } или строка css стилей
|
|
36
|
+
* children/children_r - массив DOM, Component, object, html string. _r - заменить имеющиеся
|
|
37
|
+
* child/child_r - DOM, Component, object, html string. _r - заменить имеющиеся
|
|
36
38
|
* всё остальное будет добавлено как property
|
|
37
39
|
*/
|
|
38
40
|
Component.make(tag, data = {});
|
|
41
|
+
Component.makeSVG(tag, data = {});
|
|
39
42
|
|
|
40
43
|
/**
|
|
41
44
|
* Создать теневой компонент от указанного tag, дети подключатся к нему в shadowRoot
|
|
@@ -53,6 +56,7 @@ Component.makeShadow(host, data = {}, sheet = null);
|
|
|
53
56
|
* @returns {Node}
|
|
54
57
|
*/
|
|
55
58
|
Component.config(el, data);
|
|
59
|
+
Component.configSVG(el, data);
|
|
56
60
|
|
|
57
61
|
/**
|
|
58
62
|
* Создать массив компонентов из массива объектов конфигурации
|
|
@@ -60,13 +64,14 @@ Component.config(el, data);
|
|
|
60
64
|
* @returns {array} of Elements
|
|
61
65
|
*/
|
|
62
66
|
Component.makeArray(arr);
|
|
67
|
+
Component.makeArraySVG(arr);
|
|
63
68
|
```
|
|
64
69
|
|
|
65
70
|
### Sheet
|
|
66
71
|
```js
|
|
67
72
|
/**
|
|
68
73
|
* Добавить стиль с уникальным id в head. ext - стиль можно будет удалить по id
|
|
69
|
-
* @param {string|array} style стили в виде css
|
|
74
|
+
* @param {string|array} style стили в виде css
|
|
70
75
|
* @param {string|this} id уникальный id стиля. При передаче this будет именем класса
|
|
71
76
|
* @param {boolean} ext внешний стиль - может быть удалён по id
|
|
72
77
|
*/
|
|
@@ -85,7 +90,7 @@ Sheet.removeStyle(id);
|
|
|
85
90
|
* Создать компонент и поместить его в переменную $root
|
|
86
91
|
* @param {string} tag html tag элемента
|
|
87
92
|
* @param {object} data параметры
|
|
88
|
-
* @param {string|array} style стили в виде css строки
|
|
93
|
+
* @param {string|array} style стили в виде css строки
|
|
89
94
|
* @param {string|this} id уникальный id стиля. При передаче this будет именем класса
|
|
90
95
|
* @param {boolean} ext внешний стиль - может быть удалён по id
|
|
91
96
|
*/
|
|
@@ -95,7 +100,7 @@ StyledComponent(tag, data = {}, style = null, id = null, ext = false);
|
|
|
95
100
|
* Создать компонент
|
|
96
101
|
* @param {string} tag html tag элемента
|
|
97
102
|
* @param {object} data параметры
|
|
98
|
-
* @param {string|array} style стили в виде css строки
|
|
103
|
+
* @param {string|array} style стили в виде css строки
|
|
99
104
|
* @param {string|this} id уникальный id стиля. При передаче this будет именем класса
|
|
100
105
|
* @param {boolean} ext внешний стиль - может быть удалён по id
|
|
101
106
|
*/
|
package/example/script.js
CHANGED
|
@@ -16,12 +16,7 @@ class Button extends Component {
|
|
|
16
16
|
}
|
|
17
17
|
});
|
|
18
18
|
|
|
19
|
-
Sheet.addStyle(
|
|
20
|
-
'.btn', [
|
|
21
|
-
'background: red',
|
|
22
|
-
'color: white'
|
|
23
|
-
]
|
|
24
|
-
], this, true); // this превратится в Button
|
|
19
|
+
Sheet.addStyle('.btn {background: red;color: white}', this, true); // this превратится в Button
|
|
25
20
|
}
|
|
26
21
|
}
|
|
27
22
|
|
|
@@ -34,11 +29,7 @@ class Input extends StyledComponent {
|
|
|
34
29
|
value: text,
|
|
35
30
|
class: 'inp',
|
|
36
31
|
},
|
|
37
|
-
|
|
38
|
-
'.inp', [
|
|
39
|
-
'background: blue'
|
|
40
|
-
]
|
|
41
|
-
],
|
|
32
|
+
'.inp {background: blue}',
|
|
42
33
|
'Input'
|
|
43
34
|
);
|
|
44
35
|
}
|
|
@@ -52,12 +43,7 @@ class Num {
|
|
|
52
43
|
value: text,
|
|
53
44
|
class: 'num',
|
|
54
45
|
},
|
|
55
|
-
|
|
56
|
-
'.num', [
|
|
57
|
-
'background: green',
|
|
58
|
-
'margin-left: 10px',
|
|
59
|
-
]
|
|
60
|
-
],
|
|
46
|
+
'.num {background: green;margin-left: 10px}',
|
|
61
47
|
'Num'
|
|
62
48
|
);
|
|
63
49
|
}
|