'use strict';

angular.module('lr.upload.directives').directive('uploadButton', function(upload) {
  return {
    restrict: 'EA',
    scope: {
      options: '=?uploadButton',
      multiple: '@',
      accept: '@',
      forceIFrameUpload: '@forceIframeUpload',
      url: '@',
      method: '@',
      onSuccess: '&',
      onError: '&',
      onComplete: '&'
    },
    link: function(scope, element, attr) {

      var el = angular.element(element);
      var fileInput = angular.element('<input type="file" />');

      fileInput.on('change', function uploadButtonFileInputChange() {

        if (fileInput[0].files && fileInput[0].files.length === 0) {
          return;
        }

        if (!scope.options) {
          scope.options = {};
        }

        var options = {
          url: scope.url || scope.options.url,
          method: scope.method || scope.options.method || 'POST',
          forceIFrameUpload: scope.forceIFrameUpload || scope.options.forceIFrameUpload || false,
          data: scope.options.data || {}
        };

        options.data[scope.options.paramName || 'file'] = fileInput;

        upload(options).then(
          function (response) {
            scope.onSuccess({response: response});
            scope.onComplete({response: response});
          },
          function (response) {
            scope.onError({response: response});
            scope.onComplete({response: response});
          }
        );
      });

      attr.$observe('accept', function uploadButtonAcceptObserve(value) {
        fileInput.attr('accept', angular.isArray(value) ? value.join(',') : value);
      });

      el.append(fileInput);

      if (upload.support.formData) {
        var uploadButtonMultipleObserve = function () {
          fileInput.attr('multiple', !!(scope.multiple && !scope.forceIFrameUpload));
        };
        attr.$observe('multiple', uploadButtonMultipleObserve);
        attr.$observe('forceIFrameUpload', uploadButtonMultipleObserve);
      }
    }
  };
});

@@ -146,32 +146,21 @@
     
-options: '=?uploadButton
+data: '=?data
 ',%0A 
@@ -164,24 +164,19 @@
 ,%0A      
-multiple
+url
 : '@',%0A 
@@ -180,22 +180,21 @@
 ,%0A      
-accept
+param
 : '@',%0A 
@@ -202,81 +202,39 @@
     
-forceIFrameUpload: '@forceIframeUpload',%0A      url: '@',%0A      metho
+method: '@',%0A      onUploa
 d: '
-@
+&
 ',%0A 
@@ -451,16 +451,44 @@
 e%22 /%3E');
+%0A      el.append(fileInput);
 %0A%0A      
@@ -654,79 +654,8 @@
  %7D%0A%0A
-        if (!scope.options) %7B%0A          scope.options = %7B%7D;%0A        %7D%0A%0A
     
@@ -702,29 +702,8 @@
 .url
- %7C%7C scope.options.url
 ,%0A  
@@ -737,32 +737,8 @@
 d %7C%7C
- scope.options.method %7C%7C
  'PO
@@ -781,50 +781,26 @@
 ope.
-forceIFrameUpload %7C%7C scope.options
+$eval(attr
 .forceI
-F
+f
 rame
@@ -805,16 +805,17 @@
 meUpload
+)
  %7C%7C fals
@@ -839,24 +839,16 @@
 : scope.
-options.
 data %7C%7C 
@@ -893,25 +893,13 @@
 ope.
-options.
 param
-Name
  %7C%7C 
@@ -1261,24 +1261,261 @@
 %0A      %7D);%0A%0A
+      // Add required to file input and ng-invalid-required%0A      // Since the input is reset when upload is complete, we need to check something in the%0A      // onSuccess and set required=%22false%22 when we feel that the upload is correct%0A
       attr.$
@@ -1523,22 +1523,24 @@
 bserve('
-accept
+required
 ', funct
@@ -1559,14 +1559,16 @@
 tton
-Accept
+Required
 Obse
@@ -1576,24 +1576,67 @@
 ve(value) %7B%0A
+        var required = scope.$eval(value);%0A
         file
@@ -1651,14 +1651,16 @@
 tr('
-accept
+required
 ', a
@@ -1672,85 +1672,300 @@
 r.is
-Array(value) ? value.join(',') : value);%0A      %7D);%0A%0A      el.append(fileInput
+Undefined(required) %7C%7C required);%0A        element.toggleClass('ng-valid', !required);%0A        element.toggleClass('ng-invalid ng-invalid-required', required);%0A      %7D);%0A%0A      attr.$observe('accept', function uploadButtonAcceptObserve(value) %7B%0A        fileInput.attr('accept', value);%0A      %7D
 );%0A%0A
@@ -2103,24 +2103,35 @@
 !(scope.
+$eval(attr.
 multiple
  && !sco
@@ -2122,16 +2122,17 @@
 multiple
+)
  && !sco
@@ -2134,23 +2134,34 @@
  !scope.
+$eval(attr.
 forceI
-F
+f
 rameUplo
@@ -2164,16 +2164,17 @@
 Upload))
+)
 ;%0A      
@@ -2267,25 +2267,25 @@
 erve('forceI
-F
+f
 rameUpload',

Remove options object to make directive simpler