From a16e605448f8d42f971cbfde3525ebc3d3b54512 Mon Sep 17 00:00:00 2001 From: Kitaiti Makoto Date: Tue, 29 Dec 2020 19:17:08 +0900 Subject: [PATCH 1/5] Ignore .netlify directory --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index e037a6d..1ca60ef 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,4 @@ build/ trans/ locale/ +.netlify/ -- 2.45.2 From b0aaa5da15cb50bfdd11568deeae75c1cc71f7ee Mon Sep 17 00:00:00 2001 From: Kitaiti Makoto Date: Tue, 29 Dec 2020 19:17:19 +0900 Subject: [PATCH 2/5] Ignore translate directory --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 1ca60ef..1749770 100644 --- a/.gitignore +++ b/.gitignore @@ -4,5 +4,6 @@ .sass-cache build/ trans/ +translate/ locale/ .netlify/ -- 2.45.2 From 77bb510e93d6ee2a5e22fbd6d07a8ab7fb78c8cb Mon Sep 17 00:00:00 2001 From: Kitaiti Makoto Date: Tue, 29 Dec 2020 19:23:31 +0900 Subject: [PATCH 3/5] Remove blank line --- source/javascripts/site.js | 1 - 1 file changed, 1 deletion(-) diff --git a/source/javascripts/site.js b/source/javascripts/site.js index cf2196a..00e1459 100644 --- a/source/javascripts/site.js +++ b/source/javascripts/site.js @@ -16,7 +16,6 @@ document.addEventListener('DOMContentLoaded', () => { } else { control.setAttribute('aria-expanded', 'true'); popup.setAttribute('aria-hidden', 'false'); - } }); } -- 2.45.2 From c4ebde8951882af02ceb92476612161f81efd75d Mon Sep 17 00:00:00 2001 From: Kitaiti Makoto Date: Tue, 29 Dec 2020 19:40:22 +0900 Subject: [PATCH 4/5] Refactor language switcher using class --- source/javascripts/site.js | 47 +++++++++++++++++++++++++++----------- 1 file changed, 34 insertions(+), 13 deletions(-) diff --git a/source/javascripts/site.js b/source/javascripts/site.js index 00e1459..d209995 100644 --- a/source/javascripts/site.js +++ b/source/javascripts/site.js @@ -3,20 +3,41 @@ document.getElementById('menu').addEventListener('click', evt => ) document.addEventListener('DOMContentLoaded', () => { - for (const switcher of document.getElementsByClassName('language-switcher')) { - const control = switcher.querySelector('[aria-haspopup]'); - control.addEventListener('click', event => { - const popupId = control.getAttribute('aria-controls'); - if (! popupId) return; - const popup = document.getElementById(popupId); - if (! popup) return; - if (control.getAttribute('aria-expanded') === 'true') { - control.setAttribute('aria-expanded', 'false'); - popup.setAttribute('aria-hidden', 'true'); + class LanguageSwitcher { + constructor(element) { + this.element = element; + this.control = this.element.querySelector('[aria-haspopup]'); + const popupId = this.control.getAttribute('aria-controls'); + this.popup = document.getElementById(popupId); + this.control.addEventListener('click', _ => {this.toggle();}); + } + + get expanded() { + return this.control.getAttribute('aria-expanded') === 'true'; + } + + toggle() { + if (this.expanded) { + this.collapse(); } else { - control.setAttribute('aria-expanded', 'true'); - popup.setAttribute('aria-hidden', 'false'); + this.expand(); } - }); + } + + expand() { + if (this.expanded) return; + this.control.setAttribute('aria-expanded', 'true'); + this.popup.setAttribute('aria-hidden', 'false'); + } + + collapse() { + if (! this.expanded) return; + this.control.setAttribute('aria-expanded', 'false'); + this.popup.setAttribute('aria-hidden', 'true'); + } + } + + for (const switcher of document.getElementsByClassName('language-switcher')) { + new LanguageSwitcher(switcher); } }); -- 2.45.2 From c20219c3e042451b76c27c2c3bc566eefee6b5f1 Mon Sep 17 00:00:00 2001 From: Kitaiti Makoto Date: Tue, 29 Dec 2020 19:59:09 +0900 Subject: [PATCH 5/5] Collapse language switcher when some point in page are clicked --- source/javascripts/site.js | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/source/javascripts/site.js b/source/javascripts/site.js index d209995..e507c06 100644 --- a/source/javascripts/site.js +++ b/source/javascripts/site.js @@ -37,7 +37,17 @@ document.addEventListener('DOMContentLoaded', () => { } } + const languageSwitchers = []; for (const switcher of document.getElementsByClassName('language-switcher')) { - new LanguageSwitcher(switcher); + languageSwitchers.push(new LanguageSwitcher(switcher)); } + + document.body.addEventListener('click', event => { + if (languageSwitchers.some(ls => ls.element.contains(event.target))) { + return; + } + for (const ls of languageSwitchers) { + ls.collapse(); + } + }); }); -- 2.45.2