summaryrefslogtreecommitdiff
path: root/js/cabinet.js
blob: cb8039b18e0986a43758db5f6754c89439596441 (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
/* jshint browser:true, jquery:true */
(function($) {
  $.hasFileAPI = function hasFileAPI() {
    return (window.FormData !== undefined);
  };

  $.fn.cabinet = function(input) {
    input = $(input);

    var _this = this;
    var passthrough = function(outer, inner, prevent, fn) {
      _this.on(outer, function(e) {
        if (inner === 'click') {
          input[0].click();
        } else {
          input.trigger(inner);
        }
        if (prevent) {
          e.preventDefault();
        }
        if (fn) {
          fn(e);
        }
      }, false);
    };

    input[0].filelist = Object.create(FileList);

    input.on('change', function(e) {
      this.filelist = e.target.files;
      _this.change();
    });

    passthrough('dragenter', 'dragenter', true);
    passthrough('dragover', 'dragover', true);
    passthrough('dragleave', 'dragleave', true);
    passthrough('click', 'click', false);
    passthrough('drop', 'dragleave', true, function(e) {
      input[0].filelist = e.dataTransfer.files;
      _this.change();
    });
  };
})($);