notes.js 11.7 KB
Newer Older
N
Nihad Abbasov 已提交
1
var NoteList = {
G
gitlabhq 已提交
2

3 4 5 6
  notes_path: null,
  target_params: null,
  target_id: 0,
  target_type: null,
7
  top_id: 0,
8 9
  bottom_id: 0,
  loading_more_disabled: false,
10
  reversed: false,
11

R
Riyad Preukschas 已提交
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
  init: function(tid, tt, path) {
    this.notes_path = path + ".js";
    this.target_id = tid;
    this.target_type = tt;
    this.reversed = $("#notes-list").is(".reversed");
    this.target_params = "target_type=" + this.target_type + "&target_id=" + this.target_id;

    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();
      });
    }
R
Riyad Preukschas 已提交
28

R
Riyad Preukschas 已提交
29 30
    // get initial set of notes
    this.getContent();
R
Riyad Preukschas 已提交
31

R
Riyad Preukschas 已提交
32
    disableButtonIfEmptyField(".js-note-text", ".js-comment-button");
R
Riyad Preukschas 已提交
33

R
Riyad Preukschas 已提交
34 35 36 37 38
    $("#note_attachment").change(function(e){
      var val = $('.input-file').val();
      var filename = val.replace(/^.*[\\\/]/, '');
      $(".file_name").text(filename);
    });
R
Riyad Preukschas 已提交
39

R
Riyad Preukschas 已提交
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 87 88 89
    // 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();
    });+

    $(document).on("click",
                    ".js-add-diff-note-button",
                    NoteList.addDiffNote);

    // reply to diff notes
    $(document).on("click",
                    ".js-discussion-reply-button",
                    NoteList.replyToDiscussionNote);

    // hide diff note form
    $(document).on("click",
                    ".js-close-discussion-note-form",
                    NoteList.removeDiscussionNoteForm);

    // do some specific housekeeping when removing a diff or discussion note
    $(document).on("click",
                    ".diff_file .js-note-delete," +
                    ".discussion .js-note-delete",
                    NoteList.removeDiscussionNote);

    // remove a note (in general)
    $(document).on("click",
                    ".js-note-delete",
                    NoteList.removeNote);

    // clean up previews for forms
    $(document).on("ajax:complete", ".note-form-holder", function(){
      $(this).find('#preview-note').hide();
      $(this).find('#note_note').show();
    });
  },
G
gitlabhq 已提交
90

D
Dmitriy Zaporozhets 已提交
91

R
Riyad Preukschas 已提交
92 93 94 95 96 97 98 99 100 101 102 103 104
  /**
   * Event handlers
   */


  /**
   * 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) {
    // find the form
R
Riyad Preukschas 已提交
105
    var form = $(".js-note-forms .js-discussion-note-form");
R
Riyad Preukschas 已提交
106 107 108 109 110
    var row = $(this).closest("tr");
    var nextRow = row.next();

    // does it already have notes?
    if (nextRow.is(".notes_holder")) {
R
Riyad Preukschas 已提交
111 112
      $.proxy(NoteList.replyToDiscussionNote,
              nextRow.find(".js-discussion-reply-button")
R
Riyad Preukschas 已提交
113 114 115 116 117 118 119
             ).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
R
Riyad Preukschas 已提交
120
      NoteList.setupDiscussionNoteForm($(this), row.next().find("form"));
R
Riyad Preukschas 已提交
121 122 123 124 125 126 127 128 129 130 131 132 133
    }

    e.preventDefault();
  },

  /**
   * Called in response to deleting a note on a diff line.
   *
   * Removes the actual note from view.
   * Removes the whole notes row if the last note for that line is being removed.
   *
   * Note: must be called before removeNote()
   */
R
Riyad Preukschas 已提交
134
  removeDiscussionNote: function() {
R
Riyad Preukschas 已提交
135 136 137 138
    var notes = $(this).closest(".notes");

    // check if this is the last note for this line
    if (notes.find(".note").length === 1) {
R
Riyad Preukschas 已提交
139 140 141 142
      // for discussions
      notes.closest(".discussion").remove();

      // for diff lines
R
Riyad Preukschas 已提交
143 144 145 146 147 148 149 150 151 152
      notes.closest("tr").remove();
    }
  },

  /**
   * 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.
   */
