summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/repo/components/new_branch_form.vue
blob: eac43e692b039c7d274b384210059467f01ef04c (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
<script>
  import flash, { hideFlash } from '../../flash';
  import loadingIcon from '../../vue_shared/components/loading_icon.vue';
  import eventHub from '../event_hub';

  export default {
    components: {
      loadingIcon,
    },
    props: {
      currentBranch: {
        type: String,
        required: true,
      },
    },
    data() {
      return {
        branchName: '',
        loading: false,
      };
    },
    computed: {
      btnDisabled() {
        return this.loading || this.branchName === '';
      },
    },
    methods: {
      toggleDropdown() {
        this.$dropdown.dropdown('toggle');
      },
      submitNewBranch() {
        // need to query as the element is appended outside of Vue
        const flashEl = this.$refs.flashContainer.querySelector('.flash-alert');

        this.loading = true;

        if (flashEl) {
          hideFlash(flashEl, false);
        }

        eventHub.$emit('createNewBranch', this.branchName);
      },
      showErrorMessage(message) {
        this.loading = false;
        flash(message, 'alert', this.$el);
      },
      createdNewBranch(newBranchName) {
        this.loading = false;
        this.branchName = '';

        if (this.dropdownText) {
          this.dropdownText.textContent = newBranchName;
        }
      },
    },
    created() {
      // Dropdown is outside of Vue instance & is controlled by Bootstrap
      this.$dropdown = $('.git-revision-dropdown');

      // text element is outside Vue app
      this.dropdownText = document.querySelector('.project-refs-form .dropdown-toggle-text');

      eventHub.$on('createNewBranchSuccess', this.createdNewBranch);
      eventHub.$on('createNewBranchError', this.showErrorMessage);
      eventHub.$on('toggleNewBranchDropdown', this.toggleDropdown);
    },
    destroyed() {
      eventHub.$off('createNewBranchSuccess', this.createdNewBranch);
      eventHub.$off('toggleNewBranchDropdown', this.toggleDropdown);
      eventHub.$off('createNewBranchError', this.showErrorMessage);
    },
  };
</script>

<template>
  <div>
    <div
      class="flash-container"
      ref="flashContainer"
    >
    </div>
    <p>
      Create from:
      <code>{{ currentBranch }}</code>
    </p>
    <input
      class="form-control js-new-branch-name"
      type="text"
      placeholder="Name new branch"
      v-model="branchName"
      @keyup.enter.stop.prevent="submitNewBranch"
    />
    <div class="prepend-top-default clearfix">
      <button
        type="button"
        class="btn btn-primary pull-left"
        :disabled="btnDisabled"
        @click.stop.prevent="submitNewBranch"
      >
        <loading-icon
          v-if="loading"
          :inline="true"
        />
        <span>Create</span>
      </button>
      <button
        type="button"
        class="btn btn-default pull-right"
        @click.stop.prevent="toggleDropdown"
      >
        Cancel
      </button>
    </div>
  </div>
</template>