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
|
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));
if( this.definitions && this.definitions[0] && this.definitions[0].mot) {
if(!this.word.includes('_')) {
this.word = this.definitions[0].mot;
}
}
});
}
})
},
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);
}
},
mounted: function() {
const param = window.location.search.substr(1);
param.split('&').forEach( p => {
if (p.startsWith('w=')) {
this.word = p.slice(2);
}
});
}
});
|