mirror of https://github.com/BTCPrivate/z-nomp.git
101 lines
3.3 KiB
JavaScript
101 lines
3.3 KiB
JavaScript
|
var docCookies = {
|
||
|
getItem: function (sKey) {
|
||
|
return decodeURIComponent(document.cookie.replace(new RegExp("(?:(?:^|.*;)\\s*" + encodeURIComponent(sKey).replace(/[\-\.\+\*]/g, "\\$&") + "\\s*\\=\\s*([^;]*).*$)|^.*$"), "$1")) || null;
|
||
|
},
|
||
|
setItem: function (sKey, sValue, vEnd, sPath, sDomain, bSecure) {
|
||
|
if (!sKey || /^(?:expires|max\-age|path|domain|secure)$/i.test(sKey)) { return false; }
|
||
|
var sExpires = "";
|
||
|
if (vEnd) {
|
||
|
switch (vEnd.constructor) {
|
||
|
case Number:
|
||
|
sExpires = vEnd === Infinity ? "; expires=Fri, 31 Dec 9999 23:59:59 GMT" : "; max-age=" + vEnd;
|
||
|
break;
|
||
|
case String:
|
||
|
sExpires = "; expires=" + vEnd;
|
||
|
break;
|
||
|
case Date:
|
||
|
sExpires = "; expires=" + vEnd.toUTCString();
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
document.cookie = encodeURIComponent(sKey) + "=" + encodeURIComponent(sValue) + sExpires + (sDomain ? "; domain=" + sDomain : "") + (sPath ? "; path=" + sPath : "") + (bSecure ? "; secure" : "");
|
||
|
return true;
|
||
|
},
|
||
|
removeItem: function (sKey, sPath, sDomain) {
|
||
|
if (!sKey || !this.hasItem(sKey)) { return false; }
|
||
|
document.cookie = encodeURIComponent(sKey) + "=; expires=Thu, 01 Jan 1970 00:00:00 GMT" + ( sDomain ? "; domain=" + sDomain : "") + ( sPath ? "; path=" + sPath : "");
|
||
|
return true;
|
||
|
},
|
||
|
hasItem: function (sKey) {
|
||
|
return (new RegExp("(?:^|;\\s*)" + encodeURIComponent(sKey).replace(/[\-\.\+\*]/g, "\\$&") + "\\s*\\=")).test(document.cookie);
|
||
|
}
|
||
|
};
|
||
|
|
||
|
var password = docCookies.getItem('password');
|
||
|
|
||
|
|
||
|
function showLogin(){
|
||
|
$('#adminCenter').hide();
|
||
|
$('#passwordForm').show();
|
||
|
}
|
||
|
|
||
|
function showAdminCenter(){
|
||
|
$('#passwordForm').hide();
|
||
|
$('#adminCenter').show();
|
||
|
}
|
||
|
|
||
|
function tryLogin(){
|
||
|
apiRequest('pools', {}, function(response){
|
||
|
showAdminCenter();
|
||
|
displayMenu(response.result)
|
||
|
});
|
||
|
}
|
||
|
|
||
|
function displayMenu(pools){
|
||
|
$('#poolList').after(Object.keys(pools).map(function(poolName){
|
||
|
return '<li class="poolMenuItem"><a href="#">' + poolName + '</a></li>';
|
||
|
}).join(''));
|
||
|
}
|
||
|
|
||
|
function apiRequest(func, data, callback){
|
||
|
var httpRequest = new XMLHttpRequest();
|
||
|
httpRequest.onreadystatechange = function(){
|
||
|
if (httpRequest.readyState === 4 && httpRequest.responseText){
|
||
|
if (httpRequest.status === 401){
|
||
|
docCookies.removeItem('password');
|
||
|
$('#password').val('');
|
||
|
showLogin();
|
||
|
alert('Incorrect Password');
|
||
|
}
|
||
|
else{
|
||
|
var response = JSON.parse(httpRequest.responseText);
|
||
|
callback(response);
|
||
|
}
|
||
|
}
|
||
|
};
|
||
|
httpRequest.open('POST', '/api/admin/' + func);
|
||
|
data.password = password;
|
||
|
httpRequest.setRequestHeader('Content-Type', 'application/json');
|
||
|
httpRequest.send(JSON.stringify(data));
|
||
|
}
|
||
|
|
||
|
if (password){
|
||
|
tryLogin();
|
||
|
}
|
||
|
else{
|
||
|
showLogin();
|
||
|
}
|
||
|
|
||
|
$('#passwordForm').submit(function(event){
|
||
|
event.preventDefault();
|
||
|
password = $('#password').val();
|
||
|
if (password){
|
||
|
if ($('#remember').is(':checked'))
|
||
|
docCookies.setItem('password', password, Infinity);
|
||
|
else
|
||
|
docCookies.setItem('password', password);
|
||
|
tryLogin();
|
||
|
}
|
||
|
return false;
|
||
|
});
|