From 648d592fa15ab5f43ce325e2f11ca140a49b0e37 Mon Sep 17 00:00:00 2001 From: Kazuyoshi Kato Date: Wed, 10 Apr 2019 21:59:50 -0700 Subject: [PATCH 1/2] Remove jQuery from hashchange.js and controllers --- app/controllers/crate/version.js | 10 ++++++++-- app/initializers/hashchange.js | 12 ++++++++---- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/app/controllers/crate/version.js b/app/controllers/crate/version.js index a0e07e25284..2665eea30cf 100644 --- a/app/controllers/crate/version.js +++ b/app/controllers/crate/version.js @@ -5,7 +5,6 @@ import PromiseProxyMixin from '@ember/object/promise-proxy-mixin'; import ArrayProxy from '@ember/array/proxy'; import { computed, observer } from '@ember/object'; import { later } from '@ember/runloop'; -import $ from 'jquery'; import moment from 'moment'; const NUM_VERSIONS = 5; @@ -185,6 +184,13 @@ export default Controller.extend({ }, report: observer('crate.readme', function() { - setTimeout(() => $(window).trigger('hashchange')); + if (typeof document === 'undefined') { + return; + } + setTimeout(() => { + let e = document.createEvent('CustomEvent'); + e.initCustomEvent('hashchange', true, true); + window.dispatchEvent(e); + }); }), }); diff --git a/app/initializers/hashchange.js b/app/initializers/hashchange.js index f11444cfe63..85b9a27eff3 100644 --- a/app/initializers/hashchange.js +++ b/app/initializers/hashchange.js @@ -1,5 +1,3 @@ -import $ from 'jquery'; - function decodeFragmentValue(hash) { try { return decodeURIComponent(hash.slice(1)); @@ -29,11 +27,17 @@ function hashchange() { } export function initialize() { - $(window).on('hashchange', hashchange); + if (typeof window.addEventListener === 'undefined') { + return; // Fastboot + } + window.addEventListener('hashchange', hashchange); // If clicking on a link to the same fragment as currently in the address bar, // hashchange won't be fired, so we need to manually trigger rescroll. - $(document).on('a[href]', 'click', function(event) { + document.addEventListener('click', function(event) { + if (event.target.tagName !== 'A') { + return; + } if (this.href === location.href && location.hash.length > 1) { setTimeout(function() { if (!event.defaultPrevented) { From 0de72c5500edca1d6a7371b1f5cf42fa7515004d Mon Sep 17 00:00:00 2001 From: Kazuyoshi Kato Date: Tue, 9 Jul 2019 21:12:39 -0700 Subject: [PATCH 2/2] Check `window` first --- app/initializers/hashchange.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/app/initializers/hashchange.js b/app/initializers/hashchange.js index 85b9a27eff3..d852ecf7f0a 100644 --- a/app/initializers/hashchange.js +++ b/app/initializers/hashchange.js @@ -27,8 +27,9 @@ function hashchange() { } export function initialize() { - if (typeof window.addEventListener === 'undefined') { - return; // Fastboot + if (typeof window === 'undefined' || typeof window.addEventListener === 'undefined') { + // Don't run this initializer under FastBoot + return; } window.addEventListener('hashchange', hashchange);