amadeus.js
7.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/**
* Amadeus related variables and functions
* to use systemwide in development
*/
var Amadeus = {
/**
* Set up page
*/
setup: function(callback) {
// run callback Function (if exsists)
if (callback) callback();
// set environment variables
Amadeus.setEnvVars(function() {
// import templates
Amadeus.importTemplates(function() {
// set up the navbar
Amadeus.setUpNavbar();
// set environment variables for templates
Amadeus.setUpMain();
});
});
},
/**
* Path related variables and functions
*/
paths: {
// application host
host: window.location.protocol + '//' + window.location.host,
// application resolved host
resolveHost: function() {
return window.location.href.split(/\/html\//g)[0] || Amadeus.paths.host;
},
// assets path
assets: function(path) {
base = Amadeus.paths.resolveHost() + '/assets';
if (path) {
return base + path;
} else {
return base;
}
},
// components path
components: function(path) {
base = Amadeus.paths.resolveHost() + '/html/components';
if (path) {
return base + path;
} else {
return base;
}
},
// screens path
screens: function(path) {
base = Amadeus.paths.resolveHost() + '/html/screens';
if (path) {
return base + path;
} else {
return base;
}
},
// templates path
templates: function(path) {
base = Amadeus.paths.resolveHost() + '/html/templates';
if (path) {
return base + path;
} else {
return base;
}
},
// default paths
defaults: function() {
return {
templatePath: Amadeus.paths.templates(),
hostPath: Amadeus.paths.resolveHost(),
assetPath: Amadeus.paths.assets(),
screenPath: Amadeus.paths.screens(),
componentPath: Amadeus.paths.components(),
controllerPath: Amadeus.paths.assets('/js/controllers')
}
}
},
/**
* Util funcitions
*/
utils: {
/**
* Function responsible to load an Array of files in chain
*/
progressiveLoad: function(lst, func, callback) {
if (lst instanceof Array) {
if (lst.length > 1) {
func(lst[0], function(){
lst.shift();
Amadeus.utils.progressiveLoad(lst, func, callback);
});
} else if (lst.length === 1) {
func(lst[0], callback || function(){});
} else {
callback();
}
} else {
if (callback) {
callback();
}
}
},
/**
* load a script into head
*/
loadScript: function(url, callback) {
// Adding the script tag to the head as suggested before
var head = document.getElementsByTagName('head')[0];
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = url;
script.onload = callback;
// Fire the loading
head.appendChild(script);
},
/**
* Function responsible to fetch stylesheets
*/
loadStyle: function(url, callback) {
// Adding the script tag to the head as suggested before
var head = document.getElementsByTagName('head')[0];
var style = document.createElement('link');
style.rel = 'stylesheet';
style.type = 'text/css';
style.href = url;
// Then bind the event to the callback function.
// There are several events for cross browser compatibility.
//style.onreadystatechange = callback;
style.onload = callback;
// Fire the loading
head.appendChild(style);
}
},
/**
* Import HTML templates using w3data library
*/
importTemplates: function(callback) {
w3IncludeHTML();
if (callback) callback();
},
/**
* Set location in breadcrumb
*/
setBreadcrumb: function(inactive, active) {
setTimeout(function() {
w3DisplayData('breadcrumb', { 'active': active, inactive: inactive });
if (inactive === null) {
$('#breadcrumb').addClass('no-inactive');
}
}, Amadeus.default.delay || 500);
},
/**
* Set Up the system navbar
*/
setUpNavbar: function(callback) {
setTimeout(function() {
w3DisplayData('navbar', Amadeus.paths.defaults());
if (callback) callback();
}, Amadeus.default.delay || 500);
},
/**
* Set Up the system main content env variables
*/
setUpMain: function(callback) {
setTimeout(function() {
w3DisplayData('main', Amadeus.paths.defaults());
if (callback) callback();
}, Amadeus.default.delay || 500);
},
/**
* Set up needed environment vars
*/
setEnvVars: function(callback) {
w3DisplayData('body', Amadeus.paths.defaults());
if (callback) callback();
},
/**
* load controller custom scrips if exsists
*/
loadControllers: function(callback) {
scripts = document.getElementsByTagName('script');
for (i = 0; i < scripts.length; i++) {
if (scripts[i].src.indexOf(Amadeus.paths.assets('/js/controllers')) >= 0) {
Amadeus.utils.progressiveLoad([scripts[i].src], Amadeus.utils.loadScript);
parent = scripts[i].parentNode;
parent.removeChild(scripts[i]);
}
}
if (callback) callback();
},
/**
* Responsible to load necessary assets
*/
load: function(callback) {
settings = Amadeus.paths.resolveHost() + '/settings.js'
with (Amadeus.utils) {
progressiveLoad([settings], loadScript, function() {
if (Amadeus.default) {
progressiveLoad(Amadeus.default.scripts || [], loadScript, function() {
progressiveLoad(Amadeus.default.styles || [], loadStyle, function() {
Amadeus.setup(function() {
if (callback) callback();
setTimeout(function() {
$.material.init();
Amadeus.loadControllers();
progressiveLoad([Amadeus.paths.assets('/js/main.js')], loadScript);
}, Amadeus.default.delay * 2 || 1000);
});
});
});
}
});
}
}
};