2017-06-12 14:43:21 -04:00
|
|
|
/* Developer beware! Do not add logic to showButton or hideButton
|
2018-11-15 05:15:03 -05:00
|
|
|
* that will force a reflow. Doing so will create a significant performance
|
2017-06-12 14:43:21 -04:00
|
|
|
* bottleneck for pages with large diffs. For a comprehensive list of what
|
|
|
|
* causes reflows, visit https://gist.github.com/paulirish/5d52fb081b3570c81e3a
|
|
|
|
*/
|
|
|
|
|
2022-02-01 16:12:02 -05:00
|
|
|
import { getCookie } from '~/lib/utils/common_utils';
|
2017-09-27 10:38:13 -04:00
|
|
|
|
2017-06-12 14:43:21 -04:00
|
|
|
const LINE_NUMBER_CLASS = 'diff-line-num';
|
|
|
|
const UNFOLDABLE_LINE_CLASS = 'js-unfold';
|
|
|
|
const NO_COMMENT_CLASS = 'no-comment-btn';
|
|
|
|
const EMPTY_CELL_CLASS = 'empty-cell';
|
|
|
|
const OLD_LINE_CLASS = 'old_line';
|
|
|
|
const LINE_COLUMN_CLASSES = `.${LINE_NUMBER_CLASS}, .line_content`;
|
|
|
|
const DIFF_CONTAINER_SELECTOR = '.files';
|
|
|
|
const DIFF_EXPANDED_CLASS = 'diff-expanded';
|
|
|
|
|
|
|
|
export default {
|
|
|
|
init($diffFile) {
|
2017-10-19 05:46:43 -04:00
|
|
|
/* Caching is used only when the following members are *true*.
|
|
|
|
* This is because there are likely to be
|
|
|
|
* differently configured versions of diffs in the same session.
|
|
|
|
* However if these values are true, they
|
2017-06-12 14:43:21 -04:00
|
|
|
* will be true in all cases */
|
|
|
|
|
|
|
|
if (!this.userCanCreateNote) {
|
|
|
|
// data-can-create-note is an empty string when true, otherwise undefined
|
2018-10-24 15:17:03 -04:00
|
|
|
this.userCanCreateNote =
|
|
|
|
$diffFile.closest(DIFF_CONTAINER_SELECTOR).data('canCreateNote') === '';
|
2016-07-24 16:45:11 -04:00
|
|
|
}
|
|
|
|
|
2022-02-01 16:12:02 -05:00
|
|
|
this.isParallelView = getCookie('diff_view') === 'parallel';
|
2017-02-22 12:30:17 -05:00
|
|
|
|
2017-06-12 14:43:21 -04:00
|
|
|
if (this.userCanCreateNote) {
|
2018-10-24 15:17:03 -04:00
|
|
|
$diffFile
|
2020-12-23 16:10:24 -05:00
|
|
|
.on('mouseover', LINE_COLUMN_CLASSES, (e) => this.showButton(this.isParallelView, e))
|
|
|
|
.on('mouseleave', LINE_COLUMN_CLASSES, (e) => this.hideButton(this.isParallelView, e));
|
2017-03-11 02:30:44 -05:00
|
|
|
}
|
2017-06-12 14:43:21 -04:00
|
|
|
},
|
2016-07-24 16:45:11 -04:00
|
|
|
|
2017-06-12 14:43:21 -04:00
|
|
|
showButton(isParallelView, e) {
|
|
|
|
const buttonParentElement = this.getButtonParent(e.currentTarget, isParallelView);
|
2016-07-24 16:45:11 -04:00
|
|
|
|
2017-06-12 14:43:21 -04:00
|
|
|
if (!this.validateButtonParent(buttonParentElement)) return;
|
2016-08-05 08:02:08 -04:00
|
|
|
|
2017-06-12 14:43:21 -04:00
|
|
|
buttonParentElement.classList.add('is-over');
|
|
|
|
buttonParentElement.nextElementSibling.classList.add('is-over');
|
|
|
|
},
|
2016-07-24 16:45:11 -04:00
|
|
|
|
2017-06-12 14:43:21 -04:00
|
|
|
hideButton(isParallelView, e) {
|
|
|
|
const buttonParentElement = this.getButtonParent(e.currentTarget, isParallelView);
|
2017-02-22 12:30:17 -05:00
|
|
|
|
2017-06-12 14:43:21 -04:00
|
|
|
buttonParentElement.classList.remove('is-over');
|
|
|
|
buttonParentElement.nextElementSibling.classList.remove('is-over');
|
|
|
|
},
|
|
|
|
|
|
|
|
getButtonParent(hoveredElement, isParallelView) {
|
|
|
|
if (isParallelView) {
|
|
|
|
if (!hoveredElement.classList.contains(LINE_NUMBER_CLASS)) {
|
|
|
|
return hoveredElement.previousElementSibling;
|
|
|
|
}
|
|
|
|
} else if (!hoveredElement.classList.contains(OLD_LINE_CLASS)) {
|
|
|
|
return hoveredElement.parentNode.querySelector(`.${OLD_LINE_CLASS}`);
|
2016-07-24 16:45:11 -04:00
|
|
|
}
|
2017-06-12 14:43:21 -04:00
|
|
|
return hoveredElement;
|
|
|
|
},
|
|
|
|
|
|
|
|
validateButtonParent(buttonParentElement) {
|
2018-10-24 15:17:03 -04:00
|
|
|
return (
|
|
|
|
!buttonParentElement.classList.contains(EMPTY_CELL_CLASS) &&
|
2017-06-12 14:43:21 -04:00
|
|
|
!buttonParentElement.classList.contains(UNFOLDABLE_LINE_CLASS) &&
|
|
|
|
!buttonParentElement.classList.contains(NO_COMMENT_CLASS) &&
|
2018-10-24 15:17:03 -04:00
|
|
|
!buttonParentElement.parentNode.classList.contains(DIFF_EXPANDED_CLASS)
|
|
|
|
);
|
2017-06-12 14:43:21 -04:00
|
|
|
},
|
2017-03-11 02:30:44 -05:00
|
|
|
};
|