From 7f9dc9ef80c73211ae5c66fd376482f25b720d08 Mon Sep 17 00:00:00 2001 From: Mateusz Zajac Date: Thu, 20 Oct 2016 04:27:55 -0700 Subject: [PATCH 1/5] ISSUE-1820 https://github.com/angular-ui/ui-select/issues/1820 Added a new optional attribute called objectUniqueKey for ui-select html tag. That if defined it will allow to decide if objects given are unique bases on a key defined in this attribute. --- src/uiSelectController.js | 9 +++++++-- src/uiSelectDirective.js | 14 +++++++++++++- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/src/uiSelectController.js b/src/uiSelectController.js index 1123e9946..cf9b20314 100644 --- a/src/uiSelectController.js +++ b/src/uiSelectController.js @@ -46,6 +46,7 @@ uis.controller('uiSelectCtrl', ctrl.clickTriggeredSelect = false; ctrl.$filter = $filter; ctrl.$element = $element; + ctrl.objectUniqueKey = undefined; // Use $injector to check for $animate and store a reference to it ctrl.$animate = (function () { @@ -330,9 +331,13 @@ uis.controller('uiSelectCtrl', return isActive; }; + function _getKeyForComparison(item) { + return ctrl.objectUniqueKey === undefined ? item : item[ctrl.objectUniqueKey]; + } + var _isItemSelected = function (item) { return (ctrl.selected && angular.isArray(ctrl.selected) && - ctrl.selected.filter(function (selection) { return angular.equals(selection, item); }).length > 0); + ctrl.selected.filter(function (selection) { return angular.equals(_getKeyForComparison(selection), _getKeyForComparison(item)); }).length > 0); }; var disabledItems = []; @@ -430,7 +435,7 @@ uis.controller('uiSelectCtrl', ctrl.close(skipFocusser); return; } - } + } _resetSearchInput(); $scope.$broadcast('uis:select', item); diff --git a/src/uiSelectDirective.js b/src/uiSelectDirective.js index 08bba831c..07c8e0317 100644 --- a/src/uiSelectDirective.js +++ b/src/uiSelectDirective.js @@ -121,6 +121,18 @@ uis.directive('uiSelect', } }); + $select.objectUniqueKey = $parse(attrs.objectUniqueKey); + attrs.$observe('objectUniqueKey', function() { + if(attrs.objectUniqueKey !== undefined) + { + $select.objectUniqueKey = attrs.tagging; + } + else + { + $select.objectUniqueKey = undefined; + } + }); + attrs.$observe('taggingLabel', function() { if(attrs.tagging !== undefined ) { @@ -372,7 +384,7 @@ uis.directive('uiSelect', }; var opened = false; - + scope.calculateDropdownPos = function() { if ($select.open) { dropdown = angular.element(element).querySelectorAll('.ui-select-dropdown'); From d95d9b60ace33abc65f8bb252058c126e783811b Mon Sep 17 00:00:00 2001 From: Mateusz Zajac Date: Thu, 20 Oct 2016 05:05:15 -0700 Subject: [PATCH 2/5] Corrected a typo --- src/uiSelectDirective.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/uiSelectDirective.js b/src/uiSelectDirective.js index 07c8e0317..73ed7080f 100644 --- a/src/uiSelectDirective.js +++ b/src/uiSelectDirective.js @@ -125,7 +125,7 @@ uis.directive('uiSelect', attrs.$observe('objectUniqueKey', function() { if(attrs.objectUniqueKey !== undefined) { - $select.objectUniqueKey = attrs.tagging; + $select.objectUniqueKey = attrs.objectUniqueKey; } else { From d6f31ec9f779c55eefff60b6611521b079b23360 Mon Sep 17 00:00:00 2001 From: Mateusz Zajac Date: Thu, 20 Oct 2016 05:07:49 -0700 Subject: [PATCH 3/5] Corrected styling --- src/uiSelectDirective.js | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/uiSelectDirective.js b/src/uiSelectDirective.js index 73ed7080f..d76963da3 100644 --- a/src/uiSelectDirective.js +++ b/src/uiSelectDirective.js @@ -123,12 +123,9 @@ uis.directive('uiSelect', $select.objectUniqueKey = $parse(attrs.objectUniqueKey); attrs.$observe('objectUniqueKey', function() { - if(attrs.objectUniqueKey !== undefined) - { + if(attrs.objectUniqueKey !== undefined) { $select.objectUniqueKey = attrs.objectUniqueKey; - } - else - { + } else { $select.objectUniqueKey = undefined; } }); From a6dfdec42675614e5195f51612b81c048c23d284 Mon Sep 17 00:00:00 2001 From: Mateusz Zajac Date: Thu, 20 Oct 2016 06:51:14 -0700 Subject: [PATCH 4/5] Corrected tests --- src/uiSelectDirective.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/uiSelectDirective.js b/src/uiSelectDirective.js index d76963da3..adff87d42 100644 --- a/src/uiSelectDirective.js +++ b/src/uiSelectDirective.js @@ -121,7 +121,7 @@ uis.directive('uiSelect', } }); - $select.objectUniqueKey = $parse(attrs.objectUniqueKey); + $select.objectUniqueKey = attrs.objectUniqueKey; attrs.$observe('objectUniqueKey', function() { if(attrs.objectUniqueKey !== undefined) { $select.objectUniqueKey = attrs.objectUniqueKey; From 458ff4705b69120df82529dd7f29160880f25c0d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20Zaj=C4=85c?= Date: Thu, 20 Oct 2016 16:15:16 +0200 Subject: [PATCH 5/5] Added unit test for object duplicate selection --- test/select.spec.js | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/test/select.spec.js b/test/select.spec.js index 9feb277a4..83a046cab 100644 --- a/test/select.spec.js +++ b/test/select.spec.js @@ -2762,7 +2762,16 @@ describe('ui-select tests', function() { expect(el.scope().$select.selected.length).toBe(1); clickItem(el, 'Wladimir'); expect(el.scope().$select.selected.length).toBe(2); - }); + }); + + it('should not allow duplicate objects if unique object key is specified', function() { + var el = createUiSelectMultiple({objectUniqueKey: "name"}); + expect(el.scope().$select.selected.length).toBe(0); + clickItem(el, 'Samantha'); + expect(el.scope().$select.selected.length).toBe(1); + clickItem(el, 'Samantha'); + expect(el.scope().$select.selected.length).toBe(1); + }); describe('resetSearchInput option multiple', function () { it('should be true by default', function () {