gitlabhq/app/assets/javascripts/note.js

170 lines
3.5 KiB
JavaScript
Raw Normal View History

2011-11-15 09:34:30 +01:00
var NoteList = {
2011-11-04 14:37:38 +01:00
2012-02-24 08:16:06 +01:00
notes_path: null,
target_params: null,
target_id: 0,
target_type: null,
2011-11-04 14:37:38 +01:00
first_id: 0,
last_id: 0,
2012-02-12 22:52:27 +01:00
disable:false,
2011-11-04 14:37:38 +01:00
2011-11-15 09:34:30 +01:00
init:
2012-02-24 08:16:06 +01:00
function(tid, tt, path) {
this.notes_path = path + ".js";
this.target_id = tid;
this.target_type = tt;
2012-02-27 18:05:27 +01:00
this.target_params = "&target_type=" + this.target_type + "&target_id=" + this.target_id;
// get notes
this.getContent();
// get new notes every n seconds
2011-11-04 14:37:38 +01:00
this.initRefresh();
2012-02-27 18:05:27 +01:00
$('.delete-note').live('ajax:success', function() {
$(this).closest('li').fadeOut(); });
$("#new_note").live("ajax:before", function(){
$("#submit_note").attr("disabled", "disabled");
})
$("#new_note").live("ajax:complete", function(){
$("#submit_note").removeAttr("disabled");
})
$("#note_note").live("click", function(){
$(this).css("height", "100px");
$('.attach_holder').show();
});
$("#note_attachment").change(function(e){
var val = $('.input-file').val();
2012-03-24 18:01:38 +01:00
var filename = val.replace(/^.*[\\\/]/, '');
$(".file_name").text(filename);
});
2011-11-04 14:37:38 +01:00
},
2012-02-27 18:05:27 +01:00
/**
* Load new notes to fresh list called 'new_notes_list':
* - Replace 'new_notes_list' with new list every n seconds
* - Append new notes to this list after submit
*/
initRefresh:
function() {
// init timer
var intNew = setInterval("NoteList.getNew()", 10000);
2011-11-04 14:37:38 +01:00
},
2011-11-15 09:34:30 +01:00
replace:
2012-02-27 18:05:27 +01:00
function(html) {
$("#new_notes_list").html(html);
2011-11-05 12:59:43 +01:00
},
2011-11-04 14:37:38 +01:00
prepend:
function(id, html) {
2011-11-15 09:34:30 +01:00
if(id != this.last_id) {
2012-02-27 18:05:27 +01:00
$("#new_notes_list").prepend(html);
2011-11-06 22:32:37 +01:00
}
2011-11-04 14:37:38 +01:00
},
getNew:
2011-11-15 09:34:30 +01:00
function() {
2011-11-04 14:37:38 +01:00
// refersh notes list
$.ajax({
type: "GET",
2012-02-24 08:16:06 +01:00
url: this.notes_path,
data: "last_id=" + this.last_id + this.target_params,
2011-11-04 14:37:38 +01:00
dataType: "script"});
},
2011-11-05 12:59:43 +01:00
refresh:
2011-11-15 09:34:30 +01:00
function() {
2011-11-05 12:59:43 +01:00
// refersh notes list
$.ajax({
type: "GET",
2012-02-24 08:16:06 +01:00
url: this.notes_path,
data: "first_id=" + this.first_id + "&last_id=" + this.last_id + this.target_params,
2011-11-05 12:59:43 +01:00
dataType: "script"});
},
2012-02-27 18:05:27 +01:00
/**
* Init load of notes:
* 1. Get content with ajax call
* 2. Set content of notes list with loaded one
*/
getContent:
function() {
$.ajax({
type: "GET",
url: this.notes_path,
data: "?" + this.target_params,
2012-02-27 19:44:13 +01:00
complete: function(){ $('.status').removeClass("loading")},
beforeSend: function() { $('.status').addClass("loading") },
2012-02-27 18:05:27 +01:00
dataType: "script"});
},
setContent:
function(fid, lid, html) {
this.last_id = lid;
this.first_id = fid;
$("#notes-list").html(html);
// Init infinite scrolling
this.initLoadMore();
},
/**
* Paging for old notes when scroll to bottom:
* 1. Init scroll events with 'initLoadMore'
* 2. Load onlder notes with 'getOld' method
* 3. append old notes to bottom of list with 'append'
*
*/
getOld:
2011-11-04 14:37:38 +01:00
function() {
2012-02-27 18:05:27 +01:00
$('.loading').show();
$.ajax({
type: "GET",
url: this.notes_path,
data: "first_id=" + this.first_id + this.target_params,
complete: function(){ $('.status').removeClass("loading")},
beforeSend: function() { $('.status').addClass("loading") },
dataType: "script"});
},
append:
function(id, html) {
if(this.first_id == id) {
this.disable = true;
} else {
this.first_id = id;
$("#notes-list").append(html);
}
2011-11-04 14:37:38 +01:00
},
2012-02-27 18:05:27 +01:00
2011-11-04 14:37:38 +01:00
initLoadMore:
2011-11-15 09:34:30 +01:00
function() {
2012-02-12 22:52:27 +01:00
$(document).endlessScroll({
bottomPixels: 400,
fireDelay: 1000,
fireOnce:true,
ceaseFire: function() {
return NoteList.disable;
},
callback: function(i) {
2011-11-04 14:37:38 +01:00
NoteList.getOld();
}
2012-02-12 22:52:27 +01:00
});
2011-11-04 14:37:38 +01:00
}
}