@alexgyver/component 1.3.2 → 1.3.3
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 +47 -48
- package/README.md +16 -16
- package/package.json +2 -2
package/Component.js
CHANGED
|
@@ -1,11 +1,15 @@
|
|
|
1
|
+
//#region Component
|
|
1
2
|
export class Component {
|
|
3
|
+
static ctx;
|
|
4
|
+
|
|
2
5
|
/**
|
|
3
6
|
* Создать компонент и поместить его в переменную $root
|
|
4
7
|
* @param {string} tag html tag элемента
|
|
5
8
|
* @param {object} data параметры
|
|
9
|
+
* @param {Boolean} svg SVG
|
|
6
10
|
*/
|
|
7
11
|
constructor(tag, data = {}, svg = false) {
|
|
8
|
-
|
|
12
|
+
Component.ctx = this;
|
|
9
13
|
this.$root = Component.make(tag, data, svg);
|
|
10
14
|
}
|
|
11
15
|
|
|
@@ -13,62 +17,59 @@ export class Component {
|
|
|
13
17
|
* Создать компонент
|
|
14
18
|
* @param {string} tag html tag элемента
|
|
15
19
|
* @param {object} data параметры
|
|
20
|
+
* @param {Boolean} svg SVG
|
|
16
21
|
* @returns {Node}
|
|
17
22
|
* @params
|
|
18
|
-
* tag {string} - тег html
|
|
19
|
-
* context {object} - контекст для параметра 'var' и
|
|
23
|
+
* tag {string} - тег html элемента. Если 'svg' - включится режим SVG на детей
|
|
24
|
+
* context {object} - контекст для параметра 'var' и вызовов, прокидывается в детей. Если указан явно как null - прерывает проброс
|
|
25
|
+
* parent - {Element} добавляет компонент к указанному элементу
|
|
20
26
|
* text {string} - добавить в textContent
|
|
21
27
|
* html {string} - добавить в innerHTML
|
|
22
|
-
*
|
|
23
|
-
* props {object} - добавить свойства
|
|
24
|
-
* class {string} - добавить в className
|
|
25
|
-
* also {function} - вызвать с текущим компонентом: { ... , also(el) { console.log(el); }, }
|
|
26
|
-
* push {array} - добавить к массиву
|
|
27
|
-
* var {string} - создаёт переменную $имя в указанном контексте
|
|
28
|
-
* events {object} - добавляет addEventListener'ы {event: handler}
|
|
29
|
-
* parent - {Element} добавляет компонент к указанному элементу (имеет смысл только для корневого компонента)
|
|
28
|
+
* class {string} - добавить в className (add по пробелам)
|
|
30
29
|
* style {string | object} - объект в виде { padding: '0px', ... } или строка css стилей
|
|
31
|
-
*
|
|
32
|
-
*
|
|
33
|
-
*
|
|
30
|
+
* push {array} - добавить к указанному массиву
|
|
31
|
+
* var {string} - создаёт переменную $имя в указанном контексте
|
|
32
|
+
* events {object} - добавляет addEventListener'ы {event: e => {}}
|
|
33
|
+
* children/children_r - массив {DOM, Component, object, html string}. _r - заменить имеющиеся. Без тега tag будет div
|
|
34
|
+
* child/child_r - {DOM, Component, object, html string}. _r - заменить имеющиеся. Без тега tag будет div
|
|
35
|
+
* attrs {object} - добавить аттрибуты (через setAttribute)
|
|
36
|
+
* props {object} - добавить свойства (как el[prop])
|
|
37
|
+
* also {function} - вызвать с текущим компонентом: also(el) { console.log(el); }
|
|
34
38
|
* всё остальное будет добавлено как property
|
|
35
39
|
*/
|
|
36
40
|
static make(tag, data = {}, svg = false) {
|
|
37
41
|
if (!tag || typeof data !== 'object') return null;
|
|
38
42
|
if (data instanceof Node) return data;
|
|
43
|
+
if (tag == 'svg') svg = true;
|
|
39
44
|
return Component.config(svg ? document.createElementNS("http://www.w3.org/2000/svg", tag) : document.createElement(tag), data, svg);
|
|
40
45
|
}
|
|
41
46
|
|
|
42
|
-
static makeSVG(tag, data = {}) {
|
|
43
|
-
return Component.make(tag, data, true);
|
|
44
|
-
}
|
|
45
|
-
|
|
46
47
|
/**
|
|
47
48
|
* Настроить элемент
|
|
48
49
|
* @param {Node | Array} el элемент или массив элементов
|
|
49
50
|
* @param {object} data параметры
|
|
50
|
-
* @param {
|
|
51
|
+
* @param {Boolean} svg SVG
|
|
51
52
|
* @returns {Node}
|
|
52
53
|
*/
|
|
53
54
|
static config(el, data, svg = false) {
|
|
54
55
|
if (Array.isArray(el)) {
|
|
55
56
|
el.forEach(e => Component.config(e, data, svg));
|
|
56
|
-
return;
|
|
57
|
+
return null;
|
|
57
58
|
}
|
|
58
59
|
if (!(el instanceof Node) || (typeof data !== 'object')) return el;
|
|
59
60
|
|
|
60
|
-
|
|
61
|
-
|
|
61
|
+
let ctx = data.context;
|
|
62
|
+
Component.ctx = (ctx === null) ? null : (ctx ? ctx : Component.ctx);
|
|
63
|
+
ctx = Component.ctx;
|
|
62
64
|
|
|
63
65
|
let addChild = (obj) => {
|
|
64
66
|
if (!obj) return;
|
|
65
67
|
if (obj instanceof Node) el.appendChild(obj);
|
|
66
68
|
else if (obj instanceof Component) el.appendChild(obj.$root);
|
|
69
|
+
else if (typeof obj === 'string') el.innerHTML += obj;
|
|
67
70
|
else if (typeof obj === 'object') {
|
|
68
71
|
let cmp = Component.make(obj.tag ?? 'div', obj, svg || obj.tag == 'svg');
|
|
69
72
|
if (cmp) el.appendChild(cmp);
|
|
70
|
-
} else if (typeof obj === 'string') {
|
|
71
|
-
el.innerHTML += obj;
|
|
72
73
|
}
|
|
73
74
|
}
|
|
74
75
|
|
|
@@ -78,11 +79,11 @@ export class Component {
|
|
|
78
79
|
case 'tag':
|
|
79
80
|
case 'context':
|
|
80
81
|
case 'get':
|
|
82
|
+
case 'also':
|
|
81
83
|
continue;
|
|
82
84
|
case 'text': el.textContent = val + ''; break;
|
|
83
85
|
case 'html': el.innerHTML = val; break;
|
|
84
86
|
case 'class': el.classList.add(...val.split(' ')); break;
|
|
85
|
-
case 'also': if (ctx) val.call(ctx, el); break;
|
|
86
87
|
case 'push': val.push(el); break;
|
|
87
88
|
case 'var': if (ctx) ctx['$' + val] = el; break;
|
|
88
89
|
case 'events': for (let ev in val) el.addEventListener(ev, val[ev].bind(ctx)); break;
|
|
@@ -100,17 +101,14 @@ export class Component {
|
|
|
100
101
|
default: el[key] = val; break;
|
|
101
102
|
}
|
|
102
103
|
}
|
|
103
|
-
if (data.
|
|
104
|
+
if (data.also && ctx) data.also.call(ctx, el);
|
|
104
105
|
return el;
|
|
105
106
|
}
|
|
106
107
|
|
|
107
|
-
static configSVG(el, data) {
|
|
108
|
-
return Component.config(el, data, true);
|
|
109
|
-
}
|
|
110
|
-
|
|
111
108
|
/**
|
|
112
109
|
* Создать массив компонентов из массива объектов конфигурации
|
|
113
110
|
* @param {array} arr массив объектов конфигурации
|
|
111
|
+
* @param {Boolean} svg SVG
|
|
114
112
|
* @returns {array} of Elements
|
|
115
113
|
*/
|
|
116
114
|
static makeArray(arr, svg = false) {
|
|
@@ -118,10 +116,6 @@ export class Component {
|
|
|
118
116
|
return arr.map(x => Component.make(x.tag, x, svg));
|
|
119
117
|
}
|
|
120
118
|
|
|
121
|
-
static makeArraySVG(arr) {
|
|
122
|
-
return Component.makeArray(arr, true);
|
|
123
|
-
}
|
|
124
|
-
|
|
125
119
|
/**
|
|
126
120
|
* Создать теневой компонент от указанного tag, дети подключатся к нему в shadowRoot
|
|
127
121
|
* @param {string|Node} host html tag теневого элемента или Node
|
|
@@ -151,10 +145,27 @@ export class Component {
|
|
|
151
145
|
Component.config($host, data);
|
|
152
146
|
return $host;
|
|
153
147
|
}
|
|
148
|
+
}
|
|
154
149
|
|
|
155
|
-
|
|
150
|
+
//#region SVG
|
|
151
|
+
export class SVG {
|
|
152
|
+
static make = (tag, data) => Component.make(tag, data, true);
|
|
153
|
+
static config = (el, data) => Component.config(el, data, true);
|
|
154
|
+
static makeArray = (arr) => Component.makeArray(arr, true);
|
|
155
|
+
|
|
156
|
+
static svg = (attrs = {}, props = {}) => SVG._make('svg', attrs, props);
|
|
157
|
+
static rect = (x, y, w, h, rx, ry, attrs = {}, props = {}) => SVG._make('rect', { ...attrs, x: x, y: y, width: w, height: h, rx: rx, ry: ry }, props);
|
|
158
|
+
static circle = (x, y, r, attrs = {}, props = {}) => SVG._make('circle', { ...attrs, cx: x, cy: y, r: r }, props);
|
|
159
|
+
static line = (x1, y1, x2, y2, attrs = {}, props = {}) => SVG._make('line', { ...attrs, x1: x1, y1: y1, x2: x2, y2: y2 }, props);
|
|
160
|
+
static polyline = (points, attrs = {}, props = {}) => SVG._make('polyline', { ...attrs, points: points }, props);
|
|
161
|
+
static polygon = (points, attrs = {}, props = {}) => SVG._make('polygon', { ...attrs, points: points }, props);
|
|
162
|
+
static path = (d, attrs = {}, props = {}) => SVG._make('path', { ...attrs, d: d }, props);
|
|
163
|
+
static text = (text, x, y, attrs = {}, props = {}) => SVG._make('text', { ...attrs, x: x, y: y }, { ...props, text: text });
|
|
164
|
+
|
|
165
|
+
static _make = (tag, attrs = {}, props = {}) => SVG.make(tag, { attrs: { ...attrs }, ...props });
|
|
156
166
|
}
|
|
157
167
|
|
|
168
|
+
//#region Sheet
|
|
158
169
|
export class Sheet {
|
|
159
170
|
/**
|
|
160
171
|
* Добавить стиль с уникальным id в head. ext - стиль можно будет удалить по id
|
|
@@ -197,6 +208,7 @@ export class Sheet {
|
|
|
197
208
|
static #ext = new Map();
|
|
198
209
|
}
|
|
199
210
|
|
|
211
|
+
//#region StyledComponent
|
|
200
212
|
export class StyledComponent extends Component {
|
|
201
213
|
/**
|
|
202
214
|
* Создать компонент и поместить его в переменную $root
|
|
@@ -223,17 +235,4 @@ export class StyledComponent extends Component {
|
|
|
223
235
|
Sheet.addStyle(style, id, ext);
|
|
224
236
|
return Component.make(tag, data);
|
|
225
237
|
}
|
|
226
|
-
}
|
|
227
|
-
|
|
228
|
-
export class SVG {
|
|
229
|
-
static svg = (attrs = {}, props = {}) => SVG._make('svg', attrs, props);
|
|
230
|
-
static rect = (x, y, w, h, rx, ry, attrs = {}, props = {}) => SVG._make('rect', { ...attrs, x: x, y: y, width: w, height: h, rx: rx, ry: ry }, props);
|
|
231
|
-
static circle = (x, y, r, attrs = {}, props = {}) => SVG._make('circle', { ...attrs, cx: x, cy: y, r: r }, props);
|
|
232
|
-
static line = (x1, y1, x2, y2, attrs = {}, props = {}) => SVG._make('line', { ...attrs, x1: x1, y1: y1, x2: x2, y2: y2 }, props);
|
|
233
|
-
static polyline = (points, attrs = {}, props = {}) => SVG._make('polyline', { ...attrs, points: points }, props);
|
|
234
|
-
static polygon = (points, attrs = {}, props = {}) => SVG._make('polygon', { ...attrs, points: points }, props);
|
|
235
|
-
static path = (d, attrs = {}, props = {}) => SVG._make('path', { ...attrs, d: d }, props);
|
|
236
|
-
static text = (text, x, y, attrs = {}, props = {}) => SVG._make('text', { ...attrs, x: x, y: y }, { ...props, text: text });
|
|
237
|
-
|
|
238
|
-
static _make = (tag, attrs = {}, props = {}) => Component.makeSVG(tag, { attrs: { ...attrs }, ...props });
|
|
239
238
|
}
|
package/README.md
CHANGED
|
@@ -18,26 +18,24 @@ Component(tag, data = {}, svg = false);
|
|
|
18
18
|
* @param {string} tag html tag элемента
|
|
19
19
|
* @param {object} data параметры
|
|
20
20
|
* @returns {Node}
|
|
21
|
-
* tag {string} - тег html
|
|
22
|
-
* context {object} - контекст для параметра 'var' и
|
|
21
|
+
* tag {string} - тег html элемента. Если 'svg' - включится режим SVG на детей
|
|
22
|
+
* context {object} - контекст для параметра 'var' и вызовов, прокидывается в детей. Если указан явно как null - прерывает проброс
|
|
23
|
+
* parent - {Element} добавляет компонент к указанному элементу
|
|
23
24
|
* text {string} - добавить в textContent
|
|
24
25
|
* html {string} - добавить в innerHTML
|
|
25
|
-
*
|
|
26
|
-
* props {object} - добавить свойства
|
|
27
|
-
* class {string} - добавить в className
|
|
28
|
-
* also {function} - вызвать с текущим компонентом: { ... , also(el) { console.log(el); }, }
|
|
29
|
-
* push {array} - добавить к массиву
|
|
30
|
-
* var {string} - создаёт переменную $имя в указанном контексте
|
|
31
|
-
* events {object} - добавляет addEventListener'ы {event: handler}
|
|
32
|
-
* parent - {Element} добавляет компонент к указанному элементу (имеет смысл только для корневого компонента)
|
|
26
|
+
* class {string} - добавить в className (add по пробелам)
|
|
33
27
|
* style {string | object} - объект в виде { padding: '0px', ... } или строка css стилей
|
|
34
|
-
*
|
|
35
|
-
*
|
|
36
|
-
*
|
|
28
|
+
* push {array} - добавить к указанному массиву
|
|
29
|
+
* var {string} - создаёт переменную $имя в указанном контексте
|
|
30
|
+
* events {object} - добавляет addEventListener'ы {event: e => {}}
|
|
31
|
+
* children/children_r - массив {DOM, Component, object, html string}. _r - заменить имеющиеся. Без тега tag будет div
|
|
32
|
+
* child/child_r - {DOM, Component, object, html string}. _r - заменить имеющиеся. Без тега tag будет div
|
|
33
|
+
* attrs {object} - добавить аттрибуты (через setAttribute)
|
|
34
|
+
* props {object} - добавить свойства (как el[prop])
|
|
35
|
+
* also {function} - вызвать с текущим компонентом: also(el) { console.log(el); }
|
|
37
36
|
* всё остальное будет добавлено как property
|
|
38
37
|
*/
|
|
39
38
|
Component.make(tag, data = {});
|
|
40
|
-
Component.makeSVG(tag, data = {});
|
|
41
39
|
|
|
42
40
|
/**
|
|
43
41
|
* Создать теневой компонент от указанного tag, дети подключатся к нему в shadowRoot
|
|
@@ -55,7 +53,6 @@ Component.makeShadow(host, data = {}, sheet = null);
|
|
|
55
53
|
* @returns {Node}
|
|
56
54
|
*/
|
|
57
55
|
Component.config(el, data);
|
|
58
|
-
Component.configSVG(el, data);
|
|
59
56
|
|
|
60
57
|
/**
|
|
61
58
|
* Создать массив компонентов из массива объектов конфигурации
|
|
@@ -63,12 +60,15 @@ Component.configSVG(el, data);
|
|
|
63
60
|
* @returns {array} of Elements
|
|
64
61
|
*/
|
|
65
62
|
Component.makeArray(arr);
|
|
66
|
-
Component.makeArraySVG(arr);
|
|
67
63
|
```
|
|
68
64
|
|
|
69
65
|
### SVG
|
|
70
66
|
```js
|
|
67
|
+
SVG.make(tag, data);
|
|
68
|
+
SVG.config(el, data);
|
|
69
|
+
SVG.makeArray(arr);
|
|
71
70
|
SVG.svg(attrs = {}, props = {});
|
|
71
|
+
|
|
72
72
|
SVG.rect(x, y, w, h, rx, ry, attrs = {}, props = {});
|
|
73
73
|
SVG.circle(x, y, r, attrs = {}, props = {});
|
|
74
74
|
SVG.line(x1, y1, x2, y2, attrs = {}, props = {});
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@alexgyver/component",
|
|
3
|
-
"version": "1.3.
|
|
4
|
-
"description": "Simple HTML element builder",
|
|
3
|
+
"version": "1.3.3",
|
|
4
|
+
"description": "Simple HTML&SVG element builder",
|
|
5
5
|
"main": "./Component.js",
|
|
6
6
|
"module": "./Component.js",
|
|
7
7
|
"types": "./Component.js",
|