Create popovers on demand on gutter elements to prevent element recycling messing it up
This commit is contained in:
@@ -146,13 +146,9 @@ also, all settings from the rails model needed for the editor configuration in t
|
|||||||
currentEditor.commentVisualsByLine = {};
|
currentEditor.commentVisualsByLine = {};
|
||||||
setAnnotations(currentEditor, $(editor).data('file-id'));
|
setAnnotations(currentEditor, $(editor).data('file-id'));
|
||||||
currentEditor.on("guttermousedown", handleSidebarClick);
|
currentEditor.on("guttermousedown", handleSidebarClick);
|
||||||
|
currentEditor.on("guttermousemove", showPopover);
|
||||||
});
|
});
|
||||||
|
|
||||||
function cleanupPopovers() {
|
|
||||||
// remove all possible popovers
|
|
||||||
$('.editor > .ace_gutter > .ace_gutter-layer > .ace_gutter-cell').popover('destroy');
|
|
||||||
}
|
|
||||||
|
|
||||||
function preprocess(commentText) {
|
function preprocess(commentText) {
|
||||||
// sanitize comments to deal with XSS attacks:
|
// sanitize comments to deal with XSS attacks:
|
||||||
commentText = $('div.sanitizer').text(commentText).html();
|
commentText = $('div.sanitizer').text(commentText).html();
|
||||||
@@ -160,29 +156,6 @@ also, all settings from the rails model needed for the editor configuration in t
|
|||||||
return commentText.replace(/\n/g, '<br>');
|
return commentText.replace(/\n/g, '<br>');
|
||||||
}
|
}
|
||||||
|
|
||||||
function clusterComments(comments) {
|
|
||||||
var clusters = {};
|
|
||||||
// comments need to be sorted to cluster them per line
|
|
||||||
comments.sort(function (a, b) {
|
|
||||||
return a.row - b.row;
|
|
||||||
});
|
|
||||||
while (comments.length > 0) {
|
|
||||||
// new cluster of comments
|
|
||||||
var cluster = [];
|
|
||||||
var clusterRow = comments[0].row;
|
|
||||||
// now collect all comments on this line
|
|
||||||
while (comments.length > 0 && comments[0].row === clusterRow) {
|
|
||||||
cluster.push(comments.shift());
|
|
||||||
}
|
|
||||||
// sort the comments by creation date
|
|
||||||
cluster = cluster.sort(function (a, b) {
|
|
||||||
return a.id - b.id;
|
|
||||||
});
|
|
||||||
clusters[clusterRow] = cluster;
|
|
||||||
}
|
|
||||||
return clusters;
|
|
||||||
}
|
|
||||||
|
|
||||||
function generateCommentHtmlContent(comments) {
|
function generateCommentHtmlContent(comments) {
|
||||||
var htmlContent = '';
|
var htmlContent = '';
|
||||||
comments.forEach(function(comment, index) {
|
comments.forEach(function(comment, index) {
|
||||||
@@ -211,23 +184,19 @@ also, all settings from the rails model needed for the editor configuration in t
|
|||||||
return htmlContent;
|
return htmlContent;
|
||||||
}
|
}
|
||||||
|
|
||||||
function buildPopover(fileid, line, comments) {
|
function buildPopover(comments, where) {
|
||||||
// only display the newest three comments in preview
|
// only display the newest three comments in preview
|
||||||
var maxComments = 3;
|
var maxComments = 3;
|
||||||
var htmlContent = generateCommentHtmlContent(comments.reverse().slice(0, maxComments));
|
var htmlContent = generateCommentHtmlContent(comments.reverse().slice(0, maxComments));
|
||||||
if (comments.length > maxComments) {
|
if (comments.length > maxComments) {
|
||||||
// add a hint that there are more comments than shown here
|
// add a hint that there are more comments than shown here
|
||||||
htmlContent += '<div class="popover-footer"><%= t('request_for_comments.click_for_more_comments') %></div>'
|
htmlContent += '<div class="popover-footer"><%= t('request_for_comments.click_for_more_comments') %></div>'
|
||||||
.replace('${numComments}', comments.length - maxComments);
|
.replace('${numComments}', String(comments.length - maxComments));
|
||||||
}
|
}
|
||||||
// attach the popover to the ace sidebar (where the comment icon is displayed)
|
where.popover({
|
||||||
var icon = $('*[data-file-id="' + fileid + '"]') // the editor for this file
|
|
||||||
.find('.ace_gutter > .ace_gutter-layer') // the sidebar
|
|
||||||
.find('div:nth-child(' + (parseInt(line) + 1) + ')'); // the correct line
|
|
||||||
icon.popover({
|
|
||||||
content: htmlContent,
|
content: htmlContent,
|
||||||
html: true, // necessary to style comments. XSS is not possible due to comment pre-processing (sanitizing)
|
html: true, // necessary to style comments. XSS is not possible due to comment pre-processing (sanitizing)
|
||||||
trigger: 'hover',
|
trigger: 'manual', // can only be triggered by $(where).popover('show' | 'hide')
|
||||||
container: 'body'
|
container: 'body'
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -245,22 +214,20 @@ also, all settings from the rails model needed for the editor configuration in t
|
|||||||
});
|
});
|
||||||
|
|
||||||
jqrequest.done(function(response){
|
jqrequest.done(function(response){
|
||||||
editor.commentVisualsByLine = {};
|
|
||||||
var clusters = clusterComments(response.slice());
|
|
||||||
$.each(clusters, function (line, cluster) {
|
|
||||||
editor.commentVisualsByLine[line] = generateCommentHtmlContent(cluster);
|
|
||||||
buildPopover(fileid, line, cluster);
|
|
||||||
});
|
|
||||||
|
|
||||||
$.each(response, function(index, comment) {
|
$.each(response, function(index, comment) {
|
||||||
comment.className = 'code-ocean_comment';
|
comment.className = 'code-ocean_comment';
|
||||||
});
|
});
|
||||||
session.setAnnotations(response);
|
session.setAnnotations(response);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function getCommentsForRow(editor, row){
|
||||||
|
return editor.getSession().getAnnotations().filter(function(element) {
|
||||||
|
return element.row === row;
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
function deleteComment(commentId, editor, file_id, callback) {
|
function deleteComment(commentId, editor, file_id, callback) {
|
||||||
cleanupPopovers();
|
|
||||||
var jqxhr = $.ajax({
|
var jqxhr = $.ajax({
|
||||||
type: 'DELETE',
|
type: 'DELETE',
|
||||||
url: "/comments/" + commentId
|
url: "/comments/" + commentId
|
||||||
@@ -273,7 +240,6 @@ also, all settings from the rails model needed for the editor configuration in t
|
|||||||
}
|
}
|
||||||
|
|
||||||
function updateComment(commentId, text, editor, file_id, callback) {
|
function updateComment(commentId, text, editor, file_id, callback) {
|
||||||
cleanupPopovers();
|
|
||||||
var jqxhr = $.ajax({
|
var jqxhr = $.ajax({
|
||||||
type: 'PATCH',
|
type: 'PATCH',
|
||||||
url: "/comments/" + commentId,
|
url: "/comments/" + commentId,
|
||||||
@@ -291,7 +257,6 @@ also, all settings from the rails model needed for the editor configuration in t
|
|||||||
}
|
}
|
||||||
|
|
||||||
function createComment(file_id, row, editor, commenttext){
|
function createComment(file_id, row, editor, commenttext){
|
||||||
cleanupPopovers();
|
|
||||||
var jqxhr = $.ajax({
|
var jqxhr = $.ajax({
|
||||||
data: {
|
data: {
|
||||||
comment: {
|
comment: {
|
||||||
@@ -306,7 +271,7 @@ also, all settings from the rails model needed for the editor configuration in t
|
|||||||
method: 'POST',
|
method: 'POST',
|
||||||
url: "/comments"
|
url: "/comments"
|
||||||
});
|
});
|
||||||
jqxhr.done(function(response){
|
jqxhr.done(function(){
|
||||||
setAnnotations(editor, file_id);
|
setAnnotations(editor, file_id);
|
||||||
});
|
});
|
||||||
jqxhr.fail(ajaxError);
|
jqxhr.fail(ajaxError);
|
||||||
@@ -355,13 +320,45 @@ also, all settings from the rails model needed for the editor configuration in t
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var lastRow = null;
|
||||||
|
var lastTarget = null;
|
||||||
|
function showPopover(e) {
|
||||||
|
var target = e.domEvent.target;
|
||||||
|
var row = e.getDocumentPosition().row;
|
||||||
|
|
||||||
|
if (target.className.indexOf('ace_gutter-cell') === -1 || lastRow === row) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (lastTarget === target) {
|
||||||
|
// sometimes the row gets updated before the DOM event target, so we need to wait for it to change
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
lastRow = row;
|
||||||
|
|
||||||
|
var editor = e.editor;
|
||||||
|
var comments = getCommentsForRow(editor, row);
|
||||||
|
buildPopover(comments, $(target));
|
||||||
|
lastTarget = target;
|
||||||
|
|
||||||
|
$(target).popover('show');
|
||||||
|
$(target).on('mouseleave', function () {
|
||||||
|
$(this).off('mouseleave');
|
||||||
|
$(this).popover('destroy');
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
$('.ace_gutter').on('mouseleave', function () {
|
||||||
|
lastRow = null;
|
||||||
|
lastTarget = null;
|
||||||
|
});
|
||||||
|
|
||||||
function handleSidebarClick(e) {
|
function handleSidebarClick(e) {
|
||||||
var target = e.domEvent.target;
|
var target = e.domEvent.target;
|
||||||
|
if (target.className.indexOf('ace_gutter-cell') === -1) return;
|
||||||
|
|
||||||
var editor = e.editor;
|
var editor = e.editor;
|
||||||
var fileid = $(editor.container).data('file-id');
|
var fileid = $(editor.container).data('file-id');
|
||||||
|
|
||||||
if (target.className.indexOf("ace_gutter-cell") == -1) return;
|
|
||||||
|
|
||||||
var row = e.getDocumentPosition().row;
|
var row = e.getDocumentPosition().row;
|
||||||
e.stop();
|
e.stop();
|
||||||
$('.modal-title').text('<%= t('request_for_comments.modal_title') %>'.replace('${line}', row + 1));
|
$('.modal-title').text('<%= t('request_for_comments.modal_title') %>'.replace('${line}', row + 1));
|
||||||
@@ -369,7 +366,7 @@ also, all settings from the rails model needed for the editor configuration in t
|
|||||||
var commentModal = $('#comment-modal');
|
var commentModal = $('#comment-modal');
|
||||||
|
|
||||||
var otherComments = commentModal.find('#otherComments');
|
var otherComments = commentModal.find('#otherComments');
|
||||||
var htmlContent = editor.commentVisualsByLine[row];
|
var htmlContent = generateCommentHtmlContent(getCommentsForRow(editor, row));
|
||||||
if (htmlContent) {
|
if (htmlContent) {
|
||||||
otherComments.show();
|
otherComments.show();
|
||||||
var container = otherComments.find('.container');
|
var container = otherComments.find('.container');
|
||||||
@@ -454,19 +451,4 @@ also, all settings from the rails model needed for the editor configuration in t
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
function stringDivider(str, width, spaceReplacer) {
|
|
||||||
if (str.length>width) {
|
|
||||||
var p=width;
|
|
||||||
for (;p>0 && str[p]!=' ';p--) {
|
|
||||||
}
|
|
||||||
if (p>0) {
|
|
||||||
var left = str.substring(0, p);
|
|
||||||
var right = str.substring(p+1);
|
|
||||||
return left + spaceReplacer + stringDivider(right, width, spaceReplacer);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return str;
|
|
||||||
}
|
|
||||||
|
|
||||||
</script>
|
</script>
|
||||||
|
Reference in New Issue
Block a user