Skip to content

Commit c76ef3e

Browse files
author
pkempenaers
committed
Merge branch 'develop'
2 parents 9366835 + 080ba0b commit c76ef3e

File tree

4 files changed

+83
-12
lines changed

4 files changed

+83
-12
lines changed

src/app/component/dropdownTree.controller.js

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -151,20 +151,22 @@
151151
}
152152

153153
optionClicked(option) {
154-
const indexOfOption = this.selectedOptions.indexOf(option);
155-
if (indexOfOption >= 0) {
156-
this.selectedOptions.splice(indexOfOption, 1);
157-
} else {
158-
if (this.settings.selectionLimit > 0) {
159-
if (this.settings.selectionLimit === this.selectedOptions.length) {
160-
if (this.settings.removeFromFront) {
161-
this.selectedOptions.splice(0, 1);
162-
} else {
163-
this.selectedOptions.splice(this.selectedOptions.length - 1, 1);
154+
if (this.dropdownTreeService.isSelectAble(option, this.settings)) {
155+
const indexOfOption = this.selectedOptions.indexOf(option);
156+
if (indexOfOption >= 0) {
157+
this.selectedOptions.splice(indexOfOption, 1);
158+
} else {
159+
if (this.settings.selectionLimit > 0) {
160+
if (this.settings.selectionLimit === this.selectedOptions.length) {
161+
if (this.settings.removeFromFront) {
162+
this.selectedOptions.splice(0, 1);
163+
} else {
164+
this.selectedOptions.splice(this.selectedOptions.length - 1, 1);
165+
}
164166
}
165167
}
168+
this.selectedOptions.push(option);
166169
}
167-
this.selectedOptions.push(option);
168170
}
169171
this.emitSelection();
170172
}

src/app/component/dropdownTree.service.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,55 @@ export default class DropdownTreeService {
3939
return undefined;
4040
}
4141

42+
selectAllChildOptions(option, settings, currentSelection) {
43+
const optionsToAdd = this.getOptionsToAdd(option, settings, currentSelection);
44+
45+
if (optionsToAdd.length === 0) {
46+
this.removeAllChildOptions(option, settings, currentSelection);
47+
return true;
48+
}
49+
if (settings.selectionLimit === 0) {
50+
optionsToAdd.forEach((optionToAdd) => {
51+
currentSelection.push(optionToAdd);
52+
});
53+
return true;
54+
}
55+
if (optionsToAdd.length + currentSelection.length <= settings.selectionLimit) {
56+
optionsToAdd.forEach((optionToAdd) => {
57+
currentSelection.push(optionToAdd);
58+
});
59+
return true;
60+
}
61+
62+
return false;
63+
}
64+
65+
getOptionsToAdd(option, settings, currentSelection) {
66+
let optionsToAdd = [];
67+
if (this.isFolder(option, settings)) {
68+
this.getChildOptions(option, settings)
69+
.forEach((childOption) => {
70+
optionsToAdd = optionsToAdd.concat(
71+
this.getOptionsToAdd(childOption, settings, currentSelection, false),
72+
);
73+
});
74+
} else if (currentSelection.indexOf(option) < 0) {
75+
optionsToAdd.push(option);
76+
}
77+
78+
return optionsToAdd;
79+
}
80+
81+
removeAllChildOptions(option, settings, currentSelection) {
82+
if (this.isFolder(option, settings)) {
83+
this.getChildOptions(option, settings).forEach((childOption) => {
84+
this.removeAllChildOptions(childOption, settings, currentSelection);
85+
});
86+
} else if (currentSelection.indexOf(option) >= 0) {
87+
currentSelection.splice(currentSelection.indexOf(option), 1);
88+
}
89+
}
90+
4291
isVisible(option, settings, searchText) {
4392
if (this.isFolder(option, settings) &&
4493
!this.isVisibleItem(option, settings, searchText)) {
@@ -102,4 +151,11 @@ export default class DropdownTreeService {
102151
}
103152
return !collection.some(option => compareCollection.indexOf(option) < 0);
104153
}
154+
155+
isSelectAble(option, settings) {
156+
if (!this.isFolder(option, settings)) {
157+
return true;
158+
}
159+
return settings.folderSelectable;
160+
}
105161
}

src/app/component/option-row/option-row.controller.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,19 @@
3737
this.optionClicked({ option });
3838
}
3939

40+
selectAllChilds() {
41+
const selectionChanged =
42+
this.dropdownTreeService.selectAllChildOptions(
43+
this.option,
44+
this.settings,
45+
this.selectedOptions,
46+
true,
47+
);
48+
if (selectionChanged) {
49+
this.optionClicked({ option: this.option });
50+
}
51+
}
52+
4053
isSelected() {
4154
return this.selectedOptions.indexOf(this.option) >= 0;
4255
}

src/app/component/option-row/option-row.template.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
<span ng-if="!$ctrl.isFolder()"
1616
ng-class="$ctrl.settings.childClass"></span>
1717
<span tabindex="-1"
18-
ng-click="(!$ctrl.isFolder() || $ctrl.settings.folderSelectable) && $ctrl.innerClicked($ctrl.option)"
18+
ng-click="((!$ctrl.isFolder() || $ctrl.settings.folderSelectable) && $ctrl.innerClicked($ctrl.option)) || ($ctrl.isFolder() && !$ctrl.settings.folderSelectable && $ctrl.selectAllChilds())"
1919
ng-keydown="$ctrl.keyDown($event)"
2020
class="focusable">
2121
{{$ctrl.getDisplayText()}}

0 commit comments

Comments
 (0)