Skip to content

Instantly share code, notes, and snippets.

@sheodox
Last active August 29, 2015 14:06
Show Gist options
  • Save sheodox/4d00d411340704c80add to your computer and use it in GitHub Desktop.
Save sheodox/4d00d411340704c80add to your computer and use it in GitHub Desktop.
BatchStyler
;(function(){
'use strict';
if (!window.createBatchStyler) {
window.createBatchStyler = createBatchStyler;
}
function createBatchStyler() {
var styles = {};
function createStyleText(styles) {
var styleText = '',
i;
for (i = 0; i < styles.length; i++) {
styleText += styles[i].selector + '{' + styles[i].body + '}';
}
return styleText;
}
function create(name, newStyles) {
var tag = document.createElement('style');
tag.textContent = createStyleText(newStyles);
styles[name] = {tag: tag};
enable(name);
}
function addTo(id, newStyles) {
var style = styles[id];
style.tag.text = style.tag.text + createStyleText(newStyles);
}
function toggle(id) {
styles[id].enabled ? disable(id) : enable(id);
}
function enable(id) {
var style = styles[id];
document.head.appendChild(style.tag);
style.enabled = true;
}
function disable(id) {
var style = styles[id];
document.head.removeChild(style.tag);
style.enabled = false;
}
return {create: create, addTo: addTo, toggle: toggle, enable: enable, disable: disable};
}
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment