Elgg  Version 2.3
hooks.js
Go to the documentation of this file.
1 /*
2  * Javascript hook interface
3  */
4 
5 elgg.provide('elgg.config.hooks');
6 elgg.provide('elgg.config.instant_hooks');
7 elgg.provide('elgg.config.triggered_hooks');
8 
26 elgg.register_hook_handler = function(name, type, handler, priority) {
27  elgg.assertTypeOf('string', name);
28  elgg.assertTypeOf('string', type);
29  elgg.assertTypeOf('function', handler);
30 
31  if (!name || !type) {
32  return false;
33  }
34 
35  var hooks = elgg.config.hooks;
36 
37  elgg.provide([name, type], hooks);
38 
39  if (!(hooks[name][type] instanceof elgg.ElggPriorityList)) {
40  hooks[name][type] = new elgg.ElggPriorityList();
41  }
42 
43  // call if instant and already triggered.
44  if (elgg.is_instant_hook(name, type) && elgg.is_triggered_hook(name, type)) {
45  handler(name, type, null, null);
46  }
47 
48  return hooks[name][type].insert(handler, priority);
49 };
50 
75 elgg.trigger_hook = function(name, type, params, value) {
76  elgg.assertTypeOf('string', name);
77  elgg.assertTypeOf('string', type);
78 
79  // mark as triggered
80  elgg.set_triggered_hook(name, type);
81 
82  // default to null if unpassed
83  value = !elgg.isNullOrUndefined(value) ? value : null;
84 
85  var hooks = elgg.config.hooks,
86  tempReturnValue = null,
87  returnValue = value,
88  callHookHandler = function(handler) {
89  tempReturnValue = handler(name, type, params, returnValue);
90  if (!elgg.isNullOrUndefined(tempReturnValue)) {
91  returnValue = tempReturnValue;
92  }
93  };
94 
95  elgg.provide([name, type], hooks);
96  elgg.provide(['all', type], hooks);
97  elgg.provide([name, 'all'], hooks);
98  elgg.provide(['all', 'all'], hooks);
99 
100  var hooksList = [];
101 
102  if (name != 'all' && type != 'all') {
103  hooksList.push(hooks[name][type]);
104  }
105 
106  if (type != 'all') {
107  hooksList.push(hooks['all'][type]);
108  }
109 
110  if (name != 'all') {
111  hooksList.push(hooks[name]['all']);
112  }
113 
114  hooksList.push(hooks['all']['all']);
115 
116  hooksList.every(function(handlers) {
117  if (handlers instanceof elgg.ElggPriorityList) {
118  handlers.forEach(callHookHandler);
119  }
120  return true;
121  });
122 
123  return returnValue;
124 };
125 
139 elgg.register_instant_hook = function(name, type) {
140  elgg.assertTypeOf('string', name);
141  elgg.assertTypeOf('string', type);
142 
143  return elgg.push_to_object_array(elgg.config.instant_hooks, name, type);
144 };
145 
152 elgg.is_instant_hook = function(name, type) {
153  return elgg.is_in_object_array(elgg.config.instant_hooks, name, type);
154 };
155 
162 elgg.set_triggered_hook = function(name, type) {
163  return elgg.push_to_object_array(elgg.config.triggered_hooks, name, type);
164 };
165 
172 elgg.is_triggered_hook = function(name, type) {
173  return elgg.is_in_object_array(elgg.config.triggered_hooks, name, type);
174 };
175 
176 elgg.register_instant_hook('init', 'system');
177 elgg.register_instant_hook('ready', 'system');
178 elgg.register_instant_hook('boot', 'system');
elgg
Definition: install.js:23
list style type
Definition: admin.css.php:808
$site name