Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
L
LibreTranslate
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Riße, Matthias
LibreTranslate
Commits
727b9192
Unverified
Commit
727b9192
authored
Jun 22, 2022
by
Piero Toffanin
Committed by
GitHub
Jun 22, 2022
Browse files
Options
Downloads
Plain Diff
Merge pull request #279 from SethFalco/chores-2
fix: fix race condition in settings and langs
parents
c2b82e93
6e2df73a
Branches
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
app/static/js/app.js
+84
-76
84 additions, 76 deletions
app/static/js/app.js
with
84 additions
and
76 deletions
app/static/js/app.js
+
84
−
76
View file @
727b9192
...
...
@@ -40,13 +40,16 @@ document.addEventListener('DOMContentLoaded', function(){
frontendTimeout
:
500
},
mounted
:
function
()
{
var
self
=
this
;
var
requestSettings
=
new
XMLHttpRequest
();
requestSettings
.
open
(
'
GET
'
,
BaseUrl
+
'
/frontend/settings
'
,
true
);
const
self
=
this
;
const
settingsRequest
=
new
XMLHttpRequest
();
settingsRequest
.
open
(
"
GET
"
,
BaseUrl
+
"
/frontend/settings
"
,
true
);
const
langsRequest
=
new
XMLHttpRequest
();
langsRequest
.
open
(
"
GET
"
,
BaseUrl
+
"
/languages
"
,
true
);
requestSettings
.
onload
=
function
()
{
settingsRequest
.
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
;
...
...
@@ -55,64 +58,32 @@ document.addEventListener('DOMContentLoaded', function(){
self
.
supportedFilesFormat
=
self
.
settings
.
supportedFilesFormat
;
self
.
filesTranslation
=
self
.
settings
.
filesTranslation
;
self
.
frontendTimeout
=
self
.
settings
.
frontendTimeout
;
if
(
langsRequest
.
response
)
{
handleLangsResponse
(
self
,
langsRequest
);
}
else
{
langsRequest
.
onload
=
function
()
{
handleLangsResponse
(
self
,
this
);
}
}
}
else
{
self
.
error
=
"
Cannot load /frontend/settings
"
;
self
.
loading
=
false
;
}
};
requestSettings
.
onerror
=
function
()
{
settingsRequest
.
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
;
}
const
sourceLanguage
=
self
.
langs
.
find
(
l
=>
l
.
code
===
self
.
getQueryParam
(
'
source
'
))
const
isSourceAuto
=
!
sourceLanguage
&&
self
.
getQueryParam
(
'
source
'
)
===
"
auto
"
const
targetLanguage
=
self
.
langs
.
find
(
l
=>
l
.
code
===
self
.
getQueryParam
(
'
target
'
))
if
(
sourceLanguage
||
isSourceAuto
)
{
self
.
sourceLang
=
isSourceAuto
?
"
auto
"
:
sourceLanguage
.
code
}
if
(
targetLanguage
)
{
self
.
targetLang
=
targetLanguage
.
code
}
const
defaultText
=
self
.
getQueryParam
(
'
q
'
)
if
(
defaultText
)
{
self
.
inputText
=
decodeURI
(
defaultText
)
}
self
.
loading
=
false
;
}
else
{
self
.
error
=
"
Cannot load /languages
"
;
self
.
loading
=
false
;
}
};
requestLanguages
.
onerror
=
function
()
{
langsRequest
.
onerror
=
function
()
{
self
.
error
=
"
Error while calling /languages
"
;
self
.
loading
=
false
;
};
requestLanguages
.
send
();
settingsRequest
.
send
();
langsRequest
.
send
();
},
updated
:
function
(){
M
.
FormSelect
.
init
(
this
.
$refs
.
sourceLangDropdown
);
...
...
@@ -137,27 +108,11 @@ document.addEventListener('DOMContentLoaded', function(){
// but properly display checkmarks on supported browsers.
// Also change the <select> width value depending on the <option> length
if
(
this
.
$refs
.
sourceLangDropdown
)
{
for
(
var
i
=
0
;
i
<
this
.
$refs
.
sourceLangDropdown
.
children
.
length
;
i
++
){
var
el
=
this
.
$refs
.
sourceLangDropdown
.
children
[
i
];
if
(
el
.
value
===
this
.
sourceLang
){
el
.
setAttribute
(
'
selected
'
,
''
);
this
.
$refs
.
sourceLangDropdown
.
style
.
width
=
getTextWidth
(
el
.
text
)
+
24
+
'
px
'
;
}
else
{
el
.
removeAttribute
(
'
selected
'
);
}
}
updateSelectedAttribute
(
this
.
$refs
.
sourceLangDropdown
,
this
.
sourceLang
);
}
if
(
this
.
$refs
.
targetLangDropdown
)
{
for
(
var
i
=
0
;
i
<
this
.
$refs
.
targetLangDropdown
.
children
.
length
;
i
++
){
var
el
=
this
.
$refs
.
targetLangDropdown
.
children
[
i
];
if
(
el
.
value
===
this
.
targetLang
){
el
.
setAttribute
(
'
selected
'
,
''
);
this
.
$refs
.
targetLangDropdown
.
style
.
width
=
getTextWidth
(
el
.
text
)
+
24
+
'
px
'
;
}
else
{
el
.
removeAttribute
(
'
selected
'
);
}
}
updateSelectedAttribute
(
this
.
$refs
.
targetLangDropdown
,
this
.
targetLang
);
}
},
computed
:
{
...
...
@@ -423,9 +378,62 @@ document.addEventListener('DOMContentLoaded', function(){
}
}
});
});
/**
* @param {object} self
* @param {XMLHttpRequest} response
*/
function
handleLangsResponse
(
self
,
response
)
{
if
(
response
.
status
>=
200
&&
response
.
status
<
400
)
{
self
.
langs
=
JSON
.
parse
(
response
.
response
);
if
(
self
.
langs
.
length
===
0
){
self
.
loading
=
false
;
self
.
error
=
"
No languages available. Did you install the models correctly?
"
return
;
}
self
.
langs
.
push
({
name
:
"
Auto Detect (Experimental)
"
,
code
:
"
auto
"
})
const
sourceLanguage
=
self
.
langs
.
find
(
l
=>
l
.
code
===
self
.
getQueryParam
(
"
source
"
))
const
targetLanguage
=
self
.
langs
.
find
(
l
=>
l
.
code
===
self
.
getQueryParam
(
"
target
"
))
if
(
sourceLanguage
)
{
self
.
sourceLang
=
sourceLanguage
.
code
}
if
(
targetLanguage
)
{
self
.
targetLang
=
targetLanguage
.
code
}
const
defaultText
=
self
.
getQueryParam
(
"
q
"
)
if
(
defaultText
)
{
self
.
inputText
=
decodeURI
(
defaultText
)
}
}
else
{
self
.
error
=
"
Cannot load /languages
"
;
}
self
.
loading
=
false
;
}
/**
* @param {object} langDropdown
* @param {string} lang
*/
function
updateSelectedAttribute
(
langDropdown
,
lang
)
{
for
(
const
child
of
langDropdown
.
children
)
{
if
(
child
.
value
===
lang
){
child
.
setAttribute
(
'
selected
'
,
''
);
langDropdown
.
style
.
width
=
getTextWidth
(
child
.
text
)
+
24
+
'
px
'
;
}
else
{
child
.
removeAttribute
(
'
selected
'
);
}
}
}
function
getTextWidth
(
text
)
{
var
canvas
=
getTextWidth
.
canvas
||
(
getTextWidth
.
canvas
=
document
.
createElement
(
"
canvas
"
));
var
ctx
=
canvas
.
getContext
(
"
2d
"
);
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment