-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvanilla.js
More file actions
68 lines (62 loc) · 2.14 KB
/
vanilla.js
File metadata and controls
68 lines (62 loc) · 2.14 KB
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
/**
* Returns the 1st HTMLElement or <null>, which corresponds to the <query>,
* @param query - CSS selector as a string
* @returns {HTMLElement} - the 1st element found
*/
const $$ = query => Array.from(document.querySelectorAll(query))
/**
* Binds an event handler (=function) to a DOM element
* @param element - the target element, e.g. button
* @param event - the event, e.g. 'click'
* @param func - the function to be called, e.g. handleValidation
* @returns {*} - the target element
*/
const $on = (element, event, func) => {
Array.isArray(element)
? element.forEach(arrayElement => $on(arrayElement, event, func))
: element.addEventListener(event, func)
return element
}
/**
* Runs through the HTML document and renders all handlebars script tags
* @param data - the data to be rendered
* @param querySelector - an optional querySelector
* @returns {Promise<void>}
*/
const render = async (data, querySelector) => {
Handlebars.registerHelper('toFixed', function(num) {
return num && num.toFixed(2);
});
const selector = querySelector || '[type="text/x-handlebars-template"]'
const templates = $$(selector)
for (const source of templates) {
await loadPartials(source)
const template = Handlebars.compile(source.innerHTML)
const target = source.parentElement
// remove former HTML elements
if (target.children.length > 1) {
const start = querySelector? 0 : 1
for (let i = start; i < target.children.length; i++) {
target.lastElementChild.remove()
}
}
// insert refreshed HTML elements
target.insertAdjacentHTML('beforeend', template(data))
}
}
/**
* Loads partials with the file extension '.html' from the same directory
* @param code - the source code to be parsed
* @returns {Promise<void>}
*/
async function loadPartials(code) {
const partialNames = code.innerText.match(/(?<={{(#>|>)).+?(?=\s)/g)
if (partialNames) {
for (let name of partialNames) {
name = name.trim()
const fileName = name + '.html'
const partialCode = await fetch(fileName).then(response => response.text())
Handlebars.registerPartial(name, partialCode)
}
}
}