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

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

Issue 256553002: Revert "Update all Angular libs (run update_all.sh)." (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 years, 8 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_control.dart
diff --git a/third_party/pkg/angular/lib/directive/ng_control.dart b/third_party/pkg/angular/lib/directive/ng_control.dart
index c986b64a41f482dd483d2468b0a4b91973db82fb..3c6f9690c4ab112743a360bd0821dd64dce00dfd 100644
--- a/third_party/pkg/angular/lib/directive/ng_control.dart
+++ b/third_party/pkg/angular/lib/directive/ng_control.dart
@@ -1,327 +1,235 @@
part of angular.directive;
-/**
- * The NgControl class is a super-class for handling info and error states between
- * inner controls and models. NgControl will automatically apply the associated CSS
- * classes for the error and info states that are applied as well as status flags.
- * NgControl is used with the form and fieldset as well as all other directives that
- * are used for user input with NgModel.
- */
-abstract class NgControl implements AttachAware, DetachAware {
- static const NG_VALID = "ng-valid";
- static const NG_INVALID = "ng-invalid";
- static const NG_PRISTINE = "ng-pristine";
- static const NG_DIRTY = "ng-dirty";
- static const NG_TOUCHED = "ng-touched";
- static const NG_UNTOUCHED = "ng-untouched";
- static const NG_SUBMIT_VALID = "ng-submit-valid";
- static const NG_SUBMIT_INVALID = "ng-submit-invalid";
+abstract class NgControl implements NgDetachAware {
+ static const NG_VALID_CLASS = "ng-valid";
+ static const NG_INVALID_CLASS = "ng-invalid";
+ static const NG_PRISTINE_CLASS = "ng-pristine";
+ static const NG_DIRTY_CLASS = "ng-dirty";
+ static const NG_TOUCHED_CLASS = "ng-touched";
+ static const NG_UNTOUCHED_CLASS = "ng-untouched";
+ static const NG_SUBMIT_VALID_CLASS = "ng-submit-valid";
+ static const NG_SUBMIT_INVALID_CLASS = "ng-submit-invalid";
String _name;
- bool _submitValid;
-
+ bool _dirty;
+ bool _pristine;
+ bool _valid;
+ bool _invalid;
+ bool _touched;
+ bool _untouched;
+ bool _submit_valid;
+
+ final Scope _scope;
final NgControl _parentControl;
- final Animate _animate;
- final NgElement _element;
-
- final _controls = new List<NgControl>();
- final _controlByName = new Map<String, List<NgControl>>();
+ dom.Element _element;
- /**
- * The list of errors present on the control represented by an error name and
- * an inner control instance.
- */
- final errorStates = new Map<String, Set<NgControl>>();
-
- /**
- * The list of info messages present on the control represented by an state name and
- * an inner control instance.
- */
- final infoStates = new Map<String, Set<NgControl>>();
+ final Map<String, List<NgControl>> errors = new Map<String, List<NgControl>>();
+ final List<NgControl> _controls = new List<NgControl>();
+ final Map<String, NgControl> _controlByName = new Map<String, NgControl>();
- NgControl(NgElement this._element, Injector injector,
- Animate this._animate)
- : _parentControl = injector.parent.get(NgControl);
+ NgControl(Scope this._scope, dom.Element this._element, Injector injector)
+ : _parentControl = injector.parent.get(NgControl)
+ {
+ pristine = true;
+ untouched = true;
- @override
- void attach() {
- _parentControl.addControl(this);
+ _scope.on('submitNgControl').listen((e) => _onSubmit(e.data));
}
- @override
- void detach() {
- _parentControl..removeStates(this)..removeControl(this);
+ detach() {
+ for (int i = _controls.length - 1; i >= 0; --i) {
+ removeControl(_controls[i]);
+ }
}
- /**
- * Resets the form and inner models to their pristine state.
- */
- void reset() {
- _controls.forEach((control) {
- control.reset();
- });
+ reset() {
+ _scope.broadcast('resetNgModel');
+ untouched = true;
}
- void onSubmit(bool valid) {
+ _onSubmit(bool valid) {
if (valid) {
- _submitValid = true;
- element..addClass(NG_SUBMIT_VALID)..removeClass(NG_SUBMIT_INVALID);
+ _submit_valid = true;
+ element.classes..add(NG_SUBMIT_VALID_CLASS)..remove(NG_SUBMIT_INVALID_CLASS);
} else {
- _submitValid = false;
- element..addClass(NG_SUBMIT_INVALID)..removeClass(NG_SUBMIT_VALID);
+ _submit_valid = false;
+ element.classes..add(NG_SUBMIT_INVALID_CLASS)..remove(NG_SUBMIT_VALID_CLASS);
}
- _controls.forEach((control) {
- control.onSubmit(valid);
- });
}
- NgControl get parentControl => _parentControl;
+ get submitted => _submit_valid != null;
+ get valid_submit => _submit_valid == true;
+ get invalid_submit => _submit_valid == false;
- /**
- * Whether or not the form has been submitted yet.
- */
- bool get submitted => _submitValid != null;
-
- /**
- * Whether or not the form was valid when last submitted.
- */
- bool get validSubmit => _submitValid == true;
-
- /**
- * Whether or not the form was invalid when last submitted.
- */
- bool get invalidSubmit => _submitValid == false;
-
- String get name => _name;
- void set name(value) {
+ get name => _name;
+ set name(value) {
_name = value;
+ _parentControl.addControl(this);
}
- /**
- * Whether or not the form was invalid when last submitted.
- */
- NgElement get element => _element;
+ get element => _element;
- /**
- * A control is considered valid if all inner models are valid.
- */
- bool get valid => !invalid;
+ get pristine => _pristine;
+ set pristine(value) {
+ _pristine = true;
+ _dirty = false;
- /**
- * A control is considered invalid if any inner models are invalid.
- */
- bool get invalid => errorStates.isNotEmpty;
+ element.classes..remove(NG_DIRTY_CLASS)..add(NG_PRISTINE_CLASS);
+ }
- /**
- * Whether or not the control's or model's data has not been changed.
- */
- bool get pristine => !dirty;
+ get dirty => _dirty;
+ set dirty(value) {
+ _dirty = true;
+ _pristine = false;
- /**
- * Whether or not the control's or model's data has been changed.
- */
- bool get dirty => infoStates.containsKey(NG_DIRTY);
+ element.classes..remove(NG_PRISTINE_CLASS)..add(NG_DIRTY_CLASS);
- /**
- * Whether or not the control/model has not been interacted with by the user.
- */
- bool get untouched => !touched;
+ //as soon as one of the controls/models is modified
+ //then all of the parent controls are dirty as well
+ _parentControl.dirty = true;
+ }
- /**
- * Whether or not the control/model has been interacted with by the user.
- */
- bool get touched => infoStates.containsKey(NG_TOUCHED);
+ get valid => _valid;
+ set valid(value) {
+ _invalid = false;
+ _valid = true;
- /**
- * Registers a form control into the form for validation.
- *
- * * [control] - The form control which will be registered (see [NgControl]).
- */
- void addControl(NgControl control) {
- _controls.add(control);
- if (control.name != null) {
- _controlByName.putIfAbsent(control.name, () => <NgControl>[]).add(control);
- }
+ element.classes..remove(NG_INVALID_CLASS)..add(NG_VALID_CLASS);
}
- /**
- * 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]).
- */
- void removeControl(NgControl control) {
- _controls.remove(control);
- String key = control.name;
- if (key != null && _controlByName.containsKey(key)) {
- _controlByName[key].remove(control);
- if (_controlByName[key].isEmpty) _controlByName.remove(key);
- }
+ get invalid => _invalid;
+ set invalid(value) {
+ _valid = false;
+ _invalid = true;
+
+ element.classes..remove(NG_VALID_CLASS)..add(NG_INVALID_CLASS);
}
- /**
- * Clears all the info and error states that are associated with the control.
- *
- * * [control] - The form control which will be cleared of all state (see [NgControl]).
- */
- void removeStates(NgControl control) {
- bool hasRemovals = false;
- errorStates.keys.toList().forEach((state) {
- Set matchingControls = errorStates[state];
- matchingControls.remove(control);
- if (matchingControls.isEmpty) {
- errorStates.remove(state);
- hasRemovals = true;
- }
- });
-
- infoStates.keys.toList().forEach((state) {
- Set matchingControls = infoStates[state];
- matchingControls.remove(control);
- if (matchingControls.isEmpty) {
- infoStates.remove(state);
- hasRemovals = true;
- }
- });
+ get touched => _touched;
+ set touched(value) {
+ _touched = true;
+ _untouched = false;
- if (hasRemovals) _parentControl.removeStates(this);
- }
+ element.classes..remove(NG_UNTOUCHED_CLASS)..add(NG_TOUCHED_CLASS);
- /**
- * Whether or not the control contains the given error.
- *
- * * [errorName] - The name of the error (e.g. ng-required, ng-pattern, etc...)
- */
- bool hasErrorState(String errorName) => errorStates.containsKey(errorName);
+ //as soon as one of the controls/models is touched
+ //then all of the parent controls are touched as well
+ _parentControl.touched = true;
+ }
- /**
- * Adds the given childControl/errorName to the list of errors present on the control. Once
- * added all associated parent controls will be registered with the error as well.
- *
- * * [childControl] - The child control that contains the error.
- * * [errorName] - The name of the given error (e.g. ng-required, ng-pattern, etc...).
- */
- void addErrorState(NgControl childControl, String errorName) {
- element..addClass(errorName + '-invalid')..removeClass(errorName + '-valid');
- errorStates.putIfAbsent(errorName, () => new Set()).add(childControl);
- _parentControl.addErrorState(this, errorName);
+ get untouched => _untouched;
+ set untouched(value) {
+ _touched = false;
+ _untouched = true;
+ element.classes..remove(NG_TOUCHED_CLASS)..add(NG_UNTOUCHED_CLASS);
}
/**
- * Removes the given childControl/errorName from the list of errors present on the control. Once
- * removed the control will update any parent controls depending if error is not present on
- * any other inner controls and or models.
+ * Registers a form control into the form for validation.
*
- * * [childControl] - The child control that contains the error.
- * * [errorName] - The name of the given error (e.g. ng-required, ng-pattern, etc...).
+ * * [control] - The form control which will be registered (see [ngControl]).
*/
- void removeErrorState(NgControl childControl, String errorName) {
- if (!errorStates.containsKey(errorName)) return;
-
- bool hasError = _controls.any((child) => child.hasErrorState(errorName));
- if (!hasError) {
- errorStates.remove(errorName);
- _parentControl.removeErrorState(this, errorName);
- element..removeClass(errorName + '-invalid')..addClass(errorName + '-valid');
- }
- }
-
- String _getOppositeInfoState(String state) {
- switch(state) {
- case NG_DIRTY:
- return NG_PRISTINE;
- case NG_TOUCHED:
- return NG_UNTOUCHED;
- default:
- //not all info states have an opposite value
- return null;
+ addControl(NgControl control) {
+ _controls.add(control);
+ if (control.name != null) {
+ _controlByName[control.name] = control;
}
}
/**
- * Registers a non-error state on the control with the given childControl/stateName data. Once
- * added the control will also add the same data to any associated parent controls.
+ * De-registers a form control from the list of controls associated with the
+ * form.
*
- * * [childControl] - The child control that contains the error.
- * * [stateName] - The name of the given error (e.g. ng-required, ng-pattern, etc...).
+ * * [control] - The form control which will be de-registered (see
+ * [ngControl]).
*/
- void addInfoState(NgControl childControl, String stateName) {
- String oppositeState = _getOppositeInfoState(stateName);
- if (oppositeState != null) element.removeClass(oppositeState);
- element.addClass(stateName);
- infoStates.putIfAbsent(stateName, () => new Set()).add(childControl);
- _parentControl.addInfoState(this, stateName);
+ removeControl(NgControl control) {
+ _controls.remove(control);
+ if (control.name != null) {
+ _controlByName.remove(control.name);
+ }
}
/**
- * De-registers the provided state on the control with the given childControl. The state
- * will be fully removed from the control if all of the inner controls/models also do not
- * contain the state. If so then the state will also be attempted to be removed from the
- * associated parent controls.
+ * 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.
*
- * * [childControl] - The child control that contains the error.
- * * [stateName] - The name of the given error (e.g. ng-required, ng-pattern, etc...).
+ * * [control] - The registered control object (see [ngControl]).
+ * * [errorType] - The error associated with the control (e.g. required, url,
+ * number, etc...).
+ * * [isValid] - Whether the given error is valid or not (false would mean the
+ * error is real).
*/
- void removeInfoState(NgControl childControl, String stateName) {
- String oppositeState = _getOppositeInfoState(stateName);
- if (infoStates.containsKey(stateName)) {
- bool hasState = _controls.any((child) =>
- child.infoStates.containsKey(stateName));
- if (!hasState) {
- if (oppositeState != null) element.addClass(oppositeState);
- element.removeClass(stateName);
- infoStates.remove(stateName);
- _parentControl.removeInfoState(this, stateName);
+ updateControlValidity(NgControl control, String errorType, bool isValid) {
+ List queue = errors[errorType];
+
+ if (isValid) {
+ if (queue != null) {
+ queue.remove(control);
+ if (queue.isEmpty) {
+ errors.remove(errorType);
+ _parentControl.updateControlValidity(this, errorType, true);
+ }
}
- } else if (oppositeState != null) {
- NgControl parent = this;
- do {
- parent.element..addClass(oppositeState)..removeClass(stateName);
- parent = parent.parentControl;
+ if (errors.isEmpty) {
+ valid = true;
}
- while(parent != null && parent is! NgNullControl);
+ } else {
+ if (queue == null) {
+ queue = new List<NgControl>();
+ errors[errorType] = queue;
+ _parentControl.updateControlValidity(this, errorType, false);
+ } else if (queue.contains(control)) return;
+
+ queue.add(control);
+ invalid = true;
}
}
}
class NgNullControl implements NgControl {
- var _name, _dirty, _valid, _submitValid, _pristine, _element, _touched;
- var _controls, _parentControl, _controlName, _animate, infoStates, errorStates;
+ var _name, _dirty, _valid, _invalid, _submit_valid, _pristine, _element;
+ var _touched, _untouched;
+ var _controls, _scope, _parentControl, _controlName;
var errors, _controlByName;
- NgElement element;
+ dom.Element element;
+
+ NgNullControl() {}
+ _onSubmit(bool valid) {}
+
+ addControl(control) {}
+ removeControl(control) {}
+ updateControlValidity(NgControl control, String errorType, bool isValid) {}
+
+ get name => null;
+ set name(name) {}
- void onSubmit(bool valid) {}
+ get submitted => null;
+ get valid_submit => null;
+ get invalid_submit => null;
- void addControl(control) {}
- void removeControl(control) {}
- void updateControlValidity(NgControl ctrl, String errorType, bool isValid) {}
+ get pristine => null;
+ set pristine(value) {}
- String get name => null;
- void set name(name) {}
+ get dirty => null;
+ set dirty(value) {}
- bool get submitted => false;
- bool get validSubmit => true;
- bool get invalidSubmit => false;
- bool get pristine => true;
- bool get dirty => false;
- bool get valid => true;
- bool get invalid => false;
- bool get touched => false;
- bool get untouched => true;
+ get valid => null;
+ set valid(value) {}
- get parentControl => null;
+ get invalid => null;
+ set invalid(value) {}
- String _getOppositeInfoState(String state) => null;
- void addErrorState(NgControl control, String state) {}
- void removeErrorState(NgControl control, String state) {}
- void addInfoState(NgControl control, String state) {}
- void removeInfoState(NgControl control, String state) {}
+ get touched => null;
+ set touched(value) {}
- void reset() {}
- void attach() {}
- void detach() {}
+ get untouched => null;
+ set untouched(value) {}
- bool hasErrorState(String key) => false;
+ reset() => null;
+ detach() => null;
- void removeStates(NgControl control) {}
}
« no previous file with comments | « third_party/pkg/angular/lib/directive/ng_cloak.dart ('k') | third_party/pkg/angular/lib/directive/ng_events.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698