Skip to content

Commit

Permalink
Improved content snippet handling by sorting languages, enabling auto…
Browse files Browse the repository at this point in the history
… translate preselection for websites
  • Loading branch information
Florian Krönert authored and Florian Krönert committed Aug 25, 2020
1 parent 9d59c39 commit d1eca7d
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 25 deletions.
10 changes: 6 additions & 4 deletions src/js/Translator/ContentSnippetHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
function GetUpdates(records) {
var updates = [];

var languageList = Object.keys(portalLanguages).map(function(k) { return portalLanguages[k] });
var languageList = portalLanguages;

for (var i = 0; i < records.length; i++) {
var record = records[i];
Expand Down Expand Up @@ -135,7 +135,10 @@

var child = {
recid: key,
schemaName: key.substr(key.indexOf(idSeparator) + 1)
schemaName: key.substr(key.indexOf(idSeparator) + 1),
w2ui: {
hideCheckBox: true
}
};

for (var k = 0; k < snippetsByGroup.length; k++) {
Expand All @@ -145,14 +148,13 @@
continue;
}

var websiteLanguage = portalLanguages[snippet.adx_contentsnippetlanguageid.adx_websitelanguageid];
var websiteLanguage = portalLanguages.find(function(l) { return l.adx_websitelanguageid === snippet.adx_contentsnippetlanguageid.adx_websitelanguageid; });

if (!websiteLanguage) {
continue;
}

var language = websiteLanguage.adx_PortalLanguageId.adx_lcid.toString();

child[language] = snippet.adx_value;
}

Expand Down
36 changes: 21 additions & 15 deletions src/js/Translator/TranslationHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,7 @@
this.validate();
w2popup.close();

XrmTranslator.ShowRecordSelector("TranslationHandler.ProposeTranslations", [this.record.sourceLcid.id, this.record.targetLcid.id, this.record.translateMissing ? this.record.translateMissing.id.trim() : "", this.record.apiProvider ? this.record.apiProvider.id : ""]);
XrmTranslator.ShowRecordSelector("TranslationHandler.ProposeTranslations", [this.record.sourceLcid.id, this.record.targetLcid.id, this.record.translateMissing ? this.record.translateMissing.id.trim() : "", this.record.apiProvider ? this.record.apiProvider.id : ""], (XrmTranslator.GetGrid().getSelection() || []));
},
"cancel": function () {
w2popup.close();
Expand Down Expand Up @@ -539,13 +539,15 @@

TranslationHandler.FillLanguageCodes = function(languages, userSettings, config) {
var grid = XrmTranslator.GetGrid();

var languageCount = languages.length;

// Reset schema name col
grid.columns[0].size = XrmTranslator.defaultSchemaNameSize;

return GetLocales()
.then(function(locales) {
// 100% full width, minus length of the schema name grid, divided by number of languages is space left for each language
var columnWidth = (100 - parseInt(grid.columns[0].size.replace("%"))) / languageCount;
var columnWidth = (100 - parseInt(XrmTranslator.defaultSchemaNameSize.replace("%"))) / languageCount;

for (var i = 0; i < languages.length; i++) {
var language = languages[i];
Expand All @@ -568,18 +570,22 @@
TranslationHandler.FillPortalLanguageCodes = function(portalLanguages) {
var grid = XrmTranslator.GetGrid();

var languages = Object.keys(portalLanguages).map(function(k) { return portalLanguages[k] }).reduce(function(all, cur) { if (!all[cur.adx_PortalLanguageId.adx_lcid.toString()]) { all[cur.adx_PortalLanguageId.adx_lcid.toString()] = cur.adx_PortalLanguageId.adx_languagecode; } return all; }, {});

var lcids = Object.keys(languages);
var columnWidth = (100 - parseInt(grid.columns[0].size.replace("%"))) / lcids.length;
// Reset schema name col
grid.columns[0].size = XrmTranslator.defaultSchemaNameSize;

var languages = portalLanguages
.reduce(function(all, cur) { if (!all[cur.adx_PortalLanguageId.adx_languagecode]) { all[cur.adx_PortalLanguageId.adx_languagecode] = cur.adx_PortalLanguageId.adx_lcid.toString() } return all; }, {});

var locales = Object.keys(languages);
var columnWidth = (100 - parseInt(XrmTranslator.defaultSchemaNameSize.replace("%"))) / locales.length;

for (var i = 0; i < lcids.length; i++) {
var lcid = lcids[i];
for (var i = 0; i < locales.length; i++) {
var locale = locales[i];

var editable = { type: 'text' };

grid.addColumn({ field: lcid, caption: languages[lcid] || lcid, size: columnWidth + "%", sortable: true, editable: editable });
grid.addSearch({ field: lcid, caption: languages[lcid] || lcid, type: 'text' });
grid.addColumn({ field: languages[locale], caption: locale, size: columnWidth + "%", sortable: true, editable: editable });
grid.addSearch({ field: languages[locale], caption: locale, type: 'text' });
}

return languages;
Expand All @@ -591,10 +597,10 @@
TranslationHandler.FindPortalLanguages = function () {
return WebApiClient.Retrieve({entityName: "adx_websitelanguage", queryParams: "?$select=_adx_websiteid_value&$expand=adx_PortalLanguageId($select=adx_lcid,adx_languagecode,adx_portallanguageid)"})
.then(function (r) {
return r.value.map(function(w) { return { id: w.adx_websitelanguageid, data: w }; });
})
.then(function (r) {
return r.reduce(function(all, cur) { all[cur.id] = cur.data; return all; }, {});
const languages = r.value;
languages.sort(function(a, b) { return ((a.adx_PortalLanguageId || {}).adx_languagecode || "").localeCompare((b.adx_PortalLanguageId || {}).adx_languagecode || "")});

return languages;
});
}

Expand Down
43 changes: 37 additions & 6 deletions src/js/Translator/XrmTranslator.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@

XrmTranslator.columnRestoreNeeded = false;

XrmTranslator.defaultSchemaNameSize = "20%";

var currentHandler = null;

RegExp.escape= function(s) {
Expand Down Expand Up @@ -158,6 +160,9 @@
}

function SetHandler() {
// Deactivate selectColumn on each change, only ContentSnippetHandler supports this right now
w2ui.grid.show.selectColumn = false;

if (XrmTranslator.GetType() === "attributes") {
currentHandler = AttributeHandler;
}
Expand All @@ -180,11 +185,14 @@
currentHandler = ChartHandler;
}
else if (XrmTranslator.GetType() === "content") {
w2ui.grid.show.selectColumn = true;
currentHandler = ContentSnippetHandler;
}
else if (XrmTranslator.GetType() === "webresources") {
currentHandler = WebResourceHandler;
}

w2ui.grid.refresh();
}

XrmTranslator.errorHandler = function(error) {
Expand Down Expand Up @@ -544,7 +552,7 @@

XrmTranslator.FindRecords = function(records, find, replace, useRegex, ignoreCase, column, columnName, selectRecords) {
if (!records && selectRecords) {
XrmTranslator.ShowRecordSelector("XrmTranslator.FindRecords", [find, replace, useRegex, ignoreCase, column, columnName, selectRecords]);
XrmTranslator.ShowRecordSelector("XrmTranslator.FindRecords", [find, replace, useRegex, ignoreCase, column, columnName, selectRecords], (XrmTranslator.GetGrid().getSelection() || []));
return;
}
else if (!records) {
Expand Down Expand Up @@ -603,7 +611,19 @@
ShowFindAndReplaceResults(findings);
}

XrmTranslator.ShowRecordSelector = function (callbackName, callbackParameters) {
function removeHideCheckBoxFlag (r) {
if (r.w2ui && r.w2ui.hideCheckBox) {
r.w2ui.hideCheckBox = false;
}

if (r.w2ui && r.w2ui.children) {
r.w2ui.children = r.w2ui.children.map(removeHideCheckBoxFlag);
}

return r;
}

XrmTranslator.ShowRecordSelector = function (callbackName, callbackParameters, preselectedRecords) {
if (!w2ui.recordSelectorGrid) {
var grid = {
name: 'recordSelectorGrid',
Expand Down Expand Up @@ -651,7 +671,7 @@

w2ui.recordSelectorGrid.reset(true);
w2ui.recordSelectorGrid.clear();
w2ui.recordSelectorGrid.add(JSON.parse(JSON.stringify(XrmTranslator.GetGrid().records)));
w2ui.recordSelectorGrid.add(JSON.parse(JSON.stringify(XrmTranslator.GetGrid().records)).map(removeHideCheckBoxFlag));
w2ui.recordSelectorGrid.refresh();

var callbackString = (callbackParameters || []).map(function(p) { return typeof(p) === "string" ? "'" + p + "'" : p + ""; }).join(",");
Expand All @@ -668,6 +688,14 @@
event.onComplete = function () {
$('#w2ui-popup #main').w2render('recordSelectorGrid');
w2ui.recordSelectorGrid.records.slice().forEach(function(r) { w2ui.recordSelectorGrid.expand(r.recid); });

if (preselectedRecords) {
for (let i = 0; i < preselectedRecords.length; i++) {
const id = preselectedRecords[i];

w2ui.recordSelectorGrid.select(id);
}
}
};
},
onToggle: function (event) {
Expand Down Expand Up @@ -1108,7 +1136,7 @@
{ field: 'schemaName', caption: 'Schema Name', type: 'text' }
],
columns: [
{ field: 'schemaName', caption: 'Schema Name', size: '20%', sortable: true, resizable: true, frozen: true }
{ field: 'schemaName', caption: 'Schema Name', size: XrmTranslator.defaultSchemaNameSize, sortable: true, resizable: true, frozen: true }
],
onSave: function (event) {
currentHandler.Save();
Expand Down Expand Up @@ -1259,9 +1287,12 @@
}

XrmTranslator.ClearColumns = function() {
var columns = XrmTranslator.GetColumns();
// Don't remove schema name column
var columns = XrmTranslator.GetColumns(false);

columns.forEach(function(l) { XrmTranslator.GetGrid().removeColumn(l) });
columns.forEach(function(l) {
XrmTranslator.GetGrid().removeColumn(l);
});
}

XrmTranslator.AddSummary = function(records, countChildParents) {
Expand Down

0 comments on commit d1eca7d

Please sign in to comment.