-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathappy.js
More file actions
95 lines (87 loc) · 2.58 KB
/
appy.js
File metadata and controls
95 lines (87 loc) · 2.58 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
function AppyJS(callback) {
this.target = -1;
AppyJS.loadJquery(function() {
jQuery.getJSON("builds.json", function(b) {
this.buildsObj = b;
if (typeof callback == "function") callback();
}.bind(this));
}.bind(this));
}
AppyJS.prototype.setTargetByStage = function(stage) {
// Get target build number by stage
var target = this.buildsObj[stage];
if (typeof target != "number") {
throw new Error("Target stage ('" + stage + "') not found.");
}
this.target = target;
}
AppyJS.prototype.setTargetByBuild = function(buildNumber) {
this.target = buildNumber;
}
// Starts Application
AppyJS.prototype.start = function() {
// Search for build version
for (var i = 0; i < this.buildsObj.builds.length; i++) {
var build = this.buildsObj.builds[i];
if (build.buildNumber != this.target) {
continue;
}
if (!build.enabled) {
throw new Error("Version (" + this.target + ") is disabled.");
}
var appPath = this.buildsObj.buildsPath + build.version + "/" + build.main;
AppyJS.loadJS(appPath, function(success) {
if (!success) {
throw new Error("App failed to start.");
}
});
return;
}
throw new Error("Target build (" + this.target + ") not found.")
}
AppyJS.loadJquery = function(callback) {
// Check if jQuery is present, if not load it from Google CDN.
if (typeof jQuery == "undefined") {
AppyJS.loadJS("https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js",
function(success) {
if (!success) {
throw new Error("Failed to load jquery from google cdn.");
}
if (typeof callback == "function") callback();
}
);
} else {
if (typeof callback == "function") callback();
}
}
// Generic function used to load any script (.js)
AppyJS.loadJS = function(src, callback) {
var script = document.createElement("script");
script.type = "text/javascript";
// Create callback
if (typeof callback == "function") {
if (script.readyState) {
script.onreadystatechange = function() {
if (script.readyState == "loaded" || script.readyState == "complete") {
script.onreadystatechange = null;
if (script.status == 200) {
callback(true);
} else {
callback(false);
}
}
};
} else {
script.onload = function() {
callback(true);
};
script.onerror = function() {
callback(false);
};
}
}
// Set script path
script.src = src;
// Append script to page
document.getElementsByTagName("head")[0].appendChild(script);
}