/*global jQuery, $, forced_title_callback, confirmDialog, progressTimeBar*/
// Show the modal window of an event
function show_event_dialog(event, dialog_page) {
var ajax_file = $("#hidden-ajax_file").val();
if (dialog_page == undefined) {
dialog_page = "general";
}
try {
event = JSON.parse(atob(event));
} catch (e) {
console.error(e);
return;
}
var inputs = $("#events_form :input");
var values = {};
inputs.each(function() {
values[this.name] = $(this).val();
});
// Metaconsole mode flag
var meta = $("#hidden-meta").val();
// History mode flag
var history = $("#hidden-history").val();
jQuery.post(
ajax_file,
{
page: "include/ajax/events",
get_extended_event: 1,
dialog_page: dialog_page,
event: event,
meta: meta,
history: history,
filter: values
},
function(data) {
$("#event_details_window")
.hide()
.empty()
.append(data)
.dialog({
title: event.evento,
resizable: true,
draggable: true,
modal: true,
minWidth: 875,
minHeight: 600,
close: function() {
$("#refrcounter").countdown("resume");
$("div.vc-countdown").countdown("resume");
},
overlay: {
opacity: 0.5,
background: "black"
},
width: 710,
height: 600,
autoOpen: true,
open: function() {
if (
$.ui &&
$.ui.dialog &&
$.ui.dialog.prototype._allowInteraction
) {
var ui_dialog_interaction =
$.ui.dialog.prototype._allowInteraction;
$.ui.dialog.prototype._allowInteraction = function(e) {
if ($(e.target).closest(".select2-dropdown").length)
return true;
return ui_dialog_interaction.apply(this, arguments);
};
}
},
_allowInteraction: function(event) {
return !!$(event.target).is(".select2-input") || this._super(event);
}
})
.show();
$("#refrcounter").countdown("pause");
$("div.vc-countdown").countdown("pause");
forced_title_callback();
},
"html"
);
return false;
}
// Check the response type and open it in a modal dialog or new window
function execute_response(event_id, server_id) {
var response_id = $("#select_custom_response option:selected").val();
var response_parameters_list = $('input[name^="values_params_"]');
var response_parameters = [];
if (response_parameters_list.length > 0) {
response_parameters_list.each(function() {
var acum = {
name: $(this).attr("name"),
value: $(this).val()
};
response_parameters.push(acum);
});
}
var params = [];
params.push({ name: "page", value: "include/ajax/events" });
params.push({ name: "get_response", value: 1 });
params.push({ name: "response_id", value: response_id });
params.push({ name: "server_id", value: server_id });
params.push({ name: "event_id", value: event_id });
params.push({
name: "response_parameters",
value: JSON.stringify(response_parameters)
});
jQuery.ajax({
data: params,
type: "POST",
url: $("#hidden-ajax_file").val(),
dataType: "json",
success: function(response) {
// If cannot get response abort it
if (response == null) {
return [];
}
response["event_id"] = event_id;
response["server_id"] = server_id;
if (response["type"] == "url" && response["new_window"] == 1) {
window.open(response["target"], "_blank");
} else {
show_response_dialog(response_id, response);
}
}
});
}
// Check the response type and open it in a modal dialog or new window
function execute_response_massive(events, response_id, response_parameters) {
var params = [];
params.push({ name: "page", value: "include/ajax/events" });
params.push({ name: "get_response_massive", value: 1 });
params.push({ name: "response_id", value: response_id });
params.push({ name: "events", value: JSON.stringify(events) });
params.push({ name: "response_parameters", value: response_parameters });
jQuery.ajax({
data: params,
type: "POST",
url: $("#hidden-ajax_file").val(),
dataType: "json",
success: function(data) {
// If cannot get response abort it
if (data == null) {
return [];
}
$(".container-massive-events-response").empty();
// Convert to array.
var array_data = Object.entries(data.event_response_targets);
var total_count = array_data.length;
// Each input checkeds.
array_data.forEach(function(element, index) {
var id = element[0];
var target = element[1].target;
var meta = $("#hidden-meta").val();
var event_id = id;
var server_id = 0;
if (meta != 0) {
var split_id = id.split("|");
event_id = split_id[0];
server_id = split_id[1];
}
var end = 0;
if (total_count - 1 === index) {
end = 1;
}
var response = data.event_response;
response["event_id"] = event_id;
response["server_id"] = server_id;
response["target"] = target;
if (response["type"] == "url" && response["new_window"] == 1) {
window.open(response["target"], "_blank");
} else {
var params = [];
params.push({ name: "page", value: "include/ajax/events" });
params.push({ name: "get_row_response_action", value: 1 });
params.push({ name: "response_id", value: response_id });
params.push({ name: "server_id", value: response.server_id });
params.push({ name: "end", value: end });
params.push({ name: "response", value: JSON.stringify(response) });
jQuery.ajax({
data: params,
type: "POST",
url: $("#hidden-ajax_file").val(),
dataType: "html",
success: function(data) {
$(".container-massive-events-response").append(data);
response["event_id"] = event_id;
response["server_id"] = server_id;
response["target"] = target;
var indexstr = event_id;
if (meta != 0) {
indexstr += "-" + server_id;
}
perform_response(
btoa(JSON.stringify(response)),
response_id,
indexstr
);
}
});
}
});
}
});
}
//Show the modal window of an event response
function show_response_dialog(response_id, response) {
var params = [];
params.push({ name: "page", value: "include/ajax/events" });
params.push({ name: "dialogue_event_response", value: 1 });
params.push({ name: "event_id", value: response.event_id });
params.push({ name: "target", value: response.target });
params.push({ name: "response_id", value: response_id });
params.push({ name: "server_id", value: response.server_id });
params.push({ name: "response", value: JSON.stringify(response) });
jQuery.ajax({
data: params,
type: "POST",
url: $("#hidden-ajax_file").val(),
dataType: "html",
success: function(data) {
$("#event_response_window")
.hide()
.empty()
.append(data)
.dialog({
title: $("#select_custom_response option:selected").html(),
resizable: true,
draggable: true,
modal: false,
open: function() {
perform_response(btoa(JSON.stringify(response)), response_id, "");
},
width: response["modal_width"],
height: response["modal_height"],
buttons: []
})
.show();
}
});
}
// Perform a response and put the output into a div
function perform_response(response, response_id, index) {
$("#re_exec_command" + index).hide();
$("#response_loading_command" + index).show();
$("#response_out" + index).html("");
try {
response = JSON.parse(atob(response));
} catch (e) {
console.error(e);
return;
}
var params = [];
params.push({ name: "page", value: "include/ajax/events" });
params.push({ name: "perform_event_response", value: 1 });
params.push({ name: "target", value: response["target"] });
params.push({ name: "response_id", value: response_id });
params.push({ name: "event_id", value: response["event_id"] });
params.push({ name: "server_id", value: response["server_id"] });
params.push({ name: "response", value: JSON.stringify(response) });
jQuery.ajax({
data: params,
type: "POST",
url: $("#hidden-ajax_file").val(),
dataType: "html",
success: function(data) {
var out = data.replace(/[\n|\r]/g, "
");
$("#response_out" + index).html(out);
$("#response_loading_command" + index).hide();
$("#re_exec_command" + index).show();
}
});
return false;
}
// Change the status of an event to new, in process or validated.
function event_change_status(event_ids, server_id) {
var new_status = $("#estado").val();
$("#button-status_button").attr("disabled", "disabled");
$("#response_loading").show();
jQuery.ajax({
data: {
page: "include/ajax/events",
change_status: 1,
event_ids: event_ids,
new_status: new_status,
server_id: server_id
},
type: "POST",
url: $("#hidden-ajax_file").val(),
dataType: "json",
success: function(data) {
$("#button-status_button").removeAttr("disabled");
$("#response_loading").hide();
if ($("#notification_status_success").length) {
$("#notification_status_success").hide();
}
if ($("#notification_status_error").length) {
$("#notification_status_error").hide();
}
if (data.status == "status_ok") {
$.ajax({
type: "POST",
url: "ajax.php",
data: {
page: "include/ajax/events",
get_Acknowledged: 1,
event_id: event_ids,
server_id: server_id
},
success: function(response) {
$(".general_acknowleded").html(response);
}
});
$("#table_events")
.DataTable()
.draw(false);
$("#notification_status_success").show();
$("#general_status")
.find(".general_status")
.text(data.status_title);
$("#general_status")
.find("img")
.attr("src", data.status_img);
} else {
$("#notification_status_error").show();
}
}
});
return false;
}
// Change te owner of an event to one user of empty
function event_change_owner(event_id, server_id) {
var new_owner = $("#id_owner").val();
$("#button-owner_button").attr("disabled", "disabled");
$("#response_loading").show();
jQuery.ajax({
data: {
page: "include/ajax/events",
change_owner: 1,
event_id: event_id,
server_id: server_id,
new_owner: new_owner
},
type: "POST",
url: $("#hidden-ajax_file").val(),
async: true,
dataType: "html",
success: function(data) {
$("#button-owner_button").removeAttr("disabled");
$("#response_loading").hide();
if ($("#notification_owner_success").length) {
$("#notification_owner_success").hide();
}
if ($("#notification_owner_error").length) {
$("#notification_owner_error").hide();
}
if (data == "owner_ok") {
// if (typeof dt_events !== "undefined") {
// dt_events.draw(false);
// }
$("#table_events")
.DataTable()
.draw(false);
$("#notification_owner_success").show();
if (new_owner == -1) {
$("#extended_event_general_page table td.general_owner").html(
"N/A"
);
} else {
$("#extended_event_general_page table td.general_owner").text(
new_owner
);
}
} else {
$("#notification_owner_error").show();
}
}
});
return false;
}
// Save a comment into an event
function event_comment(current_event) {
var event;
try {
event = JSON.parse(atob(current_event));
} catch (e) {
console.error(e);
return;
}
var comment = $("#textarea_comment").val();
if (comment == "") {
show_event_dialog(current_event, "comments", "comment_error");
return false;
}
var params = [];
params.push("page=include/ajax/events");
params.push("add_comment=1");
if (event.event_rep > 0) {
params.push("event_id=" + event.max_id_evento);
} else {
params.push("event_id=" + event.id_evento);
}
params.push("comment=" + comment);
params.push("server_id=" + event.server_id);
$("#button-comment_button").attr("disabled", "disabled");
$("#response_loading").show();
jQuery.ajax({
data: params.join("&"),
type: "POST",
url: $("#hidden-ajax_file").val(),
dataType: "html",
success: function() {
$("#button-comment_button").removeAttr("disabled");
$("#response_loading").hide();
$("#link_comments").click();
}
});
return false;
}
var processed = 0;
function update_event(table, id_evento, type, event_rep, row, server_id) {
var inputs = $("#events_form :input");
var values = {};
var redraw = false;
inputs.each(function() {
values[this.name] = $(this).val();
});
var t1 = new Date();
$.ajax({
async: true,
type: "POST",
url: $("#hidden-ajax_file").val(),
data: {
page: "include/ajax/events",
validate_event: type.validate_event,
in_process_event: type.in_process_event,
delete_event: type.delete_event,
id_evento: id_evento,
server_id: server_id,
event_rep: event_rep,
filter: values
},
success: function(d) {
processed += 1;
var t2 = new Date();
var diff_g = t2.getTime() - t1.getTime();
var diff_s = diff_g / 1000;
if (processed >= $(".chk_val:checked").length) {
// If operation takes less than 2 seconds, redraw.
if (diff_s < 2 || $(".chk_val:checked").length > 1) {
redraw = true;
}
if (redraw) {
$("#" + table)
.DataTable()
.draw(false);
} else {
$(row)
.closest("tr")
.remove();
}
}
},
error: function() {
processed += 1;
}
});
}
// Update events matching current filters and id_evento selected.
function validate_event(table, id_evento, event_rep, row, server_id) {
var button = document.getElementById("val-" + id_evento);
var meta = $("#hidden-meta").val();
if (meta != 0) {
button = document.getElementById("val-" + id_evento + "-" + server_id);
}
if (!button) {
// Button does not exist. Ignore.
processed += 1;
return;
}
button.children[0];
button.children[0].src = "images/spinner.gif";
return update_event(
table,
id_evento,
{ validate_event: 1 },
event_rep,
row,
server_id
);
}
function in_process_event(table, id_evento, event_rep, row, server_id) {
var button = document.getElementById("proc-" + id_evento);
var meta = $("#hidden-meta").val();
if (meta != 0) {
button = document.getElementById("proc-" + id_evento + "-" + server_id);
}
if (!button) {
// Button does not exist. Ignore.
processed += 1;
return;
}
button.children[0];
button.children[0].src = "images/spinner.gif";
return update_event(
table,
id_evento,
{ in_process_event: 1 },
event_rep,
row,
server_id
);
}
function delete_event(table, id_evento, event_rep, row, server_id) {
var button = document.getElementById("del-" + id_evento);
var meta = $("#hidden-meta").val();
if (meta != 0) {
button = document.getElementById("del-" + id_evento + "-" + server_id);
}
if (!button) {
// Button does not exist. Ignore.
processed += 1;
return;
}
var message = "