ShowSelector.vue 3.07 KB
Newer Older
1
2
<template>
  <b-row>
3
    <b-col cols="3">
4
5
6
      <h3>{{ title }}</h3>
    </b-col>
    <b-col align="right">
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
      <b-input-group prepend="Select show:">
        <b-form-input
          v-model="inputSelector"
          list="list-of-shows"
          autocomplete="off"
          @keypress="keypressed"
        />
        <b-input-group-append>
          <b-button
            variant="success"
            :disabled="disabledOk"
            @click="confirmSelector"
          >
            <b-icon-check />
          </b-button>
          <b-button
            variant="danger"
            :disabled="disabledReset"
            @click="resetSelector"
          >
            <b-icon-x />
          </b-button>
        </b-input-group-append>
      </b-input-group>
      <datalist id="list-of-shows">
        <option
          v-for="show in shows"
          :key="show.id"
        >
          {{ show.name }} (ID: {{ show.id }})
        </option>
      </datalist>
    </b-col>
    <b-col
      cols="3"
      align="right"
    >
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
      <b-dropdown
        id="ddshows"
        text="Sendereihe auswählen"
        variant="info"
      >
        <b-dropdown-item
          v-for="(show, index) in shows"
          :key="show.id"
          @click="switchShow(index)"
        >
          {{ show.name }}
        </b-dropdown-item>
      </b-dropdown>
    </b-col>
  </b-row>
</template>

<script>
import { mapGetters } from 'vuex'
63
import { BIconCheck, BIconX } from 'bootstrap-vue'
64
65

export default {
66
67
68
69
70
  components: {
    BIconCheck,
    BIconX,
  },

71
72
  props: {
    title: { type: String, default: 'Unlabeled component title' },
73
    callback: { type: Function, default: null }
74
75
  },

76
77
78
79
80
81
  data () {
    return {
      inputSelector: '',
    }
  },

82
83
84
85
86
87
88
  computed: {
    loaded () {
      return {
        shows: this.$store.state.shows.loaded.shows,
      }
    },

89
90
91
92
93
94
95
96
    disabledOk () {
      return ! this.isValidSelector()
    },

    disabledReset () {
      return this.inputSelector.length > 0 ? false : true
    },

97
98
99
100
101
102
103
104
105
    ...mapGetters({
      shows: 'shows/shows',
      selectedShow: 'shows/selectedShow',
    })
  },

  methods: {
    switchShow: function (index) {
      this.$store.commit('shows/switchShow', index)
106
      this.updateInputSelector()
107
      if (this.callback) { this.callback() }
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

    updateInputSelector () {
      this.inputSelector = this.selectedShow.name + ' (ID: ' + this.selectedShow.id + ')'
    },

    isValidSelector () {
      let pattern = /^.*\(ID: \d+\)$/
      if (pattern.test(this.inputSelector)) {
        if ( this.shows.findIndex(s => s.id == this.getSelectorId()) >= 0 ) {
          return true
        }
      }
      return false
    },

    getSelectorId () {
      let s = this.inputSelector.split('(ID: ')
      return s[s.length-1].slice(0,-1)
    },

    keypressed (k) {
      if (k.key === 'Enter') {
        if (this.isValidSelector()) {
          this.switchShow(this.shows.findIndex(s => s.id == this.getSelectorId()))
        }
      }
    },

    confirmSelector () {
      this.keypressed({key: 'Enter'})
    },

    resetSelector () {
      this.inputSelector = ''
    },

145
146
147
  }
}
</script>