Skip to content

Commit

Permalink
Shrink and move ferris when possible
Browse files Browse the repository at this point in the history
  • Loading branch information
serverwentdown committed Aug 15, 2021
1 parent d20900e commit d34ea05
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 11 deletions.
17 changes: 14 additions & 3 deletions ferris.css
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,24 @@ body.ayu .not_desired_behavior {
background: #501f21;
}

.ferris {
.ferris-container {
position: absolute;
z-index: 99;
right: 5px;
top: 30px;
width: 10%;
height: auto;
}

.ferris {
vertical-align: top;
margin-left: 0.5em;
}

.ferris-large {
width: 4.5em;
}

.ferris-small {
width: 3em;
}

.ferris-explain {
Expand Down
34 changes: 26 additions & 8 deletions ferris.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,29 +19,47 @@ document.addEventListener('DOMContentLoaded', () => {
}
})

function attachFerrises (type) {
function attachFerrises(type) {
var elements = document.getElementsByClassName(type.attr)

for (var codeBlock of elements) {
var lines = codeBlock.textContent.split(/\r|\r\n|\n/).length - 1;

if (lines >= 4) {
attachFerris(codeBlock, type)
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 attachFerris (element, type) {
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.className = 'ferris'
img.classList.add('ferris')
img.classList.add('ferris-' + size)

a.appendChild(img)

element.parentElement.insertBefore(a, element)
return a
}

0 comments on commit d34ea05

Please sign in to comment.