0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-02-03 23:00:14 -05:00
ghost/core/client/app/components/gh-uploader.js

106 lines
3.2 KiB
JavaScript
Raw Normal View History

2015-02-12 21:22:32 -07:00
import Ember from 'ember';
2014-09-15 15:46:40 -07:00
import uploader from 'ghost/assets/lib/uploader';
export default Ember.Component.extend({
2014-09-15 15:46:40 -07:00
classNames: ['image-uploader', 'js-post-image-upload'],
config: Ember.inject.service(),
imageSource: Ember.computed('image', function () {
return this.get('image') || '';
}),
/**
* Sets up the uploader on render
*/
2014-09-15 15:46:40 -07:00
setup: function () {
var $this = this.$(),
self = this;
// this.set('uploaderReference', uploader.call($this, {
// editor: true,
// fileStorage: this.get('config.fileStorage')
// }));
2014-09-15 15:46:40 -07:00
$this.on('uploadsuccess', function (event, result) {
if (result && result !== '' && result !== 'http://') {
self.sendAction('uploaded', result);
}
});
$this.on('imagecleared', function () {
2014-09-15 15:46:40 -07:00
self.sendAction('canceled');
});
},
2014-09-15 15:46:40 -07:00
// removes event listeners from the uploader
2014-09-15 15:46:40 -07:00
removeListeners: function () {
var $this = this.$();
2014-09-15 15:46:40 -07:00
$this.off();
$this.find('.js-cancel').off();
},
// didInsertElement: function () {
// Ember.run.scheduleOnce('afterRender', this, this.setup());
// },
didInsertElement: function () {
this.send('initUploader');
},
willDestroyElement: function () {
this.removeListeners();
},
// NOTE: because the uploader is sometimes in the same place in the DOM
// between transitions Glimmer will re-use the existing elements including
// those that arealready decorated by jQuery. The following works around
// situations where the image is changed without a full teardown/rebuild
didReceiveAttrs: function (attrs) {
var oldValue = attrs.oldAttrs && Ember.get(attrs.oldAttrs, 'image.value'),
newValue = attrs.newAttrs && Ember.get(attrs.newAttrs, 'image.value'),
self = this;
// always reset when we receive a blank image
// - handles navigating to populated image from blank image
if (Ember.isEmpty(newValue) && !Ember.isEmpty(oldValue)) {
self.$()[0].uploaderUi.reset();
}
// re-init if we receive a new image but the uploader is blank
// - handles back button navigating from blank image to populated image
if (!Ember.isEmpty(newValue) && this.$()) {
if (this.$('.js-upload-target').attr('src') === '') {
this.$()[0].uploaderUi.reset();
this.$()[0].uploaderUi.initWithImage();
}
}
},
actions: {
initUploader: function () {
var ref,
el,
self = this;
el = this.$();
ref = uploader.call(el, {
editor: true,
fileStorage: this.get('config.fileStorage')
});
el.on('uploadsuccess', function (event, result) {
if (result && result !== '' && result !== 'http://') {
self.sendAction('uploaded', result);
}
});
el.on('imagecleared', function () {
self.sendAction('canceled');
});
this.sendAction('initUploader', ref);
}
}
2014-09-15 15:46:40 -07:00
});