summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/ide/components/commit_sidebar/form.vue
blob: 71809ea0900d407c65236d55de97240f7905d8a5 (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
<script>
import { mapState, mapActions, mapGetters } from 'vuex';
import { sprintf, __ } from '~/locale';
import LoadingButton from '~/vue_shared/components/loading_button.vue';
import CommitMessageField from './message_field.vue';
import Actions from './actions.vue';
import { activityBarViews } from '../../constants';

export default {
  components: {
    Actions,
    LoadingButton,
    CommitMessageField,
  },
  data() {
    return {
      isCompact: true,
      componentHeight: null,
    };
  },
  computed: {
    ...mapState(['changedFiles', 'stagedFiles', 'currentActivityView']),
    ...mapState('commit', ['commitMessage', 'submitCommitLoading']),
    ...mapGetters(['hasChanges']),
    ...mapGetters('commit', ['commitButtonDisabled', 'discardDraftButtonDisabled']),
    overviewText() {
      return sprintf(
        __(
          '<strong>%{changedFilesLength} unstaged</strong> and <strong>%{stagedFilesLength} staged</strong> changes',
        ),
        {
          stagedFilesLength: this.stagedFiles.length,
          changedFilesLength: this.changedFiles.length,
        },
      );
    },
  },
  watch: {
    currentActivityView() {
      this.isCompact = !(
        this.currentActivityView === activityBarViews.commit && window.innerHeight >= 750
      );
    },
  },
  methods: {
    ...mapActions(['updateActivityBarView']),
    ...mapActions('commit', ['updateCommitMessage', 'discardDraft', 'commitChanges']),
    toggleIsSmall() {
      this.updateActivityBarView(activityBarViews.commit)
        .then(() => {
          this.isCompact = !this.isCompact;
        })
        .catch(e => {
          throw e;
        });
    },
    beforeEnterTransition() {
      const elHeight = this.isCompact
        ? this.$refs.formEl && this.$refs.formEl.offsetHeight
        : this.$refs.compactEl && this.$refs.compactEl.offsetHeight;

      this.componentHeight = elHeight + 32;
    },
    enterTransition() {
      this.$nextTick(() => {
        const elHeight = this.isCompact
          ? this.$refs.compactEl && this.$refs.compactEl.offsetHeight
          : this.$refs.formEl && this.$refs.formEl.offsetHeight;

        this.componentHeight = elHeight + 32;
      });
    },
    afterEndTransition() {
      this.componentHeight = null;
    },
  },
  activityBarViews,
};
</script>

<template>
  <div
    class="multi-file-commit-form"
    :class="{
      'is-compact': isCompact,
      'is-full': !isCompact
    }"
    :style="{
      height: componentHeight ? `${componentHeight}px` : null,
    }"
  >
    <transition
      name="commit-form-slide-up"
      @before-enter="beforeEnterTransition"
      @enter="enterTransition"
      @after-enter="afterEndTransition"
    >
      <div
        v-if="isCompact"
        class="commit-form-compact"
        ref="compactEl"
      >
        <button
          type="button"
          :disabled="!hasChanges"
          class="btn btn-primary btn-sm btn-block"
          @click="toggleIsSmall"
        >
          {{ __('Commit') }}
        </button>
        <p
          class="text-center"
          v-html="overviewText"
        ></p>
      </div>
      <form
        v-if="!isCompact"
        class="form-horizontal"
        @submit.prevent.stop="commitChanges"
        ref="formEl"
      >
        <commit-message-field
          :text="commitMessage"
          @input="updateCommitMessage"
        />
        <div class="clearfix prepend-top-15">
          <actions />
          <loading-button
            :loading="submitCommitLoading"
            :disabled="commitButtonDisabled"
            container-class="btn btn-success btn-sm pull-left"
            :label="__('Commit')"
            @click="commitChanges"
          />
          <button
            v-if="!discardDraftButtonDisabled"
            type="button"
            class="btn btn-default btn-sm pull-right"
            @click="discardDraft"
          >
            {{ __('Discard draft') }}
          </button>
          <button
            v-else
            type="button"
            class="btn btn-default btn-sm pull-right"
            @click="toggleIsSmall"
          >
            {{ __('Collapse') }}
          </button>
        </div>
      </form>
    </transition>
  </div>
</template>