Use better modal

This commit is contained in:
Maximilian Grundke
2016-04-26 16:06:32 +02:00
parent af103ef270
commit 4b5cd5a22a

View File

@ -25,27 +25,22 @@ do not put a carriage return in the line below. it will be present in the presen
</div> </div>
<% end %> <% end %>
<%= render('shared/modal', id: 'comment-modal', title: t('exercises.implement.comment.dialogtitle'), template: 'exercises/_comment_dialogcontent') %>
<script type="text/javascript"> <script type="text/javascript">
var commentitor = $('.editor'); var commentitor = $('.editor');
var userid = commentitor.data('user-id'); var userid = commentitor.data('user-id');
commentitor.each(function (index, editor) { commentitor.each(function (index, editor) {
currentEditor = ace.edit(editor); var currentEditor = ace.edit(editor);
currentEditor.setReadOnly(true); currentEditor.setReadOnly(true);
setAnnotations(currentEditor, $(editor).data('file-id')); setAnnotations(currentEditor, $(editor).data('file-id'));
currentEditor.on("guttermousedown", handleSidebarClick);
}); });
function setAnnotations(editor, fileid) { function setAnnotations(editor, fileid) {
var session = editor.getSession() var session = editor.getSession();
var inputHtml = ''
inputHtml += '<div class="input-group">'
inputHtml += '<input type="text" class="form-control" id="commentInput"'
inputHtml += 'placeholder="I\'d suggest a variable here" required>'
inputHtml += '<span class="input-group-btn"><button id="submitComment"'
inputHtml += 'class="btn btn-default"><%= t("exercises.implement.comment.addComment") %>!</button></span>'
inputHtml += '</div>'
var jqrequest = $.ajax({ var jqrequest = $.ajax({
dataType: 'json', dataType: 'json',
@ -58,57 +53,108 @@ do not put a carriage return in the line below. it will be present in the presen
jqrequest.done(function(response){ jqrequest.done(function(response){
$.each(response, function(index, comment) { $.each(response, function(index, comment) {
comment.className = "code-ocean_comment" comment.className = "code-ocean_comment";
comment.text = comment.username + ": " + comment.text comment.text = comment.username + ": " + comment.text
}) });
editor.getSession().setAnnotations(response) session.setAnnotations(response);
$(editor.container).find('.ace_gutter-cell').popover({
title: 'Add a comment',
html: true,
content: inputHtml,
position: 'right',
trigger: 'focus click'
}).on('shown.bs.popover', function() {
var container = $(editor.container)
container.find('#commentInput').focus()
container.find('#submitComment').click(_.partial(addComment, editor, fileid));
container.find('#commentInput').data('line', $(this).text())
})
}) })
} }
function addComment(editor, fileid) { function hasCommentsInRow(editor, row){
var commentInput = $(editor.container).find('#commentInput'); return editor.getSession().getAnnotations().some(function(element) {
var comment = commentInput.val() return element.row === row;
var line = commentInput.data('line') })
}
if (line == '' || comment == '') { function getCommentsForRow(editor, row){
return return editor.getSession().getAnnotations().filter(function(element) {
} else { return element.row === row;
line = parseInt(line) - 1 })
} }
$.ajax({ function deleteComment(file_id, row, editor) {
var jqxhr = $.ajax({
type: 'DELETE',
url: "/comments",
data: {
row: row,
file_id: file_id }
});
jqxhr.done(function (response) {
setAnnotations(editor, file_id);
});
jqxhr.fail(ajaxError);
}
function createComment(file_id, row, editor, commenttext){
var jqxhr = $.ajax({
data: { data: {
comment: { comment: {
user_id: userid, file_id: file_id,
file_id: fileid, row: row,
row: line,
column: 0, column: 0,
text: comment text: commenttext
} }
}, },
dataType: 'json', dataType: 'json',
method: 'POST', method: 'POST',
url: "/comments" url: "/comments"
}).done(setAnnotations(editor, fileid)) });
jqxhr.done(function(response){
$('.ace_gutter-cell').popover('hide') setAnnotations(editor, file_id);
});
jqxhr.fail(ajaxError);
} }
function handleSidebarClick(e) {
var target = e.domEvent.toElement;
var editor = e.editor;
if (target.className.indexOf("ace_gutter-cell") == -1) return;
if (!editor.isFocused()) return;
if (e.clientX > 25 + target.getBoundingClientRect().left) return;
var row = e.getDocumentPosition().row;
e.stop();
var commentModal = $('#comment-modal');
if (hasCommentsInRow(editor, row)) {
var rowComments = getCommentsForRow(editor, row);
var comments = _.pluck(rowComments, 'text').join('\n');
commentModal.find('#other-comments').text(comments);
} else {
commentModal.find('#other-comments').text('none');
}
commentModal.find('#addCommentButton').off('click');
commentModal.find('#removeAllButton').off('click');
commentModal.find('#addCommentButton').on('click', function(e){
var commenttext = commentModal.find('textarea').val();
var file_id = $(editor.container).data('file-id');
if (commenttext !== "") {
createComment(file_id, row, editor, commenttext);
commentModal.modal('hide');
}
});
commentModal.find('#removeAllButton').on('click', function(e){
var file_id = $(editor.container).data('file-id');
deleteComment(file_id,row, editor);
commentModal.modal('hide');
});
commentModal.modal('show');
}
function ajaxError(response) {
var message = ((response || {}).responseJSON || {}).message || '';
$.flash.danger({
text: message.length > 0 ? message : $('#flash').data('message-failure')
});
};
</script> </script>
<style>
#commentitor, .ace_gutter, .ace_gutter-layer { overflow: visible }
.popover { width: 400px; max-width: none; }
</style>