topic.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. $(document).ready(function(){
  2. $('.like-reply').click(function() {
  3. var _$this = $(this);
  4. var replyId = _$this.attr('data-id');
  5. var like_url = "/user/like/" + replyId;
  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(result) {
  15. if (result.judge === true)
  16. {
  17. _$this.attr("title","赞");
  18. _$this.removeClass("like-active");
  19. _$this.addClass("like-no-active");
  20. } else
  21. {
  22. window.location.href = result.url;
  23. }
  24. }});
  25. }else {
  26. $.ajax ({
  27. type : "POST",
  28. url : like_url,
  29. data:data,
  30. contentType: 'application/json;charset=UTF-8',
  31. success: function(result) {
  32. if (result.judge === true)
  33. {
  34. _$this.attr("title","取消赞");
  35. _$this.removeClass("like-no-active");
  36. _$this.addClass("like-active");
  37. } else
  38. {
  39. window.location.href = result.url;
  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. });
  50. function DoVote(voteData) {
  51. $(document).ready(function(){
  52. $('#topic-up-vote').click(function() {
  53. var data = JSON.stringify({
  54. });
  55. $.ajax ({
  56. type : "POST",
  57. url : voteData.vote_url,
  58. data:data,
  59. contentType: 'application/json;charset=UTF-8',
  60. success: function(result) {
  61. if (result.judge)
  62. {
  63. $('.votes').html(result.html);
  64. } else
  65. {
  66. window.location.href = result.url;
  67. }
  68. }});
  69. });
  70. $('#topic-down-vote').click(function() {
  71. var data = JSON.stringify({
  72. });
  73. $.ajax ({
  74. type : "DELETE",
  75. url : voteData.vote_url,
  76. data:data,
  77. contentType: 'application/json;charset=UTF-8',
  78. success: function(result) {
  79. if (result.judge)
  80. {
  81. $('.votes').html(result.html);
  82. } else
  83. {
  84. window.location.href = result.url;
  85. }
  86. }});
  87. });
  88. });
  89. }
  90. function PreviewTopic(pre_url) {
  91. $('#topic-preview').click(function() {
  92. var content = $('#content').val();
  93. $.post(pre_url, {
  94. content: $("#content").val(),
  95. choice: $("#choice").val()
  96. }, function(data) {
  97. $("#show-preview").html(data);
  98. });
  99. });
  100. }
  101. function AskTopic(pre_url) {
  102. $(document).ready(function(){
  103. PreviewTopic(pre_url);
  104. $('#tokenfield').tokenfield({
  105. limit:4
  106. });
  107. });
  108. }
  109. function EditTopic(pre_url,edit_url) {
  110. $(document).ready(function(){
  111. PreviewTopic(pre_url);
  112. $('#tokenfield').tokenfield({
  113. limit:4
  114. });
  115. $('#topic-put-btn').click(function() {
  116. var form_data = $("form#topic-put").serializeArray();
  117. var data = {};
  118. $.each(form_data,function() {
  119. data[this.name] = this.value;
  120. });
  121. data = JSON.stringify(data);
  122. $.ajax ({
  123. type : "PUT",
  124. url : edit_url,
  125. data:data,
  126. contentType: 'application/json;charset=UTF-8',
  127. success: function(result) {
  128. if (result.judge === true) {
  129. window.location.href= edit_url;
  130. }else {
  131. alert(result.error);
  132. }
  133. }
  134. });
  135. });
  136. });
  137. }