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

Commit

Permalink
fix(isDisabled): do not modify item
Browse files Browse the repository at this point in the history
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 #1200 and #1661

Partially supersedes #1641
  • Loading branch information
user378230 committed Jul 9, 2016
1 parent 43c1e4d commit b95bf9f
Showing 1 changed file with 32 additions and 8 deletions.
40 changes: 32 additions & 8 deletions src/uiSelectController.js
Original file line number Diff line number Diff line change
Expand Up @@ -324,18 +324,42 @@ uis.controller('uiSelectCtrl',
ctrl.selected.filter(function (selection) { return angular.equals(selection, item); }).length > 0);
};

var disabledItems = [];

function _updateItemDisabled(item, isDisabled) {
var disabledItemIndex = disabledItems.indexOf(item);
if (isDisabled && disabledItemIndex === -1) {
disabledItems.push(item);
}

if (!isDisabled && disabledItemIndex > -1) {
disabledItems.splice(disabledItemIndex, 0);
}
}

function _isItemDisabled(item) {
return disabledItems.indexOf(item) > -1;
}

ctrl.isDisabled = function(itemScope) {

if (!ctrl.open) return;

var itemIndex = ctrl.items.indexOf(itemScope[ctrl.itemProperty]);
var item = itemScope[ctrl.itemProperty];
var itemIndex = ctrl.items.indexOf(item);
var isDisabled = false;
var item;

if (itemIndex >= 0 && (angular.isDefined(ctrl.disableChoiceExpression) || ctrl.multiple)) {

if (itemIndex >= 0 && (!angular.isUndefined(ctrl.disableChoiceExpression) || ctrl.multiple)) {
item = ctrl.items[itemIndex];
isDisabled = !!(itemScope.$eval(ctrl.disableChoiceExpression)) || _isItemSelected(item); // force the boolean value
item._uiSelectChoiceDisabled = isDisabled; // store this for later reference
if (ctrl.multiple) {
isDisabled = _isItemSelected(item);
}

if (!isDisabled && angular.isDefined(ctrl.disableChoiceExpression)) {
isDisabled = !!(itemScope.$eval(ctrl.disableChoiceExpression));
}

_updateItemDisabled(item, isDisabled);
}

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

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

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

if (!item || !item._uiSelectChoiceDisabled) {
if (!item || !_isItemDisabled(item)) {
if(ctrl.tagging.isActivated) {
// if taggingLabel is disabled and item is undefined we pull from ctrl.search
if ( ctrl.taggingLabel === false ) {
Expand Down

0 comments on commit b95bf9f

Please sign in to comment.