image_file.js 7.08 KB
Newer Older
1
/* eslint-disable func-names, space-before-function-paren, wrap-iife, no-var, no-use-before-define, prefer-arrow-callback, no-else-return, consistent-return, prefer-template, quotes, one-var, one-var-declaration-per-line, no-unused-vars, no-return-assign, comma-dangle, quote-props, no-unused-expressions, no-sequences, object-shorthand, max-len */
Felipe Artur's avatar
Felipe Artur committed
2 3
import 'vendor/jquery.waitforimages';

4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
// 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;
    this.requestImageInfo($('.two-up.view .frame.deleted img', this.file), (function(_this) {
      return function(deletedWidth, deletedHeight) {
        return _this.requestImageInfo($('.two-up.view .frame.added img', _this.file), function(width, height) {
          _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(function() {
            _this.initView('two-up');
          });
        });
      };
    })(this));
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122

    this.views = {
      'two-up': function() {
        return $('.two-up.view .wrap', this.file).each((function(_this) {
          return function(index, wrap) {
            $('img', wrap).each(function() {
              var currentWidth;
              currentWidth = $(this).width();
              if (currentWidth > availWidth / 2) {
                return $(this).width(availWidth / 2);
              }
            });
            return _this.requestImageInfo($('img', wrap), function(width, height) {
              $('.image-info .meta-width', wrap).text(width + "px");
              $('.image-info .meta-height', wrap).text(height + "px");
              return $('.image-info', wrap).removeClass('hide');
            });
          };
        })(this));
      },
      'swipe': function() {
        var maxHeight, maxWidth;
        maxWidth = 0;
        maxHeight = 0;
        return $('.swipe.view', this.file).each((function(_this) {
          return function(index, view) {
            var $swipeWrap, $swipeBar, $swipeFrame, wrapPadding, ref;
            ref = _this.prepareFrames(view), maxWidth = ref[0], maxHeight = ref[1];
            $swipeFrame = $('.swipe-frame', view);
            $swipeWrap = $('.swipe-wrap', view);
            $swipeBar = $('.swipe-bar', view);

            $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
            });

            wrapPadding = parseInt($swipeWrap.css('right').replace('px', ''), 10);

            _this.initDraggable($swipeBar, wrapPadding, function(e, left) {
              if (left > 0 && left < $swipeFrame.width() - (wrapPadding * 2)) {
                $swipeWrap.width((maxWidth + 1) - left);
                $swipeBar.css('left', left);
              }
            });
          };
        })(this));
      },
      'onion-skin': function() {
        var dragTrackWidth, maxHeight, maxWidth;
        maxWidth = 0;
        maxHeight = 0;
        dragTrackWidth = $('.drag-track', this.file).width() - $('.dragger', this.file).width();
        return $('.onion-skin.view', this.file).each((function(_this) {
          return function(index, view) {
            var $frame, $track, $dragger, $frameAdded, framePadding, ref, dragging = false;
            ref = _this.prepareFrames(view), maxWidth = ref[0], maxHeight = ref[1];
            $frame = $('.onion-skin-frame', view);
            $frameAdded = $('.frame.added', view);
            $track = $('.drag-track', view);
            $dragger = $('.dragger', $track);

            $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);
            framePadding = parseInt($frameAdded.css('right').replace('px', ''), 10);

            _this.initDraggable($dragger, framePadding, function(e, left) {
              var opacity = left / dragTrackWidth;

              if (opacity >= 0 && opacity <= 1) {
                $dragger.css('left', left);
                $frameAdded.css('opacity', opacity);
              }
            });
          };
        })(this));
      }
    };
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
  }

  initViewModes() {
    const viewMode = viewModes[0];
    $('.view-modes', this.file).removeClass('hide');
    $('.view-modes-menu', this.file).on('click', 'li', (function(_this) {
      return function(event) {
        if (!$(event.currentTarget).hasClass('active')) {
          return _this.activateViewMode(event.currentTarget.className);
        }
      };
    })(this));
    return this.activateViewMode(viewMode);
  }

  activateViewMode(viewMode) {
    $('.view-modes-menu li', this.file).removeClass('active').filter("." + viewMode).addClass('active');
    return $(".view:visible:not(." + viewMode + ")", this.file).fadeOut(200, (function(_this) {
      return function() {
        $(".view." + viewMode, _this.file).fadeIn(200);
        return _this.initView(viewMode);
      };
    })(this));
  }

  initView(viewMode) {
    return this.views[viewMode].call(this);
  }
  // eslint-disable-next-line class-methods-use-this
  initDraggable($el, padding, callback) {
    var dragging = false;
    var $body = $('body');
    var $offsetEl = $el.parent();

    $el.off('mousedown').on('mousedown', function() {
      dragging = true;
      $body.css('user-select', 'none');
    });

    $body.off('mouseup').off('mousemove').on('mouseup', function() {
      dragging = false;
      $body.css('user-select', '');
    })
    .on('mousemove', function(e) {
      var left;
      if (!dragging) return;

      left = e.pageX - ($offsetEl.offset().left + padding);

      callback(e, left);
    });
  }

  prepareFrames(view) {
    var maxHeight, maxWidth;
    maxWidth = 0;
    maxHeight = 0;
    $('.frame', view).each((function(_this) {
      return function(index, frame) {
        var height, width;
        width = $(frame).width();
        height = $(frame).height();
        maxWidth = width > maxWidth ? width : maxWidth;
        return maxHeight = height > maxHeight ? height : maxHeight;
      };
    })(this)).css({
      width: maxWidth,
      height: maxHeight
    });
    return [maxWidth, maxHeight];
  }

  requestImageInfo(img, callback) {
    const domImg = img.get(0);
    if (domImg) {
      if (domImg.complete) {
        return callback.call(this, domImg.naturalWidth, domImg.naturalHeight);
      } else {
        return img.on('load', (function(_this) {
          return function() {
            return callback.call(_this, domImg.naturalWidth, domImg.naturalHeight);
Fatih Acet's avatar
Fatih Acet committed
204 205 206
          };
        })(this));
      }
207 208 209
    }
  }
}