This repository has been archived on 2023-03-18. You can view files and clone it, but cannot push or open issues or pull requests.
osr-discourse-src/app/assets/javascripts/discourse/app/controllers/discovery.js
2022-11-01 14:07:40 -05:00

88 lines
2.1 KiB
JavaScript

import Controller, { inject as controller } from "@ember/controller";
import { alias, equal, not } from "@ember/object/computed";
import { action } from "@ember/object";
import Category from "discourse/models/category";
import discourseComputed from "discourse-common/utils/decorators";
import DiscourseURL from "discourse/lib/url";
import { inject as service } from "@ember/service";
export default Controller.extend({
discoveryTopics: controller("discovery/topics"),
navigationCategory: controller("navigation/category"),
application: controller(),
router: service(),
viewingCategoriesList: equal(
"router.currentRouteName",
"discovery.categories"
),
loading: false,
category: alias("navigationCategory.category"),
noSubcategories: alias("navigationCategory.noSubcategories"),
loadedAllItems: not("discoveryTopics.model.canLoadMore"),
@discourseComputed(
"router.currentRouteName",
"router.currentRoute.queryParams.f",
"site.show_welcome_topic_banner"
)
showEditWelcomeTopicBanner(
currentRouteName,
hasParams,
showWelcomeTopicBanner
) {
return (
this.currentUser?.staff &&
currentRouteName === "discovery.latest" &&
showWelcomeTopicBanner &&
!hasParams
);
},
@action
loadingBegan() {
this.set("loading", true);
this.set("application.showFooter", false);
},
@action
loadingComplete() {
this.set("loading", false);
this.set("application.showFooter", this.loadedAllItems);
},
showMoreUrl(period) {
let url = "",
category = this.category;
if (category) {
url = `/c/${Category.slugFor(category)}/${category.id}${
this.noSubcategories ? "/none" : ""
}/l`;
}
url += "/top";
const urlSearchParams = new URLSearchParams();
for (const [key, value] of Object.entries(
this.router.currentRoute.queryParams
)) {
if (typeof value !== "undefined") {
urlSearchParams.set(key, value);
}
}
urlSearchParams.set("period", period);
return `${url}?${urlSearchParams.toString()}`;
},
actions: {
changePeriod(p) {
DiscourseURL.routeTo(this.showMoreUrl(p));
},
},
});