summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/ide/components/new_dropdown/modal.vue
blob: dbfaeba97087a082ecca59a3b7e75d41b290f5a9 (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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
<script>
import { GlModal, GlButton } from '@gitlab/ui';
import { mapActions, mapState, mapGetters } from 'vuex';
import { createAlert } from '~/flash';
import { __, sprintf } from '~/locale';
import { modalTypes } from '../../constants';
import { trimPathComponents, getPathParent } from '../../utils';

const i18n = {
  cancelButtonText: __('Cancel'),
};

export default {
  components: {
    GlModal,
    GlButton,
  },
  data() {
    return {
      entryName: '',
      modalType: modalTypes.blob,
      path: '',
    };
  },
  computed: {
    ...mapState(['entries']),
    ...mapGetters('fileTemplates', ['templateTypes']),
    modalTitle() {
      const entry = this.entries[this.path];

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

      return __('Create new file');
    },
    buttonLabel() {
      const entry = this.entries[this.path];

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

      return __('Create file');
    },
    actionPrimary() {
      return {
        text: this.buttonLabel,
        attributes: [{ variant: 'confirm' }],
      };
    },
    actionCancel() {
      return {
        text: i18n.cancelButtonText,
        attributes: [{ variant: 'default' }],
      };
    },
    isCreatingNewFile() {
      return this.modalType === modalTypes.blob;
    },
    placeholder() {
      return this.isCreatingNewFile ? 'dir/file_name' : 'dir/';
    },
  },
  methods: {
    ...mapActions(['createTempEntry', 'renameEntry']),
    submitAndClose() {
      this.submitForm();
      this.close();
    },
    submitForm() {
      this.entryName = trimPathComponents(this.entryName);

      if (this.modalType === modalTypes.rename) {
        if (this.entries[this.entryName] && !this.entries[this.entryName].deleted) {
          createAlert({
            message: sprintf(__('The name "%{name}" is already taken in this directory.'), {
              name: this.entryName,
            }),
            fadeTransition: false,
            addBodyClass: true,
          });
        } else {
          let parentPath = this.entryName.split('/');
          const name = parentPath.pop();
          parentPath = parentPath.join('/');

          this.renameEntry({
            path: this.path,
            name,
            parentPath,
          });
        }
      } else {
        this.createTempEntry({
          name: this.entryName,
          type: this.modalType,
        });
      }
    },
    createFromTemplate(template) {
      const parent = getPathParent(this.entryName);
      const name = parent ? `${parent}/${template.name}` : template.name;
      this.createTempEntry({
        name,
        type: this.modalType,
      });

      this.$refs.modal.toggle();
    },
    focusInput() {
      const name = this.entries[this.entryName]?.name;
      const inputValue = this.$refs.fieldName.value;

      this.$refs.fieldName.focus();

      if (name) {
        this.$refs.fieldName.setSelectionRange(inputValue.indexOf(name), inputValue.length);
      }
    },
    resetData() {
      this.entryName = '';
      this.path = '';
      this.modalType = modalTypes.blob;
    },
    open(type = modalTypes.blob, path = '') {
      this.modalType = type;
      this.path = path;

      if (this.modalType === modalTypes.rename) {
        this.entryName = path;
      } else {
        this.entryName = path ? `${path}/` : '';
      }

      this.$refs.modal.show();

      // wait for modal to show first
      this.$nextTick(() => this.focusInput());
    },
    close() {
      this.$refs.modal.hide();
    },
  },
};
</script>

<template>
  <gl-modal
    ref="modal"
    modal-id="ide-new-entry"
    data-qa-selector="new_file_modal"
    data-testid="ide-new-entry"
    :title="modalTitle"
    size="lg"
    :action-primary="actionPrimary"
    :action-cancel="actionCancel"
    @primary="submitForm"
    @cancel="resetData"
  >
    <div class="form-group row">
      <label class="label-bold col-form-label col-sm-2"> {{ __('Name') }} </label>
      <div class="col-sm-10">
        <form data-testid="file-name-form" @submit.prevent="submitAndClose">
          <input
            ref="fieldName"
            v-model.trim="entryName"
            type="text"
            class="form-control"
            data-testid="file-name-field"
            data-qa-selector="file_name_field"
            :placeholder="placeholder"
          />
        </form>
        <ul
          v-if="isCreatingNewFile"
          class="file-templates gl-mt-3 list-inline"
          data-qa-selector="template_list_content"
        >
          <li v-for="(template, index) in templateTypes" :key="index" class="list-inline-item">
            <gl-button
              variant="dashed"
              category="secondary"
              class="p-1 pr-2 pl-2"
              @click="createFromTemplate(template)"
            >
              {{ template.name }}
            </gl-button>
          </li>
        </ul>
      </div>
    </div>
  </gl-modal>
</template>