This repository has been archived by the owner on Feb 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 90
/
angular-css-injector.js
100 lines (86 loc) · 3.14 KB
/
angular-css-injector.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
'use strict';
/*
* angular-css-injector v1.0.4
* Written by Gabriel Delépine
* Special thanks to (github users) : @kleiinnn
* License: MIT
* https://github.com/Yappli/angular-css-injector/
*/
angular.module('angular.css.injector', [])
.provider('cssInjector', ['$interpolateProvider', function($interpolateProvider) {
var singlePageMode = false;
function CssInjector($compile, $rootScope){
// Variables
var head = angular.element(document.getElementsByTagName('head')[0]),
scope;
// Capture the event `locationChangeStart` when the url change. If singlePageMode===TRUE, call the function `removeAll`
$rootScope.$on('$locationChangeStart', function()
{
if(singlePageMode === true)
removeAll();
});
// Always called by the functions `addStylesheet` and `removeAll` to initialize the variable `scope`
var _initScope = function()
{
if(scope === undefined)
{
scope = $rootScope.$new(true);
}
};
// Used to add a CSS files in the head tag of the page
var addStylesheet = function(href)
{
_initScope();
if(scope.injectedStylesheets === undefined)
{
scope.injectedStylesheets = [];
head.append($compile("<link data-ng-repeat='stylesheet in injectedStylesheets' data-ng-href='" + $interpolateProvider.startSymbol() + "stylesheet.href" + $interpolateProvider.endSymbol() + "' rel='stylesheet' />")(scope)); // Found here : http://stackoverflow.com/a/11913182/1662766
}
else
{
for(var i in scope.injectedStylesheets)
{
if(scope.injectedStylesheets[i].href == href) // An url can't be added more than once. I use a loop FOR, not the function indexOf to make the code IE < 9 compatible
return;
}
}
scope.injectedStylesheets.push({href: href});
};
// gets all stylesheets
var getStyleSheets = function() {
_initScope();
return scope.injectedStylesheets === undefined ? [] : scope.injectedStylesheets;
}
var remove = function(href){
_initScope();
if(scope.injectedStylesheets){
for(var i = 0; i < scope.injectedStylesheets.length; i++){
if(scope.injectedStylesheets[i].href === href){
scope.injectedStylesheets.splice(i, 1);
return;
}
}
}
};
// Used to remove all of the CSS files added with the function `addStylesheet`
var removeAll = function()
{
_initScope();
if(scope.injectedStylesheets !== undefined)
scope.injectedStylesheets = []; // Make it empty
};
return {
add: addStylesheet,
remove: remove,
removeAll: removeAll,
getAll: getStyleSheets
};
}
this.$get = ['$compile', '$rootScope', function($compile, $rootScope){
return new CssInjector($compile, $rootScope);
}];
this.setSinglePageMode = function(mode){
singlePageMode = mode;
return this;
}
}]);