2011-11-15 09:34:30 +01:00
|
|
|
var NoteList = {
|
2011-11-04 14:37:38 +01:00
|
|
|
|
2012-08-30 20:31:55 +02:00
|
|
|
notes_path: null,
|
|
|
|
target_params: null,
|
|
|
|
target_id: 0,
|
|
|
|
target_type: null,
|
2012-09-14 17:01:34 +02:00
|
|
|
top_id: 0,
|
2012-09-14 16:52:24 +02:00
|
|
|
bottom_id: 0,
|
|
|
|
loading_more_disabled: false,
|
2012-09-14 17:01:34 +02:00
|
|
|
reversed: false,
|
2012-08-30 20:31:55 +02:00
|
|
|
|
|
|
|
init:
|
|
|
|
function(tid, tt, path) {
|
|
|
|
this.notes_path = path + ".js";
|
|
|
|
this.target_id = tid;
|
|
|
|
this.target_type = tt;
|
2012-11-14 14:36:51 +01:00
|
|
|
this.reversed = $("#notes-list").is(".reversed");
|
|
|
|
this.target_params = "target_type=" + this.target_type + "&target_id=" + this.target_id;
|
2012-08-30 20:31:55 +02:00
|
|
|
|
2012-09-14 16:52:24 +02:00
|
|
|
// get initial set of notes
|
2012-08-30 20:31:55 +02:00
|
|
|
this.getContent();
|
|
|
|
|
2012-09-14 01:32:55 +02:00
|
|
|
$("#notes-list, #new-notes-list").on("ajax:success", ".delete-note", function() {
|
2012-09-15 11:55:17 +02:00
|
|
|
$(this).closest('li').fadeOut(function() {
|
|
|
|
$(this).remove();
|
|
|
|
NoteList.updateVotes();
|
|
|
|
});
|
2012-09-14 01:32:55 +02:00
|
|
|
});
|
2012-08-30 20:31:55 +02:00
|
|
|
|
2012-09-14 16:52:24 +02:00
|
|
|
$(".note-form-holder").on("ajax:before", function(){
|
2012-09-15 11:55:17 +02:00
|
|
|
$(".submit_note").disable();
|
2012-08-30 20:31:55 +02:00
|
|
|
})
|
|
|
|
|
2012-09-14 16:52:24 +02:00
|
|
|
$(".note-form-holder").on("ajax:complete", function(){
|
2012-09-15 11:55:17 +02:00
|
|
|
$(".submit_note").enable();
|
2012-11-14 14:36:51 +01:00
|
|
|
$('#preview-note').hide();
|
|
|
|
$('#note_note').show();
|
2012-08-30 20:31:55 +02:00
|
|
|
})
|
|
|
|
|
2012-09-09 11:40:22 +02:00
|
|
|
disableButtonIfEmptyField(".note-text", ".submit_note");
|
2012-08-30 20:31:55 +02:00
|
|
|
|
|
|
|
$("#note_attachment").change(function(e){
|
2012-03-24 17:57:01 +01:00
|
|
|
var val = $('.input-file').val();
|
2012-03-24 18:01:38 +01:00
|
|
|
var filename = val.replace(/^.*[\\\/]/, '');
|
2012-03-24 17:53:16 +01:00
|
|
|
$(".file_name").text(filename);
|
2012-08-30 20:31:55 +02:00
|
|
|
});
|
2012-09-18 07:54:17 +02:00
|
|
|
|
|
|
|
if(this.reversed) {
|
|
|
|
var textarea = $(".note-text");
|
|
|
|
$('.note_advanced_opts').hide();
|
|
|
|
textarea.css("height", "40px");
|
|
|
|
textarea.on("focus", function(){
|
|
|
|
$(this).css("height", "80px");
|
|
|
|
$('.note_advanced_opts').show();
|
|
|
|
});
|
|
|
|
}
|
2012-11-14 14:36:51 +01:00
|
|
|
|
|
|
|
// Setup note preview
|
|
|
|
$(document).on('click', '#preview-link', function(e) {
|
|
|
|
$('#preview-note').text('Loading...');
|
|
|
|
|
|
|
|
$(this).text($(this).text() === "Edit" ? "Preview" : "Edit");
|
|
|
|
|
|
|
|
var note_text = $('#note_note').val();
|
|
|
|
|
|
|
|
if(note_text.trim().length === 0) {
|
|
|
|
$('#preview-note').text('Nothing to preview.');
|
|
|
|
} else {
|
|
|
|
$.post($(this).attr('href'), {note: note_text}).success(function(data) {
|
|
|
|
$('#preview-note').html(data);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
$('#preview-note, #note_note').toggle();
|
|
|
|
e.preventDefault();
|
|
|
|
});
|
2012-08-30 20:31:55 +02:00
|
|
|
},
|
2011-11-04 14:37:38 +01:00
|
|
|
|
2012-02-27 18:05:27 +01:00
|
|
|
|
2012-08-30 20:31:55 +02:00
|
|
|
/**
|
2012-09-14 16:52:24 +02:00
|
|
|
* Handle loading the initial set of notes.
|
|
|
|
* And set up loading more notes when scrolling to the bottom of the page.
|
2012-08-30 20:31:55 +02:00
|
|
|
*/
|
2012-02-27 18:05:27 +01:00
|
|
|
|
|
|
|
|
2012-08-30 20:31:55 +02:00
|
|
|
/**
|
2012-09-14 16:52:24 +02:00
|
|
|
* Gets an inital set of notes.
|
2012-08-30 20:31:55 +02:00
|
|
|
*/
|
2012-09-14 16:52:24 +02:00
|
|
|
getContent:
|
|
|
|
function() {
|
2012-08-30 20:31:55 +02:00
|
|
|
$.ajax({
|
|
|
|
type: "GET",
|
2012-02-27 18:05:27 +01:00
|
|
|
url: this.notes_path,
|
2012-11-14 14:36:51 +01:00
|
|
|
data: this.target_params,
|
2012-09-13 02:12:44 +02:00
|
|
|
complete: function(){ $('.notes-status').removeClass("loading")},
|
|
|
|
beforeSend: function() { $('.notes-status').addClass("loading") },
|
2012-02-27 18:05:27 +01:00
|
|
|
dataType: "script"});
|
2012-08-30 20:31:55 +02:00
|
|
|
},
|
2012-02-27 18:05:27 +01:00
|
|
|
|
2012-09-14 16:52:24 +02:00
|
|
|
/**
|
|
|
|
* Called in response to getContent().
|
|
|
|
* Replaces the content of #notes-list with the given html.
|
|
|
|
*/
|
2012-08-30 20:31:55 +02:00
|
|
|
setContent:
|
2012-09-14 17:01:34 +02:00
|
|
|
function(first_id, last_id, html) {
|
|
|
|
this.top_id = first_id;
|
2012-09-14 16:52:24 +02:00
|
|
|
this.bottom_id = last_id;
|
2012-02-27 18:05:27 +01:00
|
|
|
$("#notes-list").html(html);
|
|
|
|
|
2012-09-14 17:01:34 +02:00
|
|
|
// init infinite scrolling
|
2012-02-27 18:05:27 +01:00
|
|
|
this.initLoadMore();
|
2012-09-14 17:01:34 +02:00
|
|
|
|
|
|
|
// init getting new notes
|
|
|
|
if (this.reversed) {
|
|
|
|
this.initRefreshNew();
|
|
|
|
}
|
2012-08-30 20:31:55 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2012-09-14 16:52:24 +02:00
|
|
|
* Handle loading more notes when scrolling to the bottom of the page.
|
|
|
|
* The id of the last note in the list is in this.bottom_id.
|
2012-08-30 20:31:55 +02:00
|
|
|
*
|
2012-09-14 16:52:24 +02:00
|
|
|
* Set up refreshing only new notes after all notes have been loaded.
|
2012-08-30 20:31:55 +02:00
|
|
|
*/
|
2012-09-14 16:52:24 +02:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initializes loading more notes when scrolling to the bottom of the page.
|
|
|
|
*/
|
|
|
|
initLoadMore:
|
2012-08-30 20:31:55 +02:00
|
|
|
function() {
|
2012-09-14 16:52:24 +02:00
|
|
|
$(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.
|
|
|
|
*/
|
|
|
|
getMore:
|
|
|
|
function() {
|
|
|
|
// only load more notes if there are no "new" notes
|
2012-08-30 20:31:55 +02:00
|
|
|
$('.loading').show();
|
|
|
|
$.ajax({
|
|
|
|
type: "GET",
|
|
|
|
url: this.notes_path,
|
2012-11-14 14:36:51 +01:00
|
|
|
data: this.target_params + "&loading_more=1&" + (this.reversed ? "before_id" : "after_id") + "=" + this.bottom_id,
|
2012-09-13 02:12:44 +02:00
|
|
|
complete: function(){ $('.notes-status').removeClass("loading")},
|
|
|
|
beforeSend: function() { $('.notes-status').addClass("loading") },
|
2012-08-30 20:31:55 +02:00
|
|
|
dataType: "script"});
|
|
|
|
},
|
|
|
|
|
2012-09-14 16:52:24 +02:00
|
|
|
/**
|
|
|
|
* Called in response to getMore().
|
|
|
|
* Append notes to #notes-list.
|
|
|
|
*/
|
|
|
|
appendMoreNotes:
|
2012-08-30 20:31:55 +02:00
|
|
|
function(id, html) {
|
2012-09-14 16:52:24 +02:00
|
|
|
if(id != this.bottom_id) {
|
|
|
|
this.bottom_id = id;
|
2012-08-30 20:31:55 +02:00
|
|
|
$("#notes-list").append(html);
|
|
|
|
}
|
|
|
|
},
|
2012-02-27 18:05:27 +01:00
|
|
|
|
2012-09-14 16:52:24 +02:00
|
|
|
/**
|
|
|
|
* Called in response to getMore().
|
|
|
|
* Disables loading more notes when scrolling to the bottom of the page.
|
|
|
|
* Initalizes refreshing new notes.
|
|
|
|
*/
|
|
|
|
finishedLoadingMore:
|
2012-08-30 20:31:55 +02:00
|
|
|
function() {
|
2012-09-14 16:52:24 +02:00
|
|
|
this.loading_more_disabled = true;
|
|
|
|
|
|
|
|
// from now on only get new notes
|
2012-09-14 17:01:34 +02:00
|
|
|
if (!this.reversed) {
|
|
|
|
this.initRefreshNew();
|
|
|
|
}
|
2012-09-15 11:55:17 +02:00
|
|
|
// make sure we are up to date
|
|
|
|
this.updateVotes();
|
2012-09-14 16:52:24 +02: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.
|
|
|
|
* The id of the last "old" note is in this.bottom_id.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initializes getting new notes every n seconds.
|
|
|
|
*/
|
|
|
|
initRefreshNew:
|
|
|
|
function() {
|
|
|
|
setInterval("NoteList.getNew()", 10000);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2012-09-14 17:01:34 +02:00
|
|
|
* Gets the new set of notes.
|
2012-09-14 16:52:24 +02:00
|
|
|
*/
|
|
|
|
getNew:
|
|
|
|
function() {
|
|
|
|
$.ajax({
|
|
|
|
type: "GET",
|
|
|
|
url: this.notes_path,
|
2012-11-14 14:36:51 +01:00
|
|
|
data: this.target_params + "&loading_new=1&after_id=" + (this.reversed ? this.top_id : this.bottom_id),
|
2012-09-14 16:52:24 +02:00
|
|
|
dataType: "script"});
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Called in response to getNew().
|
|
|
|
* Replaces the content of #new-notes-list with the given html.
|
|
|
|
*/
|
|
|
|
replaceNewNotes:
|
|
|
|
function(html) {
|
|
|
|
$("#new-notes-list").html(html);
|
2012-09-15 11:55:17 +02:00
|
|
|
this.updateVotes();
|
2012-09-14 16:52:24 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds a single note to #new-notes-list.
|
|
|
|
*/
|
|
|
|
appendNewNote:
|
|
|
|
function(id, html) {
|
2012-09-14 17:01:34 +02:00
|
|
|
if (this.reversed) {
|
|
|
|
$("#new-notes-list").prepend(html);
|
|
|
|
} else {
|
2012-09-14 16:52:24 +02:00
|
|
|
$("#new-notes-list").append(html);
|
2011-11-04 14:37:38 +01:00
|
|
|
}
|
2012-09-15 11:55:17 +02:00
|
|
|
this.updateVotes();
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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).
|
|
|
|
*/
|
|
|
|
updateVotes:
|
|
|
|
function() {
|
|
|
|
var votes = $("#votes .votes");
|
2012-10-10 12:09:45 +02:00
|
|
|
var notes = $("#notes-list, #new-notes-list").find(".note .vote");
|
2012-09-15 11:55:17 +02: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-08-30 20:31:55 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-09-14 16:52:24 +02:00
|
|
|
var PerLineNotes = {
|
2012-08-30 20:31:55 +02:00
|
|
|
init:
|
|
|
|
function() {
|
2012-09-14 01:32:55 +02:00
|
|
|
/**
|
|
|
|
* Called when clicking on the "add note" or "reply" button for a diff line.
|
|
|
|
*
|
|
|
|
* Shows the note form below the line.
|
|
|
|
* Sets some hidden fields in the form.
|
|
|
|
*/
|
|
|
|
$(".diff_file_content").on("click", ".line_note_link, .line_note_reply_link", function(e) {
|
2012-08-30 20:31:55 +02:00
|
|
|
var form = $(".per_line_form");
|
|
|
|
$(this).closest("tr").after(form);
|
2012-09-14 01:32:55 +02:00
|
|
|
form.find("#note_line_code").val($(this).data("lineCode"));
|
2012-08-30 20:31:55 +02:00
|
|
|
form.show();
|
2012-11-14 14:36:51 +01:00
|
|
|
e.preventDefault();
|
2012-08-30 20:31:55 +02:00
|
|
|
});
|
2012-09-14 01:32:55 +02:00
|
|
|
|
2012-09-09 11:40:22 +02:00
|
|
|
disableButtonIfEmptyField(".line-note-text", ".submit_inline_note");
|
2012-09-14 01:32:55 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Called in response to successfully deleting a note on a diff line.
|
|
|
|
*
|
|
|
|
* Removes the actual note from view.
|
|
|
|
* Removes the reply button if the last note for that line has been removed.
|
|
|
|
*/
|
|
|
|
$(".diff_file_content").on("ajax:success", ".delete-note", function() {
|
|
|
|
var trNote = $(this).closest("tr");
|
|
|
|
trNote.fadeOut(function() {
|
|
|
|
$(this).remove();
|
|
|
|
});
|
|
|
|
|
|
|
|
// check if this is the last note for this line
|
|
|
|
// elements must really be removed for this to work reliably
|
|
|
|
var trLine = trNote.prev();
|
|
|
|
var trRpl = trNote.next();
|
2012-11-14 14:36:51 +01:00
|
|
|
if (trLine.is(".line_holder") && trRpl.is(".reply")) {
|
2012-09-14 01:32:55 +02:00
|
|
|
trRpl.fadeOut(function() { $(this).remove(); });
|
|
|
|
}
|
|
|
|
});
|
2012-08-30 20:31:55 +02:00
|
|
|
}
|
2011-11-04 14:37:38 +01:00
|
|
|
}
|