UNPKG

hele-router

Version:
447 lines (358 loc) 11.4 kB
(function (global, factory) { typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('hele')) : typeof define === 'function' && define.amd ? define(['exports', 'hele'], factory) : (factory((global.HEle = global.HEle || {}),global.HEle)); }(this, (function (exports,hele) { 'use strict'; var _last = function _last(arr) { return arr[arr.length - 1]; }; function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; } function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; var ownKeys = Object.keys(source); if (typeof Object.getOwnPropertySymbols === 'function') { ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) { return Object.getOwnPropertyDescriptor(source, sym).enumerable; })); } ownKeys.forEach(function (key) { _defineProperty(target, key, source[key]); }); } return target; } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); } function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); } function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); } function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; } function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; } function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; } function _possibleConstructorReturn(self, call) { if (call && (typeof call === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); } var defaultRouterName = 'router'; var Router = /*#__PURE__*/ function (_Component) { _inherits(Router, _Component); function Router(props, context) { var _this; _classCallCheck(this, Router); _this = _possibleConstructorReturn(this, _getPrototypeOf(Router).call(this, props, context)); _this.name = props.name; return _this; } _createClass(Router, [{ key: "render", value: function render() { return hele.createElement(hele.Context, { value: _defineProperty({}, this.name, this) }, this.props.children); } }]); return Router; }(hele.Component); Router.defaultProps = { name: defaultRouterName }; var HistoryRouter = /*#__PURE__*/ function (_Router) { _inherits(HistoryRouter, _Router); function HistoryRouter(props, context) { var _this; _classCallCheck(this, HistoryRouter); _this = _possibleConstructorReturn(this, _getPrototypeOf(HistoryRouter).call(this, props, context)); _this.onpopstate = function (e) { _this.update(location.pathname); }; _this.state = location.pathname; return _this; } _createClass(HistoryRouter, [{ key: "onDidMount", value: function onDidMount() { window.addEventListener('popstate', this.onpopstate); } }, { key: "onWillUnmount", value: function onWillUnmount() { window.removeEventListener('popstate', this.onpopstate); } }, { key: "push", value: function push(path) { if (!this.props.noHash && path.lastIndexOf('#') === -1) { path += location.hash; } history.pushState(null, '', path); this.update(location.pathname); } }, { key: "pop", value: function pop() { history.back(); } }]); return HistoryRouter; }(Router); var hashHistory = new Array(location.hash); var HashRouter = /*#__PURE__*/ function (_Router) { _inherits(HashRouter, _Router); function HashRouter(props, context) { var _this; _classCallCheck(this, HashRouter); _this = _possibleConstructorReturn(this, _getPrototypeOf(HashRouter).call(this, props, context)); _this.onhashchange = function () { _this.update(location.hash.slice(1)); }; _this.state = _last(hashHistory).slice(1); return _this; } _createClass(HashRouter, [{ key: "onDidMount", value: function onDidMount() { window.addEventListener('hashchange', this.onhashchange); } }, { key: "onWillUnmount", value: function onWillUnmount() { window.removeEventListener('hashchange', this.onhashchange); } }, { key: "push", value: function push(path) { var hash = '#' + path; if (_last(hashHistory) !== hash) { location.hash = hash; hashHistory.push(hash); } } }, { key: "pop", value: function pop() { if (hashHistory.length > 1) { hashHistory.pop(); var hash = _last(hashHistory); location.hash = hash; } } }]); return HashRouter; }(Router); var Switch = /*#__PURE__*/ function (_Router) { _inherits(Switch, _Router); function Switch(props, context) { var _this; _classCallCheck(this, Switch); _this = _possibleConstructorReturn(this, _getPrototypeOf(Switch).call(this, props, context)); _this._history = new Array(); _this._history.push(_this.state = props.path); return _this; } _createClass(Switch, [{ key: "push", value: function push(path) { var _history = this._history; if (_last(_history) !== path) { _history.push(path); this.update(path); } } }, { key: "pop", value: function pop() { var _history = this._history; if (_history.length > 1) { _history.pop(); var path = _last(_history); this.update(path); } } }]); return Switch; }(Router); Switch.defaultProps = _objectSpread({}, Router.defaultProps, { path: '/' }); function testRoute(pathname, path) { var exact = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false; return exact ? pathname === path : pathname.indexOf(path) >= 0; } var Route = /*#__PURE__*/ function (_Component) { _inherits(Route, _Component); function Route(props, context) { var _this; _classCallCheck(this, Route); _this = _possibleConstructorReturn(this, _getPrototypeOf(Route).call(this, props, context)); _this.state = testRoute(context[props.router].state, props.path, props.exact); return _this; } _createClass(Route, [{ key: "render", value: function render() { var props = this.props, state = this.state; return props.render ? props.render(state) : state && (props.component ? hele.createElement(props.component) : hele.createElement(hele.Fragment, null, this.props.children)); } }]); return Route; }(hele.Component); Route.defaultProps = { router: defaultRouterName }; var Link = /*#__PURE__*/ function (_Component) { _inherits(Link, _Component); function Link(props, context) { var _this; _classCallCheck(this, Link); _this = _possibleConstructorReturn(this, _getPrototypeOf(Link).call(this, props, context)); _this.onclick = _this.onclick.bind(_assertThisInitialized(_assertThisInitialized(_this))); return _this; } _createClass(Link, [{ key: "render", value: function render() { var _this$props = this.props, router = _this$props.router, back = _this$props.back, onclick = _this$props.onclick, attrs = _objectWithoutProperties(_this$props, ["router", "back", "onclick"]); return hele.createElement("a", Object.assign({}, attrs, { onclick: this.onclick }), this.props.children); } }, { key: "onclick", value: function onclick(event) { var props = this.props; var returnValue; if (props.onclick) { returnValue = props.onclick(event); } if (!event.defaultPrevented) { event.preventDefault(); var router = this.context[props.router]; if (props.back) { router.pop(); } else if (props.href) { router.push(props.href); } } return returnValue; } }]); return Link; }(hele.Component); Link.defaultProps = { href: 'javascript:;', back: false, router: defaultRouterName }; exports._last = _last; exports.defaultRouterName = defaultRouterName; exports.Router = Router; exports.HistoryRouter = HistoryRouter; exports.HashRouter = HashRouter; exports.Switch = Switch; exports.testRoute = testRoute; exports.Route = Route; exports.Link = Link; Object.defineProperty(exports, '__esModule', { value: true }); })));