topic.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. $(document).ready(function(){
  2. $('.like-reply').click(function() {
  3. var _$this = $(this);
  4. var replyId = _$this.attr('data-id');
  5. var like_url = "/replies/" + replyId + '/like';
  6. var data = JSON.stringify({
  7. });
  8. if(_$this.hasClass('like-active')){
  9. $.ajax ({
  10. type : "DELETE",
  11. url : like_url,
  12. data:data,
  13. contentType: 'application/json;charset=UTF-8',
  14. success: function(response) {
  15. if (response.status === '200')
  16. {
  17. _$this.attr("title","赞");
  18. _$this.removeClass("like-active");
  19. _$this.addClass("like-no-active");
  20. } else {
  21. window.location.href = response.url;
  22. }
  23. }});
  24. }else {
  25. $.ajax ({
  26. type : "POST",
  27. url : like_url,
  28. data:data,
  29. contentType: 'application/json;charset=UTF-8',
  30. success: function(response) {
  31. if (response.status === '200')
  32. {
  33. _$this.attr("title","取消赞");
  34. _$this.removeClass("like-no-active");
  35. _$this.addClass("like-active");
  36. } else
  37. {
  38. window.location.href = response.url;
  39. }
  40. }});
  41. }
  42. });
  43. $('.reply-author').click(function() {
  44. var _$this = $(this);
  45. var author = _$this.attr('data-id');
  46. $('#content').focus();
  47. $('#content').val('@' + author + ' ');
  48. });
  49. $('#topic-preview').click(function() {
  50. var content = $('#content').val();
  51. $.post('/topic/preview', {
  52. content: $("#content").val(),
  53. content_type: $("#content_type").val()
  54. }, function(data) {
  55. $("#show-preview").html(data);
  56. });
  57. });
  58. $('#tokenfield').tokenfield({
  59. limit:4
  60. });
  61. $('#topic-put-btn').click(function() {
  62. var _$this = $(this);
  63. var url = '/topic/' + _$this.attr("data-id");
  64. var data = {
  65. csrf_token:$('input[name="csrf_token"]').val(),
  66. title:$('input[name="title"]').val(),
  67. tags:$('input[name="tags"]').val(),
  68. category:$('select[name="category"]').val(),
  69. content:$('textarea[name="content"]').val(),
  70. content_type:$('select[name="content_type"]').val()
  71. };
  72. $.ajax ({
  73. type : "PUT",
  74. url : url,
  75. data:JSON.stringify(data),
  76. contentType: 'application/json;charset=UTF-8',
  77. success: function(response) {
  78. if (response.status === '200') {
  79. window.location.href= url;
  80. }else {
  81. if (response.description != ""){
  82. alert(response.description);
  83. }else {
  84. alert(response.message);
  85. }
  86. }
  87. }
  88. });
  89. });
  90. });