<commit_before>/*global ajax401: false, prefix: false, updateAjaxURL: false, traveler: true, FormLoader: false, moment: false*/
/*global previewColumn: false, referenceFormLinkColumn: false, aliasColumn: false, activatedOnColumn: false, sDomClean: false, titleColumn: false, updatedOnColumn: false, formColumn: false, sDomPage: false, fnAddFilterFoot: false, filterEvent: false*/
/*eslint max-nested-callbacks: [2, 4]*/

function findById(a, id) {
  var i;
  for (i = 0; i < a.length; i += 1) {
    if (a[i]._id === id) {
      return a[i];
    }
  }
  return null;
}

function setAlias(fid, alias, updateTd) {
  $.ajax({
    url: './forms/' + fid + '/alias',
    type: 'PUT',
    contentType: 'application/json',
    data: JSON.stringify({
      value: alias
    })
  }).done(function () {
    $('#modal .modal-body').append('<div class="text-success">The new alias was set.</div>');
    // update local table data
    updateTd();
  }).fail(function (jqXHR) {
    $('#modal .modal-body').append('<div class="text-error">Something was wrong: ' + jqXHR.responseText + '</div>');
  });
}

function addForm(form, cb) {
  $.ajax({
    url: './forms/',
    type: 'POST',
    contentType: 'application/json',
    dataType: 'json',
    data: JSON.stringify(form),
    processData: false
  }).done(function (json) {
    $('#modal .modal-body').append('<div class="text-success">The selected form is active now.</div>');
    cb(json);
  }).fail(function (jqXHR) {
    $('#modal .modal-body').append('<div class="text-error">Something was wrong: ' + jqXHR.responseText + '</div>');
  });
}

function setActive(fid, cb) {
  $.ajax({
    url: './forms/active',
    type: 'PUT',
    contentType: 'application/json',
    dataType: 'json',
    data: JSON.stringify({
      formid: fid
    }),
    processData: false
  }).done(function (json) {
    $('#modal .modal-body').append('<div class="text-success">The selected form is active now.</div>');
    cb(json);
  }).fail(function (jqXHR) {
    $('#modal .modal-body').append('<div class="text-error">Something was wrong: ' + jqXHR.responseText + '</div>');
  });
}


function initUsedForms(traveler, activeTable, usedTable) {
  var form;
  if (traveler.forms.length === 1) {
    form = traveler.forms[0];
  } else {
    form = findById(traveler.forms, traveler.activeForm);
  }
  if (form) {
    var active = {
      activatedOn: form.activatedOn.length ? form.activatedOn : [traveler.createdOn],
      _id: form._id,
      reference: form.reference || traveler.referenceForm,
      alias: form.alias || 'not set yet'
    };
    activeTable.fnClearTable();
    activeTable.fnAddData(active);
  }
  var used = [];
  traveler.forms.forEach(function (value) {
    if (value._id !== traveler.activeForm) {
      value.activatedOn = value.activatedOn.length ? value.activatedOn : [traveler.createdOn];
      value.reference = value.reference || traveler.referenceForm;
      used.push(value);
    }
  });
  usedTable.fnClearTable();
  usedTable.fnAddData(used);
  $('tr').removeClass('row-selected');
  $('#active-form tbody tr:first-child').addClass('row-selected');
}


