0
0
Fork 1
Spiegel von https://github.com/paviliondev/discourse-custom-wizard.git synchronisiert 2024-11-30 04:30:29 +01:00
discourse-custom-wizard/assets/javascripts/discourse/components/custom-field-input.js.es6

129 Zeilen
2,9 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";
import { or, alias } from "@ember/object/computed";
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")
2020-11-10 01:56:11 +01:00
serializerContent(klass, p2) {
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) {
if (saving) return true;
2020-11-10 01:56:11 +01:00
const originalField = this.originalField;
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];
2020-11-10 01:56:11 +01:00
if (!current) return false;
if (attr == "serializers") {
2020-11-10 01:56:11 +01:00
return this.compareArrays(current, original);
} else {
return current == original;
}
});
},
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
});
},
},
});