All files / app/assets/javascripts/profile gl_crop.js

0% Statements 0/92
0% Branches 0/18
0% Functions 0/24
0% Lines 0/90

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 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 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 204 205 206 207 208 209 210 211 212 213                                                                                                                                                                                                                                                                                                                                                                                                                                         
/* eslint-disable no-underscore-dangle, func-names */
 
import $ from 'jquery';
import Cropper from 'cropperjs';
import { isString } from 'lodash';
import { s__ } from '~/locale';
import { createAlert } from '~/alert';
import { loadCSSFile } from '../lib/utils/css_utils';
 
(() => {
  // Matches everything but the file name
  const FILENAMEREGEX = /^.*[\\/]/;
 
  class GitLabCrop {
    constructor(
      input,
      {
        filename,
        previewImage,
        modalCrop,
        pickImageEl,
        uploadImageBtn,
        modalCropImg,
        exportWidth = 200,
        exportHeight = 200,
        cropBoxWidth = 200,
        cropBoxHeight = 200,
        onBlobChange = () => {},
      } = {},
    ) {
      this.onUploadImageBtnClick = this.onUploadImageBtnClick.bind(this);
      this.onModalHide = this.onModalHide.bind(this);
      this.onModalShow = this.onModalShow.bind(this);
      this.onPickImageClick = this.onPickImageClick.bind(this);
      this.fileInput = $(input);
      this.fileInput
        .attr('name', `${this.fileInput.attr('name')}-trigger`)
        .attr('id', `${this.fileInput.attr('id')}-trigger`);
      this.exportWidth = exportWidth;
      this.exportHeight = exportHeight;
      this.cropBoxWidth = cropBoxWidth;
      this.cropBoxHeight = cropBoxHeight;
      this.form = this.fileInput.parents('form');
      this.filename = filename;
      this.previewImage = previewImage;
      this.modalCrop = modalCrop;
      this.pickImageEl = pickImageEl;
      this.uploadImageBtn = uploadImageBtn;
      this.modalCropImg = modalCropImg;
      this.filename = this.getElement(filename);
      this.previewImage = this.getElement(previewImage);
      this.pickImageEl = this.getElement(pickImageEl);
      this.modalCrop = isString(modalCrop) ? $(modalCrop) : modalCrop;
      this.uploadImageBtn = isString(uploadImageBtn) ? $(uploadImageBtn) : uploadImageBtn;
      this.cropActionsBtn = this.modalCrop.find('[data-method]');
      this.onBlobChange = onBlobChange;
      this.cropperInstance = null;
      this.bindEvents();
    }
 
    getElement(selector) {
      return $(selector, this.form);
    }
 
    bindEvents() {
      const _this = this;
      this.fileInput.on('change', function (e) {
        _this.onFileInputChange(e, this);
        this.value = null;
      });
      this.pickImageEl.on('click', this.onPickImageClick);
      this.modalCrop.on('shown.bs.modal', this.onModalShow);
      this.modalCrop.on('hidden.bs.modal', this.onModalHide);
      this.uploadImageBtn.on('click', this.onUploadImageBtnClick);
      this.cropActionsBtn.on('click', function () {
        const btn = this;
        return _this.onActionBtnClick(btn);
      });
      this.onBlobChange(null);
      this.croppedImageBlob = null;
      return null;
    }
 
    onPickImageClick() {
      return this.fileInput.trigger('click');
    }
 
    onModalShow() {
      const _this = this;
      this.cropperInstance = new Cropper(this.modalCropImg, {
        viewMode: 1,
        center: false,
        aspectRatio: 1,
        modal: true,
        scalable: false,
        rotatable: true,
        checkOrientation: true,
        zoomable: true,
        dragMode: 'move',
        guides: false,
        zoomOnTouch: false,
        zoomOnWheel: false,
        cropBoxMovable: false,
        cropBoxResizable: false,
        toggleDragModeOnDblclick: false,
        built() {
          const container = this.cropperInstance.getContainerData();
          const { cropBoxWidth, cropBoxHeight } = _this;
 
          return this.cropperInstance.setCropBoxData({
            width: cropBoxWidth,
            height: cropBoxHeight,
            left: (container.width - cropBoxWidth) / 2,
            top: (container.height - cropBoxHeight) / 2,
          });
        },
      });
    }
 
    onModalHide() {
      this.cropperInstance.destroy();
      const modalElement = document.querySelector('.modal-profile-crop');
      if (modalElement) modalElement.remove();
    }
 
    onUploadImageBtnClick(e) {
      e.preventDefault();
      this.setBlob();
      this.setPreview();
      this.modalCrop.modal('hide');
      return this.fileInput.val('');
    }
 
    onActionBtnClick(btn) {
      const data = $(btn).data();
      if (data.method) {
        return this.cropperInstance[data.method](data.option);
      }
      return null;
    }
 
    onFileInputChange(e, input) {
      return this.readFile(input);
    }
 
    readFile(input) {
      const reader = new FileReader();
      reader.onload = () => {
        this.modalCropImg.setAttribute('src', reader.result);
        import(/* webpackChunkName: 'bootstrapModal' */ 'bootstrap/js/dist/modal')
          .then(() => {
            this.modalCrop.modal('show');
          })
          .catch(() => {
            createAlert({
              message: s__(
                'UserProfile|Failed to set avatar. Please reload the page to try again.',
              ),
            });
          });
      };
      return reader.readAsDataURL(input.files[0]);
    }
 
    static dataURLtoBlob(dataURL) {
      let i = 0;
      let len = 0;
      const binary = atob(dataURL.split(',')[1]);
      const array = [];
 
      for (i = 0, len = binary.length; i < len; i += 1) {
        array.push(binary.charCodeAt(i));
      }
      return new Blob([new Uint8Array(array)], {
        type: 'image/png',
      });
    }
 
    setPreview() {
      const filename = this.fileInput.val().replace(FILENAMEREGEX, '');
      this.previewImage.attr('src', this.dataURL);
      this.previewImage.attr('srcset', this.dataURL);
      return this.filename.text(filename);
    }
 
    setBlob() {
      this.dataURL = this.cropperInstance
        .getCroppedCanvas({
          width: 200,
          height: 200,
        })
        .toDataURL('image/png');
 
      this.croppedImageBlob = GitLabCrop.dataURLtoBlob(this.dataURL);
      this.onBlobChange(this.croppedImageBlob);
      return this.croppedImageBlob;
    }
 
    getBlob() {
      return this.croppedImageBlob;
    }
  }
 
  const cropModal = document.querySelector('.modal-profile-crop');
  if (cropModal) loadCSSFile(cropModal.dataset.cropperCssPath);
 
  $.fn.glCrop = function (opts) {
    return this.each(function () {
      return $(this).data('glcrop', new GitLabCrop(this, opts));
    });
  };
})(window.gl || (window.gl = {}));