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

Added closeOnSelect attribute #362

Merged
merged 2 commits into from
Nov 18, 2014
Merged
Show file tree
Hide file tree
Changes from all 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
10 changes: 5 additions & 5 deletions examples/demo-multi-select.html
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ <h3>Multi select</h3>
<h1>Multi Selection Demos</h1>

<h3>Array of strings</h3>
<ui-select multiple ng-model="multipleDemo.colors" theme="bootstrap" ng-disabled="disabled" style="width: 300px;">
<ui-select multiple ng-model="multipleDemo.colors" theme="bootstrap" ng-disabled="disabled" close-on-select="false" style="width: 300px;">
<ui-select-match placeholder="Select colors...">{{$item}}</ui-select-match>
<ui-select-choices repeat="color in availableColors | filter:$select.search">
{{color}}
Expand All @@ -122,7 +122,7 @@ <h3>Array of strings</h3>
<p>Selected: {{multipleDemo.colors}}</p>
<hr>
<h3>Array of objects</h3>
<ui-select multiple ng-model="multipleDemo.selectedPeople" theme="bootstrap" ng-disabled="disabled" style="width: 800px;">
<ui-select multiple ng-model="multipleDemo.selectedPeople" theme="bootstrap" ng-disabled="disabled" close-on-select="false" style="width: 800px;">
<ui-select-match placeholder="Select person...">{{$item.name}} &lt;{{$item.email}}&gt;</ui-select-match>
<ui-select-choices repeat="person in people | propsFilter: {name: $select.search, age: $select.search}">
<div ng-bind-html="person.name | highlight: $select.search"></div>
Expand All @@ -136,7 +136,7 @@ <h3>Array of objects</h3>

<hr>
<h3>Deselect callback with single property binding</h3>
<ui-select multiple ng-model="multipleDemo.deSelectedPeople" on-remove="removed($item, $model)" theme="bootstrap" ng-disabled="disabled" style="width: 800px;">
<ui-select multiple ng-model="multipleDemo.deSelectedPeople" on-remove="removed($item, $model)" theme="bootstrap" ng-disabled="disabled" close-on-select="false" style="width: 800px;">
<ui-select-match placeholder="Select person...">{{$item.name}} &lt;{{$item.email}}&gt;</ui-select-match>
<ui-select-choices repeat="person.email as person in people | propsFilter: {name: $select.search, age: $select.search}">
<div ng-bind-html="person.name | highlight: $select.search"></div>
Expand All @@ -151,7 +151,7 @@ <h3>Deselect callback with single property binding</h3>

<hr>
<h3>Array of objects with single property binding</h3>
<ui-select multiple ng-model="multipleDemo.selectedPeopleSimple" theme="bootstrap" ng-disabled="disabled" style="width: 800px;">
<ui-select multiple ng-model="multipleDemo.selectedPeopleSimple" theme="bootstrap" ng-disabled="disabled" close-on-select="false" style="width: 800px;">
<ui-select-match placeholder="Select person...">{{$item.name}} &lt;{{$item.email}}&gt;</ui-select-match>
<ui-select-choices repeat="person.email as person in people | propsFilter: {name: $select.search, age: $select.search}">
<div ng-bind-html="person.name | highlight: $select.search"></div>
Expand All @@ -165,7 +165,7 @@ <h3>Array of objects with single property binding</h3>

<hr>
<h3>Array of objects (with groupBy)</h3>
<ui-select multiple ng-model="multipleDemo.selectedPeopleWithGroupBy" theme="bootstrap" ng-disabled="disabled" style="width: 800px;">
<ui-select multiple ng-model="multipleDemo.selectedPeopleWithGroupBy" theme="bootstrap" ng-disabled="disabled" close-on-select="false" style="width: 800px;">
<ui-select-match placeholder="Select person...">{{$item.name}} &lt;{{$item.email}}&gt;</ui-select-match>
<ui-select-choices group-by="someGroupFn" repeat="person in people | propsFilter: {name: $select.search, age: $select.search}">
<div ng-bind-html="person.name | highlight: $select.search"></div>
Expand Down
24 changes: 16 additions & 8 deletions src/select.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@
theme: 'bootstrap',
searchEnabled: true,
placeholder: '', // Empty by default, like HTML tag <select>
refreshDelay: 1000 // In milliseconds
refreshDelay: 1000, // In milliseconds
closeOnSelect: true
})

// See Rename minErr and make it accessible from outside https://github.com/angular/angular.js/issues/6913
Expand Down Expand Up @@ -160,6 +161,8 @@
ctrl.multiple = false; // Initialized inside uiSelect directive link function
ctrl.disableChoiceExpression = undefined; // Initialized inside uiSelect directive link function
ctrl.lockChoiceExpression = undefined; // Initialized inside uiSelect directive link function
ctrl.closeOnSelect = true; // Initialized inside uiSelect directive link function
ctrl.clickTriggeredSelect = false;

ctrl.isEmpty = function() {
return angular.isUndefined(ctrl.selected) || ctrl.selected === null || ctrl.selected === '';
Expand Down Expand Up @@ -332,8 +335,7 @@
};

// When the user clicks on an item inside the dropdown
ctrl.select = function(item, skipFocusser) {

ctrl.select = function(item, skipFocusser, $event) {
if (item === undefined || !item._uiSelectChoiceDisabled) {
var locals = {};
locals[ctrl.parserResult.itemName] = item;
Expand All @@ -343,13 +345,18 @@
$model: ctrl.parserResult.modelMapper($scope, locals)
});

if(ctrl.multiple){
if(ctrl.multiple) {
ctrl.selected.push(item);
ctrl.sizeSearchInput();
} else {
ctrl.selected = item;
}
ctrl.close(skipFocusser);
if (!ctrl.multiple || ctrl.closeOnSelect) {
ctrl.close(skipFocusser);
}
if ($event && $event.type === 'click') {
ctrl.clickTriggeredSelect = true;
}
}
};

Expand Down Expand Up @@ -624,7 +631,7 @@
var searchInput = element.querySelectorAll('input.ui-select-search');

$select.multiple = (angular.isDefined(attrs.multiple)) ? (attrs.multiple === '') ? true : (attrs.multiple.toLowerCase() === 'true') : false;

$select.closeOnSelect = (angular.isDefined(attrs.closeOnSelect) && attrs.closeOnSelect.toLowerCase() === 'false') ? false : uiSelectConfig.closeOnSelect;
$select.onSelectCallback = $parse(attrs.onSelect);
$select.onRemoveCallback = $parse(attrs.onRemove);

Expand Down Expand Up @@ -827,10 +834,11 @@
contains = element[0].contains(e.target);
}

if (!contains) {
if (!contains && !$select.clickTriggeredSelect) {
$select.close();
scope.$digest();
}
$select.clickTriggeredSelect = false;
}

// See Click everywhere but here event http://stackoverflow.com/questions/12931369
Expand Down Expand Up @@ -910,7 +918,7 @@
choices.attr('ng-repeat', RepeatParser.getNgRepeatExpression($select.parserResult.itemName, '$select.items', $select.parserResult.trackByExp, groupByExp))
.attr('ng-if', '$select.open') //Prevent unnecessary watches when dropdown is closed
.attr('ng-mouseenter', '$select.setActiveItem('+$select.parserResult.itemName +')')
.attr('ng-click', '$select.select(' + $select.parserResult.itemName + ')');
.attr('ng-click', '$select.select(' + $select.parserResult.itemName + ',false,$event)');

var rowsInner = element.querySelectorAll('.ui-select-choices-row-inner');
if (rowsInner.length !== 1) throw uiSelectMinErr('rows', "Expected 1 .ui-select-choices-row-inner but got '{0}'.", rowsInner.length);
Expand Down
17 changes: 17 additions & 0 deletions test/select.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1043,6 +1043,7 @@ describe('ui-select tests', function() {
if (attrs.disabled !== undefined) { attrsHtml += ' ng-disabled="' + attrs.disabled + '"'; }
if (attrs.required !== undefined) { attrsHtml += ' ng-required="' + attrs.required + '"'; }
if (attrs.tabindex !== undefined) { attrsHtml += ' tabindex="' + attrs.tabindex + '"'; }
if (attrs.closeOnSelect !== undefined) { attrsHtml += ' close-on-select="' + attrs.closeOnSelect + '"'; }
}

return compileTemplate(
Expand Down Expand Up @@ -1278,6 +1279,22 @@ describe('ui-select tests', function() {

});

it('should not close dropdown after selecting if closeOnSelect=false', function() {

scope.selection.selectedMultiple = [scope.people[5]]; //Samantha
var el = createUiSelectMultiple({closeOnSelect: false});
var searchInput = el.find('.ui-select-search');

expect(isDropdownOpened(el)).toEqual(false);
triggerKeydown(searchInput, Key.Down)
expect(isDropdownOpened(el)).toEqual(true);

clickItem(el, 'Wladimir');

expect(isDropdownOpened(el)).toEqual(true);

});

it('should closes dropdown when pressing ESC key from search input', function() {

scope.selection.selectedMultiple = [scope.people[4], scope.people[5], scope.people[6]]; //Wladimir, Samantha & Nicole
Expand Down