<commit_before>'use strict';

angular.module('arethusa.morph').directive('morphFormCreate', [
  'morph',
    function(morph) {
    return {
      restrict: 'E',
      scope: {
        token: '=morphToken'
      },
      link: function (scope, element, attrs) {
        var inArray = arethusaUtil.isIncluded;

        scope.m = morph;
        scope.form = scope.token.customForm;
        scope.forms = scope.token.forms;

        function dependencyMet(dependencies) {
          if (!dependencies) {
            return true;
          }
          var ok = true;
          for (var k in dependencies) {
            var depArray = dependencies[k];
            if (!inArray(depArray, scope.form.attributes[k])) {
              ok = false;
              break;
            }
          }
          return ok;
        }

        function getVisibleAttributes() {
          return arethusaUtil.inject([], morph.postagSchema, function (memo, attr) {
            var ifDependencies = (morph.dependenciesOf(attr) || {}).if;
            if (dependencyMet(ifDependencies)) {
              memo.push(attr);
            }
          });
        }

        function setVisibleAttributes() {
          scope.visibleAttributes = getVisibleAttributes();
        }

        scope.reset = function() {
          morph.resetCustomForm(scope.token);
        };

        scope.save = function() {
          cleanUpAttributes();
          addOrigin();
          addForm();
          scope.reset();
        };

        // At the point of saving we have undefined values around in the
        // forms attributes - we clean them up as to not distort our output
        function cleanUpAttributes() {
          var cleanAttrs = arethusaUtil.inject({}, scope.visibleAttributes, function(memo, attr) {
            memo[attr] = scope.form.attributes[attr];
          });
          scope.form.attributes = cleanAttrs;
        }

        function addOrigin() {
          scope.form.origin = 'you';
        }

        function addForm() {
          scope.forms.push(angular.copy(scope.form));
        }

        // TBD
        //
        // save button to create the form formally
        // watch click events in upper scopes which
        // want to edit a form - replace the form in this scope
        // then and we're all good.

        scope.$watch('form.attributes', function (newVal, oldVal) {
          setVisibleAttributes();
        }, true);

        scope.$watch('token.customForm', function(newVal, oldVal) {
          scope.form = newVal;
        });
      },
      templateUrl: 'templates/morph_form_create.html'
    };
  }
]);<commit_msg>Remove obsolete comment in morphFormCreate<commit_after>'use strict';

angular.module('arethusa.morph').directive('morphFormCreate', [
  'morph',
    function(morph) {
    return {
      restrict: 'E',
      scope: {
        token: '=morphToken'
      },
      link: function (scope, element, attrs) {
        var inArray = arethusaUtil.isIncluded;

        scope.m = morph;
        scope.form = scope.token.customForm;
        scope.forms = scope.token.forms;

        function dependencyMet(dependencies) {
          if (!dependencies) {
            return true;
          }
          var ok = true;
          for (var k in dependencies) {
            var depArray = dependencies[k];
            if (!inArray(depArray, scope.form.attributes[k])) {
              ok = false;
              break;
            }
          }
          return ok;
        }

        function getVisibleAttributes() {
          return arethusaUtil.inject([], morph.postagSchema, function (memo, attr) {
            var ifDependencies = (morph.dependenciesOf(attr) || {}).if;
            if (dependencyMet(ifDependencies)) {
              memo.push(attr);
            }
          });
        }

        function setVisibleAttributes() {
          scope.visibleAttributes = getVisibleAttributes();
        }

        scope.reset = function() {
          morph.resetCustomForm(scope.token);
        };

        scope.save = function() {
          cleanUpAttributes();
          addOrigin();
          addForm();
          scope.reset();
        };

        // At the point of saving we have undefined values around in the
        // forms attributes - we clean them up as to not distort our output
        function cleanUpAttributes() {
          var cleanAttrs = arethusaUtil.inject({}, scope.visibleAttributes, function(memo, attr) {
            memo[attr] = scope.form.attributes[attr];
          });
          scope.form.attributes = cleanAttrs;
        }

        function addOrigin() {
          scope.form.origin = 'you';
        }

        function addForm() {
          scope.forms.push(angular.copy(scope.form));
        }

        scope.$watch('form.attributes', function (newVal, oldVal) {
          setVisibleAttributes();
        }, true);

        scope.$watch('token.customForm', function(newVal, oldVal) {
          scope.form = newVal;
        });
      },
      templateUrl: 'templates/morph_form_create.html'
    };
  }
]);
{'fraction_non_alphanumeric': 0.11530065489184362, 'fraction_numerical': 0.0, 'mean_word_length': 1.7272727272727273, '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': '822201', 'n_tokens_mistral': 1358, 'n_tokens_neox': 1324, 'n_words': 303}