1
- //#region Component
2
- export class Component {
1
+ //#region EL
2
+ export class EL {
3
3
static ctx ;
4
4
5
5
/**
@@ -9,8 +9,8 @@ export class Component {
9
9
* @param {Boolean } svg SVG
10
10
*/
11
11
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 ) ;
14
14
}
15
15
16
16
/**
@@ -30,8 +30,8 @@ export class Component {
30
30
* push {array} - добавить к указанному массиву
31
31
* var {string} - создаёт переменную $имя в указанном контексте
32
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
33
+ * children/children_r - массив {DOM, EL , object, html string}. _r - заменить имеющиеся. Без тега tag будет div
34
+ * child/child_r - {DOM, EL , object, html string}. _r - заменить имеющиеся. Без тега tag будет div
35
35
* attrs {object} - добавить аттрибуты (через setAttribute)
36
36
* props {object} - добавить свойства (как el[prop])
37
37
* also {function} - вызвать с текущим компонентом: also(el) { console.log(el); }
@@ -41,7 +41,7 @@ export class Component {
41
41
if ( ! tag || typeof data !== 'object' ) return null ;
42
42
if ( data instanceof Node ) return data ;
43
43
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 ) ;
45
45
}
46
46
47
47
/**
@@ -53,22 +53,22 @@ export class Component {
53
53
*/
54
54
static config ( el , data , svg = false ) {
55
55
if ( Array . isArray ( el ) ) {
56
- el . forEach ( e => Component . config ( e , data , svg ) ) ;
56
+ el . forEach ( e => EL . config ( e , data , svg ) ) ;
57
57
return null ;
58
58
}
59
59
if ( ! ( el instanceof Node ) || ( typeof data !== 'object' ) ) return el ;
60
60
61
61
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 ;
64
64
65
65
let addChild = ( obj ) => {
66
66
if ( ! obj ) return ;
67
67
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 ) ;
69
69
else if ( typeof obj === 'string' ) el . innerHTML += obj ;
70
70
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' ) ;
72
72
if ( cmp ) el . appendChild ( cmp ) ;
73
73
}
74
74
}
@@ -113,7 +113,7 @@ export class Component {
113
113
*/
114
114
static makeArray ( arr , svg = false ) {
115
115
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 ) ) ;
117
117
}
118
118
119
119
/**
@@ -129,7 +129,7 @@ export class Component {
129
129
let $host = ( host instanceof Node ) ? host : document . createElement ( host ) ;
130
130
$host . attachShadow ( { mode : 'open' } ) ;
131
131
132
- Component . config ( $host . shadowRoot , {
132
+ EL . config ( $host . shadowRoot , {
133
133
context : data . context ,
134
134
children : [
135
135
{
@@ -142,16 +142,19 @@ export class Component {
142
142
} ) ;
143
143
delete data . children ;
144
144
delete data . child ;
145
- Component . config ( $host , data ) ;
145
+ EL . config ( $host , data ) ;
146
146
return $host ;
147
147
}
148
148
}
149
149
150
+ // legacy
151
+ export const Component = EL ;
152
+
150
153
//#region SVG
151
154
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 ) ;
155
158
156
159
static svg = ( attrs = { } , props = { } ) => SVG . _make ( 'svg' , attrs , props ) ;
157
160
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 {
209
212
}
210
213
211
214
//#region StyledComponent
212
- export class StyledComponent extends Component {
215
+ export class StyledComponent extends EL {
213
216
/**
214
217
* Создать компонент и поместить его в переменную $root
215
218
* @param {string } tag html tag элемента
@@ -233,6 +236,6 @@ export class StyledComponent extends Component {
233
236
*/
234
237
static make ( tag , data = { } , style = null , id = null , ext = false ) {
235
238
Sheet . addStyle ( style , id , ext ) ;
236
- return Component . make ( tag , data ) ;
239
+ return EL . make ( tag , data ) ;
237
240
}
238
241
}
0 commit comments