2019-11-08 11:32:20 -05:00
|
|
|
import { alias, not } from "@ember/object/computed";
|
2018-01-24 05:48:20 -05:00
|
|
|
import ComboBoxComponent from "select-kit/components/combo-box";
|
|
|
|
import DiscourseURL from "discourse/lib/url";
|
2020-01-16 12:56:53 -05:00
|
|
|
import discourseComputed from "discourse-common/utils/decorators";
|
2018-01-24 05:48:20 -05:00
|
|
|
import Category from "discourse/models/category";
|
|
|
|
import { categoryBadgeHTML } from "discourse/helpers/category-link";
|
2019-11-13 12:13:47 -05:00
|
|
|
import Site from "discourse/models/site";
|
|
|
|
|
2018-10-31 13:33:24 -04:00
|
|
|
const { isEmpty } = Ember;
|
2018-01-24 05:48:20 -05:00
|
|
|
|
|
|
|
export default ComboBoxComponent.extend({
|
|
|
|
pluginApiIdentifiers: ["category-drop"],
|
|
|
|
classNameBindings: ["categoryStyle"],
|
|
|
|
classNames: "category-drop",
|
|
|
|
verticalOffset: 3,
|
2019-11-08 11:32:20 -05:00
|
|
|
content: alias("categoriesWithShortcuts"),
|
2018-01-24 05:48:20 -05:00
|
|
|
rowComponent: "category-row",
|
|
|
|
headerComponent: "category-drop/category-drop-header",
|
|
|
|
allowAutoSelectFirst: false,
|
|
|
|
tagName: "li",
|
2019-11-08 11:32:20 -05:00
|
|
|
categoryStyle: alias("siteSettings.category_style"),
|
2018-01-24 05:48:20 -05:00
|
|
|
noCategoriesLabel: I18n.t("categories.no_subcategory"),
|
2018-01-25 03:38:02 -05:00
|
|
|
fullWidthOnMobile: true,
|
2018-04-12 04:56:32 -04:00
|
|
|
caretDownIcon: "caret-right",
|
|
|
|
caretUpIcon: "caret-down",
|
2018-05-29 11:34:34 -04:00
|
|
|
subCategory: false,
|
2019-11-08 11:32:20 -05:00
|
|
|
isAsync: not("subCategory"),
|
2018-01-24 05:48:20 -05:00
|
|
|
|
2019-11-07 16:38:28 -05:00
|
|
|
@discourseComputed(
|
|
|
|
"categories",
|
|
|
|
"hasSelection",
|
|
|
|
"subCategory",
|
|
|
|
"noSubcategories"
|
|
|
|
)
|
2018-11-15 09:21:40 -05:00
|
|
|
categoriesWithShortcuts(
|
|
|
|
categories,
|
|
|
|
hasSelection,
|
|
|
|
subCategory,
|
|
|
|
noSubcategories
|
|
|
|
) {
|
|
|
|
const shortcuts = [];
|
|
|
|
|
|
|
|
if (hasSelection || (noSubcategories && subCategory)) {
|
|
|
|
shortcuts.push({
|
2019-05-27 04:15:39 -04:00
|
|
|
name: this.allCategoriesLabel,
|
2018-11-15 09:21:40 -05:00
|
|
|
__sk_row_type: "noopRow",
|
|
|
|
id: "all-categories"
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (subCategory && (hasSelection || !noSubcategories)) {
|
|
|
|
shortcuts.push({
|
2019-05-27 04:15:39 -04:00
|
|
|
name: this.noCategoriesLabel,
|
2018-11-15 09:21:40 -05:00
|
|
|
__sk_row_type: "noopRow",
|
|
|
|
id: "no-categories"
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return shortcuts.concat(categories);
|
|
|
|
},
|
|
|
|
|
2018-01-24 05:48:20 -05:00
|
|
|
init() {
|
2018-12-10 08:05:00 -05:00
|
|
|
this._super(...arguments);
|
2018-01-24 05:48:20 -05:00
|
|
|
|
2019-05-27 04:15:39 -04:00
|
|
|
this.rowComponentOptions.setProperties({
|
|
|
|
hideParentCategory: this.subCategory,
|
2018-01-24 05:48:20 -05:00
|
|
|
allowUncategorized: true,
|
2019-05-27 04:15:39 -04:00
|
|
|
countSubcategories: this.countSubcategories,
|
2018-03-14 06:12:57 -04:00
|
|
|
displayCategoryDescription: !(
|
|
|
|
this.currentUser &&
|
|
|
|
(this.currentUser.get("staff") || this.currentUser.trust_level > 0)
|
|
|
|
)
|
2018-01-24 05:48:20 -05:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2018-12-10 08:05:00 -05:00
|
|
|
didReceiveAttrs() {
|
2019-05-27 04:15:39 -04:00
|
|
|
if (!this.categories) this.set("categories", []);
|
2018-12-10 08:05:00 -05:00
|
|
|
this.forceValue(this.get("category.id"));
|
|
|
|
},
|
|
|
|
|
2019-11-07 16:38:28 -05:00
|
|
|
@discourseComputed("content")
|
2018-02-01 17:51:29 -05:00
|
|
|
filterable(content) {
|
2018-10-31 13:33:24 -04:00
|
|
|
const contentLength = (content && content.length) || 0;
|
|
|
|
return (
|
|
|
|
contentLength >= 15 ||
|
2019-11-08 13:30:41 -05:00
|
|
|
(this.isAsync && contentLength < Category.list().length)
|
2018-10-31 13:33:24 -04:00
|
|
|
);
|
2018-02-01 17:51:29 -05:00
|
|
|
},
|
|
|
|
|
2018-01-24 05:48:20 -05:00
|
|
|
computeHeaderContent() {
|
2019-01-19 04:05:51 -05:00
|
|
|
let content = this._super(...arguments);
|
2018-01-24 05:48:20 -05:00
|
|
|
|
2019-05-27 04:15:39 -04:00
|
|
|
if (this.hasSelection) {
|
2018-01-24 05:48:20 -05:00
|
|
|
const category = Category.findById(content.value);
|
2018-06-24 16:23:31 -04:00
|
|
|
content.title = category.title;
|
2018-01-24 05:48:20 -05:00
|
|
|
content.label = categoryBadgeHTML(category, {
|
|
|
|
link: false,
|
|
|
|
allowUncategorized: true,
|
|
|
|
hideParent: true
|
|
|
|
}).htmlSafe();
|
|
|
|
} else {
|
2019-05-27 04:15:39 -04:00
|
|
|
if (this.noSubcategories) {
|
2018-06-15 11:03:24 -04:00
|
|
|
content.label = `<span class="category-name">${this.get(
|
|
|
|
"noCategoriesLabel"
|
|
|
|
)}</span>`;
|
2019-05-27 04:15:39 -04:00
|
|
|
content.title = this.noCategoriesLabel;
|
2018-01-24 05:48:20 -05:00
|
|
|
} else {
|
2018-06-15 11:03:24 -04:00
|
|
|
content.label = `<span class="category-name">${this.get(
|
|
|
|
"allCategoriesLabel"
|
|
|
|
)}</span>`;
|
2019-05-27 04:15:39 -04:00
|
|
|
content.title = this.allCategoriesLabel;
|
2018-01-24 05:48:20 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return content;
|
|
|
|
},
|
|
|
|
|
2019-11-07 16:38:28 -05:00
|
|
|
@discourseComputed("parentCategory.name", "subCategory")
|
2018-01-24 05:48:20 -05:00
|
|
|
allCategoriesLabel(categoryName, subCategory) {
|
|
|
|
if (subCategory) {
|
|
|
|
return I18n.t("categories.all_subcategories", { categoryName });
|
|
|
|
}
|
|
|
|
return I18n.t("categories.all");
|
|
|
|
},
|
|
|
|
|
2019-11-07 16:38:28 -05:00
|
|
|
@discourseComputed("parentCategory.url", "subCategory")
|
2018-01-24 05:48:20 -05:00
|
|
|
allCategoriesUrl(parentCategoryUrl, subCategory) {
|
2018-11-15 09:21:40 -05:00
|
|
|
return Discourse.getURL(subCategory ? parentCategoryUrl || "/" : "/");
|
2018-01-24 05:48:20 -05:00
|
|
|
},
|
|
|
|
|
2019-11-07 16:38:28 -05:00
|
|
|
@discourseComputed("parentCategory.url")
|
2018-01-24 05:48:20 -05:00
|
|
|
noCategoriesUrl(parentCategoryUrl) {
|
2018-11-15 09:21:40 -05:00
|
|
|
return Discourse.getURL(`${parentCategoryUrl}/none`);
|
2018-01-24 05:48:20 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
actions: {
|
|
|
|
onSelect(categoryId) {
|
2018-11-15 09:21:40 -05:00
|
|
|
let categoryURL;
|
|
|
|
|
|
|
|
if (categoryId === "all-categories") {
|
2019-05-27 04:15:39 -04:00
|
|
|
categoryURL = Discourse.getURL(this.allCategoriesUrl);
|
2018-11-15 09:21:40 -05:00
|
|
|
} else if (categoryId === "no-categories") {
|
2019-05-27 04:15:39 -04:00
|
|
|
categoryURL = Discourse.getURL(this.noCategoriesUrl);
|
2018-11-15 09:21:40 -05:00
|
|
|
} else {
|
|
|
|
const category = Category.findById(parseInt(categoryId, 10));
|
2019-11-08 13:30:41 -05:00
|
|
|
const slug = Category.slugFor(category);
|
2019-11-29 10:47:17 -05:00
|
|
|
categoryURL = Discourse.getURL(`/c/${slug}/${categoryId}`);
|
2018-11-15 09:21:40 -05:00
|
|
|
}
|
|
|
|
|
2018-01-24 05:48:20 -05:00
|
|
|
DiscourseURL.routeTo(categoryURL);
|
2018-10-31 13:33:24 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
onExpand() {
|
2019-05-27 04:15:39 -04:00
|
|
|
if (this.isAsync && isEmpty(this.asyncContent)) {
|
|
|
|
this.set("asyncContent", this.content);
|
2018-10-31 13:33:24 -04:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
onFilter(filter) {
|
2019-05-27 04:15:39 -04:00
|
|
|
if (!this.isAsync) {
|
2018-10-31 13:33:24 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isEmpty(filter)) {
|
2019-05-27 04:15:39 -04:00
|
|
|
this.set("asyncContent", this.content);
|
2018-10-31 13:33:24 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-11-08 13:30:41 -05:00
|
|
|
let results = Category.search(filter);
|
2019-01-08 20:44:24 -05:00
|
|
|
|
|
|
|
if (!this.siteSettings.allow_uncategorized_topics) {
|
|
|
|
results = results.filter(result => {
|
2019-11-13 12:13:47 -05:00
|
|
|
return result.id !== Site.currentProp("uncategorized_category_id");
|
2019-01-08 20:44:24 -05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-10-31 13:33:24 -04:00
|
|
|
results = results.sort((a, b) => {
|
|
|
|
if (a.parent_category_id && !b.parent_category_id) {
|
|
|
|
return 1;
|
|
|
|
} else if (!a.parent_category_id && b.parent_category_id) {
|
|
|
|
return -1;
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
this.set("asyncContent", results);
|
|
|
|
this.autoHighlight();
|
2018-01-24 05:48:20 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|