summaryrefslogtreecommitdiff
path: root/misc/jquery.ba-bbq.js
blob: 73e64df4f519cdd56fbb2b9b7cffa5384436d59f (plain)
1
2
3
4
5
6
7
8
9
10
11
// $Id$

/*
 * jQuery BBQ: Back Button & Query Library - v1.0.2 - 10/10/2009
 * http://benalman.com/projects/jquery-bbq-plugin/
 * 
 * Copyright (c) 2009 "Cowboy" Ben Alman
 * Dual licensed under the MIT and GPL licenses.
 * http://benalman.com/about/license/
 */
(function($,c){var g,k=document.location,i=Array.prototype.slice,E=decodeURIComponent,a=$.param,m,d,p,n=$.bbq=$.bbq||{},o,e,z,b="hashchange",v="querystring",y="fragment",q="hash",x="elemUrlAttr",h="href",D="src",C=$.browser,l=C.msie&&C.version<8,j="on"+b in c&&!l,r=/^.*\?|#.*$/g,A=/^.*\#/,t={};function s(F){return typeof F==="string"}function w(G){var F=i.call(arguments,1);return function(){return G.apply(this,F.concat(i.call(arguments)))}}function f(G,O,F,H,K){var M,L,J,N,I;if(H!==g){J=F.match(G?/^([^#]*)\#?(.*)$/:/^([^#?]*)\??([^#]*)(#?.*)/);I=J[3]||"";if(K===2&&s(H)){L=H.replace(O,"")}else{N=d(J[2]);H=s(H)?d[G?y:v](H):H;L=K===2?H:K===1?$.extend({},H,N):$.extend({},N,H);L=a(L)}M=J[1]+(G?"#":L||!J[1]?"?":"")+L+I}else{if(F){M=F.replace(O,"")}else{M=G?k[q]?k[h].replace(O,""):"":k.search.replace(/^\??/,"")}}return M}a[v]=w(f,0,r);a[y]=m=w(f,1,A);$.deparam=d=function(I,G){var H={},F={"true":!0,"false":!1,"null":null};$.each(I.replace(/\+/g," ").split("&"),function(M,O){var L=O.split("="),P=E(L[0]),K,Q=H,N=0,R=P.split("]["),J=R.length-1;if(/\[/.test(R[0])&&/\]$/.test(R[J])){R[J]=R[J].replace(/\]$/,"");R=R.shift().split("[").concat(R);J=R.length-1}else{J=0}if(L.length===2){K=E(L[1]);if(G){K=K&&!isNaN(K)?+K:K==="undefined"?g:F[K]!==g?F[K]:K}if(J){for(;N<=J;N++){P=R[N]===""?Q.length:R[N];Q=Q[P]=N<J?Q[P]||(R[N+1]&&isNaN(R[N+1])?{}:[]):K}}else{if($.isArray(H[P])){H[P].push(K)}else{if(H[P]!==g){H[P]=[H[P],K]}else{H[P]=K}}}}else{if(P){H[P]=G?g:""}}});return H};function u(I,H,G,F){if(G===g||typeof G==="boolean"){F=G;G=a[I]()}else{G=s(G)?G.replace(H,""):G}return d(G,F)}d[v]=w(u,v,r);d[y]=p=w(u,y,A);$[x]||($[x]=function(F){return $.extend(t,F)})({a:h,base:h,iframe:D,img:D,input:D,form:"action",link:h,script:D});e=$[x];function B(I,F,H,G){if(!s(H)&&typeof H!=="object"){G=H;H=F;F=g}return this.each(function(){var L=$(this),J=F||e()[(this.nodeName||"").toLowerCase()]||"",K=J&&L.attr(J)||"";L.attr(J,a[I](K,H,G))})}$.fn[v]=w(B,v);$.fn[y]=w(B,y);n.pushState=o=function(I,H){if(s(I)&&/^#/.test(I)&&H===g){H=2}var G=I!==g,F=m(k[h],G?I:{},G?H:2);k[h]=F+(/#/.test(F)?"":"#")};n.getState=function(G,F){return G===g||typeof G==="boolean"?p(G):p(F)[G]};n.pollDelay=100;$.event.special[b]={setup:function(){if(j){return false}z.start()},teardown:function(){if(j){return false}z.stop()},add:function(F,H,G){return function(J){var I=J[y]=m();J.getState=function(L,K){return L===g||typeof L==="boolean"?d(I,L):d(I,K)[L]};F.apply(this,arguments)}}};z=(function(){var G={},K,F,H,J;function I(){H=J=function(L){return L};if(l){F=$('<iframe src="javascript:0"/>').hide().appendTo("body")[0].contentWindow;J=function(){return F.document.location[q].replace(/^#/,"")};H=function(N,L){if(N!==L){var M=F.document;M.open();M.close();M.location[q]="#"+N}};H(m())}}G.start=function(){if(K){return}var M=m();H||I();(function L(){var O=m(),N=J(M);if(O!==M){H(M=O,N);$(c).trigger(b)}else{if(N!==M){o("#"+N)}}K=setTimeout(L,n.pollDelay)})()};G.stop=function(){if(!F){K&&clearTimeout(K);K=0}};return G})()})(jQuery,this);