Features.vue 4.02 KB
Newer Older
1
2
3
4
5
6
7
<template>
<v-container grid-list-md>
  <v-layout row wrap>
    <v-flex xs12>
      <h3>{{header}}</h3>
    </v-flex>

8
9
10
11
12
13
14
15
16
17
18
    <!-- Select Letter -->
    <v-flex xs12>
      <v-select
        v-bind:items="letters"
        v-model="selected"
        label="Select"
        single-line
        bottom
        prepend-icon="local_post_office"
        ></v-select>
    </v-flex>
19

Markus Opolka's avatar
Markus Opolka committed
20
    <!-- Input Fields for Search -->
21
    <v-flex xs12 md3>
22
23
24
      <v-text-field
        v-model="feature_category"
        type="text" label="Category"
25
        @keyup.enter.native="validate">
26
      </v-text-field>
27
    </v-flex>
28
    <v-flex xs12 md3>
29
30
31
32
      <v-text-field
        v-model="feature_type"
        type="text"
        label="Type"
33
        @keyup.enter.native="validate">
34
      </v-text-field>
35
    </v-flex>
36
    <v-flex xs12 md3>
37
38
39
40
      <v-text-field
        v-model="feature_subtype"
        type="text"
        label="Subtype"
41
        @keyup.enter.native="validate">
42
      </v-text-field>
43
44
    </v-flex>

Markus Opolka's avatar
Markus Opolka committed
45
    <!-- Search Buttons -->
46
47
48
49
50
    <v-flex xs12 md3>
      <v-btn @click="validate" color="blue-grey darken-2" class="white--text">Search</v-btn>
      <v-btn @click="clear" color="blue-grey darken-2" class="white--text">Clear</v-btn>
    </v-flex>

Markus Opolka's avatar
Markus Opolka committed
51
    <!-- Invalid Search Warning -->
52
    <v-flex xs12>
53
54
55
      <v-alert outline color="lime darken-2" icon="warning" :value="invalid">
        Invalid Search - Please enter at least one parameter
      </v-alert>
56
57
58
59
60
61
62
    </v-flex>

    <!-- Progress Bar -->
    <v-flex xs12 v-if="searching">
      <v-progress-linear indeterminate color="primary"></v-progress-linear>
    </v-flex>

Markus Opolka's avatar
Markus Opolka committed
63
    <!-- Search Results Meta -->
64
65
66
    <v-flex xs12 v-if="results">
      <v-card color="blue-grey lighten-1" class="white--text">
        <v-card-text>
67
          Results: {{results.length}} letter(s) - {{countedHits}} hit(s)
68
69
70
71
        </v-card-text>
      </v-card>
    </v-flex>

Markus Opolka's avatar
Markus Opolka committed
72
    <!-- Search Results -->
Markus Opolka's avatar
Markus Opolka committed
73
    <v-flex xs12 v-if="results">
74
      <result :letter="letter" v-for="letter in results" v-bind:key="letter.url" />
Markus Opolka's avatar
Markus Opolka committed
75
76
    </v-flex>

77
78
79
80
81
82
83
  </v-layout>
 </v-container>
</template>

<script>
export default {
  data () {
Markus Opolka's avatar
Markus Opolka committed
84
    // Search for Features in Letters
85
86
    return {
      header: 'Feature Search',
87
      invalid: false,
88
      searching: false,
89
90
91
      feature_category: '',
      feature_type: '',
      feature_subtype: '',
Markus Opolka's avatar
Markus Opolka committed
92
      results: null,
93
94
      letters: [
        { text: 'All' }
95
96
      ],
      selected: { text: 'All' }
97
98
99
100
    }
  },
  methods: {
    clear () {
Markus Opolka's avatar
Markus Opolka committed
101
      // Clears the search input fields
102
103
104
      this.feature_category = null
      this.feature_type = null
      this.feature_subtype = null
Markus Opolka's avatar
Markus Opolka committed
105
106
    },
    find () {
Markus Opolka's avatar
Markus Opolka committed
107
      // Call feature search API with parameters
108
109
110
111
112
113
114
115
116
      this.searching = true

      let url = 'http://localhost:3000/api/features/'
      // TODO: this is a bit silly
      if (this.selected.text !== 'All') {
        url = url + this.selected.text.replace(/ /g, '_') + '/'
      }

      let params = {
Markus Opolka's avatar
Markus Opolka committed
117
118
119
120
121
122
123
        category: this.feature_category,
        type: this.feature_type,
        subtype: this.feature_subtype
      }

      // Remove undefined values
      Object.keys(params).forEach((key) => (params[key] == null) && delete params[key])
124
      this.$http.get(url, {params: params}).then(function (data) {
Markus Opolka's avatar
Markus Opolka committed
125
126
127
        this.results = data.body
        this.searching = false
      })
128
129
    },
    validate () {
Markus Opolka's avatar
Markus Opolka committed
130
      // Validate input fields and call find()
131
      const invalid = this.feature_category.length === 0 && this.feature_type.length === 0 && this.feature_subtype.length === 0
132
133
134
135
136
137
      if (invalid) {
        this.invalid = true
      } else {
        this.invalid = false
        this.find()
      }
138
139
    }
  },
140
141
  computed: {
    countedHits: function () {
Markus Opolka's avatar
Markus Opolka committed
142
      // Sums up all hits in all letters
143
144
      let hits = 0
      for (let value of this.results) {
145
146
147
148
149
        hits = hits + value.count
      }
      return hits
    }
  },
150
  created () {
Markus Opolka's avatar
Markus Opolka committed
151
    // Create list of all letters
152
    this.$http.get('http://localhost:3000/api/letters').then(function (data) {
153
      for (let value of data.body) {
154
155
156
157
158
159
        this.letters.push({text: value.name})
      }
    })
  }
}
</script>