misago-posting.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. // Controller for posting actions
  2. $(function() {
  3. MisagoPreview = function(_controller, options) {
  4. this.$form = options.form;
  5. this.$area = options.$area;
  6. this.$frame = this.$area.find('.frame');
  7. this.$markup = this.$area.find('.misago-markup');
  8. this.$message = this.$area.find('.empty-message');
  9. if (this.$markup.text() == '') {
  10. this.$markup.hide();
  11. }
  12. this.api_url = options.api_url;
  13. this.active = true;
  14. this.previewed_data = this.$form.serialize() + '&preview=1';
  15. this.frequency = 1500;
  16. this.height = this.$markup.height();
  17. var _this = this;
  18. this.last_key_press = (new Date().getTime() / 1000);
  19. this.$form.find('.misago-editor textarea').keyup(function() {
  20. _this.last_key_press = (new Date().getTime() / 1000);
  21. })
  22. this.$frame.height(this.$form.find('.misago-editor').innerHeight() - this.$area.find('.preview-footer').outerHeight());
  23. this.update = function() {
  24. var form_data = _this.$form.serialize() + '&preview=1';
  25. var last_key = (new Date().getTime() / 1000) - _this.last_key_press;
  26. if (_this.previewed_data != form_data && last_key > 2) {
  27. $.post(_this.api_url, form_data, function(data) {
  28. var scroll = _this.$markup.height() - _this.$frame.scrollTop();
  29. if (data.preview) {
  30. if (_this.$message.is(":visible")) {
  31. _this.$message.fadeOut(function() {
  32. _this.$markup.html(data.preview);
  33. Misago.Onebox.activate(_this.$markup);
  34. _this.$markup.fadeIn();
  35. });
  36. } else {
  37. _this.$markup.html(data.preview);
  38. Misago.Onebox.activate(_this.$markup);
  39. }
  40. if (_this.$markup.height() > _this.height) {
  41. _this.$frame.scrollTop(_this.$markup.height() - scroll);
  42. }
  43. } else {
  44. _this.$markup.fadeOut(function() {
  45. _this.$markup.html("");
  46. _this.$message.fadeIn();
  47. _this.$frame.scrollTop(0);
  48. });
  49. }
  50. Misago.DOM.changed();
  51. _this.previewed_data = form_data;
  52. // set timeout
  53. if (_this.active) {
  54. window.setTimeout(function() {
  55. _this.update();
  56. }, _this.frequency);
  57. }
  58. });
  59. } else if (_this.active) {
  60. window.setTimeout(function() {
  61. _this.update();
  62. }, _this.frequency);
  63. }
  64. }
  65. this.stop = function() {
  66. this.active = false;
  67. }
  68. }
  69. MisagoPosting = function() {
  70. this._clear = function() {
  71. this.$spacer = null;
  72. this.$container = null;
  73. this.$form = null;
  74. this.$ajax_loader = null;
  75. this.$ajax_complete = null;
  76. this.$preview = null;
  77. this.submitted = false;
  78. this.posted = false;
  79. this.affix_end = 0;
  80. this.on_cancel = null;
  81. }
  82. this._clear();
  83. var _this = this;
  84. this.init = function(options) {
  85. if (this.$form !== null) {
  86. return false;
  87. }
  88. this.$form = $('#posting-form');
  89. this.$container = this.$form.parent();
  90. this.$spacer = this.$container.parent();
  91. if (options.on_cancel !== undefined) {
  92. this.on_cancel = options.on_cancel
  93. } else {
  94. this.on_cancel = null;
  95. }
  96. this.$ajax_loader = this.$container.find('.ajax-loader');
  97. this.$ajax_complete = this.$container.find('.ajax-complete');
  98. this.$preview = new MisagoPreview(this, {$area: this.$form.find('.editor-preview'), form: this.$form, api_url: options.api_url});
  99. this.$preview.update();
  100. // target height is 26 px too big
  101. this.$spacer.height(this.$container.outerHeight() - ($(document).height() - this.$spacer.offset().top));
  102. this.$container.addClass('fixed');
  103. this.$container.find('button[name="submit"]').click(function() {
  104. if (!_this.submitted && !_this.posted) {
  105. _this.submitted = true; // lock submit process until after response
  106. _this.$ajax_loader.addClass('in');
  107. var form_data = _this.$form.serialize() + '&submit=1';
  108. $.post(options.api_url, form_data, function(data) {
  109. _this.$ajax_loader.removeClass('in');
  110. if (data.post_url !== undefined) {
  111. _this.posted = true;
  112. _this.$ajax_loader.hide();
  113. _this.$ajax_complete.addClass('in')
  114. if (data.post_url.indexOf(window.location.pathname) != -1) {
  115. window.location.href = data.post_url;
  116. window.location.reload(true)
  117. } else {
  118. window.location.href = data.post_url;
  119. }
  120. } else if (data.errors !== undefined) {
  121. Misago.Alerts.error(data.errors[0]);
  122. } else if (data.interrupt !== undefined) {
  123. Misago.Alerts.info(data.interrupt);
  124. } else {
  125. Misago.Alerts.error();
  126. }
  127. _this.submitted = false;
  128. });
  129. }
  130. return false;
  131. })
  132. this.$container.find('button[name="cancel"]').click(function() {
  133. if (_this.has_content()) {
  134. var decision = confirm(lang_dismiss_editor);
  135. if (decision) {
  136. _this.cancel();
  137. }
  138. } else {
  139. _this.cancel();
  140. }
  141. return false;
  142. });
  143. return true;
  144. }
  145. this.load = function(options) {
  146. if (this.$form !== null) {
  147. return false;
  148. }
  149. $.get(options.api_url, function(data) {
  150. $('#reply-form-placeholder').html(data);
  151. Misago.DOM.changed();
  152. _this.init(options);
  153. Misago.DOM.changed();
  154. if (options.on_load !== undefined) {
  155. options.on_load();
  156. }
  157. });
  158. }
  159. this.cancel = function() {
  160. if (this.$form !== null) {
  161. if (this.$preview !== null) {
  162. this.$preview.stop();
  163. }
  164. this.$spacer.fadeOut(function() {
  165. $(this).remove();
  166. $('.main-footer').show();
  167. });
  168. if (this.on_cancel !== undefined) {
  169. this.on_cancel();
  170. }
  171. this._clear();
  172. }
  173. }
  174. this.has_content = function() {
  175. if (_this.$form !== null) {
  176. var length = $.trim(_this.$form.find('input[name="title"]').val()).length;
  177. length += $.trim(_this.$form.find('textarea').val()).length;
  178. return length > 0;
  179. } else {
  180. return false;
  181. }
  182. }
  183. }
  184. Misago.Posting = new MisagoPosting();
  185. $(window).on("beforeunload", function() {
  186. if (Misago.Posting.has_content() && !Misago.Posting.posted) {
  187. return lang_dismiss_editor;
  188. }
  189. })
  190. });