summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/diffs/components/changed_files.vue
blob: 97751db1254d5c94b614c030e4dd45ab40dff9aa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
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
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
<script>
import { mapGetters, mapActions } from 'vuex';
import ClipboardButton from '~/vue_shared/components/clipboard_button.vue';
import Icon from '~/vue_shared/components/icon.vue';
import { pluralize } from '~/lib/utils/text_utility';
import { getParameterValues, mergeUrlParams } from '~/lib/utils/url_utility';
import { contentTop } from '~/lib/utils/common_utils';
import { __ } from '~/locale';
import ChangedFilesDropdown from './changed_files_dropdown.vue';
import changedFilesMixin from '../mixins/changed_files';

export default {
  components: {
    Icon,
    ChangedFilesDropdown,
    ClipboardButton,
  },
  mixins: [changedFilesMixin],
  data() {
    return {
      isStuck: false,
      maxWidth: 'auto',
      offsetTop: 0,
    };
  },
  computed: {
    ...mapGetters('diffs', ['isInlineView', 'isParallelView', 'areAllFilesCollapsed']),
    sumAddedLines() {
      return this.sumValues('addedLines');
    },
    sumRemovedLines() {
      return this.sumValues('removedLines');
    },
    whitespaceVisible() {
      return !getParameterValues('w')[0];
    },
    toggleWhitespaceText() {
      if (this.whitespaceVisible) {
        return __('Hide whitespace changes');
      }
      return __('Show whitespace changes');
    },
    toggleWhitespacePath() {
      if (this.whitespaceVisible) {
        return mergeUrlParams({ w: 1 }, window.location.href);
      }

      return mergeUrlParams({ w: 0 }, window.location.href);
    },
    top() {
      return `${this.offsetTop}px`;
    },
  },
  created() {
    document.addEventListener('scroll', this.handleScroll);
    this.offsetTop = contentTop();
  },
  beforeDestroy() {
    document.removeEventListener('scroll', this.handleScroll);
  },
  methods: {
    ...mapActions('diffs', ['setInlineDiffViewType', 'setParallelDiffViewType', 'expandAllFiles']),
    pluralize,
    handleScroll() {
      if (!this.updating) {
        this.$nextTick(this.updateIsStuck);
        this.updating = true;
      }
    },
    updateIsStuck() {
      if (!this.$refs.wrapper) {
        return;
      }

      const scrollPosition = window.scrollY;

      this.isStuck = scrollPosition + this.offsetTop >= this.$refs.placeholder.offsetTop;
      this.updating = false;
    },
    sumValues(key) {
      return this.diffFiles.reduce((total, file) => total + file[key], 0);
    },
  },
};
</script>

<template>
  <span>
    <div ref="placeholder"></div>
    <div
      ref="wrapper"
      :style="{ top }"
      :class="{'is-stuck': isStuck}"
      class="content-block oneline-block diff-files-changed diff-files-changed-merge-request
      files-changed js-diff-files-changed"
    >
      <div class="files-changed-inner">
        <div
          class="inline-parallel-buttons d-none d-md-block"
        >
          <a
            v-if="areAllFilesCollapsed"
            class="btn btn-default"
            @click="expandAllFiles"
          >
            {{ __('Expand all') }}
          </a>
          <a
            :href="toggleWhitespacePath"
            class="btn btn-default"
          >
            {{ toggleWhitespaceText }}
          </a>
          <div class="btn-group">
            <button
              id="inline-diff-btn"
              :class="{ active: isInlineView }"
              type="button"
              class="btn js-inline-diff-button"
              data-view-type="inline"
              @click="setInlineDiffViewType"
            >
              {{ __('Inline') }}
            </button>
            <button
              id="parallel-diff-btn"
              :class="{ active: isParallelView }"
              type="button"
              class="btn js-parallel-diff-button"
              data-view-type="parallel"
              @click="setParallelDiffViewType"
            >
              {{ __('Side-by-side') }}
            </button>
          </div>
        </div>

        <div class="commit-stat-summary dropdown">
          <changed-files-dropdown
            :diff-files="diffFiles"
          />

          <span
            class="js-diff-stats-additions-deletions-expanded
            diff-stats-additions-deletions-expanded"
          >
            with
            <strong class="cgreen">
              {{ pluralize(`${sumAddedLines} addition`, sumAddedLines) }}
            </strong>
            and
            <strong class="cred">
              {{ pluralize(`${sumRemovedLines} deletion`, sumRemovedLines) }}
            </strong>
          </span>
          <div
            class="js-diff-stats-additions-deletions-collapsed
            diff-stats-additions-deletions-collapsed float-right d-sm-none"
          >
            <strong class="cgreen">
              +{{ sumAddedLines }}
            </strong>
            <strong class="cred">
              -{{ sumRemovedLines }}
            </strong>
          </div>
        </div>
      </div>
    </div>
  </span>
</template>