-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathbone.min.js
More file actions
5 lines (4 loc) · 9.67 KB
/
bone.min.js
File metadata and controls
5 lines (4 loc) · 9.67 KB
1
2
3
4
5
/*!
* GIT: https://github.com/shrekshrek/bone
**/
!function(t,e){if("object"==typeof exports&&"undefined"!=typeof module){var n=require("jquery");module.exports=e(n)}else"function"==typeof define&&define.amd?define(["jquery"],e):t.Bone=e(t.$)}(this,function(t){"use strict";var e={$:t},n=[].slice,i=function(t){return"function"==typeof t},s=function(t,e,n){var s=null==t?void 0:t[e];return void 0===s&&(s=n),i(s)?s.call(t):s},r=function(t,e){return Function.prototype.bind.apply(t,n.call(arguments,1))};e.bind=r,e.extend=Object.assign;var a=function(t){var e=[];for(var n in t)e.push(n);return e},o=function(t){return null==t?0:void 0!==t.length?t.length:a(t).length},h=function(t){return null==t||(void 0!==t.length?0===t.length:0===a(t).length)},c=0,l=function(t){var e=++c+"";return t?t+e:e},u=e.Events={},f=/\s+/,d=function(t,e,n,i,s){var r,o=0;if(n&&"object"==typeof n){void 0!==i&&"context"in s&&void 0===s.context&&(s.context=i);for(r=a(n);o<r.length;o++)e=t(e,r[o],n[r[o]],s)}else if(n&&f.test(n))for(r=n.split(f);o<r.length;o++)e=t(e,r[o],i,s);else e=t(e,n,i,s);return e};u.on=function(t,e,n){return g(this,t,e,n)};var g=function(t,e,n,i,s){if(t._events=d(v,t._events||{},e,n,{context:i,ctx:t,listening:s}),s){(t._listeners||(t._listeners={}))[s.id]=s}return t};u.listenTo=function(t,e,n){if(!t)return this;var i=t._listenId||(t._listenId=l("l")),s=this._listeningTo||(this._listeningTo={}),r=s[i];if(!r){var a=this._listenId||(this._listenId=l("l"));r=s[i]={obj:t,objId:i,id:a,listeningTo:s,count:0}}return g(t,e,n,this,r),this};var v=function(t,e,n,i){if(n){var s=t[e]||(t[e]=[]),r=i.context,a=i.ctx,o=i.listening;o&&o.count++,s.push({callback:n,context:r,ctx:r||a,listening:o})}return t};u.off=function(t,e,n){return this._events?(this._events=d(p,this._events,t,e,{context:n,listeners:this._listeners}),this):this},u.stopListening=function(t,e,n){var i=this._listeningTo;if(!i)return this;for(var s=t?[t._listenId]:a(i),r=0;r<s.length;r++){var o=i[s[r]];if(!o)break;o.obj.off(e,n,this)}return h(i)&&(this._listeningTo=void 0),this};var p=function(t,e,n,i){if(t){var s,r=0,h=i.context,c=i.listeners;if(e||n||h){for(var l=e?[e]:a(t);r<l.length;r++){e=l[r];var u=t[e];if(!u)break;for(var f=[],d=0;d<u.length;d++){var g=u[d];n&&n!==g.callback&&n!==g.callback._callback||h&&h!==g.context?f.push(g):(s=g.listening)&&0==--s.count&&(delete c[s.id],delete s.listeningTo[s.objId])}f.length?t[e]=f:delete t[e]}return o(t)?t:void 0}for(var v=a(c);r<v.length;r++)s=c[v[r]],delete c[s.id],delete s.listeningTo[s.objId]}};u.once=function(t,e,n){var i=d(_,{},t,e,r(this.off,this));return this.on(i,void 0,n)},u.listenToOnce=function(t,e,n){var i=d(_,{},e,n,r(this.stopListening,this,t));return this.listenTo(t,i)};var _=function(t,e,n,i){if(n){var s=t[e]=function(){i(e,s),n.apply(this,arguments)};s._callback=n}return t};u.trigger=function(t){if(!this._events)return this;for(var e=Math.max(0,arguments.length-1),n=Array(e),i=0;i<e;i++)n[i]=arguments[i+1];return d(m,this._events,t,void 0,n),this};var m=function(t,e,n,i){if(t){var s=t[e],r=t.all;s&&r&&(r=r.slice()),s&&y(s,i),r&&y(r,[e].concat(i))}return t},y=function(t,e){var n,i=-1,s=t.length,r=e[0],a=e[1],o=e[2];switch(e.length){case 0:for(;++i<s;)(n=t[i]).callback.call(n.ctx);return;case 1:for(;++i<s;)(n=t[i]).callback.call(n.ctx,r);return;case 2:for(;++i<s;)(n=t[i]).callback.call(n.ctx,r,a);return;case 3:for(;++i<s;)(n=t[i]).callback.call(n.ctx,r,a,o);return;default:for(;++i<s;)(n=t[i]).callback.apply(n.ctx,e);return}};e.extend(e,u);var x=e.Class=function(t){this.initialize.apply(this,arguments)};e.extend(x.prototype,u,{initialize:function(){}});var b=e.View=function(t){this.cid=l("view"),t||(t={});for(var e in E)t[E[e]]&&(this[E[e]]=t[E[e]]);this._ensureElement(),this.initialize.apply(this,arguments)},w=/^(\S+)\s*(.*)$/,E=["el","id","attributes","className","tagName","events"];e.extend(b.prototype,u,{tagName:"div",$:function(t){return this.$el.find(t)},initialize:function(){},render:function(){return this},remove:function(){return this._removeElement(),this.stopListening(),this},_removeElement:function(){this.$el.remove()},setElement:function(t){return this.undelegateEvents(),this._setElement(t),this.delegateEvents(),this},_setElement:function(t){this.$el=t instanceof e.$?t:e.$(t),this.el=this.$el[0]},delegateEvents:function(t){if(t||(t=s(this,"events")),!t)return this;this.undelegateEvents();for(var e in t){var n=t[e];if(i(n)||(n=this[n]),n){var a=e.match(w);this.delegate(a[1],a[2],r(n,this))}}return this},delegate:function(t,e,n){return this.$el.on(t+".delegateEvents"+this.cid,e,n),this},undelegateEvents:function(){return this.$el&&this.$el.off(".delegateEvents"+this.cid),this},undelegate:function(t,e,n){return this.$el.off(t+".delegateEvents"+this.cid,e,n),this},_createElement:function(t){return document.createElement(t)},_ensureElement:function(){if(this.el)this.setElement(s(this,"el"));else{var t=e.extend({},s(this,"attributes"));this.id&&(t.id=s(this,"id")),this.className&&(t.class=s(this,"className")),this.setElement(this._createElement(s(this,"tagName"))),this._setAttributes(t)}},_setAttributes:function(t){this.$el.attr(t)}});var $=e.Router=function(t){t||(t={}),t.routes&&(this.routes=t.routes),this._bindRoutes(),this.initialize.apply(this,arguments)},k=/\((.*?)\)/g,S=/(\(\?)?:\w+/g,P=/\*\w+/g,H=/[\-{}\[\]+?.,\\\^$|#\s]/g;e.extend($.prototype,u,{initialize:function(){},route:function(t,n,s){t=this._routeToRegExp(t),i(n)&&(s=n,n=""),s||(s=this[n]);var r=this;return e.history.route(t,function(i){var a=r._extractParameters(t,i);!1!==r.execute(s,a,n)&&(r.trigger.apply(r,["route:"+n].concat(a)),r.trigger("route",n,a),e.history.trigger("route",r,n,a))}),this},execute:function(t,e,n){t&&t.apply(this,e)},navigate:function(t,n){return e.history.navigate(t,n),this},_bindRoutes:function(){if(this.routes)for(var t,e=a(this.routes);null!=(t=e.pop());)this.route(t,this.routes[t])},_routeToRegExp:function(t){return t=t.replace(H,"\\$&").replace(k,"(?:$1)?").replace(S,function(t,e){return e?t:"([^/?]+)"}).replace(P,"([^?]*?)"),new RegExp("^"+t+"(?:\\?([\\s\\S]*))?$")},_extractParameters:function(t,e){var n=t.exec(e).slice(1),i=[];for(var s in n){var r=n[s];s===n.length-1?i[s]=r||null:i[s]=r?decodeURIComponent(r):null}return i}});var U=e.History=function(){this.handlers=[],this.checkUrl=r(this.checkUrl,this),"undefined"!=typeof window&&(this.location=window.location,this.history=window.history)},j=/^[#\/]|\s+$/g,C=/^\/+|\/+$/g,I=/#.*$/;U.started=!1,e.extend(U.prototype,u,{atRoot:function(){return this.location.pathname.replace(/[^\/]$/,"$&/")===this.root&&!this.getSearch()},matchRoot:function(){return this.decodeFragment(this.location.pathname).slice(0,this.root.length-1)+"/"===this.root},decodeFragment:function(t){return decodeURI(t.replace(/%25/g,"%2525"))},getSearch:function(){var t=this.location.href.replace(/#.*/,"").match(/\?.+/);return t?t[0]:""},getHash:function(t){var e=(t||this).location.href.match(/#(.*)$/);return e?e[1]:""},getPath:function(){var t=this.decodeFragment(this.location.pathname+this.getSearch()).slice(this.root.length-1);return"/"===t.charAt(0)?t.slice(1):t},getFragment:function(t){return null==t&&(t=this._usePushState||!this._wantsHashChange?this.getPath():this.getHash()),t.replace(j,"")},start:function(t){if(U.started)throw new Error("Bone.history has already been started");if(U.started=!0,this.options=e.extend({root:"/"},this.options,t),this.root=this.options.root,this._wantsHashChange=!1!==this.options.hashChange,this._hasHashChange="onhashchange"in window,this._useHashChange=this._wantsHashChange&&this._hasHashChange,this._wantsPushState=!!this.options.pushState,this._hasPushState=!(!this.history||!this.history.pushState),this._usePushState=this._wantsPushState&&this._hasPushState,this.fragment=this.getFragment(),this.root=("/"+this.root+"/").replace(C,"/"),this._wantsHashChange&&this._wantsPushState){if(!this._hasPushState&&!this.atRoot()){var n=this.root.slice(0,-1)||"/";return this.location.replace(n+"#"+this.getPath()),!0}this._hasPushState&&this.atRoot()&&this.navigate(this.getHash(),{replace:!0})}var i=window.addEventListener||function(t,e){return attachEvent("on"+t,e)};if(this._usePushState?i("popstate",this.checkUrl,!1):this._useHashChange&&i("hashchange",this.checkUrl,!1),!this.options.silent)return this.loadUrl()},stop:function(){var t=window.removeEventListener||function(t,e){return detachEvent("on"+t,e)};this._usePushState?t("popstate",this.checkUrl,!1):this._useHashChange&&t("hashchange",this.checkUrl,!1),this._checkUrlInterval&&clearInterval(this._checkUrlInterval),U.started=!1},route:function(t,e){this.handlers.unshift({route:t,callback:e})},checkUrl:function(t){if(this.getFragment()===this.fragment)return!1;this.loadUrl()},loadUrl:function(t){if(!this.matchRoot())return!1;t=this.fragment=this.getFragment(t);for(var e in this.handlers){var n=this.handlers[e];if(n.route.test(t))return n.callback(t),!0}},navigate:function(t,e){if(!U.started)return!1;e&&!0!==e||(e={trigger:!!e}),t=t.replace(I,"");var n=this.root;""!==t&&"?"!==t.charAt(0)||(n=n.slice(0,-1)||"/");var i=n+t;if(t=this.decodeFragment(t.replace(I,"")),this.fragment!==t){if(this.fragment=t,this._usePushState)this.history[e.replace?"replaceState":"pushState"]({},document.title,i);else{if(!this._wantsHashChange)return this.location.assign(i);this._updateHash(this.location,t,e.replace)}return e.trigger?this.loadUrl(t):void 0}},_updateHash:function(t,e,n){if(n){var i=t.href.replace(/(javascript:|#).*$/,"");t.replace(i+"#"+e)}else t.hash="#"+e}}),e.history=new U;var R=function(t,e){var n,i=this;n=t&&Object.prototype.hasOwnProperty.call(t,"constructor")?t.constructor:function(){return i.apply(this,arguments)},Object.assign(n,i,e);var s=function(){this.constructor=n};return s.prototype=i.prototype,n.prototype=new s,t&&Object.assign(n.prototype,t),n.__super__=i.prototype,n};return $.extend=U.extend=x.extend=b.extend=R,e});