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

Issue 1820 #1830

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/uiSelectController.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down Expand Up @@ -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 = [];
Expand Down Expand Up @@ -430,7 +435,7 @@ uis.controller('uiSelectCtrl',
ctrl.close(skipFocusser);
return;
}
}
}
_resetSearchInput();
$scope.$broadcast('uis:select', item);

Expand Down
11 changes: 10 additions & 1 deletion src/uiSelectDirective.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,15 @@ uis.directive('uiSelect',
}
});

$select.objectUniqueKey = attrs.objectUniqueKey;
attrs.$observe('objectUniqueKey', function() {
if(attrs.objectUniqueKey !== undefined) {
$select.objectUniqueKey = attrs.objectUniqueKey;
} else {
$select.objectUniqueKey = undefined;
}
});

attrs.$observe('taggingLabel', function() {
if(attrs.tagging !== undefined )
{
Expand Down Expand Up @@ -372,7 +381,7 @@ uis.directive('uiSelect',
};

var opened = false;

scope.calculateDropdownPos = function() {
if ($select.open) {
dropdown = angular.element(element).querySelectorAll('.ui-select-dropdown');
Expand Down
11 changes: 10 additions & 1 deletion test/select.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down