mirror of
https://github.com/discourse/discourse.git
synced 2025-05-24 01:21:23 +08:00
REFACTOR: Remove container from DiscourseURL
This commit is contained in:
@ -7,6 +7,7 @@ import { defaultHomepage } from "discourse/lib/utilities";
|
|||||||
import User from "discourse/models/user";
|
import User from "discourse/models/user";
|
||||||
import { default as getURL, withoutPrefix } from "discourse-common/lib/get-url";
|
import { default as getURL, withoutPrefix } from "discourse-common/lib/get-url";
|
||||||
import Session from "discourse/models/session";
|
import Session from "discourse/models/session";
|
||||||
|
import { setOwner } from "@ember/application";
|
||||||
|
|
||||||
const rewrites = [];
|
const rewrites = [];
|
||||||
const TOPIC_REGEXP = /\/t\/([^\/]+)\/(\d+)\/?(\d+)?/;
|
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`
|
// 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!
|
// which triggers a replaceState even though the topic hasn't fully loaded yet!
|
||||||
next(() => {
|
next(() => {
|
||||||
const location = DiscourseURL.get("router.location");
|
const location = this.get("router.location");
|
||||||
if (location && location.replaceURL) {
|
if (location && location.replaceURL) {
|
||||||
location.replaceURL(path);
|
location.replaceURL(path);
|
||||||
}
|
}
|
||||||
@ -231,7 +232,7 @@ const DiscourseURL = EmberObject.extend({
|
|||||||
|
|
||||||
const pathname = path.replace(/(https?\:)?\/\/[^\/]+/, "");
|
const pathname = path.replace(/(https?\:)?\/\/[^\/]+/, "");
|
||||||
|
|
||||||
if (!DiscourseURL.isInternal(path)) {
|
if (!this.isInternal(path)) {
|
||||||
return redirectTo(path);
|
return redirectTo(path);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -361,7 +362,7 @@ const DiscourseURL = EmberObject.extend({
|
|||||||
|
|
||||||
// If the topic_id is the same
|
// If the topic_id is the same
|
||||||
if (oldTopicId === newTopicId) {
|
if (oldTopicId === newTopicId) {
|
||||||
DiscourseURL.replaceState(path);
|
this.replaceState(path);
|
||||||
|
|
||||||
const container = Discourse.__container__;
|
const container = Discourse.__container__;
|
||||||
const topicController = container.lookup("controller:topic");
|
const topicController = container.lookup("controller:topic");
|
||||||
@ -436,17 +437,6 @@ const DiscourseURL = EmberObject.extend({
|
|||||||
return window.location.origin + (prefix === "/" ? "" : prefix);
|
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() {
|
get router() {
|
||||||
return Discourse.__container__.lookup("router:main");
|
return Discourse.__container__.lookup("router:main");
|
||||||
},
|
},
|
||||||
@ -455,8 +445,6 @@ const DiscourseURL = EmberObject.extend({
|
|||||||
return Discourse.__container__.lookup("service:app-events");
|
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) {
|
controllerFor(name) {
|
||||||
return Discourse.__container__.lookup("controller:" + name);
|
return Discourse.__container__.lookup("controller:" + name);
|
||||||
},
|
},
|
||||||
@ -498,6 +486,12 @@ const DiscourseURL = EmberObject.extend({
|
|||||||
const promise = transition.promise || transition;
|
const promise = transition.promise || transition;
|
||||||
promise.then(() => jumpToElement(elementId));
|
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;
|
||||||
|
@ -12,6 +12,7 @@ import { setupURL, setupS3CDN } from "discourse-common/lib/get-url";
|
|||||||
import deprecated from "discourse-common/lib/deprecated";
|
import deprecated from "discourse-common/lib/deprecated";
|
||||||
import { setIconList } from "discourse-common/lib/icon-library";
|
import { setIconList } from "discourse-common/lib/icon-library";
|
||||||
import { setPluginContainer } from "discourse/lib/plugin-api";
|
import { setPluginContainer } from "discourse/lib/plugin-api";
|
||||||
|
import { setURLContainer } from "discourse/lib/url";
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
name: "discourse-bootstrap",
|
name: "discourse-bootstrap",
|
||||||
@ -19,6 +20,7 @@ export default {
|
|||||||
// The very first initializer to run
|
// The very first initializer to run
|
||||||
initialize(container, app) {
|
initialize(container, app) {
|
||||||
setPluginContainer(container);
|
setPluginContainer(container);
|
||||||
|
setURLContainer(container);
|
||||||
|
|
||||||
// Our test environment has its own bootstrap code
|
// Our test environment has its own bootstrap code
|
||||||
if (isTesting()) {
|
if (isTesting()) {
|
||||||
|
@ -28,6 +28,7 @@ import { mapRoutes } from "discourse/mapping-router";
|
|||||||
import { currentSettings, mergeSettings } from "helpers/site-settings";
|
import { currentSettings, mergeSettings } from "helpers/site-settings";
|
||||||
import { getOwner } from "discourse-common/lib/get-owner";
|
import { getOwner } from "discourse-common/lib/get-owner";
|
||||||
import { setTopicList } from "discourse/lib/topic-list-tracker";
|
import { setTopicList } from "discourse/lib/topic-list-tracker";
|
||||||
|
import { setURLContainer } from "discourse/lib/url";
|
||||||
|
|
||||||
export function currentUser() {
|
export function currentUser() {
|
||||||
return User.create(sessionFixtures["/session/current.json"].current_user);
|
return User.create(sessionFixtures["/session/current.json"].current_user);
|
||||||
@ -174,6 +175,7 @@ export function acceptance(name, options) {
|
|||||||
Discourse.reset();
|
Discourse.reset();
|
||||||
this.container = getOwner(this);
|
this.container = getOwner(this);
|
||||||
setPluginContainer(this.container);
|
setPluginContainer(this.container);
|
||||||
|
setURLContainer(this.container);
|
||||||
if (options.beforeEach) {
|
if (options.beforeEach) {
|
||||||
options.beforeEach.call(this);
|
options.beforeEach.call(this);
|
||||||
}
|
}
|
||||||
@ -202,6 +204,7 @@ export function acceptance(name, options) {
|
|||||||
resetCustomPostMessageCallbacks();
|
resetCustomPostMessageCallbacks();
|
||||||
setTopicList(null);
|
setTopicList(null);
|
||||||
_clearSnapshots();
|
_clearSnapshots();
|
||||||
|
setURLContainer(null);
|
||||||
Discourse._runInitializer(
|
Discourse._runInitializer(
|
||||||
"instanceInitializers",
|
"instanceInitializers",
|
||||||
(initName, initializer) => {
|
(initName, initializer) => {
|
||||||
|
@ -165,7 +165,10 @@ QUnit.testStart(function(ctx) {
|
|||||||
Session.resetCurrent();
|
Session.resetCurrent();
|
||||||
User.resetCurrent();
|
User.resetCurrent();
|
||||||
resetSite(settings);
|
resetSite(settings);
|
||||||
createHelperContext({ siteSettings: settings });
|
createHelperContext({
|
||||||
|
siteSettings: settings,
|
||||||
|
capabilities: {}
|
||||||
|
});
|
||||||
|
|
||||||
_DiscourseURL.redirectedTo = null;
|
_DiscourseURL.redirectedTo = null;
|
||||||
_DiscourseURL.redirectTo = function(url) {
|
_DiscourseURL.redirectTo = function(url) {
|
||||||
|
Reference in New Issue
Block a user