summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/ide/components/new_dropdown/modal.vue
blob: bcd53ac1ba2ed42cc1dd3081ffbe913eafb2767a (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
<script>
import $ from 'jquery';
import { __ } from '~/locale';
import { mapActions, mapState, mapGetters } from 'vuex';
import GlModal from '~/vue_shared/components/gl_modal.vue';
import { modalTypes } from '../../constants';

export default {
  components: {
    GlModal,
  },
  data() {
    return {
      name: '',
    };
  },
  computed: {
    ...mapState(['entryModal']),
    ...mapGetters('fileTemplates', ['templateTypes']),
    entryName: {
      get() {
        if (this.entryModal.type === modalTypes.rename) {
          return this.name || this.entryModal.entry.name;
        }

        return this.name || (this.entryModal.path !== '' ? `${this.entryModal.path}/` : '');
      },
      set(val) {
        this.name = val;
      },
    },
    modalTitle() {
      if (this.entryModal.type === modalTypes.tree) {
        return __('Create new directory');
      } else if (this.entryModal.type === modalTypes.rename) {
        return this.entryModal.entry.type === modalTypes.tree
          ? __('Rename folder')
          : __('Rename file');
      }

      return __('Create new file');
    },
    buttonLabel() {
      if (this.entryModal.type === modalTypes.tree) {
        return __('Create directory');
      } else if (this.entryModal.type === modalTypes.rename) {
        return this.entryModal.entry.type === modalTypes.tree
          ? __('Rename folder')
          : __('Rename file');
      }

      return __('Create file');
    },
    isCreatingNew() {
      return this.entryModal.type !== modalTypes.rename;
    },
  },
  methods: {
    ...mapActions(['createTempEntry', 'renameEntry']),
    submitForm() {
      if (this.entryModal.type === modalTypes.rename) {
        this.renameEntry({
          path: this.entryModal.entry.path,
          name: this.entryName,
        });
      } else {
        this.createTempEntry({
          name: this.name,
          type: this.entryModal.type,
        });
      }
    },
    createFromTemplate(template) {
      this.createTempEntry({
        name: template.name,
        type: this.entryModal.type,
      });

      $('#ide-new-entry').modal('toggle');
    },
    focusInput() {
      this.$refs.fieldName.focus();
    },
    closedModal() {
      this.name = '';
    },
  },
};
</script>

<template>
  <gl-modal
    id="ide-new-entry"
    :header-title-text="modalTitle"
    :footer-primary-button-text="buttonLabel"
    footer-primary-button-variant="success"
    modal-size="lg"
    @submit="submitForm"
    @open="focusInput"
    @closed="closedModal"
  >
    <div
      class="form-group row"
    >
      <label class="label-bold col-form-label col-sm-2">
        {{ __('Name') }}
      </label>
      <div class="col-sm-10">
        <input
          ref="fieldName"
          v-model="entryName"
          type="text"
          class="form-control"
          placeholder="/dir/file_name"
        />
        <ul
          v-if="isCreatingNew"
          class="prepend-top-default list-inline"
        >
          <li
            v-for="(template, index) in templateTypes"
            :key="index"
            class="list-inline-item"
          >
            <button
              type="button"
              class="btn btn-missing p-1 pr-2 pl-2"
              @click="createFromTemplate(template)"
            >
              {{ template.name }}
            </button>
          </li>
        </ul>
      </div>
    </div>
  </gl-modal>
</template>