Skip to content
Snippets Groups Projects
App.vue 3.96 KiB
<template>
  <main id="app">
    <img alt="Vue logo" src="./assets/logo.png">
    <header>
      <autocomplete :debounceTime="50" :auto-select="true" :search="search" :get-result-value="result_view" @submit="select_result" placeholder="søk..."></autocomplete>
      <input type="radio" id="radio_nob" value="nob" v-model="path_lang">
      <label for="radio_nob">Bokmål</label>
      <input type="radio" id="radio_nno" value="nno" v-model="path_lang">
      <label for="radio_nno">Nynorsk</label>
    </header>
    <img id="spinner" :class="waiting ? 'show' : 'hide'" src="./assets/spinner.gif" alt="Venter på innhold" />
    <SearchResults :hits="search_results" :lang="path_lang" />
    <Article :article="article" />
  </main>
</template>

<script>
import axios from "axios"
import Article from './components/Article.vue'
import SearchResults from './components/SearchResults.vue'
import Autocomplete from '@trevoreyre/autocomplete-vue'
import '@trevoreyre/autocomplete-vue/dist/style.css'

var api_endpoint = 'https://ordbok-dev.aws.uib.no/cache/article'

export default {
  name: 'app',
  data: function() {
    let params = new URLSearchParams(window.location.search.substring(1))
    let path = window.location.pathname.split("/")
    return {
      article_id: parseInt(path.pop()),
      search_query: params.get('q'),
      path_lang: path.pop() || 'nob',
      search_results: [],
      waiting: true,
      article: {lemmas: [], body:{pronunciation: [], definitions: [], etymology: []}},
      result_view: function(result) {
        if (result._source)
          return result._source.lemmas.map(x => x.lemma).join(', ')
        else
          return 'Alle søkeresultater...'
      }
    }
  },
  computed: {
    search: function() {
      console.log(this)
      let self = this
      return function(q) {
                return new Promise(resolve => {
                  return axios.post(api_endpoint + '/' + self.path_lang + '/_search/',
                                    {
                                        "suggest": {
                                          "suggest" : {
                                            "prefix" : q,
                                            "completion" : {
                                              "field" : "suggest"
                                            }
                                          }
                                        }
                                      }).then(function(response) {
                                        let hits = response.data.suggest.suggest[0].options
                                        if(q.length) hits = hits.concat({q: q})
                                        resolve(hits)
                                      })
                                    })
        }
      }
    },
  components: {
    Article,
    Autocomplete,
    SearchResults
  },
  methods: {
    select_result: function(event) {
      if(event._source){
        this.search_results = []
        this.article = event._source
      }else{
        window.location.href = '/' + this.path_lang + "/search?q=" + event.q
      }
    }
  },
  mounted: function(){
    var self = this;
    if(this.search_query) {
      axios.get(api_endpoint + '/' + self.path_lang + '/_search?q=' + self.search_query)
      .then(function(response){
        self.search_results = response.data.hits.hits
        self.waiting = false
      })
    }
    else if(this.article_id)
    {
      axios.get(api_endpoint + '/' + self.path_lang + '/' + self.article_id)
      .then(function(response){
        self.article = response.data._source
        self.waiting = false
      })
    }
    else {
      this.waiting = false
    }
  }
}
</script>

<style>
#app {
  font-family: 'Avenir', Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  color: #2c3e50;
  margin-top: 60px;
  margin-left: 30%;
  margin-right: 10%;
}

.show {
  display: block;
}

.hide {
  display: none;
}

.autocomplete {
  width: 25em;
}
</style>