-
Notifications
You must be signed in to change notification settings - Fork 0
/
DomUtils.ts
280 lines (249 loc) · 8.35 KB
/
DomUtils.ts
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
import { DomGet } from "../dom/dom-get";
import { NotBrowserException } from "../errors";
import { SimpleVector2 } from "../math";
import { Size, StringMap } from "../types";
import * as Checkers from "../validators/misc-validators";
export interface ObjectCreatorParams {
name: string;
attr?: StringMap;
cont?: string | HTMLElement | HTMLElement[];
style?: CSSStyleDeclaration;
}
export class DomUtils {
/**
* Function returns height of window
*
* @returns window height in pixels
*/
public static getWindowHeight(): number {
if (typeof window === "undefined") {
throw new NotBrowserException();
}
// @ts-ignore
return window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight;
}
/**
* Function returns width of window
*
* @returns window width in pixels
*/
public static getWindowWidth(): number {
if (typeof window === "undefined") {
throw new NotBrowserException();
}
// @ts-ignore
return window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth;
}
/**
* Function set, append or returns text of element
*
* @param element - input element
* @param text - text to put in element
* @param append - flag if text should be append or replace previous text
* @returns element given as input
*/
public static text(element: HTMLElement, text: string, append = true): HTMLElement {
if (append) {
element.textContent += text;
} else {
element.textContent = text;
}
return element;
}
/**
* Function set, append or returns html content of element
*
* @param element - input element
* @param html - html to put in element
* @param append - flag if html should be append or replace previous content
* @returns element given as input
*/
public static html(element: HTMLElement, html: string | HTMLElement, append = true): HTMLElement {
if (append) {
if (typeof html === "string") {
element.innerHTML += html;
} else if (Checkers.isElement(html)) {
element.appendChild(html);
}
} else if (typeof html === "string") {
element.innerHTML = html;
} else if (Checkers.isElement(html)) {
element.innerHTML = "";
element.appendChild(html);
}
return element;
}
/**
* Function returns, add, remove or toggle elements classes
*
* @param element - input element
* @param name - class name or data-structures of class names
* @param force - flag if class should be toggled false
* @returns boolean if function is used to check class presence otherwise element given as input
*/
public static class(element: HTMLElement, name: string | string[], force = false): HTMLElement | boolean {
if (Array.isArray(name)) {
for (const className of name) {
DomUtils.class(element, className, force);
}
} else {
switch (name[0]) {
case "+":
element.classList.add(name.substring(1));
break;
case "-":
element.classList.remove(name.substring(1));
break;
case "/":
name = name.substring(1);
if (Checkers.isBoolean(force)) {
element.classList.toggle(name, force);
} else {
element.classList.toggle(name);
}
break;
default:
return element.classList.contains(name);
}
}
return element;
}
/**
* Function crete new element
*
* ElementManager.createElement("div") => <div></div>;
* ElementManager.createElement("div", {id: "ide"}) => <div id="ide"></div>;
* ElementManager.createElement("div", {}, "text") => <div>text</div>;
* ElementManager.createElement("div", {}, "<b>text</b>") => <div><b>text</b></div>;
* ElementManager.createElement("div", {}, "text", {color: "blue"}) => <div style="color: blue;">text</div>
*
* ElementManager.createElement({name: "div"}) => <div></div>;
* ElementManager.createElement({name: "div"}) => <div></div>;
* ElementManager.createElement({name: "div", attr: {id: "ide"}}) => <div id="ide"></div>;
*
* @param name - name of element or object contains all configuration
* @param attr - map of all element attributes
* @param cont - element content. Can be string, element or array of elements
* @param style - styles that will be applied to the element
* @returns created element
*/
public static createElement(
name: string | ObjectCreatorParams,
attr?: StringMap,
cont?: string | HTMLElement | HTMLElement[],
style?: CSSStyleDeclaration,
): HTMLElement {
if (typeof document === "undefined") {
throw new NotBrowserException();
}
if (typeof name === "object") {
return DomUtils.createElement(name.name, name.attr || {}, name.cont || "", name.style);
}
const el = document.createElement(name);
if (typeof attr === "object") {
for (const key in attr) {
if (attr.hasOwnProperty(key)) {
el.setAttribute(key, attr[key]);
}
}
}
if (typeof style === "object") {
for (const key in style) {
if (style.hasOwnProperty(key)) {
el.style[key] = style[key];
}
}
}
if (Array.isArray(cont)) {
cont.forEach((e) => {
DomUtils.html(el, e, true);
});
} else {
DomUtils.html(el, cont as string | HTMLElement);
}
return el;
}
/**
* Function remove element
*
* @param element - input element
* @returns removed element
*/
public static remove(element: Element): Element {
const parentElement = element.parentElement;
if (parentElement) {
parentElement.removeChild(element);
}
return element;
}
/**
* Function returns object with element position
*
* @param element - input element
* @returns position of element
*/
public static position(element: HTMLElement): SimpleVector2 {
let top = 0;
let left = 0;
do {
top += element.offsetTop || 0;
left += element.offsetLeft || 0;
element = element.offsetParent as HTMLElement;
} while (element);
return {
x: left,
y: top,
};
}
/**
* Function returns order of element between siblings
*
* @param element - input element
* @returns index of number
*/
public static indexOf(element: Element | null): number {
let index = 0;
while (element) {
element = element.previousElementSibling;
index++;
}
return index;
}
/**
* Function returns object with element size
*
* @param element - input element
* @returns size of element
*/
public static size(element: HTMLElement): Size {
return {
height: element.offsetHeight,
width : element.offsetWidth,
};
}
public static serialize(form: HTMLFormElement): StringMap {
const result: StringMap = {};
// if forms is not element
if (!Checkers.isElement(form)) {
return result;
}
// if form is not form
if (form.tagName.toLowerCase() !== "form") {
return result;
}
// get all inputs
const elements = DomGet.byTag("input");
// add all values to result object
for (const key in elements) {
if (!elements.hasOwnProperty(key)) {
continue;
}
const e: Element = elements[key];
const name = e.getAttribute("name");
if (name) {
result[name] = e.getAttribute("value") as string;
}
}
return result;
}
}