-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGravity.ts
238 lines (206 loc) · 5.81 KB
/
Gravity.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
///<reference path="./Environment/Scope.ts"/>
///<reference path="./Environment/Config.ts"/>
///<reference path="./Helper/ArrayHelper.ts"/>
namespace Gravity
{
export class Application
{
/**
*
*/
private config : Object = {};
/**
*
*/
private try = 0;
/**
*
*/
private env : number = Environment.Scope.LOCAL;
/**
*
*/
private catchErrors : Function = function () {};
/**
*
*/
private domManager = new Gravity.View.DomManager;
/**
*
*/
public constructor()
{
window.onbeforeunload = function () {
sessionStorage.clear();
}
}
/**
*
*/
public setScope(env : number)
{
this.env = env;
}
/**
*
*/
public setConfig(config : Environment.Config)
{
this.config = config.getConfig(this.env);
}
/**
*
*/
public getConfig()
{
return this.config;
}
/**
*
*/
private resolveConfig(di)
{
var positionArray = new Array();
var configData = this.config;
for (let key in configData) {
switch (key) {
case "urls":
this.resolveUrl(
di,
configData[key]
);
break;
case "services":
this.resolveServices(
di,
configData[key]
);
break;
}
}
//controllers executed in the final section
if (configData.hasOwnProperty("controllers")) {
this.resolveControllers(
di,
configData["controllers"]
);
} else {
throw "Config must have controllers item attached"
}
}
/**
*
*/
private resolveUrl(di, urls)
{
var url = new Url.Url();
if (Array.isArray(urls)) {
for (let key in urls) {
if (typeof urls[key] == "string") {
url.set(
key,
urls[key]
);
} else {
throw "Url must be string : " + urls[key];
}
}
} else if(typeof url == "object") {
for (var keyUrlFor in urls) {
url.set(
keyUrlFor,
urls[keyUrlFor]
);
}
} else {
throw "Url data unrecognized"
}
di.set(
"url",
url
);
}
/**
*
*/
private resolveControllers(di, controllers : any[])
{
if (controllers.length == 0) {
throw "You must load your controllers";
}
if (Array.isArray(controllers)) {
let i = 1;
for (let key in controllers) {
if (typeof controllers[key].name != "undefined") {
var temp = new controllers[key].name;
if (temp instanceof Gravity.Mvc.Controller) {
temp.setDi(di);
temp.initialize();
this.resolvePropertiesController(
temp
);
} else {
throw "Controller #" + i + " must be extend from View.Controller class";
}
i++;
} else {
throw "Config => Controller => 'name' must be initialized with View.Controller class"
}
}
} else {
throw "Config => controllers must be array"
}
}
private resolvePropertiesController(controller : Gravity.Mvc.Controller)
{
var restricted = [
"constructor",
"initialize",
"getById",
"getByTag",
"getByClass",
"getDi",
"setDi",
"getUrl",
"setUrl"
];
for (var key in controller) {
switch (typeof controller[key]) {
case "function":
if (!Helper.ArrayHelper.inArray(restricted, key)) {
var component = this.domManager.getById(key);
if (component) {
controller[key](component);
}
}
break;
}
}
controller.inject();
}
/**
*
*/
private resolveServices(di, service)
{
new service().initialize(di);
}
public catch(fn)
{
this.catchErrors = fn;
return this;
}
/**
*
*/
public start()
{
try {
var di = new Service.FactoryDefault;
this.resolveConfig(di);
} catch (e) {
this.catchErrors(e);
}
}
}
}