trpl-zh-cn/ferris.js
2022-07-19 06:57:15 +08:00

65 lines
1.5 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

var ferrisTypes = [
{
attr: 'does_not_compile',
title: '这段代码无法通过编译!'
},
{
attr: 'panics',
title: '这段代码会 Panic'
},
{
attr: 'not_desired_behavior',
title: '这段代码的运行结果不符合预期。'
}
]
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 < 4) {
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 img = document.createElement('img')
img.setAttribute('src', 'img/ferris/' + type.attr + '.svg')
img.setAttribute('title', type.title)
img.classList.add('ferris')
img.classList.add('ferris-' + size)
a.appendChild(img)
return a
}