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