Skip to content

Commit

Permalink
Merge branch 'bug/7643-fromjson-matrices-performance' of https://gith…
Browse files Browse the repository at this point in the history
…ub.com/surveyjs/survey-library into bug/7643-fromjson-matrices-performance
  • Loading branch information
andrewtelnov committed Jan 9, 2024
2 parents ba82767 + 0c983b2 commit 2b90ad0
Show file tree
Hide file tree
Showing 14 changed files with 162 additions and 42 deletions.
8 changes: 4 additions & 4 deletions src/actions/action.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ILocalizableOwner, LocalizableString } from "../localizablestring";
import { Base } from "../base";
import { Base, ComputedUpdater } from "../base";
import { surveyLocalization } from "../surveyStrings";
import { property } from "../jsonobject";
import { IListModel, ListModel } from "../list";
Expand Down Expand Up @@ -28,7 +28,7 @@ export interface IAction {
* @see enabled
* @see active
*/
visible?: boolean;
visible?: boolean | ComputedUpdater<boolean>;
/**
* The action item's title.
*
Expand All @@ -49,7 +49,7 @@ export interface IAction {
* @see active
* @see visible
*/
enabled?: boolean;
enabled?: boolean | ComputedUpdater<boolean>;
enabledIf?: () => boolean;
/**
* Specifies the visibility of the action item's title.
Expand Down Expand Up @@ -448,7 +448,7 @@ export class Action extends BaseAction implements IAction, ILocalizableOwner {
this._enabled = val;
}
public getEnabled(): boolean {
if(this.enabledIf) return this.enabledIf();
if (this.enabledIf) return this.enabledIf();
return this._enabled;
}
public setComponent(val: string): void {
Expand Down
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/defaultV2-theme/blocks/sd-table.scss
Original file line number Diff line number Diff line change
Expand Up @@ -284,9 +284,10 @@
fill: $font-questiondescription-color;
}

&:hover {
&:hover,
&:focus {
background: $primary-light;

outline: none;
svg {
fill: $primary;
}
Expand Down
5 changes: 3 additions & 2 deletions src/jsonobject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -579,7 +579,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 @@ -588,7 +589,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
18 changes: 18 additions & 0 deletions src/survey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3131,6 +3131,24 @@ export class SurveyModel extends SurveyElementCore
this.currentPageChanged(newPage, oldValue);
}
}
public tryNavigateToPage(page: PageModel): boolean {
if (this.isDesignMode) return false;
const index = this.visiblePages.indexOf(page);
if(index < 0) return false;
if(index === this.currentPageNo) return false;
if (index < this.currentPageNo) {
this.currentPageNo = index;
return true;
}
for (let i = this.currentPageNo; i < index; i++) {
const page = this.visiblePages[i];
if(!page.validate(true, true)) return false;
page.passed = true;
}
this.currentPage = page;
return true;
}

private updateCurrentPage(): void {
if (this.isCurrentPageAvailable) return;
this.currentPage = this.firstVisiblePage;
Expand Down
10 changes: 1 addition & 9 deletions src/surveyProgressButtons.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,6 @@ export class SurveyProgressButtonsModel {
.toString();
}
public clickListElement(index: number): void {
if (this.survey.isDesignMode) return;
if (index < this.survey.currentPageNo) {
this.survey.currentPageNo = index;
}
else if (index > this.survey.currentPageNo) {
for (let i: number = this.survey.currentPageNo; i < index; i++) {
if (!this.survey.nextPage()) break;
}
}
this.survey.tryNavigateToPage(this.survey.visiblePages[index]);
}
}
18 changes: 2 additions & 16 deletions src/surveyToc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,7 @@ import { PopupModel } from "./popup";
import { SurveyModel } from "./survey";
import { IsTouch } from "./utils/devices";

export function tryNavigateToPage(survey: SurveyModel, page: PageModel) {
if (survey.isDesignMode) return;
const index = survey.visiblePages.indexOf(page);
if (index < survey.currentPageNo) {
survey.currentPageNo = index;
}
else if (index > survey.currentPageNo) {
for (let i = survey.currentPageNo; i < index; i++) {
if (!survey.nextPageUIClick()) return false;
}
}
return true;
}

export function tryFocusPage(survey: SurveyModel, panel: PanelModelBase) {
export function tryFocusPage(survey: SurveyModel, panel: PanelModelBase): boolean {
if (survey.isDesignMode) return true;
panel.focusFirstQuestion();
return true;
Expand All @@ -40,7 +26,7 @@ export function createTOCListModel(survey: SurveyModel, onAction?: () => void) {
}
!!onAction && onAction();
if (page instanceof PageModel) {
return tryNavigateToPage(survey, page);
return survey.tryNavigateToPage(page);
}
return tryFocusPage(survey, page);
},
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 @@ -3111,3 +3112,30 @@ QUnit.test("getRequiredProperties", function (assert) {
requiedValues = Serializer.getRequiredProperties("text");
assert.deepEqual(requiedValues, ["name"], "required #3");
});
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");
});
8 changes: 5 additions & 3 deletions tests/surveyProgressButtonsTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { SurveyProgressButtonsModel } from "../src/surveyProgressButtons";
export default QUnit.module("SurveyProgressButtons");

QUnit.test("SurveyProgressButtonsModel list elements", function(assert) {
let json: any = {
const json: any = {
"pages": [
{
"name": "page1",
Expand Down Expand Up @@ -35,8 +35,8 @@ QUnit.test("SurveyProgressButtonsModel list elements", function(assert) {
}
]
};
let survey: SurveyModel = new SurveyModel(json);
let progress: SurveyProgressButtonsModel = new SurveyProgressButtonsModel(survey);
const survey: SurveyModel = new SurveyModel(json);
const progress: SurveyProgressButtonsModel = new SurveyProgressButtonsModel(survey);
assert.equal(progress.getListElementCss(0),
survey.css.progressButtonsListElementCurrent,
"1) Page 1 style is current");
Expand All @@ -46,6 +46,7 @@ QUnit.test("SurveyProgressButtonsModel list elements", function(assert) {
"", "1) Page 3 style is empty");

progress.clickListElement(2);
assert.equal(survey.currentPageNo, 2, "currentPageNo #1");
assert.equal(progress.getListElementCss(0),
survey.css.progressButtonsListElementPassed,
"2) Page 1 style is passed");
Expand All @@ -57,6 +58,7 @@ QUnit.test("SurveyProgressButtonsModel list elements", function(assert) {
"2) Page 3 style is current");

progress.clickListElement(0);
assert.equal(survey.currentPageNo, 0, "currentPageNo #2");
assert.equal(progress.getListElementCss(0),
survey.css.progressButtonsListElementPassed + " " +
survey.css.progressButtonsListElementCurrent,
Expand Down
65 changes: 65 additions & 0 deletions tests/surveyTOCTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -325,3 +325,68 @@ QUnit.test("TOC shouldn't show search", function (assert) {
const tocListModel = createTOCListModel(survey);
assert.equal(tocListModel.searchEnabled, false, "Search in TOC should be disabled");
});
QUnit.test("survey.tryNavigateToPage", function (assert) {
let json: any = {
"pages": [
{
"name": "page1",
"elements": [
{
"type": "text",
"name": "question1"
}
]
},
{
"name": "page2",
"elements": [
{
"type": "text",
"name": "question2",
"isRequired": true
}
]
},
{
"name": "page3",
"elements": [
{
"type": "text",
"name": "question3",
"isRequired": true
}
]
},
{
"name": "page4",
"elements": [
{
"type": "text",
"name": "question4"
}
]
}
]
};
const survey = new SurveyModel(json);
const pages = new Array<string>();
survey.onCurrentPageChanged.add((sender, options) => {
pages.push(options.newCurrentPage.name);
});
assert.equal(survey.currentPageNo, 0, "currentPageNo #1");
assert.equal(survey.tryNavigateToPage(survey.pages[3]), false, "navigate #1");
assert.equal(survey.currentPageNo, 1, "currentPageNo #2");
assert.equal(survey.tryNavigateToPage(survey.pages[2]), false, "navigate #2");
assert.equal(survey.currentPageNo, 1, "currentPageNo #3");
assert.equal(survey.tryNavigateToPage(survey.pages[0]), true, "navigate #3");
assert.equal(survey.currentPageNo, 0, "currentPageNo #4");
survey.setValue("question2", "val2");
assert.equal(survey.tryNavigateToPage(survey.pages[3]), false, "navigate #4");
assert.equal(survey.currentPageNo, 2, "currentPageNo #4");
assert.equal(survey.tryNavigateToPage(survey.pages[0]), true, "navigate #5");
assert.equal(survey.currentPageNo, 0, "currentPageNo #5");
survey.setValue("question3", "val3");
assert.equal(survey.tryNavigateToPage(survey.pages[3]), true, "navigate #6");
assert.equal(survey.currentPageNo, 3, "currentPageNo #6");
assert.deepEqual(pages, ["page2", "page1", "page3", "page1", "page4"], "Check onCurrentPageChanged");
});
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 6 additions & 1 deletion visualRegressionTests/tests/defaultV2/matrix.ts
Original file line number Diff line number Diff line change
Expand Up @@ -656,7 +656,7 @@ frameworks.forEach(framework => {
await takeElementScreenshot("question-matrix-dropdown-detail-no-header-expanded.png", questionRoot, t, comparer);

await t.click(Selector(".sd-table__cell--detail-button").filterVisible().nth(1));
await t.hover(questionRoot, { offsetX: 1, offsetY: 1 });
await t.click(questionRoot, { offsetX: 1, offsetY: 1 });

await takeElementScreenshot("question-matrix-dropdown-detail-no-header.png", questionRoot, t, comparer);
});
Expand Down Expand Up @@ -736,6 +736,11 @@ frameworks.forEach(framework => {

await t.hover(Selector(".sd-table__cell--detail-button"));
await takeElementScreenshot("question-matrix-detail-hover.png", questionRoot, t, comparer);

await t
.click(Selector("body"), { offsetX: 5, offsetY: 5 });
await t.pressKey("tab");
await takeElementScreenshot("question-matrix-detail-focus.png", questionRoot, t, comparer);
});
});

Expand Down

0 comments on commit 2b90ad0

Please sign in to comment.