Skip to content

Commit

Permalink
Add properties into selectbase #7754
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewtelnov committed Jan 29, 2024
1 parent e266677 commit b7175c5
Show file tree
Hide file tree
Showing 10 changed files with 114 additions and 48 deletions.
2 changes: 2 additions & 0 deletions src/localization/english.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ export var englishStrings = {
startSurveyText: "Start",
otherItemText: "Other (describe)",
noneItemText: "None",
refuseItemText: "Refuse to answer",
donotKnowItemText: "Do not know",
selectAllItemText: "Select All",
progressText: "Page {0} of {1}",
indexText: "{0} of {1}",
Expand Down
84 changes: 73 additions & 11 deletions src/question_baseselect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ export class QuestionSelectBase extends Question {
private cachedValueForUrlRequests: any;
private isChoicesLoaded: boolean;
private enableOnLoadingChoices: boolean;
private noneItemValue: ItemValue = new ItemValue(settings.noneItemValue);
private noneItemValue: ItemValue;
private refuseItemValue: ItemValue;
private donotKnowItemValue: ItemValue;
private newItemValue: ItemValue;
private canShowOptionItemCallback: (item: ItemValue) => boolean;
private waitingGetChoiceDisplayValueResponse: boolean;
Expand All @@ -42,10 +44,9 @@ export class QuestionSelectBase extends Question {

constructor(name: string) {
super(name);
var noneItemText = this.createLocalizableString("noneText", this.noneItemValue, true, "noneItemText");
this.noneItemValue.locOwner = this;
this.noneItemValue.setLocText(noneItemText);

this.noneItemValue = this.createDefaultItem(settings.noneItemValue, "noneText", "noneItemText");
this.refuseItemValue = this.createDefaultItem(settings.refuseItemValue, "refuseText", "refuseItemText");
this.donotKnowItemValue = this.createDefaultItem(settings.donotKnowItemValue, "donotKnowText", "donotKnowItemText");
this.createItemValues("choices");
this.registerPropertyChangedHandlers(["choices"], () => {
if (!this.filterItems()) {
Expand Down Expand Up @@ -183,7 +184,7 @@ export class QuestionSelectBase extends Question {
return this.hasOther && this.getHasOther(this.renderedValue);
}
public get isNoneSelected(): boolean {
return this.hasNone && this.getIsItemValue(this.renderedValue, this.noneItem);
return this.showNoneItem && this.getIsItemValue(this.renderedValue, this.noneItem);
}
/**
* Specifies whether to display the "None" choice item.
Expand Down Expand Up @@ -225,6 +226,49 @@ export class QuestionSelectBase extends Question {
get locNoneText(): LocalizableString {
return this.getLocalizableString("noneText");
}
public get showRefuseItem(): boolean {
return this.getPropertyValue("showRefuseItem");
}
public set showRefuseItem(val: boolean) {
this.setPropertyValue("showRefuseItem", val);
}
public get refuseItem(): ItemValue {
return this.refuseItemValue;
}
public get refuseText(): string {
return this.getLocalizableStringText("refuseText");
}
public set refuseText(val: string) {
this.setLocalizableStringText("refuseText", val);
}
get locRefuseText(): LocalizableString {
return this.getLocalizableString("refuseText");
}
public get showDonotKnowItem(): boolean {
return this.getPropertyValue("showDonotKnowItem");
}
public set showDonotKnowItem(val: boolean) {
this.setPropertyValue("showDonotKnowItem", val);
}
public get donotKnowItem(): ItemValue {
return this.donotKnowItemValue;
}
public get donotKnowText(): string {
return this.getLocalizableStringText("donotKnowText");
}
public set donotKnowText(val: string) {
this.setLocalizableStringText("donotKnowText", val);
}
get locDonotKnowText(): LocalizableString {
return this.getLocalizableString("donotKnowText");
}
private createDefaultItem(defaultValue: any, name: string, locName: string): ItemValue {
const item = new ItemValue(defaultValue);
const locStr = this.createLocalizableString(name, item, true, locName);
item.locOwner = this;
item.setLocText(locStr);
return item;
}
/**
* A Boolean expression that is evaluated against each choice item. If the expression evaluates to `false`, the choice item becomes hidden.
*
Expand Down Expand Up @@ -636,7 +680,7 @@ export class QuestionSelectBase extends Question {
): boolean {
if (!checkEmptyValue && this.isValueEmpty(val)) return false;
if (includeOther && val == this.otherItem.value) return false;
if (this.hasNone && val == this.noneItem.value) return false;
if (this.showNoneItem && val == this.noneItem.value) return false;
var choices = isFilteredChoices
? this.getFilteredChoices()
: this.activeChoices;
Expand Down Expand Up @@ -904,7 +948,7 @@ export class QuestionSelectBase extends Question {
protected canUseFilteredChoices(): boolean {
return (
!this.isAddDefaultItems &&
!this.hasNone &&
!this.showNoneItem &&
!this.hasOther &&
this.choicesOrder == "none"
);
Expand Down Expand Up @@ -953,7 +997,7 @@ export class QuestionSelectBase extends Question {
}
protected addNonChoicesItems(dict: Array<{ index: number, item: ItemValue }>, isAddAll: boolean): void {
if (
this.supportNone() && this.canShowOptionItem(this.noneItem, isAddAll, this.hasNone)
this.supportNone() && this.canShowOptionItem(this.noneItem, isAddAll, this.showNoneItem)
) {
this.addNonChoiceItem(dict, this.noneItem, settings.specialChoicesOrder.noneItem);
}
Expand All @@ -976,7 +1020,7 @@ export class QuestionSelectBase extends Question {
}
public isItemInList(item: ItemValue): boolean {
if (item === this.otherItem) return this.hasOther;
if (item === this.noneItem) return this.hasNone;
if (item === this.noneItem) return this.showNoneItem;
if (item === this.newItemValue) return false;
return true;
}
Expand Down Expand Up @@ -1920,6 +1964,8 @@ Serializer.addClass(
{ name: "separateSpecialChoices:boolean", visible: false },
{ name: "showOtherItem:boolean", alternativeName: "hasOther" },
{ name: "showNoneItem:boolean", alternativeName: "hasNone" },
{ name: "showRefuseItem:boolean", visible: false },
{ name: "showDonotKnowItem:boolean", visible: false },
{
name: "otherPlaceholder",
alternativeName: "otherPlaceHolder",
Expand All @@ -1934,7 +1980,23 @@ Serializer.addClass(
serializationProperty: "locNoneText",
dependsOn: "showNoneItem",
visibleIf: function (obj: any) {
return obj.hasNone;
return obj.showNoneItem;
},
},
{
name: "refuseText",
serializationProperty: "locRefuseText",
dependsOn: "showRefuseItem",
visibleIf: function (obj: any) {
return obj.showRefuseItem;
},
},
{
name: "donotKnowText",
serializationProperty: "locDonotKnowText",
dependsOn: "showDonotKnowItem",
visibleIf: function (obj: any) {
return obj.showDonotKnowItem;
},
},
{
Expand Down
2 changes: 1 addition & 1 deletion src/question_checkbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ export class QuestionCheckboxModel extends QuestionCheckboxBase {
if (!newValue) newValue = [];
if (!value) value = [];
if (this.isTwoValueEquals(value, newValue)) return;
if (this.hasNone) {
if (this.showNoneItem) {
var prevNoneIndex = this.noneIndexInArray(value);
var newNoneIndex = this.noneIndexInArray(newValue);
if (prevNoneIndex > -1) {
Expand Down
2 changes: 2 additions & 0 deletions src/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,8 @@ export var settings = {
* Default value: `"none"`
*/
noneItemValue: "none",
refuseItemValue: "refuse",
donotKnowItemValue: "donotknow",
/**
* An object whose properties specify the order of the special choice items (None, Other, Select All) in select-based questions.
*
Expand Down
4 changes: 2 additions & 2 deletions tests/paneltests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1178,7 +1178,7 @@ QUnit.test(
"name": "cars",
"title": "Dropdown",

"hasNone": true,
"showNoneItem": true,
"colCount": 4,
"choices": [
"Ford",
Expand All @@ -1198,7 +1198,7 @@ QUnit.test(
"title": "Checkbox",

"hasSelectAll": true,
"hasNone": true,
"showNoneItem": true,
"colCount": 4,
"choices": [
"Ford",
Expand Down
2 changes: 1 addition & 1 deletion tests/questionDropdownTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ QUnit.test("readOnlyText default", assert => {
"name": "q1",
"placeholder": "click",
"hasOther": true,
"hasNone": true,
"showNoneItem": true,
"choices": [{ value: 1, text: "item 1" }, { value: 2, text: "item 2" }, { value: 3, text: "item 3" }]
}]
};
Expand Down
18 changes: 9 additions & 9 deletions tests/question_baseselecttests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ QUnit.test("Check QuestionSelectBase and separateSpecialChoices option", functio
choices: ["Item1", "Item2"],
hasOther: true,
hasSelectAll: true,
hasNone: true,
showNoneItem: true,
colCount: 2
},
],
Expand Down Expand Up @@ -318,7 +318,7 @@ QUnit.test("check onShowingChoiceItem event", (assert) => {
type: "radiogroup",
name: "q1",
choices: [{ value: "Item1", visibleIf: "1 = 2" }, "Item2", "Item3"],
hasNone: true,
showNoneItem: true,
hasOther: true
}]
});
Expand Down Expand Up @@ -492,7 +492,7 @@ QUnit.test("checkbox vs valuePropertyName, check selectAll and none", (assert) =
name: "q1",
choices: ["apple", "banana", "orange"],
valuePropertyName: "fruit",
hasNone: true,
showNoneItem: true,
hasSelectAll: true
}
]
Expand Down Expand Up @@ -630,15 +630,15 @@ QUnit.test("checkbox and radio css", (assert) => {
type: "radiogroup",
name: "q1",
choices: ["Item 1"],
hasNone: true
showNoneItem: true
},
{
type: "checkbox",
name: "q2",
choices: ["Item 1"],
showClearButton: true,
hasSelectAll: true,
hasNone: true
showNoneItem: true
}]
});
let question1 = <QuestionRadiogroupModel>survey.getAllQuestions()[0];
Expand Down Expand Up @@ -787,7 +787,7 @@ QUnit.test("check locOwner for items", (assert) => {

QUnit.test("check renamed has... properties", (assert) => {
const question = new QuestionCheckboxModel("q1");
assert.notOk(question.hasNone);
assert.notOk(question.showNoneItem);
assert.notOk(question.hasSelectAll);
assert.notOk(question.hasOther);
assert.notOk(question.hasComment);
Expand All @@ -799,10 +799,10 @@ QUnit.test("check renamed has... properties", (assert) => {

question.showNoneItem = true;
assert.ok(question.showNoneItem);
assert.ok(question.hasNone);
question.hasNone = false;
assert.ok(question.showNoneItem);
question.showNoneItem = false;
assert.notOk(question.showNoneItem);
assert.notOk(question.showNoneItem);
assert.notOk(question.hasNone);

question.showSelectAllItem = true;
assert.ok(question.showSelectAllItem);
Expand Down
2 changes: 1 addition & 1 deletion tests/question_matrixdynamictests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4773,7 +4773,7 @@ QUnit.test("showInMultipleColumns property, and visibleIf in choices", function
name: "col2",
cellType: "checkbox",
showInMultipleColumns: true,
hasNone: true,
showNoneItem: true,
choices: [
{ value: "A", visibleIf: "{val1} = 1" },
{ value: "B", visibleIf: "{val1} = 2" },
Expand Down
2 changes: 1 addition & 1 deletion tests/question_tagbox_tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ const jsonTagboxWithSelectAll = {
type: "tagbox",
name: "question1",
hasOther: "true",
hasNone: "true",
showNoneItem: "true",
hasSelectAll: "true",
choices: [
"item1",
Expand Down
Loading

0 comments on commit b7175c5

Please sign in to comment.