]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - js/util.js
javascript fixes for reply form fetching
[quix0rs-gnu-social.git] / js / util.js
index 1ed69be28e80465820dc6d062784d46bf1a9d8b9..0bda55b602d9cda95dbf832dc3f6b0db491b3812 100644 (file)
@@ -32,7 +32,6 @@ var SN = { // StatusNet
             MaxLength: 140,
             PatternUsername: /^[0-9a-zA-Z\-_.]*$/,
             HTTP20x30x: [200, 201, 202, 203, 204, 205, 206, 300, 301, 302, 303, 304, 305, 306, 307],
-            NoticeFormMaster: null // to be cloned from the one at top
         },
 
         /**
@@ -58,6 +57,9 @@ var SN = { // StatusNet
         }
     },
 
+    V: {    // Variables
+    },
+
     /**
      * Map of localized message strings exported to script from the PHP
      * side via Action::getScriptMessages().
@@ -217,6 +219,18 @@ var SN = { // StatusNet
             return url;
         },
 
+        FormNoticeUniqueID: function (form) {
+            var oldId = form.attr('id');
+            var newId = 'form_notice_' + Math.floor(Math.random()*999999999);
+            var attrs = ['name', 'for', 'id'];
+            for (var key in attrs) {
+                form.find("[" + attrs[key] + "~='" + oldId + "']").each(function () {
+                        var newAttr = $(this).attr(attrs[key]).replace(oldId, newId);
+                        $(this).attr(attrs[key], newAttr);
+                    });
+            }
+        },
+
         /**
          * Grabs form data and submits it asynchronously, with 'ajax=1'
          * parameter added to the rest.
@@ -576,6 +590,44 @@ var SN = { // StatusNet
             }
         },
 
+        /**
+         * Setup function -- DOES NOT trigger actions immediately.
+         *
+         * Sets up event handlers on all visible notice's option <a> elements
+         * with the "popup" class so they behave as expected with AJAX.
+         *
+         * (without javascript the link goes to a page that expects you to verify
+         * the action through a form)
+         *
+         * @access private
+         */
+        NoticeOptionsAjax: function () {
+            $(document).on('click', '.notice-options > a.popup', function (e) {
+                e.preventDefault();
+                var noticeEl = $(this).closest('.notice');
+                $.ajax({
+                    url: $(this).attr('href'),
+                    data: {ajax: 1},
+                    success: function (data, textStatus, xhr) {
+                        SN.U.NoticeOptionPopup(data, noticeEl);
+                    },
+                });
+                return false;
+            });
+        },
+
+        NoticeOptionPopup: function (data, noticeEl) {
+            title = $('head > title', data).text();
+            body = $('body', data).html();
+            dialog = $(body).dialog({
+                    height: "auto",
+                    width: "auto",
+                    modal: true,
+                    resizable: true,
+                    title: title,
+                });
+        },
+
         /**
          * Setup function -- DOES NOT trigger actions immediately.
          *
@@ -618,18 +670,14 @@ var SN = { // StatusNet
             var parentNotice = notice;
             var stripForm = true; // strip a couple things out of reply forms that are inline
 
-            var list = notice.closest('.notices');
-            if (list.hasClass('threaded-replies')) {
-                // We're replying to a reply; use reply form on the end of this list.
-            } else {
-                // We're replying to a parent notice; pull its threaded list
-                // and we'll add on the end of it. Will add the threaded list if needed.
-                var list = $('ul.threaded-replies', notice);
-                if (list.length == 0) {
-                    list = $('<ul class="notices threaded-replies xoxo"></ul>');
-                    notice.append(list);
-                    list = notice.find('ul.threaded-replies');
-                }
+            var list = notice.find('.threaded-replies');
+            if (list.length == 0) {
+                list = notice.closest('.threaded-replies');
+            }
+            if (list.length == 0) {
+                list = $('<ul class="notices threaded-replies xoxo"></ul>');
+                notice.append(list);
+                list = notice.find('.threaded-replies');
             }
 
             var nextStep = function () {
@@ -642,6 +690,7 @@ var SN = { // StatusNet
                     replyForm.find('label[for=notice_to]').hide();
                     replyForm.find('label[for=notice_private]').hide();
                 }
+                replyItem.show();
 
                 // Set focus...
                 var text = replyForm.find('textarea');
@@ -661,30 +710,42 @@ var SN = { // StatusNet
                 }
             };
 
-            // Create the reply form entry at the end
+            // Create the reply form entry
             var replyItem = $('li.notice-reply', list);
             if (replyItem.length == 0) {
                 replyItem = $('<li class="notice-reply"></li>');
-
-                // Fetch a fresh copy of the notice form over AJAX.
-                var url = $('#input_form_status > form').attr('action');
-                $.get(url, {ajax: 1, inreplyto: id}, function (data, textStatus, xhr) {
-                    var formEl = document._importNode($('form', data)[0], true);
-                    replyItem.append(formEl);
-                    list.append(replyItem);
-
-                    replyForm = $(formEl);
-                    SN.Init.NoticeFormSetup(replyForm);
-                    nextStep();
-                });
-            } else {
-                replyForm = replyItem.children('form');
-                if (SN.Init.NoticeFormSetup(replyForm)) {
-                    nextStep();
+            }
+            replyForm = replyItem.children('form');
+            if (replyForm.length == 0) {
+                // Let's try another trick to avoid fetching by URL
+                var noticeForm = $('#input_form_status > form');
+                if (noticeForm.length == 0) {
+                    // No notice form found on the page, so let's just
+                    // fetch a fresh copy of the notice form over AJAX.
+                    $.ajax({
+                        url: SN.V.urlNewNotice,
+                        data: {ajax: 1, inreplyto: id},
+                        success: function (data, textStatus, xhr) {
+                            var formEl = document._importNode($('form', data)[0], true);
+                            replyForm = $(formEl);
+                            replyItem.append(replyForm);
+                            list.append(replyItem);
+
+                            SN.Init.NoticeFormSetup(replyForm);
+                            nextStep();
+                        },
+                    });
+                    // We do everything relevant in 'success' above
+                    return;
                 }
-                replyItem.show();
-                replyItem.find('textarea').focus();
+                replyForm = noticeForm.clone();
+                SN.Init.NoticeFormSetup(replyForm);
+                replyItem.append(replyForm);
+                list.append(replyItem);
             }
+            // replyForm is set, we're not fetching by URL...
+            // Next setp is to configure in-reply-to etc.
+            nextStep();
         },
 
         /**
@@ -697,11 +758,15 @@ var SN = { // StatusNet
             $(document).on('click', 'li.notice-reply-comments a', function () {
                     var url = $(this).attr('href');
                     var area = $(this).closest('.threaded-replies');
-                    $.get(url, {ajax: 1}, function (data, textStatus, xhr) {
-                        var replies = $('.threaded-replies', data);
-                        if (replies.length) {
-                            area.replaceWith(document._importNode(replies[0], true));
-                        }
+                    $.ajax({
+                        url: url,
+                        data: {ajax: 1},
+                        success: function (data, textStatus, xhr) {
+                            var replies = $('.threaded-replies', data);
+                            if (replies.length) {
+                                area.replaceWith(document._importNode(replies[0], true));
+                            }
+                        },
                     });
                     return false;
                 });
@@ -1412,9 +1477,6 @@ var SN = { // StatusNet
                         });
                     }
                 });
-
-                // Infield labels for notice form inputs.
-                $('.input_forms fieldset fieldset label').inFieldLabels({ fadeOpacity:0 });
             }
         },
 
@@ -1430,6 +1492,7 @@ var SN = { // StatusNet
                 return false;
             }
             SN.U.NoticeLocationAttach(form);
+            SN.U.FormNoticeUniqueID(form);
             SN.U.FormNoticeXHR(form);
             SN.U.FormNoticeEnhancements(form);
             SN.U.NoticeDataAttach(form);
@@ -1444,13 +1507,10 @@ var SN = { // StatusNet
          */
         Notices: function () {
             if ($('body.user_in').length > 0) {
-                var masterForm = $('.form_notice:first');
-                if (masterForm.length > 0) {
-                    SN.C.I.NoticeFormMaster = document._importNode(masterForm[0], true);
-                }
                 SN.U.NoticeRepeat();
                 SN.U.NoticeReply();
                 SN.U.NoticeInlineReplySetup();
+                SN.U.NoticeOptionsAjax();
             }
 
             SN.U.NoticeAttachments();