function ReadtheDokus() { this._currentPage; this._currentPageIndex; this._pages; this._toc = document.getElementById("dw__toc"); this._sidebar =document.querySelector("#dokuwiki__aside"); this._delimiter = ( window.location.search.indexOf(":") > -1 ? ":" : "/"); this._id = ( this._delimiter == ":" ? JSINFO["id"] : JSINFO["id"].replaceAll(":", "/")); this._startPage; } ReadtheDokus.prototype.run = function() { // Enum sidebar items to // - embed toc in the corresponding sidebar item // - collect all page links var isFound = false; this._pages = []; if (JSINFO["ACT"] == "show") { this._enumSidebarLinks(function(elem) { // Embed toc if (elem.href.indexOf(this._id) > -1) { this._embedToc(elem, this._toc); isFound = true; } // Collect page links this._pages.push(elem.href); }.bind(this)); } // Start page this._startPage = this._getStartPage(this._pages[0], this._delimiter); this._pages.unshift(this._startPage); document.querySelectorAll(".home a").forEach(function(elem) { console.log(this._startPage); elem.href = this._startPage; }.bind(this)); // Show toc on top of sidebar if item was not found in sidebar if (!isFound) { this._showToc(this._toc); } this._initToc(this._toc); this._initMobileHeader(); this._initPageButtons(); this._sidebar.querySelector("#sidebarheader #qsearch__in").setAttribute("placeholder", "Search docs"); if (this._toc) { this._toc.scrollIntoView(true); } }; ReadtheDokus.prototype.toggleTocMenu = function(elem) { var invisible = elem.parentNode.querySelector(".toc").classList.contains("invisible"); if (invisible) { this.expandTocMenu(elem); } else { this.collapseTocMenu(elem); } }; ReadtheDokus.prototype.expandTocMenu = function(elem, allChildren) { if (elem && elem.classList.contains("expandable")) { elem.parentNode.querySelector(".toc").classList.remove("invisible"); var i = elem.children[0].children[0].children[0]; i.classList.remove("fa-plus-square"); i.classList.add("fa-minus-square"); var img = elem.children[0].children[0].children[1]; img.classList.remove("plus"); img.classList.add("minus"); img.src="/docs/lib/images/minus.gif"; } }; ReadtheDokus.prototype.collapseTocMenu = function(elem, allChildren) { if (elem && elem.classList.contains("expandable")) { elem.parentNode.querySelector(".toc").classList.add("invisible"); var i = elem.children[0].children[0].children[0]; i.classList.remove("fa-minus-square"); i.classList.add("fa-plus-square"); var img = elem.children[0].children[0].children[1]; img.classList.remove("minus"); img.classList.add("plus"); img.src="/docs/lib/images/plus.gif"; } }; ReadtheDokus.prototype._enumSidebarLinks = function(callback) { callback = ( typeof callback === "function" ? callback : function(){} ); var links = this._sidebar.querySelectorAll(".aside > ul .level1 a"); links.forEach(function(elem) { callback(elem); }); }; ReadtheDokus.prototype._getStartPage = function(basePage, delimiter) { var result = ""; if (basePage && delimiter) { var re = new RegExp("\\" + delimiter + "[^\\" + delimiter + "]*[^\\" + delimiter + "]*$"); result = basePage.replace(re, "").replace(re, "") + delimiter + "start"; } return result; }; ReadtheDokus.prototype._embedToc = function(target, toc) { if (target && toc) { target.parentNode.parentNode.appendChild(toc); target.parentNode.style.display = "none"; } }; ReadtheDokus.prototype._showToc = function(toc) { if (toc) { this._toc.parentNode.style.display = "block"; } }; ReadtheDokus.prototype._initToc = function(toc) { if (toc) { this._installTocSelectHandler(); this._installTocMenuHandler(); } }; // Install click handler to highlight and expand toc menu ReadtheDokus.prototype._installTocSelectHandler = function() { this._toc.querySelectorAll(".level1 div.li").forEach(function(elem) { elem.addEventListener("click", function() { // Get level2 parent let p = this._getParent(elem, "level2"); // Remove all current this._toc.querySelectorAll(".current").forEach(function(elem) { elem.classList.remove("current"); }); // Set current to this and level2 parent if (p) { p.parentNode.classList.add("current"); p.classList.add("current"); elem.classList.add("current"); elem.scrollIntoView(true); } // Expand this.expandTocMenu(elem); // Fold the other level2 items this._toc.querySelectorAll(".level2 > div.li.expandable").forEach(function(item) { if (item != p) { this.collapseTocMenu(item); } }.bind(this)); }.bind(this)); }.bind(this)); }; // Install click handler to expand/collapse toc menu ReadtheDokus.prototype._installTocMenuHandler = function() { // Search for toc menu items which have children this._toc.querySelectorAll("div.li").forEach(function(elem) { if (elem.parentNode.querySelector(".toc")) { elem.classList.add("expandable"); // Insert +/- fontawesome icon and image elem.children[0].insertAdjacentHTML("afterbegin", '
−
'); // Install click handler elem.children[0].children[0].addEventListener("click", function(e) { this.toggleTocMenu(elem); e.stopPropagation(); e.preventDefault(); }.bind(this)); // Only level1 menu items are open at start if (!elem.parentNode.classList.contains("level1")) { this.collapseTocMenu(elem); } } // Install click handler to move an clicked item to top elem.addEventListener("click", function() { elem.scrollIntoView(true); }); }.bind(this)); }; ReadtheDokus.prototype._getParent = function(elem, level) { let current = elem.parentNode; while (current && !current.classList.contains("level1")) { if (current.classList.contains(level)) { return current.children[0]; } current = current.parentNode.parentNode; } return null; }; ReadtheDokus.prototype._initMobileHeader = function() { // Add click event handler for mobile menu document.getElementById("btn-mobilemenu").addEventListener("click", function(){ this._sidebar.classList.toggle("visible"); document.querySelector("#dokuwiki__content").classList.toggle("shift"); }.bind(this)); }; ReadtheDokus.prototype._initPageButtons = function() { // Get current page (remove hash) this._currentPage = window.location.href.replace(/#.*$/, ""); // Get current page index this._currentPageIndex = this._pages.indexOf(this._currentPage); // Show prev button if (this._currentPageIndex > 0) { document.getElementById("btn-prevpage").classList.add("visible"); document.getElementById("btn-prevpage").href = this._pages[this._currentPageIndex - 1]; } // Show next button if (this._currentPageIndex < this._pages.length - 1) { document.getElementById("btn-nextpage").classList.add("visible"); document.getElementById("btn-nextpage").href = this._pages[this._currentPageIndex + 1]; } };