<commit_before>'use strict';

angular.module('mage').controller('FormCtrl', function ($scope, $injector, appConstants, FormService, Layer, Form, $filter, $timeout, ObservationService) {

    // preview form mode
    $scope.previewMode = false;

    $scope.fs = FormService;
    $scope.saveTime = 0;

    $scope.form = FormService.editForm;

    $scope.fileUploadOptions = {
    };

    $scope.$watch('fs.editForm', function(newForm, oldForm){
        $scope.form = FormService.editForm;
        if ($scope.form) {
            angular.forEach($scope.form.fields, function(field) {
                if (field.name == 'type') {
                    $scope.typeField = field;
                }
            });
         }
    });

    // previewForm - for preview purposes, form will be copied into this
    // otherwise, actual form might get manipulated in preview mode
    $scope.previewForm = {};

    $scope.fieldTypes = FormService.fields;
    $scope.newField = {
        "title" : "New field",
        "type" : $scope.fieldTypes[0].name,
        "value" : "",
        "required" : true
    };

    // accordion settings
    $scope.accordion = {}
    $scope.accordion.oneAtATime = true;
    $scope.variants = [];

    $scope.deletableField = function(field) {
      return field.name.indexOf('field') != -1;
    }

    // create new field button click
    $scope.addNewField = function() {

        // put newField into fields array

        var id = $scope.form.fields[$scope.form.fields.length-1].id + 1;

        $scope.newField.id = id;
        $scope.newField.name = "field" + id;
        $scope.form.fields.push($scope.newField);

        $scope.newField = {
            "title" : "New field",
            "type" : $scope.fieldTypes[0].name,
            "value" : "",
            "required" : true
        };

        $scope.autoSave();
    }

    var debounceHideSave = _.debounce(function() {
      $scope.$apply(function() {
        $scope.saved = false;
      });
    }, 5000);

    var debouncedAutoSave = _.debounce(function() {
      $scope.$apply(function() {
        if ($scope.form.id) {
          $scope.form.$save({}, function() {
            $scope.saved = true;
            ObservationService.updateForm();
            $timeout(function() {
              debounceHideSave();
            });
            console.log($scope.form);
          });
        }
      });
    }, 1000);

    $scope.$on('uploadComplete', function(event, args) {
      $scope.savedTime = Date.now();
    });

    $scope.autoSave = function() {
      debouncedAutoSave();
    }

    $scope.populateVariants = function(doNotAutoSave) {
      if (!$scope.form) return;
      $scope.variantField = _.find($scope.form.fields, function(field) {
        return field.name == $scope.form.variantField;
      });
      if (!$scope.variantField) {
        // they do not want a variant
        $scope.variants = [];
        $scope.form.variantField = null;
        if (!doNotAutoSave) {
          $scope.autoSave();
        }
        return;
      }
      //$scope.form.variantField = $scope.variantField.name;
      if ($scope.variantField.type == 'dropdown') {
        $scope.variants = $filter('orderBy')($scope.variantField.choices, 'value');
        $scope.showNumberVariants = false;
      } else if ($scope.variantField.type == 'date') {
        $scope.variantField.choices = $scope.variantField.choices || [];
        $scope.variants = $filter('orderBy')($scope.variantField.choices, 'value');
        $scope.showNumberVariants = true;
      }
      if (!doNotAutoSave) {
        $scope.autoSave();
      }
    }

    $scope.$watch('form', function() {
      if (!$scope.form) return;

      $scope.populateVariants();
    });

    $scope.$watch('form.fields', function() {
      if (!$scope.form || !$scope.form.fields) return;
      angular.forEach($scope.form.fields, function(field) {
          if (field.name == 'type') {
              $scope.typeField = field;
          }
      });
    });

    $scope.$watch('form.variantField', function() {
      if (!$scope.form || !$scope.form.variantField) return;
      $scope.populateVariants()
    })

    // deletes particular field on button click
    $scope.deleteField = function (id) {
      var deletedField = _.find($scope.form.fields, function(field) { return id == field.id});
      if (deletedField) {
        deletedField.archived = true;
        $scope.populateVariants();
        $scope.autoSave();
      }
    }

    $scope.variantFilter = function(field) {
      return (field.type == 'dropdown' || field.type == 'date') && field.name != 'type';
    }

    // add new option to the field<commit_msg> Merge branch 'develop' into 'master'<commit_after>'use strict';

angular.module('mage').controller('FormCtrl', function ($scope, $injector, appConstants, FormService, Layer, Form, $filter, $timeout, ObservationService) {

    // preview form mode
    $scope.previewMode = false;

    $scope.fs = FormService;
    $scope.saveTime = 0;

    $scope.form = FormService.editForm;

    $scope.fileUploadOptions = {
    };

    $scope.$watch('fs.editForm', function(newForm, oldForm){
        $scope.form = FormService.editForm;
        if ($scope.form) {
            angular.forEach($scope.form.fields, function(field) {
                if (field.name == 'type') {
                    $scope.typeField = field;
                }
            });
         }
    });

    // previewForm - for preview purposes, form will be copied into this
    // otherwise, actual form might get manipulated in preview mode
    $scope.previewForm = {};

    $scope.fieldTypes = FormService.fields;
    $scope.newField = {
        "title" : "New field",
        "type" : $scope.fieldTypes[0].name,
        "value" : "",
        "required" : true
    };

    // accordion settings
    $scope.accordion = {}
    $scope.accordion.oneAtATime = true;
    $scope.variants = [];

    $scope.deletableField = function(field) {
      return field.name.indexOf('field') != -1;
    }

    // create new field button click
    $scope.addNewField = function() {

        // put newField into fields array

        var id = $scope.form.fields[$scope.form.fields.length-1].id + 1;

        $scope.newField.id = id;
        $scope.newField.name = "field" + id;
        $scope.form.fields.push($scope.newField);

        $scope.newField = {
            "title" : "New field",
            "type" : $scope.fieldTypes[0].name,
            "value" : "",
            "required" : true
        };

        $scope.autoSave();
    }

    var debounceHideSave = _.debounce(function() {
      $scope.$apply(function() {
        $scope.saved = false;
      });
    }, 5000);

    var debouncedAutoSave = _.debounce(function() {
      $scope.$apply(function() {
        if ($scope.form.id) {
          $scope.form.$save({}, function() {
            $scope.saved = true;
            ObservationService.updateForm();
            $timeout(function() {
              debounceHideSave();
            });
            console.log($scope.form);
          });
        }
      });
    }, 1000);

    $scope.$on('uploadComplete', function(event, args) {
      $scope.savedTime = Date.now();
    });

    $scope.autoSave = function() {
      debouncedAutoSave();
    }

    $scope.populateVariants = function(doNotAutoSave) {
      if (!$scope.form) return;
      $scope.variantField = _.find($scope.form.fields, function(field) {
        return field.name == $scope.form.variantField;
      });
      if (!$scope.variantField) {
        // they do not want a variant
        $scope.variants = [];
        $scope.form.variantField = null;
        if (!doNotAutoSave) {
          $scope.autoSave();
        }
        return;
      }
      //$scope.form.variantField = $scope.variantField.name;
      if ($scope.variantField.type == 'dropdown') {
        $scope.variants = $filter('orderBy')($scope.variantField.choices, 'value');
        $scope.showNumberVariants = false;
      } else if ($scope.variantField.type == 'date') {
        $scope.variantField.choices = $scope.variantField.choices || [];
        $scope.variants = $filter('orderBy')($scope.variantField.choices, 'value');
        $scope.showNumberVariants = true;
      }
      if (!doNotAutoSave) {
        $scope.autoSave();
      }
    }

    $scope.$watch('form', function() {
      if (!$scope.form) return;

      $scope.populateVariants();
    });

    $scope.$watch('form.fields', function() {
      if (!$scope.form || !$scope.form.fields) return;
      angular.forEach($scope.form.fields, function(field) {
          if (field.name == 'type') {
              $scope.typeField = field;
          }
      });
    });

    $scope.$watch('form.variantField', function(newValue, oldValue) {
      if (!$scope.form || newValue == oldValue) return;
      $scope.populateVariants()
    })

    // deletes particular field on button click
    $scope.deleteField = function (id) {
      var deletedField = _.find($scope.form.fields, function(field) { return id == field.id});
      if (deletedField) {
        deletedField.archived = true;
        $scope.populateVariants();
        $scope.autoSave();
      }
    }

    $scope.variantFilter = function(field) {
      return (field.type == 'dropdown' || field.type == 'date') && field.name != 'type';
    }

    // add new option to the field
{'fraction_non_alphanumeric': 0.15704956642757734, 'fraction_numerical': 0.0029975377368590085, 'mean_word_length': 2.146513977770293, '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': '1358786', 'n_tokens_mistral': 2839, 'n_tokens_neox': 2762, 'n_words': 518}