-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathWeb.js
More file actions
1562 lines (1493 loc) · 60.5 KB
/
Copy pathWeb.js
File metadata and controls
1562 lines (1493 loc) · 60.5 KB
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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* @module Web
* @description Web stuff
* @version 1.0
* @since 1.1
* @license MIT
* @author Maximilian Berkmann <maxieberkmann@gmail.com>
* @copyright Maximilian Berkmann 2016
* @requires module:essence
* @requires DataStruct
* @requires Misc
* @requires Ajax
* @type {Module}
* @exports Web
*/
var Web = new Module("Web", "Web stuff", ["DataStruct", "Misc", "Ajax"], 1, function () {
//if (!isValid($G["IP"], "ip")) getIP();
//if (!isValid($G["IP"], "ip")) getPrivateIP();
});
/* eslint no-undef: 0 */
/**
* @description Gather the cookie named $c_name
* @param {string} name Cookie name
* @returns {undefined|string} Cookie
* @see module:Web~setCookie
* @since 1.0
* @func
*/
function getCookie (name) {
var x, y, cookies = document.cookie.split(";");
for (var i = 0; i < cookies.length;i++) {
x = cookies[i].substr(0, cookies[i].indexOf("="));
y = cookies[i].substr(cookies[i].indexOf("=") + 1);
x = x.replace(/^\s+|\s+$/g, "");
if (x === name) return decodeURIComponent(y)
}
}
/**
* @description Create a cookie
* @param {string} name Cookie name
* @param {*} value Cookie value
* @param {number} [exdays] Expiration days
* @returns {undefined}
* @see module:Web~getCookie
* @since 1.0
* @func
*/
function setCookie (name, value, exdays) {
exdays? exdays %= 99983489: exdays = 99983488; //As 99983488 is the maximum value
var exdate = new Date();
exdate.setDate(exdate.getDate() + exdays);
var c_value = encodeURIComponent(value) + ((exdays === null) ? "" : "; expires = " + exdate.toUTCString());
document.cookie = name + "=" + c_value
}
/**
* @description Remove a cookie
* @param {string} name Cookie name
* @returns {undefined}
* @since 1.1
* @func
*/
function rmCookie(name) {
setCookie(name, "", -1);
}
/**
* @description Cookie history
* @type {{get: CookieHistory.get, set: CookieHistory.set, toObject: CookieHistory.toObject}}
* @since 1.1
* @see module:Web~setCookie
* @see module:Web~getCookie
* @property {function(): string[]} CookieHistory.get Get the list of cookies
* @property {Function} CookieHistory.set Set the cookie for the list of cookies
* @property {function(): Object} CookieHistory.toObject Get the dictionary form of the cookie history
*/
var CookieHistory = {
get: function () {
return document.cookie.split(";").map(function (x) {
return x.split("=")[0]
}).remove("CookieHistory", true);
},
set: function () {
setCookie("CookieHistory", this.get());
},
toObject: function () {
return Objectify(this.get(), document.cookie.split(";").filter(function (x) {
return x.split("=")[0] != "CookieHistory"
}).map(function (x) {
return x.split("=")[1]
}));
}
};
/**
* @description Local/session database
* @param {string} [name="Database"] Database name
* @param {Array|boolean} [headR=range(100)] Header rows
* @param {Array} [cells=[].fill("...")] Cells
* @param {Array|boolean} [headC=["Index", "Value"]] Header columns
* @param {string} [admin="Anonymous"] Admin's name
* @param {number} [ver=1.0] Version
* @this database
* @returns {database} Database
* @property {string} database.name Name
* @property {NumberLike[]} database.headerRow Row headers
* @property {NumberLike[]} database.headerCol Column headers
* @property {Array[]} database.content Content
* @property {string} database.admin Administrator
* @property {number} database.version Version
* @property {Array[]} database.val Value (extended content) of the database
* @property {Function} database.setStorage Store the database
* @property {Code} database.html HTML code
* @property {string} database.css CSS code
* @property {function(?string)} database.disp Display the database
* @property {Function} database.update Update the database
* @property {function(*)} database.searchAndRemove Search and remove a value
* @property {function(number, number)} database.remove Remove a cell from the database
* @property {function(): string} database.toString String representation
*/
function database (name, headR, cells, headC, admin, ver) { //Local database
this.name = name || "Database";
this.headerRow = (isNon(headR))? range(100): headR;
this.headerCol = headC || ["Index", "Value"];
this.content = (isNon(cells))? new Array(this.headerCol.length).fill("..."): cells;
this.admin = admin || "Anonymous";
this.version = ver || 1.0;
this.val = new Array(this.headerRow.length);
for (var i = 0; i < this.val.length; i++) this.val[i] = new Array(this.headerCol.length).fill(" ");
for (i = 0; i < this.headerCol.length; i++) this.val[0][i] = this.headerCol[i];
for (i = 0; i < this.content.length; i++) {
this.val[i][0] = (this.headerRow)? this.headerRow[i]: i;
for (var j = 0; j < this.content[i].length; j++) {
this.val[i][j + 1] = this.content[i][j];
//console.log("Processing " + this.content[i][j] + " at ", i, j);
}
}
this.setStorage = function () {
localStorage[this.name] = JSON.stringify(this.val)
};
this.css = "#" + this.name + "table{font-family:Consolas,Segoe UI,Tahoma,sans-serif;background: #000;}#" + this.name + "table,#" + this.name + "td,#" + this.name + "th{border:1px solid #000;color:#000;background:#fff;}#" + this.name + "tr:nth-child(even) td,#" + this.name + "tr:nth-child(even) th{background:#eee;}";
this.html = complexTable(this.name, this.headerRow, this.content, this.headerCol, this.name, false, this.css);
this.disp = function (elmId) {
$e(elmId? "#" + elmId: "body").write(this.html, true);
this.setStorage();
};
this.update = function () {
if (localStorage[this.name]) this.val = JSON.parse(localStorage[this.name]);
else this.setStorage();
this.html = complexTable(this.name, this.headerRow, this.content, this.headerCol, this.name, false, this.css);
};
this.searchAndRemove = function (vals) { //Vals = range|..
for (var n = 0; n < vals.length; n++) {
for (var i = 0; i < this.val.length; i++) {
for (var j = 0; j < this.val.length; j++) {
if (this.val[i][j] == vals[n]) this.val[i][j] = null;
}
}
}
};
this.remove = function (xs, ys) { //Vals = range|..
for (var i = 0; i < xs.length; i++) {
for (var j = 0; j < ys.length; j++) {
this.val[ys[j]][xs[i]] = null;
}
}
};
this.toString = function () {
return "database(name=" + this.name + ", headerRow=" + this.headerRow + ", headerCol=" + this.headerCol + ", content=" + this.content + ", admin=" + this.admin + ", version=" + this.version + ")";
};
return this;
}
/**
* @description Database
* @param {string} [name="DB"] Name
* @param {Array} [headers=["Index", "Value"]] Column headers
* @param {Array} [rows=[range(1), new Array(range(1).length).fill("...")].translate()] Rows
* @param {Array} [headerRows] Rows headers
* @return {DB} DB
* @this DB
* @constructor
* @property {string} DB.name Name
* @property {NumberLike[]} DB.rows Row headers
* @property {NumberLike[]} DB.headers Column headers (which should use the first column for indexes/ranks)
* @property {Array} DB.val Content of the database
* @property {Code} DB.html HTML code
* @property {string} DB.css CSS code
* @property {Function} DB.build Build the database
* @property {function(number)} DB.fill Fill-in the database
* @property {Function} DB.save Save the database
* @property {Function} DB.update Update the database
* @property {function(*, number, number)} DB.set Change the value of a cell
* @property {function(number, number): *} DB.get Get the value of a cell
* @property {function(*): number} DB.find Look for an item
* @property {function(): Array} DB.see Get a viewable copy of the database
* @property {function(?string)} DB.view Display the database
* @property {function(Array, ?number)} database.add Add a cell (without the index/rank) to the database
* @property {function(boolean)} DB.init Initialise the database
* @property {function(): string} database.toString String representation
* @property {function(number): Array} database.remove Remove a row from the database
* @property {Function} DB.onBuild Building event listener
* @property {Function} DB.onFill Filling event listener
* @property {Function} DB.onSave Saving event listener
* @property {Function} DB.onUpdate DB update event listener
* @property {Function} DB.onSet DB row set event listener
* @property {Function} DB.onGet DB row/cell gathering event listener
* @property {Function} DB.onFind Research event listener
* @property {Function} DB.onSee Visualisation event listener
* @property {Function} DB.onView DOM display event listener
* @property {Function} DB.onAdd Row addition/push event listener
* @property {Function} DB.onInit Initialisation event listener
*/
function DB (name, headers, rows, headerRows) {
this.name = name || "DB";
this.head = headers || ["Index", "Value"];
this.val = rows || [range(1), new Array(range(1).length).fill("...")].translate();
this.css = "table#"+ this.name + "{font-family:Consolas,Segoe UI,Tahoma,sans-serif;background: #000;}table#"+ this.name + ", #"+ this.name + " td, #"+ this.name + " th{border:1px solid #000;color:#000;background:#fff;}#"+ this.name + " tr:nth-child(even) td,#"+ this.name + " tr:nth-child(even) th{background:#eee;}";
this.html = "";
this.rows = headerRows || false;
//this.events = Tablify(["build", "fill", "save", "update", "set", "get", "find", "see", "view", "add", "init"], {data: null, name: "", timeStamp: new Date().getTime()});
this.onBuild = this.onFill = this.onSave = this.onUpdate = this.onSet = this.onGet = this.onFind = this.onSee = this.onView = this.onAdd = this.onInit = $f;
this.build = function () {
this.html = isNon(this.rows)? colTable("", this.head, this.val, this.name, true, this.css)/*complexTable("", this.val.line(), this.val.block(1), this.head, this.name, true, this.css)*/: complexTable("", this.rows, this.val, this.head, this.name, true, this.css);
this.onBuild(this.html);
};
this.fill = function (len) {
this.val = [];
for (var i = 0; i < len; i++) {
this.val[i] = [i].append(new Array(range(len - 1 || 1).length).fill("..."));
}
this.onFill(this.val, len);
return this.val;
};
this.save = function () {
localStorage[this.name] = JSON.stringify(this.val);
localStorage[this.name + "_head"] = JSON.stringify(this.head);
localStorage[this.name + "_html"] = this.html;
localStorage[this.name + "_rows"] = this.rows;
this.onSave(localStorage[this.name], localStorage[this.name + "_head"], localStorage[this.name + "_html"], localStorage[this.name + "_rows"]);
};
this.update = function () {
if (localStorage[this.name]) {
this.val = JSON.parse(localStorage[this.name]);
this.head = JSON.parse(localStorage[this.name + "_head"]);
this.html = localStorage[this.name + "_html"];
this.rows = (localStorage[this.name + "_rows"] == "false")? localStorage[this.name + "_rows"]: localStorage[this.name + "_rows"].split(","); //JSON.parse(localStorage[this.name + "_rows"]);
} else this.save();
this.onUpdate(this);
};
this.set = function (nval, i, j) {
if (j === -1) {
for (var k = 0; k < nval.length; k++) this.val[i || 0][k] = nval[k];
} else this.val[i || 0][j || 0] = nval || null;
this.onSet(nval, [i, j]);
};
this.get = function (i, j) {
this.onGet(i, j);
return isNon(j)? this.val[i || 0]: this.val[i || 0][j];
};
this.find = function (val) {
var p = lookfor(val, this.val);
this.onFind(val, p);
return p;
};
this.see = function () {
this.onSee(Copy(this.val).prepend(Copy(this.head).reverse()));
return isNon(this.rows)? Copy(this.val).prepend([Copy(this.head)]): Copy(this.val).prepend(Copy(this.head).reverse());
};
this.view = function (id) {
this.build();
$e(id? "#" + id: "body").write(this.html, true);
this.onView($e(id? "#" + id: "body").val(true), id);
};
this.add = function (vals, step) {
this.val.push([parseFloat(this.val.last()[0]) + (step || 1)].concat(vals));
this.onAdd(vals, this.val);
};
this.init = function (seeTable) {
this.build();
this.update();
if (seeTable) console.table(this.see());
this.onInit(this);
};
this.remove = function (i) {
this.val[i || this.val.lastIndex()] = undefined;
this.val.remove();
return this.val;
};
this.toString = function () {
return "DB(name=" + this.name + ", head=" + this.head + ", val=" + this.val + ", rows=" + this.rows + ")";
};
return this;
}
/**
* @description Process
* @param {string} name Name of the process
* @param {string} [auth="Anonymous"] Author
* @param {string} [summup=""] Summary
* @param {string} [ctt=""] Content
* @param {Function} [runnable=$f] Runnable method of the process
* @returns {process} Process
* @since 1.0
* @property {string} process.name Name
* @property {string} process.author Author
* @property {number} process.bitsize Size in bits
* @property {string} process.description Description
* @property {string} process.content Content
* @property {Function} process.update Update the process
* @property {function(server)} process.askPermission Ask the permission to integrate a particular server
* @property {Function} process.destroy Self-destruction
*/
function process (name, auth, summup, ctt, runnable) {
this.name = name;
this.author = auth || "Anonymous";
this.bitsize = 0; //Size in binary unit
this.description = summup || "";
this.content = ctt || "";
this.sig = this.name[0] + this.name[this.name.length - 1] + this.name.prod() + this.author.slice(0, 2) + "-" + (getType(this.content))[0];
//Rights/privileges ?!
this.update = function () {
if (this.author != auth || this.author === "Anonymous" || isNon(this.author)) this.sig = this.name[0] + this.name[this.name.length-1] + "-" + this.name.prod() + this.author.slice(0, 2) + "-" + getType(this.content)[0]; //H4ck
else this.sig = this.name[0] + this.name[this.name.length - 1] + this.name.prod() + this.author.slice(0, 2) + "-" + (getType(this.content))[0];
if (this.sig[this.sig.length - 1] === "N") this.bitsize = 8 * conv(this.content, 10, 2).sum();
else if (this.sig[this.sig.length - 1] === "B") this.bitsize = 8;
else if (this.sig[this.sig.length - 1] === "A") this.bitsize = (is2dArray(this.content))? 8 * this.content.numElm(): 8 * this.content.length;
else if (this.sig[this.sig.length - 1] === "O") this.bitsize = 0;
else if (this.sig[this.sig.length - 1] === "S") this.bitsize = 8 * this.content.length;
else this.bitsize = null;
};
this.update();
this.askPermission = function (serv) {
this.update();
serv.add(this);
};
this.destroy = function () {
Essence.say("The process named " + this.name + " (created by " + this.author + ") has been destroyed !", "info");
this.delete();
Essence.processList = Essence.processList.remove();
};
Essence.addProcess(this);
this.run = runnable || $f;
return this;
}
/**
* @description Server
* @param {string} [name="Server"] Name
* @param {string} [admin=""] Admin
* @param {string} [type="data"] Type (data, process, storage, authentication, register, location)
* @param {number} [ver=1.0] Version
* @param {number} [mxsz=2⁶] Maximum size of the server's database
* @returns {server} Server
* @see module:Web~DB
* @since 1.0
* @throws {Error} Invalid server type
* @property {string} server.name Name
* @property {string} server.admin Administrator
* @property {number} server.version Version
* @property {number} server.maxsize Maximum size
* @property {number} server.nb_slots Number of Slots
* @property {Array} server.slots Slots
* @property {string} server.type Server type
* @property {DB} server.data Database
* @property {function(process)} server.addProcess Process adder (only useful for process servers)
* @property {function(*)} server.add Data adder (useless for process servers)
* @property {function(number)} server.rm Remove a data from the server
* @property {Function} server.store Store the server
* @property {Function} server.update Update the server
* @property {function(process)} server.fire Remove a process from the server
* @property {Function} server.reset Reset the server
* @property {function(): string} server.toString String representation
* @property {function(Event, function(Event))} server.on OnEvt handler
* @property {Event} server.event Event
*/
function server (name, admin, type, ver, mxsz) {
this.name = name || "Server";
this.admin = admin || "";
this.version = ver || 1.0;
this.maxsize = mxsz || Math.pow(2, 14);
this.nb_slots = Math.pow(2, 6);
this.slots = mkArray(this.nb_slots, 1, "empty");
this.type = (type || "data").toLowerCase();
if (this.type === "data") this.data = new DB("db_" + this.name, ["Index", "Value"], this.slots, range(1, 1, this.maxsize));//database(this.name, range(1, 1, this.maxsize), this.slots, ["Index", "Value"], this.admin, this.version);
else if (this.type === "process") this.data = new DB("db_" + this.name, ["N°", "Name", "Author", "Description", "Content", "Bit size"], this.slots, range(1, 1, this.maxsize)); //database(this.name, range(1, 1, this.maxsize), this.slots, ["N°", "Name", "Author", "Description", "Content", "Bit size"], this.admin, this.version);
else if (this.type === "storage") this.data = new DB("db_" + this.name, ["Key", "Value"], this.slots, range(1, 1, this.maxsize));//database(this.name, range(1, 1, this.maxsize), this.slots, ["Key", "Value"], this.admin, this.version);
else if (this.type === "authentication") this.data = new DB("db_" + this.name, ["Username", "Password", "Email"], this.slots, range(1, 1, this.maxsize));//database(this.name, range(1, 1, this.maxsize), this.slots, ["Username", "Password", "Email", "Hash"], this.admin, this.version);
else if (this.type === "register" || this.type =="details") this.data = new DB("db_" + this.name, ["First name", "Last Name", "Title", "Email", "Phone", "Sex", "City/Country", "Birthday", "Websites", "Job", "Quote"], this.slots, range(1, 1, this.maxsize));//database(this.name, range(1, 1, this.maxsize), this.slots, ["First name", "Last Name", "Title", "Email", "Phone", "Sex", "City/Country", "Birthday", "Websites", "Job", "Quote"], this.admin, this.version);
else if (this.type === "location") this.data = new DB("db_" + this.name, ["Name", "Longitude", "Latitude"], this.slots, range(1, 1, this.maxsize));//database(this.name, range(1, 1, this.maxsize), this.slots, ["Name", "Longitude", "Latitude"], this.admin, this.version);
else throw new Error(this.type + " is an invalid server type.");
this.addProcess = function (pcs) {
this.event = new Event("processAdded");
if (pcs.sig.last() === "-" || pcs.bitsize > this.maxsize / this.nb_slots) console.log("[Server:" + name + "] The process named " + pcs.name + " has been rejected");
else {
var pos;
for (var i = 0; i < this.nb_slots; i++) {
if (isNon(this.slots[i])) {
this.slots[i] = [pcs.name, pcs.author, pcs.description, pcs.content, pcs.bitsize];
pos = i;
i = this.nb_slots;
break;
}
}
if (this.slots[pos] != [pcs.name, pcs.author, pcs.description, pcs.content, pcs.bitsize] && this.nb_slots < this.maxsize) { //Check if the process was added to the server
this.nb_slots += this.maxsize / this.nb_slots; //Extend by one slot
this.slots[this.nb_slots] = [pcs.name, pcs.author, pcs.description, pcs.content, pcs.bitsize];
}
}
this.event = null;
};
this.add = function (data) {
this.event = new Event("added");
var pos;
for (var i = 0; i < this.nb_slots; i++) {
if (isNon(this.slots[i]) || this.slots[i].equals([])) {
this.slots[i] = JSON.stringify(data);
pos = i;
i = this.nb_slots;
break;
}
}
if (this.slots[pos] != data && this.nb_slots < this.maxsize) { //Check if the process was added to the server
this.nb_slots += this.maxsize / this.nb_slots; //Extend by one slot
this.slots[this.nb_slots] = JSON.stringify(data);
}
this.event = null;
};
this.rm = function (n) {
this.event = new Event("remove");
this.slots[n] = null;
this.event = null;
};
this.store = function () {
this.event = new Event("storage");
localStorage["server_" + this.name] = JSON.stringify(this);
this.event = null;
};
this.update = function () {
this.event = new Event("update");
if (localStorage["server_" + this.name]) {
var self = JSON.parse(localStorage["server_" + this.name]);
this.name = self.name;
this.admin = self.admin;
this.version = self.version;
this.maxsize = self.maxsize;
this.nb_slots = self.nb_slots;
this.slots = self.slots;
this.data = new DB(self.data.name, self.data.head, self.data.val, self.data.rows) || self.data;
}else this.store();
this.event = null;
};
this.fire = function (pcs) {
this.event = new Event("fire");
for (var i in this.slots) {
if (this.slots.hasOwnProperty(i) && this.slots[i][0] === pcs.name && this.slots[i][1] === pcs.author) this.rm(i);
}
this.event = null;
};
this.reset = function () {
this.event = new Event("reset");
for (var i in this.slots) {
if (this.slots.hasOwnProperty(i)) this.rm(i)
}
this.event = null;
};
this.toString = function () {
return "server(name=" + this.name + ", admin=" + this.admin + ", type=" + this.type + ", version=" + this.version + ", maxsize=" + this.maxsize +", slots=[" + this.slots.toStr(true) + "])";
};
//Events listeners
//this.listeners = Tablify(["processAdded", "added", "remove", "update", "fire", "reset", "storage"], false);
this.on = function (evt, handler) {
if (this.event.type === evt) handler(this.event);
};
Essence.addServer(this);
return this;
}
/**
* @description Data to temporally place in a semi-global scope
* @type {*}
* @default
* @since 1.1
* @external module:essence~$G
* @memberof external:essence~$G
*/
$G["data"] = null;
/**
* @description WiFi speed(s)
* @type {NumberLike[]}
* @default
* @since 1.1
* @external module:essence~$G
* @memberof external:essence~$G
*/
$G["wifi"] = [];
/**
* @description Launch the verification of the connection
* @param {string} id ID of the element to be used
* @param {string} [src="../img/random2000x2000.jpg"] Source of the image to use for the test
* @param {number} [sz=610320] Size of the image (in Kb)
* @param {number} [delay=100] Delay (in ms)
* @param {number} [maxDelay=2e4] Maximum delay
* @returns {undefined}
* @see module:Web~CETimer
* @since 1.0
* @func
*/
function CECheck (id, src, sz, delay, maxDelay) {
window.defaultStatus = "Evalue the connexion and see the downloading speed";
if (!src) src = "../img/random2000x2000.jpg";
if (!sz) sz = 60320; //7.54MB -> Kb
if (!delay) delay = 100; //ms
if (!maxDelay) maxDelay = 2e4;
var img = new Image();
$G["t1"] = new Date().getTime();
$G["t2"] = 0;
img.src = src + "?t1=" + $G["t1"]; //To prevent the browser to load a cached version of the image
$G["data"] = img;
$e("#" + id).write("Verification in progress...");
setTimeout("CETimer('" + id + "', '" + img + "', 0, " + delay + ", " + maxDelay + ", " + sz + ")", delay); //Uncaught SyntaxError: Unexpected identifier
}
/**
* @description Connection evaluation timer
* @param {string} id ID of the element to be used
* @param {HTMLImageElement} img Image to use for the test
* @param {number} nb Number of attempts done
* @param {number} [delay=100] Delay (in ms)
* @param {number} [maxDelay=2e4] Maximum delay
* @param {number} size Size of the image (in kb)
* @returns {undefined}
* @see module:Web~CECheck
* @since 1.0
* @func
*/
function CETimer (id, img, nb, delay, maxDelay, size) {
nb++;
if (!isType(img, "HTMLImageElement")) img = $G["data"]; //Assuming CECheck() was called before
$e("#" + id).write("Verification in progress...");
if (nb * delay >= maxDelay) $e("#" + id).write(evalDownload(0)); //End of the maximum delay
else {
if (img.complete) {
$G["t2"] = new Date().getTime();
$G["wifi"].push((size / ($G["t2"] - $G["t1"])).toNDec(3) + "kbps");
$e("#" + id).write(evalDownload(size / ($G["t2"] - $G["t1"])));
Essence.time("Connexion: " + (size / ($G["t2"] - $G["t1"]).toNDigits() + " kbps"));
window.defaultStatus = "normal";
} else setTimeout("CETimer(" + id + ", " + img + ", " + nb + ", " + delay + ", " + maxDelay + ", " + size + ")", delay);
}
}
/*
ping (ms)
<0, 30>: excellent
>30, 60>: really good
>60, 100>: good
>100, 200>: okay
>200, 300>: poor
>300: bad
latency is the client-server packet transmit when ping is the go and return of that
gigue (ms): connexion fluctuation
<5: synchronized connexion
download speed (kbps/Mbps):
<0, 56>: low debit
>56, 8M>: ADSL debit
>8M, 20M>: ADSL2+
>20M, 50M>: wire
>50M, 100M>: optical fibre
>100M: Ethernet
upload speed:
<0, 56>: low debit
>56, 1M>: ADSL+
>1M, 5M>: wire
>5M, 100M>: Ethernet
*/
/**
* @description Evaluate the download speed
* @param {number} kbps Speed (in kb/start)
* @returns {string} Result
* @since 1.0
* @func
*/
function evalDownload (kbps) {
var res = "";
if (kbps === 0) res = "No connexion";
else if (kbps > 0 && kbps <= 56) res = "Low debit";
else if (kbps > 56 && kbps <= 8e3) res = "ADSL debit";
else if (kbps > 8e3 && kbps <= 2e4) res = "ADSL2 + ";
else if (kbps > 2e4 && kbps <= 5e4) res = "wire";
else if (kbps > 5e4 && kbps <= 1e5) res = "optical fibre";
else res = "Ethernet";
return res + " (" + kbps.toNDigits() +" kbps)"
}
/**
* @description Evaluate the upload speed
* @param {number} kbps Speed (in kb/start)
* @returns {string} Result
* @since 1.0
*/
function evalUpload (kbps) {
var res = "";
if (kbps === 0) res = "No connexion";
else if (kbps > 0 && kbps <= 56) res = "Low debit";
else if (kbps > 56 && kbps <= 1e3) res = "ADSL + ";
else if (kbps > 1e3 && kbps <= 5e3) res = "wire";
else res = "Ethernet";
return res + " (" + kbps.toNDigits() +" kbps)"
}
/**
* @description Evaluate the ping speed
* @param {number} ms Number of milliseconds
* @returns {string} Result
* @since 1.0
* @func
*/
function evalPing (ms) {
var res = "";
if (ms > 0 && ms <= 30) res = "Excellent";
else if (ms > 30 && ms <= 60) res = "Really good";
else if (ms > 60 && ms <= 100) res = "Good";
else if (ms > 100 && ms <= 200) res = "Okay";
else if (ms > 200 && ms <= 300) res = "Poor";
else if (ms > 300 && ms <= 400) res = "Bad";
else res = "Really bad";
return res + " (" + ms +" ms)"
}
/**
* @description Evaluate the gigue (for either upload or download speed)
* @param {NumberLike[]} [vals=$G["wifi"]] WiFi test's values
* @func
* @since 1.1
* @return {string} Gigue
*/
function evalGigue (vals) {
var sd = (vals || $G["wifi"]).map(function (v) {
return getNumFromStr(v)
}).stddev(), res;
if (sd >= 0 && sd < 5) res = "Synchronized connexion";
else if (sd >= 5 && sd < 10) res = "Slightly synchronized connexion";
else if (sd >= 10) res = "Asynchronous connexion";
return res + " (" + sd + ")"
}
/**
* @description WiFi tester.
* @global
* @since 1.1
* @type {{down: number, up: number, ping: number, gigues: number[], type: string, vals: Array, imgs: Object[], elm: Element, test: WifiTest.test, testUp: WifiTest.testUp, testDown: WifiTest.testDown, testPing: WifiTest.testPing}}
* @this WifiTest
* @property {number} WifiTest.down Download speed
* @property {number} WifiTest.up Upload speed
* @property {number} WifiTest.ping Ping
* @property {number[]} WifiTest.gigue Gigue for respectively uploads and downloads
* @property {string} WifiTest.type WiFi type
* @property {NumberLike[]} WifiTest.vals Buffered wifi test values
* @property {Object[]} WifiTest.imgs Images for the tests
* @property {Element} WifiTest.elm HTML element for outputs
* @property {function(): string} WifiTest.test Global test
* @property {function(): string} WifiTest.testUp Upload speed test
* @property {function(): string} WifiTest.testDown Download speed test
* @property {function(): string} WifiTest.testPing Ping test
*/
var WifiTest = {
down: 0,
up: 0,
ping: 0,
gigues: [0, 0], //Up, Down
type: "",
vals: [],
imgs: [{ //images with the name "randomSxS.jpg"
dim: 350, //px
size: 1912 //239KB -> Kb
}, {
dim: 500,
size: 3944 //493KB
}, {
dim: 750,
size: 8480 //1.06MB
}, {
dim: 1e3,
size: 15120 //1.89MB
}, {
dim: 1500,
size: 34080 //4.26MB
}, {
dim: 2e3,
size: 60320 //7.54MB
}, {
dim: 2500,
size: 94400 //11.8MB
}, {
dim: 3e3,
size: 135200 //16.9MB
}, {
dim: 3500,
size: 184800 //23.1MB
}, {
dim: 4e3,
size: 240800 //30.1MB
}],
elm: $e("#wifi", true) || $e("#wifitest", true) || $e("#test", true),
test: function () {
this.testUp();
this.testDown();
this.testPing();
return "Ping/Up/Down/Gigue up/Gigue down: " + [this.ping, this.up, this.down, this.gigues[0], this.gigues[1]].join("/");
},
testUp: function () {
//...
this.vals = $G["wifi"];
$G["wifi"] = [];
//this.up = ...
this.type = evalUpload(this.up).split(" ")[0];
this.gigues[0] = parseFloat(evalGigue(this.vals).split(" ")[0]);
return this.up + " Ko/start";
},
testDown: function () {
var iState = this.elm.val();
for (var i = 0; i < this.imgs;) {
CECheck(this.elm.node.id, "../img/random" + this.imgs[i].dim + "x" + this.imgs[i].dim + ".jpg", this.imgs[i].size);
while (this.elm.val() === iState || window.defaultStatus != "normal") {
//wait
}
if (this.elm.val() != iState || window.defaultStatus === "normal") i++;
}
this.vals = $G["wifi"];
$G["wifi"] = [];
this.down = (this.vals.map(function (x) {
return getNumFromStr(x);
}).mean(2) / 8).toNDec(2);
this.type = evalDownload(this.down).split(" ")[0];
this.gigues[1] = parseFloat(evalGigue(this.vals).split(" ")[0]);
return this.down + " Ko/start";
},
testPing: function () {
//...
this.type = evalPing(this.ping).split(" ")[0];
return this.ping + "ms";
}
};
/**
* @description Getting the URL parameters just like in PHP.
* @param {Str} p Parameter(start)
* @param {function(...string)} action Action to be done with the value(start) of the parameter(start)
* @returns {undefined}
* @since 1.0
* @func
*/
function parseURL (p, action) { //Doing some PHP without PHP :) !!
var urlQuery = location.href.split("?");
if (urlQuery.length > 1) {
var urlTerms = urlQuery[1].split("&");
if (isType(p, "Array")) { //Multi parameter parsing
for (var j = 0; j < p.length; j++) {
for (var i = 0; i < urlTerms.length; i++) {
var param = urlTerms[i].split("=");
if (param[0] === p[j]) {
if (isType(action, "Array")) action[j](param[1], param[0]);
else action(param[1], param[0]);
}
}
}
} else {
for (i = 0; i < urlTerms.length; i++) {
param = urlTerms[i].split("=");
if (param[0] === p) action(param[1]);
}
}
}
}
/**
* @description Web page builder.
* Structure components:
- header: header with a title and a logo
- h-menu: horizontal menu with icons
- v-menu: vertical menu
- content: "welcome to " + this.title
- aside: side section for news feed or anything you want to use it for
- footer: footer with the sponsors (if there's at least one), name of the author(s)
- article: new paper article like section
- search: search bar
Structuration:
! : new line (header!h-menu means that the h-menu is under the header)
| : at the right (content|aside means that the aside section is placed on the right of the content section)
* @param {string} [title="My web page"] Title
* @param {string} [name="index.html"] Name
* @param {string} [path="index.html"] Path
* @param {string} [author="Maximilian Berkmann"] Author
* @param {number} [ver=1.0] Version
* @param {string} [stct="header!h-menu!content|aside!footer"] Structure
* @param {string} [type="html"] Type
* @param {string} [subtitle="A simple web page"] Subtitle
* @constructor
* @this {WebPage}
* @returns {WebPage} Web page
* @see module:Web~WebApp
* @since 1.0
* @property {string} WebPage.title Title
* @property {string} WebPage.subtitle Subtitle
* @property {string} WebPage.type Document type
* @property {string} WebPage.name Name of the file
* @property {string} WebPage.path Path of the file
* @property {string} WebPage.author Author
* @property {number} WebPage.version Version
* @property {string} WebPage.structure Page structure
* @property {Code} WebPage.code Code
* @property {Code} WebPage.template Code template
* @property {Template|?string} WebPage.page Page
* @property {function(string): Code} WebPage.word2code Word (components) to template code
* @property {function(): Code} WebPage.genTemplate Transform the structure into a template
* @property {function(Object): Code} WebPage.genPage Transform the template into a page
*/
function WebPage (title, name, path, author, ver, stct, type, subtitle) {
this.title = title || "My web page";
this.subtitle = subtitle || "A simple web page";
this.type = type.normal() || "html";
this.name = name + this.type || "index." + this.type;
this.path = path + "/" + this.name || this.name;
this.author = author || "Maximilian Berkmann";
this.version = ver || 1.0;
this.structure = stct || "header!height-menu!content|aside!footer";
this.code = "";
this.template = "";
this.page = null;
/* Structure:
Components:
- header: header with a title and a logo
- height-menu: horizontal menu with icons
- v-menu: vertical menu
- content: "welcome to " + this.title
- aside: side section for news feed or anything you want to use it for
- footer: footer with the sponsors (if there's at least one), name of the author(start)
- article: new paper article like section
- search: search bar
Structuration:
! : new line (header!height-menu means that the height-menu is under the header)
| : at the right (content|aside means that the aside section is placed on the right of the content section)
*/
//Templating /(\{\{)\width * (\}\}) */g
this.word2code = function (word) {
switch (word.normal()) {
case "header": return "<header><img src='img/icon.png' /><hgroup><h1>{{title}}</h1><h3>{{subtitle}}</h3></hgroup></header>";
case "height-menu": return "<menu class='height-menu'><li onClick=''>{{0}}</li><li onClick=''>{{1}}</li><li onClick=''>{{2}}</li></menu>";
case "v-menu": return "<menu class='v-menu'><li onClick=''>{{0}}</li><li onClick=''>{{1}}</li><li onClick=''>{{2}}</li></menu>";
case "content": return "<div id='content'>{{content}}</div>";
case "aside": return "<aside>{{aside}}</aside>";
case "footer": return "<footer>{{footer}}</footer>";
case "article": return "<article id='{{article_title}}'><header>{{article_title}}</header>{{article_content}}<footer>{{article_footer}}</footer></div>";
case "search": return "<form action='search.php' method='post'><input type='search' name='search' /><input type='image' src='img/search.png' alt='?' /></form>";
default: return word
}
};
this.genTemplate = function () {
var cpnt = this.structure.split("!");
this.template = "<table class='none'>";
for (var i = 0; i < cpnt.length; i++) {
this.template += "<tr>";
var sct = cpnt.split("|");
for(var j = 0; j < sct.length; i++) this.template += this.word2code(sct[i]);
this.template += "</tr>";
}
this.template += "</table>";
return this.template
};
this.genPage = function (params) {
this.page = new Template(this.title, this.name, this.template, ["title", "subtitle", "content", "aside", "footer", "0", "1", "2", "article_title", "article_content", "article_footer"]);
//noinspection JSUnresolvedVariable
this.page = this.page.gen({
title: this.title,
subtitle: this.subtitle,
content: params.content || $G["lorem"],
aside: params.aside || "At the side",
footer: params.footer || "An EssenceJS powered web page",
article_content: params.articleContent || "Bla bla bla",
article_title: params.articleTitle || "An article",
article_footer: params.articleFooter || "By John Doe",
0: "Home",
1: "About us",
2: "Contact us"
});
return this.page;
};
return this;
}
/**
* @description Web application
* @param {string} [name="Web App"] Name
* @param {string} [path=""] Path
* @param {string} [author="Maximilian Berkmann"] Author
* @param {number} [ver=1.0] Version
* @param {string} [stct] Structure
* @returns {WebApp} Web app
* @this {WebApp}
* @constructor
* @see module:Web~WebPage
* @since 1.0
* @property {string} WebApp.name Name of the app
* @property {string} WebPage.path Path of the file
* @property {string} WebPage.author Author
* @property {number} WebPage.version Version
* @property {string[]} WebPage.dirs List of directories
* @property {Template} WebPage.pages Pages
* @property {Function} WebPage.build Build the pages
*/
function WebApp (name, path, author, ver, stct) {
this.name = name || "Web App";
this.path = path || "";
this.author = author || "Maximilian Berkmann";
this.version = ver || 1.0;
this.dirs = ["img", "script", "style"]; //All dirs which are subdirectories of the path
this.pages = [new WebPage(this.name, "index", this.path, this.author, this.version, stct), new WebPage("Contact us", "contact", this.path, this.author, this.version, stct), new WebPage("About us", "about", this.path, this.author, this.version, stct)];
this.build = function () { //Generate
for(var i = 0; i < this.pages.length; i++) this.pages[i].genPage();
};
return this;
}
/**
* @description Editor
* @param {string} [id="#editor"] ID of the container
* @param {string} [lang="none"] Language
* @param {?Preview} [prev=null] Preview
* @param {Parser} [parser] Parser
* @param {Toolbar} [tb=new Toolbar()] Toolbar
* @this {Editor}
* @returns {Editor} Editor
* @todo Fill up the syntax highlighting list
* @constructor
* @since 1.0
* @property {string} Editor.id ID of the element
* @property {HTMLElement} Editor.node Node
* @property {string} Editor.linesId ID of the lines' element
* @property {number} Editor.nbLines Number of lines
* @property {string} Editor.language Language
* @property {?Preview} Editor.previewer Previewer
* @property {?Parser} Editor.parser Parser
* @property {Code} Editor.code Code
* @property {virtualHistory} Editor.codeHistory Historic of the code
* @property {Toolbar} Editor.toolbar Toolbar
* @property {Editor} Editor.toolbar.for This editor
* @property {function(string)} Editor.toggleLine Toggle line (when marked)
* @property {function(number)} Editor.update Update
* @property {Function} Editor.clear Clear the editor
* @property {function(string)} Editor.write Clear the editor
* @property {Function} Editor.undo Undo the editor
* @property {Function} Editor.redo Redo the editor
* @property {Function} Editor.save Save the code
* @property {Function} Editor.select Select the code
* @property {Function} Editor.copy Copy the code
* @property {function(boolean)} Editor.paste Paste the code to the editor
* @property {Function} Editor.load Load a file into the editor
* @property {Function} Editor.generate Save the parsed code
* @property {Function} Editor.view See the result
* @property {function((Code), string): (Code)} Editor.highlightSyntax Highlight the syntax
* @property {function(): string} Editor.toString String representation
* @see module:Web~Toolbar
*/
function Editor (id, lang, prev, parser, tb) {
this.id = id || "#editor";
this.node = $n(this.id);
this.linesId = "#lines";
this.linesNode = $n(this.linesId);
this.nbLines = 0;
this.language = lang || "none";
this.previewer = prev || null;
this.parser = parser || (prev? this.previewer.associatedParser: null); //Sort of temporarily uncommented for linting sake
this.code = $e(this.id).val();