Просмотр файла public/assets/js/main.js

Размер файла: 8.35Kb
  1. $(function () {
  2. prettyPrint();
  3.  
  4. toastr.options = {
  5. 'toastClass' : 'toastr',
  6. 'progressBar': true,
  7. 'positionClass': 'toast-top-full-width'
  8. };
  9.  
  10. $('.markItUp').markItUp(mySettings).on('input', function () {
  11. var maxlength = $(this).attr('maxlength');
  12. var text = $(this).val().replace(/(\r\n|\n|\r)/g, "\r\n");
  13.  
  14. var currentLength = text.length;
  15. var counter = $('.js-textarea-counter');
  16.  
  17. if (currentLength > maxlength) {
  18. counter.addClass('text-danger');
  19. } else {
  20. counter.removeClass('text-danger');
  21. }
  22.  
  23. counter.text('Осталось символов: ' + (maxlength - currentLength));
  24.  
  25. if (currentLength === 0) {
  26. counter.empty();
  27. }
  28. });
  29.  
  30. $('[data-bs-toggle="tooltip"]').tooltip();
  31. $('[data-bs-toggle="popover"]').popover();
  32.  
  33. // Hide popover poppers anywhere
  34. $('body').on('click', function (e) {
  35. //did not click a popover toggle or popover
  36. if ($(e.target).data('bs-toggle') !== 'popover'
  37. && $(e.target).parents('.popover.in').length === 0) {
  38. $('[data-bs-toggle="popover"]').popover('hide');
  39. }
  40. });
  41.  
  42. // Spoiler
  43. $('.spoiler-title').on('click', function () {
  44. var spoiler = $(this).parent();
  45. spoiler.toggleClass('spoiler-open');
  46. spoiler.find('.spoiler-text:first').slideToggle();
  47. });
  48.  
  49. // Fix invalid markitup
  50. $('.markItUp .is-invalid')
  51. .closest('.markItUpBlock').parent()
  52. .find('.invalid-feedback').show();
  53. });
  54.  
  55. openSearch = function () {
  56. $('.js-form-search').toggle();
  57.  
  58. return false;
  59. };
  60.  
  61. /* Переход к форме ввода */
  62. postJump = function () {
  63. $('html, body').animate({
  64. scrollTop: ($('.post-form').offset().top)
  65. }, 100);
  66. };
  67.  
  68. /* Ответ на сообщение */
  69. postReply = function (el) {
  70. postJump();
  71.  
  72. var field = $('.markItUpEditor');
  73. var post = $(el).closest('.post');
  74. var author = post.find('.post-author').data('login');
  75.  
  76. var $lastSymbol = field.val().slice(field.val().length - 1);
  77. var separ = $.inArray($lastSymbol, ['', '\n']) !== -1 ? '' : '\n';
  78.  
  79. field.focus().val(field.val() + separ + author + ', ');
  80.  
  81. return false;
  82. };
  83.  
  84. /* Цитирование сообщения */
  85. postQuote = function (el) {
  86. postJump();
  87.  
  88. var field = $('.markItUpEditor');
  89. var post = $(el).closest('.post');
  90. var author = post.find('.post-author').data('login');
  91. var date = post.find('.post-date').text();
  92. var text = post.find('.post-message').clone();
  93. var message = $.trim(text.find('blockquote').remove().end().text());
  94.  
  95. var $lastSymbol = field.val().slice(field.val().length - 1);
  96. var separ = $.inArray($lastSymbol, ['', '\n']) !== -1 ? '' : '\n';
  97.  
  98. if (!message) {
  99. field.focus().val(field.val() + separ + author + ', ');
  100.  
  101. return false;
  102. }
  103.  
  104. field.focus().val(field.val() + separ + '[quote=' + author + ' ' + date + ']' + message + '[/quote]\n');
  105.  
  106. return false;
  107. };
  108.  
  109. /* Отправляет скрытую форму */
  110. submitForm = function (el) {
  111. if(! confirm($(el).data('confirm') ?? 'Вы подтверждаете действие?')) {
  112. return false;
  113. }
  114.  
  115. var form = $('<form action="' + $(el).attr('href') + '" method="POST"></form>')
  116. form.append('<input type="hidden" name="csrf" value="' + $(el).data('csrf') + '">');
  117.  
  118. if ($(el).data('method')) {
  119. form.append('<input type="hidden" name="_METHOD" value="' + $(el).data('method').toUpperCase() + '">');
  120. }
  121.  
  122. form.appendTo('body').submit();
  123.  
  124. return false;
  125. };
  126.  
  127. /* Вставка изображения в форму */
  128. pasteImage = function (el) {
  129. var field = $('.markItUpEditor');
  130. var paste = '[img]' + $(el).find('img').attr('src') + '[/img]';
  131.  
  132. field.focus().caret(paste);
  133. };
  134.  
  135. /* Удаление изображения из формы */
  136. cutImage = function (path) {
  137. var field = $('.markItUpEditor');
  138. var text = field.val();
  139. var cut = '[img]' + path + '[/img]';
  140.  
  141. field.val(text.replace(cut, ''));
  142. };
  143.  
  144. /* Загрузка файла */
  145. submitFile = function (el) {
  146. var form = new FormData();
  147. form.append('file', el.files[0]);
  148. form.append('id', $(el).data('id'));
  149. form.append('type', $(el).data('type'));
  150. form.append('csrf', $(el).data('csrf'));
  151.  
  152. $.ajax({
  153. data: form,
  154. type: 'post',
  155. contentType: false,
  156. processData: false,
  157. dataType: 'json',
  158. url: '/upload',
  159. beforeSend: function () {
  160. $('.js-files').append('<i class="fas fa-spinner fa-spin fa-3x mx-3"></i>');
  161. },
  162. complete: function () {
  163. $('.fa-spinner').remove();
  164. },
  165. success: function (data) {
  166. if (! data.success) {
  167. toastr.error(data.message);
  168. return false;
  169. }
  170.  
  171. if (data.success) {
  172. if (data.type === 'image') {
  173. var template = $('.js-image-template').clone();
  174.  
  175. template.find('img').attr({
  176. 'src': data.path
  177. });
  178.  
  179. pasteImage(template);
  180. } else {
  181. var template = $('.js-file-template').clone();
  182.  
  183. template.find('.js-file-link').attr({
  184. 'href': data.path
  185. }).text(data.name);
  186.  
  187. template.find('.js-file-size').text(data.size);
  188. }
  189.  
  190. template.find('.js-file-delete').attr('data-id', data.id);
  191. $('.js-files').append(template.html());
  192. }
  193. }
  194. });
  195.  
  196. el.value = ''
  197.  
  198. return false;
  199. };
  200.  
  201. /* Удаление файла */
  202. deleteFile = function (el) {
  203. var form = new FormData();
  204. form.append('type', $(el).data('type'));
  205. form.append('csrf', $(el).data('csrf'));
  206. form.append('_METHOD', 'DELETE');
  207.  
  208. $.ajax({
  209. data: form,
  210. type: 'post',
  211. contentType: false,
  212. processData: false,
  213. dataType: 'json',
  214. url: '/upload/' + $(el).data('id'),
  215. success: function (data) {
  216. if (! data.success) {
  217. toastr.error(data.message);
  218. return false;
  219. }
  220.  
  221. if (data.success) {
  222. cutImage(data.path);
  223. $(el).closest('.js-file').hide('fast');
  224. }
  225. }
  226. });
  227.  
  228. return false;
  229. };
  230.  
  231. /* Изменение рейтинга */
  232. changeRating = function (el) {
  233. $.ajax({
  234. data: {
  235. type: $(el).data('type'),
  236. vote: $(el).data('vote'),
  237. csrf: $(el).data('csrf')
  238. },
  239. dataType: 'json',
  240. type: 'post',
  241. url: '/rating/' + $(el).data('id'),
  242. success: function (data) {
  243. if (data.success) {
  244. const rating = $(el).closest('.js-rating').find('b');
  245.  
  246. $(el).closest('.js-rating').find('a').removeClass('active');
  247.  
  248. if (! data.cancel) {
  249. $(el).addClass('active');
  250. }
  251.  
  252. rating.html($(data.rating));
  253. } else {
  254. if (data.message) {
  255. toastr.error(data.message);
  256. }
  257. return false;
  258. }
  259. }
  260. });
  261.  
  262. return false;
  263. };
  264.  
  265. /* Добавление / удаление из избранного */
  266. addFavorite = function (el) {
  267. $.ajax({
  268. data: {
  269. csrf: $(el).data('csrf')
  270. },
  271. dataType: 'json',
  272. type: 'post',
  273. url: '/favorites/' + $(el).data('id'),
  274. success: function (data) {
  275.  
  276. if (! data.success) {
  277. toastr.error(data.message);
  278. return false;
  279. }
  280.  
  281. if (data.success) {
  282. if (data.type === 'add') {
  283. toastr.success(data.message);
  284. const icon = '<i class="bi bi-heart-fill"></i>';
  285. const countFavorites = parseInt($(el).text()) + 1;
  286. $(el).html(icon + ' ' + countFavorites);
  287. }
  288.  
  289. if (data.type === 'delete') {
  290. toastr.success(data.message);
  291. const icon = '<i class="bi bi-heart"></i>';
  292. const countFavorites = parseInt($(el).text()) - 1;
  293. $(el).html(icon + ' ' + countFavorites);
  294. }
  295. }
  296. }
  297. });
  298.  
  299. return false;
  300. };