Add name of input field to JavaScript function.
[Sone.git] / src / main / resources / static / javascript / sone.js
1 /* Sone JavaScript functions. */
2
3 function registerInputTextareaSwap(inputSelector, defaultText, inputFieldName) {
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").val(defaultText);
17                         $(inputField.form).submit(function() {
18                                 if (textarea.val() == "") {
19                                         return false;
20                                 }
21                                 $(inputField).val(textarea.val());
22                         });
23                 })(this, textarea);
24         });
25 }
26
27 /* hide all the “create reply” forms until a link is clicked. */
28 function addCommentLinks() {
29         $("#sone .post").each(function() {
30                 postId = $(this).attr("id");
31                 commentElement = (function(postId) {
32                         var commentElement = $("<div><span>Comment</span></div>").addClass("show-reply-form").click(function() {
33                                 replyElement = $("#sone .post#" + postId + " .create-reply");
34                                 replyElement.removeClass("hidden");
35                                 replyElement.removeClass("light");
36                                 (function(replyElement) {
37                                         replyElement.find("input.reply-input").blur(function() {
38                                                 if ($(this).hasClass("default")) {
39                                                         replyElement.addClass("light");
40                                                 }
41                                         }).focus(function() {
42                                                 replyElement.removeClass("light");
43                                         });
44                                 })(replyElement);
45                                 replyElement.find("input.reply-input").focus();
46                         });
47                         return commentElement;
48                 })(postId);
49                 $(this).find(".create-reply").addClass("hidden");
50                 $(this).find(".status-line .time").each(function() {
51                         $(this).after(commentElement.clone(true));
52                 });
53         });
54 }