'use strict';

/**
 * @ngdoc function
 * @name gruppoiconoApp.directive:uploadImage
 * @description
 * # uploadImage
 * Directive of the gruppoiconoApp
 */
angular.module('gruppoiconoApp')
    .directive('uploadImage', function(images) {
        return {
            restrict: 'A',
            scope: {
                entity: '=uploadImage',
                handlerName: '@'
            },
            link: function(scope, element) {
                var files;
                element.bind('change', function (changeEvent) {
                    files = changeEvent.target.files;
                    var fd = new FormData();
                    //Take the first selected file
                    fd.append('file', files[0]);

                    element.val('');
                    images[scope.handlerName](fd, scope.entity._id);
                });
            }
        };
    });
