UNPKG

reactxp-navigation

Version:

Plugin for ReactXP that provides a navigation framework

147 lines (146 loc) 5.25 kB
"use strict"; var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); Object.defineProperty(exports, "__esModule", { value: true }); /** * Navigator.tsx * * Copyright (c) Microsoft Corporation. All rights reserved. * Licensed under the MIT license. * * Common native implementation for Navigator on mobile. */ var RX = require("reactxp"); var Types = require("../common/Types"); exports.Types = Types; var Types_1 = require("../common/Types"); var NavigatorExperimentalDelegate_1 = require("./NavigatorExperimentalDelegate"); var DefaultDelegateSelector = /** @class */ (function () { function DefaultDelegateSelector() { } DefaultDelegateSelector.prototype.getNavigatorDelegate = function (navigator) { return new NavigatorExperimentalDelegate_1.default(navigator); }; return DefaultDelegateSelector; }()); exports.DefaultDelegateSelector = DefaultDelegateSelector; var NavigatorImpl = /** @class */ (function (_super) { __extends(NavigatorImpl, _super); function NavigatorImpl(initialProps) { var _this = _super.call(this, initialProps) || this; _this._commandQueue = []; if (!initialProps.delegateSelector) { _this._delegate = new NavigatorExperimentalDelegate_1.default(_this); } else { _this._delegate = initialProps.delegateSelector.getNavigatorDelegate(_this); } return _this; } NavigatorImpl.prototype.componentDidMount = function () { RX.Input.backButtonEvent.subscribe(this._delegate.onBackPress); }; NavigatorImpl.prototype.componentWillUnmount = function () { RX.Input.backButtonEvent.unsubscribe(this._delegate.onBackPress); }; NavigatorImpl.prototype.componentDidUpdate = function () { // Catch up with any pending commands this._processCommand(); }; NavigatorImpl.prototype.getRoutes = function () { return this._delegate.getRoutes(); }; // Push a new route if initial route doesn't exist NavigatorImpl.prototype.push = function (route) { this._enqueueCommand({ type: Types_1.CommandType.Push, param: { route: route } }); }; NavigatorImpl.prototype.pop = function () { this._enqueueCommand({ type: Types_1.CommandType.Pop, param: {} }); }; NavigatorImpl.prototype.replace = function (route) { this._enqueueCommand({ type: Types_1.CommandType.Replace, param: { route: route } }); }; NavigatorImpl.prototype.replacePrevious = function (route) { this._enqueueCommand({ type: Types_1.CommandType.Replace, param: { route: route, value: -1 } }); }; // This method replaces the route at the given index of the stack and pops to that index. NavigatorImpl.prototype.replaceAtIndex = function (route, index) { var routes = this.getRoutes(); // Pop to index route and then replace if not last one if (index < routes.length - 1) { var route_1 = routes[index]; this.popToRoute(route_1); } // Schedule a replace this.replace(route); }; // Reset route stack with default route stack NavigatorImpl.prototype.immediatelyResetRouteStack = function (nextRouteStack) { this._delegate.immediatelyResetRouteStack(nextRouteStack); }; NavigatorImpl.prototype.popToRoute = function (route) { this._enqueueCommand({ type: Types_1.CommandType.Pop, param: { route: route } }); }; NavigatorImpl.prototype.popToTop = function () { this._enqueueCommand({ type: Types_1.CommandType.Pop, param: { value: -1 } }); }; NavigatorImpl.prototype.getCurrentRoutes = function () { return this.getRoutes(); }; // Render without initial route to get a reference for Navigator object NavigatorImpl.prototype.render = function () { return this._delegate.render(); }; NavigatorImpl.prototype._enqueueCommand = function (command) { this._commandQueue.push(command); this._processCommand(); }; NavigatorImpl.prototype._processCommand = function () { this._delegate.processCommand(this._commandQueue); }; return NavigatorImpl; }(Types_1.Navigator)); exports.NavigatorImpl = NavigatorImpl; exports.default = NavigatorImpl; exports.Navigator = NavigatorImpl; exports.NavigatorDelegateSelector = new DefaultDelegateSelector();