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

Unified Diff: third_party/pkg/angular/lib/directive/ng_model.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_model.dart
===================================================================
--- third_party/pkg/angular/lib/directive/ng_model.dart (revision 33054)
+++ third_party/pkg/angular/lib/directive/ng_model.dart (working copy)
@@ -10,79 +10,60 @@
* knows how to (in)validate the model and the form in which it is declared
* (to be implemented)
*/
-@NgDirective(selector: '[ng-model]')
-class NgModel extends NgControl implements NgAttachAware {
+@NgDirective(
+ selector: '[ng-model]')
+class NgModel extends NgControl {
final NgForm _form;
- final AstParser _parser;
+ final dom.Element _element;
+ final Scope _scope;
- BoundGetter getter = ([_]) => null;
- BoundSetter setter = (_, [__]) => null;
+ Getter getter = ([_]) => null;
+ Setter setter = (_, [__]) => null;
- var _lastValue;
String _exp;
- final _validators = <NgValidatable>[];
+ String _name;
- Watch _removeWatch;
+ final List<_NgModelValidator> _validators = new List<_NgModelValidator>();
+ final Map<String, bool> currentErrors = new Map<String, bool>();
+
+ Function _removeWatch = () => null;
bool _watchCollection;
+
Function render = (value) => null;
- NgModel(Scope _scope, dom.Element _element, Injector injector,
- NgForm this._form, this._parser, NodeAttrs attrs)
- : super(_scope, _element, injector)
- {
- _exp = attrs["ng-model"];
+ NgModel(this._scope, NodeAttrs attrs, [dom.Element this._element, NgForm this._form]) {
+ _exp = 'ng-model=${attrs["ng-model"]}';
watchCollection = false;
- }
- process(value, [_]) {
- validate();
- _scope.rootScope.domWrite(() => render(value));
+ _form.addControl(this);
+ pristine = true;
}
- attach() {
- watchCollection = false;
- _scope.on('resetNgModel').listen((e) => reset());
- }
+ get element => _element;
- reset() {
- untouched = true;
- modelValue = _lastValue;
- }
-
@NgAttr('name')
get name => _name;
set name(value) {
_name = value;
- _parentControl.addControl(this);
+ _form.addControl(this);
}
- // TODO(misko): could we get rid of watch collection, and just always watch the collection?
get watchCollection => _watchCollection;
set watchCollection(value) {
if (_watchCollection == value) return;
_watchCollection = value;
- if (_removeWatch!=null) _removeWatch.remove();
+ _removeWatch();
if (_watchCollection) {
- _removeWatch = _scope.watch(
- _parser(_exp, collection: true),
- (changeRecord, _) {
- var value = changeRecord is CollectionChangeRecord ? changeRecord.iterable: changeRecord;
- process(value);
- });
- } else if (_exp != null) {
- _removeWatch = _scope.watch(_exp, process);
+ _removeWatch = _scope.$watchCollection((s) => getter(), (value) => render(value), _exp);
+ } else {
+ _removeWatch = _scope.$watch((s) => getter(), (value) => render(value), _exp);
}
}
- // TODO(misko): getters/setters need to go. We need AST here.
@NgCallback('ng-model')
set model(BoundExpression boundExpression) {
getter = boundExpression;
setter = boundExpression.assign;
-
- _scope.rootScope.runAsync(() {
- _lastValue = modelValue;
- });
}
// TODO(misko): right now viewValue and modelValue are the same,
@@ -99,23 +80,46 @@
* Executes a validation on the form against each of the validation present on the model.
*/
validate() {
- if (validators.isNotEmpty) {
+ if(validators.length > 0) {
validators.forEach((validator) {
- setValidity(validator.name, validator.isValid(viewValue));
+ setValidity(validator.name, validator.isValid());
});
} else {
valid = true;
}
}
- setValidity(String name, bool valid) {
- this.updateControlValidity(this, name, valid);
+ /**
+ * Sets the validity status of the given errorType on the model. Depending on if
+ * valid or invalid, the matching CSS classes will be added/removed on the input
+ * element associated with the model. If any errors exist on the model then invalid
+ * will be set to true otherwise valid will be set to true.
+ *
+ * * [errorType] - The name of the error (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(String errorType, bool isValid) {
+ if(isValid) {
+ if(currentErrors.containsKey(errorType)) {
+ currentErrors.remove(errorType);
+ }
+ if(valid != true && currentErrors.isEmpty) {
+ valid = true;
+ }
+ } else if(!currentErrors.containsKey(errorType)) {
+ currentErrors[errorType] = true;
+ invalid = true;
+ }
+
+ if(_form != null) {
+ _form.setValidity(this, errorType, isValid);
+ }
}
/**
* Registers a validator into the model to consider when running validate().
*/
- addValidator(NgValidatable v) {
+ addValidator(_NgModelValidator v) {
validators.add(v);
validate();
}
@@ -123,10 +127,17 @@
/**
* De-registers a validator from the model.
*/
- removeValidator(NgValidatable v) {
+ removeValidator(_NgModelValidator v) {
validators.remove(v);
validate();
}
+
+ /**
+ * Removes the model from the control/form.
+ */
+ destroy() {
+ _form.removeControl(this);
+ }
}
/**
@@ -134,30 +145,26 @@
*
* <input type="checkbox" ng-model="flag">
*
- * This creates a two way databinding between the boolean expression specified
- * in ng-model and the checkbox input element in the DOM.  If the ng-model value
- * is falsy (i.e. one of `false`, `null`, and `0`), then the checkbox is
- * unchecked. Otherwise, it is checked.  Likewise, when the checkbox is checked,
- * the model value is set to true. When unchecked, it is set to false.
+ * This creates a two way databinding between the boolean expression specified in
+ * ng-model and the checkbox input element in the DOM.  If the ng-model value is
+ * falsy (i.e. one of `false`, `null`, and `0`), then the checkbox is unchecked.
+ * Otherwise, it is checked.  Likewise, when the checkbox is checked, the model
+ * value is set to true. When unchecked, it is set to false.
+ *
+ * The AngularJS style ng-true-value / ng-false-value is not supported.
*/
@NgDirective(selector: 'input[type=checkbox][ng-model]')
class InputCheckboxDirective {
- final dom.InputElement inputElement;
- final NgModel ngModel;
- final NgTrueValue ngTrueValue;
- final NgFalseValue ngFalseValue;
- final Scope scope;
+ dom.InputElement inputElement;
+ NgModel ngModel;
+ Scope scope;
- InputCheckboxDirective(dom.Element this.inputElement, this.ngModel,
- this.scope, this.ngTrueValue, this.ngFalseValue) {
+ InputCheckboxDirective(dom.Element this.inputElement, this.ngModel, this.scope) {
ngModel.render = (value) {
- inputElement.checked = ngTrueValue.isValue(inputElement, value);
+ inputElement.checked = value == null ? false : toBool(value);
};
inputElement.onChange.listen((value) {
- ngModel.dirty = true;
- ngModel.viewValue = inputElement.checked
- ? ngTrueValue.readValue(inputElement)
- : ngFalseValue.readValue(inputElement);
+ scope.$apply(() => ngModel.viewValue = inputElement.checked);
});
}
}
@@ -165,7 +172,7 @@
/**
* Usage:
*
- * <input type="text|url|password|email" ng-model="myModel">
+ * <input type="text|number|url|password|email" ng-model="myModel">
* <textarea ng-model="myModel"></textarea>
*
* This creates a two-way binding between any string-based input element
@@ -180,108 +187,41 @@
@NgDirective(selector: 'input[type=password][ng-model]')
@NgDirective(selector: 'input[type=url][ng-model]')
@NgDirective(selector: 'input[type=email][ng-model]')
-@NgDirective(selector: 'input[type=search][ng-model]')
+@NgDirective(selector: 'input[type=number][ng-model]')
class InputTextLikeDirective {
- final dom.Element inputElement;
- final NgModel ngModel;
- final Scope scope;
+ dom.Element inputElement;
+ NgModel ngModel;
+ Scope scope;
String _inputType;
get typedValue => (inputElement as dynamic).value;
- set typedValue(value) => (inputElement as dynamic).value = (value == null) ?
- '' :
- value.toString();
+ set typedValue(value) => (inputElement as dynamic).value = (value == null) ? '' : value.toString();
- InputTextLikeDirective(this.inputElement, this.ngModel, this.scope) {
+ InputTextLikeDirective(dom.Element this.inputElement, NgModel this.ngModel, Scope this.scope) {
ngModel.render = (value) {
if (value == null) value = '';
var currentValue = typedValue;
- if (value != currentValue && !(value is num && currentValue is num &&
- value.isNaN && currentValue.isNaN)) {
+ if (value != currentValue && !(value is num && currentValue is num && value.isNaN && currentValue.isNaN)) {
typedValue = value;
}
};
- inputElement
- ..onChange.listen(processValue)
- ..onInput.listen(processValue)
- ..onBlur.listen((e) {
- if (ngModel.touched == null || ngModel.touched == false) {
- ngModel.touched = true;
- }
- });
+ inputElement.onChange.listen(relaxFnArgs(processValue));
+ inputElement.onKeyDown.listen((e) {
+ new async.Timer(Duration.ZERO, processValue);
+ scope.$skipAutoDigest();
+ });
}
- processValue([_]) {
+ processValue() {
+ ngModel.validate();
var value = typedValue;
if (value != ngModel.viewValue) {
- ngModel.dirty = true;
- ngModel.viewValue = value;
+ scope.$apply(() => ngModel.viewValue = value);
}
- ngModel.validate();
}
}
-/**
- * Usage:
- *
- * <input type="number|range" ng-model="myModel">
- *
- * Model:
- *
- * num myModel;
- *
- * This creates a two-way binding between the input and the named model property
- * (e.g., myModel in the example above). When processing the input, its value is
- * read as a [num], via the [dom.InputElement.valueAsNumber] field. If the input
- * text does not represent a number, then the model is appropriately set to
- * [double.NAN]. Setting the model property to [null] will clear the input.
- * Setting the model to [double.NAN] will have no effect (input will be left
- * unchanged).
- */
-@NgDirective(selector: 'input[type=number][ng-model]')
-@NgDirective(selector: 'input[type=range][ng-model]')
-class InputNumberLikeDirective {
- final dom.InputElement inputElement;
- final NgModel ngModel;
- final Scope scope;
-
- num get typedValue => inputElement.valueAsNumber;
- void set typedValue(num value) {
- // [chalin, 2014-02-16] This post
- // http://lists.whatwg.org/pipermail/whatwg-whatwg.org/2010-January/024829.html
- // suggests that setting `valueAsNumber` to null should clear the field, but
- // it does not. [TODO: put BUG/ISSUE number here]. We implement a
- // workaround by setting `value`. Clean-up once the bug is fixed.
- if (value == null) {
- inputElement.value = null;
- } else {
- inputElement.valueAsNumber = value;
- }
- }
-
- InputNumberLikeDirective(dom.Element this.inputElement, this.ngModel, this.scope) {
- ngModel.render = (value) {
- if (value != typedValue
- && (value == null || value is num && !value.isNaN)) {
- typedValue = value;
- }
- };
- inputElement
- ..onChange.listen(relaxFnArgs(processValue))
- ..onInput.listen(relaxFnArgs(processValue));
- }
-
- processValue() {
- num value = typedValue;
- if (value != ngModel.viewValue) {
- ngModel.dirty = true;
- scope.eval(() => ngModel.viewValue = value);
- }
- ngModel.validate();
- }
-}
-
class _UidCounter {
static final int CHAR_0 = "0".codeUnitAt(0);
static final int CHAR_9 = "9".codeUnitAt(0);
@@ -290,7 +230,7 @@
List charCodes = [CHAR_0, CHAR_0, CHAR_0];
String next() {
- for (int i = charCodes.length - 1; i >= 0; i--) {
+ for (int i = charCodes.length-1; i >= 0; i--) {
int code = charCodes[i];
if (code == CHAR_9) {
charCodes[i] = CHAR_A;
@@ -309,68 +249,8 @@
final _uidCounter = new _UidCounter();
-/**
- * Use `ng-value` directive with `<input type="radio">` or `<option>` to
- * allow binding to values other then strings. This is needed since the
- * `value` attribute on DOM element `<input type="radio" value="foo">` can
- * only be a string. With `ng-value` one can bind to any object.
- */
-@NgDirective(selector: '[ng-value]')
-class NgValue {
- final dom.Element element;
- @NgOneWay('ng-value')
- var value;
- NgValue(this.element);
-
- readValue(dom.Element element) {
- assert(this.element == null || element == this.element);
- return this.element == null ? (element as dynamic).value : value;
- }
-}
-
/**
- * `ng-true-value` allows you to select any expression to be set to
- * `ng-model` when checkbox is selected on `<input type="checkbox">`.
- */
-@NgDirective(selector: '[ng-true-value]')
-class NgTrueValue {
- final dom.Element element;
- @NgOneWay('ng-true-value')
- var value;
-
- NgTrueValue(this.element);
-
- readValue(dom.Element element) {
- assert(this.element == null || element == this.element);
- return this.element == null ? true : value;
- }
-
- isValue(dom.Element element, value) {
- assert(this.element == null || element == this.element);
- return this.element == null ? toBool(value) : value == this.value;
- }
-}
-
-/**
- * `ng-false-value` allows you to select any expression to be set to
- * `ng-model` when checkbox is deselected<input type="checkbox">`.
- */
-@NgDirective(selector: '[ng-false-value]')
-class NgFalseValue {
- final dom.Element element;
- @NgOneWay('ng-false-value')
- var value;
-
- NgFalseValue(this.element);
-
- readValue(dom.Element element) {
- assert(this.element == null || element == this.element);
- return this.element == null ? false : value;
- }
-}
-
-/**
* Usage:
*
* <input type="radio" ng-model="category">
@@ -388,25 +268,23 @@
*/
@NgDirective(selector: 'input[type=radio][ng-model]')
class InputRadioDirective {
- final dom.RadioButtonInputElement radioButtonElement;
- final NgModel ngModel;
- final NgValue ngValue;
- final Scope scope;
+ dom.RadioButtonInputElement radioButtonElement;
+ NgModel ngModel;
+ Scope scope;
InputRadioDirective(dom.Element this.radioButtonElement, this.ngModel,
- this.scope, this.ngValue, NodeAttrs attrs) {
+ this.scope, NodeAttrs attrs) {
// If there's no "name" set, we'll set a unique name. This ensures
// less surprising behavior about which radio buttons are grouped together.
if (attrs['name'] == '' || attrs['name'] == null) {
attrs["name"] = _uidCounter.next();
}
- ngModel.render = (value) {
- radioButtonElement.checked = (value == ngValue.readValue(radioButtonElement));
+ ngModel.render = (String value) {
+ radioButtonElement.checked = (value == radioButtonElement.value);
};
radioButtonElement.onClick.listen((_) {
if (radioButtonElement.checked) {
- ngModel.dirty = true;
- ngModel.viewValue = ngValue.readValue(radioButtonElement);
+ scope.$apply(() => ngModel.viewValue = radioButtonElement.value);
}
});
}
@@ -424,12 +302,10 @@
*/
@NgDirective(selector: '[contenteditable][ng-model]')
class ContentEditableDirective extends InputTextLikeDirective {
- ContentEditableDirective(dom.Element inputElement, NgModel ngModel,
- Scope scope)
- : super(inputElement, ngModel, scope);
+ ContentEditableDirective(dom.Element inputElement, NgModel ngModel, Scope scope):
+ super(inputElement, ngModel, scope);
// The implementation is identical to InputTextLikeDirective but use innerHtml instead of value
get typedValue => (inputElement as dynamic).innerHtml;
- set typedValue(String value) =>
- (inputElement as dynamic).innerHtml = (value == null) ? '' : value;
+ set typedValue(String value) => (inputElement as dynamic).innerHtml = (value == null) ? '' : value;
}
« no previous file with comments | « third_party/pkg/angular/lib/directive/ng_include.dart ('k') | third_party/pkg/angular/lib/directive/ng_model_validators.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698