UNPKG

hele-router

Version:
1 lines 6.2 kB
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports,require("hele")):"function"==typeof define&&define.amd?define(["exports","hele"],e):e(t.HEle=t.HEle||{},t.HEle)}(this,function(t,e){"use strict";var r=function(t){return t[t.length-1]};function _classCallCheck(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function _defineProperties(t,e){for(var r=0;r<e.length;r++){var n=e[r];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(t,n.key,n)}}function _createClass(t,e,r){return e&&_defineProperties(t.prototype,e),r&&_defineProperties(t,r),t}function _defineProperty(t,e,r){return e in t?Object.defineProperty(t,e,{value:r,enumerable:!0,configurable:!0,writable:!0}):t[e]=r,t}function _inherits(t,e){if("function"!=typeof e&&null!==e)throw new TypeError("Super expression must either be null or a function");t.prototype=Object.create(e&&e.prototype,{constructor:{value:t,writable:!0,configurable:!0}}),e&&_setPrototypeOf(t,e)}function _getPrototypeOf(t){return(_getPrototypeOf=Object.setPrototypeOf?Object.getPrototypeOf:function(t){return t.__proto__||Object.getPrototypeOf(t)})(t)}function _setPrototypeOf(t,e){return(_setPrototypeOf=Object.setPrototypeOf||function(t,e){return t.__proto__=e,t})(t,e)}function _objectWithoutProperties(t,e){if(null==t)return{};var r,n,o=function(t,e){if(null==t)return{};var r,n,o={},i=Object.keys(t);for(n=0;n<i.length;n++)r=i[n],e.indexOf(r)>=0||(o[r]=t[r]);return o}(t,e);if(Object.getOwnPropertySymbols){var i=Object.getOwnPropertySymbols(t);for(n=0;n<i.length;n++)r=i[n],e.indexOf(r)>=0||Object.prototype.propertyIsEnumerable.call(t,r)&&(o[r]=t[r])}return o}function _assertThisInitialized(t){if(void 0===t)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return t}function _possibleConstructorReturn(t,e){return!e||"object"!=typeof e&&"function"!=typeof e?_assertThisInitialized(t):e}var n=function(t){function Router(t,e){var r;return _classCallCheck(this,Router),(r=_possibleConstructorReturn(this,_getPrototypeOf(Router).call(this,t,e))).name=t.name,r}return _inherits(Router,e.Component),_createClass(Router,[{key:"render",value:function(){return e.createElement(e.Context,{value:_defineProperty({},this.name,this)},this.props.children)}}]),Router}();n.defaultProps={name:"router"};var o=function(t){function HistoryRouter(t,e){var r;return _classCallCheck(this,HistoryRouter),(r=_possibleConstructorReturn(this,_getPrototypeOf(HistoryRouter).call(this,t,e))).onpopstate=function(t){r.update(location.pathname)},r.state=location.pathname,r}return _inherits(HistoryRouter,n),_createClass(HistoryRouter,[{key:"onDidMount",value:function(){window.addEventListener("popstate",this.onpopstate)}},{key:"onWillUnmount",value:function(){window.removeEventListener("popstate",this.onpopstate)}},{key:"push",value:function(t){this.props.noHash||-1!==t.lastIndexOf("#")||(t+=location.hash),history.pushState(null,"",t),this.update(location.pathname)}},{key:"pop",value:function(){history.back()}}]),HistoryRouter}(),i=Array(location.hash),s=function(t){function HashRouter(t,e){var n;return _classCallCheck(this,HashRouter),(n=_possibleConstructorReturn(this,_getPrototypeOf(HashRouter).call(this,t,e))).onhashchange=function(){n.update(location.hash.slice(1))},n.state=r(i).slice(1),n}return _inherits(HashRouter,n),_createClass(HashRouter,[{key:"onDidMount",value:function(){window.addEventListener("hashchange",this.onhashchange)}},{key:"onWillUnmount",value:function(){window.removeEventListener("hashchange",this.onhashchange)}},{key:"push",value:function(t){var e="#"+t;r(i)!==e&&(location.hash=e,i.push(e))}},{key:"pop",value:function(){if(i.length>1){i.pop();var t=r(i);location.hash=t}}}]),HashRouter}(),u=function(t){function Switch(t,e){var r;return _classCallCheck(this,Switch),(r=_possibleConstructorReturn(this,_getPrototypeOf(Switch).call(this,t,e)))._history=[],r._history.push(r.state=t.path),r}return _inherits(Switch,n),_createClass(Switch,[{key:"push",value:function(t){var e=this._history;r(e)!==t&&(e.push(t),this.update(t))}},{key:"pop",value:function(){var t=this._history;if(t.length>1){t.pop();var e=r(t);this.update(e)}}}]),Switch}();function testRoute(t,e){return arguments.length>2&&void 0!==arguments[2]&&arguments[2]?t===e:t.indexOf(e)>=0}u.defaultProps=function(t){for(var e=1;e<arguments.length;e++){var r=null!=arguments[e]?arguments[e]:{},n=Object.keys(r);"function"==typeof Object.getOwnPropertySymbols&&(n=n.concat(Object.getOwnPropertySymbols(r).filter(function(t){return Object.getOwnPropertyDescriptor(r,t).enumerable}))),n.forEach(function(e){_defineProperty(t,e,r[e])})}return t}({},n.defaultProps,{path:"/"});var a=function(t){function Route(t,e){var r;return _classCallCheck(this,Route),(r=_possibleConstructorReturn(this,_getPrototypeOf(Route).call(this,t,e))).state=testRoute(e[t.router].state,t.path,t.exact),r}return _inherits(Route,e.Component),_createClass(Route,[{key:"render",value:function(){var t=this.props,r=this.state;return t.render?t.render(r):r&&(t.component?e.createElement(t.component):e.createElement(e.Fragment,null,this.props.children))}}]),Route}();a.defaultProps={router:"router"};var c=function(t){function Link(t,e){var r;return _classCallCheck(this,Link),(r=_possibleConstructorReturn(this,_getPrototypeOf(Link).call(this,t,e))).onclick=r.onclick.bind(_assertThisInitialized(_assertThisInitialized(r))),r}return _inherits(Link,e.Component),_createClass(Link,[{key:"render",value:function(){var t=this.props,r=(t.router,t.back,t.onclick,_objectWithoutProperties(t,["router","back","onclick"]));return e.createElement("a",Object.assign({},r,{onclick:this.onclick}),this.props.children)}},{key:"onclick",value:function(t){var e,r=this.props;if(r.onclick&&(e=r.onclick(t)),!t.defaultPrevented){t.preventDefault();var n=this.context[r.router];r.back?n.pop():r.href&&n.push(r.href)}return e}}]),Link}();c.defaultProps={href:"javascript:;",back:!1,router:"router"},t._last=r,t.defaultRouterName="router",t.Router=n,t.HistoryRouter=o,t.HashRouter=s,t.Switch=u,t.testRoute=testRoute,t.Route=a,t.Link=c,Object.defineProperty(t,"__esModule",{value:!0})});