aboutsummaryrefslogtreecommitdiff
path: root/assets/web.js
blob: b624edde2a14093f504d8351556ae9755e5354c5 (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
const web = new Vue({
  el: '#web',
  data: {
    word: '',
    timer: undefined,
    definitions: null,
    is_word: null,
    placeholders: ['mot', 'bonjour', 'manger', 'rire', 'jour', 'gagner', 'chanter', 'danser', 'village',
                   'France', 'baguette', 'cola', 'marguerite']
  },
  methods: {
    searchWord: function() {
      if(!this.word) return;
      if(this.timer) {
        clearTimeout(this.timer);
        this.timer = undefined;
      }
      fetch(`/def?w=${this.word}`)
        .then((response) => {
          this.is_word = response.ok;

          if( response.ok ) {
            response.arrayBuffer().then(res => {
              this.definitions = msgpack.decode(new Uint8Array(res));
            });
          }
      })
    },
    rand: (min,max) =>
      Math.floor(Math.random()*(max-min+1)+min)
    ,
    randomplaceholder: function() {
      const r = this.rand(0, this.placeholders.length-1);
      const a = this.placeholders[r];

      return a;
    },
  },
  watch: {
    word: function(w) {
      if( w === '' ) {
        this.is_word = null;
      }
      if(this.timer) {
        clearTimeout(this.timer);
        this.timer = undefined;
      }
      this.timer = setTimeout(this.searchWord, 800);
    }
  }
});