From a50db2194f452c721779b9b3c04f54051f69e822 Mon Sep 17 00:00:00 2001 From: Chris Thielen Date: Sat, 16 Sep 2017 14:18:02 -0700 Subject: [PATCH] fix(url): Add CustomEvent polyfill for IE Closes https://github.com/ui-router/angular/issues/154 Closes https://github.com/ui-router/core/issues/64 Closes https://github.com/ui-router/react/issues/62 Closes https://github.com/ui-router/angular/issues/139 Closes https://github.com/ui-router/angular/issues/153 --- src/vanilla/baseLocationService.ts | 7 +++++-- src/vanilla/utils.ts | 17 +++++++++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/src/vanilla/baseLocationService.ts b/src/vanilla/baseLocationService.ts index 9c0b1551..15587e69 100644 --- a/src/vanilla/baseLocationService.ts +++ b/src/vanilla/baseLocationService.ts @@ -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) { @@ -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)); } } diff --git a/src/vanilla/utils.ts b/src/vanilla/utils.ts index 772d9377..2c63a65d 100644 --- a/src/vanilla/utils.ts +++ b/src/vanilla/utils.ts @@ -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; + } +}