protocolo.dialogo-excluir.js
1.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
Protocolo = Protocolo || {};
Protocolo.DialogoExcluir = (function () {
function DialogoExcluir() {
this.exclusaoBtn = $('.js-exclusao-btn')
}
DialogoExcluir.prototype.iniciar = function () {
this.exclusaoBtn.on('click', onExcluirClicado.bind(this));
if (window.location.search.indexOf('excluido') > -1) {
swal('Pronto!', 'Excluído com sucesso!', 'success');
}
}
function onExcluirClicado(evento) {
event.preventDefault();
var botaoClicado = $(evento.currentTarget);
var url = botaoClicado.data('url');
var objeto = botaoClicado.data('objeto');
swal({
title: 'Tem certeza?',
text: 'Excluir "' + objeto + '"? Você não poderá recuperar depois.',
showCancelButton: true,
confirmButtonColor: '#DD6B55',
confirmButtonText: 'Sim, exclua agora!',
closeOnConfirm: false
}, onExcluirConfirmado.bind(this, url));
}
function onExcluirConfirmado(url) {
$.ajax({
url: url,
method: 'DELETE',
success: onExcluidoSucesso.bind(this),
error: onErroExcluir.bind(this)
});
}
function onExcluidoSucesso() {
var urlAtual = window.location.href;
var separador = urlAtual.indexOf('?') > -1 ? '&' : '?';
var novaUrl = urlAtual.indexOf('excluido') > -1 ? urlAtual : urlAtual + separador + 'excluido';
window.location = novaUrl;
}
function onErroExcluir(e) {
console.log('ahahahah', e.responseText);
swal('Oops!', e.responseText, 'error');
}
return DialogoExcluir;
}());
$(function () {
var dialogo = new Protocolo.DialogoExcluir();
dialogo.iniciar();
});