summaryrefslogtreecommitdiff
path: root/themes/okadmin/public/js/upload.js
blob: 39f742752abadc3323d2d635060dbe134870a047 (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
var OKUpload = function(){
	this.action = "/_services/image"
}
OKUpload.prototype.bind = function(el){
  if (el.length) el = el[0]
  el.addEventListener("change", this.handleFileSelect.bind(this))
}
OKUpload.prototype.handleFileSelect = function(e) {
  e.stopPropagation();
  e.preventDefault();

  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;
    }
    this.upload(f)
  }
}
OKUpload.prototype.upload = function(f){
  var fd = new FormData()
  fd.append('image', f)

  var request = $.ajax({
    url: this.action,
    type: "post",
    data: fd,
    dataType: "json",
    processData: false,
    contentType: false,
  })
  request.done(this.success.bind(this))
}
OKUpload.prototype.success = function(media){
  if (media.error) {
    console.log(media.error)
    return
  }
  this.add(media)
}
OKUpload.prototype.add = function(media){
  console.log(media)
}
OKUpload.prototype.error = function(error){
  throw error
}