forked from techpines/bone.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bone.io.min.js
1 lines (1 loc) · 9.05 KB
/
bone.io.min.js
1
var bone,_ref;bone={},bone.modules={},null!=("undefined"!=typeof module&&null!==module?module.exports:void 0)?module.exports=bone:window.bone=bone,bone.$=window.$,bone.log=void 0,null!=(null!=(_ref=window.console)?_ref.log:void 0)&&-1!==window.location.href.indexOf("localhost")&&(bone.log=function(t){return console.log(t)}),bone.config={},bone.set=function(t,e){return"templates"===t&&(bone.templates=e),"log"===t&&e===!1&&(bone.log=void 0),bone.config[t]=e},bone.get=function(t){return bone.config[t]},bone.async={},bone.async.eachSeries=function(t,e,n){var o,r;return n=n||function(){},t.length?(o=0,r=function(){return e(t[o],function(e){return e?(n(e),n=function(){}):(o+=1,o>=t.length?n(null):r())})},r()):n()};var adapters,contextStore,messageId;messageId=0,contextStore={},bone.io=function(t,e){var n,o;return n=null!=(o=e.config)?o.adapter:void 0,null==n&&(n="socket.io"),bone.io.adapters[n](t,e)},bone.io.defaults={},bone.io.set=function(t,e){return bone.io.defaults[t]=e},adapters=bone.io.adapters={},adapters["socket.io"]=function(t,e){var n,o,r,i,a,s,u,l,h,c,d,p,f,g,b,m,v,w,y,$;for(n={},null==(p=e.config)&&(e.config=bone.get("io.options")),n.error=e.error,n.source=t,n.options=e,n.socket=e.config.socket,n.inbound=e.inbound,null==(f=n.inbound)&&(n.inbound={}),n.outbound=e.outbound,null==(g=n.outbound)&&(n.outbound={}),null==(b=(i=n.inbound).middleware)&&(i.middleware=[]),null==(m=(a=n.outbound).middleware)&&(a.middleware=[]),null==(v=(s=n.outbound).routes)&&(s.routes=[]),null==(w=(u=n.inbound).routes)&&(u.routes=[]),y=n.outbound.routes,l=function(e){return n[e]=function(o,r){return null==r&&(r={}),null!=bone.log&&bone.log("Outbound: ["+t+":"+e+"]",o),r.mid=messageId+=1,contextStore[r.mid]=r,bone.async.eachSeries(n.outbound.middleware,function(t,e){return t(o,r,e)},function(i){return null!=i&&null!=n.error?n.error(i):n.socket.emit(""+t+":"+e,{mid:r.mid,data:o})})}},c=0,d=y.length;d>c;c++)r=y[c],l(r);$=n.inbound,h=function(e,o){return n.socket.on(""+t+":"+e,function(r){var i,a,s;return a=r.data,s=r.mid,null!=bone.log&&bone.log("Inbound: ["+t+":"+e+"]",a),i=contextStore[s],delete contextStore[s],null==i&&(i={}),i.route=e,i.data=a,i.namespace=t,i.socket=n.socket,bone.async.eachSeries(n.inbound.middleware,function(t,e){return t(a,i,e)},function(t){return null!=t&&null!=n.error?n.error(t):o.apply(n,[a,i])})})};for(o in $)r=$[o],"middleware"!==o&&h(o,r);return n};var extend,isExplorer,rootStripper,routeStripper,trailingSlash;extend=function(t){var e,n,o,r,i;for(i=Array.prototype.slice.call(arguments,1),o=0,r=i.length;r>o;o++)if(n=i[o])for(e in n)t[e]=n[e];return t},routeStripper=/^[#\/]|\s+$/g,rootStripper=/^\/+|\/+$/g,isExplorer=/msie [\w.]+/,trailingSlash=/\/$/,bone.History=function(){function t(){"undefined"!=typeof window&&(this.location=window.location,this.history=window.history)}return t.prototype.interval=50,t.prototype.getHash=function(t){var e;return e=(t||this).location.href.match(/#(.*)$/),e?e[1]:""},t.prototype.getFragment=function(t,e){var n;return null==t&&(this._hasPushState||!this._wantsHashChange||e?(t=this.location.pathname,n=this.root.replace(trailingSlash,""),t.indexOf(n)||(t=t.substr(n.length))):t=this.getHash()),t.replace(routeStripper,"")},t.prototype.start=function(t){var e,n,o,r,i,a=this;return this.options=extend({},{root:"/"},this.options,t),this.root=this.options.root,this._wantsHashChange=this.options.hashChange!==!1,this._wantsPushState=!!this.options.pushState,this._hasPushState=!!(this.options.pushState&&this.history&&this.history.pushState),o=this.getFragment(),n=document.documentMode,i=isExplorer.exec(navigator.userAgent.toLowerCase())&&(!n||7>=n),this.root=("/"+this.root+"/").replace(rootStripper,"/"),i&&this._wantsHashChange&&(this.iframe=bone.$('<iframe src="javascript:0" tabindex="-1" />').hide().appendTo("body")[0].contentWindow,this.navigate(o)),this._hasPushState?bone.$(window).on("popstate",function(){return a.checkUrl.apply(a,arguments)}):this._wantsHashChange&&"onhashchange"in window&&!i?bone.$(window).on("hashchange",function(){return a.checkUrl.apply(a,arguments)}):this._wantsHashChange&&(this._checkUrlInterval=setInterval(this.checkUrl,this.interval)),this.fragment=o,r=this.location,e=r.pathname.replace(/[^\/]$/,"$&/")===this.root,this._wantsHashChange&&this._wantsPushState&&!this._hasPushState&&!e?(this.fragment=this.getFragment(null,!0),this.location.replace(this.root+this.location.search+"#"+this.fragment),!0):(this._wantsPushState&&this._hasPushState&&e&&r.hash&&(this.fragment=this.getHash().replace(routeStripper,""),this.history.replaceState({},document.title,this.root+this.fragment+r.search)),this.options.silent?void 0:this.loadUrl())},t.prototype.route=function(t,e){return this.handlers.unshift({route:t,callback:e})},t.prototype.checkUrl=function(){var t;return t=this.getFragment(),t===this.fragment&&this.iframe&&(t=this.getFragment(this.getHash(this.iframe))),t===this.fragment?!1:(this.iframe&&this.navigate(t),this.loadUrl()||this.loadUrl(this.getHash()))},t.prototype.handlers=[],t.prototype.loadUrl=function(t){var e,n,o,r,i,a,s,u;for(n=this.fragment=this.getFragment(t),s=this.handlers,i=0,a=s.length;a>i;i++)if(o=s[i],o.route.test(n))return e=o.route.exec(n).slice(1),null!=bone.log&&bone.log("Route: ["+o.route+":"+n+"]",e),null==(u=(r=o.router).middleware)&&(r.middleware=[]),bone.async.eachSeries(o.router.middleware,function(t,e){return t.apply({router:o.router,route:n},[]),e()},function(){return o.callback.apply(o.router,e)}),!0},t.prototype.navigate=function(t,e){var n;if(e&&e!==!0||(e={trigger:e}),t=this.getFragment(t||""),this.fragment!==t){if(this.fragment=t,n=this.root+t,this._hasPushState)this.history[e.replace?"replaceState":"pushState"]({},document.title,n);else{if(!this._wantsHashChange)return this.location.assign(n);this._updateHash(this.location,t,e.replace),this.iframe&&t!==this.getFragment(this.getHash(this.iframe))&&(e.replace||this.iframe.document.open().close(),this._updateHash(this.iframe.location,t,e.replace))}return e.trigger?this.loadUrl(t):void 0}},t.prototype._updateHash=function(t,e,n){var o;return n?(o=t.href.replace(/(javascript:|#).*$/,""),t.replace(o+"#"+e)):t.hash="#"+e},t}();var id,initView;id=0,initView=function(t,e,n){var o,r,i,a,s;o=$(t),i={},i.id=id+=1,o.attr("data-bone-id",id),i.data=function(){return o.data.apply(o,arguments)},i.$=function(){return o.find.apply(o,arguments)},i.templates=bone.templates,i.el=t,i.$el=o,s=function(t,e){return i[t]=function(){var o;return null!=bone.log&&(o="View: ["+n.selector+":"+t+"]",bone.log(o,i.el,arguments)),e.apply(i,arguments)}};for(a in n)r=n[a],"events"!==a&&("[object Function]"===Object.prototype.toString.call(r)?s(a,r):i[a]=r);return null!=n.initialize&&n.initialize.apply(i,[]),i},bone.view=function(t,e){var n,o,r,i,a,s,u,l;s={},s.$=function(n){var o,r,i,a,u,l,h,c,d;if("string"==typeof n)return u=""+t+n,bone.view(u,e);for(r=n,d=$(t),h=0,c=d.length;c>h;h++)if(l=d[h],o=$(l),a=$(l).data("bone-views"),null==a&&(a={},$(l).data("bone-views",a)),i=a[t],null==i&&(i=initView(l,s,e),o.data("bone-views")[t]=i),r===i.id)return i},e.selector=t,r=e.events,u=function(n,o){var r,i,a,u,l,h;return a=/^(\S+)\s*(.*)$/,l=n.match(a),i=l[1],h=l[2],u=t,null!=h&&(u+=" "+h),r=e[o],$(function(){return $("body").on(i,u,function(n){var o,a,l,h;return l=$(n.currentTarget).parents(t)[0],null==l&&(l=n.currentTarget),null!=bone.log&&(h="Interface: ["+u+":"+i+"]",bone.log(h,l)),a=$(l).data("bone-views"),null==a&&(a={},$(l).data("bone-views",a)),o=a[t],null==o&&(o=initView(l,s,e),$(l).data("bone-views")[t]=o),$.trim(t)!==$.trim(u)&&(l=$(u).parents(t)[0]),r.call(o,n)})})};for(o in r)i=r[o],"events"!==i&&u(o,i);l=function(n,o){return s[n]=function(){var r,i,a,u,l,h;for(r=arguments,l=$(t),h=[],a=0,u=l.length;u>a;a++)i=l[a],h.push(function(i){var a,u,l;return u=$(i).data("bone-views"),null==u&&(u={},$(i).data("bone-views",u)),a=u[t],null==a&&(a=initView(i,s,e),$(i).data("bone-views")[t]=a),null!=bone.log&&(l="View: ["+t+":"+n+"]",bone.log(l,i,r)),o.apply(a,r)}(i));return h}};for(a in e)n=e[a],"events"!==a&&("[object Function]"===Object.prototype.toString.call(n)?l(a,n):s[a]=n);return s};var routeToRegex;routeToRegex=function(t){var e,n,o,r;return o=/\((.*?)\)/g,n=/(\(\?)?:\w+/g,r=/\*\w+/g,e=/[\-{}\[\]+?.,\\\^$|#\s]/g,t=t.replace(e,"\\$&").replace(o,"(?:$1)?").replace(n,function(t,e){return e?t:"([^/]+)"}).replace(r,"(.*?)"),RegExp("^"+t+"$")},bone.router=function(t){var e,n;return null==(n=(e=bone.router).handlers)&&(e.handlers=[]),$(function(){var e,n,o;o=t.routes;for(n in o)e=o[n],"routes"!==n&&(n=routeToRegex(n),bone.router.handlers.push({route:n,callback:t[e],router:t}));return null!=t.initialize?t.initialize():void 0})},bone.router.start=function(t){return bone.$(function(){return bone.history=new bone.History,bone.history.handlers=bone.router.handlers,bone.history.start(t)})},bone.router.navigate=function(t,e){return bone.history.navigate(t,e)};var $;$=bone.$,bone.mount=function(t,e,n){var o,r,i,a,s,u,l,h;if(null==n&&(n={}),r=n.data,a=n.refresh,null==a&&(a=!1),o=$(t),l=bone.templates[e],h=null!=r?l(r):l(),0!==o.children().length){if(i=o.data("bone-mount"),null==i&&(i={}),u=i.template===e,s=i.data===r,u&&s&&!a)return!1;o.children().remove()}return o.html(h),o.data("bone-mount",{template:e,data:r}),!0};