diff --git a/Gruntfile.js b/Gruntfile.js index 99edc4c1f..a346a8c82 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -137,6 +137,7 @@ module.exports = function(grunt) { "packages/node_modules/@node-red/editor-client/src/js/jquery-addons.js", "packages/node_modules/@node-red/editor-client/src/js/red.js", "packages/node_modules/@node-red/editor-client/src/js/events.js", + "packages/node_modules/@node-red/editor-client/src/js/hooks.js", "packages/node_modules/@node-red/editor-client/src/js/i18n.js", "packages/node_modules/@node-red/editor-client/src/js/settings.js", "packages/node_modules/@node-red/editor-client/src/js/user.js", diff --git a/packages/node_modules/@node-red/editor-client/src/js/hooks.js b/packages/node_modules/@node-red/editor-client/src/js/hooks.js new file mode 100644 index 000000000..0f8a9e1a1 --- /dev/null +++ b/packages/node_modules/@node-red/editor-client/src/js/hooks.js @@ -0,0 +1,156 @@ +RED.hooks = (function() { + + var VALID_HOOKS = [ + + ] + + var hooks = { } + var labelledHooks = { } + + function add(hookId, callback) { + var parts = hookId.split("."); + var id = parts[0], label = parts[1]; + + // if (VALID_HOOKS.indexOf(id) === -1) { + // throw new Error("Invalid hook '"+id+"'"); + // } + if (label && labelledHooks[label] && labelledHooks[label][id]) { + throw new Error("Hook "+hookId+" already registered") + } + var hookItem = {cb:callback, previousHook: null, nextHook: null } + + var tailItem = hooks[id]; + if (tailItem === undefined) { + hooks[id] = hookItem; + } else { + while(tailItem.nextHook !== null) { + tailItem = tailItem.nextHook + } + tailItem.nextHook = hookItem; + hookItem.previousHook = tailItem; + } + + if (label) { + labelledHooks[label] = labelledHooks[label]||{}; + labelledHooks[label][id] = hookItem; + } + } + function remove(hookId) { + var parts = hookId.split("."); + var id = parts[0], label = parts[1]; + if ( !label) { + throw new Error("Cannot remove hook without label: "+hookId) + } + if (labelledHooks[label]) { + if (id === "*") { + // Remove all hooks for this label + var hookList = Object.keys(labelledHooks[label]); + for (var i=0;i