-
Notifications
You must be signed in to change notification settings - Fork 108
/
backbone.queryparams.min.js
7 lines (7 loc) · 3.16 KB
/
backbone.queryparams.min.js
1
2
3
4
5
6
7
(function(f,g){"function"===typeof define&&define.amd?define(["underscore","backbone"],function(l,k){return g(l||f._,k||f.Backbone)}):"object"===typeof exports?module.exports=g(require("underscore"),require("backbone")):g(_,Backbone)})(this,function(f,g){function l(a,d){function b(a){return String(a).replace(c,encodeURIComponent(c))}var c=g.Router.arrayValueSplit;if(!a)return"";d=d||"";var e=[];f.each(a,function(a,g){g=d+g;if(f.isString(a)||f.isNumber(a)||f.isBoolean(a)||f.isDate(a))null!=a&&e.push(g+
"="+b(encodeURIComponent(a)));else if(f.isArray(a)){for(var h="",k=0;k<a.length;k++){var n=a[k];null!=n&&(h+=c+b(encodeURIComponent(n)))}h&&e.push(g+"="+h)}else(h=l(a,g+"."))&&e.push(h)});return e.join("&")}function k(a){try{return decodeURIComponent(a.replace(/\+/g," "))}catch(d){return a}}function p(a,d){var b=a.split("&");f.each(b,function(a){a=a.split("=");d(a.shift(),a.join("="))})}var q=/^\?(.*)/,s=/\((.*?)\)/g,m=/(\(\?)?:\w+/g,r=/\*\w+/g,t=/[\-{}\[\]+?.,\\\^$|#\s]/g,u=/^([^\?]*)/,v=/[\:\*]([^\:\?\/]+)/g,
w=/^[#\/]|\s+$/g,x=/\/$/;g.Router.arrayValueSplit="|";f.extend(g.History.prototype,{getFragment:function(a,d){var b="";if(null==a)if(this._wantsPushState||!this._wantsHashChange||d){a=this.location.pathname;this.root&&this.root.length&&(b=this.root.replace(x,""));var c=this.location.search;a.indexOf(b)||(a=a.substr(b.length));c&&this._hasPushState&&(a+=c)}else a=this.getHash();return a.replace(w,"")},getQueryParameters:function(a,d){a=this.getFragment(a,d);var b=a.replace(u,"");if(b=b.match(q)){var b=
b[1],c={};p(b,function(a,b){b=k(b);c[a]?f.isString(c[a])?c[a]=[c[a],b]:c[a].push(b):c[a]=b});return c}return{}}});f.extend(g.Router.prototype,{initialize:function(a){this.encodedSplatParts=a&&a.encodedSplatParts},_routeToRegExp:function(a){var d=r.exec(a)||{index:-1},b=m.exec(a)||{index:-1},c=a.match(v)||[];a=a.replace(t,"\\$&").replace(s,"(?:$1)?").replace(m,function(a,b){return b?a:"([^\\/\\?]+)"}).replace(r,"([^??]*?)");a=new RegExp("^"+(a+"(\\?.*)?")+"$");0<=d.index&&(a.splatMatch=0<=b?d.index-
b.index:-1);a.paramNames=f.map(c,function(a){return a.replace(/\)$/,"").substring(1)});a.namedParameters=this.namedParameters;return a},_extractParameters:function(a,d){var b=a.exec(d).slice(1),c={};0<b.length&&!b[b.length-1]&&b.splice(b.length-1,1);var e=b.length&&b[b.length-1]&&b[b.length-1].match(q);if(e){var e=e[1],l={};if(e){var m=this;p(e,function(a,b){m._setParamValue(a,b,l)})}b[b.length-1]=l;f.extend(c,l)}e=b.length;if(a.splatMatch&&this.encodedSplatParts){if(0>a.splatMatch)return b;e-=1}for(var h=
0;h<e;h++)f.isString(b[h])&&(b[h]=k(b[h]),a.paramNames&&a.paramNames.length>=h-1&&(c[a.paramNames[h]]=b[h]));return g.Router.namedParameters||a.namedParameters?[c]:b},_setParamValue:function(a,d,b){a=a.replace("[]","");a=a.replace("%5B%5D","");a=a.split(".");for(var c=0;c<a.length;c++){var e=decodeURIComponent(a[c]);c===a.length-1?b[e]=this._decodeParamValue(d,b[e]):b=b[e]=b[e]||{}}},_decodeParamValue:function(a,d){var b=g.Router.arrayValueSplit;if(b&&0<=a.indexOf(b)){for(var b=a.split(b),c=b.length-
1;0<=c;c--)b[c]?b[c]=k(b[c]):b.splice(c,1);return b}a=k(a);return d?f.isArray(d)?(d.push(a),d):[d,a]:a},toFragment:function(a,d){d&&(f.isString(d)||(d=l(d)),d&&(a+="?"+d));return a}})});