nexusframework 0.3.0-beta.73 → 0.3.0-beta.74

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (60) hide show
  1. package/LICENSE.md +55 -55
  2. package/README.md +12 -12
  3. package/about/index.json +5 -5
  4. package/about/index.nhp +4 -4
  5. package/icon.png +0 -0
  6. package/index.d.ts +0 -2
  7. package/index.js +0 -0
  8. package/index.js.map +1 -1
  9. package/index.ts +101 -101
  10. package/indexOfSkeleton.nhp +133 -133
  11. package/legacySkeleton.nhp +22 -22
  12. package/loader/overlay.css +1 -1
  13. package/loader/overlay.html +25 -25
  14. package/loader/overlay.scss +152 -152
  15. package/package.json +102 -90
  16. package/scripts/es5/compat.js +100 -100
  17. package/scripts/es5/compat.min.js +1 -1
  18. package/scripts/es5/loader.js +0 -0
  19. package/scripts/es5/loader.js.map +0 -0
  20. package/scripts/es5/loader.min.js +1 -1
  21. package/scripts/es5/loader.min.js.map +0 -0
  22. package/scripts/es5/nexusframeworkclient.js +1224 -1224
  23. package/scripts/es5/nexusframeworkclient.js.map +0 -0
  24. package/scripts/es5/nexusframeworkclient.min.js +1 -1
  25. package/scripts/es5/nexusframeworkclient.min.js.map +0 -0
  26. package/scripts/es6/compat.js +64 -64
  27. package/scripts/es6/compat.min.js +1 -1
  28. package/scripts/es6/loader.js +0 -0
  29. package/scripts/es6/loader.js.map +0 -0
  30. package/scripts/es6/loader.min.js +1 -1
  31. package/scripts/es6/loader.min.js.map +0 -0
  32. package/scripts/es6/nexusframeworkclient.js +1146 -1146
  33. package/scripts/es6/nexusframeworkclient.js.map +0 -0
  34. package/scripts/es6/nexusframeworkclient.min.js +1 -1
  35. package/scripts/es6/nexusframeworkclient.min.js.map +0 -0
  36. package/scripts/index.d.ts +259 -259
  37. package/scripts/src/compat.ts +62 -62
  38. package/scripts/src/loader.ts +385 -385
  39. package/scripts/src/nexusframeworkclient.ts +1163 -1163
  40. package/scripts/tsconfig.json +11 -11
  41. package/src/cli.d.ts +1 -0
  42. package/src/cli.js +102 -102
  43. package/src/cli.js.map +1 -1
  44. package/src/cli.ts +101 -101
  45. package/src/compileScripts.d.ts +0 -0
  46. package/src/compileScripts.js +0 -0
  47. package/src/compileScripts.js.map +1 -1
  48. package/src/compileScripts.ts +153 -153
  49. package/src/nexusframework.d.ts +5 -5
  50. package/src/nexusframework.js +44 -37
  51. package/src/nexusframework.js.map +1 -1
  52. package/src/nexusframework.ts +3631 -3622
  53. package/templates/basic/package.json +3 -3
  54. package/templates/basic/www/skeleton.nhp +1 -1
  55. package/templates/bootstrap/package.json +2 -2
  56. package/templates/bootstrap/www/skeleton.nhp +1 -1
  57. package/templates/bootstrap-material-design/package.json +2 -2
  58. package/templates/bootstrap-material-design/www/skeleton.nhp +1 -1
  59. package/tsconfig.json +25 -22
  60. package/types.d.ts +612 -612
