]> git.mxchange.org Git - friendica.git/commitdiff
Rewrite JS hooks
authorHypolite Petovan <mrpetovan@gmail.com>
Thu, 20 Sep 2018 02:51:51 +0000 (22:51 -0400)
committerHypolite Petovan <mrpetovan@gmail.com>
Thu, 20 Sep 2018 02:51:51 +0000 (22:51 -0400)
- Use event listeners instead of homebrew hooks
- Remove view/js/addon-hooks.js and its references
- Update Addon docs

doc/Addons.md
view/js/addon-hooks.js [deleted file]
view/js/main.js
view/templates/head.tpl
view/theme/frio/templates/head.tpl

index 2465db7307f0654cf5ffa400c5fd48a2312e9ab7..ec413c6ac89a6df88ba6114e0fabdcf0062f06f7 100644 (file)
@@ -103,19 +103,11 @@ function <addon_name>_template_vars($a, &$arr)
 Register your addon hooks in file `addon/*addon_name*/*addon_name*.js`.
 
 ```js
-Addon_registerHook(type, hookfnstr);
+document.addEventListener(name, callback);
 ```
 
-*type* is the name of the hook and corresponds to a known Friendica JavaScript hook.
-*hookfnstr* is the name of your JavaScript function to execute.
-
-No arguments are provided to your JavaScript callback function. Example:
-
-```javascript
-function myhook_function() {
-
-}
-```
+*name* is the name of the hook and corresponds to a known Friendica JavaScript hook.
+*callback* is a JavaScript function to execute.
 
 ## Modules
 
@@ -704,4 +696,4 @@ Here is a complete list of all hook callbacks with file locations (as of 01-Apr-
 
 ### view/js/main.js
 
-    callAddonHooks("postprocess_liveupdate");
+    document.dispatchEvent(new Event('postprocess_liveupdate'));
diff --git a/view/js/addon-hooks.js b/view/js/addon-hooks.js
deleted file mode 100644 (file)
index 3e1cb48..0000000
+++ /dev/null
@@ -1,41 +0,0 @@
-/**
- * @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)) {
-               addon_hooks[type] = [];
-       }
-
-       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();
-                               }
-                       }
-               }
-       }
-}
index 4788d90a830cc6e4296c024d66d956253b42024f..ae9cb23d8e1e41b2c42fa8290a6a22a8778d13a1 100644 (file)
@@ -478,14 +478,12 @@ function liveUpdate(src) {
                $('.wall-item-body', data).imagesLoaded(function() {
                        updateConvItems(data);
 
+                       document.dispatchEvent(new Event('postprocess_liveupdate'));
+
                        // Update the scroll position.
                        $(window).scrollTop($(window).scrollTop() + $("section").height() - orgHeight);
                });
-
-               callAddonHooks("postprocess_liveupdate");
-
        });
-
 }
 
 function imgbright(node) {
@@ -735,6 +733,8 @@ function loadScrollContent() {
                } else {
                        $("#scroll-end").fadeIn('normal');
                }
+
+               document.dispatchEvent(new Event('postprocess_liveupdate'));
        });
 }
 
index aadbfcd8eeb9e944e8d64ea951d514cde77617bb..eef985bc6f36cee453f07ec7385593afb548295a 100644 (file)
@@ -45,7 +45,6 @@
 <script type="text/javascript" src="view/asset/imagesloaded/imagesloaded.pkgd.min.js"></script>
 <script type="text/javascript" src="view/js/acl.js" ></script>
 <script type="text/javascript" src="view/asset/base64/base64.min.js" ></script>
-<script type="text/javascript" src="view/js/addon-hooks.js" ></script>
 {{if is_array($addon_hooks)}}
 {{foreach $addon_hooks as $addon_hook}}
 <script type="text/javascript" src="addon/{{$addon_hook}}/{{$addon_hook}}.js"></script>
index 7d6cadea94fad12936a587ec387055a5842a6bba..0cc5bf4af8d785b40390172cc0e9c0c6c9a8303a 100644 (file)
@@ -69,7 +69,6 @@
 <script type="text/javascript" src="view/asset/imagesloaded/imagesloaded.pkgd.min.js"></script>
 <script type="text/javascript" src="view/js/acl.js"></script>
 <script type="text/javascript" src="view/asset/base64/base64.min.js"></script>
-<script type="text/javascript" src="view/js/addon-hooks.js" ></script>
 {{if is_array($addon_hooks)}}
 {{foreach $addon_hooks as $addon_hook}}
 <script type="text/javascript" src="addon/{{$addon_hook}}/{{$addon_hook}}.js"></script>