This repository has been archived by the owner on Oct 2, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1998 from DarkIsDude/master
feat(choices): allow user select group header
- Loading branch information
Showing
4 changed files
with
135 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
uis.directive('uiSelectHeaderGroupSelectable', ['$timeout', function($timeout) { | ||
return { | ||
restrict: 'EA', | ||
require: ['^uiSelect'], | ||
scope: { | ||
isEnabled: "<?uiSelectHeaderGroupSelectable" | ||
}, | ||
link: function ($scope, $element, attrs, select) { | ||
// TODO Why that??? | ||
var $select = select[0]; | ||
if (angular.isUndefined($scope.isEnabled)) { | ||
$scope.isEnabled = true; | ||
} | ||
|
||
function isEnabled() { | ||
return angular.isUndefined($scope.isEnabled) || $scope.isEnabled; | ||
} | ||
|
||
function getElements() { | ||
if ($select.multiple && $select.groups) { | ||
return $element.querySelectorAll('.ui-select-choices-group-label'); | ||
} else { | ||
console.error('Use uiSelectHeaderGroupSelectable with no multiple uiSelect or without groupBy'); | ||
return []; | ||
} | ||
} | ||
|
||
function enableClick() { | ||
if (isEnabled()) { | ||
angular.forEach(getElements(), function(e) { | ||
var element = angular.element(e); | ||
|
||
// Check the onClick event is not already listen | ||
if (!element.hasClass('ui-select-header-group-selectable')) { | ||
element.addClass('ui-select-header-group-selectable'); | ||
|
||
element.on('click', function () { | ||
if (isEnabled()) { | ||
var group = $select.findGroupByName(element.text(), true); | ||
|
||
angular.forEach(group.items, function(item) { | ||
$timeout(function() { | ||
$select.select(item, false, ' '); | ||
}); | ||
}); | ||
} | ||
}); | ||
} | ||
}); | ||
} | ||
} | ||
|
||
function disableClick() { | ||
if (!isEnabled()) { | ||
angular.forEach(getElements(), function(e) { | ||
var element = angular.element(e); | ||
element.removeClass('ui-select-header-group-selectable'); | ||
element.off('click'); | ||
}); | ||
} | ||
} | ||
|
||
// Watch element to trigger select event | ||
$scope.$watch('isEnabled', function() { | ||
if (!isEnabled()) { | ||
disableClick(); | ||
} else { | ||
enableClick(); | ||
} | ||
}); | ||
|
||
$scope.$watch('$select.groups', enableClick); | ||
$scope.$watch(function() { | ||
return $select.selected && $select.selected.length ? $select.selected.length : -1; | ||
}, enableClick); | ||
} | ||
}; | ||
}]); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters