2020-04-29 20:09:37 -04:00
|
|
|
/* eslint-disable func-names, consistent-return, one-var, no-return-assign */
|
2017-10-07 00:25:17 -04:00
|
|
|
|
2018-03-09 15:18:59 -05:00
|
|
|
import $ from 'jquery';
|
2020-05-29 11:08:14 -04:00
|
|
|
import 'jquery.waitforimages';
|
2018-03-09 15:18:59 -05:00
|
|
|
|
2017-12-06 04:29:59 -05:00
|
|
|
// Width where images must fits in, for 2-up this gets divided by 2
|
|
|
|
const availWidth = 900;
|
|
|
|
const viewModes = ['two-up', 'swipe'];
|
|
|
|
|
|
|
|
export default class ImageFile {
|
|
|
|
constructor(file) {
|
|
|
|
this.file = file;
|
2019-11-11 13:06:27 -05:00
|
|
|
this.requestImageInfo($('.two-up.view .frame.deleted img', this.file), () =>
|
|
|
|
this.requestImageInfo($('.two-up.view .frame.added img', this.file), () => {
|
|
|
|
this.initViewModes();
|
|
|
|
|
|
|
|
// Load two-up view after images are loaded
|
|
|
|
// so that we can display the correct width and height information
|
|
|
|
const $images = $('.two-up.view img', this.file);
|
|
|
|
|
|
|
|
$images.waitForImages(() => {
|
|
|
|
this.initView('two-up');
|
|
|
|
});
|
|
|
|
}),
|
2018-10-24 15:17:03 -04:00
|
|
|
);
|
2017-12-06 04:29:59 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
initViewModes() {
|
|
|
|
const viewMode = viewModes[0];
|
|
|
|
$('.view-modes', this.file).removeClass('hide');
|
2019-11-11 13:06:27 -05:00
|
|
|
$('.view-modes-menu', this.file).on('click', 'li', event => {
|
|
|
|
if (!$(event.currentTarget).hasClass('active')) {
|
|
|
|
return this.activateViewMode(event.currentTarget.className);
|
|
|
|
}
|
|
|
|
});
|
2017-12-06 04:29:59 -05:00
|
|
|
return this.activateViewMode(viewMode);
|
|
|
|
}
|
|
|
|
|
|
|
|
activateViewMode(viewMode) {
|
2018-10-24 15:17:03 -04:00
|
|
|
$('.view-modes-menu li', this.file)
|
|
|
|
.removeClass('active')
|
2019-09-24 08:06:20 -04:00
|
|
|
.filter(`.${viewMode}`)
|
2018-10-24 15:17:03 -04:00
|
|
|
.addClass('active');
|
2019-12-18 07:07:48 -05:00
|
|
|
|
|
|
|
// eslint-disable-next-line no-jquery/no-fade
|
2019-11-11 13:06:27 -05:00
|
|
|
return $(`.view:visible:not(.${viewMode})`, this.file).fadeOut(200, () => {
|
2019-12-18 07:07:48 -05:00
|
|
|
// eslint-disable-next-line no-jquery/no-fade
|
2019-11-11 13:06:27 -05:00
|
|
|
$(`.view.${viewMode}`, this.file).fadeIn(200);
|
|
|
|
return this.initView(viewMode);
|
|
|
|
});
|
2017-12-06 04:29:59 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
initView(viewMode) {
|
|
|
|
return this.views[viewMode].call(this);
|
|
|
|
}
|
|
|
|
// eslint-disable-next-line class-methods-use-this
|
|
|
|
initDraggable($el, padding, callback) {
|
2019-12-13 19:08:27 -05:00
|
|
|
let dragging = false;
|
2019-04-08 03:39:41 -04:00
|
|
|
const $body = $('body');
|
|
|
|
const $offsetEl = $el.parent();
|
|
|
|
const dragStart = function() {
|
2017-12-06 04:29:59 -05:00
|
|
|
dragging = true;
|
|
|
|
$body.css('user-select', 'none');
|
2019-04-08 03:39:41 -04:00
|
|
|
};
|
|
|
|
const dragStop = function() {
|
|
|
|
dragging = false;
|
|
|
|
$body.css('user-select', '');
|
|
|
|
};
|
|
|
|
const dragMove = function(e) {
|
|
|
|
const moveX = e.pageX || e.touches[0].pageX;
|
|
|
|
const left = moveX - ($offsetEl.offset().left + padding);
|
|
|
|
if (!dragging) return;
|
|
|
|
|
|
|
|
callback(e, left);
|
|
|
|
};
|
|
|
|
|
|
|
|
$el
|
|
|
|
.off('mousedown')
|
|
|
|
.off('touchstart')
|
|
|
|
.on('mousedown', dragStart)
|
|
|
|
.on('touchstart', dragStart);
|
2017-12-06 04:29:59 -05:00
|
|
|
|
2018-10-24 15:17:03 -04:00
|
|
|
$body
|
|
|
|
.off('mouseup')
|
|
|
|
.off('mousemove')
|
2019-04-08 03:39:41 -04:00
|
|
|
.off('touchend')
|
|
|
|
.off('touchmove')
|
|
|
|
.on('mouseup', dragStop)
|
|
|
|
.on('touchend', dragStop)
|
|
|
|
.on('mousemove', dragMove)
|
|
|
|
.on('touchmove', dragMove);
|
2017-12-06 04:29:59 -05:00
|
|
|
}
|
|
|
|
|
2019-11-11 13:06:27 -05:00
|
|
|
static prepareFrames(view) {
|
2019-12-13 19:08:27 -05:00
|
|
|
let maxWidth = 0;
|
|
|
|
let maxHeight = 0;
|
2018-10-24 15:17:03 -04:00
|
|
|
$('.frame', view)
|
2019-11-11 13:06:27 -05:00
|
|
|
.each((index, frame) => {
|
2019-12-13 19:08:27 -05:00
|
|
|
const width = $(frame).width();
|
|
|
|
const height = $(frame).height();
|
2019-11-11 13:06:27 -05:00
|
|
|
maxWidth = width > maxWidth ? width : maxWidth;
|
|
|
|
return (maxHeight = height > maxHeight ? height : maxHeight);
|
|
|
|
})
|
2018-10-24 15:17:03 -04:00
|
|
|
.css({
|
|
|
|
width: maxWidth,
|
|
|
|
height: maxHeight,
|
|
|
|
});
|
2017-12-06 04:29:59 -05:00
|
|
|
return [maxWidth, maxHeight];
|
|
|
|
}
|
2018-06-16 09:20:30 -04:00
|
|
|
|
2018-01-09 14:22:05 -05:00
|
|
|
views = {
|
|
|
|
'two-up': function() {
|
2019-11-11 13:06:27 -05:00
|
|
|
return $('.two-up.view .wrap', this.file).each((index, wrap) => {
|
|
|
|
$('img', wrap).each(function() {
|
2019-12-13 19:08:27 -05:00
|
|
|
const currentWidth = $(this).width();
|
2019-11-11 13:06:27 -05:00
|
|
|
if (currentWidth > availWidth / 2) {
|
|
|
|
return $(this).width(availWidth / 2);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return this.requestImageInfo($('img', wrap), (width, height) => {
|
|
|
|
$('.image-info .meta-width', wrap).text(`${width}px`);
|
|
|
|
$('.image-info .meta-height', wrap).text(`${height}px`);
|
|
|
|
return $('.image-info', wrap).removeClass('hide');
|
|
|
|
});
|
|
|
|
});
|
2018-01-09 14:22:05 -05:00
|
|
|
},
|
2018-10-24 15:17:03 -04:00
|
|
|
swipe() {
|
2019-12-13 19:08:27 -05:00
|
|
|
let maxWidth = 0;
|
|
|
|
let maxHeight = 0;
|
2019-11-11 13:06:27 -05:00
|
|
|
return $('.swipe.view', this.file).each((index, view) => {
|
|
|
|
const ref = ImageFile.prepareFrames(view);
|
|
|
|
[maxWidth, maxHeight] = ref;
|
2019-12-13 19:08:27 -05:00
|
|
|
const $swipeFrame = $('.swipe-frame', view);
|
|
|
|
const $swipeWrap = $('.swipe-wrap', view);
|
|
|
|
const $swipeBar = $('.swipe-bar', view);
|
2019-11-11 13:06:27 -05:00
|
|
|
|
|
|
|
$swipeFrame.css({
|
|
|
|
width: maxWidth + 16,
|
|
|
|
height: maxHeight + 28,
|
|
|
|
});
|
|
|
|
$swipeWrap.css({
|
|
|
|
width: maxWidth + 1,
|
|
|
|
height: maxHeight + 2,
|
|
|
|
});
|
|
|
|
// Set swipeBar left position to match image frame
|
|
|
|
$swipeBar.css({
|
|
|
|
left: 1,
|
|
|
|
});
|
|
|
|
|
2019-12-13 19:08:27 -05:00
|
|
|
const wrapPadding = parseInt($swipeWrap.css('right').replace('px', ''), 10);
|
2019-11-11 13:06:27 -05:00
|
|
|
|
|
|
|
this.initDraggable($swipeBar, wrapPadding, (e, left) => {
|
|
|
|
if (left > 0 && left < $swipeFrame.width() - wrapPadding * 2) {
|
|
|
|
$swipeWrap.width(maxWidth + 1 - left);
|
|
|
|
$swipeBar.css('left', left);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2018-01-09 14:22:05 -05:00
|
|
|
},
|
|
|
|
'onion-skin': function() {
|
2019-12-13 19:08:27 -05:00
|
|
|
let maxHeight, maxWidth;
|
2018-01-09 14:22:05 -05:00
|
|
|
maxWidth = 0;
|
|
|
|
maxHeight = 0;
|
2019-12-13 19:08:27 -05:00
|
|
|
const dragTrackWidth = $('.drag-track', this.file).width() - $('.dragger', this.file).width();
|
2019-11-11 13:06:27 -05:00
|
|
|
return $('.onion-skin.view', this.file).each((index, view) => {
|
|
|
|
const ref = ImageFile.prepareFrames(view);
|
|
|
|
[maxWidth, maxHeight] = ref;
|
2019-12-13 19:08:27 -05:00
|
|
|
const $frame = $('.onion-skin-frame', view);
|
|
|
|
const $frameAdded = $('.frame.added', view);
|
|
|
|
const $track = $('.drag-track', view);
|
|
|
|
const $dragger = $('.dragger', $track);
|
2019-11-11 13:06:27 -05:00
|
|
|
|
|
|
|
$frame.css({
|
|
|
|
width: maxWidth + 16,
|
|
|
|
height: maxHeight + 28,
|
|
|
|
});
|
|
|
|
$('.swipe-wrap', view).css({
|
|
|
|
width: maxWidth + 1,
|
|
|
|
height: maxHeight + 2,
|
|
|
|
});
|
|
|
|
$dragger.css({
|
|
|
|
left: dragTrackWidth,
|
|
|
|
});
|
|
|
|
|
|
|
|
$frameAdded.css('opacity', 1);
|
2019-12-13 19:08:27 -05:00
|
|
|
const framePadding = parseInt($frameAdded.css('right').replace('px', ''), 10);
|
2019-11-11 13:06:27 -05:00
|
|
|
|
|
|
|
this.initDraggable($dragger, framePadding, (e, left) => {
|
2019-12-13 19:08:27 -05:00
|
|
|
const opacity = left / dragTrackWidth;
|
2019-11-11 13:06:27 -05:00
|
|
|
|
|
|
|
if (opacity >= 0 && opacity <= 1) {
|
|
|
|
$dragger.css('left', left);
|
|
|
|
$frameAdded.css('opacity', opacity);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2018-10-24 15:17:03 -04:00
|
|
|
},
|
|
|
|
};
|
2018-01-04 14:07:28 -05:00
|
|
|
|
2017-12-06 04:29:59 -05:00
|
|
|
requestImageInfo(img, callback) {
|
|
|
|
const domImg = img.get(0);
|
|
|
|
if (domImg) {
|
|
|
|
if (domImg.complete) {
|
|
|
|
return callback.call(this, domImg.naturalWidth, domImg.naturalHeight);
|
2016-07-24 16:45:11 -04:00
|
|
|
}
|
2020-04-29 20:09:37 -04:00
|
|
|
return img.on('load', () => callback.call(this, domImg.naturalWidth, domImg.naturalHeight));
|
2017-12-06 04:29:59 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|