2018-09-21 12:48:42 +03:00
|
|
|
$(document).on("change", "select", function(e) {
|
|
|
|
send(
|
|
|
|
"/set-lang/",
|
|
|
|
{
|
|
|
|
token: $(this).attr("data-token"),
|
|
|
|
lang: $(this).find(":selected").text()
|
|
|
|
},
|
|
|
|
function () {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
)
|
|
|
|
});
|
|
|
|
|
2018-05-22 10:34:39 +03:00
|
|
|
$('#save-crm').on("submit", function(e) {
|
|
|
|
e.preventDefault();
|
2018-09-28 16:06:42 +03:00
|
|
|
let formData = formDataToObj($(this).serializeArray());
|
|
|
|
disableForm($(this));
|
2018-05-22 10:34:39 +03:00
|
|
|
send(
|
|
|
|
$(this).attr('action'),
|
2018-09-28 16:06:42 +03:00
|
|
|
formData,
|
2018-08-14 17:49:01 +03:00
|
|
|
function (data) {
|
|
|
|
sessionStorage.setItem("createdMsg", data.message);
|
|
|
|
|
|
|
|
document.location.replace(
|
|
|
|
location.protocol.concat("//").concat(window.location.host) + data.url
|
|
|
|
);
|
2018-05-22 10:34:39 +03:00
|
|
|
}
|
|
|
|
)
|
|
|
|
});
|
|
|
|
|
|
|
|
$("#save").on("submit", function(e) {
|
|
|
|
e.preventDefault();
|
2018-09-28 16:06:42 +03:00
|
|
|
let formData = formDataToObj($(this).serializeArray());
|
|
|
|
disableForm($(this));
|
2018-05-22 10:34:39 +03:00
|
|
|
send(
|
|
|
|
$(this).attr('action'),
|
2018-09-28 16:06:42 +03:00
|
|
|
formData,
|
2018-05-23 18:03:11 +03:00
|
|
|
function (data) {
|
2018-09-28 16:06:42 +03:00
|
|
|
M.toast({
|
|
|
|
html: data.message,
|
|
|
|
displayLength: 1000,
|
|
|
|
completeCallback: function(){
|
|
|
|
enableForm();
|
|
|
|
}
|
|
|
|
});
|
2018-05-22 10:34:39 +03:00
|
|
|
}
|
|
|
|
)
|
|
|
|
});
|
|
|
|
|
|
|
|
$("#add-bot").on("submit", function(e) {
|
|
|
|
e.preventDefault();
|
2018-09-28 16:06:42 +03:00
|
|
|
disableForm($(this));
|
2018-05-22 10:34:39 +03:00
|
|
|
send(
|
|
|
|
$(this).attr('action'),
|
2018-05-28 18:16:13 +03:00
|
|
|
{
|
|
|
|
connectionId: parseInt($(this).find('input[name=connectionId]').val()),
|
|
|
|
token: $(this).find('input[name=token]').val(),
|
|
|
|
},
|
2018-05-22 10:34:39 +03:00
|
|
|
function (data) {
|
|
|
|
let bots = $("#bots");
|
|
|
|
if (bots.hasClass("hide")) {
|
|
|
|
bots.removeClass("hide")
|
|
|
|
}
|
|
|
|
$("#bots tbody").append(getBotTemplate(data));
|
2018-05-23 18:03:11 +03:00
|
|
|
$("#token").val("");
|
2018-09-21 12:48:42 +03:00
|
|
|
$('select').formSelect();
|
2018-09-28 16:06:42 +03:00
|
|
|
enableForm();
|
2018-05-22 10:34:39 +03:00
|
|
|
}
|
|
|
|
)
|
|
|
|
});
|
|
|
|
|
2018-06-28 12:17:19 +03:00
|
|
|
$(document).on("click", ".delete-bot", function(e) {
|
2018-08-30 15:07:20 +03:00
|
|
|
e.preventDefault();
|
|
|
|
var but = $(this);
|
|
|
|
var confirmText = JSON.parse(sessionStorage.getItem("confirmText"));
|
2018-09-28 16:06:42 +03:00
|
|
|
but.addClass('disabled');
|
2018-08-30 15:07:20 +03:00
|
|
|
|
|
|
|
$.confirm({
|
|
|
|
title: false,
|
|
|
|
content: confirmText["text"],
|
|
|
|
useBootstrap: false,
|
|
|
|
boxWidth: '30%',
|
|
|
|
type: 'blue',
|
|
|
|
backgroundDismiss: false,
|
|
|
|
backgroundDismissAnimation: 'shake',
|
|
|
|
buttons: {
|
|
|
|
confirm: {
|
|
|
|
text: confirmText["confirm"],
|
|
|
|
action: function () {
|
|
|
|
send("/delete-bot/",
|
|
|
|
{
|
|
|
|
token: but.attr("data-token"),
|
|
|
|
connectionId: parseInt($('input[name=connectionId]').val()),
|
|
|
|
},
|
|
|
|
function () {
|
|
|
|
but.parents("tr").remove();
|
|
|
|
if ($("#bots tbody tr").length === 0) {
|
|
|
|
$("#bots").addClass("hide");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
cancel: {
|
|
|
|
text: confirmText["cancel"],
|
2018-09-28 16:06:42 +03:00
|
|
|
action: function () {
|
|
|
|
but.removeClass('disabled');
|
|
|
|
},
|
2018-08-30 15:07:20 +03:00
|
|
|
},
|
2018-05-22 10:34:39 +03:00
|
|
|
}
|
2018-08-30 15:07:20 +03:00
|
|
|
});
|
2018-05-22 10:34:39 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
function send(url, data, callback) {
|
|
|
|
$.ajax({
|
|
|
|
url: url,
|
|
|
|
data: JSON.stringify(data),
|
|
|
|
type: "POST",
|
|
|
|
success: callback,
|
|
|
|
error: function (res){
|
2018-08-14 17:49:01 +03:00
|
|
|
if (res.status >= 400) {
|
2018-09-28 16:06:42 +03:00
|
|
|
M.toast({
|
|
|
|
html: res.responseJSON.error,
|
|
|
|
displayLength: 1000,
|
|
|
|
completeCallback: enableForm()
|
|
|
|
})
|
2018-05-22 10:34:39 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function getBotTemplate(data) {
|
|
|
|
tmpl =
|
|
|
|
`<tr>
|
2018-08-15 17:56:36 +03:00
|
|
|
<td>${data.name}</td>
|
|
|
|
<td>${data.token}</td>
|
2018-09-21 12:48:42 +03:00
|
|
|
<td>
|
|
|
|
<div class="col s3 sel-lang">
|
|
|
|
<select data-token="${data.token}">
|
|
|
|
<option value="en" selected>en</option>
|
|
|
|
<option value="ru">ru</option>
|
|
|
|
<option value="es">es</option>
|
|
|
|
</select>
|
|
|
|
</div>
|
|
|
|
</td>
|
2018-05-22 10:34:39 +03:00
|
|
|
<td>
|
2018-06-28 12:17:19 +03:00
|
|
|
<button class="delete-bot btn btn-small waves-effect waves-light light-blue darken-1" type="submit" name="action"
|
2018-08-15 17:56:36 +03:00
|
|
|
data-token="${data.token}">
|
2018-06-28 12:17:19 +03:00
|
|
|
<i class="material-icons">delete</i>
|
2018-05-22 10:34:39 +03:00
|
|
|
</button>
|
|
|
|
</td>
|
|
|
|
</tr>`;
|
|
|
|
return tmpl;
|
|
|
|
}
|
|
|
|
|
|
|
|
function formDataToObj(formArray) {
|
|
|
|
let obj = {};
|
|
|
|
for (let i = 0; i < formArray.length; i++){
|
|
|
|
obj[formArray[i]['name']] = formArray[i]['value'];
|
|
|
|
}
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
$( document ).ready(function() {
|
2018-09-21 12:48:42 +03:00
|
|
|
$('select').formSelect();
|
2018-05-22 10:34:39 +03:00
|
|
|
M.Tabs.init(document.getElementById("tab"));
|
|
|
|
if ($("table tbody").children().length === 0) {
|
|
|
|
$("#bots").addClass("hide");
|
|
|
|
}
|
2018-05-23 18:03:11 +03:00
|
|
|
|
2018-08-30 15:07:20 +03:00
|
|
|
if (!sessionStorage.getItem("confirmText")) {
|
|
|
|
let confirmText = {};
|
|
|
|
|
|
|
|
switch (navigator.language.split('-')[0]) {
|
|
|
|
case "ru":
|
|
|
|
confirmText["text"] = "Вы уверены, что хотите удалить?";
|
|
|
|
confirmText["confirm"] = "да";
|
|
|
|
confirmText["cancel"] = "нет";
|
|
|
|
break;
|
|
|
|
case "es":
|
|
|
|
confirmText["text"] = "¿Estás seguro que quieres borrar?";
|
|
|
|
confirmText["confirm"] = "sí";
|
|
|
|
confirmText["cancel"] = "no";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
confirmText["text"] = "Are you sure you want to delete?";
|
|
|
|
confirmText["confirm"] = "yes";
|
|
|
|
confirmText["cancel"] = "no";
|
|
|
|
}
|
|
|
|
|
|
|
|
sessionStorage.setItem("confirmText", JSON.stringify(confirmText));
|
|
|
|
}
|
|
|
|
|
2018-05-24 17:16:21 +03:00
|
|
|
let createdMsg = sessionStorage.getItem("createdMsg");
|
2018-05-23 18:03:11 +03:00
|
|
|
if (createdMsg) {
|
|
|
|
setTimeout(function() {
|
|
|
|
M.toast({html: createdMsg});
|
2018-05-24 17:16:21 +03:00
|
|
|
sessionStorage.removeItem("createdMsg");
|
2018-05-23 18:03:11 +03:00
|
|
|
}, 1000);
|
|
|
|
}
|
2018-05-22 10:34:39 +03:00
|
|
|
});
|
2018-09-28 16:06:42 +03:00
|
|
|
|
|
|
|
function disableForm(elem) {
|
|
|
|
$(document).find('button.btn').addClass('disabled');
|
|
|
|
elem.find(".material-icons").addClass('animate');
|
|
|
|
$("form :input").find(":input").prop("disabled", true);
|
|
|
|
}
|
|
|
|
|
|
|
|
function enableForm() {
|
|
|
|
$(document).find('button.btn').removeClass('disabled');
|
|
|
|
$(document).find(".material-icons").removeClass('animate');
|
|
|
|
$("form :input").prop("disabled", false);
|
|
|
|
}
|