2017-06-14 18:40:12 +00:00
|
|
|
// Generated by CoffeeScript 1.12.4
|
|
|
|
(function() {
|
2018-03-23 11:32:03 +00:00
|
|
|
var clipboard, createAlert, createOTPItem, currentTimeout, delay, fetchCodes, fetchInProgress, filterChange, initializeApplication, iterationCurrent, iterationNext, preFetch, refreshTimerProgress, serverConnectionError, tick, timeLeft, updateCodes, updatePreFetch;
|
2017-06-14 18:40:12 +00:00
|
|
|
|
|
|
|
currentTimeout = 0;
|
|
|
|
|
2018-03-23 11:32:03 +00:00
|
|
|
clipboard = null;
|
|
|
|
|
|
|
|
preFetch = null;
|
|
|
|
|
|
|
|
fetchInProgress = false;
|
|
|
|
|
|
|
|
serverConnectionError = false;
|
|
|
|
|
|
|
|
iterationCurrent = 'current';
|
|
|
|
|
|
|
|
iterationNext = 'next';
|
2017-06-14 18:40:12 +00:00
|
|
|
|
|
|
|
$(function() {
|
|
|
|
if ($('body').hasClass('state-signedin')) {
|
|
|
|
return initializeApplication();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
createOTPItem = function(item) {
|
|
|
|
var otpItem, tpl;
|
2017-06-14 21:15:15 +00:00
|
|
|
tpl = $('#tpl-otp-item').html();
|
2017-06-14 18:40:12 +00:00
|
|
|
otpItem = $(tpl);
|
|
|
|
otpItem.find('.badge').text(item.code.replace(/^(.{3})(.{3})$/, '$1 $2'));
|
|
|
|
otpItem.find('.title').text(item.name);
|
2017-06-14 20:21:31 +00:00
|
|
|
otpItem.find('i.fa').addClass("fa-" + item.icon);
|
2017-06-14 18:40:12 +00:00
|
|
|
return otpItem.appendTo($('#keylist'));
|
|
|
|
};
|
|
|
|
|
2017-06-14 21:15:15 +00:00
|
|
|
createAlert = function(type, keyword, message, timeout) {
|
|
|
|
var alrt, tpl;
|
|
|
|
tpl = $('#tpl-message').html();
|
|
|
|
alrt = $(tpl);
|
2018-03-23 18:06:48 +00:00
|
|
|
alrt.find('.alert').addClass("alert-" + type);
|
|
|
|
alrt.find('.alert').find('.keyword').text(keyword);
|
|
|
|
alrt.find('.alert').find('.message').text(message);
|
2017-06-14 21:15:15 +00:00
|
|
|
alrt.appendTo($('#messagecontainer'));
|
|
|
|
if (timeout > 0) {
|
|
|
|
return delay(timeout, function() {
|
|
|
|
return alrt.remove();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-06-14 18:40:12 +00:00
|
|
|
delay = function(delayMSecs, fkt) {
|
|
|
|
return window.setTimeout(fkt, delayMSecs);
|
|
|
|
};
|
|
|
|
|
2018-03-23 11:32:03 +00:00
|
|
|
fetchCodes = function(iteration) {
|
|
|
|
var data, successFunc;
|
|
|
|
if (fetchInProgress) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
fetchInProgress = true;
|
|
|
|
if (iteration === iterationCurrent) {
|
|
|
|
successFunc = updateCodes;
|
|
|
|
} else {
|
|
|
|
successFunc = updatePreFetch;
|
|
|
|
}
|
|
|
|
if (iteration === iterationCurrent && preFetch !== null) {
|
|
|
|
data = preFetch;
|
|
|
|
preFetch = null;
|
|
|
|
successFunc(data);
|
|
|
|
return;
|
|
|
|
}
|
2017-06-14 18:40:12 +00:00
|
|
|
return $.ajax({
|
2018-03-23 11:32:03 +00:00
|
|
|
url: "codes.json?it=" + iteration,
|
|
|
|
success: successFunc,
|
2017-06-14 21:15:15 +00:00
|
|
|
dataType: 'json',
|
|
|
|
error: function() {
|
2018-03-23 11:32:03 +00:00
|
|
|
fetchInProgress = false;
|
|
|
|
createAlert('danger', 'Oops.', 'Server could not be contacted. Maybe you (or the server) are offline? Reload to try again.', 0);
|
|
|
|
return serverConnectionError = true;
|
2017-06-14 21:15:15 +00:00
|
|
|
},
|
|
|
|
statusCode: {
|
|
|
|
401: function() {
|
|
|
|
return window.location.reload();
|
|
|
|
},
|
|
|
|
500: function() {
|
2018-03-23 11:32:03 +00:00
|
|
|
fetchInProgress = false;
|
|
|
|
createAlert('danger', 'Oops.', 'The server responded with an internal error. Reload to try again.', 0);
|
|
|
|
return serverConnectionError = true;
|
2017-06-14 21:15:15 +00:00
|
|
|
}
|
|
|
|
}
|
2017-06-14 18:40:12 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
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);
|
2018-03-23 11:32:03 +00:00
|
|
|
return fetchCodes(iterationCurrent);
|
2017-06-14 18:40:12 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
refreshTimerProgress = function() {
|
|
|
|
var secondsLeft;
|
|
|
|
secondsLeft = timeLeft();
|
2018-03-23 11:32:03 +00:00
|
|
|
$('#timer').css('width', (secondsLeft / 30 * 100) + "%");
|
|
|
|
if (secondsLeft < 10 && preFetch === null && !serverConnectionError) {
|
|
|
|
return fetchCodes(iterationNext);
|
|
|
|
}
|
2017-06-14 18:40:12 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
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();
|
|
|
|
}
|
2018-03-23 18:16:56 +00:00
|
|
|
$('#initLoader').hide();
|
2017-06-14 18:40:12 +00:00
|
|
|
$('#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(' ', '');
|
|
|
|
}
|
|
|
|
});
|
2018-03-23 18:06:48 +00:00
|
|
|
clipboard.on('success', function(e) {
|
2018-03-23 19:23:47 +00:00
|
|
|
createAlert('success', 'Success:', 'Code copied to clipboard', 1000);
|
2018-03-23 18:06:48 +00:00
|
|
|
return e.blur();
|
|
|
|
});
|
|
|
|
clipboard.on('error', function(e) {
|
2018-03-23 19:23:47 +00:00
|
|
|
return createAlert('danger', 'Oops.', 'Copy to clipboard failed', 2000);
|
2018-03-23 18:06:48 +00:00
|
|
|
});
|
2017-06-14 18:40:12 +00:00
|
|
|
filterChange();
|
2018-03-23 11:32:03 +00:00
|
|
|
delay(timeLeft() * 1000, function() {
|
|
|
|
return fetchCodes(iterationCurrent);
|
|
|
|
});
|
|
|
|
return fetchInProgress = false;
|
|
|
|
};
|
|
|
|
|
|
|
|
updatePreFetch = function(data) {
|
|
|
|
preFetch = data;
|
|
|
|
return fetchInProgress = false;
|
2017-06-14 18:40:12 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}).call(this);
|