@@ -1,1225 +1,1225 @@
1
- var __extends = (this && this.__extends) || (function () {
2
- var extendStatics = Object.setPrototypeOf ||
3
- ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
4
- function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
5
- return function (d, b) {
6
- extendStatics(d, b);
7
- function __() { this.constructor = d; }
8
- d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
9
- };
10
- })();
11
- /// <reference path="../index.d.ts" />
12
- (function (window) {
13
- var BSON = new window['bson'];
14
- Object.defineProperties(window, {
15
- NexusFrameworkTransport: {
16
- configurable: true,
17
- set: function (instance) {
18
- Object.defineProperty(window, "NexusFrameworkTransport", {
19
- value: instance
20
- });
21
- },
22
- get: function () {
23
- var impl;
24
- if ("XMLHttpRequest" in window) {
25
- var NexusFrameworkXMLHttpRequestResponse_1 = /** @class */ (function () {
26
- function NexusFrameworkXMLHttpRequestResponse(request, url, hadData, abResponse) {
27
- this._url = url;
28
- this.request = request;
29
- this.hadData = hadData;
30
- this.abResponse = abResponse;
31
- }
32
- Object.defineProperty(NexusFrameworkXMLHttpRequestResponse.prototype, "url", {
33
- get: function () {
34
- return this.request.responseURL || this._url;
35
- },
36
- enumerable: true,
37
- configurable: true
38
- });
39
- Object.defineProperty(NexusFrameworkXMLHttpRequestResponse.prototype, "code", {
40
- get: function () {
41
- return this.request.status;
42
- },
43
- enumerable: true,
44
- configurable: true
45
- });
46
- Object.defineProperty(NexusFrameworkXMLHttpRequestResponse.prototype, "contentLength", {
47
- get: function () {
48
- return parseInt(this.request.getResponseHeader("content-length")) || this.request.responseText.length;
49
- },
50
- enumerable: true,
51
- configurable: true
52
- });
53
- Object.defineProperty(NexusFrameworkXMLHttpRequestResponse.prototype, "contentFromJSON", {
54
- get: function () {
55
- if (!this.parsedJson)
56
- this.parsedJson = JSON.parse(this.request.responseText);
57
- return this.parsedJson;
58
- },
59
- enumerable: true,
60
- configurable: true
61
- });
62
- Object.defineProperty(NexusFrameworkXMLHttpRequestResponse.prototype, "contentFromBSON", {
63
- get: function () {
64
- if (!this.parsedBson) {
65
- var response = this.request.response;
66
- if (typeof response === "string")
67
- response = Buffer.from(response, "utf8");
68
- else
69
- response = new Buffer(response);
70
- this.parsedBson = BSON.deserialize(response, { promoteValues: true });
71
- }
72
- return this.parsedBson;
73
- },
74
- enumerable: true,
75
- configurable: true
76
- });
77
- Object.defineProperty(NexusFrameworkXMLHttpRequestResponse.prototype, "contentAsString", {
78
- get: function () {
79
- return this.request.responseText;
80
- },
81
- enumerable: true,
82
- configurable: true
83
- });
84
- Object.defineProperty(NexusFrameworkXMLHttpRequestResponse.prototype, "contentAsArrayBuffer", {
85
- get: function () {
86
- return this.request.response;
87
- },
88
- enumerable: true,
89
- configurable: true
90
- });
91
- Object.defineProperty(NexusFrameworkXMLHttpRequestResponse.prototype, "headers", {
92
- get: function () {
93
- if (!this.processedHeaders) {
94
- var headers_1 = this.processedHeaders = {};
95
- this.request.getAllResponseHeaders().split(/\r?\n/g).forEach(function (header) {
96
- var index = header.indexOf(":");
97
- var key, val;
98
- if (index > 0) {
99
- key = header.substring(0, index).trim().toLowerCase();
100
- val = header.substring(index + 1).trim();
101
- }
102
- else
103
- key = header.trim().toLowerCase();
104
- var list = headers_1[key];
105
- if (!list)
106
- list = headers_1[key] = [];
107
- if (val)
108
- list.push(val);
109
- });
110
- }
111
- return this.processedHeaders;
112
- },
113
- enumerable: true,
114
- configurable: true
115
- });
116
- return NexusFrameworkXMLHttpRequestResponse;
117
- }());
118
- var execute_1 = function (method, url, data, cb, extraHeaders, progcb, wantsBinary) {
119
- var request = new XMLHttpRequest();
120
- var responseIsArrayBuffer;
121
- try {
122
- if (!wantsBinary)
123
- throw false;
124
- request.responseType = "arraybuffer";
125
- if (request.responseType !== "arraybuffer") {
126
- console.error("Could not request arraybuffer");
127
- throw false;
128
- }
129
- responseIsArrayBuffer = true;
130
- }
131
- catch (e) {
132
- responseIsArrayBuffer = false;
133
- }
134
- request.open(method, url, true);
135
- if (extraHeaders)
136
- Object.keys(extraHeaders).forEach(function (key) {
137
- request.setRequestHeader(key, extraHeaders[key]);
138
- });
139
- if (progcb)
140
- request.onprogress = function (ev) {
141
- if (ev.lengthComputable && ev.total)
142
- progcb(ev.loaded, ev.total);
143
- };
144
- request.onreadystatechange = function (e) {
145
- if (request.readyState === XMLHttpRequest.DONE)
146
- cb(new NexusFrameworkXMLHttpRequestResponse_1(request, url, !!data, responseIsArrayBuffer));
147
- };
148
- var type = data && data.type;
149
- if (type)
150
- switch (type) {
151
- case "":
152
- case "text/urlencoded":
153
- var dat = "";
154
- for (var _i = 0, _a = data.data; _i < _a.length; _i++) {
155
- var entry = _a[_i];
156
- if (dat)
157
- dat += "&";
158
- dat += encodeURIComponent(entry[0]);
159
- dat += "=";
160
- dat += encodeURIComponent(entry[1]);
161
- }
162
- request.setRequestHeader("Content-Type", "text/urlencoded");
163
- request.send(dat);
164
- break;
165
- case "text/json":
166
- request.setRequestHeader("Content-Type", "text/json");
167
- request.send(JSON.stringify(data.data));
168
- break;
169
- case "application/bson":
170
- request.setRequestHeader("Content-Type", "application/bson");
171
- request.send(BSON.serialize(data.data));
172
- break;
173
- case "multipart/form-data":
174
- request.send(data.data);
175
- break;
176
- default:
177
- request.send(data);
178
- }
179
- else
180
- request.send(data);
181
- };
182
- impl = {
183
- get: function (url, cb, extraHeaders, progcb, wantsBinary) {
184
- execute_1("GET", url, undefined, cb, extraHeaders, progcb, wantsBinary);
185
- },
186
- head: function (url, cb, extraHeaders, progcb, wantsBinary) {
187
- execute_1("HEAD", url, undefined, cb, extraHeaders, progcb, wantsBinary);
188
- },
189
- post: function (url, data, cb, extraHeaders, progcb, wantsBinary) {
190
- execute_1("POST", url, data, cb, extraHeaders, progcb, wantsBinary);
191
- },
192
- put: function (url, data, cb, extraHeaders, progcb, wantsBinary) {
193
- execute_1("PUT", url, data, cb, extraHeaders, progcb, wantsBinary);
194
- },
195
- execute: execute_1,
196
- del: function (url, cb, extraHeaders, progcb, wantsBinary) {
197
- execute_1("DELETE", url, undefined, cb, extraHeaders, progcb, wantsBinary);
198
- }
199
- };
200
- }
201
- else
202
- throw new Error("No transport implementations");
203
- Object.defineProperty(window, "NexusFrameworkTransport", {
204
- value: impl
205
- });
206
- return impl;
207
- }
208
- },
209
- NexusFrameworkImpl: {
210
- configurable: true,
211
- set: function (instance) {
212
- Object.defineProperty(window, "NexusFrameworkImpl", {
213
- value: instance
214
- });
215
- },
216
- get: function () {
217
- var loader = window.NexusFrameworkLoader;
218
- var showError = loader.showError;
219
- var debug = /*function(...args: any[]){};*/ console.log.bind(console);
220
- var GA_ANALYTICS = {
221
- reportError: function (err, fatal) {
222
- if (window.ga)
223
- try {
224
- window.ga('send', 'exception', {
225
- 'exDescription': (err.stack || "" + err).replace(/\n/g, "\n\t"),
226
- 'exFatal': fatal
227
- });
228
- }
229
- catch (e) {
230
- console.warn(e);
231
- }
232
- },
233
- reportEvent: function (category, action, label, value) {
234
- if (window.ga)
235
- try {
236
- window.ga('send', 'event', category, action, label, value);
237
- }
238
- catch (e) {
239
- console.warn(e);
240
- }
241
- },
242
- reportPage: function (path) {
243
- if (window.ga)
244
- try {
245
- if (!path)
246
- path = location.pathname;
247
- window.ga('set', 'page', path);
248
- window.ga('send', 'pageview');
249
- }
250
- catch (e) {
251
- console.warn(e);
252
- }
253
- }
254
- };
255
- var r = document.createElement("a");
256
- var protocol = location.href.match(/^\w+:/)[0];
257
- var resolveUrl = function (url) {
258
- r.setAttribute("href", url);
259
- var href = r.href;
260
- if (/^\/\//.test(href))
261
- href = protocol + href;
262
- return href;
263
- };
264
- var convertResponse = function (res, url) {
265
- if (url === void 0) { url = location.href; }
266
- var storage;
267
- return (typeof res.data === "string") ? {
268
- url: url,
269
- code: res.code,
270
- contentAsString: res.data,
271
- get contentLength() {
272
- return parseInt(res.headers['content-length'] && res.headers['content-length'][0]) || res.data.length;
273
- },
274
- get contentAsArrayBuffer() {
275
- throw new Error("Not implemented");
276
- },
277
- get contentFromBSON() {
278
- if (!storage)
279
- storage = JSON.parse(res.data);
280
- return storage;
281
- },
282
- get contentFromJSON() {
283
- if (!storage)
284
- storage = JSON.parse(res.data);
285
- return storage;
286
- },
287
- headers: res.headers
288
- } : {
289
- url: url,
290
- code: res.code,
291
- get contentAsString() {
292
- if (!storage)
293
- storage = JSON.stringify(res.data);
294
- return storage;
295
- },
296
- get contentAsArrayBuffer() {
297
- throw new Error("Not implemented");
298
- },
299
- contentFromJSON: res.data,
300
- contentFromBSON: res.data,
301
- headers: res.headers,
302
- get contentLength() {
303
- var length = parseInt(res.headers['content-length'] && res.headers['content-length'][0]);
304
- if (length)
305
- return length;
306
- if (!storage)
307
- storage = JSON.stringify(res.data);
308
- return storage.length;
309
- }
310
- };
311
- };
312
- var loaderContainerRegex = /(^|\s)loader\-(progress|error)\-container(\s|$)/;
313
- var NexusFrameworkBase = /** @class */ (function () {
314
- function NexusFrameworkBase(url, io) {
315
- if (url === void 0) { url = "/"; }
316
- this.activerid = 0;
317
- this.components = {};
318
- this.currentUserID = undefined;
319
- this.progressVisible = false;
320
- this.animationTiming = 500;
321
- this.requestPage = this.defaultRequestPage;
322
- this._listeners = {};
323
- url = resolveUrl(url);
324
- if (!/\/$/.test(url))
325
- url += "/";
326
- Object.defineProperties(this, {
327
- io: {
328
- value: io
329
- },
330
- url: {
331
- value: url
332
- }
333
- });
334
- this._analytics = GA_ANALYTICS;
335
- }
336
- NexusFrameworkBase.prototype.resolveUrl = function (url) {
337
- if (/^\w+\:/.test(url))
338
- return url;
339
- return resolveUrl(this.url + url);
340
- };
341
- NexusFrameworkBase.prototype.disableAll = function (root) {
342
- if (root === void 0) { root = document.body; }
343
- var focusable = root.querySelectorAll("a, input, select, textarea, iframe, button, *[focusable], *[tabindex]");
344
- for (var i = 0; i < focusable.length; i++) {
345
- var child = focusable[i];
346
- var tabindex = child.getAttribute("tabindex");
347
- if (tabindex == "-1")
348
- return;
349
- child.setAttribute("data-tabindex", tabindex ? tabindex : "");
350
- child.setAttribute("data-tabindex", "-1");
351
- }
352
- };
353
- NexusFrameworkBase.prototype.enableAll = function (root) {
354
- if (root === void 0) { root = document.body; }
355
- var focusable = root.querySelectorAll("*[data-tabindex]");
356
- for (var i = 0; i < focusable.length; i++) {
357
- var child = focusable[i];
358
- child.setAttribute("tabindex", child.getAttribute("data-tabindex"));
359
- child.removeAttribute("data-tabindex");
360
- }
361
- };
362
- Object.defineProperty(NexusFrameworkBase.prototype, "analytics", {
363
- get: function () {
364
- return this._analytics;
365
- },
366
- set: function (value) {
367
- this._analytics = value ? value : GA_ANALYTICS;
368
- },
369
- enumerable: true,
370
- configurable: true
371
- });
372
- NexusFrameworkBase.prototype.reportError = function (err, fatal) {
373
- console[fatal ? "error" : "warn"](err.stack);
374
- if (this.errorreporter)
375
- this.errorreporter(err, fatal);
376
- };
377
- NexusFrameworkBase.prototype.installErrorReporter = function (errorreporter) {
378
- this.errorreporter = errorreporter;
379
- };
380
- NexusFrameworkBase.prototype.registerComponent = function (selector, impl) {
381
- var _this = this;
382
- if (impl) {
383
- this.components[selector] = impl;
384
- this.createComponents(document.head);
385
- this.createComponents(document.body);
386
- }
387
- else {
388
- delete this.components[selector];
389
- var destroy = function (root) {
390
- var elements = root.querySelectorAll(selector);
391
- if (!elements.length)
392
- return;
393
- for (var i = 0; i < elements.length; i++) {
394
- var element = elements[i];
395
- var components = element['__nf_cmapping__'];
396
- if (!components)
397
- components = element['__nf_cmapping__'] = {};
398
- var component = components[selector];
399
- if (component) {
400
- try {
401
- component.destroy();
402
- }
403
- catch (e) {
404
- _this.reportError(e);
405
- }
406
- }
407
- }
408
- };
409
- destroy(document.head);
410
- destroy(document.body);
411
- }
412
- };
413
- NexusFrameworkBase.prototype.unregisterComponent = function (selector, impl) {
414
- };
415
- NexusFrameworkBase.prototype.createComponents = function (root) {
416
- var _this = this;
417
- Object.keys(this.components).forEach(function (selector) {
418
- var elements = root.querySelectorAll(selector);
419
- if (!elements.length)
420
- return;
421
- for (var i = 0; i < elements.length; i++) {
422
- var element = elements[i];
423
- var components = element['__nf_cmapping__'];
424
- if (!components)
425
- components = element['__nf_cmapping__'] = {};
426
- if (components[selector])
427
- continue;
428
- var componentFactory = _this.components[selector];
429
- try {
430
- (components[selector] = new componentFactory()).create(element);
431
- }
432
- catch (e) {
433
- _this.reportError(e);
434
- }
435
- }
436
- });
437
- };
438
- NexusFrameworkBase.prototype.destroyComponents = function (root) {
439
- var _this = this;
440
- Object.keys(this.components).forEach(function (selector) {
441
- var elements = root.querySelectorAll(selector);
442
- if (!elements.length)
443
- return;
444
- for (var i = 0; i < elements.length; i++) {
445
- var element = elements[i];
446
- var components = element['__nf_cmapping__'];
447
- if (!components)
448
- components = element['__nf_cmapping__'] = {};
449
- var component = components[selector];
450
- if (component) {
451
- try {
452
- component.destroy();
453
- }
454
- catch (e) {
455
- _this.reportError(e);
456
- }
457
- }
458
- }
459
- });
460
- };
461
- NexusFrameworkBase.prototype.restoreComponents = function (root, state) {
462
- var _this = this;
463
- Object.keys(this.components).forEach(function (selector) {
464
- var states = state[selector];
465
- if (!states)
466
- return;
467
- var elements = root.querySelectorAll(selector);
468
- if (!elements.length)
469
- return;
470
- for (var i = 0; i < elements.length; i++) {
471
- var element = elements[i];
472
- if (states.length) {
473
- var _state = states.shift();
474
- if (_state) {
475
- var components = element['__nf_cmapping__'];
476
- if (!components)
477
- components = element['__nf_cmapping__'] = {};
478
- var component = components[selector];
479
- if (!component) {
480
- var componentFactory = _this.components[selector];
481
- try {
482
- (component = components[selector] = new componentFactory()).create(element);
483
- }
484
- catch (e) {
485
- _this.reportError(e);
486
- console.warn(e);
487
- continue;
488
- }
489
- }
490
- try {
491
- component.restore(_state);
492
- }
493
- catch (e) {
494
- _this.reportError(e);
495
- console.warn(e);
496
- }
497
- }
498
- }
499
- else
500
- break;
501
- }
502
- });
503
- };
504
- NexusFrameworkBase.prototype.saveComponents = function (root) {
505
- var _this = this;
506
- var state = {};
507
- Object.keys(this.components).forEach(function (selector) {
508
- var states = state[selector] = [];
509
- var elements = root.querySelectorAll(selector);
510
- if (!elements.length)
511
- return;
512
- for (var i = 0; i < elements.length; i++) {
513
- var element = elements[i];
514
- var components = element['__nf_cmapping__'];
515
- if (!components)
516
- components = element['__nf_cmapping__'] = {};
517
- var component = components[selector];
518
- if (!component) {
519
- var componentFactory = _this.components[selector];
520
- try {
521
- (component = components[selector] = new componentFactory).create(element);
522
- }
523
- catch (e) {
524
- states.push(undefined);
525
- _this.reportError(e);
526
- console.warn(e);
527
- continue;
528
- }
529
- }
530
- try {
531
- states.push(component.save());
532
- }
533
- catch (e) {
534
- states.push(undefined);
535
- _this.reportError(e);
536
- console.warn(e);
537
- }
538
- }
539
- });
540
- return state;
541
- };
542
- NexusFrameworkBase.prototype.initPageSystem = function (opts) {
543
- var _this = this;
544
- if (!loader)
545
- return console.error("The NexusFramework Loader is required for the dynamic Page System to work correctly.");
546
- if (!history.pushState || !history.replaceState)
547
- return console.warn("This browser is missing an essential feature required for the dynamic Page System.");
548
- if (this.pagesyshandler)
549
- return console.warn("Page System already initialized, ignoring additional requests to initialize.");
550
- opts = opts || {};
551
- var self = this;
552
- var pageStatesSize = 0;
553
- var pageStates = [];
554
- this.animationTiming = opts.animationTiming || 500;
555
- var pageStateCacheSize = opts.pageHistoryCacheSize || 25000000;
556
- var wrapCB = function (cb) {
557
- return function (res) {
558
- var user = res.headers['x-logged-user'];
559
- if (user)
560
- self.currentUserID = user[0];
561
- else
562
- self.currentUserID = undefined;
563
- debug("Current UID", self.currentUserID);
564
- cb(res);
565
- if (res.code === 200)
566
- setTimeout(function () {
567
- self._analytics.reportPage();
568
- });
569
- };
570
- };
571
- loader.showError = function (error) {
572
- var match = location.href.match(beforeHash);
573
- var baseurl = match && match[1] || location.href;
574
- var length = error.stack.length;
575
- var tooBig = pageStateCacheSize <= length;
576
- try {
577
- var cPageStates = pageStates.length;
578
- for (var i = 0; i < cPageStates; i++) {
579
- var state = pageStates[i];
580
- if (state.url === baseurl) {
581
- if (tooBig) {
582
- pageStates.splice(i, 1);
583
- pageStatesSize -= state.size;
584
- }
585
- else {
586
- state.data = { error: error };
587
- state.updated = +new Date;
588
- pageStatesSize += length - state.size;
589
- }
590
- throw true;
591
- }
592
- }
593
- if (tooBig)
594
- throw true;
595
- pageStates.push({
596
- url: baseurl,
597
- data: { error: error },
598
- updated: +new Date,
599
- size: length
600
- });
601
- pageStatesSize += length;
602
- }
603
- catch (e) {
604
- if (e !== true)
605
- throw e;
606
- }
607
- return showError(error);
608
- };
609
- var transportPageSystem = {
610
- requestPage: function (path, cb, post, rid) {
611
- if (self.pagesysprerequest && !self.pagesysprerequest(path)) {
612
- self.defaultRequestPage(path, post);
613
- return;
614
- }
615
- if (!opts.noprogress) {
616
- self.disableAll();
617
- loader.showProgress();
618
- }
619
- var url = self.resolveUrl(":pagesys/" + path);
620
- var _cb = opts.noprogress ? cb : function (res) {
621
- if (opts.noprogress)
622
- cb(res);
623
- else
624
- loader.showProgress(function () {
625
- cb(res);
626
- self.enableAll();
627
- });
628
- };
629
- if (post)
630
- window.NexusFrameworkTransport.post(url, post, wrapCB(_cb), undefined, undefined, true);
631
- else
632
- window.NexusFrameworkTransport.get(url, wrapCB(_cb), undefined, undefined, true);
633
- }
634
- };
635
- if (!opts.noio && this.io) {
636
- var io_1 = this.io;
637
- this.pagesysimpl = {
638
- requestPage: function (path, cb, post, rid) {
639
- if (io_1.connected) {
640
- if (self.pagesysprerequest && !self.pagesysprerequest(path)) {
641
- self.defaultRequestPage(path, post);
642
- return;
643
- }
644
- if (!opts.noprogress) {
645
- self.disableAll();
646
- loader.showProgress();
647
- }
648
- var headers = {
649
- "referrer": location.href
650
- };
651
- var val = document.cookie;
652
- if (val)
653
- headers['cookie'] = val;
654
- io_1.emit("page", post ? "POST" : "GET", path, post, headers, function (res) {
655
- if (rid != self.activerid)
656
- return;
657
- var cookies = res.headers['set-cookie'];
658
- if (cookies) {
659
- cookies.forEach(function (cookie) {
660
- document.cookie = cookie;
661
- });
662
- }
663
- var _cb = opts.noprogress ? cb : function (res) {
664
- if (!opts.noprogress)
665
- cb(res);
666
- else
667
- loader.showProgress(function () {
668
- cb(res);
669
- self.enableAll();
670
- });
671
- };
672
- wrapCB(_cb)(convertResponse(res, self.resolveUrl(path)));
673
- });
674
- }
675
- else
676
- transportPageSystem.requestPage(path, cb, post, rid);
677
- }
678
- };
679
- }
680
- else
681
- this.pagesysimpl = transportPageSystem;
682
- var base = document.getElementsByTagName("base");
683
- base = base && base[0];
684
- this.pagesysprerequest = opts.prerequest;
685
- this.pagesyshandler = opts.handler || (function (res, pageReady) {
686
- try {
687
- var contentType = res.headers['content-type'][0];
688
- if (!/\/html(;.+)?$/.test(contentType.toLowerCase())) {
689
- throw new Error("Content type is not html");
690
- }
691
- }
692
- catch (e) { }
693
- var content = res.contentAsString;
694
- var bodyindex = content.indexOf("<body");
695
- if (bodyindex == -1)
696
- throw new Error("Could not find start of body tag");
697
- var endbodyindex = content.indexOf("</body>") || content.indexOf("</ body>");
698
- if (endbodyindex == -1)
699
- throw new Error("Could not find end of body tag");
700
- var title = content.match(/<title>([^<]+)<\/\s*title>/);
701
- document.title = title && title[1] || "Title Not Set";
702
- var mockhtml = document.createElement("html");
703
- mockhtml.innerHTML = content.substring(bodyindex, endbodyindex) + "</body>";
704
- var loaderScript;
705
- var childs = mockhtml.children;
706
- var toAdd = [];
707
- for (var i = 0; i < childs.length; i++) {
708
- var child = childs[i];
709
- switch (child.nodeName.toUpperCase()) {
710
- case "HEAD":
711
- break;
712
- case "BODY":
713
- i = -1;
714
- childs = child.children;
715
- break;
716
- case "SCRIPT":
717
- var match = child.innerHTML.match(/^NexusFrameworkLoader\.load\((.+)\);?$/);
718
- if (match)
719
- loaderScript = JSON.parse(match[1]);
720
- break;
721
- default:
722
- if (loaderContainerRegex.test(child.className))
723
- break;
724
- toAdd.push(child);
725
- }
726
- }
727
- if (!toAdd.length)
728
- throw new Error("Nothing found in response to add to page");
729
- if (!loaderScript)
730
- throw new Error("NexusFrameworkLoader script not found...");
731
- childs = document.body.children;
732
- for (var i = childs.length - 1; i >= 0; i--) {
733
- var child = childs[i];
734
- switch (child.nodeName.toUpperCase()) {
735
- case "LINK":
736
- case "SCRIPT":
737
- break;
738
- default:
739
- if (loaderContainerRegex.test(child.className))
740
- break;
741
- document.body.removeChild(child);
742
- }
743
- }
744
- var fragment = document.createDocumentFragment();
745
- toAdd.forEach(function (el) {
746
- fragment.appendChild(el);
747
- _this.createComponents(el);
748
- });
749
- document.body.appendChild(fragment);
750
- loader.load(loaderScript, function () {
751
- self.progressVisible = true;
752
- loader.resetProgress();
753
- pageReady();
754
- });
755
- return true;
756
- });
757
- var forwardPopState;
758
- var beforeHash = /^([^#]+)(#.*)?$/;
759
- var chash = location.href.match(beforeHash)[1];
760
- var startsWith = new RegExp("^" + this.url.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&") + "(.*)$", "i");
761
- var AnchorElementComponent = /** @class */ (function () {
762
- function AnchorElementComponent() {
763
- var _this = this;
764
- this.handler = function (e) {
765
- if (_this.element.hasAttribute("data-nopagesys") || _this.element.hasAttribute("data-nodynamic"))
766
- return;
767
- var href = _this.element.getAttribute("href");
768
- if (!href || !href.length)
769
- return;
770
- var url = _this.element.href;
771
- var bhash = url.match(beforeHash);
772
- if (bhash[2] && chash === bhash[1])
773
- return;
774
- if (startsWith.test(url))
775
- try {
776
- var match = url.match(/^(.+)#.*$/);
777
- if (match)
778
- url = match[1];
779
- self.requestPage(url.substring(self.url.length));
780
- try {
781
- e.stopPropagation();
782
- }
783
- catch (e) { }
784
- try {
785
- e.preventDefault();
786
- }
787
- catch (e) { }
788
- }
789
- catch (e) {
790
- console.warn(e);
791
- }
792
- else
793
- debug("Navigating", url);
794
- };
795
- }
796
- AnchorElementComponent.prototype.create = function (element) {
797
- element.addEventListener("click", this.handler);
798
- this.element = element;
799
- };
800
- AnchorElementComponent.prototype.destroy = function () {
801
- this.element.removeEventListener("click", this.handler);
802
- };
803
- AnchorElementComponent.prototype.restore = function (data) { };
804
- AnchorElementComponent.prototype.save = function () { };
805
- return AnchorElementComponent;
806
- }());
807
- var FormElementComponent = /** @class */ (function () {
808
- function FormElementComponent() {
809
- var _this = this;
810
- this.handler = function (e) {
811
- if (_this.element.hasAttribute("data-nopagesys") || _this.element.hasAttribute("data-nodynamic"))
812
- return;
813
- var method = _this.element.getAttribute("method") || "get";
814
- var enctype = _this.element.getAttribute("enctype") || "text/urlencoded";
815
- var action = _this.element.getAttribute("action") || "";
816
- var url = resolveUrl(action);
817
- if (startsWith.test(url)) {
818
- try {
819
- var match = url.match(/^(.+)#.*$/);
820
- if (match)
821
- url = match[1];
822
- var formData = new FormData(_this.element);
823
- loader.showProgress(undefined, "Submitting Form", "Your form data is being processed");
824
- if (method.trim().toLowerCase() === "post")
825
- self.requestPage(url.substring(self.url.length), { type: enctype, data: formData });
826
- else {
827
- var first = true;
828
- var reqUrl = url.substring(self.url.length) + "?";
829
- for (var _i = 0, _a = formData; _i < _a.length; _i++) {
830
- var entry = _a[_i];
831
- if (first)
832
- first = false;
833
- else
834
- reqUrl += "&";
835
- reqUrl += encodeURIComponent(entry[0]);
836
- reqUrl += "=";
837
- reqUrl += encodeURIComponent(entry[1]);
838
- }
839
- self.requestPage(reqUrl);
840
- }
841
- try {
842
- e.stopPropagation();
843
- }
844
- catch (e) { }
845
- try {
846
- e.preventDefault();
847
- }
848
- catch (e) { }
849
- Array.prototype.forEach.call(_this.element.querySelectorAll("a, input, select, textarea, iframe, button, *[name]"), function (el) {
850
- el.setAttribute("disabled", "disabled");
851
- el.className += " disabled";
852
- });
853
- Array.prototype.forEach.call(_this.element.querySelectorAll("button:not([type]), button[type=submit]"), function (button) {
854
- button.innerHTML = "Processing Request";
855
- });
856
- Array.prototype.forEach.call(_this.element.querySelectorAll("input[type=submit]"), function (input) {
857
- input.value = "Processing Request";
858
- });
859
- return;
860
- }
861
- catch (e) {
862
- debug(e);
863
- }
864
- }
865
- debug("Submitting", url);
866
- };
867
- }
868
- FormElementComponent.prototype.create = function (element) {
869
- element.addEventListener("submit", this.handler);
870
- this.element = element;
871
- };
872
- FormElementComponent.prototype.destroy = function () {
873
- this.element.removeEventListener("submit", this.handler);
874
- };
875
- FormElementComponent.prototype.restore = function (data) { };
876
- FormElementComponent.prototype.save = function () { };
877
- return FormElementComponent;
878
- }());
879
- var requestPage = this.requestPage = function (path, post, replace) {
880
- if (replace === void 0) { replace = false; }
881
- var match = path.match(/\.([^\/]+)([\?#].*)?$/);
882
- if (match && match[0] !== "htm" && match[0] !== "html") {
883
- _this.defaultRequestPage(path, post);
884
- debug("Ignoring navigation", path, match);
885
- }
886
- else {
887
- debug("requestPage", path);
888
- var rid_1 = ++self.activerid;
889
- var baseurl_1 = _this.resolveUrl(path);
890
- var fullurl_1 = baseurl_1 + (match && match[2] || "");
891
- if (replace)
892
- history.replaceState(!!post, "Loading...", fullurl_1);
893
- else {
894
- history.pushState(!!post, "Loading...", fullurl_1);
895
- chash = fullurl_1.match(beforeHash)[1];
896
- window.scrollTo(0, 0);
897
- }
898
- loader.resetError();
899
- loader.resetProgress();
900
- _this.pagesysimpl.requestPage(path, function (res) {
901
- try {
902
- if (rid_1 !== self.activerid)
903
- return;
904
- if (!res.code) {
905
- loader.showProgress(undefined, "Connection Issue", "Check your network and try again");
906
- document.title = "Connection Issue";
907
- history.replaceState(res.hadData, "Connection Issue", fullurl_1);
908
- setTimeout(function () {
909
- if (rid_1 !== self.activerid)
910
- return;
911
- requestPage(path, post, true);
912
- }, 900000);
913
- return;
914
- }
915
- else if (res.code === 502 || res.code === 503) {
916
- loader.showProgress(undefined, "Scheduled Maintenance", "Sorry but this website is undergoing scheduled maintenance");
917
- document.title = "Scheduled Maintenance";
918
- history.replaceState(res.hadData, "Scheduled Maintenance", fullurl_1);
919
- setTimeout(function () {
920
- if (rid_1 !== self.activerid)
921
- return;
922
- requestPage(path, post, true);
923
- }, 900000);
924
- return;
925
- }
926
- var location_1 = res.headers['x-location'] || res.headers['location'];
927
- if (location_1) {
928
- var url = resolveUrl(location_1[0]);
929
- if (startsWith.test(url)) {
930
- _this.requestPage(url.substring(_this.url.length), undefined, true);
931
- return;
932
- }
933
- console.warn("Requested redirect to external url:", url);
934
- window.location.href = url;
935
- return;
936
- }
937
- var contentDisposition = res.headers['content-disposition'];
938
- if (contentDisposition && contentDisposition[0].toLowerCase() == "attachment")
939
- throw new Error("Attachment disposition");
940
- _this.pagesyshandler(res, function () {
941
- if (rid_1 !== self.activerid)
942
- return;
943
- try {
944
- var title = document.title;
945
- var length_1 = res.contentLength;
946
- var tooBig = pageStateCacheSize <= length_1;
947
- var stateData = tooBig ? undefined : {
948
- title: title,
949
- user: self.currentUserID,
950
- scroll: [window.scrollX, window.scrollY],
951
- body: self.saveComponents(document.body),
952
- basehref: base ? base['href'] : undefined,
953
- response: res
954
- };
955
- var i;
956
- var cPageStates = pageStates.length;
957
- try {
958
- for (i = 0; i < cPageStates; i++) {
959
- var state = pageStates[i];
960
- if (state.url === baseurl_1) {
961
- if (tooBig) {
962
- pageStates.splice(i, 1);
963
- pageStatesSize -= state.size;
964
- }
965
- else {
966
- state.data = stateData;
967
- state.updated = +new Date;
968
- pageStatesSize += length_1 - state.size;
969
- }
970
- throw true;
971
- }
972
- }
973
- if (!tooBig) {
974
- pageStates.push({
975
- url: baseurl_1,
976
- data: stateData,
977
- updated: +new Date,
978
- size: length_1
979
- });
980
- pageStatesSize += length_1;
981
- throw true;
982
- }
983
- else
984
- debug("Response too big to store", baseurl_1, length_1);
985
- }
986
- catch (e) {
987
- if (e === true) {
988
- var over = pageStatesSize - pageStateCacheSize;
989
- if (over > 0) {
990
- pageStates.sort(function (a, b) {
991
- return a.updated - b.updated;
992
- });
993
- var found = 0;
994
- for (i = 0; i < cPageStates; i++) {
995
- found += pageStates[i].size;
996
- if (found >= over)
997
- break;
998
- }
999
- i++;
1000
- pageStates.splice(0, i);
1001
- debug("Trimmed", i, "items...");
1002
- }
1003
- }
1004
- else
1005
- throw e;
1006
- }
1007
- history.replaceState(res.hadData, document.title, fullurl_1);
1008
- self.emit("page", baseurl_1, path);
1009
- }
1010
- catch (e) {
1011
- debug(e);
1012
- if (replace)
1013
- window.location.reload(true);
1014
- else
1015
- try {
1016
- chash = undefined;
1017
- forwardPopState = [path, post];
1018
- debug("Going backwards");
1019
- history.go(-1);
1020
- }
1021
- catch (e) { }
1022
- }
1023
- });
1024
- }
1025
- catch (e) {
1026
- debug(e);
1027
- if (replace)
1028
- window.location.reload(true);
1029
- else
1030
- try {
1031
- chash = undefined;
1032
- forwardPopState = [path, post];
1033
- debug("Going backwards");
1034
- history.go(-1);
1035
- }
1036
- catch (e) { }
1037
- }
1038
- }, post, rid_1);
1039
- }
1040
- };
1041
- this.registerComponent("a", AnchorElementComponent);
1042
- this.registerComponent("form", FormElementComponent);
1043
- window.addEventListener('popstate', function (e) {
1044
- try {
1045
- var state;
1046
- var bhash = location.href.match(beforeHash);
1047
- var cStates = pageStates.length;
1048
- var baseurl = bhash[1];
1049
- for (var i = 0; i < cStates; i++) {
1050
- var _state = pageStates[i];
1051
- if (_state.url === baseurl) {
1052
- state = _state;
1053
- break;
1054
- }
1055
- }
1056
- var hasState = !!state;
1057
- var rid_2 = ++self.activerid;
1058
- var error = hasState && state.data.error;
1059
- if (error) {
1060
- showError(error);
1061
- return;
1062
- }
1063
- if (forwardPopState) {
1064
- debug("forwardPopState", forwardPopState);
1065
- var forward_1 = forwardPopState;
1066
- setTimeout(function () {
1067
- if (rid_2 === self.activerid)
1068
- self.defaultRequestPage(forward_1[0], forward_1[1]);
1069
- });
1070
- forwardPopState = undefined;
1071
- return;
1072
- }
1073
- if (chash === baseurl) {
1074
- debug("Only hash has changed...", baseurl);
1075
- return;
1076
- }
1077
- chash = bhash[1];
1078
- if (e.state)
1079
- alert("You submitted data to this page, which cannot be re-sent. Because of this, what you're viewing may not be the same now.");
1080
- if (!hasState)
1081
- throw new Error("No state for: " + baseurl + ", reloading...");
1082
- if (state.data.user != self.currentUserID)
1083
- throw new Error("User has changed since state was created, reloading...");
1084
- document.title = state.data.title;
1085
- loader.resetProgress();
1086
- loader.resetError();
1087
- self.pagesyshandler(state.data.response, function (err) {
1088
- if (rid_2 !== self.activerid)
1089
- return;
1090
- try {
1091
- if (err)
1092
- throw err;
1093
- if (state.data.body)
1094
- self.restoreComponents(document.body, state.data.body);
1095
- if (state.data.scroll)
1096
- window.scrollTo.apply(window, state.data.scroll);
1097
- debug("Used stored page state!", state);
1098
- }
1099
- catch (err) {
1100
- debug(err);
1101
- var url = location.href;
1102
- if (startsWith.test(url)) {
1103
- try {
1104
- if (/reloading\.\.\.$/.test(err.message)) {
1105
- self.requestPage(url.substring(self.url.length), undefined, true);
1106
- return;
1107
- }
1108
- console.error("Unknown error occured, actually navigating to page...");
1109
- }
1110
- catch (e) {
1111
- console.error(e);
1112
- }
1113
- }
1114
- location.reload(true);
1115
- }
1116
- });
1117
- }
1118
- catch (err) {
1119
- debug(err);
1120
- var url = location.href;
1121
- if (startsWith.test(url)) {
1122
- try {
1123
- if (/reloading\.\.\.$/.test(err.message)) {
1124
- self.requestPage(url.substring(self.url.length), undefined, true);
1125
- return;
1126
- }
1127
- console.error("Unknown error occured, actually navigating to page...");
1128
- }
1129
- catch (e) {
1130
- console.error(e);
1131
- }
1132
- }
1133
- location.reload(true);
1134
- }
1135
- });
1136
- return true;
1137
- };
1138
- Object.defineProperty(NexusFrameworkBase.prototype, "currentUser", {
1139
- get: function () {
1140
- return this.currentUserID;
1141
- },
1142
- enumerable: true,
1143
- configurable: true
1144
- });
1145
- NexusFrameworkBase.prototype.defaultRequestPage = function (path, post) {
1146
- if (post)
1147
- throw new Error("Posting is not supported without an initialized page system, yet");
1148
- else
1149
- location.href = this.resolveUrl(path);
1150
- };
1151
- NexusFrameworkBase.prototype.on = function (event, cb) {
1152
- var listeners = this._listeners[event];
1153
- if (listeners)
1154
- listeners.push(cb);
1155
- else
1156
- this._listeners[event] = listeners = [cb];
1157
- };
1158
- NexusFrameworkBase.prototype.off = function (event, cb) {
1159
- var index;
1160
- var listeners = this._listeners[event];
1161
- if (listeners && (index = listeners.indexOf(cb)) > -1)
1162
- listeners.splice(index, 1);
1163
- };
1164
- NexusFrameworkBase.prototype.emit = function (event) {
1165
- var args = [];
1166
- for (var _i = 1; _i < arguments.length; _i++) {
1167
- args[_i - 1] = arguments[_i];
1168
- }
1169
- var self = this;
1170
- var listeners = this._listeners[event];
1171
- if (listeners)
1172
- listeners.forEach(function (cb) {
1173
- cb.apply(self, args);
1174
- });
1175
- };
1176
- return NexusFrameworkBase;
1177
- }());
1178
- var impl;
1179
- if (window.io)
1180
- impl = /** @class */ (function (_super) {
1181
- __extends(NexusFrameworkWithIO, _super);
1182
- function NexusFrameworkWithIO(url) {
1183
- var _this = _super.call(this, url, window.io({
1184
- path: "/:io"
1185
- })) || this;
1186
- var io = _this.io;
1187
- io.on("connect", function () {
1188
- io.emit("init", loader.requestedResources());
1189
- });
1190
- return _this;
1191
- }
1192
- return NexusFrameworkWithIO;
1193
- }(NexusFrameworkBase));
1194
- else
1195
- impl = /** @class */ (function (_super) {
1196
- __extends(NexusFrameworkNoIO, _super);
1197
- function NexusFrameworkNoIO(url) {
1198
- return _super.call(this, url) || this;
1199
- }
1200
- return NexusFrameworkNoIO;
1201
- }(NexusFrameworkBase));
1202
- Object.defineProperty(window, "NexusFrameworkImpl", {
1203
- value: impl
1204
- });
1205
- return impl;
1206
- }
1207
- },
1208
- NexusFrameworkClient: {
1209
- configurable: true,
1210
- set: function (instance) {
1211
- Object.defineProperty(window, "NexusFrameworkClient", {
1212
- value: instance
1213
- });
1214
- },
1215
- get: function () {
1216
- var instance = new window.NexusFrameworkImpl();
1217
- Object.defineProperty(window, "NexusFrameworkClient", {
1218
- value: instance
1219
- });
1220
- return instance;
1221
- }
1222
- }
1223
- });
1224
- })(window);
1
+ var __extends = (this && this.__extends) || (function () {
2
+ var extendStatics = Object.setPrototypeOf ||
3
+ ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
4
+ function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
5
+ return function (d, b) {
6
+ extendStatics(d, b);
7
+ function __() { this.constructor = d; }
8
+ d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
9
+ };
10
+ })();
11
+ /// <reference path="../index.d.ts" />
12
+ (function (window) {
13
+ var BSON = new window['bson'];
14
+ Object.defineProperties(window, {
15
+ NexusFrameworkTransport: {
16
+ configurable: true,
17
+ set: function (instance) {
18
+ Object.defineProperty(window, "NexusFrameworkTransport", {
19
+ value: instance
20
+ });
21
+ },
22
+ get: function () {
23
+ var impl;
24
+ if ("XMLHttpRequest" in window) {
25
+ var NexusFrameworkXMLHttpRequestResponse_1 = /** @class */ (function () {
26
+ function NexusFrameworkXMLHttpRequestResponse(request, url, hadData, abResponse) {
27
+ this._url = url;
28
+ this.request = request;
29
+ this.hadData = hadData;
30
+ this.abResponse = abResponse;
31
+ }
32
+ Object.defineProperty(NexusFrameworkXMLHttpRequestResponse.prototype, "url", {
33
+ get: function () {
34
+ return this.request.responseURL || this._url;
35
+ },
36
+ enumerable: true,
37
+ configurable: true
38
+ });
39
+ Object.defineProperty(NexusFrameworkXMLHttpRequestResponse.prototype, "code", {
40
+ get: function () {
41
+ return this.request.status;
42
+ },
43
+ enumerable: true,
44
+ configurable: true
45
+ });
46
+ Object.defineProperty(NexusFrameworkXMLHttpRequestResponse.prototype, "contentLength", {
47
+ get: function () {
48
+ return parseInt(this.request.getResponseHeader("content-length")) || this.request.responseText.length;
49
+ },
50
+ enumerable: true,
51
+ configurable: true
52
+ });
53
+ Object.defineProperty(NexusFrameworkXMLHttpRequestResponse.prototype, "contentFromJSON", {
54
+ get: function () {
55
+ if (!this.parsedJson)
56
+ this.parsedJson = JSON.parse(this.request.responseText);
57
+ return this.parsedJson;
58
+ },
59
+ enumerable: true,
60
+ configurable: true
61
+ });
62
+ Object.defineProperty(NexusFrameworkXMLHttpRequestResponse.prototype, "contentFromBSON", {
63
+ get: function () {
64
+ if (!this.parsedBson) {
65
+ var response = this.request.response;
66
+ if (typeof response === "string")
67
+ response = Buffer.from(response, "utf8");
68
+ else
69
+ response = new Buffer(response);
70
+ this.parsedBson = BSON.deserialize(response, { promoteValues: true });
71
+ }
72
+ return this.parsedBson;
73
+ },
74
+ enumerable: true,
75
+ configurable: true
76
+ });
77
+ Object.defineProperty(NexusFrameworkXMLHttpRequestResponse.prototype, "contentAsString", {
78
+ get: function () {
79
+ return this.request.responseText;
80
+ },
81
+ enumerable: true,
82
+ configurable: true
83
+ });
84
+ Object.defineProperty(NexusFrameworkXMLHttpRequestResponse.prototype, "contentAsArrayBuffer", {
85
+ get: function () {
86
+ return this.request.response;
87
+ },
88
+ enumerable: true,
89
+ configurable: true
90
+ });
91
+ Object.defineProperty(NexusFrameworkXMLHttpRequestResponse.prototype, "headers", {
92
+ get: function () {
93
+ if (!this.processedHeaders) {
94
+ var headers_1 = this.processedHeaders = {};
95
+ this.request.getAllResponseHeaders().split(/\r?\n/g).forEach(function (header) {
96
+ var index = header.indexOf(":");
97
+ var key, val;
98
+ if (index > 0) {
99
+ key = header.substring(0, index).trim().toLowerCase();
100
+ val = header.substring(index + 1).trim();
101
+ }
102
+ else
103
+ key = header.trim().toLowerCase();
104
+ var list = headers_1[key];
105
+ if (!list)
106
+ list = headers_1[key] = [];
107
+ if (val)
108
+ list.push(val);
109
+ });
110
+ }
111
+ return this.processedHeaders;
112
+ },
113
+ enumerable: true,
114
+ configurable: true
115
+ });
116
+ return NexusFrameworkXMLHttpRequestResponse;
117
+ }());
118
+ var execute_1 = function (method, url, data, cb, extraHeaders, progcb, wantsBinary) {
119
+ var request = new XMLHttpRequest();
120
+ var responseIsArrayBuffer;
121
+ try {
122
+ if (!wantsBinary)
123
+ throw false;
124
+ request.responseType = "arraybuffer";
125
+ if (request.responseType !== "arraybuffer") {
126
+ console.error("Could not request arraybuffer");
127
+ throw false;
128
+ }
129
+ responseIsArrayBuffer = true;
130
+ }
131
+ catch (e) {
132
+ responseIsArrayBuffer = false;
133
+ }
134
+ request.open(method, url, true);
135
+ if (extraHeaders)
136
+ Object.keys(extraHeaders).forEach(function (key) {
137
+ request.setRequestHeader(key, extraHeaders[key]);
138
+ });
139
+ if (progcb)
140
+ request.onprogress = function (ev) {
141
+ if (ev.lengthComputable && ev.total)
142
+ progcb(ev.loaded, ev.total);
143
+ };
144
+ request.onreadystatechange = function (e) {
145
+ if (request.readyState === XMLHttpRequest.DONE)
146
+ cb(new NexusFrameworkXMLHttpRequestResponse_1(request, url, !!data, responseIsArrayBuffer));
147
+ };
148
+ var type = data && data.type;
149
+ if (type)
150
+ switch (type) {
151
+ case "":
152
+ case "text/urlencoded":
153
+ var dat = "";
154
+ for (var _i = 0, _a = data.data; _i < _a.length; _i++) {
155
+ var entry = _a[_i];
156
+ if (dat)
157
+ dat += "&";
158
+ dat += encodeURIComponent(entry[0]);
159
+ dat += "=";
160
+ dat += encodeURIComponent(entry[1]);
161
+ }
162
+ request.setRequestHeader("Content-Type", "text/urlencoded");
163
+ request.send(dat);
164
+ break;
165
+ case "text/json":
166
+ request.setRequestHeader("Content-Type", "text/json");
167
+ request.send(JSON.stringify(data.data));
168
+ break;
169
+ case "application/bson":
170
+ request.setRequestHeader("Content-Type", "application/bson");
171
+ request.send(BSON.serialize(data.data));
172
+ break;
173
+ case "multipart/form-data":
174
+ request.send(data.data);
175
+ break;
176
+ default:
177
+ request.send(data);
178
+ }
179
+ else
180
+ request.send(data);
181
+ };
182
+ impl = {
183
+ get: function (url, cb, extraHeaders, progcb, wantsBinary) {
184
+ execute_1("GET", url, undefined, cb, extraHeaders, progcb, wantsBinary);
185
+ },
186
+ head: function (url, cb, extraHeaders, progcb, wantsBinary) {
187
+ execute_1("HEAD", url, undefined, cb, extraHeaders, progcb, wantsBinary);
188
+ },
189
+ post: function (url, data, cb, extraHeaders, progcb, wantsBinary) {
190
+ execute_1("POST", url, data, cb, extraHeaders, progcb, wantsBinary);
191
+ },
192
+ put: function (url, data, cb, extraHeaders, progcb, wantsBinary) {
193
+ execute_1("PUT", url, data, cb, extraHeaders, progcb, wantsBinary);
194
+ },
195
+ execute: execute_1,
196
+ del: function (url, cb, extraHeaders, progcb, wantsBinary) {
197
+ execute_1("DELETE", url, undefined, cb, extraHeaders, progcb, wantsBinary);
198
+ }
199
+ };
200
+ }
201
+ else
202
+ throw new Error("No transport implementations");
203
+ Object.defineProperty(window, "NexusFrameworkTransport", {
204
+ value: impl
205
+ });
206
+ return impl;
207
+ }
208
+ },
209
+ NexusFrameworkImpl: {
210
+ configurable: true,
211
+ set: function (instance) {
212
+ Object.defineProperty(window, "NexusFrameworkImpl", {
213
+ value: instance
214
+ });
215
+ },
216
+ get: function () {
217
+ var loader = window.NexusFrameworkLoader;
218
+ var showError = loader.showError;
219
+ var debug = /*function(...args: any[]){};*/ console.log.bind(console);
220
+ var GA_ANALYTICS = {
221
+ reportError: function (err, fatal) {
222
+ if (window.ga)
223
+ try {
224
+ window.ga('send', 'exception', {
225
+ 'exDescription': (err.stack || "" + err).replace(/\n/g, "\n\t"),
226
+ 'exFatal': fatal
227
+ });
228
+ }
229
+ catch (e) {
230
+ console.warn(e);
231
+ }
232
+ },
233
+ reportEvent: function (category, action, label, value) {
234
+ if (window.ga)
235
+ try {
236
+ window.ga('send', 'event', category, action, label, value);
237
+ }
238
+ catch (e) {
239
+ console.warn(e);
240
+ }
241
+ },
242
+ reportPage: function (path) {
243
+ if (window.ga)
244
+ try {
245
+ if (!path)
246
+ path = location.pathname;
247
+ window.ga('set', 'page', path);
248
+ window.ga('send', 'pageview');
249
+ }
250
+ catch (e) {
251
+ console.warn(e);
252
+ }
253
+ }
254
+ };
255
+ var r = document.createElement("a");
256
+ var protocol = location.href.match(/^\w+:/)[0];
257
+ var resolveUrl = function (url) {
258
+ r.setAttribute("href", url);
259
+ var href = r.href;
260
+ if (/^\/\//.test(href))
261
+ href = protocol + href;
262
+ return href;
263
+ };
264
+ var convertResponse = function (res, url) {
265
+ if (url === void 0) { url = location.href; }
266
+ var storage;
267
+ return (typeof res.data === "string") ? {
268
+ url: url,
269
+ code: res.code,
270
+ contentAsString: res.data,
271
+ get contentLength() {
272
+ return parseInt(res.headers['content-length'] && res.headers['content-length'][0]) || res.data.length;
273
+ },
274
+ get contentAsArrayBuffer() {
275
+ throw new Error("Not implemented");
276
+ },
277
+ get contentFromBSON() {
278
+ if (!storage)
279
+ storage = JSON.parse(res.data);
280
+ return storage;
281
+ },
282
+ get contentFromJSON() {
283
+ if (!storage)
284
+ storage = JSON.parse(res.data);
285
+ return storage;
286
+ },
287
+ headers: res.headers
288
+ } : {
289
+ url: url,
290
+ code: res.code,
291
+ get contentAsString() {
292
+ if (!storage)
293
+ storage = JSON.stringify(res.data);
294
+ return storage;
295
+ },
296
+ get contentAsArrayBuffer() {
297
+ throw new Error("Not implemented");
298
+ },
299
+ contentFromJSON: res.data,
300
+ contentFromBSON: res.data,
301
+ headers: res.headers,
302
+ get contentLength() {
303
+ var length = parseInt(res.headers['content-length'] && res.headers['content-length'][0]);
304
+ if (length)
305
+ return length;
306
+ if (!storage)
307
+ storage = JSON.stringify(res.data);
308
+ return storage.length;
309
+ }
310
+ };
311
+ };
312
+ var loaderContainerRegex = /(^|\s)loader\-(progress|error)\-container(\s|$)/;
313
+ var NexusFrameworkBase = /** @class */ (function () {
314
+ function NexusFrameworkBase(url, io) {
315
+ if (url === void 0) { url = "/"; }
316
+ this.activerid = 0;
317
+ this.components = {};
318
+ this.currentUserID = undefined;
319
+ this.progressVisible = false;
320
+ this.animationTiming = 500;
321
+ this.requestPage = this.defaultRequestPage;
322
+ this._listeners = {};
323
+ url = resolveUrl(url);
324
+ if (!/\/$/.test(url))
325
+ url += "/";
326
+ Object.defineProperties(this, {
327
+ io: {
328
+ value: io
329
+ },
330
+ url: {
331
+ value: url
332
+ }
333
+ });
334
+ this._analytics = GA_ANALYTICS;
335
+ }
336
+ NexusFrameworkBase.prototype.resolveUrl = function (url) {
337
+ if (/^\w+\:/.test(url))
338
+ return url;
339
+ return resolveUrl(this.url + url);
340
+ };
341
+ NexusFrameworkBase.prototype.disableAll = function (root) {
342
+ if (root === void 0) { root = document.body; }
343
+ var focusable = root.querySelectorAll("a, input, select, textarea, iframe, button, *[focusable], *[tabindex]");
344
+ for (var i = 0; i < focusable.length; i++) {
345
+ var child = focusable[i];
346
+ var tabindex = child.getAttribute("tabindex");
347
+ if (tabindex == "-1")
348
+ return;
349
+ child.setAttribute("data-tabindex", tabindex ? tabindex : "");
350
+ child.setAttribute("data-tabindex", "-1");
351
+ }
352
+ };
353
+ NexusFrameworkBase.prototype.enableAll = function (root) {
354
+ if (root === void 0) { root = document.body; }
355
+ var focusable = root.querySelectorAll("*[data-tabindex]");
356
+ for (var i = 0; i < focusable.length; i++) {
357
+ var child = focusable[i];
358
+ child.setAttribute("tabindex", child.getAttribute("data-tabindex"));
359
+ child.removeAttribute("data-tabindex");
360
+ }
361
+ };
362
+ Object.defineProperty(NexusFrameworkBase.prototype, "analytics", {
363
+ get: function () {
364
+ return this._analytics;
365
+ },
366
+ set: function (value) {
367
+ this._analytics = value ? value : GA_ANALYTICS;
368
+ },
369
+ enumerable: true,
370
+ configurable: true
371
+ });
372
+ NexusFrameworkBase.prototype.reportError = function (err, fatal) {
373
+ console[fatal ? "error" : "warn"](err.stack);
374
+ if (this.errorreporter)
375
+ this.errorreporter(err, fatal);
376
+ };
377
+ NexusFrameworkBase.prototype.installErrorReporter = function (errorreporter) {
378
+ this.errorreporter = errorreporter;
379
+ };
380
+ NexusFrameworkBase.prototype.registerComponent = function (selector, impl) {
381
+ var _this = this;
382
+ if (impl) {
383
+ this.components[selector] = impl;
384
+ this.createComponents(document.head);
385
+ this.createComponents(document.body);
386
+ }
387
+ else {
388
+ delete this.components[selector];
389
+ var destroy = function (root) {
390
+ var elements = root.querySelectorAll(selector);
391
+ if (!elements.length)
392
+ return;
393
+ for (var i = 0; i < elements.length; i++) {
394
+ var element = elements[i];
395
+ var components = element['__nf_cmapping__'];
396
+ if (!components)
397
+ components = element['__nf_cmapping__'] = {};
398
+ var component = components[selector];
399
+ if (component) {
400
+ try {
401
+ component.destroy();
402
+ }
403
+ catch (e) {
404
+ _this.reportError(e);
405
+ }
406
+ }
407
+ }
408
+ };
409
+ destroy(document.head);
410
+ destroy(document.body);
411
+ }
412
+ };
413
+ NexusFrameworkBase.prototype.unregisterComponent = function (selector, impl) {
414
+ };
415
+ NexusFrameworkBase.prototype.createComponents = function (root) {
416
+ var _this = this;
417
+ Object.keys(this.components).forEach(function (selector) {
418
+ var elements = root.querySelectorAll(selector);
419
+ if (!elements.length)
420
+ return;
421
+ for (var i = 0; i < elements.length; i++) {
422
+ var element = elements[i];
423
+ var components = element['__nf_cmapping__'];
424
+ if (!components)
425
+ components = element['__nf_cmapping__'] = {};
426
+ if (components[selector])
427
+ continue;
428
+ var componentFactory = _this.components[selector];
429
+ try {
430
+ (components[selector] = new componentFactory()).create(element);
431
+ }
432
+ catch (e) {
433
+ _this.reportError(e);
434
+ }
435
+ }
436
+ });
437
+ };
438
+ NexusFrameworkBase.prototype.destroyComponents = function (root) {
439
+ var _this = this;
440
+ Object.keys(this.components).forEach(function (selector) {
441
+ var elements = root.querySelectorAll(selector);
442
+ if (!elements.length)
443
+ return;
444
+ for (var i = 0; i < elements.length; i++) {
445
+ var element = elements[i];
446
+ var components = element['__nf_cmapping__'];
447
+ if (!components)
448
+ components = element['__nf_cmapping__'] = {};
449
+ var component = components[selector];
450
+ if (component) {
451
+ try {
452
+ component.destroy();
453
+ }
454
+ catch (e) {
455
+ _this.reportError(e);
456
+ }
457
+ }
458
+ }
459
+ });
460
+ };
461
+ NexusFrameworkBase.prototype.restoreComponents = function (root, state) {
462
+ var _this = this;
463
+ Object.keys(this.components).forEach(function (selector) {
464
+ var states = state[selector];
465
+ if (!states)
466
+ return;
467
+ var elements = root.querySelectorAll(selector);
468
+ if (!elements.length)
469
+ return;
470
+ for (var i = 0; i < elements.length; i++) {
471
+ var element = elements[i];
472
+ if (states.length) {
473
+ var _state = states.shift();
474
+ if (_state) {
475
+ var components = element['__nf_cmapping__'];
476
+ if (!components)
477
+ components = element['__nf_cmapping__'] = {};
478
+ var component = components[selector];
479
+ if (!component) {
480
+ var componentFactory = _this.components[selector];
481
+ try {
482
+ (component = components[selector] = new componentFactory()).create(element);
483
+ }
484
+ catch (e) {
485
+ _this.reportError(e);
486
+ console.warn(e);
487
+ continue;
488
+ }
489
+ }
490
+ try {
491
+ component.restore(_state);
492
+ }
493
+ catch (e) {
494
+ _this.reportError(e);
495
+ console.warn(e);
496
+ }
497
+ }
498
+ }
499
+ else
500
+ break;
501
+ }
502
+ });
503
+ };
504
+ NexusFrameworkBase.prototype.saveComponents = function (root) {
505
+ var _this = this;
506
+ var state = {};
507
+ Object.keys(this.components).forEach(function (selector) {
508
+ var states = state[selector] = [];
509
+ var elements = root.querySelectorAll(selector);
510
+ if (!elements.length)
511
+ return;
512
+ for (var i = 0; i < elements.length; i++) {
513
+ var element = elements[i];
514
+ var components = element['__nf_cmapping__'];
515
+ if (!components)
516
+ components = element['__nf_cmapping__'] = {};
517
+ var component = components[selector];
518
+ if (!component) {
519
+ var componentFactory = _this.components[selector];
520
+ try {
521
+ (component = components[selector] = new componentFactory).create(element);
522
+ }
523
+ catch (e) {
524
+ states.push(undefined);
525
+ _this.reportError(e);
526
+ console.warn(e);
527
+ continue;
528
+ }
529
+ }
530
+ try {
531
+ states.push(component.save());
532
+ }
533
+ catch (e) {
534
+ states.push(undefined);
535
+ _this.reportError(e);
536
+ console.warn(e);
537
+ }
538
+ }
539
+ });
540
+ return state;
541
+ };
542
+ NexusFrameworkBase.prototype.initPageSystem = function (opts) {
543
+ var _this = this;
544
+ if (!loader)
545
+ return console.error("The NexusFramework Loader is required for the dynamic Page System to work correctly.");
546
+ if (!history.pushState || !history.replaceState)
547
+ return console.warn("This browser is missing an essential feature required for the dynamic Page System.");
548
+ if (this.pagesyshandler)
549
+ return console.warn("Page System already initialized, ignoring additional requests to initialize.");
550
+ opts = opts || {};
551
+ var self = this;
552
+ var pageStatesSize = 0;
553
+ var pageStates = [];
554
+ this.animationTiming = opts.animationTiming || 500;
555
+ var pageStateCacheSize = opts.pageHistoryCacheSize || 25000000;
556
+ var wrapCB = function (cb) {
557
+ return function (res) {
558
+ var user = res.headers['x-logged-user'];
559
+ if (user)
560
+ self.currentUserID = user[0];
561
+ else
562
+ self.currentUserID = undefined;
563
+ debug("Current UID", self.currentUserID);
564
+ cb(res);
565
+ if (res.code === 200)
566
+ setTimeout(function () {
567
+ self._analytics.reportPage();
568
+ });
569
+ };
570
+ };
571
+ loader.showError = function (error) {
572
+ var match = location.href.match(beforeHash);
573
+ var baseurl = match && match[1] || location.href;
574
+ var length = error.stack.length;
575
+ var tooBig = pageStateCacheSize <= length;
576
+ try {
577
+ var cPageStates = pageStates.length;
578
+ for (var i = 0; i < cPageStates; i++) {
579
+ var state = pageStates[i];
580
+ if (state.url === baseurl) {
581
+ if (tooBig) {
582
+ pageStates.splice(i, 1);
583
+ pageStatesSize -= state.size;
584
+ }
585
+ else {
586
+ state.data = { error: error };
587
+ state.updated = +new Date;
588
+ pageStatesSize += length - state.size;
589
+ }
590
+ throw true;
591
+ }
592
+ }
593
+ if (tooBig)
594
+ throw true;
595
+ pageStates.push({
596
+ url: baseurl,
597
+ data: { error: error },
598
+ updated: +new Date,
599
+ size: length
600
+ });
601
+ pageStatesSize += length;
602
+ }
603
+ catch (e) {
604
+ if (e !== true)
605
+ throw e;
606
+ }
607
+ return showError(error);
608
+ };
609
+ var transportPageSystem = {
610
+ requestPage: function (path, cb, post, rid) {
611
+ if (self.pagesysprerequest && !self.pagesysprerequest(path)) {
612
+ self.defaultRequestPage(path, post);
613
+ return;
614
+ }
615
+ if (!opts.noprogress) {
616
+ self.disableAll();
617
+ loader.showProgress();
618
+ }
619
+ var url = self.resolveUrl(":pagesys/" + path);
620
+ var _cb = opts.noprogress ? cb : function (res) {
621
+ if (opts.noprogress)
622
+ cb(res);
623
+ else
624
+ loader.showProgress(function () {
625
+ cb(res);
626
+ self.enableAll();
627
+ });
628
+ };
629
+ if (post)
630
+ window.NexusFrameworkTransport.post(url, post, wrapCB(_cb), undefined, undefined, true);
631
+ else
632
+ window.NexusFrameworkTransport.get(url, wrapCB(_cb), undefined, undefined, true);
633
+ }
634
+ };
635
+ if (!opts.noio && this.io) {
636
+ var io_1 = this.io;
637
+ this.pagesysimpl = {
638
+ requestPage: function (path, cb, post, rid) {
639
+ if (io_1.connected) {
640
+ if (self.pagesysprerequest && !self.pagesysprerequest(path)) {
641
+ self.defaultRequestPage(path, post);
642
+ return;
643
+ }
644
+ if (!opts.noprogress) {
645
+ self.disableAll();
646
+ loader.showProgress();
647
+ }
648
+ var headers = {
649
+ "referrer": location.href
650
+ };
651
+ var val = document.cookie;
652
+ if (val)
653
+ headers['cookie'] = val;
654
+ io_1.emit("page", post ? "POST" : "GET", path, post, headers, function (res) {
655
+ if (rid != self.activerid)
656
+ return;
657
+ var cookies = res.headers['set-cookie'];
658
+ if (cookies) {
659
+ cookies.forEach(function (cookie) {
660
+ document.cookie = cookie;
661
+ });
662
+ }
663
+ var _cb = opts.noprogress ? cb : function (res) {
664
+ if (!opts.noprogress)
665
+ cb(res);
666
+ else
667
+ loader.showProgress(function () {
668
+ cb(res);
669
+ self.enableAll();
670
+ });
671
+ };
672
+ wrapCB(_cb)(convertResponse(res, self.resolveUrl(path)));
673
+ });
674
+ }
675
+ else
676
+ transportPageSystem.requestPage(path, cb, post, rid);
677
+ }
678
+ };
679
+ }
680
+ else
681
+ this.pagesysimpl = transportPageSystem;
682
+ var base = document.getElementsByTagName("base");
683
+ base = base && base[0];
684
+ this.pagesysprerequest = opts.prerequest;
685
+ this.pagesyshandler = opts.handler || (function (res, pageReady) {
686
+ try {
687
+ var contentType = res.headers['content-type'][0];
688
+ if (!/\/html(;.+)?$/.test(contentType.toLowerCase())) {
689
+ throw new Error("Content type is not html");
690
+ }
691
+ }
692
+ catch (e) { }
693
+ var content = res.contentAsString;
694
+ var bodyindex = content.indexOf("<body");
695
+ if (bodyindex == -1)
696
+ throw new Error("Could not find start of body tag");
697
+ var endbodyindex = content.indexOf("</body>") || content.indexOf("</ body>");
698
+ if (endbodyindex == -1)
699
+ throw new Error("Could not find end of body tag");
700
+ var title = content.match(/<title>([^<]+)<\/\s*title>/);
701
+ document.title = title && title[1] || "Title Not Set";
702
+ var mockhtml = document.createElement("html");
703
+ mockhtml.innerHTML = content.substring(bodyindex, endbodyindex) + "</body>";
704
+ var loaderScript;
705
+ var childs = mockhtml.children;
706
+ var toAdd = [];
707
+ for (var i = 0; i < childs.length; i++) {
708
+ var child = childs[i];
709
+ switch (child.nodeName.toUpperCase()) {
710
+ case "HEAD":
711
+ break;
712
+ case "BODY":
713
+ i = -1;
714
+ childs = child.children;
715
+ break;
716
+ case "SCRIPT":
717
+ var match = child.innerHTML.match(/^NexusFrameworkLoader\.load\((.+)\);?$/);
718
+ if (match)
719
+ loaderScript = JSON.parse(match[1]);
720
+ break;
721
+ default:
722
+ if (loaderContainerRegex.test(child.className))
723
+ break;
724
+ toAdd.push(child);
725
+ }
726
+ }
727
+ if (!toAdd.length)
728
+ throw new Error("Nothing found in response to add to page");
729
+ if (!loaderScript)
730
+ throw new Error("NexusFrameworkLoader script not found...");
731
+ childs = document.body.children;
732
+ for (var i = childs.length - 1; i >= 0; i--) {
733
+ var child = childs[i];
734
+ switch (child.nodeName.toUpperCase()) {
735
+ case "LINK":
736
+ case "SCRIPT":
737
+ break;
738
+ default:
739
+ if (loaderContainerRegex.test(child.className))
740
+ break;
741
+ document.body.removeChild(child);
742
+ }
743
+ }
744
+ var fragment = document.createDocumentFragment();
745
+ toAdd.forEach(function (el) {
746
+ fragment.appendChild(el);
747
+ _this.createComponents(el);
748
+ });
749
+ document.body.appendChild(fragment);
750
+ loader.load(loaderScript, function () {
751
+ self.progressVisible = true;
752
+ loader.resetProgress();
753
+ pageReady();
754
+ });
755
+ return true;
756
+ });
757
+ var forwardPopState;
758
+ var beforeHash = /^([^#]+)(#.*)?$/;
759
+ var chash = location.href.match(beforeHash)[1];
760
+ var startsWith = new RegExp("^" + this.url.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&") + "(.*)$", "i");
761
+ var AnchorElementComponent = /** @class */ (function () {
762
+ function AnchorElementComponent() {
763
+ var _this = this;
764
+ this.handler = function (e) {
765
+ if (_this.element.hasAttribute("data-nopagesys") || _this.element.hasAttribute("data-nodynamic"))
766
+ return;
767
+ var href = _this.element.getAttribute("href");
768
+ if (!href || !href.length)
769
+ return;
770
+ var url = _this.element.href;
771
+ var bhash = url.match(beforeHash);
772
+ if (bhash[2] && chash === bhash[1])
773
+ return;
774
+ if (startsWith.test(url))
775
+ try {
776
+ var match = url.match(/^(.+)#.*$/);
777
+ if (match)
778
+ url = match[1];
779
+ self.requestPage(url.substring(self.url.length));
780
+ try {
781
+ e.stopPropagation();
782
+ }
783
+ catch (e) { }
784
+ try {
785
+ e.preventDefault();
786
+ }
787
+ catch (e) { }
788
+ }
789
+ catch (e) {
790
+ console.warn(e);
791
+ }
792
+ else
793
+ debug("Navigating", url);
794
+ };
795
+ }
796
+ AnchorElementComponent.prototype.create = function (element) {
797
+ element.addEventListener("click", this.handler);
798
+ this.element = element;
799
+ };
800
+ AnchorElementComponent.prototype.destroy = function () {
801
+ this.element.removeEventListener("click", this.handler);
802
+ };
803
+ AnchorElementComponent.prototype.restore = function (data) { };
804
+ AnchorElementComponent.prototype.save = function () { };
805
+ return AnchorElementComponent;
806
+ }());
807
+ var FormElementComponent = /** @class */ (function () {
808
+ function FormElementComponent() {
809
+ var _this = this;
810
+ this.handler = function (e) {
811
+ if (_this.element.hasAttribute("data-nopagesys") || _this.element.hasAttribute("data-nodynamic"))
812
+ return;
813
+ var method = _this.element.getAttribute("method") || "get";
814
+ var enctype = _this.element.getAttribute("enctype") || "text/urlencoded";
815
+ var action = _this.element.getAttribute("action") || "";
816
+ var url = resolveUrl(action);
817
+ if (startsWith.test(url)) {
818
+ try {
819
+ var match = url.match(/^(.+)#.*$/);
820
+ if (match)
821
+ url = match[1];
822
+ var formData = new FormData(_this.element);
823
+ loader.showProgress(undefined, "Submitting Form", "Your form data is being processed");
824
+ if (method.trim().toLowerCase() === "post")
825
+ self.requestPage(url.substring(self.url.length), { type: enctype, data: formData });
826
+ else {
827
+ var first = true;
828
+ var reqUrl = url.substring(self.url.length) + "?";
829
+ for (var _i = 0, _a = formData; _i < _a.length; _i++) {
830
+ var entry = _a[_i];
831
+ if (first)
832
+ first = false;
833
+ else
834
+ reqUrl += "&";
835
+ reqUrl += encodeURIComponent(entry[0]);
836
+ reqUrl += "=";
837
+ reqUrl += encodeURIComponent(entry[1]);
838
+ }
839
+ self.requestPage(reqUrl);
840
+ }
841
+ try {
842
+ e.stopPropagation();
843
+ }
844
+ catch (e) { }
845
+ try {
846
+ e.preventDefault();
847
+ }
848
+ catch (e) { }
849
+ Array.prototype.forEach.call(_this.element.querySelectorAll("a, input, select, textarea, iframe, button, *[name]"), function (el) {
850
+ el.setAttribute("disabled", "disabled");
851
+ el.className += " disabled";
852
+ });
853
+ Array.prototype.forEach.call(_this.element.querySelectorAll("button:not([type]), button[type=submit]"), function (button) {
854
+ button.innerHTML = "Processing Request";
855
+ });
856
+ Array.prototype.forEach.call(_this.element.querySelectorAll("input[type=submit]"), function (input) {
857
+ input.value = "Processing Request";
858
+ });
859
+ return;
860
+ }
861
+ catch (e) {
862
+ debug(e);
863
+ }
864
+ }
865
+ debug("Submitting", url);
866
+ };
867
+ }
868
+ FormElementComponent.prototype.create = function (element) {
869
+ element.addEventListener("submit", this.handler);
870
+ this.element = element;
871
+ };
872
+ FormElementComponent.prototype.destroy = function () {
873
+ this.element.removeEventListener("submit", this.handler);
874
+ };
875
+ FormElementComponent.prototype.restore = function (data) { };
876
+ FormElementComponent.prototype.save = function () { };
877
+ return FormElementComponent;
878
+ }());
879
+ var requestPage = this.requestPage = function (path, post, replace) {
880
+ if (replace === void 0) { replace = false; }
881
+ var match = path.match(/\.([^\/]+)([\?#].*)?$/);
882
+ if (match && match[0] !== "htm" && match[0] !== "html") {
883
+ _this.defaultRequestPage(path, post);
884
+ debug("Ignoring navigation", path, match);
885
+ }
886
+ else {
887
+ debug("requestPage", path);
888
+ var rid_1 = ++self.activerid;
889
+ var baseurl_1 = _this.resolveUrl(path);
890
+ var fullurl_1 = baseurl_1 + (match && match[2] || "");
891
+ if (replace)
892
+ history.replaceState(!!post, "Loading...", fullurl_1);
893
+ else {
894
+ history.pushState(!!post, "Loading...", fullurl_1);
895
+ chash = fullurl_1.match(beforeHash)[1];
896
+ window.scrollTo(0, 0);
897
+ }
898
+ loader.resetError();
899
+ loader.resetProgress();
900
+ _this.pagesysimpl.requestPage(path, function (res) {
901
+ try {
902
+ if (rid_1 !== self.activerid)
903
+ return;
904
+ if (!res.code) {
905
+ loader.showProgress(undefined, "Connection Issue", "Check your network and try again");
906
+ document.title = "Connection Issue";
907
+ history.replaceState(res.hadData, "Connection Issue", fullurl_1);
908
+ setTimeout(function () {
909
+ if (rid_1 !== self.activerid)
910
+ return;
911
+ requestPage(path, post, true);
912
+ }, 900000);
913
+ return;
914
+ }
915
+ else if (res.code === 502 || res.code === 503) {
916
+ loader.showProgress(undefined, "Scheduled Maintenance", "Sorry but this website is undergoing scheduled maintenance");
917
+ document.title = "Scheduled Maintenance";
918
+ history.replaceState(res.hadData, "Scheduled Maintenance", fullurl_1);
919
+ setTimeout(function () {
920
+ if (rid_1 !== self.activerid)
921
+ return;
922
+ requestPage(path, post, true);
923
+ }, 900000);
924
+ return;
925
+ }
926
+ var location_1 = res.headers['x-location'] || res.headers['location'];
927
+ if (location_1) {
928
+ var url = resolveUrl(location_1[0]);
929
+ if (startsWith.test(url)) {
930
+ _this.requestPage(url.substring(_this.url.length), undefined, true);
931
+ return;
932
+ }
933
+ console.warn("Requested redirect to external url:", url);
934
+ window.location.href = url;
935
+ return;
936
+ }
937
+ var contentDisposition = res.headers['content-disposition'];
938
+ if (contentDisposition && contentDisposition[0].toLowerCase() == "attachment")
939
+ throw new Error("Attachment disposition");
940
+ _this.pagesyshandler(res, function () {
941
+ if (rid_1 !== self.activerid)
942
+ return;
943
+ try {
944
+ var title = document.title;
945
+ var length_1 = res.contentLength;
946
+ var tooBig = pageStateCacheSize <= length_1;
947
+ var stateData = tooBig ? undefined : {
948
+ title: title,
949
+ user: self.currentUserID,
950
+ scroll: [window.scrollX, window.scrollY],
951
+ body: self.saveComponents(document.body),
952
+ basehref: base ? base['href'] : undefined,
953
+ response: res
954
+ };
955
+ var i;
956
+ var cPageStates = pageStates.length;
957
+ try {
958
+ for (i = 0; i < cPageStates; i++) {
959
+ var state = pageStates[i];
960
+ if (state.url === baseurl_1) {
961
+ if (tooBig) {
962
+ pageStates.splice(i, 1);
963
+ pageStatesSize -= state.size;
964
+ }
965
+ else {
966
+ state.data = stateData;
967
+ state.updated = +new Date;
968
+ pageStatesSize += length_1 - state.size;
969
+ }
970
+ throw true;
971
+ }
972
+ }
973
+ if (!tooBig) {
974
+ pageStates.push({
975
+ url: baseurl_1,
976
+ data: stateData,
977
+ updated: +new Date,
978
+ size: length_1
979
+ });
980
+ pageStatesSize += length_1;
981
+ throw true;
982
+ }
983
+ else
984
+ debug("Response too big to store", baseurl_1, length_1);
985
+ }
986
+ catch (e) {
987
+ if (e === true) {
988
+ var over = pageStatesSize - pageStateCacheSize;
989
+ if (over > 0) {
990
+ pageStates.sort(function (a, b) {
991
+ return a.updated - b.updated;
992
+ });
993
+ var found = 0;
994
+ for (i = 0; i < cPageStates; i++) {
995
+ found += pageStates[i].size;
996
+ if (found >= over)
997
+ break;
998
+ }
999
+ i++;
1000
+ pageStates.splice(0, i);
1001
+ debug("Trimmed", i, "items...");
1002
+ }
1003
+ }
1004
+ else
1005
+ throw e;
1006
+ }
1007
+ history.replaceState(res.hadData, document.title, fullurl_1);
1008
+ self.emit("page", baseurl_1, path);
1009
+ }
1010
+ catch (e) {
1011
+ debug(e);
1012
+ if (replace)
1013
+ window.location.reload(true);
1014
+ else
1015
+ try {
1016
+ chash = undefined;
1017
+ forwardPopState = [path, post];
1018
+ debug("Going backwards");
1019
+ history.go(-1);
1020
+ }
1021
+ catch (e) { }
1022
+ }
1023
+ });
1024
+ }
1025
+ catch (e) {
1026
+ debug(e);
1027
+ if (replace)
1028
+ window.location.reload(true);
1029
+ else
1030
+ try {
1031
+ chash = undefined;
1032
+ forwardPopState = [path, post];
1033
+ debug("Going backwards");
1034
+ history.go(-1);
1035
+ }
1036
+ catch (e) { }
1037
+ }
1038
+ }, post, rid_1);
1039
+ }
1040
+ };
1041
+ this.registerComponent("a", AnchorElementComponent);
1042
+ this.registerComponent("form", FormElementComponent);
1043
+ window.addEventListener('popstate', function (e) {
1044
+ try {
1045
+ var state;
1046
+ var bhash = location.href.match(beforeHash);
1047
+ var cStates = pageStates.length;
1048
+ var baseurl = bhash[1];
1049
+ for (var i = 0; i < cStates; i++) {
1050
+ var _state = pageStates[i];
1051
+ if (_state.url === baseurl) {
1052
+ state = _state;
1053
+ break;
1054
+ }
1055
+ }
1056
+ var hasState = !!state;
1057
+ var rid_2 = ++self.activerid;
1058
+ var error = hasState && state.data.error;
1059
+ if (error) {
1060
+ showError(error);
1061
+ return;
1062
+ }
1063
+ if (forwardPopState) {
1064
+ debug("forwardPopState", forwardPopState);
1065
+ var forward_1 = forwardPopState;
1066
+ setTimeout(function () {
1067
+ if (rid_2 === self.activerid)
1068
+ self.defaultRequestPage(forward_1[0], forward_1[1]);
1069
+ });
1070
+ forwardPopState = undefined;
1071
+ return;
1072
+ }
1073
+ if (chash === baseurl) {
1074
+ debug("Only hash has changed...", baseurl);
1075
+ return;
1076
+ }
1077
+ chash = bhash[1];
1078
+ if (e.state)
1079
+ alert("You submitted data to this page, which cannot be re-sent. Because of this, what you're viewing may not be the same now.");
1080
+ if (!hasState)
1081
+ throw new Error("No state for: " + baseurl + ", reloading...");
1082
+ if (state.data.user != self.currentUserID)
1083
+ throw new Error("User has changed since state was created, reloading...");
1084
+ document.title = state.data.title;
1085
+ loader.resetProgress();
1086
+ loader.resetError();
1087
+ self.pagesyshandler(state.data.response, function (err) {
1088
+ if (rid_2 !== self.activerid)
1089
+ return;
1090
+ try {
1091
+ if (err)
1092
+ throw err;
1093
+ if (state.data.body)
1094
+ self.restoreComponents(document.body, state.data.body);
1095
+ if (state.data.scroll)
1096
+ window.scrollTo.apply(window, state.data.scroll);
1097
+ debug("Used stored page state!", state);
1098
+ }
1099
+ catch (err) {
1100
+ debug(err);
1101
+ var url = location.href;
1102
+ if (startsWith.test(url)) {
1103
+ try {
1104
+ if (/reloading\.\.\.$/.test(err.message)) {
1105
+ self.requestPage(url.substring(self.url.length), undefined, true);
1106
+ return;
1107
+ }
1108
+ console.error("Unknown error occured, actually navigating to page...");
1109
+ }
1110
+ catch (e) {
1111
+ console.error(e);
1112
+ }
1113
+ }
1114
+ location.reload(true);
1115
+ }
1116
+ });
1117
+ }
1118
+ catch (err) {
1119
+ debug(err);
1120
+ var url = location.href;
1121
+ if (startsWith.test(url)) {
1122
+ try {
1123
+ if (/reloading\.\.\.$/.test(err.message)) {
1124
+ self.requestPage(url.substring(self.url.length), undefined, true);
1125
+ return;
1126
+ }
1127
+ console.error("Unknown error occured, actually navigating to page...");
1128
+ }
1129
+ catch (e) {
1130
+ console.error(e);
1131
+ }
1132
+ }
1133
+ location.reload(true);
1134
+ }
1135
+ });
1136
+ return true;
1137
+ };
1138
+ Object.defineProperty(NexusFrameworkBase.prototype, "currentUser", {
1139
+ get: function () {
1140
+ return this.currentUserID;
1141
+ },
1142
+ enumerable: true,
1143
+ configurable: true
1144
+ });
1145
+ NexusFrameworkBase.prototype.defaultRequestPage = function (path, post) {
1146
+ if (post)
1147
+ throw new Error("Posting is not supported without an initialized page system, yet");
1148
+ else
1149
+ location.href = this.resolveUrl(path);
1150
+ };
1151
+ NexusFrameworkBase.prototype.on = function (event, cb) {
1152
+ var listeners = this._listeners[event];
1153
+ if (listeners)
1154
+ listeners.push(cb);
1155
+ else
1156
+ this._listeners[event] = listeners = [cb];
1157
+ };
1158
+ NexusFrameworkBase.prototype.off = function (event, cb) {
1159
+ var index;
1160
+ var listeners = this._listeners[event];
1161
+ if (listeners && (index = listeners.indexOf(cb)) > -1)
1162
+ listeners.splice(index, 1);
1163
+ };
1164
+ NexusFrameworkBase.prototype.emit = function (event) {
1165
+ var args = [];
1166
+ for (var _i = 1; _i < arguments.length; _i++) {
1167
+ args[_i - 1] = arguments[_i];
1168
+ }
1169
+ var self = this;
1170
+ var listeners = this._listeners[event];
1171
+ if (listeners)
1172
+ listeners.forEach(function (cb) {
1173
+ cb.apply(self, args);
1174
+ });
1175
+ };
1176
+ return NexusFrameworkBase;
1177
+ }());
1178
+ var impl;
1179
+ if (window.io)
1180
+ impl = /** @class */ (function (_super) {
1181
+ __extends(NexusFrameworkWithIO, _super);
1182
+ function NexusFrameworkWithIO(url) {
1183
+ var _this = _super.call(this, url, window.io({
1184
+ path: "/:io"
1185
+ })) || this;
1186
+ var io = _this.io;
1187
+ io.on("connect", function () {
1188
+ io.emit("init", loader.requestedResources());
1189
+ });
1190
+ return _this;
1191
+ }
1192
+ return NexusFrameworkWithIO;
1193
+ }(NexusFrameworkBase));
1194
+ else
1195
+ impl = /** @class */ (function (_super) {
1196
+ __extends(NexusFrameworkNoIO, _super);
1197
+ function NexusFrameworkNoIO(url) {
1198
+ return _super.call(this, url) || this;
1199
+ }
1200
+ return NexusFrameworkNoIO;
1201
+ }(NexusFrameworkBase));
1202
+ Object.defineProperty(window, "NexusFrameworkImpl", {
1203
+ value: impl
1204
+ });
1205
+ return impl;
1206
+ }
1207
+ },
1208
+ NexusFrameworkClient: {
1209
+ configurable: true,
1210
+ set: function (instance) {
1211
+ Object.defineProperty(window, "NexusFrameworkClient", {
1212
+ value: instance
1213
+ });
1214
+ },
1215
+ get: function () {
1216
+ var instance = new window.NexusFrameworkImpl();
1217
+ Object.defineProperty(window, "NexusFrameworkClient", {
1218
+ value: instance
1219
+ });
1220
+ return instance;
1221
+ }
1222
+ }
1223
+ });
1224
+ })(window);
1225
1225
  //# sourceMappingURL=nexusframeworkclient.js.map