From 1cdbd376ba2bc87c73aea71ab67b2db961391219 Mon Sep 17 00:00:00 2001 From: psychobunny Date: Fri, 17 May 2013 13:36:35 -0400 Subject: [PATCH 1/4] deprecated server side templates finally. code sharing between client side and server side templates; force_refresh parameter added to temasd deprecated server side templates finally. code sharing between client side and server side templates; force_refresh parameter added to templates config.json; created initialization fn for webserver --- app.js | 5 +- public/src/ajaxify.js | 2 +- public/src/templates.js | 126 +++++++++++++++++++++++++---------- public/templates/config.json | 3 + src/templates.js | 114 ------------------------------- src/webserver.js | 7 +- 6 files changed, 104 insertions(+), 153 deletions(-) delete mode 100644 src/templates.js diff --git a/app.js b/app.js index e448a6a534..6bc1b5d039 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'); @@ -12,6 +12,9 @@ global.configuration = {}; 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/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..4b73263524 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,49 +39,80 @@ 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' ]); @@ -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/config.json b/public/templates/config.json index df327ff93e..33cad7eda6 100644 --- a/public/templates/config.json +++ b/public/templates/config.json @@ -9,5 +9,8 @@ "latest": "category", "popular": "category", "active": "category" + }, + "force_refresh": { + "logout": true } } \ No newline at end of file diff --git a/src/templates.js b/src/templates.js deleted file mode 100644 index 383a3b4213..0000000000 --- a/src/templates.js +++ /dev/null @@ -1,114 +0,0 @@ -var fs = require('fs'); - - -// to be deprecated in favour of client-side only templates. - -(function(Templates) { - - global.templates = {}; - - function loadTemplates(templatesToLoad) { - 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; - }; - } - - template.prototype.file = file; - template.prototype.parse = parse; - template.prototype.html = String(html); - - global.templates[file] = new template; - }); - }(templatesToLoad[t])); - } - } - - Templates.init = function() { - loadTemplates([ - 'header', 'footer', 'register', 'home', 'topic', 'account', 'friends', - 'login', 'reset', 'reset_code', - '403', 'logout', - 'admin/header', 'admin/footer', 'admin/index', - 'emails/header', 'emails/footer', - 'emails/reset', 'emails/reset_plaintext', 'emails/email_confirm', 'emails/email_confirm_plaintext' - ]); - } - - 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) { - console.log(this.file + ' is being called on server side. Templates will be deprecated soon'); - if (Object.keys(data).length == 0) { - regex = makeRegex('[^]*'); - template = template.replace(regex, ''); - } - - 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); - } - -}(exports)); \ No newline at end of file diff --git a/src/webserver.js b/src/webserver.js index ad451882b2..2fc48b2f9c 100644 --- a/src/webserver.js +++ b/src/webserver.js @@ -20,7 +20,7 @@ var express = require('express'), (function(app) { - var templates = global.templates; + var templates = null; // Middlewares app.use(express.favicon()); // 2 args: string path and object options (i.e. expire time etc) @@ -38,6 +38,11 @@ var express = require('express'), key: 'express.sid' })); + + module.exports.init = function() { + templates = global.templates; + } + auth.initialize(app); app.use(function(req, res, next) { From e694751b7259d3da7cc28f9b77bed2958968f6d3 Mon Sep 17 00:00:00 2001 From: psychobunny Date: Fri, 17 May 2013 13:40:15 -0400 Subject: [PATCH 2/4] fixed templates bug in websockets --- src/websockets.js | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/src/websockets.js b/src/websockets.js index c2354231df..3d10d05718 100644 --- a/src/websockets.js +++ b/src/websockets.js @@ -6,8 +6,7 @@ var SocketIO = require('socket.io').listen(global.server,{log:false}), user = require('./user.js'), posts = require('./posts.js'), topics = require('./topics.js'), - categories = require('./categories.js'), - templates = require('./templates.js'); + categories = require('./categories.js'); (function(io) { var users = {}, @@ -50,10 +49,6 @@ var SocketIO = require('socket.io').listen(global.server,{log:false}), var uid = users[hs.sessionID]; - if (DEVELOPMENT === true) { - // refreshing templates - templates.init(); - } /*process.on('uncaughtException', function(err) { // handle the error safely From ba8e0ea4204cf0decc98b94bbda01cf15e8aa988 Mon Sep 17 00:00:00 2001 From: psychobunny Date: Fri, 17 May 2013 13:43:32 -0400 Subject: [PATCH 3/4] adding back global.templates to app.js --- app.js | 1 + 1 file changed, 1 insertion(+) diff --git a/app.js b/app.js index 6bc1b5d039..9858ea7023 100644 --- a/app.js +++ b/app.js @@ -7,6 +7,7 @@ var categories = require('./src/categories.js'), DEVELOPMENT = true; global.configuration = {}; +global.templates = {}; (function(config) { config['ROOT_DIRECTORY'] = __dirname; From dfdbc3bea218705f4a28d2a3c577e683d118ea85 Mon Sep 17 00:00:00 2001 From: Baris Usakli Date: Fri, 17 May 2013 14:22:34 -0400 Subject: [PATCH 4/4] admin/redis page --- public/css/style.less | 7 +++++++ public/src/templates.js | 2 +- public/templates/admin/header.tpl | 2 ++ public/templates/admin/redis.tpl | 23 +++++++++++++++++++++++ src/routes/admin.js | 31 +++++++++++++++++++++++++++++-- src/user.js | 2 +- 6 files changed, 63 insertions(+), 4 deletions(-) create mode 100644 public/templates/admin/redis.tpl 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/templates.js b/public/src/templates.js index f35f62140e..28015a169e 100644 --- a/public/src/templates.js +++ b/public/src/templates.js @@ -70,7 +70,7 @@ var templates = {}; 'login', 'reset', 'reset_code', 'account', 'confirm', '403', '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' ]); } 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 @@
  • Topics
  • Themes
  • Settings
  • +
  • Redis
  • +
  • Twitter
  • Facebook
  • diff --git a/public/templates/admin/redis.tpl b/public/templates/admin/redis.tpl new file mode 100644 index 0000000000..e52f021e02 --- /dev/null +++ b/public/templates/admin/redis.tpl @@ -0,0 +1,23 @@ +

    Redis

    +
    +
    + Redis Version {redis_version}
    +
    + Uptime in Seconds {uptime_in_seconds}
    + Uptime in Days {uptime_in_days}
    +
    + Connected Clients {connected_clients}
    + Connected Slaves {connected_slaves}
    + Blocked Clients {blocked_clients}
    +
    + + Used Memory {used_memory_human}
    + Memory Fragmentation Ratio {mem_fragmentation_ratio}
    +
    + Total Connections Received {total_connections_received}
    + Total Commands Processed {total_commands_processed}
    +
    + Keyspace Hits {keyspace_hits}
    + Keyspace Misses {keyspace_misses}
    +
    + diff --git a/src/routes/admin.js b/src/routes/admin.js index aeabfc6d48..7528e76452 100644 --- a/src/routes/admin.js +++ b/src/routes/admin.js @@ -1,16 +1,18 @@ var user = require('./../user.js'), - topics = require('./../topics.js'); + topics = require('./../topics.js'), + RDB = require('./../redis.js'); (function(Admin) { Admin.create_routes = function(app) { (function() { - var routes = ['categories', 'users', 'topics', 'settings', 'themes', 'twitter', 'facebook', 'gplus']; + var routes = ['categories', 'users', 'topics', 'settings', 'themes', 'twitter', 'facebook', 'gplus', 'redis']; for (var i=0, ii=routes.length; i