diff options
Diffstat (limited to 'assets/web.js')
-rw-r--r-- | assets/web.js | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/assets/web.js b/assets/web.js new file mode 100644 index 0000000..b624edd --- /dev/null +++ b/assets/web.js @@ -0,0 +1,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); + } + } +}); |