@alexgyver/component 1.1.1 → 1.1.3

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.
Files changed (2) hide show
  1. package/Component.js +19 -12
  2. 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 = {}, ns = false) {
7
+ constructor(tag, data = {}, svg = false) {
8
8
  data.context = this;
9
- this.$root = Component.make(tag, data, ns);
9
+ this.$root = Component.make(tag, data, svg);
10
10
  }
11
11
 
12
12
  /**
@@ -32,13 +32,13 @@ export class Component {
32
32
  * child - DOM, Component, object, html string
33
33
  * всё остальное будет добавлено как property
34
34
  */
35
- static make(tag, data = {}, ns = false) {
35
+ static make(tag, data = {}, svg = false) {
36
36
  if (!tag || typeof data !== 'object') return null;
37
37
  if (data instanceof Node) return data;
38
- return Component.config(ns ? document.createElementNS("http://www.w3.org/2000/svg", tag) : document.createElement(tag), data, ns);
38
+ return Component.config(svg ? document.createElementNS("http://www.w3.org/2000/svg", tag) : document.createElement(tag), data, svg);
39
39
  }
40
40
 
41
- static makeNS(tag, data = {}) {
41
+ static makeSVG(tag, data = {}) {
42
42
  return Component.make(tag, data, true);
43
43
  }
44
44
 
@@ -76,12 +76,18 @@ export class Component {
76
76
  * Настроить элемент
77
77
  * @param {Node} el элемент
78
78
  * @param {object} data параметры
79
- * @param {object} ns NS
79
+ * @param {object} svg SVG
80
80
  * @returns {Node}
81
81
  */
82
- static config(el, data, ns = false) {
82
+ static config(el, data, svg = false) {
83
+ if (Array.isArray(el)) {
84
+ el.forEach(e => Component.config(e, data, svg));
85
+ return;
86
+ }
83
87
  if (!(el instanceof Node) || (typeof data !== 'object')) return el;
88
+
84
89
  const context = data.context;
90
+ if ('svg' in data) svg = data.svg;
85
91
 
86
92
  let addChild = (obj) => {
87
93
  if (!obj) return;
@@ -89,7 +95,7 @@ export class Component {
89
95
  else if (obj instanceof Component) el.appendChild(obj.$root);
90
96
  else if (typeof obj === 'object') {
91
97
  if (!obj.context) obj.context = context;
92
- let cmp = Component.make(obj.tag, obj, ns);
98
+ let cmp = Component.make(obj.tag, obj, svg);
93
99
  if (cmp) el.appendChild(cmp);
94
100
  } else if (typeof obj === 'string') {
95
101
  el.innerHTML += obj;
@@ -101,6 +107,7 @@ export class Component {
101
107
  switch (key) {
102
108
  case 'tag':
103
109
  case 'context':
110
+ case 'svg':
104
111
  continue;
105
112
  case 'text': el.textContent = val; break;
106
113
  case 'html': el.innerHTML = val; break;
@@ -126,7 +133,7 @@ export class Component {
126
133
  return el;
127
134
  }
128
135
 
129
- static configNS(el, data) {
136
+ static configSVG(el, data) {
130
137
  return Component.config(el, data, true);
131
138
  }
132
139
 
@@ -135,12 +142,12 @@ export class Component {
135
142
  * @param {array} arr массив объектов конфигурации
136
143
  * @returns {array} of Elements
137
144
  */
138
- static makeArray(arr, ns = false) {
145
+ static makeArray(arr, svg = false) {
139
146
  if (!arr || !Array.isArray(arr)) return [];
140
- return arr.map(x => Component.make(x.tag, x, ns));
147
+ return arr.map(x => Component.make(x.tag, x, svg));
141
148
  }
142
149
 
143
- static makeArrayNS(arr) {
150
+ static makeArraySVG(arr) {
144
151
  return Component.makeArray(arr, true);
145
152
  }
146
153
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@alexgyver/component",
3
- "version": "1.1.1",
3
+ "version": "1.1.3",
4
4
  "description": "Simple HTML element builder",
5
5
  "main": "./Component.js",
6
6
  "module": "./Component.js",