diff options
author | href <href@random.sh> | 2021-09-01 10:30:18 +0200 |
---|---|---|
committer | href <href@random.sh> | 2021-09-01 10:30:18 +0200 |
commit | 75687711f35355bc30e4829439384aab28fcac6d (patch) | |
tree | 8f3256f472893c39720a684d390e890a152f7303 /priv/static/js/metricsgraphics/common/hooks.js | |
parent | link: post_* callbacks; html & pdftitle. (diff) |
Commit all the changes that hasn't been committed + updates.
Diffstat (limited to '')
-rw-r--r-- | priv/static/js/metricsgraphics/common/hooks.js | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/priv/static/js/metricsgraphics/common/hooks.js b/priv/static/js/metricsgraphics/common/hooks.js new file mode 100644 index 0000000..5f2adb5 --- /dev/null +++ b/priv/static/js/metricsgraphics/common/hooks.js @@ -0,0 +1,63 @@ +/** + Record of all registered hooks. + For internal use only. +*/ +MG._hooks = {}; + +/** + Add a hook callthrough to the stack. + + Hooks are executed in the order that they were registered. +*/ +MG.add_hook = function(name, func, context) { + var hooks; + + if (!MG._hooks[name]) { + MG._hooks[name] = []; + } + + hooks = MG._hooks[name]; + + var already_registered = + hooks.filter(function(hook) { + return hook.func === func; + }) + .length > 0; + + if (already_registered) { + throw 'That function is already registered.'; + } + + hooks.push({ + func: func, + context: context + }); +}; + +/** + Execute registered hooks. + + Optional arguments +*/ +MG.call_hook = function(name) { + var hooks = MG._hooks[name], + result = [].slice.apply(arguments, [1]), + processed; + + if (hooks) { + hooks.forEach(function(hook) { + if (hook.func) { + var params = processed || result; + + if (params && params.constructor !== Array) { + params = [params]; + } + + params = [].concat.apply([], params); + processed = hook.func.apply(hook.context, params); + } + }); + } + + return processed || result; +}; |