lexeme-view.js 23.1 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818
var prompter_ctrl, pos_index, created;

function list_searchoptions(list) {
  return {
    searchhidden: true,
    sopt: ['eq', 'ne'],
    value: list
  };
};

function list_column(name, displayed, list) {
  return {
    name: name,
    index: name,
    hidden: true,
    hidedlg: !displayed,
    sortable: false,
    searchoptions: list_searchoptions(list),
    stype: 'select'
  };
};

function selected_qualifiers_text(num, total, checked) {
  if (num >= 4)
    return num + ' kwalifikatorów';
  grouped = {};
  $.each(checked, function(i, elem) {
    id = elem.value;
    vocab = vocabs[id];
    if (grouped[vocab] === undefined)
      grouped[vocab] = [];
    grouped[vocab].push(elem.title);
  });
  vocab_table = [];
  owner = $('#owner-vocabulary').text();
  if (grouped[owner])
    vocab_table.push(owner + ': ' + grouped[owner].join(', '));
  for (vocab in grouped) {
    if (vocab != owner)
      vocab_table.push(vocab + ': ' + grouped[vocab].join(', '));
  }
  return vocab_table.join('; ');
}

function selected_cvs_text(num, total, checked) {
  if (num >= 4)
    return num + ' wartości';
  var values = $.map(checked, function(elem) {
    return strip(elem.title);
  });
  return values.join(', ');
}

var main_field = 'entry',
lip_searchoptions = {
  searchhidden: true,
  sopt: ['eq', 'ne']//['se', 'sd', 'me', 'md']
},
initialColModel = [
  {name: 'id', index: 'id', width: 70, search: false, hidden: true},
  {name: 'entry',index: 'entry', width: 200},
  {
    name: 'part_of_speech',
    index: 'part_of_speech',
    searchoptions: list_searchoptions(parts_of_speech),
    stype: 'select'
  },
  {
    name: 'pattern_name',
    index: 'pattern_name',
    hidden: true,
    sortable: false,
    searchoptions: lip_searchoptions,
    width: 70
  },
  {
    name: 'pattern_count',
    index: 'pattern_count',
    hidden: true,
    hidedlg: true,
    sortable: false,
    searchoptions: {searchhidden: true, sopt: ['eq', 'ge', 'le']}
  },
  {
    name: 'inflection_characteristic',
    index: 'inflection_characteristic',
    hidden: true,
    sortable: false,
    searchoptions: lip_searchoptions
  },
  {
    name: 'ic_count',
    index: 'ic_count',
    hidden: true,
    hidedlg: true,
    sortable: false,
    searchoptions: {searchhidden: true, sopt: ['eq', 'ge', 'le']}
  },
  {
    name: 'form',
    index: 'form',
    hidden: true,
    hidedlg: true,
    sortable: false,
    searchoptions: {searchhidden: true}
  },
  list_column('containing_vocabulary', true, visible_vocabularies),
  list_column('owner_vocabulary', true, visible_vocabularies),
  list_column('status', true, status_options),
  {
    name: 'comment',
    index: 'comment',
    hidden: true,
    hidedlg: true,
    sortable: false,
    searchoptions: {searchhidden: true, sopt: ['cn', 'nc']}
  },
  list_column('lexeme_qualifier', false, qualifier_options),
  list_column('lip_qualifier', false, qualifier_options),
  list_column('classification_value', false, cv_options)
],
initialColNames = ['Id', 'Hasło','Część mowy', 'Wzór', 'Liczba wzorów',
                   'Char. fleks.', 'Liczba char. fleks.', 'Forma',
                   'Słownik używający', 'Słownik właściciel', 'Status',
                   'Komentarz', 'Kwal. leksemu', 'Kwal. odmieniasia',
                   'Wartość klasyfikacji'],
