🔀 Merge branch 'release/v82'
[Sone.git] / javascript / sone.js
1 /* Sone JavaScript functions. */
2
3 function isOnline() {
4         return $("#sone").hasClass("online");
5 }
6
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() == "") {
11                                 $(this).hide();
12                                 inputField = $(this).data("inputField");
13                                 inputField.show().removeAttr("disabled").addClass("default");
14                                 (function(inputField) {
15                                         getTranslation(defaultText, function(translation) {
16                                                 inputField.val(translation);
17                                         });
18                                 })(inputField);
19                         }
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();
26                         });
27                         if (inputField.val() == "") {
28                                 inputField.addClass("default");
29                                 (function(inputField) {
30                                         getTranslation(defaultText, function(translation) {
31                                                 inputField.val(translation);
32                                         });
33                                 })(inputField);
34                         } else {
35                                 inputField.hide().attr("disabled", "disabled");
36                                 textarea.show();
37                         }
38                         $(inputField.get(0).form).submit(function() {
39                                 if (!optional && (textarea.val() == "")) {
40                                         return false;
41                                 }
42                         });
43                 })($(this), textarea);
44         });
45 }
46
47 /* hide all the â€ścreate reply” forms until a link is clicked. */
48 function addCommentLinks() {
49         if (!isOnline()) {
50                 return;
51         }
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");
63                                                 }
64                                         }).focus(function() {
65                                                 replyElement.removeClass("light");
66                                         });
67                                 })(replyElement);
68                                 replyElement.find("input.reply-input").focus();
69                         });
70                         return commentElement;
71                 })(postId);
72                 $(this).find(".create-reply").addClass("hidden");
73                 $(this).find(".status-line .time").each(function() {
74                         $(this).after(commentElement.clone(true));
75                 });
76         });
77 }
78
79 /**
80  * Retrieves the translation for the given key and calls the callback function.
81  * The callback function takes a single parameter, the translated string.
82  *
83  * @param key
84  *            The key of the translation string
85  * @param callback
86  *            The callback function
87  */
88 function getTranslation(key, callback) {
89         $.getJSON("ajax/getTranslation.ajax", {"key": key}, function(data, textStatus) {
90                 callback(data.value);
91         });
92 }
93
94 /**
95  * Fires off an AJAX request to retrieve the current status of a Sone.
96  *
97  * @param soneId
98  *            The ID of the Sone
99  */
100 function getSoneStatus(soneId) {
101         $.getJSON("ajax/getSoneStatus.ajax", {"sone": soneId}, function(data, textStatus) {
102                 updateSoneStatus(soneId, data.name, data.status, data.modified, data.lastUpdated);
103                 /* seconds! */
104                 updateInterval = 60;
105                 if (data.modified || (data.status == "downloading") || (data.status == "inserting")) {
106                         updateInterval = 5;
107                 }
108                 setTimeout(function() {
109                         getSoneStatus(soneId);
110                 }, updateInterval * 1000);
111         });
112 }
113
114 /**
115  * Filters the given Sone ID, replacing all â€ś~” characters by an underscore.
116  *
117  * @param soneId
118  *            The Sone ID to filter
119  * @returns The filtered Sone ID
120  */
121 function filterSoneId(soneId) {
122         return soneId.replace(/[^a-zA-Z0-9-]/g, "_");
123 }
124
125 /**
126  * Updates the status of the given Sone.
127  *
128  * @param soneId
129  *            The ID of the Sone to update
130  * @param status
131  *            The status of the Sone (“idle”, â€śunknown”, â€śinserting”,
132  *            â€śdownloading”)
133  * @param modified
134  *            Whether the Sone is modified
135  * @param lastUpdated
136  *            The date and time of the last update (formatted for display)
137  */
138 function updateSoneStatus(soneId, name, status, modified, lastUpdated) {
139         $("#sone .sone." + filterSoneId(soneId)).
140                 toggleClass("unknown", status == "unknown").
141                 toggleClass("idle", status == "idle").
142                 toggleClass("inserting", status == "inserting").
143                 toggleClass("downloading", status == "downloading").
144                 toggleClass("modified", modified);
145         $("#sone .sone." + filterSoneId(soneId) + " .last-update span.time").text(lastUpdated);
146         $("#sone .sone." + filterSoneId(soneId) + " .profile-link a").text(name);
147 }
148
149 var watchedSones = {};
150
151 /**
152  * Watches this Sone for updates to its status.
153  *
154  * @param soneId
155  *            The ID of the Sone to watch
156  */
157 function watchSone(soneId) {
158         if (watchedSones[soneId]) {
159                 return;
160         }
161         watchedSones[soneId] = true;
162         (function(soneId) {
163                 setTimeout(function() {
164                         getSoneStatus(soneId);
165                 }, 5000);
166         })(soneId);
167 }
168
169 /**
170  * Enhances a â€śdelete” button so that the confirmation is done on the same page.
171  *
172  * @param buttonId
173  *            The selector of the button
174  * @param text
175  *            The text to show on the button
176  * @param deleteCallback
177  *            The callback that actually deletes something
178  */
179 function enhanceDeleteButton(buttonId, text, deleteCallback) {
180         button = $(buttonId);
181         (function(button) {
182                 newButton = $("<button></button>").addClass("confirm").hide().text(text).click(function() {
183                         $(this).fadeOut("slow");
184                         deleteCallback();
185                         return false;
186                 }).insertAfter(button);
187                 (function(button, newButton) {
188                         button.click(function() {
189                                 button.fadeOut("slow", function() {
190                                         newButton.fadeIn("slow");
191                                         $(document).one("click", function() {
192                                                 if (this != newButton.get(0)) {
193                                                         newButton.fadeOut(function() {
194                                                                 button.fadeIn();
195                                                         });
196                                                 }
197                                         });
198                                 });
199                                 return false;
200                         });
201                 })(button, newButton);
202         })(button);
203 }
204
205 /**
206  * Enhances a post’s â€śdelete” button.
207  *
208  * @param buttonId
209  *            The selector of the button
210  * @param postId
211  *            The ID of the post to delete
212  * @param text
213  *            The text to replace the button with
214  */
215 function enhanceDeletePostButton(buttonId, postId, text) {
216         enhanceDeleteButton(buttonId, text, function() {
217                 $.getJSON("ajax/deletePost.ajax", { "post": postId, "formPassword": $("#sone #formPassword").text() }, function(data, textStatus) {
218                         if (data.success) {
219                                 $("#sone .post#" + postId).slideUp();
220                         } else if (data.error == "invalid-post-id") {
221                                 alert("Invalid post ID given!");
222                         } else if (data.error == "auth-required") {
223                                 alert("You need to be logged in.");
224                         } else if (data.error == "not-authorized") {
225                                 alert("You are not allowed to delete this post.");
226                         }
227                 });
228         });
229 }
230
231 /**
232  * Enhances a reply’s â€śdelete” button.
233  *
234  * @param buttonId
235  *            The selector of the button
236  * @param replyId
237  *            The ID of the reply to delete
238  * @param text
239  *            The text to replace the button with
240  */
241 function enhanceDeleteReplyButton(buttonId, replyId, text) {
242         enhanceDeleteButton(buttonId, text, function() {
243                 $.getJSON("ajax/deleteReply.ajax", { "reply": replyId, "formPassword": $("#sone #formPassword").text() }, function(data, textStatus) {
244                         if (data.success) {
245                                 $("#sone .reply#" + replyId).slideUp();
246                         } else if (data.error == "invalid-reply-id") {
247                                 alert("Invalid reply ID given!");
248                         } else if (data.error == "auth-required") {
249                                 alert("You need to be logged in.");
250                         } else if (data.error == "not-authorized") {
251                                 alert("You are not allowed to delete this reply.");
252                         }
253                 });
254         });
255 }
256
257 function getFormPassword() {
258         return $("#sone #formPassword").text();
259 }
260
261 function getSoneElement(element) {
262         return $(element).parents(".sone");
263 }
264
265 /**
266  * Generates a list of Sones by concatening the names of the given sones with a
267  * new line character (“\n”).
268  *
269  * @param sones
270  *            The sones to format
271  * @returns {String} The created string
272  */
273 function generateSoneList(sones) {
274         var soneList = "";
275         $.each(sones, function() {
276                 if (soneList != "") {
277                         soneList += "\n";
278                 }
279                 soneList += this.name;
280         });
281         return soneList;
282 }
283
284 /**
285  * Returns the ID of the Sone that this element belongs to.
286  *
287  * @param element
288  *            The element to locate the matching Sone ID for
289  * @returns The ID of the Sone, or undefined
290  */
291 function getSoneId(element) {
292         return getSoneElement(element).find(".id").text();
293 }
294
295 function getPostElement(element) {
296         return $(element).parents(".post");
297 }
298
299 function getPostId(element) {
300         return getPostElement(element).attr("id");
301 }
302
303 function getReplyElement(element) {
304         return $(element).parents(".reply");
305 }
306
307 function getReplyId(element) {
308         return getReplyElement(element).attr("id");
309 }
310
311 function likePost(postId) {
312         $.getJSON("ajax/like.ajax", { "type": "post", "post" : postId, "formPassword": getFormPassword() }, function() {
313                 $("#sone .post#" + postId + " > .inner-part > .status-line .like").addClass("hidden");
314                 $("#sone .post#" + postId + " > .inner-part > .status-line .unlike").removeClass("hidden");
315                 updatePostLikes(postId);
316         });
317 }
318
319 function unlikePost(postId) {
320         $.getJSON("ajax/unlike.ajax", { "type": "post", "post" : postId, "formPassword": getFormPassword() }, function() {
321                 $("#sone .post#" + postId + " > .inner-part > .status-line .unlike").addClass("hidden");
322                 $("#sone .post#" + postId + " > .inner-part > .status-line .like").removeClass("hidden");
323                 updatePostLikes(postId);
324         });
325 }
326
327 function updatePostLikes(postId) {
328         $.getJSON("ajax/getLikes.ajax", { "type": "post", "post": postId }, function(data, textStatus) {
329                 if (data.success) {
330                         $("#sone .post#" + postId + " > .inner-part > .status-line .likes").toggleClass("hidden", data.likes == 0)
331                         $("#sone .post#" + postId + " > .inner-part > .status-line .likes span.like-count").text(data.likes);
332                         $("#sone .post#" + postId + " > .inner-part > .status-line .likes > span").attr("title", generateSoneList(data.sones));
333                 }
334         });
335 }
336
337 function likeReply(replyId) {
338         $.getJSON("ajax/like.ajax", { "type": "reply", "reply" : replyId, "formPassword": getFormPassword() }, function() {
339                 $("#sone .reply#" + replyId + " .status-line .like").addClass("hidden");
340                 $("#sone .reply#" + replyId + " .status-line .unlike").removeClass("hidden");
341                 updateReplyLikes(replyId);
342         });
343 }
344
345 function unlikeReply(replyId) {
346         $.getJSON("ajax/unlike.ajax", { "type": "reply", "reply" : replyId, "formPassword": getFormPassword() }, function() {
347                 $("#sone .reply#" + replyId + " .status-line .unlike").addClass("hidden");
348                 $("#sone .reply#" + replyId + " .status-line .like").removeClass("hidden");
349                 updateReplyLikes(replyId);
350         });
351 }
352
353 function updateReplyLikes(replyId) {
354         $.getJSON("ajax/getLikes.ajax", { "type": "reply", "reply": replyId }, function(data, textStatus) {
355                 if (data.success) {
356                         $("#sone .reply#" + replyId + " .status-line .likes").toggleClass("hidden", data.likes == 0)
357                         $("#sone .reply#" + replyId + " .status-line .likes span.like-count").text(data.likes);
358                         $("#sone .reply#" + replyId + " .status-line .likes > span").attr("title", generateSoneList(data.sones));
359                 }
360         });
361 }