MCPcopy Index your code
hub / github.com/angular-ui/ui-router / selectPreLink

Function selectPreLink

test/angular/1.7/angular.js:35685–35768  ·  view source on GitHub ↗
(scope, element, attr, ctrls)

Source from the content-addressed store, hash-verified

35683 };
35684
35685 function selectPreLink(scope, element, attr, ctrls) {
35686
35687 var selectCtrl = ctrls[0];
35688 var ngModelCtrl = ctrls[1];
35689
35690 // if ngModel is not defined, we don't need to do anything but set the registerOption
35691 // function to noop, so options don't get added internally
35692 if (!ngModelCtrl) {
35693 selectCtrl.registerOption = noop;
35694 return;
35695 }
35696
35697
35698 selectCtrl.ngModelCtrl = ngModelCtrl;
35699
35700 // When the selected item(s) changes we delegate getting the value of the select control
35701 // to the `readValue` method, which can be changed if the select can have multiple
35702 // selected values or if the options are being generated by `ngOptions`
35703 element.on('change', function() {
35704 selectCtrl.removeUnknownOption();
35705 scope.$apply(function() {
35706 ngModelCtrl.$setViewValue(selectCtrl.readValue());
35707 });
35708 });
35709
35710 // If the select allows multiple values then we need to modify how we read and write
35711 // values from and to the control; also what it means for the value to be empty and
35712 // we have to add an extra watch since ngModel doesn't work well with arrays - it
35713 // doesn't trigger rendering if only an item in the array changes.
35714 if (attr.multiple) {
35715 selectCtrl.multiple = true;
35716
35717 // Read value now needs to check each option to see if it is selected
35718 selectCtrl.readValue = function readMultipleValue() {
35719 var array = [];
35720 forEach(element.find('option'), function(option) {
35721 if (option.selected && !option.disabled) {
35722 var val = option.value;
35723 array.push(val in selectCtrl.selectValueMap ? selectCtrl.selectValueMap[val] : val);
35724 }
35725 });
35726 return array;
35727 };
35728
35729 // Write value now needs to set the selected property of each matching option
35730 selectCtrl.writeValue = function writeMultipleValue(value) {
35731 forEach(element.find('option'), function(option) {
35732 var shouldBeSelected = !!value && (includes(value, option.value) ||
35733 includes(value, selectCtrl.selectValueMap[option.value]));
35734 var currentlySelected = option.selected;
35735
35736 // Support: IE 9-11 only, Edge 12-15+
35737 // In IE and Edge adding options to the selection via shift+click/UP/DOWN
35738 // will de-select already selected options if "selected" on those options was set
35739 // more than once (i.e. when the options were already selected)
35740 // So we only modify the selected property if necessary.
35741 // Note: this behavior cannot be replicated via unit tests because it only shows in the
35742 // actual user interface.

Callers

nothing calls this directly

Calls 5

forEachFunction · 0.70
includesFunction · 0.70
setOptionSelectedStatusFunction · 0.70
equalsFunction · 0.70
shallowCopyFunction · 0.70

Tested by

no test coverage detected