Elgg  Version 1.11
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 
24 elgg.register_hook_handler = function(name, type, handler, priority) {
25  elgg.assertTypeOf('string', name);
26  elgg.assertTypeOf('string', type);
27  elgg.assertTypeOf('function', handler);
28 
29  if (!name || !type) {
30  return false;
31  }
32 
33  var priorities = elgg.config.hooks;
34 
35  elgg.provide(name + '.' + type, priorities);
36 
37  if (!(priorities[name][type] instanceof elgg.ElggPriorityList)) {
38  priorities[name][type] = new elgg.ElggPriorityList();
39  }
40 
41  // call if instant and already triggered.
42  if (elgg.is_instant_hook(name, type) && elgg.is_triggered_hook(name, type)) {
43  handler(name, type, null, null);
44  }
45 
46  return priorities[name][type].insert(handler, priority);
47 };
48 
73 elgg.trigger_hook = function(name, type, params, value) {
74  elgg.assertTypeOf('string', name);
75  elgg.assertTypeOf('string', type);
76 
77  // mark as triggered
78  elgg.set_triggered_hook(name, type);
79 
80  // default to null if unpassed
81  value = !elgg.isNullOrUndefined(value) ? value : null;
82 
83  var hooks = elgg.config.hooks,
84  tempReturnValue = null,
85  returnValue = value,
86  callHookHandler = function(handler) {
87  tempReturnValue = handler(name, type, params, returnValue);
88  if (!elgg.isNullOrUndefined(tempReturnValue)) {
89  returnValue = tempReturnValue;
90  }
91  };
92 
93  elgg.provide(name + '.' + type, hooks);
94  elgg.provide('all.' + type, hooks);
95  elgg.provide(name + '.all', hooks);
96  elgg.provide('all.all', hooks);
97 
98  var hooksList = [];
99 
100  if (name != 'all' && type != 'all') {
101  hooksList.push(hooks[name][type]);
102  }
103 
104  if (type != 'all') {
105  hooksList.push(hooks['all'][type]);
106  }
107 
108  if (name != 'all') {
109  hooksList.push(hooks[name]['all']);
110  }
111 
112  hooksList.push(hooks['all']['all']);
113 
114  hooksList.every(function(handlers) {
115  if (handlers instanceof elgg.ElggPriorityList) {
116  handlers.forEach(callHookHandler);
117  }
118  return true;
119  });
120 
121  return returnValue;
122 };
123 
137 elgg.register_instant_hook = function(name, type) {
138  elgg.assertTypeOf('string', name);
139  elgg.assertTypeOf('string', type);
140 
141  return elgg.push_to_object_array(elgg.config.instant_hooks, name, type);
142 };
143 
150 elgg.is_instant_hook = function(name, type) {
151  return elgg.is_in_object_array(elgg.config.instant_hooks, name, type);
152 };
153 
160 elgg.set_triggered_hook = function(name, type) {
161  return elgg.push_to_object_array(elgg.config.triggered_hooks, name, type);
162 };
163 
170 elgg.is_triggered_hook = function(name, type) {
171  return elgg.is_in_object_array(elgg.config.triggered_hooks, name, type);
172 };
173 
174 elgg.register_instant_hook('init', 'system');
175 elgg.register_instant_hook('ready', 'system');
176 elgg.register_instant_hook('boot', 'system');
elgg
Definition: install.js:23
$site name
list style type
Definition: admin.php:748