Skip to content
This repository was archived by the owner on Feb 22, 2018. It is now read-only.

Commit c5236ff

Browse files
matskomhevery
authored andcommitted
docs(forms): provide documentation for newly refactored code
1 parent 82b4f3e commit c5236ff

File tree

4 files changed

+206
-72
lines changed

4 files changed

+206
-72
lines changed

lib/directive/ng_control.dart

Lines changed: 141 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
part of angular.directive;
22

3+
/**
4+
* The NgControl class is a super-class for handling info and error states between
5+
* inner controls and models. NgControl will automatically apply the associated CSS
6+
* classes for the error and info states that are applied as well as status flags.
7+
* NgControl is used with the form and fieldset as well as all other directives that
8+
* are used for user input with NgModel.
9+
*/
310
abstract class NgControl implements NgAttachAware, NgDetachAware {
411
static const NG_VALID = "ng-valid";
512
static const NG_INVALID = "ng-invalid";
@@ -15,12 +22,21 @@ abstract class NgControl implements NgAttachAware, NgDetachAware {
1522

1623
final NgControl _parentControl;
1724
final NgAnimate _animate;
18-
NgElement _element;
25+
final NgElement _element;
1926

2027
final _controls = new List<NgControl>();
2128
final _controlByName = new Map<String, List<NgControl>>();
2229

30+
/**
31+
* The list of errors present on the control represented by an error name and
32+
* an inner control instance.
33+
*/
2334
final errorStates = new Map<String, Set<NgControl>>();
35+
36+
/**
37+
* The list of info messages present on the control represented by an state name and
38+
* an inner control instance.
39+
*/
2440
final infoStates = new Map<String, Set<NgControl>>();
2541

2642
NgControl(NgElement this._element, Injector injector,
@@ -36,13 +52,16 @@ abstract class NgControl implements NgAttachAware, NgDetachAware {
3652
_parentControl.removeControl(this);
3753
}
3854

39-
reset() {
55+
/**
56+
* Resets the form and inner models to their pristine state.
57+
*/
58+
void reset() {
4059
_controls.forEach((control) {
4160
control.reset();
4261
});
4362
}
4463

45-
onSubmit(bool valid) {
64+
void onSubmit(bool valid) {
4665
if (valid) {
4766
_submit_valid = true;
4867
element..addClass(NG_SUBMIT_VALID)..removeClass(NG_SUBMIT_INVALID);
@@ -55,34 +74,69 @@ abstract class NgControl implements NgAttachAware, NgDetachAware {
5574
});
5675
}
5776

58-
get parentControl => _parentControl;
77+
NgControl get parentControl => _parentControl;
5978

60-
get submitted => _submit_valid != null;
61-
get valid_submit => _submit_valid == true;
62-
get invalid_submit => _submit_valid == false;
79+
/**
80+
* Whether or not the form has been submitted yet.
81+
*/
82+
bool get submitted => _submit_valid != null;
83+
84+
/**
85+
* Whether or not the form was valid when last submitted.
86+
*/
87+
bool get valid_submit => _submit_valid == true;
88+
89+
/**
90+
* Whether or not the form was invalid when last submitted.
91+
*/
92+
bool get invalid_submit => _submit_valid == false;
6393

64-
get name => _name;
94+
String get name => _name;
6595
set name(value) {
6696
_name = value;
6797
}
6898

69-
get element => _element;
99+
/**
100+
* Whether or not the form was invalid when last submitted.
101+
*/
102+
NgElement get element => _element;
70103

71-
get valid => !invalid;
72-
get invalid => errorStates.isNotEmpty;
104+
/**
105+
* A control is considered valid if all inner models are valid.
106+
*/
107+
bool get valid => !invalid;
73108

74-
get pristine => !dirty;
75-
get dirty => infoStates.containsKey(NG_DIRTY);
109+
/**
110+
* A control is considered invalid if any inner models are invalid.
111+
*/
112+
bool get invalid => errorStates.isNotEmpty;
76113

77-
get untouched => !touched;
78-
get touched => infoStates.containsKey(NG_TOUCHED);
114+
/**
115+
* Whether or not the control's or model's data has not been changed.
116+
*/
117+
bool get pristine => !dirty;
118+
119+
/**
120+
* Whether or not the control's or model's data has been changed.
121+
*/
122+
bool get dirty => infoStates.containsKey(NG_DIRTY);
123+
124+
/**
125+
* Whether or not the control/model has not been interacted with by the user.
126+
*/
127+
bool get untouched => !touched;
128+
129+
/**
130+
* Whether or not the control/model has been interacted with by the user.
131+
*/
132+
bool get touched => infoStates.containsKey(NG_TOUCHED);
79133

80134
/**
81135
* Registers a form control into the form for validation.
82136
*
83-
* * [control] - The form control which will be registered (see [ngControl]).
137+
* * [control] - The form control which will be registered (see [NgControl]).
84138
*/
85-
addControl(NgControl control) {
139+
void addControl(NgControl control) {
86140
_controls.add(control);
87141
if (control.name != null) {
88142
_controlByName.putIfAbsent(control.name, () => new List<NgControl>()).add(control);
@@ -93,10 +147,9 @@ abstract class NgControl implements NgAttachAware, NgDetachAware {
93147
* De-registers a form control from the list of controls associated with the
94148
* form.
95149
*
96-
* * [control] - The form control which will be de-registered (see
97-
* [ngControl]).
150+
* * [control] - The form control which will be de-registered (see [NgControl]).
98151
*/
99-
removeControl(NgControl control) {
152+
void removeControl(NgControl control) {
100153
_controls.remove(control);
101154
String key = control.name;
102155
if (key != null && _controlByName.containsKey(key)) {
@@ -107,7 +160,12 @@ abstract class NgControl implements NgAttachAware, NgDetachAware {
107160
}
108161
}
109162

110-
removeStates(NgControl control) {
163+
/**
164+
* Clears all the info and error states that are associated with the control.
165+
*
166+
* * [control] - The form control which will be cleared of all state (see [NgControl]).
167+
*/
168+
void removeStates(NgControl control) {
111169
bool hasRemovals = false;
112170
errorStates.keys.toList().forEach((state) {
113171
Set matchingControls = errorStates[state];
@@ -133,41 +191,48 @@ abstract class NgControl implements NgAttachAware, NgDetachAware {
133191
}
134192

135193
/**
136-
* Sets the validity status of the given control/errorType pair within
137-
* the list of controls registered on the form. Depending on the validation
138-
* state of the existing controls, this will either change valid to true
139-
* or invalid to true depending on if all controls are valid or if one
140-
* or more of them is invalid.
194+
* Whether or not the control contains the given error.
141195
*
142-
* * [control] - The registered control object (see [ngControl]).
143-
* * [errorType] - The error associated with the control (e.g. required, url,
144-
* number, etc...).
145-
* * [isValid] - Whether the given error is valid or not (false would mean the
146-
* error is real).
196+
* * [errorName] - The name of the error (e.g. ng-required, ng-pattern, etc...)
147197
*/
148-
bool hasErrorState(String key) => errorStates.containsKey(key);
198+
bool hasErrorState(String errorName) => errorStates.containsKey(errorName);
149199

150-
addErrorState(NgControl control, String state) {
151-
element..addClass(state + '-invalid')..removeClass(state + '-valid');
152-
errorStates.putIfAbsent(state, () => new Set()).add(control);
153-
_parentControl.addErrorState(this, state);
200+
/**
201+
* Adds the given childControl/errorName to the list of errors present on the control. Once
202+
* added all associated parent controls will be registered with the error as well.
203+
*
204+
* * [childControl] - The child control that contains the error.
205+
* * [errorName] - The name of the given error (e.g. ng-required, ng-pattern, etc...).
206+
*/
207+
void addErrorState(NgControl childControl, String errorName) {
208+
element..addClass(errorName + '-invalid')..removeClass(errorName + '-valid');
209+
errorStates.putIfAbsent(errorName, () => new Set()).add(childControl);
210+
_parentControl.addErrorState(this, errorName);
154211
}
155212

156-
removeErrorState(NgControl control, String state) {
157-
if (!errorStates.containsKey(state)) return;
213+
/**
214+
* Removes the given childControl/errorName from the list of errors present on the control. Once
215+
* removed the control will update any parent controls depending if error is not present on
216+
* any other inner controls and or models.
217+
*
218+
* * [childControl] - The child control that contains the error.
219+
* * [errorName] - The name of the given error (e.g. ng-required, ng-pattern, etc...).
220+
*/
221+
void removeErrorState(NgControl childControl, String errorName) {
222+
if (!errorStates.containsKey(errorName)) return;
158223

159-
bool hasState = _controls.isEmpty ||
160-
_controls.every((control) {
161-
return !control.hasErrorState(state);
224+
bool hasError = _controls.isEmpty ||
225+
_controls.every((childControl) {
226+
return !childControl.hasErrorState(errorName);
162227
});
163-
if (hasState) {
164-
errorStates.remove(state);
165-
_parentControl.removeErrorState(this, state);
166-
element..removeClass(state + '-invalid')..addClass(state + '-valid');
228+
if (hasError) {
229+
errorStates.remove(errorName);
230+
_parentControl.removeErrorState(this, errorName);
231+
element..removeClass(errorName + '-invalid')..addClass(errorName + '-valid');
167232
}
168233
}
169234

170-
_getOppositeInfoState(String state) {
235+
String _getOppositeInfoState(String state) {
171236
switch(state) {
172237
case NG_DIRTY:
173238
return NG_PRISTINE;
@@ -179,35 +244,51 @@ abstract class NgControl implements NgAttachAware, NgDetachAware {
179244
}
180245
}
181246

182-
addInfoState(NgControl control, String state) {
183-
String oppositeState = _getOppositeInfoState(state);
247+
/**
248+
* Registers a non-error state on the control with the given childControl/stateName data. Once
249+
* added the control will also add the same data to any associated parent controls.
250+
*
251+
* * [childControl] - The child control that contains the error.
252+
* * [stateName] - The name of the given error (e.g. ng-required, ng-pattern, etc...).
253+
*/
254+
void addInfoState(NgControl childControl, String stateName) {
255+
String oppositeState = _getOppositeInfoState(stateName);
184256
if (oppositeState != null) {
185257
element.removeClass(oppositeState);
186258
}
187-
element.addClass(state);
188-
infoStates.putIfAbsent(state, () => new Set()).add(control);
189-
_parentControl.addInfoState(this, state);
259+
element.addClass(stateName);
260+
infoStates.putIfAbsent(stateName, () => new Set()).add(childControl);
261+
_parentControl.addInfoState(this, stateName);
190262
}
191263

192-
removeInfoState(NgControl control, String state) {
193-
String oppositeState = _getOppositeInfoState(state);
194-
if (infoStates.containsKey(state)) {
264+
/**
265+
* De-registers the provided state on the control with the given childControl. The state
266+
* will be fully removed from the control if all of the inner controls/models also do not
267+
* contain the state. If so then the state will also be attempted to be removed from the
268+
* associated parent controls.
269+
*
270+
* * [childControl] - The child control that contains the error.
271+
* * [stateName] - The name of the given error (e.g. ng-required, ng-pattern, etc...).
272+
*/
273+
void removeInfoState(NgControl childControl, String stateName) {
274+
String oppositeState = _getOppositeInfoState(stateName);
275+
if (infoStates.containsKey(stateName)) {
195276
bool hasState = _controls.isEmpty ||
196-
_controls.every((control) {
197-
return !control.infoStates.containsKey(state);
277+
_controls.every((childControl) {
278+
return !childControl.infoStates.containsKey(stateName);
198279
});
199280
if (hasState) {
200281
if (oppositeState != null) {
201282
element.addClass(oppositeState);
202283
}
203-
element.removeClass(state);
204-
infoStates.remove(state);
205-
_parentControl.removeInfoState(this, state);
284+
element.removeClass(stateName);
285+
infoStates.remove(stateName);
286+
_parentControl.removeInfoState(this, stateName);
206287
}
207288
} else if (oppositeState != null) {
208289
NgControl parent = this;
209290
do {
210-
parent.element..addClass(oppositeState)..removeClass(state);
291+
parent.element..addClass(oppositeState)..removeClass(stateName);
211292
parent = parent.parentControl;
212293
}
213294
while(parent != null && !(parent is NgNullControl));

lib/directive/ng_form.dart

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,18 +49,29 @@ class NgForm extends NgControl {
4949
}
5050
}
5151

52+
/**
53+
* The name of the control. This is usually fetched via the name attribute that is
54+
* present on the element that the control is bound to.
55+
*/
5256
@NgAttr('name')
5357
get name => _name;
54-
set name(value) {
58+
set name(String value) {
5559
if (value != null) {
5660
super.name = value;
5761
_scope.context[name] = this;
5862
}
5963
}
6064

65+
/**
66+
* The list of associated child controls.
67+
*/
6168
get controls => _controlByName;
6269

63-
NgControl operator[](name) =>
70+
/**
71+
* Returns the child control that is associated with the given name. If multiple
72+
* child controls contain the same name then the first instance will be returned.
73+
*/
74+
NgControl operator[](String name) =>
6475
controls.containsKey(name) ? controls[name][0] : null;
6576
}
6677

0 commit comments

Comments
 (0)