topic.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. $(document).ready(function(){
  2. $('.like-reply').click(function() {
  3. var _$this = $(this);
  4. var pk = _$this.attr('data-id');
  5. var like_url = "/replies/" + pk + '/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 contentType = $('#content_type').val();
  51. if (contentType == "1") {
  52. $("#show-preview").html(marked($('#content').val()));
  53. } else if (contentType == "2") {
  54. var parser = new Org.Parser();
  55. var orgDocument = parser.parse($('#content').val());
  56. var orgHTMLDocument = orgDocument.convert(Org.ConverterHTML, {
  57. headerOffset: 1,
  58. exportFromLineNumber: false,
  59. suppressSubScriptHandling: false,
  60. suppressAutoLink: false
  61. });
  62. $("#show-preview").html(orgHTMLDocument.toString());
  63. } else {
  64. $("#show-preview").html($('#content').val());
  65. }
  66. });
  67. $('#tokenfield').tokenfield({
  68. limit:4
  69. });
  70. $('#topic-put-btn').click(function() {
  71. var _$this = $(this);
  72. var url = '/topic/' + _$this.attr("data-id");
  73. var data = {
  74. csrf_token:$('input[name="csrf_token"]').val(),
  75. title:$('input[name="title"]').val(),
  76. tags:$('input[name="tags"]').val(),
  77. category:$('select[name="category"]').val(),
  78. content:$('textarea[name="content"]').val(),
  79. content_type:$('select[name="content_type"]').val()
  80. };
  81. $.ajax ({
  82. type : "PUT",
  83. url : url,
  84. data:JSON.stringify(data),
  85. contentType: 'application/json;charset=UTF-8',
  86. success: function(response) {
  87. if (response.status === '200') {
  88. window.location.href= url;
  89. }else {
  90. if (response.description != ""){
  91. alert(response.description);
  92. }else {
  93. alert(response.message);
  94. }
  95. }
  96. }
  97. });
  98. });
  99. });