hooks.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /*
  2. * Javascript hook interface
  3. */
  4. elgg.provide('elgg.config.hooks');
  5. elgg.provide('elgg.config.instant_hooks');
  6. elgg.provide('elgg.config.triggered_hooks');
  7. /**
  8. * Registers a hook handler with the event system.
  9. *
  10. * The special keyword "all" can be used for either the name or the type or both
  11. * and means to call that handler for all of those hooks.
  12. *
  13. * Note that handlers registering for instant hooks will be executed immediately if the instant
  14. * hook has been previously triggered.
  15. *
  16. * @param {String} name Name of the plugin hook to register for
  17. * @param {String} type Type of the event to register for
  18. * @param {Function} handler Handle to call
  19. * @param {Number} priority Priority to call the event handler
  20. * @return {Bool}
  21. */
  22. elgg.register_hook_handler = function(name, type, handler, priority) {
  23. elgg.assertTypeOf('string', name);
  24. elgg.assertTypeOf('string', type);
  25. elgg.assertTypeOf('function', handler);
  26. if (!name || !type) {
  27. return false;
  28. }
  29. var priorities = elgg.config.hooks;
  30. elgg.provide(name + '.' + type, priorities);
  31. if (!(priorities[name][type] instanceof elgg.ElggPriorityList)) {
  32. priorities[name][type] = new elgg.ElggPriorityList();
  33. }
  34. // call if instant and already triggered.
  35. if (elgg.is_instant_hook(name, type) && elgg.is_triggered_hook(name, type)) {
  36. handler(name, type, null, null);
  37. }
  38. return priorities[name][type].insert(handler, priority);
  39. };
  40. /**
  41. * Emits a hook.
  42. *
  43. * Loops through all registered hooks and calls the handler functions in order.
  44. * Every handler function will always be called, regardless of the return value.
  45. *
  46. * @warning Handlers take the same 4 arguments in the same order as when calling this function.
  47. * This is different from the PHP version!
  48. *
  49. * @note Instant hooks do not support params or values.
  50. *
  51. * Hooks are called in this order:
  52. * specifically registered (event_name and event_type match)
  53. * all names, specific type
  54. * specific name, all types
  55. * all names, all types
  56. *
  57. * @param {String} name Name of the hook to emit
  58. * @param {String} type Type of the hook to emit
  59. * @param {Object} params Optional parameters to pass to the handlers
  60. * @param {Object} value Initial value of the return. Can be mangled by handlers
  61. *
  62. * @return {Bool}
  63. */
  64. elgg.trigger_hook = function(name, type, params, value) {
  65. elgg.assertTypeOf('string', name);
  66. elgg.assertTypeOf('string', type);
  67. // mark as triggered
  68. elgg.set_triggered_hook(name, type);
  69. // default to null if unpassed
  70. value = !elgg.isNullOrUndefined(value) ? value : null;
  71. var hooks = elgg.config.hooks,
  72. tempReturnValue = null,
  73. returnValue = value,
  74. callHookHandler = function(handler) {
  75. tempReturnValue = handler(name, type, params, returnValue);
  76. if (!elgg.isNullOrUndefined(tempReturnValue)) {
  77. returnValue = tempReturnValue;
  78. }
  79. };
  80. elgg.provide(name + '.' + type, hooks);
  81. elgg.provide('all.' + type, hooks);
  82. elgg.provide(name + '.all', hooks);
  83. elgg.provide('all.all', hooks);
  84. var hooksList = [];
  85. if (name != 'all' && type != 'all') {
  86. hooksList.push(hooks[name][type]);
  87. }
  88. if (type != 'all') {
  89. hooksList.push(hooks['all'][type]);
  90. }
  91. if (name != 'all') {
  92. hooksList.push(hooks[name]['all']);
  93. }
  94. hooksList.push(hooks['all']['all']);
  95. hooksList.every(function(handlers) {
  96. if (handlers instanceof elgg.ElggPriorityList) {
  97. handlers.forEach(callHookHandler);
  98. }
  99. return true;
  100. });
  101. return returnValue;
  102. };
  103. /**
  104. * Registers a hook as an instant hook.
  105. *
  106. * After being trigger once, registration of a handler to an instant hook will cause the
  107. * handle to be executed immediately.
  108. *
  109. * @note Instant hooks must be triggered without params or defaults. Any params or default
  110. * passed will *not* be passed to handlers executed upon registration.
  111. *
  112. * @param {String} name The hook name.
  113. * @param {String} type The hook type.
  114. * @return {Int}
  115. */
  116. elgg.register_instant_hook = function(name, type) {
  117. elgg.assertTypeOf('string', name);
  118. elgg.assertTypeOf('string', type);
  119. return elgg.push_to_object_array(elgg.config.instant_hooks, name, type);
  120. };
  121. /**
  122. * Is this hook registered as an instant hook?
  123. *
  124. * @param {String} name The hook name.
  125. * @param {String} type The hook type.
  126. */
  127. elgg.is_instant_hook = function(name, type) {
  128. return elgg.is_in_object_array(elgg.config.instant_hooks, name, type);
  129. };
  130. /**
  131. * Records that a hook has been triggered.
  132. *
  133. * @param {String} name The hook name.
  134. * @param {String} type The hook type.
  135. */
  136. elgg.set_triggered_hook = function(name, type) {
  137. return elgg.push_to_object_array(elgg.config.triggered_hooks, name, type);
  138. };
  139. /**
  140. * Has this hook been triggered yet?
  141. *
  142. * @param {String} name The hook name.
  143. * @param {String} type The hook type.
  144. */
  145. elgg.is_triggered_hook = function(name, type) {
  146. return elgg.is_in_object_array(elgg.config.triggered_hooks, name, type);
  147. };
  148. elgg.register_instant_hook('init', 'system');
  149. elgg.register_instant_hook('ready', 'system');
  150. elgg.register_instant_hook('boot', 'system');