Make input field/textarea replacement working when input field does not start out...
[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.age < 600) {
99                         updateInterval = 5;
100                 } else if (data.age < 86400) {
101                         updateInterval = 30;
102                 }
103                 setTimeout(function() {
104                         getSoneStatus(soneId);
105                 }, updateInterval * 1000);
106         });
107 }
108
109 /**
110  * Updates the status of the given Sone.
111  *
112  * @param soneId
113  *            The ID of the Sone to update
114  * @param status
115  *            The status of the Sone (“idle”, “unknown”, “inserting”,
116  *            “downloading”)
117  * @param modified
118  *            Whether the Sone is modified
119  * @param lastUpdated
120  *            The date and time of the last update (formatted for display)
121  */
122 function updateSoneStatus(soneId, status, modified, lastUpdated) {
123         $("#sone .sone." + soneId).
124                 toggleClass("unknown", status == "unknown").
125                 toggleClass("idle", status == "idle").
126                 toggleClass("inserting", status == "inserting").
127                 toggleClass("downloading", status == "downloading").
128                 toggleClass("modified", modified);
129         $("#sone .sone." + soneId + " .last-update span.time").text(lastUpdated);
130 }
131
132 var watchedSones = {};
133
134 /**
135  * Watches this Sone for updates to its status.
136  *
137  * @param soneId
138  *            The ID of the Sone to watch
139  */
140 function watchSone(soneId) {
141         if (watchedSones[soneId]) {
142                 return;
143         }
144         watchedSones[soneId] = true;
145         (function(soneId) {
146                 setTimeout(function() {
147                         getSoneStatus(soneId);
148                 }, 5000);
149         })(soneId);
150 }