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

Commit ff227e2

Browse files
committed
merge from upstream/master
2 parents aef5a80 + 6849d9a commit ff227e2

File tree

4 files changed

+63
-6
lines changed

4 files changed

+63
-6
lines changed

src/select.css

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@
2828
padding-left: 0;
2929
}
3030

31+
.select2-locked > .select2-search-choice-close{
32+
display:none;
33+
}
34+
3135
/* Selectize theme */
3236

3337
/* Helper class to show styles when focus */

src/select.js

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,8 @@
138138
* put as much logic in the controller (instead of the link functions) as possible so it can be easily tested.
139139
*/
140140
.controller('uiSelectCtrl',
141-
['$scope', '$element', '$timeout', 'RepeatParser', 'uiSelectMinErr',
142-
function($scope, $element, $timeout, RepeatParser, uiSelectMinErr) {
141+
['$scope', '$element', '$timeout', 'RepeatParser', 'uiSelectMinErr', 'uiSelectConfig',
142+
function($scope, $element, $timeout, RepeatParser, uiSelectMinErr, uiSelectConfig) {
143143

144144
var ctrl = this;
145145

@@ -160,6 +160,7 @@
160160
ctrl.refreshDelay = undefined; // Initialized inside uiSelectChoices directive link function
161161
ctrl.multiple = false; // Initialized inside uiSelect directive link function
162162
ctrl.disableChoiceExpression = undefined; // Initialized inside uiSelect directive link function
163+
ctrl.lockChoiceExpression = undefined; // Initialized inside uiSelect directive link function
163164
ctrl.closeOnSelect = true; // Initialized inside uiSelect directive link function
164165
ctrl.clickTriggeredSelect = false;
165166

@@ -174,7 +175,7 @@
174175

175176
// Most of the time the user does not want to empty the search input when in typeahead mode
176177
function _resetSearchInput() {
177-
if (ctrl.resetSearchInput) {
178+
if (ctrl.resetSearchInput || (ctrl.resetSearchInput === undefined && uiSelectConfig.resetSearchInput)) {
178179
ctrl.search = EMPTY_SEARCH;
179180
//reset activeIndex
180181
if (ctrl.selected && ctrl.items.length && !ctrl.multiple) {
@@ -307,7 +308,13 @@
307308
};
308309

309310
ctrl.isActive = function(itemScope) {
310-
return ctrl.open && ctrl.items.indexOf(itemScope[ctrl.itemProperty]) === ctrl.activeIndex;
311+
var isActive = ctrl.open && ctrl.items.indexOf(itemScope[ctrl.itemProperty]) === ctrl.activeIndex;
312+
313+
if (isActive && !angular.isUndefined(ctrl.onHighlightCallback)) {
314+
itemScope.$eval(ctrl.onHighlightCallback);
315+
}
316+
317+
return isActive;
311318
};
312319

313320
ctrl.isDisabled = function(itemScope) {
@@ -373,9 +380,24 @@
373380
e.stopPropagation();
374381
};
375382

383+
ctrl.isLocked = function(itemScope, itemIndex) {
384+
var isLocked, item = ctrl.selected[itemIndex];
385+
386+
if (item && !angular.isUndefined(ctrl.lockChoiceExpression)) {
387+
isLocked = !!(itemScope.$eval(ctrl.lockChoiceExpression)); // force the boolean value
388+
item._uiSelectChoiceLocked = isLocked; // store this for later reference
389+
}
390+
391+
return isLocked;
392+
};
393+
376394
// Remove item from multiple select
377395
ctrl.removeChoice = function(index){
378396
var removedChoice = ctrl.selected[index];
397+
398+
// if the choice is locked, can't remove it
399+
if(removedChoice._uiSelectChoiceLocked) return;
400+
379401
var locals = {};
380402
locals[ctrl.parserResult.itemName] = removedChoice;
381403

@@ -880,6 +902,7 @@
880902
$select.parseRepeatAttr(attrs.repeat, groupByExp); //Result ready at $select.parserResult
881903

882904
$select.disableChoiceExpression = attrs.uiDisableChoice;
905+
$select.onHighlightCallback = attrs.onHighlight;
883906

884907
if(groupByExp) {
885908
var groups = element.querySelectorAll('.ui-select-choices-group');
@@ -941,6 +964,7 @@
941964
return theme + (multi ? '/match-multiple.tpl.html' : '/match.tpl.html');
942965
},
943966
link: function(scope, element, attrs, $select) {
967+
$select.lockChoiceExpression = attrs.uiLockChoice;
944968
attrs.$observe('placeholder', function(placeholder) {
945969
$select.placeholder = placeholder !== undefined ? placeholder : uiSelectConfig.placeholder;
946970
});

src/select2/match-multiple.tpl.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
-->
66
<span class="ui-select-match">
77
<li class="ui-select-match-item select2-search-choice" ng-repeat="$item in $select.selected"
8-
ng-class="{'select2-search-choice-focus':$select.activeMatchIndex === $index}">
8+
ng-class="{'select2-search-choice-focus':$select.activeMatchIndex === $index, 'select2-locked':$select.isLocked(this, $index)}">
99
<span uis-transclude-append></span>
1010
<a href="javascript:;" class="ui-select-match-close select2-search-choice-close" ng-click="$select.removeChoice($index)" tabindex="-1"></a>
1111
</li>

test/select.spec.js

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ describe('ui-select tests', function() {
255255
expect(isDropdownOpened(el2)).toEqual(true);
256256

257257
var el3 = createUiSelect();
258-
expect(el3.scope().$select.disabled).toEqual(false);
258+
expect(el3.scope().$select.disabled).toBeFalsy();
259259
clickMatch(el3);
260260
expect(isDropdownOpened(el3)).toEqual(true);
261261
});
@@ -727,6 +727,35 @@ describe('ui-select tests', function() {
727727

728728
});
729729

730+
it('should invoke hover callback', function(){
731+
732+
var highlighted;
733+
scope.onHighlightFn = function ($item) {
734+
highlighted = $item;
735+
};
736+
737+
var el = compileTemplate(
738+
'<ui-select on-select="onSelectFn($item, $model)" ng-model="selection.selected"> \
739+
<ui-select-match placeholder="Pick one...">{{$select.selected.name}}</ui-select-match> \
740+
<ui-select-choices on-highlight="onHighlightFn(person)" repeat="person.name as person in people | filter: $select.search"> \
741+
<div ng-bind-html="person.name | highlight: $select.search"></div> \
742+
<div ng-bind-html="person.email | highlight: $select.search"></div> \
743+
</ui-select-choices> \
744+
</ui-select>'
745+
);
746+
747+
expect(highlighted).toBeFalsy();
748+
749+
if (!isDropdownOpened(el)){
750+
openDropdown(el);
751+
}
752+
753+
$(el).find('.ui-select-choices-row div:contains("Samantha")').trigger('mouseover');
754+
scope.$digest();
755+
756+
expect(highlighted).toBe(scope.people[5]);
757+
})
758+
730759
it('should set $item & $model correctly when invoking callback on select and no single prop. binding', function () {
731760

732761
scope.onSelectFn = function ($item, $model, $label) {

0 commit comments

Comments
 (0)