-
Notifications
You must be signed in to change notification settings - Fork 2
/
dom.ts
187 lines (154 loc) · 3.82 KB
/
dom.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
/**
* 判斷 elements 中的 css class
* @param dom elements
* @param className css class
* @returns {boolean}
*/
export function hasClass(dom: any, className: string): boolean {
return !!dom.className.match(new RegExp(`(\\s|^)${className}(\\s|$)`));
}
/**
* 在 elements 中新增 css class
* @param dom elements
* @param className
*/
export function addClass(dom: any, className: string):void {
if (!hasClass(dom, className)) {
dom.classList.add(className);
}
}
/**
* 刪除 elements 中的 css class
* @param dom elements
* @param className
*/
export function removeClass(dom: any, className: string):void {
dom.classList.remove(className);
}
/**
* 插入IFrame
* 是否事後刪除由 callback 處理
* @param frameId 識別ID
* @param url 網址
* @param callBack 回乎方法
*/
export function insertIFrame(frameId: string, url: string, callBack?: (element: HTMLIFrameElement)=> void):void {
// 插入測速工具
if (document.getElementById(frameId) === null) {
const i = document.createElement('iframe');
i.id = frameId;
i.src = url;
i.scrolling = 'no';
// @ts-ignore
i.frameborder = '0';
// @ts-ignore
i.width = 0;
// @ts-ignore
i.height = 0;
i.onload = () => {
// callBack
if (callBack) {
callBack(i);
}
};
// add iFrame
document.body.appendChild(i);
}
}
/**
* 插入Script
* @param scriptId
* @param scriptContent
*/
export function insertScriptContent(scriptId: string, scriptContent: string): void {
// 插入測速工具
if (document.getElementById(scriptId) === null) {
const s = document.createElement('script');
s.id = scriptId;
s.append(scriptContent);
// add script
document.head.appendChild(s);
}
}
/**
* 插入Script
* @param scriptId
* @param scriptUrl
* @param callBack
*/
export function insertScriptSrc(scriptId: string, scriptUrl: string, callBack?: () => void) {
// 插入測速工具
if (document.getElementById(scriptId) === null) {
const s = document.createElement('script');
s.id = scriptId;
s.src = scriptUrl;
if(callBack){
s.onload = callBack;
}
// add script
document.head.appendChild(s);
}
return false;
}
/**
* 複製字串到剪貼簿
* @param value 要複製的內容
* @param options
*/
export const copyToClipboard = (
value: string,
options?: {
isNewLine?: boolean,
callBack?: () => void,
}
):void => {
const textField = document.createElement('textarea');
if(options?.isNewLine){
// 包含換行
textField.value = value;
}else{
textField.innerText = value;
}
document.body.appendChild(textField);
textField.select();
document.execCommand('copy');
textField.remove();
if(options?.callBack){
options.callBack();
}
};
/**
* 下載 Blob
* @param blob
* @param fileName
*/
export const downloadBlob = (blob: Blob, fileName: string): void => {
downloadUrl(window.URL.createObjectURL(blob), fileName);
};
/**
* 下載 Url (ex: base64 url)
* @param url
* @param fileName
*/
export const downloadUrl = (url: string, fileName: string): void => {
const a = document.createElement('a');
a.href = url;
a.download = fileName;
a.click();
};
/**
* 取得適合的顯示位置
* @param el
*/
export const getVisiblePosition = (el: HTMLElement) => {
const rect = el.getBoundingClientRect();
const top = rect.top;
const bottom = window.innerHeight - top;
return bottom > top ? 'bottom': 'top';
};
/**
* 讓注視中的元素 失去焦點
*/
export const activeElementBlur = () => {
(document.activeElement as HTMLElement).blur();
};