forked from Coded/SIPRP
				
			
			You can not select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
		
			
		
			
				
					
					
						
							133 lines
						
					
					
						
							4.2 KiB
						
					
					
				
			
		
		
	
	
							133 lines
						
					
					
						
							4.2 KiB
						
					
					
				| evoapp.directive('fileUpload', function($rootScope, $filter, globals, $timeout) {
 | |
| 
 | |
|     return {
 | |
|         restrict: 'AE',
 | |
|         template: 
 | |
|             '<span class="btn btn-sm btn-default fileinput-button btn-block">' + 
 | |
|                 '<i class="glyphicon glyphicon-upload m-r-5"></i><span>Seleccionar Ficheiro</span>' + 
 | |
|                 '<input type="file" name="{{name}}" class="btn btn-sm btn-default" />' + 
 | |
|             '</span>' +  
 | |
|             '<div id="files" class="files"></div>' 
 | |
|         ,
 | |
|         scope: {
 | |
|             title: '@',
 | |
|             name: '@',
 | |
|             itemId: '@',
 | |
|             url: '@',
 | |
|             formData: '='
 | |
|         },
 | |
|         controller: function($rootScope, $scope, $filter, globals, $timeout) {
 | |
| 
 | |
|             $scope.files = [];
 | |
| 
 | |
|             $scope.$filter = $filter;
 | |
|             $scope.$rootScope = $rootScope;
 | |
| 
 | |
|             if($scope.$parent[$scope.itemId] == undefined)
 | |
|             {
 | |
|                 $scope.$parent[$scope.itemId] = $scope;
 | |
|             }
 | |
|             else
 | |
|             {
 | |
|                 throw Error('FileUpload ' + $scope.itemId + ' already exists!');
 | |
|             }
 | |
|         },
 | |
|         link: function (scope, elem, attrs) {
 | |
| 
 | |
|             var files = $($(elem[0]).find('#files')[0]);
 | |
| 
 | |
|             //pass specified values to specified function arguments
 | |
|             function callback(fn, args) {
 | |
|                 var fnArgs = [].concat(args);
 | |
|                 fnArgs.push.apply(fnArgs, arguments);
 | |
|                 return fn.apply(this, fnArgs);
 | |
|             };
 | |
| 
 | |
|             $(elem[0]).fileupload({
 | |
|                 url: scope.url,
 | |
|                 dataType: 'json',
 | |
|                 singleFileUploads: true,
 | |
|                 autoUpload: false,
 | |
|                 acceptFileTypes: /(\.|\/)(gif|jpe?g|png|pdf|doc|docx|odt|ods|odp)$/i
 | |
|             }).on('fileuploadadd', function (e, data) {
 | |
| 
 | |
|                 var file = data.files[0];
 | |
|                 
 | |
|                 files.html('<br />' + file.name + '<br />');
 | |
| 
 | |
|             }).on('fileuploadprocessalways', function (e, data) {
 | |
| 
 | |
| 
 | |
|             }).on('fileuploadprogressall', function (e, data) {
 | |
| 
 | |
|             }).on('fileuploaddone', function (e, data) {
 | |
| 
 | |
|                 files.text('');
 | |
| 
 | |
|             }).on('fileuploadfail', function (e, data) {
 | |
| 
 | |
|                 files.text('');
 | |
|             });
 | |
| 
 | |
|             $(elem[0]).bind('change', function (e) {
 | |
| 
 | |
|                 var f = e.target.files || [{name: this.value}];
 | |
| 
 | |
|                 scope.files = new Array();
 | |
| 
 | |
|                 var file = f[0]; 
 | |
| 
 | |
|                 if(file.error == undefined)
 | |
|                 {
 | |
|                     scope.files.push(f[0]);
 | |
|                 }
 | |
|                 else
 | |
|                 {   
 | |
|                     globals.message.alert({
 | |
|                         message: 'Tipo de Ficheiro inválido!',
 | |
|                         callback: function(){
 | |
|                             scope.reset();
 | |
|                         }
 | |
|                     });
 | |
|                 }
 | |
|             });
 | |
| 
 | |
|             scope.upload = function(formData){
 | |
| 
 | |
|                 if(scope.files.length > 0)
 | |
|                 {
 | |
|                     scope.$rootScope.$broadcast('loadingStatusActive');
 | |
| 
 | |
|                     var callbackFn = arguments[1];
 | |
| 
 | |
|                     $(elem[0]).fileupload('send', {
 | |
|                         files: scope.files,
 | |
|                         formData: formData
 | |
|                     })
 | |
|                     .success(function (result, textStatus, jqXHR) {                    
 | |
| 
 | |
|                         scope.$rootScope.$broadcast('loadingStatusInactive');
 | |
| 
 | |
|                         callback(callbackFn, [result, textStatus, jqXHR]);
 | |
|                     })
 | |
|                     .error(function (jqXHR, textStatus, errorThrown) {
 | |
| 
 | |
|                         scope.$rootScope.$broadcast('loadingStatusInactive');
 | |
| 
 | |
|                         //callback(callbackFn, [jqXHR, textStatus, errorThrown]);
 | |
|                     })
 | |
|                     .complete(function (result, textStatus, jqXHR) {
 | |
| 
 | |
|                         //callback(callbackFn, [result, textStatus, jqXHR]);
 | |
|                     });
 | |
|                 }
 | |
|             };
 | |
| 
 | |
|             scope.reset = function(){
 | |
| 
 | |
|                 scope.files = [];
 | |
|                 files.empty();
 | |
|             };
 | |
|         }
 | |
|     }
 | |
| }); |