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

Function for removing single stylesheets; Fixed single page bug #11

Merged
merged 4 commits into from
Jul 22, 2014
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 27 additions & 14 deletions angular-css-injector.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,19 @@
angular.module('angular.css.injector', [])
.provider('cssInjector', function() {
var singlePageMode = false;

function CssInjector($compile, $rootScope){
// Variables
var singlePageMode = false,
head = angular.element(document.getElementsByTagName('head')[0]),
scope;

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()
{
Expand All @@ -32,12 +31,12 @@ angular.module('angular.css.injector', [])
throw("angular.css.injector error : Please initialize your app in the HTML tag and be sure your page has a HEAD tag.");
}
};

// Used to add a CSS files in the head tag of the page
var addStylesheet = function(href)
{
_initScope();

if(scope.injectedStylesheets === undefined)
{
scope.injectedStylesheets = [];
Expand All @@ -51,29 +50,43 @@ angular.module('angular.css.injector', [])
return;
}
}

scope.injectedStylesheets.push({href: href});
};


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
};
}

this.$get = function($compile, $rootScope){
return new CssInjector($compile, $rootScope);
};

this.setSinglePageMode = function(mode){
singlePageMode = mode;
return this;
Expand Down