Skip to content
This repository was archived by the owner on Feb 8, 2023. It is now read-only.

Commit 16c75d8

Browse files
Gabriel DelépineGabriel Delépine
Gabriel Delépine
authored and
Gabriel Delépine
committed
Added CSS files can now be removed
1 parent 0a9d09c commit 16c75d8

File tree

1 file changed

+54
-17
lines changed

1 file changed

+54
-17
lines changed

angularDynamicStylesheets.js

Lines changed: 54 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,78 @@
11
/*
2-
* angularDynamicStylesheets v0.1.0
2+
* angularDynamicStylesheets v0.2.0
33
* Copyleft 2013 Yappli
4-
*
5-
* This program is free software. It comes without any warranty, to
6-
* the extent permitted by applicable law. You can redistribute it
7-
* and/or modify it under the terms of the Do What The Fuck You Want
8-
* To Public License, Version 2, as published by Sam Hocevar. See
9-
* http://sam.zoy.org/wtfpl/COPYING for more details.
4+
* License: MIT
5+
* https://github.com/Yappli/angularDynamicStylesheets/
106
*/
117
angular.module('DynamicStylesheets', [])
128
.service('dynamicStylesheets', [
139
'$compile',
14-
function($compile) {
15-
16-
var scope = angular.element('head').scope();
17-
10+
'$rootScope',
11+
function($compile, $rootScope)
12+
{
13+
// Variables
14+
var scope;
15+
var singlePageMode = false;
16+
17+
// Capture the event `locationChangeStart` when the url change. If singlePageMode===TRUE, call the function `removeAll`
18+
$rootScope.$on('$locationChangeStart', function()
19+
{
20+
console.log('locationChangeStart in dynamicStylesheets');
21+
if(singlePageMode === true)
22+
removeAll();
23+
});
24+
25+
// Always called by the functions `addStylesheet` and `removeAll` to initialize the variable `scope`
26+
var _initScope = function()
27+
{
28+
if(scope === undefined)
29+
scope = angular.element('head').scope();
30+
};
31+
32+
// Used to add a CSS files in the head tag of the page
1833
var addStylesheet = function(href)
1934
{
20-
if(scope.stylesheets_service_dynamicStylesheets === undefined)
35+
_initScope();
36+
37+
if(scope.href_array_dynamicStylesheets === undefined)
2138
{
22-
angular.element('head').scope().stylesheets_service_dynamicStylesheets = [];
23-
angular.element('head').append($compile("<link data-ng-repeat='stylesheet in stylesheets_service_dynamicStylesheets' data-ng-href='{{stylesheet.href}}' rel='stylesheet' />")(scope)); // Found here : http://stackoverflow.com/a/11913182/1662766
39+
scope.href_array_dynamicStylesheets = [];
40+
angular.element('head').append($compile("<link data-ng-repeat='stylesheet in href_array_dynamicStylesheets' data-ng-href='{{stylesheet.href}}' rel='stylesheet' />")(scope)); // Found here : http://stackoverflow.com/a/11913182/1662766
2441
}
2542
else
2643
{
27-
for(var i in scope.stylesheets_service_dynamicStylesheets)
44+
for(var i in scope.href_array_dynamicStylesheets)
2845
{
29-
if(scope.stylesheets_service_dynamicStylesheets[i].href == href) // An url can't be added more than once
46+
if(scope.href_array_dynamicStylesheets[i].href == href) // An url can't be added more than once. I use a loop FOR, not the function indexOf to make IE < 9 compatible
3047
return;
3148
}
3249
}
3350

34-
scope.stylesheets_service_dynamicStylesheets.push({href: href});
51+
scope.href_array_dynamicStylesheets.push({href: href});
52+
};
53+
54+
// Used to remove all of the CSS files added with the function `addStylesheet`
55+
var removeAll = function()
56+
{
57+
_initScope();
58+
59+
if(scope.href_array_dynamicStylesheets !== undefined)
60+
scope.href_array_dynamicStylesheets = []; // Make it empty
61+
};
62+
63+
// Used to set the boolean `singlePageMode`. If singlePageMode===TRUE, the function `removeAll` will be call every time the page change (based on the angular event `$locationChangeStart`)
64+
var setSinglePageMode = function(bool)
65+
{
66+
if(bool !== true && bool !== false)
67+
throw("Angular service `dynamicStylesheets` : function `setSinglePageMode` : Error parameter, boolean required.");
68+
69+
singlePageMode = bool;
3570
};
3671

3772
return {
3873
add: addStylesheet,
74+
removeAll: removeAll,
75+
setSinglePageMode: setSinglePageMode,
3976
};
4077
}
4178
]);

0 commit comments

Comments
 (0)