<commit_before>miqAngularApplication.directive('checkchange', function() {
  return {
    require: 'ngModel',
    link: function (scope, elem, attr, ctrl) {
      scope['formchange_' + ctrl.$name] = elem[0].name
      scope['elemType_' + ctrl.$name] = attr.type;

      scope.$watch(attr.ngModel, function() {
        if(ctrl.$viewValue != undefined) {
          if(scope['elemType_' + ctrl.$name] != "date") {
            viewModelComparison(scope, ctrl);
          }
          else {
            viewModelDateComparison(scope, ctrl);
          }
        }
        if(scope.angularForm.$pristine )
          checkForOverallFormPristinity(scope, ctrl);
      });

      ctrl.$parsers.push(function(value) {
        scope.miqService.miqFlashClear();

        if (value == scope.modelCopy[ctrl.$name]) {
          scope.angularForm[scope['formchange_' + ctrl.$name]].$setPristine(true);
        }
        if(scope.angularForm[scope['formchange_' + ctrl.$name]].$pristine) {
          checkForOverallFormPristinity(scope, ctrl);
        }
        scope.angularForm[scope['formchange_' + ctrl.$name]].$setTouched(true);
        return value;
      });

      if(scope.angularForm.$pristine)
        scope.angularForm.$setPristine(true);
    }
  }
});

var viewModelComparison = function(scope, ctrl) {
  if (ctrl.$viewValue == scope.modelCopy[ctrl.$name]) {
    scope.angularForm[scope['formchange_' + ctrl.$name]].$setPristine(true);
    scope.angularForm[scope['formchange_' + ctrl.$name]].$setUntouched(true);
    scope.angularForm.$pristine = true;
  }
  else {
    scope.angularForm[scope['formchange_' + ctrl.$name]].$setPristine(false);
    scope.angularForm.$pristine = false;
  }
};

var viewModelDateComparison = function(scope, ctrl) {
  var viewValueDate = new Date(ctrl.$viewValue);
  var viewValueDateCmp = viewValueDate.getUTCMonth() + 1 + "/" + viewValueDate.getUTCDate() + "/" + viewValueDate.getUTCFullYear();

  var modelCopyDate = new Date(scope.modelCopy[ctrl.$name]);
  var modelCopyDateCmp = modelCopyDate.getUTCMonth() + 1 + "/" + modelCopyDate.getUTCDate() + "/" + modelCopyDate.getUTCFullYear();

  if(viewValueDateCmp == modelCopyDateCmp) {
    scope.angularForm[scope['formchange_' + ctrl.$name]].$setPristine(true);
    scope.angularForm[scope['formchange_' + ctrl.$name]].$setUntouched(true);
    scope.angularForm.$pristine = true;
  }
  else {
    scope.angularForm[scope['formchange_' + ctrl.$name]].$setPristine(false);
    scope.angularForm.$pristine = false;
  }
};

var checkForOverallFormPristinity = function(scope, ctrl) {
  var modelCopyObject = JSON.parse(JSON.stringify(scope.modelCopy));
  delete modelCopyObject[ctrl.$name];

  var modelObject = JSON.parse(JSON.stringify(scope[scope.model]))
  delete modelObject[ctrl.$name];

  if(JSON.stringify(modelCopyObject) != JSON.stringify(modelObject)) {
    scope.angularForm.$pristine = false;
  }
  else {
    scope.angularForm.$pristine = true;
  }
  if(scope.angularForm.$pristine)
    scope.angularForm.$setPristine(true);
};<commit_msg>checkchange.js - use lodash for deep copy and deep compare, check that modelCopy exists<commit_after>miqAngularApplication.directive('checkchange', function() {
  return {
    require: 'ngModel',
    link: function (scope, elem, attr, ctrl) {
      scope['formchange_' + ctrl.$name] = elem[0].name
      scope['elemType_' + ctrl.$name] = attr.type;

      scope.$watch(attr.ngModel, function() {
        if(ctrl.$viewValue != undefined) {
          if(scope['elemType_' + ctrl.$name] != "date") {
            viewModelComparison(scope, ctrl);
          }
          else {
            viewModelDateComparison(scope, ctrl);
          }
        }
        if(scope.angularForm.$pristine )
          checkForOverallFormPristinity(scope, ctrl);
      });

      ctrl.$parsers.push(function(value) {
        scope.miqService.miqFlashClear();

        if (value == scope.modelCopy[ctrl.$name]) {
          scope.angularForm[scope['formchange_' + ctrl.$name]].$setPristine(true);
        }
        if(scope.angularForm[scope['formchange_' + ctrl.$name]].$pristine) {
          checkForOverallFormPristinity(scope, ctrl);
        }
        scope.angularForm[scope['formchange_' + ctrl.$name]].$setTouched(true);
        return value;
      });

      if(scope.angularForm.$pristine)
        scope.angularForm.$setPristine(true);
    }
  }
});

var viewModelComparison = function(scope, ctrl) {
  if (ctrl.$viewValue == scope.modelCopy[ctrl.$name]) {
    scope.angularForm[scope['formchange_' + ctrl.$name]].$setPristine(true);
    scope.angularForm[scope['formchange_' + ctrl.$name]].$setUntouched(true);
    scope.angularForm.$pristine = true;
  }
  else {
    scope.angularForm[scope['formchange_' + ctrl.$name]].$setPristine(false);
    scope.angularForm.$pristine = false;
  }
};

var viewModelDateComparison = function(scope, ctrl) {
  var viewValueDate = new Date(ctrl.$viewValue);
  var viewValueDateCmp = viewValueDate.getUTCMonth() + 1 + "/" + viewValueDate.getUTCDate() + "/" + viewValueDate.getUTCFullYear();

  var modelCopyDate = new Date(scope.modelCopy[ctrl.$name]);
  var modelCopyDateCmp = modelCopyDate.getUTCMonth() + 1 + "/" + modelCopyDate.getUTCDate() + "/" + modelCopyDate.getUTCFullYear();

  if(viewValueDateCmp == modelCopyDateCmp) {
    scope.angularForm[scope['formchange_' + ctrl.$name]].$setPristine(true);
    scope.angularForm[scope['formchange_' + ctrl.$name]].$setUntouched(true);
    scope.angularForm.$pristine = true;
  }
  else {
    scope.angularForm[scope['formchange_' + ctrl.$name]].$setPristine(false);
    scope.angularForm.$pristine = false;
  }
};

var checkForOverallFormPristinity = function(scope, ctrl) {
  // don't do anything before the model and modelCopy are actually initialized
  if (! ('modelCopy' in scope) || ! scope.modelCopy || ! scope.model || ! (scope.model in scope))
    return;

  var modelCopyObject = _.cloneDeep(scope.modelCopy);
  delete modelCopyObject[ctrl.$name];

  var modelObject = _.cloneDeep(scope[scope.model]);
  delete modelObject[ctrl.$name];

  scope.angularForm.$pristine = _.isEqual(modelCopyObject, modelObject);

  if (scope.angularForm.$pristine)
    scope.angularForm.$setPristine(true);
};
{'fraction_non_alphanumeric': 0.15200903663062773, 'fraction_numerical': 0.0009682104243989027, 'mean_word_length': 3.6601503759398497, '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': '1782558', 'n_tokens_mistral': 2015, 'n_tokens_neox': 1970, 'n_words': 284}