R
Riyad Preukschas 已提交
153
  removeDiscussionNoteForm: function(e) {
R
Riyad Preukschas 已提交
154 155 156 157
    var form = $(this).closest("form");
    var row = form.closest("tr");

    // show the reply button (will only work for replys)
R
Riyad Preukschas 已提交
158
    form.prev(".js-discussion-reply-button").show();
R
Riyad Preukschas 已提交
159 160

    if (row.is(".js-temp-notes-holder")) {
R
Riyad Preukschas 已提交
161
      // remove temporary row for diff lines
R
Riyad Preukschas 已提交
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
      row.remove();
    } else {
      // only remove the form
      form.remove();
    }

    e.preventDefault();
  },

  /**
   * Called in response to deleting a note of any kind.
   *
   * Removes the actual note from view.
   */
  removeNote: function() {
    $(this).closest(".note").remove();
    NoteList.updateVotes();
  },

  /**
   * Called when clicking on the "reply" button for a diff line.
   *
   * Shows the note form below the notes.
   */
R
Riyad Preukschas 已提交
186
  replyToDiscussionNote: function() {
R
Riyad Preukschas 已提交
187
    // find the form
R
Riyad Preukschas 已提交
188
    var form = $(".js-note-forms .js-discussion-note-form");
R
Riyad Preukschas 已提交
189 190 191 192 193 194 195

    // hide reply button
    $(this).hide();
    // insert the form after the button
    form.clone().insertAfter($(this));

    // show the form
R
Riyad Preukschas 已提交
196
    NoteList.setupDiscussionNoteForm($(this), $(this).next("form"));
R
Riyad Preukschas 已提交
197 198 199 200 201 202 203 204 205 206
  },

  /**
   * Shows the diff line form and does some setup.
   *
   * Sets some hidden fields in the form.
   *
   * Note: "this" must have the "discussionId", "lineCode", "noteableType" and
   *       "noteableId" data attributes set.
   */
R
Riyad Preukschas 已提交
207
  setupDiscussionNoteForm: function(data_holder, form) {
R
Riyad Preukschas 已提交
208 209 210 211 212 213 214 215
    // setup note target
    form.attr("rel", data_holder.data("discussionId"));
    form.find("#note_line_code").val(data_holder.data("lineCode"));
    form.find("#note_noteable_type").val(data_holder.data("noteableType"));
    form.find("#note_noteable_id").val(data_holder.data("noteableId"));

    // setup interaction
    disableButtonIfEmptyField(form.find(".js-note-text"), form.find(".js-comment-button"));
R
Riyad Preukschas 已提交
216
    GitLab.GfmAutoComplete.setup();
R
Riyad Preukschas 已提交
217 218

    // cleanup after successfully creating a diff note
R
Riyad Preukschas 已提交
219
    form.on("ajax:success", NoteList.removeDiscussionNoteForm);
R
Riyad Preukschas 已提交
220 221 222 223 224

    form.show();
  },


225
  /**
226 227
   * Handle loading the initial set of notes.
   * And set up loading more notes when scrolling to the bottom of the page.
228
   */
D
Dmitriy Zaporozhets 已提交
229 230


231
  /**
232
   * Gets an inital set of notes.
233
   */
R
Riyad Preukschas 已提交
234 235 236 237 238 239 240 241 242
  getContent: function() {
    $.ajax({
      url: this.notes_path,
      data: this.target_params,
      complete: function(){ $('.notes-status').removeClass("loading")},
      beforeSend: function() { $('.notes-status').addClass("loading") },
      dataType: "script"
    });
  },
D
Dmitriy Zaporozhets 已提交
243

244 245 246 247
  /**
   * Called in response to getContent().
   * Replaces the content of #notes-list with the given html.
   */
R
Riyad Preukschas 已提交
248 249 250 251 252 253 254 255 256 257 258 259 260
  setContent: function(newNoteIds, html) {
    this.top_id = newNoteIds.first();
    this.bottom_id = newNoteIds.last();
    $("#notes-list").html(html);

    if (this.reversed) {
      // init infinite scrolling
      this.initLoadMore();

      // init getting new notes
      this.initRefreshNew();
    }
  },
261 262 263


  /**
264 265
   * 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.
266
   *
267
   * Set up refreshing only new notes after all notes have been loaded.
268
   */
269 270 271 272 273


  /**
   * Initializes loading more notes when scrolling to the bottom of the page.
   */
