diff --git a/app.js b/app.js index e448a6a534..9858ea7023 100644 --- a/app.js +++ b/app.js @@ -1,5 +1,5 @@ var categories = require('./src/categories.js'), - templates = require('./src/templates.js'), + templates = require('./public/src/templates.js'), webserver = require('./src/webserver.js'), websockets = require('./src/websockets.js'), fs = require('fs'); @@ -7,11 +7,15 @@ var categories = require('./src/categories.js'), DEVELOPMENT = true; global.configuration = {}; +global.templates = {}; (function(config) { config['ROOT_DIRECTORY'] = __dirname; templates.init(); + templates.ready(function() { + webserver.init(); + }); //setup scripts to be moved outside of the app in future. function setup_categories() { diff --git a/public/css/style.less b/public/css/style.less index cef80c0496..0974c0c47a 100644 --- a/public/css/style.less +++ b/public/css/style.less @@ -699,4 +699,11 @@ body .navbar .nodebb-inline-block { #right-menu{ float:right; +} + +#admin-redis-info { + span { + display:inline-block; + width:200px; + } } \ No newline at end of file diff --git a/public/src/ajaxify.js b/public/src/ajaxify.js index 94074c825f..d5d26b1cc1 100644 --- a/public/src/ajaxify.js +++ b/public/src/ajaxify.js @@ -39,7 +39,7 @@ var ajaxify = {}; tpl_url = url; } - if (templates[tpl_url]) { + if (templates[tpl_url] && !templates.force_refresh(tpl_url)) { if (quiet !== true) { window.history.pushState({ "url": url diff --git a/public/src/templates.js b/public/src/templates.js index f35f62140e..f030d94f5c 100644 --- a/public/src/templates.js +++ b/public/src/templates.js @@ -1,8 +1,21 @@ -var templates = {}; -(function() { + +(function (module) { + var ready_callback, - config = {}; + config = {}, + templates, + fs = null; + + module.exports = templates = {}; + + try { + fs = require('fs'); + } catch (e) {} + + templates.force_refresh = function(tpl) { + return !!config.force_refresh[tpl]; + } templates.get_custom_map = function(tpl) { if (config['custom_mapping'] && tpl) { @@ -12,11 +25,11 @@ var templates = {}; } } } + return false; } templates.ready = function(callback) { - //quick implementation because introducing a lib to handle several async callbacks if (callback == null && ready_callback) ready_callback(); else ready_callback = callback; }; @@ -26,51 +39,82 @@ var templates = {}; template.html = raw_tpl; template.parse = parse; template.blocks = {}; + return template; }; function loadTemplates(templatesToLoad) { - var timestamp = new Date().getTime(); - var loaded = templatesToLoad.length; + function loadServer() { + var loaded = templatesToLoad.length; - $.getJSON('/templates/config.json', function(data) { - config = data; - }); + for (var t in templatesToLoad) { + (function(file) { + fs.readFile(global.configuration.ROOT_DIRECTORY + '/public/templates/' + file + '.tpl', function(err, html) { + var template = function() { + this.toString = function() { + return this.html; + }; + } - 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.file = file; + template.prototype.parse = parse; + template.prototype.html = String(html); + + global.templates[file] = new template; - template.prototype.parse = parse; - template.prototype.html = String(html); - template.prototype.blocks = {}; - - templates[file] = new template; - - loaded--; - if (loaded == 0) templates.ready(); - }).fail(function() { - loaded--; - if (loaded == 0) templates.ready(); - }); - }(templatesToLoad[t])); + loaded--; + if (loaded == 0) templates.ready(); + }); + }(templatesToLoad[t])); + } } + + function loadClient() { + var timestamp = new Date().getTime(); + var loaded = templatesToLoad.length; + + jQuery.getJSON('/templates/config.json', function(data) { + config = data; + }); + + for (var t in templatesToLoad) { + (function(file) { + jQuery.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); + template.prototype.blocks = {}; + + templates[file] = new template; + + loaded--; + if (loaded == 0) templates.ready(); + }).fail(function() { + loaded--; + if (loaded == 0) templates.ready(); + }); + }(templatesToLoad[t])); + } + } + + if (fs === null) loadClient(); + else loadServer(); } - function init() { + templates.init = function() { loadTemplates([ 'header', 'footer', 'register', 'home', 'topic','account', 'category', 'users', 'accountedit', 'friends', 'login', 'reset', 'reset_code', 'account', - 'confirm', '403', + 'confirm', '403', 'logout', 'emails/reset', 'emails/reset_plaintext', 'emails/email_confirm', 'emails/email_confirm_plaintext', - 'admin/index', 'admin/categories', 'admin/users', 'admin/topics', 'admin/settings', 'admin/themes', 'admin/twitter', 'admin/facebook', 'admin/gplus' + 'admin/index', 'admin/categories', 'admin/users', 'admin/topics', 'admin/settings', 'admin/themes', 'admin/twitter', 'admin/facebook', 'admin/gplus', 'admin/redis' ]); } @@ -157,9 +201,17 @@ var templates = {}; } - init(); + + + if ('undefined' !== typeof window) { + window.templates = module.exports; + templates.init(); + } + +})('undefined' === typeof module ? {module:{exports:{}}} : module) + + -}()); function load_template(callback, url, template) { var location = document.location || window.location, @@ -184,4 +236,6 @@ function load_template(callback, url, template) { document.getElementById('content').innerHTML = templates[tpl].parse(JSON.parse(data)); if (callback) callback(); }); -} \ No newline at end of file +} + + diff --git a/public/templates/admin/header.tpl b/public/templates/admin/header.tpl index edcf59f843..a987384bb4 100644 --- a/public/templates/admin/header.tpl +++ b/public/templates/admin/header.tpl @@ -94,6 +94,8 @@