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

Function selectDirective

lib/test/angular/1.4.3/angular.js:28104–28181  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

28102 *
28103 */
28104var selectDirective = function() {
28105
28106 return {
28107 restrict: 'E',
28108 require: ['select', '?ngModel'],
28109 controller: SelectController,
28110 link: function(scope, element, attr, ctrls) {
28111
28112 // if ngModel is not defined, we don't need to do anything
28113 var ngModelCtrl = ctrls[1];
28114 if (!ngModelCtrl) return;
28115
28116 var selectCtrl = ctrls[0];
28117
28118 selectCtrl.ngModelCtrl = ngModelCtrl;
28119
28120 // We delegate rendering to the `writeValue` method, which can be changed
28121 // if the select can have multiple selected values or if the options are being
28122 // generated by `ngOptions`
28123 ngModelCtrl.$render = function() {
28124 selectCtrl.writeValue(ngModelCtrl.$viewValue);
28125 };
28126
28127 // When the selected item(s) changes we delegate getting the value of the select control
28128 // to the `readValue` method, which can be changed if the select can have multiple
28129 // selected values or if the options are being generated by `ngOptions`
28130 element.on('change', function() {
28131 scope.$apply(function() {
28132 ngModelCtrl.$setViewValue(selectCtrl.readValue());
28133 });
28134 });
28135
28136 // If the select allows multiple values then we need to modify how we read and write
28137 // values from and to the control; also what it means for the value to be empty and
28138 // we have to add an extra watch since ngModel doesn't work well with arrays - it
28139 // doesn't trigger rendering if only an item in the array changes.
28140 if (attr.multiple) {
28141
28142 // Read value now needs to check each option to see if it is selected
28143 selectCtrl.readValue = function readMultipleValue() {
28144 var array = [];
28145 forEach(element.find('option'), function(option) {
28146 if (option.selected) {
28147 array.push(option.value);
28148 }
28149 });
28150 return array;
28151 };
28152
28153 // Write value now needs to set the selected property of each matching option
28154 selectCtrl.writeValue = function writeMultipleValue(value) {
28155 var items = new HashMap(value);
28156 forEach(element.find('option'), function(option) {
28157 option.selected = isDefined(items.get(option.value));
28158 });
28159 };
28160
28161 // we have to do it on each watch since ngModel watches reference, but

Callers

nothing calls this directly

Calls 4

forEachFunction · 0.70
isDefinedFunction · 0.70
equalsFunction · 0.70
shallowCopyFunction · 0.70

Tested by

no test coverage detected