Skip to content

Commit

Permalink
Add area "translation-tab-editor:table" for onSurveyInstanceCreated e… (
Browse files Browse the repository at this point in the history
#5305)

* Add area "translation-tab-editor:table" for onSurveyInstanceCreated event fix #5304

* Update etalon #5304
  • Loading branch information
andrewtelnov committed Mar 7, 2024
1 parent 2d27ec4 commit 2da865d
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 16 deletions.
38 changes: 29 additions & 9 deletions packages/survey-creator-core/src/components/tabs/translation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -830,6 +830,9 @@ export class Translation extends Base implements ITranslationLocales {
this.stringsHeaderSurvey = this.createStringsHeaderSurvey();
this.updateReadOnly();
}
protected getSurveyStringsArea(): string { return undefined; }
protected getSurveyStringsHeaderArea(): string { return undefined; }
protected onSurveyStringsCreated(survey: SurveyModel): void {}
private createStringsSurvey(): SurveyModel {
var json = { autoGrowComment: true, allowResizeComment: false };
setSurveyJSONForPropertyGrid(json, false);
Expand All @@ -842,7 +845,8 @@ export class Translation extends Base implements ITranslationLocales {
this.addTranslationGroupIntoStringsSurvey(survey.pages[0], this.root);
survey.data = this.getStringsSurveyData(survey);
survey.endLoadingFromJson();
});
this.onSurveyStringsCreated(survey);
}, this.getSurveyStringsArea());
const getTransationItem = (question: QuestionMatrixDropdownModel, rowName: any): TranslationItem => {
var itemValue = ItemValue.getItemByValue(question.rows, rowName);
return !!itemValue ? itemValue["translationData"] : null;
Expand Down Expand Up @@ -904,7 +908,7 @@ export class Translation extends Base implements ITranslationLocales {

newPage.addQuestion(matrix);
survey.currentPage = survey.pages[0];
});
}, this.getSurveyStringsHeaderArea());
return survey;
}
private addTranslationGroupIntoStringsSurvey(
Expand Down Expand Up @@ -1337,6 +1341,20 @@ export class Translation extends Base implements ITranslationLocales {
super.dispose();
}
}
export class TranslationForEditor extends Translation {
constructor(
survey: SurveyModel,
options: ISurveyCreatorOptions,
private surveyStringsCreatedCallback: (survey: SurveyModel) => void
) {
super(survey, options, true);
}
protected getSurveyStringsArea(): string { return "translation-tab-editor:table"; }
protected getSurveyStringsHeaderArea(): string { return "translation-tab-editor:table-header"; }
protected onSurveyStringsCreated(survey: SurveyModel): void {
this.surveyStringsCreatedCallback(survey);
}
}
export class TranslationEditor {
public fromLocales: Array<string> = [];
private survey: SurveyModel;
Expand All @@ -1349,11 +1367,16 @@ export class TranslationEditor {
this.survey = survey;
this.options = options;
this.locale = locale;
this.translationValue = new Translation(this.survey, this.options, true);
this.translationValue = new TranslationForEditor(this.survey, this.options, (survey: SurveyModel) => {
this.setupNavigationButtons(survey);
});
this.translation.setEditMode(this.locale);
this.translation.reset();
this.fillFromLocales();
this.setupNavigationButtons();
const action = this.translation.stringsSurvey.navigationBar.getActionById("svc-translation-fromlocale");
if(!!action) {
action.visible = this.fromLocales.length > 0;
}
}
public get translation(): Translation { return this.translationValue; }
public showDialog(): void {
Expand Down Expand Up @@ -1456,14 +1479,11 @@ export class TranslationEditor {
item.fillLocales(this.fromLocales);
});
}
private setupNavigationButtons(): void {
const survey = this.translation.stringsSurvey;
private setupNavigationButtons(survey: SurveyModel): void {
const actions = survey.navigationBar.actions;
actions.splice(0, actions.length);
if (this.options.getHasMachineTranslation()) {
if (this.fromLocales.length > 0) {
survey.addNavigationItem(this.createLocaleFromAction());
}
survey.addNavigationItem(this.createLocaleFromAction());
survey.addNavigationItem(new Action({
id: "svc-translation-machine",
iconName: "icon-language",
Expand Down
5 changes: 3 additions & 2 deletions packages/survey-creator-core/src/creator-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1999,7 +1999,7 @@ export class SurveyCreatorModel extends Base
return options.displayName;
}

public createSurvey(json: any, reason: string, model?: any, callback?: (survey: SurveyModel) => void): SurveyModel {
public createSurvey(json: any, reason: string, model?: any, callback?: (survey: SurveyModel) => void, area?: string): SurveyModel {
const survey = this.createSurveyCore(json, reason);

if (reason !== "designer" && reason !== "test") { survey.fitToContainer = false; }
Expand All @@ -2017,10 +2017,11 @@ export class SurveyCreatorModel extends Base
if (callback) {
callback(survey);
}
area = area || this.getSurveyInstanceCreatedArea(reason);
this.onSurveyInstanceCreated.fire(this, {
survey: survey,
reason: reason,
area: this.getSurveyInstanceCreatedArea(reason),
area: area,
model: !!model ? model : this.currentPlugin?.model
});
if (reason === "designer") {
Expand Down
4 changes: 2 additions & 2 deletions packages/survey-creator-core/src/creator-settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ export interface ISurveyCreatorOptions {
onGetElementEditorTitleCallback(obj: Base, title: string): string;
startUndoRedoTransaction();
stopUndoRedoTransaction();
createSurvey(json: any, reason: string, model?: any, callback?: (survey: SurveyModel)=> void);
createSurvey(json: any, reason: string, model?: any, callback?: (survey: SurveyModel)=> void, area?: string);
onConditionQuestionsGetListCallback(
propertyName: string,
obj: Base,
Expand Down Expand Up @@ -444,7 +444,7 @@ export class EmptySurveyCreatorOptions implements ISurveyCreatorOptions {
}
startUndoRedoTransaction() { }
stopUndoRedoTransaction() { }
createSurvey(json: any, reason: string, model?: any, callback?: (survey: SurveyModel) => void): SurveyModel {
createSurvey(json: any, reason: string, model?: any, callback?: (survey: SurveyModel) => void, area?: string): SurveyModel {
const survey = new SurveyModel(json);
if(!!callback) {
callback(survey);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,9 +202,11 @@ test("Implement machine translation for Creator", () => {
expect(creator.getHasMachineTranslation()).toBeTruthy();
editor = tabTranslation.model.createTranslationEditor("fr");
actions = editor.translation.stringsSurvey.navigationBar.actions;
expect(actions).toHaveLength(3);
expect(actions[0].id).toBe("svc-translation-machine");
actions[0].action();
expect(actions).toHaveLength(4);
expect(actions[0].id).toBe("svc-translation-fromlocale");
expect(actions[0].visible).toBeFalsy();
expect(actions[1].id).toBe("svc-translation-machine");
actions[1].action();
editor.apply();
const q1 = creator.survey.getQuestionByName("q1");
expect(q1.locTitle.getLocaleText("fr")).toBe("Title fr");
Expand Down Expand Up @@ -281,6 +283,7 @@ test("Machine translation from non default locale - UI", () => {
expect(editor.fromLocales).toHaveLength(2);
const actions = editor.translation.stringsSurvey.navigationBar.actions;
expect(actions).toHaveLength(4);
expect(actions[1].visible).toBeTruthy();

expect(editor.translation.root.allLocItems).toHaveLength(3);
expect(editor.translation.getVisibleLocales()).toHaveLength(1);
Expand Down Expand Up @@ -410,4 +413,34 @@ test("Machine translation and cancel", () => {
default: "Title 1",
de: "de: Title 1"
});
});
test("Modify translation strings survey in a dialog, remove actions", () => {
const creator = new CreatorTester();
const json = {
pages: [
{
name: "page1",
elements: [
{
type: "text",
name: "q1",
title: "Title 1",
}
]
}
]
};
creator.onMachineTranslate.add((sender, options) => {});
creator.onSurveyInstanceCreated.add((sender, options) => {
if(options.area === "translation-tab-editor:table") {
const actions = options.survey.navigationBar.actions;
actions.splice(actions.length - 3, 3);
}
});
creator.JSON = json;
const tabTranslation = new TabTranslationPlugin(creator);
tabTranslation.activate();
const editor = tabTranslation.model.createTranslationEditor("de");
const actions = editor.translation.stringsSurvey.navigationBar.actions;
expect(actions).toHaveLength(1);
});
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 2da865d

Please sign in to comment.