@alexgyver/component 1.3.5 → 1.3.7
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 +11 -3
- package/Component.min.js +1 -1
- package/README.md +11 -11
- package/package.json +1 -1
package/Component.js
CHANGED
|
@@ -74,7 +74,7 @@ export class EL {
|
|
|
74
74
|
}
|
|
75
75
|
|
|
76
76
|
for (const [key, val] of Object.entries(data)) {
|
|
77
|
-
if (
|
|
77
|
+
if (val === undefined || val === null) continue;
|
|
78
78
|
switch (key) {
|
|
79
79
|
case 'tag':
|
|
80
80
|
case 'context':
|
|
@@ -90,9 +90,9 @@ export class EL {
|
|
|
90
90
|
case 'parent': if (val) val.appendChild(el); break;
|
|
91
91
|
case 'attrs': for (let attr in val) el.setAttribute(attr, val[attr]); break;
|
|
92
92
|
case 'props': for (let prop in val) el[prop] = val[prop]; break;
|
|
93
|
-
case 'child_r':
|
|
93
|
+
case 'child_r': EL.clear(el); // fall
|
|
94
94
|
case 'child': addChild(val); break;
|
|
95
|
-
case 'children_r':
|
|
95
|
+
case 'children_r': EL.clear(el); // fall
|
|
96
96
|
case 'children': for (const obj of val) addChild(obj); break;
|
|
97
97
|
case 'style':
|
|
98
98
|
if (typeof val === 'string') el.style.cssText += (val + ';');
|
|
@@ -105,6 +105,14 @@ export class EL {
|
|
|
105
105
|
return el;
|
|
106
106
|
}
|
|
107
107
|
|
|
108
|
+
/**
|
|
109
|
+
* Удалить все child ноды
|
|
110
|
+
* @param {HTMLElement} el
|
|
111
|
+
*/
|
|
112
|
+
static clear(el) {
|
|
113
|
+
while (el.firstChild) el.removeChild(el.lastChild);
|
|
114
|
+
}
|
|
115
|
+
|
|
108
116
|
/**
|
|
109
117
|
* Создать массив компонентов из массива объектов конфигурации
|
|
110
118
|
* @param {array} arr массив объектов конфигурации
|
package/Component.min.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
var e={d:(t,a)=>{for(var c in a)e.o(a,c)&&!e.o(t,c)&&Object.defineProperty(t,c,{enumerable:!0,get:a[c]})},o:(e,t)=>Object.prototype.hasOwnProperty.call(e,t)},t={};e.d(t,{EL:()=>a,LV:()=>r,cj:()=>
|
|
1
|
+
var e={d:(t,a)=>{for(var c in a)e.o(a,c)&&!e.o(t,c)&&Object.defineProperty(t,c,{enumerable:!0,get:a[c]})},o:(e,t)=>Object.prototype.hasOwnProperty.call(e,t)},t={};e.d(t,{EL:()=>a,LV:()=>r,cj:()=>n,t4:()=>s,uA:()=>c});class a{static ctx;constructor(e,t={},c=!1){a.ctx=this,this.$root=a.make(e,t,c)}static make(e,t={},c=!1){return e&&"object"==typeof t?t instanceof Node?t:("svg"==e&&(c=!0),a.config(c?document.createElementNS("http://www.w3.org/2000/svg",e):document.createElement(e),t,c)):null}static config(e,t,c=!1){if(Array.isArray(e))return e.forEach((e=>a.config(e,t,c))),null;if(!(e instanceof Node)||"object"!=typeof t)return e;let s=t.context;a.ctx=null===s?null:s||a.ctx,s=a.ctx;let n=t=>{if(t)if(t instanceof Node)e.appendChild(t);else if(t instanceof a)e.appendChild(t.$root);else if("string"==typeof t)e.innerHTML+=t;else if("object"==typeof t){let s=a.make(t.tag??"div",t,c||"svg"==t.tag);s&&e.appendChild(s)}};for(const[c,r]of Object.entries(t))if(null!=r)switch(c){case"tag":case"context":case"get":case"also":continue;case"text":e.textContent=r+"";break;case"html":e.innerHTML=r;break;case"class":(Array.isArray(r)?r:r.split(" ")).map((t=>t&&e.classList.add(t)));break;case"push":r.push(e);break;case"var":s&&(s["$"+r]=e);break;case"events":for(let t in r)e.addEventListener(t,r[t].bind(s));break;case"parent":r&&r.appendChild(e);break;case"attrs":for(let t in r)e.setAttribute(t,r[t]);break;case"props":for(let t in r)e[t]=r[t];break;case"child_r":a.clear(e);case"child":n(r);break;case"children_r":a.clear(e);case"children":for(const e of r)n(e);break;case"style":if("string"==typeof r)e.style.cssText+=r+";";else for(let t in r)e.style[t]=r[t];break;default:e[c]=r}return t.also&&s&&t.also.call(s,e),e}static clear(e){for(;e.firstChild;)e.removeChild(e.lastChild)}static makeArray(e,t=!1){return e&&Array.isArray(e)?e.map((e=>a.make(e.tag,e,t))):[]}static makeShadow(e,t={},c=null){if(!e||"object"!=typeof t)return null;let s=e instanceof Node?e:document.createElement(e);return s.attachShadow({mode:"open"}),a.config(s.shadowRoot,{context:t.context,children:[{tag:"style",textContent:c??""},t.child??{},...t.children??[]]}),delete t.children,delete t.child,a.config(s,t),s}}const c=a;class s{static make=(e,t)=>a.make(e,t,!0);static config=(e,t)=>a.config(e,t,!0);static makeArray=e=>a.makeArray(e,!0);static svg=(e={},t={})=>s._make("svg",e,t);static rect=(e,t,a,c,n,r,o={},l={})=>s._make("rect",{...o,x:e,y:t,width:a,height:c,rx:n,ry:r},l);static circle=(e,t,a,c={},n={})=>s._make("circle",{...c,cx:e,cy:t,r:a},n);static line=(e,t,a,c,n={},r={})=>s._make("line",{...n,x1:e,y1:t,x2:a,y2:c},r);static polyline=(e,t={},a={})=>s._make("polyline",{...t,points:e},a);static polygon=(e,t={},a={})=>s._make("polygon",{...t,points:e},a);static path=(e,t={},a={})=>s._make("path",{...t,d:e},a);static text=(e,t,a,c={},n={})=>s._make("text",{...c,x:t,y:a},{...n,text:e});static _make=(e,t={},a={})=>s.make(e,{attrs:{...t},...a})}class n{static addStyle(e,t,a=!1){if(e&&t&&("object"==typeof t&&(t=t.constructor.name),!n.#e.has(t)&&!n.#t.has(t)))if(a){let a=document.createElement("style");document.head.appendChild(a),a.textContent=e,n.#t.set(t,a)}else n.#a||(n.#a=document.head.appendChild(document.createElement("style"))),n.#a.textContent+=e+"\r\n",n.#e.add(t)}static removeStyle(e){"object"==typeof e&&(e=e.constructor.name),n.#t.has(e)&&(n.#t.get(e).remove(),n.#t.delete(e))}static#a=null;static#e=new Set;static#t=new Map}class r extends a{constructor(e,t={},a=null,c=null,s=!1){super(e,t),n.addStyle(a,c,s)}static make(e,t={},c=null,s=null,r=!1){return n.addStyle(c,s,r),a.make(e,t)}}const o=t.uA,l=t.EL,i=t.t4,d=t.cj,p=t.LV;export{o as Component,l as EL,i as SVG,d as Sheet,p as StyledComponent};
|
package/README.md
CHANGED
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
* @param {string} tag html tag элемента
|
|
12
12
|
* @param {object} data параметры
|
|
13
13
|
*/
|
|
14
|
-
|
|
14
|
+
EL(tag, data = {}, svg = false);
|
|
15
15
|
|
|
16
16
|
/**
|
|
17
17
|
* Создать компонент
|
|
@@ -28,14 +28,14 @@ Component(tag, data = {}, svg = false);
|
|
|
28
28
|
* push {array} - добавить к указанному массиву
|
|
29
29
|
* var {string} - создаёт переменную $имя в указанном контексте
|
|
30
30
|
* events {object} - добавляет addEventListener'ы {event: e => {}}
|
|
31
|
-
* children/children_r - массив {DOM,
|
|
32
|
-
* child/child_r - {DOM,
|
|
31
|
+
* children/children_r - массив {DOM, EL, object, html string}. _r - заменить имеющиеся. Без тега tag будет div
|
|
32
|
+
* child/child_r - {DOM, EL, object, html string}. _r - заменить имеющиеся. Без тега tag будет div
|
|
33
33
|
* attrs {object} - добавить аттрибуты (через setAttribute)
|
|
34
34
|
* props {object} - добавить свойства (как el[prop])
|
|
35
35
|
* also {function} - вызвать с текущим компонентом: also(el) { console.log(el); }
|
|
36
36
|
* всё остальное будет добавлено как property
|
|
37
37
|
*/
|
|
38
|
-
|
|
38
|
+
EL.make(tag, data = {});
|
|
39
39
|
|
|
40
40
|
/**
|
|
41
41
|
* Создать теневой компонент от указанного tag, дети подключатся к нему в shadowRoot
|
|
@@ -44,7 +44,7 @@ Component.make(tag, data = {});
|
|
|
44
44
|
* @param {string} sheet css стили
|
|
45
45
|
* @returns {Node} host
|
|
46
46
|
*/
|
|
47
|
-
|
|
47
|
+
EL.makeShadow(host, data = {}, sheet = null);
|
|
48
48
|
|
|
49
49
|
/**
|
|
50
50
|
* Настроить элемент
|
|
@@ -52,14 +52,14 @@ Component.makeShadow(host, data = {}, sheet = null);
|
|
|
52
52
|
* @param {object} data параметры
|
|
53
53
|
* @returns {Node}
|
|
54
54
|
*/
|
|
55
|
-
|
|
55
|
+
EL.config(el, data);
|
|
56
56
|
|
|
57
57
|
/**
|
|
58
58
|
* Создать массив компонентов из массива объектов конфигурации
|
|
59
59
|
* @param {array} arr массив объектов конфигурации
|
|
60
60
|
* @returns {array} of Elements
|
|
61
61
|
*/
|
|
62
|
-
|
|
62
|
+
EL.makeArray(arr);
|
|
63
63
|
```
|
|
64
64
|
|
|
65
65
|
### SVG
|
|
@@ -121,7 +121,7 @@ StyledComponent.make(tag, data = {}, style = null, id = null, ext = false);
|
|
|
121
121
|
## Пример
|
|
122
122
|
Создаст контейнер с двумя вложенными блоками текста и прикрепит к body
|
|
123
123
|
```js
|
|
124
|
-
|
|
124
|
+
EL.make('div', {
|
|
125
125
|
parent: document.body,
|
|
126
126
|
class: 'my-div',
|
|
127
127
|
style: {
|
|
@@ -148,7 +148,7 @@ Component.make('div', {
|
|
|
148
148
|
```js
|
|
149
149
|
class Button {
|
|
150
150
|
constructor(text) {
|
|
151
|
-
|
|
151
|
+
EL.make('button', {
|
|
152
152
|
context: this,
|
|
153
153
|
var: 'button',
|
|
154
154
|
text: text,
|
|
@@ -169,7 +169,7 @@ btn.$button; // элемент кнопки
|
|
|
169
169
|
Некоторые трюки
|
|
170
170
|
|
|
171
171
|
```js
|
|
172
|
-
|
|
172
|
+
EL.make('div', {
|
|
173
173
|
context: this,
|
|
174
174
|
children: [
|
|
175
175
|
{}, // валидно
|
|
@@ -177,7 +177,7 @@ Component.make('div', {
|
|
|
177
177
|
{
|
|
178
178
|
// без тега - div
|
|
179
179
|
},
|
|
180
|
-
|
|
180
|
+
EL.make(...), // контекст будет проброшен сюда автоматически
|
|
181
181
|
foo && {...}, // добавить компонент если foo - true
|
|
182
182
|
{
|
|
183
183
|
tag: 'svg', // автоматически запустится режим SVG
|