-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Elmi Ahmadov
committed
Apr 9, 2020
1 parent
02d9318
commit 3bd65a5
Showing
8 changed files
with
168 additions
and
86 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# Example "router" | ||
|
||
[![GitPod Logo](../../doc/run-in-gitpod.png)](https://gitpod.io/#example=listview-cells/https://github.com/eclipsesource/tabris-decorators/tree/gplink/examples/router) | ||
|
||
## Description | ||
|
||
Demonstrates the usage of the router API. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
{ | ||
"name": "router", | ||
"version": "3.3.0", | ||
"dependencies": { | ||
"reflect-metadata": "^0.1.13" | ||
}, | ||
"optionalDependencies": { | ||
"tabris": "^3.3.0", | ||
"tabris-decorators": "3.3.0" | ||
}, | ||
"devDependencies": { | ||
"typescript": "3.3.x" | ||
}, | ||
"main": "dist/app.js", | ||
"scripts": { | ||
"start": "tabris serve -w -a", | ||
"build": "tsc -p .", | ||
"watch": "tsc -p . -w --preserveWatchOutput --inlineSourceMap", | ||
"gitpod": "tabris serve -a -w --no-intro --port 8080 --external $(gp url 8080):443" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import {NavigationView, contentView, Button, TextView, TextInput, Stack, Page, Properties} from 'tabris'; | ||
import {Router, Route, injectable, create, resolve, property, component } from 'tabris-decorators'; | ||
|
||
debugger; | ||
|
||
const navigationView = new NavigationView({ | ||
layoutData: 'stretch' | ||
}).appendTo(contentView); | ||
|
||
class MyPage1 extends Page { | ||
constructor(properties?: Properties<MyPage1>) { | ||
super(properties); | ||
this.append( | ||
<Stack stretch alignment="stretchX" padding={[0, 4]}> | ||
<TextInput/> | ||
<Button text="Open" onSelect={() => | ||
router.goTo({ | ||
route: 'MyRoute2', | ||
payload: { | ||
text: this.find(TextInput).only().text | ||
} | ||
})} | ||
/> | ||
</Stack> | ||
); | ||
} | ||
} | ||
|
||
@component | ||
class MyPage2 extends Page { | ||
@property text: string; | ||
|
||
constructor(properties?: Properties<MyPage2>) { | ||
super(properties); | ||
this.append( | ||
<Stack stretch alignment="stretchX" padding={[0, 4]}> | ||
<TextView alignment="centerX" height={32} bind-text="text"/> | ||
<Button text="Go back" onSelect={() => router.back()}/> | ||
<TextInput bind-text="text" /> | ||
<Button text="Open" onSelect={() => | ||
router.goTo({ | ||
route: 'MyRoute2', | ||
payload: { | ||
text: this._find(TextInput).only().text | ||
} | ||
})} | ||
/> | ||
</Stack> | ||
); | ||
} | ||
} | ||
|
||
@injectable({ param: 'MyRoute1' }) | ||
class MyRoute1 extends Route { | ||
page = new MyPage1({title: 'foo'}); | ||
}; | ||
|
||
@injectable({ param: 'MyRoute2' }) | ||
class MyRoute2 extends Route { | ||
page = new MyPage2({title: 'bar'}); | ||
}; | ||
|
||
const router = new Router({ | ||
navigationView, | ||
history: [{ route: 'MyRoute1' }] | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
{ | ||
"compilerOptions": { | ||
"module": "commonjs", | ||
"target": "es6", | ||
"lib": ["es6" ], | ||
"jsx": "react", | ||
"jsxFactory": "JSX.createElement", | ||
"outDir": "dist", | ||
"experimentalDecorators": true, | ||
"emitDecoratorMetadata": true, | ||
"typeRoots": ["./node_modules/@types"] | ||
}, | ||
"include": [ | ||
"./src/*.ts", | ||
"./src/*.tsx" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,8 @@ | ||
import { Page, Properties } from "tabris"; | ||
import { Page } from "tabris"; | ||
|
||
export interface RouteOptions { | ||
enableDrawer?: boolean; | ||
toolbarVisible?: boolean; | ||
} | ||
|
||
export interface RoutePage { | ||
onPayload?(payload?: any): void; | ||
} | ||
|
||
export abstract class RoutePage extends Page { | ||
constructor(properties?: Properties<RoutePage>) { | ||
super(properties); | ||
} | ||
} | ||
export type Dictionary<T> = { [key: string]: T }; | ||
|
||
export abstract class Route { | ||
page: RoutePage; | ||
options: RouteOptions = { | ||
enableDrawer: false, | ||
toolbarVisible: true | ||
} | ||
export class Route<PayloadType = Dictionary<string>> { | ||
page: Page; | ||
payload?: PayloadType; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,53 @@ | ||
import { ListLikeObvserver } from "../../internals/ListLikeObserver"; | ||
import { Mutation, List, ListLike } from "../List"; | ||
import { Dictionary } from "./Route"; | ||
|
||
export type HistoryItem = { route: string, payload?: any }; | ||
export type HistoryItem = { route: string, payload?: Dictionary<string> }; | ||
|
||
export class RouterHistory<T extends HistoryItem = HistoryItem> extends ListLikeObvserver<T> { | ||
export class RouterHistory<T extends HistoryItem = HistoryItem> { | ||
|
||
private _observer: ListLikeObvserver<T>; | ||
|
||
constructor(_callback: (ev: Mutation<T>) => void) { | ||
this._observer = new ListLikeObvserver<T>(_callback); | ||
} | ||
|
||
public push(item: T) { | ||
const source = Array.from(this.source); | ||
const source = this.getSource(); | ||
source.push(item); | ||
this.source = source; | ||
this._observer.source = source; | ||
} | ||
|
||
public pop(): T { | ||
const source = Array.from(this.source); | ||
const source = this.getSource(); | ||
const result = source.pop(); | ||
this.source = source; | ||
this._observer.source = source; | ||
return result; | ||
} | ||
|
||
public removeLast() { | ||
return this.source.pop(); | ||
return this._observer.source.pop(); | ||
} | ||
|
||
get history() { | ||
return this._observer.source; | ||
} | ||
|
||
set history(value: ListLike<T>) { | ||
this._observer.source = value; | ||
} | ||
|
||
get current() { | ||
return this.source[this.source.length - 1]; | ||
return this._observer.source[this._observer.source.length - 1]; | ||
} | ||
|
||
private getSource() { | ||
if (this._observer.source instanceof Array) { | ||
return Array.from(this._observer.source); | ||
} else if (this._observer.source instanceof List) { | ||
return List.from(this._observer.source); | ||
} | ||
throw new Error('Unsupported type'); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,18 @@ | ||
import { Router, RouterConfig } from "./Router"; | ||
import { Route } from "./Route"; | ||
import { HistoryItem } from './RouterHistory'; | ||
import { Constructor } from "../../internals/utils"; | ||
import { resolve } from "../.."; | ||
|
||
export class RouterMatcher { | ||
private _nameMap: Map<string, RouterConfig> = new Map(); | ||
|
||
constructor(router: Router) { | ||
const routes = router.routes || []; | ||
routes.forEach(item => { | ||
if (this._nameMap.has(item.name)) { | ||
throw new Error(`Route with '${item.name}' name already exists!`); | ||
} | ||
this._nameMap.set(item.name, item); | ||
}); | ||
} | ||
|
||
match(historyItem: HistoryItem): Route { | ||
const name = historyItem.route; | ||
if (this._nameMap.has(name)) { | ||
return this._createRoute(this._nameMap.get(name).route); | ||
const route = this._createRoute(name); | ||
if (!route) { | ||
throw new Error(`Route with '${name}' name does not exist!`); | ||
} | ||
throw new Error(`Route with '${name}' name does not exist!`); | ||
return route; | ||
} | ||
|
||
private _createRoute(className: Constructor<Route>): Route { | ||
return new className(); // TODO: support to pass parameters | ||
private _createRoute(name: string): Route { | ||
return resolve(Route, name); | ||
} | ||
} |