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

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