]>
Commit | Line | Data |
---|---|---|
0bf4aa26 XL |
1 | var ferrisTypes = [ |
2 | { | |
3 | attr: 'does_not_compile', | |
4 | title: 'This code does not compile!' | |
5 | }, | |
6 | { | |
7 | attr: 'panics', | |
8 | title: 'This code panics!' | |
9 | }, | |
10 | { | |
11 | attr: 'unsafe', | |
12 | title: 'This code block contains unsafe code.' | |
13 | }, | |
14 | { | |
15 | attr: 'not_desired_behavior', | |
16 | title: 'This code does not produce the desired behavior.' | |
17 | } | |
18 | ] | |
19 | ||
20 | document.addEventListener('DOMContentLoaded', () => { | |
21 | for (var ferrisType of ferrisTypes) { | |
22 | attachFerrises(ferrisType) | |
23 | } | |
24 | }) | |
25 | ||
26 | function attachFerrises (type) { | |
27 | var elements = document.getElementsByClassName(type.attr) | |
28 | ||
29 | for (var codeBlock of elements) { | |
30 | var lines = codeBlock.textContent.split(/\r|\r\n|\n/).length - 1; | |
31 | ||
32 | if (lines >= 4) { | |
33 | attachFerris(codeBlock, type) | |
34 | } | |
35 | } | |
36 | } | |
37 | ||
38 | function attachFerris (element, type) { | |
39 | var a = document.createElement('a') | |
40 | a.setAttribute('href', 'ch00-00-introduction.html#ferris') | |
41 | a.setAttribute('target', '_blank') | |
42 | ||
43 | var img = document.createElement('img') | |
44 | img.setAttribute('src', 'img/ferris/' + type.attr + '.svg') | |
45 | img.setAttribute('title', type.title) | |
46 | img.className = 'ferris' | |
47 | ||
48 | a.appendChild(img) | |
49 | ||
50 | element.parentElement.insertBefore(a, element) | |
51 | } |