-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtalk.js
128 lines (114 loc) · 3.28 KB
/
talk.js
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
const examples = document.body.querySelectorAll('.example');
for (example of examples) {
if (example) {
example.innerHTML += `
<nav>
<button name="run">Run</button>
</nav>
`;
boring(example);
}
}
const articles = document.body.querySelectorAll('article');
for (article of articles) {
article.innerHTML += `
<nav>
<button name="first">First</button>
<button name="previous">Previous</button>
<button name="next">Next</button>
<button name="last">Last</button>
<button name="firm">ApostropheCMS</button>
</nav>
`;
}
function boring(el) {
const boring = el.querySelector('.boring');
if (boring) {
el.querySelector('nav').innerHTML += `
<button name="more">More</button>
<button name="less">Less</button>
`;
}
}
document.body.addEventListener('click', (e) => {
const article = e.target.closest('article');
const name = e.target.getAttribute('name');
if (name === 'run') {
const example = e.target.closest('.example');
let display = example.querySelector('.output');
if (display) {
example.removeChild(display);
}
display = document.createElement('pre');
display.classList.add('output');
example.appendChild(display);
let code = '';
const boring = example.querySelector('pre.boring');
if (boring) {
code += boring.childNodes[0].nodeValue;
}
code += example.querySelector('pre:not(.boring)').childNodes[0].nodeValue;
const wrappedCode = `
(function() {
const realConsole = console;
{
const console = {
log(...args) {
const output = args.map(arg => {
if ((typeof arg) === 'object') {
return JSON.stringify(arg, null, ' ');
} else {
return arg;
}
}).join('\\r\\n');
display.innerText += output + '\\r\\n';
}
};
${code}
}
})();
`;
try {
eval(wrappedCode);
} catch (e) {
display.innerText += e.toString();
}
}
if (name === 'first') {
article.classList.remove('active');
article.classList.add('inactive');
const next = document.querySelector('article:first-of-type');
next.classList.remove('inactive');
next.classList.add('active');
}
if (name === 'previous') {
article.classList.remove('active');
article.classList.add('inactive');
const previous = article.previousElementSibling;
previous.classList.remove('inactive');
previous.classList.add('active');
}
if (name === 'next') {
article.classList.remove('active');
article.classList.add('inactive');
const next = article.nextElementSibling;
next.classList.remove('inactive');
next.classList.add('active');
}
if (name === 'last') {
article.classList.remove('active');
article.classList.add('inactive');
const next = document.querySelector('article:last-of-type');
next.classList.remove('inactive');
next.classList.add('active');
}
if (name === 'more') {
event.target.closest('.example').classList.add('more');
}
if (name === 'less') {
event.target.closest('.example').classList.remove('more');
}
if (name === 'firm') {
document.location = 'apostrophe.html';
}
});