<commit_before>'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);
      }
    }<commit_msg>Remove options object to make directive simpler<commit_after>'use strict';

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

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

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

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

        var options = {
          url: scope.url,
          method: scope.method || 'POST',
          forceIFrameUpload: scope.$eval(attr.forceIframeUpload) || false,
          data: scope.data || {}
        };

        options.data[scope.param || '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});
          }
        );
      });

      // Add required to file input and ng-invalid-required
      // Since the input is reset when upload is complete, we need to check something in the
      // onSuccess and set required="false" when we feel that the upload is correct
      attr.$observe('required', function uploadButtonRequiredObserve(value) {
        var required = scope.$eval(value);
        fileInput.attr('required', angular.isUndefined(required) || required);
        element.toggleClass('ng-valid', !required);
        element.toggleClass('ng-invalid ng-invalid-required', required);
      });

      attr.$observe('accept', function uploadButtonAcceptObserve(value) {
        fileInput.attr('accept', value);
      });

      if (upload.support.formData) {
        var uploadButtonMultipleObserve = function () {
          fileInput.attr('multiple', !!(scope.$eval(attr.multiple) && !scope.$eval(attr.forceIframeUpload)));
        };
        attr.$observe('multiple', uploadButtonMultipleObserve);
        attr.$observe('forceIframeUpload', uploadButtonMultipleObserve);
      }
    }
{'fraction_non_alphanumeric': 0.14695907754917475, 'fraction_numerical': 0.0013565453312231518, 'mean_word_length': 2.450858034321373, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4714898', 'n_tokens_mistral': 1212, 'n_tokens_neox': 1191, 'n_words': 229}