2021-01-29 08:30:05 +00:00
|
|
|
var template = template || {};
|
|
|
|
|
|
|
|
template.type = {};
|
|
|
|
template.type._entryMap = new Map();
|
|
|
|
template.type.register = (type, callback) => {
|
|
|
|
if (typeof type !== 'string') {
|
|
|
|
console.error(`'type' must be a string, recieved: `, type);
|
|
|
|
} else {
|
2021-01-30 00:52:25 +00:00
|
|
|
const TYPE_REGEX = /^(\w+)\s*(<\s*\?(?:\s*,\s*\?)*\s*>)?\s*$/;
|
|
|
|
const result = type.match(TYPE_REGEX);
|
|
|
|
if (result === null) {
|
|
|
|
console.error(`'${type}' is not a valid type id`);
|
2021-01-29 08:30:05 +00:00
|
|
|
} else {
|
2021-01-30 00:52:25 +00:00
|
|
|
if (result[2] === undefined) {
|
|
|
|
result[2] = 0;
|
|
|
|
} else {
|
|
|
|
result[2] = result[2].split(/\s*,\s*/).length;
|
|
|
|
}
|
|
|
|
const completeType = result[1] + ':' + result[2];
|
|
|
|
if (template.type._entryMap.get(completeType) === undefined) {
|
|
|
|
template.type._entryMap.set(completeType, async function() {
|
|
|
|
await callback.apply(null, Array.from(arguments));
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
console.error(`${type} is already a registered template!`);
|
|
|
|
}
|
2021-01-29 08:30:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-01-30 00:52:25 +00:00
|
|
|
// Courtesy of https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#escaping
|
|
|
|
function escapeRegExp(string) {
|
|
|
|
return string.replace(/[.*+?^${}()|\[\]\\]/g, '\\$&'); // $& means the whole matched string
|
|
|
|
}
|
|
|
|
|
2021-01-29 08:30:05 +00:00
|
|
|
/* Intrinsic Templates */
|
|
|
|
|
|
|
|
// Basic - Simple search and replace
|
2021-01-30 00:52:25 +00:00
|
|
|
template.type.register('Basic', (element, map) => {
|
|
|
|
let html = element.innerHTML;
|
|
|
|
function applyObject(object, path) {
|
|
|
|
for (const key in object) {
|
|
|
|
const regexKey = escapeRegExp(path + key);
|
|
|
|
html = html.replace(new RegExp(`(?:(?<!\\\\)\\\${${regexKey}})`, 'gm'), object[key]);
|
|
|
|
if (typeof object[key] === 'object') {
|
|
|
|
applyObject(object[key], path + key + '.');
|
|
|
|
}
|
|
|
|
}
|
2021-01-29 08:30:05 +00:00
|
|
|
}
|
2021-01-30 00:52:25 +00:00
|
|
|
applyObject(map, '');
|
|
|
|
html = html.replace('\\&', '&');
|
|
|
|
element.outerHTML = html.trim();
|
2021-01-29 08:30:05 +00:00
|
|
|
});
|
|
|
|
|
2021-01-30 00:52:25 +00:00
|
|
|
// Extern - Retrieve template from webserver
|
|
|
|
template.type.register('Extern', (element, name) => {
|
2021-01-29 08:30:05 +00:00
|
|
|
return fetch(`templates/${name}.html.template`, {
|
|
|
|
method: 'GET',
|
|
|
|
mode: 'no-cors',
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'text/plain'
|
|
|
|
}
|
|
|
|
}).then(response => response.text().then((data) => {
|
2021-01-30 00:52:25 +00:00
|
|
|
element.outerHTML = data;
|
2021-01-29 08:30:05 +00:00
|
|
|
})).catch(error => {
|
|
|
|
console.error(`failed to retrieve template '${name}': `, error);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
// List - Iterate over list and emit copy of child for each iteration
|
2021-01-30 00:52:25 +00:00
|
|
|
template.type.register('List<?>', async (element, subtype, arrayMap) => {
|
|
|
|
let cumulative = '';
|
|
|
|
const temp = document.createElement('template');
|
|
|
|
for (const obj of arrayMap) {
|
|
|
|
temp.innerHTML = `<template></template>`;
|
|
|
|
const child = temp.content.children[0];
|
|
|
|
child.innerHTML = element.innerHTML;
|
|
|
|
const callback = template.type._entryMap.get(subtype.type);
|
|
|
|
if (callback === undefined) {
|
|
|
|
cumulative = '';
|
|
|
|
console.error(`'${subtype.type}' is not a registered template type`);
|
|
|
|
} else {
|
|
|
|
await callback.apply(null, [ child, obj ]);
|
2021-01-29 08:30:05 +00:00
|
|
|
}
|
2021-01-30 00:52:25 +00:00
|
|
|
cumulative = cumulative + temp.innerHTML.trim();
|
2021-01-29 08:30:05 +00:00
|
|
|
}
|
2021-01-30 00:52:25 +00:00
|
|
|
element.outerHTML = cumulative;
|
2021-01-29 08:30:05 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
template._entryMap = new Map();
|
2021-01-30 00:52:25 +00:00
|
|
|
template.apply = function(pattern, promise) {
|
2021-01-29 08:30:05 +00:00
|
|
|
if (typeof pattern !== 'string') {
|
|
|
|
console.error('pattern must be a string, received: ', pattern);
|
|
|
|
} else {
|
2021-01-30 00:52:25 +00:00
|
|
|
return new template.apply.applicators(pattern);
|
2021-01-29 08:30:05 +00:00
|
|
|
}
|
|
|
|
};
|
2021-01-30 00:52:25 +00:00
|
|
|
template.apply.applicators = class {
|
|
|
|
constructor(pattern) {
|
|
|
|
this._pattern = pattern;
|
|
|
|
}
|
|
|
|
|
|
|
|
_apply(asyncArgs) {
|
|
|
|
template._entryMap.set(RegExp('^' + this._pattern + '$'), asyncArgs);
|
|
|
|
}
|
|
|
|
|
|
|
|
values(...args) {
|
|
|
|
this._apply(async () => Array.from(args));
|
|
|
|
}
|
|
|
|
|
|
|
|
promise(promise) {
|
2021-01-29 08:30:05 +00:00
|
|
|
let args = null;
|
2021-01-30 00:52:25 +00:00
|
|
|
promise = promise.then(data => args = [ data ]);
|
|
|
|
this._apply(async () => args || promise);
|
|
|
|
}
|
|
|
|
|
|
|
|
fetch(dataProcessor, url, options) {
|
|
|
|
if (typeof dataProcessor === 'string') {
|
|
|
|
const path = dataProcessor;
|
|
|
|
dataProcessor = data => {
|
|
|
|
for (const id of path.split(/\./)) {
|
|
|
|
data = data[id];
|
|
|
|
if (data === undefined) {
|
|
|
|
throw `invalid path '${path}'`;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return data;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
this.promise(
|
|
|
|
fetch(url, options || { method: 'GET', mode: 'cors' })
|
|
|
|
.then(response => response.json())
|
|
|
|
.then(data => dataProcessor(data))
|
|
|
|
);
|
2021-01-29 08:30:05 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
(() => {
|
2021-01-30 00:52:25 +00:00
|
|
|
const parseType = (type) => {
|
|
|
|
let result = type.match(/^\s*(\w+)\s*(?:<(.*)>)?\s*$/);
|
|
|
|
let id = result[1];
|
|
|
|
result = result[2] ? result[2].split(/\s*,\s*/).map(parseType) : [];
|
|
|
|
return { type: id + ':' + result.length, params: result };
|
|
|
|
};
|
|
|
|
|
2021-01-29 08:30:05 +00:00
|
|
|
const findTemplates = (element) =>
|
|
|
|
Array
|
2021-01-30 00:52:25 +00:00
|
|
|
.from(element.querySelectorAll('template'))
|
2021-01-29 08:30:05 +00:00
|
|
|
.filter(child => Boolean(child.dataset.template))
|
|
|
|
.map(child => {
|
|
|
|
const data = child.dataset.template.split(/\s*:\s*/);
|
|
|
|
return {
|
|
|
|
id: data[0],
|
2021-01-30 00:52:25 +00:00
|
|
|
typeCapture: parseType(data[1] || 'Begin'),
|
2021-01-29 08:30:05 +00:00
|
|
|
element: child
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
const expand = async (element) => {
|
|
|
|
let children = findTemplates(element);
|
|
|
|
let promises = [];
|
|
|
|
let parents = new Set();
|
|
|
|
for (const child of children) {
|
2021-01-30 00:52:25 +00:00
|
|
|
for (const [pattern, argsCallback] of template._entryMap) {
|
|
|
|
await argsCallback().then(args => {
|
2021-01-29 08:30:05 +00:00
|
|
|
if (pattern.test(child.id)) {
|
2021-01-30 00:52:25 +00:00
|
|
|
const callback = template.type._entryMap.get(child.typeCapture.type);
|
2021-01-29 08:30:05 +00:00
|
|
|
if (typeof callback !== 'function') {
|
2021-01-30 00:52:25 +00:00
|
|
|
console.error(`'${child.typeCapture.type}' is not a registered template type`);
|
2021-01-29 08:30:05 +00:00
|
|
|
} else {
|
|
|
|
let params = Array.from(args)
|
2021-01-30 00:52:25 +00:00
|
|
|
for (const subtype of child.typeCapture.params) {
|
|
|
|
params.unshift(subtype);
|
|
|
|
}
|
|
|
|
params.unshift(child.element);
|
2021-01-29 08:30:05 +00:00
|
|
|
let parent = child.element.parentElement;
|
|
|
|
if (!parents.has(parent)) {
|
|
|
|
parents.add(parent);
|
|
|
|
}
|
|
|
|
promises.push(callback.apply(null, params));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}).catch(error => {
|
|
|
|
console.error('failed to retrieve arguments: ', error);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
await Promise.all(promises);
|
|
|
|
promises = [];
|
|
|
|
for (const parent of parents) {
|
|
|
|
promises.push(expand(parent));
|
|
|
|
}
|
|
|
|
await Promise.all(promises);
|
2021-01-30 00:52:25 +00:00
|
|
|
};
|
2021-01-29 08:30:05 +00:00
|
|
|
|
|
|
|
template.expand = async () => expand(document.children[0]);
|
2021-01-30 00:52:25 +00:00
|
|
|
})();
|