Sha256: e2ce89bc189a760ac240511b326c12ae56a47f06dcdac6db2c1e75a58908ae47

Contents?: true

Size: 1.55 KB

Versions: 4

Compression:

Stored size: 1.55 KB

Contents

function new_form_field(type) {
  if ($.inArray(type, ['text_field', 'select', 'multiselect', 'date_field', 'radio', 'check_box']) == -1) {
    alert('Unknown field type.');
  } else {
    // find an index for the new form field (largest index plus one)
    var new_index = 0;
    $("#form-fields input[name$='[_type]']").each(function(i, el) {
      num = parseInt(el.id.replace(/form_fields_(\d+)__type/i, "$1"));
      if (new_index < num) new_index = num;
    });

    $.get('/admin/fields/new/', {
      'type': type,
      'index': (new_index + 1)
    },
    function(data) {
      $('#form-fields table tbody').append(data);
      setup_tooltips();
    });
  }

  return false;
}

function delete_form_field(button) {
  if (confirm('Are you sure?')) {
    $(button).closest('tr').remove();
  }
  return false;
}

function move_form_field(event) {
  direction = $(this).text().toLowerCase();
  row = $(this).closest('tr');
  if (direction == 'up') {
    row.prev('tr').before(row);
  } else {
    row.next('tr').after(row);
  }
  // COrrect indexes
  $("#form-fields tbody tr").each(function(i, row) {
    $(row).find("input").each(function(ri, input) {
      $(input).attr('id',  $(input).attr('id').replace(/_\d+_/,"_"+i+"_"));
      $(input).attr('name',  $(input).attr('name').replace(/\[\d+\]/,"["+i+"]"));
    });
    $(row).find("label").each(function(li, label) {
      $(label).attr('for',  $(label).attr('for').replace(/_\d+_/,"_"+i+"_"));
    });
  });
}
$('#form-fields .page_up').live('click',move_form_field);
$('#form-fields .page_down').live('click',move_form_field);

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
noodall-form-builder-0.5.4 app/assets/javascripts/admin/formbuilder.js
noodall-form-builder-0.5.3 app/assets/javascripts/admin/formbuilder.js
noodall-form-builder-0.5.2 app/assets/javascripts/admin/formbuilder.js
noodall-form-builder-0.5.0 app/assets/javascripts/admin/formbuilder.js