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 get_globals = await _require('/_lib/get_globals.jst')
7 let get_session = await _require('/_lib/get_session.jst')
8 //let icon_cart_small = await env.site.get_min_svg('/_svg/icon_cart_small.svg')
9 let icon_search_mono = await env.site.get_min_svg('/_svg/icon_search_mono.svg')
10 let logo_large = await env.site.get_min_svg('/_svg/logo_large.svg')
11 let menu = await env.site.get_menu('/_menu.json')
12 let page = await _require('/_lib/page.jst')
14 // initialize env.cart
17 let transaction = await env.site.Transaction()
18 let signed_in_as, site_title, copyright
20 let session = await get_session(env, transaction)
21 signed_in_as = session.get_json('signed_in_as', null)
23 let globals = await get_globals(env, transaction)
24 site_title = globals.get_json('site_title')
25 copyright = globals.get_json('copyright')
28 transaction.rollback()
35 // extract top-level directory name
36 assert(env.parsed_url.pathname.slice(0, 1) === '/')
37 let index = env.parsed_url.pathname.indexOf('/', 1)
38 let dir = index === -1 ? '' : env.parsed_url.pathname.slice(1, index)
44 menu.entries[menu.index[dir]].name
52 // extract top-level directory name
53 assert(env.parsed_url.pathname.slice(0, 1) === '/')
54 let index = env.parsed_url.pathname.indexOf('/', 1)
55 let dir = index === -1 ? '' : env.parsed_url.pathname.slice(1, index)
59 div.row.align-items-center.py-3 {
64 div.'mb-1'.text-right {
65 span#signed-in-status {
66 if (signed_in_as !== null)
67 'Signed in.' //`Signed in as ${signed_in_as}.`
72 if (signed_in_as !== null)
73 a#sign-in(href="#" style="display: none;") {'Sign in'}
75 a#sign-in(href="#") {'Sign in'}
77 if (signed_in_as !== null)
78 a#sign-up(href="/my_account/sign_up/index.html" style="display: none;") {'Sign up'}
80 a#sign-up(href="/my_account/sign_up/index.html") {'Sign up'}
82 if (signed_in_as !== null)
83 a#sign-out(href="#") {'Sign out'}
85 a#sign-out(href="#" style="display: none;") {'Sign out'}
88 form(action="/search/index.html") {
90 input.form-control(name="query" type="text" placeholder="Search" aria-describedby="search-button") {}
91 div.input-group-append {
92 button.btn.btn-outline-secondary#search-button(type="submit") {
93 _out.push(icon_search_mono)
100 //div.'col-sm-1'.vbottom {
101 // // a nested div is used to avoid hover colour on the padding
102 // div.nav-li-a(style="text-align: center;") {
103 // a(href="/online_store/view_cart/index.html") {
105 // _out.push(icon_cart_small)
109 // `${(env.cart.items || []).length}`
118 nav.navbar.navbar-expand-lg.navbar-dark.bg-primary.scrollbar-fix {
120 //a.navbar-brand(href="#") {'Navbar'}
122 button.navbar-toggler(type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation") {
123 span.navbar-toggler-icon {}
125 div.collapse.navbar-collapse#navbarSupportedContent {
126 ul.navbar-nav.mr-auto {
127 if (dir.length === 0)
129 a.nav-link(href="/index.html") {
131 span.sr-only {' (current)'}
136 a.nav-link(href="/index.html") {'Home'}
138 let entries = menu.entries
139 for (let i = 0; i < entries.length; ++i)
140 if (entries[i].navbar)
141 if (entries[i].dir === dir)
143 a.nav-link(href=`/${entries[i].dir}/index.html`) {
145 span.sr-only {' (current)'}
150 a.nav-link(href=`/${entries[i].dir}/index.html`) {
154 //li.nav-item.dropdown {
155 // a.nav-link.dropdown-toggle#navbarDropdown(href="#" role="button" data-toggle="dropdown" aria-expanded="false") {
158 // div.dropdown-menu(aria-labelledby="navbarDropdown") {
159 // a.dropdown-item(href="#") {
163 // a.dropdown-item(href="#") {
166 // div.dropdown-divider {}
167 // a.dropdown-item(href="#") {
168 // 'Something else here'
173 // a.nav-link.disabled {
178 ul.navbar-nav.ml-auto {
180 a.nav-link#give-feedback(href="#") {'Give feedback'}
191 footer.scrollbar-fix {
193 a(rel="license" href="http://creativecommons.org/licenses/by-sa/3.0/") {
194 img(alt="Creative Commons License" style="border-width:0;" src="/images/by-sa_3.0_88x31.png") {}
198 a(href="https://git.ndcode.org/public/ndcode_site.git") {
201 ' and licensed under a '
202 a(rel="license" href="http://creativecommons.org/licenses/by-sa/3.0/") {
203 'Creative Commons Attribution-ShareAlike 3.0 Unported License'
208 p {'Example code fragments embedded within the text are placed in the public domain unless otherwise noted.'}
210 p {`Copyright © ${new XDate(env.now).getUTCFullYear()} ${copyright}.`}
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')