-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
88 lines (67 loc) · 2.53 KB
/
index.js
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
(function (root, factory) {
'use strict';
if (typeof define === 'function' && define.amd) {
define(['angular'], factory);
} else if (typeof exports === 'object') {
factory(require('angular'));
module.exports = 'ngDropImage';
} else {
// Browser globals (root is window), we don't register it.
factory(root.angular);
}
}(window , function (angular) {
'use strict';
var directive = function($q, $localStorage) {
return {
require: '^ngModel',
restrict: 'A',
scope: { 'ngModel': '=' },
link: ($scope, $element, $attrs, $ngModel) => {
$scope.$storage = $localStorage.$default({
image: $scope.ngModel
});
$ngModel.$setViewValue($scope.$storage.image);
$($element[0]).on('dragover dragenter dragleave', (e) => {
e.preventDefault();
e.stopPropagation();
return false;
});
$($element[0]).on('drop', e => {
e.stopPropagation();
e.preventDefault();
var files = e.originalEvent.dataTransfer.files;
if(files.length){
readFile(files[0]).then((value) => {
$ngModel.$setViewValue(value);
storage(value);
});
} else {
var url = e.originalEvent.dataTransfer.getData('url');
$ngModel.$setViewValue(url);
storage(url);
}
$scope.$apply();
return false;
})
function storage(value){
$scope.$storage.image = value;
};
function readFile(file) {
var deferred = $q.defer();
var reader = new FileReader();
reader.onload = event => {
deferred.resolve(event.target.result);
};
reader.onerror = event => {
deferred.reject(e);
};
reader.readAsDataURL(file);
return deferred.promise;
}
}
}
}
directive.$inject = ['$q', '$localStorage'];
angular.module('ngDropImage', ['ngStorage'])
.directive('ngDropImage', directive);
}))