From 8e314eba69a38bf616abd66f429d8cc9a84bc398 Mon Sep 17 00:00:00 2001 From: Randall Morey Date: Wed, 2 Dec 2020 16:41:20 -0500 Subject: [PATCH] =?UTF-8?q?feat:=20=F0=9F=8E=B8=20autopopulate=20origin=20?= =?UTF-8?q?field=20in=20development?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ui/desktop/app/routes/origin.js | 23 ++++++++++++++++++++++- ui/desktop/config/environment.js | 5 +++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/ui/desktop/app/routes/origin.js b/ui/desktop/app/routes/origin.js index 9d3704e92..9e3df0ace 100644 --- a/ui/desktop/app/routes/origin.js +++ b/ui/desktop/app/routes/origin.js @@ -1,7 +1,9 @@ import Route from '@ember/routing/route'; import { inject as service } from '@ember/service'; +import { getOwner } from '@ember/application'; import { action } from '@ember/object'; import loading from 'ember-loading/decorator'; +import config from '../config/environment'; export default class OriginRoute extends Route { // =services @@ -20,6 +22,17 @@ export default class OriginRoute extends Route { return this.store.adapterFor('application'); } + /** + * Looks up the window object indirectly. + * @type {Window} + */ + get window() { + // The Ember way of accessing globals... + const document = getOwner(this).lookup('service:-document').documentElement; + // defaultView === window, but without using globals directly + return document.parentNode.defaultView; + } + // =methods /** @@ -36,7 +49,15 @@ export default class OriginRoute extends Route { */ setupController(controller) { super.setupController(...arguments); - const origin = this.origin.rendererOrigin; + let origin = this.origin.rendererOrigin; + // If origin is unset and this is a development environment, + // autoset the origin field of the UI for better DX. + // The controller URL is almost always the same as the current window when + // using mocks, and this makes development more rapid since developers + // do not need to fill an origin on every session. + if (!origin && config.autoOrigin) { + origin = this.window.location.origin; + } controller.setProperties({ origin }); } diff --git a/ui/desktop/config/environment.js b/ui/desktop/config/environment.js index d549aba56..e4b94e3fc 100644 --- a/ui/desktop/config/environment.js +++ b/ui/desktop/config/environment.js @@ -50,6 +50,11 @@ module.exports = function (environment) { // ENV.APP.LOG_TRANSITIONS = true; // ENV.APP.LOG_TRANSITIONS_INTERNAL = true; // ENV.APP.LOG_VIEW_LOOKUPS = true; + + // This flag tells the origin route to autoset the controller origin field + // of the UI, which only makes sense in development where the origin is + // usually the same as the application origin. + ENV.autoOrigin = true; } if (environment === 'test') {