initial_sort_rules = [{field: main_field, order: 'asc', a_tergo: false}],
grid_caption = "Leksemy", deleted, deleted_cr,
edit_form_id = 'lexeme-edit-form',
edit_form_cancel_id = 'lexeme-edit-cancel',
edit_form_submit_id = 'lexeme-edit-submit',
qualifier_options = {
  noneSelectedText: 'Wybierz',
  selectedText: selected_qualifiers_text,//'# 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;
  });
}

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 sort_rule_special_features(rule) {
  $('#entries_a_tergo').prop('checked', rule['a_tergo']);
};

function prepare_sort_rules(sort_rules, old_sort_rules) {
  if (sort_rules[0].field == 'entry') {
    a_tergo = false;
    $.each(old_sort_rules, function(i, rule) {
      if (rule.field == 'entry') {
        a_tergo = rule.a_tergo;
        return false;
      }
      return null;
    });
    sort_rules[0].a_tergo = a_tergo;
  }
}

function init_form_widgets() {
  $(document).on('click', '.lip-row span.remove', function() {
    var li = $(this).closest('li');
    var name = $('input', li)[0].name;
    if (name.substring(0,7) != 'lip_add')
      deleted.push(name.split('-')[0]);
    li.remove();
    show_changed();
    $('#table-preview').html('');
  });
  $(document).on('click', '#add-row', function() {
    var id = lexeme_id();
    var pos = $('#id_part_of_speech').val();
    var new_row = $(get_new_row_html(id, pos));
    $('#pattern-list').append(new_row);
    var elem = $('#pattern-list .lip-qualifiers').last();
    elem.multiselect2(qualifier_options);
    elem.next().width('220px');
    show_changed();
  });

  $(document).on('click', '.cr-row span.remove', function() {
    var li = $(this).closest('li');
    if (!li.hasClass('cr-add'))
      deleted_cr.push(Number(li[0].id.split('-')[1]));
    li.remove();
    show_changed();
  });
  $(document).on('click', '#add-cr-row', function() {
    var id = lexeme_id();
    var pos = $('#id_part_of_speech').val();
    var new_row = $(get_new_cr_row_html(id, pos));
    $('#cr-list').append(new_row);
    show_changed();
  });
  $(document).on('change', '#id_part_of_speech', check_pos);

  function update_preview() {
    var lip_row = $(this).closest('.lip-row');
    if (lip_row.hasClass('lip-row-active')) {
      set_active_lip_row.call(lip_row);
    }
  }
  $(document).on('change', '#' + edit_form_id + ' input', update_preview);
  $(document).on('change', '#' + edit_form_id + ' textarea', update_preview);
  $(document).on('change', '#' + edit_form_id + ' select', update_preview);

  $(document).on('click', '.lip-row', set_active_lip_row);

  $(document).on('click', '#lexeme-edit-delete', delete_lexeme);

  $('#prompter-dialog').dialog({
    autoOpen: false,
    width: 460,
    modal: true,
    buttons: [
      {
        text: "Anuluj",
        click: function() { $(this).dialog("close"); }
      },
      {
        text: "Wybierz",
        click: input_prompter_pattern
      }
    ]
  });
  $(document).on('click', 'button.prompter', display_prompter);
  $(document).on('click', '.prompter-row', set_active_prompter_row);
  $(document).on('click', '#prompter-checkboxes :checkbox', reload_prompter);

  $('#add-button').click(add_lexeme);
  $('#default-owner-dialog').dialog({
    autoOpen: false,
    modal: true,
    buttons: [
      {
        text: "Anuluj",
        click: function() { $(this).dialog("close"); }
      },
      {
        text: "Wybierz",
        click: function() {
          var value = $('#default-owner').find(':checked').val();
          if (value) {
            set_default_owner(value);
            $(this).dialog("close");
            make_new_lexeme();
          }
        }
      }
    ]
  });

  $(document).on('click', '#move-lexeme .label', function() {
    $('#move-lexeme').toggleClass('move-hidden');
  });
  $(document).on('change', '#id_new_owner', reload_classifications);
  $(document).on('keyup', '#id_entry', show_homonym_count);
}

function lexeme_id() {
  return $('#' + edit_form_id).find('input[name=id]').value();
}

function reload_classifications() {
  $('#classifications').load(
    ajax_classification_forms,
    'lexeme_id=' + lexeme_id() + '&vocab_id=' + $('#id_new_owner').val());
}

