This repository has been archived by the owner on Nov 6, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathUtils.js
255 lines (220 loc) · 5.68 KB
/
Utils.js
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
import { Equals } from "./Symbols";
import Record from "./Record";
export const update = (data, newData) => {
const updatedData = Object.assign(Object.create(null), data, newData);
if (data instanceof Record) {
return new data.constructor(updatedData);
} else {
return new Record(updatedData);
}
};
export const navigate = (url, dispatch = true) => {
let pathname = window.location.pathname;
let search = window.location.search;
let hash = window.location.hash;
let fullPath = pathname + search + hash;
if (fullPath !== url) {
if (dispatch) {
window.history.pushState({}, "", url);
dispatchEvent(new PopStateEvent("popstate"));
} else {
window.history.replaceState({}, "", url);
}
}
};
export const insertStyles = (styles) => {
let style = document.createElement("style");
document.head.appendChild(style);
style.innerHTML = styles;
};
export const at = (enums) => (array, index) => {
const { just, nothing } = enums;
if (array.length >= index + 1 && index >= 0) {
return new just(array[index]);
} else {
return new nothing();
}
};
class DataTransfer {
constructor() {
this.effectAllowed = "none";
this.dropEffect = "none";
this.files = [];
this.types = [];
this.cache = {};
}
getData(format) {
return this.cache[format] || "";
}
setData(format, data) {
this.cache[format] = data;
return null;
}
clearData() {
this.cache = {};
return null;
}
}
export const normalizeEvent = (event) => {
return new Proxy(event, {
get: function (obj, prop) {
if (prop in obj) {
const value = obj[prop];
if (value instanceof Function) {
return () => obj[prop]();
} else {
return value;
}
} else {
switch (prop) {
// onCopy onCut onPaste
case "clipboardData":
return (obj.clipboardData = new DataTransfer());
// drag events
case "dataTransfer":
return (obj.dataTransfer = new DataTransfer());
// onCompositionEnd onCompositionStart onCompositionUpdate
case "data":
return "";
// onKeyDown onKeyPress onKeyUp
case "altKey":
return false;
case "charCode":
return -1;
case "ctrlKey":
return false;
case "key":
return "";
case "keyCode":
return -1;
case "locale":
return "";
case "location":
return -1;
case "metaKey":
return false;
case "repeat":
return false;
case "shiftKey":
return false;
case "which":
return -1;
// onClick onContextMenu onDoubleClick onDrag onDragEnd
// onDragEnter onDragExit onDragLeave onDragOver onDragStart
// onDrop onMouseDown onMouseEnter onMouseLeave
// onMouseMove onMouseOut onMouseOver onMouseUp
case "button":
return -1;
case "buttons":
return -1;
case "clientX":
return -1;
case "clientY":
return -1;
case "pageX":
return -1;
case "pageY":
return -1;
case "screenX":
return -1;
case "screenY":
return -1;
// onScroll
case "detail":
return -1;
// onWheel
case "deltaMode":
return -1;
case "deltaX":
return -1;
case "deltaY":
return -1;
case "deltaZ":
return -1;
// onAnimationStart onAnimationEnd onAnimationIteration
case "animationName":
return "";
case "pseudoElement":
return "";
case "elapsedTime":
return -1;
// onTransitionEnd
case "propertyName":
return "";
default:
return undefined;
}
}
},
});
};
export const bindFunctions = (target, exclude) => {
const descriptors = Object.getOwnPropertyDescriptors(
Reflect.getPrototypeOf(target)
);
for (let key in descriptors) {
if (exclude && exclude[key]) {
continue;
}
const value = descriptors[key].value;
if (typeof value !== "function") {
continue;
}
target[key] = value.bind(target);
}
};
export const setConstants = (target, object) => {
if (!object) {
return;
}
const properties = {};
Object.keys(object).forEach((key) => {
let value = null;
properties[key] = {
get: () => {
if (!value) {
value = object[key]();
}
return value;
},
};
});
Object.defineProperties(target, properties);
};
export const array = function () {
let items = Array.from(arguments);
if (Array.isArray(items[0]) && items.length === 1) {
return items[0];
} else {
return items;
}
};
export const style = function (items) {
const result = {};
const setKeyValue = (key, value) => {
result[key.toString().trim()] = value.toString().trim();
};
for (let item of items) {
if (typeof item === "string") {
item.split(";").forEach((prop) => {
const [key, value] = prop.split(":");
if (key && value) {
setKeyValue(key, value);
}
});
} else if (item instanceof Map) {
for (let [key, value] of item) {
setKeyValue(key, value);
}
} else if (item instanceof Array) {
for (let [key, value] of item) {
setKeyValue(key, value);
}
} else {
for (let key in item) {
setKeyValue(key, item[key]);
}
}
}
return result;
};