1 /* Sone JavaScript functions. */
4 return $("#sone").hasClass("online");
7 function registerInputTextareaSwap(inputSelector, defaultText, inputFieldName, optional) {
8 $(inputSelector).each(function() {
9 textarea = $("<textarea name=\"" + inputFieldName + "\"></textarea>").blur(function() {
10 if ($(this).val() == "") {
12 inputField = $(this).data("inputField");
13 inputField.show().removeAttr("disabled").addClass("default");
14 (function(inputField) {
15 getTranslation(defaultText, function(translation) {
16 inputField.val(translation);
20 }).hide().data("inputField", $(this)).val($(this).val());
21 $(this).after(textarea);
22 (function(inputField, textarea) {
23 inputField.focus(function() {
24 $(this).hide().attr("disabled", "disabled");
25 textarea.show().focus();
27 if (inputField.val() == "") {
28 inputField.addClass("default");
29 (function(inputField) {
30 getTranslation(defaultText, function(translation) {
31 inputField.val(translation);
35 inputField.hide().attr("disabled", "disabled");
38 $(inputField.get(0).form).submit(function() {
39 if (!optional && (textarea.val() == "")) {
43 })($(this), textarea);
47 /* hide all the “create reply” forms until a link is clicked. */
48 function addCommentLinks() {
52 $("#sone .post").each(function() {
53 postId = $(this).attr("id");
54 commentElement = (function(postId) {
55 var commentElement = $("<div><span>Comment</span></div>").addClass("show-reply-form").click(function() {
56 replyElement = $("#sone .post#" + postId + " .create-reply");
57 replyElement.removeClass("hidden");
58 replyElement.removeClass("light");
59 (function(replyElement) {
60 replyElement.find("input.reply-input").blur(function() {
61 if ($(this).hasClass("default")) {
62 replyElement.addClass("light");
65 replyElement.removeClass("light");
68 replyElement.find("input.reply-input").focus();
70 return commentElement;
72 $(this).find(".create-reply").addClass("hidden");
73 $(this).find(".status-line .time").each(function() {
74 $(this).after(commentElement.clone(true));
80 * Retrieves the translation for the given key and calls the callback function.
81 * The callback function takes a single parameter, the translated string.
84 * The key of the translation string
86 * The callback function
88 function getTranslation(key, callback) {
89 $.getJSON("ajax/getTranslation.ajax", {"key": key}, function(data, textStatus) {
95 * Fires off an AJAX request to retrieve the current status of a Sone.
100 function getSoneStatus(soneId) {
101 $.getJSON("ajax/getSoneStatus.ajax", {"sone": soneId}, function(data, textStatus) {
102 updateSoneStatus(soneId, data.status, data.modified, data.lastUpdated);
105 if (data.modified || (data.status == "downloading") || (data.status == "inserting")) {
108 setTimeout(function() {
109 getSoneStatus(soneId);
110 }, updateInterval * 1000);
115 * Updates the status of the given Sone.
118 * The ID of the Sone to update
120 * The status of the Sone (“idle”, “unknown”, “inserting”,
123 * Whether the Sone is modified
125 * The date and time of the last update (formatted for display)
127 function updateSoneStatus(soneId, status, modified, lastUpdated) {
128 $("#sone .sone." + soneId).
129 toggleClass("unknown", status == "unknown").
130 toggleClass("idle", status == "idle").
131 toggleClass("inserting", status == "inserting").
132 toggleClass("downloading", status == "downloading").
133 toggleClass("modified", modified);
134 $("#sone .sone." + soneId + " .last-update span.time").text(lastUpdated);
137 var watchedSones = {};
140 * Watches this Sone for updates to its status.
143 * The ID of the Sone to watch
145 function watchSone(soneId) {
146 if (watchedSones[soneId]) {
149 watchedSones[soneId] = true;
151 setTimeout(function() {
152 getSoneStatus(soneId);
158 * Enhances a “delete” button so that the confirmation is done on the same page.
161 * The selector of the button
162 * @param translationKey
163 * The translation key of the text to show on the button
164 * @param deleteCallback
165 * The callback that actually deletes something
167 function enhanceDeleteButton(buttonId, translationKey, deleteCallback) {
168 button = $(buttonId);
170 getTranslation(translationKey, function(translation) {
171 newButton = $("<button></button>").addClass("confirm").hide().text(translation).click(function() {
172 $(this).fadeOut("slow");
175 }).insertAfter(button);
176 (function(button, newButton) {
177 button.click(function() {
178 button.fadeOut("slow", function() {
179 newButton.fadeIn("slow");
180 $(document).one("click", function() {
181 if (this != newButton.get(0)) {
182 newButton.fadeOut(function() {
190 })(button, newButton);
196 * Enhances a post’s “delete” button.
199 * The selector of the button
201 * The ID of the post to delete
203 function enhanceDeletePostButton(buttonId, postId) {
204 enhanceDeleteButton(buttonId, "WebInterface.Confirmation.DeletePostButton", function() {
205 $.getJSON("ajax/deletePost.ajax", { "post": postId, "formPassword": $("#sone #formPassword").text() }, function(data, textStatus) {
207 $("#sone .post#" + postId).slideUp();
208 } else if (data.error == "invalid-post-id") {
209 alert("Invalid post ID given!");
210 } else if (data.error == "auth-required") {
211 alert("You need to be logged in.");
212 } else if (data.error == "not-authorized") {
213 alert("You are not allowed to delete this post.");
220 * Enhances a reply’s “delete” button.
223 * The selector of the button
225 * The ID of the reply to delete
227 function enhanceDeleteReplyButton(buttonId, replyId) {
228 enhanceDeleteButton(buttonId, "WebInterface.Confirmation.DeleteReplyButton", function() {
229 $.getJSON("ajax/deleteReply.ajax", { "reply": replyId, "formPassword": $("#sone #formPassword").text() }, function(data, textStatus) {
231 $("#sone .reply#" + replyId).slideUp();
232 } else if (data.error == "invalid-reply-id") {
233 alert("Invalid reply ID given!");
234 } else if (data.error == "auth-required") {
235 alert("You need to be logged in.");
236 } else if (data.error == "not-authorized") {
237 alert("You are not allowed to delete this reply.");
243 function getFormPassword() {
244 return $("#sone #formPassword").text();
247 function getSoneElement(element) {
248 return $(element).parents(".sone");
252 * Returns the ID of the Sone that this element belongs to.
255 * The element to locate the matching Sone ID for
256 * @returns The ID of the Sone, or undefined
258 function getSoneId(element) {
259 return getSoneElement(element).find(".id").text();
262 function getPostElement(element) {
263 return $(element).parents(".post");
266 function getPostId(element) {
267 return getPostElement(element).attr("id");
270 function getReplyElement(element) {
271 return $(element).parents(".reply");
274 function getReplyId(element) {
275 return getReplyElement(element).attr("id");
278 function likePost(postId) {
279 $.getJSON("ajax/like.ajax", { "type": "post", "post" : postId, "formPassword": getFormPassword() }, function() {
280 $("#sone .post#" + postId + " > .status-line .like").addClass("hidden");
281 $("#sone .post#" + postId + " > .status-line .unlike").removeClass("hidden");
282 updatePostLikes(postId);
286 function unlikePost(postId) {
287 $.getJSON("ajax/unlike.ajax", { "type": "post", "post" : postId, "formPassword": getFormPassword() }, function() {
288 $("#sone .post#" + postId + " > .status-line .unlike").addClass("hidden");
289 $("#sone .post#" + postId + " > .status-line .like").removeClass("hidden");
290 updatePostLikes(postId);
294 function updatePostLikes(postId) {
295 $.getJSON("ajax/getLikes.ajax", { "type": "post", "post": postId }, function(data, textStatus) {
297 $("#sone .post#" + postId + " > .status-line .likes").toggleClass("hidden", data.likes == 0)
298 $("#sone .post#" + postId + " > .status-line .likes span.like-count").text(data.likes);
303 function likeReply(replyId) {
304 $.getJSON("ajax/like.ajax", { "type": "reply", "reply" : replyId, "formPassword": getFormPassword() }, function() {
305 $("#sone .reply#" + replyId + " .status-line .like").addClass("hidden");
306 $("#sone .reply#" + replyId + " .status-line .unlike").removeClass("hidden");
307 updateReplyLikes(replyId);
311 function unlikeReply(replyId) {
312 $.getJSON("ajax/unlike.ajax", { "type": "reply", "reply" : replyId, "formPassword": getFormPassword() }, function() {
313 $("#sone .reply#" + replyId + " .status-line .unlike").addClass("hidden");
314 $("#sone .reply#" + replyId + " .status-line .like").removeClass("hidden");
315 updateReplyLikes(replyId);
319 function updateReplyLikes(replyId) {
320 $.getJSON("ajax/getLikes.ajax", { "type": "reply", "reply": replyId }, function(data, textStatus) {
322 $("#sone .reply#" + replyId + " .status-line .likes").toggleClass("hidden", data.likes == 0)
323 $("#sone .reply#" + replyId + " .status-line .likes span.like-count").text(data.likes);