diff options
Diffstat (limited to 'js/lib')
-rw-r--r-- | js/lib/elgglib.js | 134 | ||||
-rw-r--r-- | js/lib/events.js | 22 | ||||
-rw-r--r-- | js/lib/languages.js | 51 | ||||
-rw-r--r-- | js/lib/prototypes.js | 44 |
4 files changed, 151 insertions, 100 deletions
diff --git a/js/lib/elgglib.js b/js/lib/elgglib.js index a8ab2020c..78a863803 100644 --- a/js/lib/elgglib.js +++ b/js/lib/elgglib.js @@ -5,7 +5,7 @@ var elgg = elgg || {}; /**
* Pointer to the global context
- *
+ *
* @see elgg.require
* @see elgg.provide
*/
@@ -13,55 +13,60 @@ elgg.global = this; /**
* Convenience reference to an empty function.
- *
+ *
* Save memory by not generating multiple empty functions.
*/
elgg.nullFunction = function() {};
/**
- *
+ * This forces an inheriting class to implement the method or
+ * it will throw an error.
+ *
* @example
* AbstractClass.prototype.toBeImplemented = elgg.abstractMethod;
- *
- * Now this forces an inheriting class to implement the method or
- * it will throw an error.
*/
elgg.abstractMethod = function() {
throw new Error("Oops... you forgot to implement an abstract method!");
};
/**
- * Check if the value is an array.
- *
+ * Check if the value is an array.
+ *
* No sense in reinventing the wheel!
- *
+ *
+ * @param {*} val
+ *
* @return boolean
*/
elgg.isArray = jQuery.isArray;
/**
- * Check if the value is a function.
- *
+ * Check if the value is a function.
+ *
* No sense in reinventing the wheel!
- *
+ *
+ * @param {*} val
+ *
* @return boolean
*/
elgg.isFunction = jQuery.isFunction;
/**
* Check if the value is a "plain" object (i.e., created by {} or new Object())
- *
+ *
* No sense in reinventing the wheel!
- *
+ *
+ * @param {*} val
+ *
* @return boolean
*/
elgg.isPlainObject = jQuery.isPlainObject;
/**
* Check if the value is a string
- *
+ *
* @param {*} val
- *
+ *
* @return boolean
*/
elgg.isString = function(val) {
@@ -70,9 +75,9 @@ elgg.isString = function(val) { /**
* Check if the value is a number
- *
+ *
* @param {*} val
- *
+ *
* @return boolean
*/
elgg.isNumber = function(val) {
@@ -81,12 +86,12 @@ elgg.isNumber = function(val) { /**
* Check if the value is an object
- *
+ *
* @note This returns true for functions and arrays! If you want to return true only
* for "plain" objects (created using {} or new Object()) use elgg.isPlainObject.
- *
+ *
* @param {*} val
- *
+ *
* @return boolean
*/
elgg.isObject = function(val) {
@@ -95,9 +100,9 @@ elgg.isObject = function(val) { /**
* Check if the value is undefined
- *
+ *
* @param {*} val
- *
+ *
* @return boolean
*/
elgg.isUndefined = function(val) {
@@ -106,9 +111,9 @@ elgg.isUndefined = function(val) { /**
* Check if the value is null
- *
+ *
* @param {*} val
- *
+ *
* @return boolean
*/
elgg.isNull = function(val) {
@@ -117,9 +122,9 @@ elgg.isNull = function(val) { /**
* Check if the value is either null or undefined
- *
+ *
* @param {*} val
- *
+ *
* @return boolean
*/
elgg.isNullOrUndefined = function(val) {
@@ -128,25 +133,25 @@ elgg.isNullOrUndefined = function(val) { /**
* Throw an exception of the type doesn't match
- *
+ *
* @todo Might be more appropriate for debug mode only?
*/
elgg.assertTypeOf = function(type, val) {
if (typeof val !== type) {
- throw new TypeError("Expecting param of " +
- arguments.caller + "to be a(n) " + type + "." +
- " Was actually a(n) " + typeof val + ".");
+ throw new TypeError("Expecting param of " +
+ arguments.caller + "to be a(n) " + type + "." +
+ " Was actually a(n) " + typeof val + ".");
}
};
/**
* Throw an error if the required package isn't present
- *
+ *
* @param {String} pkg The required package (e.g., 'elgg.package')
*/
elgg.require = function(pkg) {
elgg.assertTypeOf('string', pkg);
-
+
var parts = pkg.split('.'),
cur = elgg.global,
part, i;
@@ -162,31 +167,31 @@ elgg.require = function(pkg) { /**
* Generate the skeleton for a package.
- *
+ *
* <pre>
* elgg.provide('elgg.package.subpackage');
* </pre>
- *
+ *
* is equivalent to
- *
+ *
* <pre>
* elgg = elgg || {};
* elgg.package = elgg.package || {};
* elgg.package.subpackage = elgg.package.subpackage || {};
* </pre>
- *
+ *
* @example elgg.provide('elgg.config.translations')
- *
+ *
* @param {string} pkg The package name.
*/
elgg.provide = function(pkg, opt_context) {
elgg.assertTypeOf('string', pkg);
-
+
var parts = pkg.split('.'),
- context = opt_context || elgg.global,
- part, i;
-
-
+ context = opt_context || elgg.global,
+ part, i;
+
+
for (i = 0; i < parts.length; i += 1) {
part = parts[i];
context[part] = context[part] || {};
@@ -196,11 +201,11 @@ elgg.provide = function(pkg, opt_context) { /**
* Inherit the prototype methods from one constructor into another.
- *
+ *
* @example
* <pre>
* function ParentClass(a, b) { }
- *
+ *
* ParentClass.prototype.foo = function(a) { alert(a); }
*
* function ChildClass(a, b, c) {
@@ -224,7 +229,7 @@ elgg.inherit = function(Child, Parent) { /**
* Prepend elgg.config.wwwroot to a url if the url doesn't already have it.
- *
+ *
* @param {String} url The url to extend
* @return {String} The extended url
* @private
@@ -232,18 +237,18 @@ elgg.inherit = function(Child, Parent) { elgg.normalize_url = function(url) {
url = url || '';
elgg.assertTypeOf('string', url);
-
+
// jslint complains if you use /regexp/ shorthand here... ?!?!
if ((new RegExp("^(https?:)?//")).test(url)) {
return url;
}
-
- return elgg.config.wwwroot + url;
+
+ return elgg.config.wwwroot + url.ltrim('/');
};
/**
* Displays system messages via javascript rather than php.
- *
+ *
* @param {String} msgs The message we want to display
* @param {Number} delay The amount of time to display the message in milliseconds. Defaults to 6 seconds.
* @param {String} type The type of message (typically 'error' or 'message')
@@ -253,18 +258,19 @@ elgg.system_messages = function(msgs, delay, type) { if (elgg.isUndefined(msgs)) {
return;
}
-
- var classes = [],
+
+ var classes = ['elgg_system_message', 'radius8'],
messages_html = [],
- i;
-
- //validate delay. Must be a positive integer.
- delay = parseInt(delay, 10);
+ appendMessage = function(msg) {
+ messages_html.push('<div class="' + classes.join(' ') + '"><p>' + msg + '</p></div>');
+ }, i;
+
+ //validate delay. Must be a positive integer.
+ delay = parseInt(delay || 6000, 10);
if (isNaN(delay) || delay <= 0) {
delay = 6000;
}
-
- classes = ['elgg_system_message', 'radius8'];
+
if (type === 'error') {
classes.push('messages_error');
}
@@ -273,13 +279,11 @@ elgg.system_messages = function(msgs, delay, type) { if (!elgg.isArray(msgs)) {
msgs = [msgs];
}
-
- for (i in msgs) {
- messages_html.push('<div class="' + classes.join(' ') + '"><p>' + msgs[i] + '</p></div>');
- }
-
+
+ msgs.forEach(appendMessage);
+
$(messages_html.join('')).appendTo('#elgg_system_messages')
- .animate({opacity: '1.0'}, delay).fadeOut('slow');
+ .animate({opacity: '1.0'}, delay).fadeOut('slow');
};
/**
@@ -303,7 +307,7 @@ elgg.register_error = function(errors, delay) { /**
* Meant to mimic the php forward() function by simply redirecting the
* user to another page.
- *
+ *
* @param {String} url The url to forward to
*/
elgg.forward = function(url) {
diff --git a/js/lib/events.js b/js/lib/events.js index ad05a9888..c1aa6fd9a 100644 --- a/js/lib/events.js +++ b/js/lib/events.js @@ -1,22 +1,22 @@ elgg.provide('elgg.config.events'); /** - * + * */ 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; } - + var events = elgg.config.events; - + elgg.provide(event_name + '.' + event_type, events); - + if (!(events[event_name][event_type] instanceof elgg.ElggPriorityList)) { events[event_name][event_type] = new elgg.ElggPriorityList(); } @@ -25,22 +25,22 @@ elgg.register_event_handler = function(event_name, event_type, handler, priority }; /** - * + * */ elgg.trigger_event = function(event_name, event_type, opt_object) { elgg.assertTypeOf('string', event_name); elgg.assertTypeOf('string', event_type); var events = elgg.config.events, - callEventHandler = function(handler) { - return handler(event_name, event_type, opt_object) !== false; - } - + callEventHandler = function(handler) { + return handler(event_name, event_type, opt_object) !== false; + }; + 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], diff --git a/js/lib/languages.js b/js/lib/languages.js index 640764282..82cfa7a01 100644 --- a/js/lib/languages.js +++ b/js/lib/languages.js @@ -1,3 +1,4 @@ +/*globals vsprintf*/
/**
* Provides language-related functionality
*/
@@ -7,13 +8,13 @@ elgg.config.language = 'en'; elgg.add_translation = function(lang, translations) {
elgg.provide('elgg.config.translations.' + lang);
-
+
$.extend(elgg.config.translations[lang], translations);
};
/**
* Load the translations for the given language.
- *
+ *
* If no language is specified, the default language is used.
* @param {string} language
* @return {XMLHttpRequest}
@@ -38,41 +39,43 @@ elgg.reload_all_translations = function(language) { */
elgg.get_language = function() {
var user = elgg.get_loggedin_user();
-
+
if (user && user.language) {
return user.language;
}
-
+
return elgg.config.language;
};
/**
* Translates a string
- *
- * @param {String} key The string to translate
+ *
+ * @param {String} key The string to translate
+ * @param {Array} argv vsprintf support
* @param {String} language The language to display it in
+ *
* @return {String} The translation
*/
-elgg.echo = function(key, language) {
- var translations,
- dlang = elgg.get_language();
-
- language = language || dlang;
-
- translations = elgg.config.translations[language];
- if (translations && translations[key]) {
- return translations[key];
- }
-
- if (language === dlang) {
- return undefined;
+elgg.echo = function(key, argv, language) {
+ //elgg.echo('str', 'en')
+ if (elgg.isString(argv)) {
+ language = argv;
+ argv = [];
}
-
- translations = elgg.config.translations[dlang];
- if (translations && translations[key]) {
- return translations[key];
+
+ //elgg.echo('str', [...], 'en')
+ var translations = elgg.config.translations,
+ dlang = elgg.get_language(),
+ map;
+
+ language = language || dlang;
+ argv = argv || [];
+
+ map = translations[language] || translations[dlang];
+ if (map && map[key]) {
+ return vsprintf(map[key], argv);
}
-
+
return undefined;
};
diff --git a/js/lib/prototypes.js b/js/lib/prototypes.js new file mode 100644 index 000000000..0f0014818 --- /dev/null +++ b/js/lib/prototypes.js @@ -0,0 +1,44 @@ +/** + * + */ +if (!Array.prototype.every) { + Array.prototype.every = function(callback) { + var len = this.length, i; + + for (i = 0; i < len; i++) { + if (i in this && !callback.call(null, this[i], i)) { + return false; + } + } + + return true; + }; +} + +/** + * + */ +if (!Array.prototype.forEach) { + Array.prototype.forEach = function(callback) { + var len = this.length, i; + + for (i = 0; i < len; i++) { + if (i in this) { + callback.call(null, this[i], i); + } + } + }; +} + +/** + * + */ +if (!String.prototype.ltrim) { + String.prototype.ltrim = function(str) { + if (this.indexOf(str) === 0) { + return this.substring(str.length); + } else { + return this; + } + }; +}
\ No newline at end of file |