forked from winstromming/sassdown
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscripts.js
More file actions
61 lines (54 loc) · 2.53 KB
/
Copy pathscripts.js
File metadata and controls
61 lines (54 loc) · 2.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
(function (d) {
// Helper functions
function query (str, context) { return (context || d).querySelector(str); }
function queryAll (str, context) { return Array.prototype.slice.call((context || d).querySelectorAll(str)); }
function generateId () { return (Math.random()+1).toString(36).substring(2); }
// Insert the results
var resultTemplate = query('.result-template').innerHTML;
while(resultTemplate.indexOf('\\/script') !== -1){
// .replace seems to only replace 1 occurrence of '\/script' at a time, even if using regex with the 'g' flag
resultTemplate = resultTemplate.replace('\\/script', '/script');
}
queryAll('.result-placeholder').forEach(function (placeholder) {
var parent = placeholder.parentNode;
var content = resultTemplate
.replace('[[body]]', placeholder.innerHTML)
.replace('[[onload]]', 'parent.adjustIframeHeight(window.frameElement.id)');
var iframe = d.createElement('iframe');
parent.appendChild(iframe);
parent.removeChild(placeholder);
iframe.setAttribute('id', generateId());
iframe.setAttribute('height', 0);
var doc = iframe.contentWindow.document;
doc.open();
doc.write(content);
doc.close();
});
// Toggling navigation folders
queryAll('#nav .heading').forEach(function (heading) {
// Find the link to the current page and mark it and its parent folder as active/open
var list = heading.nextElementSibling;
queryAll('a', list).some(function (link) {
var href = link.getAttribute('href').replace(/^[\.\/]*/, '');
if (window.location.pathname.match(new RegExp(href + '$'))) {
link.classList.add('is-active');
link.parentNode.parentNode.classList.add('is-open');
link.parentNode.parentNode.previousElementSibling.classList.add('is-open');
return true;
}
});
// Toggle a folder open/closed when its heading is clicked
heading.addEventListener('click', function () {
this.classList.toggle('is-open');
this.nextElementSibling.classList.toggle('is-open');
});
});
}(document));
// Resizes the given iframe to the height of the content.
// Must be placed here, will be called from inside the iframe.
function adjustIframeHeight (id) {
setTimeout(function () {
var iframe = document.getElementById(id);
iframe.height = iframe.contentWindow.document.body.scrollHeight;
}, 100);
}