1
0
mirror of synced 2024-11-22 20:36:01 +03:00
mg-transport-telegram/static/script.js

123 lines
3.4 KiB
JavaScript
Raw Normal View History

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()),
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));
$("#token").val("");
2018-05-22 10:34:39 +03:00
}
)
});
$(document).on("click", ".activity-bot", function(e) {
let but = $(this);
send("/activity-bot/",
{
token: but.attr("data-token"),
active: (but.attr("data-activity") === 'true'),
2018-05-28 18:16:13 +03:00
connectionId: parseInt($('input[name=connectionId]').val()),
2018-05-22 10:34:39 +03:00
},
function () {
if (but.attr("data-activity") === 'true') {
but.find('i').replaceWith('<i class="material-icons">play_arrow</i>');
but.attr("data-activity", "false")
} else {
but.find('i').replaceWith('<i class="material-icons">pause</i>');
2018-05-22 10:34:39 +03:00
but.attr("data-activity", "true")
}
}
)
});
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) {
let resObj = JSON.parse(res.responseText);
2018-05-24 17:16:21 +03:00
sessionStorage.setItem("createdMsg", resObj.Message);
2018-05-22 10:34:39 +03:00
document.location.replace(
location.protocol.concat("//").concat(window.location.host) + resObj.Url
2018-05-22 10:34:39 +03:00
);
}
} else {
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>
<button class="activity-bot btn btn-small waves-effect waves-light light-blue darken-1" type="submit" name="action"
2018-05-22 10:34:39 +03:00
data-activity="true" data-token="${bot.token}">
<i class="material-icons">pause</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-24 17:16:21 +03:00
let createdMsg = sessionStorage.getItem("createdMsg");
if (createdMsg) {
setTimeout(function() {
M.toast({html: createdMsg});
2018-05-24 17:16:21 +03:00
sessionStorage.removeItem("createdMsg");
}, 1000);
}
2018-05-22 10:34:39 +03:00
});