2019-10-23 12:30:52 -04:00
|
|
|
import Component from "@ember/component";
|
2021-07-18 21:33:58 -04:00
|
|
|
import { action } from "@ember/object";
|
2023-10-10 14:38:59 -04:00
|
|
|
import { equal, gt, gte } from "@ember/object/computed";
|
|
|
|
import { inject as service } from "@ember/service";
|
|
|
|
import { classNames } from "@ember-decorators/component";
|
2023-10-08 17:35:53 -04:00
|
|
|
import DeleteThemesConfirm from "discourse/components/modal/delete-themes-confirm";
|
2023-10-10 14:38:59 -04:00
|
|
|
import discourseComputed, { bind } from "discourse-common/utils/decorators";
|
|
|
|
import { COMPONENTS, THEMES } from "admin/models/theme";
|
2018-08-30 15:23:15 -04:00
|
|
|
|
2023-02-23 10:32:53 -05:00
|
|
|
@classNames("themes-list")
|
|
|
|
export default class ThemesList extends Component {
|
|
|
|
@service router;
|
2023-10-08 17:35:53 -04:00
|
|
|
@service modal;
|
2023-02-23 10:32:53 -05:00
|
|
|
|
|
|
|
THEMES = THEMES;
|
|
|
|
COMPONENTS = COMPONENTS;
|
|
|
|
filterTerm = null;
|
2023-10-08 17:35:53 -04:00
|
|
|
selectInactiveMode = false;
|
2023-02-23 10:32:53 -05:00
|
|
|
|
|
|
|
@gt("themesList.length", 0) hasThemes;
|
|
|
|
|
|
|
|
@gt("activeThemes.length", 0) hasActiveThemes;
|
|
|
|
|
|
|
|
@gt("inactiveThemes.length", 0) hasInactiveThemes;
|
2018-08-30 15:23:15 -04:00
|
|
|
|
2023-02-23 10:32:53 -05:00
|
|
|
@gte("themesList.length", 10) showFilter;
|
2018-08-30 15:23:15 -04:00
|
|
|
|
2023-02-23 10:32:53 -05:00
|
|
|
@equal("currentTab", THEMES) themesTabActive;
|
2018-08-30 15:23:15 -04:00
|
|
|
|
2023-02-23 10:32:53 -05:00
|
|
|
@equal("currentTab", COMPONENTS) componentsTabActive;
|
2018-08-30 15:23:15 -04:00
|
|
|
|
2019-11-07 16:38:28 -05:00
|
|
|
@discourseComputed("themes", "components", "currentTab")
|
2018-08-30 15:23:15 -04:00
|
|
|
themesList(themes, components) {
|
2019-05-27 04:15:39 -04:00
|
|
|
if (this.themesTabActive) {
|
2018-08-30 15:23:15 -04:00
|
|
|
return themes;
|
|
|
|
} else {
|
|
|
|
return components;
|
|
|
|
}
|
2023-02-23 10:32:53 -05:00
|
|
|
}
|
2018-08-30 15:23:15 -04:00
|
|
|
|
2019-11-07 16:38:28 -05:00
|
|
|
@discourseComputed(
|
2018-08-30 15:23:15 -04:00
|
|
|
"themesList",
|
|
|
|
"currentTab",
|
|
|
|
"themesList.@each.user_selectable",
|
2021-07-18 21:33:58 -04:00
|
|
|
"themesList.@each.default",
|
2023-10-08 17:35:53 -04:00
|
|
|
"themesList.@each.markedToDelete",
|
2021-07-18 21:33:58 -04:00
|
|
|
"filterTerm"
|
2018-08-30 15:23:15 -04:00
|
|
|
)
|
|
|
|
inactiveThemes(themes) {
|
2021-07-18 21:33:58 -04:00
|
|
|
let results;
|
2019-05-27 04:15:39 -04:00
|
|
|
if (this.componentsTabActive) {
|
2021-07-18 21:33:58 -04:00
|
|
|
results = themes.filter(
|
|
|
|
(theme) => theme.get("parent_themes.length") <= 0
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
results = themes.filter(
|
|
|
|
(theme) => !theme.get("user_selectable") && !theme.get("default")
|
|
|
|
);
|
2018-08-30 15:23:15 -04:00
|
|
|
}
|
2021-07-18 21:33:58 -04:00
|
|
|
return this._filterThemes(results, this.filterTerm);
|
2023-02-23 10:32:53 -05:00
|
|
|
}
|
2018-08-30 15:23:15 -04:00
|
|
|
|
2023-10-08 17:35:53 -04:00
|
|
|
@discourseComputed("themesList.@each.markedToDelete")
|
|
|
|
selectedThemesOrComponents() {
|
|
|
|
return this.themesList.filter((theme) => theme.markedToDelete);
|
|
|
|
}
|
|
|
|
|
|
|
|
@discourseComputed("themesList.@each.markedToDelete")
|
|
|
|
selectedCount() {
|
|
|
|
return this.selectedThemesOrComponents.length;
|
|
|
|
}
|
|
|
|
|
2019-11-07 16:38:28 -05:00
|
|
|
@discourseComputed(
|
2018-08-30 15:23:15 -04:00
|
|
|
"themesList",
|
|
|
|
"currentTab",
|
|
|
|
"themesList.@each.user_selectable",
|
2021-07-18 21:33:58 -04:00
|
|
|
"themesList.@each.default",
|
|
|
|
"filterTerm"
|
2018-08-30 15:23:15 -04:00
|
|
|
)
|
2019-01-23 04:20:13 -05:00
|
|
|
activeThemes(themes) {
|
2021-07-18 21:33:58 -04:00
|
|
|
let results;
|
2019-05-27 04:15:39 -04:00
|
|
|
if (this.componentsTabActive) {
|
2021-07-18 21:33:58 -04:00
|
|
|
results = themes.filter((theme) => theme.get("parent_themes.length") > 0);
|
2019-01-23 04:20:13 -05:00
|
|
|
} else {
|
2021-07-18 21:33:58 -04:00
|
|
|
results = themes
|
2020-09-01 13:24:41 -04:00
|
|
|
.filter((theme) => theme.get("user_selectable") || theme.get("default"))
|
|
|
|
.sort((a, b) => {
|
|
|
|
if (a.get("default") && !b.get("default")) {
|
|
|
|
return -1;
|
|
|
|
} else if (b.get("default")) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return a
|
|
|
|
.get("name")
|
|
|
|
.toLowerCase()
|
|
|
|
.localeCompare(b.get("name").toLowerCase());
|
|
|
|
});
|
2018-08-30 15:23:15 -04:00
|
|
|
}
|
2021-07-18 21:33:58 -04:00
|
|
|
return this._filterThemes(results, this.filterTerm);
|
2023-02-23 10:32:53 -05:00
|
|
|
}
|
2023-10-08 17:35:53 -04:00
|
|
|
@discourseComputed("themesList.@each.markedToDelete")
|
|
|
|
someInactiveSelected() {
|
|
|
|
return (
|
|
|
|
this.selectedCount > 0 &&
|
|
|
|
this.selectedCount !== this.inactiveThemes.length
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
@discourseComputed("themesList.@each.markedToDelete")
|
|
|
|
allInactiveSelected() {
|
|
|
|
return this.selectedCount === this.inactiveThemes.length;
|
|
|
|
}
|
2021-07-18 21:33:58 -04:00
|
|
|
|
|
|
|
_filterThemes(themes, term) {
|
|
|
|
term = term?.trim()?.toLowerCase();
|
|
|
|
if (!term) {
|
|
|
|
return themes;
|
|
|
|
}
|
|
|
|
return themes.filter(({ name }) => name.toLowerCase().includes(term));
|
2023-02-23 10:32:53 -05:00
|
|
|
}
|
2018-08-30 15:23:15 -04:00
|
|
|
|
2023-10-08 17:35:53 -04:00
|
|
|
@bind
|
|
|
|
toggleInactiveMode(event) {
|
|
|
|
event?.preventDefault();
|
|
|
|
this.inactiveThemes.forEach((theme) => theme.set("markedToDelete", false));
|
|
|
|
this.toggleProperty("selectInactiveMode");
|
|
|
|
}
|
|
|
|
|
2021-07-18 21:33:58 -04:00
|
|
|
@action
|
|
|
|
changeView(newTab) {
|
|
|
|
if (newTab !== this.currentTab) {
|
2023-10-08 17:35:53 -04:00
|
|
|
this.set("selectInactiveMode", false);
|
2021-07-18 21:33:58 -04:00
|
|
|
this.set("currentTab", newTab);
|
|
|
|
if (!this.showFilter) {
|
|
|
|
this.set("filterTerm", null);
|
2018-08-30 15:23:15 -04:00
|
|
|
}
|
2021-07-18 21:33:58 -04:00
|
|
|
}
|
2023-02-23 10:32:53 -05:00
|
|
|
}
|
2021-07-18 21:33:58 -04:00
|
|
|
|
|
|
|
@action
|
|
|
|
navigateToTheme(theme) {
|
|
|
|
this.router.transitionTo("adminCustomizeThemes.show", theme);
|
2023-02-23 10:32:53 -05:00
|
|
|
}
|
2023-10-08 17:35:53 -04:00
|
|
|
|
|
|
|
@action
|
|
|
|
toggleAllInactive() {
|
|
|
|
const markedToDelete = this.selectedCount === 0;
|
|
|
|
this.inactiveThemes.forEach((theme) =>
|
|
|
|
theme.set("markedToDelete", markedToDelete)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
deleteConfirmation() {
|
|
|
|
this.modal.show(DeleteThemesConfirm, {
|
|
|
|
model: {
|
|
|
|
selectedThemesOrComponents: this.selectedThemesOrComponents,
|
|
|
|
type: this.themesTabActive ? "themes" : "components",
|
|
|
|
refreshAfterDelete: () => {
|
|
|
|
this.set("selectInactiveMode", false);
|
|
|
|
if (this.themesTabActive) {
|
|
|
|
this.set(
|
|
|
|
"themes",
|
|
|
|
this.themes.filter(
|
|
|
|
(theme) => !this.selectedThemesOrComponents.includes(theme)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
this.set(
|
|
|
|
"components",
|
|
|
|
this.components.filter(
|
|
|
|
(component) =>
|
|
|
|
!this.selectedThemesOrComponents.includes(component)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
2023-02-23 10:32:53 -05:00
|
|
|
}
|