summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/blob/blob_file_dropzone.js.coffee
blob: 9df932817f6aa785360c5b0a973f3d3895369be7 (plain)
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
class @BlobFileDropzone
  constructor: (form, method) ->
    form_dropzone = form.find('.dropzone')
    Dropzone.autoDiscover = false
    dropzone = form_dropzone.dropzone(
      autoDiscover: false
      autoProcessQueue: false
      url: form.attr('action')
      # Rails uses a hidden input field for PUT
      # http://stackoverflow.com/questions/21056482/how-to-set-method-put-in-form-tag-in-rails
      method: method
      clickable: true
      uploadMultiple: false
      paramName: "file"
      maxFilesize: gon.max_file_size or 10
      parallelUploads: 1
      maxFiles: 1
      addRemoveLinks: true
      previewsContainer: '.dropzone-previews'
      headers:
        "X-CSRF-Token": $("meta[name=\"csrf-token\"]").attr("content")

      init: ->
        this.on 'addedfile', (file) ->
          $('.dropzone-alerts').html('').hide()

          return

        this.on 'success', (header, response) ->
          window.location.href = response.filePath
          return

        this.on 'maxfilesexceeded', (file) ->
          @removeFile file
          return

        this.on 'sending', (file, xhr, formData) ->
          formData.append('target_branch', form.find('.js-target-branch').val())
          formData.append('create_merge_request', form.find('.js-create-merge-request').val())
          formData.append('commit_message', form.find('.js-commit-message').val())
          return

      # Override behavior of adding error underneath preview
      error: (file, errorMessage) ->
        stripped = $("<div/>").html(errorMessage).text();
        $('.dropzone-alerts').html('Error uploading file: \"' + stripped + '\"').show()
        @removeFile file
        return
    )

    submitButton = form.find('#submit-all')[0]
    submitButton.addEventListener 'click', (e) ->
      e.preventDefault()
      e.stopPropagation()
      alert "Please select a file" if dropzone[0].dropzone.getQueuedFiles().length == 0
      dropzone[0].dropzone.processQueue()
      return false