All files / app/assets/javascripts/image_diff replaced_image_diff.js

96.87% Statements 31/32
100% Branches 5/5
88.88% Functions 8/9
100% Lines 31/31

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 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            7x           7x 7x           7x 7x 7x       1x   1x 1x 3x         4x   4x 4x 4x   4x 4x 4x       4x       12x       5x 1x     4x   4x     4x 4x     4x         4x         3x     3x 2x           2x        
import imageDiffHelper from './helpers/index';
import ImageDiff from './image_diff';
import { viewTypes, isValidViewType } from './view_types';
 
export default class ReplacedImageDiff extends ImageDiff {
  init(defaultViewType = viewTypes.TWO_UP) {
    this.imageFrameEls = {
      [viewTypes.TWO_UP]: this.el.querySelector('.two-up .js-image-frame'),
      [viewTypes.SWIPE]: this.el.querySelector('.swipe .js-image-frame'),
      [viewTypes.ONION_SKIN]: this.el.querySelector('.onion-skin .js-image-frame'),
    };
 
    const viewModesEl = this.el.querySelector('.view-modes-menu');
    this.viewModesEls = {
      [viewTypes.TWO_UP]: viewModesEl.querySelector('.two-up'),
      [viewTypes.SWIPE]: viewModesEl.querySelector('.swipe'),
      [viewTypes.ONION_SKIN]: viewModesEl.querySelector('.onion-skin'),
    };
 
    this.currentView = defaultViewType;
    this.generateImageEls();
    this.bindEvents();
  }
 
  generateImageEls() {
    this.imageEls = {};
 
    const viewTypeNames = Object.getOwnPropertyNames(viewTypes);
    viewTypeNames.forEach((viewType) => {
      this.imageEls[viewType] = this.imageFrameEls[viewType].querySelector('img');
    });
  }
 
  bindEvents() {
    super.bindEvents();
 
    this.changeToViewTwoUp = this.changeView.bind(this, viewTypes.TWO_UP);
    this.changeToViewSwipe = this.changeView.bind(this, viewTypes.SWIPE);
    this.changeToViewOnionSkin = this.changeView.bind(this, viewTypes.ONION_SKIN);
 
    this.viewModesEls[viewTypes.TWO_UP].addEventListener('click', this.changeToViewTwoUp);
    this.viewModesEls[viewTypes.SWIPE].addEventListener('click', this.changeToViewSwipe);
    this.viewModesEls[viewTypes.ONION_SKIN].addEventListener('click', this.changeToViewOnionSkin);
  }
 
  get imageEl() {
    return this.imageEls[this.currentView];
  }
 
  get imageFrameEl() {
    return this.imageFrameEls[this.currentView];
  }
 
  changeView(newView) {
    if (!isValidViewType(newView)) {
      return;
    }
 
    const indicator = imageDiffHelper.removeCommentIndicator(this.imageFrameEl);
 
    this.currentView = newView;
 
    // Clear existing badges on new view
    const existingBadges = this.imageFrameEl.querySelectorAll('.design-note-pin');
    [...existingBadges].map((badge) => badge.remove());
 
    // Remove existing references to old view image badges
    this.imageBadges = [];
 
    // Image_file.js has a fade animation of 200ms for loading the view
    // Need to wait an additional 250ms for the images to be displayed
    // on window in order to re-normalize their dimensions
    setTimeout(this.renderNewView.bind(this, indicator), 250);
  }
 
  renderNewView(indicator) {
    // Generate badge coordinates on new view
    this.renderBadges();
 
    // Re-render indicator in new view
    if (indicator.removed) {
      const normalizedIndicator = imageDiffHelper.resizeCoordinatesToImageElement(this.imageEl, {
        x: indicator.x,
        y: indicator.y,
        width: indicator.image.width,
        height: indicator.image.height,
      });
      imageDiffHelper.showCommentIndicator(this.imageFrameEl, normalizedIndicator);
    }
  }
}