2016-06-30 12:21:47 +02:00
|
|
|
import computed from 'ember-computed';
|
|
|
|
import injectService from 'ember-service/inject';
|
|
|
|
import {isEmpty} from 'ember-utils';
|
2016-05-24 14:06:59 +02:00
|
|
|
import ModalComponent from 'ghost-admin/components/modals/base';
|
|
|
|
import cajaSanitizers from 'ghost-admin/utils/caja-sanitizers';
|
2017-01-19 12:40:31 +01:00
|
|
|
import {task} from 'ember-concurrency';
|
2015-11-18 11:50:48 +01:00
|
|
|
|
|
|
|
export default ModalComponent.extend({
|
|
|
|
model: null,
|
|
|
|
|
2016-02-26 14:25:47 +01:00
|
|
|
url: '',
|
|
|
|
newUrl: '',
|
|
|
|
|
2016-06-30 12:21:47 +02:00
|
|
|
config: injectService(),
|
|
|
|
notifications: injectService(),
|
2015-11-18 11:50:48 +01:00
|
|
|
|
2016-02-26 14:25:47 +01:00
|
|
|
image: computed('model.model', 'model.imageProperty', {
|
2015-11-18 11:50:48 +01: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 14:25:47 +01:00
|
|
|
didReceiveAttrs() {
|
|
|
|
let image = this.get('image');
|
|
|
|
this.set('url', image);
|
|
|
|
this.set('newUrl', image);
|
2015-11-18 11:50:48 +01:00
|
|
|
},
|
|
|
|
|
2016-02-26 14:25:47 +01: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 11:50:48 +01:00
|
|
|
keyDown() {
|
|
|
|
this._setErrorState(false);
|
|
|
|
},
|
|
|
|
|
|
|
|
_setErrorState(state) {
|
|
|
|
if (state) {
|
2016-02-26 14:25:47 +01:00
|
|
|
this.$('.url').addClass('error');
|
2015-11-18 11:50:48 +01:00
|
|
|
} else {
|
2016-02-26 14:25:47 +01:00
|
|
|
this.$('.url').removeClass('error');
|
2015-11-18 11:50:48 +01:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2016-02-26 14:25:47 +01:00
|
|
|
_validateUrl(url) {
|
|
|
|
if (!isEmpty(url) && !cajaSanitizers.url(url)) {
|
|
|
|
this._setErrorState(true);
|
|
|
|
return {message: 'Image URI is not valid'};
|
2015-11-18 11:50:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
},
|
2016-02-26 14:25:47 +01:00
|
|
|
// end validation
|
2015-11-18 11:50:48 +01:00
|
|
|
|
2017-01-19 12:40:31 +01:00
|
|
|
uploadImage: task(function* () {
|
|
|
|
let model = this.get('model.model');
|
|
|
|
let newUrl = this.get('newUrl');
|
|
|
|
let result = this._validateUrl(newUrl);
|
|
|
|
let notifications = this.get('notifications');
|
|
|
|
|
|
|
|
if (result === true) {
|
|
|
|
this.set('image', newUrl);
|
|
|
|
|
|
|
|
try {
|
|
|
|
yield model.save();
|
|
|
|
} catch (e) {
|
|
|
|
notifications.showAPIError(e, {key: 'image.upload'});
|
|
|
|
} finally {
|
|
|
|
this.send('closeModal');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}).drop(),
|
|
|
|
|
2015-11-18 11:50:48 +01:00
|
|
|
actions: {
|
2016-02-26 14:25:47 +01:00
|
|
|
fileUploaded(url) {
|
|
|
|
this.set('url', url);
|
|
|
|
this.set('newUrl', url);
|
|
|
|
},
|
|
|
|
|
|
|
|
removeImage() {
|
|
|
|
this.set('url', '');
|
|
|
|
this.set('newUrl', '');
|
|
|
|
},
|
|
|
|
|
2015-11-18 11:50:48 +01:00
|
|
|
confirm() {
|
2017-01-19 12:40:31 +01:00
|
|
|
this.get('uploadImage').perform();
|
2015-11-18 11:50:48 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|