jquery.query.js 9.2 KB

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