1
0
Fork 0
mirror of https://github.com/Luzifer/vault-otp-ui.git synced 2024-11-08 08:10:11 +00:00
vault-otp-ui/application.js
Knut Ahlers 72dcb026e9
Add error handling to frontend
Signed-off-by: Knut Ahlers <knut@ahlers.me>
2017-06-14 23:15:15 +02:00

121 lines
3.3 KiB
JavaScript

// Generated by CoffeeScript 1.12.4
(function() {
var clipboard, createAlert, createOTPItem, currentTimeout, delay, fetchCodes, filterChange, initializeApplication, refreshTimerProgress, tick, timeLeft, updateCodes;
currentTimeout = 0;
clipboard = void 0;
$(function() {
if ($('body').hasClass('state-signedin')) {
return initializeApplication();
}
});
createOTPItem = function(item) {
var otpItem, tpl;
tpl = $('#tpl-otp-item').html();
otpItem = $(tpl);
otpItem.find('.badge').text(item.code.replace(/^(.{3})(.{3})$/, '$1 $2'));
otpItem.find('.title').text(item.name);
otpItem.find('i.fa').addClass("fa-" + item.icon);
return otpItem.appendTo($('#keylist'));
};
createAlert = function(type, keyword, message, timeout) {
var alrt, tpl;
tpl = $('#tpl-message').html();
alrt = $(tpl);
alrt.addClass("alert-" + type);
alrt.find('.keyword').text(keyword);
alrt.find('.message').text(message);
alrt.appendTo($('#messagecontainer'));
if (timeout > 0) {
return delay(timeout, function() {
return alrt.remove();
});
}
};
delay = function(delayMSecs, fkt) {
return window.setTimeout(fkt, delayMSecs);
};
fetchCodes = function() {
return $.ajax({
url: 'codes.json',
success: updateCodes,
dataType: 'json',
error: function() {
createAlert('danger', 'Oops.', 'Server could not be contacted. Maybe you (or the server) are offline? I will retry in a few seconds.', 5000);
return delay(5000, fetchCodes);
},
statusCode: {
401: function() {
return window.location.reload();
},
500: function() {
createAlert('danger', 'Oops.', 'The server responded with an internal error. I will retry in a few seconds.', 2000);
return delay(2000, fetchCodes);
}
}
});
};
filterChange = function() {
var filter;
filter = $('#filter').val().toLowerCase();
return $('.otp-item').each(function(idx, el) {
if ($(el).find('.title').text().toLowerCase().match(filter) === null) {
return $(el).hide();
} else {
return $(el).show();
}
});
};
initializeApplication = function() {
$('#keylist').empty();
$('#filter').bind('keyup', filterChange);
tick(500, refreshTimerProgress);
return fetchCodes();
};
refreshTimerProgress = function() {
var secondsLeft;
secondsLeft = timeLeft();
return $('#timer').css('width', (secondsLeft / 30 * 100) + "%");
};
tick = function(delay, fkt) {
return window.setInterval(fkt, delay);
};
timeLeft = function() {
var now;
now = new Date().getTime();
return (currentTimeout - now) / 1000;
};
updateCodes = function(data) {
var i, len, ref, token;
currentTimeout = new Date(data.next_wrap).getTime();
if (clipboard) {
clipboard.destroy();
}
$('#keylist').empty();
ref = data.tokens;
for (i = 0, len = ref.length; i < len; i++) {
token = ref[i];
createOTPItem(token);
}
clipboard = new Clipboard('.otp-item', {
text: function(trigger) {
return $(trigger).find('.badge').text().replace(' ', '');
}
});
filterChange();
return delay(timeLeft() * 1000, fetchCodes);
};
}).call(this);