-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(LocationServices): Create BaseLocationServices class to cons…
…olidate code paths
- Loading branch information
1 parent
7c90911
commit 7a03cac
Showing
16 changed files
with
254 additions
and
222 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
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
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,54 @@ | ||
/** | ||
* @internalapi | ||
* @module vanilla | ||
*/ /** */ | ||
|
||
import { LocationServices } from "../common/coreservices"; | ||
import { Disposable } from "../interface"; | ||
import { UIRouter } from "../router"; | ||
import { LocationLike, HistoryLike } from "./interface"; | ||
import { parseUrl, getParams, buildUrl } from "./utils"; | ||
import { isDefined } from "../common/predicates"; | ||
import { extend, deregAll, removeFrom } from "../common/common"; | ||
/** A base `LocationServices` */ | ||
export abstract class BaseLocationServices implements LocationServices, Disposable { | ||
constructor(router: UIRouter, public fireAfterUpdate: boolean) { | ||
this._location = window && window.location; | ||
this._history = window && window.history; | ||
} | ||
|
||
_listener = evt => this._listeners.forEach(cb => cb(evt)); | ||
|
||
private _listeners: Function[] = []; | ||
_location: LocationLike; | ||
_history: HistoryLike; | ||
|
||
abstract _get(): string; | ||
abstract _set(state: any, title: string, url: string, replace: boolean); | ||
|
||
hash = () => parseUrl(this._get()).hash; | ||
path = () => parseUrl(this._get()).path; | ||
search = () => getParams(parseUrl(this._get()).search); | ||
|
||
url(url?: string, replace: boolean = true): string { | ||
if (isDefined(url) && url !== this._get()) { | ||
this._set(null, null, url, replace); | ||
|
||
if (this.fireAfterUpdate) { | ||
let evt = extend(new Event("locationchange"), { url }); | ||
this._listeners.forEach(cb => cb(evt)); | ||
} | ||
} | ||
|
||
return buildUrl(this); | ||
} | ||
|
||
onChange(cb: EventListener) { | ||
this._listeners.push(cb); | ||
return () => removeFrom(this._listeners, cb); | ||
} | ||
|
||
dispose(router: UIRouter) { | ||
deregAll(this._listeners); | ||
} | ||
} |
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 was deleted.
Oops, something went wrong.
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,29 @@ | ||
/** | ||
* @internalapi | ||
* @module vanilla | ||
*/ | ||
/** */ | ||
import { trimHashVal } from "./utils"; | ||
import { UIRouter } from "../router"; | ||
import { BaseLocationServices } from "./baseLocationService"; | ||
|
||
/** A `LocationServices` that uses the browser hash "#" to get/set the current location */ | ||
export class HashLocationService extends BaseLocationServices { | ||
constructor(router: UIRouter) { | ||
super(router, false); | ||
window.addEventListener('hashchange', this._listener, false); | ||
} | ||
|
||
_get() { | ||
return trimHashVal(this._location.hash); | ||
} | ||
_set(state: any, title: string, url: string, replace: boolean) { | ||
this._location.hash = url; | ||
} | ||
|
||
dispose (router: UIRouter) { | ||
super.dispose(router); | ||
window.removeEventListener('hashchange', this._listener); | ||
} | ||
} | ||
|
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
This file was deleted.
Oops, something went wrong.
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,25 @@ | ||
/** | ||
* @internalapi | ||
* @module vanilla | ||
*/ | ||
/** */ | ||
import { LocationConfig } from "../common/coreservices"; | ||
import { isDefined } from "../common/predicates"; | ||
import { noop } from "../common/common"; | ||
|
||
/** A `LocationConfig` mock that gets/sets all config from an in-memory object */ | ||
export class MemoryLocationConfig implements LocationConfig { | ||
_baseHref = ''; | ||
_port = 80; | ||
_protocol = "http"; | ||
_host = "localhost"; | ||
_hashPrefix = ""; | ||
|
||
port = () => this._port; | ||
protocol = () => this._protocol; | ||
host = () => this._host; | ||
baseHref = () => this._baseHref; | ||
html5Mode = () => false; | ||
hashPrefix = (newval?) => isDefined(newval) ? this._hashPrefix = newval : this._hashPrefix; | ||
dispose = noop; | ||
} |
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,24 @@ | ||
/** | ||
* @internalapi | ||
* @module vanilla | ||
*/ | ||
/** */ | ||
import { BaseLocationServices } from "./baseLocationService"; | ||
import { UIRouter } from "../router"; | ||
|
||
/** A `LocationServices` that gets/sets the current location from an in-memory object */ | ||
export class MemoryLocationService extends BaseLocationServices { | ||
_url: string; | ||
|
||
constructor(router: UIRouter) { | ||
super(router, true); | ||
} | ||
|
||
_get() { | ||
return this._url; | ||
} | ||
|
||
_set(state: any, title: string, url: string, replace: boolean) { | ||
this._url = url; | ||
} | ||
} |
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,35 @@ | ||
/** | ||
* @internalapi | ||
* @module vanilla | ||
*/ | ||
/** */ | ||
import { BrowserLocationConfig } from "./browserLocationConfig"; | ||
import { HashLocationService } from "./hashLocationService"; | ||
import { locationPluginFactory } from "./utils"; | ||
import { LocationPlugin, ServicesPlugin } from "./interface"; | ||
import { UIRouter } from "../router"; | ||
import { PushStateLocationService } from "./pushStateLocationService"; | ||
import { MemoryLocationService } from "./memoryLocationService"; | ||
import { MemoryLocationConfig } from "./memoryLocationConfig"; | ||
import { $injector } from "./$injector"; | ||
import { $q } from "./$q"; | ||
import { services } from "../common/coreservices"; | ||
|
||
export function servicesPlugin(router: UIRouter): ServicesPlugin { | ||
services.$injector = $injector; | ||
services.$q = $q; | ||
|
||
return { name: "vanilla.services", $q, $injector, dispose: () => null }; | ||
} | ||
|
||
/** A `UIRouterPlugin` uses the browser hash to get/set the current location */ | ||
export const hashLocationPlugin: (router: UIRouter) => LocationPlugin = | ||
locationPluginFactory('vanilla.hashBangLocation', false, HashLocationService, BrowserLocationConfig); | ||
|
||
/** A `UIRouterPlugin` that gets/sets the current location using the browser's `location` and `history` apis */ | ||
export const pushStateLocationPlugin: (router: UIRouter) => LocationPlugin = | ||
locationPluginFactory("vanilla.pushStateLocation", true, PushStateLocationService, BrowserLocationConfig); | ||
|
||
/** A `UIRouterPlugin` that gets/sets the current location from an in-memory object */ | ||
export const memoryLocationPlugin: (router: UIRouter) => LocationPlugin = | ||
locationPluginFactory("vanilla.memoryLocation", false, MemoryLocationService, MemoryLocationConfig); |
Oops, something went wrong.