/* 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')); } }}); if (typeof String.prototype.endsWith !== 'function') { String.prototype.endsWith = function(suffix) { return this.indexOf(suffix, this.length - suffix.length) !== -1; }; } if (typeof String.prototype.format !== 'function') { String.prototype.format = function() { var formatted = this; for (var arg in arguments) { formatted = formatted.replace("{" + arg + "}", arguments[arg]); } return formatted; }; } 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); }); var modal_open = $(this).attr('data-modal-open'); if (modal_open) $(modal_open).modal('show'); } /* default function to prevent undefined */ function get_next_table_id(){} function get_previous_table_id(){} var datatable_submit_search = function() {}; var current_tab = "table"; var shortcut_url = ''; var show_shortcut_menu = false; var alert_url = ''; var debug = false; var datatables_i18n; var current_modal; var default_search_vector; var pin_search_url; var static_path = '/static/'; var datatables_default = { "processing": true, "serverSide": true, "scrollX": true, "scrollY": true, "searching": false, "scrollCollapse": true, "pageLength": 10, "lengthMenu": [ 5, 10, 25, 50, 100 ] }; var datatables_static_default = { "searching": false, "scrollCollapse": true, "pageLength": 10, "lengthMenu": [ 5, 10, 25, 50, 100 ] }; var show_msg = "Show"; var info_show_msg = "Showing" var entries_msg = "entries"; var info_entries_msg = "entrie(s)"; var table_to_msg = "to"; var table_of_msg = "of"; var no_data_gallery_msg = "No data available in the gallery"; 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."; var added_message = " items added."; var select_only_one_msg = "Select only one item."; var search_pinned_msg = ""; 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_site").change(function(){ $.post('/' + url_path + 'update-current-item/', {item:'site', value:$("#current_site").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_warehouse").change(function(){ $.post('/' + url_path + 'update-current-item/', {item:'warehouse', value:$("#current_warehouse").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_site-shortcut').change(function(){ $("#id_select_site-shortcut").attr( 'title', $('#id_select_site-shortcut').val()); $.post('/' + url_path + 'update-current-item/', {item: "site", value:$("#id_site-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 ); }); $('#id_warehouse-shortcut').change(function(){ $("#id_select_warehouse-shortcut").attr( 'title', $('#id_select_warehouse-shortcut').val()); $.post('/' + url_path + 'update-current-item/', {item: "warehouse", value:$("#id_warehouse-shortcut").val()}, load_opened_shortcut_menu ); }); $('#id_treatment-shortcut').change(function(){ $("#id_select_treatment-shortcut").attr( 'title', $('#id_select_treatment-shortcut').val()); $.post('/' + url_path + 'update-current-item/', {item: "treatment", value:$("#id_treatment-shortcut").val()}, load_opened_shortcut_menu ); }); $('#id_treatmentfile-shortcut').change(function(){ $("#id_select_treatmentfile-shortcut").attr( 'title', $('#id_select_treatmentfile-shortcut').val()); $.post('/' + url_path + 'update-current-item/', {item: "treatmentfile", value:$("#id_treatmentfile-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 add_message(message, message_type, target, clear, dismiss){ if (!message_type) message_type = 'info'; if (!target) target = "#message_list"; if (typeof dismiss == "undefined") dismiss = true; var html = ""; if (!clear) html = $(target).html(); html += '
'; $(target).html(html); } function load_opened_shortcut_menu(){ load_shortcut_menu(true); } function load_shortcut_menu(opened){ if (show_shortcut_menu && shortcut_url){ $('.modal-progress').modal('show'); $.ajax({ url: shortcut_url, cache: false, success:function(html){ init_shortcut_menu(html); load_alerts(); if(opened){ $("#dropdown-toggle-shortcut-menu").click(); } }, error:function(XMLHttpRequest, textStatus, errorThrows){ close_wait(); } }); } else { load_alerts(); } } var load_alerts = function(){ if (!alert_url) return; $('.modal-progress').modal('show'); $.ajax({ url: alert_url, cache: false, success:function(json){ $('.modal-progress').modal('hide'); var html = ""; for (idx in json["alerts"]){ var b = json["alerts"][idx]; var url = "/bookmark/" + b["query_id"] + "/" ; html += ''; html += b["label"]; html += ' '; html += b["number"]; html += ''; html += ' '; } $("#alert-list").html(html); }, error:function(XMLHttpRequest, textStatus, errorThrows){ close_wait(); }, dataType: 'json' }); } 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(); $("#clear-search-button").click(function(){ $(this).parent().parent().children('input').prop("value", ""); $("#id_search_vector").addClass('input-progress'); enable_save(); }); $("#pin-search-button").click(function(){ if (!pin_search_url){ return; } var url = pin_search_url.replace( 'item', $(this).attr('data-item-type') ); var current_search = $(this).parent().parent().children('input').prop("value"); ajax_post( url, {'value': current_search}, "", function(){ display_info(search_pinned_msg); } ); }); $("#submit-search").click(function(){ $(".search_button").click(); }); register_wait_button(); }); var register_wait_button = function(){ $(".wait-button").click(function(){short_wait()}); }; $(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); }); var bookmark_url = ""; var bookmark_delete_url = "/bookmark/delete/"; var load_bookmark_list = function(){ if (!bookmark_url) return; $.get(bookmark_url, function(data) { var bookmark_list = ""; for (idx in data['bookmarks']){ var bookmark = data['bookmarks'][idx]; bookmark_list += '' + '' + bookmark['label'] + '' + '' + '' + ''; } $('#bookmark-list').html(bookmark_list); if (!bookmark_list){ $('#load-bookmark').addClass('disabled'); } else { $('#load-bookmark').removeClass('disabled'); } $("#bookmark-list span a.input-link").click(function(){ $("#id_search_vector").val( $(this).attr('data-query').replace(/''/g, '"')); enable_save(); $(".search_button").click(); return false; }); $("#bookmark-list span a.close").click(function(){ var id = $(this).attr('data-id'); dt_qa_open(bookmark_delete_url + id + '/'); return false; }); }, 'json'); } var autorefresh = false; var autorefresh_message_start = ""; var autorefresh_message_end = ""; function startRefresh() { if (!autorefresh) return; autorefresh_link = $('#autorefreshpage').attr("data-link"); autorefresh_div_id = $('#autorefreshpage').attr("data-div"); $.get(autorefresh_link, function(data) { $('#' + autorefresh_div_id).html(data); }); setTimeout(startRefresh, 10000); } $(document).on("click", '#autorefreshpage', function(){ autorefresh_div_id = $('#autorefreshpage').attr("data-div"); if (!$(this).hasClass('disabled')) { $(this).addClass('disabled'); autorefresh = false; display_info(autorefresh_message_end); $("#" + autorefresh_div_id).find('select').prop('disabled', false); $("#" + autorefresh_div_id).find('input').prop('disabled', false); } else { $(this).removeClass('disabled'); autorefresh = true; setTimeout(startRefresh, 10000); display_info(autorefresh_message_start); $("#" + autorefresh_div_id).find('select').prop('disabled', true); $("#" + autorefresh_div_id).find('input').prop('disabled', true); } }); $(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(); }); var closed_wait = true; function short_wait(check_wait){ if (check_wait && closed_wait){ return } closed_wait = false; $('.modal-progress').modal('show'); $('.progress-1').show('slow'); } function long_wait(check_wait){ if (check_wait && closed_wait){ return } closed_wait = false; $('.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(true); }, 240000); } function close_wait(){ closed_wait = true; $('.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); } register_wait_button(); 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, callback){ $('.modal-progress').modal('show'); $.ajax({ url: url, cache: false, success:function(html){ close_wait(); if (callback) callback(); }, 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(); } } 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 input.parent().attr('data-alt-name'); } var enable_save = function(){ if ($(".search-widget input").val()){ $("#save-search-button").removeClass('disabled'); $("#clear-search-button").removeClass('disabled'); } else { $("#save-search-button").addClass('disabled'); $("#clear-search-button").addClass('disabled'); } } function clear_search_field(){ $("#id_search_vector").val(""); add_message("-", 'info', "#advanced-search-info", true, false); enable_save(); } function _clear_search_criteria_fields(query){ var datatables_length = $(".dataTables_length select").val(); document.getElementById('wizard-form').reset(); // hidden input are not cleared $("#modal-advanced-search input[type='hidden']").each( function(){ if($(this).attr("name") != 'csrfmiddlewaretoken'){ $(this).val("") } } ); $(".dataTables_length select").val(datatables_length); $('.dataTables_length select option[value="' + datatables_length + '"]' ).prop('selected', true); if(query) $("#id_search_vector").val(query); filter_search_fields(); } function update_search_field(){ var query = $("#id_search_vector").val(); if (!query){ query = ""; } 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(); if (v.match( new RegExp(' > ') )){ matches = v.split(' > '); v = matches[matches.length - 1]; } } else if ($(this).filter('input[type="number"]').length){ v = $(this).val(); } if (v == "") return; var term = get_label_from_input(item_for_label); if(!term || typeof term == "undefined") return; if (query) query += " "; query += term + '="' + 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(); } var term = get_label_from_input(item_for_label); if(!term || typeof term == "undefined") return; if (query) query += " "; query += get_label_from_input(item_for_label) + '="' + $(this).text() + '"'; }); } ); _clear_search_criteria_fields(query); if (query){ add_message(query, 'secondary', "#advanced-search-info", true, false); } else { add_message("-", 'secondary', "#advanced-search-info", true, false); } enable_save(); } 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 += '