DEV: Convert discovery controllers to native class syntax (#22938)

This commit is contained in:
David Taylor 2023-08-02 17:46:27 +01:00 committed by GitHub
parent 5dc3a276c8
commit 089dead654
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 92 additions and 101 deletions

View File

@ -1,37 +1,37 @@
import Controller, { inject as controller } from "@ember/controller"; import { inject as service } from "@ember/service";
import { alias, equal, not } from "@ember/object/computed"; import { alias, equal, not } from "@ember/object/computed";
import Controller, { inject as controller } from "@ember/controller";
import { action } from "@ember/object"; import { action } from "@ember/object";
import Category from "discourse/models/category"; import Category from "discourse/models/category";
import DiscourseURL from "discourse/lib/url"; import DiscourseURL from "discourse/lib/url";
import { inject as service } from "@ember/service";
export default Controller.extend({ export default class DiscoveryController extends Controller {
discoveryTopics: controller("discovery/topics"), @service router;
navigationCategory: controller("navigation/category"),
application: controller(),
router: service(),
viewingCategoriesList: equal(
"router.currentRouteName",
"discovery.categories"
),
loading: false,
category: alias("navigationCategory.category"), @controller("discovery/topics") discoveryTopics;
noSubcategories: alias("navigationCategory.noSubcategories"), @controller("navigation/category") navigationCategory;
@controller application;
loadedAllItems: not("discoveryTopics.model.canLoadMore"), @equal("router.currentRouteName", "discovery.categories")
viewingCategoriesList;
@alias("navigationCategory.category") category;
@alias("navigationCategory.noSubcategories") noSubcategories;
@not("discoveryTopics.model.canLoadMore") loadedAllItems;
loading = false;
@action @action
loadingBegan() { loadingBegan() {
this.set("loading", true); this.set("loading", true);
this.set("application.showFooter", false); this.set("application.showFooter", false);
}, }
@action @action
loadingComplete() { loadingComplete() {
this.set("loading", false); this.set("loading", false);
this.set("application.showFooter", this.loadedAllItems); this.set("application.showFooter", this.loadedAllItems);
}, }
showMoreUrl(period) { showMoreUrl(period) {
let url = "", let url = "",
@ -58,18 +58,17 @@ export default Controller.extend({
urlSearchParams.set("period", period); urlSearchParams.set("period", period);
return `${url}?${urlSearchParams.toString()}`; return `${url}?${urlSearchParams.toString()}`;
}, }
get showLoadingSpinner() { get showLoadingSpinner() {
return ( return (
this.get("loading") && this.get("loading") &&
this.siteSettings.page_loading_indicator === "spinner" this.siteSettings.page_loading_indicator === "spinner"
); );
}, }
actions: { @action
changePeriod(p) { changePeriod(p) {
DiscourseURL.routeTo(this.showMoreUrl(p)); DiscourseURL.routeTo(this.showMoreUrl(p));
}, }
}, }
});

View File

@ -1,9 +1,9 @@
import DiscoveryController from "discourse/controllers/discovery";
import { inject as controller } from "@ember/controller"; import { inject as controller } from "@ember/controller";
import { reads } from "@ember/object/computed";
import DiscoveryController from "discourse/controllers/discovery";
import { action } from "@ember/object"; import { action } from "@ember/object";
import { dasherize } from "@ember/string"; import { dasherize } from "@ember/string";
import discourseComputed from "discourse-common/utils/decorators"; import discourseComputed from "discourse-common/utils/decorators";
import { reads } from "@ember/object/computed";
const subcategoryStyleComponentNames = { const subcategoryStyleComponentNames = {
rows: "categories_only", rows: "categories_only",
@ -17,17 +17,19 @@ const mobileCompatibleViews = [
"subcategories_with_featured_topics", "subcategories_with_featured_topics",
]; ];
export default DiscoveryController.extend({ export default class CategoriesController extends DiscoveryController {
discovery: controller(), @controller discovery;
// this makes sure the composer isn't scoping to a specific category // this makes sure the composer isn't scoping to a specific category
category: null, category = null;
@reads("currentUser.staff") canEdit;
canEdit: reads("currentUser.staff"),
@discourseComputed @discourseComputed
isCategoriesRoute() { isCategoriesRoute() {
return this.router.currentRouteName === "discovery.categories"; return this.router.currentRouteName === "discovery.categories";
}, }
@discourseComputed("model.parentCategory") @discourseComputed("model.parentCategory")
categoryPageStyle(parentCategory) { categoryPageStyle(parentCategory) {
let style = this.siteSettings.desktop_category_page_style; let style = this.siteSettings.desktop_category_page_style;
@ -50,7 +52,7 @@ export default DiscoveryController.extend({
? "categories_only" ? "categories_only"
: style; : style;
return dasherize(componentName); return dasherize(componentName);
}, }
@action @action
showInserted(event) { showInserted(event) {
@ -59,11 +61,10 @@ export default DiscoveryController.extend({
// Move inserted into topics // Move inserted into topics
this.model.loadBefore(tracker.get("newIncoming"), true); this.model.loadBefore(tracker.get("newIncoming"), true);
tracker.resetTracking(); tracker.resetTracking();
}, }
actions: { @action
refresh() { refresh() {
this.send("triggerRefresh"); this.send("triggerRefresh");
}, }
}, }
});

View File

@ -1,60 +1,68 @@
import { inject as controller } from "@ember/controller";
import { inject as service } from "@ember/service";
import { alias, empty, equal, gt, not, readOnly } from "@ember/object/computed"; import { alias, empty, equal, gt, not, readOnly } from "@ember/object/computed";
import BulkTopicSelection from "discourse/mixins/bulk-topic-selection"; import BulkTopicSelection from "discourse/mixins/bulk-topic-selection";
import DismissTopics from "discourse/mixins/dismiss-topics"; import DismissTopics from "discourse/mixins/dismiss-topics";
import DiscoveryController from "discourse/controllers/discovery"; import DiscoveryController from "discourse/controllers/discovery";
import I18n from "I18n"; import I18n from "I18n";
import Topic from "discourse/models/topic"; import Topic from "discourse/models/topic";
import { inject as controller } from "@ember/controller";
import deprecated from "discourse-common/lib/deprecated"; import deprecated from "discourse-common/lib/deprecated";
import discourseComputed from "discourse-common/utils/decorators"; import discourseComputed from "discourse-common/utils/decorators";
import { endWith } from "discourse/lib/computed"; import { endWith } from "discourse/lib/computed";
import { routeAction } from "discourse/helpers/route-action"; import { routeAction } from "discourse/helpers/route-action";
import { inject as service } from "@ember/service";
import { userPath } from "discourse/lib/url"; import { userPath } from "discourse/lib/url";
import { action } from "@ember/object"; import { action } from "@ember/object";
const controllerOpts = { export default class TopicsController extends DiscoveryController.extend(
discovery: controller(), BulkTopicSelection,
router: service(), DismissTopics
) {
@service router;
@controller discovery;
period: null, period = null;
canCreateTopicOnCategory: null, canCreateTopicOnCategory = null;
selected = null;
expandGloballyPinned = false;
expandAllPinned = false;
canStar: alias("currentUser.id"), @alias("currentUser.id") canStar;
showTopicPostBadges: not("new"), @not("new") showTopicPostBadges;
redirectedReason: alias("currentUser.user_option.redirected_to_top.reason"), @alias("currentUser.user_option.redirected_to_top.reason") redirectedReason;
@readOnly("model.params.order") order;
@readOnly("model.params.ascending") ascending;
@gt("model.topics.length", 0) hasTopics;
@empty("model.more_topics_url") allLoaded;
@endWith("model.filter", "latest") latest;
@endWith("model.filter", "top") top;
@equal("period", "yearly") yearly;
@equal("period", "quarterly") quarterly;
@equal("period", "monthly") monthly;
@equal("period", "weekly") weekly;
@equal("period", "daily") daily;
expandGloballyPinned: false, // Remove these loading actions which are defined in `DiscoveryController`
expandAllPinned: false,
order: readOnly("model.params.order"),
ascending: readOnly("model.params.ascending"),
selected: null,
// Remove these actions which are defined in `DiscoveryController`
// We want them to bubble in DiscoveryTopicsController // We want them to bubble in DiscoveryTopicsController
@action @action
loadingBegan() { loadingBegan() {
this.set("application.showFooter", false); this.set("application.showFooter", false);
return true; return true;
}, }
@action @action
loadingComplete() { loadingComplete() {
this.set("application.showFooter", this.loadedAllItems); this.set("application.showFooter", this.loadedAllItems);
return true; return true;
}, }
@discourseComputed("model.filter", "model.topics.length") @discourseComputed("model.filter", "model.topics.length")
showDismissRead(filter, topicsLength) { showDismissRead(filter, topicsLength) {
return this._isFilterPage(filter, "unread") && topicsLength > 0; return this._isFilterPage(filter, "unread") && topicsLength > 0;
}, }
@discourseComputed("model.filter", "model.topics.length") @discourseComputed("model.filter", "model.topics.length")
showResetNew(filter, topicsLength) { showResetNew(filter, topicsLength) {
return this._isFilterPage(filter, "new") && topicsLength > 0; return this._isFilterPage(filter, "new") && topicsLength > 0;
}, }
callResetNew(dismissPosts = false, dismissTopics = false, untrack = false) { callResetNew(dismissPosts = false, dismissTopics = false, untrack = false) {
const tracked = const tracked =
@ -80,7 +88,7 @@ const controllerOpts = {
tracked ? { skipResettingParams: ["filter", "f"] } : {} tracked ? { skipResettingParams: ["filter", "f"] } : {}
); );
}); });
}, }
// Show newly inserted topics // Show newly inserted topics
@action @action
@ -91,26 +99,25 @@ const controllerOpts = {
// Move inserted into topics // Move inserted into topics
this.model.loadBefore(tracker.get("newIncoming"), true); this.model.loadBefore(tracker.get("newIncoming"), true);
tracker.resetTracking(); tracker.resetTracking();
}, }
actions: { @action
changeSort() { changeSort() {
deprecated( deprecated(
"changeSort has been changed from an (action) to a (route-action)", "changeSort has been changed from an (action) to a (route-action)",
{ {
since: "2.6.0", since: "2.6.0",
dropFrom: "2.7.0", dropFrom: "2.7.0",
id: "discourse.topics.change-sort", id: "discourse.topics.change-sort",
} }
); );
return routeAction("changeSort", this.router._router, ...arguments)(); return routeAction("changeSort", this.router._router, ...arguments)();
}, }
},
@action @action
refresh() { refresh() {
this.send("triggerRefresh"); this.send("triggerRefresh");
}, }
afterRefresh(filter, list, listModel = list) { afterRefresh(filter, list, listModel = list) {
this.setProperties({ model: listModel }); this.setProperties({ model: listModel });
@ -121,22 +128,12 @@ const controllerOpts = {
} }
this.send("loadingComplete"); this.send("loadingComplete");
}, }
hasTopics: gt("model.topics.length", 0),
allLoaded: empty("model.more_topics_url"),
latest: endWith("model.filter", "latest"),
top: endWith("model.filter", "top"),
yearly: equal("period", "yearly"),
quarterly: equal("period", "quarterly"),
monthly: equal("period", "monthly"),
weekly: equal("period", "weekly"),
daily: equal("period", "daily"),
@discourseComputed("model.filter") @discourseComputed("model.filter")
new(filter) { new(filter) {
return filter?.endsWith("new") && !this.currentUser?.new_new_view_enabled; return filter?.endsWith("new") && !this.currentUser?.new_new_view_enabled;
}, }
@discourseComputed("allLoaded", "model.topics.length") @discourseComputed("allLoaded", "model.topics.length")
footerMessage(allLoaded, topicsLength) { footerMessage(allLoaded, topicsLength) {
@ -161,7 +158,7 @@ const controllerOpts = {
}); });
} }
} }
}, }
@discourseComputed("allLoaded", "model.topics.length") @discourseComputed("allLoaded", "model.topics.length")
footerEducation(allLoaded, topicsLength) { footerEducation(allLoaded, topicsLength) {
@ -186,11 +183,5 @@ const controllerOpts = {
`${this.currentUser.get("username_lower")}/preferences/tracking` `${this.currentUser.get("username_lower")}/preferences/tracking`
), ),
}); });
}, }
}; }
export default DiscoveryController.extend(
controllerOpts,
BulkTopicSelection,
DismissTopics
);