Framework agnostic front-end router based on store pattern http://router.maniak.pro
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

1 lines
1.9 KiB

window.R={_l:location,_r:history.replaceState.bind(history),_p:history.pushState.bind(history),_d:decodeURIComponent,_e:encodeURIComponent,init:function(o){if(o==null){o={}}this.root||(this.root=o.root||"root");this.views||(this.views=o.views||window.Views||[this.root]);this.render||(this.render=o.render||window.render);this.h||(this.h=o.helpers||window._);window.onpopstate=this.url_changed.bind(this);this.read()},cache:{},getters:{},setters:{},param:function(k){var base;if(this.getters[k]){return(base=this.cache)[k]||(base[k]=this.getters[k](this._d(this.params[k]||"")))}else{return this._d(this.params[k]||"")}},write:function(){var i,j,k,l,v;for(i=j=0,l=arguments.length;j<l;i=++j){k=arguments[i];if(!(i%2)){v=arguments[i+1];this._write(k,v)}}this._r({},this._l.pathname,this.to_path(this.view,this.params));this.url_changed()},_write:function(k,v){var _v=this.setters[k]?this.setters[k](v):v;_v?this.params[k]=this._e(_v):delete this.params[k]},toggle:function(f,s){this.write(f,s!=null?s?1:void 0:!this.params[f]?1:void 0)},go:function(p){this._p({},null,p);this.url_changed()},read:function(){var ref;ref=this.split_path(this._l.hash),this.view=ref[0],this.params=ref[1];this._safe_params()},split_path:function(p){p=p.slice(2);if(!p.length){return [this.root,{}]}var list,params,view;list=this.h.compact(p.split("/"));view=this._existance(list.shift());params=this.h.fromPairs(this.h.chunk(list,2));return[view,params]},_existance:function(v){if(this.h.includes(this.views,v)){return v}else{return"not_found"}},_safe_params:function(){return this.safe_params=this.h.fromPairs(this.h.reject(this.h.toPairs(this.params),function(p){return/^_/.test(p[0])}))},to_path:function(v,p){var a;if(v==null){v=this.view}if(p==null){p=this.safe_params}a=this.h.flatten(this.h.reject(this.h.toPairs(p),function(p){return!p[1]}));return"#/"+v+"/"+a.join("/")},url_changed:function(){this.cache={};this.read();this.render()}};