Skip to content

Commit

Permalink
Allow to change the default operator in the Condition Editor fix #3895
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewtelnov committed Mar 17, 2023
1 parent 0c588d0 commit 791ee79
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 6 deletions.
1 change: 1 addition & 0 deletions packages/survey-creator-core/src/creator-settings.ts
Expand Up @@ -51,6 +51,7 @@ export var settings = {
showAddQuestionButton: true
},
logic: {
defaultOperator: "equal",
visibleActions: [],
logicItemTitleMaxChars: 50,
openBracket: "{",
Expand Down
Expand Up @@ -10,7 +10,7 @@ import { getLogicString } from "../components/tabs/logic-types";
export class ConditionEditorItem {
public conjunction: string = "and";
public questionName: string;
public operator: string = "equal";
public operator: string = settings.logic.defaultOperator;
public value: any;
}
export class SurveyConditionEditorItem extends ConditionEditorItem {
Expand Down Expand Up @@ -521,7 +521,7 @@ export class ConditionEditor extends PropertyEditorSetupValue {
questionOperator.choices = this.getOperators();
questionOperator.value = item.operator;
questionOperator.onOpened.add((_, opt) => {
const json = this.getQuestionConditionJson(panel.getQuestionByName("questionName").value, "equal");
const json = this.getQuestionConditionJson(panel.getQuestionByName("questionName").value, this.defaultOperator);
const qType = !!json ? json.type : null;

opt.choices.forEach((choice, index) => {
Expand Down Expand Up @@ -815,7 +815,7 @@ export class ConditionEditor extends PropertyEditorSetupValue {
private updateOperatorEnables(panel: PanelModel) {
const questionName = panel.getQuestionByName("questionName");
if (!questionName) return;
const json = this.getQuestionConditionJson(questionName.value, "equal");
const json = this.getQuestionConditionJson(questionName.value, this.defaultOperator);
const qType = !!json ? json.type : null;
const questionOperator = <QuestionDropdownModel>panel.getQuestionByName("operator");
if (!questionOperator) return;
Expand Down Expand Up @@ -857,13 +857,14 @@ export class ConditionEditor extends PropertyEditorSetupValue {
valueQuestion.width = isValueSameLine ? "35%" : "";
}
}
private get defaultOperator(): string { return settings.logic.defaultOperator; }
private getFirstEnabledOperator(choices: Array<ItemValue>): string {
for (let i = 0; i < choices.length; i++) {
if (choices[i].isEnabled) {
return choices[i].value;
}
}
return "equal";
return this.defaultOperator;
}
private onPanelAdded() {
this.setItemToPanel(
Expand All @@ -876,7 +877,7 @@ export class ConditionEditor extends PropertyEditorSetupValue {
this.context = this.getContextFromPanels();
this.rebuildQuestionValue(panel);
if (!this.isSettingPanelValues) {
panel.getQuestionByName("operator").value = "equal";
panel.getQuestionByName("operator").value = this.defaultOperator;
}
}
if (name == "operator") {
Expand Down
Expand Up @@ -1654,4 +1654,18 @@ test("Check errors for logic popup", () => {
expect(editor.apply()).toBe(true);
expect(panel.getQuestionByName("questionName").errors.length > 0).toBe(false);
expect(notifierLog).toBe("->called");
});
});
test("Change the default operator", () => {
settings.logic.defaultOperator = "anyof";
var survey = new SurveyModel({
questions: [
{ type: "checkbox", name: "q1", choices: [1, 2, 3] },
{ type: "dropdown", name: "q2", choices: [1, 2, 3] }
]
});
var editor = new ConditionEditor(survey, survey.getQuestionByName("q1"));
expect(editor.panel.panels).toHaveLength(1);
var panel = editor.panel.panels[0];
expect(panel.getQuestionByName("operator").value).toEqual("anyof");
settings.logic.defaultOperator = "equal";
});

0 comments on commit 791ee79

Please sign in to comment.