f19b0d137629823bc735222398902228180d00a2
[Sone.git] / src / main / resources / static / javascript / sone.js
1 /* Sone JavaScript functions. */
2
3 function registerInputTextareaSwap(inputSelector, defaultText, inputFieldName, optional) {
4         $(inputSelector).each(function() {
5                 textarea = $("<textarea name=\"" + inputFieldName + "\"></textarea>").blur(function() {
6                         if ($(this).val() == "") {
7                                 $(this).hide();
8                                 inputField = $(this).data("inputField");
9                                 inputField.show().removeAttr("disabled").addClass("default");
10                                 (function(inputField) {
11                                         getTranslation(defaultText, function(translation) {
12                                                 inputField.val(translation);
13                                         });
14                                 })(inputField);
15                         }
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();
22                         });
23                         if (inputField.val() == "") {
24                                 inputField.addClass("default");
25                                 (function(inputField) {
26                                         getTranslation(defaultText, function(translation) {
27                                                 inputField.val(translation);
28                                         });
29                                 })(inputField);
30                         } else {
31                                 inputField.hide().attr("disabled", "disabled");
32                                 textarea.show();
33                         }
34                         $(inputField.get(0).form).submit(function() {
35                                 if (!optional && (textarea.val() == "")) {
36                                         return false;
37                                 }
38                         });
39                 })($(this), textarea);
40         });
41 }
42
43 /* hide all the “create reply” forms until a link is clicked. */
44 function addCommentLinks() {
45         $("#sone .post").each(function() {
46                 postId = $(this).attr("id");
47                 commentElement = (function(postId) {
48                         var commentElement = $("<div><span>Comment</span></div>").addClass("show-reply-form").click(function() {
49                                 replyElement = $("#sone .post#" + postId + " .create-reply");
50                                 replyElement.removeClass("hidden");
51                                 replyElement.removeClass("light");
52                                 (function(replyElement) {
53                                         replyElement.find("input.reply-input").blur(function() {
54                                                 if ($(this).hasClass("default")) {
55                                                         replyElement.addClass("light");
56                                                 }
57                                         }).focus(function() {
58                                                 replyElement.removeClass("light");
59                                         });
60                                 })(replyElement);
61                                 replyElement.find("input.reply-input").focus();
62                         });
63                         return commentElement;
64                 })(postId);
65                 $(this).find(".create-reply").addClass("hidden");
66                 $(this).find(".status-line .time").each(function() {
67                         $(this).after(commentElement.clone(true));
68                 });
69         });
70 }
71
72 /**
73  * Retrieves the translation for the given key and calls the callback function.
74  * The callback function takes a single parameter, the translated string.
75  *
76  * @param key
77  *            The key of the translation string
78  * @param callback
79  *            The callback function
80  */
81 function getTranslation(key, callback) {
82         $.getJSON("ajax/getTranslation.ajax", {"key": key}, function(data, textStatus) {
83                 callback(data.value);
84         });
85 }
86
87 /**
88  * Fires off an AJAX request to retrieve the current status of a Sone.
89  *
90  * @param soneId
91  *            The ID of the Sone
92  */
93 function getSoneStatus(soneId) {
94         $.getJSON("ajax/getSoneStatus.ajax", {"sone": soneId}, function(data, textStatus) {
95                 updateSoneStatus(soneId, data.status, data.modified, data.lastUpdated);
96                 /* seconds! */
97                 updateInterval = 60;
98                 if (data.modified || (data.status == "downloading") || (data.status == "inserting")) {
99                         updateInterval = 5;
100                 }
101                 setTimeout(function() {
102                         getSoneStatus(soneId);
103                 }, updateInterval * 1000);
104         });
105 }
106
107 /**
108  * Updates the status of the given Sone.
109  *
110  * @param soneId
111  *            The ID of the Sone to update
112  * @param status
113  *            The status of the Sone (“idle”, “unknown”, “inserting”,
114  *            “downloading”)
115  * @param modified
116  *            Whether the Sone is modified
117  * @param lastUpdated
118  *            The date and time of the last update (formatted for display)
119  */
120 function updateSoneStatus(soneId, status, modified, lastUpdated) {
121         $("#sone .sone." + soneId).
122                 toggleClass("unknown", status == "unknown").
123                 toggleClass("idle", status == "idle").
124                 toggleClass("inserting", status == "inserting").
125                 toggleClass("downloading", status == "downloading").
126                 toggleClass("modified", modified);
127         $("#sone .sone." + soneId + " .last-update span.time").text(lastUpdated);
128 }
129
130 var watchedSones = {};
131
132 /**
133  * Watches this Sone for updates to its status.
134  *
135  * @param soneId
136  *            The ID of the Sone to watch
137  */
138 function watchSone(soneId) {
139         if (watchedSones[soneId]) {
140                 return;
141         }
142         watchedSones[soneId] = true;
143         (function(soneId) {
144                 setTimeout(function() {
145                         getSoneStatus(soneId);
146                 }, 5000);
147         })(soneId);
148 }
149
150 /**
151  * Enhances a “delete” button so that the confirmation is done on the same page.
152  *
153  * @param buttonId
154  *            The selector of the button
155  * @param translationKey
156  *            The translation key of the text to show on the button
157  * @param deleteCallback
158  *            The callback that actually deletes something
159  */
160 function enhanceDeleteButton(buttonId, translationKey, deleteCallback) {
161         button = $(buttonId);
162         (function(button) {
163                 getTranslation(translationKey, function(translation) {
164                         newButton = $("<button></button>").addClass("confirm").hide().text(translation).click(function() {
165                                 $(this).fadeOut("slow");
166                                 deleteCallback();
167                                 return false;
168                         }).insertAfter(button);
169                         (function(button, newButton) {
170                                 button.click(function() {
171                                         button.fadeOut("slow", function() {
172                                                 newButton.fadeIn("slow");
173                                                 $(document).one("click", function() {
174                                                         if (this != newButton.get(0)) {
175                                                                 newButton.fadeOut(function() {
176                                                                         button.fadeIn();
177                                                                 });
178                                                         }
179                                                 });
180                                         });
181                                         return false;
182                                 });
183                         })(button, newButton);
184                 });
185         })(button);
186 }
187
188 /**
189  * Enhances a post’s “delete” button.
190  *
191  * @param buttonId
192  *            The selector of the button
193  * @param postId
194  *            The ID of the post to delete
195  */
196 function enhanceDeletePostButton(buttonId, postId) {
197         enhanceDeleteButton(buttonId, "WebInterface.Confirmation.DeletePostButton", function() {
198                 $.getJSON("ajax/deletePost.ajax", { "post": postId, "formPassword": $("#sone #formPassword").text() }, function(data, textStatus) {
199                         if (data.success) {
200                                 $("#sone .post#" + postId).slideUp();
201                         } else if (data.error == "invalid-post-id") {
202                                 alert("Invalid post ID given!");
203                         } else if (data.error == "auth-required") {
204                                 alert("You need to be logged in.");
205                         } else if (data.error == "not-authorized") {
206                                 alert("You are not allowed to delete this post.");
207                         }
208                 });
209         });
210 }
211
212 /**
213  * Enhances a reply’s “delete” button.
214  *
215  * @param buttonId
216  *            The selector of the button
217  * @param replyId
218  *            The ID of the reply to delete
219  */
220 function enhanceDeleteReplyButton(buttonId, replyId) {
221         enhanceDeleteButton(buttonId, "WebInterface.Confirmation.DeleteReplyButton", function() {
222                 $.getJSON("ajax/deleteReply.ajax", { "reply": replyId, "formPassword": $("#sone #formPassword").text() }, function(data, textStatus) {
223                         if (data.success) {
224                                 $("#sone .reply#" + replyId).slideUp();
225                         } else if (data.error == "invalid-reply-id") {
226                                 alert("Invalid reply ID given!");
227                         } else if (data.error == "auth-required") {
228                                 alert("You need to be logged in.");
229                         } else if (data.error == "not-authorized") {
230                                 alert("You are not allowed to delete this reply.");
231                         }
232                 });
233         });
234 }
235
236 function getFormPassword() {
237         return $("#sone #formPassword").text();
238 }
239
240 function getSoneElement(element) {
241         return $(element).parent(".sone");
242 }
243
244 /**
245  * Returns the ID of the Sone that this element belongs to.
246  *
247  * @param element
248  *            The element to locate the matching Sone ID for
249  * @returns The ID of the Sone, or undefined
250  */
251 function getSoneId(element) {
252         return getSoneElement(element).find(".id").text();
253 }
254
255 function getPostElement(element) {
256         return $(element).parents(".post");
257 }
258
259 function getPostId(element) {
260         return getPostElement(element).attr("id");
261 }
262
263 function likePost(postId) {
264         $.getJSON("ajax/likePost.ajax", { "post" : postId, "formPassword": getFormPassword() }, function() {
265                 $("#sone .post#" + postId + " > .status-line .like").addClass("hidden");
266                 $("#sone .post#" + postId + " > .status-line .unlike").removeClass("hidden");
267                 updatePostLikes(postId);
268         });
269 }
270
271 function unlikePost(postId) {
272         $.getJSON("ajax/unlikePost.ajax", { "post" : postId, "formPassword": getFormPassword() }, function() {
273                 $("#sone .post#" + postId + " > .status-line .unlike").addClass("hidden");
274                 $("#sone .post#" + postId + " > .status-line .like").removeClass("hidden");
275                 updatePostLikes(postId);
276         });
277 }
278
279 function updatePostLikes(postId) {
280         $.getJSON("ajax/getPostLikes.ajax", { "post": postId }, function(data, textStatus) {
281                 if (data.success) {
282                         $("#sone .post#" + postId + " > .status-line .likes span.like-count").text(data.likes);
283                 }
284         });
285 }