-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathloader.js
More file actions
173 lines (167 loc) · 6.41 KB
/
loader.js
File metadata and controls
173 lines (167 loc) · 6.41 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
/*jshint esnext: true, enforceall: true, browser: true*/
/*exported loader*/
/*globals ajax, eventManager, async, LZString*/
/**
* @file
* @author Ángel González <aglezabad@gmail.com>
* @version 0.0.1
*/
/**
* @class Loader
* @classdesc Class that loads and unloads dependencies in an application.
* @requires AWAF/ajax
* @requires caolan/async
* @requires pieroxy/lz-string
*/
export class Loader{
constructor(){
const LOAD_OK = 0;
const UNLOAD_OK = 0;
const LOAD_ERROR = 1;
const URL_NOT_DEFINED = 10;
const FILE_LOADED = 11;
const FILE_NOT_LOADED = 12;
const MISSING_CONTENT_DIV = 20;
const CANT_UNLOAD = 30;
}
/**
* Check if a browser interpreter has supported HTML5 imports.
* @since 0.0.1
* @returns {boolean} True -> Browser accepts HTML5 imports. False -> Browser can't use imports.
*/
static supportsHTML5Imports(){
return document.createElement('link')['import'] !== null;
}
/**
* Checks if a web application file (js/css/html) is already loaded.
* @since 0.0.1
* @param {string} url URL of file.
* @returns {boolean} True if file loaded. False if not.
*/
isLoaded(url){
if (url === undefined) {
return false;
}
let urlFragmented = url.split('.');
switch (urlFragmented[urlFragmented.length - 1]) {
case 'js':
return document.querySelector('script[src="' + url + '"]') !== null;
case 'css':
return document.querySelector('link[href="' + url + '"]') !== null;
case 'html':
return document.querySelector('div#' + LZString.compressToUTF16(url)) !== null;
}
return false;
}
/**
* Loads a file (JS/CSS/HTML).
* @since 0.0.1
* @param {string} url URL of file.
* @param {function} callback Function returned with a number value (status).
*/
load(url, callback){
if (url === undefined) {
return callback(this.URL_NOT_DEFINED);
}
if(this.isLoaded(url)){
return callback(this.FILE_LOADED);
}
let urlFragmented = url.split('.'),
element;
switch (urlFragmented[urlFragmented.length - 1]) {
case 'js':
element = document.createElement('script');
element.setAttribute('type', 'text/javascript');
element.setAttribute('src', url);
element.addEventListener('load', function () {
return callback(this.LOAD_OK);
});
element.addEventListener('error', function () {
return callback(this.LOAD_ERROR);
});
document.body.appendChild(element);
break;
case 'css':
element = document.createElement('link');
element.setAttribute('type', 'text/css');
element.setAttribute('rel', 'stylesheet');
element.setAttribute('href', url);
element.addEventListener('load', function () {
return callback(this.LOAD_OK);
});
element.addEventListener('error', function () {
return callback(this.LOAD_ERROR);
});
document.head.appendChild(element);
break;
case 'html':
let content = document.querySelector('div#content');
if (content === null) {
return callback(this.MISSING_CONTENT_DIV);
}
if (Loader.supportsHTML5Imports()) {
element = document.createElement('link');
element.setAttribute('rel','import');
element.setAttribute('href',url);
element.addEventListener('load', function () {
content = element['import'];
content.setAttribute('id',LZString.compressToUTF16(url));
return callback(this.LOAD_OK);
});
element.addEventListener('error', function () {
return callback(this.LOAD_ERROR);
});
document.head.appendChild(element);
} else {
ajax.get(url, function (response) {
element = document.createDocumentFragment();
element.appendChild(response);
content.appendChild(element);
content.setAttribute('id', LZString.compressToUTF16(url));
return callback(this.LOAD_OK);
}, function (status) {
return callback(status);
});
}
break;
}
}
/**
* Unloads a file (CSS/HTML).
* @since 0.0.1
* @param {string} url URL of file.
* @param {function} callback Function returned with a status value.
*/
unload(url, callback){
let element, clonedElement;
if (url === undefined) {
return callback(this.URL_NOT_DEFINED);
}
if (!this.isLoaded(url)) {
return callback(this.FILE_NOT_LOADED);
}
let urlFragmented = url.split('.');
switch(urlFragmented[urlFragmented.length - 1]) {
case 'js':
//Can't unload added Javascript files yet.
return callback(this.CANT_UNLOAD);
case 'css':
element = document.querySelector('link[href="' + url + '"]');
element.parentNode.removeChild(element);
return callback(this.UNLOAD_OK);
case 'html':
if (Loader.supportsHTML5Imports()) {
element = document.querySelector('link[href=' + url + ']');
element.parentNode.removeChild(element);
}
element = document.querySelector('div#' + LZString.compressToUTF16(url));
if (element === null) {
return callback(this.MISSING_CONTENT_DIV);
}
clonedElement = element.cloneNode(false);
element.parentNode.replaceChild(clonedElement, element);
element.setAttribute('id', 'content');
return callback(this.UNLOAD_OK);
}
}
}