cleanup(forms): remove facade (#12804)

This commit is contained in:
Victor Berchet 2016-11-11 10:47:34 -08:00 committed by GitHub
parent 7886561997
commit 45ddd6ba78
11 changed files with 58 additions and 89 deletions

View File

@ -7,7 +7,6 @@
*/ */
import {Observable} from '../facade/async'; import {Observable} from '../facade/async';
import {isPresent} from '../facade/lang';
import {AbstractControl} from '../model'; import {AbstractControl} from '../model';
/** /**
@ -20,49 +19,43 @@ import {AbstractControl} from '../model';
export abstract class AbstractControlDirective { export abstract class AbstractControlDirective {
get control(): AbstractControl { throw new Error('unimplemented'); } get control(): AbstractControl { throw new Error('unimplemented'); }
get value(): any { return isPresent(this.control) ? this.control.value : null; } get value(): any { return this.control ? this.control.value : null; }
get valid(): boolean { return isPresent(this.control) ? this.control.valid : null; } get valid(): boolean { return this.control ? this.control.valid : null; }
get invalid(): boolean { return isPresent(this.control) ? this.control.invalid : null; } get invalid(): boolean { return this.control ? this.control.invalid : null; }
get pending(): boolean { return isPresent(this.control) ? this.control.pending : null; } get pending(): boolean { return this.control ? this.control.pending : null; }
get errors(): {[key: string]: any} { get errors(): {[key: string]: any} { return this.control ? this.control.errors : null; }
return isPresent(this.control) ? this.control.errors : null;
}
get pristine(): boolean { return isPresent(this.control) ? this.control.pristine : null; } get pristine(): boolean { return this.control ? this.control.pristine : null; }
get dirty(): boolean { return isPresent(this.control) ? this.control.dirty : null; } get dirty(): boolean { return this.control ? this.control.dirty : null; }
get touched(): boolean { return isPresent(this.control) ? this.control.touched : null; } get touched(): boolean { return this.control ? this.control.touched : null; }
get untouched(): boolean { return isPresent(this.control) ? this.control.untouched : null; } get untouched(): boolean { return this.control ? this.control.untouched : null; }
get disabled(): boolean { return isPresent(this.control) ? this.control.disabled : null; } get disabled(): boolean { return this.control ? this.control.disabled : null; }
get enabled(): boolean { return isPresent(this.control) ? this.control.enabled : null; } get enabled(): boolean { return this.control ? this.control.enabled : null; }
get statusChanges(): Observable<any> { get statusChanges(): Observable<any> { return this.control ? this.control.statusChanges : null; }
return isPresent(this.control) ? this.control.statusChanges : null;
}
get valueChanges(): Observable<any> { get valueChanges(): Observable<any> { return this.control ? this.control.valueChanges : null; }
return isPresent(this.control) ? this.control.valueChanges : null;
}
get path(): string[] { return null; } get path(): string[] { return null; }
reset(value: any = undefined): void { reset(value: any = undefined): void {
if (isPresent(this.control)) this.control.reset(value); if (this.control) this.control.reset(value);
} }
hasError(errorCode: string, path: string[] = null): boolean { hasError(errorCode: string, path: string[] = null): boolean {
return isPresent(this.control) ? this.control.hasError(errorCode, path) : false; return this.control ? this.control.hasError(errorCode, path) : false;
} }
getError(errorCode: string, path: string[] = null): any { getError(errorCode: string, path: string[] = null): any {
return isPresent(this.control) ? this.control.getError(errorCode, path) : null; return this.control ? this.control.getError(errorCode, path) : null;
} }
} }

View File

@ -13,7 +13,7 @@ import {ControlValueAccessor, NG_VALUE_ACCESSOR} from './control_value_accessor'
export const CHECKBOX_VALUE_ACCESSOR: any = { export const CHECKBOX_VALUE_ACCESSOR: any = {
provide: NG_VALUE_ACCESSOR, provide: NG_VALUE_ACCESSOR,
useExisting: forwardRef(() => CheckboxControlValueAccessor), useExisting: forwardRef(() => CheckboxControlValueAccessor),
multi: true multi: true,
}; };
/** /**

View File

@ -8,8 +8,6 @@
import {Directive, ElementRef, Renderer, forwardRef} from '@angular/core'; import {Directive, ElementRef, Renderer, forwardRef} from '@angular/core';
import {isBlank} from '../facade/lang';
import {ControlValueAccessor, NG_VALUE_ACCESSOR} from './control_value_accessor'; import {ControlValueAccessor, NG_VALUE_ACCESSOR} from './control_value_accessor';
export const DEFAULT_VALUE_ACCESSOR: any = { export const DEFAULT_VALUE_ACCESSOR: any = {
@ -45,7 +43,7 @@ export class DefaultValueAccessor implements ControlValueAccessor {
constructor(private _renderer: Renderer, private _elementRef: ElementRef) {} constructor(private _renderer: Renderer, private _elementRef: ElementRef) {}
writeValue(value: any): void { writeValue(value: any): void {
var normalizedValue = isBlank(value) ? '' : value; const normalizedValue = value == null ? '' : value;
this._renderer.setElementProperty(this._elementRef.nativeElement, 'value', normalizedValue); this._renderer.setElementProperty(this._elementRef.nativeElement, 'value', normalizedValue);
} }

View File

@ -8,8 +8,6 @@
import {Directive, Self} from '@angular/core'; import {Directive, Self} from '@angular/core';
import {isPresent} from '../facade/lang';
import {AbstractControlDirective} from './abstract_control_directive'; import {AbstractControlDirective} from './abstract_control_directive';
import {ControlContainer} from './control_container'; import {ControlContainer} from './control_container';
import {NgControl} from './ng_control'; import {NgControl} from './ng_control';
@ -19,27 +17,13 @@ export class AbstractControlStatus {
constructor(cd: AbstractControlDirective) { this._cd = cd; } constructor(cd: AbstractControlDirective) { this._cd = cd; }
get ngClassUntouched(): boolean { get ngClassUntouched(): boolean { return this._cd.control ? this._cd.control.untouched : false; }
return isPresent(this._cd.control) ? this._cd.control.untouched : false; get ngClassTouched(): boolean { return this._cd.control ? this._cd.control.touched : false; }
} get ngClassPristine(): boolean { return this._cd.control ? this._cd.control.pristine : false; }
get ngClassTouched(): boolean { get ngClassDirty(): boolean { return this._cd.control ? this._cd.control.dirty : false; }
return isPresent(this._cd.control) ? this._cd.control.touched : false; get ngClassValid(): boolean { return this._cd.control ? this._cd.control.valid : false; }
} get ngClassInvalid(): boolean { return this._cd.control ? this._cd.control.invalid : false; }
get ngClassPristine(): boolean { get ngClassPending(): boolean { return this._cd.control ? this._cd.control.pending : false; }
return isPresent(this._cd.control) ? this._cd.control.pristine : false;
}
get ngClassDirty(): boolean {
return isPresent(this._cd.control) ? this._cd.control.dirty : false;
}
get ngClassValid(): boolean {
return isPresent(this._cd.control) ? this._cd.control.valid : false;
}
get ngClassInvalid(): boolean {
return isPresent(this._cd.control) ? this._cd.control.invalid : false;
}
get ngClassPending(): boolean {
return isPresent(this._cd.control) ? this._cd.control.pending : false;
}
} }
export const ngControlStatusHost = { export const ngControlStatusHost = {
@ -49,7 +33,7 @@ export const ngControlStatusHost = {
'[class.ng-dirty]': 'ngClassDirty', '[class.ng-dirty]': 'ngClassDirty',
'[class.ng-valid]': 'ngClassValid', '[class.ng-valid]': 'ngClassValid',
'[class.ng-invalid]': 'ngClassInvalid', '[class.ng-invalid]': 'ngClassInvalid',
'[class.ng-pending]': 'ngClassPending' '[class.ng-pending]': 'ngClassPending',
}; };
/** /**

View File

@ -9,7 +9,6 @@
import {Directive, Inject, Optional, Self, forwardRef} from '@angular/core'; import {Directive, Inject, Optional, Self, forwardRef} from '@angular/core';
import {EventEmitter} from '../facade/async'; import {EventEmitter} from '../facade/async';
import {isPresent} from '../facade/lang';
import {AbstractControl, FormControl, FormGroup} from '../model'; import {AbstractControl, FormControl, FormGroup} from '../model';
import {NG_ASYNC_VALIDATORS, NG_VALIDATORS} from '../validators'; import {NG_ASYNC_VALIDATORS, NG_VALIDATORS} from '../validators';
@ -103,7 +102,7 @@ export class NgForm extends ControlContainer implements Form {
removeControl(dir: NgModel): void { removeControl(dir: NgModel): void {
resolvedPromise.then(() => { resolvedPromise.then(() => {
const container = this._findContainer(dir.path); const container = this._findContainer(dir.path);
if (isPresent(container)) { if (container) {
container.removeControl(dir.name); container.removeControl(dir.name);
} }
}); });
@ -122,7 +121,7 @@ export class NgForm extends ControlContainer implements Form {
removeFormGroup(dir: NgModelGroup): void { removeFormGroup(dir: NgModelGroup): void {
resolvedPromise.then(() => { resolvedPromise.then(() => {
const container = this._findContainer(dir.path); const container = this._findContainer(dir.path);
if (isPresent(container)) { if (container) {
container.removeControl(dir.name); container.removeControl(dir.name);
} }
}); });

View File

@ -11,7 +11,7 @@ import {AbstractControl} from '../model';
import {AsyncValidatorFn, Validator, ValidatorFn} from './validators'; import {AsyncValidatorFn, Validator, ValidatorFn} from './validators';
export function normalizeValidator(validator: ValidatorFn | Validator): ValidatorFn { export function normalizeValidator(validator: ValidatorFn | Validator): ValidatorFn {
if ((<Validator>validator).validate !== undefined) { if ((<Validator>validator).validate) {
return (c: AbstractControl) => (<Validator>validator).validate(c); return (c: AbstractControl) => (<Validator>validator).validate(c);
} else { } else {
return <ValidatorFn>validator; return <ValidatorFn>validator;
@ -19,7 +19,7 @@ export function normalizeValidator(validator: ValidatorFn | Validator): Validato
} }
export function normalizeAsyncValidator(validator: AsyncValidatorFn | Validator): AsyncValidatorFn { export function normalizeAsyncValidator(validator: AsyncValidatorFn | Validator): AsyncValidatorFn {
if ((<Validator>validator).validate !== undefined) { if ((<Validator>validator).validate) {
return (c: AbstractControl) => (<Validator>validator).validate(c); return (c: AbstractControl) => (<Validator>validator).validate(c);
} else { } else {
return <AsyncValidatorFn>validator; return <AsyncValidatorFn>validator;

View File

@ -8,8 +8,6 @@
import {Directive, ElementRef, Renderer, forwardRef} from '@angular/core'; import {Directive, ElementRef, Renderer, forwardRef} from '@angular/core';
import {isBlank} from '../facade/lang';
import {ControlValueAccessor, NG_VALUE_ACCESSOR} from './control_value_accessor'; import {ControlValueAccessor, NG_VALUE_ACCESSOR} from './control_value_accessor';
export const NUMBER_VALUE_ACCESSOR: any = { export const NUMBER_VALUE_ACCESSOR: any = {
@ -45,7 +43,7 @@ export class NumberValueAccessor implements ControlValueAccessor {
writeValue(value: number): void { writeValue(value: number): void {
// The value needs to be normalized for IE9, otherwise it is set to 'null' when null // The value needs to be normalized for IE9, otherwise it is set to 'null' when null
const normalizedValue = isBlank(value) ? '' : value; const normalizedValue = value == null ? '' : value;
this._renderer.setElementProperty(this._elementRef.nativeElement, 'value', normalizedValue); this._renderer.setElementProperty(this._elementRef.nativeElement, 'value', normalizedValue);
} }

View File

@ -29,13 +29,12 @@ export class RadioControlRegistry {
} }
remove(accessor: RadioControlValueAccessor) { remove(accessor: RadioControlValueAccessor) {
var indexToRemove = -1; for (let i = this._accessors.length - 1; i >= 0; --i) {
for (var i = 0; i < this._accessors.length; ++i) {
if (this._accessors[i][1] === accessor) { if (this._accessors[i][1] === accessor) {
indexToRemove = i; this._accessors.splice(i, 1);
return;
} }
} }
this._accessors.splice(indexToRemove, 1);
} }
select(accessor: RadioControlValueAccessor) { select(accessor: RadioControlValueAccessor) {
@ -99,7 +98,7 @@ export class RadioControlValueAccessor implements ControlValueAccessor,
/** @internal */ /** @internal */
_fn: Function; _fn: Function;
onChange = () => {}; onChange = () => {};
onTouched = () => {} onTouched = () => {};
@Input() name: string; @Input() name: string;
@Input() formControlName: string; @Input() formControlName: string;

View File

@ -8,7 +8,7 @@
import {Directive, ElementRef, Host, Input, OnDestroy, Optional, Renderer, forwardRef} from '@angular/core'; import {Directive, ElementRef, Host, Input, OnDestroy, Optional, Renderer, forwardRef} from '@angular/core';
import {isBlank, isPresent, isPrimitive, looseIdentical} from '../facade/lang'; import {isPrimitive, looseIdentical} from '../facade/lang';
import {ControlValueAccessor, NG_VALUE_ACCESSOR} from './control_value_accessor'; import {ControlValueAccessor, NG_VALUE_ACCESSOR} from './control_value_accessor';
@ -19,7 +19,7 @@ export const SELECT_VALUE_ACCESSOR: any = {
}; };
function _buildValueString(id: string, value: any): string { function _buildValueString(id: string, value: any): string {
if (isBlank(id)) return `${value}`; if (id == null) return `${value}`;
if (!isPrimitive(value)) value = 'Object'; if (!isPrimitive(value)) value = 'Object';
return `${id}: ${value}`.slice(0, 50); return `${id}: ${value}`.slice(0, 50);
} }
@ -86,7 +86,7 @@ export class SelectControlValueAccessor implements ControlValueAccessor {
writeValue(value: any): void { writeValue(value: any): void {
this.value = value; this.value = value;
var valueString = _buildValueString(this._getOptionId(value), value); const valueString = _buildValueString(this._getOptionId(value), value);
this._renderer.setElementProperty(this._elementRef.nativeElement, 'value', valueString); this._renderer.setElementProperty(this._elementRef.nativeElement, 'value', valueString);
} }
@ -115,8 +115,8 @@ export class SelectControlValueAccessor implements ControlValueAccessor {
/** @internal */ /** @internal */
_getOptionValue(valueString: string): any { _getOptionValue(valueString: string): any {
let value = this._optionMap.get(_extractId(valueString)); const value = this._optionMap.get(_extractId(valueString));
return isPresent(value) ? value : valueString; return value != null ? value : valueString;
} }
} }
@ -136,7 +136,7 @@ export class NgSelectOption implements OnDestroy {
constructor( constructor(
private _element: ElementRef, private _renderer: Renderer, private _element: ElementRef, private _renderer: Renderer,
@Optional() @Host() private _select: SelectControlValueAccessor) { @Optional() @Host() private _select: SelectControlValueAccessor) {
if (isPresent(this._select)) this.id = this._select._registerOption(); if (this._select) this.id = this._select._registerOption();
} }
@Input('ngValue') @Input('ngValue')
@ -150,7 +150,7 @@ export class NgSelectOption implements OnDestroy {
@Input('value') @Input('value')
set value(value: any) { set value(value: any) {
this._setElementValue(value); this._setElementValue(value);
if (isPresent(this._select)) this._select.writeValue(this._select.value); if (this._select) this._select.writeValue(this._select.value);
} }
/** @internal */ /** @internal */
@ -159,7 +159,7 @@ export class NgSelectOption implements OnDestroy {
} }
ngOnDestroy() { ngOnDestroy() {
if (isPresent(this._select)) { if (this._select) {
this._select._optionMap.delete(this.id); this._select._optionMap.delete(this.id);
this._select.writeValue(this._select.value); this._select.writeValue(this._select.value);
} }

View File

@ -8,7 +8,7 @@
import {Directive, ElementRef, Host, Input, OnDestroy, OpaqueToken, Optional, Renderer, Type, forwardRef} from '@angular/core'; import {Directive, ElementRef, Host, Input, OnDestroy, OpaqueToken, Optional, Renderer, Type, forwardRef} from '@angular/core';
import {isBlank, isPresent, isPrimitive, looseIdentical} from '../facade/lang'; import {isPrimitive, looseIdentical} from '../facade/lang';
import {ControlValueAccessor, NG_VALUE_ACCESSOR} from './control_value_accessor'; import {ControlValueAccessor, NG_VALUE_ACCESSOR} from './control_value_accessor';
@ -19,7 +19,7 @@ export const SELECT_MULTIPLE_VALUE_ACCESSOR = {
}; };
function _buildValueString(id: string, value: any): string { function _buildValueString(id: string, value: any): string {
if (isBlank(id)) return `${value}`; if (id == null) return `${value}`;
if (typeof value === 'string') value = `'${value}'`; if (typeof value === 'string') value = `'${value}'`;
if (!isPrimitive(value)) value = 'Object'; if (!isPrimitive(value)) value = 'Object';
return `${id}: ${value}`.slice(0, 50); return `${id}: ${value}`.slice(0, 50);
@ -78,19 +78,19 @@ export class SelectMultipleControlValueAccessor implements ControlValueAccessor
let selected: Array<any> = []; let selected: Array<any> = [];
if (_.hasOwnProperty('selectedOptions')) { if (_.hasOwnProperty('selectedOptions')) {
let options: HTMLCollection = _.selectedOptions; let options: HTMLCollection = _.selectedOptions;
for (var i = 0; i < options.length; i++) { for (let i = 0; i < options.length; i++) {
let opt: any = options.item(i); const opt: any = options.item(i);
let val: any = this._getOptionValue(opt.value); const val: any = this._getOptionValue(opt.value);
selected.push(val); selected.push(val);
} }
} }
// Degrade on IE // Degrade on IE
else { else {
let options: HTMLCollection = <HTMLCollection>_.options; let options: HTMLCollection = <HTMLCollection>_.options;
for (var i = 0; i < options.length; i++) { for (let i = 0; i < options.length; i++) {
let opt: HTMLOption = options.item(i); let opt: HTMLOption = options.item(i);
if (opt.selected) { if (opt.selected) {
let val: any = this._getOptionValue(opt.value); const val: any = this._getOptionValue(opt.value);
selected.push(val); selected.push(val);
} }
} }
@ -121,8 +121,8 @@ export class SelectMultipleControlValueAccessor implements ControlValueAccessor
/** @internal */ /** @internal */
_getOptionValue(valueString: string): any { _getOptionValue(valueString: string): any {
let opt = this._optionMap.get(_extractId(valueString)); const opt = this._optionMap.get(_extractId(valueString));
return isPresent(opt) ? opt._value : valueString; return opt ? opt._value : valueString;
} }
} }
@ -146,7 +146,7 @@ export class NgSelectMultipleOption implements OnDestroy {
constructor( constructor(
private _element: ElementRef, private _renderer: Renderer, private _element: ElementRef, private _renderer: Renderer,
@Optional() @Host() private _select: SelectMultipleControlValueAccessor) { @Optional() @Host() private _select: SelectMultipleControlValueAccessor) {
if (isPresent(this._select)) { if (this._select) {
this.id = this._select._registerOption(this); this.id = this._select._registerOption(this);
} }
} }
@ -161,7 +161,7 @@ export class NgSelectMultipleOption implements OnDestroy {
@Input('value') @Input('value')
set value(value: any) { set value(value: any) {
if (isPresent(this._select)) { if (this._select) {
this._value = value; this._value = value;
this._setElementValue(_buildValueString(this.id, value)); this._setElementValue(_buildValueString(this.id, value));
this._select.writeValue(this._select.value); this._select.writeValue(this._select.value);
@ -181,7 +181,7 @@ export class NgSelectMultipleOption implements OnDestroy {
} }
ngOnDestroy() { ngOnDestroy() {
if (isPresent(this._select)) { if (this._select) {
this._select._optionMap.delete(this.id); this._select._optionMap.delete(this.id);
this._select.writeValue(this._select.value); this._select.writeValue(this._select.value);
} }

View File

@ -12,8 +12,6 @@ import {isPresent} from '../facade/lang';
import {AbstractControl} from '../model'; import {AbstractControl} from '../model';
import {NG_VALIDATORS, Validators} from '../validators'; import {NG_VALIDATORS, Validators} from '../validators';
/** /**
* An interface that can be implemented by classes that can act as validators. * An interface that can be implemented by classes that can act as validators.
* *
@ -135,7 +133,7 @@ export class MinLengthValidator implements Validator,
} }
validate(c: AbstractControl): {[key: string]: any} { validate(c: AbstractControl): {[key: string]: any} {
return isPresent(this.minlength) ? this._validator(c) : null; return this.minlength == null ? null : this._validator(c);
} }
registerOnValidatorChange(fn: () => void) { this._onChange = fn; } registerOnValidatorChange(fn: () => void) { this._onChange = fn; }
@ -215,7 +213,7 @@ export const PATTERN_VALIDATOR: any = {
@Directive({ @Directive({
selector: '[pattern][formControlName],[pattern][formControl],[pattern][ngModel]', selector: '[pattern][formControlName],[pattern][formControl],[pattern][ngModel]',
providers: [PATTERN_VALIDATOR], providers: [PATTERN_VALIDATOR],
host: {'[attr.pattern]': 'pattern? pattern : null'} host: {'[attr.pattern]': 'pattern ? pattern : null'}
}) })
export class PatternValidator implements Validator, export class PatternValidator implements Validator,
OnChanges { OnChanges {
@ -234,7 +232,7 @@ export class PatternValidator implements Validator,
} }
validate(c: AbstractControl): {[key: string]: any} { validate(c: AbstractControl): {[key: string]: any} {
return isPresent(this.pattern) ? this._validator(c) : null; return this.pattern ? this._validator(c) : null;
} }
registerOnValidatorChange(fn: () => void) { this._onChange = fn; } registerOnValidatorChange(fn: () => void) { this._onChange = fn; }