Skip to content
Snippets Groups Projects
Forked from Språksamlingane / beta.ordbok.uib.no
2249 commits behind the upstream repository.
App.vue 3.38 KiB
<template>
  <div id="app">
    <img alt="Vue logo" src="./assets/logo.png">
    <div class="">
      <autocomplete :search="search" :get-result-value="result_view" @submit="select_result" placeholder="søk..."></autocomplete>
    </div>
    <SearchResults :hits="search_results" />
    <Article :article="article" />
  </div>
</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'

window.onpopstate = function (event) {
  if (event.state) {
    console.log(event)
    app.__vue__._data.article = event.state
  } else {
    console.log(event)
  }
}

export default {
  name: 'app',
  data: function() {
    return {
      article_id: parseInt(window.location.href.split("/").pop()),
      search_query: window.location.href.split("?q=")[1],
      search_results: [],
      article: {lemmas: []},
      search: function(q) {
        return new Promise(resolve => {
          return axios.post('https://search-ordbok-prototype-6qpfmm6fz5jvy5ba3e6blryeqe.eu-west-1.es.amazonaws.com/ordbok/_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)
                              })
        })
      },
      result_view: function(result) {
        if (result._source)
          return result._source.lemmas.map(x => x.word).join(', ')
        else
          return 'Alle søkeresultater...'
      }
    }
  },
  components: {
    Article,
    Autocomplete,
    SearchResults
  },
  methods: {
    select_result: function(event) {
      if(event._source){
        this.search_results = []
        this.article = event._source
        history.pushState(this.article, this.article.lemmas.map(x => x.word).join(', '), event._id)
      }else{
        window.location.href="search?q=" + event.q
      }
    }
  },
  mounted: function(){
    var self = this;
    if(this.search_query) {
      axios.get('https://search-ordbok-prototype-6qpfmm6fz5jvy5ba3e6blryeqe.eu-west-1.es.amazonaws.com/ordbok/_search?q=' + self.search_query)
      .then(function(response){
        self.search_results = response.data.hits.hits
      })
    }
    else
    {
      axios.get('https://search-ordbok-prototype-6qpfmm6fz5jvy5ba3e6blryeqe.eu-west-1.es.amazonaws.com/ordbok/_doc/' + self.article_id)
      .then(function(response){
        self.article = response.data._source
        history.replaceState(self.article, self.article.lemmas.map(x => x.word).join(', '))
      })
    }
  }
}
</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%;
}

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