Blame view

dictionary/static/js/arg_realizations.js 14.6 KB
Bartłomiej Nitoń authored
1
$(function() {
Bartłomiej Nitoń authored
2
3
  window.selected_real_row_id = -1;
Bartłomiej Nitoń authored
4
5
6
7
8
9
10
11
12
13
14
  $('#realization-type #realization-type-select').live('change', function (event) {
	  if($(this).val() == 'phrase_type') {
		  $('#create-realization').load(ajax_realization_arg_form, {'form_data': '',
              														'main_argument': false, 
              														'real_id': ''});
	  }
	  else if($(this).val() == 'positions') {
		  $('#create-realization').load(ajax_positions_extension_form);
	  }
  });
Bartłomiej Nitoń authored
15
16
17
18
19
20
  $('#main-argument').load(ajax_realization_arg_form, {'form_data': '',
	                                                   'main_argument': true, 
	                                                   'real_id': ''});
  $('#create-realization').load(ajax_realization_arg_form, {'form_data': '',
                                                            'main_argument': false, 
                                                            'real_id': ''});
Bartłomiej Nitoń authored
21
Bartłomiej Nitoń authored
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
  $('#main-argument .argument-add-form #id_arg_type').live('change', function (event) {
	  realization_arguments_form_change(0, '', true, $(event.target).closest('.argument-add-form'));
  });

  $('#main-argument .argument-add-form #id_value_parameter_0').live('change', function (event) {
	  show_realizations();
  });

  $('#main-argument .argument-add-form #id_value_arguments_0').live('change', function (event) {
	  realization_arguments_form_change(-1, '', true, $(event.target).closest('.argument-add-form'));
  });

  $('#create-realization .argument-add-form #id_arg_type').live('change', function (event) {
	  realization_arguments_form_change(0, '', false, $(event.target).closest('.argument-add-form'));
  });

  $('#create-realization .argument-add-form #id_value_positions_0, #create-realization .argument-add-form #id_value_arguments_0, #create-realization .argument-add-form #id_value_parameter_0').live('change', function (event) {
	  realization_arguments_form_change(-1, '', false, $(event.target).closest('.argument-add-form'));
  });

  $('#create-realization .argument-add-form').live('submit', add_realization);
Bartłomiej Nitoń authored
43
  $('#create-realization .positions-add-form').live('submit', add_realization);
Bartłomiej Nitoń authored
44
45
46
47
48
49
50
51
52
53

  $('#modify-realization-dialog .argument-add-form #id_arg_type').live('change', function (event) {
	  realization_arguments_form_change(0, '', false, $(event.target).closest('.argument-add-form'));
  });

  $('#modify-realization-dialog .argument-add-form #id_value_positions_0, #modify-realization-dialog .argument-add-form #id_value_arguments_0, #modify-realization-dialog .argument-add-form #id_value_parameter_0').live('change', function (event) {
	  realization_arguments_form_change(-1, '', false, $(event.target).closest('.argument-add-form'));
  });

  $('#modify-realization-dialog .argument-add-form').live('submit', add_realization);
Bartłomiej Nitoń authored
54
  $('#modify-realization-dialog .positions-add-form').live('submit', add_realization);
Bartłomiej Nitoń authored
55
56
57
58
59
60
61
62
63

  $("#get-realizations").live('click', getRealizations);

  $("$[id$='-add_position_button']").live('click', function(event){openAddPositionDialog(event)});
  $("$[id$='-add_argument_button']").live('click', function(event){openAddArgumentDialog(event, null, 'Tworzenie typu frazy:')});
  $("$[id$='-add_realization_button']").live('click', function(event){openAddArgumentDialog(event, null, 'Tworzenie rozwinięcia typu frazy:')});
  $("$[id$='-select_argument_button']").live('click', function(event){openSelectArgumentDialog(event, null)});

  $("[id$='-argument-form-dialog'] .argument-add-form #id_arg_type'").live('change', function(event) {
Bartłomiej Nitoń authored
64
65
66
	  arguments_form_change(0, null, $(event.target).closest(".argument-add-form"), null)
  });
Bartłomiej Nitoń authored
67
  $("[id$='-argument-form-dialog'] .argument-add-form #id_value_positions_0, [id$='-argument-form-dialog'] .argument-add-form #id_value_arguments_0, [id$='-argument-form-dialog'] .argument-add-form #id_value_parameter_0").live('change', function(event) {
Bartłomiej Nitoń authored
68
69
	  arguments_form_change(-1, null, $(event.target).closest(".argument-add-form"), null)
  });
Bartłomiej Nitoń authored
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

  $('form.create-position-form').live('submit', create_position_form_submit);
  $('#position #remove-position').live('click', remove_position);
  $('#argument #remove-argument').live('click', remove_argument);
  $('#position #edit-position').live('click', edit_position);
  $('#argument #edit-argument').live('click', edit_argument);
  $('#argument #edit-realization').live('click', edit_realization);

  $("[id$='-argument-form-dialog'] .argument-add-form").live('submit', argument_form_submit);

  $('form.create-position-form').live('reset', function(){
	  $(this).closest("$[id$='-dialog']").dialog('close');
  });
  $('form.argument-add-form').live('reset', function(){
	  $(this).closest("$[id$='-dialog']").dialog('close');
  });
  $('form.argument-add-form #show-realizations').live('click', show_argument_realizations);

  addModifyRealizationDialog();
});

