Skip to content

Commit

Permalink
Preset Editor: Modify languages tab
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewtelnov committed Aug 23, 2024
1 parent 708e01a commit ea8deb1
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 32 deletions.
48 changes: 21 additions & 27 deletions packages/creator-presets-core/src/presets-editable-languages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,42 +20,36 @@ export class CreatorPresetEditableLanguages extends CreatorPresetEditableBase {
choices: this.getCreatorLocales()
},
{
type: "panel", name: "languages_survey_panel",
type: "boolean",
name: this.surveyUseEnglishNames,
title: "Translate Survey language names to Engish",
titleLocation: "hidden",
renderAs: "checkbox"
},
{
type: "checkbox",
name: this.surveyLocalesName,
title: "Survey languages",
elements: [
{
type: "boolean",
name: this.surveyShowInEnglishName,
titleLocation: "hidden",
labelFalse: "Display languages in their native form",
labelTrue: "Display languages in English",
},
{
type: "checkbox",
name: this.surveyLocalesName,
titleLocation: "hidden",
minSelectedChoices: 1,
colCount: 3,
showSelectAllItem: true,
choices: this.getSurveyLocales()
}
]
minSelectedChoices: 1,
colCount: 3,
showSelectAllItem: true,
choices: this.getSurveyLocales()
}
]
}
] };
}
protected getJsonValueCore(model: SurveyModel, creator: SurveyCreatorModel): any {
const creatorLocale = model.getValue(this.creatorLocaleName);
const showInEnglish = model.getValue(this.surveyShowInEnglishName) === true;
const useEnglishNames = model.getValue(this.surveyUseEnglishNames) === true;
const question = <QuestionCheckboxModel>model.getQuestionByName(this.surveyLocalesName);
if(!creatorLocale && question.isAllSelected && !showInEnglish) return undefined;
if(!creatorLocale && question.isAllSelected && !useEnglishNames) return undefined;
const res: any = {};
if(creatorLocale) {
res.creator = creatorLocale;
}
if(showInEnglish) {
res.showNamesInEnglish = true;
if(useEnglishNames) {
res.useEnglishNames = true;
}
if(!question.isAllSelected && Array.isArray(question.value)) {
res.surveyLocales = [];
Expand All @@ -66,7 +60,7 @@ export class CreatorPresetEditableLanguages extends CreatorPresetEditableBase {
protected setupQuestionsValueCore(model: SurveyModel, json: any, creator: SurveyCreatorModel): void {
json = json || {};
model.setValue(this.creatorLocaleName, json.creator);
model.setValue(this.surveyShowInEnglishName, json.showInEnglish === true);
model.setValue(this.surveyUseEnglishNames, json.showInEnglish === true);
this.updateLocaleNames(model);
const question = <QuestionCheckboxModel>model.getQuestionByName(this.surveyLocalesName);
const locales = json.surveyLocales;
Expand All @@ -77,14 +71,14 @@ export class CreatorPresetEditableLanguages extends CreatorPresetEditableBase {
}
}
protected updateOnValueChangedCore(model: SurveyModel, name: string): void {
if(name === this.surveyShowInEnglishName) {
if(name === this.surveyUseEnglishNames) {
this.updateLocaleNames(model);
}
}
private get creatorLocaleName() : string { return this.path + "_creator"; }
private get surveyLocalesName(): string { return this.path + "_surveyLocales"; }
private get surveyShowInEnglishName(): string { return this.path + "_surveyShowInEnglish"; }
private getIsShowInEnglishSelected(model: SurveyModel): boolean { return model.getValue(this.surveyShowInEnglishName) === true; }
private get surveyUseEnglishNames(): string { return this.path + "_surveyUseEnglishNames"; }
private getIsShowInEnglishSelected(model: SurveyModel): boolean { return model.getValue(this.surveyUseEnglishNames) === true; }
private getCreatorLocales(): Array<ItemValue> {
return this.getLocaleItemValues(editorLocalization.getLocales(), false);
}
Expand Down
4 changes: 2 additions & 2 deletions packages/creator-presets-core/tests/presets-editor.tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -815,15 +815,15 @@ test("Preset edit model, Languages tab - show in English", () => {
const editor = new CreatorPresetEditorModel();
const survey = editor.model;
const surveyLocalesQuestion = <QuestionCheckboxModel>survey.getQuestionByName("languages_surveyLocales");
const showInEnglishQuestion = <QuestionBooleanModel>survey.getQuestionByName("languages_surveyShowInEnglish");
const showInEnglishQuestion = <QuestionBooleanModel>survey.getQuestionByName("languages_surveyUseEnglishNames");

expect(showInEnglishQuestion.value).toBe(false);
const item = ItemValue.getItemByValue(surveyLocalesQuestion.choices, "de");
expect(item.text).toBe("DE");
showInEnglishQuestion.value = true;
expect(item.text).toBe("DE-English");
expect(editor.applyFromSurveyModel()).toBeTruthy();
expect(editor.json.languages?.showNamesInEnglish).toBeTruthy();
expect(editor.json.languages?.useEnglishNames).toBeTruthy();
expect(surveyLocalization.showNamesInEnglish).toBeTruthy();

surveyLocalization.showNamesInEnglish = false;
Expand Down
4 changes: 2 additions & 2 deletions packages/survey-creator-core/src/presets/presets-languages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ export class CreatorPresetLanguages extends CreatorPresetBase {
super.applyCore(creator);
const locale = this.json?.creator || "";
const supportedLocales = this.json?.surveyLocales || [];
const showNamesInEnglish = this.json?.showNamesInEnglish;
const useEnglishNames = this.json?.useEnglishNames;
creator.locale = locale;
surveyLocalization.supportedLocales = supportedLocales;
surveyLocalization.showNamesInEnglish = showNamesInEnglish === true;
surveyLocalization.showNamesInEnglish = useEnglishNames === true;
}
}
2 changes: 1 addition & 1 deletion packages/survey-creator-core/src/presets/presets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export interface ICreatorPresetData {
languages?: {
creator?: string,
surveyLocales?: Array<string>,
showNamesInEnglish?: boolean,
useEnglishNames?: boolean,
};
propertyGrid?: {
definition?: ISurveyPropertyGridDefinition,
Expand Down

0 comments on commit ea8deb1

Please sign in to comment.