-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFiles.js
402 lines (378 loc) · 13.2 KB
/
Files.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
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
402
//noinspection JSValidateJSDoc
/**
* @module Files
* @description File management and control
* @version 1.0
* @since 1.1
* @license MIT
* @author Maximilian Berkmann <[email protected]>
* @copyright Maximilian Berkmann 2016
* @requires mdodule:essence
* @requires Ajax
* @requires Misc
* @requires DataStruct
* @type {Module}
* @exports File
*/
var Files = new Module("Files", "File management and control", ["Ajax", "Misc", "DOM", "DataStruct"]);
/* eslint no-undef: 0 */
/**
* @description Keeps the file name even if it's not in the same directory as the file that uses this
* @param {string} path Path
* @returns {*} File name
* @since 1.0
* @func
*/
function stripPath (path) { //Keeps the file name even if it's not in the same directory as this library or the files using it
return path.split("/")[path.split("/").length - 1]
}
/**
* @description Get the name of the current file
* @param {boolean} [withExt=false] With the extension
* @returns {string} File name
* @since 1.0
* @func
*/
function getFilename (withExt) {
return withExt? stripPath(location.pathname): stripPath(location.pathname).get(-stripPath(location.pathname).lastIndexOf(".") - 1);
}
/**
* @description A bit like stripPath but which would preserve the directories that aren't listed in the local path
* @param {string} path Path
* @param {string} [localPath="file:///"] Local path
* @returns {string} Current path
* @since 1.0
* @func
*/
function getCurrentPath (path, localPath) {
if (!localPath) localPath = "file:///";
var parts = path.split("/"), res, pParts = localPath.split("/"), i = 0, j = 0;
while(localPath.has(parts[i])) i++;
res = parts.get(i).join("/");
while (res.has(pParts[j])) {
if (debugging) console.log("Gone through " + pParts[j]);
j++;
}
if (j > 0) {
for(i = 0; i < j; i++) res = "../" + res;
}
return res
}
/**
* @description Get the path for an external file
* @param {string} path Full path
* @returns {string} External path
* @func
* @since 1.1
*/
function getExtPath (path) {
var cp = location.href;
var parentPath = cp.sameFirst(path);
return "../".repeat(getCurrentPath(cp, parentPath).count("/")) + getCurrentPath(path, parentPath);
}
/**
* @description Get the filename list of the path list
* @param {string[]} list Path list
* @returns {Array} File name list
* @since 1.0
* @func
*/
function filenameList (list) {
var res = [];
for(var i = 0; i < list.length; i++) res.push(stripPath(list[i]));
return res.remove()
}
/**
* @description Get the directory's path of the file (opposite of stripPath())
* @param {string} [path=location.href] Path
* @returns {string} Directory path
* @since 1.0
* @func
*/
function getDirectoryPath (path) {
if (!path) path = location.href;
return path.get(0, path.indexOf(stripPath(path)) - 1)
}
/**
* @description ActiveX file manipulation
* @param {string} filename Filename
* @param {string} text2write Text to write to the file
* @param {boolean} [close=false] Closing flag
* @param {boolean} [remove=false] Removing flag
* @returns {undefined}
* @since 1.0
* @func
*/
function AX (filename, text2write, close, remove) {
var fso = new ActiveXObject("Scripting.FileSystemObject");
//Bool: flat the file of the same name if it's already present
fso.CreateTextFile(filename,true);
//Opening type: 1-read only; 2-rewriting; 8-continue to write at the end, create (true) or not (false) the file if it doesn't exist
var otf = fso.OpenTextFile(filename, 1, true);
//WriteLn add a new line
//- the file has to be already opened in mode 2 or 8
otf.Write(text2write);
/* the file has to be opened in the read mod
- Read read to the specified number of characters. */
otf.ReadAll();
if (close && confirm("Do you really want to close this file ?")) otf.Close();
if (remove && confirm("Do you really want to delete this file ?")) fso.DeleteFile(filename)
}
/**
* @description Execute a file
* @param {string} file File name
* @param {string} ext Extension
* @returns {undefined}
* @since 1.0
* @func
*/
function execFile (file, ext) {
var wshShell = new ActiveXObject("WScript.Shell");
wshShell.Run(file + "." + ext, 1, true)
}
/**
* @description Copy to clipboard
* @param {*} txt Text to copy
* @param {string} type Type of the text
* @returns {undefined}
* @since 1.0
* @func
*/
function copyToClipboard (txt, type) { //Works only for IE
clipboardData.setData(type || "Text", txt)
}
/**
* @description Save text into a file
* @param {*} txt Text
* @param {string} name Filename
* @param {string} [type="plain"] Type
* @returns {undefined}
* @since 1.0
* @func
*/
function save (txt, name, type) { //Save into a file of the corresponding type
var txtfile = new Blob([txt], {type: "text/" + (type || "plain")});
var dlLink = document.createElement("a");
dlLink.download = name;
dlLink.innerHTML = "Download File";
if (window.webkitURL != null) dlLink.href = window.webkitURL.createObjectURL(txtfile); //Chrome allows the link to be clicked without actually adding it to the DOM.
else { //Firefox requires the link to be added to the DOM before it can be clicked.
dlLink.href = window.URL.createObjectURL(txtfile);
dlLink.onclick = function (evt) {
document.body.removeChild(evt.target);
};
dlLink.style.display = "none";
document.body.appendChild(dlLink);
}
dlLink.click()
}
/**
* @description Get the file's content
* @param {string} fname File name
* @returns {string} File's content
* @since 1.0
* @see module:Files~getFC
* @func
* @deprecated
*/
function getFileContent (fname) {
$G["fct"] = ""; //File content
var rawFile = window.XMLHttpRequest? new XMLHttpRequest(): new ActiveXObject("Microsoft.XMLHTTP");
rawFile.open("GET", fname, false);
rawFile.onreadystatechange = function () {
if (this.readyState === 4 && this.status === 200) {
$G["fct"] = rawFile.responseText; //Because returning it won't allow the actual content to be returned
return rawFile.responseText
}
};
rawFile.send(null);
return $G["fct"];
}
/**
* @description getFileContent using the XHR object
* @param {string} fname File name
* @param {boolean} [crossOrigin=false] Cross Origin flag (for accessing resources outside of the same origin)
* @returns {string} File's content
* @since 1.0
* @inheritdoc
* @func
*/
function getFC (fname, crossOrigin) {
if (!crossOrigin && (fname.has("://") && fname.split("//")[0] != location.protocol)) crossOrigin = true;
var res = "", xhr = crossOrigin? new CORS(fname, "GET", false, function (req) {
res = req.responseText;
return req.responseText;
}, function () {
return "Nothing";
}): new XHR(fname, "GET", false, function (req) {
res = req.responseText;
return req.responseText;
}, function () {
return "Nothing";
});
xhr.init();
return res;
}
/**
* @description Evaluate a file (useful for getting JSON data and into JS objects)
* @param {string} filename Filename
* @param {boolean} [crossOrigin=false] Is a CORS request needed ?
* @returns {*} Object of the file
* @since 1.1
* @func
*/
function evalFile (filename, crossOrigin) {
return (new Function("return " + getFC(filename, crossOrigin)))();
}
/**
* @description Keyword getter
* @param {Str} text Text
* @param {boolean} [noSymbols=false] Ignore symbols
* @returns {Array} Keywords
* @since 1.1
* @func
*/
function getKeywords (text, noSymbols) {
var txt = (isType(text, "Array") ? text.join(" ") : text).replace(/(\.|!|\?|;|:|"|,|\t|\n|\f|\r|\v|\{|})+/gm, " ").split(" ").remove(); //The \b would treat a-b as "a - b"
var kw = occurrenceSort(txt).filter(function (x) { //Filter out non-keywords words
return noSymbols? (["=", "+", "-", "*", "/", "\\", "%", "#", "'", "@", "^", "$", "£", "µ", "~", "&", "[", "]", "(", ")", "|", "`"].has(x)? false: txt.count(x) > 3): txt.count(x) > 3;
});
return kw.map(function (w) {
return w + ": " + txt.count(w) + " (" + markConv(txt.count(w), txt.length) + "%)";
});
}
/**
* @description Web spider
* @param {string[]} [filenames=[]] Names of each files to crawl through
* @returns {Spider} Web spider
* @this {Spider}
* @since 1.1
* @constructor
* @property {string[]} Spider.name Directory containing the files to crawl through
* @property {string[]} Spider.keywords Keywords
* @property {function(...boolean): Array[]} Spider.get Keyword getter
* @property {function(...boolean): string[]} Spider.getAll Get all the keywords nice and clean
* @property {function(...boolean): string[]} Spider.getWords Get all the key-words
* @property {function(...boolean): number[]} Spider.getOccurrences Get the number of occurrences of all the keywords
* @property {function(...boolean): number[]} Spider.getFreq Get the frequency of all the keywords
* @property {function(...boolean): number} Spider.getCoverage Get the coverage of all the keywords compared to all the words
* @property {function(...boolean): string[]} Spider.getGlobalKeywords Get all the keywords of all the file at once
* @property {function(): string} Spider.toString String representation
*/
function Spider (filenames) {
this.dir = filenames || [];
this.keywords = [];
this.get = function (withSymbols, crossOrigin) { //Keywords infos
/*
Words: getKeywords(...).map(x => x.split(":")[0])
Occurrences: getKeywords(...).map(x => parseInt(x.split(" ")[1]))
Frequency: getKeywords(...).map(x => parseFloat(x.split(" ")[2].replace(/^\((\d|\d\.\d{1,})\%\)$/, "$1")))
*/
for (var i = 0; i < this.dir.length; i++) this.keywords[i] = getKeywords(getFC(this.dir[i], crossOrigin), !withSymbols);
return this.keywords;
};
this.getAll = function (withSymbols, crossOrigin) {
return this.get(withSymbols, crossOrigin).linearise();
};
//noinspection JSUnusedGlobalSymbols
this.getWords = function (withSymbols, crossOrigin) { //Occurring words
return this.getAll(withSymbols, crossOrigin).map(function (x) {
return x.split(":")[0]
})
};
this.getOccurrences = function (withSymbols, crossOrigin) {
return this.getAll(withSymbols, crossOrigin).map(function (x) {
return parseInt(x.split(" ")[1])
})
};
this.getFreq = function (withSymbols, crossOrigin) { //Frequency
return this.getAll(withSymbols, crossOrigin).map(function (x) {
return parseFloat(x.split(" ")[2].replace(/^\((\d+|\d+\.\d+)%\)$/, "$1"));
})
};
//noinspection JSUnusedGlobalSymbols
this.getCoverage = function (withSymbols, crossOrigin) {
return this.getFreq(withSymbols, crossOrigin).sum().toNDec(2);
};
//noinspection JSUnusedGlobalSymbols
this.getGlobalKeywords = function (withSymbols, crossOrigin) {
var fullDir = this.dir.map(function (file) {
return getFC(file, crossOrigin);
}).toStr();
return getKeywords(fullDir, !withSymbols);
};
this.toString = function () {
return "Spider(dir=" + this.dir + ", keywords= " + this.keywords.toStr(true) + ")";
};
return this;
}
//1. Get the file (if it exists) then get the content saved in an history accessible to the code.
//2. When the the code changes/updates the content, it's saved in the file (which is created if it didn't exist)
//noinspection JSUnusedGlobalSymbols
/**
* @description A mediator between data of the code and files to keep both interfaces up-to-date
* @param {string} [filename="data.json"] Filename
* @param {*} [data=null] Data to write to the file (if needed).
* @return {$Data} Mediator object (sort of an API)
* @todo Make sure that save() would save at the right place (since it would get it to be downloaded by the user)
*/
function $Data (filename, data) {
this.name = filename || "data.json";
this.data = null || data;
var self = this;
this.req = new CORS(this.name, "GET", false, function (xhr) {
self.data = xhr.response;
}, function () {
Essence.say("The file %c" + self.name + "%c isn't available !", "warn", "text-decoration: italic;", "text-decoration: none;");
}, function () {
anim("Loading", 0, 5e3, 500, false, !isNon(self.req.data));
}, this.data);
this.req.init();
this.req.update();
this.history = new virtualHistory(this.data);
this.save = function () {
save(this.data, this.name, this.data.split(".").last());
};
this.undo = function () {
this.history.undo();
this.data = this.history.src;
};
this.redo = function () {
this.history.redo();
this.data = this.history.src;
};
this.update = function (newData) {
this.history.update(newData || this.data);
if (newData) this.data = newData;
this.save();
};
this.get = function () {
this.req.init();
this.req.update();
return this.data;
};
this.toString = function () {
return "$Data(name=" + this.name + ", data=" + this.data + ", history=" + this.history.getStates() + ", req=" + this.req + ")";
};
return this;
}
/**
* @description Load a stylesheet in a deferred manner.<br />
* Inspired by {@link https://developers.google.com/speed/docs/insights/OptimizeCSSDelivery|Google's CSS Optimized delivery}.
* @param {String} file Filename
* @returns {undefined}
* @since 1.1
* @func
*/
function loadDeferredStyle (file) {
var addStylesNode = tryNode("noscript#deferredStyles", "body");
var replacement = document.createElement("div");
replacement.innerHTML = addStylesNode.innerText;
document.body.appendChild(replacement);
addStylesNode.delete();
var raf = requestAnimationFrame || mozRequestAnimationFrame || webkitRequestAnimationFrame || msRequestAnimationFrame;
if (raf) raf(function () {
wait(loadDeferredStyle);
});
else window.addEventListener("load", loadDeferredStyle);
}