note.js 6.07 KB
Newer Older
1
var NoteList = {
gitlabhq's avatar
gitlabhq committed
2

3 4 5 6
  notes_path: null,
  target_params: null,
  target_id: 0,
  target_type: null,
7 8
  bottom_id: 0,
  loading_more_disabled: false,
9 10 11 12 13 14 15 16

  init:
    function(tid, tt, path) {
      this.notes_path = path + ".js";
      this.target_id = tid;
      this.target_type = tt;
      this.target_params = "&target_type=" + this.target_type + "&target_id=" + this.target_id;

17
      // get initial set of notes
18 19
      this.getContent();

20 21 22
      $("#notes-list, #new-notes-list").on("ajax:success", ".delete-note", function() {
        $(this).closest('li').fadeOut();
      });
23

24
      $(".note-form-holder").on("ajax:before", function(){
25
        $(".submit_note").disable()
26 27
      })

28
      $(".note-form-holder").on("ajax:complete", function(){
29
        $(".submit_note").enable()
30 31
      })

32
      disableButtonIfEmptyField(".note-text", ".submit_note");
33

34
      $(".note-text").on("focus", function(){
35 36 37 38 39
        $(this).css("height", "80px");
        $('.note_advanced_opts').show();
      });

      $("#note_attachment").change(function(e){
40
        var val = $('.input-file').val();
41
        var filename = val.replace(/^.*[\\\/]/, '');
42
        $(".file_name").text(filename);
43 44
      });
    },
gitlabhq's avatar
gitlabhq committed
45

Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
46

47
  /**
48 49
   * Handle loading the initial set of notes.
   * And set up loading more notes when scrolling to the bottom of the page.
50
   */
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
51 52


53
  /**
54
   * Gets an inital set of notes.
55
   */
56 57
  getContent:
    function() {
58 59
      $.ajax({
        type: "GET",
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
60 61
      url: this.notes_path,
      data: "?" + this.target_params,
62 63
      complete: function(){ $('.notes-status').removeClass("loading")},
      beforeSend: function() { $('.notes-status').addClass("loading") },
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
64
      dataType: "script"});
65
    },
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
66

67 68 69 70
  /**
   * Called in response to getContent().
   * Replaces the content of #notes-list with the given html.
   */
71
  setContent:
72 73
    function(last_id, html) {
      this.bottom_id = last_id;
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
74 75 76 77
      $("#notes-list").html(html);

      // Init infinite scrolling
      this.initLoadMore();
78 79 80 81
    },


  /**
82 83
   * 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.
84
   *
85
   * Set up refreshing only new notes after all notes have been loaded.
86
   */
87 88 89 90 91 92


  /**
   * Initializes loading more notes when scrolling to the bottom of the page.
   */
  initLoadMore:
93
    function() {
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
      $(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
113 114 115 116
      $('.loading').show();
      $.ajax({
        type: "GET",
        url: this.notes_path,
117
        data: "loading_more=1&after_id=" + this.bottom_id + this.target_params,
118 119
        complete: function(){ $('.notes-status').removeClass("loading")},
        beforeSend: function() { $('.notes-status').addClass("loading") },
120 121 122
        dataType: "script"});
    },

123 124 125 126 127
  /**
   * Called in response to getMore().
   * Append notes to #notes-list.
   */
  appendMoreNotes:
128
    function(id, html) {
129 130
      if(id != this.bottom_id) {
        this.bottom_id = id;
131 132 133
        $("#notes-list").append(html);
      }
    },
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
134

135 136 137 138 139 140
  /**
   * Called in response to getMore().
   * Disables loading more notes when scrolling to the bottom of the page.
   * Initalizes refreshing new notes.
   */
  finishedLoadingMore:
141
    function() {
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
      this.loading_more_disabled = true;

      // from now on only get new notes
      this.initRefreshNew();
    },


  /**
   * 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);
    },

  /**
   * Gets the new set of notes (i.e. all notes after ).
   */
  getNew:
    function() {
      $.ajax({
        type: "GET",
      url: this.notes_path,
      data: "loading_new=1&after_id=" + this.bottom_id + this.target_params,
      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);
    },

  /**
   * Adds a single note to #new-notes-list.
   */
  appendNewNote:
    function(id, html) {
      if(id != this.bottom_id) {
        $("#new-notes-list").append(html);
gitlabhq's avatar
gitlabhq committed
194
      }
195 196 197
    }
};

198
var PerLineNotes = {
199 200
  init:
    function() {
201 202 203 204 205 206 207
      /**
       * 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) {
208 209
        var form = $(".per_line_form");
        $(this).closest("tr").after(form);
210
        form.find("#note_line_code").val($(this).data("lineCode"));
211 212 213
        form.show();
        return false;
      });
214

215
      disableButtonIfEmptyField(".line-note-text", ".submit_inline_note");
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236

      /**
       * 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();
        if (trLine.hasClass("line_holder") && trRpl.hasClass("reply")) {
          trRpl.fadeOut(function() { $(this).remove(); });
        }
      });
237
    }
gitlabhq's avatar
gitlabhq committed
238
}