diff --git a/public/src/ajaxify.js b/public/src/ajaxify.js index e3a5192071..0e881f0da2 100644 --- a/public/src/ajaxify.js +++ b/public/src/ajaxify.js @@ -18,17 +18,20 @@ var ajaxify = {}; current_state = url; window.history.pushState({}, url, "/" + url); - - jQuery('#content').fadeOut(100, function() { - content.innerHTML = templates[tpl_url]; - exec_body_scripts(content); - - ajaxify.enable(); - if (callback) { - callback(); - } + + jQuery('#content, #footer').fadeOut(150, function() { + //content.innerHTML = templates[tpl_url]; + load_template(function() { + exec_body_scripts(content); + + ajaxify.enable(); + if (callback) { + callback(); + } + + jQuery('#content, #footer').fadeIn(200); + }); - jQuery('#content').fadeIn(200); }); } @@ -111,4 +114,5 @@ var ajaxify = {}; evalScript(scripts[i]); } }; + }(jQuery)); \ No newline at end of file diff --git a/public/src/app.js b/public/src/app.js index 7d64a87537..435e667cb1 100644 --- a/public/src/app.js +++ b/public/src/app.js @@ -135,4 +135,7 @@ var socket, if (data.status === 'ok') alert('Logged out.'); }); }) + + + }()); diff --git a/public/src/templates.js b/public/src/templates.js index cbe94d1643..c43c30f1ac 100644 --- a/public/src/templates.js +++ b/public/src/templates.js @@ -1,19 +1,122 @@ var templates = {}; -function loadTemplates(templatesToLoad) { - var timestamp = new Date().getTime(); - - for (var t in templatesToLoad) { - (function(template) { - $.get('/templates/' + template + '.tpl?v=' + timestamp, function(html) { - templates[template] = html; - }); - }(templatesToLoad[t])); +(function() { + + function loadTemplates(templatesToLoad) { + var timestamp = new Date().getTime(); + + for (var t in templatesToLoad) { + (function(file) { + $.get('/templates/' + file + '.tpl?v=' + timestamp, function(html) { + var template = function() { + this.toString = function() { + return this.html; + }; + } + + template.prototype.parse = parse; + template.prototype.html = String(html); + + templates[file] = new template; + }); + }(templatesToLoad[t])); + } + } + + + function init() { + loadTemplates([ + 'header', 'footer', 'register', 'home', + 'login', 'reset', 'reset_code', 'account_settings', + 'logout', + 'emails/reset', 'emails/reset_plaintext' + ]); + } + + + //modified from https://github.com/psychobunny/dcp.templates + var parse = function(data) { + function replace(key, value, template) { + var searchRegex = new RegExp('{' + key + '}', 'g'); + return template.replace(searchRegex, value); + } + + function makeRegex(block) { + return new RegExp("[^]*", 'g'); + } + + function getBlock(regex, block, template) { + data = template.match(regex); + if (data == null) return; + + data = data[0] + .replace("", "") + .replace("", ""); + + return data; + } + + function setBlock(regex, block, template) { + return template.replace(regex, block); + } + + var template = this.html, regex, block; + + return (function parse(data, namespace, template) { + + for (var d in data) { + if (data.hasOwnProperty(d)) { + if (data[d] instanceof String || data[d] === null) { + continue; + } else if (data[d].constructor == Array) { + namespace += d; + + regex = makeRegex(d), + block = getBlock(regex, namespace, template) + if (block == null) continue; + + var numblocks = data[d].length - 1, i = 0, result = ""; + + do { + result += parse(data[d][i], namespace + '.', block); + } while (i++ < numblocks); + + template = setBlock(regex, result, template); + + } else if (data[d] instanceof Object) { + namespace += d + '.'; + + regex = makeRegex(d), + block = getBlock(regex, namespace, template) + if (block == null) continue; + + block = parse(data[d], namespace, block); + template = setBlock(regex, block, template); + } else { + template = replace(namespace + d, data[d], template); + } + } + } + + return template; + + })(data, "", template); } -} -function templates_init() { - loadTemplates(['register', 'home', 'login', 'reset']); -} -templates_init(); \ No newline at end of file + init(); + +}()); + +function load_template(callback) { + var location = document.location || window.location, + rootUrl = location.protocol + '//' + (location.hostname || location.host) + (location.port ? ':' + location.port : ''); + + var url = location.href.replace(rootUrl +'/', ''); + if (url == '') url = 'home'; + jQuery.get('api/' + url, function(data) { + + document.getElementById('content').innerHTML = templates[url].parse(JSON.parse(data)); + if (callback) callback(); + }); +} \ No newline at end of file diff --git a/public/templates/footer.tpl b/public/templates/footer.tpl index 9cea2835f6..472f5db1e0 100644 --- a/public/templates/footer.tpl +++ b/public/templates/footer.tpl @@ -2,7 +2,7 @@ -
+