summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/note.js
blob: c791623b91d06a1a96de88f2f4b751900d736369 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
var NoteList = {

first_id: 0,
last_id: 0,
resource_name: null,

init:
  function(resource_name, first_id, last_id) {
    this.resource_name = resource_name;
    this.first_id = first_id;
    this.last_id = last_id;
    this.initRefresh();
    this.initLoadMore();
  },

getOld:
  function() {
    $('.loading').show();
    $.ajax({
      type: "GET",
      url: location.href,
      data: "first_id=" + this.first_id,
      complete: function(){ $('.loading').hide()},
      dataType: "script"});
  },

append:
  function(id, html) {
    this.first_id = id;
    $("#notes-list").append(html);
    this.initLoadMore();
  },

replace:
  function(fid, lid, html) {
    this.first_id = fid;
    this.last_id = lid;
    $("#notes-list").html(html);
    this.initLoadMore();
  },

prepend:
  function(id, html) {
    if(id != this.last_id) {
      this.last_id = id;
      $("#notes-list").prepend(html);
    }
  },

getNew:
  function() {
    // refersh notes list
    $.ajax({
      type: "GET",
      url: location.href,
      data: "last_id=" + this.last_id,
      dataType: "script"});
  },

refresh:
  function() {
    // refersh notes list
    $.ajax({
      type: "GET",
      url: location.href,
      data: "first_id=" + this.first_id + "&last_id=" + this.last_id,
      dataType: "script"});
  },

initRefresh:
  function() {
    // init timer
    var intNew = setInterval("NoteList.getNew()", 15000);
    var intRefresh = setInterval("NoteList.refresh()", 90000);
  },

initLoadMore:
  function() {
    $(window).bind('scroll', function(){
      if($(window).scrollTop() == $(document).height() - $(window).height()){
        $(window).unbind('scroll');
        NoteList.getOld();
      }
    });
  }
}