All files / app/assets/javascripts dropzone_input.js

75% Statements 114/152
71.79% Branches 28/39
59.37% Functions 19/32
74.14% Lines 109/147

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 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295                        102x               2x 1x     1x       469x 469x 469x 469x 469x 469x 469x 469x 469x 469x 469x 469x               469x     469x 469x 469x 469x 469x   469x 343x 343x     126x   126x                                                                         2x   2x 2x 2x     4x 4x           2x 2x 2x               2x 2x   2x 2x       126x       126x                   126x                                               126x 4x 4x 4x 4x     4x 1x 1x 1x 3x 2x 2x 2x 2x 2x   2x 2x 2x 2x   2x             126x 3x 3x     126x 3x 3x     3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x     126x       126x   126x   126x         126x 2x 2x 2x 2x 2x 2x 2x       2x     126x 2x 2x   2x   2x     2x   2x 2x               126x 4x     4x   4x                 126x   126x 126x       126x    
import Dropzone from 'dropzone';
import $ from 'jquery';
import { escape } from 'lodash';
import './behaviors/preview_markdown';
import { spriteIcon } from '~/lib/utils/common_utils';
import { getFilename } from '~/lib/utils/file_upload';
import { truncate } from '~/lib/utils/text_utility';
import { n__, __ } from '~/locale';
import PasteMarkdownTable from './behaviors/markdown/paste_markdown_table';
import axios from './lib/utils/axios_utils';
import csrf from './lib/utils/csrf';
 
Dropzone.autoDiscover = false;
 
/**
 * Return the error message string from the given response.
 *
 * @param {String|Object} res
 */
function getErrorMessage(res) {
  if (!res || typeof res === 'string') {
    return res;
  }
 
  return res.message;
}
 
