]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - js/util.js
Make inline reply forms close when clicking outside them if there's no text again.
[quix0rs-gnu-social.git] / js / util.js
index 6bba34fae66bf97414b9d5fb0a18437a94c89000..29fdfe2a98f47f0ac8fa5d2c278af0cd1abf7df6 100644 (file)
@@ -104,7 +104,7 @@ var SN = { // StatusNet
 
                 SN.U.Counter(form);
 
-                NDT = form.find('[name=status_textarea]');
+                NDT = form.find('.notice_data-text:first');
 
                 NDT.bind('keyup', function(e) {
                     SN.U.Counter(form);
@@ -183,7 +183,7 @@ var SN = { // StatusNet
          * @return number of chars
          */
         CharacterCount: function(form) {
-            return form.find('[name=status_textarea]').val().length;
+            return form.find('.notice_data-text:first').val().length;
         },
 
         /**
@@ -327,7 +327,7 @@ var SN = { // StatusNet
                 dataType: 'xml',
                 timeout: '60000',
                 beforeSend: function(formData) {
-                    if (form.find('[name=status_textarea]').val() == '') {
+                    if (form.find('.notice_data-text:first').val() == '') {
                         form.addClass(SN.C.S.Warning);
                         return false;
                     }
@@ -612,10 +612,7 @@ var SN = { // StatusNet
                         list.append(replyItem);
 
                         var form = replyForm = $(formEl);
-                        SN.U.NoticeLocationAttach(form);
-                        SN.U.FormNoticeXHR(form);
-                        SN.U.FormNoticeEnhancements(form);
-                        SN.U.NoticeDataAttach(form);
+                        SN.Init.NoticeFormSetup(form);
 
                         nextStep();
                     };
@@ -643,6 +640,7 @@ var SN = { // StatusNet
                                 '</li>');
             placeholder.click(function() {
                 SN.U.NoticeInlineReplyTrigger(notice);
+                return false;
             });
             placeholder.find('input').val(SN.msg('reply_placeholder'));
             list.append(placeholder);
@@ -1280,6 +1278,13 @@ var SN = { // StatusNet
             return false;
         },
 
+        /**
+         * Switch to another active input sub-form.
+         * This will hide the current form (if any), show the new one, and
+         * update the input type tab selection state.
+         *
+         * @param {String} tag
+         */
        switchInputFormTab: function(tag) {
            // The one that's current isn't current anymore
            $('.input_form_nav_tab.current').removeClass('current');
@@ -1303,14 +1308,46 @@ var SN = { // StatusNet
             if ($('body.user_in').length > 0) {
                 $('.ajax-notice').each(function() {
                     var form = $(this);
-                    SN.U.NoticeLocationAttach(form);
-                    SN.U.FormNoticeXHR(form);
-                    SN.U.FormNoticeEnhancements(form);
-                    SN.U.NoticeDataAttach(form);
+                    SN.Init.NoticeFormSetup(form);
+                });
+
+                // Make inline reply forms self-close when clicking out.
+                $('body').bind('click', function(e) {
+                    var openReplies = $('li.notice-reply');
+                    if (openReplies.length > 0) {
+                        var target = $(e.target).closest('li.notice-reply');
+                        if (target.length == 0) {
+                            // There are inline replies open, and we
+                            // clicked outside of one...
+                            openReplies.each(function() {
+                                var replyItem = $(this);
+                                // Only close if there's been no edit.
+                                if (replyItem.find('.notice_data-text:first').val() == '') {
+                                    var parentNotice = replyItem.closest('li.notice');
+                                    replyItem.remove();
+                                    SN.U.NoticeInlineReplyPlaceholder(parentNotice);
+                                }
+                            });
+                        }
+                    }
                 });
             }
         },
 
+        /**
+         * Encapsulate notice form setup for a single form.
+         * Plugins can add extra setup by monkeypatching this
+         * function.
+         *
+         * @param {jQuery} form
+         */
+        NoticeFormSetup: function(form) {
+            SN.U.NoticeLocationAttach(form);
+            SN.U.FormNoticeXHR(form);
+            SN.U.FormNoticeEnhancements(form);
+            SN.U.NoticeDataAttach(form);
+        },
+
         /**
          * Run setup code for notice timeline views items:
          *