diff_file.vue 4.8 KB
Newer Older
F
Felipe Artur 已提交
1
<script>
2
import { mapActions, mapGetters, mapState } from 'vuex';
F
Felipe Artur 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
import _ from 'underscore';
import { __, sprintf } from '~/locale';
import createFlash from '~/flash';
import DiffFileHeader from './diff_file_header.vue';
import DiffContent from './diff_content.vue';

export default {
  components: {
    DiffFileHeader,
    DiffContent,
  },
  props: {
    file: {
      type: Object,
      required: true,
    },
19 20
    canCurrentUserFork: {
      type: Boolean,
F
Felipe Artur 已提交
21 22 23 24 25 26 27 28 29 30
      required: true,
    },
  },
  data() {
    return {
      isLoadingCollapsedDiff: false,
      forkMessageVisible: false,
    };
  },
  computed: {
31
    ...mapState('diffs', ['currentDiffFileId']),
32
    ...mapGetters(['isNotesFetched', 'discussionsStructuredByLineCode']),
F
Felipe Artur 已提交
33 34 35 36 37 38 39 40 41 42 43 44 45
    isCollapsed() {
      return this.file.collapsed || false;
    },
    viewBlobLink() {
      return sprintf(
        __('You can %{linkStart}view the blob%{linkEnd} instead.'),
        {
          linkStart: `<a href="${_.escape(this.file.viewPath)}">`,
          linkEnd: '</a>',
        },
        false,
      );
    },
46
    showExpandMessage() {
47
      return (
48
        this.isCollapsed ||
49 50 51 52
        (!this.file.highlightedDiffLines &&
          !this.isLoadingCollapsedDiff &&
          !this.file.tooLarge &&
          this.file.text)
53
      );
54
    },
55 56 57
    showLoadingIcon() {
      return this.isLoadingCollapsedDiff || (!this.file.renderIt && !this.isCollapsed);
    },
F
Felipe Artur 已提交
58 59
  },
  methods: {
60
    ...mapActions('diffs', ['loadCollapsedDiff', 'assignDiscussionsToDiff']),
F
Felipe Artur 已提交
61
    handleToggle() {
62 63
      const { highlightedDiffLines, parallelDiffLines } = this.file;
      if (!highlightedDiffLines && parallelDiffLines !== undefined && !parallelDiffLines.length) {
F
Felipe Artur 已提交
64 65 66
        this.handleLoadCollapsedDiff();
      } else {
        this.file.collapsed = !this.file.collapsed;
67
        this.file.renderIt = true;
F
Felipe Artur 已提交
68 69 70 71 72 73 74 75 76
      }
    },
    handleLoadCollapsedDiff() {
      this.isLoadingCollapsedDiff = true;

      this.loadCollapsedDiff(this.file)
        .then(() => {
          this.isLoadingCollapsedDiff = false;
          this.file.collapsed = false;
77
          this.file.renderIt = true;
F
Felipe Artur 已提交
78
        })
79 80 81 82 83 84 85 86
        .then(() => {
          requestIdleCallback(
            () => {
              this.assignDiscussionsToDiff(this.discussionsStructuredByLineCode);
            },
            { timeout: 1000 },
          );
        })
F
Felipe Artur 已提交
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
        .catch(() => {
          this.isLoadingCollapsedDiff = false;
          createFlash(__('Something went wrong on our end. Please try again!'));
        });
    },
    showForkMessage() {
      this.forkMessageVisible = true;
    },
    hideForkMessage() {
      this.forkMessageVisible = false;
    },
  },
};
</script>

<template>
  <div
    :id="file.fileHash"
105 106 107
    :class="{
      'is-active': currentDiffFileId === file.fileHash
    }"
F
Felipe Artur 已提交
108 109 110
    class="diff-file file-holder"
  >
    <diff-file-header
111
      :can-current-user-fork="canCurrentUserFork"
F
Felipe Artur 已提交
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
      :diff-file="file"
      :collapsible="true"
      :expanded="!isCollapsed"
      :add-merge-request-buttons="true"
      class="js-file-title file-title"
      @toggleFile="handleToggle"
      @showForkMessage="showForkMessage"
    />

    <div
      v-if="forkMessageVisible"
      class="js-file-fork-suggestion-section file-fork-suggestion">
      <span class="file-fork-suggestion-note">
        You're not allowed to <span class="js-file-fork-suggestion-section-action">edit</span>
        files in this project directly. Please fork this project,
        make your changes there, and submit a merge request.
      </span>
      <a
        :href="file.forkPath"
        class="js-fork-suggestion-button btn btn-grouped btn-inverted btn-success"
      >
        Fork
      </a>
      <button
        class="js-cancel-fork-suggestion-button btn btn-grouped"
        type="button"
        @click="hideForkMessage"
      >
        Cancel
      </button>
    </div>

    <diff-content
T
Tim Zallmann 已提交
145
      v-if="!isCollapsed && file.renderIt"
F
Felipe Artur 已提交
146 147 148
      :class="{ hidden: isCollapsed || file.tooLarge }"
      :diff-file="file"
    />
C
Clement Ho 已提交
149
    <gl-loading-icon
150
      v-if="showLoadingIcon"
F
Felipe Artur 已提交
151 152 153
      class="diff-content loading"
    />
    <div
154
      v-else-if="showExpandMessage"
F
Felipe Artur 已提交
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
      class="nothing-here-block diff-collapsed"
    >
      {{ __('This diff is collapsed.') }}
      <a
        class="click-to-expand js-click-to-expand"
        href="#"
        @click.prevent="handleToggle"
      >
        {{ __('Click to expand it.') }}
      </a>
    </div>
    <div
      v-if="file.tooLarge"
      class="nothing-here-block diff-collapsed js-too-large-diff"
    >
      {{ __('This source diff could not be displayed because it is too large.') }}
      <span v-html="viewBlobLink"></span>
    </div>
  </div>
</template>
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191

<style>
@keyframes shadow-fade {
  from {
    box-shadow: 0 0 4px #919191;
  }

  to {
    box-shadow: 0 0 0 #dfdfdf;
  }
}

.diff-file.is-active {
  box-shadow: 0 0 0 #dfdfdf;
  animation: shadow-fade 1.2s 0.1s 1;
}
</style>