(scope, element, attr, ctrls)
| 33509 | }; |
| 33510 | |
| 33511 | function selectPreLink(scope, element, attr, ctrls) { |
| 33512 | |
| 33513 | var selectCtrl = ctrls[0]; |
| 33514 | var ngModelCtrl = ctrls[1]; |
| 33515 | |
| 33516 | // if ngModel is not defined, we don't need to do anything but set the registerOption |
| 33517 | // function to noop, so options don't get added internally |
| 33518 | if (!ngModelCtrl) { |
| 33519 | selectCtrl.registerOption = noop; |
| 33520 | return; |
| 33521 | } |
| 33522 | |
| 33523 | |
| 33524 | selectCtrl.ngModelCtrl = ngModelCtrl; |
| 33525 | |
| 33526 | // When the selected item(s) changes we delegate getting the value of the select control |
| 33527 | // to the `readValue` method, which can be changed if the select can have multiple |
| 33528 | // selected values or if the options are being generated by `ngOptions` |
| 33529 | element.on('change', function() { |
| 33530 | selectCtrl.removeUnknownOption(); |
| 33531 | scope.$apply(function() { |
| 33532 | ngModelCtrl.$setViewValue(selectCtrl.readValue()); |
| 33533 | }); |
| 33534 | }); |
| 33535 | |
| 33536 | // If the select allows multiple values then we need to modify how we read and write |
| 33537 | // values from and to the control; also what it means for the value to be empty and |
| 33538 | // we have to add an extra watch since ngModel doesn't work well with arrays - it |
| 33539 | // doesn't trigger rendering if only an item in the array changes. |
| 33540 | if (attr.multiple) { |
| 33541 | selectCtrl.multiple = true; |
| 33542 | |
| 33543 | // Read value now needs to check each option to see if it is selected |
| 33544 | selectCtrl.readValue = function readMultipleValue() { |
| 33545 | var array = []; |
| 33546 | forEach(element.find('option'), function(option) { |
| 33547 | if (option.selected && !option.disabled) { |
| 33548 | var val = option.value; |
| 33549 | array.push(val in selectCtrl.selectValueMap ? selectCtrl.selectValueMap[val] : val); |
| 33550 | } |
| 33551 | }); |
| 33552 | return array; |
| 33553 | }; |
| 33554 | |
| 33555 | // Write value now needs to set the selected property of each matching option |
| 33556 | selectCtrl.writeValue = function writeMultipleValue(value) { |
| 33557 | forEach(element.find('option'), function(option) { |
| 33558 | var shouldBeSelected = !!value && (includes(value, option.value) || |
| 33559 | includes(value, selectCtrl.selectValueMap[option.value])); |
| 33560 | var currentlySelected = option.selected; |
| 33561 | |
| 33562 | // Support: IE 9-11 only, Edge 12-15+ |
| 33563 | // In IE and Edge adding options to the selection via shift+click/UP/DOWN |
| 33564 | // will de-select already selected options if "selected" on those options was set |
| 33565 | // more than once (i.e. when the options were already selected) |
| 33566 | // So we only modify the selected property if necessary. |
| 33567 | // Note: this behavior cannot be replicated via unit tests because it only shows in the |
| 33568 | // actual user interface. |
nothing calls this directly
no test coverage detected