summaryrefslogtreecommitdiff
path: root/public/assets/js/vendor/view/uploadview.js
blob: fd8b085ada1c5461239732be7e4775ee565e1929 (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
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
var UploadView = View.extend({

  // define uploadAction

  events: {
    "change [type=file]": "handleFileSelect",
    "submit form": "preventDefault",
  },
  
  initialize: function(){
    this.$file = this.$("[type=file]")
    this.$upload = this.$(".upload-icon")
  },
  
  beforeUpload: function(){
  },
  
  handleFileSelect: function(e) {
    e.stopPropagation();
    e.preventDefault();
    
    this.beforeUpload()

    var files = e.dataTransfer ? e.dataTransfer.files : e.target.files

    for (var i = 0, f; f = files[i]; i++) {
      if ( ! f.type.match('image.*')) {
        continue;
      }
      
      if (is_mobile && f.type.match(/jpg|jpeg/)) {
        this.fixOrientationAndUpload(f)
      }
      else {
        this.upload(f)
      }
      // this.getImageDimensions(f)
    }
  },

  fixOrientationAndUpload: function(f){
    var reader = new FileReader();
    var img = new Image ()
    
    reader.addEventListener("load", function () {
      img.src = reader.result
    }, false)

    img.onload = function(){
      var canvas = renderToCanvas(img, {
        correctOrientation: true,
        scale: 1
      })
      var dataURI = canvas.toDataURL("image/jpeg", 0.8)
      var blob = dataUriToBlob( dataURI )
      this.upload(blob)
    }.bind(this)

    reader.readAsDataURL(f)
  },

/*
  getImageDimensions: function(f){
    var base = this
    
    this.$upload.addClass('uploading')

    var reader = new FileReader()

    reader.onload = function(e) {
      var image = new Image()
      image.onload = function(){
        var width = image.naturalWidth,
          height = image.naturalHeight
        base.upload(f, width, height)
      }
      image.src = e.target.result
    }
    
    reader.readAsDataURL(f);
  },
*/

  upload: function(f){
    var fd = new FormData()
    fd.append('image', f)
//     fd.append('width', width)
//     fd.append('height', height)
//     fd.append('_csrf', $("[name=_csrf]").val())
    
    if (this.mediaTag) {
      fd.append('tag', this.mediaTag)
    }

    var request = $.ajax({
      url: this.uploadAction,
      type: "post",
      data: fd,
      dataType: "json",
      processData: false,
      contentType: false,
    })
    request.done(this.success.bind(this))
  },
  
  success: function(media){
    if (media.error) {
      // console.log(media.error)
      this.$upload.removeClass('uploading')
      this.error(media.error)
      return
    }
    this.$upload.removeClass('uploading')
    this.add(media)
  },
  
  add: function(media){
    console.log(media)
  },
  
  error: function(error){
  },

})