1 /* Sone JavaScript functions. */
4 return $("#sone").hasClass("online");
7 function registerInputTextareaSwap(inputSelector, defaultText, inputFieldName, optional, dontUseTextarea) {
8 $(inputSelector).each(function() {
9 textarea = $(dontUseTextarea ? "<input type=\"text\" name=\"" + inputFieldName + "\">" : "<textarea name=\"" + inputFieldName + "\"></textarea>").blur(function() {
10 if ($(this).val() == "") {
12 inputField = $(this).data("inputField");
13 inputField.show().removeAttr("disabled").addClass("default");
14 inputField.val(defaultText);
16 }).hide().data("inputField", $(this)).val($(this).val());
17 $(this).after(textarea);
18 (function(inputField, textarea) {
19 inputField.focus(function() {
20 $(this).hide().attr("disabled", "disabled");
21 textarea.show().focus();
23 if (inputField.val() == "") {
24 inputField.addClass("default");
25 inputField.val(defaultText);
27 inputField.hide().attr("disabled", "disabled");
30 $(inputField.get(0).form).submit(function() {
31 if (!optional && (textarea.val() == "")) {
35 })($(this), textarea);
39 /* hide all the “create reply” forms until a link is clicked. */
40 function addCommentLinks() {
44 $("#sone .post").each(function() {
45 postId = $(this).attr("id");
46 addCommentLink(postId, $(this));
51 * Adds a “comment” link to all status lines contained in the given element.
56 * The element to add a “comment” link to
58 function addCommentLink(postId, element) {
59 commentElement = (function(postId) {
60 var commentElement = $("<div><span>Comment</span></div>").addClass("show-reply-form").click(function() {
61 replyElement = $("#sone .post#" + postId + " .create-reply");
62 replyElement.removeClass("hidden");
63 replyElement.removeClass("light");
64 (function(replyElement) {
65 replyElement.find("input.reply-input").blur(function() {
66 if ($(this).hasClass("default")) {
67 replyElement.addClass("light");
70 replyElement.removeClass("light");
73 replyElement.find("input.reply-input").focus();
75 return commentElement;
77 element.find(".create-reply").addClass("hidden");
78 element.find(".status-line .time").each(function() {
79 $(this).after(commentElement.clone(true));
84 * Retrieves the translation for the given key and calls the callback function.
85 * The callback function takes a single parameter, the translated string.
88 * The key of the translation string
90 * The callback function
92 function getTranslation(key, callback) {
93 $.getJSON("ajax/getTranslation.ajax", {"key": key}, function(data, textStatus) {
99 * Fires off an AJAX request to retrieve the current status of a Sone.
104 function getSoneStatus(soneId) {
105 $.getJSON("ajax/getSoneStatus.ajax", {"sone": soneId}, function(data, textStatus) {
106 updateSoneStatus(soneId, data.name, data.status, data.modified, data.lastUpdated);
109 if (data.modified || (data.status == "downloading") || (data.status == "inserting")) {
112 setTimeout(function() {
113 getSoneStatus(soneId);
114 }, updateInterval * 1000);
119 * Filters the given Sone ID, replacing all “~” characters by an underscore.
122 * The Sone ID to filter
123 * @returns The filtered Sone ID
125 function filterSoneId(soneId) {
126 return soneId.replace(/[^a-zA-Z0-9-]/g, "_");
130 * Updates the status of the given Sone.
133 * The ID of the Sone to update
135 * The status of the Sone (“idle”, “unknown”, “inserting”,
138 * Whether the Sone is modified
140 * The date and time of the last update (formatted for display)
142 function updateSoneStatus(soneId, name, status, modified, lastUpdated) {
143 $("#sone .sone." + filterSoneId(soneId)).
144 toggleClass("unknown", status == "unknown").
145 toggleClass("idle", status == "idle").
146 toggleClass("inserting", status == "inserting").
147 toggleClass("downloading", status == "downloading").
148 toggleClass("modified", modified);
149 $("#sone .sone." + filterSoneId(soneId) + " .last-update span.time").text(lastUpdated);
150 $("#sone .sone." + filterSoneId(soneId) + " .profile-link a").text(name);
153 var watchedSones = {};
156 * Watches this Sone for updates to its status.
159 * The ID of the Sone to watch
161 function watchSone(soneId) {
162 if (watchedSones[soneId]) {
165 watchedSones[soneId] = true;
167 setTimeout(function() {
168 getSoneStatus(soneId);
174 * Enhances a “delete” button so that the confirmation is done on the same page.
177 * The selector of the button
179 * The text to show on the button
180 * @param deleteCallback
181 * The callback that actually deletes something
183 function enhanceDeleteButton(buttonId, text, deleteCallback) {
184 button = $(buttonId);
186 newButton = $("<button></button>").addClass("confirm").hide().text(text).click(function() {
187 $(this).fadeOut("slow");
190 }).insertAfter(button);
191 (function(button, newButton) {
192 button.click(function() {
193 button.fadeOut("slow", function() {
194 newButton.fadeIn("slow");
195 $(document).one("click", function() {
196 if (this != newButton.get(0)) {
197 newButton.fadeOut(function() {
205 })(button, newButton);
210 * Enhances a post’s “delete” button.
213 * The selector of the button
215 * The ID of the post to delete
217 * The text to replace the button with
219 function enhanceDeletePostButton(buttonId, postId, text) {
220 enhanceDeleteButton(buttonId, text, function() {
221 $.getJSON("ajax/deletePost.ajax", { "post": postId, "formPassword": $("#sone #formPassword").text() }, function(data, textStatus) {
223 $("#sone .post#" + postId).slideUp();
224 } else if (data.error == "invalid-post-id") {
225 alert("Invalid post ID given!");
226 } else if (data.error == "auth-required") {
227 alert("You need to be logged in.");
228 } else if (data.error == "not-authorized") {
229 alert("You are not allowed to delete this post.");
236 * Enhances a reply’s “delete” button.
239 * The selector of the button
241 * The ID of the reply to delete
243 * The text to replace the button with
245 function enhanceDeleteReplyButton(buttonId, replyId, text) {
246 enhanceDeleteButton(buttonId, text, function() {
247 $.getJSON("ajax/deleteReply.ajax", { "reply": replyId, "formPassword": $("#sone #formPassword").text() }, function(data, textStatus) {
249 $("#sone .reply#" + replyId).slideUp();
250 } else if (data.error == "invalid-reply-id") {
251 alert("Invalid reply ID given!");
252 } else if (data.error == "auth-required") {
253 alert("You need to be logged in.");
254 } else if (data.error == "not-authorized") {
255 alert("You are not allowed to delete this reply.");
261 function getFormPassword() {
262 return $("#sone #formPassword").text();
265 function getSoneElement(element) {
266 return $(element).parents(".sone");
270 * Generates a list of Sones by concatening the names of the given sones with a
271 * new line character (“\n”).
274 * The sones to format
275 * @returns {String} The created string
277 function generateSoneList(sones) {
279 $.each(sones, function() {
280 if (soneList != "") {
283 soneList += this.name;
289 * Returns the ID of the Sone that this element belongs to.
292 * The element to locate the matching Sone ID for
293 * @returns The ID of the Sone, or undefined
295 function getSoneId(element) {
296 return getSoneElement(element).find(".id").text();
299 function getPostElement(element) {
300 return $(element).parents(".post");
303 function getPostId(element) {
304 return getPostElement(element).attr("id");
307 function getReplyElement(element) {
308 return $(element).parents(".reply");
311 function getReplyId(element) {
312 return getReplyElement(element).attr("id");
315 function likePost(postId) {
316 $.getJSON("ajax/like.ajax", { "type": "post", "post" : postId, "formPassword": getFormPassword() }, function() {
317 $("#sone .post#" + postId + " > .inner-part > .status-line .like").addClass("hidden");
318 $("#sone .post#" + postId + " > .inner-part > .status-line .unlike").removeClass("hidden");
319 updatePostLikes(postId);
323 function unlikePost(postId) {
324 $.getJSON("ajax/unlike.ajax", { "type": "post", "post" : postId, "formPassword": getFormPassword() }, function() {
325 $("#sone .post#" + postId + " > .inner-part > .status-line .unlike").addClass("hidden");
326 $("#sone .post#" + postId + " > .inner-part > .status-line .like").removeClass("hidden");
327 updatePostLikes(postId);
331 function updatePostLikes(postId) {
332 $.getJSON("ajax/getLikes.ajax", { "type": "post", "post": postId }, function(data, textStatus) {
334 $("#sone .post#" + postId + " > .inner-part > .status-line .likes").toggleClass("hidden", data.likes == 0)
335 $("#sone .post#" + postId + " > .inner-part > .status-line .likes span.like-count").text(data.likes);
336 $("#sone .post#" + postId + " > .inner-part > .status-line .likes > span").attr("title", generateSoneList(data.sones));
341 function likeReply(replyId) {
342 $.getJSON("ajax/like.ajax", { "type": "reply", "reply" : replyId, "formPassword": getFormPassword() }, function() {
343 $("#sone .reply#" + replyId + " .status-line .like").addClass("hidden");
344 $("#sone .reply#" + replyId + " .status-line .unlike").removeClass("hidden");
345 updateReplyLikes(replyId);
349 function unlikeReply(replyId) {
350 $.getJSON("ajax/unlike.ajax", { "type": "reply", "reply" : replyId, "formPassword": getFormPassword() }, function() {
351 $("#sone .reply#" + replyId + " .status-line .unlike").addClass("hidden");
352 $("#sone .reply#" + replyId + " .status-line .like").removeClass("hidden");
353 updateReplyLikes(replyId);
357 function updateReplyLikes(replyId) {
358 $.getJSON("ajax/getLikes.ajax", { "type": "reply", "reply": replyId }, function(data, textStatus) {
360 $("#sone .reply#" + replyId + " .status-line .likes").toggleClass("hidden", data.likes == 0)
361 $("#sone .reply#" + replyId + " .status-line .likes span.like-count").text(data.likes);
362 $("#sone .reply#" + replyId + " .status-line .likes > span").attr("title", generateSoneList(data.sones));