53e23695885d5cd135e930ddf2f3c365247a9b38
[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                                 $(this).data("inputField").show().removeAttr("disabled");
9                         }
10                 }).hide().data("inputField", $(this));
11                 $(this).after(textarea);
12                 (function(inputField, textarea) {
13                         $(inputField).focus(function() {
14                                 $(this).hide().attr("disabled", "disabled");
15                                 textarea.show().focus();
16                         }).addClass("default");
17                         (function(inputField) {
18                                 getTranslation(defaultText, function(translation) {
19                                         $(inputField).val(translation);
20                                 });
21                         })(inputField);
22                         $(inputField.form).submit(function() {
23                                 if (!optional && (textarea.val() == "")) {
24                                         return false;
25                                 }
26                         });
27                 })(this, textarea);
28         });
29 }
30
31 /* hide all the “create reply” forms until a link is clicked. */
32 function addCommentLinks() {
33         $("#sone .post").each(function() {
34                 postId = $(this).attr("id");
35                 commentElement = (function(postId) {
36                         var commentElement = $("<div><span>Comment</span></div>").addClass("show-reply-form").click(function() {
37                                 replyElement = $("#sone .post#" + postId + " .create-reply");
38                                 replyElement.removeClass("hidden");
39                                 replyElement.removeClass("light");
40                                 (function(replyElement) {
41                                         replyElement.find("input.reply-input").blur(function() {
42                                                 if ($(this).hasClass("default")) {
43                                                         replyElement.addClass("light");
44                                                 }
45                                         }).focus(function() {
46                                                 replyElement.removeClass("light");
47                                         });
48                                 })(replyElement);
49                                 replyElement.find("input.reply-input").focus();
50                         });
51                         return commentElement;
52                 })(postId);
53                 $(this).find(".create-reply").addClass("hidden");
54                 $(this).find(".status-line .time").each(function() {
55                         $(this).after(commentElement.clone(true));
56                 });
57         });
58 }
59
60 /**
61  * Retrieves the translation for the given key and calls the callback function.
62  * The callback function takes a single parameter, the translated string.
63  *
64  * @param key
65  *            The key of the translation string
66  * @param callback
67  *            The callback function
68  */
69 function getTranslation(key, callback) {
70         $.getJSON("ajax/getTranslation.ajax", {"key": key}, function(data, textStatus) {
71                 callback(data.value);
72         });
73 }
74
75 /**
76  * Fires off an AJAX request to retrieve the current status of a Sone.
77  *
78  * @param soneId
79  *            The ID of the Sone
80  */
81 function getSoneStatus(soneId) {
82         $.getJSON("ajax/getSoneStatus.ajax", {"sone": soneId}, function(data, textStatus) {
83                 updateSoneStatus(soneId, data.status, data.modified, data.lastUpdated);
84                 /* seconds! */
85                 updateInterval = 60;
86                 if (data.age < 600) {
87                         updateInterval = 5;
88                 } else if (data.age < 86400) {
89                         updateInterval = 30;
90                 }
91                 setTimeout(function() {
92                         getSoneStatus(soneId);
93                 }, updateInterval * 1000);
94         });
95 }
96
97 /**
98  * Updates the status of the given Sone.
99  *
100  * @param soneId
101  *            The ID of the Sone to update
102  * @param status
103  *            The status of the Sone (“idle”, “unknown”, “inserting”,
104  *            “downloading”)
105  * @param modified
106  *            Whether the Sone is modified
107  * @param lastUpdated
108  *            The date and time of the last update (formatted for display)
109  */
110 function updateSoneStatus(soneId, status, modified, lastUpdated) {
111         $("#sone .sone." + soneId).
112                 toggleClass("unknown", status == "unknown").
113                 toggleClass("idle", status == "idle").
114                 toggleClass("inserting", status == "inserting").
115                 toggleClass("downloading", status == "downloading").
116                 toggleClass("modified", modified);
117         $("#sone .sone." + soneId + " .last-update span.time").text(lastUpdated);
118 }
119
120 var watchedSones = {};
121
122 /**
123  * Watches this Sone for updates to its status.
124  *
125  * @param soneId
126  *            The ID of the Sone to watch
127  */
128 function watchSone(soneId) {
129         if (watchedSones[soneId]) {
130                 return;
131         }
132         watchedSones[soneId] = true;
133         (function(soneId) {
134                 setTimeout(function() {
135                         getSoneStatus(soneId);
136                 }, 5000);
137         })(soneId);
138 }