From c9e0fe6d2e1cfe3a196c45782b089a4a29bb7c88 Mon Sep 17 00:00:00 2001 From: Andrew Date: Thu, 9 Jan 2025 08:45:26 +0000 Subject: [PATCH] =?UTF-8?q?Add=20sjsVersion=20property=20into=20survey=20a?= =?UTF-8?q?nd=20show=20warning=20on=20loading=20JSON=20=E2=80=A6=20(#9261)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Add sjsVersion property into survey and show warning on loading JSON created by newer version of Creator #9219 * Change warning text --- packages/survey-core/entries/chunks/model.ts | 2 ++ packages/survey-core/src/settings.ts | 1 + packages/survey-core/src/survey.ts | 17 +++++++++++++- packages/survey-core/tests/surveytests.ts | 24 ++++++++++++++++++++ 4 files changed, 43 insertions(+), 1 deletion(-) diff --git a/packages/survey-core/entries/chunks/model.ts b/packages/survey-core/entries/chunks/model.ts index 5f6befe235..6feca9cddc 100644 --- a/packages/survey-core/entries/chunks/model.ts +++ b/packages/survey-core/entries/chunks/model.ts @@ -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 { diff --git a/packages/survey-core/src/settings.ts b/packages/survey-core/src/settings.ts index 937bf30b5d..3d43267ef5 100644 --- a/packages/survey-core/src/settings.ts +++ b/packages/survey-core/src/settings.ts @@ -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. * diff --git a/packages/survey-core/src/survey.ts b/packages/survey-core/src/survey.ts index 006ba832bb..eb2d30f169 100644 --- a/packages/survey-core/src/survey.ts +++ b/packages/survey-core/src/survey.ts @@ -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. @@ -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): void { throw new Error("Method not implemented."); } @@ -6515,6 +6521,7 @@ 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) { @@ -6522,6 +6529,13 @@ export class SurveyModel extends SurveyElementCore } 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); @@ -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 }, diff --git a/packages/survey-core/tests/surveytests.ts b/packages/survey-core/tests/surveytests.ts index 2aa679c72a..457af720db 100644 --- a/packages/survey-core/tests/surveytests.ts +++ b/packages/survey-core/tests/surveytests.ts @@ -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; @@ -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; +});