mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-02-03 23:00:14 -05:00
87 lines
2.3 KiB
JavaScript
87 lines
2.3 KiB
JavaScript
|
import Ember from 'ember';
|
||
|
import ModalComponent from 'ghost/components/modals/base';
|
||
|
import upload from 'ghost/assets/lib/uploader';
|
||
|
import cajaSanitizers from 'ghost/utils/caja-sanitizers';
|
||
|
|
||
|
const {computed, inject, isEmpty} = Ember;
|
||
|
|
||
|
export default ModalComponent.extend({
|
||
|
|
||
|
acceptEncoding: 'image/*',
|
||
|
model: null,
|
||
|
submitting: false,
|
||
|
|
||
|
config: inject.service(),
|
||
|
notifications: inject.service(),
|
||
|
|
||
|
imageUrl: computed('model.model', 'model.imageProperty', {
|
||
|
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);
|
||
|
}
|
||
|
}),
|
||
|
|
||
|
didInsertElement() {
|
||
|
this._super(...arguments);
|
||
|
upload.call(this.$('.js-drop-zone'), {
|
||
|
fileStorage: this.get('config.fileStorage')
|
||
|
});
|
||
|
},
|
||
|
|
||
|
keyDown() {
|
||
|
this._setErrorState(false);
|
||
|
},
|
||
|
|
||
|
_setErrorState(state) {
|
||
|
if (state) {
|
||
|
this.$('.js-upload-url').addClass('error');
|
||
|
} else {
|
||
|
this.$('.js-upload-url').removeClass('error');
|
||
|
}
|
||
|
},
|
||
|
|
||
|
_setImageProperty() {
|
||
|
let value;
|
||
|
|
||
|
if (this.$('.js-upload-url').val()) {
|
||
|
value = this.$('.js-upload-url').val();
|
||
|
|
||
|
if (!isEmpty(value) && !cajaSanitizers.url(value)) {
|
||
|
this._setErrorState(true);
|
||
|
return {message: 'Image URI is not valid'};
|
||
|
}
|
||
|
} else {
|
||
|
value = this.$('.js-upload-target').attr('src');
|
||
|
}
|
||
|
|
||
|
this.set('imageUrl', value);
|
||
|
return true;
|
||
|
},
|
||
|
|
||
|
actions: {
|
||
|
confirm() {
|
||
|
let model = this.get('model.model');
|
||
|
let notifications = this.get('notifications');
|
||
|
let result = this._setImageProperty();
|
||
|
|
||
|
if (!result.message) {
|
||
|
this.set('submitting', true);
|
||
|
|
||
|
model.save().catch((err) => {
|
||
|
notifications.showAPIError(err, {key: 'image.upload'});
|
||
|
}).finally(() => {
|
||
|
this.send('closeModal');
|
||
|
});
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
});
|