You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

230 lines
6.6 KiB

3 years ago
  1. $(function () {
  2. var default_error_message = 'Server error, please try again later.';
  3. $.ajaxSetup({
  4. beforeSend: function (xhr, settings) {
  5. if (!/^(GET|HEAD|OPTIONS|TRACE)$/i.test(settings.type) && !this.crossDomain) {
  6. xhr.setRequestHeader('X-CSRFToken', csrf_token);
  7. }
  8. }
  9. });
  10. $(document).ajaxError(function (event, request, settings) {
  11. var message = null;
  12. if (request.responseJSON && request.responseJSON.hasOwnProperty('message')) {
  13. message = request.responseJSON.message;
  14. } else if (request.responseText) {
  15. var IS_JSON = true;
  16. try {
  17. var data = JSON.parse(request.responseText);
  18. }
  19. catch (err) {
  20. IS_JSON = false;
  21. }
  22. if (IS_JSON && data !== undefined && data.hasOwnProperty('message')) {
  23. message = JSON.parse(request.responseText).message;
  24. } else {
  25. message = default_error_message;
  26. }
  27. } else {
  28. message = default_error_message;
  29. }
  30. toast(message, 'error');
  31. });
  32. var flash = null;
  33. function toast(body, category) {
  34. clearTimeout(flash);
  35. var $toast = $('#toast');
  36. if (category === 'error') {
  37. $toast.css('background-color', 'red')
  38. } else {
  39. $toast.css('background-color', '#333')
  40. }
  41. $toast.text(body).fadeIn();
  42. flash = setTimeout(function () {
  43. $toast.fadeOut();
  44. }, 3000);
  45. }
  46. var hover_timer = null;
  47. function show_profile_popover(e) {
  48. var $el = $(e.target);
  49. hover_timer = setTimeout(function () {
  50. hover_timer = null;
  51. $.ajax({
  52. type: 'GET',
  53. url: $el.data('href'),
  54. success: function (data) {
  55. $el.popover({
  56. html: true,
  57. content: data,
  58. trigger: 'manual',
  59. animation: false
  60. });
  61. $el.popover('show');
  62. $('.popover').on('mouseleave', function () {
  63. setTimeout(function () {
  64. $el.popover('hide');
  65. }, 200);
  66. });
  67. }
  68. });
  69. }, 500);
  70. }
  71. function hide_profile_popover(e) {
  72. var $el = $(e.target);
  73. if (hover_timer) {
  74. clearTimeout(hover_timer);
  75. hover_timer = null;
  76. } else {
  77. setTimeout(function () {
  78. if (!$('.popover:hover').length) {
  79. $el.popover('hide');
  80. }
  81. }, 200);
  82. }
  83. }
  84. function update_followers_count(id) {
  85. var $el = $('#followers-count-' + id);
  86. $.ajax({
  87. type: 'GET',
  88. url: $el.data('href'),
  89. success: function (data) {
  90. $el.text(data.count);
  91. }
  92. });
  93. }
  94. function update_collectors_count(id) {
  95. $.ajax({
  96. type: 'GET',
  97. url: $('#collectors-count-' + id).data('href'),
  98. success: function (data) {
  99. console.log(data);
  100. $('#collectors-count-' + id).text(data.count);
  101. }
  102. });
  103. }
  104. function update_notifications_count() {
  105. var $el = $('#notification-badge');
  106. $.ajax({
  107. type: 'GET',
  108. url: $el.data('href'),
  109. success: function (data) {
  110. if (data.count === 0) {
  111. $('#notification-badge').hide();
  112. } else {
  113. $el.show();
  114. $el.text(data.count)
  115. }
  116. }
  117. });
  118. }
  119. function follow(e) {
  120. var $el = $(e.target);
  121. var id = $el.data('id');
  122. $.ajax({
  123. type: 'POST',
  124. url: $el.data('href'),
  125. success: function (data) {
  126. $el.prev().show();
  127. $el.hide();
  128. update_followers_count(id);
  129. toast(data.message);
  130. }
  131. });
  132. }
  133. function unfollow(e) {
  134. var $el = $(e.target);
  135. var id = $el.data('id');
  136. $.ajax({
  137. type: 'POST',
  138. url: $el.data('href'),
  139. success: function (data) {
  140. $el.next().show();
  141. $el.hide();
  142. update_followers_count(id);
  143. toast(data.message);
  144. }
  145. });
  146. }
  147. function collect(e) {
  148. var $el = $(e.target).data('href') ? $(e.target) : $(e.target).parent('.collect-btn');
  149. var id = $el.data('id');
  150. $.ajax({
  151. type: 'POST',
  152. url: $el.data('href'),
  153. success: function (data) {
  154. $el.prev().show();
  155. $el.hide();
  156. update_collectors_count(id);
  157. toast(data.message);
  158. }
  159. });
  160. }
  161. function uncollect(e) {
  162. var $el = $(e.target).data('href') ? $(e.target) : $(e.target).parent('.uncollect-btn');
  163. var id = $el.data('id');
  164. $.ajax({
  165. type: 'POST',
  166. url: $el.data('href'),
  167. success: function (data) {
  168. $el.next().show();
  169. $el.hide();
  170. update_collectors_count(id);
  171. toast(data.message);
  172. }
  173. });
  174. }
  175. $('.profile-popover').hover(show_profile_popover.bind(this), hide_profile_popover.bind(this));
  176. $(document).on('click', '.follow-btn', follow.bind(this));
  177. $(document).on('click', '.unfollow-btn', unfollow.bind(this));
  178. $(document).on('click', '.collect-btn', collect.bind(this));
  179. $(document).on('click', '.uncollect-btn', uncollect.bind(this));
  180. // hide or show tag edit form
  181. $('#tag-btn').click(function () {
  182. $('#tags').hide();
  183. $('#tag-form').show();
  184. });
  185. $('#cancel-tag').click(function () {
  186. $('#tag-form').hide();
  187. $('#tags').show();
  188. });
  189. // hide or show description edit form
  190. $('#description-btn').click(function () {
  191. $('#description').hide();
  192. $('#description-form').show();
  193. });
  194. $('#cancel-description').click(function () {
  195. $('#description-form').hide();
  196. $('#description').show();
  197. });
  198. // delete confirm modal
  199. $('#confirm-delete').on('show.bs.modal', function (e) {
  200. $('.delete-form').attr('action', $(e.relatedTarget).data('href'));
  201. });
  202. if (is_authenticated) {
  203. setInterval(update_notifications_count, 30000);
  204. }
  205. $("[data-toggle='tooltip']").tooltip({title: moment($(this).data('timestamp')).format('lll')})
  206. });