summaryrefslogtreecommitdiffstats
path: root/src/components/Searcher.vue
blob: a1093a2b3a457ec7356f9b3fa26473ca536f3205 (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
<template>
  <div id="searcher">
    <input 
      type="text" 
      v-model='search' 
      ref='input' 
      @keydown="hotkeys" 
      @blur="close()"
      placeholder="Search!"
    />

    <Sugestion 
      v-for="sugestion in sugestions" 
      :key="sugestion[0]" 
      :command="sugestion[1]" 
      :class="{selected: sugestion[0] === current[0]}" 
    />
  </div>
</template>

<script>
import CommandsService from '@/services/CommandsService'
import Sugestion from '@/components/Sugestion.vue'

export default {
  data() {
    return {
      commands: CommandsService,
      search: '',
      sugestions: [],
      current: []
    }
  },

  methods: {
    set_sugestions() {
      const sugestions = []
      let is_current_set = false

      Object.entries(this.commands).forEach((element) => {
        if(element[0].startsWith(this.search) && element[1] instanceof Function) {
          sugestions.push(element)
          if(!is_current_set){ 
            this.current = element
            is_current_set = true;
          }
        }
      });
      this.sugestions = sugestions
    },

    close() {
      this.$emit('close')
    },

    change_current(a=1) {
      let index = this.sugestions.findIndex(element => element===this.current)
      let new_current = this.sugestions[index+a]

      if (new_current) this.current = new_current 
      else this.current = this.sugestions[0]
    },

    async execute_current() {
      let out = await this.current[1](this)

      console.log(out)
      if(out instanceof Object) {
        this.search = ''
        this.commands = out
      }
    },

    hotkeys(e) {
      e.preventDefault()

      switch(e.code) {
        case 'Escape':
        this.close()
        break
        case 'ArrowDown':
          this.change_current(1)
          break
        case 'ArrowUp':
          this.change_current(-1)
          break
        case 'Tab':
          this.change_current(1)
          break
        case 'Enter':
          this.execute_current()
          break
        case 'ShiftLeft':
          this.commands = CommandsService
          break
      }
    },
  },

  watch: {
    search() {
      this.set_sugestions()
    },

    commands() {
      this.set_sugestions()
    }
  },

  computed: {
    
  },

  components: {
    Sugestion
  },

  created() {
    this.set_sugestions()

    this.$nextTick(function () {
      this.$refs.input.focus()
    })
  }
}
</script>

<style scoped>
  #searcher {
    position: absolute;
    width: 30%;
    height: 80%;
    top: 50%;
    left: 50%;
    background-color: #202020;
    padding: 20px;
    border-radius: 10px;
    transform: translate(-50%, -50%);
  }

  #searcher input {
    width: 100%;
    margin-bottom: 20px;
    padding: 10px;
    padding-right: 0px;
    height: 20px;
    border: none;
    background-color: #303030;
  }

  .selected {
    background-color: #505050;
  }
</style>