Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pure JS implementation #457

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 6 additions & 31 deletions Resources/public/js/translation.js
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ const TranslationManager = (() => {
let newValue = document.getElementById('inputContent-' + lexikTranslationId + '-' + locales[i]).value;
if (oldValue !== newValue) {
update = true;
document.getElementById('content-' + lexikTranslationId + '-' + locales[i]).innerText = newValue;
document.getElementById('content-' + lexikTranslationId + '-' + locales[i]).textContent = newValue;
}
}
if (update) {
Expand All @@ -244,36 +244,11 @@ const TranslationManager = (() => {
let params = {};
let saveButton = document.getElementById('saveButton-' + lexikTranslationId);
let trElement = saveButton.closest('tr.content');
let tdElements = trElement.querySelectorAll('td');
let translationsElements = trElement.querySelectorAll('td span.locale');

let spanColumnText = Array.from(tdElements).map(function (td, index) {
if (index <= 2) {
let span = td.querySelector('span');
if (span) {
let th = trElement.closest('table').querySelectorAll('th')[index];
return {
value: span.innerText.trim(),
column: th ? '_' + th.id.split('-')[0] : null
};
}
return null;
}
}).filter(Boolean);

let translationsTexts = Array.from(translationsElements).map(function (translationElement) {
return translationElement.innerText.trim() + '-' + translationElement.id;
});

translationsTexts.forEach(function (translationText) {
let parts = translationText.split('-');
let translation = parts[0];
let locale = parts[3];
params[locale] = translation;
});

spanColumnText.forEach(function (spanText) {
params[spanText.column] = spanText.value;
let tdElements = trElement.querySelectorAll('td[class^="col-"]');
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While this is shorter it doesn't read very easily. After a while you can make out that text is collected from td elements (which probably contain translations). Can you make it easier to follow somehow?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It simplify the code as we don't need to lookup for the related column header ID as we already have all needed information in the tr. We just have to iterate on td that have a class that start with col- to construct the parameters.

image

It seems rather easy to read for me. Easier than before for sure.

tdElements.forEach(function (td, index) {
let span = td.querySelector('span');
let col = td.classList[0].replace('col-', '');
params[col] = span.textContent;
});

translationApiManager.updateTranslation(lexikTranslationId, params).then(function (response) {
Expand Down