Skip to content

Commit

Permalink
Add sjsVersion property into survey and show warning on loading JSON … (
Browse files Browse the repository at this point in the history
#9261)

* Add sjsVersion property into survey and show warning on loading JSON created by newer version of Creator #9219

* Change warning text
  • Loading branch information
andrewtelnov authored Jan 9, 2025
1 parent fccf10d commit c9e0fe6
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 1 deletion.
2 changes: 2 additions & 0 deletions packages/survey-core/entries/chunks/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
//import "../../src/modern.scss";

import { DomWindowHelper } from "../../src/global_variables_utils";
import { settings } from "../../src/settings";

export var Version: string;
export var ReleaseDate: string;
Version = `${process.env.VERSION}`;
settings.version = Version;
ReleaseDate = `${process.env.RELEASE_DATE}`;

export function checkLibraryVersion(ver: string, libraryName: string): void {
Expand Down
1 change: 1 addition & 0 deletions packages/survey-core/src/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ const columnWidthsByType: { [index: string]: { minWidth?: string, width?: string
*/

export var settings = {
version: "",
/**
* An object that configures survey appearance when the survey is being designed in Survey Creator.
*
Expand Down
17 changes: 16 additions & 1 deletion packages/survey-core/src/survey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ import { SurveyTaskManagerModel } from "./surveyTaskManager";
import { ProgressButtons } from "./progress-buttons";
import { TOCModel } from "./surveyToc";
import { DomDocumentHelper, DomWindowHelper } from "./global_variables_utils";
import { ConsoleWarnings } from "./console-warnings";

/**
* The `SurveyModel` object contains properties and methods that allow you to control the survey and access its elements.
Expand Down Expand Up @@ -1116,7 +1117,12 @@ export class SurveyModel extends SurveyElementCore

this.locTitle.onStringChanged.add(() => this.titleIsEmpty = this.locTitle.isEmpty);
}

public get sjsVersion(): string {
return this.getPropertyValue("sjsVersion");
}
public set sjsVersion(val: string) {
this.setPropertyValue("sjsVersion", val);
}
processClosedPopup(question: IQuestion, popupModel: PopupModel<any>): void {
throw new Error("Method not implemented.");
}
Expand Down Expand Up @@ -6515,13 +6521,21 @@ export class SurveyModel extends SurveyElementCore
if (!json) return;
this.questionHashesClear();
this.jsonErrors = null;
this.sjsVersion = undefined;
const jsonConverter = new JsonObject();
jsonConverter.toObject(json, this, options);
if (jsonConverter.errors.length > 0) {
this.jsonErrors = jsonConverter.errors;
}
this.onStateAndCurrentPageChanged();
this.updateState();
if(!!this.sjsVersion && !!settings.version) {
if(Helpers.compareVerions(this.sjsVersion, settings.version) > 0) {
ConsoleWarnings.warn("The version of the survey JSON schema (v"
+ this.sjsVersion + ") is newer than your current Form Library version ("
+ settings.version + "). Please update the Form Library to make sure that all survey features work as expected.");
}
}
}
startLoadingFromJson(json?: any): void {
super.startLoadingFromJson(json);
Expand Down Expand Up @@ -8318,6 +8332,7 @@ Serializer.addClass("survey", [
name: "calculatedValues:calculatedvalues",
className: "calculatedvalue", isArray: true
},
{ name: "sjsVersion", visible: false },
{ name: "surveyId", visible: false },
{ name: "surveyPostId", visible: false },
{ name: "surveyShowDataSaving:boolean", visible: false },
Expand Down
24 changes: 24 additions & 0 deletions packages/survey-core/tests/surveytests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ import { DomWindowHelper } from "../src/global_variables_utils";
import { ListModel } from "../src/list";
import { _setIsTouch } from "../src/utils/devices";
import { oldDefaultTheme, setOldTheme } from "./oldTheme";
import { ConsoleWarnings } from "../src/console-warnings";

export default QUnit.module("Survey");

settings.autoAdvanceDelay = 0;
Expand Down Expand Up @@ -21143,3 +21145,25 @@ QUnit.test("#9110 check focus question inside paneldynamic works correctly", fun
SurveyElement.ScrollElementToViewCore = oldScrollElementToViewCore;
SurveyElement.ScrollElementToTop = oldScrollElementToTop;
});
QUnit.test("Show warning on loadig JSON created in higher version of Creator", function (assert) {
const oldVersion = settings.version;
const prevWarn = ConsoleWarnings.warn;
let reportText: string = "";
ConsoleWarnings.warn = (text: string) => {
reportText = text;
};
const checkFunc = (jsonVer: string, sjsVer: string, showWarn: boolean): void => {
reportText = "";
settings.version = sjsVer;
new SurveyModel({
sjsVersion: jsonVer
});
assert.equal(!!reportText, showWarn, "jsonVersion: " + jsonVer + ", sjsVer: " + sjsVer);
};
checkFunc("1.12.19", "2.0.2", false);
checkFunc("2.0.2", "1.12.19", true);
checkFunc("2.0.2", "2.0.2", false);
checkFunc("2.0.3", "2.0.2", true);
ConsoleWarnings.warn = prevWarn;
settings.version = oldVersion;
});

0 comments on commit c9e0fe6

Please sign in to comment.