Skip to content

Commit

Permalink
fix(url): Add CustomEvent polyfill for IE
Browse files Browse the repository at this point in the history
  • Loading branch information
christopherthielen authored and wawyed committed Mar 3, 2018
1 parent 3de195f commit 066a985
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/vanilla/baseLocationService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@ 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 { parseUrl, getParams, buildUrl, getCustomEventCtor } from "./utils";
import { isDefined } from "../common/predicates";
import { extend, deregAll, removeFrom } from "../common/common";

const Evt: typeof CustomEvent = getCustomEventCtor();

/** A base `LocationServices` */
export abstract class BaseLocationServices implements LocationServices, Disposable {
constructor(router: UIRouter, public fireAfterUpdate: boolean) {
Expand Down Expand Up @@ -59,7 +62,7 @@ export abstract class BaseLocationServices implements LocationServices, Disposab
this._set(null, null, url, replace);

if (this.fireAfterUpdate) {
let evt = extend(new Event("locationchange"), { url });
let evt = extend(new Evt("locationchange"), { url });
this._listeners.forEach(cb => cb(evt));
}
}
Expand Down
17 changes: 17 additions & 0 deletions src/vanilla/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,20 @@ export function locationPluginFactory(
};
}

export function getCustomEventCtor(): typeof CustomEvent {
// CustomEvent Polyfill
function _CustomEvent(event, params) {
params = params || { bubbles: false, cancelable: false, detail: undefined };
let evt = document.createEvent( 'CustomEvent' );
evt.initCustomEvent( event, params.bubbles, params.cancelable, params.detail );
return evt;
}
_CustomEvent.prototype = Event.prototype;

try {
new CustomEvent('foo');
return CustomEvent;
} catch (_err) {
return _CustomEvent as any;
}
}

0 comments on commit 066a985

Please sign in to comment.