discourse/app/assets/javascripts/admin/addon/components/themes-list.js

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

111 lines
2.8 KiB
JavaScript
Raw Normal View History

import { classNames } from "@ember-decorators/component";
import { inject as service } from "@ember/service";
import { equal, gt, gte } from "@ember/object/computed";
import { COMPONENTS, THEMES } from "admin/models/theme";
import Component from "@ember/component";
import discourseComputed from "discourse-common/utils/decorators";
import { action } from "@ember/object";
2018-08-30 15:23:15 -04:00
@classNames("themes-list")
export default class ThemesList extends Component {
@service router;
THEMES = THEMES;
COMPONENTS = COMPONENTS;
filterTerm = null;
@gt("themesList.length", 0) hasThemes;
@gt("activeThemes.length", 0) hasActiveThemes;
@gt("inactiveThemes.length", 0) hasInactiveThemes;
2018-08-30 15:23:15 -04:00
@gte("themesList.length", 10) showFilter;
2018-08-30 15:23:15 -04:00
@equal("currentTab", THEMES) themesTabActive;
2018-08-30 15:23:15 -04:00
@equal("currentTab", COMPONENTS) componentsTabActive;
2018-08-30 15:23:15 -04:00
@discourseComputed("themes", "components", "currentTab")
2018-08-30 15:23:15 -04:00
themesList(themes, components) {
if (this.themesTabActive) {
2018-08-30 15:23:15 -04:00
return themes;
} else {
return components;
}
}
2018-08-30 15:23:15 -04:00
@discourseComputed(
2018-08-30 15:23:15 -04:00
"themesList",
"currentTab",
"themesList.@each.user_selectable",
"themesList.@each.default",
"filterTerm"
2018-08-30 15:23:15 -04:00
)
inactiveThemes(themes) {
let results;
if (this.componentsTabActive) {
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
}
return this._filterThemes(results, this.filterTerm);
}
2018-08-30 15:23:15 -04:00
@discourseComputed(
2018-08-30 15:23:15 -04:00
"themesList",
"currentTab",
"themesList.@each.user_selectable",
"themesList.@each.default",
"filterTerm"
2018-08-30 15:23:15 -04:00
)
activeThemes(themes) {
let results;
if (this.componentsTabActive) {
results = themes.filter((theme) => theme.get("parent_themes.length") > 0);
} else {
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
}
return this._filterThemes(results, this.filterTerm);
}
_filterThemes(themes, term) {
term = term?.trim()?.toLowerCase();
if (!term) {
return themes;
}
return themes.filter(({ name }) => name.toLowerCase().includes(term));
}
2018-08-30 15:23:15 -04:00
@action
changeView(newTab) {
if (newTab !== this.currentTab) {
this.set("currentTab", newTab);
if (!this.showFilter) {
this.set("filterTerm", null);
2018-08-30 15:23:15 -04:00
}
}
}
@action
navigateToTheme(theme) {
this.router.transitionTo("adminCustomizeThemes.show", theme);
}
}