Skip to content

Commit

Permalink
Merge branch 'master' into bug/8513-sticky-always
Browse files Browse the repository at this point in the history
  • Loading branch information
tsv2013 authored Jul 12, 2024
2 parents 8ee612b + 6231c31 commit 4d024e9
Show file tree
Hide file tree
Showing 6 changed files with 176 additions and 32 deletions.
1 change: 1 addition & 0 deletions src/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,7 @@ export class Base {
const prop = this.getPropertyByName(name);
if(!prop || prop.isCustom && this.isCreating) return undefined;
const dValue = prop.defaultValue;
if (!!prop.defaultValueFunc) return dValue;
if (!this.isPropertyEmpty(dValue) && !Array.isArray(dValue)) return dValue;
const locStr = this.localizableStrings ? this.localizableStrings[name] : undefined;
if(locStr && locStr.localizationName) return this.getLocalizationString(locStr.localizationName);
Expand Down
2 changes: 1 addition & 1 deletion src/jsonobject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,7 @@ export class JsonObjectProperty implements IObject, IJsonPropertyInfo {
}
if(this.isLocalizable) return value === null || value === undefined;
return (
(value === false && (this.type == "boolean" || this.type == "switch")) ||
(value === false && (this.type == "boolean" || this.type == "switch") && !this.defaultValueFunc) ||
value === "" || Helpers.isValueEmpty(value)
);
}
Expand Down
27 changes: 15 additions & 12 deletions src/question_comment.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { Question } from "./question";
import { Serializer } from "./jsonobject";
import { QuestionFactory } from "./questionfactory";
import { LocalizableString } from "./localizablestring";
import { QuestionTextBase } from "./question_textbase";
import { increaseHeightByContent } from "./utils/utils";
import { settings } from "./settings";
Expand Down Expand Up @@ -47,26 +45,31 @@ export class QuestionCommentModel extends QuestionTextBase {
* Default value: `false` (inherited from `SurveyModel`'s [`autoGrowComment`](https://surveyjs.io/form-library/documentation/surveymodel#autoGrowComment) property)
* @see allowResize
*/
public get autoGrow(): boolean {
return this.getPropertyValue("autoGrow") || (this.survey && this.survey.autoGrowComment);
public get autoGrow(): boolean | undefined {
return this.getPropertyValue("autoGrow");
}
public set autoGrow(val: boolean) {
public set autoGrow(val: boolean | undefined) {
this.setPropertyValue("autoGrow", val);
}
public get renderedAutoGrow(): boolean {
const autoGrow = this.autoGrow;
return autoGrow === undefined && this.survey ? this.survey.autoGrowComment : !!autoGrow;
}
/**
* Specifies whether to display a resize handle for the comment area.
*
* Default value: `true` (inherited from `SurveyModel`'s [`allowResizeComment`](https://surveyjs.io/form-library/documentation/surveymodel#allowResizeComment) property)
* @see autoGrow
*/
public get allowResize(): boolean {
public get allowResize(): boolean | undefined {
return this.getPropertyValue("allowResize");
}
public set allowResize(val: boolean) {
public set allowResize(val: boolean | undefined) {
this.setPropertyValue("allowResize", val);
}
public get renderedAllowResize(): boolean {
return this.allowResize && (this.survey && this.survey.allowResizeComment) && !this.isPreviewStyle && !this.isReadOnlyStyle;
const res = this.allowResize;
const allowResize = res === undefined && this.survey ? this.survey.allowResizeComment : !!res;
return allowResize && !this.isPreviewStyle && !this.isReadOnlyStyle;
}
public get resizeStyle() {
return this.renderedAllowResize ? "both" : "none";
Expand All @@ -81,7 +84,7 @@ export class QuestionCommentModel extends QuestionTextBase {
super.afterRenderQuestionElement(el);
}
public updateElement(): void {
if (this.element && this.autoGrow) {
if (this.element && this.renderedAutoGrow) {
setTimeout(() => increaseHeightByContent(this.element), 1);
}
}
Expand Down Expand Up @@ -138,8 +141,8 @@ Serializer.addClass(
default: "default",
choices: ["default", "onBlur", "onTyping"],
},
{ name: "autoGrow:boolean" },
{ name: "allowResize:boolean", default: true },
{ name: "autoGrow:boolean", defaultFunc: () => undefined },
{ name: "allowResize:boolean", defaultFunc: () => undefined },
{ name: "acceptCarriageReturn:boolean", default: true, visible: false }
],
function () {
Expand Down
8 changes: 5 additions & 3 deletions src/survey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3330,7 +3330,9 @@ export class SurveyModel extends SurveyElementCore
}
private updateActivePage(): void {
const newPage = this.isShowStartingPage ? this.startedPage : this.currentPage;
this.setPropertyValue("activePage", newPage);
if (newPage !== this.activePage) {
this.setPropertyValue("activePage", newPage);
}
}
private onStateAndCurrentPageChanged(): void {
this.updateActivePage();
Expand Down Expand Up @@ -6073,11 +6075,11 @@ export class SurveyModel extends SurveyElementCore
isCompleted: string,
response: any
) {
self.isLoading = false;
if (success) {
self.isCompletedBefore = isCompleted == "completed";
self.loadSurveyFromServiceJson(json);
}
self.isLoading = false;
}
);
} else {
Expand All @@ -6086,10 +6088,10 @@ export class SurveyModel extends SurveyElementCore
result: string,
response: any
) {
self.isLoading = false;
if (success) {
self.loadSurveyFromServiceJson(result);
}
self.isLoading = false;
});
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ function scrollElementByChildId(id: string) {
function navigateToUrl(url: string): void {
const location = DomWindowHelper.getLocation();
if (!url || !location) return;
location.href = url;
location.href = encodeURIComponent(url);
}

function wrapUrlForBackgroundImage(url: string): string {
Expand Down
168 changes: 153 additions & 15 deletions tests/surveytests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ import { defaultV2Css } from "../src/defaultCss/defaultV2Css";
import { StylesManager } from "../src/stylesmanager";
import { ITheme } from "../src/themes";
import { Cover } from "../src/header";
import { DomWindowHelper } from "../src/global_variables_utils";

export default QUnit.module("Survey");

Expand Down Expand Up @@ -6611,6 +6612,49 @@ QUnit.test("surveyId + clientId", function (assert) {
assert.equal(q1.name, "q1", "The survey created from the string");
});

QUnit.test("surveyId + clientId several page render", function (assert) {
let log = "";
let curState = "";
const json = { questions: [{ type: "text", name: "q1" }] };
class dxSurveyServiceTester extends dxSurveyService {
public getSurveyJsonAndIsCompleted(surveyId: string, clientId: string, onLoad: (success: boolean, surveyJson: any, result: string, response: any) => void) {
if (onLoad) {
onLoad(true, json, clientId, "");
}
}
}
class SurveyTester extends SurveyModel {
protected createSurveyService(): dxSurveyService {
return new dxSurveyServiceTester();
}
protected propertyValueChanged(name: string, oldValue: any, newValue: any, arrayChanges?: ArrayChanges, target?: Base): void {
super.propertyValueChanged(name, oldValue, newValue, arrayChanges);
if (name === "isLoading" || name === "state" || name === "activePage") {
log += ("-> " + name + ":" + newValue);
}
}
protected onLoadSurveyFromService(): void {
super.onLoadSurveyFromService();
curState = this.state;
assert.equal(curState, "loading");
}
protected setPropertyValueDirectly(name: string, val: any): void {
if (name === "activePage" && !!val) {
assert.ok(this.activePage === undefined, "this.activePage undefined");
assert.ok(!!val, "new activePage");
}
super.setPropertyValueDirectly(name, val);
}
}

let survey = new SurveyTester({ surveyId: "surveyDummyId", clientId: "completed" });
assert.equal(survey.state, "completedbefore", "The survey is running");
assert.equal(log, "-> state:empty-> state:loading-> isLoading:true-> activePage:page1-> state:completedbefore-> isLoading:false");

let q1 = survey.getQuestionByName("q1");
assert.equal(q1.name, "q1", "The survey created from the string");
});

QUnit.test(
"Question description and text processing, variable, Bug #632",
function (assert) {
Expand Down Expand Up @@ -15500,16 +15544,40 @@ QUnit.test("survey.autoGrowComment", function (assert) {
]
};
let survey = new SurveyModel(json);
let comment1 = survey.getQuestionByName("comment1");
let comment2 = survey.getQuestionByName("comment2");
let comment1 = survey.getQuestionByName("comment1") as QuestionCommentModel;
let comment2 = survey.getQuestionByName("comment2") as QuestionCommentModel;

assert.equal(survey.autoGrowComment, true);
assert.equal(comment1.autoGrow, true);
assert.equal(comment2.autoGrow, true);
assert.equal(survey.autoGrowComment, true, "#1");
assert.equal(comment1.renderedAutoGrow, true, "#2");
assert.equal(comment2.renderedAutoGrow, true, "#3");

survey.autoGrowComment = false;
assert.equal(comment1.autoGrow, false);
assert.equal(comment2.autoGrow, true);
assert.equal(comment1.renderedAutoGrow, false, "#4");
assert.equal(comment2.renderedAutoGrow, true, "#5");
});
QUnit.test("save comment autoGrow && autoResize", function (assert) {
let json = {
autoGrowComment: true,
pages: [
{
elements: [
{
type: "comment",
name: "q1",
}
]
}
]
};
let survey = new SurveyModel(json);
let question = survey.getQuestionByName("q1") as QuestionCommentModel;
assert.deepEqual(question.toJSON(), { name: "q1" }, "#1");
question.allowResize = false;
question.autoGrow = false;
assert.deepEqual(question.toJSON(), { name: "q1", allowResize: false, autoGrow: false }, "#2");
question.allowResize = true;
question.autoGrow = true;
assert.deepEqual(question.toJSON(), { name: "q1", allowResize: true, autoGrow: true }, "#3");
});
QUnit.test("survey.allowResizeComment", function (assert) {
let json = {
Expand All @@ -15531,26 +15599,79 @@ QUnit.test("survey.allowResizeComment", function (assert) {
]
};
let survey = new SurveyModel(json);
let comment1 = survey.getQuestionByName("comment1");
let comment2 = survey.getQuestionByName("comment2");
let comment1 = survey.getQuestionByName("comment1") as QuestionCommentModel;
let comment2 = survey.getQuestionByName("comment2") as QuestionCommentModel;

assert.equal(survey.allowResizeComment, false);
assert.equal(comment1.renderedAllowResize, false);
assert.equal(comment2.renderedAllowResize, false);
assert.equal(comment1.renderedAllowResize, false, "comment1 survey.allowResizeComment = false, #1");
assert.equal(comment2.renderedAllowResize, false, "comment2 survey.allowResizeComment = false, #2");

survey.allowResizeComment = true;
assert.equal(comment1.renderedAllowResize, true);
assert.equal(comment2.renderedAllowResize, false);
assert.equal(comment1.renderedAllowResize, true, "comment1 survey.allowResizeComment = true, #3");
assert.equal(comment2.renderedAllowResize, false, "comment2 survey.allowResizeComment = true, #4");

comment1.readOnly = true;
assert.equal(comment1.renderedAllowResize, false);
assert.equal(comment1.renderedAllowResize, false, "#5");
comment1.readOnly = false;

survey.showPreview();
let comment1Preview = survey.getQuestionByName("comment1");
assert.equal(comment1Preview.renderedAllowResize, false);
assert.equal(comment1Preview.renderedAllowResize, false, "#6");
});

QUnit.test("survey.allowResizeComment & survey.autoGrowComment override this properties for individual properties", function (assert) {
let json = {
allowResizeComment: false,
autoGrowComment: false,
pages: [
{
elements: [
{
type: "comment",
name: "comment1",
autoGrow: true,
allowResize: true
},
{
type: "comment",
name: "comment2",
},
{
type: "comment",
name: "comment3",
autoGrow: false
}
]
}
]
};
let survey = new SurveyModel(json);
let comment1 = survey.getQuestionByName("comment1") as QuestionCommentModel;
let comment2 = survey.getQuestionByName("comment2") as QuestionCommentModel;
let comment3 = survey.getQuestionByName("comment3") as QuestionCommentModel;

assert.equal(comment1.renderedAllowResize, true, "comment1 survey.allowResizeComment = false, #1");
assert.equal(comment1.renderedAutoGrow, true, "comment1 survey.autoGrowComment = false, #2");
assert.equal(comment2.renderedAllowResize, false, "comment2 survey.allowResizeComment = false, #3");
assert.equal(comment2.renderedAutoGrow, false, "comment2 survey.autoGrowComment = false, #4");
assert.equal(comment3.renderedAutoGrow, false, "comment2 survey.autoGrowComment = false, #5");

survey.allowResizeComment = true;
survey.autoGrowComment = true;
assert.equal(comment1.renderedAllowResize, true, "comment1 survey.allowResizeComment = true, #6");
assert.equal(comment1.renderedAutoGrow, true, "comment1 survey.autoGrowComment = true, #7");
assert.equal(comment2.renderedAllowResize, true, "comment2 survey.allowResizeComment = true, #8");
assert.equal(comment2.renderedAutoGrow, true, "comment2 survey.autoGrowComment = true, #9");
assert.equal(comment3.renderedAutoGrow, false, "comment2 survey.autoGrowComment = true, #10");
});

QUnit.test("getDefaultPropertyValue for comment properties autoGrow & allowResize", function (assert) {
let comment = new QuestionCommentModel("q1");

assert.strictEqual(comment.getDefaultPropertyValue("autoGrow"), undefined, "autoGrow");
assert.strictEqual(comment.getDefaultPropertyValue("allowResize"), undefined, "allowResize");
});

QUnit.test("utils.increaseHeightByContent", assert => {
let element = {
getBoundingClientRect: () => { return { height: 50, width: 100, x: 10, y: 10 }; },
Expand Down Expand Up @@ -20069,4 +20190,21 @@ QUnit.test("_isElementShouldBeSticky", (assert) => {

rootElement.scrollTop = 0;
assert.notOk(survey._isElementShouldBeSticky(""), "empty selector - always false (no scroll)");
});
QUnit.test("survey navigateToUrl encode url", function (assert) {
var survey = new SurveyModel({
questions: [
{
type: "text",
name: "q1",
}
],
"navigateToUrl": "javascript:alert(2)",
});

const location: Location = {} as any;
DomWindowHelper.getLocation = <any>(() => location);

survey.doComplete();
assert.equal(location.href, "javascript%3Aalert(2)", "encoded URL");
});

0 comments on commit 4d024e9

Please sign in to comment.