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

Commit 205fae2

Browse files
committed
Move more code to their specific directives. Using scope events for notifications
1 parent c659a2d commit 205fae2

File tree

5 files changed

+40
-24
lines changed

5 files changed

+40
-24
lines changed

src/bootstrap/select-multiple.tpl.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
autocapitalize="off"
88
spellcheck="false"
99
class="ui-select-search input-xs"
10-
placeholder="{{$select.getPlaceholder()}}"
10+
placeholder="{{$selectMultiple.getPlaceholder()}}"
1111
ng-disabled="$select.disabled"
1212
ng-hide="$select.disabled"
1313
ng-click="$select.activate()"

src/select2/select-multiple.tpl.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
aria-label="{{ $select.baseTitle }}"
1717
aria-activedescendant="ui-select-choices-row-{{ $select.generatedId }}-{{ $select.activeIndex }}"
1818
class="select2-input ui-select-search"
19-
placeholder="{{$select.getPlaceholder()}}"
19+
placeholder="{{$selectMultiple.getPlaceholder()}}"
2020
ng-disabled="$select.disabled"
2121
ng-hide="$select.disabled"
2222
ng-model="$select.search"

src/uiSelectController.js

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,10 @@ uis.controller('uiSelectCtrl',
6161
ctrl.activate = function(initSearchValue, avoidReset) {
6262
if (!ctrl.disabled && !ctrl.open) {
6363
if(!avoidReset) _resetSearchInput();
64-
if (ctrl.focusser) ctrl.focusser.prop('disabled', true); //Will reactivate it on .close()
64+
65+
$scope.$broadcast('uis:activate');
66+
6567
ctrl.open = true;
66-
ctrl.activeMatchIndex = -1;
6768

6869
ctrl.activeIndex = ctrl.activeIndex >= ctrl.items.length ? 0 : ctrl.activeIndex;
6970

@@ -261,24 +262,19 @@ uis.controller('uiSelectCtrl',
261262
}
262263
}
263264

265+
$scope.$broadcast('uis:select', item);
266+
264267
var locals = {};
265268
locals[ctrl.parserResult.itemName] = item;
266269

267-
if(ctrl.multiple) {
268-
ctrl.selected.push(item);
269-
ctrl.sizeSearchInput();
270-
} else {
271-
ctrl.selected = item;
272-
}
273-
274270
$timeout(function(){
275271
ctrl.onSelectCallback($scope, {
276272
$item: item,
277273
$model: ctrl.parserResult.modelMapper($scope, locals)
278274
});
279275
});
280276

281-
if (!ctrl.multiple || ctrl.closeOnSelect) {
277+
if (ctrl.closeOnSelect) {
282278
ctrl.close(skipFocusser);
283279
}
284280
if ($event && $event.type === 'click') {
@@ -294,12 +290,9 @@ uis.controller('uiSelectCtrl',
294290
if (ctrl.ngModel && ctrl.ngModel.$setTouched) ctrl.ngModel.$setTouched();
295291
_resetSearchInput();
296292
ctrl.open = false;
297-
if (!ctrl.multiple){
298-
$timeout(function(){
299-
ctrl.focusser.prop('disabled', false);
300-
if (!skipFocusser) ctrl.focusser[0].focus();
301-
},0,false);
302-
}
293+
294+
$scope.$broadcast('uis:close', skipFocusser);
295+
303296
};
304297

305298
ctrl.setFocus = function(){
@@ -334,12 +327,6 @@ uis.controller('uiSelectCtrl',
334327
return isLocked;
335328
};
336329

337-
ctrl.getPlaceholder = function(){
338-
//Refactor single?
339-
if(ctrl.multiple && ctrl.selected.length) return;
340-
return ctrl.placeholder;
341-
};
342-
343330
var sizeWatch = null;
344331
ctrl.sizeSearchInput = function() {
345332

src/uisMultipleDirective.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ uis.directive('uisMultiple', ['uiSelectMinErr', function(uiSelectMinErr) {
3333

3434
};
3535

36+
ctrl.getPlaceholder = function(){
37+
//Refactor single?
38+
if($select.selected.length) return;
39+
return $select.placeholder;
40+
};
3641

3742

3843
}],
@@ -139,6 +144,15 @@ uis.directive('uisMultiple', ['uiSelectMinErr', function(uiSelectMinErr) {
139144
$select.selected = ngModel.$viewValue;
140145
};
141146

147+
scope.$on('uis:select', function (event, item) {
148+
$select.selected.push(item);
149+
$select.sizeSearchInput();
150+
});
151+
152+
scope.$on('uis:activate', function () {
153+
$select.activeMatchIndex = -1;
154+
});
155+
142156
scope.$watch('$select.disabled', function(newValue, oldValue) {
143157
// As the search input field may now become visible, it may be necessary to recompute its size
144158
if (oldValue && !newValue) $select.sizeSearchInput();

src/uisSingleDirective.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,21 @@ uis.directive('uisSingle', ['$timeout','$compile', function($timeout, $compile)
4949
$select.selected = ngModel.$viewValue;
5050
};
5151

52+
scope.$on('uis:select', function (event, item) {
53+
$select.selected = item;
54+
});
55+
56+
scope.$on('uis:close', function (event, skipFocusser) {
57+
$timeout(function(){
58+
$select.focusser.prop('disabled', false);
59+
if (!skipFocusser) $select.focusser[0].focus();
60+
},0,false);
61+
});
62+
63+
scope.$on('uis:activate', function () {
64+
focusser.prop('disabled', true); //Will reactivate it on .close()
65+
});
66+
5267
//Idea from: https://github.com/ivaynberg/select2/blob/79b5bf6db918d7560bdd959109b7bcfb47edaf43/select2.js#L1954
5368
var focusser = angular.element("<input ng-disabled='$select.disabled' class='ui-select-focusser ui-select-offscreen' type='text' id='{{ $select.focusserId }}' aria-label='{{ $select.focusserTitle }}' aria-haspopup='true' role='button' />");
5469
$compile(focusser)(scope);

0 commit comments

Comments
 (0)