Skip to content

Commit

Permalink
ChoicesFrom~ - Suport new properties in the matrix columns fix #6711 (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewtelnov committed Aug 15, 2023
1 parent 72d5665 commit 26bfde0
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 15 deletions.
45 changes: 30 additions & 15 deletions src/question_baseselect.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { property, Serializer } from "./jsonobject";
import { SurveyError } from "./survey-error";
import { ISurveyImpl, ISurvey } from "./base-interfaces";
import { ISurveyImpl, ISurvey, ISurveyData } from "./base-interfaces";
import { SurveyModel } from "./survey";
import { Question } from "./question";
import { ItemValue } from "./itemvalue";
Expand Down Expand Up @@ -1019,26 +1019,31 @@ export class QuestionSelectBase extends Question {
: this.activeChoices;
}
protected get activeChoices(): Array<ItemValue> {
const question = this.findCarryForwardQuestion();
const selBaseQuestion = this.getQuestionWithChoicesCore(question);
const arrayQuestion = !selBaseQuestion ? this.getQuestionWithArrayValue(question) : null;
this.setCarryForwardQuestionType(!!selBaseQuestion, !!arrayQuestion);
const question = this.getCarryForwardQuestion();
if (this.carryForwardQuestionType === "select") {
selBaseQuestion.addDependedQuestion(this);
return this.getChoicesFromSelectQuestion(selBaseQuestion);
(<QuestionSelectBase>question).addDependedQuestion(this);
return this.getChoicesFromSelectQuestion((<QuestionSelectBase>question));
}
if (this.carryForwardQuestionType === "array") {
(<any>arrayQuestion).addDependedQuestion(this);
return this.getChoicesFromArrayQuestion(arrayQuestion);
(<any>question).addDependedQuestion(this);
return this.getChoicesFromArrayQuestion(question);
}
return this.choicesFromUrl ? this.choicesFromUrl : this.getChoices();
}
getCarryForwardQuestion(data?: ISurveyData): Question {
const question = this.findCarryForwardQuestion(data);
const selBaseQuestion = this.getQuestionWithChoicesCore(question);
const arrayQuestion = !selBaseQuestion ? this.getQuestionWithArrayValue(question) : null;
this.setCarryForwardQuestionType(!!selBaseQuestion, !!arrayQuestion);
return !!selBaseQuestion || !!arrayQuestion ? question : null;
}
private getQuestionWithChoices(): QuestionSelectBase {
return this.getQuestionWithChoicesCore(this.findCarryForwardQuestion());
}
private findCarryForwardQuestion(): Question {
if (!this.choicesFromQuestion || !this.data) return null;
return <Question>this.data.findQuestionByName(this.choicesFromQuestion);
private findCarryForwardQuestion(data?: ISurveyData): Question {
if(!data) data = this.data;
if (!this.choicesFromQuestion || !data) return null;
return <Question>data.findQuestionByName(this.choicesFromQuestion);
}
private getQuestionWithChoicesCore(question: Question): QuestionSelectBase {
if(!!question && !!question.visibleChoices && (Serializer.isDescendantOf(question.getType(), "selectbase")) && question !== this)
Expand Down Expand Up @@ -1778,6 +1783,16 @@ export class QuestionCheckboxBase extends QuestionSelectBase {
keys.push("choices");
}
}

function checkCopyPropVisibility(obj: any, mode: string): boolean {
if(!obj) return false;
if(!!obj.templateQuestion) {
const data = obj.colOwner?.data;
obj = obj.templateQuestion;
if(!obj.getCarryForwardQuestion(data)) return false;
}
return obj.carryForwardQuestionType === mode;
}
Serializer.addClass(
"selectbase",
[
Expand All @@ -1799,21 +1814,21 @@ Serializer.addClass(
choices: ["all", "selected", "unselected"],
dependsOn: "choicesFromQuestion",
visibleIf: (obj: any) => {
return obj.carryForwardQuestionType === "select";
return checkCopyPropVisibility(obj, "select");
},
},
{
name: "choiceValuesFromQuestion",
dependsOn: "choicesFromQuestion",
visibleIf: (obj: any) => {
return obj.carryForwardQuestionType === "array";
return checkCopyPropVisibility(obj, "array");
},
},
{
name: "choiceTextsFromQuestion",
dependsOn: "choicesFromQuestion",
visibleIf: (obj: any) => {
return obj.carryForwardQuestionType === "array";
return checkCopyPropVisibility(obj, "array");
},
},
{
Expand Down
45 changes: 45 additions & 0 deletions tests/surveyserializationtests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -671,3 +671,48 @@ QUnit.test(
assert.ok(property.isVisible("", page), "navigationTitle visible for buttons nav");
}
);
QUnit.test("choiceValuesFromQuestion properties visibility", function (assert) {
const survey = new SurveyModel({
questions: [
{ name: "q1", type: "dropdown", choices: [1, 2, 3] },
{ name: "q2", type: "matrixdynamic" },
{ name: "q3", type: "dropdown", choicesFromQuestion: "q1" },
{ name: "q4", type: "dropdown", choicesFromQuestion: "q2" },
{ name: "q5", type: "matrixdropdown",
columns: [
{ name: "col1", cellType: "dropdown", choicesFromQuestion: "q1" },
{ name: "col2", cellType: "dropdown", choicesFromQuestion: "q2" }
]
}
],
});
const q1 = survey.getQuestionByName("q1");
const q3 = survey.getQuestionByName("q3");
const q4 = survey.getQuestionByName("q4");
const q5 = <QuestionMatrixDropdownModelBase>survey.getQuestionByName("q5");
const col1 = q5.columns[0];
const col2 = q5.columns[1];
const propMode = Serializer.findProperty("dropdown", "choicesFromQuestionMode");
const propValues = Serializer.findProperty("dropdown", "choiceValuesFromQuestion");
const propTexts = Serializer.findProperty("dropdown", "choiceTextsFromQuestion");

assert.equal(propMode.visibleIf(q1), false, "q1.choicesFromQuestionMode");
assert.equal(propValues.visibleIf(q1), false, "q1.choiceValuesFromQuestion");
assert.equal(propTexts.visibleIf(q1), false, "q1.choiceTextsFromQuestion");

assert.equal(propMode.visibleIf(q3), true, "q3.choicesFromQuestionMode");
assert.equal(propValues.visibleIf(q3), false, "q3.choiceValuesFromQuestion");
assert.equal(propTexts.visibleIf(q3), false, "q3.choiceTextsFromQuestion");

assert.equal(propMode.visibleIf(q4), false, "q4.choicesFromQuestionMode");
assert.equal(propValues.visibleIf(q4), true, "q4.choiceValuesFromQuestion");
assert.equal(propTexts.visibleIf(q4), true, "q4.choiceTextsFromQuestion");

assert.equal(propMode.visibleIf(col1), true, "col1.choicesFromQuestionMode");
assert.equal(propValues.visibleIf(col1), false, "col1.choiceValuesFromQuestion");
assert.equal(propTexts.visibleIf(col1), false, "col1.choiceTextsFromQuestion");

assert.equal(propMode.visibleIf(col2), false, "col2.choicesFromQuestionMode");
assert.equal(propValues.visibleIf(col2), true, "col2.choiceValuesFromQuestion");
assert.equal(propTexts.visibleIf(col2), true, "col2.choiceTextsFromQuestion");
});

0 comments on commit 26bfde0

Please sign in to comment.