DEV: Update action syntax for routes/application.js (#27282)

This commit is contained in:
David Taylor 2024-06-04 13:38:24 +01:00 committed by GitHub
parent d02e40e989
commit 0b10e335ae
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -21,14 +21,12 @@ import getURL from "discourse-common/lib/get-url";
import I18n from "discourse-i18n";
import NotActivatedModal from "../components/modal/not-activated";
function unlessStrictlyReadOnly(method, message) {
return function () {
if (this.site.isReadOnly && !this.site.isStaffWritesOnly) {
this.dialog.alert(message);
} else {
this[method]();
}
};
function unlessStrictlyReadOnly(cb, message) {
if (this.site.isReadOnly && !this.site.isStaffWritesOnly) {
this.dialog.alert(message);
} else {
cb();
}
}
const ApplicationRoute = DiscourseRoute.extend({
@ -74,183 +72,204 @@ const ApplicationRoute = DiscourseRoute.extend({
return true;
},
actions: {
toggleMobileView() {
mobile.toggleMobileView();
},
@action
toggleMobileView() {
mobile.toggleMobileView();
},
toggleSidebar() {
this.controllerFor("application").send("toggleSidebar");
},
@action
toggleSidebar() {
this.controllerFor("application").send("toggleSidebar");
},
logout: unlessStrictlyReadOnly(
"_handleLogout",
I18n.t("read_only_mode.logout_disabled")
),
@action
logout() {
unlessStrictlyReadOnly.apply(this, [
() => this._handleLogout(),
I18n.t("read_only_mode.logout_disabled"),
]);
},
_collectTitleTokens(tokens) {
tokens.push(this.siteTitle);
if (
(window.location.pathname === getURL("/") ||
window.location.pathname === getURL("/login")) &&
this.shortSiteDescription !== ""
) {
tokens.push(this.shortSiteDescription);
@action
_collectTitleTokens(tokens) {
tokens.push(this.siteTitle);
if (
(window.location.pathname === getURL("/") ||
window.location.pathname === getURL("/login")) &&
this.shortSiteDescription !== ""
) {
tokens.push(this.shortSiteDescription);
}
this.documentTitle.setTitle(tokens.join(" - "));
},
@action
composePrivateMessage(user, post) {
const recipients = user ? user.get("username") : "";
const reply = post
? `${window.location.protocol}//${window.location.host}${post.url}`
: null;
const title = post
? I18n.t("composer.reference_topic_title", {
title: post.topic.title,
})
: null;
// used only once, one less dependency
return this.composer.open({
action: Composer.PRIVATE_MESSAGE,
recipients,
archetypeId: "private_message",
draftKey: Composer.NEW_PRIVATE_MESSAGE_KEY,
draftSequence: 0,
reply,
title,
});
},
@action
error(err, transition) {
const xhrOrErr = err.jqXHR ? err.jqXHR : err;
const exceptionController = this.controllerFor("exception");
let shouldBubble = false;
const themeOrPluginSource = identifySource(err);
if (!(xhrOrErr instanceof RouteException)) {
shouldBubble = true;
// eslint-disable-next-line no-console
console.error(
...[consolePrefix(err, themeOrPluginSource), xhrOrErr].filter(Boolean)
);
if (xhrOrErr && xhrOrErr.status === 404) {
return this.router.transitionTo("exception-unknown");
}
this.documentTitle.setTitle(tokens.join(" - "));
},
composePrivateMessage(user, post) {
const recipients = user ? user.get("username") : "";
const reply = post
? `${window.location.protocol}//${window.location.host}${post.url}`
: null;
const title = post
? I18n.t("composer.reference_topic_title", {
title: post.topic.title,
})
: null;
// used only once, one less dependency
return this.composer.open({
action: Composer.PRIVATE_MESSAGE,
recipients,
archetypeId: "private_message",
draftKey: Composer.NEW_PRIVATE_MESSAGE_KEY,
draftSequence: 0,
reply,
title,
});
},
error(err, transition) {
const xhrOrErr = err.jqXHR ? err.jqXHR : err;
const exceptionController = this.controllerFor("exception");
let shouldBubble = false;
const themeOrPluginSource = identifySource(err);
if (!(xhrOrErr instanceof RouteException)) {
shouldBubble = true;
// eslint-disable-next-line no-console
console.error(
...[consolePrefix(err, themeOrPluginSource), xhrOrErr].filter(Boolean)
if (themeOrPluginSource) {
this.clientErrorHandler.displayErrorNotice(
"Error loading route",
themeOrPluginSource
);
if (xhrOrErr && xhrOrErr.status === 404) {
return this.router.transitionTo("exception-unknown");
}
if (themeOrPluginSource) {
this.clientErrorHandler.displayErrorNotice(
"Error loading route",
themeOrPluginSource
);
}
}
}
exceptionController.setProperties({
lastTransition: transition,
thrown: xhrOrErr,
});
exceptionController.setProperties({
lastTransition: transition,
thrown: xhrOrErr,
});
if (transition.intent.url) {
if (transition.method === "replace") {
DiscourseURL.replaceState(transition.intent.url);
} else {
DiscourseURL.pushState(transition.intent.url);
}
}
this.intermediateTransitionTo("exception");
return shouldBubble;
},
showLogin: unlessStrictlyReadOnly(
"handleShowLogin",
I18n.t("read_only_mode.login_disabled")
),
showCreateAccount(createAccountProps = {}) {
if (this.site.isReadOnly) {
this.dialog.alert(I18n.t("read_only_mode.login_disabled"));
if (transition.intent.url) {
if (transition.method === "replace") {
DiscourseURL.replaceState(transition.intent.url);
} else {
this.handleShowCreateAccount(createAccountProps);
DiscourseURL.pushState(transition.intent.url);
}
},
}
showForgotPassword() {
this.modal.show(ForgotPassword);
},
this.intermediateTransitionTo("exception");
return shouldBubble;
},
showNotActivated(props) {
this.modal.show(NotActivatedModal, { model: props });
},
@action
showLogin() {
unlessStrictlyReadOnly.apply(this, [
() => this.handleShowLogin(),
I18n.t("read_only_mode.login_disabled"),
]);
},
showUploadSelector() {
document.getElementById("file-uploader").click();
},
@action
showCreateAccount(createAccountProps = {}) {
if (this.site.isReadOnly) {
this.dialog.alert(I18n.t("read_only_mode.login_disabled"));
} else {
this.handleShowCreateAccount(createAccountProps);
}
},
showKeyboardShortcutsHelp() {
this.modal.show(KeyboardShortcutsHelp);
},
@action
showForgotPassword() {
this.modal.show(ForgotPassword);
},
// Close the current modal, and destroy its state.
closeModal(initiatedBy) {
return this.modal.close(initiatedBy);
},
@action
showNotActivated(props) {
this.modal.show(NotActivatedModal, { model: props });
},
/**
@action
showUploadSelector() {
document.getElementById("file-uploader").click();
},
@action
showKeyboardShortcutsHelp() {
this.modal.show(KeyboardShortcutsHelp);
},
// Close the current modal, and destroy its state.
@action
closeModal(initiatedBy) {
return this.modal.close(initiatedBy);
},
/**
Hide the modal, but keep it with all its state so that it can be shown again later.
This is useful if you want to prompt for confirmation. hideModal, ask "Are you sure?",
user clicks "No", reopenModal. If user clicks "Yes", be sure to call closeModal.
**/
hideModal() {
return this.modal.hide();
},
@action
hideModal() {
return this.modal.hide();
},
reopenModal() {
return this.modal.reopen();
},
@action
reopenModal() {
return this.modal.reopen();
},
editCategory(category) {
DiscourseURL.routeTo(`/c/${Category.slugFor(category)}/edit`);
},
@action
editCategory(category) {
DiscourseURL.routeTo(`/c/${Category.slugFor(category)}/edit`);
},
checkEmail(user) {
user.checkEmail();
},
@action
checkEmail(user) {
user.checkEmail();
},
createNewTopicViaParams(title, body, categoryId, tags) {
deprecated(
"createNewTopicViaParam on the application route is deprecated. Use the composer service instead",
{ id: "discourse.createNewTopicViaParams" }
);
getOwnerWithFallback(this).lookup("service:composer").openNewTopic({
title,
body,
categoryId,
tags,
});
},
@action
createNewTopicViaParams(title, body, categoryId, tags) {
deprecated(
"createNewTopicViaParam on the application route is deprecated. Use the composer service instead",
{ id: "discourse.createNewTopicViaParams" }
);
getOwnerWithFallback(this).lookup("service:composer").openNewTopic({
title,
body,
categoryId,
tags,
});
},
createNewMessageViaParams({
recipients = "",
topicTitle = "",
topicBody = "",
hasGroups = false,
} = {}) {
deprecated(
"createNewMessageViaParams on the application route is deprecated. Use the composer service instead",
{ id: "discourse.createNewMessageViaParams" }
);
getOwnerWithFallback(this).lookup("service:composer").openNewMessage({
recipients,
title: topicTitle,
body: topicBody,
hasGroups,
});
},
@action
createNewMessageViaParams({
recipients = "",
topicTitle = "",
topicBody = "",
hasGroups = false,
} = {}) {
deprecated(
"createNewMessageViaParams on the application route is deprecated. Use the composer service instead",
{ id: "discourse.createNewMessageViaParams" }
);
getOwnerWithFallback(this).lookup("service:composer").openNewMessage({
recipients,
title: topicTitle,
body: topicBody,
hasGroups,
});
},
handleShowLogin() {