Skip to content

Commit 9d316a7

Browse files
BREAKING CHANGE: Move html5Mode and hashPrefix from LocationServices to LocationConfig interface
### End users should not notice
1 parent 96f979b commit 9d316a7

File tree

5 files changed

+39
-64
lines changed

5 files changed

+39
-64
lines changed

src/common/coreservices.ts

+6-18
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
*/
77
/** for typedoc */
88
import {IInjectable, Obj} from "./common";
9+
import { Disposable } from "../interface";
910

1011
export let notImplemented = (fnname: string) => () => {
1112
throw new Error(`${fnname}(): No coreservices implementation for UI-Router is loaded.`);
@@ -14,17 +15,9 @@ export let notImplemented = (fnname: string) => () => {
1415
let services: CoreServices = {
1516
$q: undefined,
1617
$injector: undefined,
17-
location: <any> {},
18-
locationConfig: <any> {},
1918
template: <any> {}
2019
};
2120

22-
["setUrl", "path", "search", "hash", "onChange"]
23-
.forEach(key => services.location[key] = notImplemented(key));
24-
25-
["port", "protocol", "host", "baseHref", "html5Mode", "hashPrefix" ]
26-
.forEach(key => services.locationConfig[key] = notImplemented(key));
27-
2821
export interface $QLikeDeferred {
2922
resolve: (val?: any) => void;
3023
reject: (reason?: any) => void;
@@ -51,30 +44,25 @@ export interface $InjectorLike {
5144
export interface CoreServices {
5245
$q: $QLike;
5346
$injector: $InjectorLike;
54-
/** Services related to getting or setting the browser location (url) */
55-
location: LocationServices;
56-
/** Retrieves configuration for how to construct a URL. */
57-
locationConfig: LocationConfig;
5847
template: TemplateServices;
5948
}
6049

61-
export interface LocationServices {
50+
export interface LocationServices extends Disposable {
6251
setUrl(newurl: string, replace?: boolean): void;
6352
path(): string;
6453
search(): { [key: string]: any };
6554
hash(): string;
6655
onChange(callback: Function): Function;
67-
html5Mode(): boolean;
68-
hashPrefix(): string;
69-
hashPrefix(newprefix: string): string;
7056
}
7157

72-
export interface LocationConfig {
58+
export interface LocationConfig extends Disposable {
7359
port(): number;
7460
protocol(): string;
7561
host(): string;
76-
7762
baseHref(): string;
63+
html5Mode(): boolean;
64+
hashPrefix(): string;
65+
hashPrefix(newprefix: string): string;
7866
}
7967

8068
export interface TemplateServices {

src/vanilla/browserLocationConfig.ts

+18-4
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,39 @@ import { LocationConfig } from "../common/coreservices";
99
/** A `LocationConfig` that delegates to the browser's `location` object */
1010
export class BrowserLocationConfig implements LocationConfig {
1111
private _baseHref = undefined;
12+
private _hashPrefix = "";
1213

13-
port() {
14+
constructor(router?, private _isHtml5 = false) { }
15+
16+
port(): number {
1417
return parseInt(location.port);
1518
}
1619

17-
protocol () {
20+
protocol(): string {
1821
return location.protocol;
1922
}
2023

21-
host() {
24+
host(): string {
2225
return location.host;
2326
}
2427

25-
baseHref(href?: string) {
28+
html5Mode(): boolean {
29+
return this._isHtml5;
30+
}
31+
32+
hashPrefix(): string;
33+
hashPrefix(newprefix?: string): string {
34+
return isDefined(newprefix) ? this._hashPrefix = newprefix : this._hashPrefix;
35+
};
36+
37+
baseHref(href?: string): string {
2638
return isDefined(href) ? this._baseHref = href : this._baseHref || this.applyDocumentBaseHref();
2739
}
2840

2941
applyDocumentBaseHref() {
3042
let baseTags = document.getElementsByTagName("base");
3143
return this._baseHref = baseTags.length ? baseTags[0].href.substr(location.origin.length) : "";
3244
}
45+
46+
dispose() {}
3347
}

src/vanilla/hashLocation.ts

+1-13
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import { BrowserLocationConfig } from "./browserLocationConfig";
1414
/** A `LocationServices` that uses the browser hash "#" to get/set the current location */
1515
export class HashLocationService implements LocationServices, Disposable {
1616
private _listeners: Function[] = [];
17-
private _hashPrefix = "";
1817

1918
hash() {
2019
return splitHash(trimHashVal(location.hash))[1];
@@ -37,22 +36,11 @@ export class HashLocationService implements LocationServices, Disposable {
3736
return pushTo(this._listeners, () => window.removeEventListener('hashchange', cb));
3837
}
3938

40-
html5Mode() {
41-
return false;
42-
}
43-
44-
hashPrefix(newprefix?: string): string {
45-
if(isDefined(newprefix)) {
46-
this._hashPrefix = newprefix;
47-
}
48-
return this._hashPrefix;
49-
}
50-
5139
dispose() {
5240
deregAll(this._listeners);
5341
}
5442
}
5543

5644
/** A `UIRouterPlugin` uses the browser hash to get/set the current location */
5745
export const hashLocationPlugin: (router: UIRouter) => LocationPlugin =
58-
locationPluginFactory('vanilla.hashBangLocation', HashLocationService, BrowserLocationConfig);
46+
locationPluginFactory('vanilla.hashBangLocation', false, HashLocationService, BrowserLocationConfig);

src/vanilla/memoryLocation.ts

+6-11
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import { isDefined } from "../common/index";
66
import { LocationConfig, LocationServices } from "../common/coreservices";
77
import { splitQuery, getParams, splitHash, locationPluginFactory } from "./utils";
8-
import { removeFrom, unnestR, deregAll } from "../common/common";
8+
import { removeFrom, unnestR, deregAll, noop } from "../common/common";
99
import { UIRouter } from "../router";
1010
import { LocationPlugin } from "./interface";
1111
import { isArray } from "../common/predicates";
@@ -17,17 +17,20 @@ export class MemoryLocationConfig implements LocationConfig {
1717
_port = 80;
1818
_protocol = "http";
1919
_host = "localhost";
20+
_hashPrefix = "";
2021

2122
port = () => this._port;
2223
protocol = () => this._protocol;
2324
host = () => this._host;
2425
baseHref = () => this._baseHref;
26+
html5Mode = () => false;
27+
hashPrefix = (newval?) => isDefined(newval) ? this._hashPrefix = newval : this._hashPrefix;
28+
dispose = noop;
2529
}
2630

2731
/** A `LocationServices` that gets/sets the current location from an in-memory object */
2832
export class MemoryLocationService implements LocationServices, Disposable {
2933
_listeners: Function[] = [];
30-
_hashPrefix = "";
3134
_url = {
3235
path: '',
3336
search: {},
@@ -65,14 +68,6 @@ export class MemoryLocationService implements LocationServices, Disposable {
6568
return this._url.search;
6669
}
6770

68-
html5Mode() {
69-
return false;
70-
}
71-
72-
hashPrefix(newprefix?: string): string {
73-
return isDefined(newprefix) ? this._hashPrefix = newprefix : this._hashPrefix;
74-
}
75-
7671
setUrl(url: string, replace: boolean = false) {
7772
if (isDefined(url)) {
7873
let path = splitHash(splitQuery(url)[0])[0];
@@ -98,4 +93,4 @@ export class MemoryLocationService implements LocationServices, Disposable {
9893

9994
/** A `UIRouterPlugin` that gets/sets the current location from an in-memory object */
10095
export const memoryLocationPlugin: (router: UIRouter) => LocationPlugin =
101-
locationPluginFactory("vanilla.memoryLocation", MemoryLocationService, MemoryLocationConfig);
96+
locationPluginFactory("vanilla.memoryLocation", false, MemoryLocationService, MemoryLocationConfig);

src/vanilla/pushStateLocation.ts

+8-18
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* @module vanilla
44
*/ /** */
55
import { isDefined } from "../common/index";
6-
import { LocationServices } from "../common/coreservices";
6+
import { LocationServices, LocationConfig } from "../common/coreservices";
77
import { splitQuery, trimHashVal, getParams, locationPluginFactory } from "./utils";
88
import { LocationPlugin } from "./interface";
99
import { UIRouter } from "../router";
@@ -18,21 +18,22 @@ import { BrowserLocationConfig } from "./browserLocationConfig";
1818
*/
1919
export class PushStateLocationService implements LocationServices, Disposable {
2020
private _listeners: Function[] = [];
21-
private _hashPrefix = "";
2221
private _location: Location;
2322
private _history: History;
23+
private _config: LocationConfig;
2424

25-
constructor(public router: UIRouter) {
25+
constructor(router: UIRouter) {
2626
this._location = location;
2727
this._history = history;
28+
this._config = router.urlService.config;
2829
};
2930

3031
hash() {
3132
return trimHashVal(this._location.hash);
3233
}
3334

3435
path() {
35-
let base = this.router.urlConfig.baseHref();
36+
let base = this._config.baseHref();
3637
let path = this._location.pathname;
3738
let idx = path.indexOf(base);
3839
if (idx !== 0) throw new Error(`current url: ${path} does not start with <base> tag ${base}`);
@@ -45,7 +46,7 @@ export class PushStateLocationService implements LocationServices, Disposable {
4546

4647
setUrl(url: string, replace: boolean = false) {
4748
if (isDefined(url)) {
48-
let fullUrl = this.router.urlConfig.baseHref() + url;
49+
let fullUrl = this._config.baseHref() + url;
4950
if (replace) this._history.replaceState(null, null, fullUrl);
5051
else this._history.pushState(null, null, fullUrl);
5152
}
@@ -56,23 +57,12 @@ export class PushStateLocationService implements LocationServices, Disposable {
5657
return pushTo(this._listeners, () => window.removeEventListener("popstate", cb));
5758
}
5859

59-
html5Mode() {
60-
return true;
61-
}
62-
63-
hashPrefix(newprefix?: string): string {
64-
if(isDefined(newprefix)) {
65-
this._hashPrefix = newprefix;
66-
}
67-
return this._hashPrefix;
68-
}
69-
7060
dispose(router: UIRouter) {
7161
deregAll(this._listeners);
7262
}
73-
};
63+
}
7464

7565
/** A `UIRouterPlugin` that gets/sets the current location using the browser's `location` and `history` apis */
7666
export const pushStateLocationPlugin: (router: UIRouter) => LocationPlugin =
77-
locationPluginFactory("vanilla.pushStateLocation", PushStateLocationService, BrowserLocationConfig);
67+
locationPluginFactory("vanilla.pushStateLocation", true, PushStateLocationService, BrowserLocationConfig);
7868

0 commit comments

Comments
 (0)