Skip to content

Commit

Permalink
Merge branch 'main' into skip-theme
Browse files Browse the repository at this point in the history
  • Loading branch information
dbauszus-glx committed Nov 17, 2023
2 parents c5b4fcb + 3c04e78 commit dab5321
Show file tree
Hide file tree
Showing 13 changed files with 152 additions and 114 deletions.
70 changes: 48 additions & 22 deletions lib/ui/layers/view.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -32,22 +32,22 @@ mapp.utils.merge(mapp.dictionaries, {
export default (layer) => {

if (layer.view === null) {

// Do not create a layer view.
return layer;
return layer;
}

layer.view = mapp.utils.html.node`<div class="layer-view">`

// Create content from layer view panels and plugins
const content = Object.keys(layer)
.map(key => mapp.ui.layers.panels[key] && mapp.ui.layers.panels[key](layer))
.filter(panel => typeof panel !== 'undefined')

// Set default order for panel if not explicit in layer config.
layer.panelOrder = layer.panelOrder || ['draw-drawer','dataviews-drawer','filter-drawer','style-drawer','meta']
layer.panelOrder = layer.panelOrder || ['draw-drawer', 'dataviews-drawer', 'filter-drawer', 'style-drawer', 'meta']

content.sort((a,b)=>{
content.sort((a, b) => {

// Sort according to data-id in panelOrder array.
return layer.panelOrder.findIndex(chk => chk === a.dataset?.id)
Expand All @@ -62,16 +62,16 @@ export default (layer) => {
data-id=zoomToExtent
title=${mapp.dictionary.layer_zoom_to_extent}
class="mask-icon fullscreen"
onclick=${async e=>{
// response indicates whether locations were found.
let response = await layer.zoomToExtent()
// disable button if no locations were found.
e.target.disabled = !response
}}>` || ``
// Create toogleDisplay button for header.
onclick=${async e => {
// response indicates whether locations were found.
let response = await layer.zoomToExtent()
// disable button if no locations were found.
e.target.disabled = !response
}}>` || ``

// Create toggleDisplay button for header.
const toggleDisplay = mapp.utils.html.node`
<button
data-id=display-toggle
Expand All @@ -98,7 +98,7 @@ export default (layer) => {
// Create layer drawer node.
layer.drawer = mapp.ui.elements.drawer({
data_id: `layer-drawer`,
class: `layer-view raised ${layer.classList || ''} ${content.length? '' : 'empty'}`,
class: `layer-view raised ${layer.classList || ''} ${content.length ? '' : 'empty'}`,
header: header,
content: content
})
Expand All @@ -112,23 +112,49 @@ export default (layer) => {
content.forEach(el => layer.view.append(el))
}

// Create a div for the magnifying glass icon
const zoomButton = mapp.utils.html.node`<button class="mask-icon search" title="Zoom To" data-id="zoom-to">`

// Add an event listener to the magnifying glass icon to show the zoom level button when clicked
zoomButton.addEventListener('click', () => {
// Zoom to the extent of the layer if table provided.
if (layer.tableCurrent() !== null) {
layer.zoomToExtent();
layer.show();
};

// Zoom to the zoom level if tables object.
for (const key in layer.tables) {
if (layer.tables[key] !== null) {
layer.mapview.Map.getView().setZoom(key);
layer.show();
}
}
});

// Add the zoom Button before the layer toggle.
layer.view.querySelector('[data-id=display-toggle]').before(zoomButton)

// The layer view drawer should be disabled if layer.tables are not available for the current zoom level.
layer.mapview.Map.getTargetElement().addEventListener('changeEnd', ()=>{
layer.mapview.Map.getTargetElement().addEventListener('changeEnd', () => {

if (!layer.tables) return;

if (layer.tableCurrent() === null) {

// Collapse drawer and disable layer.view.
layer.view.querySelector('[data-id=layer-drawer]').classList.remove('expanded')
layer.view.classList.add('disabled')

} else {
// Remove the active state from the layer view toggle button.
layer.view.querySelector('[data-id=display-toggle]').classList.remove('on')
// Set the layer toggle button to disabled.
layer.view.querySelector('[data-id=display-toggle]').classList.add('disabled')

// Enable the layer.view.
layer.view.classList.remove('disabled')
} else {
// Set the layer toggle button to enabled.
layer.view.querySelector('[data-id=display-toggle]').classList.remove('disabled')
}

})

return layer
Expand Down
13 changes: 10 additions & 3 deletions mod/user/_user.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
const view = require('../view')

const methods = {
admin: {
handler: require('./admin'),
handler: (req,res) => {
req.params.template = 'user_admin_view'
req.params.language = req.params.user.language
req.params.user = req.params.user.email
view(req, res)
},
admin: true
},
register: {
Expand Down Expand Up @@ -56,13 +63,13 @@ module.exports = (req, res) => {
if (!req.params.user && (method.login || method.admin)) {

req.params.msg = 'login_required'
return
return methods.login.handler(req,res)
}

if (req.params.user && (!req.params.user.admin && method.admin)) {

req.params.msg = 'admin_required'
return
return methods.login.handler(req,res)
}

method.handler(req, res)
Expand Down
10 changes: 0 additions & 10 deletions mod/user/admin.js

This file was deleted.

5 changes: 3 additions & 2 deletions mod/user/delete.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ module.exports = async (req, res) => {
template: 'deleted_account',
language: user.language,
to: user.email,
host: `${req.headers.host.includes('localhost') && req.headers.host || process.env.ALIAS || req.headers.host}${process.env.DIR}`,
protocol: `${req.headers.host.includes('localhost') && 'http' || 'https'}://`
host: `${req.headers.origin
|| req.headers.referer && new URL(req.headers.referer).origin
|| 'https://' + (process.env.ALIAS || req.headers.host)}${process.env.DIR}`
})

res.send('User account deleted.')
Expand Down
9 changes: 4 additions & 5 deletions mod/user/login.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,10 @@ async function post(req, res) {

const date = new Date()

// Get the protocol and host for account verification email.
const protocol = `${req.headers.host.includes('localhost') && 'http' || 'https'}://`
const host = `${req.headers.host.includes('localhost') && req.headers.host || process.env.ALIAS || req.headers.host}${process.env.DIR}`
// Get the host for the account verification email.
const host = `${req.headers.origin
|| req.headers.referer && new URL(req.headers.referer).origin
|| 'https://' + (process.env.ALIAS || req.headers.host)}${process.env.DIR}`

// Update access_log and return user record matched by email.
let rows = await acl(`
Expand Down Expand Up @@ -169,7 +170,6 @@ async function post(req, res) {
language: user.language,
to: user.email,
host: host,
protocol: protocol,
remote_address
})

Expand Down Expand Up @@ -247,7 +247,6 @@ async function post(req, res) {
to: user.email,
host: host,
failed_attempts: parseInt(process.env.FAILED_ATTEMPTS) || 3,
protocol: protocol,
verificationtoken: verificationtoken,
remote_address
})
Expand Down
11 changes: 6 additions & 5 deletions mod/user/register.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,10 @@ async function post(req, res) {
// Get the date for logs.
const date = new Date().toISOString().replace(/\..*/,'')

// Get the protocol and host for account verification email.
const protocol = `${req.headers.host.includes('localhost') && 'http' || 'https'}://`
const host = `${req.headers.host.includes('localhost') && req.headers.host || process.env.ALIAS || req.headers.host}${process.env.DIR}`
// Get the host for account verification email.
const host = `${req.headers.origin
|| req.headers.referer && new URL(req.headers.referer).origin
|| 'https://' + (process.env.ALIAS || req.headers.host)}${process.env.DIR}`

// The password will be reset for exisiting user accounts.
if (user) {
Expand Down Expand Up @@ -129,7 +130,7 @@ async function post(req, res) {
language: user.language,
to: user.email,
host: host,
link: `${protocol}${host}/api/user/verify/${verificationtoken}`,
link: `${host}/api/user/verify/${verificationtoken}`,
remote_address
})

Expand Down Expand Up @@ -171,7 +172,7 @@ async function post(req, res) {
language,
to: req.body.email,
host: host,
link: `${protocol}${host}/api/user/verify/${verificationtoken}`,
link: `${host}/api/user/verify/${verificationtoken}`,
remote_address
})

Expand Down
11 changes: 5 additions & 6 deletions mod/user/update.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,18 @@ module.exports = async (req, res) => {
return res.status(500).send(error_message)
}

const protocol = `${req.headers.host.includes('localhost') && 'http' || 'https'}://`

const host = `${req.headers.host.includes('localhost') && req.headers.host || process.env.ALIAS || req.headers.host}${process.env.DIR}`
const host = `${req.headers.origin
|| req.headers.referer && new URL(req.headers.referer).origin
|| 'https://' + (process.env.ALIAS || req.headers.host)}${process.env.DIR}`

// Send email to the user account if an account has been approved.
if (req.params.field === 'approved' && req.params.value === 'true') {
if (req.params.field === 'approved' && req.params.value === true) {

await mailer({
template: 'approved_account',
language: req.params.user.language,
to: email,
host: host,
protocol: protocol
host: host
})
}

Expand Down
7 changes: 4 additions & 3 deletions mod/user/verify.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,14 @@ module.exports = async (req, res) => {
// Get array of mail promises.
const mail_promises = rows.map(async row => {

await mailer({
return await mailer({
template: 'admin_email',
language: row.language,
to: row.email,
email: user.email,
host: `${req.headers.host.includes('localhost') && req.headers.host || process.env.ALIAS || req.headers.host}${process.env.DIR}`,
protocol: `${req.headers.host.includes('localhost') && 'http' || 'https'}://`
host: `${req.headers.origin
|| req.headers.referer && new URL(req.headers.referer).origin
|| 'https://' + (process.env.ALIAS || req.headers.host)}${process.env.DIR}`
})
})

Expand Down
47 changes: 24 additions & 23 deletions mod/utils/mailer.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,41 +34,42 @@ module.exports = async params => {

const template = await languageTemplates(params)

await getBody(template)

const mailTemplate = {
to: params.to,
from: email,
sender: email,
subject: replaceStringParams(template.subject, params),
html: template.html ? replaceStringParams(template.html, params) : undefined,
text: template.text ? replaceStringParams(template.text, params) : undefined
}

const result = await transport.sendMail(mailTemplate).catch(err => console.error(err))

logger(result, 'mailer')
}

async function getBody(template){

if (template.text) {

// Prevent mail template from having text and html
delete template.html

if (Object.hasOwn(getFrom, template.text?.split(':')[0])) {
if (Object.hasOwn(getFrom, template.text.split(':')[0])) {

template.text = await getFrom[template.text.split(':')[0]](template.text)

if (!template.text) return;
template.text = await getFrom[template.text.split(':')[0]](template.text)
}

template.text = replaceStringParams(template.text, params)

template.text = template.text.replace(/^(?!\s+$)\s+/gm, '')
}

if (Object.hasOwn(getFrom, template.html?.split(':')[0])) {

template.html = await getFrom[template.html.split(':')[0]](template.html)
if (template.html) {

if (!template.text) return;
if (Object.hasOwn(getFrom, template.html.split(':')[0])) {

template.html = replaceStringParams(template.html, params)
template.html = await getFrom[template.html.split(':')[0]](template.html)
}
}

template.subject = replaceStringParams(template.subject, params)

template.to = params.to
template.from = email
template.sender = email

const result = await transport.sendMail(template).catch(err => console.error(err))

logger(result, 'mailer')
}

function replaceStringParams(string, params) {
Expand Down
Loading

0 comments on commit dab5321

Please sign in to comment.