Add default texts for input fields with AJAX.
[Sone.git] / src / main / resources / static / javascript / sone.js
index 7fbdab9..5d75952 100644 (file)
@@ -1,25 +1,31 @@
 /* Sone JavaScript functions. */
 
-function registerInputTextareaSwap(inputSelector, defaultText) {
-       (function(inputSelector, defaultText) {
-               $(inputSelector).focus(function() {
-                       if ($(this).hasClass("default")) {
-                               $(this).removeClass("default").val("");
-                       }
-               }).blur(function() {
-                       alert($(this).val());
+function registerInputTextareaSwap(inputSelector, defaultText, inputFieldName, optional) {
+       $(inputSelector).each(function() {
+               textarea = $("<textarea name=\"" + inputFieldName + "\"></textarea>").blur(function() {
                        if ($(this).val() == "") {
-                               alert("setting default");
-                               $(this).val(defaultText).addClass("default");
-                       }
-               }).addClass("default").val(defaultText);
-               $($(inputSelector).get(0).form).submit(function() {
-                       if ($(inputSelector).hasClass("default")) {
-                               $(inputSelector).val("");
+                               $(this).hide();
+                               $(this).data("inputField").show().removeAttr("disabled");
                        }
-                       alert(($(inputSelector).hasClass("default") ? "def: ": "ok: ") + $(inputSelector).val());
-               });
-       })(inputSelector, defaultText);
+               }).hide().data("inputField", $(this));
+               $(this).after(textarea);
+               (function(inputField, textarea) {
+                       $(inputField).focus(function() {
+                               $(this).hide().attr("disabled", "disabled");
+                               textarea.show().focus();
+                       }).addClass("default");
+                       (function(inputField) {
+                               $.getJSON("ajax/getTranslation.ajax", {"key": defaultText}, function(data, textStatus) {
+                                       $(inputField).val(data.value);
+                               });
+                       })(inputField);
+                       $(inputField.form).submit(function() {
+                               if (!optional && (textarea.val() == "")) {
+                                       return false;
+                               }
+                       });
+               })(this, textarea);
+       });
 }
 
 /* hide all the “create reply” forms until a link is clicked. */
@@ -27,7 +33,7 @@ function addCommentLinks() {
        $("#sone .post").each(function() {
                postId = $(this).attr("id");
                commentElement = (function(postId) {
-                       var commentElement = $("<div>Comment</div>").addClass("show-reply-form").click(function() {
+                       var commentElement = $("<div><span>Comment</span></div>").addClass("show-reply-form").click(function() {
                                replyElement = $("#sone .post#" + postId + " .create-reply");
                                replyElement.removeClass("hidden");
                                replyElement.removeClass("light");