xref: /template/readthedokus/js/readthedokus.js (revision 8dab89dff7d7a27dcacf1cfbfe1a3805b0791127)
1function ReadtheDokus()
2{
3
4	this._currentPage;
5	this._currentPageIndex;
6	this._pages;
7	this._toc = document.getElementById("dw__toc");
8	this._sidebar =document.querySelector("#dokuwiki__aside");
9
10}
11
12ReadtheDokus.prototype.run = function()
13{
14
15	var isFound = false;
16	this._pages = [];
17
18	// Enum sidebar items to
19	//   - embed toc in the corresponding sidebar item
20	//   - collect all page links
21	if (JSINFO["ACT"] == "show")
22	{
23		this._enumSidebarLinks(function(elem) {
24			// Embed toc
25			if (elem.href.indexOf(JSINFO["id"]) > -1)
26			{
27				this._embedToc(elem);
28				if (this._toc)
29				{
30					this._toc.scrollIntoView(true);
31				}
32				isFound = true;
33			}
34
35			// Collect page links
36			this._pages.push(elem.href);
37		}.bind(this));
38	}
39
40	// Show toc on top of sidebar if item was not found in sidebar
41	if (!isFound)
42	{
43		if (this._toc)
44		{
45			this._showToc();
46			this._toc.scrollIntoView(true);
47		}
48	}
49
50	this._initToc();
51	this._initMobileHeader();
52	this._initPageButtons();
53	this._sidebar.querySelector("#sidebarheader #qsearch__in").setAttribute("placeholder", "Search docs");
54
55};
56
57ReadtheDokus.prototype._enumSidebarLinks = function(callback)
58{
59
60	callback = ( typeof callback === "function" ? callback : function(){} );
61	//var links = document.querySelectorAll("#dokuwiki__aside .aside > ul .level1 a");
62	var links = this._sidebar.querySelectorAll("#dokuwiki__aside .aside > ul .level1 a");
63
64	links.forEach(function(elem) {
65		callback(elem);
66	});
67
68};
69
70ReadtheDokus.prototype._embedToc = function(target)
71{
72
73	if (target && this._toc)
74	{
75		target.parentNode.parentNode.appendChild(this._toc);
76		target.parentNode.style.display = "none";
77	}
78
79};
80
81ReadtheDokus.prototype._showToc = function()
82{
83
84	if (this._toc)
85	{
86		this._toc.parentNode.style.display = "block";
87	}
88
89};
90
91ReadtheDokus.prototype._initToc = function()
92{
93
94	if (this._toc)
95	{
96		// Add click event handler to highlight an menu item
97		this._toc.querySelectorAll(".level2 a").forEach(function(elem) {
98			elem.addEventListener("click", function() {
99				this._toc.querySelectorAll(".current").forEach(function(elem) {
100					elem.classList.remove("current");
101				});
102				elem.classList.add("current");
103				elem.scrollIntoView(true);
104			}.bind(this));
105		}.bind(this));
106
107		// Add click event handler to expand/collapse menu
108		this._toc.querySelectorAll(".level1 a").forEach(function(elem) {
109			if (elem.parentNode.parentNode.querySelector(".toc"))
110			{
111				// elem.insertAdjacentHTML("afterbegin", '<i class="far fa-plus-square"></i>');
112				// elem.insertAdjacentHTML("afterbegin", '<img class="btn-expand minus" src="/docs/lib/images/minus.gif" alt="−">');
113				elem.insertAdjacentHTML("afterbegin", '<div class="btn-expand"><i class="far fa-plus-square"></i><img class="minus" src="/docs/lib/images/minus.gif" alt="−"></div>');
114
115				var child = elem.childNodes[0];
116				child.addEventListener("click", function(e) {
117					child.parentNode.parentNode.parentNode.querySelector(".toc").classList.toggle("invisible");
118					if (child.classList.contains("minus"))
119					{
120						child.classList.remove("minus");
121						child.classList.add("plus");
122						child.src="/docs/lib/images/plus.gif";
123					}
124					else
125					{
126						child.classList.remove("plus");
127						child.classList.add("minus");
128						child.src="/docs/lib/images/minus.gif";
129					}
130					e.stopPropagation();
131					e.preventDefault();
132				});
133			}
134			elem.addEventListener("click", function() {
135				elem.scrollIntoView(true);
136			});
137		});
138	}
139
140};
141
142ReadtheDokus.prototype._initMobileHeader = function()
143{
144
145	// Add click event handler for mobile menu
146	document.getElementById("btn-mobilemenu").addEventListener("click", function(){
147		this._sidebar.classList.toggle("visible");
148		document.querySelector("#dokuwiki__content").classList.toggle("shift");
149	}.bind(this));
150
151};
152
153ReadtheDokus.prototype._initPageButtons = function()
154{
155
156	// Get current page
157	var pos = window.location.href.indexOf("#");
158	if (pos > -1)
159	{
160		this._currentPage = window.location.href.substring(0, pos);
161	}
162	else
163	{
164		this._currentPage = window.location.href;
165	}
166
167	// Get current page index
168	this._currentPageIndex = this._pages.indexOf(this._currentPage);
169
170	// Show prev button
171	if (this._currentPageIndex > 0)
172	{
173		document.getElementById("btn-prevpage").classList.add("visible");
174		document.getElementById("btn-prevpage").href = this._pages[this._currentPageIndex - 1];
175	}
176
177	// Show next button
178	if (this._currentPageIndex < this._pages.length - 1)
179	{
180		document.getElementById("btn-nextpage").classList.add("visible");
181		document.getElementById("btn-nextpage").href = this._pages[this._currentPageIndex + 1];
182	}
183
184};
185