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() {
49 $("#sone .post").each(function() {
50 postId = $(this).attr("id");
51 commentElement = (function(postId) {
52 var commentElement = $("<div><span>Comment</span></div>").addClass("show-reply-form").click(function() {
53 replyElement = $("#sone .post#" + postId + " .create-reply");
54 replyElement.removeClass("hidden");
55 replyElement.removeClass("light");
56 (function(replyElement) {
57 replyElement.find("input.reply-input").blur(function() {
58 if ($(this).hasClass("default")) {
59 replyElement.addClass("light");
62 replyElement.removeClass("light");
65 replyElement.find("input.reply-input").focus();
67 return commentElement;
69 $(this).find(".create-reply").addClass("hidden");
70 $(this).find(".status-line .time").each(function() {
71 $(this).after(commentElement.clone(true));
77 * Retrieves the translation for the given key and calls the callback function.
78 * The callback function takes a single parameter, the translated string.
81 * The key of the translation string
83 * The callback function
85 function getTranslation(key, callback) {
86 $.getJSON("ajax/getTranslation.ajax", {"key": key}, function(data, textStatus) {
92 * Fires off an AJAX request to retrieve the current status of a Sone.
97 function getSoneStatus(soneId) {
98 $.getJSON("ajax/getSoneStatus.ajax", {"sone": soneId}, function(data, textStatus) {
99 updateSoneStatus(soneId, data.status, data.modified, data.lastUpdated);
102 if (data.modified || (data.status == "downloading") || (data.status == "inserting")) {
105 setTimeout(function() {
106 getSoneStatus(soneId);
107 }, updateInterval * 1000);
112 * Updates the status of the given Sone.
115 * The ID of the Sone to update
117 * The status of the Sone (“idle”, “unknown”, “inserting”,
120 * Whether the Sone is modified
122 * The date and time of the last update (formatted for display)
124 function updateSoneStatus(soneId, status, modified, lastUpdated) {
125 $("#sone .sone." + soneId).
126 toggleClass("unknown", status == "unknown").
127 toggleClass("idle", status == "idle").
128 toggleClass("inserting", status == "inserting").
129 toggleClass("downloading", status == "downloading").
130 toggleClass("modified", modified);
131 $("#sone .sone." + soneId + " .last-update span.time").text(lastUpdated);
134 var watchedSones = {};
137 * Watches this Sone for updates to its status.
140 * The ID of the Sone to watch
142 function watchSone(soneId) {
143 if (watchedSones[soneId]) {
146 watchedSones[soneId] = true;
148 setTimeout(function() {
149 getSoneStatus(soneId);
155 * Enhances a “delete” button so that the confirmation is done on the same page.
158 * The selector of the button
159 * @param translationKey
160 * The translation key of the text to show on the button
161 * @param deleteCallback
162 * The callback that actually deletes something
164 function enhanceDeleteButton(buttonId, translationKey, deleteCallback) {
165 button = $(buttonId);
167 getTranslation(translationKey, function(translation) {
168 newButton = $("<button></button>").addClass("confirm").hide().text(translation).click(function() {
169 $(this).fadeOut("slow");
172 }).insertAfter(button);
173 (function(button, newButton) {
174 button.click(function() {
175 button.fadeOut("slow", function() {
176 newButton.fadeIn("slow");
177 $(document).one("click", function() {
178 if (this != newButton.get(0)) {
179 newButton.fadeOut(function() {
187 })(button, newButton);
193 * Enhances a post’s “delete” button.
196 * The selector of the button
198 * The ID of the post to delete
200 function enhanceDeletePostButton(buttonId, postId) {
201 enhanceDeleteButton(buttonId, "WebInterface.Confirmation.DeletePostButton", function() {
202 $.getJSON("ajax/deletePost.ajax", { "post": postId, "formPassword": $("#sone #formPassword").text() }, function(data, textStatus) {
204 $("#sone .post#" + postId).slideUp();
205 } else if (data.error == "invalid-post-id") {
206 alert("Invalid post ID given!");
207 } else if (data.error == "auth-required") {
208 alert("You need to be logged in.");
209 } else if (data.error == "not-authorized") {
210 alert("You are not allowed to delete this post.");
217 * Enhances a reply’s “delete” button.
220 * The selector of the button
222 * The ID of the reply to delete
224 function enhanceDeleteReplyButton(buttonId, replyId) {
225 enhanceDeleteButton(buttonId, "WebInterface.Confirmation.DeleteReplyButton", function() {
226 $.getJSON("ajax/deleteReply.ajax", { "reply": replyId, "formPassword": $("#sone #formPassword").text() }, function(data, textStatus) {
228 $("#sone .reply#" + replyId).slideUp();
229 } else if (data.error == "invalid-reply-id") {
230 alert("Invalid reply ID given!");
231 } else if (data.error == "auth-required") {
232 alert("You need to be logged in.");
233 } else if (data.error == "not-authorized") {
234 alert("You are not allowed to delete this reply.");
240 function getFormPassword() {
241 return $("#sone #formPassword").text();
244 function getSoneElement(element) {
245 return $(element).parents(".sone");
249 * Returns the ID of the Sone that this element belongs to.
252 * The element to locate the matching Sone ID for
253 * @returns The ID of the Sone, or undefined
255 function getSoneId(element) {
256 return getSoneElement(element).find(".id").text();
259 function getPostElement(element) {
260 return $(element).parents(".post");
263 function getPostId(element) {
264 return getPostElement(element).attr("id");
267 function likePost(postId) {
268 $.getJSON("ajax/likePost.ajax", { "post" : postId, "formPassword": getFormPassword() }, function() {
269 $("#sone .post#" + postId + " > .status-line .like").addClass("hidden");
270 $("#sone .post#" + postId + " > .status-line .unlike").removeClass("hidden");
271 updatePostLikes(postId);
275 function unlikePost(postId) {
276 $.getJSON("ajax/unlikePost.ajax", { "post" : postId, "formPassword": getFormPassword() }, function() {
277 $("#sone .post#" + postId + " > .status-line .unlike").addClass("hidden");
278 $("#sone .post#" + postId + " > .status-line .like").removeClass("hidden");
279 updatePostLikes(postId);
283 function updatePostLikes(postId) {
284 $.getJSON("ajax/getPostLikes.ajax", { "post": postId }, function(data, textStatus) {
286 $("#sone .post#" + postId + " > .status-line .likes span.like-count").text(data.likes);