Browse Source

dropzone directive

develop
Robin Thoni 8 years ago
parent
commit
f73c17028b
1 changed files with 64 additions and 0 deletions
  1. 64
    0
      public/app/controllers/directives/dropzone.controller.js

+ 64
- 0
public/app/controllers/directives/dropzone.controller.js View File

@@ -0,0 +1,64 @@
1
+/**
2
+ * Created by robin on 4/22/16.
3
+ */
4
+
5
+(function () {
6
+    angular.module('app').directive("imageDropzone", function() {
7
+        return {
8
+            restrict : "A",
9
+            scope: {
10
+                'imageDropzone': '&'
11
+            },
12
+            link: function (scope, elem) {
13
+                function processEvent(evt)
14
+                {
15
+                    if (evt != null) {
16
+                        evt.stopPropagation();
17
+                        evt.preventDefault();
18
+                        if (evt.dataTransfer) {
19
+                            evt.dataTransfer.effectAllowed = 'copy';
20
+                            evt.dataTransfer.dropEffect = 'copy';
21
+                        }
22
+                        else {
23
+                            evt.originalEvent.dataTransfer.effectAllowed = 'copy';
24
+                            evt.originalEvent.dataTransfer.dropEffect = 'copy';
25
+                        }
26
+                    }
27
+                    return false;
28
+                }
29
+                elem.bind('dragover', processEvent);
30
+                elem.bind('dragenter', processEvent);
31
+                elem.bind('dragstart', processEvent);
32
+                elem.bind('drop', function(evt) {
33
+                    evt.stopPropagation();
34
+                    evt.preventDefault();
35
+
36
+                    var files = null;
37
+                    if (evt.dataTransfer) {
38
+                        files = evt.dataTransfer.files;
39
+                    }
40
+                    else {
41
+                        files = evt.originalEvent.dataTransfer.files;
42
+                    }
43
+
44
+                    for (var i = 0, f; f = files[i]; i++) {
45
+                        var reader = new FileReader();
46
+                        reader.readAsBinaryString(f);
47
+
48
+                        reader.onload = (function(file) {
49
+                            return function(e) {
50
+                                scope.$apply(function() {
51
+                                    scope.imageDropzone({
52
+                                        content: e.target.result,
53
+                                        file: file
54
+                                    });
55
+                                });
56
+                            };
57
+                        })(f);
58
+                    }
59
+                    return false;
60
+                });
61
+            }
62
+        }
63
+    })
64
+})();

Loading…
Cancel
Save