Commit fac4e3f2e5ea62ed4b92722f5eb1a650ccccf4b1
1 parent
d9b15fc1
Exists in
master
and in
4 other branches
Reformat notes JS
Showing
1 changed file
with
177 additions
and
189 deletions
Show diff stats
app/assets/javascripts/notes.js
| @@ -9,85 +9,84 @@ var NoteList = { | @@ -9,85 +9,84 @@ var NoteList = { | ||
| 9 | loading_more_disabled: false, | 9 | loading_more_disabled: false, |
| 10 | reversed: false, | 10 | reversed: false, |
| 11 | 11 | ||
| 12 | - init: | ||
| 13 | - function(tid, tt, path) { | ||
| 14 | - this.notes_path = path + ".js"; | ||
| 15 | - this.target_id = tid; | ||
| 16 | - this.target_type = tt; | ||
| 17 | - this.reversed = $("#notes-list").is(".reversed"); | ||
| 18 | - this.target_params = "target_type=" + this.target_type + "&target_id=" + this.target_id; | ||
| 19 | - | ||
| 20 | - if(this.reversed) { | ||
| 21 | - var textarea = $(".note-text"); | ||
| 22 | - $('.note_advanced_opts').hide(); | ||
| 23 | - textarea.css("height", "40px"); | ||
| 24 | - textarea.on("focus", function(){ | ||
| 25 | - $(this).css("height", "80px"); | ||
| 26 | - $('.note_advanced_opts').show(); | ||
| 27 | - }); | ||
| 28 | - } | 12 | + init: function(tid, tt, path) { |
| 13 | + this.notes_path = path + ".js"; | ||
| 14 | + this.target_id = tid; | ||
| 15 | + this.target_type = tt; | ||
| 16 | + this.reversed = $("#notes-list").is(".reversed"); | ||
| 17 | + this.target_params = "target_type=" + this.target_type + "&target_id=" + this.target_id; | ||
| 18 | + | ||
| 19 | + if(this.reversed) { | ||
| 20 | + var textarea = $(".note-text"); | ||
| 21 | + $('.note_advanced_opts').hide(); | ||
| 22 | + textarea.css("height", "40px"); | ||
| 23 | + textarea.on("focus", function(){ | ||
| 24 | + $(this).css("height", "80px"); | ||
| 25 | + $('.note_advanced_opts').show(); | ||
| 26 | + }); | ||
| 27 | + } | ||
| 29 | 28 | ||
| 30 | - // get initial set of notes | ||
| 31 | - this.getContent(); | 29 | + // get initial set of notes |
| 30 | + this.getContent(); | ||
| 32 | 31 | ||
| 33 | - disableButtonIfEmptyField(".js-note-text", ".js-comment-button"); | 32 | + disableButtonIfEmptyField(".js-note-text", ".js-comment-button"); |
| 34 | 33 | ||
| 35 | - $("#note_attachment").change(function(e){ | ||
| 36 | - var val = $('.input-file').val(); | ||
| 37 | - var filename = val.replace(/^.*[\\\/]/, ''); | ||
| 38 | - $(".file_name").text(filename); | ||
| 39 | - }); | 34 | + $("#note_attachment").change(function(e){ |
| 35 | + var val = $('.input-file').val(); | ||
| 36 | + var filename = val.replace(/^.*[\\\/]/, ''); | ||
| 37 | + $(".file_name").text(filename); | ||
| 38 | + }); | ||
| 40 | 39 | ||
| 41 | - // Setup note preview | ||
| 42 | - $(document).on('click', '#preview-link', function(e) { | ||
| 43 | - $('#preview-note').text('Loading...'); | ||
| 44 | - | ||
| 45 | - $(this).text($(this).text() === "Edit" ? "Preview" : "Edit"); | ||
| 46 | - | ||
| 47 | - var note_text = $('#note_note').val(); | ||
| 48 | - | ||
| 49 | - if(note_text.trim().length === 0) { | ||
| 50 | - $('#preview-note').text('Nothing to preview.'); | ||
| 51 | - } else { | ||
| 52 | - $.post($(this).attr('href'), {note: note_text}).success(function(data) { | ||
| 53 | - $('#preview-note').html(data); | ||
| 54 | - }); | ||
| 55 | - } | ||
| 56 | - | ||
| 57 | - $('#preview-note, #note_note').toggle(); | ||
| 58 | - });+ | ||
| 59 | - | ||
| 60 | - $(document).on("click", | ||
| 61 | - ".js-add-diff-note-button", | ||
| 62 | - NoteList.addDiffNote); | ||
| 63 | - | ||
| 64 | - // reply to diff notes | ||
| 65 | - $(document).on("click", | ||
| 66 | - ".js-discussion-reply-button", | ||
| 67 | - NoteList.replyToDiscussionNote); | ||
| 68 | - | ||
| 69 | - // hide diff note form | ||
| 70 | - $(document).on("click", | ||
| 71 | - ".js-close-discussion-note-form", | ||
| 72 | - NoteList.removeDiscussionNoteForm); | ||
| 73 | - | ||
| 74 | - // do some specific housekeeping when removing a diff or discussion note | ||
| 75 | - $(document).on("click", | ||
| 76 | - ".diff_file .js-note-delete," + | ||
| 77 | - ".discussion .js-note-delete", | ||
| 78 | - NoteList.removeDiscussionNote); | ||
| 79 | - | ||
| 80 | - // remove a note (in general) | ||
| 81 | - $(document).on("click", | ||
| 82 | - ".js-note-delete", | ||
| 83 | - NoteList.removeNote); | ||
| 84 | - | ||
| 85 | - // clean up previews for forms | ||
| 86 | - $(document).on("ajax:complete", ".note-form-holder", function(){ | ||
| 87 | - $(this).find('#preview-note').hide(); | ||
| 88 | - $(this).find('#note_note').show(); | ||
| 89 | - }); | ||
| 90 | - }, | 40 | + // Setup note preview |
| 41 | + $(document).on('click', '#preview-link', function(e) { | ||
| 42 | + $('#preview-note').text('Loading...'); | ||
| 43 | + | ||
| 44 | + $(this).text($(this).text() === "Edit" ? "Preview" : "Edit"); | ||
| 45 | + | ||
| 46 | + var note_text = $('#note_note').val(); | ||
| 47 | + | ||
| 48 | + if(note_text.trim().length === 0) { | ||
| 49 | + $('#preview-note').text('Nothing to preview.'); | ||
| 50 | + } else { | ||
| 51 | + $.post($(this).attr('href'), {note: note_text}).success(function(data) { | ||
| 52 | + $('#preview-note').html(data); | ||
| 53 | + }); | ||
| 54 | + } | ||
| 55 | + | ||
| 56 | + $('#preview-note, #note_note').toggle(); | ||
| 57 | + });+ | ||
| 58 | + | ||
| 59 | + $(document).on("click", | ||
| 60 | + ".js-add-diff-note-button", | ||
| 61 | + NoteList.addDiffNote); | ||
| 62 | + | ||
| 63 | + // reply to diff notes | ||
| 64 | + $(document).on("click", | ||
| 65 | + ".js-discussion-reply-button", | ||
| 66 | + NoteList.replyToDiscussionNote); | ||
| 67 | + | ||
| 68 | + // hide diff note form | ||
| 69 | + $(document).on("click", | ||
| 70 | + ".js-close-discussion-note-form", | ||
| 71 | + NoteList.removeDiscussionNoteForm); | ||
| 72 | + | ||
| 73 | + // do some specific housekeeping when removing a diff or discussion note | ||
| 74 | + $(document).on("click", | ||
| 75 | + ".diff_file .js-note-delete," + | ||
| 76 | + ".discussion .js-note-delete", | ||
| 77 | + NoteList.removeDiscussionNote); | ||
| 78 | + | ||
| 79 | + // remove a note (in general) | ||
| 80 | + $(document).on("click", | ||
| 81 | + ".js-note-delete", | ||
| 82 | + NoteList.removeNote); | ||
| 83 | + | ||
| 84 | + // clean up previews for forms | ||
| 85 | + $(document).on("ajax:complete", ".note-form-holder", function(){ | ||
| 86 | + $(this).find('#preview-note').hide(); | ||
| 87 | + $(this).find('#note_note').show(); | ||
| 88 | + }); | ||
| 89 | + }, | ||
| 91 | 90 | ||
| 92 | 91 | ||
| 93 | /** | 92 | /** |
| @@ -232,35 +231,33 @@ var NoteList = { | @@ -232,35 +231,33 @@ var NoteList = { | ||
| 232 | /** | 231 | /** |
| 233 | * Gets an inital set of notes. | 232 | * Gets an inital set of notes. |
| 234 | */ | 233 | */ |
| 235 | - getContent: | ||
| 236 | - function() { | ||
| 237 | - $.ajax({ | ||
| 238 | - url: this.notes_path, | ||
| 239 | - data: this.target_params, | ||
| 240 | - complete: function(){ $('.notes-status').removeClass("loading")}, | ||
| 241 | - beforeSend: function() { $('.notes-status').addClass("loading") }, | ||
| 242 | - dataType: "script" | ||
| 243 | - }); | ||
| 244 | - }, | 234 | + getContent: function() { |
| 235 | + $.ajax({ | ||
| 236 | + url: this.notes_path, | ||
| 237 | + data: this.target_params, | ||
| 238 | + complete: function(){ $('.notes-status').removeClass("loading")}, | ||
| 239 | + beforeSend: function() { $('.notes-status').addClass("loading") }, | ||
| 240 | + dataType: "script" | ||
| 241 | + }); | ||
| 242 | + }, | ||
| 245 | 243 | ||
| 246 | /** | 244 | /** |
| 247 | * Called in response to getContent(). | 245 | * Called in response to getContent(). |
| 248 | * Replaces the content of #notes-list with the given html. | 246 | * Replaces the content of #notes-list with the given html. |
| 249 | */ | 247 | */ |
| 250 | - setContent: | ||
| 251 | - function(newNoteIds, html) { | ||
| 252 | - this.top_id = newNoteIds.first(); | ||
| 253 | - this.bottom_id = newNoteIds.last(); | ||
| 254 | - $("#notes-list").html(html); | ||
| 255 | - | ||
| 256 | - if (this.reversed) { | ||
| 257 | - // init infinite scrolling | ||
| 258 | - this.initLoadMore(); | ||
| 259 | - | ||
| 260 | - // init getting new notes | ||
| 261 | - this.initRefreshNew(); | ||
| 262 | - } | ||
| 263 | - }, | 248 | + setContent: function(newNoteIds, html) { |
| 249 | + this.top_id = newNoteIds.first(); | ||
| 250 | + this.bottom_id = newNoteIds.last(); | ||
| 251 | + $("#notes-list").html(html); | ||
| 252 | + | ||
| 253 | + if (this.reversed) { | ||
| 254 | + // init infinite scrolling | ||
| 255 | + this.initLoadMore(); | ||
| 256 | + | ||
| 257 | + // init getting new notes | ||
| 258 | + this.initRefreshNew(); | ||
| 259 | + } | ||
| 260 | + }, | ||
| 264 | 261 | ||
| 265 | 262 | ||
| 266 | /** | 263 | /** |
| @@ -274,66 +271,62 @@ var NoteList = { | @@ -274,66 +271,62 @@ var NoteList = { | ||
| 274 | /** | 271 | /** |
| 275 | * Initializes loading more notes when scrolling to the bottom of the page. | 272 | * Initializes loading more notes when scrolling to the bottom of the page. |
| 276 | */ | 273 | */ |
| 277 | - initLoadMore: | ||
| 278 | - function() { | ||
| 279 | - $(document).endlessScroll({ | ||
| 280 | - bottomPixels: 400, | ||
| 281 | - fireDelay: 1000, | ||
| 282 | - fireOnce:true, | ||
| 283 | - ceaseFire: function() { | ||
| 284 | - return NoteList.loading_more_disabled; | ||
| 285 | - }, | ||
| 286 | - callback: function(i) { | ||
| 287 | - NoteList.getMore(); | ||
| 288 | - } | ||
| 289 | - }); | ||
| 290 | - }, | 274 | + initLoadMore: function() { |
| 275 | + $(document).endlessScroll({ | ||
| 276 | + bottomPixels: 400, | ||
| 277 | + fireDelay: 1000, | ||
| 278 | + fireOnce:true, | ||
| 279 | + ceaseFire: function() { | ||
| 280 | + return NoteList.loading_more_disabled; | ||
| 281 | + }, | ||
| 282 | + callback: function(i) { | ||
| 283 | + NoteList.getMore(); | ||
| 284 | + } | ||
| 285 | + }); | ||
| 286 | + }, | ||
| 291 | 287 | ||
| 292 | /** | 288 | /** |
| 293 | * Gets an additional set of notes. | 289 | * Gets an additional set of notes. |
| 294 | */ | 290 | */ |
| 295 | - getMore: | ||
| 296 | - function() { | ||
| 297 | - // only load more notes if there are no "new" notes | ||
| 298 | - $('.loading').show(); | ||
| 299 | - $.ajax({ | ||
| 300 | - url: this.notes_path, | ||
| 301 | - data: this.target_params + "&loading_more=1&" + (this.reversed ? "before_id" : "after_id") + "=" + this.bottom_id, | ||
| 302 | - complete: function(){ $('.notes-status').removeClass("loading")}, | ||
| 303 | - beforeSend: function() { $('.notes-status').addClass("loading") }, | ||
| 304 | - dataType: "script" | ||
| 305 | - }); | ||
| 306 | - }, | 291 | + getMore: function() { |
| 292 | + // only load more notes if there are no "new" notes | ||
| 293 | + $('.loading').show(); | ||
| 294 | + $.ajax({ | ||
| 295 | + url: this.notes_path, | ||
| 296 | + data: this.target_params + "&loading_more=1&" + (this.reversed ? "before_id" : "after_id") + "=" + this.bottom_id, | ||
| 297 | + complete: function(){ $('.notes-status').removeClass("loading")}, | ||
| 298 | + beforeSend: function() { $('.notes-status').addClass("loading") }, | ||
| 299 | + dataType: "script" | ||
| 300 | + }); | ||
| 301 | + }, | ||
| 307 | 302 | ||
| 308 | /** | 303 | /** |
| 309 | * Called in response to getMore(). | 304 | * Called in response to getMore(). |
| 310 | * Append notes to #notes-list. | 305 | * Append notes to #notes-list. |
| 311 | */ | 306 | */ |
| 312 | - appendMoreNotes: | ||
| 313 | - function(newNoteIds, html) { | ||
| 314 | - var lastNewNoteId = newNoteIds.last(); | ||
| 315 | - if(lastNewNoteId != this.bottom_id) { | ||
| 316 | - this.bottom_id = lastNewNoteId; | ||
| 317 | - $("#notes-list").append(html); | ||
| 318 | - } | ||
| 319 | - }, | 307 | + appendMoreNotes: function(newNoteIds, html) { |
| 308 | + var lastNewNoteId = newNoteIds.last(); | ||
| 309 | + if(lastNewNoteId != this.bottom_id) { | ||
| 310 | + this.bottom_id = lastNewNoteId; | ||
| 311 | + $("#notes-list").append(html); | ||
| 312 | + } | ||
| 313 | + }, | ||
| 320 | 314 | ||
| 321 | /** | 315 | /** |
| 322 | * Called in response to getMore(). | 316 | * Called in response to getMore(). |
| 323 | * Disables loading more notes when scrolling to the bottom of the page. | 317 | * Disables loading more notes when scrolling to the bottom of the page. |
| 324 | * Initalizes refreshing new notes. | 318 | * Initalizes refreshing new notes. |
| 325 | */ | 319 | */ |
| 326 | - finishedLoadingMore: | ||
| 327 | - function() { | ||
| 328 | - this.loading_more_disabled = true; | 320 | + finishedLoadingMore: function() { |
| 321 | + this.loading_more_disabled = true; | ||
| 329 | 322 | ||
| 330 | - // from now on only get new notes | ||
| 331 | - if (!this.reversed) { | ||
| 332 | - this.initRefreshNew(); | ||
| 333 | - } | ||
| 334 | - // make sure we are up to date | ||
| 335 | - this.updateVotes(); | ||
| 336 | - }, | 323 | + // from now on only get new notes |
| 324 | + if (!this.reversed) { | ||
| 325 | + this.initRefreshNew(); | ||
| 326 | + } | ||
| 327 | + // make sure we are up to date | ||
| 328 | + this.updateVotes(); | ||
| 329 | + }, | ||
| 337 | 330 | ||
| 338 | 331 | ||
| 339 | /** | 332 | /** |
| @@ -348,45 +341,41 @@ var NoteList = { | @@ -348,45 +341,41 @@ var NoteList = { | ||
| 348 | /** | 341 | /** |
| 349 | * Initializes getting new notes every n seconds. | 342 | * Initializes getting new notes every n seconds. |
| 350 | */ | 343 | */ |
| 351 | - initRefreshNew: | ||
| 352 | - function() { | ||
| 353 | - setInterval("NoteList.getNew()", 10000); | ||
| 354 | - }, | 344 | + initRefreshNew: function() { |
| 345 | + setInterval("NoteList.getNew()", 10000); | ||
| 346 | + }, | ||
| 355 | 347 | ||
| 356 | /** | 348 | /** |
| 357 | * Gets the new set of notes. | 349 | * Gets the new set of notes. |
| 358 | */ | 350 | */ |
| 359 | - getNew: | ||
| 360 | - function() { | ||
| 361 | - $.ajax({ | ||
| 362 | - url: this.notes_path, | ||
| 363 | - data: this.target_params + "&loading_new=1&after_id=" + (this.reversed ? this.top_id : this.bottom_id), | ||
| 364 | - dataType: "script" | ||
| 365 | - }); | ||
| 366 | - }, | 351 | + getNew: function() { |
| 352 | + $.ajax({ | ||
| 353 | + url: this.notes_path, | ||
| 354 | + data: this.target_params + "&loading_new=1&after_id=" + (this.reversed ? this.top_id : this.bottom_id), | ||
| 355 | + dataType: "script" | ||
| 356 | + }); | ||
| 357 | + }, | ||
| 367 | 358 | ||
| 368 | /** | 359 | /** |
| 369 | * Called in response to getNew(). | 360 | * Called in response to getNew(). |
| 370 | * Replaces the content of #new-notes-list with the given html. | 361 | * Replaces the content of #new-notes-list with the given html. |
| 371 | */ | 362 | */ |
| 372 | - replaceNewNotes: | ||
| 373 | - function(newNoteIds, html) { | ||
| 374 | - $("#new-notes-list").html(html); | ||
| 375 | - this.updateVotes(); | ||
| 376 | - }, | 363 | + replaceNewNotes: function(newNoteIds, html) { |
| 364 | + $("#new-notes-list").html(html); | ||
| 365 | + this.updateVotes(); | ||
| 366 | + }, | ||
| 377 | 367 | ||
| 378 | /** | 368 | /** |
| 379 | * Adds a single note to #new-notes-list. | 369 | * Adds a single note to #new-notes-list. |
| 380 | */ | 370 | */ |
| 381 | - appendNewNote: | ||
| 382 | - function(id, html) { | ||
| 383 | - if (this.reversed) { | ||
| 384 | - $("#notes-list").prepend(html); | ||
| 385 | - } else { | ||
| 386 | - $("#notes-list").append(html); | ||
| 387 | - } | ||
| 388 | - this.updateVotes(); | ||
| 389 | - }, | 371 | + appendNewNote: function(id, html) { |
| 372 | + if (this.reversed) { | ||
| 373 | + $("#notes-list").prepend(html); | ||
| 374 | + } else { | ||
| 375 | + $("#notes-list").append(html); | ||
| 376 | + } | ||
| 377 | + this.updateVotes(); | ||
| 378 | + }, | ||
| 390 | 379 | ||
| 391 | /** | 380 | /** |
| 392 | * Recalculates the votes and updates them (if they are displayed at all). | 381 | * Recalculates the votes and updates them (if they are displayed at all). |
| @@ -396,25 +385,24 @@ var NoteList = { | @@ -396,25 +385,24 @@ var NoteList = { | ||
| 396 | * Might produce inaccurate results when not all notes have been loaded and a | 385 | * Might produce inaccurate results when not all notes have been loaded and a |
| 397 | * recalculation is triggered (e.g. when deleting a note). | 386 | * recalculation is triggered (e.g. when deleting a note). |
| 398 | */ | 387 | */ |
| 399 | - updateVotes: | ||
| 400 | - function() { | ||
| 401 | - var votes = $("#votes .votes"); | ||
| 402 | - var notes = $("#notes-list, #new-notes-list").find(".note .vote"); | ||
| 403 | - | ||
| 404 | - // only update if there is a vote display | ||
| 405 | - if (votes.size()) { | ||
| 406 | - var upvotes = notes.filter(".upvote").size(); | ||
| 407 | - var downvotes = notes.filter(".downvote").size(); | ||
| 408 | - var votesCount = upvotes + downvotes; | ||
| 409 | - var upvotesPercent = votesCount ? (100.0 / votesCount * upvotes) : 0; | ||
| 410 | - var downvotesPercent = votesCount ? (100.0 - upvotesPercent) : 0; | ||
| 411 | - | ||
| 412 | - // change vote bar lengths | ||
| 413 | - votes.find(".bar-success").css("width", upvotesPercent+"%"); | ||
| 414 | - votes.find(".bar-danger").css("width", downvotesPercent+"%"); | ||
| 415 | - // replace vote numbers | ||
| 416 | - votes.find(".upvotes").text(votes.find(".upvotes").text().replace(/\d+/, upvotes)); | ||
| 417 | - votes.find(".downvotes").text(votes.find(".downvotes").text().replace(/\d+/, downvotes)); | ||
| 418 | - } | 388 | + updateVotes: function() { |
| 389 | + var votes = $("#votes .votes"); | ||
| 390 | + var notes = $("#notes-list").find(".note .vote"); | ||
| 391 | + | ||
| 392 | + // only update if there is a vote display | ||
| 393 | + if (votes.size()) { | ||
| 394 | + var upvotes = notes.filter(".upvote").size(); | ||
| 395 | + var downvotes = notes.filter(".downvote").size(); | ||
| 396 | + var votesCount = upvotes + downvotes; | ||
| 397 | + var upvotesPercent = votesCount ? (100.0 / votesCount * upvotes) : 0; | ||
| 398 | + var downvotesPercent = votesCount ? (100.0 - upvotesPercent) : 0; | ||
| 399 | + | ||
| 400 | + // change vote bar lengths | ||
| 401 | + votes.find(".bar-success").css("width", upvotesPercent+"%"); | ||
| 402 | + votes.find(".bar-danger").css("width", downvotesPercent+"%"); | ||
| 403 | + // replace vote numbers | ||
| 404 | + votes.find(".upvotes").text(votes.find(".upvotes").text().replace(/\d+/, upvotes)); | ||
| 405 | + votes.find(".downvotes").text(votes.find(".downvotes").text().replace(/\d+/, downvotes)); | ||
| 419 | } | 406 | } |
| 407 | + } | ||
| 420 | }; | 408 | }; |