tina4js 0.0.2 → 0.0.3
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.
- package/.parcelrc +4 -4
- package/bin/tina4.js +103 -0
- package/index.html +10 -10
- package/package.json +31 -27
- package/readme.md +169 -165
- package/tina4/Api.ts +41 -41
- package/tina4/Get.ts +8 -8
- package/tina4/Globals.ts +31 -31
- package/tina4/History.ts +25 -25
- package/tina4/Post.ts +9 -9
- package/tina4/Route.ts +2 -2
- package/tina4/Router.ts +163 -163
- package/tina4/Tina4.ts +94 -94
- package/tina4/components/Tina4Api.ts +12 -12
- package/tina4/components/Tina4Config.ts +9 -9
- package/tina4.ts +5 -5
package/tina4/Globals.ts
CHANGED
|
@@ -1,32 +1,32 @@
|
|
|
1
|
-
export class Globals {
|
|
2
|
-
|
|
3
|
-
static initialize () {
|
|
4
|
-
window['tina4'] = {};
|
|
5
|
-
}
|
|
6
|
-
|
|
7
|
-
static defined() {
|
|
8
|
-
return (window['tina4'] !== undefined);
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
static set(name, value) {
|
|
12
|
-
if (!this.defined()) {
|
|
13
|
-
this.initialize();
|
|
14
|
-
}
|
|
15
|
-
window['tina4'][name] = value;
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
static append (name, value) {
|
|
19
|
-
if (window['tina4'][name] === undefined) {
|
|
20
|
-
window['tina4'][name] = [];
|
|
21
|
-
}
|
|
22
|
-
window['tina4'][name].push(value);
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
static get (name) {
|
|
26
|
-
if (this.defined() && window['tina4'][name]) {
|
|
27
|
-
return window['tina4'][name];
|
|
28
|
-
} else {
|
|
29
|
-
return null;
|
|
30
|
-
}
|
|
31
|
-
}
|
|
1
|
+
export class Globals {
|
|
2
|
+
|
|
3
|
+
static initialize () {
|
|
4
|
+
window['tina4'] = {};
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
static defined() {
|
|
8
|
+
return (window['tina4'] !== undefined);
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
static set(name, value) {
|
|
12
|
+
if (!this.defined()) {
|
|
13
|
+
this.initialize();
|
|
14
|
+
}
|
|
15
|
+
window['tina4'][name] = value;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
static append (name, value) {
|
|
19
|
+
if (window['tina4'][name] === undefined) {
|
|
20
|
+
window['tina4'][name] = [];
|
|
21
|
+
}
|
|
22
|
+
window['tina4'][name].push(value);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
static get (name) {
|
|
26
|
+
if (this.defined() && window['tina4'][name]) {
|
|
27
|
+
return window['tina4'][name];
|
|
28
|
+
} else {
|
|
29
|
+
return null;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
32
|
}
|
package/tina4/History.ts
CHANGED
|
@@ -1,26 +1,26 @@
|
|
|
1
|
-
export class History {
|
|
2
|
-
|
|
3
|
-
static addHistory(path, target, method) {
|
|
4
|
-
let historyInfo = JSON.parse(localStorage.getItem('history'));
|
|
5
|
-
let pathInfo = {path: null};
|
|
6
|
-
if (historyInfo.length > 0) {
|
|
7
|
-
pathInfo = historyInfo[historyInfo.length - 1];
|
|
8
|
-
}
|
|
9
|
-
if (method === 'GET' && pathInfo.path !== path || method === 'POST') {
|
|
10
|
-
historyInfo.push({path: path, target: target});
|
|
11
|
-
localStorage.setItem('history', JSON.stringify(historyInfo));
|
|
12
|
-
}
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
static resolveHistory() {
|
|
16
|
-
let historyInfo = JSON.parse(localStorage.getItem('history'));
|
|
17
|
-
historyInfo.pop();
|
|
18
|
-
let pathInfo = {path: "/", target: null};
|
|
19
|
-
if (historyInfo.length > 0) {
|
|
20
|
-
pathInfo = historyInfo[historyInfo.length - 1];
|
|
21
|
-
localStorage.setItem('history', JSON.stringify(historyInfo));
|
|
22
|
-
}
|
|
23
|
-
return pathInfo;
|
|
24
|
-
}
|
|
25
|
-
|
|
1
|
+
export class History {
|
|
2
|
+
|
|
3
|
+
static addHistory(path, target, method) {
|
|
4
|
+
let historyInfo = JSON.parse(localStorage.getItem('history'));
|
|
5
|
+
let pathInfo = {path: null};
|
|
6
|
+
if (historyInfo.length > 0) {
|
|
7
|
+
pathInfo = historyInfo[historyInfo.length - 1];
|
|
8
|
+
}
|
|
9
|
+
if (method === 'GET' && pathInfo.path !== path || method === 'POST') {
|
|
10
|
+
historyInfo.push({path: path, target: target});
|
|
11
|
+
localStorage.setItem('history', JSON.stringify(historyInfo));
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
static resolveHistory() {
|
|
16
|
+
let historyInfo = JSON.parse(localStorage.getItem('history'));
|
|
17
|
+
historyInfo.pop();
|
|
18
|
+
let pathInfo = {path: "/", target: null};
|
|
19
|
+
if (historyInfo.length > 0) {
|
|
20
|
+
pathInfo = historyInfo[historyInfo.length - 1];
|
|
21
|
+
localStorage.setItem('history', JSON.stringify(historyInfo));
|
|
22
|
+
}
|
|
23
|
+
return pathInfo;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
26
|
}
|
package/tina4/Post.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import {Route} from "./Route"
|
|
2
|
-
import {Router} from "./Router";
|
|
3
|
-
|
|
4
|
-
export class Post implements Route {
|
|
5
|
-
static;
|
|
6
|
-
private method : string = 'POST';
|
|
7
|
-
add(path: string, callback): void {
|
|
8
|
-
Router.add(this.method, path, callback)
|
|
9
|
-
}
|
|
1
|
+
import {Route} from "./Route"
|
|
2
|
+
import {Router} from "./Router";
|
|
3
|
+
|
|
4
|
+
export class Post implements Route {
|
|
5
|
+
static;
|
|
6
|
+
private method : string = 'POST';
|
|
7
|
+
add(path: string, callback): void {
|
|
8
|
+
Router.add(this.method, path, callback)
|
|
9
|
+
}
|
|
10
10
|
}
|
package/tina4/Route.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
export interface Route {
|
|
2
|
-
add(path: string, callback):void;
|
|
1
|
+
export interface Route {
|
|
2
|
+
add(path: string, callback):void;
|
|
3
3
|
}
|
package/tina4/Router.ts
CHANGED
|
@@ -1,163 +1,163 @@
|
|
|
1
|
-
import {Globals} from "./Globals";
|
|
2
|
-
import {Tina4} from "./Tina4";
|
|
3
|
-
|
|
4
|
-
export class Router {
|
|
5
|
-
|
|
6
|
-
private readonly url: string;
|
|
7
|
-
private readonly target: string;
|
|
8
|
-
private readonly method: string;
|
|
9
|
-
private readonly data;
|
|
10
|
-
private matchesPath;
|
|
11
|
-
private matchesUrl;
|
|
12
|
-
private params;
|
|
13
|
-
private matched;
|
|
14
|
-
|
|
15
|
-
constructor(url, target, method, data=null) {
|
|
16
|
-
//console.log('Constructing', url, target, method, data);
|
|
17
|
-
if (method === undefined) method = 'GET';
|
|
18
|
-
this.url = url;
|
|
19
|
-
this.target = target;
|
|
20
|
-
this.method = method;
|
|
21
|
-
this.data = data;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
static add(method, path, callback) {
|
|
25
|
-
Tina4.initialize();
|
|
26
|
-
//console.log('Adding Route', method, path);
|
|
27
|
-
if (Globals.defined()) {
|
|
28
|
-
Globals.append('routes', {method: method, path: path, callback: callback});
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
static response(content, httpCode, contentType) {
|
|
33
|
-
console.log('Response', content, httpCode, contentType);
|
|
34
|
-
|
|
35
|
-
//manipulate content based on the contentType
|
|
36
|
-
if ( typeof content === 'object' &&
|
|
37
|
-
!Array.isArray(content) &&
|
|
38
|
-
content !== null
|
|
39
|
-
) {
|
|
40
|
-
content = JSON.stringify(content);
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
return content;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
//https://stackoverflow.com/questions/831030/how-to-get-get-request-parameters-in-javascript
|
|
47
|
-
getRequestParams() {
|
|
48
|
-
let s1 = location.search.substring(1, location.search.length).split('&'),
|
|
49
|
-
r = {}, s2, i;
|
|
50
|
-
for (i = 0; i < s1.length; i += 1) {
|
|
51
|
-
s2 = s1[i].split('=');
|
|
52
|
-
r[decodeURIComponent(s2[0])] = decodeURIComponent(s2[1]);
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
if (JSON.stringify(r) == '{"":"undefined"}') return {};
|
|
56
|
-
return r;
|
|
57
|
-
};
|
|
58
|
-
|
|
59
|
-
cleanUrl(url) {
|
|
60
|
-
const regex = /(.*)\/\/(.*)\//g;
|
|
61
|
-
url = url.replace(regex, '/').replace('#','');
|
|
62
|
-
return url;
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
match (url:string, path:string, method:string): boolean {
|
|
66
|
-
if (this.method !== method) return false;
|
|
67
|
-
url = this.cleanUrl(url);
|
|
68
|
-
//console.log('Matching', url, path, method);
|
|
69
|
-
const urlExpression = /(.*)\/(.*)|{(.*)}/g;
|
|
70
|
-
const pathExpression = /(.*)\/(.*)|{(.*)}/g;
|
|
71
|
-
|
|
72
|
-
if (url === path) {
|
|
73
|
-
return true;
|
|
74
|
-
} else {
|
|
75
|
-
this.matchesUrl = urlExpression.exec (url);
|
|
76
|
-
this.matchesPath = pathExpression.exec (path);
|
|
77
|
-
if (this.matchesUrl.length === this.matchesPath.length) {
|
|
78
|
-
this.matched = true;
|
|
79
|
-
this.matchesUrl.every(function (urlPath, index) {
|
|
80
|
-
if (index !== 0 && urlPath !== undefined) {
|
|
81
|
-
//console.log('Matching', urlPath, this.matchesPath[index]);
|
|
82
|
-
if (urlPath !== this.matchesPath[index] && this.matchesPath[index][0] !== '{') {
|
|
83
|
-
//console.log('Not matching', url, path);
|
|
84
|
-
this.matched = false;
|
|
85
|
-
return false;
|
|
86
|
-
} else if (this.matchesPath[index][0] === '{') {
|
|
87
|
-
this.params[this.matchesPath[index].replace('{', '').replace('}', '')] = urlPath;
|
|
88
|
-
this.matched = true;
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
return true;
|
|
92
|
-
}, this);
|
|
93
|
-
if (this.matched) return true;
|
|
94
|
-
} else {
|
|
95
|
-
return false;
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
parse(url: string, target: string, callback) {
|
|
101
|
-
//console.log (window['tina4']['routes']);
|
|
102
|
-
let routes = Globals.get('routes');
|
|
103
|
-
let rendered = false;
|
|
104
|
-
routes.every(function(route){
|
|
105
|
-
if (this.match(url, route.path, route.method )) {
|
|
106
|
-
if (route.method === 'GET') {
|
|
107
|
-
history.pushState({}, '', url);
|
|
108
|
-
}
|
|
109
|
-
route.callback( function(content, httpCode, contentType) {
|
|
110
|
-
callback ( target, Router.response(content, httpCode, contentType) );
|
|
111
|
-
}, this.params );
|
|
112
|
-
rendered = true;
|
|
113
|
-
}
|
|
114
|
-
return true;
|
|
115
|
-
}.bind(this));
|
|
116
|
-
|
|
117
|
-
if (!rendered) {
|
|
118
|
-
//See if we can find a twig file
|
|
119
|
-
Tina4.renderTemplate(`${url}.twig`, {}, function (html) {
|
|
120
|
-
callback(target, Router.response(html, 200, 'text/html'));
|
|
121
|
-
});
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
submitHandler(event) {
|
|
126
|
-
if (event.preventDefault) event.preventDefault();
|
|
127
|
-
let form = event.target;
|
|
128
|
-
//console.log ('Form', form.action, window.location.pathname, form);
|
|
129
|
-
if (form.action === undefined) form.action = window.location.pathname;
|
|
130
|
-
|
|
131
|
-
// @ts-ignore
|
|
132
|
-
window.navigate(form.action, form.getAttribute("target") , 'POST', form);
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
run() {
|
|
136
|
-
// @ts-ignore
|
|
137
|
-
window.submitHandler = this.submitHandler;
|
|
138
|
-
this.params = this.getRequestParams();
|
|
139
|
-
this.params.data = this.data;
|
|
140
|
-
this.parse(this.url, this.target, function (target, content) {
|
|
141
|
-
//console.log ('Target', target, content);
|
|
142
|
-
if (document.getElementById(target) !== null) {
|
|
143
|
-
document.getElementById(target).innerHTML = content;
|
|
144
|
-
//Attach the form submit handler
|
|
145
|
-
let forms = Array.prototype.slice.call(document.getElementsByTagName('form'));
|
|
146
|
-
forms.forEach(function (form) {
|
|
147
|
-
//console.log('Found form', form.method);
|
|
148
|
-
if (form.attachEvent) {
|
|
149
|
-
// @ts-ignore
|
|
150
|
-
form.attachEvent("submit", window.submitHandler);
|
|
151
|
-
} else {
|
|
152
|
-
// @ts-ignore
|
|
153
|
-
form.addEventListener("submit", window.submitHandler);
|
|
154
|
-
}
|
|
155
|
-
});
|
|
156
|
-
} else {
|
|
157
|
-
console.log('Cannot find route')
|
|
158
|
-
}
|
|
159
|
-
});
|
|
160
|
-
}
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
|
|
1
|
+
import {Globals} from "./Globals";
|
|
2
|
+
import {Tina4} from "./Tina4";
|
|
3
|
+
|
|
4
|
+
export class Router {
|
|
5
|
+
|
|
6
|
+
private readonly url: string;
|
|
7
|
+
private readonly target: string;
|
|
8
|
+
private readonly method: string;
|
|
9
|
+
private readonly data;
|
|
10
|
+
private matchesPath;
|
|
11
|
+
private matchesUrl;
|
|
12
|
+
private params;
|
|
13
|
+
private matched;
|
|
14
|
+
|
|
15
|
+
constructor(url, target, method, data=null) {
|
|
16
|
+
//console.log('Constructing', url, target, method, data);
|
|
17
|
+
if (method === undefined) method = 'GET';
|
|
18
|
+
this.url = url;
|
|
19
|
+
this.target = target;
|
|
20
|
+
this.method = method;
|
|
21
|
+
this.data = data;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
static add(method, path, callback) {
|
|
25
|
+
Tina4.initialize();
|
|
26
|
+
//console.log('Adding Route', method, path);
|
|
27
|
+
if (Globals.defined()) {
|
|
28
|
+
Globals.append('routes', {method: method, path: path, callback: callback});
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
static response(content, httpCode, contentType) {
|
|
33
|
+
console.log('Response', content, httpCode, contentType);
|
|
34
|
+
|
|
35
|
+
//manipulate content based on the contentType
|
|
36
|
+
if ( typeof content === 'object' &&
|
|
37
|
+
!Array.isArray(content) &&
|
|
38
|
+
content !== null
|
|
39
|
+
) {
|
|
40
|
+
content = JSON.stringify(content);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
return content;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
//https://stackoverflow.com/questions/831030/how-to-get-get-request-parameters-in-javascript
|
|
47
|
+
getRequestParams() {
|
|
48
|
+
let s1 = location.search.substring(1, location.search.length).split('&'),
|
|
49
|
+
r = {}, s2, i;
|
|
50
|
+
for (i = 0; i < s1.length; i += 1) {
|
|
51
|
+
s2 = s1[i].split('=');
|
|
52
|
+
r[decodeURIComponent(s2[0])] = decodeURIComponent(s2[1]);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
if (JSON.stringify(r) == '{"":"undefined"}') return {};
|
|
56
|
+
return r;
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
cleanUrl(url) {
|
|
60
|
+
const regex = /(.*)\/\/(.*)\//g;
|
|
61
|
+
url = url.replace(regex, '/').replace('#','');
|
|
62
|
+
return url;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
match (url:string, path:string, method:string): boolean {
|
|
66
|
+
if (this.method !== method) return false;
|
|
67
|
+
url = this.cleanUrl(url);
|
|
68
|
+
//console.log('Matching', url, path, method);
|
|
69
|
+
const urlExpression = /(.*)\/(.*)|{(.*)}/g;
|
|
70
|
+
const pathExpression = /(.*)\/(.*)|{(.*)}/g;
|
|
71
|
+
|
|
72
|
+
if (url === path) {
|
|
73
|
+
return true;
|
|
74
|
+
} else {
|
|
75
|
+
this.matchesUrl = urlExpression.exec (url);
|
|
76
|
+
this.matchesPath = pathExpression.exec (path);
|
|
77
|
+
if (this.matchesUrl.length === this.matchesPath.length) {
|
|
78
|
+
this.matched = true;
|
|
79
|
+
this.matchesUrl.every(function (urlPath, index) {
|
|
80
|
+
if (index !== 0 && urlPath !== undefined) {
|
|
81
|
+
//console.log('Matching', urlPath, this.matchesPath[index]);
|
|
82
|
+
if (urlPath !== this.matchesPath[index] && this.matchesPath[index][0] !== '{') {
|
|
83
|
+
//console.log('Not matching', url, path);
|
|
84
|
+
this.matched = false;
|
|
85
|
+
return false;
|
|
86
|
+
} else if (this.matchesPath[index][0] === '{') {
|
|
87
|
+
this.params[this.matchesPath[index].replace('{', '').replace('}', '')] = urlPath;
|
|
88
|
+
this.matched = true;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
return true;
|
|
92
|
+
}, this);
|
|
93
|
+
if (this.matched) return true;
|
|
94
|
+
} else {
|
|
95
|
+
return false;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
parse(url: string, target: string, callback) {
|
|
101
|
+
//console.log (window['tina4']['routes']);
|
|
102
|
+
let routes = Globals.get('routes');
|
|
103
|
+
let rendered = false;
|
|
104
|
+
routes.every(function(route){
|
|
105
|
+
if (this.match(url, route.path, route.method )) {
|
|
106
|
+
if (route.method === 'GET') {
|
|
107
|
+
history.pushState({}, '', url);
|
|
108
|
+
}
|
|
109
|
+
route.callback( function(content, httpCode, contentType) {
|
|
110
|
+
callback ( target, Router.response(content, httpCode, contentType) );
|
|
111
|
+
}, this.params );
|
|
112
|
+
rendered = true;
|
|
113
|
+
}
|
|
114
|
+
return true;
|
|
115
|
+
}.bind(this));
|
|
116
|
+
|
|
117
|
+
if (!rendered) {
|
|
118
|
+
//See if we can find a twig file
|
|
119
|
+
Tina4.renderTemplate(`${url}.twig`, {}, function (html) {
|
|
120
|
+
callback(target, Router.response(html, 200, 'text/html'));
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
submitHandler(event) {
|
|
126
|
+
if (event.preventDefault) event.preventDefault();
|
|
127
|
+
let form = event.target;
|
|
128
|
+
//console.log ('Form', form.action, window.location.pathname, form);
|
|
129
|
+
if (form.action === undefined) form.action = window.location.pathname;
|
|
130
|
+
|
|
131
|
+
// @ts-ignore
|
|
132
|
+
window.navigate(form.action, form.getAttribute("target") , 'POST', form);
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
run() {
|
|
136
|
+
// @ts-ignore
|
|
137
|
+
window.submitHandler = this.submitHandler;
|
|
138
|
+
this.params = this.getRequestParams();
|
|
139
|
+
this.params.data = this.data;
|
|
140
|
+
this.parse(this.url, this.target, function (target, content) {
|
|
141
|
+
//console.log ('Target', target, content);
|
|
142
|
+
if (document.getElementById(target) !== null) {
|
|
143
|
+
document.getElementById(target).innerHTML = content;
|
|
144
|
+
//Attach the form submit handler
|
|
145
|
+
let forms = Array.prototype.slice.call(document.getElementsByTagName('form'));
|
|
146
|
+
forms.forEach(function (form) {
|
|
147
|
+
//console.log('Found form', form.method);
|
|
148
|
+
if (form.attachEvent) {
|
|
149
|
+
// @ts-ignore
|
|
150
|
+
form.attachEvent("submit", window.submitHandler);
|
|
151
|
+
} else {
|
|
152
|
+
// @ts-ignore
|
|
153
|
+
form.addEventListener("submit", window.submitHandler);
|
|
154
|
+
}
|
|
155
|
+
});
|
|
156
|
+
} else {
|
|
157
|
+
console.log('Cannot find route')
|
|
158
|
+
}
|
|
159
|
+
});
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
|
package/tina4/Tina4.ts
CHANGED
|
@@ -1,95 +1,95 @@
|
|
|
1
|
-
import {Globals} from "./Globals"
|
|
2
|
-
import {Router} from "./Router";
|
|
3
|
-
import {History} from "./History";
|
|
4
|
-
import Twig from 'twig';
|
|
5
|
-
import {Tina4Config} from "./components/Tina4Config";
|
|
6
|
-
import {Tina4Api} from "./components/Tina4Api";
|
|
7
|
-
|
|
8
|
-
export class Tina4 {
|
|
9
|
-
constructor(config = null) {
|
|
10
|
-
if (config === undefined || config === null) {
|
|
11
|
-
if (Globals.defined() && Globals.get('config')) {
|
|
12
|
-
config = Globals.get('config');
|
|
13
|
-
} else {
|
|
14
|
-
config = {defaultTarget: 'root'};
|
|
15
|
-
}
|
|
16
|
-
}
|
|
17
|
-
console.log('Config found', config);
|
|
18
|
-
Tina4.resolveRoutes(window.location.pathname, config.defaultTarget);
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
static initialize(config = null) {
|
|
22
|
-
if (!Globals.defined()) {
|
|
23
|
-
console.log('Initialize Tina4');
|
|
24
|
-
Globals.initialize();
|
|
25
|
-
Globals.set('routes', []);
|
|
26
|
-
Globals.set('twig', Twig);
|
|
27
|
-
localStorage.setItem('history', JSON.stringify([]));
|
|
28
|
-
Tina4.registerComponents();
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
if (config !== null) {
|
|
32
|
-
Globals.set('config', config);
|
|
33
|
-
} else {
|
|
34
|
-
Globals.set('config', {defaultTarget: 'root'});
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
//back navigation in the browser
|
|
38
|
-
window.onpopstate=function(e){
|
|
39
|
-
if (e.state) {
|
|
40
|
-
let pathInfo = History.resolveHistory ();
|
|
41
|
-
Tina4.resolveRoutes(pathInfo.path, pathInfo.target, 'GET', {});
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
//add the navigate handler
|
|
46
|
-
// @ts-ignore
|
|
47
|
-
window.navigate = function (path, target = 'root', method: string = 'GET', data = null) {
|
|
48
|
-
if (Globals.get('config').defaultTarget !== null) {
|
|
49
|
-
target = Globals.get('config').defaultTarget;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
Tina4.resolveRoutes(path, target, method, data);
|
|
53
|
-
};
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
static registerComponents() {
|
|
57
|
-
console.log('Register components');
|
|
58
|
-
customElements.define('tina4-config', Tina4Config);
|
|
59
|
-
customElements.define('tina4-api', Tina4Api);
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
static resolveRoutes(path, target, method: string = 'GET', data = null) {
|
|
63
|
-
History.addHistory (path, target, method);
|
|
64
|
-
return new Router(path, target, method, data).run();
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
static getFileExtension(filename) {
|
|
68
|
-
let split = filename.trim().split('.');
|
|
69
|
-
return split[split.length - 1];
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
static renderTemplate(content, params, callback) {
|
|
73
|
-
if (this.getFileExtension(content) == 'twig') {
|
|
74
|
-
try {
|
|
75
|
-
Twig.twig(
|
|
76
|
-
{
|
|
77
|
-
id: content,
|
|
78
|
-
href: `/templates/${content}`,
|
|
79
|
-
async:true,
|
|
80
|
-
load: (template) => {
|
|
81
|
-
console.log(`Template loaded: `, template, params);
|
|
82
|
-
callback(template.render(params));
|
|
83
|
-
}
|
|
84
|
-
});
|
|
85
|
-
}
|
|
86
|
-
catch(exception) {
|
|
87
|
-
callback( Twig.twig({ref: content}).render(params));
|
|
88
|
-
}
|
|
89
|
-
} else {
|
|
90
|
-
let template = Twig.twig({data: content});
|
|
91
|
-
callback( template.render(params));
|
|
92
|
-
}
|
|
93
|
-
return false;
|
|
94
|
-
}
|
|
1
|
+
import {Globals} from "./Globals"
|
|
2
|
+
import {Router} from "./Router";
|
|
3
|
+
import {History} from "./History";
|
|
4
|
+
import Twig from 'twig';
|
|
5
|
+
import {Tina4Config} from "./components/Tina4Config";
|
|
6
|
+
import {Tina4Api} from "./components/Tina4Api";
|
|
7
|
+
|
|
8
|
+
export class Tina4 {
|
|
9
|
+
constructor(config = null) {
|
|
10
|
+
if (config === undefined || config === null) {
|
|
11
|
+
if (Globals.defined() && Globals.get('config')) {
|
|
12
|
+
config = Globals.get('config');
|
|
13
|
+
} else {
|
|
14
|
+
config = {defaultTarget: 'root'};
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
console.log('Config found', config);
|
|
18
|
+
Tina4.resolveRoutes(window.location.pathname, config.defaultTarget);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
static initialize(config = null) {
|
|
22
|
+
if (!Globals.defined()) {
|
|
23
|
+
console.log('Initialize Tina4');
|
|
24
|
+
Globals.initialize();
|
|
25
|
+
Globals.set('routes', []);
|
|
26
|
+
Globals.set('twig', Twig);
|
|
27
|
+
localStorage.setItem('history', JSON.stringify([]));
|
|
28
|
+
Tina4.registerComponents();
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
if (config !== null) {
|
|
32
|
+
Globals.set('config', config);
|
|
33
|
+
} else {
|
|
34
|
+
Globals.set('config', {defaultTarget: 'root'});
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
//back navigation in the browser
|
|
38
|
+
window.onpopstate=function(e){
|
|
39
|
+
if (e.state) {
|
|
40
|
+
let pathInfo = History.resolveHistory ();
|
|
41
|
+
Tina4.resolveRoutes(pathInfo.path, pathInfo.target, 'GET', {});
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
//add the navigate handler
|
|
46
|
+
// @ts-ignore
|
|
47
|
+
window.navigate = function (path, target = 'root', method: string = 'GET', data = null) {
|
|
48
|
+
if (Globals.get('config').defaultTarget !== null) {
|
|
49
|
+
target = Globals.get('config').defaultTarget;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
Tina4.resolveRoutes(path, target, method, data);
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
static registerComponents() {
|
|
57
|
+
console.log('Register components');
|
|
58
|
+
customElements.define('tina4-config', Tina4Config);
|
|
59
|
+
customElements.define('tina4-api', Tina4Api);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
static resolveRoutes(path, target, method: string = 'GET', data = null) {
|
|
63
|
+
History.addHistory (path, target, method);
|
|
64
|
+
return new Router(path, target, method, data).run();
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
static getFileExtension(filename) {
|
|
68
|
+
let split = filename.trim().split('.');
|
|
69
|
+
return split[split.length - 1];
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
static renderTemplate(content, params, callback) {
|
|
73
|
+
if (this.getFileExtension(content) == 'twig') {
|
|
74
|
+
try {
|
|
75
|
+
Twig.twig(
|
|
76
|
+
{
|
|
77
|
+
id: content,
|
|
78
|
+
href: `/templates/${content}`,
|
|
79
|
+
async:true,
|
|
80
|
+
load: (template) => {
|
|
81
|
+
console.log(`Template loaded: `, template, params);
|
|
82
|
+
callback(template.render(params));
|
|
83
|
+
}
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
catch(exception) {
|
|
87
|
+
callback( Twig.twig({ref: content}).render(params));
|
|
88
|
+
}
|
|
89
|
+
} else {
|
|
90
|
+
let template = Twig.twig({data: content});
|
|
91
|
+
callback( template.render(params));
|
|
92
|
+
}
|
|
93
|
+
return false;
|
|
94
|
+
}
|
|
95
95
|
}
|