aboutsummaryrefslogtreecommitdiff
path: root/engine/js
diff options
context:
space:
mode:
Diffstat (limited to 'engine/js')
-rw-r--r--engine/js/classes/ElggPriorityList.js60
-rw-r--r--engine/js/lib/elgglib.js19
-rw-r--r--engine/js/lib/events.js65
-rw-r--r--engine/js/lib/languages.js17
-rw-r--r--engine/js/lib/security.js14
-rw-r--r--engine/js/lib/ui.js9
-rw-r--r--engine/js/lib/ui.widgets.js4
-rw-r--r--engine/js/tests/ElggEventsTest.js28
-rw-r--r--engine/js/tests/ElggLibTest.js39
-rw-r--r--engine/js/tests/ElggPriorityListTest.js47
10 files changed, 253 insertions, 49 deletions
diff --git a/engine/js/classes/ElggPriorityList.js b/engine/js/classes/ElggPriorityList.js
new file mode 100644
index 000000000..521fbbb64
--- /dev/null
+++ b/engine/js/classes/ElggPriorityList.js
@@ -0,0 +1,60 @@
+elgg.ElggPriorityList = function() {
+ this.length = 0;
+ this.priorities_ = [];
+};
+
+elgg.ElggPriorityList.prototype.insert = function(obj, opt_priority) {
+ if (opt_priority == undefined) {
+ opt_priority = 500;
+ }
+
+ opt_priority = parseInt(opt_priority);
+ if (opt_priority < 0) {
+ opt_priority = 0;
+ }
+
+ if (this.priorities_[opt_priority] == undefined) {
+ this.priorities_[opt_priority] = [];
+ }
+
+ this.priorities_[opt_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) {
+ 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) {
+ if (!callback(elems[i], index)) {
+ return false;
+ };
+ }
+ }
+
+ return true;
+};
+
+elgg.ElggPriorityList.prototype.remove = function(obj) {
+ this.priorities_.forEach(function(elems, priority) {
+ var index;
+ while ((index = elems.indexOf(obj)) != -1) {
+ elems.splice(index, 1);
+ }
+ });
+}; \ No newline at end of file
diff --git a/engine/js/lib/elgglib.js b/engine/js/lib/elgglib.js
index 5c32deaaf..f7c30bdc2 100644
--- a/engine/js/lib/elgglib.js
+++ b/engine/js/lib/elgglib.js
@@ -9,11 +9,10 @@
*/
var elgg = elgg || {};
-elgg.init = function() {
- //if the user clicks a system message, make it disappear
- $('.elgg_system_message').live('click', function() {
- $(this).stop().fadeOut('fast');
- });
+elgg.assertTypeOf = function(type, param) {
+ if (typeof param !== type) {
+ throw new TypeError("Expecting param to be a " + type + ". Was a " + typeof param + ".");
+ }
};
/**
@@ -176,12 +175,4 @@ elgg.register_error = function(errors, delay) {
*/
elgg.forward = function(url) {
location.href = elgg.extendUrl(url);
-};
-
-/**
- * Initialise Elgg
- * @todo How should plugins, etc. initialize themselves?
- */
-$(function() {
- elgg.init();
-});
+}; \ No newline at end of file
diff --git a/engine/js/lib/events.js b/engine/js/lib/events.js
new file mode 100644
index 000000000..358dd6280
--- /dev/null
+++ b/engine/js/lib/events.js
@@ -0,0 +1,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;
+}; \ No newline at end of file
diff --git a/engine/js/lib/languages.js b/engine/js/lib/languages.js
index 6ac83f350..3231cf77d 100644
--- a/engine/js/lib/languages.js
+++ b/engine/js/lib/languages.js
@@ -5,10 +5,6 @@ elgg.provide('elgg.config.translations');
elgg.config.language = 'en';
-elgg.config.translations.init = function() {
- elgg.reload_all_translations();
-};
-
elgg.add_translation = function(lang, translations) {
elgg.provide('elgg.config.translations.' + lang);
@@ -37,11 +33,6 @@ elgg.reload_all_translations = function(language) {
};
/**
- * @deprecated Use elgg.reload_all_translations
- */
-elgg.config.translations.load = elgg.reload_all_translations;
-
-/**
* Get the current language
* @return {String}
*/
@@ -85,6 +76,8 @@ elgg.echo = function(key, language) {
return undefined;
};
-$(function() {
- elgg.config.translations.init();
-}); \ No newline at end of file
+elgg.config.translations.init = function() {
+ elgg.reload_all_translations();
+};
+
+elgg.register_event_handler('boot', 'system', elgg.config.translations.init); \ No newline at end of file
diff --git a/engine/js/lib/security.js b/engine/js/lib/security.js
index f4494111b..bdd762560 100644
--- a/engine/js/lib/security.js
+++ b/engine/js/lib/security.js
@@ -5,11 +5,6 @@ elgg.provide('elgg.security');
elgg.security.token = {};
-elgg.security.init = function() {
- //refresh security token every 5 minutes
- setInterval(elgg.security.refreshToken, elgg.security.interval);
-};
-
elgg.security.setToken = function(json) {
//update the convenience object
elgg.security.token = json;
@@ -67,6 +62,9 @@ elgg.security.addToken = function(data) {
throw new TypeError("elgg.security.addToken not implemented for " + (typeof data) + "s");
};
-$(function() {
- elgg.security.init();
-}); \ No newline at end of file
+elgg.security.init = function() {
+ //refresh security token every 5 minutes
+ setInterval(elgg.security.refreshToken, elgg.security.interval);
+};
+
+elgg.register_event_handler('boot', 'system', elgg.security.init); \ No newline at end of file
diff --git a/engine/js/lib/ui.js b/engine/js/lib/ui.js
index b584d66d1..4a9c64e70 100644
--- a/engine/js/lib/ui.js
+++ b/engine/js/lib/ui.js
@@ -1,6 +1,11 @@
elgg.provide('elgg.ui');
elgg.ui.init = function () {
+ //if the user clicks a system message, make it disappear
+ $('.elgg_system_message').live('click', function() {
+ $(this).stop().fadeOut('fast');
+ });
+
$('a.collapsibleboxlink').click(elgg.ui.toggleCollapsibleBox);
// set-up hover class for dragged widgets
@@ -113,6 +118,4 @@ elgg.ui.toggleCollapsibleBox = function () {
};
})(jQuery);
-$(function() {
- elgg.ui.init();
-}); \ No newline at end of file
+elgg.register_event_handler('init', 'system', elgg.ui.init); \ No newline at end of file
diff --git a/engine/js/lib/ui.widgets.js b/engine/js/lib/ui.widgets.js
index 02a6d0e16..1e3163709 100644
--- a/engine/js/lib/ui.widgets.js
+++ b/engine/js/lib/ui.widgets.js
@@ -128,6 +128,4 @@ var toggleContent = elgg.ui.widgets.toggleContent,
widget_state = elgg.ui.widgets.state,
outputWidgetList = elgg.ui.widgets.outputList;
-$(function() {
- elgg.ui.widgets.init();
-});
+elgg.register_event_handler('init', 'system', elgg.ui.widgets.init); \ No newline at end of file
diff --git a/engine/js/tests/ElggEventsTest.js b/engine/js/tests/ElggEventsTest.js
new file mode 100644
index 000000000..cc30e8418
--- /dev/null
+++ b/engine/js/tests/ElggEventsTest.js
@@ -0,0 +1,28 @@
+ElggEventsTest = TestCase("ElggEventsTest");
+
+ElggEventsTest.prototype.setUp = function() {
+ elgg.config.events = {};
+ elgg.provide('elgg.config.events.all.all');
+};
+
+ElggEventsTest.prototype.testEventHandlersMustBeFunctions = function() {
+ assertException(function() { elgg.register_event_handler('str', 'str', 'oops'); });
+};
+
+ElggEventsTest.prototype.testReturnValueDefaultsToTrue = function() {
+ assertTrue(elgg.trigger_event('fee', 'fum'));
+
+ elgg.register_event_handler('fee', 'fum', function() {});
+ assertTrue(elgg.trigger_event('fee', 'fum'));
+};
+
+ElggEventsTest.prototype.testCanGlomEventsWithAll = function() {
+ elgg.register_event_handler('all', 'bar', function() { throw new Error(); });
+ assertException("all,bar", function() { elgg.trigger_event('foo', 'bar'); });
+
+ elgg.register_event_handler('foo', 'all', function() { throw new Error(); });
+ assertException("foo,all", function() { elgg.trigger_event('foo', 'baz'); });
+
+ elgg.register_event_handler('all', 'all', function() { throw new Error(); });
+ assertException("all,all", function() { elgg.trigger_event('pinky', 'winky'); });
+}; \ No newline at end of file
diff --git a/engine/js/tests/ElggLibTest.js b/engine/js/tests/ElggLibTest.js
index 920296408..ed4db24e1 100644
--- a/engine/js/tests/ElggLibTest.js
+++ b/engine/js/tests/ElggLibTest.js
@@ -7,6 +7,35 @@ ElggLibTest.prototype.testGlobal = function() {
assertTrue(window === elgg.global);
};
+ElggLibTest.prototype.testAssertTypeOf = function() {
+ var noexceptions = [
+ ['string', ''],
+ ['object', {}],
+ ['boolean', true],
+ ['boolean', false],
+ ['undefined', undefined],
+ ['number', 0],
+ ['function', function() {}],
+ ];
+
+ for (var i in noexceptions) {
+ assertNoException(function() {
+ elgg.assertTypeOf.apply(elgg, noexceptions[i]);
+ });
+ }
+
+ var exceptions = [
+ ['function', {}],
+ ['object', function() {}],
+ ];
+
+ for (var i in exceptions) {
+ assertException(function() {
+ elgg.assertTypeOf.apply(elgg, exceptions[i]);
+ });
+ }
+};
+
ElggLibTest.prototype.testProvide = function() {
elgg.provide('foo.bar.baz');
@@ -39,7 +68,6 @@ ElggLibTest.prototype.testInherit = function() {
elgg.inherit(Child, Base);
-
assertInstanceOf(Base, new Child());
assertEquals(Child, Child.prototype.constructor);
};
@@ -53,11 +81,4 @@ ElggLibTest.prototype.testExtendUrl = function() {
url = 'pg/test';
assertEquals('http://www.elgg.org/pg/test', elgg.extendUrl(url));
-};
-
-
-
-
-
-
-
+}; \ No newline at end of file
diff --git a/engine/js/tests/ElggPriorityListTest.js b/engine/js/tests/ElggPriorityListTest.js
new file mode 100644
index 000000000..2549e0ee0
--- /dev/null
+++ b/engine/js/tests/ElggPriorityListTest.js
@@ -0,0 +1,47 @@
+ElggPriorityListTest = TestCase("ElggPriorityListTest");
+
+ElggPriorityListTest.prototype.setUp = function() {
+ this.list = new elgg.ElggPriorityList();
+};
+
+ElggPriorityListTest.prototype.tearDown = function() {
+ this.list = null;
+};
+
+ElggPriorityListTest.prototype.testInsert = function() {
+ this.list.insert('foo');
+
+ assertEquals('foo', this.list.priorities_[500][0]);
+
+ this.list.insert('bar', 501);
+
+ assertEquals('foo', this.list.priorities_[501][0]);
+};
+
+ElggPriorityListTest.prototype.testInsertRespectsPriority = function() {
+ var values = [5, 4, 3, 2, 1, 0];
+
+ for (var i in values) {
+ this.list.insert(values[i], values[i]);
+ }
+
+ this.list.forEach(function(elem, idx)) {
+ assertEquals(elem, idx);
+ }
+};
+
+ElggPriorityListTest.prototype.testInsertHandlesDuplicatePriorities = function() {
+ values = [0, 1, 2, 3, 4, 5, 6, 7, 8 , 9];
+
+ for (var i in values) {
+ this.list.insert(values[i], values[i]/3);
+ }
+
+ this.list.forEach(function(elem, idx) {
+ assertEquals(elem, idx);
+ });
+};
+
+ElggPriorityListTest.prototype.testEveryDefaultsToTrue = function() {
+ assertTrue(this.list.every(function() {}));
+}; \ No newline at end of file