Skip to content

Commit b95bf9f

Browse files
committed
fix(isDisabled): do not modify item
Previously the item from the list was modified with the _uiSelectChoiceDisabled property. This allowed a leakage of information from ui-select to outside the directive. It also caused issues when the was used outside of the directive. This commit adds a reference array to store disabled items and so prevents the need to modify the item in place. Closes angular-ui#1200 and angular-ui#1661 Partially supersedes angular-ui#1641
1 parent 43c1e4d commit b95bf9f

File tree

1 file changed

+32
-8
lines changed

1 file changed

+32
-8
lines changed

src/uiSelectController.js

+32-8
Original file line numberDiff line numberDiff line change
@@ -324,18 +324,42 @@ uis.controller('uiSelectCtrl',
324324
ctrl.selected.filter(function (selection) { return angular.equals(selection, item); }).length > 0);
325325
};
326326

327+
var disabledItems = [];
328+
329+
function _updateItemDisabled(item, isDisabled) {
330+
var disabledItemIndex = disabledItems.indexOf(item);
331+
if (isDisabled && disabledItemIndex === -1) {
332+
disabledItems.push(item);
333+
}
334+
335+
if (!isDisabled && disabledItemIndex > -1) {
336+
disabledItems.splice(disabledItemIndex, 0);
337+
}
338+
}
339+
340+
function _isItemDisabled(item) {
341+
return disabledItems.indexOf(item) > -1;
342+
}
343+
327344
ctrl.isDisabled = function(itemScope) {
328345

329346
if (!ctrl.open) return;
330347

331-
var itemIndex = ctrl.items.indexOf(itemScope[ctrl.itemProperty]);
348+
var item = itemScope[ctrl.itemProperty];
349+
var itemIndex = ctrl.items.indexOf(item);
332350
var isDisabled = false;
333-
var item;
351+
352+
if (itemIndex >= 0 && (angular.isDefined(ctrl.disableChoiceExpression) || ctrl.multiple)) {
334353

335-
if (itemIndex >= 0 && (!angular.isUndefined(ctrl.disableChoiceExpression) || ctrl.multiple)) {
336-
item = ctrl.items[itemIndex];
337-
isDisabled = !!(itemScope.$eval(ctrl.disableChoiceExpression)) || _isItemSelected(item); // force the boolean value
338-
item._uiSelectChoiceDisabled = isDisabled; // store this for later reference
354+
if (ctrl.multiple) {
355+
isDisabled = _isItemSelected(item);
356+
}
357+
358+
if (!isDisabled && angular.isDefined(ctrl.disableChoiceExpression)) {
359+
isDisabled = !!(itemScope.$eval(ctrl.disableChoiceExpression));
360+
}
361+
362+
_updateItemDisabled(item, isDisabled);
339363
}
340364

341365
return isDisabled;
@@ -344,11 +368,11 @@ uis.controller('uiSelectCtrl',
344368

345369
// When the user selects an item with ENTER or clicks the dropdown
346370
ctrl.select = function(item, skipFocusser, $event) {
347-
if (item === undefined || !item._uiSelectChoiceDisabled) {
371+
if (item === undefined || !_isItemDisabled(item)) {
348372

349373
if ( ! ctrl.items && ! ctrl.search && ! ctrl.tagging.isActivated) return;
350374

351-
if (!item || !item._uiSelectChoiceDisabled) {
375+
if (!item || !_isItemDisabled(item)) {
352376
if(ctrl.tagging.isActivated) {
353377
// if taggingLabel is disabled and item is undefined we pull from ctrl.search
354378
if ( ctrl.taggingLabel === false ) {

0 commit comments

Comments
 (0)