2017-11-21 05:53:09 -05:00
|
|
|
import ComboBoxComponent from "select-kit/components/combo-box";
|
|
|
|
import { on } from "ember-addons/ember-computed-decorators";
|
|
|
|
import computed from "ember-addons/ember-computed-decorators";
|
|
|
|
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";
|
2017-11-21 05:53:09 -05:00
|
|
|
const { get, isNone, isEmpty } = Ember;
|
|
|
|
|
|
|
|
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() {
|
|
|
|
this._super();
|
|
|
|
|
|
|
|
this.get("rowComponentOptions").setProperties({
|
|
|
|
allowUncategorized: this.get("allowUncategorized"),
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2017-11-21 05:53:09 -05:00
|
|
|
filterComputedContent(computedContent, computedValue, filter) {
|
|
|
|
if (isEmpty(filter)) { return computedContent; }
|
|
|
|
|
|
|
|
const _matchFunction = (f, text) => {
|
|
|
|
return text.toLowerCase().indexOf(f) > -1;
|
|
|
|
};
|
|
|
|
const lowerFilter = filter.toLowerCase();
|
|
|
|
|
|
|
|
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");
|
|
|
|
return _matchFunction(lowerFilter, text) || _matchFunction(lowerFilter, categoryName);
|
|
|
|
} else {
|
|
|
|
return _matchFunction(lowerFilter, text);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
@computed("rootNone", "rootNoneLabel")
|
|
|
|
none(rootNone, rootNoneLabel) {
|
|
|
|
if (this.siteSettings.allow_uncategorized_topics || this.get("allowUncategorized")) {
|
|
|
|
if (!isNone(rootNone)) {
|
|
|
|
return rootNoneLabel || "category.none";
|
|
|
|
} else {
|
|
|
|
return Category.findUncategorized();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return "category.choose";
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2018-02-14 05:49:23 -05:00
|
|
|
computeHeaderContent() {
|
2018-03-22 06:29:55 -04:00
|
|
|
let content = this._super();
|
2018-02-14 05:49:23 -05:00
|
|
|
|
|
|
|
if (this.get("hasSelection")) {
|
|
|
|
const category = Category.findById(content.value);
|
|
|
|
const parentCategoryId = category.get("parent_category_id");
|
2018-02-14 10:42:20 -05:00
|
|
|
const hasParentCategory = Ember.isPresent(parentCategoryId);
|
2018-02-14 05:49:23 -05:00
|
|
|
|
|
|
|
let badge = "";
|
|
|
|
|
2018-02-14 10:42:20 -05:00
|
|
|
if (hasParentCategory) {
|
2018-02-14 05:49:23 -05:00
|
|
|
const parentCategory = Category.findById(parentCategoryId);
|
|
|
|
badge += categoryBadgeHTML(parentCategory, {
|
|
|
|
link: false,
|
|
|
|
allowUncategorized: true
|
|
|
|
}).htmlSafe();
|
|
|
|
}
|
|
|
|
|
|
|
|
badge += categoryBadgeHTML(category, {
|
|
|
|
link: false,
|
2018-02-14 10:42:20 -05:00
|
|
|
hideParent: hasParentCategory ? true : false,
|
2018-02-14 05:49:23 -05:00
|
|
|
allowUncategorized: true
|
|
|
|
}).htmlSafe();
|
|
|
|
|
|
|
|
content.label = badge;
|
|
|
|
}
|
|
|
|
|
|
|
|
return content;
|
|
|
|
},
|
|
|
|
|
2017-11-21 05:53:09 -05:00
|
|
|
@on("didRender")
|
|
|
|
_bindComposerResizing() {
|
|
|
|
this.appEvents.on("composer:resized", this, this.applyDirection);
|
|
|
|
},
|
|
|
|
|
|
|
|
@on("willDestroyElement")
|
|
|
|
_unbindComposerResizing() {
|
|
|
|
this.appEvents.off("composer:resized");
|
|
|
|
},
|
|
|
|
|
2018-03-23 11:12:22 -04:00
|
|
|
didSelect(computedContentItem) {
|
|
|
|
if (this.attrs.onChooseCategory) {
|
|
|
|
this.attrs.onChooseCategory(computedContentItem.originalContent);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2017-11-21 05:53:09 -05:00
|
|
|
computeContent() {
|
|
|
|
const categories = Discourse.SiteSettings.fixed_category_positions_on_create ?
|
|
|
|
Category.list() :
|
|
|
|
Category.listByActivity();
|
|
|
|
|
|
|
|
let scopedCategoryId = this.get("scopedCategoryId");
|
|
|
|
if (scopedCategoryId) {
|
|
|
|
const scopedCat = Category.findById(scopedCategoryId);
|
|
|
|
scopedCategoryId = scopedCat.get("parent_category_id") || scopedCat.get("id");
|
|
|
|
}
|
|
|
|
|
|
|
|
const excludeCategoryId = this.get("excludeCategoryId");
|
|
|
|
|
|
|
|
return categories.filter(c => {
|
|
|
|
const categoryId = this.valueForContentItem(c);
|
|
|
|
|
|
|
|
if (scopedCategoryId && categoryId !== scopedCategoryId && get(c, "parent_category_id") !== scopedCategoryId) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.get("allowSubCategories") === false && c.get("parentCategory") ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((this.get("allowUncategorized") === false && get(c, "isUncategorizedCategory")) || excludeCategoryId === categoryId) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-01-25 06:03:13 -05:00
|
|
|
if (this.get("permissionType")) {
|
|
|
|
return this.get("permissionType") === get(c, "permission");
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2017-11-21 05:53:09 -05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|