function load_content(id, is_created) {
  $('#edit').load(ajax_edit_form, 'id='+id, function() {
    edit_form_init();
    created = Boolean(is_created);
  });
  $('#variant0').load(ajax_inflection_tables, 'id='+id+'&variant=0');
  $('#variant1').load(ajax_inflection_tables, 'id='+id+'&variant=1');
  $('#odm').load(ajax_odm_forms, 'id='+id);
  $('#history').load(ajax_history_table, 'id='+id);
  grid.jqGrid('setGridParam', {'lastSelectedId' : id});
}

function edit_form_init() {
  $('button', '#edit').button();
  $('#pattern-list.editable').sortable().sortable({
    change: function(event, ui) {
      show_changed();
    }
  });
  //$('#pattern-list').disableSelection();
  $('.lip-row', '#pattern-list').first().click();
  $('#id_vocabularies').multiselect2({
    noneSelectedText: 'Wybierz słowniki',
    selectedText: '# słowników',
    selectedList: 4,
    header: false
  });
  $('#id_qualifiers').multiselect2(qualifier_options);
  $('.lip-qualifiers').multiselect2(qualifier_options);
  $('.lip-qualifiers').next().width('220px');
  $('.classification-values').multiselect2({
    noneSelectedText: 'Wybierz',
    selectedText: selected_cvs_text,//'# wartości',
    selectedList: 4,
    header: false
  });
  $('button.prompter').button();
  var owner = $('#id_new_owner').find(':selected').text()
  var bold_owner = function(text) {
    if (text == owner)
      return '<strong>' + text + '</strong>';
    else
      return text;
  };
  $('#id_new_owner').selectmenu({
    width:'auto', style: 'popup', format: bold_owner});
  show_homonym_count();
  deleted = [];
  deleted_cr = [];
  created = false;
  pos_index = $('#id_part_of_speech').prop('selectedIndex');
  hide_changed();
  if (ctrl)
    ctrl.remove();
}

function edit_form_submit() {
  var this_form = $(this);
  var form_data = this_form.serializeArray();
  form_data.push({name: 'deleted', value: deleted});
  form_data.push({name: 'deleted_cr', value: deleted_cr});
  var vocabularies = [];
  var qualifiers = [];
  var cvs = {};
  form_data = $.map(form_data, function(elem) {
    if (elem.name != 'vocabularies' && elem.name != 'qualifiers' &&
        !(/^cl[0-9]+-values$/.test(elem.name)))
      return elem;
    else {
      if (elem.name == 'vocabularies')
        vocabularies.push(elem.value);
      if (elem.name == 'qualifiers')
        qualifiers.push(elem.value);
      if (/^cl[0-9]+-values$/.test(elem.name)) {
        if (cvs[elem.name] === undefined)
          cvs[elem.name] = [];
        cvs[elem.name].push(elem.value);
      }
      return null;
    }
  });
  form_data.push({name: 'vocabularies', value: vocabularies});
  form_data.push({name: 'qualifiers', value: qualifiers});
  $.each(cvs, function(name, value) {
    form_data.push({name: name, value: value});
  });
  form_data.push({name: 'classification_values', value: cvs});
  hide_changed();
  ctrl = getBusyOverlay(
    'viewport',
    {color: 'black', opacity: 0.5},
    {size: 100});
  var result = $.ajaxJSON({
    method: 'post',
    url: ajax_update_lexeme,
    save: true,
    data: {
      form_data: form_data,
      mask: grid.jqGrid('getGridParam', 'postData').mask
    },
    description: "Zapisanie zmian",
    callback: function() {
      grid.jqGrid('setGridParam', {'setOnComplete' : true});
      grid.trigger("reloadGrid");
      load_content(lexeme_id());
    },
    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;
    }
  });
  if (result === false && ctrl) {
    ctrl.remove();
  }
  return false;
}

