jquery.query-2.0.1.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /**
  2. * jQuery.query - Query String Modification and Creation for jQuery
  3. * Written by Blair Mitchelmore (blair DOT mitchelmore AT gmail DOT com)
  4. * Licensed under the WTFPL (http://sam.zoy.org/wtfpl/).
  5. * Date: 2008/05/28
  6. *
  7. * @author Blair Mitchelmore
  8. * @version 2.0.1
  9. *
  10. **/
  11. new function(settings) {
  12. // Various Settings
  13. var $separator = settings.separator || '&';
  14. var $spaces = settings.spaces === false ? false : true;
  15. var $suffix = settings.suffix === false ? '' : '[]';
  16. var $prefix = settings.prefix === false ? false : true;
  17. var $hash = $prefix ? settings.hash === true ? "#" : "?" : "";
  18. jQuery.query = new function() {
  19. var is = function(o, t) {
  20. return o != undefined && o !== null && (!!t ? o.constructor == t : true);
  21. };
  22. var parse = function(path) {
  23. var m, rx = /\[([^[]*)\]/g, match = /^(\S+?)(\[\S*\])?$/.exec(path), base = match[1], tokens = [];
  24. while (m = rx.exec(match[2])) tokens.push(m[1]);
  25. return [base, tokens];
  26. };
  27. var set = function(target, tokens, value) {
  28. var o, token = tokens.shift();
  29. if (typeof target != 'object') target = null;
  30. if (token === "") {
  31. if (!target) target = [];
  32. if (is(target, Array)) {
  33. target.push(tokens.length == 0 ? value : set(null, tokens.slice(0), value));
  34. } else if (is(target, Object)) {
  35. var i = 0;
  36. while (target[i++] != null);
  37. target[--i] = tokens.length == 0 ? value : set(target[i], tokens.slice(0), value);
  38. } else {
  39. target = [];
  40. target.push(tokens.length == 0 ? value : set(null, tokens.slice(0), value));
  41. }
  42. } else if (token && token.match(/^\s*[0-9]+\s*$/)) {
  43. var index = parseInt(token, 10);
  44. if (!target) target = [];
  45. target[index] = tokens.length == 0 ? value : set(target[index], tokens.slice(0), value);
  46. } else if (token) {
  47. var index = token.replace(/^\s*|\s*$/g, "");
  48. if (!target) target = {};
  49. if (is(target, Array)) {
  50. var temp = {};
  51. for (var i = 0; i < target.length; ++i) {
  52. temp[i] = target[i];
  53. }
  54. target = temp;
  55. }
  56. target[index] = tokens.length == 0 ? value : set(target[index], tokens.slice(0), value);
  57. } else {
  58. return value;
  59. }
  60. return target;
  61. };
  62. var queryObject = function(a) {
  63. var self = this;
  64. self.keys = {};
  65. if (a.queryObject) {
  66. jQuery.each(a.get(), function(key, val) {
  67. self.SET(key, val);
  68. });
  69. } else {
  70. jQuery.each(arguments, function() {
  71. var q = "" + this;
  72. q = q.replace(/^[?#]/,''); // remove any leading ? || #
  73. q = q.replace(/[;&]$/,''); // remove any trailing & || ;
  74. if ($spaces) q = q.replace(/[+]/g,' '); // replace +'s with spaces
  75. jQuery.each(q.split(/[&;]/), function(){
  76. var key = this.split('=')[0];
  77. var val = this.split('=')[1];
  78. if (!key) return;
  79. if (/^[+-]?[0-9]+\.[0-9]*$/.test(val)) // simple float regex
  80. val = parseFloat(val);
  81. else if (/^[+-]?[0-9]+$/.test(val)) // simple int regex
  82. val = parseInt(val, 10);
  83. val = (!val && val !== 0) ? true : val;
  84. if (val !== false && val !== true && typeof val != 'number')
  85. val = decodeURIComponent(val);
  86. self.SET(key, val);
  87. });
  88. });
  89. }
  90. return self;
  91. };
  92. queryObject.prototype = {
  93. queryObject: true,
  94. has: function(key, type) {
  95. var value = this.get(key);
  96. return is(value, type);
  97. },
  98. GET: function(key) {
  99. if (!is(key)) return this.keys;
  100. var parsed = parse(key), base = parsed[0], tokens = parsed[1];
  101. var target = this.keys[base];
  102. while (target != null && tokens.length != 0) {
  103. target = target[tokens.shift()];
  104. }
  105. return typeof target == 'number' ? target : target || "";
  106. },
  107. get: function(key) {
  108. var target = this.GET(key);
  109. if (is(target, Object))
  110. return jQuery.extend(true, {}, target);
  111. else if (is(target, Array))
  112. return target.slice(0);
  113. return target;
  114. },
  115. SET: function(key, val) {
  116. var value = !is(val) ? null : val;
  117. var parsed = parse(key), base = parsed[0], tokens = parsed[1];
  118. var target = this.keys[base];
  119. this.keys[base] = set(target, tokens.slice(0), value);
  120. return this;
  121. },
  122. set: function(key, val) {
  123. return this.copy().SET(key, val);
  124. },
  125. REMOVE: function(key) {
  126. return this.SET(key, null).COMPACT();
  127. },
  128. remove: function(key) {
  129. return this.copy().REMOVE(key);
  130. },
  131. EMPTY: function() {
  132. var self = this;
  133. jQuery.each(self.keys, function(key, value) {
  134. delete self.keys[key];
  135. });
  136. return self;
  137. },
  138. empty: function() {
  139. return this.copy().EMPTY();
  140. },
  141. copy: function() {
  142. return new queryObject(this);
  143. },
  144. COMPACT: function() {
  145. function build(orig) {
  146. var obj = typeof orig == "object" ? is(orig, Array) ? [] : {} : orig;
  147. if (typeof orig == 'object') {
  148. function add(o, key, value) {
  149. if (is(o, Array))
  150. o.push(value);
  151. else
  152. o[key] = value;
  153. }
  154. jQuery.each(orig, function(key, value) {
  155. if (!is(value)) return true;
  156. add(obj, key, build(value));
  157. });
  158. }
  159. return obj;
  160. }
  161. this.keys = build(this.keys);
  162. return this;
  163. },
  164. compact: function() {
  165. return this.copy().COMPACT();
  166. },
  167. toString: function() {
  168. var i = 0, queryString = [], chunks = [], self = this;
  169. var addFields = function(arr, key, value) {
  170. if (!is(value) || value === false) return;
  171. var o = [key];
  172. if (value !== true) {
  173. o.push("=");
  174. o.push(encodeURIComponent(value));
  175. }
  176. arr.push(o.join(""));
  177. };
  178. var build = function(obj, base) {
  179. var newKey = function(key) {
  180. return !base || base == "" ? [key].join("") : [base, "[", key, "]"].join("");
  181. };
  182. jQuery.each(obj, function(key, value) {
  183. if (typeof value == 'object')
  184. build(value, newKey(key));
  185. else
  186. addFields(chunks, newKey(key), value);
  187. });
  188. };
  189. build(this.keys);
  190. if (chunks.length > 0) queryString.push($hash);
  191. queryString.push(chunks.join($separator));
  192. return queryString.join("");
  193. }
  194. };
  195. return new queryObject(location.search, location.hash);
  196. };
  197. }(jQuery.query || {}); // Pass in jQuery.query as settings object