function getRealizations()
{
    ShowProgressAnimation();
    $.ajaxJSON({
      method: 'post',
      url: ajax_create_realizations,
      data: {
        form_data: ''
      },
    callback: function(result) {         
	  HideProgressAnimation();
      lemma_view_location = window.location.toString();
      var url = lemma_view_location.replace('/rozwiniecia_typow_fraz/', '/rozwiniecia_typow_fraz/'+result['file_name']);
      window.open(url,'Download');
    },
    error_callback: function(xhr, status, error) {
      HideProgressAnimation();
      error_alert(status + ': ' + error);
    },
  });
  return false;
}

function removeRealization(tabId)
{
  if(tabId != -1)
  {
	ShowProgressAnimation();
    realization_id = tabId.replace('realization_', ''); 

    main_arg_form = $('#main-argument .argument-add-form');
    main_arg_data = main_arg_form.serializeArray();
    main_arg_data = prepareArgFormData(main_arg_form, main_arg_data, -1);

    $.ajaxJSON({
      method: 'post',
      url: ajax_remove_realization,
      data: {
        realization_id: realization_id,
        main_arg_data: main_arg_data 
      },
      callback: function(result) { 
       window.selected_real_row_id = -1;
       show_realizations();	
       HideProgressAnimation();
      },
      error_callback: function(xhr, status, error) {
        HideProgressAnimation();
        error_alert(status + ': ' + error);
      },
      bad_data_callback: function(result) {
        if (result == 'empty main argument fields') {
          HideProgressAnimation();
          error_alert('Wypełnij wszystkie pola formularza wyboru głównego typu frazy.');
          return false;
        }
        else
        {
          HideProgressAnimation();
          return true;
        }
      },
    });
  }	
}

function realization_arguments_form_change(lastActualValueIdx, arg_id, main_argument, this_form) {
    form_data = this_form.serializeArray();
    this_dialog = this_form.closest("$[id$='-dialog']");

  	if(!arg_id) {
  	  form_data = prepareArgFormData(this_form, form_data, lastActualValueIdx);
      form_data.push({name: 'arg_id', value: ''});
    }
    else
        form_data.push({name: 'arg_id', value: arg_id});

    data = JSON.stringify(form_data);

    place_to_load_form = null
    if (this_dialog.length)
    	place_to_load_form = this_dialog;
    else if (main_argument)
    	place_to_load_form = $('#main-argument');
    else
    	place_to_load_form = $('#create-realization');
    place_to_load_form.empty();
    place_to_load_form.load(ajax_realization_arg_form, {'form_data': data,
    											        'main_argument': main_argument, 
    											        'real_id': ''}, 
    											        function() {
    											        	if(main_argument) {
    											        		show_realizations();
    											        	}
    											        });    
} 