function before_save(){
  // sprawdzić wzór z charflem
  var ok = true;
  $('.lip-row').each(function() {
    var ic = $(this).find('select.inflection-characteristic').val();
    var pattern = $(this).find('input.pattern').val();
    var result = $.ajax({
      type: 'get',
      url: ajax_check_pattern,
      data: {pattern_name: pattern, ic_id: ic},
      async: false
    }).responseText;
    if ($.parseJSON(result).answer != 'yes')
      ok = false;
  });
  return ok || confirm("Wzór nie pasuje do charakterystyki - na pewno zapisać?");
}

var new_row_counter = 1; // potrzebne tylko dla niepowtarzalności idów
function get_new_row_html(id, pos) {
  var new_row_html = $.ajax({
    type: 'get',
    url: ajax_new_lip_row,
    data: {id: id, pos_id: pos},
    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;
}

var new_cr_row_counter = 1; // potrzebne tylko dla niepowtarzalności idów
function get_new_cr_row_html(id, pos) {
  var new_row_html = $.ajax({
    type: 'get',
    url: ajax_new_cr_row,
    data: {id: id, pos_id: pos},
    async: false
  }).responseText;
  var row_html = new_row_html.replace(/NUM/g, new_cr_row_counter);
  new_cr_row_counter++;
  return row_html;
}

function set_active_lip_row() {
  $('.lip-row-active').removeClass('lip-row-active ui-state-active');
  $(this).addClass('lip-row-active ui-state-active');
  // tabelka
  var data = {
    id: lexeme_id(),
    entry: $('#id_entry').val(),
    pattern: $('input.pattern', this).value(),
    inflection_characteristic:
      $('.inflection-characteristic', this).value(),
    lip_id: $('input.pattern', this)[0].name.split('-')[0]
  };
  $('#table-preview').load(
    ajax_table_preview, $.param(data),
    function(response, status) {
      if (status == 'error') {
        $('#table-preview').html('');
      }
    });
}

function delete_lexeme() {
  var id = lexeme_id();
  if (confirm('Na pewno usunąć leksem?')) {
    $.ajaxJSON({
      method: 'post',
      url: ajax_delete_lexeme,
      data: {id: id},
      description: 'Usunięcie leksemu',
      callback: function() {
        grid.trigger("reloadGrid");
        findAndScroll();
      }
    });
  }
}

// filtry

function create_button(button_class, icon_class, text) {
  var button = $('<a/>')
    .addClass('ui-state-default ui-corner-all ' + button_class)
    .addClass('fm-button fm-button-icon-left');
  var icon = $('<span/>').addClass('ui-icon ' + icon_class);
  button.append(icon).append(text);
  return button;
}

function add_buttons(f) {
  if (f[0].p) {
    var button_save = create_button(
      'ui-save', 'ui-icon-disk', 'Zapisz filtr');
    var button_load = create_button(
      'ui-load', 'ui-icon-arrowthickstop-1-s', 'Załaduj filtr');
    f.next().find('tr').last().children().first()
      .append(button_save).append(button_load);
    button_save.click(function() {
      var filter_name, filters, data;
      filter_name = prompt('Wybierz nazwę filtru do zapisania')
      if(filter_name) {
        filters = $('.searchFilter').jqFilter('filterData');
        post_data = {
          name: filter_name,
          serialized_filter: $.toJSON(filters) // to jest string
        };
        var save_filter_data = {
          method: 'post',
          url: ajax_save_filter,
          data: post_data,
          description: 'Zapisanie filtru'
        }
        $.ajaxJSON($.extend(save_filter_data, {
          callback: function(data) {
            if (data.exists
                && confirm('Filtr o tej nazwie już istnieje. Nadpisać?')) {
              $.ajaxJSON($.extend(save_filter_data, {
                data: $.extend(post_data, {force: true})
              }));
            }
          }
        }));
      }
      return false;
    });
    button_load.click(function() {
      $.ajaxJSON({
        method: 'get',
        url: ajax_get_filters,
        data: {},
        description: 'Pobranie listy filtrów',
        callback: filter_choice
      });
      return false;
    });
  }
}

function filter_choice(ajax_data) {
  var filters = ajax_data.filters;
  list = $('#filter-list');
  list.html('');
  // napakować okienko na podstawie wyniku z ajaxa
  $.each(filters, function(i, filter) {
    var item = $('<li/>').addClass('ui-state-default').attr('id', filter.id);
    var delete_button = $('<span/>')
      .addClass('remove ui-icon ui-icon-closethick');
    delete_button.click(function() {
      if (confirm('Usunąć filtr "' + filter.name + '"?')) {
        $.ajaxJSON({
          method: 'post',
          url: ajax_delete_filter,
          data: {id: filter.id},
          description: 'Usunięcie filtru',
          callback: function() {
            item.remove();
          }
        });
      }
      return false;
    });
    item.append(delete_button);
    item.append(filter.name);
    item.click(function() {
      put_filter(filter.json);
      $("#load-filter-dialog").dialog('close');
    });
    list.append(item);
  });
  $("#load-filter-dialog").dialog('open');
}


function display_prompter() {
  var cv;
  $('#prompter-list').html('');
  $('#prompter-table-preview').html('');
  $('#prompter-checkboxes').hide();
  $('#prompter-checkboxes').find(':checkbox').prop('checked', true);
  cvs = $('#id_cl' + commonness + '-values').val();
  if (!cvs || cvs.length == 0) {
    $('#prompter-commonness').prop('checked', false);
    $('#prompter-commonness').prop('disabled', true);
    $('#prompter-commonness').next().addClass('disabled');
  } else {
    $('#prompter-commonness').prop('disabled', false);
    $('#prompter-commonness').next().removeClass('disabled');
  }
  $('#prompter-dialog').dialog('open');
  reload_prompter({button: this});
}

function reload_prompter(arg) {
  prompter_ctrl = getBusyOverlay(
    $('#prompter-dialog')[0],
    {},
    {size: 30});
  // załadować dane do promptera
  if (arg.button)
    elem = $(arg.button).closest('.lip-row');
  else
    elem = $('.lip-row-active');
  var ic = elem.find('select.inflection-characteristic').val();
  var id = lexeme_id();
  var cvs = $('#id_cl' + commonness + '-values').val();
  var ic_check, cv_check, bl_check;
  var entry = $('#id_entry').val();
  var pos = $('#id_part_of_speech').val();
  ic_check = $('#prompter-ic').prop('checked');
  cv_check = $('#prompter-commonness').prop('checked');
  bl_check = $('#prompter-blacklist').prop('checked');
  $('#prompter-list').load(
    ajax_prompter_list +
      '?' + $.param({
        ic_id: ic,
        lexeme_id: id,
        entry: entry,
        pos_id: pos,
        commonness_ids: $.toJSON(cvs),
        ic_check: ic_check,
        cv_check: cv_check,
        bl_check: bl_check
      }),
    function() {
      prompter_ctrl.remove();
      $('#prompter-list').children().first().click();
      $('#prompter-checkboxes').show();
    });
}

function set_active_prompter_row() {
  $('.prompter-row-active').removeClass('prompter-row-active');
  $(this).addClass('prompter-row-active');
  // tabelka
  var data = {
    id: lexeme_id(),
    entry: $('#id_entry').val(),
    pos: $('#id_part_of_speech').val(),
    pattern: $('.pattern', this).text(),
    inflection_characteristic: $('.ic-id', this).text(),
    lip_id: 'lip_add'
  };
  $('#prompter-table-preview').load(
    ajax_table_preview, $.param(data),
    function(response, status) {
      if (status == 'error') {
        $('#prompter-table-preview').html('');
      }
    });
}

function input_prompter_pattern() {
  var active = $('.prompter-row-active');
  var pattern;
  if (active.length > 0) {
    $('#prompter-dialog').dialog('close');
    pattern = active.find('.pattern').text();
    $('.lip-row-active').find('input.pattern').val(pattern);
    show_changed();
  }
}

function check_pos() {
  var pos = $('#id_part_of_speech').val();
  var bad_lips = [], confirmed;
  $('.lip-row').each(function() {
    var ic = $(this).find('select.inflection-characteristic').val();
    var ic_entry = $(this).find(
      'select.inflection-characteristic :selected').text();
    var pattern = $(this).find('input.pattern').val();
    var result = $.ajax({
      type: 'get',
      url: ajax_check_pos,
      data: {pos_id: pos, ic_id: ic},
      async: false
    }).responseText;
    if ($.parseJSON(result).answer != 'yes')
      bad_lips.push({ic: ic_entry, pattern: pattern});
  });
  if (bad_lips || $('.cr-row').length > 0) {
    var lips_text = '', cr_text = '';
    if (bad_lips.length > 0) {
      var lips = [];
      $.each(bad_lips, function(i, t) {
        lips.push('(' + t.ic + ', ' + t.pattern + ')');
      });
    }
    lips_text = " sposoby odmiany: " + lips.join(', ');
    if ($('.cr-row').length > 0) {
      if (lips_text)
        cr_text = ' i';
      cr_text += ' wszystkie odsyłacze';
    }
    var confirmed = confirm('Zostaną usunięte' + lips_text + cr_text +
                            '.\nKontynuować?');
    if (!confirmed) {
      this.selectedIndex = pos_index;
    }
  }
  if (confirmed || !(bad_lips || $('.cr-row').length > 0)) {
    pos_index = this.selectedIndex;
    var result = $.ajax({ // znowu brzydko...
      type: 'get',
      url: ajax_get_ics,
      data: {pos_id: pos},
      async: false
    });
    var ics = $.parseJSON(result.responseText).ics;
    var ic_entries = $.map(ics, function(a) { return a[1]; });
    $('.lip-row').each(function() {
      var select = $(this).find('select.inflection-characteristic');
      var ic_entry = select.find(':selected').text();
      var index;
      if (ic_entries.indexOf(ic_entry) != -1) {
        select.empty();
        $.each(ics, function(i, a) {
          var option = new Option(a[1], a[0]);
          if (a[1] == ic_entry) {
            index = i;
          }
          select.prop('options').add(option);
        });
        select.selectedIndex = index;
      } else {
        var li = $(this);
        // copypasta...
        var name = li.find('input')[0].name;
        if (name.substring(0,7) != 'lip_add')
          deleted.push(name.split('-')[0]);
        li.remove();
        $('#table-preview').html('');
      }
    });
    $('.cr-row').each(function() {
      var li = $(this);
      // copypasta...
      if (!li.hasClass('cr-add'))
        deleted_cr.push(Number(li[0].id.split('-')[1]));
      li.remove();
    });
  }
}

function add_lexeme() {
  // trzeba zdecydować, jaki słownik właściciel
  if (default_owner == '') {
    $('#default-owner-dialog').dialog('open');
  } else {
    make_new_lexeme();
  }
}

function set_default_owner(vocab_id) {
  default_owner = vocab_id;
  $.ajaxJSON({
    method: 'post',
    url: ajax_save_default_owner,
    data: {vocab_id: vocab_id},
    description: "Zapisanie domyślnego słownika"
  });
}

function make_new_lexeme() {
  // stworzyć ajaxem nowy leksem i go po prostu otworzyć...
  $.ajaxJSON({
    method: 'post',
    url: ajax_create_lexeme,
    data: {vocab_id: default_owner},
    description: "Utworzenie leksemu",
    callback: function(data) {
      load_content(data.id, true);
    }
  });
}

function show_homonym_count() {
  var entry = $('#id_entry').val();
  $.ajaxJSON({
    method: 'get',
    url: ajax_homonym_count,
    data: {entry: entry, lexeme_id: lexeme_id()},
    descripton: "Pobranie liczby homonimów",
    callback: function(data) {
      var info = '';
      if (entry == $('#id_entry').val()) {
        if (data.count > 0) {
          info = data.count + ' homonim';
          if (data.count > 1 && data.count < 5)
            info += 'y';
          else if (data.count >= 5)
            info += 'ów'; // "22 homonimów", ale nigdy nie będzie aż tyle
        }
        $('#homonym-count').text(info);
      }
    }
  });
}