This repository has been archived by the owner on Oct 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 65
/
Copy pathangular-load.js
85 lines (70 loc) · 2.16 KB
/
angular-load.js
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
/* angular-load.js / v0.4.2 / (c) 2014, 2015, 2016 Uri Shaked / MIT Licence */
angular.module('angularLoad', [])
.service('angularLoad', ['$document', '$q', '$timeout', function ($document, $q, $timeout) {
var document = $document[0];
var promises = {};
function loader(createElement) {
return function(url) {
if (typeof promises[url] === 'undefined') {
var deferred = $q.defer();
var element = createElement(url);
element.onload = element.onreadystatechange = function (e) {
if (element.readyState && element.readyState !== 'complete' && element.readyState !== 'loaded') {
return;
}
$timeout(function () {
deferred.resolve(e);
});
};
element.onerror = function (e) {
$timeout(function () {
deferred.reject(e);
});
};
promises[url] = deferred.promise;
}
return promises[url];
};
}
/**
* Dynamically loads the given script
* @param src The url of the script to load dynamically
* @returns {*} Promise that will be resolved once the script has been loaded.
*/
this.loadScript = loader(function (src) {
var script = document.createElement('script');
script.src = src;
document.body.appendChild(script);
return script;
});
/**
* Dynamically loads the given CSS file
* @param href The url of the CSS to load dynamically
* @returns {*} Promise that will be resolved once the CSS file has been loaded.
*/
this.loadCSS = loader(function (href) {
var style = document.createElement('link');
style.rel = 'stylesheet';
style.type = 'text/css';
style.href = href;
document.head.appendChild(style);
return style;
});
/**
* Dynamically unloads the given CSS file
* @param href The url of the CSS to unload dynamically
* @returns boolean that will be true once the CSS file has been unloaded successfully or otherwise false.
*/
this.unloadCSS = function (href) {
delete promises[href];
var docHead = document.head;
if(docHead) {
var targetCss = docHead.querySelector('[href="' + href + '"]');
if(targetCss) {
targetCss.remove();
return true;
}
}
return false;
};
}]);