diff options
author | ewinslow <ewinslow@36083f99-b078-4883-b0ff-0f9b5a30f544> | 2010-11-14 08:28:22 +0000 |
---|---|---|
committer | ewinslow <ewinslow@36083f99-b078-4883-b0ff-0f9b5a30f544> | 2010-11-14 08:28:22 +0000 |
commit | 72a4b251503eeb2ae4cc8efdea1f522817652406 (patch) | |
tree | dd611bd8cce5fbcbf1c398da5619287a7487ee9a | |
parent | aa5bb3f0bc17d858831412da82eef805ecafac89 (diff) | |
download | elgg-72a4b251503eeb2ae4cc8efdea1f522817652406.tar.gz elgg-72a4b251503eeb2ae4cc8efdea1f522817652406.tar.bz2 |
Refs #2538: Refactored and cleaned up ElggPriorityList and events
git-svn-id: http://code.elgg.org/elgg/trunk@7312 36083f99-b078-4883-b0ff-0f9b5a30f544
-rw-r--r-- | js/classes/ElggPriorityList.js | 63 | ||||
-rw-r--r-- | js/lib/events.js | 87 |
2 files changed, 75 insertions, 75 deletions
diff --git a/js/classes/ElggPriorityList.js b/js/classes/ElggPriorityList.js index 521fbbb64..324b07cac 100644 --- a/js/classes/ElggPriorityList.js +++ b/js/classes/ElggPriorityList.js @@ -1,60 +1,73 @@ +/** + * + */ elgg.ElggPriorityList = function() { this.length = 0; this.priorities_ = []; }; +/** + * + */ elgg.ElggPriorityList.prototype.insert = function(obj, opt_priority) { - if (opt_priority == undefined) { - opt_priority = 500; - } + var priority = parseInt(opt_priority || 500, 10); - opt_priority = parseInt(opt_priority); - if (opt_priority < 0) { - opt_priority = 0; - } - - if (this.priorities_[opt_priority] == undefined) { - this.priorities_[opt_priority] = []; + priority = Math.max(priority, 0); + + if (elgg.isUndefined(this.priorities_[priority])) { + this.priorities_[priority] = []; } - - this.priorities_[opt_priority].push(obj); + + this.priorities_[priority].push(obj); this.length++; }; +/** + * + */ elgg.ElggPriorityList.prototype.forEach = function(callback) { elgg.assertTypeOf('function', callback); - var index = 0; - for (var p in this.priorities_) { - var elems = this.priorities_[p]; - for (var i in elems) { + var index = 0, p, i, elems; + for (p in this.priorities_) { + elems = this.priorities_[p]; + for (i in elems) { callback(elems[i], index); index++; } } }; +/** + * + */ elgg.ElggPriorityList.prototype.every = function(callback) { elgg.assertTypeOf('function', callback); - - var index = 0; - for (var p in this.priorities_) { - var elems = this.priorities_[p]; - for (var i in elems) { + + var index = 0, p, elems, i; + + for (p in this.priorities_) { + elems = this.priorities_[p]; + for (i in elems) { if (!callback(elems[i], index)) { return false; - }; + } + index++; } } - + return true; }; +/** + * + */ elgg.ElggPriorityList.prototype.remove = function(obj) { - this.priorities_.forEach(function(elems, priority) { + this.priorities_.forEach(function(elems) { var index; - while ((index = elems.indexOf(obj)) != -1) { + while ((index = elems.indexOf(obj)) !== -1) { elems.splice(index, 1); + this.length--; } }); };
\ No newline at end of file diff --git a/js/lib/events.js b/js/lib/events.js index 358dd6280..ad05a9888 100644 --- a/js/lib/events.js +++ b/js/lib/events.js @@ -1,65 +1,52 @@ 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) { +/** + * + */ +elgg.register_event_handler = function(event_name, event_type, handler, priority) { + elgg.assertTypeOf('string', event_name); + elgg.assertTypeOf('string', event_type); + elgg.assertTypeOf('function', handler); + + if (!event_name || !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(); + elgg.provide(event_name + '.' + event_type, events); + + + if (!(events[event_name][event_type] instanceof elgg.ElggPriorityList)) { + events[event_name][event_type] = new elgg.ElggPriorityList(); } - return events[event][type].insert(callback, priority); + return events[event_name][event_type].insert(handler, priority); }; -elgg.trigger_event = function(event, type, object) { - elgg.assertTypeOf('string', event); - elgg.assertTypeOf('string', event); +/** + * + */ +elgg.trigger_event = function(event_name, event_type, opt_object) { + elgg.assertTypeOf('string', event_name); + elgg.assertTypeOf('string', event_type); - 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; + var events = elgg.config.events, + callEventHandler = function(handler) { + return handler(event_name, event_type, opt_object) !== 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; + elgg.provide(event_name + '.' + event_type, events); + elgg.provide('all.' + event_type, events); + elgg.provide(event_name + '.all', events); + elgg.provide('all.all', events); + + return [ + events[event_name][event_type], + events['all'][event_type], + events[event_name]['all'], + events['all']['all'] + ].every(function(handlers) { + return !(handlers instanceof elgg.ElggPriorityList) || handlers.every(callEventHandler); + }); };
\ No newline at end of file |