Index: js_theming.js =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/js_theming/js_theming.js,v retrieving revision 1.7 diff -u -p -r1.7 js_theming.js --- js_theming.js 19 Jul 2008 09:27:18 -0000 1.7 +++ js_theming.js 3 Oct 2008 15:41:00 -0000 @@ -36,52 +36,63 @@ Drupal.drupalAttributes = function(attri /** - * Parse an array into a valid urlencoded query string. + * Convert an array into a valid urlencoded query string. * - * @param $query - * The array to be processed e.g. $_GET. - * @param $exclude + * @param query + * The object to be processed e.g. {'key':'value'}. Nested objects are + * allowed (e.g. {'a':'b', 'aa':{'0':'bb', '1':'cc'}}). + * @param exclude * The array filled with keys to be excluded. Use parent[child] to exclude * nested items. - * @param $parent - * Should not be passed, only used in recursive calls. + * @param parent + * Should not be passed; only used in recursive calls. * @return * An urlencoded string which can be appended to/as the URL query string. */ -Drupal.queryStringEncode = function(query, exclude, parent) { +Drupal.queryStringEncode = function(query /*, exclude, parent */) { var params = []; + exclude = arguments[1] || []; + parent = arguments[2] || null; + for ( var key in query ) { - key = Drupal.urlEncode(key); /* This doesnt do anything yet */ - if( parent != undefined ) { - key = parent + '[' + key + ']'; /* i dont know yet what this does */ - } - if ( exclude[key] != undefined ) { - continue; - alert('fuck'); + key = Drupal.encodeURIComponent(key); + if (parent) { + newkey = parent + '[' + key + ']'; + } + else { + newkey = key; } + + if (exclude.indexOf(key) >= 0) { + continue; // Skip exclude keys + } + + /* + * This handles the case where query looks like this: + * {'a':'b', 'c':{'0':'foo', '1':'bar'}} + * + * The above will be encoded to a query string like this: + * "a=b&c[0]=foo&c[1]=bar" + * + * This should (I hope) keep it compatible with the PHP equivalent function, + * and also handle PHP-style GET arrays. + */ if (typeof(query[key]) == 'object') { + //if (query[key] instanceof Array) { params.push(Drupal.queryStringEncode(query[key], exclude, key)); } else { - params.push(key + '=' + Drupal.urlEncode(query[key])); + params.push(newkey + '=' + Drupal.encodeURIComponent(query[key])); } } - var output = ''; - for ( var i = 0; i < params.length; i++ ){ - output += params[i]; - } - return output; -} - - -/* helper function, returns true if message is equal to current array element */ -/* used to check if a message already exist in Drupal.messages.messages[type] */ -Drupal. strEqual = function (element, index, array) { - return (element == this); + return params.join('&'); } /** - * Wrapper around urlencode() which avoids Apache quirks. + * Emulate the PHP drupal_urlencode() function. + * + * This is a convenience function for encoding Drupal URLs. It simply calls + * the drupal.js function Drupal.encodeURIComponent(). * * Should be used when placing arbitrary data in an URL. Note that Drupal paths * are urlencoded() when passed through url() and do not require urlencoding() @@ -101,7 +112,7 @@ Drupal. strEqual = function (element, in * @todo: define this function */ Drupal.urlEncode = function (text) { - return text; /* placeholder function under i get to define it */ + return Drupal.encodeURIComponent(text); } /* Misc function for debugging */ @@ -248,7 +259,7 @@ Drupal.url = function (path, options) { } if (typeof(options['query']) == 'object') { - options['query'] = queryStringEncode(options['query']); + options['query'] = Drupal.queryStringEncode(options['query']); } if (options['query'].length > 0 ){ return Drupal.settings.basePath + path + '?' + options['query'];