aboutsummaryrefslogtreecommitdiff
path: root/js/lib/events.js
blob: 358dd62803435ab5c1e4e4c6017ca2b4bc426217 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
elgg.provide('elgg.config.events');
elgg.provide('elgg.config.events.all');
elgg.provide('elgg.config.events.all.all');

elgg.register_event_handler = function(event, type, callback, priority) {
	elgg.assertTypeOf('string', event);
	elgg.assertTypeOf('string', event);
	elgg.assertTypeOf('function', callback);
	
	if (!event || !type) {
		return false;
	}
	
	elgg.provide('elgg.config.events.' + event + '.' + type);

	var events = elgg.config.events;
	
	if (!(events[event][type] instanceof elgg.ElggPriorityList)) {
		events[event][type] = new elgg.ElggPriorityList();
	}

	return events[event][type].insert(callback, priority);
};

elgg.trigger_event = function(event, type, object) {
	elgg.assertTypeOf('string', event);
	elgg.assertTypeOf('string', event);

	elgg.provide('elgg.config.events.' + event + '.' + type);
	elgg.provide('elgg.config.events.all.' + type);
	elgg.provide('elgg.config.events.' + event + '.all');
	elgg.provide('elgg.config.events.all.all');
	
	var events = elgg.config.events;
	
	var callEventHandler = function(handler) { 
		return handler(event, type, object) !== false; 
	};
	
	if (events[event][type] instanceof elgg.ElggPriorityList) {
		if (!events[event][type].every(callEventHandler)) {
			return false;
		}
	}
	
	if (events['all'][type] instanceof elgg.ElggPriorityList) {
		if (!events['all'][type].every(callEventHandler)) {
			return false;
		}
	}
	
	if (events[event]['all'] instanceof elgg.ElggPriorityList) {
		if (!events[event]['all'].every(callEventHandler)) {
			return false;
		}
	}
	
	if (events['all']['all'] instanceof elgg.ElggPriorityList) {
		if (!events['all']['all'].every(callEventHandler)) {
			return false;
		}
	}
		
	return true;
};