forked from MicrosoftDX/Vorlonjs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vorlon.tools.ts
401 lines (351 loc) · 13.5 KB
/
vorlon.tools.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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
module VORLON {
export class Tools {
public static get IsWindowAvailable(): boolean {
return typeof window != 'undefined';
}
public static QuerySelectorById(root: HTMLElement, id: string): HTMLElement {
if (root.querySelector) {
return <HTMLElement>root.querySelector("#" + id);
}
return document.getElementById(id);
}
public static SetImmediate(func: () => void): void {
if (window.setImmediate) {
setImmediate(func);
} else {
setTimeout(func, 0);
}
}
public static setLocalStorageValue(key: string, data: string) {
if (localStorage) {
try {
localStorage.setItem(key, data);
}
catch (e) {
//local storage is not available (private mode maybe)
}
}
}
public static getLocalStorageValue(key: string) {
if (localStorage) {
try {
return localStorage.getItem(key);
}
catch (e) {
//local storage is not available (private mode maybe)
return "";
}
}
}
public static Hook(rootObject: any, functionToHook: string, hookingFunction: (...optionalParams: any[]) => void): void {
var previousFunction = rootObject[functionToHook];
rootObject[functionToHook] = (...optionalParams: any[]) => {
hookingFunction(optionalParams);
previousFunction.apply(rootObject, optionalParams);
}
return previousFunction;
}
public static HookProperty(rootObjectName: string, propertyToHook: string, callback){
var rootObject = (Tools.IsWindowAvailable ? window : global)[rootObjectName];
var initialValue = rootObject[propertyToHook];
Object.defineProperty(rootObject, propertyToHook, {
get: function() {
if (callback){
var stack = VORLON.Tools.getCallStack(1);
stack.property = propertyToHook;
callback(stack);
}
return initialValue;
}
});
}
public static getCallStack(skipped){
skipped = skipped || 0;
try {
//Throw an error to generate a stack trace
throw new Error();
}
catch(e) {
//Split the stack trace into each line
var stackLines = e.stack.split('\n');
var callerIndex = 0;
//Now walk though each line until we find a path reference
for(var i=2 + skipped, l = stackLines.length; i<l; i++){ //first lines will be error and this utility method
if(!(stackLines[i].indexOf("http://") >= 0))
continue;
//We skipped all the lines with out an http so we now have a script reference
//This one is the class constructor, the next is the getScriptPath() call
//The one after that is the user code requesting the path info (so offset by 2)
callerIndex = i;
break;
}
var res = <any>{
stack : e.stack,
// fullPath : pathParts ? pathParts[1] : null,
// path : pathParts ? pathParts[2] : null,
// file : pathParts ? pathParts[3] : null
};
var linetext = stackLines[callerIndex];
//Now parse the string for each section we want to return
//var pathParts = linetext.match(/((http[s]?:\/\/.+\/)([^\/]+\.js))([\/]):/);
// if (pathParts){
//
// }
var opening = linetext.indexOf("http://") || linetext.indexOf("https://");
if (opening > 0){
var closing = linetext.indexOf(")", opening);
if (closing < 0)
closing = linetext.length - 1;
var filename = linetext.substr(opening, closing - opening);
var linestart = filename.indexOf(":", filename.lastIndexOf("/"));
res.file = filename.substr(0, linestart);
res.line = filename.substr(linestart + 1);
}
return res;
}
}
public static CreateCookie(name: string, value: string, days: number): void {
var expires: string;
if (days) {
var date = new Date();
date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
expires = "; expires=" + date.toUTCString();
} else {
expires = "";
}
document.cookie = name + "=" + value + expires + "; path=/";
}
public static ReadCookie(name: string): string {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for (var i = 0; i < ca.length; i++) {
var c = ca[i];
while (c.charAt(0) === ' ') {
c = c.substring(1, c.length);
}
if (c.indexOf(nameEQ) === 0) {
return c.substring(nameEQ.length, c.length);
}
}
return "";
}
// from http://stackoverflow.com/questions/105034/how-to-create-a-guid-uuid-in-javascript/2117523#answer-2117523
public static CreateGUID(): string {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g,(c) => {
var r = Math.random() * 16 | 0, v = c === 'x' ? r : (r & 0x3 | 0x8);
return v.toString(16);
});
}
public static RemoveEmpties(arr: string[]): number {
var len = arr.length;
for (var i = len - 1; i >= 0; i--) {
if (!arr[i]) {
arr.splice(i, 1);
len--;
}
}
return len;
}
public static AddClass(e: HTMLElement, name: string): HTMLElement {
if (e.classList) {
if (name.indexOf(" ") < 0) {
e.classList.add(name);
} else {
var namesToAdd = name.split(" ");
Tools.RemoveEmpties(namesToAdd);
for (var i = 0, len = namesToAdd.length; i < len; i++) {
e.classList.add(namesToAdd[i]);
}
}
return e;
} else {
var className = e.className;
var names = className.split(" ");
var l = Tools.RemoveEmpties(names);
var toAdd;
if (name.indexOf(" ") >= 0) {
namesToAdd = name.split(" ");
Tools.RemoveEmpties(namesToAdd);
for (i = 0; i < l; i++) {
var found = namesToAdd.indexOf(names[i]);
if (found >= 0) {
namesToAdd.splice(found, 1);
}
}
if (namesToAdd.length > 0) {
toAdd = namesToAdd.join(" ");
}
} else {
var saw = false;
for (i = 0; i < l; i++) {
if (names[i] === name) {
saw = true;
break;
}
}
if (!saw) {
toAdd = name;
}
}
if (toAdd) {
if (l > 0 && names[0].length > 0) {
e.className = className + " " + toAdd;
} else {
e.className = toAdd;
}
}
return e;
}
}
public static RemoveClass(e: HTMLElement, name: string): HTMLElement {
if (e.classList) {
if (e.classList.length === 0) {
return e;
}
var namesToRemove = name.split(" ");
Tools.RemoveEmpties(namesToRemove);
for (var i = 0, len = namesToRemove.length; i < len; i++) {
e.classList.remove(namesToRemove[i]);
}
return e;
} else {
var original = e.className;
if (name.indexOf(" ") >= 0) {
namesToRemove = name.split(" ");
Tools.RemoveEmpties(namesToRemove);
} else {
if (original.indexOf(name) < 0) {
return e;
}
namesToRemove = [name];
}
var removed;
var names = original.split(" ");
var namesLen = Tools.RemoveEmpties(names);
for (i = namesLen - 1; i >= 0; i--) {
if (namesToRemove.indexOf(names[i]) >= 0) {
names.splice(i, 1);
removed = true;
}
}
if (removed) {
e.className = names.join(" ");
}
return e;
}
}
public static ToggleClass(e: HTMLElement, name: string, callback? : (hasClass:boolean) => void) {
if (e.className.match(name)) {
Tools.RemoveClass(e, name);
if (callback)
callback(false);
} else {
Tools.AddClass(e, name);
if (callback)
callback(true);
}
}
public static htmlToString(text) {
return text.replace(/</g, '<').replace(/>/g, '>');
}
}
export class FluentDOM {
public element: HTMLElement;
public childs: Array<FluentDOM>;
public parent: FluentDOM;
constructor(nodeType: string, className?: string, parentElt?: Element, parent?: FluentDOM) {
this.childs = [];
if (nodeType) {
this.element = document.createElement(nodeType);
if (className)
this.element.className = className;
if (parentElt)
parentElt.appendChild(this.element);
this.parent = parent;
if (parent) {
parent.childs.push(this);
}
}
}
public static forElement(element: HTMLElement) {
var res = new FluentDOM(null);
res.element = element;
return res;
}
addClass(classname: string) {
this.element.classList.add(classname);
return this;
}
toggleClass(classname: string) {
this.element.classList.toggle(classname);
return this;
}
className(classname: string) {
this.element.className = classname;
return this;
}
opacity(opacity: string) {
this.element.style.opacity = opacity;
return this;
}
display(display: string) {
this.element.style.display = display;
return this;
}
hide() {
this.element.style.display = 'none';
return this;
}
visibility(visibility: string) {
this.element.style.visibility = visibility;
return this;
}
text(text: string) {
this.element.textContent = text;
return this;
}
html(text: string) {
this.element.innerHTML = text;
return this;
}
attr(name: string, val: string) {
this.element.setAttribute(name, val);
return this;
}
editable(editable: boolean) {
this.element.contentEditable = editable ? "true" : "false";
return this;
}
style(name: string, val: string) {
this.element.style[name] = val;
return this;
}
appendTo(elt: Element) {
elt.appendChild(this.element);
return this;
}
append(nodeType: string, className?: string, callback?: (fdom: FluentDOM) => void) {
var child = new FluentDOM(nodeType, className, this.element, this);
if (callback) {
callback(child);
}
return this;
}
createChild(nodeType: string, className?: string) {
var child = new FluentDOM(nodeType, className, this.element, this);
return child;
}
click(callback: (EventTarget) => void) {
this.element.addEventListener('click', callback);
return this;
}
blur(callback: (EventTarget) => void) {
this.element.addEventListener('blur', callback);
return this;
}
keydown(callback: (EventTarget) => void) {
this.element.addEventListener('keydown', callback);
return this;
}
}
}