]> git.mxchange.org Git - friendica.git/blob - view/js/addon-hooks.js
Merge pull request #5734 from nupplaphil/friendica-5716-followup
[friendica.git] / view / js / addon-hooks.js
1 /**
2  * @file addon-hooks.js
3  * @brief Provide a way for add-ons to register a JavaScript hook
4  */
5
6 var addon_hooks = {};
7
8 /**
9  * @brief Register a JavaScript hook to be called from other Javascript files
10  * @pre the .js file from which the hook will be called is included in the document response
11  * @param type which type of hook i.e. where should it be called along with other hooks of the same type
12  * @param hookfnstr name of the JavaScript function name that needs to be called
13  */
14 function Addon_registerHook(type, hookfnstr)
15 {
16         if (!addon_hooks.hasOwnProperty(type)) {
17                 addon_hooks[type] = [];
18         }
19
20         addon_hooks[type].push(hookfnstr);
21 }
22
23 /**
24  * @brief Call all registered hooks of a certain type, i.e. at the same point of the JavaScript code execution
25  * @param typeOfHook string indicating which type of hooks to be called among the registered hooks
26  */
27 function callAddonHooks(typeOfHook)
28 {
29         if (typeof addon_hooks !== 'undefined') {
30                 var myTypeOfHooks = addon_hooks[typeOfHook];
31                 if (typeof myTypeOfHooks !== 'undefined') {
32                         for (addon_hook_idx = 0; addon_hook_idx < myTypeOfHooks.length; addon_hook_idx++) {
33                                 var hookfnstr = myTypeOfHooks[addon_hook_idx];
34                                 var hookfn = window[hookfnstr];
35                                 if (typeof hookfn === "function") {
36                                         hookfn();
37                                 }
38                         }
39                 }
40         }
41 }