export default function dropzoneInput(form, config = { parallelUploads: 2 }) {
  const divHover = '<div class="div-dropzone-hover"></div>';
  const iconPaperclip = spriteIcon('paperclip', 'div-dropzone-icon s24');
  const $attachingFileMessage = form.find('.attaching-file-message');
  const $cancelButton = form.find('.button-cancel-uploading-files');
  const $retryLink = form.find('.retry-uploading-link');
  const $uploadProgress = form.find('.uploading-progress');
  const $uploadingErrorContainer = form.find('.uploading-error-container');
  const $uploadingErrorMessage = form.find('.uploading-error-message');
  const $uploadingProgressContainer = form.find('.uploading-progress-container');
  const uploadsPath = form.data('uploads-path') || window.uploads_path || null;
  const maxFileSize = gon.max_file_size || 10;
  const formTextarea = form.find('.js-gfm-input');
  let handlePaste;
  let pasteText;
  let addFileToForm;
  let updateAttachingMessage;
  let uploadFile;
  let hasPlainText;
 
  formTextarea.wrap('<div class="div-dropzone"></div>');
 
  // Add dropzone area to the form.
  const $mdArea = formTextarea.closest('.md-area');
  const $formDropzone = form.find('.div-dropzone');
  $formDropzone.parent().addClass('div-dropzone-wrapper');
  $formDropzone.append(divHover);
  $formDropzone.find('.div-dropzone-hover').append(iconPaperclip);
 
  if (!uploadsPath) {
    $formDropzone.addClass('js-invalid-dropzone');
    return null;
  }
 
  formTextarea.on('paste', (event) => handlePaste(event));
 
  const dropzone = $formDropzone.dropzone({
    url: uploadsPath,
    dictDefaultMessage: '',
    clickable: form.get(0).querySelector('[data-button-type="attach-file"]') ?? true,
    paramName: 'file',
    maxFilesize: maxFileSize,
    uploadMultiple: false,
    headers: csrf.headers,
    previewContainer: false,
    ...config,
    dragover: () => {
      $mdArea.addClass('is-dropzone-hover');
      form.find('.div-dropzone-hover').css('opacity', 0.7);
    },
    dragleave: () => {
      $mdArea.removeClass('is-dropzone-hover');
      form.find('.div-dropzone-hover').css('opacity', 0);
    },
    drop: () => {
      $mdArea.removeClass('is-dropzone-hover');
      form.find('.div-dropzone-hover').css('opacity', 0);
      formTextarea.focus();
    },
    success(header, response) {
      const processingFileCount = this.getQueuedFiles().length + this.getUploadingFiles().length;
      const shouldPad = processingFileCount >= 1;
 
      addFileToForm(response.link.url, header.size);
      pasteText(response.link.markdown, shouldPad);
    },
    error: (file, errorMessage = __('Attaching the file failed.'), xhr) => {
      // If 'error' event is fired by dropzone, the second parameter is error message.
      // If the 'errorMessage' parameter is empty, the default error message is set.
      // If the 'error' event is fired by backend (xhr) error response, the third parameter is
      // xhr object (xhr.responseText is error message).
      // On error we hide the 'Attach' and 'Cancel' buttons
      // and show an error.
      const message = getErrorMessage(errorMessage || xhr.responseText);
 
      $uploadingErrorContainer.removeClass('hide');
      $uploadingErrorMessage.html(message);
      $cancelButton.addClass('hide');
    },
    totaluploadprogress(totalUploadProgress) {
      updateAttachingMessage(this.files, $attachingFileMessage);
      $uploadProgress.text(`${Math.round(totalUploadProgress)}%`);
    },
    sending: () => {
      // DOM elements already exist.
      // Instead of dynamically generating them,
      // we just either hide or show them.
      $uploadingErrorContainer.addClass('hide');
      $uploadingProgressContainer.removeClass('hide');
      $cancelButton.removeClass('hide');
    },
    removedfile: () => {
      $cancelButton.addClass('hide');
      $uploadingProgressContainer.addClass('hide');
      $uploadingErrorContainer.addClass('hide');
    },
    queuecomplete: () => {
      $('.dz-preview').remove();
      $('.markdown-area').trigger('input');
 
      $uploadingProgressContainer.addClass('hide');
      $cancelButton.addClass('hide');
    },
  });
 
  const child = $(dropzone[0]).children('textarea');
 
  // removeAllFiles(true) stops uploading files (if any)
  // and remove them from dropzone files queue.
  $cancelButton.on('click', (e) => {
    e.preventDefault();
    e.stopPropagation();
    Dropzone.forElement($formDropzone.get(0)).removeAllFiles(true);
  });
 
  // If 'error' event is fired, we store a failed files,
  // clear dropzone files queue, change status of failed files to undefined,
  // and add that files to the dropzone files queue again.
  // addFile() adds file to dropzone files queue and upload it.
  $retryLink.on('click', (e) => {
    const dropzoneInstance = Dropzone.forElement(
      e.target.closest('.js-main-target-form').querySelector('.div-dropzone'),
    );
    const failedFiles = dropzoneInstance.files;
 
    e.preventDefault();
 
    // 'true' parameter of removeAllFiles() cancels
    // uploading of files that are being uploaded at the moment.
    dropzoneInstance.removeAllFiles(true);
 
    failedFiles.map((failedFile) => {
      const file = failedFile;
 
      if (file.status === Dropzone.ERROR) {
        file.status = undefined;
        file.accepted = undefined;
      }
 
      return dropzoneInstance.addFile(file);
    });
  });
 
  handlePaste = (event) => {
    const pasteEvent = event.originalEvent;
    const { clipboardData } = pasteEvent;
    Eif (clipboardData && clipboardData.items) {
      const converter = new PasteMarkdownTable(clipboardData);
      // Apple Numbers copies a table as an image, HTML, and text, so
      // we need to check for the presence of a table first.
      if (converter.isTable()) {
        event.preventDefault();
        const text = converter.convertToTableMarkdown();
        pasteText(text);
      } else if (!hasPlainText(pasteEvent)) {
        const fileList = [...clipboardData.files];
        fileList.forEach((file) => {
          Eif (file.type.indexOf('image') !== -1) {
            event.preventDefault();
            const MAX_FILE_NAME_LENGTH = 246;
 
            const filename = getFilename(file) || 'image.png';
            const truncateFilename = truncate(filename, MAX_FILE_NAME_LENGTH);
            const text = `{{${truncateFilename}}}`;
            pasteText(text);
 
            uploadFile(file, truncateFilename);
          }
        });
      }
    }
  };
 
  hasPlainText = (data) => {
    const clipboardDataList = [...data.clipboardData.items];
    return clipboardDataList.some((item) => item.type === 'text/plain');
  };
 
  pasteText = (text, shouldPad) => {
    let formattedText = text;
    Iif (shouldPad) {
      formattedText += '\n\n';
    }
    const textarea = child.get(0);
    const caretStart = textarea.selectionStart;
    const caretEnd = textarea.selectionEnd;
    const textEnd = $(child).val().length;
    const beforeSelection = $(child).val().substring(0, caretStart);
    const afterSelection = $(child).val().substring(caretEnd, textEnd);
    $(child).val(beforeSelection + formattedText + afterSelection);
    textarea.setSelectionRange(caretStart + formattedText.length, caretEnd + formattedText.length);
    textarea.style.height = `${textarea.scrollHeight}px`;
    formTextarea.get(0).dispatchEvent(new Event('input'));
    return formTextarea.trigger('input');
  };
 
  addFileToForm = (path) => {
    $(form).append(`<input type="hidden" name="files[]" value="${escape(path)}">`);
  };
 
  const showSpinner = () => $uploadingProgressContainer.removeClass('hide');
 
  const closeSpinner = () => $uploadingProgressContainer.addClass('hide');
 
  const showError = (message) => {
    $uploadingErrorContainer.removeClass('hide');
    $uploadingErrorMessage.html(message);
  };
 
  const insertToTextArea = (filename, url) => {
    const $child = $(child);
    const textarea = $child.get(0);
    const caretStart = textarea.selectionStart;
    const caretEnd = textarea.selectionEnd;
    const formattedText = `{{${filename}}}`;
    $child.val((index, val) => val.replace(formattedText, url));
    textarea.setSelectionRange(
      caretStart - formattedText.length + url.length,
      caretEnd - formattedText.length + url.length,
    );
    $child.trigger('change');
  };
 
  uploadFile = (item, filename) => {
    const formData = new FormData();
    formData.append('file', item, filename);
 
    showSpinner();
 
    axios
      .post(uploadsPath, formData)
      .then(({ data }) => {
        const md = data.link.markdown;
 
        insertToTextArea(filename, md);
        closeSpinner();
      })
      .catch((e) => {
        showError(e.response.data.message);
        closeSpinner();
      });
  };
 
  updateAttachingMessage = (files, messageContainer) => {
    const filesCount = files.filter(
      (file) => file.status === 'uploading' || file.status === 'queued',
    ).length;
    const attachingMessage = n__('Attaching a file', 'Attaching %d files', filesCount);
 
    messageContainer.text(`${attachingMessage} -`);
  };
 
  function handleAttachFile(e) {
    e.preventDefault();
    $(this).closest('.gfm-form').find('.div-dropzone').click();
    formTextarea.focus();
  }
 
  form.find('.markdown-selector').click(handleAttachFile);
 
  const $attachFileButton = form.find('.js-attach-file-button');
  Iif ($attachFileButton.length) {
    $attachFileButton.get(0).addEventListener('click', handleAttachFile);
  }
 
  return $formDropzone.get(0) ? Dropzone.forElement($formDropzone.get(0)) : null;
}