<commit_before>(function () {
'use strict';

  angular.module('hutmap.controllers').

  controller('HutFormInstanceCtrl', 
    ['$scope', '$http', '$upload', '$modalInstance', '$cookies', 'hutFormUrl', 'header', '$log',
    function($scope, $http, $upload, $modalInstance, $cookies, hutFormUrl, header, $log) {
    
    $scope.hut = null;
    $scope.header = header;
    $scope.submitting = false;

    $scope.select2Options = {
      width: 'resolve',
      placeholderOption: 'first',
    };

    $http.get(hutFormUrl).
      success(function(data, status, headers, config) {
        $scope.hutForm = data;
      });
    
    var submitHut = function(url, hut) {
      $scope.submitting = true;
      $upload.upload({
        url: url, 
        headers: {'X-CSRFToken': $cookies.csrftoken},
        data: hut,
        file: hut.photo || '',
        formDataAppender: function(fd, key, val) {
          if (val === null) {
            fd.append(key, '');
          } else if (angular.isArray(val)) {
            angular.forEach(val, function(v) {
              fd.append(key, v);
            });
          } else {
            fd.append(key, val);
          }
        },
      }).progress(function(evt) {
        $log.info('percent: ' + parseInt(100.0 * evt.loaded / evt.total));
      }).success(function(data, status, headers, config) {
        if (status === 201) {
          $modalInstance.close(hut);
        } else {
          $scope.hutForm = data;
        }
        $scope.submitting = false;
      }).error(function(data, status, headers, config){
        $log.error('ERROR: ', status, data);
        $modalInstance.dismiss('error');
        $scope.submitting = false;
      });
    };

    $scope.submit = function() {
      if ($scope.hut != null) {
        submitHut(hutFormUrl, $scope.hut);
      } else {
        $log.warn('$scope.hut was null', $scope.hut);
      }
    };

    $scope.cancel = function() {
      $modalInstance.dismiss('cancel');
    };

    // TODO: #31 get rid of dirty hack for select fields
    $scope.$watch('hut', function(newVal) {
      if (newVal) {
        angular.forEach(newVal, function(value, field) {
          if (typeof(value) === 'boolean') {
            if (value === null) {
              $scope.hut[field] = 1;
            } else if (value) {
              $scope.hut[field] = 2;
            } else {
              $scope.hut[field] = 3;
            }
          }
        });
      }
    });

  }]);

})();<commit_msg>Revamped hack to be a little less hacky and work in all cases.<commit_after>(function () {
'use strict';

  angular.module('hutmap.controllers').

  controller('HutFormInstanceCtrl', 
    ['$scope', '$http', '$upload', '$modalInstance', '$cookies', 'hutFormUrl', 'header', '$log',
    function($scope, $http, $upload, $modalInstance, $cookies, hutFormUrl, header, $log) {
    
    $scope.hut = null;
    $scope.header = header;
    $scope.submitting = false;

    $scope.select2Options = {
      width: 'resolve',
      placeholderOption: 'first',
    };

    $http.get(hutFormUrl).
      success(function(data, status, headers, config) {
        $scope.hutForm = data;
      });
    
    var submitHut = function(url, hut) {
      $scope.submitting = true;
      $upload.upload({
        url: url, 
        headers: {'X-CSRFToken': $cookies.csrftoken},
        data: hut,
        file: hut.photo || '',
        formDataAppender: function(fd, key, val) {
          if (val === null) {
            fd.append(key, '');
          } else if (angular.isArray(val)) {
            angular.forEach(val, function(v) {
              fd.append(key, v);
            });
          } else {
            fd.append(key, val);
          }
        },
      }).progress(function(evt) {
        $log.info('percent: ' + parseInt(100.0 * evt.loaded / evt.total));
      }).success(function(data, status, headers, config) {
        if (status === 201) {
          $modalInstance.close(hut);
        } else {
          $scope.hutForm = data;
        }
        $scope.submitting = false;
      }).error(function(data, status, headers, config){
        $log.error('ERROR: ', status, data);
        $modalInstance.dismiss('error');
        $scope.submitting = false;
      });
    };

    $scope.submit = function() {
      if ($scope.hut != null) {
        submitHut(hutFormUrl, $scope.hut);
      } else {
        $log.warn('$scope.hut was null', $scope.hut);
      }
    };

    $scope.cancel = function() {
      $modalInstance.dismiss('cancel');
    };

    // TODO: #31 get rid of dirty hack for select fields
    $scope.$watch('hut', function(newVal) {
      if (newVal) {
        $('select:not(select[multiple])').each(function(index, select) {
          var name = $(select).attr('name');
          var selectedOption = $(select).children('option[selected]').first();
          var value = $(selectedOption).attr('value');
          $scope.hut[name] = value;
        });
      }
    });

  }]);

})();
{'fraction_non_alphanumeric': 0.17174571140262362, 'fraction_numerical': 0.004641775983854692, 'mean_word_length': 2.0349050826699324, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '4154952', 'n_tokens_mistral': 1595, 'n_tokens_neox': 1551, 'n_words': 304}