|
183 | 183 | var repeat = RepeatParser.parse(repeatAttr),
|
184 | 184 | setItemsFn = groupByExp ? updateGroups : setPlainItems;
|
185 | 185 |
|
| 186 | + ctrl.isGrouped = !!groupByExp; |
186 | 187 | ctrl.itemProperty = repeat.lhs;
|
187 | 188 |
|
188 | 189 | // See https://github.com/angular/angular.js/blob/v1.2.15/src/ng/directive/ngRepeat.js#L259
|
|
308 | 309 | // See https://github.com/ivaynberg/select2/blob/3.4.6/select2.js#L1431
|
309 | 310 | function _ensureHighlightVisible() {
|
310 | 311 | var container = $element.querySelectorAll('.ui-select-choices-content');
|
311 |
| - var rows = container.querySelectorAll('.ui-select-choices-row'); |
312 |
| - if (rows.length < 1) { |
313 |
| - throw uiSelectMinErr('rows', "Expected multiple .ui-select-choices-row but got '{0}'.", rows.length); |
| 312 | + var choices = container.querySelectorAll('.ui-select-choices-row'); |
| 313 | + if (choices.length < 1) { |
| 314 | + throw uiSelectMinErr('choices', "Expected multiple .ui-select-choices-row but got '{0}'.", choices.length); |
314 | 315 | }
|
315 | 316 |
|
316 |
| - var highlighted = rows[ctrl.activeIndex]; |
| 317 | + var highlighted = choices[ctrl.activeIndex]; |
317 | 318 | var posY = highlighted.offsetTop + highlighted.clientHeight - container[0].scrollTop;
|
318 | 319 | var height = container[0].offsetHeight;
|
319 | 320 |
|
320 | 321 | if (posY > height) {
|
321 | 322 | container[0].scrollTop += posY - height;
|
322 | 323 | } else if (posY < highlighted.clientHeight) {
|
323 |
| - container[0].scrollTop -= highlighted.clientHeight - posY; |
| 324 | + if (ctrl.isGrouped && ctrl.activeIndex === 0) |
| 325 | + container[0].scrollTop = 0; //To make group header visible when going all the way up |
| 326 | + else |
| 327 | + container[0].scrollTop -= highlighted.clientHeight - posY; |
324 | 328 | }
|
325 | 329 | }
|
326 | 330 |
|
|
533 | 537 | var groupByExp = tAttrs.groupBy;
|
534 | 538 | return function link(scope, element, attrs, $select, transcludeFn) {
|
535 | 539 |
|
| 540 | + var choices; |
| 541 | + |
536 | 542 | if(groupByExp) {
|
537 |
| - var groups = element.querySelectorAll('.ui-select-choices-group'); |
538 |
| - groups.attr('ng-repeat', RepeatParser.getGroupNgRepeatExpression()); |
539 |
| - } |
| 543 | + var group = element.querySelectorAll('.ui-select-choices-group'); |
| 544 | + if (group.length !== 1) { |
| 545 | + throw uiSelectMinErr('rows', "Expected 1 .ui-select-choices-group but got '{0}'.", group.length); |
| 546 | + } |
| 547 | + group.attr('ng-repeat', RepeatParser.getGroupNgRepeatExpression()); |
| 548 | + |
| 549 | + choices = group.querySelectorAll('.ui-select-choices-row'); |
| 550 | + if (choices.length !== 1) { |
| 551 | + throw uiSelectMinErr('rows', "Expected 1 .ui-select-choices-row but got '{0}'.", choices.length); |
| 552 | + } |
| 553 | + |
| 554 | + }else{ |
| 555 | + |
| 556 | + choices = element.querySelectorAll('.ui-select-choices-row'); |
| 557 | + if (choices.length !== 2) { |
| 558 | + throw uiSelectMinErr('rows', "Expected 2 .ui-select-choices-row but got '{0}'.", choices.length); |
| 559 | + } |
540 | 560 |
|
541 |
| - var choices = element.querySelectorAll('.ui-select-choices-row'); |
542 |
| - if (choices.length !== 1) { |
543 |
| - throw uiSelectMinErr('rows', "Expected 1 .ui-select-choices-row but got '{0}'.", choices.length); |
544 | 561 | }
|
545 | 562 |
|
546 | 563 | choices.attr('ng-repeat', RepeatParser.getNgRepeatExpression(repeat.lhs, '$select.items', repeat.trackByExp, groupByExp))
|
547 | 564 | .attr('ng-mouseenter', '$select.setActiveItem('+repeat.lhs+')')
|
548 | 565 | .attr('ng-click', '$select.select(' + repeat.lhs + ')');
|
549 | 566 |
|
| 567 | + //Remove unused .ui-select-choices-row element (simple/group) to avoid problems when _ensureHighlightVisible() |
| 568 | + //We aren't using ngIf since content at element won't be ready when getting at current link fn |
| 569 | + var otherRow = element.querySelectorAll('.ui-select-choices-row'); |
| 570 | + angular.forEach(otherRow, function(row){ |
| 571 | + if (choices[0] !== row) row.remove(); |
| 572 | + }); |
550 | 573 |
|
551 | 574 | transcludeFn(function(clone) {
|
552 |
| - var rowsInner = element.querySelectorAll('.ui-select-choices-row-inner'); |
| 575 | + var rowsInner = choices.querySelectorAll('.ui-select-choices-row-inner'); |
553 | 576 | if (rowsInner.length !== 1)
|
554 | 577 | throw uiSelectMinErr('rows', "Expected 1 .ui-select-choices-row-inner but got '{0}'.", rowsInner.length);
|
555 | 578 |
|
|
0 commit comments