Elgg  Version 4.3
hooks.js
Go to the documentation of this file.
1 /*
2  * Javascript hook interface
3  */
4 
5 !function() {
6  // counter for tracking registration order
7  var index = 0;
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 hooks = elgg.config.hooks;
34 
35  elgg.provide([name, type], hooks);
36 
37  if (!hooks[name][type].length) {
38  hooks[name][type] = [];
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  hooks[name][type].push({
47  priority: priority,
48  index: index++,
49  handler: handler
50  });
51  return true;
52  }
53 }();
54 
62 elgg.push_to_object_array = function(object, parent, value) {
63  elgg.assertTypeOf('object', object);
64  elgg.assertTypeOf('string', parent);
65 
66  if (!(object[parent] instanceof Array)) {
67  object[parent] = [];
68  }
69 
70  if ($.inArray(value, object[parent]) < 0) {
71  return object[parent].push(value);
72  }
73 
74  return false;
75 };
76 
84 elgg.is_in_object_array = function(object, parent, value) {
85  elgg.assertTypeOf('object', object);
86  elgg.assertTypeOf('string', parent);
87 
88  return typeof(object[parent]) != 'undefined' && $.inArray(value, object[parent]) >= 0;
89 };
90 
111 elgg.trigger_hook = function(name, type, params, value) {
112  elgg.assertTypeOf('string', name);
113  elgg.assertTypeOf('string', type);
114 
115  // mark as triggered
116  elgg.set_triggered_hook(name, type);
117 
118  // default to null if unpassed
119  value = (value != null) ? value : null;
120 
121  var hooks = elgg.config.hooks,
122  registrations = [],
123  push = Array.prototype.push;
124 
125  elgg.provide([name, type], hooks);
126  elgg.provide(['all', type], hooks);
127  elgg.provide([name, 'all'], hooks);
128  elgg.provide(['all', 'all'], hooks);
129 
130  if (hooks[name][type].length) {
131  if (name !== 'all' && type !== 'all') {
132  push.apply(registrations, hooks[name][type]);
133  }
134  }
135  if (hooks['all'][type].length) {
136  if (type !== 'all') {
137  push.apply(registrations, hooks['all'][type]);
138  }
139  }
140  if (hooks[name]['all'].length) {
141  if (name !== 'all') {
142  push.apply(registrations, hooks[name]['all']);
143  }
144  }
145  if (hooks['all']['all'].length) {
146  push.apply(registrations, hooks['all']['all']);
147  }
148 
149  registrations.sort(function (a, b) {
150  // priority first
151  if (a.priority < b.priority) {
152  return -1;
153  }
154  if (a.priority > b.priority) {
155  return 1;
156  }
157 
158  // then insertion order
159  return (a.index < b.index) ? -1 : 1;
160  });
161 
162  // only synchronous handlers
163  $.each(registrations, function (i, registration) {
164  var handler_return = registration.handler(name, type, params, value);
165  if (handler_return != null) {
166  value = handler_return;
167  }
168  });
169 
170  return value;
171 };
172 
186 elgg.register_instant_hook = function(name, type) {
187  elgg.assertTypeOf('string', name);
188  elgg.assertTypeOf('string', type);
189 
190  return elgg.push_to_object_array(elgg.config.instant_hooks, name, type);
191 };
192 
199 elgg.is_instant_hook = function(name, type) {
200  return elgg.is_in_object_array(elgg.config.instant_hooks, name, type);
201 };
202 
209 elgg.set_triggered_hook = function(name, type) {
210  return elgg.push_to_object_array(elgg.config.triggered_hooks, name, type);
211 };
212 
219 elgg.is_triggered_hook = function(name, type) {
220  return elgg.is_in_object_array(elgg.config.triggered_hooks, name, type);
221 };
222 
223 elgg.register_instant_hook('init', 'system');
224 elgg.register_instant_hook('ready', 'system');
225 elgg.register_instant_hook('boot', 'system');
$site name
Definition: settings.php:21
if(!$enabled) if(PHP_SAPI!== 'cli')
$data value
Definition: default.php:27
return['admin/delete_admin_notices'=>['access'=> 'admin'],'admin/menu/save'=>['access'=> 'admin'],'admin/plugins/activate'=>['access'=> 'admin'],'admin/plugins/activate_all'=>['access'=> 'admin'],'admin/plugins/deactivate'=>['access'=> 'admin'],'admin/plugins/deactivate_all'=>['access'=> 'admin'],'admin/plugins/set_priority'=>['access'=> 'admin'],'admin/security/settings'=>['access'=> 'admin'],'admin/security/regenerate_site_secret'=>['access'=> 'admin'],'admin/site/cache/invalidate'=>['access'=> 'admin'],'admin/site/flush_cache'=>['access'=> 'admin'],'admin/site/icons'=>['access'=> 'admin'],'admin/site/set_maintenance_mode'=>['access'=> 'admin'],'admin/site/set_robots'=>['access'=> 'admin'],'admin/site/unlock_upgrade'=>['access'=> 'admin'],'admin/site/settings'=>['access'=> 'admin'],'admin/upgrade'=>['access'=> 'admin'],'admin/upgrade/reset'=>['access'=> 'admin'],'admin/user/ban'=>['access'=> 'admin'],'admin/user/bulk/ban'=>['access'=> 'admin'],'admin/user/bulk/delete'=>['access'=> 'admin'],'admin/user/bulk/unban'=>['access'=> 'admin'],'admin/user/bulk/validate'=>['access'=> 'admin'],'admin/user/change_email'=>['access'=> 'admin'],'admin/user/delete'=>['access'=> 'admin'],'admin/user/login_as'=>['access'=> 'admin'],'admin/user/logout_as'=>[],'admin/user/makeadmin'=>['access'=> 'admin'],'admin/user/resetpassword'=>['access'=> 'admin'],'admin/user/removeadmin'=>['access'=> 'admin'],'admin/user/unban'=>['access'=> 'admin'],'admin/user/validate'=>['access'=> 'admin'],'annotation/delete'=>[],'avatar/upload'=>[],'comment/save'=>[],'diagnostics/download'=>['access'=> 'admin'],'entity/delete'=>[],'entity/mute'=>[],'entity/subscribe'=>[],'entity/unmute'=>[],'entity/unsubscribe'=>[],'login'=>['access'=> 'logged_out'],'logout'=>[],'notifications/mute'=>['access'=> 'public'],'plugins/settings/remove'=>['access'=> 'admin'],'plugins/settings/save'=>['access'=> 'admin'],'plugins/usersettings/save'=>[],'river/delete'=>[],'settings/notifications'=>[],'settings/notifications/subscriptions'=>[],'user/changepassword'=>['access'=> 'public'],'user/requestnewpassword'=>['access'=> 'public'],'useradd'=>['access'=> 'admin'],'usersettings/save'=>[],'widgets/add'=>[],'widgets/delete'=>[],'widgets/move'=>[],'widgets/save'=>[],]
Definition: actions.php:62
var elgg
Definition: elgglib.js:4