| OLD | NEW |
| 1 part of angular.directive; | 1 part of angular.directive; |
| 2 | 2 |
| 3 abstract class NgControl { | 3 abstract class NgControl implements NgDetachAware { |
| 4 static const NG_VALID_CLASS = "ng-valid"; | 4 static const NG_VALID_CLASS = "ng-valid"; |
| 5 static const NG_INVALID_CLASS = "ng-invalid"; | 5 static const NG_INVALID_CLASS = "ng-invalid"; |
| 6 static const NG_PRISTINE_CLASS = "ng-pristine"; | 6 static const NG_PRISTINE_CLASS = "ng-pristine"; |
| 7 static const NG_DIRTY_CLASS = "ng-dirty"; | 7 static const NG_DIRTY_CLASS = "ng-dirty"; |
| 8 static const NG_TOUCHED_CLASS = "ng-touched"; |
| 9 static const NG_UNTOUCHED_CLASS = "ng-untouched"; |
| 10 static const NG_SUBMIT_VALID_CLASS = "ng-submit-valid"; |
| 11 static const NG_SUBMIT_INVALID_CLASS = "ng-submit-invalid"; |
| 8 | 12 |
| 9 String _name; | 13 String _name; |
| 10 bool _dirty; | 14 bool _dirty; |
| 11 bool _pristine; | 15 bool _pristine; |
| 12 bool _valid; | 16 bool _valid; |
| 13 bool _invalid; | 17 bool _invalid; |
| 18 bool _touched; |
| 19 bool _untouched; |
| 20 bool _submit_valid; |
| 14 | 21 |
| 15 get element => null; | 22 final Scope _scope; |
| 23 final NgControl _parentControl; |
| 24 dom.Element _element; |
| 25 |
| 26 final Map<String, List<NgControl>> errors = new Map<String, List<NgControl>>
(); |
| 27 final List<NgControl> _controls = new List<NgControl>(); |
| 28 final Map<String, NgControl> _controlByName = new Map<String, NgControl>(); |
| 29 |
| 30 NgControl(Scope this._scope, dom.Element this._element, Injector injector) |
| 31 : _parentControl = injector.parent.get(NgControl) |
| 32 { |
| 33 pristine = true; |
| 34 untouched = true; |
| 35 |
| 36 _scope.on('submitNgControl').listen((e) => _onSubmit(e.data)); |
| 37 } |
| 38 |
| 39 detach() { |
| 40 for (int i = _controls.length - 1; i >= 0; --i) { |
| 41 removeControl(_controls[i]); |
| 42 } |
| 43 } |
| 44 |
| 45 reset() { |
| 46 _scope.broadcast('resetNgModel'); |
| 47 untouched = true; |
| 48 } |
| 49 |
| 50 _onSubmit(bool valid) { |
| 51 if (valid) { |
| 52 _submit_valid = true; |
| 53 element.classes..add(NG_SUBMIT_VALID_CLASS)..remove(NG_SUBMIT_INVALID_CLAS
S); |
| 54 } else { |
| 55 _submit_valid = false; |
| 56 element.classes..add(NG_SUBMIT_INVALID_CLASS)..remove(NG_SUBMIT_VALID_CLAS
S); |
| 57 } |
| 58 } |
| 59 |
| 60 get submitted => _submit_valid != null; |
| 61 get valid_submit => _submit_valid == true; |
| 62 get invalid_submit => _submit_valid == false; |
| 16 | 63 |
| 17 get name => _name; | 64 get name => _name; |
| 18 set name(name) => _name = name; | 65 set name(value) { |
| 66 _name = value; |
| 67 _parentControl.addControl(this); |
| 68 } |
| 69 |
| 70 get element => _element; |
| 19 | 71 |
| 20 get pristine => _pristine; | 72 get pristine => _pristine; |
| 21 set pristine(value) { | 73 set pristine(value) { |
| 22 _pristine = true; | 74 _pristine = true; |
| 23 _dirty = false; | 75 _dirty = false; |
| 24 | 76 |
| 25 element.classes.remove(NG_DIRTY_CLASS); | 77 element.classes..remove(NG_DIRTY_CLASS)..add(NG_PRISTINE_CLASS); |
| 26 element.classes.add(NG_PRISTINE_CLASS); | |
| 27 } | 78 } |
| 28 | 79 |
| 29 get dirty => _dirty; | 80 get dirty => _dirty; |
| 30 set dirty(value) { | 81 set dirty(value) { |
| 31 _dirty = true; | 82 _dirty = true; |
| 32 _pristine = false; | 83 _pristine = false; |
| 33 | 84 |
| 34 element.classes.remove(NG_PRISTINE_CLASS); | 85 element.classes..remove(NG_PRISTINE_CLASS)..add(NG_DIRTY_CLASS); |
| 35 element.classes.add(NG_DIRTY_CLASS); | 86 |
| 87 //as soon as one of the controls/models is modified |
| 88 //then all of the parent controls are dirty as well |
| 89 _parentControl.dirty = true; |
| 36 } | 90 } |
| 37 | 91 |
| 38 get valid => _valid; | 92 get valid => _valid; |
| 39 set valid(value) { | 93 set valid(value) { |
| 40 _invalid = false; | 94 _invalid = false; |
| 41 _valid = true; | 95 _valid = true; |
| 42 | 96 |
| 43 element.classes.remove(NG_INVALID_CLASS); | 97 element.classes..remove(NG_INVALID_CLASS)..add(NG_VALID_CLASS); |
| 44 element.classes.add(NG_VALID_CLASS); | |
| 45 } | 98 } |
| 46 | 99 |
| 47 get invalid => _invalid; | 100 get invalid => _invalid; |
| 48 set invalid(value) { | 101 set invalid(value) { |
| 49 _valid = false; | 102 _valid = false; |
| 50 _invalid = true; | 103 _invalid = true; |
| 51 | 104 |
| 52 element.classes.remove(NG_VALID_CLASS); | 105 element.classes..remove(NG_VALID_CLASS)..add(NG_INVALID_CLASS); |
| 53 element.classes.add(NG_INVALID_CLASS); | |
| 54 } | 106 } |
| 55 | 107 |
| 108 get touched => _touched; |
| 109 set touched(value) { |
| 110 _touched = true; |
| 111 _untouched = false; |
| 112 |
| 113 element.classes..remove(NG_UNTOUCHED_CLASS)..add(NG_TOUCHED_CLASS); |
| 114 |
| 115 //as soon as one of the controls/models is touched |
| 116 //then all of the parent controls are touched as well |
| 117 _parentControl.touched = true; |
| 118 } |
| 119 |
| 120 get untouched => _untouched; |
| 121 set untouched(value) { |
| 122 _touched = false; |
| 123 _untouched = true; |
| 124 element.classes..remove(NG_TOUCHED_CLASS)..add(NG_UNTOUCHED_CLASS); |
| 125 } |
| 126 |
| 127 /** |
| 128 * Registers a form control into the form for validation. |
| 129 * |
| 130 * * [control] - The form control which will be registered (see [ngControl]). |
| 131 */ |
| 132 addControl(NgControl control) { |
| 133 _controls.add(control); |
| 134 if (control.name != null) { |
| 135 _controlByName[control.name] = control; |
| 136 } |
| 137 } |
| 138 |
| 139 /** |
| 140 * De-registers a form control from the list of controls associated with the |
| 141 * form. |
| 142 * |
| 143 * * [control] - The form control which will be de-registered (see |
| 144 * [ngControl]). |
| 145 */ |
| 146 removeControl(NgControl control) { |
| 147 _controls.remove(control); |
| 148 if (control.name != null) { |
| 149 _controlByName.remove(control.name); |
| 150 } |
| 151 } |
| 152 |
| 153 /** |
| 154 * Sets the validity status of the given control/errorType pair within |
| 155 * the list of controls registered on the form. Depending on the validation |
| 156 * state of the existing controls, this will either change valid to true |
| 157 * or invalid to true depending on if all controls are valid or if one |
| 158 * or more of them is invalid. |
| 159 * |
| 160 * * [control] - The registered control object (see [ngControl]). |
| 161 * * [errorType] - The error associated with the control (e.g. required, url, |
| 162 * number, etc...). |
| 163 * * [isValid] - Whether the given error is valid or not (false would mean the |
| 164 * error is real). |
| 165 */ |
| 166 updateControlValidity(NgControl control, String errorType, bool isValid) { |
| 167 List queue = errors[errorType]; |
| 168 |
| 169 if (isValid) { |
| 170 if (queue != null) { |
| 171 queue.remove(control); |
| 172 if (queue.isEmpty) { |
| 173 errors.remove(errorType); |
| 174 _parentControl.updateControlValidity(this, errorType, true); |
| 175 } |
| 176 } |
| 177 if (errors.isEmpty) { |
| 178 valid = true; |
| 179 } |
| 180 } else { |
| 181 if (queue == null) { |
| 182 queue = new List<NgControl>(); |
| 183 errors[errorType] = queue; |
| 184 _parentControl.updateControlValidity(this, errorType, false); |
| 185 } else if (queue.contains(control)) return; |
| 186 |
| 187 queue.add(control); |
| 188 invalid = true; |
| 189 } |
| 190 } |
| 56 } | 191 } |
| 192 |
| 193 class NgNullControl implements NgControl { |
| 194 var _name, _dirty, _valid, _invalid, _submit_valid, _pristine, _element; |
| 195 var _touched, _untouched; |
| 196 var _controls, _scope, _parentControl, _controlName; |
| 197 var errors, _controlByName; |
| 198 dom.Element element; |
| 199 |
| 200 NgNullControl() {} |
| 201 _onSubmit(bool valid) {} |
| 202 |
| 203 addControl(control) {} |
| 204 removeControl(control) {} |
| 205 updateControlValidity(NgControl control, String errorType, bool isValid) {} |
| 206 |
| 207 get name => null; |
| 208 set name(name) {} |
| 209 |
| 210 get submitted => null; |
| 211 get valid_submit => null; |
| 212 get invalid_submit => null; |
| 213 |
| 214 get pristine => null; |
| 215 set pristine(value) {} |
| 216 |
| 217 get dirty => null; |
| 218 set dirty(value) {} |
| 219 |
| 220 get valid => null; |
| 221 set valid(value) {} |
| 222 |
| 223 get invalid => null; |
| 224 set invalid(value) {} |
| 225 |
| 226 get touched => null; |
| 227 set touched(value) {} |
| 228 |
| 229 get untouched => null; |
| 230 set untouched(value) {} |
| 231 |
| 232 reset() => null; |
| 233 detach() => null; |
| 234 |
| 235 } |
| OLD | NEW |