/* global $ uniqId*/ /* exported load_modal */ /*JS to Show user modals : - Confirm dialogs. - Display dialogs. - Load modal windows. - Logo Previews. - General show messages. */ var ENTERPRISE_DIR = "enterprise"; /** * Display a dialog with an image * * @param {string} icon_name The name of the icon you will display * @param {string} icon_path The path to the icon * @param {Object} incoming_options All options * grayed: {bool} True to display the background black * title {string} 'Logo preview' by default */ function logo_preview(icon_name, icon_path, incoming_options) { // Get the options options = { grayed: false, title: "Logo preview" }; $.extend(options, incoming_options); if (icon_name == "") return; $dialog = $("
"); $image = $(''); $image.css("max-width", "500px").css("max-height", "500px"); try { $dialog .hide() .html($image) .dialog({ title: options.title, resizable: true, draggable: true, modal: true, dialogClass: options.grayed ? "dialog-grayed" : "", overlay: { opacity: 0.5, background: "black" }, minHeight: 1, width: $image.width, close: function() { $dialog.empty().remove(); } }) .show(); } catch (err) { // console.log(err); } } // Advanced Form control. // eslint-disable-next-line no-unused-vars function load_modal(settings) { var AJAX_RUNNING = 0; var data = new FormData(); if (settings.extradata) { settings.extradata.forEach(function(item) { if (item.value != undefined) { if (item.value instanceof Object || item.value instanceof Array) { data.append(item.name, JSON.stringify(item.value)); } else { data.append(item.name, item.value); } } }); } data.append("page", settings.onshow.page); data.append("method", settings.onshow.method); if (settings.onshow.extradata != undefined) { data.append("extradata", JSON.stringify(settings.onshow.extradata)); } if (settings.target == undefined) { var uniq = uniqId(); var div = document.createElement("div"); div.id = "div-modal-" + uniq; div.style.display = "none"; if (document.getElementById("main") == null) { // MC env. document.getElementById("page").append(div); } else { document.getElementById("main").append(div); } var id_modal_target = "#div-modal-" + uniq; settings.target = $(id_modal_target); } var width = 630; if (settings.onshow.width) { width = settings.onshow.width; } if (settings.modal.overlay == undefined) { settings.modal.overlay = { opacity: 0.5, background: "black" }; } if (settings.beforeClose == undefined) { settings.beforeClose = function() {}; } settings.target.html("Loading modal..."); settings.target .dialog({ title: "Loading", close: false, width: 200, buttons: [] }) .show(); var required_buttons = []; if (settings.modal.cancel != undefined) { //The variable contains a function // that is responsible for executing the method it receives from settings // which confirms the closure of a modal var cancelModal = function() { settings.target.dialog("close"); if (AJAX_RUNNING) return; AJAX_RUNNING = 1; var formdata = new FormData(); formdata.append("page", settings.oncancel.page); formdata.append("method", settings.oncancel.method); $.ajax({ method: "post", url: settings.url, processData: false, contentType: false, data: formdata, success: function(data) { if (typeof settings.oncancel.callback == "function") { settings.oncancel.callback(data); settings.target.dialog("close"); } AJAX_RUNNING = 0; }, error: function(data) { // console.log(data); AJAX_RUNNING = 0; } }); }; required_buttons.push({ class: "ui-widget ui-state-default ui-corner-all ui-button-text-only sub upd submit-cancel", text: settings.modal.cancel, click: function() { if (settings.oncancel != undefined) { if (typeof settings.oncancel.confirm == "function") { //receive function settings.oncancel.confirm(cancelModal); } else if (settings.oncancel != undefined) { cancelModal(); } } else { $(this).dialog("close"); } } }); } if (settings.modal.ok != undefined) { var btnClickHandler = function(d) { if (AJAX_RUNNING) return; if (settings.onsubmit != undefined) { if (settings.onsubmit.preaction != undefined) { settings.onsubmit.preaction(); } AJAX_RUNNING = 1; if (settings.onsubmit.dataType == undefined) { settings.onsubmit.dataType = "html"; } var formdata = new FormData(); if (settings.extradata) { settings.extradata.forEach(function(item) { if (item.value != undefined) formdata.append(item.name, item.value); }); } formdata.append("page", settings.onsubmit.page); formdata.append("method", settings.onsubmit.method); var flagError = false; if (Array.isArray(settings.form) === false) { $("#" + settings.form + " :input").each(function() { if (this.checkValidity() === false) { var select2 = $(this).attr("data-select2-id"); if (typeof select2 !== typeof undefined && select2 !== false) { $(this) .next() .attr("title", this.validationMessage); $(this) .next() .tooltip({ tooltipClass: "uitooltip", position: { my: "right bottom", at: "right top", using: function(position, feedback) { $(this).css(position); $("