2022-03-16 14:09:23 +01:00
|
|
|
import Component from "@ember/component";
|
2024-11-22 17:11:51 +01:00
|
|
|
import Category from "discourse/models/category";
|
|
|
|
import { observes } from "discourse-common/utils/decorators";
|
2019-07-26 10:59:41 +02:00
|
|
|
|
2022-03-16 14:09:23 +01:00
|
|
|
export default Component.extend({
|
2023-12-08 17:23:01 +01:00
|
|
|
categories: [],
|
|
|
|
|
2019-07-27 08:10:26 +02:00
|
|
|
didInsertElement() {
|
2024-11-22 17:11:51 +01:00
|
|
|
this._super(...arguments);
|
2021-03-28 11:06:49 +02:00
|
|
|
const property = this.field.property || "id";
|
2020-04-15 05:21:40 +02:00
|
|
|
const value = this.field.value;
|
2021-03-28 11:06:49 +02:00
|
|
|
|
2019-07-27 08:10:26 +02:00
|
|
|
if (value) {
|
2021-03-28 11:06:49 +02:00
|
|
|
this.set(
|
|
|
|
"categories",
|
|
|
|
[...value].reduce((result, v) => {
|
|
|
|
let val =
|
|
|
|
property === "id" ? Category.findById(v) : Category.findBySlug(v);
|
2021-04-12 08:26:22 +02:00
|
|
|
if (val) {
|
|
|
|
result.push(val);
|
|
|
|
}
|
2021-03-28 11:06:49 +02:00
|
|
|
return result;
|
|
|
|
}, [])
|
|
|
|
);
|
2019-07-27 08:10:26 +02:00
|
|
|
}
|
|
|
|
},
|
2021-03-28 11:06:49 +02:00
|
|
|
|
|
|
|
@observes("categories")
|
2019-07-26 10:59:41 +02:00
|
|
|
setValue() {
|
2021-03-28 11:06:49 +02:00
|
|
|
const categories = (this.categories || []).filter((c) => !!c);
|
|
|
|
const property = this.field.property || "id";
|
|
|
|
|
|
|
|
if (categories.length) {
|
|
|
|
this.set(
|
|
|
|
"field.value",
|
|
|
|
categories.reduce((result, c) => {
|
|
|
|
if (c && c[property]) {
|
|
|
|
result.push(c[property]);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}, [])
|
|
|
|
);
|
2019-07-26 11:12:58 +02:00
|
|
|
}
|
2021-03-28 11:06:49 +02:00
|
|
|
},
|
|
|
|
});
|