From c9f5c056862553d5102d1dfb7d964ea449573d59 Mon Sep 17 00:00:00 2001 From: ewinslow Date: Sun, 14 Nov 2010 11:33:29 +0000 Subject: Refs #2538: Added vsprintf support to elgg.echo. Added unit tests for normalize_url, added prototype definitions for Array#forEach for compatibility with IE. git-svn-id: http://code.elgg.org/elgg/trunk@7313 36083f99-b078-4883-b0ff-0f9b5a30f544 --- js/lib/elgglib.js | 134 ++++++++++++++++++++++++++++-------------------------- 1 file changed, 69 insertions(+), 65 deletions(-) (limited to 'js/lib/elgglib.js') 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. - * + * *
  * elgg.provide('elgg.package.subpackage');
  * 
- * + * * is equivalent to - * + * *
  * elgg = elgg || {};
  * elgg.package = elgg.package || {};
  * elgg.package.subpackage = elgg.package.subpackage || {};
  * 
- * + * * @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 *
  * 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('

' + msg + '

'); + }, 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('

' + msgs[i] + '

'); - } - + + 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) { -- cgit v1.2.3