The version of vichan running on lainchan.org
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

141 lignes
4.8KB

  1. /*
  2. * ajax.js
  3. * https://github.com/savetheinternet/Tinyboard/blob/master/js/ajax.js
  4. *
  5. * Released under the MIT license
  6. * Copyright (c) 2013 Michael Save <savetheinternet@tinyboard.org>
  7. * Copyright (c) 2013-2014 Marcin Łabanowski <marcin@6irc.net>
  8. *
  9. * Usage:
  10. * $config['additional_javascript'][] = 'js/jquery.min.js';
  11. * $config['additional_javascript'][] = 'js/ajax.js';
  12. *
  13. */
  14. $(window).ready(function() {
  15. var settings = new script_settings('ajax');
  16. var do_not_ajax = false;
  17. // Enable submit button if disabled (cache problem)
  18. $('input[type="submit"]').removeAttr('disabled');
  19. var setup_form = function($form) {
  20. $form.submit(function() {
  21. if (do_not_ajax)
  22. return true;
  23. var form = this;
  24. var submit_txt = $(this).find('input[type="submit"]').val();
  25. if (window.FormData === undefined)
  26. return true;
  27. var formData = new FormData(this);
  28. formData.append('json_response', '1');
  29. formData.append('post', submit_txt);
  30. $(document).trigger("ajax_before_post", formData);
  31. var updateProgress = function(e) {
  32. var percentage;
  33. if (e.position === undefined) { // Firefox
  34. percentage = Math.round(e.loaded * 100 / e.total);
  35. }
  36. else { // Chrome?
  37. percentage = Math.round(e.position * 100 / e.total);
  38. }
  39. $(form).find('input[type="submit"]').val(_('Posting... (#%)').replace('#', percentage));
  40. };
  41. $.ajax({
  42. url: this.action,
  43. type: 'POST',
  44. xhr: function() {
  45. var xhr = $.ajaxSettings.xhr();
  46. if(xhr.upload) {
  47. xhr.upload.addEventListener('progress', updateProgress, false);
  48. }
  49. return xhr;
  50. },
  51. success: function(post_response) {
  52. if (post_response.error) {
  53. if (post_response.banned) {
  54. // You are banned. Must post the form normally so the user can see the ban message.
  55. do_not_ajax = true;
  56. $(form).find('input[type="submit"]').each(function() {
  57. var $replacement = $('<input type="hidden">');
  58. $replacement.attr('name', $(this).attr('name'));
  59. $replacement.val(submit_txt);
  60. $(this)
  61. .after($replacement)
  62. .replaceWith($('<input type="button">').val(submit_txt));
  63. });
  64. $(form).submit();
  65. } else {
  66. alert(post_response.error);
  67. $(form).find('input[type="submit"]').val(submit_txt);
  68. $(form).find('input[type="submit"]').removeAttr('disabled');
  69. }
  70. } else if (post_response.redirect && post_response.id) {
  71. if (!$(form).find('input[name="thread"]').length
  72. || (!settings.get('always_noko_replies', true) && !post_response.noko)) {
  73. document.location = post_response.redirect;
  74. } else {
  75. $.ajax({
  76. url: document.location,
  77. success: function(data) {
  78. $(data).find('div.post.reply').each(function() {
  79. var id = $(this).attr('id');
  80. if($('#' + id).length == 0) {
  81. $(this).insertAfter($('div.post:last').next()).after('<br class="clear">');
  82. $(document).trigger('new_post', this);
  83. // watch.js & auto-reload.js retrigger
  84. setTimeout(function() { $(window).trigger("scroll"); }, 100);
  85. }
  86. });
  87. highlightReply(post_response.id);
  88. window.location.hash = post_response.id;
  89. $(window).scrollTop($(document).height());
  90. $(form).find('input[type="submit"]').val(submit_txt);
  91. $(form).find('input[type="submit"]').removeAttr('disabled');
  92. $(form).find('input[name="subject"],input[name="file_url"],\
  93. textarea[name="body"],input[type="file"]').val('').change();
  94. },
  95. cache: false,
  96. contentType: false,
  97. processData: false
  98. }, 'html');
  99. }
  100. $(form).find('input[type="submit"]').val(_('Posted...'));
  101. $(document).trigger("ajax_after_post", post_response);
  102. } else {
  103. alert(_('An unknown error occured when posting!'));
  104. $(form).find('input[type="submit"]').val(submit_txt);
  105. $(form).find('input[type="submit"]').removeAttr('disabled');
  106. }
  107. },
  108. error: function(xhr, status, er) {
  109. console.log(xhr);
  110. alert(_('The server took too long to submit your post. Your post was probably still submitted. If it wasn\'t, we might be experiencing issues right now -- please try your post again later. Error information: ') + "<div><textarea>" + JSON.stringify(xhr) + "</textarea></div>");
  111. $(form).find('input[type="submit"]').val(submit_txt);
  112. $(form).find('input[type="submit"]').removeAttr('disabled');
  113. },
  114. data: formData,
  115. cache: false,
  116. contentType: false,
  117. processData: false
  118. }, 'json');
  119. $(form).find('input[type="submit"]').val(_('Posting...'));
  120. $(form).find('input[type="submit"]').attr('disabled', true);
  121. return false;
  122. });
  123. };
  124. setup_form($('form[name="post"]'));
  125. $(window).on('quick-reply', function() {
  126. $('form#quick-reply').off('submit');
  127. setup_form($('form#quick-reply'));
  128. });
  129. });