Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(992)

Unified Diff: third_party/pkg/angular/lib/directive/ng_form.dart

Issue 180843004: Revert revision 33053 (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 6 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: third_party/pkg/angular/lib/directive/ng_form.dart
===================================================================
--- third_party/pkg/angular/lib/directive/ng_form.dart (revision 33054)
+++ third_party/pkg/angular/lib/directive/ng_form.dart (working copy)
@@ -7,70 +7,145 @@
*/
@NgDirective(
selector: 'form',
- publishTypes : const <Type>[NgControl],
visibility: NgDirective.CHILDREN_VISIBILITY)
@NgDirective(
selector: 'fieldset',
- publishTypes : const <Type>[NgControl],
visibility: NgDirective.CHILDREN_VISIBILITY)
@NgDirective(
selector: '.ng-form',
- publishTypes : const <Type>[NgControl],
visibility: NgDirective.CHILDREN_VISIBILITY)
@NgDirective(
selector: '[ng-form]',
- publishTypes : const <Type>[NgControl],
visibility: NgDirective.CHILDREN_VISIBILITY)
-class NgForm extends NgControl implements Map<String, NgControl> {
+class NgForm extends NgControl implements NgDetachAware, Map<String, NgModel> {
+ final NgForm _parentForm;
+ final dom.Element _element;
+ final Scope _scope;
+
+ final Map<String, List<NgControl>> currentErrors = new Map<String, List<NgControl>>();
+
+ final List<NgControl> _controls = new List<NgControl>();
+ final Map<String, NgControl> _controlByName = new Map<String, NgControl>();
+
/**
- * Instantiates a new instance of NgForm. Upon creation, the instance of the
- * class will be bound to the formName property on the scope (where formName
- * refers to the name value acquired from the name attribute present on the
- * form DOM element).
+ * Instantiates a new instance of NgForm. Upon creation, the instance of the class will
+ * be bound to the formName property on the scope (where formName refers to the name
+ * value acquired from the name attribute present on the form DOM element).
*
* * [scope] - The scope to bind the form instance to.
* * [element] - The form DOM element.
* * [injector] - An instance of Injector.
*/
- NgForm(Scope scope, dom.Element element, Injector injector) :
- super(scope, element, injector) {
-
- if (!element.attributes.containsKey('action')) {
- element.onSubmit.listen((event) {
+ NgForm(this._scope, dom.Element this._element, Injector injector):
+ _parentForm = injector.parent.get(NgForm)
+ {
+ if(!this._element.attributes.containsKey('action')) {
+ this._element.onSubmit.listen((event) {
event.preventDefault();
- _scope.broadcast('submitNgControl', valid == null ? false : valid);
- reset();
});
}
+
+ this.pristine = true;
}
+ detach() {
+ for (int i = _controls.length - 1; i >= 0; --i) {
+ removeControl(_controls[i]);
+ }
+ }
+
+ get element => _element;
+
@NgAttr('name')
get name => _name;
- set name(value) {
- super.name = value;
- _scope.context[name] = this;
+ set name(name) {
+ _name = name;
+ _scope[name] = this;
}
+ /**
+ * Sets the validity status of the given control/errorType pair within
+ * the list of controls registered on the form. Depending on the validation
+ * state of the existing controls, this will either change valid to true
+ * or invalid to true depending on if all controls are valid or if one
+ * or more of them is invalid.
+ *
+ * * [control] - The registered control object (see [ngControl]).
+ * * [errorType] - The error associated with the control (e.g. required, url, number, etc...).
+ * * [isValid] - Whether or not the given error is valid or not (false would mean the error is real).
+ */
+ setValidity(NgControl control, String errorType, bool isValid) {
+ List queue = currentErrors[errorType];
+
+ if(isValid) {
+ if(queue != null) {
+ queue.remove(control);
+ if(queue.isEmpty) {
+ currentErrors.remove(errorType);
+ if(currentErrors.isEmpty) {
+ valid = true;
+ }
+ _parentForm.setValidity(this, errorType, true);
+ }
+ }
+ } else {
+ if(queue == null) {
+ queue = new List<NgControl>();
+ currentErrors[errorType] = queue;
+ _parentForm.setValidity(this, errorType, false);
+ } else if(queue.contains(control)) {
+ return;
+ }
+
+ queue.add(control);
+ invalid = true;
+ }
+ }
+
//FIXME: fix this reflection bug that shows up when Map is implemented
- operator []=(String key, value) {
- if (key == 'name') {
- name = value;
+ operator []=(String name, value) {
+ if(name == 'name'){
+ this.name = value;
} else {
- _controlByName[key] = value;
+ _controlByName[name] = value;
}
}
//FIXME: fix this reflection bug that shows up when Map is implemented
operator[](name) {
- if (name == 'valid') {
+ if(name == 'valid') {
return valid;
- } else if (name == 'invalid') {
+ } else if(name == 'invalid') {
return invalid;
} else {
return _controlByName[name];
}
}
+ /**
+ * Registers a form control into the form for validation.
+ *
+ * * [control] - The form control which will be registered (see [ngControl]).
+ */
+ addControl(NgControl control) {
+ _controls.add(control);
+ if(control.name != null) {
+ _controlByName[control.name] = control;
+ }
+ }
+
+ /**
+ * De-registers a form control from the list of controls associated with the form.
+ *
+ * * [control] - The form control which will be de-registered (see [ngControl]).
+ */
+ removeControl(NgControl control) {
+ _controls.remove(control);
+ if(control.name != null) {
+ _controlByName.remove(control.name);
+ }
+ }
+
bool get isEmpty => false;
bool get isNotEmpty => !isEmpty;
get values => null;
@@ -85,14 +160,35 @@
putIfAbsent(_, __) => null;
}
-class NgNullForm extends NgNullControl implements NgForm {
+class NgNullForm implements NgForm {
+ var _name, _dirty, _valid, _invalid, _pristine, _element;
+ var _controls, _scope, _parentForm, _controlName;
+ var currentErrors, _controlByName;
+ dom.Element element;
NgNullForm() {}
-
operator[](name) {}
operator []=(String name, value) {}
+ addControl(control) {}
+ removeControl(control) {}
+ setValidity(control, String errorType, bool isValid) {}
+ get name => null;
+ set name(name) {}
+
+ get pristine => null;
+ set pristine(value) {}
+
+ get dirty => null;
+ set dirty(value) {}
+
+ get valid => null;
+ set valid(value) {}
+
+ get invalid => null;
+ set invalid(value) {}
+
bool get isEmpty => false;
- bool get isNotEmpty => true;
+ bool get isNotEmpty => !isEmpty;
get values => null;
get keys => null;
get length => null;
@@ -103,4 +199,6 @@
addAll(_) => null;
forEach(_) => null;
putIfAbsent(_, __) => null;
+
+ detach() => null;
}
« no previous file with comments | « third_party/pkg/angular/lib/directive/ng_events.dart ('k') | third_party/pkg/angular/lib/directive/ng_if.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698