1 let assert = require('assert')
2 let XDate = require('xdate')
4 return async (env, head, body, scripts) => {
5 //let cart = await _require('/online_store/cart.jst')
6 //let icon_cart_small = await env.site.get_min_svg('/_svg/icon_cart_small.svg')
7 let icon_search_mono = await env.site.get_min_svg('/_svg/icon_search_mono.svg')
8 let logo_large = await env.site.get_min_svg('/_svg/logo_large.svg')
9 let menu = await env.site.get_menu('/_menu.json')
10 let page = await _require('/_lib/page.jst')
12 // initialize env.cart
19 // extract top-level directory name
20 assert(env.parsed_url.pathname.slice(0, 1) === '/')
21 let index = env.parsed_url.pathname.indexOf('/', 1)
22 let dir = index === -1 ? '' : env.parsed_url.pathname.slice(1, index)
25 let transaction = await env.site.database.Transaction()
27 let globals = await (await transaction.get({})).get('globals')
28 ;`${await globals.get_json('site_title')}: ${
31 menu.entries[menu.index[dir]].name
35 transaction.rollback()
43 // extract top-level directory name
44 assert(env.parsed_url.pathname.slice(0, 1) === '/')
45 let index = env.parsed_url.pathname.indexOf('/', 1)
46 let dir = index === -1 ? '' : env.parsed_url.pathname.slice(1, index)
50 div.row.align-items-center.py-3 {
55 div.'mb-1'.text-right {
56 span#signed-in-status {
57 if (env.signed_in_as !== null)
58 'Signed in.' //`Signed in as ${env.signed_in_as}.`
63 if (env.signed_in_as !== null)
64 a#sign-in(href="#" style="display: none;") {'Sign in'}
66 a#sign-in(href="#") {'Sign in'}
68 if (env.signed_in_as !== null)
69 a#sign-up(href="/my_account/sign_up/index.html" style="display: none;") {'Sign up'}
71 a#sign-up(href="/my_account/sign_up/index.html") {'Sign up'}
73 if (env.signed_in_as !== null)
74 a#sign-out(href="#") {'Sign out'}
76 a#sign-out(href="#" style="display: none;") {'Sign out'}
79 form(action="/search/index.html") {
81 input.form-control(name="query" type="text" placeholder="Search" aria-describedby="search-button") {}
82 div.input-group-append {
83 button.btn.btn-outline-secondary#search-button(type="submit") {
84 _out.push(icon_search_mono)
91 //div.'col-sm-1'.vbottom {
92 // // a nested div is used to avoid hover colour on the padding
93 // div.nav-li-a(style="text-align: center;") {
94 // a(href="/online_store/view_cart/index.html") {
96 // _out.push(icon_cart_small)
100 // `${(env.cart.items || []).length}`
109 nav.navbar.navbar-expand-lg.navbar-dark.bg-primary.scrollbar-fix {
111 //a.navbar-brand(href="#") {'Navbar'}
113 button.navbar-toggler(type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation") {
114 span.navbar-toggler-icon {}
116 div.collapse.navbar-collapse#navbarSupportedContent {
117 ul.navbar-nav.mr-auto {
118 if (dir.length === 0)
120 a.nav-link(href="/index.html") {
122 span.sr-only {' (current)'}
127 a.nav-link(href="/index.html") {'Home'}
129 let entries = menu.entries
130 for (let i = 0; i < entries.length; ++i)
131 if (entries[i].navbar)
132 if (entries[i].dir === dir)
134 a.nav-link(href=`/${entries[i].dir}/index.html`) {
136 span.sr-only {' (current)'}
141 a.nav-link(href=`/${entries[i].dir}/index.html`) {
145 //li.nav-item.dropdown {
146 // a.nav-link.dropdown-toggle#navbarDropdown(href="#" role="button" data-toggle="dropdown" aria-expanded="false") {
149 // div.dropdown-menu(aria-labelledby="navbarDropdown") {
150 // a.dropdown-item(href="#") {
154 // a.dropdown-item(href="#") {
157 // div.dropdown-divider {}
158 // a.dropdown-item(href="#") {
159 // 'Something else here'
164 // a.nav-link.disabled {
169 ul.navbar-nav.ml-auto {
171 a.nav-link#give-feedback(href="#") {'Give feedback'}
182 footer.scrollbar-fix {
184 a(rel="license" href="http://creativecommons.org/licenses/by-sa/3.0/") {
185 img(alt="Creative Commons License" style="border-width:0;" src="/images/by-sa_3.0_88x31.png") {}
189 a(href="https://git.ndcode.org/public/ndcode_site.git") {
192 ' and licensed under a '
193 a(rel="license" href="http://creativecommons.org/licenses/by-sa/3.0/") {
194 'Creative Commons Attribution-ShareAlike 3.0 Unported License'
199 p {'Example code fragments embedded within the text are placed in the public domain unless otherwise noted.'}
202 let transaction = await env.site.database.Transaction()
204 let globals = await (await transaction.get({})).get('globals')
205 ;`Copyright © ${new XDate().getUTCFullYear()} ${await globals.get('copyright')}.`
208 transaction.rollback()
215 div#sign-in-modal.modal.fade(role="dialog") {
219 span.h4.modal-title {'Sign in'}
225 label.form-label(for="sign-in-email") {'Email'}
226 input.form-control#sign-in-email(type="text" placeholder="Account email address" required="required" maxlength=256) {}
233 label.form-label(for="sign-in-password") {'Password'}
234 input.form-control#sign-in-password(type="password" placeholder="Account password" required="required" minlength=8 maxlength=256) {}
241 a(href="/my_account/sign_up/index.html") {'Sign up'}
246 a(href="/my_account/password_reset/index.html") {'Password reset'}
250 button.btn.btn-outline-secondary(type="button" data-dismiss="modal") {
253 button.btn.btn-primary#sign-in-submit(type="button") {
261 div#feedback-modal.modal.fade(role="dialog") {
265 span.h4.modal-title {'Give feedback'}
269 'Did you notice something not quite right, or just want to share your impression of this page?'
274 label.form-label(for="feedback-message") {'Message'}
275 textarea.form-control#feedback-message(placeholder="Please tell us your thoughts" required="required" rows=4 maxlength=65536) {}
281 button.btn.btn-outline-secondary(type="button" data-dismiss="modal") {
284 button.btn.btn-primary#feedback-submit(type="button") {
292 div#message-modal.modal.fade(role="dialog") {
296 span.h4.modal-title {'Message'}
298 div.modal-body#message-modal-message {
301 button.btn.btn-outline-secondary(type="button" data-dismiss="modal") {
311 script(src="/js/api_call.js") {}
314 let api_account_sign_in = async (...args) => api_call(
315 '/api/account/sign_in.json',
318 let api_account_sign_out = async (...args) => api_call(
319 '/api/account/sign_out.json',
322 let api_feedback = async (...args) => api_call(
323 '/api/feedback.json',
327 // this function can be overridden in a further script
328 function sign_in_out(status) {
331 document.addEventListener(
335 document.getElementById('sign-in').addEventListener(
338 document.getElementById('sign-in-email').value = ''
339 document.getElementById('sign-in-password').value = ''
340 $('#sign-in-modal').modal('show')
344 $('#sign-in-modal').on(
348 $('#sign-in-email').focus()
352 document.getElementById('sign-in-submit').addEventListener(
357 email = document.getElementById('sign-in-email').value.slice(0, 256).toLowerCase()
358 await api_account_sign_in(
360 document.getElementById('sign-in-password').value.slice(0, 256)
365 error instanceof Problem ?
371 (error.stack || error.message).toString()
376 if (problem.title === 'Email not yet verified') {
377 location.href = `/my_account/send_verification_email?email=${encodeURIComponent(email)}`
381 document.getElementById('message-modal-message').textContent = problem.detail
382 $('#sign-in-modal').modal('hide')
383 $('#message-modal').modal('show')
387 document.getElementById('signed-in-status').textContent = 'Signed in.' //`Signed in as ${email}.`
390 $('#sign-out').show()
393 document.getElementById('message-modal-message').textContent = `You are now signed in as "${email}".`
394 $('#sign-in-modal').modal('hide')
395 $('#message-modal').modal('show')
400 document.getElementById('sign-out').addEventListener(
404 await api_account_sign_out()
408 error instanceof Problem ?
414 (error.stack || error.message).toString()
419 document.getElementById('message-modal-message').textContent = problem.detail
420 $('#sign-in-modal').modal('hide')
421 $('#message-modal').modal('show')
425 document.getElementById('signed-in-status').textContent = 'Browsing as guest.'
428 $('#sign-out').hide()
431 document.getElementById('message-modal-message').textContent = `You are now signed out.`
432 $('#sign-in-modal').modal('hide')
433 $('#message-modal').modal('show')
438 document.getElementById('give-feedback').addEventListener(
441 $('#feedback-message').text('')
442 $('#feedback-modal').modal('show')
447 $('#feedback-modal').on(
450 $('#feedback-message').focus()
454 document.getElementById('feedback-submit').addEventListener(
460 document.getElementById('feedback-message').value.slice(0, 65536)
465 error instanceof Problem ?
471 (error.stack || error.message).toString()
476 document.getElementById('message-modal-message').textContent = problem.detail
477 $('#feedback-modal').modal('hide')
478 $('#message-modal').modal('show')
482 document.getElementById('message-modal-message').textContent = 'Thanks! We have received your feedback.'
483 $('#feedback-modal').modal('hide')
484 $('#message-modal').modal('show')