Skip to content
This repository was archived by the owner on Oct 2, 2019. It is now read-only.

Commit 19a9b9a

Browse files
committed
Add option to enable or disable it
1 parent e166672 commit 19a9b9a

File tree

2 files changed

+68
-14
lines changed

2 files changed

+68
-14
lines changed

src/uiSelectHeaderGroupSelectableDirective.js

Lines changed: 50 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,38 +2,77 @@ uis.directive('uiSelectHeaderGroupSelectable', ['$timeout', function($timeout) {
22
return {
33
restrict: 'EA',
44
require: ['^uiSelect'],
5-
scope: true,
5+
scope: {
6+
isEnabled: "<?uiSelectHeaderGroupSelectable"
7+
},
68
link: function ($scope, $element, attrs, select) {
79
// TODO Why that???
810
var $select = select[0];
11+
if (angular.isUndefined($scope.isEnabled)) {
12+
$scope.isEnabled = true;
13+
}
914

10-
$scope.$watch('$select.groups', function() {
15+
function isEnabled() {
16+
return angular.isUndefined($scope.isEnabled) || $scope.isEnabled;
17+
}
18+
19+
function getElements() {
1120
if ($select.multiple && $select.groups) {
12-
var elements = $element.querySelectorAll('.ui-select-choices-group-label');
21+
return $element.querySelectorAll('.ui-select-choices-group-label');
22+
} else {
23+
console.error('Use uiSelectHeaderGroupSelectable with no multiple uiSelect or without groupBy');
24+
return [];
25+
}
26+
}
1327

14-
angular.forEach(elements, function(e) {
28+
function enableClick() {
29+
if (isEnabled()) {
30+
angular.forEach(getElements(), function(e) {
1531
var element = angular.element(e);
1632

1733
// Check the onClick event is not already listen
1834
if (!element.hasClass('ui-select-header-group-selectable')) {
1935
element.addClass('ui-select-header-group-selectable');
2036

2137
element.on('click', function () {
22-
// TODO It's the good way?
23-
var group = $select.findGroupByName(element.text(), true);
38+
if (isEnabled()) {
39+
var group = $select.findGroupByName(element.text(), true);
2440

25-
angular.forEach(group.items, function(item) {
26-
$timeout(function() {
27-
$select.select(item, false, ' ');
41+
angular.forEach(group.items, function(item) {
42+
$timeout(function() {
43+
$select.select(item, false, ' ');
44+
});
2845
});
29-
});
46+
}
3047
});
3148
}
3249
});
50+
}
51+
}
52+
53+
function disableClick() {
54+
if (!isEnabled()) {
55+
angular.forEach(getElements(), function(e) {
56+
var element = angular.element(e);
57+
element.removeClass('ui-select-header-group-selectable');
58+
element.off('click');
59+
});
60+
}
61+
}
62+
63+
// Watch element to trigger select event
64+
$scope.$watch('isEnabled', function() {
65+
if (!isEnabled()) {
66+
disableClick();
3367
} else {
34-
console.error('Use uiSelectHeaderGroupSelectable with no multiple uiSelect or without groupBy');
68+
enableClick();
3569
}
3670
});
71+
72+
$scope.$watch('$select.groups', enableClick);
73+
$scope.$watch(function() {
74+
return $select.selected && $select.selected.length ? $select.selected.length : -1;
75+
}, enableClick);
3776
}
3877
};
3978
}]);

test/select.spec.js

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1926,7 +1926,7 @@ describe('ui-select tests', function () {
19261926
if (attrs.refresh !== undefined) { choicesAttrsHtml += ' refresh="' + attrs.refresh + '"'; }
19271927
if (attrs.refreshDelay !== undefined) { choicesAttrsHtml += ' refresh-delay="' + attrs.refreshDelay + '"'; }
19281928
if (attrs.lockChoice !== undefined) { matchesAttrsHtml += ' ui-lock-choice="' + attrs.lockChoice + '"'; }
1929-
if (attrs.uiSelectHeaderGroupSelectable !== undefined) { choicesAttrsHtml += ' ui-select-header-group-selectable'; }
1929+
if (attrs.uiSelectHeaderGroupSelectable !== undefined) { choicesAttrsHtml += ' ui-select-header-group-selectable="' + attrs.uiSelectHeaderGroupSelectable + '"'; }
19301930
}
19311931

19321932
matchesAttrsHtml += ' placeholder="' + matchesPlaceholder + '"';
@@ -2083,6 +2083,7 @@ describe('ui-select tests', function () {
20832083
expect(containerWidth - newWidth).toBeLessThan(10);
20842084

20852085
});
2086+
20862087
it('should move to last match when pressing BACKSPACE key from search', function () {
20872088

20882089
var el = createUiSelectMultiple();
@@ -2157,7 +2158,6 @@ describe('ui-select tests', function () {
21572158

21582159
});
21592160

2160-
21612161
it('should move to last match when pressing LEFT key from search', function () {
21622162

21632163
var el = createUiSelectMultiple();
@@ -3176,7 +3176,22 @@ describe('ui-select tests', function () {
31763176

31773177
expect(ctrl.selected.length).toEqual(2);
31783178
});
3179-
})
3179+
3180+
it('should don\'t work with false attribute', function () {
3181+
var el = createUiSelectMultiple({ groupBy: "'age'", uiSelectHeaderGroupSelectable: false });
3182+
var ctrl = el.scope().$select;
3183+
3184+
showChoicesForSearch(el, '');
3185+
expect(ctrl.multiple).toEqual(true);
3186+
expect(ctrl.groups.length).toEqual(5);
3187+
openDropdown(el);
3188+
3189+
$(el).find('div.ui-select-header-group-selectable').first().click();
3190+
showChoicesForSearch(el, '');
3191+
3192+
expect(ctrl.selected.length).toEqual(0);
3193+
});
3194+
});
31803195
});
31813196

31823197
it('should add an id to the search input field', function () {

0 commit comments

Comments
 (0)