Skip to content

Commit

Permalink
Add limit property
Browse files Browse the repository at this point in the history
Closes #32
  • Loading branch information
indrimuska committed Aug 26, 2016
1 parent 5434247 commit 2b74db8
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions src/angular-selector.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
options: '=?',
debounce: '=?',
create: '&?',
limit: '=?',
rtl: '=?',
api: '=?',
change: '&?',
Expand Down Expand Up @@ -75,6 +76,7 @@
groupAttr: 'group',
options: [],
debounce: 0,
limit: Infinity,
remoteParam: 'q',
remoteValidationParam: 'value',
removeButton: true,
Expand Down Expand Up @@ -270,6 +272,7 @@
});
};
scope.open = function () {
if (scope.multiple && (scope.selectedValues || []).length >= scope.limit) return;
scope.isOpen = true;
scope.dropdownPosition();
$timeout(scope.scrollToHighlighted);
Expand Down Expand Up @@ -329,6 +332,8 @@
});
};
scope.set = function (option) {
if (scope.multiple && (scope.selectedValues || []).length >= scope.limit) return;

if (!angular.isDefined(option))
option = scope.filteredOptions[scope.highlighted];

Expand All @@ -339,7 +344,7 @@
if (scope.selectedValues.indexOf(option) < 0)
scope.selectedValues.push(option);
}
if (!scope.multiple || scope.closeAfterSelection) scope.close();
if (!scope.multiple || scope.closeAfterSelection || (scope.selectedValues || []).length >= scope.limit) scope.close();
scope.resetInput();
selectCtrl.$setDirty();
};
Expand Down Expand Up @@ -491,7 +496,7 @@
return filter(scope.options, function (option) {
return scope.optionEquals(option, value);
})[0];
}).filter(function (value) { return angular.isDefined(value); });
}).filter(function (value) { return angular.isDefined(value); }).slice(0, scope.limit);
};
scope.$watch('value', function (newValue, oldValue) {
if (angular.equals(newValue, oldValue)) return;
Expand Down Expand Up @@ -575,7 +580,7 @@
'ng-class="{open: isOpen, empty: !filteredOptions.length && (!create || !search), multiple: multiple, \'has-value\': hasValue(), rtl: rtl, ' +
'loading: loading, \'remove-button\': removeButton, disabled: disabled}">' +
'<select name="{{name}}" ng-hide="true" ng-required="required && !hasValue()" ' +
'ng-model="selectedValues" multiple ng-options="option as getObjValue(option, labelAttr) for option in selectedValues" ng-hide="true"></select>' +
'ng-model="selectedValues" multiple ng-options="option as getObjValue(option, labelAttr) for option in selectedValues"></select>' +
'<label class="selector-input">' +
'<ul class="selector-values">' +
'<li ng-repeat="(index, option) in selectedValues track by index">' +
Expand All @@ -585,8 +590,8 @@
'</div>' +
'</li>' +
'</ul>' +
'<input ng-model="search" placeholder="{{!hasValue() ? placeholder : \'\'}}" ng-model-options="{ debounce: debounce }"' +
'ng-disabled="disabled" ng-readonly="disableSearch" ng-required="required && !hasValue()">' +
'<input ng-model="search" placeholder="{{!hasValue() ? placeholder : \'\'}}" ng-model-options="{debounce: debounce}"' +
'ng-disabled="disabled" ng-readonly="disableSearch" ng-required="required && !hasValue()" autocomplete="off">' +
'<div ng-if="!multiple || loading" class="selector-helper selector-global-helper" ng-click="!disabled && removeButton && unset()">' +
'<span class="selector-icon"></span>' +
'</div>' +
Expand Down

0 comments on commit 2b74db8

Please sign in to comment.