diff_file_header.vue 6.8 KB
Newer Older
F
Felipe Artur 已提交
1 2
<script>
import _ from 'underscore';
3
import { mapActions, mapGetters } from 'vuex';
F
Felipe Artur 已提交
4 5
import ClipboardButton from '~/vue_shared/components/clipboard_button.vue';
import Icon from '~/vue_shared/components/icon.vue';
T
Tim Zallmann 已提交
6
import FileIcon from '~/vue_shared/components/file_icon.vue';
F
Felipe Artur 已提交
7 8 9 10 11 12 13 14 15 16
import Tooltip from '~/vue_shared/directives/tooltip';
import { truncateSha } from '~/lib/utils/text_utility';
import { __, s__, sprintf } from '~/locale';
import EditButton from './edit_button.vue';

export default {
  components: {
    ClipboardButton,
    EditButton,
    Icon,
T
Tim Zallmann 已提交
17
    FileIcon,
F
Felipe Artur 已提交
18 19 20 21 22
  },
  directives: {
    Tooltip,
  },
  props: {
23 24 25 26 27
    discussionPath: {
      type: String,
      required: false,
      default: '',
    },
F
Felipe Artur 已提交
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
    diffFile: {
      type: Object,
      required: true,
    },
    collapsible: {
      type: Boolean,
      required: false,
      default: false,
    },
    addMergeRequestButtons: {
      type: Boolean,
      required: false,
      default: false,
    },
    expanded: {
      type: Boolean,
      required: false,
      default: true,
    },
47 48
    canCurrentUserFork: {
      type: Boolean,
F
Felipe Artur 已提交
49 50 51 52 53 54 55 56 57
      required: true,
    },
  },
  data() {
    return {
      blobForkSuggestion: null,
    };
  },
  computed: {
58
    ...mapGetters('diffs', ['diffHasExpandedDiscussions', 'diffHasDiscussions']),
59 60 61
    hasExpandedDiscussions() {
      return this.diffHasExpandedDiscussions(this.diffFile);
    },
F
Felipe Artur 已提交
62 63 64 65 66 67 68 69 70 71 72
    icon() {
      if (this.diffFile.submodule) {
        return 'archive';
      }

      return this.diffFile.blob.icon;
    },
    titleLink() {
      if (this.diffFile.submodule) {
        return this.diffFile.submoduleTreeUrl || this.diffFile.submoduleLink;
      }
73
      return this.discussionPath;
F
Felipe Artur 已提交
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
    },
    filePath() {
      if (this.diffFile.submodule) {
        return `${this.diffFile.filePath} @ ${truncateSha(this.diffFile.blob.id)}`;
      }

      if (this.diffFile.deletedFile) {
        return sprintf(__('%{filePath} deleted'), { filePath: this.diffFile.filePath }, false);
      }

      return this.diffFile.filePath;
    },
    titleTag() {
      return this.diffFile.fileHash ? 'a' : 'span';
    },
    isUsingLfs() {
      return this.diffFile.storedExternally && this.diffFile.externalStorage === 'lfs';
    },
    collapseIcon() {
      return this.expanded ? 'chevron-down' : 'chevron-right';
    },
    viewFileButtonText() {
      const truncatedContentSha = _.escape(truncateSha(this.diffFile.contentSha));
      return sprintf(
        s__('MergeRequests|View file @ %{commitId}'),
        {
          commitId: `<span class="commit-sha">${truncatedContentSha}</span>`,
        },
        false,
      );
    },
    viewReplacedFileButtonText() {
      const truncatedBaseSha = _.escape(truncateSha(this.diffFile.diffRefs.baseSha));
      return sprintf(
        s__('MergeRequests|View replaced file @ %{commitId}'),
        {
          commitId: `<span class="commit-sha">${truncatedBaseSha}</span>`,
        },
        false,
      );
    },
115 116 117
    gfmCopyText() {
      return `\`${this.diffFile.filePath}\``;
    },
F
Felipe Artur 已提交
118 119
  },
  methods: {
120
    ...mapActions('diffs', ['toggleFileDiscussions']),
121
    handleToggleFile(e, checkTarget) {
122 123 124 125 126
      if (
        !checkTarget ||
        e.target === this.$refs.header ||
        (e.target.classList && e.target.classList.contains('diff-toggle-caret'))
      ) {
F
Felipe Artur 已提交
127 128 129 130 131 132
        this.$emit('toggleFile');
      }
    },
    showForkMessage() {
      this.$emit('showForkMessage');
    },
133
    handleToggleDiscussions() {
134 135
      this.toggleFileDiscussions(this.diffFile);
    },
F
Felipe Artur 已提交
136 137 138 139 140 141 142 143
  },
};
</script>

