2017-09-11 10:41:17 +03:00
|
|
|
import ModalComponent from 'ghost-admin/components/modal-base';
|
2017-05-29 21:50:03 +03:00
|
|
|
import cajaSanitizers from 'ghost-admin/utils/caja-sanitizers';
|
2017-08-22 10:53:26 +03:00
|
|
|
import {computed} from '@ember/object';
|
|
|
|
import {isEmpty} from '@ember/utils';
|
2017-10-30 12:38:01 +03:00
|
|
|
import {inject as service} from '@ember/service';
|
2017-01-19 14:40:31 +03:00
|
|
|
import {task} from 'ember-concurrency';
|
2015-11-18 13:50:48 +03:00
|
|
|
|
|
|
|
export default ModalComponent.extend({
|
2018-01-11 20:43:23 +03:00
|
|
|
config: service(),
|
|
|
|
notifications: service(),
|
|
|
|
|
2015-11-18 13:50:48 +03:00
|
|
|
model: null,
|
|
|
|
|
2016-02-26 16:25:47 +03:00
|
|
|
url: '',
|
|
|
|
newUrl: '',
|
2017-07-31 12:44:26 +03:00
|
|
|
_isUploading: false,
|
2016-02-26 16:25:47 +03:00
|
|
|
|
2017-11-24 22:02:23 +03:00
|
|
|
image: computed('model.{model,imageProperty}', {
|
2015-11-18 13:50:48 +03:00
|
|
|
get() {
|
|
|
|
let imageProperty = this.get('model.imageProperty');
|
|
|
|
|
|
|
|
return this.get(`model.model.${imageProperty}`);
|
|
|
|
},
|
|
|
|
|
|
|
|
set(key, value) {
|
|
|
|
let model = this.get('model.model');
|
|
|
|
let imageProperty = this.get('model.imageProperty');
|
|
|
|
|
|
|
|
return model.set(imageProperty, value);
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
|
2016-02-26 16:25:47 +03:00
|
|
|
didReceiveAttrs() {
|
2021-07-15 17:27:29 +03:00
|
|
|
this._super(...arguments);
|
|
|
|
|
2019-03-06 16:53:54 +03:00
|
|
|
let image = this.image;
|
2016-02-26 16:25:47 +03:00
|
|
|
this.set('url', image);
|
|
|
|
this.set('newUrl', image);
|
2015-11-18 13:50:48 +03:00
|
|
|
},
|
|
|
|
|
2018-01-11 20:43:23 +03:00
|
|
|
actions: {
|
|
|
|
fileUploaded(url) {
|
|
|
|
this.set('url', url);
|
|
|
|
this.set('newUrl', url);
|
|
|
|
},
|
|
|
|
|
|
|
|
removeImage() {
|
|
|
|
this.set('url', '');
|
|
|
|
this.set('newUrl', '');
|
|
|
|
},
|
|
|
|
|
|
|
|
confirm() {
|
2019-03-06 16:53:54 +03:00
|
|
|
this.uploadImage.perform();
|
2018-01-11 20:43:23 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
isUploading() {
|
|
|
|
this.toggleProperty('_isUploading');
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2016-02-26 16:25:47 +03:00
|
|
|
// TODO: should validation be handled in the gh-image-uploader component?
|
|
|
|
// pro - consistency everywhere, simplification here
|
|
|
|
// con - difficult if the "save" is happening externally as it does here
|
|
|
|
//
|
|
|
|
// maybe it should be handled at the model level?
|
|
|
|
// - automatically present everywhere
|
|
|
|
// - file uploads should always result in valid urls so it should only
|
|
|
|
// affect the url input form
|
2015-11-18 13:50:48 +03:00
|
|
|
keyDown() {
|
|
|
|
this._setErrorState(false);
|
|
|
|
},
|
|
|
|
|
|
|
|
_setErrorState(state) {
|
|
|
|
if (state) {
|
2020-01-10 18:12:39 +03:00
|
|
|
this.element.querySelector('.url').classList.add('error');
|
2015-11-18 13:50:48 +03:00
|
|
|
} else {
|
2020-01-10 18:12:39 +03:00
|
|
|
this.element.querySelector('.url').classList.remove('error');
|
2015-11-18 13:50:48 +03:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2016-02-26 16:25:47 +03:00
|
|
|
_validateUrl(url) {
|
|
|
|
if (!isEmpty(url) && !cajaSanitizers.url(url)) {
|
|
|
|
this._setErrorState(true);
|
|
|
|
return {message: 'Image URI is not valid'};
|
2015-11-18 13:50:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
},
|
2016-02-26 16:25:47 +03:00
|
|
|
// end validation
|
2015-11-18 13:50:48 +03:00
|
|
|
|
2017-01-19 14:40:31 +03:00
|
|
|
uploadImage: task(function* () {
|
|
|
|
let model = this.get('model.model');
|
2019-03-06 16:53:54 +03:00
|
|
|
let newUrl = this.newUrl;
|
2017-01-19 14:40:31 +03:00
|
|
|
let result = this._validateUrl(newUrl);
|
2019-03-06 16:53:54 +03:00
|
|
|
let notifications = this.notifications;
|
2017-01-19 14:40:31 +03:00
|
|
|
|
|
|
|
if (result === true) {
|
|
|
|
this.set('image', newUrl);
|
|
|
|
|
|
|
|
try {
|
|
|
|
yield model.save();
|
|
|
|
} catch (e) {
|
|
|
|
notifications.showAPIError(e, {key: 'image.upload'});
|
|
|
|
} finally {
|
|
|
|
this.send('closeModal');
|
|
|
|
}
|
|
|
|
}
|
2018-01-11 20:43:23 +03:00
|
|
|
}).drop()
|
2015-11-18 13:50:48 +03:00
|
|
|
});
|