2018-05-22 10:34:39 +03:00
|
|
|
$('#save-crm').on("submit", function(e) {
|
|
|
|
e.preventDefault();
|
|
|
|
send(
|
|
|
|
$(this).attr('action'),
|
|
|
|
formDataToObj($(this).serializeArray()),
|
|
|
|
function () {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
)
|
|
|
|
});
|
|
|
|
|
|
|
|
$("#save").on("submit", function(e) {
|
|
|
|
e.preventDefault();
|
|
|
|
send(
|
|
|
|
$(this).attr('action'),
|
|
|
|
formDataToObj($(this).serializeArray()),
|
2018-05-23 18:03:11 +03:00
|
|
|
function (data) {
|
|
|
|
M.toast({html: data});
|
2018-05-22 10:34:39 +03:00
|
|
|
}
|
|
|
|
)
|
|
|
|
});
|
|
|
|
|
|
|
|
$("#add-bot").on("submit", function(e) {
|
|
|
|
e.preventDefault();
|
|
|
|
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-05-22 10:34:39 +03:00
|
|
|
}
|
|
|
|
)
|
|
|
|
});
|
|
|
|
|
2018-06-28 12:17:19 +03:00
|
|
|
$(document).on("click", ".delete-bot", function(e) {
|
2018-05-22 10:34:39 +03:00
|
|
|
let but = $(this);
|
2018-06-28 12:17:19 +03:00
|
|
|
send("/delete-bot/",
|
2018-05-22 10:34:39 +03:00
|
|
|
{
|
|
|
|
token: but.attr("data-token"),
|
2018-05-28 18:16:13 +03:00
|
|
|
connectionId: parseInt($('input[name=connectionId]').val()),
|
2018-05-22 10:34:39 +03:00
|
|
|
},
|
|
|
|
function () {
|
2018-06-28 12:17:19 +03:00
|
|
|
but.parents("tr").remove();
|
|
|
|
if ($("#bots tbody tr").length === 0) {
|
|
|
|
$("#bots").addClass("hide");
|
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){
|
|
|
|
if (res.status < 400) {
|
|
|
|
if (res.responseText) {
|
2018-05-23 18:03:11 +03:00
|
|
|
let resObj = JSON.parse(res.responseText);
|
2018-05-24 17:16:21 +03:00
|
|
|
sessionStorage.setItem("createdMsg", resObj.Message);
|
2018-05-23 18:03:11 +03:00
|
|
|
|
2018-05-22 10:34:39 +03:00
|
|
|
document.location.replace(
|
2018-05-23 18:03:11 +03:00
|
|
|
location.protocol.concat("//").concat(window.location.host) + resObj.Url
|
2018-05-22 10:34:39 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
} else {
|
2018-05-23 17:03:37 +03:00
|
|
|
M.toast({html: res.responseText})
|
2018-05-22 10:34:39 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function getBotTemplate(data) {
|
|
|
|
let bot = JSON.parse(data);
|
|
|
|
tmpl =
|
|
|
|
`<tr>
|
|
|
|
<td>${bot.name}</td>
|
|
|
|
<td>${bot.token}</td>
|
|
|
|
<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"
|
|
|
|
data-token="${bot.token}">
|
|
|
|
<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() {
|
|
|
|
M.Tabs.init(document.getElementById("tab"));
|
|
|
|
if ($("table tbody").children().length === 0) {
|
|
|
|
$("#bots").addClass("hide");
|
|
|
|
}
|
2018-05-23 18:03:11 +03:00
|
|
|
|
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
|
|
|
});
|