Last active
August 29, 2015 14:06
-
-
Save sheodox/4d00d411340704c80add to your computer and use it in GitHub Desktop.
BatchStyler
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;(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