Commit d9c94a8b authored by Alfredo Sumaran's avatar Alfredo Sumaran

Merge branch '25364-fix-preview-multi-request' into 'master'

Resolve "Clicking markdown 'Preview' tab spawns two XHR requests"

Closes #25364

See merge request !8031
parents 04b20bc0 dbf4d80c
/* eslint-disable func-names, space-before-function-paren, no-var, one-var, one-var-declaration-per-line, wrap-iife, no-else-return, consistent-return, object-shorthand, comma-dangle, no-param-reassign, padded-blocks, camelcase, prefer-arrow-callback, max-len */ /* eslint-disable func-names, no-var, object-shorthand, comma-dangle, prefer-arrow-callback */
// MarkdownPreview // MarkdownPreview
// //
// Handles toggling the "Write" and "Preview" tab clicks, rendering the preview, // Handles toggling the "Write" and "Preview" tab clicks, rendering the preview,
// and showing a warning when more than `x` users are referenced. // and showing a warning when more than `x` users are referenced.
// //
(function() { (function () {
var lastTextareaPreviewed, markdownPreview, previewButtonSelector, writeButtonSelector; var lastTextareaPreviewed;
var markdownPreview;
var previewButtonSelector;
var writeButtonSelector;
window.MarkdownPreview = (function() { window.MarkdownPreview = (function () {
function MarkdownPreview() {} function MarkdownPreview() {}
// Minimum number of users referenced before triggering a warning // Minimum number of users referenced before triggering a warning
...@@ -16,73 +19,71 @@ ...@@ -16,73 +19,71 @@
MarkdownPreview.prototype.ajaxCache = {}; MarkdownPreview.prototype.ajaxCache = {};
MarkdownPreview.prototype.showPreview = function(form) { MarkdownPreview.prototype.showPreview = function ($form) {
var mdText, preview; var mdText;
preview = form.find('.js-md-preview'); var preview = $form.find('.js-md-preview');
mdText = form.find('textarea.markdown-area').val(); if (preview.hasClass('md-preview-loading')) {
return;
}
mdText = $form.find('textarea.markdown-area').val();
if (mdText.trim().length === 0) { if (mdText.trim().length === 0) {
preview.text('Nothing to preview.'); preview.text('Nothing to preview.');
return this.hideReferencedUsers(form); this.hideReferencedUsers($form);
} else { } else {
preview.text('Loading...'); preview.addClass('md-preview-loading').text('Loading...');
return this.renderMarkdown(mdText, (function(_this) { this.fetchMarkdownPreview(mdText, (function (response) {
return function(response) { preview.removeClass('md-preview-loading').html(response.body);
preview.html(response.body);
preview.renderGFM(); preview.renderGFM();
return _this.renderReferencedUsers(response.references.users, form); this.renderReferencedUsers(response.references.users, $form);
}; }).bind(this));
})(this));
} }
}; };
MarkdownPreview.prototype.renderMarkdown = function(text, success) { MarkdownPreview.prototype.fetchMarkdownPreview = function (text, success) {
if (!window.preview_markdown_path) { if (!window.preview_markdown_path) {
return; return;
} }
if (text === this.ajaxCache.text) { if (text === this.ajaxCache.text) {
return success(this.ajaxCache.response); success(this.ajaxCache.response);
return;
} }
return $.ajax({ $.ajax({
type: 'POST', type: 'POST',
url: window.preview_markdown_path, url: window.preview_markdown_path,
data: { data: {
text: text text: text
}, },
dataType: 'json', dataType: 'json',
success: (function(_this) { success: (function (response) {
return function(response) { this.ajaxCache = {
_this.ajaxCache = {
text: text, text: text,
response: response response: response
}; };
return success(response); success(response);
}; }).bind(this)
})(this)
}); });
}; };
MarkdownPreview.prototype.hideReferencedUsers = function(form) { MarkdownPreview.prototype.hideReferencedUsers = function ($form) {
var referencedUsers; $form.find('.referenced-users').hide();
referencedUsers = form.find('.referenced-users');
return referencedUsers.hide();
}; };
MarkdownPreview.prototype.renderReferencedUsers = function(users, form) { MarkdownPreview.prototype.renderReferencedUsers = function (users, $form) {
var referencedUsers; var referencedUsers;
referencedUsers = form.find('.referenced-users'); referencedUsers = $form.find('.referenced-users');
if (referencedUsers.length) { if (referencedUsers.length) {
if (users.length >= this.referenceThreshold) { if (users.length >= this.referenceThreshold) {
referencedUsers.show(); referencedUsers.show();
return referencedUsers.find('.js-referenced-users-count').text(users.length); referencedUsers.find('.js-referenced-users-count').text(users.length);
} else { } else {
return referencedUsers.hide(); referencedUsers.hide();
} }
} }
}; };
return MarkdownPreview; return MarkdownPreview;
}());
})();
markdownPreview = new window.MarkdownPreview(); markdownPreview = new window.MarkdownPreview();
...@@ -92,19 +93,14 @@ ...@@ -92,19 +93,14 @@
lastTextareaPreviewed = null; lastTextareaPreviewed = null;
$.fn.setupMarkdownPreview = function() { $.fn.setupMarkdownPreview = function () {
var $form, form_textarea; var $form = $(this);
$form = $(this); $form.find('textarea.markdown-area').on('input', function () {
form_textarea = $form.find('textarea.markdown-area'); markdownPreview.hideReferencedUsers($form);
form_textarea.on('input', function() {
return markdownPreview.hideReferencedUsers($form);
});
return form_textarea.on('blur', function() {
return markdownPreview.showPreview($form);
}); });
}; };
$(document).on('markdown-preview:show', function(e, $form) { $(document).on('markdown-preview:show', function (e, $form) {
if (!$form) { if (!$form) {
return; return;
} }
...@@ -115,10 +111,10 @@ ...@@ -115,10 +111,10 @@
// toggle content // toggle content
$form.find('.md-write-holder').hide(); $form.find('.md-write-holder').hide();
$form.find('.md-preview-holder').show(); $form.find('.md-preview-holder').show();
return markdownPreview.showPreview($form); markdownPreview.showPreview($form);
}); });
$(document).on('markdown-preview:hide', function(e, $form) { $(document).on('markdown-preview:hide', function (e, $form) {
if (!$form) { if (!$form) {
return; return;
} }
...@@ -129,34 +125,33 @@ ...@@ -129,34 +125,33 @@
// toggle content // toggle content
$form.find('.md-write-holder').show(); $form.find('.md-write-holder').show();
$form.find('textarea.markdown-area').focus(); $form.find('textarea.markdown-area').focus();
return $form.find('.md-preview-holder').hide(); $form.find('.md-preview-holder').hide();
}); });
$(document).on('markdown-preview:toggle', function(e, keyboardEvent) { $(document).on('markdown-preview:toggle', function (e, keyboardEvent) {
var $target; var $target;
$target = $(keyboardEvent.target); $target = $(keyboardEvent.target);
if ($target.is('textarea.markdown-area')) { if ($target.is('textarea.markdown-area')) {
$(document).triggerHandler('markdown-preview:show', [$target.closest('form')]); $(document).triggerHandler('markdown-preview:show', [$target.closest('form')]);
return keyboardEvent.preventDefault(); keyboardEvent.preventDefault();
} else if (lastTextareaPreviewed) { } else if (lastTextareaPreviewed) {
$target = lastTextareaPreviewed; $target = lastTextareaPreviewed;
$(document).triggerHandler('markdown-preview:hide', [$target.closest('form')]); $(document).triggerHandler('markdown-preview:hide', [$target.closest('form')]);
return keyboardEvent.preventDefault(); keyboardEvent.preventDefault();
} }
}); });
$(document).on('click', previewButtonSelector, function(e) { $(document).on('click', previewButtonSelector, function (e) {
var $form; var $form;
e.preventDefault(); e.preventDefault();
$form = $(this).closest('form'); $form = $(this).closest('form');
return $(document).triggerHandler('markdown-preview:show', [$form]); $(document).triggerHandler('markdown-preview:show', [$form]);
}); });
$(document).on('click', writeButtonSelector, function(e) { $(document).on('click', writeButtonSelector, function (e) {
var $form; var $form;
e.preventDefault(); e.preventDefault();
$form = $(this).closest('form'); $form = $(this).closest('form');
return $(document).triggerHandler('markdown-preview:hide', [$form]); $(document).triggerHandler('markdown-preview:hide', [$form]);
}); });
}());
}).call(this);
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment