2020-10-20 07:40:23 +02:00
|
|
|
import Component from "@ember/component";
|
2020-11-10 01:56:11 +01:00
|
|
|
import discourseComputed, { observes } from "discourse-common/utils/decorators";
|
2021-06-08 13:39:49 +02:00
|
|
|
import { alias, equal, or } from "@ember/object/computed";
|
2021-04-12 07:10:02 +02:00
|
|
|
import I18n from "I18n";
|
2020-10-20 07:40:23 +02:00
|
|
|
|
|
|
|
export default Component.extend({
|
2021-03-28 11:06:49 +02:00
|
|
|
tagName: "tr",
|
|
|
|
topicSerializers: ["topic_view", "topic_list_item"],
|
|
|
|
postSerializers: ["post"],
|
|
|
|
groupSerializers: ["basic_group"],
|
|
|
|
categorySerializers: ["basic_category"],
|
|
|
|
showInputs: or("field.new", "field.edit"),
|
|
|
|
classNames: ["custom-field-input"],
|
|
|
|
loading: or("saving", "destroying"),
|
|
|
|
destroyDisabled: alias("loading"),
|
|
|
|
closeDisabled: alias("loading"),
|
2021-06-08 13:39:49 +02:00
|
|
|
isExternal: equal("field.id", "external"),
|
2021-03-28 11:06:49 +02:00
|
|
|
|
2020-11-10 01:56:11 +01:00
|
|
|
didInsertElement() {
|
2021-03-28 11:06:49 +02:00
|
|
|
this.set("originalField", JSON.parse(JSON.stringify(this.field)));
|
2020-11-10 01:56:11 +01:00
|
|
|
},
|
2021-03-28 11:06:49 +02:00
|
|
|
|
2021-11-02 09:58:15 +01:00
|
|
|
@discourseComputed("field.klass")
|
|
|
|
serializerContent(klass) {
|
|
|
|
const serializers = this.get(`${klass}Serializers`);
|
2021-03-28 11:06:49 +02:00
|
|
|
|
2021-11-02 09:58:15 +01:00
|
|
|
if (serializers) {
|
|
|
|
return serializers.reduce((result, key) => {
|
|
|
|
result.push({
|
|
|
|
id: key,
|
|
|
|
name: I18n.t(`admin.wizard.custom_field.serializers.${key}`),
|
|
|
|
});
|
|
|
|
return result;
|
|
|
|
}, []);
|
|
|
|
}
|
|
|
|
},
|
2021-10-23 14:06:12 +02:00
|
|
|
|
2021-03-28 11:06:49 +02:00
|
|
|
@observes("field.klass")
|
2020-11-10 01:56:11 +01:00
|
|
|
clearSerializersWhenClassChanges() {
|
2021-03-28 11:06:49 +02:00
|
|
|
this.set("field.serializers", null);
|
2020-11-10 01:56:11 +01:00
|
|
|
},
|
2021-03-28 11:06:49 +02:00
|
|
|
|
2020-11-10 01:56:11 +01:00
|
|
|
compareArrays(array1, array2) {
|
2021-03-28 11:06:49 +02:00
|
|
|
return (
|
|
|
|
array1.length === array2.length &&
|
|
|
|
array1.every((value, index) => {
|
|
|
|
return value === array2[index];
|
|
|
|
})
|
|
|
|
);
|
2020-11-10 01:56:11 +01:00
|
|
|
},
|
2021-03-28 11:06:49 +02:00
|
|
|
|
2020-11-10 01:56:11 +01:00
|
|
|
@discourseComputed(
|
2021-03-28 11:06:49 +02:00
|
|
|
"saving",
|
2021-06-08 13:39:49 +02:00
|
|
|
"isExternal",
|
2021-03-28 11:06:49 +02:00
|
|
|
"field.name",
|
|
|
|
"field.klass",
|
|
|
|
"field.type",
|
|
|
|
"field.serializers"
|
2020-11-10 01:56:11 +01:00
|
|
|
)
|
2021-06-08 13:39:49 +02:00
|
|
|
saveDisabled(saving, isExternal) {
|
|
|
|
if (saving || isExternal) {
|
2021-04-12 08:12:20 +02:00
|
|
|
return true;
|
|
|
|
}
|
2021-03-28 11:06:49 +02:00
|
|
|
|
2020-11-10 01:56:11 +01:00
|
|
|
const originalField = this.originalField;
|
2021-04-12 08:12:20 +02:00
|
|
|
if (!originalField) {
|
|
|
|
return false;
|
|
|
|
}
|
2021-03-28 11:06:49 +02:00
|
|
|
|
|
|
|
return ["name", "klass", "type", "serializers"].every((attr) => {
|
2020-11-10 01:56:11 +01:00
|
|
|
let current = this.get(attr);
|
|
|
|
let original = originalField[attr];
|
2021-03-28 11:06:49 +02:00
|
|
|
|
2021-04-12 08:12:20 +02:00
|
|
|
if (!current) {
|
|
|
|
return false;
|
|
|
|
}
|
2021-03-28 11:06:49 +02:00
|
|
|
|
2021-04-12 07:44:47 +02:00
|
|
|
if (attr === "serializers") {
|
2020-11-10 01:56:11 +01:00
|
|
|
return this.compareArrays(current, original);
|
|
|
|
} else {
|
2021-04-12 07:44:47 +02:00
|
|
|
return current === original;
|
2020-11-10 01:56:11 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
2021-03-28 11:06:49 +02:00
|
|
|
|
2020-10-20 07:40:23 +02:00
|
|
|
actions: {
|
|
|
|
edit() {
|
2021-03-28 11:06:49 +02:00
|
|
|
this.set("field.edit", true);
|
2020-10-20 07:40:23 +02:00
|
|
|
},
|
2021-03-28 11:06:49 +02:00
|
|
|
|
2020-10-20 07:40:23 +02:00
|
|
|
close() {
|
|
|
|
if (this.field.edit) {
|
2021-03-28 11:06:49 +02:00
|
|
|
this.set("field.edit", false);
|
2020-10-20 07:40:23 +02:00
|
|
|
}
|
2020-11-09 11:44:32 +01:00
|
|
|
},
|
2021-03-28 11:06:49 +02:00
|
|
|
|
2020-11-09 11:44:32 +01:00
|
|
|
destroy() {
|
2021-03-28 11:06:49 +02:00
|
|
|
this.set("destroying", true);
|
2020-11-09 11:44:32 +01:00
|
|
|
this.removeField(this.field);
|
2020-11-10 01:56:11 +01:00
|
|
|
},
|
2021-03-28 11:06:49 +02:00
|
|
|
|
2020-11-10 01:56:11 +01:00
|
|
|
save() {
|
2021-03-28 11:06:49 +02:00
|
|
|
this.set("saving", true);
|
|
|
|
|
2020-11-10 01:56:11 +01:00
|
|
|
const field = this.field;
|
2021-03-28 11:06:49 +02:00
|
|
|
|
2020-11-10 01:56:11 +01:00
|
|
|
let data = {
|
|
|
|
id: field.id,
|
|
|
|
klass: field.klass,
|
|
|
|
type: field.type,
|
|
|
|
serializers: field.serializers,
|
2021-03-28 11:06:49 +02:00
|
|
|
name: field.name,
|
|
|
|
};
|
|
|
|
|
2020-11-10 01:56:11 +01:00
|
|
|
this.saveField(data).then((result) => {
|
2021-03-28 11:06:49 +02:00
|
|
|
this.set("saving", false);
|
2020-11-10 01:56:11 +01:00
|
|
|
if (result.success) {
|
2021-03-28 11:06:49 +02:00
|
|
|
this.set("field.edit", false);
|
2020-11-10 01:56:11 +01:00
|
|
|
} else {
|
2021-03-28 11:06:49 +02:00
|
|
|
this.set("saveIcon", "times");
|
2020-11-10 01:56:11 +01:00
|
|
|
}
|
2024-11-22 15:04:06 +01:00
|
|
|
setTimeout(() => {
|
|
|
|
if (this.isDestroyed) {
|
|
|
|
return;
|
|
|
|
};
|
|
|
|
this.set("saveIcon", null);
|
|
|
|
}, 10000);
|
2020-11-10 01:56:11 +01:00
|
|
|
});
|
2021-03-28 11:06:49 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|