Skip to content

Commit a50db21

Browse files
fix(url): Add CustomEvent polyfill for IE
Closes ui-router/angular#154 Closes #64 Closes ui-router/react#62 Closes ui-router/angular#139 Closes ui-router/angular#153
1 parent c8110fc commit a50db21

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

src/vanilla/baseLocationService.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,12 @@ import { LocationServices } from "../common/coreservices";
77
import { Disposable } from "../interface";
88
import { UIRouter } from "../router";
99
import { LocationLike, HistoryLike } from "./interface";
10-
import { parseUrl, getParams, buildUrl } from "./utils";
10+
import { parseUrl, getParams, buildUrl, getCustomEventCtor } from "./utils";
1111
import { isDefined } from "../common/predicates";
1212
import { extend, deregAll, removeFrom } from "../common/common";
13+
14+
const Evt: typeof CustomEvent = getCustomEventCtor();
15+
1316
/** A base `LocationServices` */
1417
export abstract class BaseLocationServices implements LocationServices, Disposable {
1518
constructor(router: UIRouter, public fireAfterUpdate: boolean) {
@@ -59,7 +62,7 @@ export abstract class BaseLocationServices implements LocationServices, Disposab
5962
this._set(null, null, url, replace);
6063

6164
if (this.fireAfterUpdate) {
62-
let evt = extend(new Event("locationchange"), { url });
65+
let evt = extend(new Evt("locationchange"), { url });
6366
this._listeners.forEach(cb => cb(evt));
6467
}
6568
}

src/vanilla/utils.ts

+17
Original file line numberDiff line numberDiff line change
@@ -78,3 +78,20 @@ export function locationPluginFactory(
7878
};
7979
}
8080

81+
export function getCustomEventCtor(): typeof CustomEvent {
82+
// CustomEvent Polyfill
83+
function _CustomEvent(event, params) {
84+
params = params || { bubbles: false, cancelable: false, detail: undefined };
85+
let evt = document.createEvent( 'CustomEvent' );
86+
evt.initCustomEvent( event, params.bubbles, params.cancelable, params.detail );
87+
return evt;
88+
}
89+
_CustomEvent.prototype = Event.prototype;
90+
91+
try {
92+
new CustomEvent('foo');
93+
return CustomEvent;
94+
} catch (_err) {
95+
return _CustomEvent as any;
96+
}
97+
}

0 commit comments

Comments
 (0)