X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;ds=sidebyside;f=src%2Fmain%2Fresources%2Fstatic%2Fjavascript%2Fsone.js;h=149520f3650ee0e46e379d3d820400b08831cce6;hb=51cdfee0b350955d9f858adb892f18b2a4fa8ab4;hp=01c04a1cbb60d82c7cf171a8114db44129bb12be;hpb=fd5860dbb46063f229492354af88f1ad3701772c;p=Sone.git
diff --git a/src/main/resources/static/javascript/sone.js b/src/main/resources/static/javascript/sone.js
index 01c04a1..149520f 100644
--- a/src/main/resources/static/javascript/sone.js
+++ b/src/main/resources/static/javascript/sone.js
@@ -1,5 +1,24 @@
/* Sone JavaScript functions. */
+/* jQuery overrides. */
+oldGetJson = jQuery.prototype.getJSON;
+jQuery.prototype.getJSON = function(url, data, successCallback, errorCallback) {
+ if (typeof errorCallback == "undefined") {
+ return oldGetJson(url, data, successCallback);
+ }
+ if (jQuery.isFunction(data)) {
+ errorCallback = successCallback;
+ successCallback = data;
+ data = null;
+ }
+ return jQuery.ajax({
+ data: data,
+ error: errorCallback,
+ success: successCallback,
+ url: url
+ });
+}
+
function isOnline() {
return $("#sone").hasClass("online");
}
@@ -80,6 +99,8 @@ function addCommentLink(postId, element) {
});
}
+var translations = {};
+
/**
* Retrieves the translation for the given key and calls the callback function.
* The callback function takes a single parameter, the translated string.
@@ -90,31 +111,17 @@ function addCommentLink(postId, element) {
* The callback function
*/
function getTranslation(key, callback) {
+ if (key in translations) {
+ callback(translations[key]);
+ return;
+ }
$.getJSON("ajax/getTranslation.ajax", {"key": key}, function(data, textStatus) {
- callback(data.value);
- });
-}
-
-/**
- * Fires off an AJAX request to retrieve the current status of a Sone.
- *
- * @param soneId
- * The ID of the Sone
- * @param local
- * true
if the Sone is local, false
- * otherwise
- */
-function getSoneStatus(soneId, local) {
- $.getJSON("ajax/getSoneStatus.ajax", {"sone": soneId}, function(data, textStatus) {
- updateSoneStatus(soneId, data.name, data.status, data.modified, data.lastUpdated);
- /* seconds! */
- updateInterval = 60;
- if (local || data.modified || (data.status == "downloading") || (data.status == "inserting")) {
- updateInterval = 5;
+ if ((data != null) && data.success) {
+ translations[key] = data.value;
+ callback(data.value);
}
- setTimeout(function() {
- getSoneStatus(soneId, local);
- }, updateInterval * 1000);
+ }, function(xmlHttpRequest, textStatus, error) {
+ /* ignore error. */
});
}
@@ -139,43 +146,24 @@ function filterSoneId(soneId) {
* âdownloadingâ)
* @param modified
* Whether the Sone is modified
+ * @param locked
+ * Whether the Sone is locked
* @param lastUpdated
* The date and time of the last update (formatted for display)
*/
-function updateSoneStatus(soneId, name, status, modified, lastUpdated) {
+function updateSoneStatus(soneId, name, status, modified, locked, lastUpdated) {
$("#sone .sone." + filterSoneId(soneId)).
toggleClass("unknown", status == "unknown").
toggleClass("idle", status == "idle").
toggleClass("inserting", status == "inserting").
toggleClass("downloading", status == "downloading").
toggleClass("modified", modified);
+ $("#sone .sone." + filterSoneId(soneId) + " .lock").toggleClass("hidden", locked);
+ $("#sone .sone." + filterSoneId(soneId) + " .unlock").toggleClass("hidden", !locked);
$("#sone .sone." + filterSoneId(soneId) + " .last-update span.time").text(lastUpdated);
$("#sone .sone." + filterSoneId(soneId) + " .profile-link a").text(name);
}
-var watchedSones = {};
-
-/**
- * Watches this Sone for updates to its status.
- *
- * @param soneId
- * The ID of the Sone to watch
- * @param local
- * true
if the Sone is local, false
- * otherwise
- */
-function watchSone(soneId, local) {
- if (watchedSones[soneId]) {
- return;
- }
- watchedSones[soneId] = true;
- (function(soneId) {
- setTimeout(function() {
- getSoneStatus(soneId, local);
- }, 5000);
- })(soneId);
-}
-
/**
* Enhances a âdeleteâ button so that the confirmation is done on the same page.
*
@@ -225,6 +213,9 @@ function enhanceDeleteButton(buttonId, text, deleteCallback) {
function enhanceDeletePostButton(buttonId, postId, text) {
enhanceDeleteButton(buttonId, text, function() {
$.getJSON("ajax/deletePost.ajax", { "post": postId, "formPassword": $("#sone #formPassword").text() }, function(data, textStatus) {
+ if (data == null) {
+ return;
+ }
if (data.success) {
$("#sone .post#" + postId).slideUp();
} else if (data.error == "invalid-post-id") {
@@ -234,6 +225,8 @@ function enhanceDeletePostButton(buttonId, postId, text) {
} else if (data.error == "not-authorized") {
alert("You are not allowed to delete this post.");
}
+ }, function(xmlHttpRequest, textStatus, error) {
+ /* ignore error. */
});
});
}
@@ -251,6 +244,9 @@ function enhanceDeletePostButton(buttonId, postId, text) {
function enhanceDeleteReplyButton(buttonId, replyId, text) {
enhanceDeleteButton(buttonId, text, function() {
$.getJSON("ajax/deleteReply.ajax", { "reply": replyId, "formPassword": $("#sone #formPassword").text() }, function(data, textStatus) {
+ if (data == null) {
+ return;
+ }
if (data.success) {
$("#sone .reply#" + replyId).slideUp();
} else if (data.error == "invalid-reply-id") {
@@ -260,6 +256,8 @@ function enhanceDeleteReplyButton(buttonId, replyId, text) {
} else if (data.error == "not-authorized") {
alert("You are not allowed to delete this reply.");
}
+ }, function(xmlHttpRequest, textStatus, error) {
+ /* ignore error. */
});
});
}
@@ -284,7 +282,7 @@ function generateSoneList(sones) {
var soneList = "";
$.each(sones, function() {
if (soneList != "") {
- soneList += "\n";
+ soneList += ", ";
}
soneList += this.name;
});
@@ -319,54 +317,78 @@ function getReplyId(element) {
}
function likePost(postId) {
- $.getJSON("ajax/like.ajax", { "type": "post", "post" : postId, "formPassword": getFormPassword() }, function() {
+ $.getJSON("ajax/like.ajax", { "type": "post", "post" : postId, "formPassword": getFormPassword() }, function(data, textStatus) {
+ if ((data == null) || !data.success) {
+ return;
+ }
$("#sone .post#" + postId + " > .inner-part > .status-line .like").addClass("hidden");
$("#sone .post#" + postId + " > .inner-part > .status-line .unlike").removeClass("hidden");
updatePostLikes(postId);
+ }, function(xmlHttpRequest, textStatus, error) {
+ /* ignore error. */
});
}
function unlikePost(postId) {
- $.getJSON("ajax/unlike.ajax", { "type": "post", "post" : postId, "formPassword": getFormPassword() }, function() {
+ $.getJSON("ajax/unlike.ajax", { "type": "post", "post" : postId, "formPassword": getFormPassword() }, function(data, textStatus) {
+ if ((data == null) || !data.success) {
+ return;
+ }
$("#sone .post#" + postId + " > .inner-part > .status-line .unlike").addClass("hidden");
$("#sone .post#" + postId + " > .inner-part > .status-line .like").removeClass("hidden");
updatePostLikes(postId);
+ }, function(xmlHttpRequest, textStatus, error) {
+ /* ignore error. */
});
}
function updatePostLikes(postId) {
$.getJSON("ajax/getLikes.ajax", { "type": "post", "post": postId }, function(data, textStatus) {
- if (data.success) {
+ if ((data != null) && data.success) {
$("#sone .post#" + postId + " > .inner-part > .status-line .likes").toggleClass("hidden", data.likes == 0)
$("#sone .post#" + postId + " > .inner-part > .status-line .likes span.like-count").text(data.likes);
$("#sone .post#" + postId + " > .inner-part > .status-line .likes > span").attr("title", generateSoneList(data.sones));
}
+ }, function(xmlHttpRequest, textStatus, error) {
+ /* ignore error. */
});
}
function likeReply(replyId) {
- $.getJSON("ajax/like.ajax", { "type": "reply", "reply" : replyId, "formPassword": getFormPassword() }, function() {
+ $.getJSON("ajax/like.ajax", { "type": "reply", "reply" : replyId, "formPassword": getFormPassword() }, function(data, textStatus) {
+ if ((data == null) || !data.success) {
+ return;
+ }
$("#sone .reply#" + replyId + " .status-line .like").addClass("hidden");
$("#sone .reply#" + replyId + " .status-line .unlike").removeClass("hidden");
updateReplyLikes(replyId);
+ }, function(xmlHttpRequest, textStatus, error) {
+ /* ignore error. */
});
}
function unlikeReply(replyId) {
- $.getJSON("ajax/unlike.ajax", { "type": "reply", "reply" : replyId, "formPassword": getFormPassword() }, function() {
+ $.getJSON("ajax/unlike.ajax", { "type": "reply", "reply" : replyId, "formPassword": getFormPassword() }, function(data, textStatus) {
+ if ((data == null) || !data.success) {
+ return;
+ }
$("#sone .reply#" + replyId + " .status-line .unlike").addClass("hidden");
$("#sone .reply#" + replyId + " .status-line .like").removeClass("hidden");
updateReplyLikes(replyId);
+ }, function(xmlHttpRequest, textStatus, error) {
+ /* ignore error. */
});
}
function updateReplyLikes(replyId) {
$.getJSON("ajax/getLikes.ajax", { "type": "reply", "reply": replyId }, function(data, textStatus) {
- if (data.success) {
+ if ((data != null) && data.success) {
$("#sone .reply#" + replyId + " .status-line .likes").toggleClass("hidden", data.likes == 0)
$("#sone .reply#" + replyId + " .status-line .likes span.like-count").text(data.likes);
$("#sone .reply#" + replyId + " .status-line .likes > span").attr("title", generateSoneList(data.sones));
}
+ }, function(xmlHttpRequest, textStatus, error) {
+ /* ignore error. */
});
}
@@ -383,11 +405,17 @@ function updateReplyLikes(replyId) {
*/
function postReply(postId, text, callbackFunction) {
$.getJSON("ajax/createReply.ajax", { "formPassword" : getFormPassword(), "post" : postId, "text": text }, function(data, textStatus) {
+ if (data == null) {
+ /* TODO - show error */
+ return;
+ }
if (data.success) {
callbackFunction(true, null, data.reply);
} else {
callbackFunction(false, data.error);
}
+ }, function(xmlHttpRequest, textStatus, error) {
+ /* ignore error. */
});
}
@@ -402,8 +430,249 @@ function postReply(postId, text, callbackFunction) {
*/
function getReply(replyId, callbackFunction) {
$.getJSON("ajax/getReply.ajax", { "reply" : replyId }, function(data, textStatus) {
- if (data.success) {
+ if ((data != null) && data.success) {
callbackFunction(data.soneId, data.soneName, data.time, data.displayTime, data.text, data.html);
}
+ }, function(xmlHttpRequest, textStatus, error) {
+ /* ignore error. */
});
}
+
+/**
+ * Ajaxifies the given notification by replacing the form with AJAX.
+ *
+ * @param notification
+ * jQuery object representing the notification.
+ */
+function ajaxifyNotification(notification) {
+ notification.find("form.dismiss").submit(function() {
+ return false;
+ });
+ notification.find("form.dismiss button").click(function() {
+ $.getJSON("ajax/dismissNotification.ajax", { "formPassword" : getFormPassword(), "notification" : notification.attr("id") }, function(data, textStatus) {
+ /* dismiss in case of error, too. */
+ notification.slideUp();
+ }, function(xmlHttpRequest, textStatus, error) {
+ /* ignore error. */
+ });
+ });
+ return notification;
+}
+
+function getStatus() {
+ $.getJSON("ajax/getStatus.ajax", {}, function(data, textStatus) {
+ if ((data != null) && data.success) {
+ /* process Sone information. */
+ $.each(data.sones, function(index, value) {
+ updateSoneStatus(value.id, value.name, value.status, value.modified, value.locked, value.lastUpdated);
+ });
+ /* process notifications. */
+ $.each(data.notifications, function(index, value) {
+ oldNotification = $("#sone #notification-area .notification#" + value.id);
+ notification = ajaxifyNotification(createNotification(value.id, value.text, value.dismissable)).hide();
+ if (oldNotification.length != 0) {
+ oldNotification.replaceWith(notification.show());
+ } else {
+ $("#sone #notification-area").append(notification);
+ notification.slideDown();
+ }
+ });
+ $.each(data.removedNotifications, function(index, value) {
+ $("#sone #notification-area .notification#" + value.id).slideUp();
+ });
+ /* do it again in 5 seconds. */
+ setTimeout(getStatus, 5000);
+ } else {
+ /* data.success was false, wait 30 seconds. */
+ setTimeout(getStatus, 30000);
+ }
+ }, function(xmlHttpRequest, textStatus, error) {
+ /* something really bad happend, wait a minute. */
+ setTimeout(getStatus, 60000);
+ })
+}
+
+/**
+ * Creates a new notification.
+ *
+ * @param id
+ * The ID of the notificaiton
+ * @param text
+ * The text of the notification
+ * @param dismissable
+ * true
if the notification can be dismissed by the
+ * user
+ */
+function createNotification(id, text, dismissable) {
+ notification = $("