aboutsummaryrefslogtreecommitdiffstats
path: root/mediagoblin/static/js/post_comment.js
blob: 431c222fab04eaaae1b3931f47ea594588d1cfe5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/**
 * GNU MediaGoblin -- federated, autonomous media hosting
 * Copyright (C) 2011, 2012 MediaGoblin contributors.  See AUTHORS.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

$(document).ready(function(){
    $(function() {
        // Hide this button if script is enabled
        $('.form_submit_buttons').find('input').hide();

        // Include this link if script is enabled
        $('.form_submit_buttons').append(
            '<a class="button_action" id="post_comment" type="button">' +
            'Add this comment </a>');

        $('#post_comment').click(function() {
            $.ajax({
                url: $('#postCommentURL').val(),
                data: $('#form_comment').serialize(),
                type: 'POST',
                success: function(response) {
                    var message = $(response).find('.mediagoblin_messages');
                    var commentsInResponse = $($(response).find('.media_comments')).find('li');
                    var commentsInPage = $('.media_comments').find('ul');
                    
                    // Post the message
                    message.css({"position":"fixed", "top":"50px", "width":"100%"});
                    $('body').append(message);
                    message.delay(1500).fadeOut();

                    // Checking if there is new comment
                    if(commentsInResponse.length != $(commentsInPage).find('li').length) {
                        // Post comment and scroll down to it
                        var newComment = commentsInResponse[commentsInResponse.length - 1];
                        $('#form_comment').fadeOut('fast');
                        $('#button_addcomment').fadeIn('fast');
                        $('#comment_preview').replaceWith("<div id=comment_preview></div>");
                        $(commentsInPage).append(newComment);
                        $('html, body').animate({
                            scrollTop: $(newComment).offset().top
                        }, 1000);
                    }
                },
                error: function(error) {
                    console.log(error);
                }
            });
        });
    });
});