<template>
  <div
    ref="header"
    class="js-file-title file-title file-title-flex-parent"
144
    @click="handleToggleFile($event, true)"
F
Felipe Artur 已提交
145 146 147 148 149 150 151
  >
    <div class="file-header-content">
      <icon
        v-if="collapsible"
        :name="collapseIcon"
        :size="16"
        aria-hidden="true"
T
Tim Zallmann 已提交
152
        class="diff-toggle-caret append-right-5"
F
Felipe Artur 已提交
153 154 155
        @click.stop="handleToggle"
      />
      <a
156
        v-once
F
Felipe Artur 已提交
157 158
        ref="titleWrapper"
        :href="titleLink"
159
        class="append-right-4 js-title-wrapper"
F
Felipe Artur 已提交
160
      >
T
Tim Zallmann 已提交
161 162 163
        <file-icon
          :file-name="filePath"
          :size="18"
F
Felipe Artur 已提交
164
          aria-hidden="true"
T
Tim Zallmann 已提交
165 166
          css-classes="js-file-icon append-right-5"
        />
F
Felipe Artur 已提交
167 168 169 170 171 172
        <span v-if="diffFile.renamedFile">
          <strong
            v-tooltip
            :title="diffFile.oldPath"
            class="file-title-name"
            data-container="body"
173 174
            v-html="diffFile.oldPathHtml"
          ></strong>
F
Felipe Artur 已提交
175 176 177 178 179 180

          <strong
            v-tooltip
            :title="diffFile.newPath"
            class="file-title-name"
            data-container="body"
181 182
            v-html="diffFile.newPathHtml"
          ></strong>
F
Felipe Artur 已提交
183 184 185 186
        </span>

        <strong
          v-else
M
Mike Greiling 已提交
187
          v-tooltip
F
Felipe Artur 已提交
188 189 190 191 192 193 194 195 196 197 198
          :title="filePath"
          class="file-title-name"
          data-container="body"
        >
          {{ filePath }}
        </strong>
      </a>

      <clipboard-button
        :title="__('Copy file path to clipboard')"
        :text="diffFile.filePath"
199
        :gfm="gfmCopyText"
F
Felipe Artur 已提交
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
        css-class="btn-default btn-transparent btn-clipboard"
      />

      <small
        v-if="diffFile.modeChanged"
        ref="fileMode"
      >
        {{ diffFile.aMode }}{{ diffFile.bMode }}
      </small>

      <span
        v-if="isUsingLfs"
        class="label label-lfs append-right-5"
      >
        {{ __('LFS') }}
      </span>
    </div>

    <div
      v-if="!diffFile.submodule && addMergeRequestButtons"
220
      class="file-actions d-none d-sm-block"
F
Felipe Artur 已提交
221 222 223 224 225
    >
      <template
        v-if="diffFile.blob && diffFile.blob.readableText"
      >
        <button
226
          :disabled="!diffHasDiscussions(diffFile)"
227
          :class="{ active: hasExpandedDiscussions }"
F
Felipe Artur 已提交
228
          :title="s__('MergeRequests|Toggle comments for this file')"
229
          class="js-btn-vue-toggle-comments btn"
F
Felipe Artur 已提交
230
          type="button"
231
          @click="handleToggleDiscussions"
F
Felipe Artur 已提交
232 233 234 235 236 237
        >
          <icon name="comment" />
        </button>

        <edit-button
          v-if="!diffFile.deletedFile"
238
          :can-current-user-fork="canCurrentUserFork"
F
Felipe Artur 已提交
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
          :edit-path="diffFile.editPath"
          :can-modify-blob="diffFile.canModifyBlob"
          @showForkMessage="showForkMessage"
        />
      </template>

      <a
        v-if="diffFile.replacedViewPath"
        :href="diffFile.replacedViewPath"
        class="btn view-file js-view-file"
        v-html="viewReplacedFileButtonText"
      >
      </a>
      <a
        :href="diffFile.viewPath"
        class="btn view-file js-view-file"
        v-html="viewFileButtonText"
      >
      </a>

      <a
        v-if="diffFile.externalUrl"
M
Mike Greiling 已提交
261
        v-tooltip
F
Felipe Artur 已提交
262 263 264 265 266 267 268 269 270 271 272
        :href="diffFile.externalUrl"
        :title="`View on ${diffFile.formattedExternalUrl}`"
        target="_blank"
        rel="noopener noreferrer"
        class="btn btn-file-option"
      >
        <icon name="external-link" />
      </a>
    </div>
  </div>
</template>