modal.vue 2.6 KB
Newer Older
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 { __ } from '../../../locale';
  import popupDialog from '../../../vue_shared/components/popup_dialog.vue';
  import RepoStore from '../../stores/repo_store';
  import RepoHelper from '../../helpers/repo_helper';

  export default {
    props: {
      type: {
        type: String,
        required: true,
      },
    },
    data() {
      return {
        entryName: '',
      };
    },
    components: {
      popupDialog,
    },
    methods: {
      createEntryInStore() {
        if (this.entryName === '') return;

        const fileName = this.type === 'tree' ? '.gitkeep' : this.entryName;
        let tree = null;

        if (this.type === 'tree') {
          tree = RepoHelper.serializeTree({
            name: this.entryName,
            path: this.entryName,
            tempFile: true,
          });
          RepoStore.files.push(tree);

          RepoHelper.setDirectoryOpen(tree, tree.name);
        }

        const file = RepoHelper.serializeBlob({
          name: fileName,
          path: tree ? `${tree}/${fileName}` : fileName,
          tempFile: true,
        });

        if (tree) {
          RepoStore.addFilesToDirectory(tree, RepoStore.files, [file]);
        } else {
          RepoStore.addFilesToDirectory(tree, RepoStore.files, [...RepoStore.files, file]);
        }

        RepoHelper.setFile(file, file);
        RepoStore.editMode = true;
        RepoStore.toggleBlobView();

        this.toggleModalOpen();
      },
      toggleModalOpen() {
        this.$emit('toggle');
      },
    },
    computed: {
      modalTitle() {
        if (this.type === 'tree') {
          return __('Create new directory');
        }

        return __('Create new file');
      },
      buttonLabel() {
        if (this.type === 'tree') {
          return __('Create directory');
        }

        return __('Create file');
      },
      formLabelName() {
        if (this.type === 'tree') {
          return __('Directory name');
        }

        return __('File name');
      },
    },
  };
</script>

<template>
  <popup-dialog
    :title="modalTitle"
    :primary-button-label="buttonLabel"
    kind="success"
    @toggle="toggleModalOpen"
    @submit="createEntryInStore"
  >
    <form
      class="form-horizontal"
      slot="body"
      @submit.prevent="createEntryInStore"
    >
      <fieldset class="form-group append-bottom-0">
        <label class="label-light col-sm-3">
          {{ formLabelName }}
        </label>
        <div class="col-sm-9">
          <input
            type="text"
            class="form-control"
            v-model="entryName"
          />
        </div>
      </fieldset>
    </form>
  </popup-dialog>
</template>