Team:WITS-CSIR SA/js/lib/sammy-latest.min.js

From 2011.igem.org

// -- Sammy.js -- /sammy.js // http://sammyjs.org // Version: 0.6.3 // Built: 2011-01-27 10:31:14 -0800 (function(h,j){var o,g="([^/]+)",k=/:([\w\d]+)/g,l=/\?([^#]*)$/,c=function(p){return Array.prototype.slice.call(p)},d=function(p){return Object.prototype.toString.call(p)==="[object Function]"},m=function(p){return Object.prototype.toString.call(p)==="[object Array]"},i=function(p){return decodeURIComponent(p.replace(/\+/g," "))},b=encodeURIComponent,f=function(p){return String(p).replace(/&(?!\w+;)/g,"&").replace(/</g,"<").replace(/>/g,">").replace(/"/g,""")},n=function(p){return function(q,r){return this.route.apply(this,[p,q,r])}},a={},e=[];o=function(){var q=c(arguments),r,p;o.apps=o.apps||{};if(q.length===0||q[0]&&d(q[0])){return o.apply(o,["body"].concat(q))}else{if(typeof(p=q.shift())=="string"){r=o.apps[p]||new o.Application();r.element_selector=p;if(q.length>0){h.each(q,function(s,t){r.use(t)})}if(r.element_selector!=p){delete o.apps[p]}o.apps[r.element_selector]=r;return r}}};o.VERSION="0.6.3";o.addLogger=function(p){e.push(p)};o.log=function(){var p=c(arguments);p.unshift("["+Date()+"]");h.each(e,function(r,q){q.apply(o,p)})};if(typeof j.console!="undefined"){if(d(j.console.log.apply)){o.addLogger(function(){j.console.log.apply(j.console,arguments)})}else{o.addLogger(function(){j.console.log(arguments)})}}else{if(typeof console!="undefined"){o.addLogger(function(){console.log.apply(console,arguments)})}}h.extend(o,{makeArray:c,isFunction:d,isArray:m});o.Object=function(p){return h.extend(this,p||{})};h.extend(o.Object.prototype,{escapeHTML:f,h:f,toHash:function(){var p={};h.each(this,function(r,q){if(!d(q)){p[r]=q}});return p},toHTML:function(){var p="";h.each(this,function(r,q){if(!d(q)){p+=""+r+" "+q+"
"}});return p},keys:function(p){var q=[];for(var r in this){if(!d(this[r])||!p){q.push(r)}}return q},has:function(p){return this[p]&&h.trim(this[p].toString())!=""},join:function(){var q=c(arguments);var p=q.shift();return q.join(p)},log:function(){o.log.apply(o,arguments)},toString:function(p){var q=[];h.each(this,function(s,r){if(!d(r)||p){q.push('"'+s+'": '+r.toString())}});return"Sammy.Object: {"+q.join(",")+"}"}});o.HashLocationProxy=function(q,p){this.app=q;this.is_native=false;this._startPolling(p)};o.HashLocationProxy.prototype={bind:function(){var p=this,q=this.app;h(j).bind("hashchange."+this.app.eventNamespace(),function(s,r){if(p.is_native===false&&!r){o.log("native hash change exists, using");p.is_native=true;j.clearInterval(o.HashLocationProxy._interval)}q.trigger("location-changed")});if(!o.HashLocationProxy._bindings){o.HashLocationProxy._bindings=0}o.HashLocationProxy._bindings++},unbind:function(){h(j).unbind("hashchange."+this.app.eventNamespace());o.HashLocationProxy._bindings--;if(o.HashLocationProxy._bindings<=0){j.clearInterval(o.HashLocationProxy._interval)}},getLocation:function(){var p=j.location.toString().match(/^[^#]*(#.+)$/);return p?p[1]:""},setLocation:function(p){return(j.location=p)},_startPolling:function(r){var q=this;if(!o.HashLocationProxy._interval){if(!r){r=10}var p=function(){var s=q.getLocation();if(!o.HashLocationProxy._last_location||s!=o.HashLocationProxy._last_location){j.setTimeout(function(){h(j).trigger("hashchange",[true])},13)}o.HashLocationProxy._last_location=s};p();o.HashLocationProxy._interval=j.setInterval(p,r)}}};o.Application=function(p){var q=this;this.routes={};this.listeners=new o.Object({});this.arounds=[];this.befores=[];this.namespace=(new Date()).getTime()+"-"+parseInt(Math.random()*1000,10);this.context_prototype=function(){o.EventContext.apply(this,arguments)};this.context_prototype.prototype=new o.EventContext();if(d(p)){p.apply(this,[this])}if(!this._location_proxy){this.setLocationProxy(new o.HashLocationProxy(this,this.run_interval_every))}if(this.debug){this.bindToAllEvents(function(s,r){q.log(q.toString(),s.cleaned_type,r||{})})}};o.Application.prototype=h.extend({},o.Object.prototype,{ROUTE_VERBS:["get","post","put","delete"],APP_EVENTS:["run","unload","lookup-route","run-route","route-found","event-context-before","event-context-after","changed","error","check-form-submission","redirect","location-changed"],_last_route:null,_location_proxy:null,_running:false,element_selector:"body",debug:false,raise_errors:false,run_interval_every:50,template_engine:null,toString:function(){return"Sammy.Application:"+this.element_selector},$element:function(p){return p?h(this.element_selector).find(p):h(this.element_selector)},use:function(){var p=c(arguments),r=p.shift(),q=r||"";try{p.unshift(this);if(typeof r=="string"){q="Sammy."+r;r=o[r]}r.apply(this,p)}catch(s){if(typeof r==="undefined"){this.error("Plugin Error: called use() but plugin ("+q.toString()+") is not defined",s)}else{if(!d(r)){this.error("Plugin Error: called use() but '"+q.toString()+"' is not a function",s)}else{this.error("Plugin Error",s)}}}return this},setLocationProxy:function(p){var q=this._location_proxy;this._location_proxy=p;if(this.isRunning()){if(q){q.unbind()}this._location_proxy.bind()}},route:function(t,q,v){var s=this,u=[],p,r;if(!v&&d(q)){q=t;v=q;t="any"}t=t.toLowerCase();if(q.constructor==String){k.lastIndex=0;while((r=k.exec(q))!==null){u.push(r[1])}q=new RegExp("^"+q.replace(k,g)+"$")}if(typeof v=="string"){v=s[v]}p=function(w){var x={verb:w,path:q,callback:v,param_names:u};s.routes[w]=s.routes[w]||[];s.routes[w].push(x)};if(t==="any"){h.each(this.ROUTE_VERBS,function(x,w){p(w)})}else{p(t)}return this},get:n("get"),post:n("post"),put:n("put"),del:n("delete"),any:n("any"),mapRoutes:function(q){var p=this;h.each(q,function(r,s){p.route.apply(p,s)});return this},eventNamespace:function(){return["sammy-app",this.namespace].join("-")},bind:function(p,r,t){var s=this;if(typeof t=="undefined"){t=r}var q=function(){var w,u,v;w=arguments[0];v=arguments[1];if(v&&v.context){u=v.context;delete v.context}else{u=new s.context_prototype(s,"bind",w.type,v,w.target)}w.cleaned_type=w.type.replace(s.eventNamespace(),"");t.apply(u,[w,v])};if(!this.listeners[p]){this.listeners[p]=[]}this.listeners[p].push(q);if(this.isRunning()){this._listen(p,q)}return this},trigger:function(p,q){this.$element().trigger([p,this.eventNamespace()].join("."),[q]);return this},refresh:function(){this.last_location=null;this.trigger("location-changed");return this},before:function(p,q){if(d(p)){q=p;p={}}this.befores.push([p,q]);return this},after:function(p){return this.bind("event-context-after",p)},around:function(p){this.arounds.push(p);return this},isRunning:function(){return this._running},helpers:function(p){h.extend(this.context_prototype.prototype,p);return this},helper:function(p,q){this.context_prototype.prototype[p]=q;return this},run:function(p){if(this.isRunning()){return false}var q=this;h.each(this.listeners.toHash(),function(r,s){h.each(s,function(u,t){q._listen(r,t)})});this.trigger("run",{start_url:p});this._running=true;this.last_location=null;if(this.getLocation()==""&&typeof p!="undefined"){this.setLocation(p)}this._checkLocation();this._location_proxy.bind();this.bind("location-changed",function(){q._checkLocation()});this.bind("submit",function(s){var r=q._checkFormSubmission(h(s.target).closest("form"));return(r===false)?s.preventDefault():false});h(j).bind("beforeunload",function(){q.unload()});return this.trigger("changed")},unload:function(){if(!this.isRunning()){return false}var p=this;this.trigger("unload");this._location_proxy.unbind();this.$element().unbind("submit").removeClass(p.eventNamespace());h.each(this.listeners.toHash(),function(q,r){h.each(r,function(t,s){p._unlisten(q,s)})});this._running=false;return this},bindToAllEvents:function(q){var p=this;h.each(this.APP_EVENTS,function(r,s){p.bind(s,q)});h.each(this.listeners.keys(true),function(s,r){if(p.APP_EVENTS.indexOf(r)==-1){p.bind(r,q)}});return this},routablePath:function(p){return p.replace(l,"")},lookupRoute:function(s,q){var r=this,p=false;this.trigger("lookup-route",{verb:s,path:q});if(typeof this.routes[s]!="undefined"){h.each(this.routes[s],function(u,t){if(r.routablePath(q).match(t.path)){p=t;return false}})}return p},runRoute:function(r,E,t,w){var s=this,C=this.lookupRoute(r,E),q,z,u,y,D,A,x,B,p;this.log("runRoute",[r,E].join(" "));this.trigger("run-route",{verb:r,path:E,params:t});if(typeof t=="undefined"){t={}}h.extend(t,this._parseQueryString(E));if(C){this.trigger("route-found",{route:C});if((B=C.path.exec(this.routablePath(E)))!==null){B.shift();h.each(B,function(F,G){if(C.param_names[F]){t[C.param_names[F]]=i(G)}else{if(!t.splat){t.splat=[]}t.splat.push(i(G))}})}q=new this.context_prototype(this,r,E,t,w);u=this.arounds.slice(0);D=this.befores.slice(0);x=[q].concat(t.splat);z=function(){var F;while(D.length>0){A=D.shift();if(s.contextMatchesOptions(q,A[0])){F=A[1].apply(q,[q]);if(F===false){return false}}}s.last_route=C;q.trigger("event-context-before",{context:q});F=C.callback.apply(q,x);q.trigger("event-context-after",{context:q});return F};h.each(u.reverse(),function(F,G){var H=z;z=function(){return G.apply(q,[H])}});try{p=z()}catch(v){this.error(["500 Error",r,E].join(" "),v)}return p}else{return this.notFound(r,E)}},contextMatchesOptions:function(s,u,q){var r=u;if(typeof r==="undefined"||r=={}){return true}if(typeof q==="undefined"){q=true}if(typeof r==="string"||d(r.test)){r={path:r}}if(r.only){return this.contextMatchesOptions(s,r.only,true)}else{if(r.except){return this.contextMatchesOptions(s,r.except,false)}}var p=true,t=true;if(r.path){if(d(r.path.test)){p=r.path.test(s.path)}else{p=(r.path.toString()===s.path)}}if(r.verb){t=r.verb===s.verb}return q?(t&&p):!(t&&p)},getLocation:function(){return this._location_proxy.getLocation()},setLocation:function(p){return this._location_proxy.setLocation(p)},swap:function(p){return this.$element().html(p)},templateCache:function(p,q){if(typeof q!="undefined"){return a[p]=q}else{return a[p]}},clearTemplateCache:function(){return a={}},notFound:function(r,q){var p=this.error(["404 Not Found",r,q].join(" "));return(r==="get")?p:true},error:function(q,p){if(!p){p=new Error()}p.message=[q,p.message].join(" ");this.trigger("error",{message:p.message,error:p});if(this.raise_errors){throw (p)}else{this.log(p.message,p)}},_checkLocation:function(){var p,q;p=this.getLocation();if(!this.last_location||this.last_location[0]!="get"||this.last_location[1]!=p){this.last_location=["get",p];q=this.runRoute("get",p)}return q},_getFormVerb:function(r){var q=h(r),s,p;p=q.find('input[name="_method"]');if(p.length>0){s=p.val()}if(!s){s=q[0].getAttribute("method")}if(!s||s==""){s="get"}return h.trim(s.toString().toLowerCase())},_checkFormSubmission:function(r){var p,s,u,t,q;this.trigger("check-form-submission",{form:r});p=h(r);s=p.attr("action");u=this._getFormVerb(p);this.log("_checkFormSubmission",p,s,u);if(u==="get"){this.setLocation(s+"?"+this._serializeFormParams(p));q=false}else{t=h.extend({},this._parseFormParams(p));q=this.runRoute(u,s,t,r.get(0))}return(typeof q=="undefined")?false:q},_serializeFormParams:function(q){var s="",p=q.serializeArray(),r;if(p.length>0){s=this._encodeFormPair(p[0].name,p[0].value);for(r=1;r<p.length;r++){s=s+"&"+this._encodeFormPair(p[r].name,p[r].value)}}return s},_encodeFormPair:function(p,q){return b(p)+"="+b(q)},_parseFormParams:function(p){var s={},r=p.serializeArray(),q;for(q=0;q<r.length;q++){s=this._parseParamPair(s,r[q].name,r[q].value)}return s},_parseQueryString:function(s){var u={},r,q,t,p;r=s.match(l);if(r){q=r[1].split("&");for(p=0;p<q.length;p++){t=q[p].split("=");u=this._parseParamPair(u,i(t[0]),i(t[1]))}}return u},_parseParamPair:function(r,p,q){if(r[p]){if(m(r[p])){r[p].push(q)}else{r[p]=[r[p],q]}}else{r[p]=q}return r},_listen:function(p,q){return this.$element().bind([p,this.eventNamespace()].join("."),q)},_unlisten:function(p,q){return this.$element().unbind([p,this.eventNamespace()].join("."),q)}});o.RenderContext=function(p){this.event_context=p;this.callbacks=[];this.previous_content=null;this.content=null;this.next_engine=false;this.waiting=false};o.RenderContext.prototype=h.extend({},o.Object.prototype,{then:function(r){if(!d(r)){if(typeof r==="string"&&r in this.event_context){var q=this.event_context[r];r=function(s){return q.apply(this.event_context,[s])}}else{return this}}var p=this;if(this.waiting){this.callbacks.push(r)}else{this.wait();j.setTimeout(function(){var s=r.apply(p,[p.content,p.previous_content]);if(s!==false){p.next(s)}},13)}return this},wait:function(){this.waiting=true},next:function(p){this.waiting=false;if(typeof p!=="undefined"){this.previous_content=this.content;this.content=p}if(this.callbacks.length>0){this.then(this.callbacks.shift())}},load:function(p,q,s){var r=this;return this.then(function(){var t,u,w,v;if(d(q)){s=q;q={}}else{q=h.extend({},q)}if(s){this.then(s)}if(typeof p==="string"){w=(p.match(/\.json$/)||q.json);t=((w&&q.cache===true)||q.cache!==false);r.next_engine=r.event_context.engineFor(p);delete q.cache;delete q.json;if(q.engine){r.next_engine=q.engine;delete q.engine}if(t&&(u=this.event_context.app.templateCache(p))){return u}this.wait();h.ajax(h.extend({url:p,data:{},dataType:w?"json":null,type:"get",success:function(x){if(t){r.event_context.app.templateCache(p,x)}r.next(x)}},q));return false}else{if(p.nodeType){return p.innerHTML}if(p.selector){r.next_engine=p.attr("data-engine");if(q.clone===false){return p.remove()[0].innerHTML.toString()}else{return p[0].innerHTML.toString()}}}})},render:function(p,q,r){if(d(p)&&!q){return this.then(p)}else{if(!q&&this.content){q=this.content}return this.load(p).interpolate(q,p).then(r)}},partial:function(p,q){return this.render(p,q).swap()},send:function(){var r=this,q=c(arguments),p=q.shift();if(m(q[0])){q=q[0]}return this.then(function(s){q.push(function(t){r.next(t)});r.wait();p.apply(p,q);return false})},collect:function(t,s,p){var r=this;var q=function(){if(d(t)){s=t;t=this.content}var u=[],v=false;h.each(t,function(w,y){var x=s.apply(r,[w,y]);if(x.jquery&&x.length==1){x=x[0];v=true}u.push(x);return x});return v?u:u.join("")};return p?q():this.then(q)},renderEach:function(p,q,r,s){if(m(q)){s=r;r=q;q=null}return this.load(p).then(function(u){var t=this;if(!r){r=m(this.previous_content)?this.previous_content:[]}if(s){h.each(r,function(v,x){var y={},w=this.next_engine||p;q?(y[q]=x):(y=x);s(x,t.event_context.interpolate(u,y,w))})}else{return this.collect(r,function(v,x){var y={},w=this.next_engine||p;q?(y[q]=x):(y=x);return this.event_context.interpolate(u,y,w)},true)}})},interpolate:function(s,r,p){var q=this;return this.then(function(u,t){if(!s&&t){s=t}if(this.next_engine){r=this.next_engine;this.next_engine=false}var v=q.event_context.interpolate(u,s,r);return p?t+v:v})},swap:function(){return this.then(function(p){this.event_context.swap(p)}).trigger("changed",{})},appendTo:function(p){return this.then(function(q){h(p).append(q)}).trigger("changed",{})},prependTo:function(p){return this.then(function(q){h(p).prepend(q)}).trigger("changed",{})},replace:function(p){return this.then(function(q){h(p).html(q)}).trigger("changed",{})},trigger:function(p,q){return this.then(function(r){if(typeof q=="undefined"){q={content:r}}this.event_context.trigger(p,q)})}});o.EventContext=function(t,s,q,r,p){this.app=t;this.verb=s;this.path=q;this.params=new o.Object(r);this.target=p};o.EventContext.prototype=h.extend({},o.Object.prototype,{$element:function(){return this.app.$element(c(arguments).shift())},engineFor:function(r){var q=this,p;if(d(r)){return r}r=(r||q.app.template_engine).toString();if((p=r.match(/\.([^\.]+)$/))){r=p[1]}if(r&&d(q[r])){return q[r]}if(q.app.template_engine){return this.engineFor(q.app.template_engine)}return function(s,t){return s}},interpolate:function(q,r,p){return this.engineFor(p).apply(this,[q,r])},render:function(p,q,r){return new o.RenderContext(this).render(p,q,r)},renderEach:function(p,q,r,s){return new o.RenderContext(this).renderEach(p,q,r,s)},load:function(p,q,r){return new o.RenderContext(this).load(p,q,r)},partial:function(p,q){return new o.RenderContext(this).partial(p,q)},send:function(){var p=new o.RenderContext(this);return p.send.apply(p,arguments)},redirect:function(){var r,q=c(arguments),p=this.app.getLocation();if(q.length>1){q.unshift("/");r=this.join.apply(this,q)}else{r=q[0]}this.trigger("redirect",{to:r});this.app.last_location=[this.verb,this.path];this.app.setLocation(r);if(p==r){this.app.trigger("location-changed")}},trigger:function(p,q){if(typeof q=="undefined"){q={}}if(!q.context){q.context=this}return this.app.trigger(p,q)},eventNamespace:function(){return this.app.eventNamespace()},swap:function(p){return this.app.swap(p)},notFound:function(){return this.app.notFound(this.verb,this.path)},json:function(p){return h.parseJSON(p)},toString:function(){return"Sammy.EventContext: "+[this.verb,this.path,this.params].join(" ")}});h.sammy=j.Sammy=o})(jQuery,window);