app directive notubeUpload Ticket Upload ImageValidation timeout funct

  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
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
app.directive('notubeUpload', ['Ticket', 'Upload', 'ImageValidation', '$timeout', function(Ticket, Upload, ImageValidation, $timeout) {
return {
- restrict: 'A',
+ restrict: 'E',
require: 'ngModel',
+ templateUrl: 'src/app/templates/directives/notube_upload.html',
+ replace: true,
+ scope: {
+ type:'='
+ },
link: function(scope, element, attrs, ngModel) {
+ var model = {
+ error: false,
+ error_message: '',
+ progress: 0,
+ verify: false,
+ file: undefined,
+ completed: false,
+ error_message: "Unsupported file type"
+ };
+
+ scope.verify_all = function() {
+ var verify = true;
+ angular.forEach(scope.models, function(obj, ind) {
+ if (!obj.verify)
+ verify = false;
+ });
+ return verify
+ };
+
+ scope.complete_all = function() {
+ var completed = false;
+ angular.forEach(scope.models, function(obj, ind) {
+ if (!obj.completed)
+ completed = true;
+ });
+ var result = completed && scope.image_type;
+ return result
+ }
+
+
+ scope.upload = function($files, $index) {
+
+ if ($files && $files.length) {
+ if (scope.image_type) {
+ ImageValidation.save({'filename': $files[0].name}).$promise.then(function(data) {
+ scope.models[$index].verify = true;
+ scope.models[$index].error = false;
+ }, function(data) {
+ scope.models[$index].verify = false;
+ scope.models[$index].error = true;
+ });
+ return;
+ }
+ Ticket.save({'filenames': JSON.stringify({'filename': $files[0].name, 'name': 'file'})}).$promise.then(function(data) {
+ scope.models[$index].error = false;
+ ngModel.$setViewValue(data._id);
+ Upload.upload({
+ method: 'POST',
+ url: data.upload.path,
+ fields: data.upload.params,
+ file: $files[0]
+ }).progress(function (evt) {
+ scope.models[$index].progress = parseInt(100.0 * evt.loaded / evt.total);
+ console.log('progress: ' + scope.models[$index].progress + '% ' + evt.config.file.name);
+ }).success(function (data, status, headers, config) {
+ scope.models[$index].completed = true;
+ console.log('file ' + config.file.name + 'uploaded. Response: ' + data);
+ });
+ }, function(data) {
+ scope.models[$index].error = true;
+ })
+ }
+ }
+
+ scope.$watch("type", function(value){
+ scope.models = [angular.copy(model)];
+ if (['video', 'audio'].indexOf(value) > -1)
+ scope.image_type = false;
+ else
+ scope.image_type = true
+ });
+
+ scope.add_other = function() {
+ scope.models.push(angular.copy(model));
+ };
+
+
+ scope.upload_images = function() {
+ var filenames = [];
+ angular.forEach(scope.models, function(obj, ind) {
+ filenames.push({'filename': obj.file[0].name, 'name': 'image_' + ind});
+ });
+ Ticket.save({'filenames': JSON.stringify(filenames)}).$promise.then(function(data) {
+ ngModel.$setViewValue(data._id);
+ $timeout(function() {
+ angular.forEach(data.uploads, function(obj, ind) {
+ Upload.upload({
+ method: 'POST',
+ url: obj.upload.path,
+ fields: obj.upload.params,
+ file: scope.models[ind].file[0]
+ }).progress(function (evt) {
+ scope.models[ind].progress = parseInt(100.0 * evt.loaded / evt.total);
+ console.log('progress: ' + scope.models[ind].progress + '% ' + evt.config.file.name);
+ }).success(function (data, status, headers, config) {
+ scope.models[ind].completed = true;
+ console.log('file ' + config.file.name + 'uploaded. Response: ' + data);
+ });
+ });
+ }, 100)
+ });
+
+ }
+ }
+ }
+}]);