summaryrefslogtreecommitdiff
path: root/ishtar_common/static/js/ishtar.js
blob: fbf1ddc17d6a9550f875cd8941da68e5bc1922ee (plain)
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
/* CSRFToken management */

$.ajaxSetup({
beforeSend: function(xhr, settings) {
    function getCookie(name) {
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
    if (!(/^http:.*/.test(settings.url) || /^https:.*/.test(settings.url))) {
        // Only send the token to relative URLs i.e. locally.
        xhr.setRequestHeader("X-CSRFToken", getCookie('csrftoken'));
    }
}});

function manage_async_link(event){
    event.preventDefault();
    var url = $(this).attr('href');
    var target = $(this).attr('data-target');
    $.get(url, function(data) {
        $(target).html(data);
    });
}

/* default function to prevent undefined */
function get_next_table_id(){}
function get_previous_table_id(){}

var shortcut_url = '';
var datatables_i18n;
var current_modal;

var datatables_default = {
    "processing": true,
    "serverSide": true,
    "scrollX": true,
    "scrollY": true,
    "searching": false,
    "scrollCollapse": true
};

var advanced_menu = false;
var shortcut_menu_hide = false;
var activate_all_search_url = '/activate-all-search/';
var activate_own_search_url = '/activate-own-search/';
var activate_advanced_url = '/activate-advanced-menu/';
var activate_simple_url = '/activate-simple-menu/';
var shortcut_menu_hide_url = '/hide-shortcut-menu/'
var shortcut_menu_show_url = '/show-shortcut-menu/'

function init_shortcut_menu(html){
    close_wait();
    $("#context-menu").html(html);
    $("#shortcut-menu .chosen-select").chosen();
    $("#shortcut-menu .chosen-container").css('width', '100%');
    if (advanced_menu) {
        init_advanced_shortcut_fields();
    } else {
        init_shortcut_fields();
    }
    $("#short-menu-advanced").click(function(){
        $.get(url_path + activate_advanced_url,
               load_opened_shortcut_menu
        );
    });
    $("#short-menu-simple").click(function(){
        $.get(url_path + activate_simple_url,
               load_opened_shortcut_menu
        );
    });

    /*
    $(".short-menu-close").click(function(){
        $('#shortcut-menu div').hide();
        $('#shortcut-menu table').hide();
        $(".short-menu-close").hide();
        $(".short-menu-open").show();
        $.get(shortcut_menu_hide_url);
    });
    $(".short-menu-open").click(function(){
        $('#shortcut-menu div').show();
        $('#shortcut-menu table').show();
        $(".short-menu-open").hide();
        $(".short-menu-close").show();
        $.get(shortcut_menu_show_url);
    });
    if (shortcut_menu_hide){
        $('#shortcut-menu div').hide();
        $('#shortcut-menu table').hide();
        $(".short-menu-close").hide();
        $(".short-menu-open").show();
    }
    */
}

function init_shortcut_fields(){
    $("#current_file").change(function(){
        $.post('/' + url_path + 'update-current-item/',
               {item:'file', value:$("#current_file").val()},
               load_opened_shortcut_menu
        );
    });
    $("#current_operation").change(function(){
        $.post('/' + url_path + 'update-current-item/',
               {item:'operation', value:$("#current_operation").val()},
               load_opened_shortcut_menu
        );
    });
    $("#current_contextrecord").change(function(){
        $.post('/' + url_path + 'update-current-item/',
               {item:'contextrecord', value:$("#current_contextrecord").val()},
               load_opened_shortcut_menu
        );
    });
    $("#current_find").change(function(){
        $.post('/' + url_path + 'update-current-item/',
               {item:'find', value:$("#current_find").val()},
               load_opened_shortcut_menu
        );
    });
    $("#current_treatment").change(function(){
        $.post('/' + url_path + 'update-current-item/',
               {item:'treatment', value:$("#current_treatment").val()},
               load_opened_shortcut_menu
        );
    });
    $("#current_treatmentfile").change(function(){
        $.post('/' + url_path + 'update-current-item/',
               {item:'treatmentfile', value:$("#current_treatmentfile").val()},
               load_opened_shortcut_menu
        );
    });
}

function init_advanced_shortcut_fields(){
    $('#id_file-shortcut').change(function(){
        $("#id_select_file-shortcut").attr(
            'title', $('#id_select_file-shortcut').val());
        $.post('/' + url_path + 'update-current-item/',
               {item: "file", value:$("#id_file-shortcut").val()},
               load_opened_shortcut_menu
        );
    });
    $('#id_operation-shortcut').change(function(){
        $("#id_select_operation-shortcut").attr(
            'title', $('#id_select_operation-shortcut').val());
        $.post('/' + url_path + 'update-current-item/',
               {item: "operation", value:$("#id_operation-shortcut").val()},
               load_opened_shortcut_menu
        );
    });
    $('#id_contextrecord-shortcut').change(function(){
        $("#id_select_contextrecord-shortcut").attr(
            'title', $('#id_select_contextrecord-shortcut').val());
        $.post('/' + url_path + 'update-current-item/',
               {item: "contextrecord", value:$("#id_contextrecord-shortcut").val()},
               load_opened_shortcut_menu
        );
    });
    $('#id_find-shortcut').change(function(){
        $("#id_select_find-shortcut").attr(
            'title', $('#id_select_find-shortcut').val());
        $.post('/' + url_path + 'update-current-item/',
               {item: "find", value:$("#id_find-shortcut").val()},
               load_opened_shortcut_menu
        );
    });
}

function display_info(msg){
    $('#message .information .content').html(msg);
    $('#message').fadeIn('slow');
    $('#message .information').fadeIn('slow');
    setTimeout(
        function(){
            $('#message .information').fadeOut('slow');
            $('#message').fadeOut('slow');
        }, 5000);
}

function load_opened_shortcut_menu(){
    load_shortcut_menu(true);
}

function load_shortcut_menu(opened){
    if (!shortcut_url) return;
    $('.modal-progress').modal('show');
    $.ajax({
        url: shortcut_url,
        cache: false,
        success:function(html){
            init_shortcut_menu(html);
            if(opened){
                $("#dropdown-toggle-shortcut-menu").click();
            }
        },
        error:function(XMLHttpRequest, textStatus, errorThrows){
            close_wait();
        }
    });
}

function dynamic_load(url, target){
    $.ajax({
        url: url,
        success: function(data, textStatus, jqXHR) {
            $(target).html(jqXHR.responseText);
            var response = $(jqXHR.responseText);
            var responseScript = response.find("script");
            $.each(responseScript, function(idx, val) {
                eval(val.text);
            } );
            responseScript = response.filter("script");
            $.each(responseScript, function(idx, val) {
                eval(val.text);
            } );
   }
    });
}

$(document).ready(function(){
    $("#main_menu > ul > li > ul").hide();
    $("#main_menu ul ul .selected").parents().show();
    var items = new Array('file', 'operation');
    if ($(document).height() < 1.5*$(window).height()){
        $('#to_bottom_arrow').hide();
        $('#to_top_arrow').hide();
    }
    $('#language-selector a').click(function(){
        $('#language-selector input[name="language"]').val(
            $(this).attr("data-lang"));
        $('#language-form').submit();
    });
    load_shortcut_menu();
    $('#current_items select').change(function(){
        $(this).attr('class', $(this).children("option:selected").attr('class'));
    });
    $("a.async-link").click(manage_async_link);
    $(".chosen-select").chosen();
});

$(document).on("click", '#to_bottom_arrow', function(){
  $("html, body").animate({ scrollTop: $(document).height() }, 1000);
});

$(document).on("click", '#to_top_arrow', function(){
  $("html, body").animate({ scrollTop: 0}, 1000);
});

$(document).on("click", '.check-all', function(){
  $(this).closest('table'
        ).find('input:checkbox'
        ).attr('checked', $(this).is(':checked'));
});

$(document).on("click", '#main_menu > ul > li', function(){
    var current_id = $(this).attr('id');
    $("#main_menu ul ul").not($(this).parents('ul')).not($(this).find('ul')
                        ).hide('slow');
    $(this).find('ul').show('slow');
});

/* manage help texts */
$(document).on("click", '.help_display', function(){
    var help_text_id = $(this).attr("href") + "_help";
    $(help_text_id).toggle();
});

$(document).on("click", '#progress-content', function(){
    $('.modal-progress').hide();
});

function long_wait(){
    $('.modal-progress').modal('show');
    $('.progress-1').show('slow');
    setTimeout(function(){
        $('.progress-1').hide('slow');
        $('.progress-2').show('slow');
    }, 60000);
    setTimeout(function(){
        $('.progress-2').hide('slow');
        $('.progress-3').show('slow');
    }, 120000);
    setTimeout(function(){
        $('.progress-3').hide('slow');
        $('.progress-4').show('slow');
    }, 180000);
    setTimeout(function(){
        $('.progress-4').hide('slow');
        long_wait();
    }, 240000);
}

function close_wait(){
    $('.modal-progress').modal('hide');
    if (current_modal){
        $('body').addClass('modal-open');
    }
}

var last_window;

function load_window(url, speed, on_success, no_jump){
    $('.modal-progress').modal('show');
    $.ajax({
        url: url,
        cache: false,
        success:function(html){
            close_wait();
            $(".previous_page").remove();
            $(".next_page").remove();
            var register_id = $(html).find(".card-header").attr("data-sheet-id");
            var current_sheet = $('[data-sheet-id="' + register_id + '"]');
            if (current_sheet.length){
                current_sheet.parent().remove();
            }
            var current_id = $(html).find(".card-header").parent().attr("id");
            var current_label = $(html).find(".card-label").html();
            var new_sheet_list = new Array();
            for (idx in sheet_list){
                if (sheet_list[idx]['register_id'] != register_id){
                    new_sheet_list.push(sheet_list[idx]);
                }
            }
            sheet_list = new_sheet_list;
            sheet_list.push({'id': current_id, 'label': current_label,
                             'register_id': register_id});
            $("#window").append(html);
            $("#" + last_window).show();
            update_window_menu();
            if (!no_jump){
                // jump to this window
                var url = location.href;
                location.href = "#" + last_window;
                history.replaceState(null, null, url);
            }
            if (on_success) on_success();
        },
        error:function(XMLHttpRequest, textStatus, errorThrows){
            close_wait();
        }
    });
}

function load_current_window(url, model_name){
    var id;
    if (advanced_menu){
        id = $("#id_" + model_name + "-shortcut").val();
    } else {
        id = $("#current_" + model_name).val();
    }
    if (!id) return;
    url = url.split('/');
    url[url.length - 1] = id;
    url.push('');
    return load_window(url.join('/'));
}

function hide_window(window_id){
    $("#" + window_id).remove();
    splitted = window_id.split('-');
    register_id = "";
    for (idx=0 ; idx < splitted.length - 1 ; idx++){
        if (idx > 0) register_id += "-";
        register_id += splitted[idx]
    }
    var new_sheet_list = new Array();
    for (idx in sheet_list){
        if (sheet_list[idx]['register_id'] != register_id){
            new_sheet_list.push(sheet_list[idx]);
        }
    }
    sheet_list = new_sheet_list;
    update_window_menu();
}

function load_url(url){
    $('.modal-progress').modal('show');
    $.ajax({
        url: url,
        cache: false,
        success:function(html){
            close_wait();
        },
        error:function(XMLHttpRequest, textStatus, errorThrows){
            close_wait();
        }
    });
}

function open_window(url){
    var newwindow = window.open(url, '_blank',
                                'height=400,width=600,scrollbars=yes');
    if (window.focus) {newwindow.focus()}
    return false;
}

function save_and_close_window(name_label, name_pk, item_name, item_pk){
  var main_page = opener.document;
  jQuery(main_page).find("#"+name_label).val(item_name);
  jQuery(main_page).find("#"+name_pk).val(item_pk);
  opener.focus();
  self.close();
}

function save_and_close_window_many(name_label, name_pk, item_name, item_pk){
  var main_page = opener.document;
  var lbl_ = jQuery(main_page).find("#"+name_label);
  var val_ = jQuery(main_page).find("#"+name_pk);
  if (val_.val()){
    var v = lbl_.val();
    v = v.slice(0, v.lastIndexOf(","));
    lbl_.val(v + ", " + item_name + ", ");
    val_.val(val_.val() + ", " + item_pk);
    lbl_.change();
  } else {
    jQuery(main_page).find("#"+name_label).val(item_name);
    jQuery(main_page).find("#"+name_pk).val(item_pk);
  }
  opener.focus();
  self.close();
}

function multiRemoveItem(selItems, name, idx){
    for(id in selItems){
        if(selItems[id] == idx){
            selItems.splice(id, 1);
        }
    }
    jQuery("#selected_"+name+"_"+idx).remove();
}

function closeAllWindows(){
    jQuery("#window > div").hide("slow");
    jQuery("#window").html("");
}

function show_hide_flex(id){
    if ($(id).is(':hidden')){
        $(id).css('display', 'flex');
    } else {
        $(id).hide();
    }
}

var activate_all_search_msg = "Searches in the shortcut menu deals with all items.";
var activate_own_search_msg = "Searches in the shortcut menu deals with only your items.";

function activate_all_search(){
    $('.activate_all_search').removeClass('disabled');
    $('.activate_own_search').addClass('disabled');
    $.get(activate_all_search_url, function(data) {
        display_info(activate_all_search_msg);
    });
    return false;
}

function activate_own_search(){
    $('.activate_own_search').removeClass('disabled');
    $('.activate_all_search').addClass('disabled');
    $.get(activate_own_search_url, function(data) {
        display_info(activate_own_search_msg);
    });
    return false;
}

function get_label_from_input(input){
    if (input.parent().filter('.input-group').length){
        input = input.parent();
    }
    return "<strong>" + input.prev().html() + "</strong>";
}


function update_search_resume(){
    var summary = "";
    var inputs = $('#wizard-form input').map(
        function(){
            var v = "";
            var item_for_label = $(this);
            if ($(this).filter(":checkbox:checked").length){
                var check = $(this).val();
                if (check == "on"){
                    v = YES;
                } else {
                    v = $(this).parent().first().contents().filter(function() {
                        return this.nodeType == 3;
                    }).text();
                    item_for_label = $(this).parent().parent().parent();
                }
            } else if ($(this).filter(":text").length){
                v = $(this).val();
            } else if ($(this).filter('input[type="number"]').length){
                v = $(this).val();
            }
            if (v == "") return;
            if (summary) summary += " ; ";
            summary += get_label_from_input(item_for_label) + " " + v;
        }
    ).get();
    var selects = $('#wizard-form select').each(
        function(){
            $(this).find('option').not(':first').filter(':selected').map(
                function(){
                    var item_for_label = $(this).parent();
                    if(item_for_label.filter('optgroup').length){
                        item_for_label = item_for_label.parent();
                    }
                    if (summary) summary += " ; ";
                    summary += get_label_from_input(item_for_label) + " " + $(this).text();
            });
        }
    );

    if (summary != ""){
        $('#advanced-search-resume-content').html(summary);
        $('#advanced-search-resume').show();
    } else {
        $('#advanced-search-resume').hide();
    }
}

var sheet_list = new Array();

function update_window_menu(){
    if (!sheet_list.length){
        $("#window-fixed-menu").hide();
        return
    }
    var html = "";
    for (idx in sheet_list){
        var sheet = sheet_list[idx];
        html += '<li class="nav-item">';
        html += '<a class="nav-link" href="#' + sheet['id'] + '">' +
            sheet['label'] +'</a>';
        html += '</li>';
    }
    $("#window-fixed-menu-list").html(html);
    $("#window-fixed-menu").show();
}

function toggle_window_menu(){
    $("#window-fixed-menu").toggleClass('hidden');
    return false;
}


function register_advanced_search(){
    $(".advanced-search-reset").click(
        function(){
            document.getElementById('wizard-form').reset();
            $('#advanced-search-resume').hide();
        }
    );
    $(".advanced-search-valid").click(update_search_resume);
}


function manage_pinned_search(name, data){
    $('#pinned_search_content_' + name).html('');
    for (idx in data){
        if (idx == 'pinned-search' && data[idx] != ''){
           $('#pinned_search_content_' + name).html(data[idx]);
        }
    }
    if ($('#pinned_search_content_' + name).html()){
        $('#pinned_search_' + name).show();
    } else {
        $('#pinned_search_' + name).hide();
    }
}