(scope, element, attr, ctrls)
| 34198 | }; |
| 34199 | |
| 34200 | function selectPreLink(scope, element, attr, ctrls) { |
| 34201 | |
| 34202 | var selectCtrl = ctrls[0]; |
| 34203 | var ngModelCtrl = ctrls[1]; |
| 34204 | |
| 34205 | // if ngModel is not defined, we don't need to do anything but set the registerOption |
| 34206 | // function to noop, so options don't get added internally |
| 34207 | if (!ngModelCtrl) { |
| 34208 | selectCtrl.registerOption = noop; |
| 34209 | return; |
| 34210 | } |
| 34211 | |
| 34212 | |
| 34213 | selectCtrl.ngModelCtrl = ngModelCtrl; |
| 34214 | |
| 34215 | // When the selected item(s) changes we delegate getting the value of the select control |
| 34216 | // to the `readValue` method, which can be changed if the select can have multiple |
| 34217 | // selected values or if the options are being generated by `ngOptions` |
| 34218 | element.on('change', function() { |
| 34219 | selectCtrl.removeUnknownOption(); |
| 34220 | scope.$apply(function() { |
| 34221 | ngModelCtrl.$setViewValue(selectCtrl.readValue()); |
| 34222 | }); |
| 34223 | }); |
| 34224 | |
| 34225 | // If the select allows multiple values then we need to modify how we read and write |
| 34226 | // values from and to the control; also what it means for the value to be empty and |
| 34227 | // we have to add an extra watch since ngModel doesn't work well with arrays - it |
| 34228 | // doesn't trigger rendering if only an item in the array changes. |
| 34229 | if (attr.multiple) { |
| 34230 | selectCtrl.multiple = true; |
| 34231 | |
| 34232 | // Read value now needs to check each option to see if it is selected |
| 34233 | selectCtrl.readValue = function readMultipleValue() { |
| 34234 | var array = []; |
| 34235 | forEach(element.find('option'), function(option) { |
| 34236 | if (option.selected && !option.disabled) { |
| 34237 | var val = option.value; |
| 34238 | array.push(val in selectCtrl.selectValueMap ? selectCtrl.selectValueMap[val] : val); |
| 34239 | } |
| 34240 | }); |
| 34241 | return array; |
| 34242 | }; |
| 34243 | |
| 34244 | // Write value now needs to set the selected property of each matching option |
| 34245 | selectCtrl.writeValue = function writeMultipleValue(value) { |
| 34246 | forEach(element.find('option'), function(option) { |
| 34247 | var shouldBeSelected = !!value && (includes(value, option.value) || |
| 34248 | includes(value, selectCtrl.selectValueMap[option.value])); |
| 34249 | var currentlySelected = option.selected; |
| 34250 | |
| 34251 | // Support: IE 9-11 only, Edge 12-15+ |
| 34252 | // In IE and Edge adding options to the selection via shift+click/UP/DOWN |
| 34253 | // will de-select already selected options if "selected" on those options was set |
| 34254 | // more than once (i.e. when the options were already selected) |
| 34255 | // So we only modify the selected property if necessary. |
| 34256 | // Note: this behavior cannot be replicated via unit tests because it only shows in the |
| 34257 | // actual user interface. |
nothing calls this directly
no test coverage detected