Blame view

frontend/web/js/comments.js 1.97 KB
8a34dff0   Aleksandr   + коментарии (fro...
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
  $(document).ready(function(){
  
      applyScript();
  
      /*var pubnub = PUBNUB({
          subscribe_key: 'demo',
          publish_key: 'demo'
      });
  
      pubnub.subscribe({
          channel: 'comment',
          message: function(m){console.log(m)},
          error: function (error) {
            // Handle error here
            console.log(JSON.stringify(error));
          }
      }); */
  });
  
  function applyScript()
  {
  
      var parent_id = 0;
  
      var textarea = $(".new_comment").find("textarea");
      var replyName;
  
      $(".comment_reply").each(function()
      {
          $(this).hide();
      });
  
      $(".btn-reply").each(function()
      {
          $(this).on("click", function()
          {
              replyName = $(this).siblings().find(".comment_username").text();
              parent_id = $(this).parents(".comment_body").attr("comment_id");
  
              textarea.val(replyName + ', ');
  
          });
  
      });
  
      $(".btn-send").on('click', function()
      {
          var content = textarea.val();
  
          if (content.indexOf(replyName) > -1 == false)
          {
              parent_id = 0;
          }
          createComment(content, parent_id);
      });
  
      function reload_comments()
      {
          var path = window.location.protocol + "//" + window.location.host + "/";
          $.ajax({
              type: 'GET',
              url : path + 'comment',
              data : {
                  'action' : 'reload'
              },
              success: function(res)
              {
                  $("#comment_wrapper").html(res);
                  console.log('reloaded');
  
                  applyScript();
              }
          });
      }
      function createComment(content, parent_id)
      {
          $.ajax({
              type: 'POST',
              url : '/comment/create',
              data: {
                  'content' : content,
                  'parent_id' : parent_id
              },
              success: function(res)
              {
                  //reload_comments();
                  alert('Comment sent');
              }
          });
      }
  
  }