<commit_before>var defaultFramework = "bootstrap3";

AutoForm = function(schema) {
  var self = this;
  if (schema instanceof SimpleSchema) {
    self._simpleSchema = schema;
  } else if ("Collection2" in Meteor && schema instanceof Meteor.Collection2) {
    self._collection = schema;
    self._simpleSchema = self._collection.simpleSchema();
  } else {
    self._simpleSchema = new SimpleSchema(schema);
  }

  self._hooks = {
    before: {
      //insert, update, remove, "methodName"
    },
    after: {
      //insert, update, remove, "methodName"
    },
    formToDoc: null,
    docToForm: null,
    onSubmit: null,
    onSuccess: null,
    onError: null
  };
};

AutoForm.prototype.simpleSchema = function() {
  return this._simpleSchema;
};

AutoForm.prototype.hooks = function(hooks) {
  _.extend(this._hooks, hooks);
};

AutoForm.resetForm = function(formID, simpleSchema) {
  if (typeof formID !== "string") {
    return;
  }

  simpleSchema && simpleSchema.namedContext(formID).resetValidation();
  clearSelections(formID);
};

if (typeof Handlebars !== 'undefined') {
  Handlebars.registerHelper("autoForm", function(options) {
    var hash = (options || {}).hash || {};

    //copy hash from quickForm if this is an autoForm created by the quickForm helper
    if (hash.qfHash) {
      hash = hash.qfHash;
    }

    var schemaObj = getAutoForm(hash.schema);

    var flatDoc;
    if (hash.doc) {
      var mDoc = new MongoObject(hash.doc);
      flatDoc = mDoc.getFlatObject();
      mDoc = null;

      var docToForm = schemaObj._hooks.docToForm || schemaObj.docToForm;
      if (typeof docToForm === "function") {
        flatDoc = docToForm(flatDoc);
      }
    } else {
      flatDoc = {};
    }

    // Set up the context to be used for everything within the autoform
    var autoFormContext = {};

    // If a custom context was supplied, keep it
    autoFormContext.context = (this instanceof Window) ? {} : _.clone(this);
    autoFormContext.context._afObj = schemaObj;
    autoFormContext.context._ss = schemaObj.simpleSchema();
    autoFormContext.context._doc = hash.doc;
    autoFormContext.context._flatDoc = flatDoc;
    autoFormContext.context._framework = hash.framework || defaultFramework;
    autoFormContext.context._validationType = hash.validation || "submitThenKeyup";

    // Remove from hash everything that we don't want as a form attribute
    hash = cleanObj(hash, ["__content", "schema", "validation", "framework", "doc", "qfHash"]);

    autoFormContext.atts = hash.atts || hash;
    //formID is used to track input selections so that they are retained
    //when the form is rerendered. If the id attribute is not provided,
    //we use a generic ID, which will usually still result in retension
    //of values, but might not work properly if any forms have input
    //elements (schema keys) with the same name
    autoFormContext.context._formID = autoFormContext.atts.id || "_afGenericID";

    return Template._autoForm.withData(autoFormContext);
  });

  Handlebars.registerHelper("quickForm", function(options) {
    var hash = (options || {}).hash || {};

    var autoForm = getAutoForm(hash.schema);

    var context = {
      formFields: []
    };

    // Look through all fields defined in the schema, and make
    // a list of those that should be on the form.
    _.each(autoForm.simpleSchema().schema(), function(fieldDefs, field) {
      var info = {name: field};

      if ((fieldDefs.denyInsert && hash.type === "insert") ||
              (fieldDefs.denyUpdate && hash.type === "update")) {
        return;
      }
      if (_.isArray(fieldDefs.allowedValues)) {
        info.options = "allowed";
      }
      context.formFields.push(info);
    });

    // Prepare additional form customizations
    context.buttonClasses = hash.buttonClasses || "";
    context.buttonContent = hash.buttonContent || "Submit";
    context.needsButton = true;

    // Determine what type of form to create
    switch (hash.type) {
      case "insert":
      case "update":
      case "remove":
        context.buttonClasses += " " + hash.type;
        break;

      case "method":
        context.method = hash.method;
        break;

      case "readonly":
        context.isReadOnly = true;
        context.needsButton = false;
        break;

      case "disabled":
        context.isDisabled = true;
        context.needsButton = false;
        break;

      default:

        break;
    }

    // Remove from hash everything that we don't want passed along to autoform
    hash = cleanObj(hash, ["__content", "buttonContent", "buttonClasses", "method", "type"]);

    // Store the hash for the autoForm helper to use
    context.qfHash = hash;

    return Template._quickForm.withData(context);
  });

  Template._autoForm.afQuickField = function(name, options) {
    var hash = (options || {}).hash || {};
    var afContext = hash.autoform || this, ss = afContext._ss;
    if (!ss)
      throw new Error("afQuickField helper must be used within an autoForm block");

    var defs = getDefs(ss, name); //defs will not be undefined<commit_msg>WIP implementation of the migration feature See issue #66<commit_after>var defaultFramework = "bootstrap3";

AutoForm = function(schema) {
  var self = this;
  if (schema instanceof SimpleSchema) {
    self._simpleSchema = schema;
  } else if ("Collection2" in Meteor && schema instanceof Meteor.Collection2) {
    self._collection = schema;
    self._simpleSchema = self._collection.simpleSchema();
  } else {
    self._simpleSchema = new SimpleSchema(schema);
  }

  self._hooks = {
    before: {
      //insert, update, remove, "methodName"
    },
    after: {
      //insert, update, remove, "methodName"
    },
    formToDoc: null,
    docToForm: null,
    onSubmit: null,
    onSuccess: null,
    onError: null
  };
};

AutoForm.prototype.simpleSchema = function() {
  return this._simpleSchema;
};

AutoForm.prototype.hooks = function(hooks) {
  _.extend(this._hooks, hooks);
};

AutoForm.resetForm = function(formID, simpleSchema) {
  if (typeof formID !== "string") {
    return;
  }

  simpleSchema && simpleSchema.namedContext(formID).resetValidation();
  clearSelections(formID);
};

if (typeof Handlebars !== 'undefined') {
  Handlebars.registerHelper("autoForm", function(options) {
    var hash = (options || {}).hash || {};

    //copy hash from quickForm if this is an autoForm created by the quickForm helper
    if (hash.qfHash) {
      hash = hash.qfHash;
    }

    var schemaObj = getAutoForm(hash.schema);

    // Set up the context to be used for everything within the autoform
    var autoFormContext = {};
    autoFormContext.context = (this instanceof Window) ? {} : _.clone(this);
    
    //formID is used to track input selections so that they are retained
    //when the form is rerendered. If the id attribute is not provided,
    //we use a generic ID, which will usually still result in retension
    //of values, but might not work properly if any forms have input
    //elements (schema keys) with the same name
    autoFormContext.context._formID = autoFormContext.atts.id || "_afGenericID";

    // Migration: get the doc after "hot code push"
    if (Package.reload) {
      var migrationId = 'autoform_' + autoFormContext.context._formID;
      hash.doc = Package.reload.Reload._migrationData(migrationId).doc;
    }

    var flatDoc;
    if (hash.doc) {
      var mDoc = new MongoObject(hash.doc);
      flatDoc = mDoc.getFlatObject();
      mDoc = null;

      var docToForm = schemaObj._hooks.docToForm;
      if (typeof docToForm === "function") {
        flatDoc = docToForm(flatDoc);
      }
    } else {
      flatDoc = {};
    }

    // If a custom context was supplied, keep it
    autoFormContext.context._afObj = schemaObj;
    autoFormContext.context._ss = schemaObj.simpleSchema();
    autoFormContext.context._doc = hash.doc;
    autoFormContext.context._flatDoc = flatDoc;
    autoFormContext.context._framework = hash.framework || defaultFramework;
    autoFormContext.context._validationType = hash.validation || "submitThenKeyup";

    // Remove from hash everything that we don't want as a form attribute
    hash = cleanObj(hash, ["__content", "schema", "validation", "framework", "doc", "qfHash"]);

    autoFormContext.atts = hash.atts || hash;

    var template = Template._autoForm.withData(autoFormContext);

    // Migration: save the doc before "hode code push"
    if (Package.reload) {
      Package.reload.Reload._onMigrate(migrationId, function () {
        return [true, {doc: formValues(template, schemaObj._hooks.formToDoc)}];
      });
    }

    return template;
  });

  Handlebars.registerHelper("quickForm", function(options) {
    var hash = (options || {}).hash || {};

    var autoForm = getAutoForm(hash.schema);

    var context = {
      formFields: []
    };

    // Look through all fields defined in the schema, and make
    // a list of those that should be on the form.
    _.each(autoForm.simpleSchema().schema(), function(fieldDefs, field) {
      var info = {name: field};

      if ((fieldDefs.denyInsert && hash.type === "insert") ||
              (fieldDefs.denyUpdate && hash.type === "update")) {
        return;
      }
      if (_.isArray(fieldDefs.allowedValues)) {
        info.options = "allowed";
      }
      context.formFields.push(info);
    });

    // Prepare additional form customizations
    context.buttonClasses = hash.buttonClasses || "";
    context.buttonContent = hash.buttonContent || "Submit";
    context.needsButton = true;

    // Determine what type of form to create
    switch (hash.type) {
      case "insert":
      case "update":
      case "remove":
        context.buttonClasses += " " + hash.type;
        break;

      case "method":
        context.method = hash.method;
        break;

      case "readonly":
        context.isReadOnly = true;
        context.needsButton = false;
        break;

      case "disabled":
        context.isDisabled = true;
        context.needsButton = false;
        break;

      default:

        break;
    }

    // Remove from hash everything that we don't want passed along to autoform
    hash = cleanObj(hash, ["__content", "buttonContent", "buttonClasses", "method", "type"]);

    // Store the hash for the autoForm helper to use
    context.qfHash = hash;

    return Template._quickForm.withData(context);
  });

  Template._autoForm.destroyed = function () {
    var self = this;
    if (Package.reload) {
      formId = self.firstNode.id;
      // We need to unset the migration data here. Something like:
      // Package.reload.Reload._onMigrate('autoform_' + formId, null);
      // Meteor core Pull Request?
    }
  };

  Template._autoForm.afQuickField = function(name, options) {
    var hash = (options || {}).hash || {};
    var afContext = hash.autoform || this, ss = afContext._ss;
    if (!ss)
      throw new Error("afQuickField helper must be used within an autoForm block");

    var defs = getDefs(ss, name); //defs will not be undefined
{'fraction_non_alphanumeric': 0.11995647837519267, 'fraction_numerical': 0.0007253604134554357, 'mean_word_length': 2.8797045374604293, 'pattern_counts': {'":': 12, '<': 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': '6111422', 'n_tokens_mistral': 3232, 'n_tokens_neox': 3155, 'n_words': 904}