function show_realizations() {
    window.selected_real_row_id = -1;
    this_form = $('#main-argument .argument-add-form');
    form_data = this_form.serializeArray();
    form_data = prepareArgFormData(this_form, form_data, -1);
    form_data.push({name: 'manage', value: true});

    data = JSON.stringify(form_data)
    $('#realizations').load(ajax_show_realizations, {'form_data' : data}, function() {
      $("tr.RealizationTableRow").click(function() {
        selectTr(this.id)});
      $("tr.RealizationTableRow").dblclick(function() {
Bartłomiej Nitoń authored
200
    	  modifyRealization(this.id, $(this).attr('type'));
Bartłomiej Nitoń authored
201
202
203
204
205
206
207
208
      });

      $("#delete-realization").click(function(e) {
        removeRealization(window.selected_real_row_id)});
    });
    return false;
}
Bartłomiej Nitoń authored
209
function modifyRealization(html_id, type) {
Bartłomiej Nitoń authored
210
211
212
	id = html_id.replace('realization_', '');
	$('#modify-realization-dialog').empty();
	$('#modify-realization-dialog').data('real_id', html_id);
Bartłomiej Nitoń authored
213
214
215
216
217
218
219
220
221
	$('#modify-realization-dialog').data('extension_type', type);
	if(type == 'phrase_type') {
		$('#modify-realization-dialog').load(ajax_realization_arg_form, {'form_data': '',
	                                                                     'main_argument': false, 
	                                                                     'real_id': id}).dialog('open');
	}
	else if(type == 'positions') {
		$('#modify-realization-dialog').load(ajax_positions_extension_form, {'extension_id': id}).dialog('open');
	}
Bartłomiej Nitoń authored
222
223
224
225
226
227
228
229
230
231
232
233
234
235
}

function addModifyRealizationDialog() {
    $('#modify-realization-dialog').dialog({ 
    	title: 'Zmiana rozwinięcia typu frazy',
    	autoOpen: false, 
	    modal: true,
	    width: 'auto', 
	    height: 'auto',
	    closeOnEscape: true,
    });
}

function add_realization() {
Bartłomiej Nitoń authored
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
    var extension_form = $(this);
    var extension_type = extension_form.parent().data('extension_type');
    if(!extension_type) {
    	extension_type = $('#realization-type #realization-type-select').val();
    }
    if(extension_type == 'phrase_type') {
    	add_phrase_type_extension(extension_form);
    }
    else if(extension_type == 'positions') {
    	add_positions_extension(extension_form);
    }
    return false;
}

function add_phrase_type_extension(realization_form) {
	ShowProgressAnimation();
	var real_id = realization_form.parent().data('real_id');

	var realization_data = realization_form.serializeArray();
Bartłomiej Nitoń authored
255
256
257
    realization_data = prepareArgFormData(realization_form, realization_data, -1);
    realization_data.push({name: 'lex_arg_choosing', value: false});
Bartłomiej Nitoń authored
258
    var main_arg_form = $('#main-argument .argument-add-form');
Bartłomiej Nitoń authored
259
260
261
262
    main_arg_data = main_arg_form.serializeArray();
    main_arg_data = prepareArgFormData(main_arg_form, main_arg_data, -1)
    main_arg_data.push({name: 'lex_arg_choosing', value: false});
Bartłomiej Nitoń authored
263
   $.ajaxJSON({
Bartłomiej Nitoń authored
264
265
266
267
268
269
270
      method: 'post',
      url: ajax_add_arg_realization,
      data: {
        main_arg_data: main_arg_data,
        realization_data: realization_data
      },
Bartłomiej Nitoń authored
271
    callback: function(result) {
Bartłomiej Nitoń authored
272
273
274
275
276
277
278
279
280
      if(result['added']) {
          if(real_id) {
        	  removeRealization(real_id);
          }
          else {
        	  show_realizations();
          } 
      }    
      $('#modify-realization-dialog').dialog('close');
Bartłomiej Nitoń authored
281
282
283
      $('#modify-realization-dialog').data('real_id', '');
  	  $('#modify-realization-dialog').data('extension_type', '');
  	  $('#modify-realization-dialog').empty();
Bartłomiej Nitoń authored
284
285
286
287
288
289
290
291
292
293
294
      HideProgressAnimation();
    },
    error_callback: function(xhr, status, error) {
      HideProgressAnimation();
      error_alert(status + ': ' + error);
    },
    bad_data_callback: function(result) {
      if (result == 'select opinion') {
          HideProgressAnimation();
          error_alert('Wybierz opinię.');
          return false;
Bartłomiej Nitoń authored
295
      }
Bartłomiej Nitoń authored
296
297
298
299
300
      else if (result) {
    	  HideProgressAnimation();
          error_alert(result);
          return false;
      }
Bartłomiej Nitoń authored
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
      else {
        HideProgressAnimation();
        return true;
      }
    },
  });
}

