Skip to content

Commit

Permalink
BREAKING CHANGE: Move html5Mode and hashPrefix from `LocationServ…
Browse files Browse the repository at this point in the history
…ices` to `LocationConfig` interface

### End users should not notice
  • Loading branch information
christopherthielen committed Dec 20, 2016
1 parent 96f979b commit 9d316a7
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 64 deletions.
24 changes: 6 additions & 18 deletions src/common/coreservices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/
/** for typedoc */
import {IInjectable, Obj} from "./common";
import { Disposable } from "../interface";

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

["setUrl", "path", "search", "hash", "onChange"]
.forEach(key => services.location[key] = notImplemented(key));

["port", "protocol", "host", "baseHref", "html5Mode", "hashPrefix" ]
.forEach(key => services.locationConfig[key] = notImplemented(key));

export interface $QLikeDeferred {
resolve: (val?: any) => void;
reject: (reason?: any) => void;
Expand All @@ -51,30 +44,25 @@ export interface $InjectorLike {
export interface CoreServices {
$q: $QLike;
$injector: $InjectorLike;
/** Services related to getting or setting the browser location (url) */
location: LocationServices;
/** Retrieves configuration for how to construct a URL. */
locationConfig: LocationConfig;
template: TemplateServices;
}

export interface LocationServices {
export interface LocationServices extends Disposable {
setUrl(newurl: string, replace?: boolean): void;
path(): string;
search(): { [key: string]: any };
hash(): string;
onChange(callback: Function): Function;
html5Mode(): boolean;
hashPrefix(): string;
hashPrefix(newprefix: string): string;
}

export interface LocationConfig {
export interface LocationConfig extends Disposable {
port(): number;
protocol(): string;
host(): string;

baseHref(): string;
html5Mode(): boolean;
hashPrefix(): string;
hashPrefix(newprefix: string): string;
}

export interface TemplateServices {
Expand Down
22 changes: 18 additions & 4 deletions src/vanilla/browserLocationConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,39 @@ import { LocationConfig } from "../common/coreservices";
/** A `LocationConfig` that delegates to the browser's `location` object */
export class BrowserLocationConfig implements LocationConfig {
private _baseHref = undefined;
private _hashPrefix = "";

port() {
constructor(router?, private _isHtml5 = false) { }

port(): number {
return parseInt(location.port);
}

protocol () {
protocol(): string {
return location.protocol;
}

host() {
host(): string {
return location.host;
}

baseHref(href?: string) {
html5Mode(): boolean {
return this._isHtml5;
}

hashPrefix(): string;
hashPrefix(newprefix?: string): string {
return isDefined(newprefix) ? this._hashPrefix = newprefix : this._hashPrefix;
};

baseHref(href?: string): string {
return isDefined(href) ? this._baseHref = href : this._baseHref || this.applyDocumentBaseHref();
}

applyDocumentBaseHref() {
let baseTags = document.getElementsByTagName("base");
return this._baseHref = baseTags.length ? baseTags[0].href.substr(location.origin.length) : "";
}

dispose() {}
}
14 changes: 1 addition & 13 deletions src/vanilla/hashLocation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import { BrowserLocationConfig } from "./browserLocationConfig";
/** A `LocationServices` that uses the browser hash "#" to get/set the current location */
export class HashLocationService implements LocationServices, Disposable {
private _listeners: Function[] = [];
private _hashPrefix = "";

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

html5Mode() {
return false;
}

hashPrefix(newprefix?: string): string {
if(isDefined(newprefix)) {
this._hashPrefix = newprefix;
}
return this._hashPrefix;
}

dispose() {
deregAll(this._listeners);
}
}

/** A `UIRouterPlugin` uses the browser hash to get/set the current location */
export const hashLocationPlugin: (router: UIRouter) => LocationPlugin =
locationPluginFactory('vanilla.hashBangLocation', HashLocationService, BrowserLocationConfig);
locationPluginFactory('vanilla.hashBangLocation', false, HashLocationService, BrowserLocationConfig);
17 changes: 6 additions & 11 deletions src/vanilla/memoryLocation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import { isDefined } from "../common/index";
import { LocationConfig, LocationServices } from "../common/coreservices";
import { splitQuery, getParams, splitHash, locationPluginFactory } from "./utils";
import { removeFrom, unnestR, deregAll } from "../common/common";
import { removeFrom, unnestR, deregAll, noop } from "../common/common";
import { UIRouter } from "../router";
import { LocationPlugin } from "./interface";
import { isArray } from "../common/predicates";
Expand All @@ -17,17 +17,20 @@ export class MemoryLocationConfig implements LocationConfig {
_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;
}

/** A `LocationServices` that gets/sets the current location from an in-memory object */
export class MemoryLocationService implements LocationServices, Disposable {
_listeners: Function[] = [];
_hashPrefix = "";
_url = {
path: '',
search: {},
Expand Down Expand Up @@ -65,14 +68,6 @@ export class MemoryLocationService implements LocationServices, Disposable {
return this._url.search;
}

html5Mode() {
return false;
}

hashPrefix(newprefix?: string): string {
return isDefined(newprefix) ? this._hashPrefix = newprefix : this._hashPrefix;
}

setUrl(url: string, replace: boolean = false) {
if (isDefined(url)) {
let path = splitHash(splitQuery(url)[0])[0];
Expand All @@ -98,4 +93,4 @@ export class MemoryLocationService implements LocationServices, Disposable {

/** A `UIRouterPlugin` that gets/sets the current location from an in-memory object */
export const memoryLocationPlugin: (router: UIRouter) => LocationPlugin =
locationPluginFactory("vanilla.memoryLocation", MemoryLocationService, MemoryLocationConfig);
locationPluginFactory("vanilla.memoryLocation", false, MemoryLocationService, MemoryLocationConfig);
26 changes: 8 additions & 18 deletions src/vanilla/pushStateLocation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* @module vanilla
*/ /** */
import { isDefined } from "../common/index";
import { LocationServices } from "../common/coreservices";
import { LocationServices, LocationConfig } from "../common/coreservices";
import { splitQuery, trimHashVal, getParams, locationPluginFactory } from "./utils";
import { LocationPlugin } from "./interface";
import { UIRouter } from "../router";
Expand All @@ -18,21 +18,22 @@ import { BrowserLocationConfig } from "./browserLocationConfig";
*/
export class PushStateLocationService implements LocationServices, Disposable {
private _listeners: Function[] = [];
private _hashPrefix = "";
private _location: Location;
private _history: History;
private _config: LocationConfig;

constructor(public router: UIRouter) {
constructor(router: UIRouter) {
this._location = location;
this._history = history;
this._config = router.urlService.config;
};

hash() {
return trimHashVal(this._location.hash);
}

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

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

html5Mode() {
return true;
}

hashPrefix(newprefix?: string): string {
if(isDefined(newprefix)) {
this._hashPrefix = newprefix;
}
return this._hashPrefix;
}

dispose(router: UIRouter) {
deregAll(this._listeners);
}
};
}

/** 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", PushStateLocationService, BrowserLocationConfig);
locationPluginFactory("vanilla.pushStateLocation", true, PushStateLocationService, BrowserLocationConfig);

0 comments on commit 9d316a7

Please sign in to comment.