cirandas.net

ref: master

public/javascripts/comments.js


  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
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
noosfero.comments = {

  load: function() {
    if (noosfero.comments.loaded) return
    noosfero.comments.loaded = true

    var url = window.location.href
    if (window.MessageBus)
      MessageBus.subscribe(url+'/new_comment', function (data) {
        if (data.user_login == noosfero.user_data.login) return
        noosfero.comments.receiveComment(data)
      })

    $('.display-comment-form').unbind()
    $('.display-comment-form').click(function () {
      var $button = $(this)
      noosfero.comments.toggleBox($button.parents('.post_comment_box'))
      $($button).hide()
      $button.closest('.page-comment-form').find('input[type="text"]:visible,textarea').first().focus()
      return false
    });

    $('#cancel-comment').die()
    $('#cancel-comment').live("click", function () {
      var $button = $(this)
      noosfero.comments.toggleBox($button.parents('.post_comment_box'))
      noosfero.comments.showDisplayCommentbutton()
      var page_comment_form = $button.parents('.page-comment-form')
      page_comment_form.find('.errorExplanation').remove()
      return false
    });
  },

  toggleBox: function (div) {
    if (div.hasClass('opened')) {
      div.removeClass('opened')
      div.addClass('closed')
    } else {
      div.removeClass('closed')
      div.addClass('opened')
    }
  },

  button: $(),
  receiveComment: function (data) {
    var self = noosfero.comments
    var form = self.button.parents("form")
    var post_comment_box = self.button.parents('.post_comment_box')
    var page_comment_form = self.button.parents('.page-comment-form')
    var comment_div = $('#comments_list.comments')

    if (data.render_target === null) {
      //Comment for approval
      form.find("input[type='text']").add('textarea').each(function() {
        this.value = ''
      })
      page_comment_form.find('.errorExplanation').remove()
    } else if (data.render_target == 'form') {
      //Comment with errors
      $.scrollTo(page_comment_form)
      page_comment_form.html(data.html)
      $('.display-comment-form').hide()
    } else if ($('#' + data.render_target).size() > 0) {
      //Comment of reply
      $('#'+ data.render_target).replaceWith(data.html)
      $('#' + data.render_target).effect("highlight", {}, 3000)
      noosfero.modal.close()
      noosfero.comments.incrementCommentCount(comment_div)
    } else {
      if ($("#comment_order").val() == 'newest')
        $('.article-comments-list').prepend(data.html)
      else
        $('.article-comments-list').append(data.html)

      form.find("input[type='text']").add('textarea').each(function() {
        this.value = ''
      })

      page_comment_form.find('.errorExplanation').remove()
      noosfero.modal.close()
      noosfero.comments.incrementCommentCount(comment_div)
    }

    if ($('#recaptcha_response_field').val()) {
      Recaptcha.reload()
    }

    if (data.msg)
      display_notice(data.msg)

    if (self.button.length) {
      close_loading()
      noosfero.comments.toggleBox(self.button.closest('.post_comment_box'))
      noosfero.comments.showDisplayCommentbutton()
      self.button.removeClass('comment-button-loading')
      self.button.enable()
      self.button = $();
    }
  },

  save: function (button) {
    button = $(button);
    this.button = button
    button.addClass('comment-button-loading');
    open_loading(DEFAULT_LOADING_MESSAGE);

    var form = button.parents("form")
    $.post(form.attr("action"), form.serialize(), function(data) {
      noosfero.comments.receiveComment(data)
    }, 'json')
  },

  incrementCommentCount: function (comment_div) {
    comment_div.find('.comment-count').add('#article-header .comment-count').each(function() {
      var count = parseInt($(this).html());
      update_comment_count($(this), count + 1);
    });
  },

  showDisplayCommentbutton: function () {
    if($('.post_comment_box.opened').length==0)
      $('.display-comment-form').show();
  },
}

noosfero.comments.order = {

  load: function () {
    if (noosfero.comments.order.loaded) return
      noosfero.comments.order.loaded = true

    $("#comment_order").change(function () {
      var url = $("#page_url").val()
      noosfero.comments.order.send(this.value, url)
    })
  },

  send: function (order, url) {
    var container = $('#comments_list')
    container.addClass('fetching')
    $.ajax({url: url, data: {comment_order: order},
      success: function (response) {
        container.replaceWith(response)
      },
      complete: function () { container.removeClass('fetching') }
    })
  },
}

noosfero.comments.pagination = {
  load: function () {
    pagination.infiniteScroll(DEFAULT_LOADING_MESSAGE, {
      load: function (url) {
        $.get(url, function (data) {
          data = $(data)
          var newPagination = data.find('.pagination')
          var newComments = data.find('.article-comments-list .article-comment')
          pagination.showMore(newPagination, function () {
            $('.article-comments-list .article-comment:last').after(newComments);
          })

          pagination.loading = false
        })
      },
    });
  }
}

noosfero.comments.load()