debian-mirror-gitlab/app/assets/javascripts/diffs/components/diff_expansion_cell.vue

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

308 lines
9 KiB
Vue
Raw Normal View History

2019-10-12 21:52:04 +05:30
<script>
2022-07-16 23:28:13 +05:30
import { GlTooltipDirective, GlSafeHtmlDirective, GlIcon, GlLoadingIcon } from '@gitlab/ui';
import { mapActions } from 'vuex';
2021-09-04 01:27:46 +05:30
import createFlash from '~/flash';
2020-11-24 15:15:51 +05:30
import { s__, sprintf } from '~/locale';
import glFeatureFlagsMixin from '~/vue_shared/mixins/gl_feature_flags_mixin';
2022-07-16 23:28:13 +05:30
import { UNFOLD_COUNT, INLINE_DIFF_LINES_KEY } from '../constants';
2019-10-12 21:52:04 +05:30
import * as utils from '../store/utils';
const EXPAND_ALL = 0;
const EXPAND_UP = 1;
const EXPAND_DOWN = 2;
export default {
2022-07-16 23:28:13 +05:30
i18n: {
showMore: sprintf(s__('Diffs|Show %{unfoldCount} lines'), { unfoldCount: UNFOLD_COUNT }),
showAll: s__('Diffs|Show all unchanged lines'),
},
2019-10-12 21:52:04 +05:30
components: {
2020-11-24 15:15:51 +05:30
GlIcon,
2022-07-16 23:28:13 +05:30
GlLoadingIcon,
},
directives: {
GlTooltip: GlTooltipDirective,
SafeHtml: GlSafeHtmlDirective,
2019-10-12 21:52:04 +05:30
},
2020-11-24 15:15:51 +05:30
mixins: [glFeatureFlagsMixin()],
2019-10-12 21:52:04 +05:30
props: {
2022-07-16 23:28:13 +05:30
file: {
type: Object,
2019-10-12 21:52:04 +05:30
required: true,
},
line: {
type: Object,
required: true,
},
isTop: {
type: Boolean,
required: false,
default: false,
},
isBottom: {
type: Boolean,
required: false,
default: false,
},
2022-07-16 23:28:13 +05:30
inline: {
type: Boolean,
required: true,
},
lineCountBetween: {
type: Number,
required: false,
default: -1,
},
},
data() {
return { loading: { up: false, down: false, all: false } };
2019-10-12 21:52:04 +05:30
},
computed: {
canExpandUp() {
return !this.isBottom;
},
canExpandDown() {
return this.isBottom || !this.isTop;
},
2022-07-16 23:28:13 +05:30
isLineCountSmall() {
return this.lineCountBetween >= 20 || this.lineCountBetween === -1;
},
showExpandDown() {
return this.canExpandDown && this.isLineCountSmall;
},
showExpandUp() {
return this.canExpandUp && this.isLineCountSmall;
},
2019-10-12 21:52:04 +05:30
},
methods: {
...mapActions('diffs', ['loadMoreLines']),
getPrevLineNumber(oldLineNumber, newLineNumber) {
2022-07-16 23:28:13 +05:30
const index = utils.getPreviousLineIndex(this.file, {
2019-10-12 21:52:04 +05:30
oldLineNumber,
newLineNumber,
});
2020-04-08 14:13:33 +05:30
2022-07-16 23:28:13 +05:30
return this.file[INLINE_DIFF_LINES_KEY][index - 2]?.new_line || 0;
2019-10-12 21:52:04 +05:30
},
callLoadMoreLines(
endpoint,
params,
lineNumbers,
fileHash,
isExpandDown = false,
nextLineNumbers = {},
) {
this.loadMoreLines({ endpoint, params, lineNumbers, fileHash, isExpandDown, nextLineNumbers })
.then(() => {
this.isRequesting = false;
})
.catch(() => {
2021-09-04 01:27:46 +05:30
createFlash({
message: s__('Diffs|Something went wrong while fetching diff lines.'),
});
2019-10-12 21:52:04 +05:30
this.isRequesting = false;
2022-07-16 23:28:13 +05:30
})
.finally(() => {
this.loading = { up: false, down: false, all: false };
2019-10-12 21:52:04 +05:30
});
},
handleExpandLines(type = EXPAND_ALL) {
if (this.isRequesting) {
return;
}
this.isRequesting = true;
2022-07-16 23:28:13 +05:30
const endpoint = this.file.context_lines_path;
2019-10-12 21:52:04 +05:30
const oldLineNumber = this.line.meta_data.old_pos || 0;
const newLineNumber = this.line.meta_data.new_pos || 0;
const offset = newLineNumber - oldLineNumber;
2022-07-16 23:28:13 +05:30
const expandOptions = { endpoint, oldLineNumber, newLineNumber, offset };
2019-10-12 21:52:04 +05:30
if (type === EXPAND_UP) {
2022-07-16 23:28:13 +05:30
this.loading.up = true;
2019-10-12 21:52:04 +05:30
this.handleExpandUpLines(expandOptions);
} else if (type === EXPAND_DOWN) {
2022-07-16 23:28:13 +05:30
this.loading.down = true;
2019-10-12 21:52:04 +05:30
this.handleExpandDownLines(expandOptions);
} else {
2022-07-16 23:28:13 +05:30
this.loading.all = true;
2019-10-12 21:52:04 +05:30
this.handleExpandAllLines(expandOptions);
}
},
2020-04-08 14:13:33 +05:30
handleExpandUpLines(expandOptions) {
2022-07-16 23:28:13 +05:30
const { endpoint, oldLineNumber, newLineNumber, offset } = expandOptions;
2019-10-12 21:52:04 +05:30
const bottom = this.isBottom;
const lineNumber = newLineNumber - 1;
const to = lineNumber;
let since = lineNumber - UNFOLD_COUNT;
let unfold = true;
const prevLineNumber = this.getPrevLineNumber(oldLineNumber, newLineNumber);
if (since <= prevLineNumber + 1) {
since = prevLineNumber + 1;
unfold = false;
}
2022-07-16 23:28:13 +05:30
const params = { since, to, bottom, offset, unfold };
2019-10-12 21:52:04 +05:30
const lineNumbers = { oldLineNumber, newLineNumber };
2022-07-16 23:28:13 +05:30
this.callLoadMoreLines(endpoint, params, lineNumbers, this.file.file_hash);
2019-10-12 21:52:04 +05:30
},
handleExpandDownLines(expandOptions) {
const {
endpoint,
oldLineNumber: metaOldPos,
newLineNumber: metaNewPos,
offset,
} = expandOptions;
const bottom = true;
const nextLineNumbers = {
old_line: metaOldPos,
new_line: metaNewPos,
};
let unfold = true;
let isExpandDown = false;
let oldLineNumber = metaOldPos;
let newLineNumber = metaNewPos;
let lineNumber = metaNewPos + 1;
let since = lineNumber;
let to = lineNumber + UNFOLD_COUNT;
if (!this.isBottom) {
const prevLineNumber = this.getPrevLineNumber(oldLineNumber, newLineNumber);
isExpandDown = true;
oldLineNumber = prevLineNumber - offset;
newLineNumber = prevLineNumber;
lineNumber = prevLineNumber + 1;
since = lineNumber;
to = lineNumber + UNFOLD_COUNT;
if (to >= metaNewPos) {
to = metaNewPos - 1;
unfold = false;
}
}
2022-07-16 23:28:13 +05:30
const params = { since, to, bottom, offset, unfold };
2019-10-12 21:52:04 +05:30
const lineNumbers = { oldLineNumber, newLineNumber };
this.callLoadMoreLines(
endpoint,
params,
lineNumbers,
2022-07-16 23:28:13 +05:30
this.file.file_hash,
2019-10-12 21:52:04 +05:30
isExpandDown,
nextLineNumbers,
);
},
handleExpandAllLines(expandOptions) {
2022-07-16 23:28:13 +05:30
const { endpoint, oldLineNumber, newLineNumber, offset } = expandOptions;
2019-10-12 21:52:04 +05:30
const bottom = this.isBottom;
const unfold = false;
let since;
let to;
if (this.isTop) {
since = 1;
to = newLineNumber - 1;
} else if (bottom) {
since = newLineNumber + 1;
to = -1;
} else {
const prevLineNumber = this.getPrevLineNumber(oldLineNumber, newLineNumber);
since = prevLineNumber + 1;
to = newLineNumber - 1;
}
2022-07-16 23:28:13 +05:30
const params = { since, to, bottom, offset, unfold };
2019-10-12 21:52:04 +05:30
const lineNumbers = { oldLineNumber, newLineNumber };
2022-07-16 23:28:13 +05:30
this.callLoadMoreLines(endpoint, params, lineNumbers, this.file.file_hash);
2019-10-12 21:52:04 +05:30
},
},
2022-07-16 23:28:13 +05:30
EXPAND_DOWN,
EXPAND_UP,
2019-10-12 21:52:04 +05:30
};
</script>
<template>
2022-07-16 23:28:13 +05:30
<div
v-if="glFeatures.updatedDiffExpansionButtons"
class="diff-grid-row diff-grid-row-full diff-tr line_holder match expansion"
>
<div :class="{ parallel: !inline }" class="diff-grid-left diff-grid-2-col left-side">
<div
class="diff-td diff-line-num gl-text-center! gl-p-0! gl-w-full! gl-display-flex gl-flex-direction-column"
>
<button
v-if="showExpandDown"
v-gl-tooltip.left
:title="s__('Diffs|Next 20 lines')"
type="button"
class="js-unfold-down gl-rounded-0 gl-border-0 diff-line-expand-button"
@click="handleExpandLines($options.EXPAND_DOWN)"
>
<gl-loading-icon v-if="loading.down" size="sm" color="dark" inline />
<gl-icon v-else name="expand-down" />
</button>
<button
v-if="lineCountBetween !== -1 && lineCountBetween < 20"
v-gl-tooltip.left
:title="s__('Diffs|Expand all lines')"
type="button"
class="js-unfold-all gl-rounded-0 gl-border-0 diff-line-expand-button"
@click="handleExpandLines()"
>
<gl-loading-icon v-if="loading.all" size="sm" color="dark" inline />
<gl-icon v-else name="expand" />
</button>
<button
v-if="showExpandUp"
v-gl-tooltip.left
:title="s__('Diffs|Previous 20 lines')"
type="button"
class="js-unfold gl-rounded-0 gl-border-0 diff-line-expand-button"
@click="handleExpandLines($options.EXPAND_UP)"
>
<gl-loading-icon v-if="loading.up" size="sm" color="dark" inline />
<gl-icon v-else name="expand-up" />
</button>
</div>
<div
v-safe-html="line.rich_text"
class="gl-display-flex! gl-flex-direction-column gl-justify-content-center diff-td line_content left-side gl-white-space-normal!"
></div>
</div>
</div>
<div v-else class="content js-line-expansion-content">
2022-04-04 11:22:00 +05:30
<button
type="button"
:disabled="!canExpandDown"
class="js-unfold-down gl-mx-2 gl-py-4 gl-cursor-pointer"
2022-07-16 23:28:13 +05:30
@click="handleExpandLines($options.EXPAND_DOWN)"
2021-01-29 00:20:46 +05:30
>
2021-02-22 17:27:13 +05:30
<gl-icon :size="12" name="expand-down" />
2021-01-29 00:20:46 +05:30
<span>{{ $options.i18n.showMore }}</span>
2022-04-04 11:22:00 +05:30
</button>
<button
type="button"
class="js-unfold-all gl-mx-2 gl-py-4 gl-cursor-pointer"
@click="handleExpandLines()"
>
2021-02-22 17:27:13 +05:30
<gl-icon :size="12" name="expand" />
2021-01-29 00:20:46 +05:30
<span>{{ $options.i18n.showAll }}</span>
2022-04-04 11:22:00 +05:30
</button>
<button
type="button"
:disabled="!canExpandUp"
class="js-unfold gl-mx-2 gl-py-4 gl-cursor-pointer"
2022-07-16 23:28:13 +05:30
@click="handleExpandLines($options.EXPAND_UP)"
2021-01-29 00:20:46 +05:30
>
2021-02-22 17:27:13 +05:30
<gl-icon :size="12" name="expand-up" />
2021-01-29 00:20:46 +05:30
<span>{{ $options.i18n.showMore }}</span>
2022-04-04 11:22:00 +05:30
</button>
2021-01-29 00:20:46 +05:30
</div>
2019-10-12 21:52:04 +05:30
</template>