Last active
December 26, 2015 13:29
-
-
Save paceaux/7158433 to your computer and use it in GitHub Desktop.
Stylesheet toggler. Uses the CSSOM (Cascading StyleSheet Object Model). Drop this somewhere on your page and it'll find your stylesheets and let you turn them on and off.
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
window.cssom = { | |
init: function () { | |
var _this = window.cssom; | |
_this.functions.bootstrapTable(); | |
}, | |
data: { | |
table: {}, | |
rootClass: "CSSOM", | |
tableClass: "table", | |
rowButton: { | |
text: { | |
off: "Disabled", | |
on: "Enabled" | |
}, | |
class: "stylesheet__toggle" | |
}, | |
tableHead: { | |
text: { | |
cell1: "Stylesheet", | |
cell2: "Title", | |
cell3: "State" | |
}, | |
class: "table__thead" | |
}, | |
tableButton: { | |
text: { | |
on: "Hide Stylesheets", | |
off: "Show stylesheets" | |
}, | |
buttonClass: "cssom__toggle" | |
}, | |
tableCaption: "Toggle Stylesheets" | |
}, | |
events: { | |
buttonToggle: function (e) { | |
var _this = window.cssom, | |
data = _this.data, | |
rowButton = data.rowButton, | |
el = e.target, | |
state = el.getAttribute('data-state'), | |
sheetIndex = el.getAttribute('data-stylesheet'); | |
if (state === "enabled") { | |
el.setAttribute("data-state", "disabled"); | |
el.innerHTML = rowButton.text.off; | |
_this.functions.toggleStyleSheet(sheetIndex); | |
} else { | |
el.setAttribute("data-state", "enabled"); | |
el.innerHTML = rowButton.text.on; | |
_this.functions.toggleStyleSheet(sheetIndex); | |
} | |
}, | |
hideTable: function (e){ | |
var _this = window.cssom, | |
wrapper = _this.data.rootClass; | |
document.getElementsByClassName(wrapper)[0].className = _this.data.rootClass + " collapsed"; | |
}, | |
showTable: function (e){ | |
var _this = window.cssom, | |
wrapper = _this.data.rootClass; | |
document.getElementsByClassName(wrapper)[0].className = _this.data.rootClass + " expanded" | |
}, | |
tableToggle: function(e) { | |
var _this = window.cssom, | |
data = _this.data, | |
wrapper = document.getElementsByClassName(data.rootClass)[0], | |
button = e.target; | |
if (wrapper.classList[1]) { | |
wrapper.classList.remove("collapsed"); | |
button.innerText = data.tableButton.text.on; | |
} else { | |
wrapper.classList.add("collapsed"); | |
button.innerText = data.tableButton.text.off; | |
} | |
}, | |
}, | |
helpers: { | |
setupTableWrapper: function () { | |
var _this = window.cssom, | |
wrapper = document.createElement('div'); | |
wrapper.className = _this.data.rootClass; | |
_this.data.tableWrapper = wrapper; | |
}, | |
setupTableToggle: function () { | |
var _this = window.cssom, | |
data = _this.data, | |
toggleText = document.createTextNode(data.tableButton.text.on), | |
toggle = document.createElement('button'); | |
toggle.className = _this.data.tableButton.buttonClass; | |
toggle.appendChild(toggleText); | |
toggle.addEventListener('click',_this.events.tableToggle); | |
_this.data.tableWrapper.appendChild(toggle); | |
}, | |
setupTableHead: function () { | |
var _this = window.cssom, | |
data = _this.data, | |
tHead = data.table.createTHead(), | |
tHeadRow = tHead.insertRow(), | |
cell1 = tHeadRow.insertCell(0), | |
cell2 = tHeadRow.insertCell(1), | |
cell3 = tHeadRow.insertCell(2); | |
tHead.className = data.tableHead.class; | |
cell1.innerText = data.tableHead.text.cell1; | |
cell1.id = data.tableHead.text.cell1; | |
cell2.innerText = data.tableHead.text.cell2; | |
cell2.id = data.tableHead.text.cell2; | |
cell3.innerText = data.tableHead.text.cell3; | |
cell3.id = data.tableHead.text.cell3; | |
}, | |
setupTableCaption: function () { | |
var _this = window.cssom, | |
data = _this.data, | |
table = data.table, | |
caption = table.createCaption(); | |
caption.innerText = _this.data.tableCaption; | |
}, | |
setupTable: function () { | |
var _this = window.cssom, | |
table = document.createElement('table'), | |
tBody = document.createElement('tbody'); | |
table.appendChild(tBody); | |
table.className = _this.data.rootClass + "__" + _this.data.tableClass; | |
_this.data.table = table; | |
}, | |
addTableToWrap: function () { | |
var _this = window.cssom; | |
_this.data.tableWrapper.appendChild(_this.data.table); | |
document.body.appendChild(_this.data.tableWrapper); | |
}, | |
addRowData: function (row, styleSheet) { | |
var _this = window.cssom, | |
h = row.insertCell(0), | |
t = row.insertCell(1), | |
d = row.insertCell(2); | |
if (styleSheet !== undefined) { | |
var ref = styleSheet.href !== undefined ? styleSheet.href : "nada", | |
title = styleSheet.title !== undefined ? styleSheet.title : "", | |
disabled = styleSheet.disabled !== undefined ? styleSheet.disabled : false, | |
sheetIndex = row.getAttribute('data-index'); | |
ref = document.createTextNode(ref); | |
title = document.createTextNode(title); | |
disabled = _this.functions.createButton(sheetIndex); | |
h.appendChild(ref); | |
t.appendChild(title); | |
d.appendChild(disabled); | |
} | |
}, | |
setupRows: function () { | |
var _this = window.cssom, | |
table = _this.data.table, | |
styleSheets = document.styleSheets, | |
i = 0; | |
for (var styleSheet in styleSheets) { | |
if (styleSheet !== undefined) { | |
var row = table.insertRow(i), | |
sheet = styleSheets[i]; | |
row.setAttribute('data-index', i); | |
_this.helpers.addRowData(row, sheet); | |
i++; | |
} | |
} | |
} | |
}, | |
functions: { | |
bootstrapTable: function (){ | |
var _this = window.cssom; | |
_this.helpers.setupTableWrapper(); | |
_this.helpers.setupTableToggle(); | |
_this.helpers.setupTable(); | |
_this.helpers.setupRows(); | |
_this.helpers.setupTableCaption(); | |
_this.helpers.setupTableHead(); | |
_this.helpers.addTableToWrap(); | |
}, | |
createButton: function (sheetIndex){ | |
var _this = window.cssom, | |
rowButton = _this.data.rowButton, | |
b = document.createElement('button'); | |
b.innerHTML = rowButton.text.on; | |
b.addEventListener('click', _this.events.buttonToggle); | |
b.setAttribute("data-state", "enabled"); | |
b.setAttribute('data-stylesheet', sheetIndex) | |
return b; | |
}, | |
toggleStyleSheet: function (sheetIndex) { | |
var styleSheet = document.styleSheets[sheetIndex]; | |
if (styleSheet.disabled === true) { | |
styleSheet.disabled = false; | |
} else { | |
styleSheet.disabled = true; | |
} | |
} | |
} | |
}; | |
window.cssom.init(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment