<commit_before>/**
 * Module dependencies.
 */

var autosubmit = require('autosubmit');
var autovalidate = require('autovalidate');
var log = require('debug')('democracyos:form-view');
var inherit = require('inherit');
var isMobile = require('is-mobile')();
var o = require('dom');
var spin = require('spin');
var t = require('t');
var Tip = require('tip');
var validate = require('validate');
var View = require('view');

/**
 * Expose FormView.
 */

module.exports = FormView;

/**
 * Base Form View
 *
 * @return {FormView} `FormView` instance.
 * @api public
 */

function FormView(template, options) {
  if (!(this instanceof FormView))
    return inherit(template, FormView);

  View.call(this, template, options);

  this.autovalidate('form[autovalidate]');
  this.autosubmit('form[autosubmit]');
  this.on('insert', this.bound('oninsert'));
  this.messages();
}

View(FormView);

FormView.prototype.oninsert = function() {
  this.on('request', this.bound('loading'));
  this.on('response', this.bound('unloading'));
  this.on('response', this.bound('response'));
};

FormView.prototype.removeMessages = function() {
  this.find('li.msg').remove();
};

FormView.prototype.errors = function(msg) {
  this.messages(msg, 'error');
};

FormView.prototype.success = function(msg) {
  this.messages(msg, 'success');
};

FormView.prototype.messages = function(messages, type, fade) {
  this.removeMessages();
  messages = messages || [];
  if ('string' === typeof messages) {
    messages = [messages];
  }
  var ul = this.find('.form-messages');
  if (!ul.length) {
    ul = o('<ul class="form-messages">');
    this.find('form').prepend(ul);
  }

  messages.forEach(function (msg) {
    var li = o(document.createElement('li'));
    li
      .addClass('msg')
      .addClass(type || null)
      .addClass(fade ? 'fade' : null)
      .html(msg);
    ul.append(li);
  });
};

FormView.prototype.disable = function() {
  this.disabled = true;
  validate.disable(this.el);
};

FormView.prototype.enable = function() {
  this.disabled = false;
  validate.enable(this.el);
};

FormView.prototype.spin = function() {
  var but = this.find('button');
  if (!but.length)
    return;
  var self = this;
  this.spinTimer = setTimeout(function() {
    self.spinner = spin(but[0], {size: 20}).light();
    but.addClass("spin")
  }, 500);
};

FormView.prototype.unspin = function() {
  clearTimeout(this.spinTimer);
  if (!this.spinner)
    return;
  this.find('button').removeClass('spin');
  this.spinner.remove();
  this.spinner = null;
};

FormView.prototype.loading = function() {
  var self = this;
  this.disable();
  this.messageTimer = setTimeout(function() {
    self.messages(t('Please wait'), 'sending');
    self.spin();
    self.find('a.cancel').addClass('enabled')
  }, 1000);
};

FormView.prototype.unloading = function() {
  clearTimeout(this.messageTimer);
  this.removeMessages();
  this.unspin();
  this.enable();
  this.find('a.cancel').removeClass('enabled')
};

FormView.prototype.response = function(err, res) {
  if (err) {
    return this.errors([err]);
  };
  if (!res.ok) {
    return this.errors([JSON.parse(res.text).error]);
  };
  if (res.body && res.body.error) {
    return this.errors([err || res.body.error]);
  };

  this.emit('success', res);
};

FormView.prototype.destroy = function() {
  clearTimeout(this.messageTimer);
  this.unspin();
};

FormView.prototype.field = function(name) {
  var strfind = 'input[name="' + name + '"], textarea[name="' + name + '"]';
  return this.find(strfind);
};

FormView.prototype.get = function(name) {
  var el = this.field(name);
  var chk = 'checkbox' == el.attr('type');<commit_msg>Fix FormView error handling<commit_after>/**
 * Module dependencies.
 */

var autosubmit = require('autosubmit');
var autovalidate = require('autovalidate');
var log = require('debug')('democracyos:form-view');
var inherit = require('inherit');
var isMobile = require('is-mobile')();
var o = require('dom');
var spin = require('spin');
var t = require('t');
var Tip = require('tip');
var validate = require('validate');
var View = require('view');

/**
 * Expose FormView.
 */

module.exports = FormView;

/**
 * Base Form View
 *
 * @return {FormView} `FormView` instance.
 * @api public
 */

function FormView(template, options) {
  if (!(this instanceof FormView))
    return inherit(template, FormView);

  View.call(this, template, options);

  this.autovalidate('form[autovalidate]');
  this.autosubmit('form[autosubmit]');
  this.on('insert', this.bound('oninsert'));
  this.messages();
}

View(FormView);

FormView.prototype.oninsert = function() {
  this.on('request', this.bound('loading'));
  this.on('response', this.bound('unloading'));
  this.on('response', this.bound('response'));
};

FormView.prototype.removeMessages = function() {
  this.find('li.msg').remove();
};

FormView.prototype.errors = function(msg) {
  this.messages(msg, 'error');
};

FormView.prototype.success = function(msg) {
  this.messages(msg, 'success');
};

FormView.prototype.messages = function(messages, type, fade) {
  this.removeMessages();
  messages = messages || [];
  if ('string' === typeof messages) {
    messages = [messages];
  }
  var ul = this.find('.form-messages');
  if (!ul.length) {
    ul = o('<ul class="form-messages">');
    this.find('form').prepend(ul);
  }

  messages.forEach(function (msg) {
    var li = o(document.createElement('li'));
    li
      .addClass('msg')
      .addClass(type || null)
      .addClass(fade ? 'fade' : null)
      .html(msg);
    ul.append(li);
  });
};

FormView.prototype.disable = function() {
  this.disabled = true;
  validate.disable(this.el);
};

FormView.prototype.enable = function() {
  this.disabled = false;
  validate.enable(this.el);
};

FormView.prototype.spin = function() {
  var but = this.find('button');
  if (!but.length)
    return;
  var self = this;
  this.spinTimer = setTimeout(function() {
    self.spinner = spin(but[0], {size: 20}).light();
    but.addClass("spin")
  }, 500);
};

FormView.prototype.unspin = function() {
  clearTimeout(this.spinTimer);
  if (!this.spinner)
    return;
  this.find('button').removeClass('spin');
  this.spinner.remove();
  this.spinner = null;
};

FormView.prototype.loading = function() {
  var self = this;
  this.disable();
  this.messageTimer = setTimeout(function() {
    self.messages(t('Please wait'), 'sending');
    self.spin();
    self.find('a.cancel').addClass('enabled')
  }, 1000);
};

FormView.prototype.unloading = function() {
  clearTimeout(this.messageTimer);
  this.removeMessages();
  this.unspin();
  this.enable();
  this.find('a.cancel').removeClass('enabled')
};

FormView.prototype.response = function(err, res) {
  if (err) {
    return this.errors([err]);
  };
  if (!res.ok) {
    return this.errors([JSON.parse(res.text).error]);
  };
  if (res.body && res.body.error) {
    return this.errors([res.body.error]);
  };

  this.emit('success', res);
};

FormView.prototype.destroy = function() {
  clearTimeout(this.messageTimer);
  this.unspin();
};

FormView.prototype.field = function(name) {
  var strfind = 'input[name="' + name + '"], textarea[name="' + name + '"]';
  return this.find(strfind);
};

FormView.prototype.get = function(name) {
  var el = this.field(name);
  var chk = 'checkbox' == el.attr('type');
{'fraction_non_alphanumeric': 0.19433420008211305, 'fraction_numerical': 0.0027371014096072258, 'mean_word_length': 4.1683168316831685, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '4409458', 'n_tokens_mistral': 2441, 'n_tokens_neox': 2412, 'n_words': 451}