From ac70c66901b8f592a2ee214a69c119a2f20b1aa5 Mon Sep 17 00:00:00 2001 From: Martin Mouterde Date: Wed, 28 Jan 2015 10:54:22 +0100 Subject: [PATCH] Add media attribute to link tag Add an facultative argument 'media' to addStylesheet function which is used to make a link specific to the given media. Media attribute may be provided : 'screen' (by default) could be changed by 'print'. A given href could be added for several media. --- angular-css-injector.js | 49 +++++++++++++++++++---------------------- 1 file changed, 23 insertions(+), 26 deletions(-) diff --git a/angular-css-injector.js b/angular-css-injector.js index 46e954a..97618bb 100644 --- a/angular-css-injector.js +++ b/angular-css-injector.js @@ -32,39 +32,36 @@ angular.module('angular.css.injector', []) }; // Used to add a CSS files in the head tag of the page - var addStylesheet = function(href) - { - _initScope(); + var addStylesheet = function (href, media){ + _initScope(); - if(scope.injectedStylesheets === undefined) - { - scope.injectedStylesheets = []; - head.append($compile("")(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; + media = media || 'screen'; + + if (scope.injectedStylesheets === undefined){ + scope.injectedStylesheets = []; + head.append($compile("")(scope)); // Found here : http://stackoverflow.com/a/11913182/1662766 + } + else{ + for (var i in scope.injectedStylesheets){ + if (scope.injectedStylesheets[i].href == href && scope.injectedStylesheets[i].media == media) // An url can't be added more than once for given media. I use a loop FOR, not the function indexOf to make the code IE < 9 compatible + return; + } } - } - scope.injectedStylesheets.push({href: href}); + scope.injectedStylesheets.push({href: href, media: media}); }; - 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; - } + 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()