UNPKG

@iobroker/adapter-react

Version:

React classes to develop admin interfaces for ioBroker with react.

145 lines (116 loc) 4.69 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports["default"] = void 0; var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck")); var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass")); var _assertThisInitialized2 = _interopRequireDefault(require("@babel/runtime/helpers/assertThisInitialized")); var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits")); var _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn")); var _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf")); var _react = _interopRequireDefault(require("react")); function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = (0, _getPrototypeOf2["default"])(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = (0, _getPrototypeOf2["default"])(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return (0, _possibleConstructorReturn2["default"])(this, result); }; } function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } } /** * @template P Type of the properties object. * @template S Type of the internal state object. * @extends {React.Component<P, S>} */ var Router = /*#__PURE__*/function (_React$Component) { (0, _inherits2["default"])(Router, _React$Component); var _super = _createSuper(Router); /** * @param {P} props The React properties of this component. */ function Router(props) { var _this; (0, _classCallCheck2["default"])(this, Router); _this = _super.call(this, props); _this.onHashChangedBound = _this.onHashChanged.bind((0, _assertThisInitialized2["default"])(_this)); return _this; } (0, _createClass2["default"])(Router, [{ key: "componentDidMount", value: function componentDidMount() { window.addEventListener('hashchange', this.onHashChangedBound); } }, { key: "componentWillUnmount", value: function componentWillUnmount() { window.removeEventListener('hashchange', this.onHashChangedBound); } }, { key: "onHashChanged", value: function onHashChanged() {// override this function } /** * Gets the location object. * @returns {{ tab: string; dialog: string; id: string; arg: string; }} */ }], [{ key: "getLocation", value: function getLocation() { var hash = window.location.hash; hash = hash.replace(/^#/, ''); var parts = hash.split('/').map(function (item) { try { return item ? decodeURIComponent(item) : ''; } catch (e) { console.error('Router: Cannot decode ' + item); return item || ''; } }); // #tabName/dialogName/deviceId return { tab: parts[0] || '', dialog: parts[1] || '', id: parts[2] || '', arg: parts[3] || '' }; } /** * Navigate to a new location. Any parameters that are not set will be taken from the current location. * @param {string | undefined} [tab] * @param {string | undefined} [dialog] * @param {string | undefined} [id] * @param {string | undefined} [arg] */ }, { key: "doNavigate", value: function doNavigate(tab, dialog, id, arg) { var hash = ''; var location = Router.getLocation(); if (arg !== undefined && !id) { id = location.id; } if (id && !dialog) { dialog = location.dialog; } if (dialog && !tab) { tab = location.tab; } else if (tab === null) { tab = location.tab; } if (tab) { hash = '#' + tab; if (dialog) { hash += '/' + dialog; if (id) { hash += '/' + id; if (arg !== undefined) { hash += '/' + arg; } } } } if (window.location.hash !== hash) { window.location.hash = hash; } } }]); return Router; }(_react["default"].Component); var _default = Router; exports["default"] = _default; //# sourceMappingURL=Router.js.map