Skip to content

Commit

Permalink
Fix rating question serialization issue
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewtelnov committed Oct 13, 2023
1 parent 5812b8f commit cb0ade3
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 10 deletions.
23 changes: 15 additions & 8 deletions src/jsonobject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ export class JsonObjectProperty implements IObject {
public classNamePart: string;
public baseClassName: string;
public defaultValueValue: any;
public defaultValueFunc: () => any;
public defaultValueFunc: (obj: Base) => any;
public serializationProperty: string;
public displayName: string;
public category: string = "";
Expand Down Expand Up @@ -310,8 +310,8 @@ export class JsonObjectProperty implements IObject {
public get hasToUseGetValue() {
return this.onGetValue || this.serializationProperty;
}
public get defaultValue() {
let result: any = !!this.defaultValueFunc ? this.defaultValueFunc() : this.defaultValueValue;
public getDefaultValue(obj: Base): any {
let result: any = !!this.defaultValueFunc ? this.defaultValueFunc(obj) : this.defaultValueValue;
if (
!!JsonObjectProperty.getItemValuesDefaultValue &&
JsonObject.metaData.isDescendantOf(this.className, "itemvalue")
Expand All @@ -320,12 +320,19 @@ export class JsonObjectProperty implements IObject {
}
return result;
}
public set defaultValue(newValue) {
public get defaultValue(): any {
return this.getDefaultValue(undefined);
}
public set defaultValue(newValue: any) {
this.defaultValueValue = newValue;
}
public isDefaultValue(value: any): boolean {
if (!Helpers.isValueEmpty(this.defaultValue)) {
return Helpers.isTwoValueEquals(value, this.defaultValue, false, true, false);
return this.isDefaultValueByObj(undefined, value);
}
public isDefaultValueByObj(obj: Base, value: any): boolean {
const dValue = this.getDefaultValue(obj);
if (!Helpers.isValueEmpty(dValue)) {
return Helpers.isTwoValueEquals(value, dValue, false, true, false);
}
if(this.isLocalizable) return value === null || value === undefined;
return (
Expand Down Expand Up @@ -1588,7 +1595,7 @@ export class JsonObject {
)
return;
var value = property.getValue(obj);
if (!storeDefaults && property.isDefaultValue(value)) return;
if (!storeDefaults && property.isDefaultValueByObj(obj, value)) return;
if (this.isValueArray(value)) {
var arrValue = [];
for (var i = 0; i < value.length; i++) {
Expand All @@ -1601,7 +1608,7 @@ export class JsonObject {
var hasValue =
typeof obj["getPropertyValue"] === "function" &&
obj["getPropertyValue"](property.name, null) !== null;
if ((storeDefaults && hasValue) || !property.isDefaultValue(value)) {
if ((storeDefaults && hasValue) || !property.isDefaultValueByObj(obj, value)) {
if (!Serializer.onSerializingProperty || !Serializer.onSerializingProperty(obj, property, value, result)) {
result[property.name] = value;
}
Expand Down
5 changes: 3 additions & 2 deletions src/question_rating.ts
Original file line number Diff line number Diff line change
Expand Up @@ -531,7 +531,7 @@ export class QuestionRatingModel extends Question {
public get isSmiley() {
return this.rateType == "smileys";
}
protected getDefaultItemComponent(): string {
getDefaultItemComponent(): string {
if (this.renderAs == "dropdown") return "";
if (this.isStar) return "sv-rating-item-star";
if (this.isSmiley) return "sv-rating-item-smiley";
Expand Down Expand Up @@ -960,7 +960,8 @@ Serializer.addClass(
choices: ["auto", "buttons", "dropdown"],
visibleIndex: 20
},
{ name: "itemComponent", visible: false }
{ name: "itemComponent", visible: false,
defaultFunc: (obj: any): any => { return !!obj ? obj.getDefaultItemComponent(): "sv-rating-item"; } }
],
function () {
return new QuestionRatingModel("");
Expand Down
7 changes: 7 additions & 0 deletions tests/question_ratingtests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1483,4 +1483,11 @@ QUnit.test("rating items custom component", (assert) => {
const survey2 = new SurveyModel(json2);
const q2 = <QuestionRatingModel>survey2.getQuestionByName("q1");
assert.equal(q2.itemComponent, "custom-item");
});
QUnit.test("Generate empty rating", (assert) => {
const q1 = new QuestionRatingModel("q1");
assert.deepEqual(q1.toJSON(), { name: "q1" });
q1.rateType = "stars";
assert.deepEqual(q1.toJSON(), { name: "q1", rateType: "stars" });
q1.rateType = "stars";
});

0 comments on commit cb0ade3

Please sign in to comment.