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