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

Commit

Permalink
style(directives): some code cleanup, clarification & simplification
Browse files Browse the repository at this point in the history
  • Loading branch information
vicb committed Apr 8, 2014
1 parent 99023a3 commit 9c03a3d
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 106 deletions.
80 changes: 34 additions & 46 deletions lib/directive/ng_control.dart
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,13 @@ abstract class NgControl implements NgAttachAware, NgDetachAware {
: _parentControl = injector.parent.get(NgControl);

@override
attach() => _parentControl.addControl(this);
void attach() {
_parentControl.addControl(this);
}

@override
detach() {
_parentControl.removeStates(this);
_parentControl.removeControl(this);
void detach() {
_parentControl..removeStates(this)..removeControl(this);
}

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

String get name => _name;
set name(value) {
void set name(value) {
_name = value;
}

Expand Down Expand Up @@ -139,7 +140,7 @@ abstract class NgControl implements NgAttachAware, NgDetachAware {
void addControl(NgControl control) {
_controls.add(control);
if (control.name != null) {
_controlByName.putIfAbsent(control.name, () => new List<NgControl>()).add(control);
_controlByName.putIfAbsent(control.name, () => <NgControl>[]).add(control);
}
}

Expand All @@ -154,9 +155,7 @@ abstract class NgControl implements NgAttachAware, NgDetachAware {
String key = control.name;
if (key != null && _controlByName.containsKey(key)) {
_controlByName[key].remove(control);
if (_controlByName[key].isEmpty) {
_controlByName.remove(key);
}
if (_controlByName[key].isEmpty) _controlByName.remove(key);
}
}

Expand Down Expand Up @@ -185,9 +184,7 @@ abstract class NgControl implements NgAttachAware, NgDetachAware {
}
});

if (hasRemovals) {
_parentControl.removeStates(this);
}
if (hasRemovals) _parentControl.removeStates(this);
}

/**
Expand All @@ -213,19 +210,16 @@ abstract class NgControl implements NgAttachAware, NgDetachAware {
/**
* 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.
* any other inner controls and or models.
*
* * [childControl] - The child control that contains the error.
* * [errorName] - The name of the given error (e.g. ng-required, ng-pattern, etc...).
*/
void removeErrorState(NgControl childControl, String errorName) {
if (!errorStates.containsKey(errorName)) return;

bool hasError = _controls.isEmpty ||
_controls.every((childControl) {
return !childControl.hasErrorState(errorName);
});
if (hasError) {
bool hasError = _controls.any((child) => child.hasErrorState(errorName));
if (!hasError) {
errorStates.remove(errorName);
_parentControl.removeErrorState(this, errorName);
element..removeClass(errorName + '-invalid')..addClass(errorName + '-valid');
Expand Down Expand Up @@ -253,9 +247,7 @@ abstract class NgControl implements NgAttachAware, NgDetachAware {
*/
void addInfoState(NgControl childControl, String stateName) {
String oppositeState = _getOppositeInfoState(stateName);
if (oppositeState != null) {
element.removeClass(oppositeState);
}
if (oppositeState != null) element.removeClass(oppositeState);
element.addClass(stateName);
infoStates.putIfAbsent(stateName, () => new Set()).add(childControl);
_parentControl.addInfoState(this, stateName);
Expand All @@ -273,14 +265,10 @@ abstract class NgControl implements NgAttachAware, NgDetachAware {
void removeInfoState(NgControl childControl, String stateName) {
String oppositeState = _getOppositeInfoState(stateName);
if (infoStates.containsKey(stateName)) {
bool hasState = _controls.isEmpty ||
_controls.every((childControl) {
return !childControl.infoStates.containsKey(stateName);
});
if (hasState) {
if (oppositeState != null) {
element.addClass(oppositeState);
}
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);
Expand All @@ -291,7 +279,7 @@ abstract class NgControl implements NgAttachAware, NgDetachAware {
parent.element..addClass(oppositeState)..removeClass(stateName);
parent = parent.parentControl;
}
while(parent != null && !(parent is NgNullControl));
while(parent != null && parent is! NgNullControl);
}
}
}
Expand All @@ -302,15 +290,14 @@ class NgNullControl implements NgControl {
var errors, _controlByName;
NgElement element;

NgNullControl() {}
onSubmit(bool valid) {}
void onSubmit(bool valid) {}

addControl(control) {}
removeControl(control) {}
updateControlValidity(NgControl control, String errorType, bool isValid) {}
void addControl(control) {}
void removeControl(control) {}
void updateControlValidity(NgControl ctrl, String errorType, bool isValid) {}

get name => null;
set name(name) {}
String get name => null;
void set name(name) {}

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

get parentControl => null;

_getOppositeInfoState(String state) {}
addErrorState(NgControl control, String state) {}
removeErrorState(NgControl control, String state) {}
addInfoState(NgControl control, String state) {}
removeInfoState(NgControl control, String state) {}
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) {}

reset() => null;
attach() => null;
detach() => null;
void reset() {}
void attach() {}
void detach() {}

bool hasErrorState(String key) => false;
removeStates(NgControl control) {}

void removeStates(NgControl control) {}
}
Loading

0 comments on commit 9c03a3d

Please sign in to comment.