@alexgyver/component 1.1.2 → 1.1.4
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 +29 -23
- package/package.json +1 -1
package/Component.js
CHANGED
|
@@ -4,9 +4,9 @@ export class Component {
|
|
|
4
4
|
* @param {string} tag html tag элемента
|
|
5
5
|
* @param {object} data параметры
|
|
6
6
|
*/
|
|
7
|
-
constructor(tag, data = {},
|
|
7
|
+
constructor(tag, data = {}, svg = false) {
|
|
8
8
|
data.context = this;
|
|
9
|
-
this.$root = Component.make(tag, data,
|
|
9
|
+
this.$root = Component.make(tag, data, svg);
|
|
10
10
|
}
|
|
11
11
|
|
|
12
12
|
/**
|
|
@@ -15,30 +15,32 @@ export class Component {
|
|
|
15
15
|
* @param {object} data параметры
|
|
16
16
|
* @returns {Node}
|
|
17
17
|
* @params
|
|
18
|
-
* tag {string} тег html элемента (для указания в children например)
|
|
19
|
-
*
|
|
20
|
-
*
|
|
21
|
-
*
|
|
22
|
-
*
|
|
23
|
-
*
|
|
24
|
-
*
|
|
18
|
+
* tag {string} - тег html элемента (для указания в children например)
|
|
19
|
+
* svg {boolean} - создавать как SVG элемент
|
|
20
|
+
* context {object} - контекст для параметра 'var' и вызовов 'also'
|
|
21
|
+
* text {string} - добавить в textContent
|
|
22
|
+
* html {string} - добавить в innerHTML
|
|
23
|
+
* attrs {object} - добавить аттрибуты
|
|
24
|
+
* props {object} - добавить свойства
|
|
25
|
+
* class {string} - добавить в className
|
|
25
26
|
* also {function} - вызвать с текущим компонентом: { ... , also(el) { console.log(el); }, }
|
|
26
27
|
* export {array} - положить в 0 ячейку указанного массива
|
|
27
|
-
*
|
|
28
|
-
*
|
|
28
|
+
* push {array} - добавить к массиву
|
|
29
|
+
* var {string} - создаёт переменную $имя в указанном контексте
|
|
30
|
+
* events {object} - добавляет addEventListener'ы {event: handler}
|
|
29
31
|
* parent - {Element} добавляет компонент к указанному элементу (имеет смысл только для корневого компонента)
|
|
30
|
-
* style {string | object} объект в виде { padding: '0px', ... } или строка css стилей
|
|
32
|
+
* style {string | object} - объект в виде { padding: '0px', ... } или строка css стилей
|
|
31
33
|
* children - массив DOM, Component, object, html string
|
|
32
34
|
* child - DOM, Component, object, html string
|
|
33
35
|
* всё остальное будет добавлено как property
|
|
34
36
|
*/
|
|
35
|
-
static make(tag, data = {},
|
|
37
|
+
static make(tag, data = {}, svg = false) {
|
|
36
38
|
if (!tag || typeof data !== 'object') return null;
|
|
37
39
|
if (data instanceof Node) return data;
|
|
38
|
-
return Component.config(
|
|
40
|
+
return Component.config(svg ? document.createElementNS("http://www.w3.org/2000/svg", tag) : document.createElement(tag), data, svg);
|
|
39
41
|
}
|
|
40
42
|
|
|
41
|
-
static
|
|
43
|
+
static makeSVG(tag, data = {}) {
|
|
42
44
|
return Component.make(tag, data, true);
|
|
43
45
|
}
|
|
44
46
|
|
|
@@ -76,16 +78,18 @@ export class Component {
|
|
|
76
78
|
* Настроить элемент
|
|
77
79
|
* @param {Node} el элемент
|
|
78
80
|
* @param {object} data параметры
|
|
79
|
-
* @param {object}
|
|
81
|
+
* @param {object} svg SVG
|
|
80
82
|
* @returns {Node}
|
|
81
83
|
*/
|
|
82
|
-
static config(el, data,
|
|
84
|
+
static config(el, data, svg = false) {
|
|
83
85
|
if (Array.isArray(el)) {
|
|
84
|
-
el.forEach(e => Component.config(e, data,
|
|
86
|
+
el.forEach(e => Component.config(e, data, svg));
|
|
85
87
|
return;
|
|
86
88
|
}
|
|
87
89
|
if (!(el instanceof Node) || (typeof data !== 'object')) return el;
|
|
90
|
+
|
|
88
91
|
const context = data.context;
|
|
92
|
+
if ('svg' in data) svg = data.svg;
|
|
89
93
|
|
|
90
94
|
let addChild = (obj) => {
|
|
91
95
|
if (!obj) return;
|
|
@@ -93,7 +97,7 @@ export class Component {
|
|
|
93
97
|
else if (obj instanceof Component) el.appendChild(obj.$root);
|
|
94
98
|
else if (typeof obj === 'object') {
|
|
95
99
|
if (!obj.context) obj.context = context;
|
|
96
|
-
let cmp = Component.make(obj.tag, obj,
|
|
100
|
+
let cmp = Component.make(obj.tag, obj, svg);
|
|
97
101
|
if (cmp) el.appendChild(cmp);
|
|
98
102
|
} else if (typeof obj === 'string') {
|
|
99
103
|
el.innerHTML += obj;
|
|
@@ -105,12 +109,14 @@ export class Component {
|
|
|
105
109
|
switch (key) {
|
|
106
110
|
case 'tag':
|
|
107
111
|
case 'context':
|
|
112
|
+
case 'svg':
|
|
108
113
|
continue;
|
|
109
114
|
case 'text': el.textContent = val; break;
|
|
110
115
|
case 'html': el.innerHTML = val; break;
|
|
111
116
|
case 'class': el.classList.add(...val.split(' ')); break;
|
|
112
117
|
case 'also': if (context) val.call(context, el); break;
|
|
113
118
|
case 'export': val[0] = el; break;
|
|
119
|
+
case 'push': val.push(el); break;
|
|
114
120
|
case 'var': if (context) context['$' + val] = el; break;
|
|
115
121
|
case 'events': for (let ev in val) if (val[ev]) el.addEventListener(ev, val[ev].bind(context)); break;
|
|
116
122
|
case 'parent': if (val instanceof Node || val instanceof DocumentFragment) val.append(el); break;
|
|
@@ -130,7 +136,7 @@ export class Component {
|
|
|
130
136
|
return el;
|
|
131
137
|
}
|
|
132
138
|
|
|
133
|
-
static
|
|
139
|
+
static configSVG(el, data) {
|
|
134
140
|
return Component.config(el, data, true);
|
|
135
141
|
}
|
|
136
142
|
|
|
@@ -139,12 +145,12 @@ export class Component {
|
|
|
139
145
|
* @param {array} arr массив объектов конфигурации
|
|
140
146
|
* @returns {array} of Elements
|
|
141
147
|
*/
|
|
142
|
-
static makeArray(arr,
|
|
148
|
+
static makeArray(arr, svg = false) {
|
|
143
149
|
if (!arr || !Array.isArray(arr)) return [];
|
|
144
|
-
return arr.map(x => Component.make(x.tag, x,
|
|
150
|
+
return arr.map(x => Component.make(x.tag, x, svg));
|
|
145
151
|
}
|
|
146
152
|
|
|
147
|
-
static
|
|
153
|
+
static makeArraySVG(arr) {
|
|
148
154
|
return Component.makeArray(arr, true);
|
|
149
155
|
}
|
|
150
156
|
}
|