(function ($, document) {
function revoke(page, key) {
Dashboard.confirm(Globalize.translate('MessageConfirmRevokeApiKey'), Globalize.translate('HeaderConfirmRevokeApiKey'), function (result) {
if (result) {
Dashboard.showLoadingMsg();
ApiClient.ajax({
type: "DELETE",
url: ApiClient.getUrl('Auth/Keys/' + key)
}).done(function () {
loadData(page);
});
}
});
}
function renderKeys(page, keys, users) {
var rows = keys.map(function (item) {
var html = '';
html += '
';
html += '
';
html += '';
html += '
';
html += '
';
html += (item.AccessToken);
html += '
';
html += '
';
html += (item.AppName || '');
html += '
';
html += '
';
html += (item.DeviceName || '');
html += '
';
html += '
';
var user = users.filter(function (u) {
return u.Id == item.UserId;
})[0];
if (user) {
html += user.Name;
}
html += '
';
html += '
';
var date = parseISO8601Date(item.DateCreated, { toLocal: true });
html += date.toLocaleDateString() + ' ' + LiveTvHelpers.getDisplayTime(date);
html += '
';
html += '
';
return html;
}).join('');
var elem = $('.resultBody', page).html(rows).parents('.tblApiKeys').table("refresh").trigger('create');
$('.btnRevoke', elem).on('click', function () {
revoke(page, this.getAttribute('data-token'));
});
Dashboard.hideLoadingMsg();
}
function loadData(page) {
Dashboard.showLoadingMsg();
ApiClient.getUsers().done(function (users) {
ApiClient.getJSON(ApiClient.getUrl('Auth/Keys')).done(function (result) {
renderKeys(page, result.Items, users);
});
});
}
$(document).on('pageinit', "#serverSecurityPage", function () {
var page = this;
$('.btnNewKey', page).on('click', function () {
$('.newKeyPanel', page).panel('toggle');
$('#txtAppName', page).val('').focus();
});
}).on('pageshow', "#serverSecurityPage", function () {
var page = this;
loadData(page);
});
window.ServerSecurityPage = {
onSubmit: function(e) {
var form = this;
var page = $(form).parents('.page');
Dashboard.showLoadingMsg();
ApiClient.ajax({
type: "POST",
url: ApiClient.getUrl('Auth/Keys/', {
App: $('#txtAppName', form).val()
})
}).done(function () {
$('.newKeyPanel', page).panel('close');
loadData(page);
});
return false;
}
};
})(jQuery, document);