2015-11-18 13:50:48 +03:00
|
|
|
import Ember from 'ember';
|
2016-05-24 15:06:59 +03:00
|
|
|
import ModalComponent from 'ghost-admin/components/modals/base';
|
|
|
|
import cajaSanitizers from 'ghost-admin/utils/caja-sanitizers';
|
2015-11-18 13:50:48 +03:00
|
|
|
|
2016-01-19 16:03:27 +03:00
|
|
|
const {
|
|
|
|
computed,
|
|
|
|
inject: {service},
|
|
|
|
isEmpty
|
|
|
|
} = Ember;
|
2015-11-18 13:50:48 +03:00
|
|
|
|
|
|
|
export default ModalComponent.extend({
|
|
|
|
model: null,
|
|
|
|
submitting: false,
|
|
|
|
|
2016-02-26 16:25:47 +03:00
|
|
|
url: '',
|
|
|
|
newUrl: '',
|
|
|
|
|
2016-01-19 16:03:27 +03:00
|
|
|
config: service(),
|
|
|
|
notifications: service(),
|
2015-11-18 13:50:48 +03:00
|
|
|
|
2016-02-26 16:25:47 +03:00
|
|
|
image: computed('model.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() {
|
|
|
|
let image = this.get('image');
|
|
|
|
this.set('url', image);
|
|
|
|
this.set('newUrl', image);
|
2015-11-18 13:50:48 +03:00
|
|
|
},
|
|
|
|
|
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) {
|
2016-02-26 16:25:47 +03:00
|
|
|
this.$('.url').addClass('error');
|
2015-11-18 13:50:48 +03:00
|
|
|
} else {
|
2016-02-26 16:25:47 +03:00
|
|
|
this.$('.url').removeClass('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
|
|
|
|
|
|
|
actions: {
|
2016-02-26 16:25:47 +03:00
|
|
|
fileUploaded(url) {
|
|
|
|
this.set('url', url);
|
|
|
|
this.set('newUrl', url);
|
|
|
|
},
|
|
|
|
|
|
|
|
removeImage() {
|
|
|
|
this.set('url', '');
|
|
|
|
this.set('newUrl', '');
|
|
|
|
},
|
|
|
|
|
2015-11-18 13:50:48 +03:00
|
|
|
confirm() {
|
|
|
|
let model = this.get('model.model');
|
2016-02-26 16:25:47 +03:00
|
|
|
let newUrl = this.get('newUrl');
|
|
|
|
let result = this._validateUrl(newUrl);
|
2015-11-18 13:50:48 +03:00
|
|
|
let notifications = this.get('notifications');
|
|
|
|
|
2016-02-26 16:25:47 +03:00
|
|
|
if (result === true) {
|
2015-11-18 13:50:48 +03:00
|
|
|
this.set('submitting', true);
|
2016-02-26 16:25:47 +03:00
|
|
|
this.set('image', newUrl);
|
2015-11-18 13:50:48 +03:00
|
|
|
|
|
|
|
model.save().catch((err) => {
|
|
|
|
notifications.showAPIError(err, {key: 'image.upload'});
|
|
|
|
}).finally(() => {
|
|
|
|
this.send('closeModal');
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|