'use strict';
var getIpInfo = function (ip) {
return Promise.all([
$.ajax({
url: '/api/ip/' + ip + '?json',
type: 'GET',
dataType: 'json'
}),
$.ajax({
url: '/api/ipwhois/' + ip + '?json',
type: 'GET',
dataType: 'json'
})
]);
};
var modal;
var openModal = function (content, closeButton, onCloseCallback) {
modal = $('
');
if (closeButton) {
var cb = $('×
');
$(cb).on('click', function () {
onCloseCallback && onCloseCallback();
closeModal();
});
modal.find('.modal-content').append(cb);
}
modal.find('.modal-content').append(content);
$('body').append(modal);
modal.show();
};
var closeModal = function () {
modal.remove();
};
var startSpinner = function (el) {
var spinner = document.createElement('div');
spinner.className = 'spinner-wrapper';
spinner.innerHTML = '';
spinner.firstChild.innerHTML = '';
el.prepend(spinner);
};
var stopSpinner = function (el) {
el.find('.spinner-wrapper').remove();
};
var bindIpModal = function (link) {
var ip = link.html();
var content = $('');
content.append('IP: ' + ip + '
');
content.append('');
var ic = content.find('.inner-content');
link.on('click', function (event) {
event.preventDefault();
startSpinner(ic);
getIpInfo(ip).then(function (resp) {
var keyLabel = {
as: 'AS',
attacks: 'Targets',
as_name: 'AS Name',
country: 'Country',
hostname: 'Hostname',
count: 'Reports',
maxdate: 'Most Recent Report',
mindate: 'First Reported',
network: 'Network',
updated: 'Updated'
}
var table = $('');
var body = table.find('tbody');
var i = 0;
$.each($.extend(resp[0]['ip'], resp[1]['ipwhois']), function (key, value) {
var className = i % 2 === 0 ? 'evenrow' : 'oddrow';
if (typeof keyLabel[key] !== 'undefined') {
body.append(
'| ' + keyLabel[key] + ': | ' + (value || '- none -') + ' |
'
)
i++;
}
});
ic.append(table);
}).catch(function () {
ic.append('Unable to fetch IP info. Please try again later.
');
}).finally(function () {
ic.append(
''
);
stopSpinner(ic);
});
openModal(content, true, function () {
ic.empty()
});
});
};
$('.ip-modal').each(function (i, link) {
bindIpModal($(link));
});