$(function () {

  ajax401(prefix);

  updateAjaxURL(prefix);

  var activeColumns = [previewColumn, aliasColumn, activatedOnColumn, referenceFormLinkColumn];
  var activeTable = $('#active-form').dataTable({
    aaData: [],
    bAutoWidth: true,
    aoColumns: activeColumns,
    sDom: sDomClean
  });

  var usedColumns = activeColumns;

  fnAddFilterFoot('#used-forms', usedColumns);
  var usedTable = $('#used-forms').dataTable({
    aaData: [],
    bAutoWidth: true,
    aoColumns: usedColumns,
    sDom: sDomPage
  });
<commit_msg>handle only one form and activeForm is not _id case<commit_after>/*global ajax401: false, prefix: false, updateAjaxURL: false, traveler: true, FormLoader: false, moment: false*/
/*global previewColumn: false, referenceFormLinkColumn: false, aliasColumn: false, activatedOnColumn: false, sDomClean: false, titleColumn: false, updatedOnColumn: false, formColumn: false, sDomPage: false, fnAddFilterFoot: false, filterEvent: false*/
/*eslint max-nested-callbacks: [2, 4]*/

function findById(a, id) {
  var i;
  for (i = 0; i < a.length; i += 1) {
    if (a[i]._id === id) {
      return a[i];
    }
  }
  return null;
}

function setAlias(fid, alias, updateTd) {
  $.ajax({
    url: './forms/' + fid + '/alias',
    type: 'PUT',
    contentType: 'application/json',
    data: JSON.stringify({
      value: alias
    })
  }).done(function () {
    $('#modal .modal-body').append('<div class="text-success">The new alias was set.</div>');
    // update local table data
    updateTd();
  }).fail(function (jqXHR) {
    $('#modal .modal-body').append('<div class="text-error">Something was wrong: ' + jqXHR.responseText + '</div>');
  });
}

function addForm(form, cb) {
  $.ajax({
    url: './forms/',
    type: 'POST',
    contentType: 'application/json',
    dataType: 'json',
    data: JSON.stringify(form),
    processData: false
  }).done(function (json) {
    $('#modal .modal-body').append('<div class="text-success">The selected form is active now.</div>');
    cb(json);
  }).fail(function (jqXHR) {
    $('#modal .modal-body').append('<div class="text-error">Something was wrong: ' + jqXHR.responseText + '</div>');
  });
}

function setActive(fid, cb) {
  $.ajax({
    url: './forms/active',
    type: 'PUT',
    contentType: 'application/json',
    dataType: 'json',
    data: JSON.stringify({
      formid: fid
    }),
    processData: false
  }).done(function (json) {
    $('#modal .modal-body').append('<div class="text-success">The selected form is active now.</div>');
    cb(json);
  }).fail(function (jqXHR) {
    $('#modal .modal-body').append('<div class="text-error">Something was wrong: ' + jqXHR.responseText + '</div>');
  });
}


function initUsedForms(traveler, activeTable, usedTable) {
  var form;
  if (traveler.forms.length === 1) {
    form = traveler.forms[0];
  } else {
    form = findById(traveler.forms, traveler.activeForm);
  }
  if (form) {
    var active = {
      activatedOn: form.activatedOn.length ? form.activatedOn : [traveler.createdOn],
      _id: form._id,
      reference: form.reference || traveler.referenceForm,
      alias: form.alias || 'not set yet'
    };
    activeTable.fnClearTable();
    activeTable.fnAddData(active);
  }
  var used = [];
  if (traveler.forms.length > 1) {
    traveler.forms.forEach(function (value) {
      if (value._id !== traveler.activeForm) {
        value.activatedOn = value.activatedOn.length ? value.activatedOn : [traveler.createdOn];
        value.reference = value.reference || traveler.referenceForm;
        used.push(value);
      }
    });
  }
  usedTable.fnClearTable();
  usedTable.fnAddData(used);
  $('tr').removeClass('row-selected');
  $('#active-form tbody tr:first-child').addClass('row-selected');
}


$(function () {

  ajax401(prefix);

  updateAjaxURL(prefix);

  var activeColumns = [previewColumn, aliasColumn, activatedOnColumn, referenceFormLinkColumn];
  var activeTable = $('#active-form').dataTable({
    aaData: [],
    bAutoWidth: true,
    aoColumns: activeColumns,
    sDom: sDomClean
  });

  var usedColumns = activeColumns;

  fnAddFilterFoot('#used-forms', usedColumns);
  var usedTable = $('#used-forms').dataTable({
    aaData: [],
    bAutoWidth: true,
    aoColumns: usedColumns,
    sDom: sDomPage
  });

{'fraction_non_alphanumeric': 0.17487450820784153, 'fraction_numerical': 0.003391670058336725, 'mean_word_length': 3.5675340768277573, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 28, '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': '7039415', 'n_tokens_mistral': 2439, 'n_tokens_neox': 2392, 'n_words': 536}