BigW Consortium Gitlab

dropzone_input.js 8.51 KB
Newer Older
1
/* eslint-disable func-names, space-before-function-paren, wrap-iife, max-len, one-var, no-var, one-var-declaration-per-line, no-unused-vars, camelcase, no-undef, quotes, no-useless-concat, prefer-template, quote-props, comma-dangle, object-shorthand, consistent-return, no-plusplus, prefer-arrow-callback, padded-blocks, max-len */
Fatih Acet committed
2

3
/*= require preview_markdown */
Fatih Acet committed
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

(function() {
  this.DropzoneInput = (function() {
    function DropzoneInput(form) {
      var $mdArea, alertAttr, alertClass, appendToTextArea, btnAlert, child, closeAlertMessage, closeSpinner, divAlert, divHover, divSpinner, dropzone, form_dropzone, form_textarea, getFilename, handlePaste, iconPaperclip, iconSpinner, insertToTextArea, isImage, max_file_size, pasteText, project_uploads_path, showError, showSpinner, uploadFile, uploadProgress;
      Dropzone.autoDiscover = false;
      alertClass = "alert alert-danger alert-dismissable div-dropzone-alert";
      alertAttr = "class=\"close\" data-dismiss=\"alert\"" + "aria-hidden=\"true\"";
      divHover = "<div class=\"div-dropzone-hover\"></div>";
      divSpinner = "<div class=\"div-dropzone-spinner\"></div>";
      divAlert = "<div class=\"" + alertClass + "\"></div>";
      iconPaperclip = "<i class=\"fa fa-paperclip div-dropzone-icon\"></i>";
      iconSpinner = "<i class=\"fa fa-spinner fa-spin div-dropzone-icon\"></i>";
      uploadProgress = $("<div class=\"div-dropzone-progress\"></div>");
      btnAlert = "<button type=\"button\"" + alertAttr + ">&times;</button>";
      project_uploads_path = window.project_uploads_path || null;
      max_file_size = gon.max_file_size || 10;
      form_textarea = $(form).find(".js-gfm-input");
      form_textarea.wrap("<div class=\"div-dropzone\"></div>");
      form_textarea.on('paste', (function(_this) {
        return function(event) {
          return handlePaste(event);
        };
      })(this));
      $mdArea = $(form_textarea).closest('.md-area');
      $(form).setupMarkdownPreview();
      form_dropzone = $(form).find('.div-dropzone');
      form_dropzone.parent().addClass("div-dropzone-wrapper");
      form_dropzone.append(divHover);
      form_dropzone.find(".div-dropzone-hover").append(iconPaperclip);
      form_dropzone.append(divSpinner);
      form_dropzone.find(".div-dropzone-spinner").append(iconSpinner);
      form_dropzone.find(".div-dropzone-spinner").append(uploadProgress);
      form_dropzone.find(".div-dropzone-spinner").css({
        "opacity": 0,
        "display": "none"
      });
      dropzone = form_dropzone.dropzone({
        url: project_uploads_path,
        dictDefaultMessage: "",
        clickable: true,
        paramName: "file",
        maxFilesize: max_file_size,
        uploadMultiple: false,
        headers: {
          "X-CSRF-Token": $("meta[name=\"csrf-token\"]").attr("content")
        },
        previewContainer: false,
        processing: function() {
          return $(".div-dropzone-alert").alert("close");
        },
        dragover: function() {
          $mdArea.addClass('is-dropzone-hover');
          form.find(".div-dropzone-hover").css("opacity", 0.7);
        },
        dragleave: function() {
          $mdArea.removeClass('is-dropzone-hover');
          form.find(".div-dropzone-hover").css("opacity", 0);
        },
        drop: function() {
          $mdArea.removeClass('is-dropzone-hover');
          form.find(".div-dropzone-hover").css("opacity", 0);
          form_textarea.focus();
        },
        success: function(header, response) {
          pasteText(response.link.markdown);
        },
        error: function(temp) {
          var checkIfMsgExists, errorAlert;
          errorAlert = $(form).find('.error-alert');
          checkIfMsgExists = errorAlert.children().length;
          if (checkIfMsgExists === 0) {
            errorAlert.append(divAlert);
            $(".div-dropzone-alert").append(btnAlert + "Attaching the file failed.");
          }
        },
        totaluploadprogress: function(totalUploadProgress) {
          uploadProgress.text(Math.round(totalUploadProgress) + "%");
        },
        sending: function() {
          form_dropzone.find(".div-dropzone-spinner").css({
            "opacity": 0.7,
            "display": "inherit"
          });
        },
        queuecomplete: function() {
          uploadProgress.text("");
          $(".dz-preview").remove();
          $(".markdown-area").trigger("input");
          $(".div-dropzone-spinner").css({
            "opacity": 0,
            "display": "none"
          });
        }
      });
      child = $(dropzone[0]).children("textarea");
      handlePaste = function(event) {
        var filename, image, pasteEvent, text;
        pasteEvent = event.originalEvent;
        if (pasteEvent.clipboardData && pasteEvent.clipboardData.items) {
          image = isImage(pasteEvent);
          if (image) {
            event.preventDefault();
            filename = getFilename(pasteEvent) || "image.png";
            text = "{{" + filename + "}}";
            pasteText(text);
            return uploadFile(image.getAsFile(), filename);
          }
        }
      };
      isImage = function(data) {
        var i, item;
        i = 0;
        while (i < data.clipboardData.items.length) {
          item = data.clipboardData.items[i];
          if (item.type.indexOf("image") !== -1) {
            return item;
          }
          i++;
        }
        return false;
      };
      pasteText = function(text) {
        var afterSelection, beforeSelection, caretEnd, caretStart, textEnd;
        caretStart = $(child)[0].selectionStart;
        caretEnd = $(child)[0].selectionEnd;
        textEnd = $(child).val().length;
        beforeSelection = $(child).val().substring(0, caretStart);
        afterSelection = $(child).val().substring(caretEnd, textEnd);
        $(child).val(beforeSelection + text + afterSelection);
        child.get(0).setSelectionRange(caretStart + text.length, caretEnd + text.length);
        return form_textarea.trigger("input");
      };
      getFilename = function(e) {
        var value;
        if (window.clipboardData && window.clipboardData.getData) {
          value = window.clipboardData.getData("Text");
        } else if (e.clipboardData && e.clipboardData.getData) {
          value = e.clipboardData.getData("text/plain");
        }
        value = value.split("\r");
        return value.first();
      };
      uploadFile = function(item, filename) {
        var formData;
        formData = new FormData();
        formData.append("file", item, filename);
        return $.ajax({
          url: project_uploads_path,
          type: "POST",
          data: formData,
          dataType: "json",
          processData: false,
          contentType: false,
          headers: {
            "X-CSRF-Token": $("meta[name=\"csrf-token\"]").attr("content")
          },
          beforeSend: function() {
            showSpinner();
            return closeAlertMessage();
          },
          success: function(e, textStatus, response) {
            return insertToTextArea(filename, response.responseJSON.link.markdown);
          },
          error: function(response) {
            return showError(response.responseJSON.message);
          },
          complete: function() {
            return closeSpinner();
          }
        });
      };
      insertToTextArea = function(filename, url) {
        return $(child).val(function(index, val) {
          return val.replace("{{" + filename + "}}", url + "\n");
        });
      };
      appendToTextArea = function(url) {
        return $(child).val(function(index, val) {
          return val + url + "\n";
        });
      };
      showSpinner = function(e) {
        return form.find(".div-dropzone-spinner").css({
          "opacity": 0.7,
          "display": "inherit"
        });
      };
      closeSpinner = function() {
        return form.find(".div-dropzone-spinner").css({
          "opacity": 0,
          "display": "none"
        });
      };
      showError = function(message) {
        var checkIfMsgExists, errorAlert;
        errorAlert = $(form).find('.error-alert');
        checkIfMsgExists = errorAlert.children().length;
        if (checkIfMsgExists === 0) {
          errorAlert.append(divAlert);
          return $(".div-dropzone-alert").append(btnAlert + message);
        }
      };
      closeAlertMessage = function() {
        return form.find(".div-dropzone-alert").alert("close");
      };
      form.find(".markdown-selector").click(function(e) {
        e.preventDefault();
        $(this).closest('.gfm-form').find('.div-dropzone').click();
      });
    }

    return DropzoneInput;

  })();

}).call(this);