8000 upd · GyverLibs/Component.js@ede4de1 · GitHub
[go: up one dir, main page]

Skip to content

Commit ede4de1

Browse files
committed
upd
1 parent c1dcc29 commit ede4de1

File tree

5 files changed

+41
-38
lines changed

5 files changed

+41
-38
lines changed

Component.js

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
//#region Component
2-
export class Component {
1+
//#region EL
2+
export class EL {
33
static ctx;
44

55
/**
@@ -9,8 +9,8 @@ export class Component {
99
* @param {Boolean} svg SVG
1010
*/
1111
constructor(tag, data = {}, svg = false) {
12-
Component.ctx = this;
13-
this.$root = Component.make(tag, data, svg);
12+
EL.ctx = this;
13+
this.$root = EL.make(tag, data, svg);
1414
}
1515

1616
/**
@@ -30,8 +30,8 @@ export class Component {
3030
* push {array} - добавить к указанному массиву
3131
* var {string} - создаёт переменную $имя в указанном контексте
3232
* 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
33+
* children/children_r - массив {DOM, EL, object, html string}. _r - заменить имеющиеся. Без тега tag будет div
34+
* child/child_r - {DOM, EL, object, html string}. _r - заменить имеющиеся. Без тега tag будет div
3535
* attrs {object} - добавить аттрибуты (через setAttribute)
3636
* props {object} - добавить свойства (как el[prop])
3737
* also {function} - вызвать с текущим компонентом: also(el) { console.log(el); }
@@ -41,7 +41,7 @@ export class Component {
4141
if (!tag || typeof data !== 'object') return null;
4242
if (data instanceof Node) return data;
4343
if (tag == 'svg') svg = true;
44-
return Component.config(svg ? document.createElementNS("http://www.w3.org/2000/svg", tag) : document.createElement(tag), data, svg);
44+
return EL.config(svg ? document.createElementNS("http://www.w3.org/2000/svg", tag) : document.createElement(tag), data, svg);
4545
}
4646

4747
/**
@@ -53,22 +53,22 @@ export class Component {
5353
*/
5454
static config(el, data, svg = false) {
5555
if (Array.isArray(el)) {
56-
el.forEach(e => Component.config(e, data, svg));
56+
el.forEach(e => EL.config(e, data, svg));
5757
return null;
5858
}
5959
if (!(el instanceof Node) || (typeof data !== 'object')) return el;
6060

6161
let ctx = data.context;
62-
Component.ctx = (ctx === null) ? null : (ctx ? ctx : Component.ctx);
63-
ctx = Component.ctx;
62+
EL.ctx = (ctx === null) ? null : (ctx ? ctx : EL.ctx);
63+
ctx = EL.ctx;
6464

6565
let addChild = (obj) => {
6666
if (!obj) return;
6767
if (obj instanceof Node) el.appendChild(obj);
68-
else if (obj instanceof Component) el.appendChild(obj.$root);
68+
else if (obj instanceof EL) el.appendChild(obj.$root);
6969
else if (typeof obj === 'string') el.innerHTML += obj;
7070
else if (typeof obj === 'object') {
71-
let cmp = Component.make(obj.tag ?? 'div', obj, svg || obj.tag == 'svg');
71+
let cmp = EL.make(obj.tag ?? 'div', obj, svg || obj.tag == 'svg');
7272
if (cmp) el.appendChild(cmp);
7373
}
7474
}
@@ -113,7 +113,7 @@ export class Component {
113113
*/
114114
static makeArray(arr, svg = false) {
115115
if (!arr || !Array.isArray(arr)) return [];
116-
return arr.map(x => Component.make(x.tag, x, svg));
116+
return arr.map(x => EL.make(x.tag, x, svg));
117117
}
118118

119119
/**
@@ -129,7 +129,7 @@ export class Component {
129129
let $host = (host instanceof Node) ? host : document.createElement(host);
130130
$host.attachShadow({ mode: 'open' });
131131

132-
Component.config($host.shadowRoot, {
132+
EL.config($host.shadowRoot, {
133133
context: data.context,
134134
children: [
135135
{
@@ -142,16 +142,19 @@ export class Component {
142142
});
143143
delete data.children;
144144
delete data.child;
145-
Component.config($host, data);
145+
EL.config($host, data);
146146
return $host;
147147
}
148148
}
149149

150+
// legacy
151+
export const Component = EL;
152+
150153
//#region SVG
151154
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+
static make = (tag, data) => EL.make(tag, data, true);
156+
static config = (el, data) => EL.config(el, data, true);
157+
static makeArray = (arr) => EL.makeArray(arr, true);
155158

156159
static svg = (attrs = {}, props = {}) => SVG._make('svg', attrs, props);
157160
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);
@@ -209,7 +212,7 @@ export class Sheet {
209212
}
210213

211214
//#region StyledComponent
212-
export class StyledComponent extends Component {
215+
export class StyledComponent extends EL {
213216
/**
214217
* Создать компонент и поместить его в переменную $root
215218
* @param {string} tag html tag элемента
@@ -233,6 +236,6 @@ export class StyledComponent extends Component {
233236
*/
234237
static make(tag, data = {}, style = null, id = null, ext = false) {
235238
Sheet.addStyle(style, id, ext);
236-
return Component.make(tag, data);
239+
return EL.make(tag, data);
237240
}
238241
}

Component.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
* @param {string} tag html tag элемента
1212
* @param {object} data параметры
1313
*/
14-
Component(tag, data = {}, svg = false);
14+
EL(tag, data = {}, svg = false);
1515

1616
/**
1717
* Создать компонент
@@ -28,14 +28,14 @@ Component(tag, data = {}, svg = false);
2828
* push {array} - добавить к указанному массиву
2929
* var {string} - создаёт переменную $имя в указанном контексте
3030
* 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
31+
* children/children_r - массив {DOM, EL, object, html string}. _r - заменить имеющиеся. Без тега tag будет div
32+
* child/child_r - {DOM, EL, object, html string}. _r - заменить имеющиеся. Без тега tag будет div
3333
* attrs {object} - добавить аттрибуты (через setAttribute)
3434
* props {object} - добавить свойства (как el[prop])
3535
* also {function} - вызвать с текущим компонентом: also(el) { console.log(el); }
3636
* всё остальное будет добавлено как property
3737
*/
38-
Component.make(tag, data = {});
38+
EL.make(tag, data = {});
3939

4040
/**
4141
* Создать теневой компонент от указанного tag, дети подключатся к нему в shadowRoot
@@ -44,22 +44,22 @@ Component.make(tag, data = {});
4444
* @param {string} sheet css стили
4545
* @returns {Node} host
4646
*/
47-
Component.makeShadow(host, data = {}, sheet = null);
47+
EL.makeShadow(host, data = {}, sheet = null);
4848

4949
/**
5050
* Настроить элемент
5151
* @param {Node | Array} el элемент или массив элементов
5252
* @param {object} data параметры
5353
* @returns {Node}
5454
*/
55-
Component.config(el, data);
55+
EL.config(el, data);
5656

5757
/**
5858
* Создать массив компонентов из массива объектов конфигурации
5959
* @param {array} arr массив объектов конфигурации
6060
* @returns {array} of Elements
6161
*/
62-
Component.makeArray(arr);
62+
EL.makeArray(arr);
6363
```
6464

6565
### SVG
@@ -121,7 +121,7 @@ StyledComponent.make(tag, data = {}, style = null, id = null, ext = false);
121121
## Пример
122122
Создаст контейнер с двумя вложенными блоками текста и прикрепит к body
123123
```js
124-
Component.make('div', {
124+
EL.make('div', {
125125
parent: document.body,
126126
class: 'my-div',
127127
style: {
@@ -148,7 +148,7 @@ Component.make('div', {
148148
```js
149149
class Button {
150150
constructor(text) {
151-
Component.make('button', {
151+
EL.make('button', {
152152
context: this,
153153
var: 'button',
154154
text: text,
@@ -169,15 +169,15 @@ btn.$button; // элемент кнопки
169169
Некоторые трюки
170170

171171
```js
172-
Component.make('div', {
172+
EL.make('div', {
173173
context: this,
174174
children: [
175175
{}, // валидно
176176
null, // валидно
177177
{
178178
// без тега - div
179179
},
180-
Component.make(...), // контекст будет проброшен сюда автоматически
180+
EL.make(...), // контекст будет проброшен сюда автоматически
181181
foo && {...}, // добавить компонент если foo - true
182182
{
183183
tag: 'svg', // автоматически запустится режим SVG

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@alexgyver/component",
3-
"version": "1.3.4",
3+
"version": "1.3.5",
44
"description": "Simple HTML&SVG element builder",
55
"main": "./Component.js",
66
"module": "./Component.js",

test/script.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { Component, Sheet, StyledComponent } from "https://gyverlibs.github.io/Component.js/Component.min.js";
1+
import { EL, Sheet, StyledComponent } from "https://gyverlibs.github.io/EL.js/EL.min.js";
22

33
// кнопка наследует, стили добавляются отдельно
4-
class Button extends Component {
4+
class Button extends EL {
55
constructor(text, handler) {
66

77
super('button', {
@@ -75,15 +75,15 @@ function Checkbox(name) {
7575
}
7676

7777
function Container(children) {
78-
return Component.make('div',
78+
return EL.make('div',
7979
{
8080
children: children,
8181
});
8282
}
8383

8484
class ShadowComponent {
8585
constructor() {
86-
Component.makeShadow('div', {
86+
EL.makeShadow('div', {
8787
context: this,
8888
parent: document.body,
8989
events: {
@@ -106,7 +106,7 @@ class ShadowComponent {
106106
document.addEventListener('kek', () => console.log('kek!'));
107107

108108
document.addEventListener("DOMContentLoaded", () => {
109-
Component.make('h1', {
109+
EL.make('h1', {
110110
text: 'Hello!',
111111
parent: document.body,
112112
});

0 commit comments

Comments
 (0)
0