Merge branch 'message-recipient'
[Sone.git] / src / main / resources / static / javascript / sone.js
index b7f323e..02a02a8 100644 (file)
@@ -560,7 +560,7 @@ function ajaxifyNotification(notification) {
 }
 
 function getStatus() {
-       $.getJSON("getStatus.ajax", {}, function(data, textStatus) {
+       $.getJSON("getStatus.ajax", {"loadAllSones": isKnownSonesPage()}, function(data, textStatus) {
                if ((data != null) && data.success) {
                        /* process Sone information. */
                        $.each(data.sones, function(index, value) {
@@ -636,7 +636,7 @@ function isViewSonePage() {
  *
  * @returns The ID of the currently shown Sone
  */
-function getSoneId() {
+function getShownSoneId() {
        return $("#sone .sone-id").text();
 }
 
@@ -656,10 +656,20 @@ function isViewPostPage() {
  *
  * @returns The ID of the currently shown post
  */
-function getPostId() {
+function getShownPostId() {
        return $("#sone .post-id").text();
 }
 
+/**
+ * Returns whether the current page is the “known Sones” page.
+ *
+ * @returns {Boolean} <code>true</code> if the current page is the “known
+ *          Sones” page, <code>false</code> otherwise
+ */
+function isKnownSonesPage() {
+       return getPageId() == "known-sones";
+}
+
 var loadedPosts = {};
 var loadedReplies = {};
 
@@ -670,7 +680,7 @@ function loadNewPost(postId) {
        loadedPosts[postId] = true;
        $.getJSON("getPost.ajax", { "post" : postId }, function(data, textStatus) {
                if ((data != null) && data.success) {
-                       if (!isIndexPage() && !(isViewSonePage() && (getSoneId() == data.post.sone))) {
+                       if (!isIndexPage() && !(isViewSonePage() && (getShownSoneId() == data.post.sone))) {
                                return;
                        }
                        var firstOlderPost = null;
@@ -815,6 +825,21 @@ $(document).ready(function() {
                });
        });
 
+       /* ajaxify input field on “view Sone” page. */
+       getTranslation("WebInterface.DefaultText.Message", function(defaultText) {
+               registerInputTextareaSwap("#sone #post-message input[name=text]", defaultText, "text", false, false);
+               $("#sone #post-message").submit(function() {
+                       text = $(this).find(":input:enabled").val();
+                       $.getJSON("ajax/createPost.ajax", { "formPassword": getFormPassword(), "recipient": $("#sone #sone-id").text(), "text": text }, function(data, textStatus) {
+                               if ((data != null) && data.success) {
+                                       loadNewPost(data.postId);
+                               }
+                       });
+                       $(this).find(":input:enabled").val("").blur();
+                       return false;
+               });
+       });
+
        /* Ajaxifies all posts. */
        /* calling getTranslation here will cache the necessary values. */
        getTranslation("WebInterface.Confirmation.DeletePostButton", function(text) {