Created
March 14, 2019 15:57
-
-
Save leandro/4e6cc3282a34f2befc341819508e80b1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var modalQuarteirao = $('#modal-quarteirao') | |
, botaoQuarteiraoCancelar = modalQuarteirao.find('.cancelar'); | |
new ProtetorDeAlteracoes(modalQuarteirao, botaoQuarteiraoCancelar); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
!function($) { | |
var containersMonitoraveis = [] | |
, mapaDeVisibilidade = [] | |
, checksums = [] | |
, timer; | |
function ProtetorDeAlteracoes(camposContainer, botaoCancelar) { | |
this.container = camposContainer; | |
this.botao = botaoCancelar; | |
if (!containersMonitoraveis.length) { | |
timer = setInterval(monitoraContainers, 200); | |
} | |
containersMonitoraveis.push(camposContainer[0]); | |
mapaDeVisibilidade.push(camposContainer.is(':visible')); | |
checksums.push(checksumDeForm(camposContainer)); | |
atrelaEventoAoBotaoCancelar(botaoCancelar, camposContainer); | |
} | |
function atrelaEventoAoBotaoCancelar(botao, container) { | |
var jqueryKeys, eventsKey, eventsObj, clickEvents; | |
botao.on('click', checaAlteracoes.bind(container)); | |
jqueryKeys = Object.keys(botao[0]) | |
.filter(function(key) { return /^jQuery\d+$/.test(key); }); | |
eventsKey = jqueryKeys | |
.filter(function(key) { return !!botao[0][key].events })[0]; | |
eventsObj = botao[0][eventsKey]; | |
clickEvents = eventsObj.events.click; | |
clickEvents.unshift(clickEvents.pop()); | |
} | |
function checaAlteracoes(ev) { | |
var index = containersMonitoraveis.indexOf(this[0]) | |
, pergunta = 'Você deseja cancelar as alterações feitas nesses campos?'; | |
if (checksums[index] == checksumDeForm(this)) return; | |
if (confirm(pergunta)) return; | |
ev.stopImmediatePropagation(); | |
} | |
function armazenaChecksumDosValoresIniciais(container, index) { | |
checksums[index] = checksumDeForm(container); | |
} | |
function checksumDeForm(container) { | |
var campos = container.find('input').toArray() | |
, valores = $.map(campos, function(campo) { return $(campo).val(); }) | |
, checksum = Util.string.checksum(valores.join('')); | |
return checksum; | |
} | |
function monitoraContainers() { | |
var visivel, container; | |
$(containersMonitoraveis).each(function(i, el) { | |
container = $(this); | |
visivel = container.is(':visible'); | |
if (visivel == mapaDeVisibilidade[i]) return; | |
mapaDeVisibilidade[i] = visivel; | |
if (!visivel) return; | |
armazenaChecksumDosValoresIniciais(container, i); | |
}); | |
} | |
window.ProtetorDeAlteracoes = ProtetorDeAlteracoes; | |
}(jQuery); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
!function() { | |
var util = { | |
/* , ... */ | |
string: { | |
/* , ... */ | |
checksum: function(string) { | |
var hash = 0, i = 0, t = string.length; | |
for (; i < t; i++) { | |
var character = string.charCodeAt(i); | |
hash = ((hash << 5) - hash) + character; | |
hash = hash & hash; | |
} | |
return hash; | |
} | |
} | |
/* , ... */ | |
} | |
window.Util = util; | |
}(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment