Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement readOnly property for survey #6291

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions src/survey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2489,6 +2489,17 @@ export class SurveyModel extends SurveyElementCore
if (value != "edit" && value != "display") return;
this.setPropertyValue("mode", value);
}

public get readOnly(): boolean {
const mode = this.mode;
const readOnly = this.getPropertyValue("readOnly");
return mode === "display" || readOnly;
}

public set readOnly(val: boolean) {
this.setPropertyValue("readOnly", val);
}

private onModeChanged() {
for (var i = 0; i < this.pages.length; i++) {
var page = this.pages[i];
Expand Down Expand Up @@ -3229,15 +3240,15 @@ export class SurveyModel extends SurveyElementCore
* @see mode
*/
public get isEditMode(): boolean {
return this.mode == "edit";
return !this.readOnly;
}
/**
* Returns `true` if the survey is in display mode or in preview mode.
* @see mode
* @see showPreviewBeforeComplete
*/
public get isDisplayMode(): boolean {
return this.mode == "display" || this.state == "preview";
return this.readOnly || this.state == "preview";
}
public get isUpdateValueTextOnTyping(): boolean {
return this.textUpdateMode == "onTyping";
Expand Down Expand Up @@ -4352,7 +4363,7 @@ export class SurveyModel extends SurveyElementCore
return new CssClassBuilder()
.append(this.css.root)
.append(this.css.rootMobile, this.isMobile)
.append(this.css.rootReadOnly, this.mode === "display")
.append(this.css.rootReadOnly, this.readOnly)
.toString();
}
private resizeObserver: ResizeObserver;
Expand Down Expand Up @@ -7107,6 +7118,7 @@ Serializer.addClass("survey", [
default: "left",
choices: ["left", "right"],
},
{ name: "readOnly:boolean", default: false },
{ name: "mode", default: "edit", choices: ["edit", "display"] },
{ name: "storeOthersAsComment:boolean", default: true },
{ name: "maxTextLength:number", default: 0, minValue: 0 },
Expand Down
28 changes: 28 additions & 0 deletions tests/surveytests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17027,3 +17027,31 @@ QUnit.test("Do not run onComplete twice if complete trigger and completeLastPage
survey.completeLastPage();
assert.equal(counter, 1, "onComplete called one time");
});

QUnit.test("Check readOnly flag", function (assert) {
let survey = new SurveyModel({
readOnly: true,
"elements": [
{
"type": "text",
"name": "question1"
},
],
});
assert.equal(survey.mode, "edit");
assert.ok(survey.readOnly);
survey.readOnly = false;
assert.notOk(survey.readOnly);
survey = new SurveyModel({
mode: "display",
"elements": [
{
"type": "text",
"name": "question1"
},
],
});
assert.ok(survey.readOnly);
survey.mode = "edit";
assert.notOk(survey.readOnly);
});