From 7df304d9551a2b23782c9a0029c6d2d5cd1224c4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Thuret?= Date: Sat, 9 Oct 2021 18:16:50 +0200 Subject: [PATCH 1/2] Move the javascript in his own file --- app/static/js/app.js | 325 ++++++++++++++++++++++++++++++++++++++ app/templates/index.html | 331 +-------------------------------------- 2 files changed, 327 insertions(+), 329 deletions(-) create mode 100644 app/static/js/app.js diff --git a/app/static/js/app.js b/app/static/js/app.js new file mode 100644 index 0000000..b957b08 --- /dev/null +++ b/app/static/js/app.js @@ -0,0 +1,325 @@ +// @license magnet:?xt=urn:btih:0b31508aeb0634b347b8270c7bee4d411b5d4109&dn=agpl-3.0.txt AGPL-3.0 +// API host/endpoint +var BaseUrl = window.location.protocol + "//" + window.location.host; +var htmlRegex = /<(.*)>.*?|<(.*)\/>/; +document.addEventListener('DOMContentLoaded', function(){ + var sidenavElems = document.querySelectorAll('.sidenav'); + var sidenavInstances = M.Sidenav.init(sidenavElems); + + var app = new Vue({ + el: '#app', + delimiters: ['[[',']]'], + data: { + BaseUrl: BaseUrl, + loading: true, + error: "", + langs: [], + settings: {}, + sourceLang: "", + targetLang: "", + + loadingTranslation: false, + inputText: "", + inputTextareaHeight: 250, + savedTanslatedText: "", + translatedText: "", + output: "", + charactersLimit: -1, + + copyTextLabel: "Copy text", + + suggestions: false, + isSuggesting: false, + }, + mounted: function(){ + var self = this; + var requestSettings = new XMLHttpRequest(); + requestSettings.open('GET', BaseUrl + '/frontend/settings', true); + + requestSettings.onload = function() { + if (this.status >= 200 && this.status < 400) { + // Success! + self.settings = JSON.parse(this.response); + self.sourceLang = self.settings.language.source.code; + self.targetLang = self.settings.language.target.code; + self.charactersLimit = self.settings.charLimit; + self.suggestions = self.settings.suggestions; + }else { + self.error = "Cannot load /frontend/settings"; + self.loading = false; + } + }; + + requestSettings.onerror = function() { + self.error = "Error while calling /frontend/settings"; + self.loading = false; + }; + + requestSettings.send(); + + var requestLanguages = new XMLHttpRequest(); + requestLanguages.open('GET', BaseUrl + '/languages', true); + + requestLanguages.onload = function() { + if (this.status >= 200 && this.status < 400) { + // Success! + self.langs = JSON.parse(this.response); + self.langs.push({ name: 'Auto Detect (Experimental)', code: 'auto' }) + if (self.langs.length === 0){ + self.loading = false; + self.error = "No languages available. Did you install the models correctly?" + return; + } + + self.loading = false; + } else { + self.error = "Cannot load /languages"; + self.loading = false; + } + }; + + requestLanguages.onerror = function() { + self.error = "Error while calling /languages"; + self.loading = false; + }; + + requestLanguages.send(); + }, + updated: function(){ + M.FormSelect.init(this.$refs.sourceLangDropdown); + M.FormSelect.init(this.$refs.targetLangDropdown); + if (this.inputText === ""){ + this.$refs.inputTextarea.style.height = this.inputTextareaHeight + "px"; + this.$refs.translatedTextarea.style.height = this.inputTextareaHeight + "px"; + }else{ + this.$refs.inputTextarea.style.height = this.$refs.translatedTextarea.style.height = "1px"; + this.$refs.inputTextarea.style.height = Math.max(this.inputTextareaHeight, this.$refs.inputTextarea.scrollHeight + 32) + "px"; + this.$refs.translatedTextarea.style.height = Math.max(this.inputTextareaHeight, this.$refs.translatedTextarea.scrollHeight + 32) + "px"; + } + + if (this.charactersLimit !== -1 && this.inputText.length >= this.charactersLimit){ + this.inputText = this.inputText.substring(0, this.charactersLimit); + } + + // Update "selected" attribute (to overcome a vue.js limitation) + // but properly display checkmarks on supported browsers. + // Also change the