diff --git a/packages/survey-vue3-ui/src/Page.vue b/packages/survey-vue3-ui/src/Page.vue
index 7f41e24520..013994de9b 100644
--- a/packages/survey-vue3-ui/src/Page.vue
+++ b/packages/survey-vue3-ui/src/Page.vue
@@ -5,7 +5,7 @@
-
+
-
+
if (this.isLoadingFromJson) return;
this.setArrayPropertyDirectly("rows", this.buildRows());
}
- protected onAddElement(element: IElement, index: number) {
+
+ private locCountRowUpdates = 0;
+ private blockRowsUpdates() {
+ this.locCountRowUpdates++;
+ }
+ private releaseRowsUpdates() {
+ this.locCountRowUpdates--;
+ }
+ private updateRowsBeforeElementRemoved(element: IElement): void {
+ const elementRow = this.findRowByElement(element);
+ const elementRowIndex = this.rows.indexOf(elementRow);
+ const elementIndexInRow = elementRow.elements.indexOf(element);
+ elementRow.elements.splice(elementIndexInRow, 1);
+ if(elementRow.elements.length == 0) {
+ this.rows.splice(elementRowIndex, 1);
+ } else if(!elementRow.elements[0].startWithNewLine && this.rows[elementRowIndex - 1]) {
+ elementRow.elements.forEach(el => this.rows[elementRowIndex - 1].addElement(el));
+ this.rows.splice(elementRowIndex, 1);
+ }
+ else {
+ elementRow.updateVisible();
+ }
+ }
+ private updateRowsOnElementAdded(element: IElement): void {
+ const index = this.elements.indexOf(element);
+ const targetElement = this.elements[index + 1];
+ const createRowAtIndex = (index: number) => {
+ const row = this.createRowAndSetLazy(index);
+ if(this.isDesignModeV2) {
+ row.setIsLazyRendering(false);
+ }
+ this.rows.splice(index, 0, row);
+ return row;
+ };
+ const updateRow = (row: QuestionRowModel, start: number, deleteCount: number, ...elements: IElement[]) => {
+ const removedElements = row.elements.splice(start, deleteCount, ...elements);
+ row.updateVisible();
+ return removedElements;
+ };
+ if(!targetElement) {
+ if(index == 0 || element.startWithNewLine) {
+ updateRow(createRowAtIndex(this.rows.length), 0, 0, element);
+ } else {
+ this.rows[this.rows.length - 1].addElement(element);
+ }
+ return;
+ }
+ const targetRow = this.findRowByElement(targetElement);
+ if(!targetRow) return;
+ const targetRowIndex = this.rows.indexOf(targetRow);
+ const targetElementIndexInRow = targetRow.elements.indexOf(targetElement);
+ if(targetElementIndexInRow == 0) {
+ if(!targetElement.startWithNewLine) {
+ updateRow(targetRow, 0, 0, element);
+ }
+ else if(element.startWithNewLine || targetRowIndex < 1) {
+ createRowAtIndex(targetRowIndex).addElement(element);
+ } else {
+ this.rows[targetRowIndex - 1].addElement(element);
+ }
+ } else {
+ if(element.startWithNewLine) {
+ updateRow(createRowAtIndex(targetRowIndex + 1), 0, 0, ...[element].concat(updateRow(targetRow, targetElementIndexInRow, targetRow.elements.length)));
+ } else {
+ updateRow(targetRow, targetElementIndexInRow, 0, element);
+ }
+ }
+ }
+ protected onAddElement(element: IElement, index: number): void {
element.setSurveyImpl(this.surveyImpl);
element.parent = this;
this.markQuestionListDirty();
if (this.canBuildRows()) {
- let dragDropInfo = settings.supportCreatorV2 ? this.getDragDropInfo() : undefined;
- this.dragDropPanelHelper.updateRowsOnElementAdded(element, index, dragDropInfo, this);
+ this.updateRowsOnElementAdded(element);
}
if (element.isPanel) {
var p = element;
@@ -1106,7 +1173,9 @@ export class PanelModelBase extends SurveyElement
}
}
private onElementStartWithNewLineChanged(element: any) {
- this.onRowsChanged();
+ if(this.locCountRowUpdates > 0) return;
+ this.updateRowsBeforeElementRemoved(element);
+ this.updateRowsOnElementAdded(element);
}
private updateRowsVisibility(element: any) {
var rows = this.rows;
@@ -1169,7 +1238,9 @@ export class PanelModelBase extends SurveyElement
if (elIndex < 0) return;
row.elements.splice(elIndex, 1);
if (row.elements.length > 0) {
+ this.blockRowsUpdates();
row.elements[0].startWithNewLine = true;
+ this.releaseRowsUpdates();
row.updateVisible();
} else {
if (row.index >= 0) {
@@ -1364,7 +1435,39 @@ export class PanelModelBase extends SurveyElement
}
return true;
}
-
+ public insertElement(element: IElement, dest?: IElement, location: "bottom" | "top" | "left" | "right" = "bottom"): void {
+ if(!dest) {
+ this.addElement(element);
+ return;
+ }
+ this.blockRowsUpdates();
+ let index = this.elements.indexOf(dest);
+ const destRow = this.findRowByElement(dest);
+ if(location == "left" || location == "right") {
+ if(location == "right") {
+ element.startWithNewLine = false;
+ index++;
+ }
+ else {
+ if(destRow.elements.indexOf(dest) == 0) {
+ dest.startWithNewLine = false;
+ element.startWithNewLine = true;
+ } else {
+ element.startWithNewLine = false;
+ }
+ }
+ }
+ else {
+ element.startWithNewLine = true;
+ if(location == "top") {
+ index = this.elements.indexOf(destRow.elements[0]);
+ } else {
+ index = this.elements.indexOf(destRow.elements[destRow.elements.length - 1]) + 1;
+ }
+ }
+ this.releaseRowsUpdates();
+ this.addElement(element, index);
+ }
public insertElementAfter(element: IElement, after: IElement) {
const index = this.elements.indexOf(after);
if (index >= 0) this.addElement(element, index + 1);
@@ -1410,7 +1513,7 @@ export class PanelModelBase extends SurveyElement
*
* This method returns `null` if the panel cannot be created or added to this panel/page; otherwise, the method returns the created panel.
* @param name A panel name.
- * @see elements
+ * @see elementsup
* @see addElement
*/
public addNewPanel(name: string = null): PanelModel {
diff --git a/src/react/panel-base.tsx b/src/react/panel-base.tsx
index 22a3e75fbf..3f8f9ddb08 100644
--- a/src/react/panel-base.tsx
+++ b/src/react/panel-base.tsx
@@ -77,22 +77,8 @@ export class SurveyPanelBase extends SurveyElementBase {
&& this.panelBase.isVisible && !!this.panelBase.survey
);
}
- private renderedRowsCache: any = {};
protected renderRows(css: any): Array {
- if (this.changedStatePropName !== "rows") {
- this.renderedRowsCache = {};
- }
- var rows:Array = [];
- var questionRows = this.panelBase.rows;
- for (var i = 0; i < questionRows.length; i++) {
- var row = this.renderedRowsCache[questionRows[i].id];
- if (!row) {
- row = this.createRow(questionRows[i], css);
- this.renderedRowsCache[questionRows[i].id] = row;
- }
- rows.push(row);
- }
- return rows;
+ return this.panelBase.rows.map((row) => this.createRow(row, css));
}
protected createRow(row: QuestionRowModel, css: any): JSX.Element {
return (
diff --git a/tests/paneltests.ts b/tests/paneltests.ts
index c5da3b5526..cbeb86bcb2 100644
--- a/tests/paneltests.ts
+++ b/tests/paneltests.ts
@@ -12,6 +12,7 @@ import { AdaptiveActionContainer } from "../src/actions/adaptive-container";
import { ActionContainer } from "../src/actions/container";
import { IElement } from "../src/base-interfaces";
import { StylesManager } from "../src/stylesmanager";
+import { assert } from "console";
export default QUnit.module("Panel");
@@ -1973,4 +1974,741 @@ QUnit.test("Render name for collapsed/expanded questions in design-time", functi
assert.equal(panel.state, "default", "the panel is not collapsed or expanded");
assert.equal(panel.hasTitle, false, "We do not render the title");
assert.notOk(panel.locTitle.renderedHtml, "Render title is empty, #3");
+});
+
+QUnit.test("Check updateRowsOnElementAdded: insert on empty page", function(assert) {
+ const survey = new SurveyModel();
+ survey.fromJSON({
+ pages: [
+ {
+ name: "page1"
+ }
+ ]
+ });
+ const page = survey.getPageByName("page1");
+ const question = new QuestionTextModel("q1");
+ page.addElement(question, 0);
+ assert.equal(page.rows.length, 1);
+ assert.equal(page.rows[0].visibleElements.length, 1);
+ assert.equal(page.rows[0].visibleElements[0].name, "q1");
+
+});
+QUnit.test("Check updateRowsOnElementAdded: insert into page with latest index and swnl: false", function(assert) {
+ const survey = new SurveyModel();
+ survey.fromJSON({
+ pages: [
+ {
+ name: "page1",
+ elements: [
+ {
+ type: "text",
+ name: "q1"
+ }
+ ]
+ }
+ ]
+ });
+ const page = survey.getPageByName("page1");
+ const question = new QuestionTextModel("q2");
+ question.startWithNewLine = false;
+ page.addElement(question, 1);
+ assert.equal(page.rows.length, 1);
+ assert.equal(page.rows[0].visibleElements.length, 2);
+ assert.equal(page.rows[0].visibleElements[0].name, "q1");
+ assert.equal(page.rows[0].visibleElements[1].name, "q2");
+});
+QUnit.test("Check updateRowsOnElementAdded: insert into page with latest index and swnl: true", function(assert) {
+ const survey = new SurveyModel();
+ survey.fromJSON({
+ pages: [
+ {
+ name: "page1",
+ elements: [
+ {
+ type: "text",
+ name: "q1"
+ }
+ ]
+ }
+ ]
+ });
+ const page = survey.getPageByName("page1");
+ const question = new QuestionTextModel("q2");
+ question.startWithNewLine = true;
+ page.addElement(question, 1);
+ assert.equal(page.rows.length, 2);
+ assert.equal(page.rows[0].visibleElements.length, 1);
+ assert.equal(page.rows[0].visibleElements[0].name, "q1");
+ assert.equal(page.rows[1].visibleElements.length, 1);
+ assert.equal(page.rows[1].visibleElements[0].name, "q2");
+});
+QUnit.test("Check updateRowsOnElementAdded: insert into page with zero index and swnl: false for next element", function(assert) {
+ const json = {
+ pages: [
+ {
+ name: "page1",
+ elements: [
+ {
+ name: "q1",
+ type: "text",
+ startWithNewLine: false
+ }
+ ]
+ }
+ ]
+ };
+ let survey = new SurveyModel();
+ survey.fromJSON(json);
+ let page = survey.getPageByName("page1");
+ let question = new QuestionTextModel("q2");
+ page.addElement(question, 0);
+ assert.equal(page.rows.length, 1);
+ assert.equal(page.rows[0].visibleElements.length, 2);
+ assert.equal(page.rows[0].visibleElements[0].name, "q2");
+ assert.equal(page.rows[0].visibleElements[1].name, "q1");
+
+ survey = new SurveyModel();
+ survey.fromJSON(json);
+ page = survey.getPageByName("page1");
+ question = new QuestionTextModel("q2");
+ question.startWithNewLine = false;
+ page.addElement(question, 0);
+ assert.equal(page.rows.length, 1);
+ assert.equal(page.rows[0].visibleElements.length, 2);
+ assert.equal(page.rows[0].visibleElements[0].name, "q2");
+ assert.equal(page.rows[0].visibleElements[1].name, "q1");
+});
+QUnit.test("Check updateRowsOnElementAdded: insert into page with zero index and swnl: true for next element", function(assert) {
+ const json = {
+ pages: [
+ {
+ name: "page1",
+ elements: [
+ {
+ name: "q1",
+ type: "text",
+ startWithNewLine: true
+ }
+ ]
+ }
+ ]
+ };
+ let survey = new SurveyModel();
+ survey.fromJSON(json);
+ let page = survey.getPageByName("page1");
+ let question = new QuestionTextModel("q2");
+ page.addElement(question, 0);
+ assert.equal(page.rows.length, 2);
+ assert.equal(page.rows[0].visibleElements.length, 1);
+ assert.equal(page.rows[0].visibleElements[0].name, "q2");
+ assert.equal(page.rows[1].visibleElements.length, 1);
+ assert.equal(page.rows[1].visibleElements[0].name, "q1");
+
+ survey = new SurveyModel();
+ survey.fromJSON(json);
+ page = survey.getPageByName("page1");
+ question = new QuestionTextModel("q2");
+ question.startWithNewLine = false;
+ page.addElement(question, 0);
+ assert.equal(page.rows[0].visibleElements.length, 1);
+ assert.equal(page.rows[0].visibleElements[0].name, "q2");
+ assert.equal(page.rows[1].visibleElements.length, 1);
+ assert.equal(page.rows[1].visibleElements[0].name, "q1");
+});
+QUnit.test("Check updateRowsOnElementAdded method: insert between elements in one row with swnl: false", function(assert) {
+ const survey = new SurveyModel();
+ survey.fromJSON({
+ pages: [
+ {
+ name: "page1",
+ elements: [
+ { type: "text", name: "q1" },
+ { type: "text", name: "q2" },
+ { type: "text", name: "q3", startWithNewLine: false },
+ { type: "text", name: "q4", startWithNewLine: false },
+ { type: "text", name: "q5" }
+ ]
+ }
+ ]
+ });
+ const page = survey.getPageByName("page1");
+ assert.equal(page.rows.length, 3);
+ assert.deepEqual(page.rows[0].visibleElements.map(q => q.name), ["q1"]);
+ assert.deepEqual(page.rows[1].visibleElements.map(q => q.name), ["q2", "q3", "q4"]);
+ assert.deepEqual(page.rows[2].visibleElements.map(q => q.name), ["q5"]);
+
+ let question = new QuestionTextModel("q6");
+ question.startWithNewLine = false;
+ page.addElement(question, 2);
+ assert.equal(page.rows.length, 3);
+ assert.deepEqual(page.rows[0].visibleElements.map(q => q.name), ["q1"]);
+ assert.deepEqual(page.rows[1].visibleElements.map(q => q.name), ["q2", "q6", "q3", "q4"]);
+ assert.deepEqual(page.rows[2].visibleElements.map(q => q.name), ["q5"]);
+
+ question = new QuestionTextModel("q7");
+ question.startWithNewLine = false;
+ page.addElement(question, 4);
+ assert.equal(page.rows.length, 3);
+ assert.deepEqual(page.rows[0].visibleElements.map(q => q.name), ["q1"]);
+ assert.deepEqual(page.rows[1].visibleElements.map(q => q.name), ["q2", "q6", "q3", "q7", "q4"]);
+ assert.deepEqual(page.rows[2].visibleElements.map(q => q.name), ["q5"]);
+});
+QUnit.test("Check updateRowsOnElementAdded method: insert between elements in one row with swnl: true", function(assert) {
+ const survey = new SurveyModel();
+ survey.fromJSON({
+ pages: [
+ {
+ name: "page1",
+ elements: [
+ { type: "text", name: "q1" },
+ { type: "text", name: "q2" },
+ { type: "text", name: "q3", startWithNewLine: false },
+ { type: "text", name: "q4", startWithNewLine: false },
+ { type: "text", name: "q5" }
+ ]
+ }
+ ]
+ });
+ const page = survey.getPageByName("page1");
+ assert.equal(page.rows.length, 3);
+ assert.deepEqual(page.rows[0].visibleElements.map(q => q.name), ["q1"]);
+ assert.deepEqual(page.rows[1].visibleElements.map(q => q.name), ["q2", "q3", "q4"]);
+ assert.deepEqual(page.rows[2].visibleElements.map(q => q.name), ["q5"]);
+
+ let question = new QuestionTextModel("q6");
+ question.startWithNewLine = true;
+ page.addElement(question, 2);
+ assert.equal(page.rows.length, 4);
+ assert.deepEqual(page.rows[0].visibleElements.map(q => q.name), ["q1"]);
+ assert.deepEqual(page.rows[1].visibleElements.map(q => q.name), ["q2"]);
+ assert.deepEqual(page.rows[2].visibleElements.map(q => q.name), ["q6", "q3", "q4"]);
+ assert.deepEqual(page.rows[3].visibleElements.map(q => q.name), ["q5"]);
+
+ question = new QuestionTextModel("q7");
+ question.startWithNewLine = true;
+ page.addElement(question, 4);
+ assert.equal(page.rows.length, 5);
+ assert.deepEqual(page.rows[0].visibleElements.map(q => q.name), ["q1"]);
+ assert.deepEqual(page.rows[1].visibleElements.map(q => q.name), ["q2"]);
+ assert.deepEqual(page.rows[2].visibleElements.map(q => q.name), ["q6", "q3"]);
+ assert.deepEqual(page.rows[3].visibleElements.map(q => q.name), ["q7", "q4"]);
+ assert.deepEqual(page.rows[4].visibleElements.map(q => q.name), ["q5"]);
+});
+QUnit.test("Check updateRowsOnElementAdded method: insert between rows with swnl: false", function(assert) {
+ const survey = new SurveyModel();
+ survey.fromJSON({
+ pages: [
+ {
+ name: "page1",
+ elements: [
+ { type: "text", name: "q1" },
+ { type: "text", name: "q2", startWithNewLine: false },
+ { type: "text", name: "q3", },
+ { type: "text", name: "q4", startWithNewLine: false },
+ { type: "text", name: "q5" }
+ ]
+ }
+ ]
+ });
+ const page = survey.getPageByName("page1");
+ assert.equal(page.rows.length, 3);
+ assert.deepEqual(page.rows[0].visibleElements.map(q => q.name), ["q1", "q2"]);
+ assert.deepEqual(page.rows[1].visibleElements.map(q => q.name), ["q3", "q4"]);
+ assert.deepEqual(page.rows[2].visibleElements.map(q => q.name), ["q5"]);
+
+ let question = new QuestionTextModel("q6");
+ question.startWithNewLine = false;
+ page.addElement(question, 2);
+ assert.equal(page.rows.length, 3);
+ assert.deepEqual(page.rows[0].visibleElements.map(q => q.name), ["q1", "q2", "q6"]);
+ assert.deepEqual(page.rows[1].visibleElements.map(q => q.name), ["q3", "q4"]);
+ assert.deepEqual(page.rows[2].visibleElements.map(q => q.name), ["q5"]);
+
+ question = new QuestionTextModel("q7");
+ question.startWithNewLine = false;
+ page.addElement(question, 5);
+ assert.equal(page.rows.length, 3);
+ assert.deepEqual(page.rows[0].visibleElements.map(q => q.name), ["q1", "q2", "q6"]);
+ assert.deepEqual(page.rows[1].visibleElements.map(q => q.name), ["q3", "q4", "q7"]);
+ assert.deepEqual(page.rows[2].visibleElements.map(q => q.name), ["q5"]);
+});
+QUnit.test("Check updateRowsOnElementAdded method: insert between rows with swnl: true", function(assert) {
+ const survey = new SurveyModel();
+ survey.fromJSON({
+ pages: [
+ {
+ name: "page1",
+ elements: [
+ { type: "text", name: "q1" },
+ { type: "text", name: "q2", startWithNewLine: false },
+ { type: "text", name: "q3", },
+ { type: "text", name: "q4", startWithNewLine: false },
+ { type: "text", name: "q5" }
+ ]
+ }
+ ]
+ });
+ const page = survey.getPageByName("page1");
+ assert.equal(page.rows.length, 3);
+ assert.deepEqual(page.rows[0].visibleElements.map(q => q.name), ["q1", "q2"]);
+ assert.deepEqual(page.rows[1].visibleElements.map(q => q.name), ["q3", "q4"]);
+ assert.deepEqual(page.rows[2].visibleElements.map(q => q.name), ["q5"]);
+
+ let question = new QuestionTextModel("q6");
+ question.startWithNewLine = true;
+ page.addElement(question, 2);
+ assert.equal(page.rows.length, 4);
+ assert.deepEqual(page.rows[0].visibleElements.map(q => q.name), ["q1", "q2"]);
+ assert.deepEqual(page.rows[1].visibleElements.map(q => q.name), ["q6"]);
+ assert.deepEqual(page.rows[2].visibleElements.map(q => q.name), ["q3", "q4"]);
+ assert.deepEqual(page.rows[3].visibleElements.map(q => q.name), ["q5"]);
+
+ question = new QuestionTextModel("q7");
+ question.startWithNewLine = true;
+ page.addElement(question, 5);
+ assert.equal(page.rows.length, 5);
+ assert.deepEqual(page.rows[0].visibleElements.map(q => q.name), ["q1", "q2"]);
+ assert.deepEqual(page.rows[1].visibleElements.map(q => q.name), ["q6"]);
+ assert.deepEqual(page.rows[2].visibleElements.map(q => q.name), ["q3", "q4"]);
+ assert.deepEqual(page.rows[3].visibleElements.map(q => q.name), ["q7"]);
+ assert.deepEqual(page.rows[4].visibleElements.map(q => q.name), ["q5"]);
+});
+QUnit.test("Check swnl changed: change swnl for first element on page", function(assert) {
+ const survey = new SurveyModel();
+ survey.fromJSON({
+ pages: [
+ {
+ name: "page1",
+ elements: [
+ { type: "text", name: "q1" },
+ { type: "text", name: "q2", startWithNewLine: false },
+ ]
+ }
+ ]
+ });
+ const page = survey.getPageByName("page1");
+ const question = survey.getQuestionByName("q1");
+
+ assert.equal(page.rows.length, 1);
+ assert.deepEqual(page.rows[0].visibleElements.map(q => q.name), ["q1", "q2"]);
+
+ question.startWithNewLine = false;
+ assert.equal(page.rows.length, 1);
+ assert.deepEqual(page.rows[0].visibleElements.map(q => q.name), ["q1", "q2"]);
+
+ question.startWithNewLine = true;
+ assert.equal(page.rows.length, 1);
+ assert.deepEqual(page.rows[0].visibleElements.map(q => q.name), ["q1", "q2"]);
+});
+QUnit.test("Check swnl changed: change swnl for first element in row", function(assert) {
+ const survey = new SurveyModel();
+ survey.fromJSON({
+ pages: [
+ {
+ name: "page1",
+ elements: [
+ { type: "text", name: "q1" },
+ { type: "text", name: "q2" },
+ { type: "text", name: "q3", startWithNewLine: false },
+ { type: "text", name: "q4", startWithNewLine: false },
+ { type: "text", name: "q5" },
+ ]
+ }
+ ]
+ });
+ const page = survey.getPageByName("page1");
+ const question = survey.getQuestionByName("q2");
+
+ assert.equal(page.rows.length, 3);
+ assert.deepEqual(page.rows[0].visibleElements.map(q => q.name), ["q1"]);
+ assert.deepEqual(page.rows[1].visibleElements.map(q => q.name), ["q2", "q3", "q4"]);
+ assert.deepEqual(page.rows[2].visibleElements.map(q => q.name), ["q5"]);
+
+ question.startWithNewLine = false;
+ assert.equal(page.rows.length, 2);
+ assert.deepEqual(page.rows[0].visibleElements.map(q => q.name), ["q1", "q2", "q3", "q4"]);
+ assert.deepEqual(page.rows[1].visibleElements.map(q => q.name), ["q5"]);
+});
+
+QUnit.test("Check swnl changed: change swnl for last element in row", function(assert) {
+ const survey = new SurveyModel();
+ survey.fromJSON({
+ pages: [
+ {
+ name: "page1",
+ elements: [
+ { type: "text", name: "q1" },
+ { type: "text", name: "q2" },
+ { type: "text", name: "q3", startWithNewLine: false },
+ { type: "text", name: "q4", startWithNewLine: false },
+ { type: "text", name: "q5" },
+ ]
+ }
+ ]
+ });
+ const page = survey.getPageByName("page1");
+ const question = survey.getQuestionByName("q4");
+
+ assert.equal(page.rows.length, 3);
+ assert.deepEqual(page.rows[0].visibleElements.map(q => q.name), ["q1"]);
+ assert.deepEqual(page.rows[1].visibleElements.map(q => q.name), ["q2", "q3", "q4"]);
+ assert.deepEqual(page.rows[2].visibleElements.map(q => q.name), ["q5"]);
+
+ question.startWithNewLine = true;
+ assert.equal(page.rows.length, 4);
+ assert.deepEqual(page.rows[0].visibleElements.map(q => q.name), ["q1"]);
+ assert.deepEqual(page.rows[1].visibleElements.map(q => q.name), ["q2", "q3"]);
+ assert.deepEqual(page.rows[2].visibleElements.map(q => q.name), ["q4"]);
+ assert.deepEqual(page.rows[3].visibleElements.map(q => q.name), ["q5"]);
+});
+
+QUnit.test("Check swnl changed: change swnl for middle element in row", function(assert) {
+ const survey = new SurveyModel();
+ survey.fromJSON({
+ pages: [
+ {
+ name: "page1",
+ elements: [
+ { type: "text", name: "q1" },
+ { type: "text", name: "q2" },
+ { type: "text", name: "q3", startWithNewLine: false },
+ { type: "text", name: "q4", startWithNewLine: false },
+ { type: "text", name: "q5", startWithNewLine: false },
+ { type: "text", name: "q6", startWithNewLine: false },
+ { type: "text", name: "q7" },
+ ]
+ }
+ ]
+ });
+ const page = survey.getPageByName("page1");
+ const question = survey.getQuestionByName("q4");
+
+ assert.equal(page.rows.length, 3);
+ assert.deepEqual(page.rows[0].visibleElements.map(q => q.name), ["q1"]);
+ assert.deepEqual(page.rows[1].visibleElements.map(q => q.name), ["q2", "q3", "q4", "q5", "q6"]);
+ assert.deepEqual(page.rows[2].visibleElements.map(q => q.name), ["q7"]);
+
+ question.startWithNewLine = true;
+ assert.equal(page.rows.length, 4);
+ assert.deepEqual(page.rows[0].visibleElements.map(q => q.name), ["q1"]);
+ assert.deepEqual(page.rows[1].visibleElements.map(q => q.name), ["q2", "q3"]);
+ assert.deepEqual(page.rows[2].visibleElements.map(q => q.name), ["q4", "q5", "q6"]);
+ assert.deepEqual(page.rows[3].visibleElements.map(q => q.name), ["q7"]);
+});
+
+QUnit.test("Check swnl changed: change swnl for single element in row", function(assert) {
+ const survey = new SurveyModel();
+ survey.fromJSON({
+ pages: [
+ {
+ name: "page1",
+ elements: [
+ { type: "text", name: "q1" },
+ { type: "text", name: "q2" },
+ { type: "text", name: "q3", startWithNewLine: false },
+ { type: "text", name: "q4", startWithNewLine: false },
+ { type: "text", name: "q5" },
+ { type: "text", name: "q6" },
+ ]
+ }
+ ]
+ });
+ const page = survey.getPageByName("page1");
+ const question = survey.getQuestionByName("q5");
+
+ assert.equal(page.rows.length, 4);
+ assert.deepEqual(page.rows[0].visibleElements.map(q => q.name), ["q1"]);
+ assert.deepEqual(page.rows[1].visibleElements.map(q => q.name), ["q2", "q3", "q4"]);
+ assert.deepEqual(page.rows[2].visibleElements.map(q => q.name), ["q5"]);
+ assert.deepEqual(page.rows[3].visibleElements.map(q => q.name), ["q6"]);
+
+ question.startWithNewLine = false;
+ assert.equal(page.rows.length, 3);
+ assert.deepEqual(page.rows[0].visibleElements.map(q => q.name), ["q1"]);
+ assert.deepEqual(page.rows[1].visibleElements.map(q => q.name), ["q2", "q3", "q4", "q5"]);
+ assert.deepEqual(page.rows[2].visibleElements.map(q => q.name), ["q6"]);
+});
+
+QUnit.test("Check insert function: insert in empty page", (assert) => {
+ const survey = new SurveyModel();
+ survey.fromJSON({
+ pages: [
+ {
+ name: "page1",
+ elements: [
+ ]
+ }
+ ]
+ });
+ const page = survey.getPageByName("page1");
+ assert.equal(page.rows.length, 0);
+ page.insertElement(new QuestionTextModel("q1"));
+ assert.equal(page.rows.length, 1);
+ assert.deepEqual(page.rows[0].visibleElements.map(q => q.name), ["q1"]);
+});
+
+QUnit.test("Check insert function: insert before first element in row", (assert) => {
+ const survey = new SurveyModel();
+ survey.fromJSON({
+ pages: [
+ {
+ name: "page1",
+ elements: [
+ {
+ type: "text",
+ name: "q1",
+ },
+ {
+ type: "text",
+ name: "q2",
+ startWithNewLine: false
+ },
+ {
+ type: "text",
+ name: "q3",
+ },
+ {
+ type: "text",
+ name: "q4",
+ startWithNewLine: false
+ },
+ {
+ type: "text",
+ name: "q5",
+ },
+ {
+ type: "text",
+ name: "q6",
+ startWithNewLine: false
+ },
+ ]
+ }
+ ]
+ });
+ const page = survey.getPageByName("page1");
+ let calledBuildRows = 0;
+ page["onRowsChanged"] = () => {
+ calledBuildRows++;
+ };
+ assert.equal(page.rows.length, 3);
+ assert.deepEqual(page.rows[0].visibleElements.map(q => q.name), ["q1", "q2"]);
+ assert.deepEqual(page.rows[1].visibleElements.map(q => q.name), ["q3", "q4"]);
+ assert.deepEqual(page.rows[2].visibleElements.map(q => q.name), ["q5", "q6"]);
+
+ page.insertElement(new QuestionTextModel("q7"), survey.getQuestionByName("q3"), "left");
+ assert.equal(page.rows.length, 3);
+ assert.deepEqual(page.rows[0].visibleElements.map(q => q.name), ["q1", "q2"]);
+ assert.deepEqual(page.rows[1].visibleElements.map(q => q.name), ["q7", "q3", "q4"]);
+ assert.deepEqual(page.rows[2].visibleElements.map(q => q.name), ["q5", "q6"]);
+
+ assert.equal(calledBuildRows, 0);
+});
+QUnit.test("Check insert function: insert after last element in row", (assert) => {
+ const survey = new SurveyModel();
+ survey.fromJSON({
+ pages: [
+ {
+ name: "page1",
+ elements: [
+ {
+ type: "text",
+ name: "q1",
+ },
+ {
+ type: "text",
+ name: "q2",
+ startWithNewLine: false
+ },
+ {
+ type: "text",
+ name: "q3",
+ },
+ {
+ type: "text",
+ name: "q4",
+ startWithNewLine: false
+ },
+ {
+ type: "text",
+ name: "q5",
+ },
+ {
+ type: "text",
+ name: "q6",
+ startWithNewLine: false
+ },
+ ]
+ }
+ ]
+ });
+ const page = survey.getPageByName("page1");
+ let calledBuildRows = 0;
+ page["onRowsChanged"] = () => {
+ calledBuildRows++;
+ };
+ assert.equal(page.rows.length, 3);
+ assert.deepEqual(page.rows[0].visibleElements.map(q => q.name), ["q1", "q2"]);
+ assert.deepEqual(page.rows[1].visibleElements.map(q => q.name), ["q3", "q4"]);
+ assert.deepEqual(page.rows[2].visibleElements.map(q => q.name), ["q5", "q6"]);
+
+ page.insertElement(new QuestionTextModel("q7"), survey.getQuestionByName("q4"), "right");
+ assert.equal(page.rows.length, 3);
+ assert.deepEqual(page.rows[0].visibleElements.map(q => q.name), ["q1", "q2"]);
+ assert.deepEqual(page.rows[1].visibleElements.map(q => q.name), ["q3", "q4", "q7"]);
+ assert.deepEqual(page.rows[2].visibleElements.map(q => q.name), ["q5", "q6"]);
+
+ assert.equal(calledBuildRows, 0);
+});
+
+QUnit.test("Check insert function: insert between elements in row", (assert) => {
+ const survey = new SurveyModel();
+ survey.fromJSON({
+ pages: [
+ {
+ name: "page1",
+ elements: [
+ {
+ type: "text",
+ name: "q1",
+ },
+ {
+ type: "text",
+ name: "q2",
+ startWithNewLine: false
+ },
+ {
+ type: "text",
+ name: "q3",
+ },
+ {
+ type: "text",
+ name: "q4",
+ startWithNewLine: false
+ },
+ {
+ type: "text",
+ name: "q5",
+ },
+ {
+ type: "text",
+ name: "q6",
+ startWithNewLine: false
+ },
+ ]
+ }
+ ]
+ });
+ const page = survey.getPageByName("page1");
+
+ let calledBuildRows = 0;
+ page["onRowsChanged"] = () => {
+ calledBuildRows++;
+ };
+
+ assert.equal(page.rows.length, 3);
+ assert.deepEqual(page.rows[0].visibleElements.map(q => q.name), ["q1", "q2"]);
+ assert.deepEqual(page.rows[1].visibleElements.map(q => q.name), ["q3", "q4"]);
+ assert.deepEqual(page.rows[2].visibleElements.map(q => q.name), ["q5", "q6"]);
+
+ page.insertElement(new QuestionTextModel("q7"), survey.getQuestionByName("q3"), "right");
+ assert.equal(page.rows.length, 3);
+ assert.deepEqual(page.rows[0].visibleElements.map(q => q.name), ["q1", "q2"]);
+ assert.deepEqual(page.rows[1].visibleElements.map(q => q.name), ["q3", "q7", "q4"]);
+ assert.deepEqual(page.rows[2].visibleElements.map(q => q.name), ["q5", "q6"]);
+
+ page.insertElement(new QuestionTextModel("q8"), survey.getQuestionByName("q7"), "left");
+ assert.equal(page.rows.length, 3);
+ assert.deepEqual(page.rows[0].visibleElements.map(q => q.name), ["q1", "q2"]);
+ assert.deepEqual(page.rows[1].visibleElements.map(q => q.name), ["q3", "q8", "q7", "q4"]);
+ assert.deepEqual(page.rows[2].visibleElements.map(q => q.name), ["q5", "q6"]);
+
+ assert.equal(calledBuildRows, 0);
+
+});
+
+QUnit.test("Check insert function: insert between rows", (assert) => {
+ const survey = new SurveyModel();
+ survey.fromJSON({
+ pages: [
+ {
+ name: "page1",
+ elements: [
+ {
+ type: "text",
+ name: "q1",
+ },
+ {
+ type: "text",
+ name: "q2",
+ startWithNewLine: false
+ },
+ {
+ type: "text",
+ name: "q3",
+ },
+ {
+ type: "text",
+ name: "q4",
+ startWithNewLine: false
+ },
+ {
+ type: "text",
+ name: "q5",
+ },
+ {
+ type: "text",
+ name: "q6",
+ startWithNewLine: false
+ },
+ ]
+ }
+ ]
+ });
+ const page = survey.getPageByName("page1");
+
+ let calledBuildRows = 0;
+ page["onRowsChanged"] = () => {
+ calledBuildRows++;
+ };
+
+ assert.equal(page.rows.length, 3);
+ assert.deepEqual(page.rows[0].visibleElements.map(q => q.name), ["q1", "q2"]);
+ assert.deepEqual(page.rows[1].visibleElements.map(q => q.name), ["q3", "q4"]);
+ assert.deepEqual(page.rows[2].visibleElements.map(q => q.name), ["q5", "q6"]);
+
+ page.insertElement(new QuestionTextModel("q7"), survey.getQuestionByName("q4"), "bottom");
+ assert.equal(page.rows.length, 4);
+ assert.deepEqual(page.rows[0].visibleElements.map(q => q.name), ["q1", "q2"]);
+ assert.deepEqual(page.rows[1].visibleElements.map(q => q.name), ["q3", "q4"]);
+ assert.deepEqual(page.rows[2].visibleElements.map(q => q.name), ["q7"]);
+ assert.deepEqual(page.rows[3].visibleElements.map(q => q.name), ["q5", "q6"]);
+
+ page.insertElement(new QuestionTextModel("q8"), survey.getQuestionByName("q3"), "top");
+ assert.equal(page.rows.length, 5);
+ assert.deepEqual(page.rows[0].visibleElements.map(q => q.name), ["q1", "q2"]);
+ assert.deepEqual(page.rows[1].visibleElements.map(q => q.name), ["q8"]);
+ assert.deepEqual(page.rows[2].visibleElements.map(q => q.name), ["q3", "q4"]);
+ assert.deepEqual(page.rows[3].visibleElements.map(q => q.name), ["q7"]);
+ assert.deepEqual(page.rows[4].visibleElements.map(q => q.name), ["q5", "q6"]);
+
+ page.insertElement(new QuestionTextModel("q9"), survey.getQuestionByName("q4"), "top");
+ assert.equal(page.rows.length, 6);
+ assert.deepEqual(page.rows[0].visibleElements.map(q => q.name), ["q1", "q2"]);
+ assert.deepEqual(page.rows[1].visibleElements.map(q => q.name), ["q8"]);
+ assert.deepEqual(page.rows[2].visibleElements.map(q => q.name), ["q9"]);
+ assert.deepEqual(page.rows[3].visibleElements.map(q => q.name), ["q3", "q4"]);
+ assert.deepEqual(page.rows[4].visibleElements.map(q => q.name), ["q7"]);
+ assert.deepEqual(page.rows[5].visibleElements.map(q => q.name), ["q5", "q6"]);
+
+ page.insertElement(new QuestionTextModel("q10"), survey.getQuestionByName("q3"), "bottom");
+ assert.equal(page.rows.length, 7);
+ assert.deepEqual(page.rows[0].visibleElements.map(q => q.name), ["q1", "q2"]);
+ assert.deepEqual(page.rows[1].visibleElements.map(q => q.name), ["q8"]);
+ assert.deepEqual(page.rows[2].visibleElements.map(q => q.name), ["q9"]);
+ assert.deepEqual(page.rows[3].visibleElements.map(q => q.name), ["q3", "q4"]);
+ assert.deepEqual(page.rows[4].visibleElements.map(q => q.name), ["q10"]);
+ assert.deepEqual(page.rows[5].visibleElements.map(q => q.name), ["q7"]);
+ assert.deepEqual(page.rows[6].visibleElements.map(q => q.name), ["q5", "q6"]);
+
+ assert.equal(calledBuildRows, 0);
});
\ No newline at end of file