gitlabhq/app/assets/javascripts/notes.js

573 lines
16 KiB
JavaScript
Raw Normal View History

2011-11-15 09:34:30 +01:00
var NoteList = {
2011-11-04 14:37:38 +01:00
notes_path: null,
target_params: null,
target_id: 0,
target_type: null,
2012-09-14 17:01:34 +02:00
top_id: 0,
bottom_id: 0,
loading_more_disabled: false,
2012-09-14 17:01:34 +02:00
reversed: false,
2012-11-22 00:23:26 +01:00
init: function(tid, tt, path) {
2012-11-22 02:56:42 +01:00
NoteList.notes_path = path + ".js";
NoteList.target_id = tid;
NoteList.target_type = tt;
NoteList.reversed = $("#notes-list").is(".reversed");
NoteList.target_params = "target_type=" + NoteList.target_type + "&target_id=" + NoteList.target_id;
NoteList.setupMainTargetNoteForm();
2012-11-22 02:56:42 +01:00
if(NoteList.reversed) {
var form = $(".js-main-target-form");
form.find(".note-form-actions").hide();
2012-11-22 02:56:42 +01:00
var textarea = form.find(".js-note-text");
2012-11-22 00:23:26 +01:00
textarea.css("height", "40px");
textarea.on("focus", function(){
2012-11-22 02:56:42 +01:00
textarea.css("height", "80px");
form.find(".note-form-actions").show();
2012-11-22 00:23:26 +01:00
});
}
2012-11-14 14:36:51 +01:00
2012-11-22 00:23:26 +01:00
// get initial set of notes
2012-11-22 02:56:42 +01:00
NoteList.getContent();
2012-11-20 17:46:55 +01:00
2012-11-22 02:56:42 +01:00
// add a new diff note
2012-11-22 00:23:26 +01:00
$(document).on("click",
".js-add-diff-note-button",
NoteList.addDiffNote);
2012-11-22 02:56:42 +01:00
// reply to diff/discussion notes
2012-11-22 00:23:26 +01:00
$(document).on("click",
".js-discussion-reply-button",
NoteList.replyToDiscussionNote);
2012-11-22 02:56:42 +01:00
// setup note preview
$(document).on("click",
".js-note-preview-button",
NoteList.previewNote);
2012-12-03 22:34:50 +01:00
// update the file name when an attachment is selected
$(document).on("change",
".js-note-attachment-input",
NoteList.updateFormAttachment);
2012-11-22 00:23:26 +01:00
// hide diff note form
$(document).on("click",
".js-close-discussion-note-form",
NoteList.removeDiscussionNoteForm);
// remove a note (in general)
$(document).on("click",
".js-note-delete",
NoteList.removeNote);
2012-12-03 22:34:50 +01:00
// reset main target form after submit
2012-12-03 20:35:09 +01:00
$(document).on("ajax:complete",
".js-main-target-form",
2012-12-03 22:34:50 +01:00
NoteList.resetMainTargetForm);
2011-11-04 14:37:38 +01:00
2012-02-27 18:05:27 +01:00
2012-12-03 22:34:50 +01:00
$(document).on("click",
".js-choose-note-attachment-button",
NoteList.chooseNoteAttachment);
2013-01-15 10:12:17 +01:00
$(document).on("click",
".js-show-outdated-discussion",
function(e) { $(this).next('.outdated-discussion').show(); e.preventDefault() });
2012-12-03 22:34:50 +01:00
},
2012-11-20 17:46:55 +01:00
2012-12-03 20:35:09 +01:00
/**
2012-12-03 22:34:50 +01:00
* When clicking on buttons
2012-12-03 20:35:09 +01:00
*/
2012-11-20 17:46:55 +01:00
/**
* Called when clicking on the "add a comment" button on the side of a diff line.
*
* Inserts a temporary row for the form below the line.
* Sets up the form and shows it.
*/
addDiffNote: function(e) {
e.preventDefault();
2012-11-20 17:46:55 +01:00
// find the form
var form = $(".js-new-note-form");
2012-11-20 17:46:55 +01:00
var row = $(this).closest("tr");
var nextRow = row.next();
// does it already have notes?
if (nextRow.is(".notes_holder")) {
2012-12-02 20:43:39 +01:00
$.proxy(NoteList.replyToDiscussionNote,
nextRow.find(".js-discussion-reply-button")
2012-11-20 17:46:55 +01:00
).call();
} else {
// add a notes row and insert the form
row.after('<tr class="notes_holder js-temp-notes-holder"><td class="notes_line" colspan="2"></td><td class="notes_content"></td></tr>');
form.clone().appendTo(row.next().find(".notes_content"));
// show the form
2012-12-02 20:43:39 +01:00
NoteList.setupDiscussionNoteForm($(this), row.next().find("form"));
2012-11-20 17:46:55 +01:00
}
},
2012-12-03 22:34:50 +01:00
/**
* Called when clicking the "Choose File" button.
*
* Opesn the file selection dialog.
*/
chooseNoteAttachment: function() {
var form = $(this).closest("form");
form.find(".js-note-attachment-input").click();
},
2012-11-22 02:56:42 +01:00
/**
* Shows the note preview.
*
* Lets the server render GFM into Html and displays it.
*
* Note: uses the Toggler behavior to toggle preview/edit views/buttons
*/
previewNote: function(e) {
e.preventDefault();
2012-11-22 02:56:42 +01:00
var form = $(this).closest("form");
var preview = form.find('.js-note-preview');
var noteText = form.find('.js-note-text').val();
2012-11-22 02:56:42 +01:00
if(noteText.trim().length === 0) {
2012-11-22 02:56:42 +01:00
preview.text('Nothing to preview.');
} else {
2012-11-22 02:56:42 +01:00
preview.text('Loading...');
$.post($(this).data('url'), {note: noteText})
2012-11-22 02:56:42 +01:00
.success(function(previewData) {
preview.html(previewData);
});
}
},
2012-11-20 17:46:55 +01:00
/**
* Called in response to "cancel" on a diff note form.
*
* Shows the reply button again.
* Removes the form and if necessary it's temporary row.
*/
2012-12-03 21:43:17 +01:00
removeDiscussionNoteForm: function() {
2012-11-20 17:46:55 +01:00
var form = $(this).closest("form");
var row = form.closest("tr");
// show the reply button (will only work for replys)
2012-12-02 20:43:39 +01:00
form.prev(".js-discussion-reply-button").show();
2012-11-20 17:46:55 +01:00
if (row.is(".js-temp-notes-holder")) {
2012-12-02 20:43:39 +01:00
// remove temporary row for diff lines
2012-11-20 17:46:55 +01:00
row.remove();
} else {
// only remove the form
form.remove();
}
},
/**
* Called in response to deleting a note of any kind.
*
* Removes the actual note from view.
2012-12-03 21:43:17 +01:00
* Removes the whole discussion if the last note is being removed.
2012-11-20 17:46:55 +01:00
*/
removeNote: function() {
2012-12-03 21:43:17 +01:00
var note = $(this).closest(".note");
var notes = note.closest(".notes");
// check if this is the last note for this line
if (notes.find(".note").length === 1) {
// for discussions
notes.closest(".discussion").remove();
// for diff lines
notes.closest("tr").remove();
}
note.remove();
2012-11-20 17:46:55 +01:00
NoteList.updateVotes();
},
/**
* Called when clicking on the "reply" button for a diff line.
*
* Shows the note form below the notes.
*/
2012-12-02 20:43:39 +01:00
replyToDiscussionNote: function() {
2012-11-20 17:46:55 +01:00
// find the form
var form = $(".js-new-note-form");
2012-11-20 17:46:55 +01:00
// hide reply button
$(this).hide();
// insert the form after the button
form.clone().insertAfter($(this));
// show the form
2012-12-02 20:43:39 +01:00
NoteList.setupDiscussionNoteForm($(this), $(this).next("form"));
2012-11-20 17:46:55 +01:00
},
2012-11-20 17:46:55 +01:00
/**
* Helper for inserting and setting up note forms.
*/
2012-12-03 20:35:09 +01:00
/**
* Called in response to creating a note failing validation.
*
* Adds the rendered errors to the respective form.
* If "discussionId" is null or undefined, the main target form is assumed.
*/
errorsOnForm: function(errorsHtml, discussionId) {
// find the form
if (discussionId) {
var form = $("form[rel='"+discussionId+"']");
} else {
var form = $(".js-main-target-form");
}
form.find(".js-errors").remove();
form.prepend(errorsHtml);
form.find(".js-note-text").focus();
},
/**
* Shows the diff/discussion form and does some setup on it.
2012-11-20 17:46:55 +01:00
*
* Sets some hidden fields in the form.
*
* Note: dataHolder must have the "discussionId", "lineCode", "noteableType"
* and "noteableId" data attributes set.
2012-11-20 17:46:55 +01:00
*/
setupDiscussionNoteForm: function(dataHolder, form) {
2012-11-20 17:46:55 +01:00
// setup note target
form.attr("rel", dataHolder.data("discussionId"));
2013-01-02 20:49:27 +01:00
form.find("#note_commit_id").val(dataHolder.data("commitId"));
form.find("#note_line_code").val(dataHolder.data("lineCode"));
form.find("#note_noteable_type").val(dataHolder.data("noteableType"));
form.find("#note_noteable_id").val(dataHolder.data("noteableId"));
2012-11-20 17:46:55 +01:00
NoteList.setupNoteForm(form);
2012-11-20 17:46:55 +01:00
2012-12-03 21:43:17 +01:00
form.find(".js-note-text").focus();
},
/**
* Shows the main form and does some setup on it.
*
* Sets some hidden fields in the form.
*/
setupMainTargetNoteForm: function() {
// find the form
var form = $(".js-new-note-form");
// insert the form after the button
form.clone().replaceAll($(".js-main-target-form"));
form = form.prev("form");
// show the form
NoteList.setupNoteForm(form);
// fix classes
form.removeClass("js-new-note-form");
form.addClass("js-main-target-form");
// remove unnecessary fields and buttons
form.find("#note_line_code").remove();
form.find(".js-close-discussion-note-form").remove();
},
/**
* General note form setup.
*
* * deactivates the submit button when text is empty
* * hides the preview button when text is empty
* * setup GFM auto complete
* * show the form
*/
setupNoteForm: function(form) {
disableButtonIfEmptyField(form.find(".js-note-text"), form.find(".js-comment-button"));
2012-12-03 21:43:17 +01:00
form.removeClass("js-new-note-form");
// setup preview buttons
2012-12-03 21:43:17 +01:00
form.find(".js-note-edit-button, .js-note-preview-button")
.tooltip({ placement: 'left' });
2012-12-03 21:43:17 +01:00
previewButton = form.find(".js-note-preview-button");
form.find(".js-note-text").on("input", function() {
if ($(this).val().trim() !== "") {
2012-12-03 21:43:17 +01:00
previewButton.removeClass("turn-off").addClass("turn-on");
} else {
2012-12-03 21:43:17 +01:00
previewButton.removeClass("turn-on").addClass("turn-off");
}
});
2012-12-03 22:34:50 +01:00
// remove notify commit author checkbox for non-commit notes
if (form.find("#note_noteable_type").val() !== "Commit") {
form.find(".js-notify-commit-author").remove();
}
GitLab.GfmAutoComplete.setup();
2012-11-20 17:46:55 +01:00
form.show();
},
/**
* Handle loading the initial set of notes.
* And set up loading more notes when scrolling to the bottom of the page.
*/
2012-02-27 18:05:27 +01:00
/**
* Gets an initial set of notes.
*/
2012-11-22 00:23:26 +01:00
getContent: function() {
$.ajax({
2012-11-22 02:56:42 +01:00
url: NoteList.notes_path,
data: NoteList.target_params,
2012-12-03 21:43:17 +01:00
complete: function(){ $('.js-notes-busy').removeClass("loading")},
beforeSend: function() { $('.js-notes-busy').addClass("loading") },
2012-11-22 00:23:26 +01:00
dataType: "script"
});
},
2012-02-27 18:05:27 +01:00
/**
* Called in response to getContent().
* Replaces the content of #notes-list with the given html.
*/
2012-11-22 00:23:26 +01:00
setContent: function(newNoteIds, html) {
2012-11-22 02:56:42 +01:00
NoteList.top_id = newNoteIds.first();
NoteList.bottom_id = newNoteIds.last();
2012-11-22 00:23:26 +01:00
$("#notes-list").html(html);
2012-11-22 02:57:22 +01:00
// for the wall
2012-11-22 02:56:42 +01:00
if (NoteList.reversed) {
2012-11-22 00:23:26 +01:00
// init infinite scrolling
2012-11-22 02:56:42 +01:00
NoteList.initLoadMore();
2012-11-22 00:23:26 +01:00
// init getting new notes
2012-11-22 02:56:42 +01:00
NoteList.initRefreshNew();
2012-11-22 00:23:26 +01:00
}
},
/**
* Handle loading more notes when scrolling to the bottom of the page.
2012-11-22 02:56:42 +01:00
* The id of the last note in the list is in NoteList.bottom_id.
*
* Set up refreshing only new notes after all notes have been loaded.
*/
/**
* Initializes loading more notes when scrolling to the bottom of the page.
*/
2012-11-22 00:23:26 +01:00
initLoadMore: function() {
$(document).endlessScroll({
bottomPixels: 400,
fireDelay: 1000,
fireOnce:true,
ceaseFire: function() {
return NoteList.loading_more_disabled;
},
callback: function(i) {
NoteList.getMore();
}
});
},
/**
* Gets an additional set of notes.
*/
2012-11-22 00:23:26 +01:00
getMore: function() {
// only load more notes if there are no "new" notes
$('.loading').show();
$.ajax({
2012-11-22 02:56:42 +01:00
url: NoteList.notes_path,
data: NoteList.target_params + "&loading_more=1&" + (NoteList.reversed ? "before_id" : "after_id") + "=" + NoteList.bottom_id,
2012-12-03 21:43:17 +01:00
complete: function(){ $('.js-notes-busy').removeClass("loading")},
beforeSend: function() { $('.js-notes-busy').addClass("loading") },
2012-11-22 00:23:26 +01:00
dataType: "script"
});
},
/**
* Called in response to getMore().
* Append notes to #notes-list.
*/
2012-11-22 00:23:26 +01:00
appendMoreNotes: function(newNoteIds, html) {
var lastNewNoteId = newNoteIds.last();
2012-11-22 02:56:42 +01:00
if(lastNewNoteId != NoteList.bottom_id) {
NoteList.bottom_id = lastNewNoteId;
2012-11-22 00:23:26 +01:00
$("#notes-list").append(html);
}
},
2012-02-27 18:05:27 +01:00
/**
* Called in response to getMore().
* Disables loading more notes when scrolling to the bottom of the page.
*/
2012-11-22 00:23:26 +01:00
finishedLoadingMore: function() {
2012-11-22 02:56:42 +01:00
NoteList.loading_more_disabled = true;
2012-11-22 00:23:26 +01:00
// make sure we are up to date
2012-11-22 02:56:42 +01:00
NoteList.updateVotes();
2012-11-22 00:23:26 +01:00
},
/**
* Handle refreshing and adding of new notes.
*
* New notes are all notes that are created after the site has been loaded.
* The "old" notes are in #notes-list the "new" ones will be in #new-notes-list.
2012-11-22 02:56:42 +01:00
* The id of the last "old" note is in NoteList.bottom_id.
*/
/**
* Initializes getting new notes every n seconds.
2012-11-22 02:57:22 +01:00
*
* Note: only used on wall.
*/
2012-11-22 00:23:26 +01:00
initRefreshNew: function() {
setInterval("NoteList.getNew()", 10000);
},
/**
2012-09-14 17:01:34 +02:00
* Gets the new set of notes.
2012-11-22 02:57:22 +01:00
*
* Note: only used on wall.
*/
2012-11-22 00:23:26 +01:00
getNew: function() {
$.ajax({
2012-11-22 02:56:42 +01:00
url: NoteList.notes_path,
data: NoteList.target_params + "&loading_new=1&after_id=" + (NoteList.reversed ? NoteList.top_id : NoteList.bottom_id),
2012-11-22 00:23:26 +01:00
dataType: "script"
});
},
/**
* Called in response to getNew().
* Replaces the content of #new-notes-list with the given html.
2012-11-22 02:57:22 +01:00
*
* Note: only used on wall.
*/
2012-11-22 00:23:26 +01:00
replaceNewNotes: function(newNoteIds, html) {
$("#new-notes-list").html(html);
2012-11-22 02:56:42 +01:00
NoteList.updateVotes();
2012-11-22 00:23:26 +01:00
},
/**
2012-11-22 02:57:22 +01:00
* Adds a single common note to #notes-list.
*/
2012-11-22 00:23:26 +01:00
appendNewNote: function(id, html) {
2012-11-22 02:56:42 +01:00
$("#notes-list").append(html);
NoteList.updateVotes();
2012-11-22 00:23:26 +01:00
},
2012-11-22 02:56:42 +01:00
/**
2012-11-22 02:57:22 +01:00
* Adds a single discussion note to #notes-list.
2012-12-03 20:35:09 +01:00
*
* Also removes the corresponding form.
2012-11-22 02:56:42 +01:00
*/
appendNewDiscussionNote: function(discussionId, diffRowHtml, noteHtml) {
2012-12-03 20:35:09 +01:00
var form = $("form[rel='"+discussionId+"']");
var row = form.closest("tr");
// is this the first note of discussion?
if (row.is(".js-temp-notes-holder")) {
2012-12-03 20:35:09 +01:00
// insert the note and the reply button after the temp row
row.after(diffRowHtml);
2012-12-03 20:35:09 +01:00
// remove the note (will be added again below)
row.next().find(".note").remove();
}
// append new note to all matching discussions
$(".notes[rel='"+discussionId+"']").append(noteHtml);
2012-12-03 20:35:09 +01:00
// cleanup after successfully creating a diff/discussion note
$.proxy(NoteList.removeDiscussionNoteForm, form).call();
},
2012-11-22 02:57:22 +01:00
/**
* Adds a single wall note to #new-notes-list.
*
* Note: only used on wall.
*/
appendNewWallNote: function(id, html) {
$("#new-notes-list").prepend(html);
},
2012-12-03 22:34:50 +01:00
/**
* Called in response the main target form has been successfully submitted.
*
* Removes any errors.
* Resets text and preview.
* Resets buttons.
*/
resetMainTargetForm: function(){
var form = $(this);
// remove validation errors
form.find(".js-errors").remove();
// reset text and preview
var previewContainer = form.find(".js-toggler-container.note_text_and_preview");
if (previewContainer.is(".on")) {
previewContainer.removeClass("on");
}
form.find(".js-note-text").val("").trigger("input");
},
/**
* Called after an attachment file has been selected.
*
* Updates the file name for the selected attachment.
*/
updateFormAttachment: function() {
var form = $(this).closest("form");
// get only the basename
var filename = $(this).val().replace(/^.*[\\\/]/, '');
form.find(".js-attachment-filename").text(filename);
},
/**
* Recalculates the votes and updates them (if they are displayed at all).
*
* Assumes all relevant notes are displayed (i.e. there are no more notes to
* load via getMore()).
* Might produce inaccurate results when not all notes have been loaded and a
* recalculation is triggered (e.g. when deleting a note).
*/
2012-11-22 00:23:26 +01:00
updateVotes: function() {
var votes = $("#votes .votes");
2012-11-22 02:56:42 +01:00
var notes = $("#notes-list .note .vote");
2012-11-22 00:23:26 +01:00
// only update if there is a vote display
if (votes.size()) {
var upvotes = notes.filter(".upvote").size();
var downvotes = notes.filter(".downvote").size();
var votesCount = upvotes + downvotes;
var upvotesPercent = votesCount ? (100.0 / votesCount * upvotes) : 0;
var downvotesPercent = votesCount ? (100.0 - upvotesPercent) : 0;
// change vote bar lengths
votes.find(".bar-success").css("width", upvotesPercent+"%");
votes.find(".bar-danger").css("width", downvotesPercent+"%");
// replace vote numbers
votes.find(".upvotes").text(votes.find(".upvotes").text().replace(/\d+/, upvotes));
votes.find(".downvotes").text(votes.find(".downvotes").text().replace(/\d+/, downvotes));
}
2012-11-22 00:23:26 +01:00
}
};