2015-03-13 12:39:28 +03:00
|
|
|
import Ember from 'ember';
|
|
|
|
|
2016-01-19 16:03:27 +03:00
|
|
|
const {
|
|
|
|
$,
|
|
|
|
Component,
|
2016-02-26 16:25:47 +03:00
|
|
|
run,
|
|
|
|
uuid
|
2016-01-19 16:03:27 +03:00
|
|
|
} = Ember;
|
2015-10-28 14:36:45 +03:00
|
|
|
|
|
|
|
export default Component.extend({
|
2015-11-20 19:40:41 +03:00
|
|
|
_scrollWrapper: null,
|
|
|
|
|
2016-02-26 16:25:47 +03:00
|
|
|
init() {
|
|
|
|
this._super(...arguments);
|
|
|
|
this.set('imageUploadComponents', Ember.A([]));
|
|
|
|
},
|
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
didInsertElement() {
|
2015-11-15 14:06:49 +03:00
|
|
|
this._super(...arguments);
|
2015-11-20 19:40:41 +03:00
|
|
|
this._scrollWrapper = this.$().closest('.entry-preview-content');
|
2015-10-06 15:14:00 +03:00
|
|
|
this.adjustScrollPosition(this.get('scrollPosition'));
|
2016-02-26 16:25:47 +03:00
|
|
|
run.scheduleOnce('afterRender', this, this.registerImageUploadComponents);
|
2015-03-13 12:39:28 +03:00
|
|
|
},
|
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
didReceiveAttrs(attrs) {
|
2015-11-15 14:06:49 +03:00
|
|
|
this._super(...arguments);
|
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
if (!attrs.oldAttrs) {
|
|
|
|
return;
|
|
|
|
}
|
2015-10-06 15:14:00 +03:00
|
|
|
|
|
|
|
if (attrs.newAttrs.scrollPosition && attrs.newAttrs.scrollPosition.value !== attrs.oldAttrs.scrollPosition.value) {
|
|
|
|
this.adjustScrollPosition(attrs.newAttrs.scrollPosition.value);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (attrs.newAttrs.markdown.value !== attrs.oldAttrs.markdown.value) {
|
2016-02-26 16:25:47 +03:00
|
|
|
// we need to clear the rendered components as we are unable to
|
|
|
|
// retain a reliable reference for the component's position in the
|
|
|
|
// document
|
|
|
|
// TODO: it may be possible to extract the dropzones and use the
|
|
|
|
// image src as a key, re-connecting any that match and
|
|
|
|
// dropping/re-rendering any unknown/no-source instances
|
|
|
|
this.set('imageUploadComponents', Ember.A([]));
|
|
|
|
run.scheduleOnce('afterRender', this, this.registerImageUploadComponents);
|
2015-10-06 15:14:00 +03:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
adjustScrollPosition(scrollPosition) {
|
|
|
|
let scrollWrapper = this._scrollWrapper;
|
2015-03-13 12:39:28 +03:00
|
|
|
|
2015-06-13 17:34:09 +03:00
|
|
|
if (scrollWrapper) {
|
|
|
|
scrollWrapper.scrollTop(scrollPosition);
|
|
|
|
}
|
2015-10-06 15:14:00 +03:00
|
|
|
},
|
2015-03-13 12:39:28 +03:00
|
|
|
|
2016-02-26 16:25:47 +03:00
|
|
|
registerImageUploadComponents() {
|
|
|
|
let dropzones = $('.js-drop-zone');
|
|
|
|
|
|
|
|
dropzones.each((i, el) => {
|
|
|
|
let id = uuid();
|
|
|
|
let destinationElementId = `image-uploader-${id}`;
|
|
|
|
let src = $(el).find('.js-upload-target').attr('src');
|
2015-03-13 12:39:28 +03:00
|
|
|
|
2016-02-26 16:25:47 +03:00
|
|
|
let imageUpload = Ember.Object.create({
|
|
|
|
destinationElementId,
|
|
|
|
id,
|
|
|
|
src,
|
|
|
|
index: i
|
2015-10-06 15:14:00 +03:00
|
|
|
});
|
2015-03-13 12:39:28 +03:00
|
|
|
|
2016-02-26 16:25:47 +03:00
|
|
|
el.id = destinationElementId;
|
|
|
|
$(el).empty();
|
|
|
|
$(el).removeClass('image-uploader');
|
|
|
|
|
|
|
|
run.schedule('afterRender', () => {
|
|
|
|
this.get('imageUploadComponents').pushObject(imageUpload);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
actions: {
|
|
|
|
updateImageSrc(index, url) {
|
|
|
|
this.attrs.updateImageSrc(index, url);
|
|
|
|
},
|
2015-03-13 12:39:28 +03:00
|
|
|
|
2016-02-26 16:25:47 +03:00
|
|
|
updateHeight() {
|
|
|
|
this.attrs.updateHeight(this.$().height());
|
2015-10-06 15:14:00 +03:00
|
|
|
}
|
|
|
|
}
|
2015-03-13 12:39:28 +03:00
|
|
|
});
|