R
Riyad Preukschas 已提交
274 275 276 277 278 279 280 281 282 283 284 285 286
  initLoadMore: function() {
    $(document).endlessScroll({
      bottomPixels: 400,
      fireDelay: 1000,
      fireOnce:true,
      ceaseFire: function() {
        return NoteList.loading_more_disabled;
      },
      callback: function(i) {
        NoteList.getMore();
      }
    });
  },
287 288 289 290

  /**
   * Gets an additional set of notes.
   */
R
Riyad Preukschas 已提交
291 292 293 294 295 296 297 298 299 300 301
  getMore: function() {
    // only load more notes if there are no "new" notes
    $('.loading').show();
    $.ajax({
      url: this.notes_path,
      data: this.target_params + "&loading_more=1&" + (this.reversed ? "before_id" : "after_id") + "=" + this.bottom_id,
      complete: function(){ $('.notes-status').removeClass("loading")},
      beforeSend: function() { $('.notes-status').addClass("loading") },
      dataType: "script"
    });
  },
302

303 304 305 306
  /**
   * Called in response to getMore().
   * Append notes to #notes-list.
   */
R
Riyad Preukschas 已提交
307 308 309 310 311 312 313
  appendMoreNotes: function(newNoteIds, html) {
    var lastNewNoteId = newNoteIds.last();
    if(lastNewNoteId != this.bottom_id) {
      this.bottom_id = lastNewNoteId;
      $("#notes-list").append(html);
    }
  },
D
Dmitriy Zaporozhets 已提交
314

315 316 317 318 319
  /**
   * Called in response to getMore().
   * Disables loading more notes when scrolling to the bottom of the page.
   * Initalizes refreshing new notes.
   */
R
Riyad Preukschas 已提交
320 321
  finishedLoadingMore: function() {
    this.loading_more_disabled = true;
322

R
Riyad Preukschas 已提交
323 324 325 326 327 328 329
    // from now on only get new notes
    if (!this.reversed) {
      this.initRefreshNew();
    }
    // make sure we are up to date
    this.updateVotes();
  },
330 331 332 333 334 335 336 337 338 339 340 341 342 343


  /**
   * 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.
   */
R
Riyad Preukschas 已提交
344 345 346
  initRefreshNew: function() {
    setInterval("NoteList.getNew()", 10000);
  },
347 348

  /**
349
   * Gets the new set of notes.
350
   */
R
Riyad Preukschas 已提交
351 352 353 354 355 356 357
  getNew: function() {
    $.ajax({
      url: this.notes_path,
      data: this.target_params + "&loading_new=1&after_id=" + (this.reversed ? this.top_id : this.bottom_id),
      dataType: "script"
    });
  },
358 359 360 361 362

  /**
   * Called in response to getNew().
   * Replaces the content of #new-notes-list with the given html.
   */
R
Riyad Preukschas 已提交
363 364 365 366
  replaceNewNotes: function(newNoteIds, html) {
    $("#new-notes-list").html(html);
    this.updateVotes();
  },
367 368 369 370

  /**
   * Adds a single note to #new-notes-list.
   */
R
Riyad Preukschas 已提交
371 372 373 374 375 376 377 378
  appendNewNote: function(id, html) {
    if (this.reversed) {
      $("#notes-list").prepend(html);
    } else {
      $("#notes-list").append(html);
    }
    this.updateVotes();
  },
379

380 381 382 383 384 385 386 387 388 389 390 391 392 393
  appendNewDiscussionNote: function(discussionId, diffRowHtml, noteHtml) {
    // is this the first note of discussion?
    var row = $("form[rel='"+discussionId+"']").closest("tr");
    if (row.is(".js-temp-notes-holder")) {
      // insert the note and the reply button after it
      row.after(diffRowHtml);
      // will be added again below
      row.next().find(".note").remove();
    }

    // append new note to all matching discussions
    $(".notes[rel='"+discussionId+"']").append(noteHtml);
  },

394 395 396 397 398 399 400 401
  /**
   * 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).
   */
R
Riyad Preukschas 已提交
402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419
  updateVotes: function() {
    var votes = $("#votes .votes");
    var notes = $("#notes-list").find(".note .vote");

    // 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));
420
    }
R
Riyad Preukschas 已提交
421
  }
422
};