diff --git a/docs/_template/light-dark-theme/partials/affix.tmpl.partial b/docs/_template/light-dark-theme/partials/affix.tmpl.partial index dd5d5d70e..ffbd27dcb 100644 --- a/docs/_template/light-dark-theme/partials/affix.tmpl.partial +++ b/docs/_template/light-dark-theme/partials/affix.tmpl.partial @@ -4,10 +4,10 @@

Theme

- +
{{^_disableContribution}}
diff --git a/docs/_template/light-dark-theme/partials/head.tmpl.partial b/docs/_template/light-dark-theme/partials/head.tmpl.partial index 306d12343..3deee4aba 100644 --- a/docs/_template/light-dark-theme/partials/head.tmpl.partial +++ b/docs/_template/light-dark-theme/partials/head.tmpl.partial @@ -12,14 +12,14 @@ - - + {{#_noindex}}{{/_noindex}} {{#_enableSearch}}{{/_enableSearch}} {{#_enableNewTab}}{{/_enableNewTab}} + diff --git a/docs/_template/light-dark-theme/styles/styleswitcher.js b/docs/_template/light-dark-theme/styles/styleswitcher.js index 9ca71bb18..e80a17daf 100644 --- a/docs/_template/light-dark-theme/styles/styleswitcher.js +++ b/docs/_template/light-dark-theme/styles/styleswitcher.js @@ -1,66 +1,27 @@ -function getSelectionChange(e){ - var selectValue = e.options[e.selectedIndex].value; - if (selectValue != null) setActiveStyleSheet(selectValue); -} - -function setActiveStyleSheet(title) { - var i, a, main; - for(i=0; (a = document.getElementsByTagName("link")[i]); i++) { - if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) { - a.disabled = true; - if(a.getAttribute("title") == title) a.disabled = false; - } - } -} +const baseUrl = document.getElementById("docfx-style:rel").content; +var themeElement; -function getActiveStyleSheet() { - var i, a; - for(i=0; (a = document.getElementsByTagName("link")[i]); i++) { - if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title") && !a.disabled) return a.getAttribute("title"); - } - return null; +function onThemeSelect(event) { + const theme = event.target.value; + window.localStorage.setItem("theme", theme); + window.themeElement.href = getUrl(theme); } -function getPreferredStyleSheet() { - var i, a; - for(i=0; (a = document.getElementsByTagName("link")[i]); i++) { - if(a.getAttribute("rel").indexOf("style") != -1 - && a.getAttribute("rel").indexOf("alt") == -1 - && a.getAttribute("title") - ) return a.getAttribute("title"); - } - return null; +function getUrl(slug) { + return baseUrl + "styles/" + slug + ".css"; } -function createCookie(name,value,days) { - if (days) { - var date = new Date(); - date.setTime(date.getTime()+(days*24*60*60*1000)); - var expires = "; expires="+date.toGMTString(); - } - else expires = ""; - document.cookie = name+"="+value+expires+"; path=/"; -} +const themeElement = document.createElement("link"); +themeElement.rel = "stylesheet"; -function readCookie(name) { - var nameEQ = name + "="; - var ca = document.cookie.split(';'); - for(var i=0;i < ca.length;i++) { - var c = ca[i]; - while (c.charAt(0)==' ') c = c.substring(1,c.length); - if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length); - } - return null; -} +const theme = window.localStorage.getItem("theme") || "light"; +themeElement.href = getUrl(theme); -window.onload = function(e) { - var cookie = readCookie("style"); - var title = cookie ? cookie : getPreferredStyleSheet(); - setActiveStyleSheet(title); - document.getElementById("theme-switcher").value = title; -} +document.head.appendChild(themeElement); +window.themeElement = themeElement; -window.onunload = function(e) { - var title = getActiveStyleSheet(); - createCookie("style", title, 365); -} \ No newline at end of file +document.addEventListener("DOMContentLoaded", function() { + const themeSwitcher = document.getElementById("theme-switcher"); + themeSwitcher.onchange = onThemeSelect; + themeSwitcher.value = theme; +}, false);