Commit d76b15d1f8c96b7fc8403bf0add96febadfabaa1
1 parent
7bd2535a
ostrzeganie przed usunięciem atrybutów przy zmianie lub usunięciu odmieniasia
Showing
1 changed file
with
77 additions
and
29 deletions
media/js/lexeme-view.js
1 | -var prompter_ctrl, pos_index, created; | |
1 | +var prompter_ctrl, created; | |
2 | 2 | |
3 | 3 | function list_searchoptions(list) { |
4 | 4 | "use strict"; |
... | ... | @@ -222,16 +222,39 @@ function get_ics() { |
222 | 222 | return parseInt($(el).val(), 10);}).toArray(); |
223 | 223 | } |
224 | 224 | |
225 | +function get_ic_entries() { | |
226 | + "use strict"; | |
227 | + return $('.inflection-characteristic option:selected').map(function(i, el) { | |
228 | + return $(el).text();}).toArray(); | |
229 | +} | |
230 | + | |
225 | 231 | function get_entry() { |
226 | 232 | "use strict"; |
227 | 233 | return $('#id_entry').val(); |
228 | 234 | } |
229 | 235 | |
236 | +function join_attrs(attrs) { | |
237 | + "use strict"; | |
238 | + return $.map(attrs, function(attr) { | |
239 | + return $("[for='id_attr" + attr + "-value']").text().replace(':', ''); | |
240 | + }).join(', '); | |
241 | +} | |
242 | + | |
230 | 243 | function init_form_widgets() { |
231 | 244 | "use strict"; |
232 | 245 | $(document).on('click', '.lip-row span.remove', function() { |
233 | 246 | var li = $(this).closest('li'); |
234 | 247 | var name = $('input', li)[0].name; |
248 | + var ic_entry = li.find('.inflection-characteristic option:selected').text(); | |
249 | + var ics = get_ic_entries(); | |
250 | + ics.splice(ics.indexOf(ic_entry), 1); | |
251 | + var stale_attrs = check_attrs(ics); | |
252 | + if (stale_attrs.length > 0) { | |
253 | + if (!window.confirm("Atrybuty: " + join_attrs(stale_attrs) + | |
254 | + ' zostaną usunięte. Kontynouwać?')) { | |
255 | + return; | |
256 | + } | |
257 | + } | |
235 | 258 | if (name.substring(0,7) !== 'lip_add') |
236 | 259 | deleted.push(name.split('-')[0]); |
237 | 260 | li.remove(); |
... | ... | @@ -241,8 +264,9 @@ function init_form_widgets() { |
241 | 264 | }); |
242 | 265 | $(document).on('click', '#add-row', function() { |
243 | 266 | var id = lexeme_id(); |
244 | - var pos = get_pos(); | |
245 | - var new_row = $(get_new_row_html(id, pos)); | |
267 | + var new_row = $(get_new_row_html(id)); | |
268 | + new_row.find('.inflection-characteristic option:selected') | |
269 | + .addClass('last-selected'); | |
246 | 270 | var pattern_list = $('#pattern-list'); |
247 | 271 | pattern_list.append(new_row); |
248 | 272 | var elem = pattern_list.find('.lip-qualifiers').last(); |
... | ... | @@ -364,9 +388,23 @@ function init_form_widgets() { |
364 | 388 | }); |
365 | 389 | $(document).on('change', '#id_part_of_speech', check_pos); |
366 | 390 | $(document).on('change', '#id_new_owner', reload_classifications); |
367 | - // TODO trzeba też uwzględniać usunięcie odmieniasia | |
368 | - // TODO ostrzegać przed znikaniem atrybutów | |
369 | - $(document).on('change', '.inflection-characteristic', reload_attributes); | |
391 | + $(document).on('change', '.inflection-characteristic', function() { | |
392 | + var new_ics = get_ic_entries(); | |
393 | + var stale_attrs = check_attrs(new_ics); | |
394 | + var confirmed; | |
395 | + if (stale_attrs.length > 0) { | |
396 | + confirmed = window.confirm("Atrybuty: " + join_attrs(stale_attrs) + | |
397 | + ' zostaną usunięte. Kontynouwać?'); | |
398 | + if (!confirmed) { | |
399 | + $(this).find('option.last-selected').prop('selected', true); | |
400 | + } | |
401 | + } | |
402 | + if (confirmed || stale_attrs.length === 0) { | |
403 | + $(this).find('option.last-selected').removeClass('last-selected'); | |
404 | + $(this).find('option:selected').addClass('last-selected'); | |
405 | + reload_attributes(); | |
406 | + } | |
407 | + }); | |
370 | 408 | $(document).on('keyup', '#id_entry', show_homonym_count); |
371 | 409 | } |
372 | 410 | jqgrid.init_form_widgets = init_form_widgets; |
... | ... | @@ -494,7 +532,8 @@ function edit_form_init() { |
494 | 532 | deleted = []; |
495 | 533 | deleted_cr = []; |
496 | 534 | created = false; |
497 | - pos_index = $('#id_part_of_speech').prop('selectedIndex'); | |
535 | + $('#id_part_of_speech').find('option:selected').addClass('last-selected'); | |
536 | + $('.inflection-characteristic option:selected').addClass('last-selected'); | |
498 | 537 | jqgrid.hide_changed(); |
499 | 538 | if (jqgrid.ctrl) |
500 | 539 | jqgrid.ctrl.remove(); |
... | ... | @@ -600,12 +639,12 @@ function before_save(){ |
600 | 639 | } |
601 | 640 | |
602 | 641 | var new_row_counter = 1; // potrzebne tylko dla niepowtarzalności idów |
603 | -function get_new_row_html(id, pos) { | |
642 | +function get_new_row_html(id) { | |
604 | 643 | "use strict"; |
605 | 644 | var new_row_html = $.ajaxJSON({ |
606 | 645 | method: 'get', |
607 | 646 | url: $dj.ajax_new_lip_row, |
608 | - data: {lexeme_id: id, pos_id: pos, num: new_row_counter}, | |
647 | + data: {lexeme_id: id, pos_id: get_pos(), num: new_row_counter}, | |
609 | 648 | async: false // w zasadzie się tak nie powinno robić |
610 | 649 | }).html; |
611 | 650 | new_row_counter++; |
... | ... | @@ -868,10 +907,29 @@ function input_prompter_pattern() { |
868 | 907 | } |
869 | 908 | } |
870 | 909 | |
910 | +function check_attrs(ics) { | |
911 | + "use strict"; | |
912 | + var new_attrs = $.ajaxJSON({ | |
913 | + method: 'get', | |
914 | + url: $dj.ajax_check_attributes, | |
915 | + data: {lexeme_id: lexeme_id(), pos: get_pos(), ics: ics}, | |
916 | + async: false | |
917 | + }).attrs; | |
918 | + var old_attrs = $('#extra-attributes').find('select').map(function(i, el) { | |
919 | + return parseInt(el.id.split('-')[0].substring('id_attr'.length), 10); | |
920 | + }); | |
921 | + var stale_attrs = []; | |
922 | + $.each(old_attrs, function(i, attr) { | |
923 | + if (new_attrs.indexOf(attr) === -1) | |
924 | + stale_attrs.push(attr); | |
925 | + }); | |
926 | + return stale_attrs; | |
927 | +} | |
928 | + | |
871 | 929 | var check_pos = function() { |
872 | 930 | "use strict"; |
873 | 931 | var pos = get_pos(); |
874 | - var bad_lips = [], good_ics = [], stale_attrs = [], confirmed; | |
932 | + var bad_lips = [], good_ics = [], confirmed; | |
875 | 933 | var lip_row_elems = $('.lip-row'), cr_row_elems = $('.cr-row'); |
876 | 934 | lip_row_elems.each(function() { |
877 | 935 | var ic = $(this).find('select.inflection-characteristic').val(); |
... | ... | @@ -889,19 +947,7 @@ var check_pos = function() { |
889 | 947 | else |
890 | 948 | good_ics.push(ic_entry); |
891 | 949 | }); |
892 | - var new_attrs = $.ajaxJSON({ | |
893 | - method: 'get', | |
894 | - url: $dj.ajax_check_attributes, | |
895 | - data: {lexeme_id: lexeme_id(), pos: pos, ics: good_ics}, | |
896 | - async: false | |
897 | - }).attrs; | |
898 | - var old_attrs = $('#extra-attributes').find('select').map(function(i, el) { | |
899 | - return parseInt(el.id.split('-')[0].substring('id_attr'.length), 10); | |
900 | - }); | |
901 | - $.each(old_attrs, function(i, attr) { | |
902 | - if (new_attrs.indexOf(attr) === -1) | |
903 | - stale_attrs.push(attr); | |
904 | - }); | |
950 | + var stale_attrs = check_attrs(good_ics); | |
905 | 951 | if (bad_lips.length > 0 || cr_row_elems.length > 0 || stale_attrs.length > 0) { |
906 | 952 | var lips_text = '', cr_text = '', lips = []; |
907 | 953 | if (bad_lips.length > 0) { |
... | ... | @@ -918,19 +964,18 @@ var check_pos = function() { |
918 | 964 | // TODO ostrzegać przed znikaniem klasyfikacji [kiedyś] |
919 | 965 | var attr_text = ''; |
920 | 966 | if (stale_attrs.length > 0) { |
921 | - attr_text = "atrybuty: " + $.map(stale_attrs, function(attr) { | |
922 | - return $("[for='id_attr" + attr + "-value']").text().replace(':', ''); | |
923 | - }).join(', ') + '\n'; | |
967 | + attr_text = "atrybuty: " + join_attrs(stale_attrs) + '\n'; | |
924 | 968 | } |
925 | 969 | confirmed = window.confirm( |
926 | 970 | 'Zostaną usunięte:\n' + lips_text + cr_text + attr_text + 'Kontynuować?'); |
927 | 971 | if (!confirmed) { |
928 | - this.selectedIndex = pos_index; | |
972 | + $(this).find('option.last-selected').prop('selected', true); | |
929 | 973 | } |
930 | 974 | } |
931 | 975 | if (confirmed || !(bad_lips.length > 0 || cr_row_elems.length > 0 || |
932 | 976 | stale_attrs.length > 0)) { |
933 | - pos_index = this.selectedIndex; | |
977 | + $(this).find('option.last-selected').removeClass('last-selected'); | |
978 | + $(this).find('option:selected').addClass('last-selected'); | |
934 | 979 | var result = $.ajax({ // znowu brzydko... |
935 | 980 | type: 'get', |
936 | 981 | url: $dj.ajax_get_ics, |
... | ... | @@ -939,6 +984,7 @@ var check_pos = function() { |
939 | 984 | }); |
940 | 985 | var ics = $.parseJSON(result.responseText).ics; |
941 | 986 | var ic_entries = $.map(ics, function(a) { return a[1]; }); |
987 | + var reload_preview = false; | |
942 | 988 | lip_row_elems.each(function() { |
943 | 989 | var select = $(this).find('select.inflection-characteristic'); |
944 | 990 | var ic_entry = select.find(':selected').text(); |
... | ... | @@ -961,9 +1007,11 @@ var check_pos = function() { |
961 | 1007 | if (name.substring(0,7) !== 'lip_add') |
962 | 1008 | deleted.push(name.split('-')[0]); |
963 | 1009 | li.remove(); |
964 | - $('#table-preview').html(''); | |
1010 | + reload_preview = true; | |
965 | 1011 | } |
966 | 1012 | }); |
1013 | + if (reload_preview) | |
1014 | + $('#table-preview').html(''); | |
967 | 1015 | cr_row_elems.each(function() { |
968 | 1016 | var li = $(this); |
969 | 1017 | // copypasta... |
... | ... |