| OLD | NEW |
| 1 part of angular.directive; | 1 part of angular.directive; |
| 2 | 2 |
| 3 /** | 3 /** |
| 4 * The form directive listens on submission requests and, depending, | 4 * The form directive listens on submission requests and, depending, |
| 5 * on if an action is set, the form will automatically either allow | 5 * on if an action is set, the form will automatically either allow |
| 6 * or prevent the default browser submission from occurring. | 6 * or prevent the default browser submission from occurring. |
| 7 */ | 7 */ |
| 8 @NgDirective( | 8 @NgDirective( |
| 9 selector: 'form', | 9 selector: 'form', |
| 10 visibility: NgDirective.CHILDREN_VISIBILITY) | 10 visibility: NgDirective.CHILDREN_VISIBILITY) |
| 11 class NgForm { | 11 @NgDirective( |
| 12 dom.Element _element; | 12 selector: 'fieldset', |
| 13 Scope _scope; | 13 visibility: NgDirective.CHILDREN_VISIBILITY) |
| 14 @NgDirective( |
| 15 selector: '.ng-form', |
| 16 visibility: NgDirective.CHILDREN_VISIBILITY) |
| 17 @NgDirective( |
| 18 selector: '[ng-form]', |
| 19 visibility: NgDirective.CHILDREN_VISIBILITY) |
| 20 class NgForm extends NgControl implements NgDetachAware, Map<String, NgModel> { |
| 21 final NgForm _parentForm; |
| 22 final dom.Element _element; |
| 23 final Scope _scope; |
| 14 | 24 |
| 15 NgForm(this._scope, this._element) { | 25 final Map<String, List<NgControl>> currentErrors = new Map<String, List<NgCont
rol>>(); |
| 26 |
| 27 final List<NgControl> _controls = new List<NgControl>(); |
| 28 final Map<String, NgControl> _controlByName = new Map<String, NgControl>(); |
| 29 |
| 30 /** |
| 31 * Instantiates a new instance of NgForm. Upon creation, the instance of the c
lass will |
| 32 * be bound to the formName property on the scope (where formName refers to th
e name |
| 33 * value acquired from the name attribute present on the form DOM element). |
| 34 * |
| 35 * * [scope] - The scope to bind the form instance to. |
| 36 * * [element] - The form DOM element. |
| 37 * * [injector] - An instance of Injector. |
| 38 */ |
| 39 NgForm(this._scope, dom.Element this._element, Injector injector): |
| 40 _parentForm = injector.parent.get(NgForm) |
| 41 { |
| 16 if(!this._element.attributes.containsKey('action')) { | 42 if(!this._element.attributes.containsKey('action')) { |
| 17 this._element.onSubmit.listen((event) { | 43 this._element.onSubmit.listen((event) { |
| 18 event.preventDefault(); | 44 event.preventDefault(); |
| 19 }); | 45 }); |
| 20 } | 46 } |
| 47 |
| 48 this.pristine = true; |
| 21 } | 49 } |
| 50 |
| 51 detach() { |
| 52 for (int i = _controls.length - 1; i >= 0; --i) { |
| 53 removeControl(_controls[i]); |
| 54 } |
| 55 } |
| 56 |
| 57 get element => _element; |
| 58 |
| 59 @NgAttr('name') |
| 60 get name => _name; |
| 61 set name(name) { |
| 62 _name = name; |
| 63 _scope[name] = this; |
| 64 } |
| 65 |
| 66 /** |
| 67 * Sets the validity status of the given control/errorType pair within |
| 68 * the list of controls registered on the form. Depending on the validation |
| 69 * state of the existing controls, this will either change valid to true |
| 70 * or invalid to true depending on if all controls are valid or if one |
| 71 * or more of them is invalid. |
| 72 * |
| 73 * * [control] - The registered control object (see [ngControl]). |
| 74 * * [errorType] - The error associated with the control (e.g. required, url,
number, etc...). |
| 75 * * [isValid] - Whether or not the given error is valid or not (false would m
ean the error is real). |
| 76 */ |
| 77 setValidity(NgControl control, String errorType, bool isValid) { |
| 78 List queue = currentErrors[errorType]; |
| 79 |
| 80 if(isValid) { |
| 81 if(queue != null) { |
| 82 queue.remove(control); |
| 83 if(queue.isEmpty) { |
| 84 currentErrors.remove(errorType); |
| 85 if(currentErrors.isEmpty) { |
| 86 valid = true; |
| 87 } |
| 88 _parentForm.setValidity(this, errorType, true); |
| 89 } |
| 90 } |
| 91 } else { |
| 92 if(queue == null) { |
| 93 queue = new List<NgControl>(); |
| 94 currentErrors[errorType] = queue; |
| 95 _parentForm.setValidity(this, errorType, false); |
| 96 } else if(queue.contains(control)) { |
| 97 return; |
| 98 } |
| 99 |
| 100 queue.add(control); |
| 101 invalid = true; |
| 102 } |
| 103 } |
| 104 |
| 105 //FIXME: fix this reflection bug that shows up when Map is implemented |
| 106 operator []=(String name, value) { |
| 107 if(name == 'name'){ |
| 108 this.name = value; |
| 109 } else { |
| 110 _controlByName[name] = value; |
| 111 } |
| 112 } |
| 113 |
| 114 //FIXME: fix this reflection bug that shows up when Map is implemented |
| 115 operator[](name) { |
| 116 if(name == 'valid') { |
| 117 return valid; |
| 118 } else if(name == 'invalid') { |
| 119 return invalid; |
| 120 } else { |
| 121 return _controlByName[name]; |
| 122 } |
| 123 } |
| 124 |
| 125 /** |
| 126 * Registers a form control into the form for validation. |
| 127 * |
| 128 * * [control] - The form control which will be registered (see [ngControl]). |
| 129 */ |
| 130 addControl(NgControl control) { |
| 131 _controls.add(control); |
| 132 if(control.name != null) { |
| 133 _controlByName[control.name] = control; |
| 134 } |
| 135 } |
| 136 |
| 137 /** |
| 138 * De-registers a form control from the list of controls associated with the f
orm. |
| 139 * |
| 140 * * [control] - The form control which will be de-registered (see [ngControl]
). |
| 141 */ |
| 142 removeControl(NgControl control) { |
| 143 _controls.remove(control); |
| 144 if(control.name != null) { |
| 145 _controlByName.remove(control.name); |
| 146 } |
| 147 } |
| 148 |
| 149 bool get isEmpty => false; |
| 150 bool get isNotEmpty => !isEmpty; |
| 151 get values => null; |
| 152 get keys => null; |
| 153 get length => null; |
| 154 clear() => null; |
| 155 remove(_) => null; |
| 156 containsKey(_) => false; |
| 157 containsValue(_) => false; |
| 158 addAll(_) => null; |
| 159 forEach(_) => null; |
| 160 putIfAbsent(_, __) => null; |
| 22 } | 161 } |
| 162 |
| 163 class NgNullForm implements NgForm { |
| 164 var _name, _dirty, _valid, _invalid, _pristine, _element; |
| 165 var _controls, _scope, _parentForm, _controlName; |
| 166 var currentErrors, _controlByName; |
| 167 dom.Element element; |
| 168 NgNullForm() {} |
| 169 operator[](name) {} |
| 170 operator []=(String name, value) {} |
| 171 addControl(control) {} |
| 172 removeControl(control) {} |
| 173 setValidity(control, String errorType, bool isValid) {} |
| 174 |
| 175 get name => null; |
| 176 set name(name) {} |
| 177 |
| 178 get pristine => null; |
| 179 set pristine(value) {} |
| 180 |
| 181 get dirty => null; |
| 182 set dirty(value) {} |
| 183 |
| 184 get valid => null; |
| 185 set valid(value) {} |
| 186 |
| 187 get invalid => null; |
| 188 set invalid(value) {} |
| 189 |
| 190 bool get isEmpty => false; |
| 191 bool get isNotEmpty => !isEmpty; |
| 192 get values => null; |
| 193 get keys => null; |
| 194 get length => null; |
| 195 clear() => null; |
| 196 remove(_) => null; |
| 197 containsKey(_) => false; |
| 198 containsValue(_) => false; |
| 199 addAll(_) => null; |
| 200 forEach(_) => null; |
| 201 putIfAbsent(_, __) => null; |
| 202 |
| 203 detach() => null; |
| 204 } |
| OLD | NEW |