|
1 | 1 | /*
|
2 |
| -* angularDynamicStylesheets v0.1.0 |
| 2 | +* angularDynamicStylesheets v0.2.0 |
3 | 3 | * 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/ |
10 | 6 | */
|
11 | 7 | angular.module('DynamicStylesheets', [])
|
12 | 8 | .service('dynamicStylesheets', [
|
13 | 9 | '$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 |
18 | 33 | var addStylesheet = function(href)
|
19 | 34 | {
|
20 |
| - if(scope.stylesheets_service_dynamicStylesheets === undefined) |
| 35 | + _initScope(); |
| 36 | + |
| 37 | + if(scope.href_array_dynamicStylesheets === undefined) |
21 | 38 | {
|
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 |
24 | 41 | }
|
25 | 42 | else
|
26 | 43 | {
|
27 |
| - for(var i in scope.stylesheets_service_dynamicStylesheets) |
| 44 | + for(var i in scope.href_array_dynamicStylesheets) |
28 | 45 | {
|
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 |
30 | 47 | return;
|
31 | 48 | }
|
32 | 49 | }
|
33 | 50 |
|
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; |
35 | 70 | };
|
36 | 71 |
|
37 | 72 | return {
|
38 | 73 | add: addStylesheet,
|
| 74 | + removeAll: removeAll, |
| 75 | + setSinglePageMode: setSinglePageMode, |
39 | 76 | };
|
40 | 77 | }
|
41 | 78 | ]);
|
0 commit comments