Skip to content

Commit

Permalink
refactor(LocationServices): Create BaseLocationServices class to cons…
Browse files Browse the repository at this point in the history
…olidate code paths
  • Loading branch information
christopherthielen committed Jan 16, 2017
1 parent 7c90911 commit 7a03cac
Show file tree
Hide file tree
Showing 16 changed files with 254 additions and 222 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"watch": "run-p watch:*",
"watch:buildjs": "tsc -w",
"watch:buildesm": "tsc -w -m es6 --outDir lib-esm",
"watch:dts-downlevel": "npm run fixdts",
"watch:test": "karma start --singleRun=false --autoWatch=true --autoWatchInterval=1",
"debug": "karma start --singleRun=false --autoWatch=true --autoWatchInterval=1 --browsers=Chrome"
},
Expand Down
5 changes: 3 additions & 2 deletions src/vanilla/$injector.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
/**
* @internalapi
* @module vanilla
*/ /** */
*/
/** */
import {
extend, assertPredicate, isFunction, isArray, isInjectable, $InjectorLike, IInjectable
extend, assertPredicate, isFunction, isArray, isInjectable, $InjectorLike, IInjectable
} from "../common/index";

// globally available injectables
Expand Down
3 changes: 2 additions & 1 deletion src/vanilla/$q.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
/**
* @internalapi
* @module vanilla
*/ /** */
*/
/** */
import { isArray, isObject, $QLike } from "../common/index";

/**
Expand Down
54 changes: 54 additions & 0 deletions src/vanilla/baseLocationService.ts
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);
}
}
4 changes: 2 additions & 2 deletions src/vanilla/browserLocationConfig.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/**
* @internalapi
* @module vanilla
*/ /** */

*/
/** */
import { isDefined } from "../common/predicates";
import { LocationConfig } from "../common/coreservices";

Expand Down
43 changes: 0 additions & 43 deletions src/vanilla/hashLocation.ts

This file was deleted.

29 changes: 29 additions & 0 deletions src/vanilla/hashLocationService.ts
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);
}
}

28 changes: 11 additions & 17 deletions src/vanilla/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,17 @@
*
* @internalapi
* @module vanilla
*/ /** */
import { UIRouter } from "../router";
*/
/** */
export * from "./$q";
export * from "./$injector";

import { services } from "../common/coreservices";
import { ServicesPlugin } from "./interface";
import { $q } from "./$q";
import { $injector } from "./$injector";
export * from "./baseLocationService";
export * from "./hashLocationService";
export * from "./memoryLocationService";
export * from "./pushStateLocationService";

export { $q, $injector };
export * from "./memoryLocationConfig";
export * from "./browserLocationConfig";

export function servicesPlugin(router: UIRouter): ServicesPlugin {
services.$injector = $injector;
services.$q = $q;

return { name: "vanilla.services", $q, $injector, dispose: () => null };
}

export * from "./hashLocation";
export * from "./memoryLocation";
export * from "./pushStateLocation";
export * from "./plugins";
5 changes: 3 additions & 2 deletions src/vanilla/interface.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
/**
* @internalapi
* @module vanilla
*/ /** */
import { LocationConfig, LocationServices } from '../common/coreservices';
*/
/** */
import { LocationConfig, LocationServices } from "../common/coreservices";
import { UIRouterPlugin } from "../interface";
import { $InjectorLike, $QLike } from "../common/index";

Expand Down
77 changes: 0 additions & 77 deletions src/vanilla/memoryLocation.ts

This file was deleted.

25 changes: 25 additions & 0 deletions src/vanilla/memoryLocationConfig.ts
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;
}
24 changes: 24 additions & 0 deletions src/vanilla/memoryLocationService.ts
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;
}
}
35 changes: 35 additions & 0 deletions src/vanilla/plugins.ts
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);
Loading

0 comments on commit 7a03cac

Please sign in to comment.