From 98e5e1cd53f963a1f9f0a59f1b50709449c44c83 Mon Sep 17 00:00:00 2001 From: Robin Ward Date: Wed, 19 Aug 2020 12:08:04 -0400 Subject: [PATCH] REFACTOR: Remove container from `DiscourseURL` --- .../javascripts/discourse/app/lib/url.js | 30 ++++++++----------- .../pre-initializers/discourse-bootstrap.js | 2 ++ test/javascripts/helpers/qunit-helpers.js | 3 ++ test/javascripts/test_helper.js | 5 +++- 4 files changed, 21 insertions(+), 19 deletions(-) diff --git a/app/assets/javascripts/discourse/app/lib/url.js b/app/assets/javascripts/discourse/app/lib/url.js index a7e5dccacad..916a3826d9a 100644 --- a/app/assets/javascripts/discourse/app/lib/url.js +++ b/app/assets/javascripts/discourse/app/lib/url.js @@ -7,6 +7,7 @@ import { defaultHomepage } from "discourse/lib/utilities"; import User from "discourse/models/user"; import { default as getURL, withoutPrefix } from "discourse-common/lib/get-url"; import Session from "discourse/models/session"; +import { setOwner } from "@ember/application"; const rewrites = []; const TOPIC_REGEXP = /\/t\/([^\/]+)\/(\d+)\/?(\d+)?/; @@ -189,7 +190,7 @@ const DiscourseURL = EmberObject.extend({ // while URLs are loading. For example, while a topic loads it sets `currentPost` // which triggers a replaceState even though the topic hasn't fully loaded yet! next(() => { - const location = DiscourseURL.get("router.location"); + const location = this.get("router.location"); if (location && location.replaceURL) { location.replaceURL(path); } @@ -231,7 +232,7 @@ const DiscourseURL = EmberObject.extend({ const pathname = path.replace(/(https?\:)?\/\/[^\/]+/, ""); - if (!DiscourseURL.isInternal(path)) { + if (!this.isInternal(path)) { return redirectTo(path); } @@ -361,7 +362,7 @@ const DiscourseURL = EmberObject.extend({ // If the topic_id is the same if (oldTopicId === newTopicId) { - DiscourseURL.replaceState(path); + this.replaceState(path); const container = Discourse.__container__; const topicController = container.lookup("controller:topic"); @@ -436,17 +437,6 @@ const DiscourseURL = EmberObject.extend({ return window.location.origin + (prefix === "/" ? "" : prefix); }, - // TODO: These container calls can be replaced eventually if we migrate this to a service - // object. - - /** - @private - - Get a handle on the application's router. Note that currently it uses `__container__` which is not - advised but there is no other way to access the router. - - @property router - **/ get router() { return Discourse.__container__.lookup("router:main"); }, @@ -455,8 +445,6 @@ const DiscourseURL = EmberObject.extend({ return Discourse.__container__.lookup("service:app-events"); }, - // Get a controller. Note that currently it uses `__container__` which is not - // advised but there is no other way to access the router. controllerFor(name) { return Discourse.__container__.lookup("controller:" + name); }, @@ -498,6 +486,12 @@ const DiscourseURL = EmberObject.extend({ const promise = transition.promise || transition; promise.then(() => jumpToElement(elementId)); } -}).create(); +}); +let _urlInstance = DiscourseURL.create(); -export default DiscourseURL; +export function setURLContainer(container) { + _urlInstance.container = container; + setOwner(_urlInstance, container); +} + +export default _urlInstance; diff --git a/app/assets/javascripts/discourse/app/pre-initializers/discourse-bootstrap.js b/app/assets/javascripts/discourse/app/pre-initializers/discourse-bootstrap.js index 3cc3b0f1415..1f209bee96c 100644 --- a/app/assets/javascripts/discourse/app/pre-initializers/discourse-bootstrap.js +++ b/app/assets/javascripts/discourse/app/pre-initializers/discourse-bootstrap.js @@ -12,6 +12,7 @@ import { setupURL, setupS3CDN } from "discourse-common/lib/get-url"; import deprecated from "discourse-common/lib/deprecated"; import { setIconList } from "discourse-common/lib/icon-library"; import { setPluginContainer } from "discourse/lib/plugin-api"; +import { setURLContainer } from "discourse/lib/url"; export default { name: "discourse-bootstrap", @@ -19,6 +20,7 @@ export default { // The very first initializer to run initialize(container, app) { setPluginContainer(container); + setURLContainer(container); // Our test environment has its own bootstrap code if (isTesting()) { diff --git a/test/javascripts/helpers/qunit-helpers.js b/test/javascripts/helpers/qunit-helpers.js index 75c41026a27..8be26c06409 100644 --- a/test/javascripts/helpers/qunit-helpers.js +++ b/test/javascripts/helpers/qunit-helpers.js @@ -28,6 +28,7 @@ import { mapRoutes } from "discourse/mapping-router"; import { currentSettings, mergeSettings } from "helpers/site-settings"; import { getOwner } from "discourse-common/lib/get-owner"; import { setTopicList } from "discourse/lib/topic-list-tracker"; +import { setURLContainer } from "discourse/lib/url"; export function currentUser() { return User.create(sessionFixtures["/session/current.json"].current_user); @@ -174,6 +175,7 @@ export function acceptance(name, options) { Discourse.reset(); this.container = getOwner(this); setPluginContainer(this.container); + setURLContainer(this.container); if (options.beforeEach) { options.beforeEach.call(this); } @@ -202,6 +204,7 @@ export function acceptance(name, options) { resetCustomPostMessageCallbacks(); setTopicList(null); _clearSnapshots(); + setURLContainer(null); Discourse._runInitializer( "instanceInitializers", (initName, initializer) => { diff --git a/test/javascripts/test_helper.js b/test/javascripts/test_helper.js index 692454e8646..63478540d2f 100644 --- a/test/javascripts/test_helper.js +++ b/test/javascripts/test_helper.js @@ -165,7 +165,10 @@ QUnit.testStart(function(ctx) { Session.resetCurrent(); User.resetCurrent(); resetSite(settings); - createHelperContext({ siteSettings: settings }); + createHelperContext({ + siteSettings: settings, + capabilities: {} + }); _DiscourseURL.redirectedTo = null; _DiscourseURL.redirectTo = function(url) {