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) + }); + + } + } + } +}]);