From 2cf55dcf9f1aab64c4edf02a9a9395c5d9b7f0e3 Mon Sep 17 00:00:00 2001 From: Julian Lam Date: Thu, 21 Nov 2013 12:28:10 -0500 Subject: [PATCH 1/2] added action:page.load hook --- public/src/ajaxify.js | 52 ++++++++++++++++++++++++++++++++----------- src/routes/plugins.js | 21 ++++++++++++++++- 2 files changed, 59 insertions(+), 14 deletions(-) diff --git a/public/src/ajaxify.js b/public/src/ajaxify.js index be38d66057..f233627fde 100644 --- a/public/src/ajaxify.js +++ b/public/src/ajaxify.js @@ -1,5 +1,6 @@ -var ajaxify = {}; +"use strict"; +var ajaxify = {}; (function ($) { /*global app, templates, utils*/ @@ -23,8 +24,9 @@ var ajaxify = {}; window.onpopstate = function (event) { // "quiet": If set to true, will not call pushState - if (event !== null && event.state && event.state.url !== undefined) + if (event !== null && event.state && event.state.url !== undefined) { ajaxify.go(event.state.url, null, null, true); + } }; var pagination; @@ -35,7 +37,10 @@ var ajaxify = {}; app.enter_room('global'); pagination = pagination || document.getElementById('pagination'); - if (pagination) pagination.parentNode.style.display = 'none'; + if (pagination) { + pagination.parentNode.style.display = 'none'; + } + window.onscroll = null; // end @@ -65,8 +70,20 @@ var ajaxify = {}; if (quiet !== true) { if (window.history && window.history.pushState) { window.history.pushState({ - "url": url - }, url, RELATIVE_PATH + "/" + url); + url: url + }, url, RELATIVE_PATH + '/' + url); + + $.ajax(RELATIVE_PATH + '/plugins/fireHook', { + type: 'PUT', + data: { + _csrf: $('#csrf_token').val(), + hook: 'page.load', + args: { + template: tpl_url, + url: '/' + url + } + } + }); } } @@ -78,7 +95,9 @@ var ajaxify = {}; templates.load_template(function () { exec_body_scripts(content); require(['forum/' + tpl_url], function(script) { - if (script && script.init) script.init(); + if (script && script.init) { + script.init(); + } }); if (callback) { @@ -88,10 +107,13 @@ var ajaxify = {}; app.process_page(); jQuery('#content, #footer').stop(true, true).fadeIn(200, function () { - if (window.location.hash) + if (window.location.hash) { hash = window.location.hash; - if (hash) + } + + if (hash) { app.scrollToPost(hash.substr(1)); + } }); utils.refreshTitle(url); @@ -105,23 +127,27 @@ var ajaxify = {}; }; $('document').ready(function () { - if (!window.history || !window.history.pushState) return; // no ajaxification for old browsers + if (!window.history || !window.history.pushState) { + return; // no ajaxification for old browsers + } content = content || document.getElementById('content'); // Enhancing all anchors to ajaxify... $(document.body).on('click', 'a', function (e) { function hrefEmpty(href) { - return href == 'javascript:;' || href == window.location.href + "#" || href.slice(-1) === "#"; + return href === 'javascript:;' || href === window.location.href + "#" || href.slice(-1) === "#"; } - if (hrefEmpty(this.href) || this.target !== '' || this.protocol === 'javascript:') + if (hrefEmpty(this.href) || this.target !== '' || this.protocol === 'javascript:') { return; + } - if(!window.location.pathname.match(/\/(403|404)$/g)) + if(!window.location.pathname.match(/\/(403|404)$/g)) { app.previousUrl = window.location.href; + } - if (this.getAttribute('data-ajaxify') == 'false') { + if (this.getAttribute('data-ajaxify') === 'false') { return; } diff --git a/src/routes/plugins.js b/src/routes/plugins.js index 33e384e8d2..d4243426ed 100644 --- a/src/routes/plugins.js +++ b/src/routes/plugins.js @@ -1,9 +1,28 @@ +"use strict"; + var nconf = require('nconf'), path = require('path'), fs = require('fs'), Plugins = require('../plugins'), PluginRoutes = function(app) { + app.get('/plugins/fireHook', function(req, res) { + // GET = filter + Plugins.fireHook('filter:' + req.query.hook, req.query.args, function(err, returnData) { + if (typeof returnData === 'object') { + res.json(200, returnData); + } else { + res.send(200, returnData); + } + }); + }); + + app.put('/plugins/fireHook', function(req, res) { + // PUT = action + Plugins.fireHook('action:' + req.body.hook, req.body.args); + res.send(200); + }); + // Static Assets app.get('/plugins/:id/*', function(req, res) { var relPath = req.url.replace('/plugins/' + req.params.id, ''); @@ -15,7 +34,7 @@ var nconf = require('nconf'), } else { res.redirect('/404'); } - }) + }); } else { res.redirect('/404'); } From a9ce8393e41fae83b3962723961595e8ffd6da5e Mon Sep 17 00:00:00 2001 From: Julian Lam Date: Thu, 21 Nov 2013 15:05:45 -0500 Subject: [PATCH 2/2] added footer.build and page.load hooks --- app.js | 4 +++- public/src/ajaxify.js | 3 ++- public/templates/footer.tpl | 1 + src/plugins.js | 2 +- src/webserver.js | 6 +++++- 5 files changed, 12 insertions(+), 4 deletions(-) diff --git a/app.js b/app.js index e3e8457545..374c198bf9 100644 --- a/app.js +++ b/app.js @@ -113,7 +113,9 @@ ], customTemplates); - templates.ready(webserver.init); + plugins.ready(function() { + templates.ready(webserver.init); + }); Notifications.init(); } else { diff --git a/public/src/ajaxify.js b/public/src/ajaxify.js index f233627fde..1052d03541 100644 --- a/public/src/ajaxify.js +++ b/public/src/ajaxify.js @@ -80,7 +80,8 @@ var ajaxify = {}; hook: 'page.load', args: { template: tpl_url, - url: '/' + url + url: url, + uid: app.uid } } }); diff --git a/public/templates/footer.tpl b/public/templates/footer.tpl index 85343f1e32..de5c022e5b 100644 --- a/public/templates/footer.tpl +++ b/public/templates/footer.tpl @@ -60,6 +60,7 @@ diff --git a/src/plugins.js b/src/plugins.js index 97394884db..029a666183 100644 --- a/src/plugins.js +++ b/src/plugins.js @@ -284,7 +284,7 @@ var fs = require('fs'), winston.warn("Plugin: " + file + " is corrupted or invalid. Please check plugin.json for errors.") return next(err, null); } - + _self.isActive(config.id, function(err, active) { if (err) next(new Error('no-active-state')); diff --git a/src/webserver.js b/src/webserver.js index cb0613b60d..c6ddb36d02 100644 --- a/src/webserver.js +++ b/src/webserver.js @@ -309,7 +309,11 @@ var path = require('path'), // translate all static templates served by webserver here. ex. footer, logout translator.translate(templates.footer.toString(), function(parsedTemplate) { - templates.footer = parsedTemplate; + plugins.fireHook('filter:footer.build', '', function(err, appendHTML) { + templates.footer = templates.footer.parse({ + footerHTML: appendHTML + }); + }); }); translator.translate(templates.logout.toString(), function(parsedTemplate) { templates.logout = parsedTemplate;