-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShadowHostElement.js
More file actions
238 lines (207 loc) · 6.28 KB
/
ShadowHostElement.js
File metadata and controls
238 lines (207 loc) · 6.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
const deepClone = data => JSON.parse(JSON.stringify(data));
const getRandomInteger = (min = 0, max = 9) => {
const math = Math.random();
const mathI = math * (max - min);
const floor = Math.floor(mathI);
const minMFR = floor + min;
return minMFR;
};
const createEvent = ({
name = "custom event",
detail = {},
eventParams = {},
target = window
}) => {
const eventDetail = {
bubbles: true,
cancelable: true,
composed: true,
detail: { ...detail, target: target },
...eventParams,
};
const newEvent = new CustomEvent(name, eventDetail);
target.dispatchEvent(newEvent);
};
// # ELEMENT #
const getParentEl = (el, int = 1) => {
let parentEl = el.parentElement;
for (; int > 1; int--) parentEl = getParentEl(parentEl);
return parentEl;
};
const clearContainerEl = containerEl => {
[...containerEl.children].forEach(el => {
if (!(el instanceof HTMLTemplateElement)) el.remove();
});
};
const makeCloneTemplate = inner => {
const tempEl = document.createElement("template");
tempEl.innerHTML = inner;
return tempEl.content.cloneNode(true);
};
// # FILE #
const makeFileUrl = fileInner => {
const text = JSON.stringify(fileInner);
const mimeType = "application/json" || "text/plain";
const file = new Blob([text], { type: mimeType });
const fileUrl = URL.createObjectURL(file);
URL.revokeObjectURL(File);
return fileUrl;
};
// # CASE #
const unCaseString = string => {
string = string.trim();
string = string.replace(/-|_/g, ' ');
string = string.replace(/[A-Z]/g, match => ` ${ match.toLowerCase() }`);
return string.trim();
};
// camelCase
const toCamelCase = string => {
string = unCaseString(string);
string = string.replace(/\s\w/g, match => match[1].toUpperCase());
return string;
};
// snake_case
const toSnakeCase = string => {
string = unCaseString(string);
string = string.replace(/\s\w/g, match => `_${ match[1] }`);
return string;
};
// kebab-case
const toKebabCase = string => {
string = unCaseString(string);
string = string.replace(/\s\w/g, match => `-${ match[1] }`);
return string;
};
// PascalCase
const toPascalCase = string => {
string = unCaseString(string);
const str = string.replace(/\s\w/g, match => match[1].toUpperCase());
const [head, tail] = [str.charAt(0).toUpperCase(), str.slice(1, str.length)];
return head + tail;
};
// UPPER_CASE_SNAKE_CASE
const toUpperSnakeCase = string => {
string = unCaseString(string);
string = string.replace(/\s\w/g, match => `_${ match[1] }`);
return string.toUpperCase();
};
/**
* Castom Component Base Class Element
*/
class ShadowHostElement extends HTMLElement {
constructor() {
super();
const html = this.html || '';
const css = `<style>:host{display:block;box-sizing:border-box}${ this.css || '' }</style>`;
this.attachShadow({ mode: "open" });
this.shadowRoot.append(makeCloneTemplate(html), makeCloneTemplate(css));
this.getEls && this.getEls.forEach(selector => this.#getElsBySelector(selector));
this.props && Object.keys(this.props).forEach(propName => this.#setComponentsProps(propName));
}
static get nameIs() {
return toKebabCase(this.prototype.constructor.name)
}
#getElsBySelector(selector) {
const isStr = typeof selector === "string";
const isArr = selector instanceof Array;
const isEmpty = isArr && selector.length === 0;
const isCorrectArr = !isEmpty && typeof selector[0] === "string";
if (!isStr && !isCorrectArr) {
const massege = 'This.getEls only <<str>> || <<[str]>>';
return console.error(massege, selector, this);
}
const toCamelCaseSelector = selector => toCamelCase(
(!selector[0] === "." && !selector[0] === "#")
? selector
: selector.slice(1));
const argumentName = isArr
? `${toCamelCaseSelector(selector[0])}Els`
: `${toCamelCaseSelector(selector)}El`;
const makeArgument = (name, func) => Object.defineProperty(
this,
name,
{ get: () => func, configurable: true, });
isArr
? makeArgument(argumentName, [...this.shadowRoot.querySelectorAll(selector[0])])
: makeArgument(argumentName, this.shadowRoot.querySelector(selector));
}
#setComponentsProps(propertyName) {
const property = this.props[propertyName];
let oldValue;
if (this.hasOwnProperty(propertyName)) {
oldValue = this[propertyName];
}
["transformer", "observer"].forEach( functionName => {
if (!property.hasOwnProperty(functionName)) return;
const isFunction = property[functionName] instanceof Function;
let func;
if (!isFunction) {
const massege = `Property can only be function.`;
console.error(massege, this, propertyName);
return (property[functionName] = ({ val }) => val);
}
property[functionName] = isFunction ? property[functionName] : func;
property[functionName] = property[functionName].bind(this);
});
const shadowPropName = `_${ propertyName }`;
Object.defineProperty(this, propertyName, {
enumerable: true,
configurable: false,
set: value => {
const oldValue = this[shadowPropName];
if (value === oldValue) return value;
if (property.hasOwnProperty("transformer")) {
value = property.transformer({ value, oldValue, [propertyName]: value });
}
if (value === oldValue) return value;
this[shadowPropName] = value;
if (property.hasOwnProperty("observer")) {
property.observer({ value, oldValue, [propertyName]: value });
}
return value;
},
get: () => {
if (property.hasOwnProperty("getter") && property.getter instanceof Function)
return property.getter.bind(this)();
return this[shadowPropName];
},
});
this[propertyName] = (() => {
if (oldValue !== undefined) return oldValue;
if (
this.constructor.observedAttributes &&
this.constructor.observedAttributes.includes(propertyName)
) {
if (this.hasAttribute(propertyName)) {
return this.getAttribute(propertyName) || true;
}
}
if (property.hasOwnProperty("default")) {
return property.default;
}
})();
}
emitEvent(name = 'custom event', detail = {}, params = {}) {
createEvent({ target: this, name, detail, params });
}
clearContainerEl(containerEl) {
clearContainerEl(containerEl);
}
}
window.ShadowHostElement = ShadowHostElement;
export {
deepClone,
getRandomInteger,
createEvent,
getParentEl,
clearContainerEl,
makeCloneTemplate,
makeFileUrl,
unCaseString,
toCamelCase,
toSnakeCase,
toKebabCase,
toPascalCase,
toUpperSnakeCase,
ShadowHostElement
}