|
| 1 | +import application = require("application"); |
| 2 | +import { LocationStrategy } from 'angular2/router'; |
| 3 | +import { NgZone, ApplicationRef, Inject, forwardRef } from 'angular2/core'; |
| 4 | +import { log } from "./common"; |
| 5 | + |
| 6 | +interface LocationState |
| 7 | +{ |
| 8 | + state: any, |
| 9 | + title: string, |
| 10 | + url: string, |
| 11 | + queryParams: string |
| 12 | +} |
| 13 | + |
| 14 | +export class NSLocationStrategy extends LocationStrategy { |
| 15 | + private states = new Array<LocationState>(); |
| 16 | + private popStateCallbacks = new Array<(_: any) => any>(); |
| 17 | + private ngZone: NgZone; |
| 18 | + constructor(@Inject(forwardRef(() => NgZone)) zone: NgZone){ |
| 19 | + super(); |
| 20 | + |
| 21 | + this.ngZone = zone; |
| 22 | + //if(application.android){ |
| 23 | + //application.android.on("activityBackPressed", (args: application.AndroidActivityBackPressedEventData) => { |
| 24 | + //this.ngZone.run( () => { |
| 25 | + //if(this.states.length > 1){ |
| 26 | + //this.back(); |
| 27 | + //args.cancel = true; |
| 28 | + //} |
| 29 | + //}); |
| 30 | + //}) |
| 31 | + //} |
| 32 | + } |
| 33 | + |
| 34 | + path(): string { |
| 35 | + log("NSLocationStrategy.path()"); |
| 36 | + if(this.states.length > 0){ |
| 37 | + return this.states[this.states.length - 1].url; |
| 38 | + } |
| 39 | + return "/"; |
| 40 | + } |
| 41 | + prepareExternalUrl(internal: string): string { |
| 42 | + log("NSLocationStrategy.prepareExternalUrl() internal: " + internal); |
| 43 | + return internal; |
| 44 | + } |
| 45 | + pushState(state: any, title: string, url: string, queryParams: string): void { |
| 46 | + log(`NSLocationStrategy.pushState state: ${state}, title: ${title}, url: ${url}, queryParams: ${queryParams}`); |
| 47 | + |
| 48 | + this.states.push({ |
| 49 | + state: state, |
| 50 | + title: title, |
| 51 | + url: url, |
| 52 | + queryParams: queryParams }); |
| 53 | + |
| 54 | + } |
| 55 | + replaceState(state: any, title: string, url: string, queryParams: string): void { |
| 56 | + log(`NSLocationStrategy.replaceState state: ${state}, title: ${title}, url: ${url}, queryParams: ${queryParams}`); |
| 57 | + |
| 58 | + this.states.pop() |
| 59 | + this.states.push({ |
| 60 | + state: state, |
| 61 | + title: title, |
| 62 | + url: url, |
| 63 | + queryParams: queryParams }); |
| 64 | + } |
| 65 | + forward(): void { |
| 66 | + log("NSLocationStrategy.forward"); |
| 67 | + throw new Error("Not implemented"); |
| 68 | + } |
| 69 | + back(): void { |
| 70 | + log("NSLocationStrategy.back"); |
| 71 | + |
| 72 | + var state = this.states.pop(); |
| 73 | + this.callPopState(state, true); |
| 74 | + } |
| 75 | + onPopState(fn: (_: any) => any): void { |
| 76 | + log("NSLocationStrategy.onPopState"); |
| 77 | + this.popStateCallbacks.push(fn); |
| 78 | + } |
| 79 | + getBaseHref(): string { |
| 80 | + log("NSLocationStrategy.getBaseHref()"); |
| 81 | + return ""; |
| 82 | + } |
| 83 | + |
| 84 | + private callPopState(state:LocationState, pop: boolean = true){ |
| 85 | + var change = { url: state.url, pop: pop}; |
| 86 | + for(var fn of this.popStateCallbacks){ |
| 87 | + fn(change); |
| 88 | + } |
| 89 | + } |
| 90 | +} |
0 commit comments