forked from Together-Java/ModernJava
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ferris.js
78 lines (65 loc) · 1.93 KB
/
ferris.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
var ferrisTypes = [
{
attr: 'does_not_compile',
title: 'This code does not compile!'
},
{
attr: 'panics',
title: 'This code will crash!'
},
{
attr: 'not_desired_behavior',
title: 'This code does not produce the desired behavior.'
}
]
document.addEventListener('DOMContentLoaded', () => {
for (var ferrisType of ferrisTypes) {
attachFerrises(ferrisType)
}
})
function attachFerrises(type) {
var elements = document.getElementsByClassName(type.attr)
for (var codeBlock of elements) {
var lines = codeBlock.innerText.replace(/\n$/, '').split(/\n/).length
var size = 'large'
if (lines < 5) {
size = 'small'
}
var container = prepareFerrisContainer(codeBlock, size == 'small')
container.appendChild(createFerris(type, size))
}
}
function prepareFerrisContainer(element, useButtons) {
var foundButtons = element.parentElement.querySelector('.buttons')
if (useButtons && foundButtons) {
return foundButtons
}
var div = document.createElement('div')
div.classList.add('ferris-container')
element.parentElement.insertBefore(div, element)
return div
}
function createFerris(type, size) {
var a = document.createElement('a')
// a.setAttribute('href', 'ch00-00-introduction.html#ferris')
a.setAttribute('target', '_blank')
var toFileName = function (t) {
// return '../img/' + t.attr + '.svg';
if (t.attr == 'panics') {
return '../img/duke_armcross.webp'
}
else if (t.attr == 'does_not_compile') {
return '../img/duke_computer.webp'
}
else if (t.attr == 'not_desired_behavior') {
return '../img/duke_finger.webp'
}
}
var img = document.createElement('img')
img.setAttribute('src', toFileName(type))
img.setAttribute('title', type.title)
img.classList.add('ferris')
img.classList.add('ferris-' + size)
a.appendChild(img)
return a
}