2018-11-24 17:03:51 +08:00

128 lines
4.8 KiB
JavaScript

import BaseEditRoute from '../base-edit';
import $ from 'jquery';
export default BaseEditRoute.extend({
afterModel(model) {
const me = this;
me._super(...arguments);
if (model.owner && model.users) {
let owner = model.users.findBy('id', model.owner);
owner && (owner.selected = true);
}
this.set('breadcrumbs',
[{route: 'customer-application.list', params: 1, text: 'Customer Application'},
{text: 'Edit Customer Application[' + model.name + ']'}]);
},
actions: {
addImage() {
const me = this;
me.set('controller.model.addImage', true);
me.set('controller.model.image', {});
},
submitAddImage() {
const me = this;
me.set('controller.errors', {});
if (me.get('controller.model.image.file')) {
me.set('controller.errors', {});
me.get('store').ajaxPost('application/add-image',
new FormData($('#form_' + me.get('controller.model.id'))[0]))
.then(image => {
me.get('controller.model.images').pushObject(image);
me.set('controller.model.addImage', false);
});
}
else {
me.set('controller.errors.image', ['No image file selected']);
}
},
closeAddImage() {
const me = this;
me.set('controller.model.addImage', false);
},
removeImage(image) {
const me = this;
me.get('dialog').confirm('Are you sure to remove image?', () => {
me.get('store').ajaxPost('application/remove-image', image).then(() => {
me.get('message').alert('Image removed');
me.get('controller.model.images').removeObject(image);
});
});
},
moveImageUp(image) {
const me = this;
me.moveUp(me.get('controller.model.images'), image, 'updateImage');
},
moveImageDown(image) {
const me = this;
me.moveDown(me.get('controller.model.images'), image, 'updateImage');
},
// attachments
addAttachment() {
const me = this;
me.set('controller.model.addAttachment', true);
me.set('controller.model.attachment', {});
},
submitAddAttachment() {
const me = this;
me.set('controller.errors', {});
if (me.get('controller.model.attachment.file')) {
me.get('store').ajaxPost('application/add-attachment',
new FormData($('#form_' + me.get('controller.model.id'))[0]))
.then(attachment => {
me.get('controller.model.attachments').pushObject(attachment);
me.set('controller.model.addAttachment', false);
});
}
else {
me.set('controller.errors.attachment', ['No attachment file selected']);
}
},
closeAddAttachment() {
const me = this;
me.set('controller.model.addAttachment', false);
},
removeAttachment(attachment) {
const me = this;
me.get('dialog').confirm('Are you sure to remove attachment?', () => {
me.get('store').ajaxPost('application/remove-attachment', attachment).then(() => {
me.get('message').alert('Attachment removed');
me.get('controller.model.attachments').removeObject(attachment);
});
});
},
moveAttachmentUp(attachment) {
const me = this;
me.moveUp(me.get('controller.model.attachments'), attachment, 'updateAttachment');
},
moveAttachmentDown(attachment) {
const me = this;
me.moveDown(me.get('controller.model.attachments'), attachment, 'updateAttachment');
}
},
updateImage(image) {
this.get('ajax').doPost('application/update-image', image, false);
},
updateAttachment(attachment) {
this.get('ajax').doPost('application/update-attachment', attachment, false);
},
moveUp(files, file, update) {
if (files && files.length > 1) {
let index = files.indexOf(file);
files.removeObject(file);
files.insertAt(index - 1, file);
--file.sort;
this[update](file);
}
},
moveDown(files, file, update) {
if (files && files.length > 1) {
let index = files.indexOf(file);
files.removeObject(file);
files.insertAt(index + 1, file);
++file.sort;
this[update](file);
}
}
});