1
0
Fork 0
discourse-custom-wizard-unl.../assets/javascripts/discourse/components/custom-field-input.js.es6

136 Zeilen
3 KiB
Text

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-04-12 07:10:02 +02:00
import { alias, or } from "@ember/object/computed";
import I18n from "I18n";
2020-10-20 07:40:23 +02:00
const generateContent = function (array, type) {
return array.map((key) => ({
2020-10-20 07:40:23 +02:00
id: key,
name: I18n.t(`admin.wizard.custom_field.${type}.${key}`),
2020-10-20 07:40:23 +02:00
}));
};
2020-10-20 07:40:23 +02:00
export default Component.extend({
tagName: "tr",
topicSerializers: ["topic_view", "topic_list_item"],
postSerializers: ["post"],
groupSerializers: ["basic_group"],
categorySerializers: ["basic_category"],
klassContent: generateContent(
["topic", "post", "group", "category"],
"klass"
),
typeContent: generateContent(
["string", "boolean", "integer", "json"],
"type"
),
showInputs: or("field.new", "field.edit"),
classNames: ["custom-field-input"],
loading: or("saving", "destroying"),
destroyDisabled: alias("loading"),
closeDisabled: alias("loading"),
2020-11-10 01:56:11 +01:00
didInsertElement() {
this.set("originalField", JSON.parse(JSON.stringify(this.field)));
2020-11-10 01:56:11 +01:00
},
@discourseComputed("field.klass")
2021-04-12 07:44:47 +02:00
serializerContent(klass) {
2020-10-20 07:40:23 +02:00
const serializers = this.get(`${klass}Serializers`);
2020-10-20 07:40:23 +02:00
if (serializers) {
return generateContent(serializers, "serializers");
2020-10-20 07:40:23 +02:00
} else {
return [];
}
},
@observes("field.klass")
2020-11-10 01:56:11 +01:00
clearSerializersWhenClassChanges() {
this.set("field.serializers", null);
2020-11-10 01:56:11 +01:00
},
2020-11-10 01:56:11 +01:00
compareArrays(array1, array2) {
return (
array1.length === array2.length &&
array1.every((value, index) => {
return value === array2[index];
})
);
2020-11-10 01:56:11 +01:00
},
2020-11-10 01:56:11 +01:00
@discourseComputed(
"saving",
"field.name",
"field.klass",
"field.type",
"field.serializers"
2020-11-10 01:56:11 +01:00
)
saveDisabled(saving) {
2021-04-12 08:12:20 +02:00
if (saving) {
return true;
}
2020-11-10 01:56:11 +01:00
const originalField = this.originalField;
2021-04-12 08:12:20 +02:00
if (!originalField) {
return false;
}
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-04-12 08:12:20 +02:00
if (!current) {
return false;
}
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
}
});
},
2020-10-20 07:40:23 +02:00
actions: {
edit() {
this.set("field.edit", true);
2020-10-20 07:40:23 +02:00
},
2020-10-20 07:40:23 +02:00
close() {
if (this.field.edit) {
this.set("field.edit", false);
2020-10-20 07:40:23 +02:00
}
2020-11-09 11:44:32 +01:00
},
2020-11-09 11:44:32 +01:00
destroy() {
this.set("destroying", true);
2020-11-09 11:44:32 +01:00
this.removeField(this.field);
2020-11-10 01:56:11 +01:00
},
2020-11-10 01:56:11 +01:00
save() {
this.set("saving", true);
2020-11-10 01:56:11 +01:00
const field = this.field;
2020-11-10 01:56:11 +01:00
let data = {
id: field.id,
klass: field.klass,
type: field.type,
serializers: field.serializers,
name: field.name,
};
2020-11-10 01:56:11 +01:00
this.saveField(data).then((result) => {
this.set("saving", false);
2020-11-10 01:56:11 +01:00
if (result.success) {
this.set("field.edit", false);
2020-11-10 01:56:11 +01:00
} else {
this.set("saveIcon", "times");
2020-11-10 01:56:11 +01:00
}
setTimeout(() => this.set("saveIcon", null), 10000);
2020-11-10 01:56:11 +01:00
});
},
},
});