styleguide js codestyle

master
Sindre Sorhus 2014-06-20 18:03:41 +02:00
parent fd30043527
commit a612a7ee47
1 changed files with 23 additions and 14 deletions

View File

@ -1157,11 +1157,11 @@
var snippetToggle = document.querySelector('#snippet-toggle');
var snippets;
snippetToggle.addEventListener('click', function() {
if (snippets){
snippetToggle.addEventListener('click', function () {
if (snippets) {
for (var i = 0; i < snippets.length; i++) {
snippets[i].classList.toggle('auto-gen-code-visible');
};
}
}
});
@ -1172,6 +1172,7 @@
function createCodeSamples() {
var codeWrappers = document.querySelectorAll('.code-sample');
for (var i = 0; i < codeWrappers.length; i++) {
var codeWrapper = codeWrappers[i];
var clonedNodes = getClonedNonTextNodes(codeWrapper);
@ -1180,14 +1181,16 @@
var preElement = document.createElement('pre');
var codeElement = document.createElement('code');
for (var j = 0; j < clonedNodes.length; j++) {
codeElement.appendChild(document.createTextNode(clonedNodes[j].outerHTML));
if (j+1 < clonedNodes.length) {
if (j + 1 < clonedNodes.length) {
codeElement.appendChild(document.createTextNode('\n'));
}
}
preElement.appendChild(codeElement);
preElement.appendChild(codeElement);
preElement.classList.add('auto-gen-code-snippet');
preElement.classList.add('container');
@ -1195,11 +1198,13 @@
clearDiv.classList.add('clear');
var parent = codeWrapper.parentNode;
if (codeWrapper.nextSibling) {
parent.insertBefore(preElement, codeWrapper.nextSibling);
} else {
parent.appendChild(preElement);
}
parent.insertBefore(clearDiv, preElement);
}
}
@ -1207,14 +1212,17 @@
function getClonedNonTextNodes(element) {
var nonTextNodes = [];
var childNodes = element.childNodes;
for (var i = 0; i < childNodes.length; i++) {
var childElement = childNodes[i];
if (childElement.nodeType !== 3) {
// Found a valid child element
nonTextNodes.push(childElement.cloneNode(true));
}
}
return nonTextNodes;
return nonTextNodes;
}
function beautifyNodes(elements) {
@ -1225,29 +1233,30 @@
function beautifyNode(element, depth) {
var childNodes = element.childNodes;
var singleIndent = " ";
var currentDepthSpacing = "";
var singleIndent = ' ';
var currentDepthSpacing = '';
for (var i = 0; i < depth; i++) {
currentDepthSpacing += singleIndent;
}
var nextDepthSpacing = currentDepthSpacing+singleIndent;
var nextDepthSpacing = currentDepthSpacing + singleIndent;
var subChildren = [];
for (var i = 0; i < childNodes.length; i++) {
var childElement = childNodes[i];
if (childElement.nodeType === 3) {
// Found a text node
if (childElement.nodeValue.indexOf('\n') >= 0) {
if ((i+1) < childNodes.length) {
childElement.nodeValue = '\n'+nextDepthSpacing;
if (i + 1 < childNodes.length) {
childElement.nodeValue = '\n' + nextDepthSpacing;
} else {
childElement.nodeValue = '\n'+currentDepthSpacing;
childElement.nodeValue = '\n' + currentDepthSpacing;
}
}
} else {
beautifyNode(childElement, depth+1);
beautifyNode(childElement, depth + 1);
}
}
}