2016-12-11 22:57:36 -05:00
|
|
|
/* eslint-disable func-names, no-var, object-shorthand, comma-dangle, prefer-arrow-callback */
|
2016-12-14 00:26:26 -05:00
|
|
|
|
2016-07-26 23:32:10 -04:00
|
|
|
// MarkdownPreview
|
|
|
|
//
|
|
|
|
// Handles toggling the "Write" and "Preview" tab clicks, rendering the preview,
|
|
|
|
// and showing a warning when more than `x` users are referenced.
|
|
|
|
//
|
2016-12-11 22:55:17 -05:00
|
|
|
(function () {
|
2016-12-11 22:57:36 -05:00
|
|
|
var lastTextareaPreviewed;
|
|
|
|
var markdownPreview;
|
|
|
|
var previewButtonSelector;
|
|
|
|
var writeButtonSelector;
|
2016-07-24 16:45:11 -04:00
|
|
|
|
2016-12-11 22:55:17 -05:00
|
|
|
window.MarkdownPreview = (function () {
|
2016-07-24 16:45:11 -04:00
|
|
|
function MarkdownPreview() {}
|
|
|
|
|
2016-07-26 23:32:10 -04:00
|
|
|
// Minimum number of users referenced before triggering a warning
|
2016-07-24 16:45:11 -04:00
|
|
|
MarkdownPreview.prototype.referenceThreshold = 10;
|
|
|
|
|
|
|
|
MarkdownPreview.prototype.ajaxCache = {};
|
|
|
|
|
2016-12-11 22:55:17 -05:00
|
|
|
MarkdownPreview.prototype.showPreview = function ($form) {
|
2016-12-27 17:30:51 -05:00
|
|
|
var mdText;
|
2016-12-11 22:57:36 -05:00
|
|
|
var preview = $form.find('.js-md-preview');
|
2016-12-27 17:30:51 -05:00
|
|
|
if (preview.hasClass('md-preview-loading')) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
mdText = $form.find('textarea.markdown-area').val();
|
|
|
|
|
2016-07-24 16:45:11 -04:00
|
|
|
if (mdText.trim().length === 0) {
|
|
|
|
preview.text('Nothing to preview.');
|
2016-12-11 22:35:19 -05:00
|
|
|
this.hideReferencedUsers($form);
|
2016-12-27 17:30:51 -05:00
|
|
|
} else {
|
2016-12-11 22:48:49 -05:00
|
|
|
preview.addClass('md-preview-loading').text('Loading...');
|
2016-12-11 22:55:17 -05:00
|
|
|
this.fetchMarkdownPreview(mdText, (function (response) {
|
2016-12-11 22:48:49 -05:00
|
|
|
preview.removeClass('md-preview-loading').html(response.body);
|
2016-12-11 22:18:40 -05:00
|
|
|
preview.renderGFM();
|
2016-12-11 22:35:19 -05:00
|
|
|
this.renderReferencedUsers(response.references.users, $form);
|
2016-12-11 22:18:40 -05:00
|
|
|
}).bind(this));
|
2016-07-24 16:45:11 -04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-12-11 22:55:17 -05:00
|
|
|
MarkdownPreview.prototype.fetchMarkdownPreview = function (text, success) {
|
2016-04-13 08:17:42 -04:00
|
|
|
if (!window.preview_markdown_path) {
|
2016-07-24 16:45:11 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (text === this.ajaxCache.text) {
|
2016-12-11 22:15:03 -05:00
|
|
|
success(this.ajaxCache.response);
|
|
|
|
return;
|
2016-07-24 16:45:11 -04:00
|
|
|
}
|
2016-12-11 22:15:03 -05:00
|
|
|
$.ajax({
|
2016-07-24 16:45:11 -04:00
|
|
|
type: 'POST',
|
2016-04-13 08:17:42 -04:00
|
|
|
url: window.preview_markdown_path,
|
2016-07-24 16:45:11 -04:00
|
|
|
data: {
|
|
|
|
text: text
|
|
|
|
},
|
|
|
|
dataType: 'json',
|
2016-12-11 22:55:17 -05:00
|
|
|
success: (function (response) {
|
2016-12-11 22:18:40 -05:00
|
|
|
this.ajaxCache = {
|
|
|
|
text: text,
|
|
|
|
response: response
|
2016-07-24 16:45:11 -04:00
|
|
|
};
|
2016-12-11 22:18:40 -05:00
|
|
|
success(response);
|
|
|
|
}).bind(this)
|
2016-07-24 16:45:11 -04:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2016-12-11 22:55:17 -05:00
|
|
|
MarkdownPreview.prototype.hideReferencedUsers = function ($form) {
|
2016-12-11 22:35:19 -05:00
|
|
|
$form.find('.referenced-users').hide();
|
2016-07-24 16:45:11 -04:00
|
|
|
};
|
|
|
|
|
2016-12-11 22:55:17 -05:00
|
|
|
MarkdownPreview.prototype.renderReferencedUsers = function (users, $form) {
|
2016-07-24 16:45:11 -04:00
|
|
|
var referencedUsers;
|
2016-12-11 22:35:19 -05:00
|
|
|
referencedUsers = $form.find('.referenced-users');
|
2016-07-24 16:45:11 -04:00
|
|
|
if (referencedUsers.length) {
|
|
|
|
if (users.length >= this.referenceThreshold) {
|
|
|
|
referencedUsers.show();
|
2016-12-11 22:15:03 -05:00
|
|
|
referencedUsers.find('.js-referenced-users-count').text(users.length);
|
2016-07-24 16:45:11 -04:00
|
|
|
} else {
|
2016-12-11 22:15:03 -05:00
|
|
|
referencedUsers.hide();
|
2016-07-24 16:45:11 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return MarkdownPreview;
|
2016-12-11 22:55:17 -05:00
|
|
|
}());
|
2016-07-24 16:45:11 -04:00
|
|
|
|
2016-12-14 00:26:26 -05:00
|
|
|
markdownPreview = new window.MarkdownPreview();
|
2016-07-24 16:45:11 -04:00
|
|
|
|
|
|
|
previewButtonSelector = '.js-md-preview-button';
|
|
|
|
|
|
|
|
writeButtonSelector = '.js-md-write-button';
|
|
|
|
|
|
|
|
lastTextareaPreviewed = null;
|
|
|
|
|
2016-12-11 22:55:17 -05:00
|
|
|
$.fn.setupMarkdownPreview = function () {
|
2016-12-11 22:15:03 -05:00
|
|
|
var $form = $(this);
|
2016-12-27 17:23:57 -05:00
|
|
|
$form.find('textarea.markdown-area').on('input', function () {
|
|
|
|
markdownPreview.hideReferencedUsers($form);
|
|
|
|
});
|
2016-07-24 16:45:11 -04:00
|
|
|
};
|
|
|
|
|
2016-12-11 22:55:17 -05:00
|
|
|
$(document).on('markdown-preview:show', function (e, $form) {
|
2016-07-24 16:45:11 -04:00
|
|
|
if (!$form) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
lastTextareaPreviewed = $form.find('textarea.markdown-area');
|
2016-07-26 23:32:10 -04:00
|
|
|
// toggle tabs
|
2016-07-24 16:45:11 -04:00
|
|
|
$form.find(writeButtonSelector).parent().removeClass('active');
|
|
|
|
$form.find(previewButtonSelector).parent().addClass('active');
|
2016-07-26 23:32:10 -04:00
|
|
|
// toggle content
|
2016-07-24 16:45:11 -04:00
|
|
|
$form.find('.md-write-holder').hide();
|
|
|
|
$form.find('.md-preview-holder').show();
|
2016-12-11 22:15:03 -05:00
|
|
|
markdownPreview.showPreview($form);
|
2016-07-24 16:45:11 -04:00
|
|
|
});
|
|
|
|
|
2016-12-11 22:55:17 -05:00
|
|
|
$(document).on('markdown-preview:hide', function (e, $form) {
|
2016-07-24 16:45:11 -04:00
|
|
|
if (!$form) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
lastTextareaPreviewed = null;
|
2016-07-26 23:32:10 -04:00
|
|
|
// toggle tabs
|
2016-07-24 16:45:11 -04:00
|
|
|
$form.find(writeButtonSelector).parent().addClass('active');
|
|
|
|
$form.find(previewButtonSelector).parent().removeClass('active');
|
2016-07-26 23:32:10 -04:00
|
|
|
// toggle content
|
2016-07-24 16:45:11 -04:00
|
|
|
$form.find('.md-write-holder').show();
|
|
|
|
$form.find('textarea.markdown-area').focus();
|
2016-12-11 22:15:03 -05:00
|
|
|
$form.find('.md-preview-holder').hide();
|
2016-07-24 16:45:11 -04:00
|
|
|
});
|
|
|
|
|
2016-12-11 22:55:17 -05:00
|
|
|
$(document).on('markdown-preview:toggle', function (e, keyboardEvent) {
|
2016-07-24 16:45:11 -04:00
|
|
|
var $target;
|
|
|
|
$target = $(keyboardEvent.target);
|
|
|
|
if ($target.is('textarea.markdown-area')) {
|
|
|
|
$(document).triggerHandler('markdown-preview:show', [$target.closest('form')]);
|
2016-12-11 22:15:03 -05:00
|
|
|
keyboardEvent.preventDefault();
|
2016-07-24 16:45:11 -04:00
|
|
|
} else if (lastTextareaPreviewed) {
|
|
|
|
$target = lastTextareaPreviewed;
|
|
|
|
$(document).triggerHandler('markdown-preview:hide', [$target.closest('form')]);
|
2016-12-11 22:15:03 -05:00
|
|
|
keyboardEvent.preventDefault();
|
2016-07-24 16:45:11 -04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2016-12-11 22:55:17 -05:00
|
|
|
$(document).on('click', previewButtonSelector, function (e) {
|
2016-07-24 16:45:11 -04:00
|
|
|
var $form;
|
|
|
|
e.preventDefault();
|
|
|
|
$form = $(this).closest('form');
|
2016-12-11 22:15:03 -05:00
|
|
|
$(document).triggerHandler('markdown-preview:show', [$form]);
|
2016-07-24 16:45:11 -04:00
|
|
|
});
|
|
|
|
|
2016-12-11 22:55:17 -05:00
|
|
|
$(document).on('click', writeButtonSelector, function (e) {
|
2016-07-24 16:45:11 -04:00
|
|
|
var $form;
|
|
|
|
e.preventDefault();
|
|
|
|
$form = $(this).closest('form');
|
2016-12-11 22:15:03 -05:00
|
|
|
$(document).triggerHandler('markdown-preview:hide', [$form]);
|
2016-07-24 16:45:11 -04:00
|
|
|
});
|
2016-12-11 22:57:36 -05:00
|
|
|
}());
|