blob: ee8f977e2cb4587ecfa338433f8fc8f30c2e4631 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
|
// menu.js - menu system for small site - 2022 Viljami Ilola
const jsonURL = 'pages.json'
var conf = {}
var pages = {}
var currentPage = null;
loadPages = async() => {
await fetch( jsonURL )
.then( response => response.json() )
.then( json => conf = json )
.catch( error => {
document.body.innerHTML = `Error loading ${jsonURL}<hr>${error}`
} )
pages = conf.pages
installHamburger()
createMenu()
createPlaceholders()
hashToPage()
}
createPlaceholders = () => {
pagesMain = document.createElement('main')
Object.keys(pages).forEach(p => {
if ( pages[p].URL && pages[p].URL !== "" ) {
const pageSection = document.createElement('section')
pageSection.id = `${p}_page`
pageSection.className='page'
pagesMain.appendChild( pageSection )
}
})
document.documentElement.lastChild.append( pagesMain )
}
createMenuIcon = (p) => {
const icon = pages[p].menuIcon ? pages[p].menuIcon : ''
if ( icon === '' ) return null
const iconImg = document.createElement('img')
iconImg.src = icon
const iconAnchor = document.createElement('a')
iconAnchor.href = `#${p}`
iconAnchor.appendChild( iconImg )
const iconDiv = document.createElement('div')
iconDiv.className = 'menuIcon'
iconDiv.appendChild( iconAnchor )
return iconDiv
}
createMenuText = (p) => {
const url = pages[p].URL ? pages[p].URL : ""
const name = pages[p].menuName ? pages[p].menuName : url
if ( name === '' ) return null
const textNode = document.createTextNode( name )
const textAnchor = document.createElement( url === "" ? 'div' : 'a')
textAnchor.href = `#${p}`
textAnchor.appendChild( textNode )
const textDiv = document.createElement('div')
textDiv.className = 'menuText'
textDiv.appendChild( textAnchor )
return textDiv
}
createMenu = () => {
const menuNav = document.createElement('nav')
menuNav.id = 'menu'
menuNav.className = 'menuHidden'
Object.keys(pages).forEach(p => {
const entryDiv = document.createElement('div')
entryDiv.className = 'menuItem'
entryDiv.id = `${p}_menuEntry`
const iconDiv = createMenuIcon(p)
const textDiv = createMenuText(p)
if ( iconDiv ) entryDiv.appendChild( iconDiv )
if ( textDiv ) entryDiv.appendChild( textDiv )
const url = pages[p].URL ? pages[p].URL : ""
const name = pages[p].menuName ? pages[p].menuName : url
if ( url === "" ) entryDiv.className +=
name === "" ? ' menuSpacer' : ' menuTitle'
else entryDiv.onclick = () => window.location.hash = p;
if ( pages[p].hidden ) entryDiv.style.display = 'none';
menuNav.append( entryDiv )
})
document.documentElement.lastChild.append( menuNav )
const spacerDiv = document.createElement('div')
spacerDiv.id = 'spaceBeforePage'
document.documentElement.lastChild.append( spacerDiv )
}
installHamburger = () => {
hamburgerDiv = document.createElement('div')
hamburgerDiv.id = 'hamburgerMenu'
hamburgerDiv.onclick = () => toggleMenu()
document.documentElement.lastChild.append( hamburgerDiv )
}
toggleMenu = () => {
const menuNav = document.getElementById('menu')
menuNav.className = menuNav.className === 'menuHidden' ? '' : 'menuHidden'
if (menuNav.className === '') window.scrollTo(0,0)
}
hashToPage = async () => {
const p = !document.location.hash
? Object.keys(pages)[0] : document.location.hash.substr(1) in pages
? document.location.hash.substr(1) : Object.keys(pages)[0]
if ( p === Object.keys(pages)[0] ) {
window.history.replaceState('', '', window.location.pathname)
}
if ( p === currentPage ) return false
const pageElement = document.getElementById(`${p}_page`)
if ( !currentPage ) currentPage = p;
else document.getElementById(`${currentPage}_page`).className = 'page'
pageElement.className = 'page pageActive'
if ( !pageElement.loaded ) await fetch( pages[p].URL )
.then( response => {
if (!response.ok) return `ERROR loading "${pages[p].URL}"!`
return response.text()
} )
.then( text => {
pageElement.innerHTML = text;
pageElement.loaded = true;
} )
if ( pageElement.innerHTML.startsWith("redirect = ") ) {
pageElement.loaded = false;
window.location.assign( pageElement.innerHTML.slice( 11 ) );
}
document.getElementById(`${currentPage}_menuEntry`)
.className = 'menuItem'
document.getElementById(`${p}_menuEntry`)
.className = 'menuItem menuItemActive'
currentPage = p
if (!pages[p].pageTitle) document.title = conf.title
else document.title = `${pages[p].pageTitle} - ${conf.title}`
}
hideMenu = () => document.getElementById('menu').className = 'menuHidden';
window.onload = async() => {
loadPages()
}
window.onhashchange = () => {
hideMenu()
hashToPage()
}
|