hscc

Soure code of <https://hugo.soucy.cc>.
git clone git://soucy.cc/hscc.git
Log | Files | Refs

commit 7cb8e533338f0f2f727f1886baf27c67c0ed1df3
parent 36b7e785bffebe92d7925f667858a6234e83eeb6
Author: Hugo Soucy <hugo@soucy.cc>
Date:   Fri, 25 Feb 2022 21:01:03 -0500

Remove global JS and go modular

Diffstat:
Asrc/js/archives.js | 41+++++++++++++++++++++++++++++++++++++++++
Dsrc/js/index.js | 41-----------------------------------------
Msrc/js/js.sh | 16+++++++++++-----
3 files changed, 52 insertions(+), 46 deletions(-)

diff --git a/src/js/archives.js b/src/js/archives.js @@ -0,0 +1,41 @@ +(function () { + 'use strict'; + /* Archives Page */ + var archives = document.querySelector('[data-js-archives="list"]'); + + if (archives) { + var archives_headers = [].slice.call(archives.querySelectorAll('[data-js-archives="header"]')); + + archives_headers.forEach(function(header) { + var year = header.querySelector('[data-js-archives="year"]'); + var unfold = "tout déplier"; + var fold = "tout replier"; + + var checkbox = document.createElement('input'); + var label = document.createElement('label'); + + checkbox.id = year.id + '_checkbox'; + checkbox.setAttribute('type', 'checkbox'); + checkbox.addEventListener('change', function(evt) { + var details = [].slice.call(header.parentNode.querySelectorAll('details')); + + for (var i = 0; i < details.length; i++) { + var checked = evt.currentTarget.checked; + + details[i].open = checked; + + if (checked) { + evt.currentTarget.nextElementSibling.innerHTML = fold; + } else { + evt.currentTarget.nextElementSibling.innerHTML = unfold; + } + } + }); + + label.innerHTML = '<span>tout déplier</span>'; + label.insertAdjacentElement('afterbegin', checkbox); + + header.insertAdjacentElement('beforeend', label); + }); + } +})(); diff --git a/src/js/index.js b/src/js/index.js @@ -1,41 +0,0 @@ -(function () { - 'use strict'; - - /* Archives Page */ - const archives = document.querySelector('[data-js-archives="list"]'); - - if (archives) { - const archives_headers = [].slice.call(archives.querySelectorAll('[data-js-archives="header"]')); - - archives_headers.forEach(function(header) { - const year = header.querySelector('[data-js-archives="year"]'); - const unfold = "tout déplier"; - const fold = "tout replier"; - - let checkbox = document.createElement('input'); - let label = document.createElement('label'); - - checkbox.id = year.id + '_checkbox'; - checkbox.setAttribute('type', 'checkbox'); - checkbox.addEventListener('change', function(evt) { - [].slice.call(header.parentNode.querySelectorAll('details')) - .forEach(function(details) { - let checked = evt.currentTarget.checked; - - details.open = checked; - - if (checked) { - evt.currentTarget.nextElementSibling.innerHTML = fold; - } else { - evt.currentTarget.nextElementSibling.innerHTML = unfold; - } - }); - }); - - label.innerHTML = '<span>tout déplier</span>'; - label.insertAdjacentElement('afterbegin', checkbox); - - header.insertAdjacentElement('beforeend', label); - }); - } -})(); diff --git a/src/js/js.sh b/src/js/js.sh @@ -1,14 +1,20 @@ #!/bin/sh + # You can use it with entr to rebuild on change: # find . | entr -d ./js.sh +# Dependencies: +# - jsmin (clang) + dst="../../public_html/js/" -while read line; do - [ ! -z "$line" ] && cat "./$line" -done <<-EOF > "${dst}index.min.js" +# while read -r line; do +# [ -n "$line" ] && cat "./$line" +# done <<-EOF > "${dst}index.min.js" +# index.js + +# EOF -index.js -EOF +jsmin < archives.js > "${dst}archives.min.js" echo "JS builded! @ $(date +"%Y-%m-%d %T")"