@alexgyver/component 1.0.24 → 1.0.26
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 +51 -16
- package/example/script.js +24 -0
- package/package.json +1 -1
package/component.js
CHANGED
|
@@ -24,6 +24,7 @@ export class Component {
|
|
|
24
24
|
parent - {Element} добавляет компонент к указанному элементу (имеет смысл только для корневого компонента)
|
|
25
25
|
style {string | object} объект в виде { padding: '0px', ... } или строка css стилей
|
|
26
26
|
children - массив DOM, Component, object, html string
|
|
27
|
+
child - DOM, Component, object, html string
|
|
27
28
|
всё остальное будет добавлено как property
|
|
28
29
|
*/
|
|
29
30
|
/**
|
|
@@ -38,6 +39,36 @@ export class Component {
|
|
|
38
39
|
return Component.config(document.createElement(tag), data);
|
|
39
40
|
}
|
|
40
41
|
|
|
42
|
+
/**
|
|
43
|
+
* Создать теневой компонент от указанного tag, дети подключатся к нему в shadowRoot
|
|
44
|
+
* @param {string|Node} host html tag теневого элемента или Node
|
|
45
|
+
* @param {object} data параметры внешнего элемента
|
|
46
|
+
* @param {string} sheet css стили
|
|
47
|
+
* @returns {Node} host
|
|
48
|
+
*/
|
|
49
|
+
static makeShadow(host, data = {}, sheet = null) {
|
|
50
|
+
if (!host || typeof data !== 'object') return null;
|
|
51
|
+
|
|
52
|
+
let $host = (host instanceof Node) ? host : document.createElement(host);
|
|
53
|
+
$host.attachShadow({ mode: 'open' });
|
|
54
|
+
|
|
55
|
+
Component.config($host.shadowRoot, {
|
|
56
|
+
context: data.context,
|
|
57
|
+
children: [
|
|
58
|
+
{
|
|
59
|
+
tag: 'style',
|
|
60
|
+
textContent: sheet ?? '',
|
|
61
|
+
},
|
|
62
|
+
data.child ?? {},
|
|
63
|
+
...(data.children ?? []),
|
|
64
|
+
]
|
|
65
|
+
});
|
|
66
|
+
delete data.children;
|
|
67
|
+
delete data.child;
|
|
68
|
+
Component.config($host, data);
|
|
69
|
+
return $host;
|
|
70
|
+
}
|
|
71
|
+
|
|
41
72
|
/**
|
|
42
73
|
* Настроить элемент
|
|
43
74
|
* @param {Node} el элемент
|
|
@@ -45,9 +76,24 @@ export class Component {
|
|
|
45
76
|
* @returns {Node}
|
|
46
77
|
*/
|
|
47
78
|
static config(el, data) {
|
|
48
|
-
if (!(el instanceof Node)) return el;
|
|
79
|
+
if (!(el instanceof Node) || (typeof data !== 'object')) return el;
|
|
49
80
|
const context = data.context;
|
|
81
|
+
|
|
82
|
+
let addChild = (obj) => {
|
|
83
|
+
if (!obj) return;
|
|
84
|
+
if (obj instanceof Node) el.appendChild(obj);
|
|
85
|
+
else if (obj instanceof Component) el.appendChild(obj.$root);
|
|
86
|
+
else if (typeof obj === 'object') {
|
|
87
|
+
if (!obj.context) obj.context = context;
|
|
88
|
+
let cmp = Component.make(obj.tag, obj);
|
|
89
|
+
if (cmp) el.appendChild(cmp);
|
|
90
|
+
} else if (typeof obj === 'string') {
|
|
91
|
+
el.innerHTML += obj;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
50
95
|
for (const [key, val] of Object.entries(data)) {
|
|
96
|
+
if (!val) continue;
|
|
51
97
|
switch (key) {
|
|
52
98
|
case 'tag':
|
|
53
99
|
case 'context':
|
|
@@ -59,27 +105,15 @@ export class Component {
|
|
|
59
105
|
case 'export': val[0] = el; break;
|
|
60
106
|
case 'var': if (context) context['$' + val] = el; break;
|
|
61
107
|
case 'events': for (let ev in val) if (val[ev]) el.addEventListener(ev, val[ev].bind(context)); break;
|
|
62
|
-
case 'parent': if (val instanceof
|
|
108
|
+
case 'parent': if (val instanceof Node || val instanceof DocumentFragment) val.append(el); break;
|
|
63
109
|
case 'attrs': for (let attr in val) el.setAttribute(attr, val[attr]); break;
|
|
64
110
|
case 'props': for (let prop in val) el[prop] = val[prop]; break;
|
|
111
|
+
case 'child': addChild(val); break;
|
|
112
|
+
case 'children': for (const obj of val) addChild(obj); break;
|
|
65
113
|
case 'style':
|
|
66
114
|
if (typeof val === 'string') el.style.cssText += (val + ';');
|
|
67
115
|
else for (let st in val) el.style[st] = val[st];
|
|
68
116
|
break;
|
|
69
|
-
case 'children':
|
|
70
|
-
for (const obj of val) {
|
|
71
|
-
if (!obj) continue;
|
|
72
|
-
if (obj instanceof Node) el.appendChild(obj);
|
|
73
|
-
else if (obj instanceof Component) el.appendChild(obj.$root);
|
|
74
|
-
else if (typeof obj === 'object') {
|
|
75
|
-
if (!obj.context) obj.context = context;
|
|
76
|
-
let cmp = Component.make(obj.tag, obj);
|
|
77
|
-
if (cmp) el.appendChild(cmp);
|
|
78
|
-
} else if (typeof obj === 'string') {
|
|
79
|
-
el.innerHTML += obj;
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
break;
|
|
83
117
|
default: el[key] = val; break;
|
|
84
118
|
}
|
|
85
119
|
}
|
|
@@ -92,6 +126,7 @@ export class Component {
|
|
|
92
126
|
* @returns {array} of Elements
|
|
93
127
|
*/
|
|
94
128
|
static makeArray(arr) {
|
|
129
|
+
if (!arr || !Array.isArray(arr)) return [];
|
|
95
130
|
return arr.map(x => Component.make(x.tag, x));
|
|
96
131
|
}
|
|
97
132
|
}
|
package/example/script.js
CHANGED
|
@@ -87,6 +87,30 @@ function Container(children) {
|
|
|
87
87
|
});
|
|
88
88
|
}
|
|
89
89
|
|
|
90
|
+
class ShadowComponent {
|
|
91
|
+
constructor() {
|
|
92
|
+
Component.makeShadow('div', {
|
|
93
|
+
context: this,
|
|
94
|
+
parent: document.body,
|
|
95
|
+
events: {
|
|
96
|
+
click: () => this.$div.dispatchEvent(new Event('kek', { bubbles: true, composed: true })),
|
|
97
|
+
},
|
|
98
|
+
children: [
|
|
99
|
+
{
|
|
100
|
+
tag: 'div',
|
|
101
|
+
text: 'Hello!',
|
|
102
|
+
class: 'myclass',
|
|
103
|
+
var: 'div',
|
|
104
|
+
}
|
|
105
|
+
]
|
|
106
|
+
}, '.myclass{color:red;}'
|
|
107
|
+
);
|
|
108
|
+
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
document.addEventListener('kek', () => console.log('kek!'));
|
|
113
|
+
|
|
90
114
|
document.addEventListener("DOMContentLoaded", () => {
|
|
91
115
|
Component.make('h1', {
|
|
92
116
|
text: 'Hello!',
|