From a11103a3a7c5ab1f2f4cef785862219c5b881363 Mon Sep 17 00:00:00 2001 From: Pascal Deklerck Date: Wed, 13 Jun 2018 09:31:08 +0200 Subject: [PATCH] Code organisation and documentation of JS hook code --- view/js/addon-hooks.js | 28 ++++++++++++++++++++++++++++ view/js/main.js | 15 +-------------- 2 files changed, 29 insertions(+), 14 deletions(-) diff --git a/view/js/addon-hooks.js b/view/js/addon-hooks.js index 2530f10bea..8094a7d64a 100644 --- a/view/js/addon-hooks.js +++ b/view/js/addon-hooks.js @@ -1,5 +1,16 @@ +/** + * @file addon-hooks.js + * @brief Provide a way for add-ons to register a JavaScript hook + */ + var addon_hooks={}; +/** + * @brief Register a JavaScript hook to be called from other Javascript files + * @pre the .js file from which the hook will be called is included in the document response + * @param type which type of hook i.e. where should it be called along with other hooks of the same type + * @param hookfnstr name of the JavaScript function name that needs to be called + */ function Addon_registerHook( type, hookfnstr ) { if (!addon_hooks.hasOwnProperty(type)) { @@ -8,3 +19,20 @@ function Addon_registerHook( type, hookfnstr ) addon_hooks[type].push( hookfnstr ); } + +/** + * @brief Call all registered hooks of a certain type, i.e. at the same point of the JavaScript code execution + * @param typeOfHook string indicating which type of hooks to be called among the registered hooks + */ +function callAddonHooks( typeOfHook ) { + if (typeof addon_hooks !== 'undefined') { + var myTypeOfHooks = addon_hooks[typeOfHook]; + if (typeof myTypeOfHooks !== 'undefined') { + for (addon_hook_idx = 0; addon_hook_idx < myTypeOfHooks.length; addon_hook_idx++) { + var hookfnstr = myTypeOfHooks[addon_hook_idx]; + var hookfn = window[hookfnstr]; + if (typeof hookfn === "function") hookfn(); + } + } + } +} diff --git a/view/js/main.js b/view/js/main.js index f0bab12806..b5043601ce 100644 --- a/view/js/main.js +++ b/view/js/main.js @@ -395,19 +395,6 @@ function NavUpdate() { timer = setTimeout(NavUpdate, updateInterval); } -function callAddonHooks(typeOfHook) { - if (typeof addon_hooks !== 'undefined') { - var myTypeOfHooks = addon_hooks[typeOfHook]; - if (typeof myTypeOfHooks !== 'undefined') { - for (addon_hook_idx = 0; addon_hook_idx < myTypeOfHooks.length; addon_hook_idx++) { - var hookfnstr = myTypeOfHooks[addon_hook_idx]; - var hookfn = window[hookfnstr]; - if (typeof hookfn === "function") hookfn(); - } - } - } -} - function liveUpdate(src) { if ((src == null) || stopped || !profile_uid) { $('.like-rotator').hide(); return; @@ -473,7 +460,7 @@ function liveUpdate(src) { prev = ident; }); - callAddonHooks("postprocess"); + callAddonHooks("postprocess_liveupdate"); $('.like-rotator').hide(); if (commentBusy) {