summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/ide/components/file_templates/dropdown.vue
blob: ef1f6de3a8614a95f390de52fc71a32fb0df5899 (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
<script>
import $ from 'jquery';
import { mapActions, mapState } from 'vuex';
import DropdownButton from '~/vue_shared/components/dropdown/dropdown_button.vue';

export default {
  components: {
    DropdownButton,
  },
  props: {
    data: {
      type: Array,
      required: false,
      default: () => [],
    },
    label: {
      type: String,
      required: true,
    },
    title: {
      type: String,
      required: false,
      default: null,
    },
    isAsyncData: {
      type: Boolean,
      required: false,
      default: false,
    },
    searchable: {
      type: Boolean,
      required: false,
      default: false,
    },
  },
  data() {
    return {
      search: '',
    };
  },
  computed: {
    ...mapState('fileTemplates', ['templates', 'isLoading']),
    outputData() {
      return (this.isAsyncData ? this.templates : this.data).filter(t => {
        if (!this.searchable) return true;

        return t.name.toLowerCase().indexOf(this.search.toLowerCase()) >= 0;
      });
    },
    showLoading() {
      return this.isAsyncData ? this.isLoading : false;
    },
  },
  mounted() {
    $(this.$el).on('show.bs.dropdown', this.fetchTemplatesIfAsync);
  },
  beforeDestroy() {
    $(this.$el).off('show.bs.dropdown', this.fetchTemplatesIfAsync);
  },
  methods: {
    ...mapActions('fileTemplates', ['fetchTemplateTypes']),
    fetchTemplatesIfAsync() {
      if (this.isAsyncData) {
        this.fetchTemplateTypes();
      }
    },
    clickItem(item) {
      this.$emit('click', item);
    },
  },
};
</script>

<template>
  <div class="dropdown">
    <dropdown-button
      :toggle-text="label"
      data-display="static"
    />
    <div class="dropdown-menu pb-0">
      <div
        v-if="title"
        class="dropdown-title ml-0 mr-0"
      >
        {{ title }}
      </div>
      <div
        v-if="!showLoading && searchable"
        class="dropdown-input"
      >
        <input
          v-model="search"
          :placeholder="__('Filter...')"
          type="search"
          class="dropdown-input-field"
        />
        <i
          aria-hidden="true"
          class="fa fa-search dropdown-input-search"
        ></i>
      </div>
      <div class="dropdown-content">
        <gl-loading-icon
          v-if="showLoading"
          :size="2"
        />
        <ul v-else>
          <li
            v-for="(item, index) in outputData"
            :key="index"
          >
            <button
              type="button"
              @click="clickItem(item)"
            >
              {{ item.name }}
            </button>
          </li>
        </ul>
      </div>
    </div>
  </div>
</template>