MCPcopy
hub / github.com/angular-ui/ui-grid / selectPreLink

Function selectPreLink

lib/test/angular/1.8.0/angular.js:35750–35833  ·  view source on GitHub ↗
(scope, element, attr, ctrls)

Source from the content-addressed store, hash-verified

35748 };
35749
35750 function selectPreLink(scope, element, attr, ctrls) {
35751
35752 var selectCtrl = ctrls[0];
35753 var ngModelCtrl = ctrls[1];
35754
35755 // if ngModel is not defined, we don't need to do anything but set the registerOption
35756 // function to noop, so options don't get added internally
35757 if (!ngModelCtrl) {
35758 selectCtrl.registerOption = noop;
35759 return;
35760 }
35761
35762
35763 selectCtrl.ngModelCtrl = ngModelCtrl;
35764
35765 // When the selected item(s) changes we delegate getting the value of the select control
35766 // to the `readValue` method, which can be changed if the select can have multiple
35767 // selected values or if the options are being generated by `ngOptions`
35768 element.on('change', function() {
35769 selectCtrl.removeUnknownOption();
35770 scope.$apply(function() {
35771 ngModelCtrl.$setViewValue(selectCtrl.readValue());
35772 });
35773 });
35774
35775 // If the select allows multiple values then we need to modify how we read and write
35776 // values from and to the control; also what it means for the value to be empty and
35777 // we have to add an extra watch since ngModel doesn't work well with arrays - it
35778 // doesn't trigger rendering if only an item in the array changes.
35779 if (attr.multiple) {
35780 selectCtrl.multiple = true;
35781
35782 // Read value now needs to check each option to see if it is selected
35783 selectCtrl.readValue = function readMultipleValue() {
35784 var array = [];
35785 forEach(element.find('option'), function(option) {
35786 if (option.selected && !option.disabled) {
35787 var val = option.value;
35788 array.push(val in selectCtrl.selectValueMap ? selectCtrl.selectValueMap[val] : val);
35789 }
35790 });
35791 return array;
35792 };
35793
35794 // Write value now needs to set the selected property of each matching option
35795 selectCtrl.writeValue = function writeMultipleValue(value) {
35796 forEach(element.find('option'), function(option) {
35797 var shouldBeSelected = !!value && (includes(value, option.value) ||
35798 includes(value, selectCtrl.selectValueMap[option.value]));
35799 var currentlySelected = option.selected;
35800
35801 // Support: IE 9-11 only, Edge 12-15+
35802 // In IE and Edge adding options to the selection via shift+click/UP/DOWN
35803 // will de-select already selected options if "selected" on those options was set
35804 // more than once (i.e. when the options were already selected)
35805 // So we only modify the selected property if necessary.
35806 // Note: this behavior cannot be replicated via unit tests because it only shows in the
35807 // 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