diff --git a/public/js/reader.js b/public/js/reader.js index 11c0364..b681e26 100644 --- a/public/js/reader.js +++ b/public/js/reader.js @@ -60,6 +60,7 @@ $('#page-select').change(function(){ function showControl(idx) { $('#page-select').val(idx); UIkit.modal($('#modal-sections')).show(); + styleModal(); } function jumpTo(page) { var ary = window.location.pathname.split('/'); diff --git a/public/js/theme.js b/public/js/theme.js new file mode 100644 index 0000000..b23dff1 --- /dev/null +++ b/public/js/theme.js @@ -0,0 +1,76 @@ +const getTheme = () => { + var theme = localStorage.getItem('theme'); + if (!theme) theme = 'light'; + return theme; +}; + +const saveTheme = theme => { + localStorage.setItem('theme', theme); +}; + +const toggleTheme = () => { + const theme = getTheme(); + const newTheme = theme === 'dark' ? 'light' : 'dark'; + setTheme(newTheme); + saveTheme(newTheme); +}; + +// https://stackoverflow.com/a/28344281 +const hasClass = (ele,cls) => { + return !!ele.className.match(new RegExp('(\\s|^)'+cls+'(\\s|$)')); +}; +const addClass = (ele,cls) => { + if (!hasClass(ele,cls)) ele.className += " "+cls; +}; +const removeClass = (ele,cls) => { + if (hasClass(ele,cls)) { + var reg = new RegExp('(\\s|^)'+cls+'(\\s|$)'); + ele.className=ele.className.replace(reg,' '); + } +}; + +const addClassToClass = (targetCls, newCls) => { + const elements = document.getElementsByClassName(targetCls); + for (let i = 0; i < elements.length; i++) { + addClass(elements[i], newCls); + } +}; + +const removeClassFromClass = (targetCls, newCls) => { + const elements = document.getElementsByClassName(targetCls); + for (let i = 0; i < elements.length; i++) { + removeClass(elements[i], newCls); + } +}; + +const setTheme = themeStr => { + if (themeStr === 'dark') { + document.getElementsByTagName('html')[0].style.background = 'rgb(20, 20, 20)'; + addClass(document.getElementsByTagName('body')[0], 'uk-light'); + addClassToClass('uk-card', 'uk-card-secondary'); + removeClassFromClass('uk-card', 'uk-card-default'); + } + else { + document.getElementsByTagName('html')[0].style.background = ''; + removeClass(document.getElementsByTagName('body')[0], 'uk-light'); + removeClassFromClass('uk-card', 'uk-card-secondary'); + addClassToClass('uk-card', 'uk-card-default'); + } +}; + +const styleModal = () => { + const color = getTheme() === 'dark' ? '#222' : ''; + $('.uk-modal-header').css('background', color); + $('.uk-modal-body').css('background', color); + $('.uk-modal-footer').css('background', color); +}; + +// do it before document is ready to prevent the initial flash of white +setTheme(getTheme()); + +document.addEventListener('DOMContentLoaded', () => { + // because this script is attached at the top of HTML, the style on uk-card + // won't be applied because the elements are not available yet. We have to + // apply the theme again for it to take effect + setTheme(getTheme()); +}, false); diff --git a/public/js/title.js b/public/js/title.js index d309c1e..a9af970 100644 --- a/public/js/title.js +++ b/public/js/title.js @@ -30,6 +30,7 @@ function showModal(encodedPath, pages, percentage, encodedeTitle, encodedEntryTi }); UIkit.modal($('#modal')).show(); + styleModal(); } function updateProgress(titleID, entryID, page) { $.post('/api/progress/' + titleID + '/' + entryID + '/' + page, function(data) { diff --git a/src/views/layout.ecr b/src/views/layout.ecr index fc015d8..cf1853b 100644 --- a/src/views/layout.ecr +++ b/src/views/layout.ecr @@ -13,6 +13,7 @@
+