]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - js/util.js
Merge branch '1.0.x' of git://gitorious.org/statusnet/mainline
[quix0rs-gnu-social.git] / js / util.js
index d62adf33a7fa85c1332fe42866d14baff9ca7907..6a67da4bcd25ac82bac1cc13a1854b59dd1c0129 100644 (file)
@@ -30,8 +30,7 @@ var SN = { // StatusNet
             CounterBlackout: false,
             MaxLength: 140,
             PatternUsername: /^[0-9a-zA-Z\-_.]*$/,
-            HTTP20x30x: [200, 201, 202, 203, 204, 205, 206, 300, 301, 302, 303, 304, 305, 306, 307],
-            UploadCounter: 0
+            HTTP20x30x: [200, 201, 202, 203, 204, 205, 206, 300, 301, 302, 303, 304, 305, 306, 307]
         },
 
         S: { // Selector
@@ -85,7 +84,7 @@ var SN = { // StatusNet
                 form.find('#'+SN.C.S.NoticeTextCount).text(jQuery.data(form[0], 'ElementData').MaxLength);
             }
 
-            if ($('body')[0].id != 'conversation' && window.location.hash.length === 0) {
+            if ($('body')[0].id != 'conversation' && window.location.hash.length === 0 && $(window).scrollTop() == 0) {
                 form.find('textarea').focus();
             }
         },
@@ -173,7 +172,6 @@ var SN = { // StatusNet
         FormNoticeXHR: function(form) {
             SN.C.I.NoticeDataGeo = {};
             form.append('<input type="hidden" name="ajax" value="1"/>');
-            
             form.ajaxForm({
                 dataType: 'xml',
                 timeout: '60000',
@@ -230,10 +228,9 @@ var SN = { // StatusNet
                         }
                         else {
                             if (parseInt(xhr.status) === 0 || jQuery.inArray(parseInt(xhr.status), SN.C.I.HTTP20x30x) >= 0) {
-                                form.resetForm();
-                                SN.U.NoticeClearAttachments(form);
-                                SN.C.I.UploadCounter = 0;
-                                SN.U.NoticeNewAttachment($('fieldset', form));
+                                form
+                                    .resetForm()
+                                    .find('#'+SN.C.S.NoticeDataAttachSelected).remove();
                                 SN.U.FormNoticeEnhancements(form);
                             }
                             else {
@@ -261,9 +258,10 @@ var SN = { // StatusNet
                             form.append('<p class="form_response success">'+result+'</p>');
                         }
                         else {
+                            // New notice post was successful. If on our timeline, show it!
+                            var notice = document._importNode($('li', data)[0], true);
                             var notices = $('#notices_primary .notices');
-                            if (notices.length > 0) {
-                                var notice = document._importNode($('li', data)[0], true);
+                            if (notices.length > 0 && SN.U.belongsOnTimeline(notice)) {
                                 if ($('#'+notice.id).length === 0) {
                                     var notice_irt_value = $('#'+SN.C.S.NoticeInReplyTo).val();
                                     var notice_irt = '#notices_primary #notice-'+notice_irt_value;
@@ -284,15 +282,16 @@ var SN = { // StatusNet
                                 }
                             }
                             else {
+                                // Not on a timeline that this belongs on?
+                                // Just show a success message.
                                 result = document._importNode($('title', data)[0], true);
                                 result_title = result.textContent || result.innerHTML;
                                 form.append('<p class="form_response success">'+result_title+'</p>');
                             }
                         }
                         form.resetForm();
-                        SN.U.NoticeClearAttachments(form);
-                        SN.C.I.UploadCounter = 0;
-                        SN.U.NoticeNewAttachment($('fieldset', form));
+                        form.find('#'+SN.C.S.NoticeInReplyTo).val('');
+                        form.find('#'+SN.C.S.NoticeDataAttachSelected).remove();
                         SN.U.FormNoticeEnhancements(form);
                     }
                 },
@@ -313,11 +312,6 @@ var SN = { // StatusNet
                 }
             });
         },
-        
-        NoticeClearAttachments: function(form) {
-            $('input:file', form).remove();
-            $('div[class=' + SN.C.S.Success + ']', form).remove();
-        },
 
         NoticeReply: function() {
             if ($('#'+SN.C.S.NoticeDataText).length > 0 && $('#content .notice_reply').length > 0) {
@@ -477,31 +471,25 @@ var SN = { // StatusNet
             }
         },
 
-        NoticeDataAttach: function(NDANum) {
-            NDA = $('#'+SN.C.S.NoticeDataAttach+NDANum);
+        NoticeDataAttach: function() {
+            NDA = $('#'+SN.C.S.NoticeDataAttach);
             NDA.change(function() {
-                S = '<div id="'+SN.C.S.NoticeDataAttachSelected+SN.C.I.UploadCounter+'" class="'+SN.C.S.Success+'"><code>'+$(this).val()+'</code> <button class="close">&#215;</button></div>';
-                $('#'+SN.C.S.FormNotice).append(S);
-                
-                $('#'+SN.C.S.NoticeDataAttachSelected+SN.C.I.UploadCounter+' button').click(function(){
-                    $('#'+this.parentNode.getAttribute("id")).remove();
-                    $('#'+this.parentNode.getAttribute("id").replace("_selected", "")).remove();
+                S = '<div id="'+SN.C.S.NoticeDataAttachSelected+'" class="'+SN.C.S.Success+'"><code>'+$(this).val()+'</code> <button class="close">&#215;</button></div>';
+                NDAS = $('#'+SN.C.S.NoticeDataAttachSelected);
+                if (NDAS.length > 0) {
+                    NDAS.replaceWith(S);
+                }
+                else {
+                    $('#'+SN.C.S.FormNotice).append(S);
+                }
+                $('#'+SN.C.S.NoticeDataAttachSelected+' button').click(function(){
+                    $('#'+SN.C.S.NoticeDataAttachSelected).remove();
                     NDA.val('');
 
                     return false;
                 });
-                SN.C.I.UploadCounter++;
-                NDA.attr('style', 'display: none;');
-                SN.U.NoticeNewAttachment(NDA.parent());
-                $('#notice_data-attach-label').attr('for', SN.C.S.NoticeDataAttach+SN.C.I.UploadCounter);
             });
         },
-        
-        NoticeNewAttachment: function(parent) {
-            NEWFILE = '<input id="'+SN.C.S.NoticeDataAttach+SN.C.I.UploadCounter+'" class="attach" type="file" name="attach'+SN.C.I.UploadCounter+'" title="'+NoticeAttachment_text.AttachFile+'"/>';
-            parent.append(NEWFILE);
-            SN.U.NoticeDataAttach(SN.C.I.UploadCounter);            
-        },
 
         NoticeLocationAttach: function() {
             var NLat = $('#'+SN.C.S.NoticeLat).val();
@@ -722,6 +710,38 @@ var SN = { // StatusNet
             Delete: function() {
                 $.cookie(SN.C.S.StatusNetInstance, null);
             }
+        },
+
+        /**
+         * Check if the current page is a timeline where the current user's
+         * posts should be displayed immediately on success.
+         *
+         * @fixme this should be done in a saner way, with machine-readable
+         * info about what page we're looking at.
+         */
+        belongsOnTimeline: function(notice) {
+            var action = $("body").attr('id');
+            if (action == 'public') {
+                return true;
+            }
+
+            var profileLink = $('#nav_profile a').attr('href');
+            if (profileLink) {
+                var authorUrl = $(notice).find('.entry-title .author a.url').attr('href');
+                if (authorUrl == profileLink) {
+                    if (action == 'all' || action == 'showstream') {
+                        // Posts always show on your own friends and profile streams.
+                        return true;
+                    }
+                }
+            }
+
+            // @fixme tag, group, reply timelines should be feasible as well.
+            // Mismatch between id-based and name-based user/group links currently complicates
+            // the lookup, since all our inline mentions contain the absolute links but the
+            // UI links currently on the page use malleable names.
+
+            return false;
         }
     },
 
@@ -735,7 +755,7 @@ var SN = { // StatusNet
                     SN.U.FormNoticeEnhancements($(this));
                 });
 
-                SN.U.NoticeDataAttach("");
+                SN.U.NoticeDataAttach();
             }
         },