import BaseEditRoute from '../base-edit'; import $ from 'jquery'; export default BaseEditRoute.extend({ afterModel(model) { const me = this; me._super(...arguments); this.set('breadcrumbs', [{route: 'local-product.list', params: 1, text: 'Local Product'}, {text: 'Edit Local Product [' + 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('local-product/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('local-product/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('local-product/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('local-product/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('local-product/update-image', image, false); }, updateAttachment(attachment) { this.get('ajax').doPost('local-product/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); } } });