2017-11-21 05:53:09 -05:00
|
|
|
import ComboBoxComponent from "select-kit/components/combo-box";
|
2019-11-07 16:38:28 -05:00
|
|
|
import discourseComputed from "discourse-common/utils/decorators";
|
2017-11-21 05:53:09 -05:00
|
|
|
import PermissionType from "discourse/models/permission-type";
|
|
|
|
import Category from "discourse/models/category";
|
2018-02-14 05:49:23 -05:00
|
|
|
import { categoryBadgeHTML } from "discourse/helpers/category-link";
|
2020-01-16 12:51:29 -05:00
|
|
|
const { get, isPresent, isEmpty } = Ember;
|
2017-11-21 05:53:09 -05:00
|
|
|
|
|
|
|
export default ComboBoxComponent.extend({
|
|
|
|
pluginApiIdentifiers: ["category-chooser"],
|
|
|
|
classNames: "category-chooser",
|
|
|
|
filterable: true,
|
|
|
|
castInteger: true,
|
|
|
|
allowUncategorized: false,
|
|
|
|
rowComponent: "category-row",
|
|
|
|
noneRowComponent: "none-category-row",
|
|
|
|
allowSubCategories: true,
|
2018-01-25 06:03:13 -05:00
|
|
|
permissionType: PermissionType.FULL,
|
2017-11-21 05:53:09 -05:00
|
|
|
|
2018-01-25 04:41:10 -05:00
|
|
|
init() {
|
2019-01-19 04:05:51 -05:00
|
|
|
this._super(...arguments);
|
2018-01-25 04:41:10 -05:00
|
|
|
|
2019-05-27 04:15:39 -04:00
|
|
|
this.rowComponentOptions.setProperties({
|
|
|
|
allowUncategorized: this.allowUncategorized
|
2018-01-25 04:41:10 -05:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2017-11-21 05:53:09 -05:00
|
|
|
filterComputedContent(computedContent, computedValue, filter) {
|
2018-06-15 11:03:24 -04:00
|
|
|
if (isEmpty(filter)) {
|
|
|
|
return computedContent;
|
|
|
|
}
|
2017-11-21 05:53:09 -05:00
|
|
|
|
2019-05-27 04:15:39 -04:00
|
|
|
if (this.scopedCategoryId) {
|
2019-06-24 04:45:30 -04:00
|
|
|
computedContent = this.categoriesByScope(this.scopedCategoryId).map(c =>
|
2019-01-10 10:49:53 -05:00
|
|
|
this.computeContentItem(c)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-11-21 05:53:09 -05:00
|
|
|
const _matchFunction = (f, text) => {
|
2018-06-26 06:19:14 -04:00
|
|
|
return this._normalize(text).indexOf(f) > -1;
|
2017-11-21 05:53:09 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
return computedContent.filter(c => {
|
|
|
|
const category = Category.findById(get(c, "value"));
|
|
|
|
const text = get(c, "name");
|
|
|
|
if (category && category.get("parentCategory")) {
|
|
|
|
const categoryName = category.get("parentCategory.name");
|
2018-06-15 11:03:24 -04:00
|
|
|
return (
|
2018-06-26 06:19:14 -04:00
|
|
|
_matchFunction(filter, text) || _matchFunction(filter, categoryName)
|
2018-06-15 11:03:24 -04:00
|
|
|
);
|
2017-11-21 05:53:09 -05:00
|
|
|
} else {
|
2018-06-26 06:19:14 -04:00
|
|
|
return _matchFunction(filter, text);
|
2017-11-21 05:53:09 -05:00
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2019-11-07 16:38:28 -05:00
|
|
|
@discourseComputed("rootNone", "rootNoneLabel")
|
2017-11-21 05:53:09 -05:00
|
|
|
none(rootNone, rootNoneLabel) {
|
2020-01-16 12:51:29 -05:00
|
|
|
if (isPresent(rootNone)) {
|
|
|
|
return rootNoneLabel || "category.none";
|
|
|
|
} else if (
|
2018-06-15 11:03:24 -04:00
|
|
|
this.siteSettings.allow_uncategorized_topics ||
|
2019-05-27 04:15:39 -04:00
|
|
|
this.allowUncategorized
|
2018-06-15 11:03:24 -04:00
|
|
|
) {
|
2020-01-16 12:51:29 -05:00
|
|
|
return Category.findUncategorized();
|
2017-11-21 05:53:09 -05:00
|
|
|
} else {
|
|
|
|
return "category.choose";
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2018-02-14 05:49:23 -05:00
|
|
|
computeHeaderContent() {
|
2019-01-19 04:05:51 -05:00
|
|
|
let content = this._super(...arguments);
|
2018-02-14 05:49:23 -05:00
|
|
|
|
2019-05-27 04:15:39 -04:00
|
|
|
if (this.hasSelection) {
|
2018-02-14 05:49:23 -05:00
|
|
|
const category = Category.findById(content.value);
|
2020-01-19 06:07:54 -05:00
|
|
|
content.label = categoryBadgeHTML(category, {
|
2018-02-14 05:49:23 -05:00
|
|
|
link: false,
|
2020-01-19 06:07:54 -05:00
|
|
|
hideParent: !!category.parent_category_id,
|
|
|
|
allowUncategorized: true,
|
|
|
|
recursive: true
|
2018-02-14 05:49:23 -05:00
|
|
|
}).htmlSafe();
|
|
|
|
}
|
|
|
|
|
|
|
|
return content;
|
|
|
|
},
|
|
|
|
|
2018-03-23 11:12:22 -04:00
|
|
|
didSelect(computedContentItem) {
|
|
|
|
if (this.attrs.onChooseCategory) {
|
|
|
|
this.attrs.onChooseCategory(computedContentItem.originalContent);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2019-01-03 12:03:01 -05:00
|
|
|
didClearSelection() {
|
|
|
|
if (this.attrs.onChooseCategory) {
|
|
|
|
this.attrs.onChooseCategory(null);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2017-11-21 05:53:09 -05:00
|
|
|
computeContent() {
|
2019-05-27 04:15:39 -04:00
|
|
|
return this.categoriesByScope(this.scopedCategoryId);
|
2019-01-10 10:49:53 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
categoriesByScope(scopedCategoryId = null) {
|
2018-06-15 11:03:24 -04:00
|
|
|
const categories = Discourse.SiteSettings.fixed_category_positions_on_create
|
|
|
|
? Category.list()
|
|
|
|
: Category.listByActivity();
|
2017-11-21 05:53:09 -05:00
|
|
|
|
|
|
|
if (scopedCategoryId) {
|
|
|
|
const scopedCat = Category.findById(scopedCategoryId);
|
2018-06-15 11:03:24 -04:00
|
|
|
scopedCategoryId =
|
|
|
|
scopedCat.get("parent_category_id") || scopedCat.get("id");
|
2017-11-21 05:53:09 -05:00
|
|
|
}
|
|
|
|
|
2019-05-27 04:15:39 -04:00
|
|
|
const excludeCategoryId = this.excludeCategoryId;
|
2017-11-21 05:53:09 -05:00
|
|
|
|
|
|
|
return categories.filter(c => {
|
|
|
|
const categoryId = this.valueForContentItem(c);
|
|
|
|
|
2018-06-15 11:03:24 -04:00
|
|
|
if (
|
|
|
|
scopedCategoryId &&
|
|
|
|
categoryId !== scopedCategoryId &&
|
|
|
|
get(c, "parent_category_id") !== scopedCategoryId
|
|
|
|
) {
|
2017-11-21 05:53:09 -05:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-05-27 04:15:39 -04:00
|
|
|
if (this.allowSubCategories === false && c.get("parentCategory")) {
|
2017-11-21 05:53:09 -05:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-06-15 11:03:24 -04:00
|
|
|
if (
|
2019-05-27 04:15:39 -04:00
|
|
|
(this.allowUncategorized === false &&
|
2018-06-15 11:03:24 -04:00
|
|
|
get(c, "isUncategorizedCategory")) ||
|
|
|
|
excludeCategoryId === categoryId
|
|
|
|
) {
|
2017-11-21 05:53:09 -05:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-05-27 04:15:39 -04:00
|
|
|
if (this.permissionType) {
|
|
|
|
return this.permissionType === get(c, "permission");
|
2018-01-25 06:03:13 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2017-11-21 05:53:09 -05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|