Webserver: Central function for server communication

This commit is contained in:
lukeIam committed 2013-12-13 17:43:23 +01:00
1 parent 2142807ffc
commit 7463da19f4
1 file changed
+95 -151
+95 -151
View File
@@ -21,14 +21,14 @@ function replaceTransitionHandler() {
var promise = $to.data('promise'); var promise = $to.data('promise');
if (promise) { if (promise) {
$to.removeData('promise'); $to.removeData('promise');
$('div[data-role="content"]').wrap('<div class="overlay" />'); /*$('div[data-role="content"]').wrap('<div class="overlay" />');
$.mobile.loading('show', { $.mobile.loading('show', {
text: 'Loading data...', text: 'Loading data...',
textVisible: true textVisible: true
}); });*/
return promise.then(function () { return promise.then(function () {
$.mobile.loading('hide'); /*$.mobile.loading('hide');
$('div[data-role="content"]').unwrap(); $('div[data-role="content"]').unwrap();*/
return oldDefaultTransitionHandler(name, reverse, $to, $from); return oldDefaultTransitionHandler(name, reverse, $to, $from);
}); });
} }
@@ -40,9 +40,8 @@ function initPageLoadHandler() {
//pageLoadHandler for displayProfile //pageLoadHandler for displayProfile
$(document).on('pagebeforeshow', '#displayProfile', function () { $(document).on('pagebeforeshow', '#displayProfile', function () {
if (profileIdRequest >= 0) { if (profileIdRequest >= 0) {
var promise = $.ajax({ var promise = request({
url: "getProfile?profileId=" + profileIdRequest, url: "getProfile?profileId=" + profileIdRequest
headers: { "session": sessionId }
}).done(function (result) { }).done(function (result) {
handleDisplayProfileData(result); handleDisplayProfileData(result);
@@ -56,25 +55,15 @@ function initPageLoadHandler() {
dataToUpload["Avatar"] = $('#playerAvatar').data("changed") ? { "base64Data": $('#playerAvatar').prop("src") } : null; dataToUpload["Avatar"] = $('#playerAvatar').data("changed") ? { "base64Data": $('#playerAvatar').prop("src") } : null;
dataToUpload["Password"] = $('#playerPassword').prop("value") != "**oldPassword**" ? $('#playerPassword').prop("value") : null; dataToUpload["Password"] = $('#playerPassword').prop("value") != "**oldPassword**" ? $('#playerPassword').prop("value") : null;
$('div[data-role="content"]').wrap('<div class="overlay" />'); request({
$.mobile.loading('show', {
text: 'Uploading profile...',
textVisible: true
});
$.ajax({
url: "sendProfile", url: "sendProfile",
dataType: "json", dataType: "json",
contentType: "application/json;charset=utf-8", contentType: "application/json;charset=utf-8",
type: "POST", type: "POST",
headers: { "session": sessionId }, headers: { "session": sessionId },
data: JSON.stringify(dataToUpload), data: JSON.stringify(dataToUpload),
success: function (msg) { }, "Uploading profile...").done(function () {
history.back(); history.back();
}
}).always(function () {
$.mobile.loading('hide');
$('div[data-role="content"]').unwrap();
}); });
}); });
@@ -106,25 +95,15 @@ function initPageLoadHandler() {
dataToUpload["Avatar"] = $('#playerAvatar').data("changed") ? { "base64Data": $('#playerAvatar').prop("src") } : null; dataToUpload["Avatar"] = $('#playerAvatar').data("changed") ? { "base64Data": $('#playerAvatar').prop("src") } : null;
dataToUpload["Password"] = $('#playerPassword').prop("value") != "**oldPassword**" ? $('#playerPassword').prop("value") : null; dataToUpload["Password"] = $('#playerPassword').prop("value") != "**oldPassword**" ? $('#playerPassword').prop("value") : null;
$('div[data-role="content"]').wrap('<div class="overlay" />'); request({
$.mobile.loading('show', {
text: 'Creating profile...',
textVisible: true
});
$.ajax({
url: "sendProfile", url: "sendProfile",
dataType: "json", dataType: "json",
contentType: "application/json;charset=utf-8", contentType: "application/json;charset=utf-8",
type: "POST", type: "POST",
headers: { "session": sessionId }, headers: { "session": sessionId },
data: JSON.stringify(dataToUpload), data: JSON.stringify(dataToUpload)
success: function (msg) { }, 'Creating profile...').done(function () {
$.mobile.changePage("#login", { transition: "slidefade" }); $.mobile.changePage("#login", { transition: "slidefade" });
}
}).always(function () {
$.mobile.loading('hide');
$('div[data-role="content"]').unwrap();
}); });
}); });
@@ -184,7 +163,7 @@ function initPageLoadHandler() {
//pageLoadHandler for selectProfile //pageLoadHandler for selectProfile
$(document).on('pagebeforeshow', '#selectProfile', function () { $(document).on('pagebeforeshow', '#selectProfile', function () {
var promise = $.ajax({ var promise = request({
url: "getProfileList", url: "getProfileList",
headers: { "session": sessionId } headers: { "session": sessionId }
}).done(function (data) { }).done(function (data) {
@@ -213,7 +192,7 @@ function initPageLoadHandler() {
//pageLoadHandler for displaySong //pageLoadHandler for displaySong
$(document).on('pagebeforeshow', '#displaySong', function () { $(document).on('pagebeforeshow', '#displaySong', function () {
var promise = $.ajax({ var promise = request({
url: "getSong?songId=" + songIdRequest, url: "getSong?songId=" + songIdRequest,
headers: { "session": sessionId } headers: { "session": sessionId }
}).done(function (result) { }).done(function (result) {
@@ -281,7 +260,7 @@ function initPageLoadHandler() {
} }
if (allSongsCache == null) { if (allSongsCache == null) {
var promise = $.ajax({ var promise = request({
url: "getAllSongs", url: "getAllSongs",
headers: { "session": sessionId } headers: { "session": sessionId }
}).done(function (data) { }).done(function (data) {
@@ -299,7 +278,7 @@ function initPageLoadHandler() {
//pageLoadHandler for selectUserAdmin //pageLoadHandler for selectUserAdmin
$(document).on('pagebeforeshow', '#selectUserAdmin', function () { $(document).on('pagebeforeshow', '#selectUserAdmin', function () {
var promise = $.ajax({ var promise = request({
url: "getProfileList", url: "getProfileList",
headers: { "session": sessionId } headers: { "session": sessionId }
}).done(function (data) { }).done(function (data) {
@@ -325,29 +304,21 @@ function initPageLoadHandler() {
//pageLoadHandler for displayUserAdmin //pageLoadHandler for displayUserAdmin
$(document).on('pagebeforeshow', '#displayUserAdmin', function () { $(document).on('pagebeforeshow', '#displayUserAdmin', function () {
var promise = $.ajax({ var promise = request({
url: "getUserRole?profileId=" + profileIdRequest, url: "getUserRole?profileId=" + profileIdRequest,
headers: { "session": sessionId } headers: { "session": sessionId }
}).done(function (result) { }).done(function (result) {
$('#roleAdministrator').prop("checked", ((result & 0x01) != 0)).checkboxradio("refresh"); $('#roleAdministrator').prop("checked", ((result & 0x01) != 0)).checkboxradio("refresh");
$('#btnRoleSave').unbind('click').click(function () { $('#btnRoleSave').unbind('click').click(function () {
$('div[data-role="content"]').wrap('<div class="overlay" />');
$.mobile.loading('show', {
text: 'Save...',
textVisible: true
});
var role = 0; var role = 0;
if ($('#roleAdministrator').prop("checked")) { if ($('#roleAdministrator').prop("checked")) {
role = (role | 0x01); role = (role | 0x01);
} }
$.ajax({ request({
url: "setUserRole?profileId=" + profileIdRequest + "&userRole=" + role, url: "setUserRole?profileId=" + profileIdRequest + "&userRole=" + role,
headers: { "session": sessionId } headers: { "session": sessionId }
}).done(function (result) { }, "Saving...").done(function (result) {
$.mobile.loading('hide');
$('div[data-role="content"]').unwrap();
history.back(); history.back();
}); });
}); });
@@ -375,24 +346,12 @@ function pagebeforeshowLogin() {
$.mobile.changePage("#main", { transition: "slidefade" }); $.mobile.changePage("#main", { transition: "slidefade" });
} }
else { else {
$('div[data-role="content"]').wrap('<div class="overlay" />'); request({
$.mobile.loading('show', {
text: 'Login...',
textVisible: true
});
$.ajax({
url: "getOwnProfileId", url: "getOwnProfileId",
headers: { "session": sessionId } headers: { "session": sessionId }
}).done(function (result) { }, 'Login...').done(function (result) {
ownProfileId = result; ownProfileId = result;
$.mobile.loading('hide');
$('div[data-role="content"]').unwrap();
$.mobile.changePage("#main", { transition: "slidefade" }); $.mobile.changePage("#main", { transition: "slidefade" });
}).fail(function (result) {
$.mobile.loading('hide');
$('div[data-role="content"]').unwrap();
}); });
} }
} }
@@ -409,39 +368,23 @@ function initLoginPageHandler() {
$('#loginPassword').keypress(keyPressed); $('#loginPassword').keypress(keyPressed);
$('#loginButton').click(function () { $('#loginButton').click(function () {
$('div[data-role="content"]').wrap('<div class="overlay" />'); var username = $('#loginName').prop("value");
$.mobile.loading('show', { var password = $('#loginPassword').prop("value");
text: 'Login...',
textVisible: true
});
username = $('#loginName').prop("value"); request({
password = $('#loginPassword').prop("value");
$.ajax({
url: "login?username=" + username + "&password=" + password url: "login?username=" + username + "&password=" + password
}).done(function (result) { }).done(function (result) {
sessionId = result; sessionId = result;
if (window.localStorage) { if (window.localStorage) {
window.localStorage.setItem("VocaluxeSessionKey", sessionId); window.localStorage.setItem("VocaluxeSessionKey", sessionId);
} }
$.ajax({ request({
url: "getOwnProfileId", url: "getOwnProfileId",
headers: { "session": sessionId } headers: { "session": sessionId }
}).done(function (result) { }, 'Login...').done(function (result) {
ownProfileId = result; ownProfileId = result;
$.mobile.loading('hide');
$('div[data-role="content"]').unwrap();
$.mobile.changePage("#main", { transition: "slidefade" }); $.mobile.changePage("#main", { transition: "slidefade" });
}).fail(function (result) {
$.mobile.loading('hide');
$('div[data-role="content"]').unwrap();
}); });
}).fail(function (result) {
$.mobile.loading('hide');
$('div[data-role="content"]').unwrap();
}); });
}); });
@@ -462,23 +405,12 @@ function initMainPageHandler() {
}); });
$('#currentSongLink').click(function () { $('#currentSongLink').click(function () {
$('div[data-role="content"]').wrap('<div class="overlay" />'); request({
$.mobile.loading('show', {
text: 'Getting current song...',
textVisible: true
});
$.ajax({
url: "getCurrentSongId", url: "getCurrentSongId",
headers: { "session": sessionId } headers: { "session": sessionId }
}).done(function (result) { }, 'Getting current song...').done(function (result) {
songIdRequest = parseInt(result); songIdRequest = parseInt(result);
$.mobile.loading('hide');
$('div[data-role="content"]').unwrap();
$.mobile.changePage("#displaySong", { transition: "slidefade" }); $.mobile.changePage("#displaySong", { transition: "slidefade" });
}).fail(function (result) {
$.mobile.loading('hide');
$('div[data-role="content"]').unwrap();
}); });
}); });
@@ -491,30 +423,18 @@ function initMainPageHandler() {
$('#capture').change(function (eventData) { $('#capture').change(function (eventData) {
if (eventData && eventData.target && eventData.target.files && eventData.target.files.length == 1) { if (eventData && eventData.target && eventData.target.files && eventData.target.files.length == 1) {
$('div[data-role="content"]').wrap('<div class="overlay" />');
$.mobile.loading('show', {
text: 'Uploading photo...',
textVisible: true
});
var file = eventData.target.files[0]; var file = eventData.target.files[0];
var reader = new FileReader(); var reader = new FileReader();
reader.onloadend = function (e) { reader.onloadend = function (e) {
$.ajax({ request({
url: "sendPhoto", url: "sendPhoto",
dataType: "json", dataType: "json",
contentType: "application/json;charset=utf-8", contentType: "application/json;charset=utf-8",
type: "POST", type: "POST",
headers: { "session": sessionId }, headers: { "session": sessionId },
data: JSON.stringify({ Photo: { base64Data: e.target.result } }), data: JSON.stringify({ Photo: { base64Data: e.target.result } })
success: function (msg) { }, 'Uploading photo...');
}
}).always(function () {
$.mobile.loading('hide');
$('div[data-role="content"]').unwrap();
});
}; };
reader.readAsDataURL(file); reader.readAsDataURL(file);
@@ -531,62 +451,54 @@ function initMainPageHandler() {
function initKeyboardPageHandler() { function initKeyboardPageHandler() {
$('#keyboardButtonUp').click(function () { $('#keyboardButtonUp').click(function () {
$.ajax({ request({
url: "sendKeyEvent?key=up", url: "sendKeyEvent?key=up"
headers: { "session": sessionId } });
})
}); });
$('#keyboardButtonDown').click(function () { $('#keyboardButtonDown').click(function () {
$.ajax({ request({
url: "sendKeyEvent?key=down", url: "sendKeyEvent?key=down"
headers: { "session": sessionId } });
})
}); });
$('#keyboardButtonLeft').click(function () { $('#keyboardButtonLeft').click(function () {
$.ajax({ request({
url: "sendKeyEvent?key=left", url: "sendKeyEvent?key=left"
headers: { "session": sessionId } });
})
}); });
$('#keyboardButtonRight').click(function () { $('#keyboardButtonRight').click(function () {
$.ajax({ request({
url: "sendKeyEvent?key=right", url: "sendKeyEvent?key=right"
headers: { "session": sessionId } });
})
}); });
$('#keyboardButtonEscape').click(function () { $('#keyboardButtonEscape').click(function () {
$.ajax({ request({
url: "sendKeyEvent?key=escape", url: "sendKeyEvent?key=escape"
headers: { "session": sessionId } });
})
}); });
$('#keyboardButtonkeyboardButtonTab').click(function () { $('#keyboardButtonkeyboardButtonTab').click(function () {
$.ajax({ request({
url: "sendKeyEvent?key=tab", url: "sendKeyEvent?key=tab"
headers: { "session": sessionId } });
})
}); });
$('#keyboardButtonReturn').click(function () { $('#keyboardButtonReturn').click(function () {
$.ajax({ request({
url: "sendKeyEvent?key=return", url: "sendKeyEvent?key=return"
headers: { "session": sessionId } });
})
}); });
$('#keyboardButtonKeys').keyup(function (e) { $('#keyboardButtonKeys').keyup(function (e) {
var c = String.fromCharCode(e.keyCode); var c = String.fromCharCode(e.keyCode);
if (c.match(/\w/)) { if (c.match(/\w/)) {
c = e.keyCode >= 65 ? c.toLowerCase() : c; c = e.keyCode >= 65 ? c.toLowerCase() : c;
$.ajax({ request({
url: "sendKeyEvent?key=" + c, url: "sendKeyEvent?key=" + c
headers: { "session": sessionId } });
})
} }
var oldText = $('#keyboardButtonKeys')[0].value; var oldText = $('#keyboardButtonKeys')[0].value;
if (oldText.length > 0) { if (oldText.length > 0) {
@@ -606,9 +518,8 @@ function delayedImageLoad(elem, id, fail) {
elem.src = ""; elem.src = "";
$(elem).addClass("imageLoaderImg"); $(elem).addClass("imageLoaderImg");
$.ajax({ request({
url: "delayedImage?id=" + id, url: "delayedImage?id=" + id
headers: { "session": sessionId }
}).done(function (result) { }).done(function (result) {
$(elem).removeClass("imageLoaderImg"); $(elem).removeClass("imageLoaderImg");
elem.src = result.base64Data; elem.src = result.base64Data;
@@ -648,14 +559,13 @@ function logout() {
} }
function checkSession() { function checkSession() {
$.ajax({ request({
url: "getOwnProfileId", url: "getOwnProfileId"
headers: { "session": sessionId }
}).done(function (result) { }).done(function (result) {
if (result == -1) { if (result == -1) {
logout(); logout();
} }
}).fail(function (result) { }, "noOverlay").fail(function (result) {
logout(); logout();
}); });
} }
@@ -663,3 +573,37 @@ function checkSession() {
function initHeartbeat() { function initHeartbeat() {
setInterval(checkSession, 20000); setInterval(checkSession, 20000);
} }
function request(data, message) {
if (!message) {
message = "Loading...";
}
if (message != "noOverlay") {
$('div[data-role="content"]').wrap('<div class="overlay" />');
$.mobile.loading('show', {
text: message,
textVisible: true
});
}
if (!data["headers"]) {
data["headers"] = {};
}
data["headers"]["session"] = sessionId;
return $.ajax(data).always(function (result) {
if (message != "noOverlay") {
$.mobile.loading('hide');
$('div[data-role="content"]').unwrap();
}
}).fail(function (result) {
//TODO:Fail Handling
});
}
function showError(message) {
if (!message) {
message = "Error...";
}
}