function add_positions_extension(extension_form) {
	ShowProgressAnimation();
	var extension_id = extension_form.parent().data('real_id');

    var main_phrase_type_form = $('#main-argument .argument-add-form');
    var main_phrase_type_data = main_phrase_type_form.serializeArray();
    main_phrase_type_data = prepareArgFormData(main_phrase_type_form, main_phrase_type_data, -1)
    main_phrase_type_data.push({name: 'lex_arg_choosing', value: false});

	var opinion = '';
	var positions = [];
	var extension_data = extension_form.serializeArray();
	extension_data = $.map(extension_data, function(elem) {
      if (elem.name != 'value' && elem.name != 'opinion')
        return elem;
      else {
        if (elem.name == 'value')
            positions.push(elem.value);
        else if (elem.name == 'opinion')
        	opinion = elem.value;
      }
    });
	extension_data.push({name: 'positions', value: positions});
	extension_data.push({name: 'opinion', value: opinion});

   $.ajaxJSON({
      method: 'post',
      url: ajax_add_positions_extension,
      data: {
        main_phrase_type_data: main_phrase_type_data,
        extension_data: extension_data
      },

    callback: function(result) {
      if(result['added']) {
          if(extension_id) {
        	  removeRealization(extension_id);
          }
          else {
        	  show_realizations();
          } 
      }    
      $('#modify-realization-dialog').dialog('close');
      $('#modify-realization-dialog').data('real_id', '');
  	  $('#modify-realization-dialog').data('extension_type', '');
  	  $('#modify-realization-dialog').empty();
      HideProgressAnimation();
    },
    error_callback: function(xhr, status, error) {
      HideProgressAnimation();
      error_alert(status + ': ' + error);
    },
    bad_data_callback: function(result) {
      if (result == 'select opinion') {
          HideProgressAnimation();
          error_alert('Wybierz opinię.');
          return false;
      }
      else if (result) {
    	  HideProgressAnimation();
          error_alert(result);
          return false;
      }
      else {
Bartłomiej Nitoń authored
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
        HideProgressAnimation();
        return true;
      }
    },
  });
}

function selectTr(id)
{  	  
  	if(selected_real_row_id != -1)
  	{
  	  var recent_row = document.getElementById(selected_real_row_id);
  	  var recent_descendants = getDescendantsNodes(recent_row);
  	  for(var i = 0; i<recent_descendants.length; i++)
  	    $(recent_descendants[i]).css("background-color", "");
  	}
  	if(selected_real_row_id == id)
  	  selected_real_row_id = -1;
  	else
  	{
  	  selected_real_row_id = id;
  	  var row = document.getElementById(id);
  	  var descendants = getDescendantsNodes(row);
  	  for(var i = 0; i<descendants.length; i++)
  	    $(descendants[i]).css("background-color", "LightSteelBlue");  
  	}
}