Ticket #868: issue_868_rev.patch

File issue_868_rev.patch, 4.9 KB (added by Vijeth, 7 years ago)

Revised patch for graceful degradation when JS is disabled

  • new file mediagoblin/static/js/post_comment.js

    From 5143cf04cbc5b842d678fb8f494e2ddc948246c3 Mon Sep 17 00:00:00 2001
    From: vijeth-aradhya <vijthaaa@gmail.com>
    Date: Sun, 22 Jan 2017 23:57:16 +0530
    Subject: [PATCH] post_comment.js, media.html: Add AJAX for posting comments
    
    Stop reloading the page when a comment is posted which helps
    in not stopping the media being played (for example, a song)
    
    Fixes https://issues.mediagoblin.org/ticket/868
    ---
     mediagoblin/static/js/post_comment.js              | 63 ++++++++++++++++++++++
     .../templates/mediagoblin/user_pages/media.html    |  3 ++
     2 files changed, 66 insertions(+)
     create mode 100644 mediagoblin/static/js/post_comment.js
    
    diff --git a/mediagoblin/static/js/post_comment.js b/mediagoblin/static/js/post_comment.js
    new file mode 100644
    index 0000000..431c222
    - +  
     1/**
     2 * GNU MediaGoblin -- federated, autonomous media hosting
     3 * Copyright (C) 2011, 2012 MediaGoblin contributors.  See AUTHORS.
     4 *
     5 * This program is free software: you can redistribute it and/or modify
     6 * it under the terms of the GNU Affero General Public License as published by
     7 * the Free Software Foundation, either version 3 of the License, or
     8 * (at your option) any later version.
     9 *
     10 * This program is distributed in the hope that it will be useful,
     11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
     12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     13 * GNU Affero General Public License for more details.
     14 *
     15 * You should have received a copy of the GNU Affero General Public License
     16 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
     17 */
     18
     19$(document).ready(function(){
     20    $(function() {
     21        // Hide this button if script is enabled
     22        $('.form_submit_buttons').find('input').hide();
     23
     24        // Include this link if script is enabled
     25        $('.form_submit_buttons').append(
     26            '<a class="button_action" id="post_comment" type="button">' +
     27            'Add this comment </a>');
     28
     29        $('#post_comment').click(function() {
     30            $.ajax({
     31                url: $('#postCommentURL').val(),
     32                data: $('#form_comment').serialize(),
     33                type: 'POST',
     34                success: function(response) {
     35                    var message = $(response).find('.mediagoblin_messages');
     36                    var commentsInResponse = $($(response).find('.media_comments')).find('li');
     37                    var commentsInPage = $('.media_comments').find('ul');
     38                   
     39                    // Post the message
     40                    message.css({"position":"fixed", "top":"50px", "width":"100%"});
     41                    $('body').append(message);
     42                    message.delay(1500).fadeOut();
     43
     44                    // Checking if there is new comment
     45                    if(commentsInResponse.length != $(commentsInPage).find('li').length) {
     46                        // Post comment and scroll down to it
     47                        var newComment = commentsInResponse[commentsInResponse.length - 1];
     48                        $('#form_comment').fadeOut('fast');
     49                        $('#button_addcomment').fadeIn('fast');
     50                        $('#comment_preview').replaceWith("<div id=comment_preview></div>");
     51                        $(commentsInPage).append(newComment);
     52                        $('html, body').animate({
     53                            scrollTop: $(newComment).offset().top
     54                        }, 1000);
     55                    }
     56                },
     57                error: function(error) {
     58                    console.log(error);
     59                }
     60            });
     61        });
     62    });
     63});
     64 No newline at end of file
  • mediagoblin/templates/mediagoblin/user_pages/media.html

    diff --git a/mediagoblin/templates/mediagoblin/user_pages/media.html b/mediagoblin/templates/mediagoblin/user_pages/media.html
    index ed3d184..7038510 100644
    a b  
    2929          src="{{ request.staticdirect('/js/comment_show.js') }}"></script>
    3030  <script type="text/javascript"
    3131          src="{{ request.staticdirect('/js/keyboard_navigation.js') }}"></script>
     32  <script type="text/javascript"
     33          src="{{ request.staticdirect('/js/post_comment.js') }}"></script>
    3234
    3335  {% template_hook("location_head") %}
    3436  {% template_hook("media_head") %}
     
    116118            <input type="submit" value="{% trans %}Add this comment{% endtrans %}" class="button_action" />
    117119              {{ csrf_token }}
    118120          </div>
     121          <input type="hidden" value="{{ request.urlgen('mediagoblin.user_pages.media_post_comment', user= media.get_actor.username, media_id=media.id) }}" id="postCommentURL" />
    119122          <input type="hidden" value="{{ request.urlgen('mediagoblin.user_pages.media_preview_comment') }}" id="previewURL" />
    120123          <input type="hidden" value="{% trans %}Comment Preview{% endtrans %}" id="previewText"/>
    121124        </form>