Skip to content

Commit

Permalink
Merge pull request #7635 from surveyjs/bug/7630-custom-local-prop-def…
Browse files Browse the repository at this point in the history
…aultvalue

default value attribute doesn't work correctly for custom localizable…
  • Loading branch information
andrewtelnov authored Jan 9, 2024
2 parents 1823f0d + f6a1b21 commit 18b7759
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 7 deletions.
8 changes: 4 additions & 4 deletions src/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -810,10 +810,10 @@ export class Base {
public unRegisterFunctionOnPropertiesValueChanged(names: Array<string>, key: string = null): void {
this.unregisterPropertyChangedHandlers(names, key);
}
public createCustomLocalizableObj(name: string) {
var locStr = this.getLocalizableString(name);
if (locStr) return;
this.createLocalizableString(name, <ILocalizableOwner>(<any>this), false, true);
public createCustomLocalizableObj(name: string): LocalizableString {
const locStr = this.getLocalizableString(name);
if(locStr) return locStr;
return this.createLocalizableString(name, <ILocalizableOwner>(<any>this), false, true);
}
public getLocale(): string {
const locOwner = this.getSurvey();
Expand Down
5 changes: 3 additions & 2 deletions src/jsonobject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -574,7 +574,8 @@ export class CustomPropertiesCollection {
prop.serializationProperty &&
obj.createCustomLocalizableObj
) {
obj.createCustomLocalizableObj(prop.name);
const locStr = obj.createCustomLocalizableObj(prop.name);
locStr.defaultValue = prop.defaultValue;
var locDesc = {
get: function () {
return obj.getLocalizableString(prop.name);
Expand All @@ -583,7 +584,7 @@ export class CustomPropertiesCollection {
Object.defineProperty(obj, prop.serializationProperty, locDesc);
var desc = {
get: function () {
return obj.getLocalizableStringText(prop.name, prop.defaultValue);
return obj.getLocalizableStringText(prop.name);
},
set: function (v: any) {
obj.setLocalizableStringText(prop.name, v);
Expand Down
3 changes: 2 additions & 1 deletion src/localizablestring.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ export class LocalizableString implements ILocalizableString {
public searchText: string;
public searchIndex: number;
public disableLocalization: boolean;
public defaultValue: string;
constructor(
public owner: ILocalizableOwner,
public useMarkdown: boolean = false,
Expand Down Expand Up @@ -131,7 +132,7 @@ export class LocalizableString implements ILocalizableString {
res = this.onGetLocalizationTextCallback(res);
}
}
if (!res) res = "";
if (!res) res = this.defaultValue || "";
return res;
}
private getRootDialect(loc: string): string {
Expand Down
28 changes: 28 additions & 0 deletions tests/jsonobjecttests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { SurveyModel } from "../src/survey";
import { CalculatedValue } from "../src/calculatedValue";
import { QuestionHtmlModel } from "../src/question_html";
import { ImageItemValue } from "../src/question_imagepicker";
import { PageModel } from "../src/page";

class Car extends Base implements ILocalizableOwner {
public locale: string;
Expand Down Expand Up @@ -3101,3 +3102,30 @@ QUnit.test("Validated property values", function (assert) {
assert.equal(survey.jsonErrors[1].message, "The property value: 'edf' is incorrect for property 'textUpdateMode'.", "errors[1].message");
assert.equal(survey.jsonErrors[1].element.getType(), "text", "errors[1].element");
});
QUnit.test("Create localizable property with default value", function (assert) {
Serializer.addProperty("question", { name: "customProp:text", isLocalizable: true, default: "Question text" });
Serializer.addProperty("page", { name: "customProp:text", isLocalizable: true, default: "Page text" });
const question = new Question("q1");
const page = new PageModel("page1");
assert.equal(question["customProp"], "Question text", "Question prop #1");
assert.equal(page["customProp"], "Page text", "Page prop #1");
assert.equal(question.getPropertyValue("customProp"), "Question text", "Question getPropertyValue #1");
assert.equal(page.getPropertyValue("customProp"), "Page text", "Page getPropertyValue #1");

question["customProp"] = "Set question val";
page["customProp"] = "Set page val";
assert.equal(question["customProp"], "Set question val", "Question prop #2");
assert.equal(page["customProp"], "Set page val", "Page prop #2");
assert.equal(question.getPropertyValue("customProp"), "Set question val", "Question getPropertyValue #2");
assert.equal(page.getPropertyValue("customProp"), "Set page val", "Page getPropertyValue #2");

question.resetPropertyValue("customProp");
page.resetPropertyValue("customProp");
assert.equal(question["customProp"], "Question text", "Question prop #3");
assert.equal(page["customProp"], "Page text", "Page prop #3");
assert.equal(question.getPropertyValue("customProp"), "Question text", "Question getPropertyValue #3");
assert.equal(page.getPropertyValue("customProp"), "Page text", "Page getPropertyValue #3");

Serializer.removeProperty("question", "customProp");
Serializer.removeProperty("page", "customProp");
});
21 changes: 21 additions & 0 deletions tests/localizablestringtests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -835,3 +835,24 @@ QUnit.test("Fire onStringChanged when localizationName is set", function (assert
locString.localizationName = "completeText";
assert.equal(callCount, 1, "onStringChanged is called");
});
QUnit.test("Support defaultValue for localizable strings", function (assert) {
const owner = new LocalizableOwnerTester("");
const locStr1 = new LocalizableString(owner, true);
locStr1.defaultValue = "str1";
const locStr2 = new LocalizableString(owner, true, "locStr2");
locStr2.defaultValue = "str2";
const locStr3 = new LocalizableString(owner, true);
locStr3.localizationName = "completeText";
locStr3.defaultValue = "str3";
assert.equal(locStr1.text, "str1", "str1 #1");
assert.equal(locStr2.text, "str2", "str2 #1");
assert.equal(locStr3.text, "Complete", "str3 #1");

locStr1.text = "new str1";
assert.equal(locStr1.text, "new str1", "str1 #2");

owner.locale = "de";
assert.equal(locStr1.text, "new str1", "str1 #3");
assert.equal(locStr2.text, "str2", "str2 #3");
assert.equal(locStr3.text, "Abschließen", "str3 #3");
});

0 comments on commit 18b7759

Please sign in to comment.