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