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

Function selectPreLink

lib/test/angular/1.7.0/angular.js:34423–34506  ·  view source on GitHub ↗
(scope, element, attr, ctrls)

Source from the content-addressed store, hash-verified

34421 };
34422
34423 function selectPreLink(scope, element, attr, ctrls) {
34424
34425 var selectCtrl = ctrls[0];
34426 var ngModelCtrl = ctrls[1];
34427
34428 // if ngModel is not defined, we don't need to do anything but set the registerOption
34429 // function to noop, so options don't get added internally
34430 if (!ngModelCtrl) {
34431 selectCtrl.registerOption = noop;
34432 return;
34433 }
34434
34435
34436 selectCtrl.ngModelCtrl = ngModelCtrl;
34437
34438 // When the selected item(s) changes we delegate getting the value of the select control
34439 // to the `readValue` method, which can be changed if the select can have multiple
34440 // selected values or if the options are being generated by `ngOptions`
34441 element.on('change', function() {
34442 selectCtrl.removeUnknownOption();
34443 scope.$apply(function() {
34444 ngModelCtrl.$setViewValue(selectCtrl.readValue());
34445 });
34446 });
34447
34448 // If the select allows multiple values then we need to modify how we read and write
34449 // values from and to the control; also what it means for the value to be empty and
34450 // we have to add an extra watch since ngModel doesn't work well with arrays - it
34451 // doesn't trigger rendering if only an item in the array changes.
34452 if (attr.multiple) {
34453 selectCtrl.multiple = true;
34454
34455 // Read value now needs to check each option to see if it is selected
34456 selectCtrl.readValue = function readMultipleValue() {
34457 var array = [];
34458 forEach(element.find('option'), function(option) {
34459 if (option.selected && !option.disabled) {
34460 var val = option.value;
34461 array.push(val in selectCtrl.selectValueMap ? selectCtrl.selectValueMap[val] : val);
34462 }
34463 });
34464 return array;
34465 };
34466
34467 // Write value now needs to set the selected property of each matching option
34468 selectCtrl.writeValue = function writeMultipleValue(value) {
34469 forEach(element.find('option'), function(option) {
34470 var shouldBeSelected = !!value && (includes(value, option.value) ||
34471 includes(value, selectCtrl.selectValueMap[option.value]));
34472 var currentlySelected = option.selected;
34473
34474 // Support: IE 9-11 only, Edge 12-15+
34475 // In IE and Edge adding options to the selection via shift+click/UP/DOWN
34476 // will de-select already selected options if "selected" on those options was set
34477 // more than once (i.e. when the options were already selected)
34478 // So we only modify the selected property if necessary.
34479 // Note: this behavior cannot be replicated via unit tests because it only shows in the
34480 // 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