MCPcopy Create free account
hub / github.com/OpenReservation/OpenReservation / selectPreLink

Function selectPreLink

OpenReservation/wwwroot/Scripts/angular.js:30396–30458  ·  view source on GitHub ↗
(scope, element, attr, ctrls)

Source from the content-addressed store, hash-verified

30394 };
30395
30396 function selectPreLink(scope, element, attr, ctrls) {
30397 // if ngModel is not defined, we don't need to do anything
30398 var ngModelCtrl = ctrls[1];
30399 if (!ngModelCtrl) return;
30400
30401 var selectCtrl = ctrls[0];
30402
30403 selectCtrl.ngModelCtrl = ngModelCtrl;
30404
30405 // When the selected item(s) changes we delegate getting the value of the select control
30406 // to the `readValue` method, which can be changed if the select can have multiple
30407 // selected values or if the options are being generated by `ngOptions`
30408 element.on('change',
30409 function () {
30410 scope.$apply(function () {
30411 ngModelCtrl.$setViewValue(selectCtrl.readValue());
30412 });
30413 });
30414
30415 // If the select allows multiple values then we need to modify how we read and write
30416 // values from and to the control; also what it means for the value to be empty and
30417 // we have to add an extra watch since ngModel doesn't work well with arrays - it
30418 // doesn't trigger rendering if only an item in the array changes.
30419 if (attr.multiple) {
30420 // Read value now needs to check each option to see if it is selected
30421 selectCtrl.readValue = function readMultipleValue() {
30422 var array = [];
30423 forEach(element.find('option'),
30424 function (option) {
30425 if (option.selected) {
30426 array.push(option.value);
30427 }
30428 });
30429 return array;
30430 };
30431
30432 // Write value now needs to set the selected property of each matching option
30433 selectCtrl.writeValue = function writeMultipleValue(value) {
30434 var items = new HashMap(value);
30435 forEach(element.find('option'),
30436 function (option) {
30437 option.selected = isDefined(items.get(option.value));
30438 });
30439 };
30440
30441 // we have to do it on each watch since ngModel watches reference, but
30442 // we need to work of an array, so we need to see if anything was inserted/removed
30443 var lastView, lastViewRef = NaN;
30444 scope.$watch(function selectMultipleWatch() {
30445 if (lastViewRef === ngModelCtrl.$viewValue && !equals(lastView, ngModelCtrl.$viewValue)) {
30446 lastView = shallowCopy(ngModelCtrl.$viewValue);
30447 ngModelCtrl.$render();
30448 }
30449 lastViewRef = ngModelCtrl.$viewValue;
30450 });
30451
30452 // If we are a multiple select then value is now a collection
30453 // so the meaning of $isEmpty changes

Callers

nothing calls this directly

Calls 4

forEachFunction · 0.85
isDefinedFunction · 0.85
equalsFunction · 0.85
shallowCopyFunction · 0.85

Tested by

no test coverage detected