controller-min.js revision 3902ae010d351d0fffbc3cab3be9a0c979e3d584
YUI.add("controller",function(a){var h=a.HistoryHash,e=a.Lang,b=a.QueryString,j=a.Array,f=a.HistoryBase.html5&&(!a.UA.android||a.UA.android>=3),g=a.config.win,k=g.location,i=[],d="ready";function c(){c.superclass.constructor.apply(this,arguments);}a.Controller=a.extend(c,a.Base,{html5:f,root:"",routes:[],_regexPathParam:/([:*])([\w-]+)/g,_regexUrlQuery:/\?([^#]*).*$/,initializer:function(m){var l=this;m||(m={});m.routes&&(l.routes=m.routes);e.isValue(m.root)&&(l.root=m.root);l._routes=[];j.each(l.routes,function(n){l.route(n.path,n.callback);});if(f){l._history=new a.HistoryHTML5({force:true});l._history.after("change",l._afterHistoryChange,l);}else{a.on("hashchange",l._afterHistoryChange,g,l);}l.publish(d,{defaultFn:l._defReadyFn,fireOnce:true,preventable:false});l.once("initializedChange",function(){setTimeout(function(){l.fire(d,{dispatched:!!l._dispatched});},20);});},destructor:function(){if(f){this._history.detachAll();}else{a.detach("hashchange",this._afterHistoryChange,g);}},dispatch:function(){this.once(d,function(){var l=this._getHashPath();this._ready=true;if(f&&l&&l.charAt(0)==="/"){this._history.replace(null,{url:this._joinURL(l)});}else{this._dispatch(this._getPath());}});return this;},match:function(l){return j.filter(this._routes,function(m){return l.search(m.regex)>-1;});},replace:function(l){return this._queue(l,true);},route:function(m,n){var l=[];this._routes.push({callback:n,keys:l,path:m,regex:this._getRegex(m,l)});return this;},save:function(l){return this._queue(l);},_decode:function(l){return decodeURIComponent(l.replace(/\+/g," "));},_dequeue:function(){var l=this,m;if(!YUI.Env.windowLoaded){a.once("load",function(){l._dequeue();});return this;}m=i.shift();return m?m():this;},_dispatch:function(p){var m=this,l=m.match(p),o;m._dispatching=m._dispatched=true;if(!l||!l.length){return m;}o=m._getRequest(p);function n(r){var t,s,q;if(r){a.error(r);}else{if((q=l.shift())){s=q.regex.exec(p);t=typeof q.callback==="string"?m[q.callback]:q.callback;if(s.length===q.keys.length+1){o.params=j.hash(q.keys,s.slice(1));}else{o.params=s.concat();}t.call(m,o,n);}}}n();m._dispatching=false;return m._dequeue();},_getHashPath:function(){return h.getHash().replace(this._regexUrlQuery,"");},_getPath:f?function(){return this._removeRoot(k.pathname);}:function(){return this._getHashPath()||this._removeRoot(k.pathname);},_getQuery:f?function(){return k.search.substring(1);}:function(){var m=h.getHash(),l=m.match(this._regexUrlQuery);return m&&l?l[1]:k.search.substring(1);},_getRegex:function(m,l){if(m instanceof RegExp){return m;}m=m.replace(this._regexPathParam,function(o,n,p){l.push(p);return n==="*"?"(.*?)":"([^/]*)";});return new RegExp("^"+m+"$");},_getRequest:function(l){return{path:l,query:this._parseQuery(this._getQuery())};},_joinURL:function(m){var l=this.root;if(m.charAt(0)==="/"){m=m.substring(1);}return l&&l.charAt(l.length-1)==="/"?l+m:l+"/"+m;},_parseQuery:b&&b.parse?b.parse:function(o){var p=this._decode,r=o.split("&"),n=0,m=r.length,l={},q;for(;n<m;++n){q=r[n].split("=");if(q[0]){l[p(q[0])]=p(q[1]||"");}}return l;},_queue:function(){var m=arguments,l=this;i.push(function(){if(f){setTimeout(function(){l._save.apply(l,m);},1);}else{l._dispatching=true;l._save.apply(l,m);}return this;});return !this._dispatching?this._dequeue():this;},_removeRoot:function(m){var l=this.root;if(l&&m.indexOf(l)===0){m=m.substring(l.length);}return m.charAt(0)==="/"?m:"/"+m;},_save:f?function(l,m){this._ready=true;this._history[m?"replace":"add"](null,{url:typeof l==="string"?this._joinURL(l):l});return this;}:function(l,m){this._ready=true;if(typeof l==="string"&&l.charAt(0)!=="/"){l="/"+l;}h[m?"replaceHash":"setHash"](l);return this;},_afterHistoryChange:function(m){var l=this;if(l._ready){l._dispatch(l._getPath());}},_defReadyFn:function(l){this._ready=true;}},{NAME:"controller"});},"@VERSION@",{requires:["array-extras","base-build","history"],optional:["querystring-parse"]});