| 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 publishTypes : const <Type>[NgControl], |
| 10 visibility: NgDirective.CHILDREN_VISIBILITY) | 11 visibility: NgDirective.CHILDREN_VISIBILITY) |
| 11 @NgDirective( | 12 @NgDirective( |
| 12 selector: 'fieldset', | 13 selector: 'fieldset', |
| 14 publishTypes : const <Type>[NgControl], |
| 13 visibility: NgDirective.CHILDREN_VISIBILITY) | 15 visibility: NgDirective.CHILDREN_VISIBILITY) |
| 14 @NgDirective( | 16 @NgDirective( |
| 15 selector: '.ng-form', | 17 selector: '.ng-form', |
| 18 publishTypes : const <Type>[NgControl], |
| 16 visibility: NgDirective.CHILDREN_VISIBILITY) | 19 visibility: NgDirective.CHILDREN_VISIBILITY) |
| 17 @NgDirective( | 20 @NgDirective( |
| 18 selector: '[ng-form]', | 21 selector: '[ng-form]', |
| 22 publishTypes : const <Type>[NgControl], |
| 19 visibility: NgDirective.CHILDREN_VISIBILITY) | 23 visibility: NgDirective.CHILDREN_VISIBILITY) |
| 20 class NgForm extends NgControl implements NgDetachAware, Map<String, NgModel> { | 24 class NgForm extends NgControl implements Map<String, NgControl> { |
| 21 final NgForm _parentForm; | |
| 22 final dom.Element _element; | |
| 23 final Scope _scope; | |
| 24 | |
| 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 /** | 25 /** |
| 31 * Instantiates a new instance of NgForm. Upon creation, the instance of the c
lass will | 26 * Instantiates a new instance of NgForm. Upon creation, the instance of the |
| 32 * be bound to the formName property on the scope (where formName refers to th
e name | 27 * class will be bound to the formName property on the scope (where formName |
| 33 * value acquired from the name attribute present on the form DOM element). | 28 * refers to the name value acquired from the name attribute present on the |
| 29 * form DOM element). |
| 34 * | 30 * |
| 35 * * [scope] - The scope to bind the form instance to. | 31 * * [scope] - The scope to bind the form instance to. |
| 36 * * [element] - The form DOM element. | 32 * * [element] - The form DOM element. |
| 37 * * [injector] - An instance of Injector. | 33 * * [injector] - An instance of Injector. |
| 38 */ | 34 */ |
| 39 NgForm(this._scope, dom.Element this._element, Injector injector): | 35 NgForm(Scope scope, dom.Element element, Injector injector) : |
| 40 _parentForm = injector.parent.get(NgForm) | 36 super(scope, element, injector) { |
| 41 { | 37 |
| 42 if(!this._element.attributes.containsKey('action')) { | 38 if (!element.attributes.containsKey('action')) { |
| 43 this._element.onSubmit.listen((event) { | 39 element.onSubmit.listen((event) { |
| 44 event.preventDefault(); | 40 event.preventDefault(); |
| 41 _scope.broadcast('submitNgControl', valid == null ? false : valid); |
| 42 reset(); |
| 45 }); | 43 }); |
| 46 } | 44 } |
| 47 | |
| 48 this.pristine = true; | |
| 49 } | |
| 50 | |
| 51 detach() { | |
| 52 for (int i = _controls.length - 1; i >= 0; --i) { | |
| 53 removeControl(_controls[i]); | |
| 54 } | |
| 55 } | 45 } |
| 56 | 46 |
| 57 get element => _element; | |
| 58 | |
| 59 @NgAttr('name') | 47 @NgAttr('name') |
| 60 get name => _name; | 48 get name => _name; |
| 61 set name(name) { | 49 set name(value) { |
| 62 _name = name; | 50 super.name = value; |
| 63 _scope[name] = this; | 51 _scope.context[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 } | 52 } |
| 104 | 53 |
| 105 //FIXME: fix this reflection bug that shows up when Map is implemented | 54 //FIXME: fix this reflection bug that shows up when Map is implemented |
| 106 operator []=(String name, value) { | 55 operator []=(String key, value) { |
| 107 if(name == 'name'){ | 56 if (key == 'name') { |
| 108 this.name = value; | 57 name = value; |
| 109 } else { | 58 } else { |
| 110 _controlByName[name] = value; | 59 _controlByName[key] = value; |
| 111 } | 60 } |
| 112 } | 61 } |
| 113 | 62 |
| 114 //FIXME: fix this reflection bug that shows up when Map is implemented | 63 //FIXME: fix this reflection bug that shows up when Map is implemented |
| 115 operator[](name) { | 64 operator[](name) { |
| 116 if(name == 'valid') { | 65 if (name == 'valid') { |
| 117 return valid; | 66 return valid; |
| 118 } else if(name == 'invalid') { | 67 } else if (name == 'invalid') { |
| 119 return invalid; | 68 return invalid; |
| 120 } else { | 69 } else { |
| 121 return _controlByName[name]; | 70 return _controlByName[name]; |
| 122 } | 71 } |
| 123 } | 72 } |
| 124 | 73 |
| 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; | 74 bool get isEmpty => false; |
| 150 bool get isNotEmpty => !isEmpty; | 75 bool get isNotEmpty => !isEmpty; |
| 151 get values => null; | 76 get values => null; |
| 152 get keys => null; | 77 get keys => null; |
| 153 get length => null; | 78 get length => null; |
| 154 clear() => null; | 79 clear() => null; |
| 155 remove(_) => null; | 80 remove(_) => null; |
| 156 containsKey(_) => false; | 81 containsKey(_) => false; |
| 157 containsValue(_) => false; | 82 containsValue(_) => false; |
| 158 addAll(_) => null; | 83 addAll(_) => null; |
| 159 forEach(_) => null; | 84 forEach(_) => null; |
| 160 putIfAbsent(_, __) => null; | 85 putIfAbsent(_, __) => null; |
| 161 } | 86 } |
| 162 | 87 |
| 163 class NgNullForm implements NgForm { | 88 class NgNullForm extends NgNullControl 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() {} | 89 NgNullForm() {} |
| 90 |
| 169 operator[](name) {} | 91 operator[](name) {} |
| 170 operator []=(String name, value) {} | 92 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 | 93 |
| 190 bool get isEmpty => false; | 94 bool get isEmpty => false; |
| 191 bool get isNotEmpty => !isEmpty; | 95 bool get isNotEmpty => true; |
| 192 get values => null; | 96 get values => null; |
| 193 get keys => null; | 97 get keys => null; |
| 194 get length => null; | 98 get length => null; |
| 195 clear() => null; | 99 clear() => null; |
| 196 remove(_) => null; | 100 remove(_) => null; |
| 197 containsKey(_) => false; | 101 containsKey(_) => false; |
| 198 containsValue(_) => false; | 102 containsValue(_) => false; |
| 199 addAll(_) => null; | 103 addAll(_) => null; |
| 200 forEach(_) => null; | 104 forEach(_) => null; |
| 201 putIfAbsent(_, __) => null; | 105 putIfAbsent(_, __) => null; |
| 202 | |
| 203 detach() => null; | |
| 204 } | 106 } |
| OLD | NEW |