pattern-view.js 4.33 KB
var main_field = 'name',
initialColModel = [
  {name:'name', index:'name', width:150},
  {name:'type', index:'type', width:70},
  {name:'part_of_speech', index:'part_of_speech', width:200}
],
initialColNames = ['Id', 'Typ', 'Cz. mowy'],
initial_sort_rules = [{field: main_field, order: 'asc'}],
grid_caption = "Wzory",
edit_form_id = 'pattern-edit-form',
edit_form_cancel_id = 'pattern-edit-cancel',
edit_form_submit_id = 'pattern-edit-submit',
deleted,
qualifier_options = { // copypasta z leksemów...
  noneSelectedText: 'Wybierz kwalifikatory',
  selectedText: '# kwalifikatorów',
  selectedList: 4,
  header: false,
  create: function() {
    var select = $(this);
    var selected = select.val();
    if (selected) {
      $.each(selected, function(i, value) {
        toggle_excluded(select, value, false);
      });
    }
  },
  click: function(event, ui) {
    toggle_excluded(this, ui.value, !ui.checked);
  }
},
reverse_exclusion_classes = {};

for (ec in exclusion_classes) {
  $.each(exclusion_classes[ec], function(i, q_id) {
    reverse_exclusion_classes[q_id] = ec;
  });
}

// duuuużo copypasty :(
function toggle_excluded(select, value, enable) {
  var ec = reverse_exclusion_classes[Number(value)],
      values = exclusion_classes[ec];
  if (values)
    $.each(values, function(i, v) {
      if (v != Number(value))
        multiselect_toggle(select, v, enable);
    });
};

function init_form_widgets() {
  $('span.remove').live('click', function() {
    li = $(this).closest('li');
    name = li.find('input')[0].id;
    if (name.split('-')[1] != 'add')
      deleted.push(name.split('-')[1]);
    li.remove();
    show_changed();
  });
  $('.add-ending').live('click', function() {
    var id = $('input[name=id]', $(this).closest('form')).value();
    var new_row = $(get_new_row_html());
    $(this).closest('tr').find('.ending-group').append(new_row);
    $('#ending-list .qualifiers').last().multiselect(qualifier_options);
    show_changed();
  });
}

function load_content(id) {
  $('#edit').load(ajax_edit_form, 'id='+id, edit_form_init);
  grid.jqGrid('setGridParam', {'lastSelectedId' : id});
}

function edit_form_init() {
  $('button', '#edit').button();
  $('#ending-list.editable .ending-group').sortable().sortable({
    change: function(event, ui) {
      show_changed();
    }
  });
  //$('#ending-list').disableSelection();
  $('#ending-list .qualifiers').multiselect(qualifier_options);
  deleted = [];
  hide_changed();
  if (ctrl)
    ctrl.remove();
}

function edit_form_submit() {
  this_form = $(this);
  form_data = this_form.serializeArray();
  form_data.push({name: 'deleted', value: deleted});
  deleted = [];
  var ending_list = [];
  var rows = $('#ending-list tr');
  $.each(rows, function(i, row){
    var label = $(row).find('td:eq(0) strong').html();
    var endings = [];
    $(row).find('td:eq(1) input').map(function(i, input) {
      var id = $(input).attr('id').split('-')[1], q_id;
      if (id == 'add')
        q_id = '#id_add-' + $(input).attr('id').split('-')[2] + '-qualifiers';
      else
        q_id = '#id_end' + id + '-qualifiers';
      endings.push({
        id: id,
        string: $(input).val(),
        qualifiers: $(q_id).val() || []
      });
    });
    ending_list.push({base_form_label: label, endings: endings});
  });
  form_data.push({name: 'ending_list', value: ending_list})
  ctrl = getBusyOverlay(
    'viewport',
    {color: 'black', opacity: 0.5},
    {size: 100});
  $.ajaxJSON({
    method: 'post',
    url: ajax_update_pattern,
    data: {
      form_data: form_data
    },
    description: "Zapisanie zmian",
    callback: function() {
      hide_changed();
      grid.jqGrid('setGridParam', {'setOnComplete' : true});
      grid.trigger("reloadGrid");
    },
    error_callback: function(xhr, status, error) {
      error_alert(status + ': ' + error);
      if (ctrl)
        ctrl.remove();
    },
    bad_data_callback: function(result) {
      if (ctrl)
        ctrl.remove();
      return true;
    }
  });
  return false;
}

var new_row_counter = 1; // potrzebne tylko dla niepowtarzalności idów
function get_new_row_html() {
  var new_row_html = $.ajax({
    type: 'get',
    url: ajax_new_ending_row,
    data: {},
    async: false // w zasadzie się tak nie powinno robić
  }).responseText;
  var row_html = new_row_html.replace(/NUM/g, new_row_counter);
  new_row_counter++;
  return row_html;
}

function add_buttons(f) {
}