debian-mirror-gitlab/app/assets/javascripts/preview_markdown.js

201 lines
5.9 KiB
JavaScript
Raw Normal View History

2017-08-17 22:00:37 +05:30
/* eslint-disable func-names, no-var, object-shorthand, comma-dangle, prefer-arrow-callback */
2016-09-29 09:46:39 +05:30
// MarkdownPreview
//
2017-08-17 22:00:37 +05:30
// Handles toggling the "Write" and "Preview" tab clicks, rendering the preview
2017-09-10 17:25:29 +05:30
// (including the explanation of quick actions), and showing a warning when
2017-08-17 22:00:37 +05:30
// more than `x` users are referenced.
2016-09-29 09:46:39 +05:30
//
2017-08-17 22:00:37 +05:30
(function () {
var lastTextareaPreviewed;
var lastTextareaHeight = null;
var markdownPreview;
var previewButtonSelector;
var writeButtonSelector;
window.MarkdownPreview = (function () {
2016-09-13 17:45:13 +05:30
function MarkdownPreview() {}
2016-09-29 09:46:39 +05:30
// Minimum number of users referenced before triggering a warning
2016-09-13 17:45:13 +05:30
MarkdownPreview.prototype.referenceThreshold = 10;
2017-08-17 22:00:37 +05:30
MarkdownPreview.prototype.emptyMessage = 'Nothing to preview.';
2016-09-13 17:45:13 +05:30
MarkdownPreview.prototype.ajaxCache = {};
2017-08-17 22:00:37 +05:30
MarkdownPreview.prototype.showPreview = function ($form) {
var mdText;
var preview = $form.find('.js-md-preview');
var url = preview.data('url');
if (preview.hasClass('md-preview-loading')) {
return;
}
mdText = $form.find('textarea.markdown-area').val();
2016-09-13 17:45:13 +05:30
if (mdText.trim().length === 0) {
2017-08-17 22:00:37 +05:30
preview.text(this.emptyMessage);
this.hideReferencedUsers($form);
2016-09-13 17:45:13 +05:30
} else {
2017-08-17 22:00:37 +05:30
preview.addClass('md-preview-loading').text('Loading...');
this.fetchMarkdownPreview(mdText, url, (function (response) {
var body;
if (response.body.length > 0) {
body = response.body;
} else {
body = this.emptyMessage;
}
preview.removeClass('md-preview-loading').html(body);
preview.renderGFM();
this.renderReferencedUsers(response.references.users, $form);
if (response.references.commands) {
this.renderReferencedCommands(response.references.commands, $form);
}
}).bind(this));
2016-09-13 17:45:13 +05:30
}
};
2017-08-17 22:00:37 +05:30
MarkdownPreview.prototype.fetchMarkdownPreview = function (text, url, success) {
if (!url) {
2016-09-13 17:45:13 +05:30
return;
}
if (text === this.ajaxCache.text) {
2017-08-17 22:00:37 +05:30
success(this.ajaxCache.response);
return;
2016-09-13 17:45:13 +05:30
}
2017-08-17 22:00:37 +05:30
$.ajax({
2016-09-13 17:45:13 +05:30
type: 'POST',
2017-08-17 22:00:37 +05:30
url: url,
2016-09-13 17:45:13 +05:30
data: {
text: text
},
dataType: 'json',
2017-08-17 22:00:37 +05:30
success: (function (response) {
this.ajaxCache = {
text: text,
response: response
2016-09-13 17:45:13 +05:30
};
2017-08-17 22:00:37 +05:30
success(response);
}).bind(this)
2016-09-13 17:45:13 +05:30
});
};
2017-08-17 22:00:37 +05:30
MarkdownPreview.prototype.hideReferencedUsers = function ($form) {
$form.find('.referenced-users').hide();
2016-09-13 17:45:13 +05:30
};
2017-08-17 22:00:37 +05:30
MarkdownPreview.prototype.renderReferencedUsers = function (users, $form) {
2016-09-13 17:45:13 +05:30
var referencedUsers;
2017-08-17 22:00:37 +05:30
referencedUsers = $form.find('.referenced-users');
2016-09-13 17:45:13 +05:30
if (referencedUsers.length) {
if (users.length >= this.referenceThreshold) {
referencedUsers.show();
2017-08-17 22:00:37 +05:30
referencedUsers.find('.js-referenced-users-count').text(users.length);
2016-09-13 17:45:13 +05:30
} else {
2017-08-17 22:00:37 +05:30
referencedUsers.hide();
2016-09-13 17:45:13 +05:30
}
}
};
2017-08-17 22:00:37 +05:30
MarkdownPreview.prototype.hideReferencedCommands = function ($form) {
$form.find('.referenced-commands').hide();
};
2016-09-13 17:45:13 +05:30
2017-08-17 22:00:37 +05:30
MarkdownPreview.prototype.renderReferencedCommands = function (commands, $form) {
var referencedCommands;
referencedCommands = $form.find('.referenced-commands');
if (commands.length > 0) {
referencedCommands.html(commands);
referencedCommands.show();
} else {
referencedCommands.html('');
referencedCommands.hide();
}
};
2016-09-13 17:45:13 +05:30
2017-08-17 22:00:37 +05:30
return MarkdownPreview;
}());
markdownPreview = new window.MarkdownPreview();
2016-09-13 17:45:13 +05:30
previewButtonSelector = '.js-md-preview-button';
writeButtonSelector = '.js-md-write-button';
lastTextareaPreviewed = null;
2017-08-17 22:00:37 +05:30
$.fn.setupMarkdownPreview = function () {
var $form = $(this);
$form.find('textarea.markdown-area').on('input', function () {
markdownPreview.hideReferencedUsers($form);
2016-09-13 17:45:13 +05:30
});
};
2017-08-17 22:00:37 +05:30
$(document).on('markdown-preview:show', function (e, $form) {
2016-09-13 17:45:13 +05:30
if (!$form) {
return;
}
2017-08-17 22:00:37 +05:30
2016-09-13 17:45:13 +05:30
lastTextareaPreviewed = $form.find('textarea.markdown-area');
2017-08-17 22:00:37 +05:30
lastTextareaHeight = lastTextareaPreviewed.height();
2016-09-29 09:46:39 +05:30
// toggle tabs
2016-09-13 17:45:13 +05:30
$form.find(writeButtonSelector).parent().removeClass('active');
$form.find(previewButtonSelector).parent().addClass('active');
2017-08-17 22:00:37 +05:30
2016-09-29 09:46:39 +05:30
// toggle content
2016-09-13 17:45:13 +05:30
$form.find('.md-write-holder').hide();
$form.find('.md-preview-holder').show();
2017-08-17 22:00:37 +05:30
markdownPreview.showPreview($form);
2016-09-13 17:45:13 +05:30
});
2017-08-17 22:00:37 +05:30
$(document).on('markdown-preview:hide', function (e, $form) {
2016-09-13 17:45:13 +05:30
if (!$form) {
return;
}
lastTextareaPreviewed = null;
2017-08-17 22:00:37 +05:30
if (lastTextareaHeight) {
$form.find('textarea.markdown-area').height(lastTextareaHeight);
}
2016-09-29 09:46:39 +05:30
// toggle tabs
2016-09-13 17:45:13 +05:30
$form.find(writeButtonSelector).parent().addClass('active');
$form.find(previewButtonSelector).parent().removeClass('active');
2017-08-17 22:00:37 +05:30
2016-09-29 09:46:39 +05:30
// toggle content
2016-09-13 17:45:13 +05:30
$form.find('.md-write-holder').show();
$form.find('textarea.markdown-area').focus();
2017-08-17 22:00:37 +05:30
$form.find('.md-preview-holder').hide();
markdownPreview.hideReferencedCommands($form);
2016-09-13 17:45:13 +05:30
});
2017-08-17 22:00:37 +05:30
$(document).on('markdown-preview:toggle', function (e, keyboardEvent) {
2016-09-13 17:45:13 +05:30
var $target;
$target = $(keyboardEvent.target);
if ($target.is('textarea.markdown-area')) {
$(document).triggerHandler('markdown-preview:show', [$target.closest('form')]);
2017-08-17 22:00:37 +05:30
keyboardEvent.preventDefault();
2016-09-13 17:45:13 +05:30
} else if (lastTextareaPreviewed) {
$target = lastTextareaPreviewed;
$(document).triggerHandler('markdown-preview:hide', [$target.closest('form')]);
2017-08-17 22:00:37 +05:30
keyboardEvent.preventDefault();
2016-09-13 17:45:13 +05:30
}
});
2017-08-17 22:00:37 +05:30
$(document).on('click', previewButtonSelector, function (e) {
2016-09-13 17:45:13 +05:30
var $form;
e.preventDefault();
$form = $(this).closest('form');
2017-08-17 22:00:37 +05:30
$(document).triggerHandler('markdown-preview:show', [$form]);
2016-09-13 17:45:13 +05:30
});
2017-08-17 22:00:37 +05:30
$(document).on('click', writeButtonSelector, function (e) {
2016-09-13 17:45:13 +05:30
var $form;
e.preventDefault();
$form = $(this).closest('form');
2017-08-17 22:00:37 +05:30
$(document).triggerHandler('markdown-preview:hide', [$form]);
2016-09-13 17:45:13 +05:30
});
2017-08-17 22:00:37 +05:30
}());