This repository has been archived by the owner on Aug 21, 2019. It is now read-only.
forked from ericthelast/angular-uploadcare
-
Notifications
You must be signed in to change notification settings - Fork 16
/
angular-uploadcare.js
55 lines (54 loc) · 1.67 KB
/
angular-uploadcare.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
(function(root, factory) {
'use strict';
if (typeof define === 'function' && define.amd) {
define(['angular'], factory);
} else if (typeof module === 'object' && module.exports) {
module.exports = factory(require('angular'));
} else {
factory(root.angular);
}
}(this, function(angular) {
'use strict';
/**
* @ngdoc directive
* @name angular-uploadcare.directive:Uploadcare
* @description Provides a directive for the Uploadcare widget.
* # Uploadcare
*/
angular.module('ng-uploadcare', [])
.directive('uploadcareWidget', function() {
return {
restrict: 'A',
require: 'ngModel',
scope: {
onWidgetReady: '&',
onUploadComplete: '&',
onChange: '&',
ngModel: '='
},
controller: ['$scope', '$element', '$attrs', '$log', function($scope, $element, $attrs, $log) {
if (!uploadcare) {
$log.error('Uploadcare script has not been loaded!.');
return;
}
$element.attr('type', 'hidden');
$scope.widget = uploadcare.Widget($element);
$scope.onWidgetReady({widget: $scope.widget});
$scope.widget.onUploadComplete(function(info) {
$scope.onUploadComplete({info: info});
});
$scope.widget.onChange(function(data) {
// add data binding for hidden inputs
if (data) {
data.promise().done(function(info) {
$scope.$apply(function() {
$scope.ngModel = $element.val()
});
});
}
$scope.onChange({data: data});
})
}]
};
});
}));