Commit 9305ef8940988fc9baad7c5196819f272445dd9b

Authored by Dmitriy Zaporozhets
1 parent f554aa38

Remove old methods and classes

Signed-off-by: Dmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>
app/assets/javascripts/notes.js
@@ -1,584 +0,0 @@ @@ -1,584 +0,0 @@
1 -var NoteList = {  
2 - id: null,  
3 - notes_path: null,  
4 - target_params: null,  
5 - target_id: 0,  
6 - target_type: null,  
7 -  
8 - init: function(tid, tt, path) {  
9 - NoteList.notes_path = path + ".json";  
10 - NoteList.target_id = tid;  
11 - NoteList.target_type = tt;  
12 - NoteList.target_params = "target_type=" + NoteList.target_type + "&target_id=" + NoteList.target_id;  
13 -  
14 - NoteList.setupMainTargetNoteForm();  
15 -  
16 - // Unbind events to prevent firing twice  
17 - $(document).off("click", ".js-add-diff-note-button");  
18 - $(document).off("click", ".js-discussion-reply-button");  
19 - $(document).off("click", ".js-note-preview-button");  
20 - $(document).off("click", ".js-note-attachment-input");  
21 - $(document).off("click", ".js-close-discussion-note-form");  
22 - $(document).off("click", ".js-note-delete");  
23 - $(document).off("click", ".js-note-edit");  
24 - $(document).off("click", ".js-note-edit-cancel");  
25 - $(document).off("click", ".js-note-attachment-delete");  
26 - $(document).off("click", ".js-choose-note-attachment-button");  
27 - $(document).off("click", ".js-show-outdated-discussion");  
28 -  
29 - $(document).off("ajax:complete", ".js-main-target-form");  
30 -  
31 -  
32 - // add a new diff note  
33 - $(document).on("click",  
34 - ".js-add-diff-note-button",  
35 - NoteList.addDiffNote);  
36 -  
37 - // reply to diff/discussion notes  
38 - $(document).on("click",  
39 - ".js-discussion-reply-button",  
40 - NoteList.replyToDiscussionNote);  
41 -  
42 - // setup note preview  
43 - $(document).on("click",  
44 - ".js-note-preview-button",  
45 - NoteList.previewNote);  
46 -  
47 - // update the file name when an attachment is selected  
48 - $(document).on("change",  
49 - ".js-note-attachment-input",  
50 - NoteList.updateFormAttachment);  
51 -  
52 - // hide diff note form  
53 - $(document).on("click",  
54 - ".js-close-discussion-note-form",  
55 - NoteList.removeDiscussionNoteForm);  
56 -  
57 - // remove a note (in general)  
58 - $(document).on("click",  
59 - ".js-note-delete",  
60 - NoteList.removeNote);  
61 -  
62 - // show the edit note form  
63 - $(document).on("click",  
64 - ".js-note-edit",  
65 - NoteList.showEditNoteForm);  
66 -  
67 - // cancel note editing  
68 - $(document).on("click",  
69 - ".note-edit-cancel",  
70 - NoteList.cancelNoteEdit);  
71 -  
72 - // delete note attachment  
73 - $(document).on("click",  
74 - ".js-note-attachment-delete",  
75 - NoteList.deleteNoteAttachment);  
76 -  
77 - // update the note after editing  
78 - $(document).on("ajax:complete",  
79 - "form.edit_note",  
80 - NoteList.updateNote);  
81 -  
82 - // reset main target form after submit  
83 - $(document).on("ajax:complete",  
84 - ".js-main-target-form",  
85 - NoteList.resetMainTargetForm);  
86 -  
87 -  
88 - $(document).on("click",  
89 - ".js-choose-note-attachment-button",  
90 - NoteList.chooseNoteAttachment);  
91 -  
92 - $(document).on("click",  
93 - ".js-show-outdated-discussion",  
94 - function(e) { $(this).next('.outdated-discussion').show(); e.preventDefault() });  
95 - },  
96 -  
97 -  
98 - /**  
99 - * When clicking on buttons  
100 - */  
101 -  
102 - /**  
103 - * Called when clicking on the "add a comment" button on the side of a diff line.  
104 - *  
105 - * Inserts a temporary row for the form below the line.  
106 - * Sets up the form and shows it.  
107 - */  
108 - addDiffNote: function(e) {  
109 - e.preventDefault();  
110 -  
111 - // find the form  
112 - var form = $(".js-new-note-form");  
113 - var row = $(this).closest("tr");  
114 - var nextRow = row.next();  
115 -  
116 - // does it already have notes?  
117 - if (nextRow.is(".notes_holder")) {  
118 - $.proxy(NoteList.replyToDiscussionNote,  
119 - nextRow.find(".js-discussion-reply-button")  
120 - ).call();  
121 - } else {  
122 - // add a notes row and insert the form  
123 - row.after('<tr class="notes_holder js-temp-notes-holder"><td class="notes_line" colspan="2"></td><td class="notes_content"></td></tr>');  
124 - form.clone().appendTo(row.next().find(".notes_content"));  
125 -  
126 - // show the form  
127 - NoteList.setupDiscussionNoteForm($(this), row.next().find("form"));  
128 - }  
129 - },  
130 -  
131 - /**  
132 - * Called when clicking the "Choose File" button.  
133 - *  
134 - * Opens the file selection dialog.  
135 - */  
136 - chooseNoteAttachment: function() {  
137 - var form = $(this).closest("form");  
138 -  
139 - form.find(".js-note-attachment-input").click();  
140 - },  
141 -  
142 - /**  
143 - * Shows the note preview.  
144 - *  
145 - * Lets the server render GFM into Html and displays it.  
146 - *  
147 - * Note: uses the Toggler behavior to toggle preview/edit views/buttons  
148 - */  
149 - previewNote: function(e) {  
150 - e.preventDefault();  
151 -  
152 - var form = $(this).closest("form");  
153 - var preview = form.find('.js-note-preview');  
154 - var noteText = form.find('.js-note-text').val();  
155 -  
156 - if(noteText.trim().length === 0) {  
157 - preview.text('Nothing to preview.');  
158 - } else {  
159 - preview.text('Loading...');  
160 - $.post($(this).data('url'), {note: noteText})  
161 - .success(function(previewData) {  
162 - preview.html(previewData);  
163 - });  
164 - }  
165 - },  
166 -  
167 - /**  
168 - * Called in response to "cancel" on a diff note form.  
169 - *  
170 - * Shows the reply button again.  
171 - * Removes the form and if necessary it's temporary row.  
172 - */  
173 - removeDiscussionNoteForm: function() {  
174 - var form = $(this).closest("form");  
175 - var row = form.closest("tr");  
176 -  
177 - // show the reply button (will only work for replies)  
178 - form.prev(".js-discussion-reply-button").show();  
179 -  
180 - if (row.is(".js-temp-notes-holder")) {  
181 - // remove temporary row for diff lines  
182 - row.remove();  
183 - } else {  
184 - // only remove the form  
185 - form.remove();  
186 - }  
187 - },  
188 -  
189 - /**  
190 - * Called in response to deleting a note of any kind.  
191 - *  
192 - * Removes the actual note from view.  
193 - * Removes the whole discussion if the last note is being removed.  
194 - */  
195 - removeNote: function() {  
196 - var note = $(this).closest(".note");  
197 - var notes = note.closest(".notes");  
198 -  
199 - // check if this is the last note for this line  
200 - if (notes.find(".note").length === 1) {  
201 - // for discussions  
202 - notes.closest(".discussion").remove();  
203 -  
204 - // for diff lines  
205 - notes.closest("tr").remove();  
206 - }  
207 -  
208 - note.remove();  
209 - NoteList.updateVotes();  
210 - },  
211 -  
212 - /**  
213 - * Called in response to clicking the edit note link  
214 - *  
215 - * Replaces the note text with the note edit form  
216 - * Adds a hidden div with the original content of the note to fill the edit note form with  
217 - * if the user cancels  
218 - */  
219 - showEditNoteForm: function(e) {  
220 - e.preventDefault();  
221 - var note = $(this).closest(".note");  
222 - note.find(".note-text").hide();  
223 -  
224 - // Show the attachment delete link  
225 - note.find(".js-note-attachment-delete").show();  
226 -  
227 - GitLab.GfmAutoComplete.setup();  
228 -  
229 - var form = note.find(".note-edit-form");  
230 - form.show();  
231 -  
232 - var textarea = form.find("textarea");  
233 - if (form.find(".note-original-content").length === 0) {  
234 - var p = $("<p></p>").text(textarea.val());  
235 - var hidden_div = $('<div class="note-original-content"></div>').append(p);  
236 - form.append(hidden_div);  
237 - hidden_div.hide();  
238 - }  
239 - textarea.focus();  
240 - },  
241 -  
242 - /**  
243 - * Called in response to clicking the cancel button when editing a note  
244 - *  
245 - * Resets and hides the note editing form  
246 - */  
247 - cancelNoteEdit: function(e) {  
248 - e.preventDefault();  
249 - var note = $(this).closest(".note");  
250 - NoteList.resetNoteEditing(note);  
251 - },  
252 -  
253 -  
254 - /**  
255 - * Called in response to clicking the delete attachment link  
256 - *  
257 - * Removes the attachment wrapper view, including image tag if it exists  
258 - * Resets the note editing form  
259 - */  
260 - deleteNoteAttachment: function() {  
261 - var note = $(this).closest(".note");  
262 - note.find(".note-attachment").remove();  
263 - NoteList.resetNoteEditing(note);  
264 - NoteList.rewriteTimestamp(note.find(".note-last-update"));  
265 - },  
266 -  
267 -  
268 - /**  
269 - * Called when clicking on the "reply" button for a diff line.  
270 - *  
271 - * Shows the note form below the notes.  
272 - */  
273 - replyToDiscussionNote: function() {  
274 - // find the form  
275 - var form = $(".js-new-note-form");  
276 -  
277 - // hide reply button  
278 - $(this).hide();  
279 - // insert the form after the button  
280 - form.clone().insertAfter($(this));  
281 -  
282 - // show the form  
283 - NoteList.setupDiscussionNoteForm($(this), $(this).next("form"));  
284 - },  
285 -  
286 -  
287 - /**  
288 - * Helper for inserting and setting up note forms.  
289 - */  
290 -  
291 -  
292 - /**  
293 - * Called in response to creating a note failing validation.  
294 - *  
295 - * Adds the rendered errors to the respective form.  
296 - * If "discussionId" is null or undefined, the main target form is assumed.  
297 - */  
298 - errorsOnForm: function(errorsHtml, discussionId) {  
299 - // find the form  
300 - if (discussionId) {  
301 - var form = $("form[rel='"+discussionId+"']");  
302 - } else {  
303 - var form = $(".js-main-target-form");  
304 - }  
305 -  
306 - form.find(".js-errors").remove();  
307 - form.prepend(errorsHtml);  
308 -  
309 - form.find(".js-note-text").focus();  
310 - },  
311 -  
312 -  
313 - /**  
314 - * Shows the diff/discussion form and does some setup on it.  
315 - *  
316 - * Sets some hidden fields in the form.  
317 - *  
318 - * Note: dataHolder must have the "discussionId", "lineCode", "noteableType"  
319 - * and "noteableId" data attributes set.  
320 - */  
321 - setupDiscussionNoteForm: function(dataHolder, form) {  
322 - // setup note target  
323 - form.attr("rel", dataHolder.data("discussionId"));  
324 - form.find("#note_commit_id").val(dataHolder.data("commitId"));  
325 - form.find("#note_line_code").val(dataHolder.data("lineCode"));  
326 - form.find("#note_noteable_type").val(dataHolder.data("noteableType"));  
327 - form.find("#note_noteable_id").val(dataHolder.data("noteableId"));  
328 -  
329 - NoteList.setupNoteForm(form);  
330 -  
331 - form.find(".js-note-text").focus();  
332 - },  
333 -  
334 - /**  
335 - * Shows the main form and does some setup on it.  
336 - *  
337 - * Sets some hidden fields in the form.  
338 - */  
339 - setupMainTargetNoteForm: function() {  
340 - // find the form  
341 - var form = $(".js-new-note-form");  
342 - // insert the form after the button  
343 - form.clone().replaceAll($(".js-main-target-form"));  
344 -  
345 - form = form.prev("form");  
346 -  
347 - // show the form  
348 - NoteList.setupNoteForm(form);  
349 -  
350 - // fix classes  
351 - form.removeClass("js-new-note-form");  
352 - form.addClass("js-main-target-form");  
353 -  
354 - // remove unnecessary fields and buttons  
355 - form.find("#note_line_code").remove();  
356 - form.find(".js-close-discussion-note-form").remove();  
357 - },  
358 -  
359 - /**  
360 - * General note form setup.  
361 - *  
362 - * * deactivates the submit button when text is empty  
363 - * * hides the preview button when text is empty  
364 - * * setup GFM auto complete  
365 - * * show the form  
366 - */  
367 - setupNoteForm: function(form) {  
368 - disableButtonIfEmptyField(form.find(".js-note-text"), form.find(".js-comment-button"));  
369 -  
370 - form.removeClass("js-new-note-form");  
371 -  
372 - // setup preview buttons  
373 - form.find(".js-note-edit-button, .js-note-preview-button")  
374 - .tooltip({ placement: 'left' });  
375 -  
376 - previewButton = form.find(".js-note-preview-button");  
377 - form.find(".js-note-text").on("input", function() {  
378 - if ($(this).val().trim() !== "") {  
379 - previewButton.removeClass("turn-off").addClass("turn-on");  
380 - } else {  
381 - previewButton.removeClass("turn-on").addClass("turn-off");  
382 - }  
383 - });  
384 -  
385 - // remove notify commit author checkbox for non-commit notes  
386 - if (form.find("#note_noteable_type").val() !== "Commit") {  
387 - form.find(".js-notify-commit-author").remove();  
388 - }  
389 -  
390 - GitLab.GfmAutoComplete.setup();  
391 -  
392 - form.show();  
393 - },  
394 -  
395 -  
396 - /**  
397 - * Handle loading the initial set of notes.  
398 - * And set up loading more notes when scrolling to the bottom of the page.  
399 - */  
400 -  
401 -  
402 - /**  
403 - * Gets an initial set of notes.  
404 - */  
405 - getContent: function() {  
406 - $.ajax({  
407 - url: NoteList.notes_path,  
408 - data: NoteList.target_params,  
409 - complete: function(){ $('.js-notes-busy').removeClass("loading")},  
410 - beforeSend: function() { $('.js-notes-busy').addClass("loading") },  
411 - success: function(data) {  
412 - NoteList.setContent(data.html);  
413 - },  
414 - dataType: "json"  
415 - });  
416 - },  
417 -  
418 - /**  
419 - * Called in response to getContent().  
420 - * Replaces the content of #notes-list with the given html.  
421 - */  
422 - setContent: function(html) {  
423 - $("#notes-list").html(html);  
424 - },  
425 -  
426 -  
427 - /**  
428 - * Adds a single common note to #notes-list.  
429 - */  
430 - appendNewNote: function(id, html) {  
431 - $("#notes-list").append(html);  
432 - NoteList.updateVotes();  
433 - },  
434 -  
435 - /**  
436 - * Adds a single discussion note to #notes-list.  
437 - *  
438 - * Also removes the corresponding form.  
439 - */  
440 - appendNewDiscussionNote: function(discussionId, diffRowHtml, noteHtml) {  
441 - var form = $("form[rel='"+discussionId+"']");  
442 - var row = form.closest("tr");  
443 -  
444 - // is this the first note of discussion?  
445 - if (row.is(".js-temp-notes-holder")) {  
446 - // insert the note and the reply button after the temp row  
447 - row.after(diffRowHtml);  
448 - // remove the note (will be added again below)  
449 - row.next().find(".note").remove();  
450 - }  
451 -  
452 - // append new note to all matching discussions  
453 - $(".notes[rel='"+discussionId+"']").append(noteHtml);  
454 -  
455 - // cleanup after successfully creating a diff/discussion note  
456 - $.proxy(NoteList.removeDiscussionNoteForm, form).call();  
457 - },  
458 -  
459 - /**  
460 - * Called in response the main target form has been successfully submitted.  
461 - *  
462 - * Removes any errors.  
463 - * Resets text and preview.  
464 - * Resets buttons.  
465 - */  
466 - resetMainTargetForm: function(){  
467 - var form = $(this);  
468 -  
469 - // remove validation errors  
470 - form.find(".js-errors").remove();  
471 -  
472 - // reset text and preview  
473 - var previewContainer = form.find(".js-toggler-container.note_text_and_preview");  
474 - if (previewContainer.is(".on")) {  
475 - previewContainer.removeClass("on");  
476 - }  
477 - form.find(".js-note-text").val("").trigger("input");  
478 - },  
479 -  
480 - /**  
481 - * Called after an attachment file has been selected.  
482 - *  
483 - * Updates the file name for the selected attachment.  
484 - */  
485 - updateFormAttachment: function() {  
486 - var form = $(this).closest("form");  
487 -  
488 - // get only the basename  
489 - var filename = $(this).val().replace(/^.*[\\\/]/, '');  
490 -  
491 - form.find(".js-attachment-filename").text(filename);  
492 - },  
493 -  
494 - /**  
495 - * Recalculates the votes and updates them (if they are displayed at all).  
496 - *  
497 - * Assumes all relevant notes are displayed (i.e. there are no more notes to  
498 - * load via getMore()).  
499 - * Might produce inaccurate results when not all notes have been loaded and a  
500 - * recalculation is triggered (e.g. when deleting a note).  
501 - */  
502 - updateVotes: function() {  
503 - var votes = $("#votes .votes");  
504 - var notes = $("#notes-list .note .vote");  
505 -  
506 - // only update if there is a vote display  
507 - if (votes.size()) {  
508 - var upvotes = notes.filter(".upvote").size();  
509 - var downvotes = notes.filter(".downvote").size();  
510 - var votesCount = upvotes + downvotes;  
511 - var upvotesPercent = votesCount ? (100.0 / votesCount * upvotes) : 0;  
512 - var downvotesPercent = votesCount ? (100.0 - upvotesPercent) : 0;  
513 -  
514 - // change vote bar lengths  
515 - votes.find(".bar-success").css("width", upvotesPercent+"%");  
516 - votes.find(".bar-danger").css("width", downvotesPercent+"%");  
517 - // replace vote numbers  
518 - votes.find(".upvotes").text(votes.find(".upvotes").text().replace(/\d+/, upvotes));  
519 - votes.find(".downvotes").text(votes.find(".downvotes").text().replace(/\d+/, downvotes));  
520 - }  
521 - },  
522 -  
523 - /**  
524 - * Called in response to the edit note form being submitted  
525 - *  
526 - * Updates the current note field.  
527 - * Hides the edit note form  
528 - */  
529 - updateNote: function(e, xhr, settings) {  
530 - response = JSON.parse(xhr.responseText);  
531 - if (response.success) {  
532 - var note_li = $("#note_" + response.id);  
533 - var note_text = note_li.find(".note-text");  
534 - note_text.html(response.note).show();  
535 -  
536 - var note_form = note_li.find(".note-edit-form");  
537 - var original_content = note_form.find(".note-original-content");  
538 - original_content.remove();  
539 - note_form.hide();  
540 - note_form.find(".btn-save").enableButton();  
541 -  
542 - // Update the "Edited at xxx label" on the note to show it's just been updated  
543 - NoteList.rewriteTimestamp(note_li.find(".note-last-update"));  
544 - }  
545 - },  
546 -  
547 - /**  
548 - * Called in response to the 'cancel note' link clicked, or after deleting a note attachment  
549 - *  
550 - * Hides the edit note form and shows the note  
551 - * Resets the edit note form textarea with the original content of the note  
552 - */  
553 - resetNoteEditing: function(note) {  
554 - note.find(".note-text").show();  
555 -  
556 - // Hide the attachment delete link  
557 - note.find(".js-note-attachment-delete").hide();  
558 -  
559 - // Put the original content of the note back into the edit form textarea  
560 - var form = note.find(".note-edit-form");  
561 - var original_content = form.find(".note-original-content");  
562 - form.find("textarea").val(original_content.text());  
563 - original_content.remove();  
564 -  
565 - note.find(".note-edit-form").hide();  
566 - },  
567 -  
568 - /**  
569 - * Utility function to generate new timestamp text for a note  
570 - *  
571 - */  
572 - rewriteTimestamp: function(element) {  
573 - // Strip all newlines from the existing timestamp  
574 - var ts = element.text().replace(/\n/g, ' ').trim();  
575 -  
576 - // If the timestamp already has '(Edited xxx ago)' text, remove it  
577 - ts = ts.replace(new RegExp("\\(Edited [A-Za-z0-9 ]+\\)$", "gi"), "");  
578 -  
579 - // Append "(Edited just now)"  
580 - ts = (ts + " <small>(Edited just now)</small>");  
581 -  
582 - element.html(ts);  
583 - }  
584 -};  
app/assets/stylesheets/sections/notes.scss
@@ -262,7 +262,6 @@ ul.notes { @@ -262,7 +262,6 @@ ul.notes {
262 } 262 }
263 .new_note { 263 .new_note {
264 display: none; 264 display: none;
265 -  
266 .buttons { 265 .buttons {
267 float: left; 266 float: left;
268 margin-top: 8px; 267 margin-top: 8px;
app/helpers/notes_helper.rb
1 module NotesHelper 1 module NotesHelper
2 # Helps to distinguish e.g. commit notes in mr notes list 2 # Helps to distinguish e.g. commit notes in mr notes list
3 def note_for_main_target?(note) 3 def note_for_main_target?(note)
4 - (@target_type.camelize == note.noteable_type && !note.for_diff_line?) 4 + (@noteable.class.name == note.noteable_type && !note.for_diff_line?)
5 end 5 end
6 6
7 def note_target_fields 7 def note_target_fields
@@ -21,14 +21,6 @@ module NotesHelper @@ -21,14 +21,6 @@ module NotesHelper
21 end 21 end
22 end 22 end
23 23
24 - def loading_more_notes?  
25 - params[:loading_more].present?  
26 - end  
27 -  
28 - def loading_new_notes?  
29 - params[:loading_new].present?  
30 - end  
31 -  
32 def note_timestamp(note) 24 def note_timestamp(note)
33 # Shows the created at time and the updated at time if different 25 # Shows the created at time and the updated at time if different
34 ts = "#{time_ago_with_tooltip(note.created_at, 'bottom', 'note_created_ago')} ago" 26 ts = "#{time_ago_with_tooltip(note.created_at, 'bottom', 'note_created_ago')} ago"
@@ -41,4 +33,13 @@ module NotesHelper @@ -41,4 +33,13 @@ module NotesHelper
41 end 33 end
42 ts.html_safe 34 ts.html_safe
43 end 35 end
  36 +
  37 + def noteable_json(noteable)
  38 + {
  39 + id: noteable.id,
  40 + class: noteable.class.name,
  41 + resources: noteable.class.table_name,
  42 + project_id: noteable.project.id,
  43 + }.to_json
  44 + end
44 end 45 end