crafty-4/app/frontend/templates/panel/server_webhooks.html

374 lines
13 KiB
HTML
Raw Normal View History

2023-06-03 16:21:23 +00:00
{% extends ../base.html %}
{% block meta %}
{% end %}
{% block title %}Crafty Controller - {{ translate('serverDetails', 'serverDetails', data['lang']) }}{% end %}
{% block content %}
<div class="content-wrapper">
<!-- Page Title Header Starts-->
<div class="row page-title-header">
<div class="col-12">
<div class="page-header">
<h4 class="page-title">
{{ translate('serverDetails', 'serverDetails', data['lang']) }} - {{
data['server_stats']['server_id']['server_name'] }}
<br />
<small>UUID: {{ data['server_stats']['server_id']['server_uuid'] }}</small>
</h4>
</div>
</div>
</div>
<!-- Page Title Header Ends-->
{% include "parts/details_stats.html" %}
<div class="row">
<div class="col-sm-12 grid-margin">
<div class="card">
<div class="card-body pt-0">
<span class="d-none d-sm-block">
{% include "parts/server_controls_list.html" %}
</span>
<span class="d-block d-sm-none">
{% include "parts/m_server_controls_list.html" %}
</span>
<div class="row">
<div class="col-md-12 col-sm-12" style="overflow-x:auto;">
<div class="card">
<div class="card-header header-sm d-flex justify-content-between align-items-center">
2023-06-03 22:38:44 +00:00
<h4 class="card-title"><i class="fas fa-calendar"></i> {{ translate('webhooks', 'webhooks', data['lang']) }} </h4>
2023-06-03 16:21:23 +00:00
{% if data['user_data']['hints'] %}
<span class="too_small" title="{{ translate('serverSchedules', 'cannotSee', data['lang']) }}" ,
data-content="{{ translate('serverSchedules', 'cannotSeeOnMobile', data['lang']) }}" ,
data-placement="bottom"></span>
{% end %}
<div><button
2023-06-03 17:36:38 +00:00
onclick="location.href=`/panel/add_webhook?id={{ data['server_stats']['server_id']['server_id'] }}`"
2023-06-03 22:38:44 +00:00
class="btn btn-info">{{ translate('webhooks', 'new', data['lang']) }}<i
2023-06-03 16:21:23 +00:00
class="fas fa-pencil-alt"></i></button></div>
</div>
<div class="card-body">
2023-06-03 22:38:44 +00:00
<table class="table table-hover d-none d-lg-block responsive-table" id="webhook_table" width="100%" style="table-layout:fixed;">
2023-06-03 22:10:39 +00:00
<thead>
<tr class="rounded">
2023-06-03 22:38:44 +00:00
<th style="width: 10%; min-width: 10px;">{{ translate('webhooks', 'name', data['lang']) }}
2023-06-03 22:10:39 +00:00
</th>
2023-06-03 22:38:44 +00:00
<th style="width: 20%; min-width: 50px;">{{ translate('webhooks', 'type', data['lang']) }}</th>
<th style="width: 50%; min-width: 50px;">{{ translate('webhooks', 'trigger', data['lang']) }}</th>
<th style="width: 10%; min-width: 50px;">{{ translate('webhooks', 'enabled',
2023-06-03 22:10:39 +00:00
data['lang']) }}</th>
2023-06-03 22:38:44 +00:00
<th style="width: 10%; min-width: 50px;">{{ translate('webhooks', 'edit', data['lang'])
2023-06-03 22:10:39 +00:00
}}</th>
</tr>
</thead>
<tbody>
{% for webhook in data['webhooks'] %}
<tr>
<td id="{{webhook.name}}" class="id">
<p>{{webhook.name}}</p>
</td>
<td id="{{webhook.webhook_type}}" class="type">
<p>{{webhook.webhook_type}}</p>
</td>
<td id="{{webhook.trigger}}" class="trigger" style="overflow: scroll; max-width: 30px;">
<p style="overflow: scroll;" class="no-scroll">{{webhook.trigger}}</p>
</td>
<td id="{{webhook.enabled}}" class="enabled">
<input style="width: 10px !important;" type="checkbox" class="webhook-enabled-toggle" data-webhook-id="{{webhook.id}}" data-webhook-enabled="{{ 'true' if webhook.enabled else 'false' }}">
</td>
<td id="webhook_edit" class="action">
<button onclick="window.location.href='/panel/webhook_edit?id={{ data['server_stats']['server_id']['server_id'] }}&webhook_id={{webhook.id}}'" class="btn btn-info">
<i class="fas fa-pencil-alt"></i>
</button>
<br>
<br>
<button data-webhook={{ webhook.id }} class="btn btn-danger del_button">
<i class="fas fa-trash" aria-hidden="true"></i>
</button>
2023-06-03 22:38:44 +00:00
<button data-webhook={{ webhook.id }} data-toggle="tooltip" title="{{ translate('webhooks', 'run', data['lang']) }}" class="btn btn-outline-warning test-socket">
<i class="fa-solid fa-vial"></i>
</button>
</td>
</tr>
{% end %}
</tbody>
</table>
<table class="table table-hover d-block d-lg-none responsive-table" id="webhook_table_mini" width="100%" style="table-layout:fixed;">
<thead>
<tr class="rounded">
<th style="width: 33.33%; min-width: 10px;">Name
</th>
<th style="width: 33.33%; min-width: 50px;">{{ translate('webhooks', 'enabled',
data['lang']) }}</th>
<th style="width: 33.33%; min-width: 50px;">{{ translate('webhooks', 'edit', data['lang'])
}}</th>
</tr>
</thead>
<tbody>
{% for webhook in data['webhooks'] %}
<tr>
<td id="{{webhook.name}}" class="id">
<p>{{webhook.name}}</p>
</td>
<td id="{{webhook.enabled}}" class="enabled">
<input style="width: 10px !important;" type="checkbox" class="webhook-enabled-toggle" data-webhook-id="{{webhook.id}}" data-webhook-enabled="{{ 'true' if webhook.enabled else 'false' }}">
</td>
<td id="webhook_edit" class="action">
<button onclick="window.location.href='/panel/webhook_edit?id={{ data['server_stats']['server_id']['server_id'] }}&webhook_id={{webhook.id}}'" class="btn btn-info">
<i class="fas fa-pencil-alt"></i>
</button>
<br>
<br>
<button data-webhook={{ webhook.id }} class="btn btn-danger del_button">
<i class="fas fa-trash" aria-hidden="true"></i>
</button>
<button data-webhook={{ webhook.id }} data-toggle="tooltip" title="{{ translate('webhooks', 'run', data['lang']) }}" class="btn btn-outline-warning test-socket">
2023-06-03 22:10:39 +00:00
<i class="fa-solid fa-vial"></i>
</button>
</td>
</tr>
{% end %}
</tbody>
</table>
2023-06-03 16:21:23 +00:00
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<style>
.popover-body {
color: white !important;
;
}
.toggle-handle {
background-color: white !important;
}
.toggle-on {
color: black !important;
}
.toggle {
height: 0px !important;
}
</style>
</div>
<style>
/* Hide scrollbar for Chrome, Safari and Opera */
td::-webkit-scrollbar {
display: none;
}
/* Hide scrollbar for IE, Edge and Firefox */
td {
-ms-overflow-style: none;
/* IE and Edge */
scrollbar-width: none;
/* Firefox */
}
</style>
<!-- content-wrapper ends -->
{% end %}
{% block js %}
<script>
2023-06-03 22:38:44 +00:00
$(document).ready(function () {
console.log('ready for JS!')
$('#webhook_table').DataTable({
'order': [4, 'asc'],
}
);
});
$(document).ready(function () {
console.log('ready for JS!')
$('#webhook_table_mini').DataTable({
'order': [2, 'asc']
}
);
document.getElementById('webhook_table_mini_wrapper').hidden = true;
});
$(document).ready(function () {
$('[data-toggle="popover"]').popover();
if ($(window).width() < 1000) {
$('.too_small').popover("show");
document.getElementById('webhook_table_wrapper').hidden = true;
document.getElementById('webhook_table_mini_wrapper').hidden = false;
}
});
$(window).ready(function () {
$('body').click(function () {
$('.too_small').popover("hide");
});
});
$(window).resize(function () {
// This will execute whenever the window is resized
if ($(window).width() < 1000) {
$('.too_small').popover("show");
document.getElementById('webhook_table_wrapper').hidden = true;
document.getElementById('webhook_table_mini_wrapper').hidden = false;
}
else {
$('.too_small').popover("hide");
document.getElementById('webhook_table_wrapper').hidden = false;
document.getElementById('webhook_table_mini_wrapper').hidden = true;
} // New width
});
2023-06-03 16:21:23 +00:00
function debounce(func, timeout = 300) {
let timer;
return (...args) => {
clearTimeout(timer);
timer = setTimeout(() => { func.apply(this, args); }, timeout);
};
}
$(() => {
2023-06-03 22:10:39 +00:00
$('.webhook-enabled-toggle').bootstrapToggle({
2023-06-03 16:21:23 +00:00
on: 'Yes',
off: 'No',
onstyle: 'success',
offstyle: 'danger',
})
2023-06-03 22:10:39 +00:00
$('.webhook-enabled-toggle').each(function () {
const enabled = JSON.parse(this.getAttribute('data-webhook-enabled'));
2023-06-03 16:21:23 +00:00
$(this).bootstrapToggle(enabled ? 'on' : 'off')
})
2023-06-03 22:10:39 +00:00
$('.webhook-enabled-toggle').change(function () {
const id = this.getAttribute('data-webhook-id');
2023-06-03 16:21:23 +00:00
const enabled = this.checked;
2023-06-03 22:10:39 +00:00
fetch(`/api/v2/servers/{{data['server_id']}}/webhook/${id}`, {
2023-06-03 16:21:23 +00:00
method: 'PATCH',
body: JSON.stringify({ enabled }),
headers: {
'Content-Type': 'application/json',
},
})
});
})
const serverId = new URLSearchParams(document.location.search).get('id')
</script>
<script>
//used to get cookies from browser - this is part of tornados xsrf protection - it's for extra security
function getCookie(name) {
var r = document.cookie.match("\\b" + name + "=([^;]*)\\b");
return r ? r[1] : undefined;
}
$(document).ready(function () {
console.log("ready!");
});
$(".del_button").click(function () {
2023-06-03 22:10:39 +00:00
var webhook_id = $(this).data('webhook');
bootbox.confirm({
2023-06-03 22:38:44 +00:00
message: "{{ translate('webhooks', 'areYouSureDel', data['lang']) }}",
2023-06-03 22:10:39 +00:00
buttons: {
cancel: {
label: '<i class="fas fa-times"></i> {{ translate("serverSchedules", "cancel", data['lang']) }}'
},
confirm: {
className: 'btn-outline-danger',
label: '<i class="fas fa-check"></i> {{ translate("serverSchedules", "confirm", data['lang']) }}'
}
},
callback: function (result) {
console.log(result);
if (result == true) {
del_hook(webhook_id, serverId);
}
}
});
});
2023-06-03 16:21:23 +00:00
2023-06-03 22:10:39 +00:00
$(".test-socket").click(function () {
var webhook_id = $(this).data('webhook');
2023-06-03 16:21:23 +00:00
bootbox.confirm({
2023-06-03 22:38:44 +00:00
message: "{{ translate('webhooks', 'areYouSureRun', data['lang']) }}",
2023-06-03 16:21:23 +00:00
buttons: {
cancel: {
label: '<i class="fas fa-times"></i> {{ translate("serverSchedules", "cancel", data['lang']) }}'
},
confirm: {
className: 'btn-outline-danger',
label: '<i class="fas fa-check"></i> {{ translate("serverSchedules", "confirm", data['lang']) }}'
}
},
callback: function (result) {
console.log(result);
if (result == true) {
2023-06-03 22:10:39 +00:00
test_hook(webhook_id, serverId);
2023-06-03 16:21:23 +00:00
}
}
});
});
2023-06-03 22:10:39 +00:00
async function test_hook(webhook_id, id) {
var token = getCookie("_xsrf")
let res = await fetch(`/api/v2/servers/${id}/webhook/${webhook_id}/`, {
method: 'POST',
headers: {
'token': token,
},
});
let responseData = await res.json();
if (responseData.status === "ok") {
bootbox.alert("Webhook Sent!")
}else{
console.log(responseData);
bootbox.alert({
title: responseData.status,
message: responseData.error
});
}
}
2023-06-03 16:21:23 +00:00
2023-06-03 22:10:39 +00:00
async function del_hook(webhook_id, id) {
2023-06-03 16:21:23 +00:00
var token = getCookie("_xsrf")
2023-06-03 22:10:39 +00:00
let res = await fetch(`/api/v2/servers/${id}/webhook/${webhook_id}`, {
2023-06-03 16:21:23 +00:00
method: 'DELETE',
headers: {
'token': token,
},
});
2023-06-03 22:10:39 +00:00
let responseData = await res.json();
2023-06-03 16:21:23 +00:00
if (responseData.statusText === "OK") {
window.location.reload();
2023-06-03 22:10:39 +00:00
}else{
bootbox.alert({
title: responseData.stats,
message: responseData.error
});
2023-06-03 16:21:23 +00:00
}
}
</script>
{% end %}