jquery.query-2.1.7.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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 (is(target, Object))
  113. return jQuery.extend(true, {}, target);
  114. else if (is(target, Array))
  115. return target.slice(0);
  116. return target;
  117. },
  118. SET: function(key, val) {
  119. var value = !is(val) ? null : val;
  120. var parsed = parse(key), base = parsed[0], tokens = parsed[1];
  121. var target = this.keys[base];
  122. this.keys[base] = set(target, tokens.slice(0), value);
  123. return this;
  124. },
  125. set: function(key, val) {
  126. return this.copy().SET(key, val);
  127. },
  128. REMOVE: function(key) {
  129. return this.SET(key, null).COMPACT();
  130. },
  131. remove: function(key) {
  132. return this.copy().REMOVE(key);
  133. },
  134. EMPTY: function() {
  135. var self = this;
  136. jQuery.each(self.keys, function(key, value) {
  137. delete self.keys[key];
  138. });
  139. return self;
  140. },
  141. load: function(url) {
  142. var hash = url.replace(/^.*?[#](.+?)(?:\?.+)?$/, "$1");
  143. var search = url.replace(/^.*?[?](.+?)(?:#.+)?$/, "$1");
  144. return new queryObject(url.length == search.length ? '' : search, url.length == hash.length ? '' : hash);
  145. },
  146. empty: function() {
  147. return this.copy().EMPTY();
  148. },
  149. copy: function() {
  150. return new queryObject(this);
  151. },
  152. COMPACT: function() {
  153. function build(orig) {
  154. var obj = typeof orig == "object" ? is(orig, Array) ? [] : {} : orig;
  155. if (typeof orig == 'object') {
  156. function add(o, key, value) {
  157. if (is(o, Array))
  158. o.push(value);
  159. else
  160. o[key] = value;
  161. }
  162. jQuery.each(orig, function(key, value) {
  163. if (!is(value)) return true;
  164. add(obj, key, build(value));
  165. });
  166. }
  167. return obj;
  168. }
  169. this.keys = build(this.keys);
  170. return this;
  171. },
  172. compact: function() {
  173. return this.copy().COMPACT();
  174. },
  175. toString: function() {
  176. var i = 0, queryString = [], chunks = [], self = this;
  177. var encode = function(str) {
  178. str = str + "";
  179. if ($spaces) str = str.replace(/ /g, "+");
  180. return encodeURIComponent(str);
  181. };
  182. var addFields = function(arr, key, value) {
  183. if (!is(value) || value === false) return;
  184. var o = [encode(key)];
  185. if (value !== true) {
  186. o.push("=");
  187. o.push(encode(value));
  188. }
  189. arr.push(o.join(""));
  190. };
  191. var build = function(obj, base) {
  192. var newKey = function(key) {
  193. return !base || base == "" ? [key].join("") : [base, "[", key, "]"].join("");
  194. };
  195. jQuery.each(obj, function(key, value) {
  196. if (typeof value == 'object')
  197. build(value, newKey(key));
  198. else
  199. addFields(chunks, newKey(key), value);
  200. });
  201. };
  202. build(this.keys);
  203. if (chunks.length > 0) queryString.push($hash);
  204. queryString.push(chunks.join($separator));
  205. return queryString.join("");
  206. }
  207. };
  208. return new queryObject(location.search, location.hash);
  209. };
  210. }(jQuery.query || {}); // Pass in jQuery.query as settings object