DEV: Convert DiscourseURL to native class syntax (#27284)

This commit is contained in:
David Taylor 2024-06-03 14:59:05 +01:00 committed by GitHub
parent a3d0a9edbb
commit 29b826c8f7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -77,10 +77,10 @@ let _jumpScheduled = false;
let _transitioning = false; let _transitioning = false;
let lockOn = null; let lockOn = null;
const DiscourseURL = EmberObject.extend({ class DiscourseURL extends EmberObject {
isJumpScheduled() { isJumpScheduled() {
return _transitioning || _jumpScheduled; return _transitioning || _jumpScheduled;
}, }
// Jumps to a particular post in the stream // Jumps to a particular post in the stream
jumpToPost(postNumber, opts) { jumpToPost(postNumber, opts) {
@ -156,7 +156,7 @@ const DiscourseURL = EmberObject.extend({
return; return;
} }
}); });
}, }
replaceState(path) { replaceState(path) {
if (path.startsWith("#")) { if (path.startsWith("#")) {
@ -175,12 +175,12 @@ const DiscourseURL = EmberObject.extend({
this.router._routerMicrolib.replaceURL(path); this.router._routerMicrolib.replaceURL(path);
}); });
} }
}, }
pushState(path) { pushState(path) {
path = withoutPrefix(path); path = withoutPrefix(path);
this.router._routerMicrolib.updateURL(path); this.router._routerMicrolib.updateURL(path);
}, }
routeToTag(a) { routeToTag(a) {
// skip when we are provided nowhere to route to // skip when we are provided nowhere to route to
@ -194,7 +194,7 @@ const DiscourseURL = EmberObject.extend({
} }
return this.routeTo(a.href); return this.routeTo(a.href);
}, }
/** /**
Our custom routeTo method is used to intelligently overwrite default routing Our custom routeTo method is used to intelligently overwrite default routing
@ -261,15 +261,15 @@ const DiscourseURL = EmberObject.extend({
} }
return this.handleURL(path, opts); return this.handleURL(path, opts);
}, }
routeToUrl(url, opts = {}) { routeToUrl(url, opts = {}) {
this.routeTo(getURL(url), opts); this.routeTo(getURL(url), opts);
}, }
rewrite(regexp, replacement, opts) { rewrite(regexp, replacement, opts) {
rewrites.push({ regexp, replacement, opts: opts || {} }); rewrites.push({ regexp, replacement, opts: opts || {} });
}, }
redirectAbsolute(url) { redirectAbsolute(url) {
// Redirects will kill a test runner // Redirects will kill a test runner
@ -278,11 +278,11 @@ const DiscourseURL = EmberObject.extend({
} }
window.location = url; window.location = url;
return true; return true;
}, }
redirectTo(url) { redirectTo(url) {
return this.redirectAbsolute(getURL(url)); return this.redirectAbsolute(getURL(url));
}, }
// Determines whether a URL is internal or not // Determines whether a URL is internal or not
isInternal(url) { isInternal(url) {
@ -312,7 +312,7 @@ const DiscourseURL = EmberObject.extend({
} }
return true; return true;
}, }
/** /**
If the URL is in the topic form, /t/something/:topic_id/:post_number If the URL is in the topic form, /t/something/:topic_id/:post_number
@ -373,7 +373,7 @@ const DiscourseURL = EmberObject.extend({
} }
return false; return false;
}, }
/** /**
@private @private
@ -390,33 +390,33 @@ const DiscourseURL = EmberObject.extend({
(path === "/" || path === "/" + homepage) && (path === "/" || path === "/" + homepage) &&
(oldPath === "/" || oldPath === "/" + homepage) (oldPath === "/" || oldPath === "/" + homepage)
); );
}, }
// This has been extracted so it can be tested. // This has been extracted so it can be tested.
get origin() { get origin() {
const prefix = getURL("/"); const prefix = getURL("/");
return window.location.origin + (prefix === "/" ? "" : prefix); return window.location.origin + (prefix === "/" ? "" : prefix);
}, }
get isComposerOpen() { get isComposerOpen() {
return this.container.lookup("service:composer")?.visible; return this.container.lookup("service:composer")?.visible;
}, }
get router() { get router() {
return this.container.lookup("router:main"); return this.container.lookup("router:main");
}, }
get routerService() { get routerService() {
return this.container.lookup("service:router"); return this.container.lookup("service:router");
}, }
get appEvents() { get appEvents() {
return this.container.lookup("service:app-events"); return this.container.lookup("service:app-events");
}, }
controllerFor(name) { controllerFor(name) {
return this.container.lookup("controller:" + name); return this.container.lookup("controller:" + name);
}, }
/** /**
Be wary of looking up the router. In this case, we have links in our Be wary of looking up the router. In this case, we have links in our
@ -445,7 +445,7 @@ const DiscourseURL = EmberObject.extend({
const promise = transition.promise || transition; const promise = transition.promise || transition;
return promise.then(() => this.jumpToElement(elementId)); return promise.then(() => this.jumpToElement(elementId));
}, }
jumpToElement(elementId) { jumpToElement(elementId) {
if (_jumpScheduled || isEmpty(elementId)) { if (_jumpScheduled || isEmpty(elementId)) {
@ -468,8 +468,8 @@ const DiscourseURL = EmberObject.extend({
}); });
lockOn.lock(); lockOn.lock();
}); });
}, }
}); }
let _urlInstance = DiscourseURL.create(); let _urlInstance = DiscourseURL.create();