##// END OF EJS Templates
comments: toggle icon will now mark hidden inline comments as visible for easier switching of outdated comments.
marcink -
r2612:9288dd39 default
parent child Browse files
Show More
@@ -1,809 +1,811 b''
1 1 // # Copyright (C) 2010-2018 RhodeCode GmbH
2 2 // #
3 3 // # This program is free software: you can redistribute it and/or modify
4 4 // # it under the terms of the GNU Affero General Public License, version 3
5 5 // # (only), as published by the Free Software Foundation.
6 6 // #
7 7 // # This program is distributed in the hope that it will be useful,
8 8 // # but WITHOUT ANY WARRANTY; without even the implied warranty of
9 9 // # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 10 // # GNU General Public License for more details.
11 11 // #
12 12 // # You should have received a copy of the GNU Affero General Public License
13 13 // # along with this program. If not, see <http://www.gnu.org/licenses/>.
14 14 // #
15 15 // # This program is dual-licensed. If you wish to learn more about the
16 16 // # RhodeCode Enterprise Edition, including its added features, Support services,
17 17 // # and proprietary license terms, please see https://rhodecode.com/licenses/
18 18
19 19 var firefoxAnchorFix = function() {
20 20 // hack to make anchor links behave properly on firefox, in our inline
21 21 // comments generation when comments are injected firefox is misbehaving
22 22 // when jumping to anchor links
23 23 if (location.href.indexOf('#') > -1) {
24 24 location.href += '';
25 25 }
26 26 };
27 27
28 28 var linkifyComments = function(comments) {
29 29 var firstCommentId = null;
30 30 if (comments) {
31 31 firstCommentId = $(comments[0]).data('comment-id');
32 32 }
33 33
34 34 if (firstCommentId){
35 35 $('#inline-comments-counter').attr('href', '#comment-' + firstCommentId);
36 36 }
37 37 };
38 38
39 39 var bindToggleButtons = function() {
40 40 $('.comment-toggle').on('click', function() {
41 41 $(this).parent().nextUntil('tr.line').toggle('inline-comments');
42 42 });
43 43 };
44 44
45 45 /* Comment form for main and inline comments */
46 46 (function(mod) {
47 47
48 48 if (typeof exports == "object" && typeof module == "object") {
49 49 // CommonJS
50 50 module.exports = mod();
51 51 }
52 52 else {
53 53 // Plain browser env
54 54 (this || window).CommentForm = mod();
55 55 }
56 56
57 57 })(function() {
58 58 "use strict";
59 59
60 60 function CommentForm(formElement, commitId, pullRequestId, lineNo, initAutocompleteActions, resolvesCommentId) {
61 61 if (!(this instanceof CommentForm)) {
62 62 return new CommentForm(formElement, commitId, pullRequestId, lineNo, initAutocompleteActions, resolvesCommentId);
63 63 }
64 64
65 65 // bind the element instance to our Form
66 66 $(formElement).get(0).CommentForm = this;
67 67
68 68 this.withLineNo = function(selector) {
69 69 var lineNo = this.lineNo;
70 70 if (lineNo === undefined) {
71 71 return selector
72 72 } else {
73 73 return selector + '_' + lineNo;
74 74 }
75 75 };
76 76
77 77 this.commitId = commitId;
78 78 this.pullRequestId = pullRequestId;
79 79 this.lineNo = lineNo;
80 80 this.initAutocompleteActions = initAutocompleteActions;
81 81
82 82 this.previewButton = this.withLineNo('#preview-btn');
83 83 this.previewContainer = this.withLineNo('#preview-container');
84 84
85 85 this.previewBoxSelector = this.withLineNo('#preview-box');
86 86
87 87 this.editButton = this.withLineNo('#edit-btn');
88 88 this.editContainer = this.withLineNo('#edit-container');
89 89 this.cancelButton = this.withLineNo('#cancel-btn');
90 90 this.commentType = this.withLineNo('#comment_type');
91 91
92 92 this.resolvesId = null;
93 93 this.resolvesActionId = null;
94 94
95 95 this.closesPr = '#close_pull_request';
96 96
97 97 this.cmBox = this.withLineNo('#text');
98 98 this.cm = initCommentBoxCodeMirror(this, this.cmBox, this.initAutocompleteActions);
99 99
100 100 this.statusChange = this.withLineNo('#change_status');
101 101
102 102 this.submitForm = formElement;
103 103 this.submitButton = $(this.submitForm).find('input[type="submit"]');
104 104 this.submitButtonText = this.submitButton.val();
105 105
106 106 this.previewUrl = pyroutes.url('repo_commit_comment_preview',
107 107 {'repo_name': templateContext.repo_name,
108 108 'commit_id': templateContext.commit_data.commit_id});
109 109
110 110 if (resolvesCommentId){
111 111 this.resolvesId = '#resolve_comment_{0}'.format(resolvesCommentId);
112 112 this.resolvesActionId = '#resolve_comment_action_{0}'.format(resolvesCommentId);
113 113 $(this.commentType).prop('disabled', true);
114 114 $(this.commentType).addClass('disabled');
115 115
116 116 // disable select
117 117 setTimeout(function() {
118 118 $(self.statusChange).select2('readonly', true);
119 119 }, 10);
120 120
121 121 var resolvedInfo = (
122 122 '<li class="resolve-action">' +
123 123 '<input type="hidden" id="resolve_comment_{0}" name="resolve_comment_{0}" value="{0}">' +
124 124 '<button id="resolve_comment_action_{0}" class="resolve-text btn btn-sm" onclick="return Rhodecode.comments.submitResolution({0})">{1} #{0}</button>' +
125 125 '</li>'
126 126 ).format(resolvesCommentId, _gettext('resolve comment'));
127 127 $(resolvedInfo).insertAfter($(this.commentType).parent());
128 128 }
129 129
130 130 // based on commitId, or pullRequestId decide where do we submit
131 131 // out data
132 132 if (this.commitId){
133 133 this.submitUrl = pyroutes.url('repo_commit_comment_create',
134 134 {'repo_name': templateContext.repo_name,
135 135 'commit_id': this.commitId});
136 136 this.selfUrl = pyroutes.url('repo_commit',
137 137 {'repo_name': templateContext.repo_name,
138 138 'commit_id': this.commitId});
139 139
140 140 } else if (this.pullRequestId) {
141 141 this.submitUrl = pyroutes.url('pullrequest_comment_create',
142 142 {'repo_name': templateContext.repo_name,
143 143 'pull_request_id': this.pullRequestId});
144 144 this.selfUrl = pyroutes.url('pullrequest_show',
145 145 {'repo_name': templateContext.repo_name,
146 146 'pull_request_id': this.pullRequestId});
147 147
148 148 } else {
149 149 throw new Error(
150 150 'CommentForm requires pullRequestId, or commitId to be specified.')
151 151 }
152 152
153 153 // FUNCTIONS and helpers
154 154 var self = this;
155 155
156 156 this.isInline = function(){
157 157 return this.lineNo && this.lineNo != 'general';
158 158 };
159 159
160 160 this.getCmInstance = function(){
161 161 return this.cm
162 162 };
163 163
164 164 this.setPlaceholder = function(placeholder) {
165 165 var cm = this.getCmInstance();
166 166 if (cm){
167 167 cm.setOption('placeholder', placeholder);
168 168 }
169 169 };
170 170
171 171 this.getCommentStatus = function() {
172 172 return $(this.submitForm).find(this.statusChange).val();
173 173 };
174 174 this.getCommentType = function() {
175 175 return $(this.submitForm).find(this.commentType).val();
176 176 };
177 177
178 178 this.getResolvesId = function() {
179 179 return $(this.submitForm).find(this.resolvesId).val() || null;
180 180 };
181 181
182 182 this.getClosePr = function() {
183 183 return $(this.submitForm).find(this.closesPr).val() || null;
184 184 };
185 185
186 186 this.markCommentResolved = function(resolvedCommentId){
187 187 $('#comment-label-{0}'.format(resolvedCommentId)).find('.resolved').show();
188 188 $('#comment-label-{0}'.format(resolvedCommentId)).find('.resolve').hide();
189 189 };
190 190
191 191 this.isAllowedToSubmit = function() {
192 192 return !$(this.submitButton).prop('disabled');
193 193 };
194 194
195 195 this.initStatusChangeSelector = function(){
196 196 var formatChangeStatus = function(state, escapeMarkup) {
197 197 var originalOption = state.element;
198 198 return '<div class="flag_status ' + $(originalOption).data('status') + ' pull-left"></div>' +
199 199 '<span>' + escapeMarkup(state.text) + '</span>';
200 200 };
201 201 var formatResult = function(result, container, query, escapeMarkup) {
202 202 return formatChangeStatus(result, escapeMarkup);
203 203 };
204 204
205 205 var formatSelection = function(data, container, escapeMarkup) {
206 206 return formatChangeStatus(data, escapeMarkup);
207 207 };
208 208
209 209 $(this.submitForm).find(this.statusChange).select2({
210 210 placeholder: _gettext('Status Review'),
211 211 formatResult: formatResult,
212 212 formatSelection: formatSelection,
213 213 containerCssClass: "drop-menu status_box_menu",
214 214 dropdownCssClass: "drop-menu-dropdown",
215 215 dropdownAutoWidth: true,
216 216 minimumResultsForSearch: -1
217 217 });
218 218 $(this.submitForm).find(this.statusChange).on('change', function() {
219 219 var status = self.getCommentStatus();
220 220
221 221 if (status && !self.isInline()) {
222 222 $(self.submitButton).prop('disabled', false);
223 223 }
224 224
225 225 var placeholderText = _gettext('Comment text will be set automatically based on currently selected status ({0}) ...').format(status);
226 226 self.setPlaceholder(placeholderText)
227 227 })
228 228 };
229 229
230 230 // reset the comment form into it's original state
231 231 this.resetCommentFormState = function(content) {
232 232 content = content || '';
233 233
234 234 $(this.editContainer).show();
235 235 $(this.editButton).parent().addClass('active');
236 236
237 237 $(this.previewContainer).hide();
238 238 $(this.previewButton).parent().removeClass('active');
239 239
240 240 this.setActionButtonsDisabled(true);
241 241 self.cm.setValue(content);
242 242 self.cm.setOption("readOnly", false);
243 243
244 244 if (this.resolvesId) {
245 245 // destroy the resolve action
246 246 $(this.resolvesId).parent().remove();
247 247 }
248 248 // reset closingPR flag
249 249 $('.close-pr-input').remove();
250 250
251 251 $(this.statusChange).select2('readonly', false);
252 252 };
253 253
254 254 this.globalSubmitSuccessCallback = function(){
255 255 // default behaviour is to call GLOBAL hook, if it's registered.
256 256 if (window.commentFormGlobalSubmitSuccessCallback !== undefined){
257 257 commentFormGlobalSubmitSuccessCallback()
258 258 }
259 259 };
260 260
261 261 this.submitAjaxPOST = function(url, postData, successHandler, failHandler) {
262 262 failHandler = failHandler || function() {};
263 263 var postData = toQueryString(postData);
264 264 var request = $.ajax({
265 265 url: url,
266 266 type: 'POST',
267 267 data: postData,
268 268 headers: {'X-PARTIAL-XHR': true}
269 269 })
270 270 .done(function(data) {
271 271 successHandler(data);
272 272 })
273 273 .fail(function(data, textStatus, errorThrown){
274 274 alert(
275 275 "Error while submitting comment.\n" +
276 276 "Error code {0} ({1}).".format(data.status, data.statusText));
277 277 failHandler()
278 278 });
279 279 return request;
280 280 };
281 281
282 282 // overwrite a submitHandler, we need to do it for inline comments
283 283 this.setHandleFormSubmit = function(callback) {
284 284 this.handleFormSubmit = callback;
285 285 };
286 286
287 287 // overwrite a submitSuccessHandler
288 288 this.setGlobalSubmitSuccessCallback = function(callback) {
289 289 this.globalSubmitSuccessCallback = callback;
290 290 };
291 291
292 292 // default handler for for submit for main comments
293 293 this.handleFormSubmit = function() {
294 294 var text = self.cm.getValue();
295 295 var status = self.getCommentStatus();
296 296 var commentType = self.getCommentType();
297 297 var resolvesCommentId = self.getResolvesId();
298 298 var closePullRequest = self.getClosePr();
299 299
300 300 if (text === "" && !status) {
301 301 return;
302 302 }
303 303
304 304 var excludeCancelBtn = false;
305 305 var submitEvent = true;
306 306 self.setActionButtonsDisabled(true, excludeCancelBtn, submitEvent);
307 307 self.cm.setOption("readOnly", true);
308 308
309 309 var postData = {
310 310 'text': text,
311 311 'changeset_status': status,
312 312 'comment_type': commentType,
313 313 'csrf_token': CSRF_TOKEN
314 314 };
315 315
316 316 if (resolvesCommentId) {
317 317 postData['resolves_comment_id'] = resolvesCommentId;
318 318 }
319 319
320 320 if (closePullRequest) {
321 321 postData['close_pull_request'] = true;
322 322 }
323 323
324 324 var submitSuccessCallback = function(o) {
325 325 // reload page if we change status for single commit.
326 326 if (status && self.commitId) {
327 327 location.reload(true);
328 328 } else {
329 329 $('#injected_page_comments').append(o.rendered_text);
330 330 self.resetCommentFormState();
331 331 timeagoActivate();
332 332
333 333 // mark visually which comment was resolved
334 334 if (resolvesCommentId) {
335 335 self.markCommentResolved(resolvesCommentId);
336 336 }
337 337 }
338 338
339 339 // run global callback on submit
340 340 self.globalSubmitSuccessCallback();
341 341
342 342 };
343 343 var submitFailCallback = function(){
344 344 self.resetCommentFormState(text);
345 345 };
346 346 self.submitAjaxPOST(
347 347 self.submitUrl, postData, submitSuccessCallback, submitFailCallback);
348 348 };
349 349
350 350 this.previewSuccessCallback = function(o) {
351 351 $(self.previewBoxSelector).html(o);
352 352 $(self.previewBoxSelector).removeClass('unloaded');
353 353
354 354 // swap buttons, making preview active
355 355 $(self.previewButton).parent().addClass('active');
356 356 $(self.editButton).parent().removeClass('active');
357 357
358 358 // unlock buttons
359 359 self.setActionButtonsDisabled(false);
360 360 };
361 361
362 362 this.setActionButtonsDisabled = function(state, excludeCancelBtn, submitEvent) {
363 363 excludeCancelBtn = excludeCancelBtn || false;
364 364 submitEvent = submitEvent || false;
365 365
366 366 $(this.editButton).prop('disabled', state);
367 367 $(this.previewButton).prop('disabled', state);
368 368
369 369 if (!excludeCancelBtn) {
370 370 $(this.cancelButton).prop('disabled', state);
371 371 }
372 372
373 373 var submitState = state;
374 374 if (!submitEvent && this.getCommentStatus() && !self.isInline()) {
375 375 // if the value of commit review status is set, we allow
376 376 // submit button, but only on Main form, isInline means inline
377 377 submitState = false
378 378 }
379 379
380 380 $(this.submitButton).prop('disabled', submitState);
381 381 if (submitEvent) {
382 382 $(this.submitButton).val(_gettext('Submitting...'));
383 383 } else {
384 384 $(this.submitButton).val(this.submitButtonText);
385 385 }
386 386
387 387 };
388 388
389 389 // lock preview/edit/submit buttons on load, but exclude cancel button
390 390 var excludeCancelBtn = true;
391 391 this.setActionButtonsDisabled(true, excludeCancelBtn);
392 392
393 393 // anonymous users don't have access to initialized CM instance
394 394 if (this.cm !== undefined){
395 395 this.cm.on('change', function(cMirror) {
396 396 if (cMirror.getValue() === "") {
397 397 self.setActionButtonsDisabled(true, excludeCancelBtn)
398 398 } else {
399 399 self.setActionButtonsDisabled(false, excludeCancelBtn)
400 400 }
401 401 });
402 402 }
403 403
404 404 $(this.editButton).on('click', function(e) {
405 405 e.preventDefault();
406 406
407 407 $(self.previewButton).parent().removeClass('active');
408 408 $(self.previewContainer).hide();
409 409
410 410 $(self.editButton).parent().addClass('active');
411 411 $(self.editContainer).show();
412 412
413 413 });
414 414
415 415 $(this.previewButton).on('click', function(e) {
416 416 e.preventDefault();
417 417 var text = self.cm.getValue();
418 418
419 419 if (text === "") {
420 420 return;
421 421 }
422 422
423 423 var postData = {
424 424 'text': text,
425 425 'renderer': templateContext.visual.default_renderer,
426 426 'csrf_token': CSRF_TOKEN
427 427 };
428 428
429 429 // lock ALL buttons on preview
430 430 self.setActionButtonsDisabled(true);
431 431
432 432 $(self.previewBoxSelector).addClass('unloaded');
433 433 $(self.previewBoxSelector).html(_gettext('Loading ...'));
434 434
435 435 $(self.editContainer).hide();
436 436 $(self.previewContainer).show();
437 437
438 438 // by default we reset state of comment preserving the text
439 439 var previewFailCallback = function(){
440 440 self.resetCommentFormState(text)
441 441 };
442 442 self.submitAjaxPOST(
443 443 self.previewUrl, postData, self.previewSuccessCallback,
444 444 previewFailCallback);
445 445
446 446 $(self.previewButton).parent().addClass('active');
447 447 $(self.editButton).parent().removeClass('active');
448 448 });
449 449
450 450 $(this.submitForm).submit(function(e) {
451 451 e.preventDefault();
452 452 var allowedToSubmit = self.isAllowedToSubmit();
453 453 if (!allowedToSubmit){
454 454 return false;
455 455 }
456 456 self.handleFormSubmit();
457 457 });
458 458
459 459 }
460 460
461 461 return CommentForm;
462 462 });
463 463
464 464 /* comments controller */
465 465 var CommentsController = function() {
466 466 var mainComment = '#text';
467 467 var self = this;
468 468
469 469 this.cancelComment = function(node) {
470 470 var $node = $(node);
471 471 var $td = $node.closest('td');
472 472 $node.closest('.comment-inline-form').remove();
473 473 return false;
474 474 };
475 475
476 476 this.getLineNumber = function(node) {
477 477 var $node = $(node);
478 478 var lineNo = $node.closest('td').attr('data-line-number');
479 479 if (lineNo === undefined && $node.data('commentInline')){
480 480 lineNo = $node.data('commentLineNo')
481 481 }
482 482
483 483 return lineNo
484 484 };
485 485
486 486 this.scrollToComment = function(node, offset, outdated) {
487 487 if (offset === undefined) {
488 488 offset = 0;
489 489 }
490 490 var outdated = outdated || false;
491 491 var klass = outdated ? 'div.comment-outdated' : 'div.comment-current';
492 492
493 493 if (!node) {
494 494 node = $('.comment-selected');
495 495 if (!node.length) {
496 496 node = $('comment-current')
497 497 }
498 498 }
499 499 $wrapper = $(node).closest('div.comment');
500 500 $comment = $(node).closest(klass);
501 501 $comments = $(klass);
502 502
503 503 // show hidden comment when referenced.
504 504 if (!$wrapper.is(':visible')){
505 505 $wrapper.show();
506 506 }
507 507
508 508 $('.comment-selected').removeClass('comment-selected');
509 509
510 510 var nextIdx = $(klass).index($comment) + offset;
511 511 if (nextIdx >= $comments.length) {
512 512 nextIdx = 0;
513 513 }
514 514 var $next = $(klass).eq(nextIdx);
515 515
516 516 var $cb = $next.closest('.cb');
517 517 $cb.removeClass('cb-collapsed');
518 518
519 519 var $filediffCollapseState = $cb.closest('.filediff').prev();
520 520 $filediffCollapseState.prop('checked', false);
521 521 $next.addClass('comment-selected');
522 522 scrollToElement($next);
523 523 return false;
524 524 };
525 525
526 526 this.nextComment = function(node) {
527 527 return self.scrollToComment(node, 1);
528 528 };
529 529
530 530 this.prevComment = function(node) {
531 531 return self.scrollToComment(node, -1);
532 532 };
533 533
534 534 this.nextOutdatedComment = function(node) {
535 535 return self.scrollToComment(node, 1, true);
536 536 };
537 537
538 538 this.prevOutdatedComment = function(node) {
539 539 return self.scrollToComment(node, -1, true);
540 540 };
541 541
542 542 this.deleteComment = function(node) {
543 543 if (!confirm(_gettext('Delete this comment?'))) {
544 544 return false;
545 545 }
546 546 var $node = $(node);
547 547 var $td = $node.closest('td');
548 548 var $comment = $node.closest('.comment');
549 549 var comment_id = $comment.attr('data-comment-id');
550 550 var url = AJAX_COMMENT_DELETE_URL.replace('__COMMENT_ID__', comment_id);
551 551 var postData = {
552 552 'csrf_token': CSRF_TOKEN
553 553 };
554 554
555 555 $comment.addClass('comment-deleting');
556 556 $comment.hide('fast');
557 557
558 558 var success = function(response) {
559 559 $comment.remove();
560 560 return false;
561 561 };
562 562 var failure = function(data, textStatus, xhr) {
563 563 alert("error processing request: " + textStatus);
564 564 $comment.show('fast');
565 565 $comment.removeClass('comment-deleting');
566 566 return false;
567 567 };
568 568 ajaxPOST(url, postData, success, failure);
569 569 };
570 570
571 571 this.toggleWideMode = function (node) {
572 572 if ($('#content').hasClass('wrapper')) {
573 573 $('#content').removeClass("wrapper");
574 574 $('#content').addClass("wide-mode-wrapper");
575 575 $(node).addClass('btn-success');
576 576 } else {
577 577 $('#content').removeClass("wide-mode-wrapper");
578 578 $('#content').addClass("wrapper");
579 579 $(node).removeClass('btn-success');
580 580 }
581 581 return false;
582 582 };
583 583
584 584 this.toggleComments = function(node, show) {
585 585 var $filediff = $(node).closest('.filediff');
586 586 if (show === true) {
587 587 $filediff.removeClass('hide-comments');
588 588 } else if (show === false) {
589 589 $filediff.find('.hide-line-comments').removeClass('hide-line-comments');
590 590 $filediff.addClass('hide-comments');
591 591 } else {
592 592 $filediff.find('.hide-line-comments').removeClass('hide-line-comments');
593 593 $filediff.toggleClass('hide-comments');
594 594 }
595 595 return false;
596 596 };
597 597
598 598 this.toggleLineComments = function(node) {
599 599 self.toggleComments(node, true);
600 600 var $node = $(node);
601 // mark outdated comments as visible before the toggle;
602 $(node.closest('tr')).find('.comment-outdated').show();
601 603 $node.closest('tr').toggleClass('hide-line-comments');
602 604 };
603 605
604 606 this.createCommentForm = function(formElement, lineno, placeholderText, initAutocompleteActions, resolvesCommentId){
605 607 var pullRequestId = templateContext.pull_request_data.pull_request_id;
606 608 var commitId = templateContext.commit_data.commit_id;
607 609
608 610 var commentForm = new CommentForm(
609 611 formElement, commitId, pullRequestId, lineno, initAutocompleteActions, resolvesCommentId);
610 612 var cm = commentForm.getCmInstance();
611 613
612 614 if (resolvesCommentId){
613 615 var placeholderText = _gettext('Leave a comment, or click resolve button to resolve TODO comment #{0}').format(resolvesCommentId);
614 616 }
615 617
616 618 setTimeout(function() {
617 619 // callbacks
618 620 if (cm !== undefined) {
619 621 commentForm.setPlaceholder(placeholderText);
620 622 if (commentForm.isInline()) {
621 623 cm.focus();
622 624 cm.refresh();
623 625 }
624 626 }
625 627 }, 10);
626 628
627 629 // trigger scrolldown to the resolve comment, since it might be away
628 630 // from the clicked
629 631 if (resolvesCommentId){
630 632 var actionNode = $(commentForm.resolvesActionId).offset();
631 633
632 634 setTimeout(function() {
633 635 if (actionNode) {
634 636 $('body, html').animate({scrollTop: actionNode.top}, 10);
635 637 }
636 638 }, 100);
637 639 }
638 640
639 641 return commentForm;
640 642 };
641 643
642 644 this.createGeneralComment = function (lineNo, placeholderText, resolvesCommentId) {
643 645
644 646 var tmpl = $('#cb-comment-general-form-template').html();
645 647 tmpl = tmpl.format(null, 'general');
646 648 var $form = $(tmpl);
647 649
648 650 var $formPlaceholder = $('#cb-comment-general-form-placeholder');
649 651 var curForm = $formPlaceholder.find('form');
650 652 if (curForm){
651 653 curForm.remove();
652 654 }
653 655 $formPlaceholder.append($form);
654 656
655 657 var _form = $($form[0]);
656 658 var autocompleteActions = ['approve', 'reject', 'as_note', 'as_todo'];
657 659 var commentForm = this.createCommentForm(
658 660 _form, lineNo, placeholderText, autocompleteActions, resolvesCommentId);
659 661 commentForm.initStatusChangeSelector();
660 662
661 663 return commentForm;
662 664 };
663 665
664 666 this.createComment = function(node, resolutionComment) {
665 667 var resolvesCommentId = resolutionComment || null;
666 668 var $node = $(node);
667 669 var $td = $node.closest('td');
668 670 var $form = $td.find('.comment-inline-form');
669 671
670 672 if (!$form.length) {
671 673
672 674 var $filediff = $node.closest('.filediff');
673 675 $filediff.removeClass('hide-comments');
674 676 var f_path = $filediff.attr('data-f-path');
675 677 var lineno = self.getLineNumber(node);
676 678 // create a new HTML from template
677 679 var tmpl = $('#cb-comment-inline-form-template').html();
678 680 tmpl = tmpl.format(escapeHtml(f_path), lineno);
679 681 $form = $(tmpl);
680 682
681 683 var $comments = $td.find('.inline-comments');
682 684 if (!$comments.length) {
683 685 $comments = $(
684 686 $('#cb-comments-inline-container-template').html());
685 687 $td.append($comments);
686 688 }
687 689
688 690 $td.find('.cb-comment-add-button').before($form);
689 691
690 692 var placeholderText = _gettext('Leave a comment on line {0}.').format(lineno);
691 693 var _form = $($form[0]).find('form');
692 694 var autocompleteActions = ['as_note', 'as_todo'];
693 695 var commentForm = this.createCommentForm(
694 696 _form, lineno, placeholderText, autocompleteActions, resolvesCommentId);
695 697
696 698 $.Topic('/ui/plugins/code/comment_form_built').prepareOrPublish({
697 699 form: _form,
698 700 parent: $td[0],
699 701 lineno: lineno,
700 702 f_path: f_path}
701 703 );
702 704
703 705 // set a CUSTOM submit handler for inline comments.
704 706 commentForm.setHandleFormSubmit(function(o) {
705 707 var text = commentForm.cm.getValue();
706 708 var commentType = commentForm.getCommentType();
707 709 var resolvesCommentId = commentForm.getResolvesId();
708 710
709 711 if (text === "") {
710 712 return;
711 713 }
712 714
713 715 if (lineno === undefined) {
714 716 alert('missing line !');
715 717 return;
716 718 }
717 719 if (f_path === undefined) {
718 720 alert('missing file path !');
719 721 return;
720 722 }
721 723
722 724 var excludeCancelBtn = false;
723 725 var submitEvent = true;
724 726 commentForm.setActionButtonsDisabled(true, excludeCancelBtn, submitEvent);
725 727 commentForm.cm.setOption("readOnly", true);
726 728 var postData = {
727 729 'text': text,
728 730 'f_path': f_path,
729 731 'line': lineno,
730 732 'comment_type': commentType,
731 733 'csrf_token': CSRF_TOKEN
732 734 };
733 735 if (resolvesCommentId){
734 736 postData['resolves_comment_id'] = resolvesCommentId;
735 737 }
736 738
737 739 var submitSuccessCallback = function(json_data) {
738 740 $form.remove();
739 741 try {
740 742 var html = json_data.rendered_text;
741 743 var lineno = json_data.line_no;
742 744 var target_id = json_data.target_id;
743 745
744 746 $comments.find('.cb-comment-add-button').before(html);
745 747
746 748 //mark visually which comment was resolved
747 749 if (resolvesCommentId) {
748 750 commentForm.markCommentResolved(resolvesCommentId);
749 751 }
750 752
751 753 // run global callback on submit
752 754 commentForm.globalSubmitSuccessCallback();
753 755
754 756 } catch (e) {
755 757 console.error(e);
756 758 }
757 759
758 760 // re trigger the linkification of next/prev navigation
759 761 linkifyComments($('.inline-comment-injected'));
760 762 timeagoActivate();
761 763 commentForm.setActionButtonsDisabled(false);
762 764
763 765 };
764 766 var submitFailCallback = function(){
765 767 commentForm.resetCommentFormState(text)
766 768 };
767 769 commentForm.submitAjaxPOST(
768 770 commentForm.submitUrl, postData, submitSuccessCallback, submitFailCallback);
769 771 });
770 772 }
771 773
772 774 $form.addClass('comment-inline-form-open');
773 775 };
774 776
775 777 this.createResolutionComment = function(commentId){
776 778 // hide the trigger text
777 779 $('#resolve-comment-{0}'.format(commentId)).hide();
778 780
779 781 var comment = $('#comment-'+commentId);
780 782 var commentData = comment.data();
781 783 if (commentData.commentInline) {
782 784 this.createComment(comment, commentId)
783 785 } else {
784 786 Rhodecode.comments.createGeneralComment('general', "$placeholder", commentId)
785 787 }
786 788
787 789 return false;
788 790 };
789 791
790 792 this.submitResolution = function(commentId){
791 793 var form = $('#resolve_comment_{0}'.format(commentId)).closest('form');
792 794 var commentForm = form.get(0).CommentForm;
793 795
794 796 var cm = commentForm.getCmInstance();
795 797 var renderer = templateContext.visual.default_renderer;
796 798 if (renderer == 'rst'){
797 799 var commentUrl = '`#{0} <{1}#comment-{0}>`_'.format(commentId, commentForm.selfUrl);
798 800 } else if (renderer == 'markdown') {
799 801 var commentUrl = '[#{0}]({1}#comment-{0})'.format(commentId, commentForm.selfUrl);
800 802 } else {
801 803 var commentUrl = '{1}#comment-{0}'.format(commentId, commentForm.selfUrl);
802 804 }
803 805
804 806 cm.setValue(_gettext('TODO from comment {0} was fixed.').format(commentUrl));
805 807 form.submit();
806 808 return false;
807 809 };
808 810
809 811 };
General Comments 0
You need to be logged in to leave comments. Login now