diff --git a/web/src/app/js/app.js b/web/src/app/js/app.js index cf05740..348fe3f 100644 --- a/web/src/app/js/app.js +++ b/web/src/app/js/app.js @@ -7,19 +7,37 @@ app.script = function(name, cback) { loadScript(name, file, cback); } -app.template = function(name, cback) { - if (window.__cache_templates === undefined) - window.__cache_templates = {}; - file = app.vars.js_dir + '/templates/' + - name.replaceAll('.', '/') + '.html'; - name = 'template__'+name.replaceAll('.', '-'); - if (window.__cache_templates[name] !== undefined) - return cback(window.__cache_templates[name]); - http.get(file, {}, function(html) { - html = html.trim(); - window.__cache_templates[name] = html; - return cback(html); - }); +app.template = { + load: function(name, cback) { + if (window.__cache_templates === undefined) + window.__cache_templates = {}; + file = app.vars.js_dir + '/templates/' + + name.replaceAll('.', '/') + '.html'; + name = 'template__'+name.replaceAll('.', '-'); + if (window.__cache_templates[name] !== undefined) + return cback(window.__cache_templates[name]); + http.get(file, {}, function(html) { + html = html.trim(); + window.__cache_templates[name] = html; + return cback(html); + }); + }, + fill: function(data, tpl) { + const res = Array.from(tpl.matchAll(/\{\.[^\{\}]+\}/g)); + var matches = []; + for (var i = 0; i < res.length; i++) { + const m = res[i][0]; + matches.push(m.replace(/^\{\./, '') + .replace(/\}$/, '')); + } + for (var i = 0; i < matches.length; i++) { + const k = matches[0]; + if (k.match(/^[a-zA-Z0-9_\.]+$/)) + tpl = tpl.replaceAll('{.'+k+'}', + eval(`data.${k}`)); + } + return tpl; + }, } function scriptPageHandler(modname, cfg) { @@ -33,10 +51,12 @@ function scriptPageHandler(modname, cfg) { } window.onload = function(e) { - scriptPageHandler('pages.home', { exact: '' }); - scriptPageHandler('pages.meet', { exact: 'meet' }); - scriptPageHandler('pages.crushes', { exact: 'crushes' }); - scriptPageHandler('pages.chat', { exact: 'chat' }); + if (getNormalizedURI() === app.vars.app_dir) { + scriptPageHandler('pages.home', { exact: '' }); + scriptPageHandler('pages.meet', { exact: 'meet' }); + scriptPageHandler('pages.crushes', { exact: 'crushes' }); + scriptPageHandler('pages.chat', { exact: 'chat' }); + } window.onhashchange(); } diff --git a/web/src/app/js/base.js b/web/src/app/js/base.js index 1743086..b09ac1b 100644 --- a/web/src/app/js/base.js +++ b/web/src/app/js/base.js @@ -29,6 +29,10 @@ function loadScript(id, file, cback) { document.body.appendChild(script); } +function getNormalizedURI() { + return window.location.pathname.replace(/\/+?$/, ''); +} + function JSON_to_URLEncoded(element,key,list) { var list = list || []; if (typeof(element) == 'object') {