|
| 1 | +/** |
| 2 | + * Copyright (c) 2015-present, Pavel Aksonov |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * This source code is licensed under the BSD-style license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. |
| 7 | + * |
| 8 | + */ |
| 9 | + |
| 10 | +import Route from './Route'; |
| 11 | +import Actions from './Actions'; |
| 12 | +import debug from './debug'; |
| 13 | + |
| 14 | +export class RouterDelegate { |
| 15 | + onPush(route:Route, props:{ [key: string]: any}):boolean { |
| 16 | + return true; |
| 17 | + } |
| 18 | + onPop(num: number = 1, route:Route, props:{ [key: string]: any}): boolean { |
| 19 | + return true; |
| 20 | + } |
| 21 | + onReplace(route:Route, props:{ [key: string]: any}):boolean { |
| 22 | + return true; |
| 23 | + } |
| 24 | + onReset(route:Route, props:{ [key: string]: any}):boolean { |
| 25 | + return true; |
| 26 | + } |
| 27 | + onSwitch(route:Route, props:{ [key: string]: any}):boolean { |
| 28 | + return true; |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +export default class BaseRouter { |
| 33 | + name:string; |
| 34 | + routes: {[key: string]: Route }; |
| 35 | + schemas: {[key: string]: {[key:string]:any } }; |
| 36 | + props: { [key: string]: any}; |
| 37 | + parentRoute: ?Route; |
| 38 | + nextRoute: ?Route; |
| 39 | + _stack: Array<string>; |
| 40 | + delegate:RouterDelegate; |
| 41 | + |
| 42 | + set stack(stack:Array<string>) { |
| 43 | + if (!stack || !stack.length) { |
| 44 | + throw new Error("Cannot be set to empty stack"); |
| 45 | + } |
| 46 | + this._stack = stack; |
| 47 | + } |
| 48 | + |
| 49 | + get stack():Array<string> { |
| 50 | + return this._stack; |
| 51 | + } |
| 52 | + |
| 53 | + get currentRoute():Route { |
| 54 | + return this.routes[this._stack[this._stack.length-1]]; |
| 55 | + } |
| 56 | + |
| 57 | + get previousRoute():Route { |
| 58 | + if (this._stack.length > 1){ |
| 59 | + return this.routes[this._stack[this._stack.length-2]]; |
| 60 | + } else { |
| 61 | + return null; |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + constructor(routes: Array<{ [key: string]: any}>, |
| 66 | + schemas:Array<{ [key: string]: any}> = [], |
| 67 | + stack:Array<string> = null, props:{ [key: string]: any} = {}){ |
| 68 | + this.schemas = {}; |
| 69 | + this.routes = {}; |
| 70 | + this.pop = this.pop.bind(this); |
| 71 | + this.route = this.route.bind(this); |
| 72 | + this.delegate = new RouterDelegate(); |
| 73 | + if (!routes || !routes.length){ |
| 74 | + throw new Error("routes is not defined"); |
| 75 | + } |
| 76 | + this.props = props; |
| 77 | + this.name = props && props.name; |
| 78 | + this.parentRoute = props && props.route; |
| 79 | + if (this.parentRoute){ |
| 80 | + if (this.parentRoute.parent){ |
| 81 | + // copy parent schemas |
| 82 | + Object.keys(this.parentRoute.parent.schemas).forEach(el=> |
| 83 | + this._addSchema(this.parentRoute.parent.schemas[el].name, this.parentRoute.parent.schemas[el])); |
| 84 | + } |
| 85 | + this.parentRoute.childRouter = this; |
| 86 | + } |
| 87 | + |
| 88 | + |
| 89 | + schemas.forEach(el=>this._addSchema(el.name, el)); |
| 90 | + let selected = null; |
| 91 | + routes.forEach(el=>{if (el.initial) selected = el.name;this._addRoute(el.name, el)}); |
| 92 | + |
| 93 | + // select first one as initial |
| 94 | + if (!stack || !stack.length){ |
| 95 | + stack = [selected || routes[0].name]; |
| 96 | + } |
| 97 | + this.stack = stack; |
| 98 | + // add actions |
| 99 | + this._addActions(); |
| 100 | + } |
| 101 | + |
| 102 | + _addSchema(name: string, props:{ [key: string]: any}){ |
| 103 | + if (!name){ |
| 104 | + throw new Error("Schema name is not defined"); |
| 105 | + } |
| 106 | + if (this.schemas[name]){ |
| 107 | + throw new Error("Schema="+name+" is not unique!"); |
| 108 | + } |
| 109 | + this.schemas[name] = props; |
| 110 | + } |
| 111 | + |
| 112 | + _addRoute(routeName: string, props:{ [key: string]: any}){ |
| 113 | + if (!routeName){ |
| 114 | + throw new Error("Route name is not defined"); |
| 115 | + } |
| 116 | + const schemaName: string = props.schema || 'default'; |
| 117 | + const schema = this.schemas[schemaName] || {}; |
| 118 | + // pass router data to inner routes |
| 119 | + const {children, name, header, footer, showNavigationBar, route, component, hideNavBar, sceneConfig, type, ...routerProps} = this.props; |
| 120 | + const routeProps = Object.assign({}, schema, routerProps, props); |
| 121 | + |
| 122 | + if (this.routes[routeName]){ |
| 123 | + throw new Error("Route="+routeName+" is not unique!"); |
| 124 | + } |
| 125 | + |
| 126 | + this.routes[routeName] = new Route(routeProps, this); |
| 127 | + |
| 128 | + } |
| 129 | + |
| 130 | + _addActions(){ |
| 131 | + if (!Actions.currentRouter){ |
| 132 | + Actions.currentRouter = this; |
| 133 | + debug("Set current router:"+this.name); |
| 134 | + } |
| 135 | + Object.keys(this.routes).forEach(name=>{ |
| 136 | + if (!Actions[name]){ |
| 137 | + Actions[name] = function(data){ |
| 138 | + return Actions.route(name, data); |
| 139 | + } |
| 140 | + } |
| 141 | + }); |
| 142 | + } |
| 143 | + |
| 144 | + route(name: string, props:{ [key: string]: any} = {}){ |
| 145 | + if (!this.routes[name]){ |
| 146 | + throw new Error("No route is defined for name="+name); |
| 147 | + } |
| 148 | + const type = props.type ? props.type : this.routes[name].type; |
| 149 | + const action = type === "switch" ? "jump": type; |
| 150 | + if (!action){ |
| 151 | + throw new Error("No type is defined for name="+name); |
| 152 | + } |
| 153 | + if (!this["_"+action]){ |
| 154 | + throw new Error("No type="+action+" is supported"); |
| 155 | + } |
| 156 | + this.nextRoute = this.routes[name]; |
| 157 | + |
| 158 | + const handler = "on"+capitalizeFirstLetter(action); |
| 159 | + if (this.delegate[handler]) { |
| 160 | + debug("Run handler "+handler); |
| 161 | + const res:boolean = this.delegate[handler](this.routes[name], props); |
| 162 | + if (!res) { |
| 163 | + console.log("Ignore "+action+", handler returns false"); |
| 164 | + return false; |
| 165 | + } |
| 166 | + } else { |
| 167 | + throw new Error("No handler "+handler+" for route="+name); |
| 168 | + } |
| 169 | + this["_"+action](name, props); |
| 170 | + return true; |
| 171 | + } |
| 172 | + |
| 173 | + _modal(name:string, props:{ [key: string]: any} ) { |
| 174 | + } |
| 175 | + |
| 176 | + _push(name:string, props:{ [key: string]: any} ){ |
| 177 | + this._stack.push(name); |
| 178 | + } |
| 179 | + |
| 180 | + _replace(name:string, props:{ [key: string]: any} ) { |
| 181 | + this._stack[this._stack.length - 1] = name; |
| 182 | + } |
| 183 | + /*** |
| 184 | + * Reset every scene with an array of routes |
| 185 | + * @param route defined route |
| 186 | + */ |
| 187 | + _reset(name:string, props:{ [key: string]: any} ) { |
| 188 | + this._stack = [name]; |
| 189 | + } |
| 190 | + |
| 191 | + _jump(name:string, props:{ [key: string]: any} ) { |
| 192 | + if (this._stack.indexOf(name) != -1){ |
| 193 | + const pos = this._stack.indexOf(name); |
| 194 | + // swap latest with found element (because currentRoute always points to latest) |
| 195 | + this._stack[pos] = this._stack[this._stack.length - 1]; |
| 196 | + this._stack[this._stack.length - 1] = name; |
| 197 | + } else { |
| 198 | + this._stack.push(name); |
| 199 | + } |
| 200 | + } |
| 201 | + |
| 202 | + pop(num: number = 1, props:{ [key: string]: any} = {}){ |
| 203 | + if (this._stack.length <= num){ |
| 204 | + return false; |
| 205 | + } |
| 206 | + this.nextRoute = null; |
| 207 | + if (this.delegate.onPop && this.delegate.onPop(num, this.currentRoute, props)){ |
| 208 | + const routes = this._stack.splice(-num, num); |
| 209 | + return true; |
| 210 | + } |
| 211 | + return false; |
| 212 | + } |
| 213 | + |
| 214 | + dismiss() { |
| 215 | + return this.delegate.onDismiss && this.delegate.onDismiss(); |
| 216 | + } |
| 217 | + |
| 218 | + |
| 219 | +} |
| 220 | + |
| 221 | +function capitalizeFirstLetter(string) { |
| 222 | + return string.charAt(0).toUpperCase() + string.slice(1); |
| 223 | +} |
0 commit comments