<commit_before>var form_auto_save = true;


function form_form(app, type, id) {
  var self = this;
  self.app = app;
  self.type = type;
  self.toString = function(){ return type; };

  var _meta = null;
  var _modules = null;
  var _related = null;
  var _id = id;
  var _created = null;
  var _modified = null;
  var _state = null;
  var _idx = {};
  var _dirty = false;
  var _logs = [];

  form_make_listener(self);

  var $el = $$('form');
  self.$el = function () {
    return $el;
  };

  var $controls = $$('form-controls');
  self.$controls = function () {
    update_controls();
    return $controls;
  };

  var $info = $$('form-info');
  self.$info = function(){
    update_info();
    return $info;
  };

  var $form = $$('form', {parent: $el});

  function update_form() {
    $form.empty();
    var $t = $form;
    var s = [];
    for (var i = 0; i < _meta.length; i++) {
      var d = _meta[i];
      if (d.begin) {
        s.push($t);
        var $x = $("<div></div>");
        if (d.options && d.options.className)
          $x.addClass(d.options.className);
        if ($t.data('float'))
          $x.css({'float':'left'});
        if (d.begin == 'row')
          $x.data('float',true);
        $t.append($x);
        $t = $x;
      }
      else if (d.end) {
        $t = s.pop();
      }
      else {
       var f = create_field(d);
        var $fel = f.$el();
        if ($t.data('float'))
          $fel.css({'float':'left'});
        $t.append($fel);
        _idx[d.name] = f;
      }
    }
    $t.append("<div style='clear:both;'></div>");
  }

  function create_field(d) {
    var f = new indicated_field(d);
    f.field.form = self;
    if (f.field.init)
      f.field.init();
    f.add_listener('change', function () {
      _dirty = true;
      self.$save.prop('disabled', false);
      self.emit('change');
      if (form_auto_save)
        save(4000);
    });
    // for reference fields
    f.add_listener('add', function (f) {
      if (d.options && d.options.type)
        self.emit('create', {type: d.options.type, field: f.field});
    });
    f.add_listener('browse', function (f) {
      if (d.options && d.options.type)
        self.emit('browse', {type: d.options.type, field: f.field})
    });
    f.add_listener('select', function(f, o){
      if (d.options && d.options.type)
        self.emit('select', {type: d.options.type, id: o._id, field: f.field});
    })
    return f;
  }

  function update_controls()
  {
    $controls.empty();
    //  var $title = $$('title', {el: 'span', parent: $controls}).text(type);
    //    var $cancel = $$('btn btn-primary', {el: 'button', parent: $controls}).text('CLOSE');
    //    $cancel.click(function(){
    //      self.emit('close', self.data);
    //    });
    self.$save = $$('save', {el: 'button', parent: $controls}).text('SAVE');
    if (!_dirty)
      self.$save.prop('disabled', true);
    self.$time = $$('time', {el: 'span', parent: $controls});
    self.$save.click(function () {
      save(0);
    });
    if (_modified)
      self.$time.text(' Last modified ' + timeSince(new Date(_modified)) + '.');
    else
      self.$time.text(' New record.');
  }

  var save_delayed = -1;
  function save(delay) {

    function do_save() {
      if (!_dirty) return;
      console.log(Date.now(), "save", self.type);
      $.ajax({
        url: self.url(),
        data: { val: JSON.stringify(self.data) },
        method: 'post',
        success: function (o) {
          if (o.name && o.name.indexOf("Error") != -1)
            self.error(o);
          else {<commit_msg>deep fields - do we ever go deeper?<commit_after>var form_auto_save = true;


function form_form(app, type, id) {
  var self = this;
  self.app = app;
  self.type = type;
  self.toString = function(){ return type; };

  var _meta = null;
  var _modules = null;
  var _related = null;
  var _id = id;
  var _created = null;
  var _modified = null;
  var _state = null;
  var _idx = {};
  var _dirty = false;
  var _logs = [];

  form_make_listener(self);

  var $el = $$('form');
  self.$el = function () {
    return $el;
  };

  var $controls = $$('form-controls');
  self.$controls = function () {
    update_controls();
    return $controls;
  };

  var $info = $$('form-info');
  self.$info = function(){
    update_info();
    return $info;
  };

  var $form = $$('form', {parent: $el});

  function update_form() {
    $form.empty();
    var $t = $form;
    var s = [];
    for (var i = 0; i < _meta.length; i++) {
      var d = _meta[i];
      if (d.begin) {
        s.push($t);
        var $x = $("<div></div>");
        if (d.options && d.options.className)
          $x.addClass(d.options.className);
        if ($t.data('float'))
          $x.css({'float':'left'});
        if (d.begin == 'row')
          $x.data('float',true);
        $t.append($x);
        $t = $x;
      }
      else if (d.end) {
        $t = s.pop();
      }
      else {
       var f = create_field(d);
        var $fel = f.$el();
        if ($t.data('float'))
          $fel.css({'float':'left'});
        $t.append($fel);
        _idx[d.name] = f;
      }
    }
    $t.append("<div style='clear:both;'></div>");
  }

  function create_field(d) {
    var f = new indicated_field(d);
    f.field.form = self;
    if (f.field.init)
      f.field.init();
    f.add_listener('change', function () {
      _dirty = true;
      self.$save.prop('disabled', false);
      self.emit('change');
      if (form_auto_save)
        save(4000);
    });
    // for reference fields
    f.add_listener('add', function (f) {
      if (d.options && d.options.type)
        self.emit('create', {type: d.options.type, field: f.field});
    });
    f.add_listener('browse', function (f) {
      if (d.options && d.options.type)
        self.emit('browse', {type: d.options.type, field: f.field})
    });
    f.add_listener('select', function(f, o){
      if (d.options && d.options.type)
        self.emit('select', {type: d.options.type, id: o._id, field: f.field});
    })
    return f;
  }

  function update_controls()
  {
    $controls.empty();
    //  var $title = $$('title', {el: 'span', parent: $controls}).text(type);
    //    var $cancel = $$('btn btn-primary', {el: 'button', parent: $controls}).text('CLOSE');
    //    $cancel.click(function(){
    //      self.emit('close', self.data);
    //    });
    self.$save = $$('save', {el: 'button', parent: $controls}).text('SAVE');
    if (!_dirty)
      self.$save.prop('disabled', true);
    self.$time = $$('time', {el: 'span', parent: $controls});
    self.$save.click(function () {
      save(0);
    });
    if (_modified)
      self.$time.text(' Last modified ' + timeSince(new Date(_modified)) + '.');
    else
      self.$time.text(' New record.');
  }

  var save_delayed = -1;
  function save(delay) {

    function do_save() {
      if (!_dirty) return;
      console.log(Date.now(), "save", self.type);
      var d = self.data;
      for (var n in d) {
        var a = n.split('.');
        if (a.length == 2) {
          var p = a[0];
          if (!d[p])
            d[p] = {};
          d[p][a[1]] = d[n];
          d[n] = null;
        }
      }
      $.ajax({
        url: self.url(),
        data: { val: JSON.stringify(d) },
        method: 'post',
        success: function (o) {
          if (o.name && o.name.indexOf("Error") != -1)
            self.error(o);
          else {
{'fraction_non_alphanumeric': 0.20273379347678983, 'fraction_numerical': 0.002571389903911219, 'mean_word_length': 2.136672325976231, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 11, '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': '4972197', 'n_tokens_mistral': 2609, 'n_tokens_neox': 2572, 'n_words': 578}