0
0
Fork 1
Spiegel von https://github.com/paviliondev/discourse-custom-wizard.git synchronisiert 2024-11-24 10:20:28 +01:00
discourse-custom-wizard/assets/javascripts/discourse/components/custom-field-input.js

133 Zeilen
3 KiB
JavaScript

2020-10-20 07:40:23 +02:00
import Component from "@ember/component";
import { alias, equal, or } from "@ember/object/computed";
2024-11-22 17:11:51 +01:00
import discourseComputed, { observes } from "discourse-common/utils/decorators";
2021-04-12 07:10:02 +02:00
import I18n from "I18n";
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"],
showInputs: or("field.new", "field.edit"),
classNames: ["custom-field-input"],
loading: or("saving", "destroying"),
destroyDisabled: alias("loading"),
closeDisabled: alias("loading"),
isExternal: equal("field.id", "external"),
2020-11-10 01:56:11 +01:00
didInsertElement() {
2024-11-22 17:11:51 +01:00
this._super(...arguments);
this.set("originalField", JSON.parse(JSON.stringify(this.field)));
2020-11-10 01:56:11 +01:00
},
@discourseComputed("field.klass")
serializerContent(klass) {
const serializers = this.get(`${klass}Serializers`);
if (serializers) {
return serializers.reduce((result, key) => {
result.push({
id: key,
name: I18n.t(`admin.wizard.custom_field.serializers.${key}`),
});
return result;
}, []);
}
},
@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",
"isExternal",
"field.name",
"field.klass",
"field.type",
"field.serializers"
2020-11-10 01:56:11 +01:00
)
saveDisabled(saving, isExternal) {
if (saving || isExternal) {
2021-04-12 08:12:20 +02:00
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
}
2024-11-22 15:04:06 +01:00
setTimeout(() => {
if (this.isDestroyed) {
return;
2024-11-22 16:51:24 +01:00
}
2024-11-22 15:04:06 +01:00
this.set("saveIcon", null);
}, 10000);
2020-11-10 01:56:11 +01:00
});
},
},
});