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

Commit 9c03a3d

Browse files
committed
style(directives): some code cleanup, clarification & simplification
1 parent 99023a3 commit 9c03a3d

File tree

2 files changed

+88
-106
lines changed

2 files changed

+88
-106
lines changed

lib/directive/ng_control.dart

Lines changed: 34 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,13 @@ abstract class NgControl implements NgAttachAware, NgDetachAware {
4444
: _parentControl = injector.parent.get(NgControl);
4545

4646
@override
47-
attach() => _parentControl.addControl(this);
47+
void attach() {
48+
_parentControl.addControl(this);
49+
}
4850

4951
@override
50-
detach() {
51-
_parentControl.removeStates(this);
52-
_parentControl.removeControl(this);
52+
void detach() {
53+
_parentControl..removeStates(this)..removeControl(this);
5354
}
5455

5556
/**
@@ -92,7 +93,7 @@ abstract class NgControl implements NgAttachAware, NgDetachAware {
9293
bool get invalidSubmit => _submitValid == false;
9394

9495
String get name => _name;
95-
set name(value) {
96+
void set name(value) {
9697
_name = value;
9798
}
9899

@@ -139,7 +140,7 @@ abstract class NgControl implements NgAttachAware, NgDetachAware {
139140
void addControl(NgControl control) {
140141
_controls.add(control);
141142
if (control.name != null) {
142-
_controlByName.putIfAbsent(control.name, () => new List<NgControl>()).add(control);
143+
_controlByName.putIfAbsent(control.name, () => <NgControl>[]).add(control);
143144
}
144145
}
145146

@@ -154,9 +155,7 @@ abstract class NgControl implements NgAttachAware, NgDetachAware {
154155
String key = control.name;
155156
if (key != null && _controlByName.containsKey(key)) {
156157
_controlByName[key].remove(control);
157-
if (_controlByName[key].isEmpty) {
158-
_controlByName.remove(key);
159-
}
158+
if (_controlByName[key].isEmpty) _controlByName.remove(key);
160159
}
161160
}
162161

@@ -185,9 +184,7 @@ abstract class NgControl implements NgAttachAware, NgDetachAware {
185184
}
186185
});
187186

188-
if (hasRemovals) {
189-
_parentControl.removeStates(this);
190-
}
187+
if (hasRemovals) _parentControl.removeStates(this);
191188
}
192189

193190
/**
@@ -213,19 +210,16 @@ abstract class NgControl implements NgAttachAware, NgDetachAware {
213210
/**
214211
* Removes the given childControl/errorName from the list of errors present on the control. Once
215212
* removed the control will update any parent controls depending if error is not present on
216-
* any other inner controls and or models.
213+
* any other inner controls and or models.
217214
*
218215
* * [childControl] - The child control that contains the error.
219216
* * [errorName] - The name of the given error (e.g. ng-required, ng-pattern, etc...).
220217
*/
221218
void removeErrorState(NgControl childControl, String errorName) {
222219
if (!errorStates.containsKey(errorName)) return;
223220

224-
bool hasError = _controls.isEmpty ||
225-
_controls.every((childControl) {
226-
return !childControl.hasErrorState(errorName);
227-
});
228-
if (hasError) {
221+
bool hasError = _controls.any((child) => child.hasErrorState(errorName));
222+
if (!hasError) {
229223
errorStates.remove(errorName);
230224
_parentControl.removeErrorState(this, errorName);
231225
element..removeClass(errorName + '-invalid')..addClass(errorName + '-valid');
@@ -253,9 +247,7 @@ abstract class NgControl implements NgAttachAware, NgDetachAware {
253247
*/
254248
void addInfoState(NgControl childControl, String stateName) {
255249
String oppositeState = _getOppositeInfoState(stateName);
256-
if (oppositeState != null) {
257-
element.removeClass(oppositeState);
258-
}
250+
if (oppositeState != null) element.removeClass(oppositeState);
259251
element.addClass(stateName);
260252
infoStates.putIfAbsent(stateName, () => new Set()).add(childControl);
261253
_parentControl.addInfoState(this, stateName);
@@ -273,14 +265,10 @@ abstract class NgControl implements NgAttachAware, NgDetachAware {
273265
void removeInfoState(NgControl childControl, String stateName) {
274266
String oppositeState = _getOppositeInfoState(stateName);
275267
if (infoStates.containsKey(stateName)) {
276-
bool hasState = _controls.isEmpty ||
277-
_controls.every((childControl) {
278-
return !childControl.infoStates.containsKey(stateName);
279-
});
280-
if (hasState) {
281-
if (oppositeState != null) {
282-
element.addClass(oppositeState);
283-
}
268+
bool hasState = _controls.any((child) =>
269+
child.infoStates.containsKey(stateName));
270+
if (!hasState) {
271+
if (oppositeState != null) element.addClass(oppositeState);
284272
element.removeClass(stateName);
285273
infoStates.remove(stateName);
286274
_parentControl.removeInfoState(this, stateName);
@@ -291,7 +279,7 @@ abstract class NgControl implements NgAttachAware, NgDetachAware {
291279
parent.element..addClass(oppositeState)..removeClass(stateName);
292280
parent = parent.parentControl;
293281
}
294-
while(parent != null && !(parent is NgNullControl));
282+
while(parent != null && parent is! NgNullControl);
295283
}
296284
}
297285
}
@@ -302,15 +290,14 @@ class NgNullControl implements NgControl {
302290
var errors, _controlByName;
303291
NgElement element;
304292

305-
NgNullControl() {}
306-
onSubmit(bool valid) {}
293+
void onSubmit(bool valid) {}
307294

308-
addControl(control) {}
309-
removeControl(control) {}
310-
updateControlValidity(NgControl control, String errorType, bool isValid) {}
295+
void addControl(control) {}
296+
void removeControl(control) {}
297+
void updateControlValidity(NgControl ctrl, String errorType, bool isValid) {}
311298

312-
get name => null;
313-
set name(name) {}
299+
String get name => null;
300+
void set name(name) {}
314301

315302
bool get submitted => false;
316303
bool get validSubmit => true;
@@ -324,16 +311,17 @@ class NgNullControl implements NgControl {
324311

325312
get parentControl => null;
326313

327-
_getOppositeInfoState(String state) {}
328-
addErrorState(NgControl control, String state) {}
329-
removeErrorState(NgControl control, String state) {}
330-
addInfoState(NgControl control, String state) {}
331-
removeInfoState(NgControl control, String state) {}
314+
String _getOppositeInfoState(String state) => null;
315+
void addErrorState(NgControl control, String state) {}
316+
void removeErrorState(NgControl control, String state) {}
317+
void addInfoState(NgControl control, String state) {}
318+
void removeInfoState(NgControl control, String state) {}
332319

333-
reset() => null;
334-
attach() => null;
335-
detach() => null;
320+
void reset() {}
321+
void attach() {}
322+
void detach() {}
336323

337324
bool hasErrorState(String key) => false;
338-
removeStates(NgControl control) {}
325+
326+
void removeStates(NgControl control) {}
339327
}

0 commit comments

Comments
 (0)