2016-10-19 20:12:13 +03:00
|
|
|
/**
|
|
|
|
* @license
|
|
|
|
* Copyright Google Inc. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by an MIT-style license that can be
|
|
|
|
* found in the LICENSE file at https://angular.io/license
|
|
|
|
*/
|
|
|
|
|
perf: switch angular to use StaticInjector instead of ReflectiveInjector
This change allows ReflectiveInjector to be tree shaken resulting
in not needed Reflect polyfil and smaller bundles.
Code savings for HelloWorld using Closure:
Reflective: bundle.js: 105,864(34,190 gzip)
Static: bundle.js: 154,889(33,555 gzip)
645( 2%)
BREAKING CHANGE:
`platformXXXX()` no longer accepts providers which depend on reflection.
Specifically the method signature when from `Provider[]` to
`StaticProvider[]`.
Example:
Before:
```
[
MyClass,
{provide: ClassA, useClass: SubClassA}
]
```
After:
```
[
{provide: MyClass, deps: [Dep1,...]},
{provide: ClassA, useClass: SubClassA, deps: [Dep1,...]}
]
```
NOTE: This only applies to platform creation and providers for the JIT
compiler. It does not apply to `@Compotent` or `@NgModule` provides
declarations.
Benchpress note: Previously Benchpress also supported reflective
provides, which now require static providers.
DEPRECATION:
- `ReflectiveInjector` is now deprecated as it will be remove. Use
`Injector.create` as a replacement.
closes #18496
2017-08-03 12:33:29 -07:00
|
|
|
import {Directive, ElementRef, Renderer2, StaticProvider, forwardRef} from '@angular/core';
|
2016-10-19 20:12:13 +03:00
|
|
|
|
|
|
|
import {ControlValueAccessor, NG_VALUE_ACCESSOR} from './control_value_accessor';
|
|
|
|
|
perf: switch angular to use StaticInjector instead of ReflectiveInjector
This change allows ReflectiveInjector to be tree shaken resulting
in not needed Reflect polyfil and smaller bundles.
Code savings for HelloWorld using Closure:
Reflective: bundle.js: 105,864(34,190 gzip)
Static: bundle.js: 154,889(33,555 gzip)
645( 2%)
BREAKING CHANGE:
`platformXXXX()` no longer accepts providers which depend on reflection.
Specifically the method signature when from `Provider[]` to
`StaticProvider[]`.
Example:
Before:
```
[
MyClass,
{provide: ClassA, useClass: SubClassA}
]
```
After:
```
[
{provide: MyClass, deps: [Dep1,...]},
{provide: ClassA, useClass: SubClassA, deps: [Dep1,...]}
]
```
NOTE: This only applies to platform creation and providers for the JIT
compiler. It does not apply to `@Compotent` or `@NgModule` provides
declarations.
Benchpress note: Previously Benchpress also supported reflective
provides, which now require static providers.
DEPRECATION:
- `ReflectiveInjector` is now deprecated as it will be remove. Use
`Injector.create` as a replacement.
closes #18496
2017-08-03 12:33:29 -07:00
|
|
|
export const RANGE_VALUE_ACCESSOR: StaticProvider = {
|
2016-10-19 20:12:13 +03:00
|
|
|
provide: NG_VALUE_ACCESSOR,
|
|
|
|
useExisting: forwardRef(() => RangeValueAccessor),
|
|
|
|
multi: true
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2018-11-01 15:23:01 -05:00
|
|
|
* @description
|
|
|
|
* The `ControlValueAccessor` for writing a range value and listening to range input changes.
|
|
|
|
* The value accessor is used by the `FormControlDirective`, `FormControlName`, and `NgModel`
|
|
|
|
* directives.
|
2016-10-19 20:12:13 +03:00
|
|
|
*
|
2018-09-20 15:18:14 +01:00
|
|
|
* @usageNotes
|
2018-11-01 15:23:01 -05:00
|
|
|
*
|
|
|
|
* ### Using a range input with a reactive form
|
|
|
|
*
|
|
|
|
* The following example shows how to use a range input with a reactive form.
|
|
|
|
*
|
|
|
|
* ```ts
|
|
|
|
* const ageControl = new FormControl();
|
|
|
|
* ```
|
2018-09-20 15:18:14 +01:00
|
|
|
*
|
|
|
|
* ```
|
2018-11-01 15:23:01 -05:00
|
|
|
* <input type="range" [formControl]="ageControl">
|
2018-09-20 15:18:14 +01:00
|
|
|
* ```
|
|
|
|
*
|
2018-09-08 17:53:53 +01:00
|
|
|
* @ngModule ReactiveFormsModule
|
2018-11-01 15:23:01 -05:00
|
|
|
* @ngModule FormsModule
|
2016-10-19 20:12:13 +03:00
|
|
|
*/
|
|
|
|
@Directive({
|
|
|
|
selector:
|
|
|
|
'input[type=range][formControlName],input[type=range][formControl],input[type=range][ngModel]',
|
|
|
|
host: {
|
|
|
|
'(change)': 'onChange($event.target.value)',
|
|
|
|
'(input)': 'onChange($event.target.value)',
|
|
|
|
'(blur)': 'onTouched()'
|
|
|
|
},
|
|
|
|
providers: [RANGE_VALUE_ACCESSOR]
|
|
|
|
})
|
|
|
|
export class RangeValueAccessor implements ControlValueAccessor {
|
2018-11-01 15:23:01 -05:00
|
|
|
/**
|
|
|
|
* @description
|
|
|
|
* The registered callback function called when a change or input event occurs on the input
|
|
|
|
* element.
|
|
|
|
*/
|
2016-10-19 20:12:13 +03:00
|
|
|
onChange = (_: any) => {};
|
2018-11-01 15:23:01 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @description
|
|
|
|
* The registered callback function called when a blur event occurs on the input element.
|
|
|
|
*/
|
2016-10-19 20:12:13 +03:00
|
|
|
onTouched = () => {};
|
|
|
|
|
2017-06-20 11:35:16 +02:00
|
|
|
constructor(private _renderer: Renderer2, private _elementRef: ElementRef) {}
|
2016-10-19 20:12:13 +03:00
|
|
|
|
2018-11-01 15:23:01 -05:00
|
|
|
/**
|
|
|
|
* Sets the "value" property on the input element.
|
|
|
|
*
|
|
|
|
* @param value The checked value
|
|
|
|
*/
|
2016-10-19 20:12:13 +03:00
|
|
|
writeValue(value: any): void {
|
2017-06-20 11:35:16 +02:00
|
|
|
this._renderer.setProperty(this._elementRef.nativeElement, 'value', parseFloat(value));
|
2016-10-19 20:12:13 +03:00
|
|
|
}
|
|
|
|
|
2018-11-01 15:23:01 -05:00
|
|
|
/**
|
|
|
|
* @description
|
|
|
|
* Registers a function called when the control value changes.
|
|
|
|
*
|
|
|
|
* @param fn The callback function
|
|
|
|
*/
|
2017-04-17 11:13:30 -07:00
|
|
|
registerOnChange(fn: (_: number|null) => void): void {
|
2016-10-19 20:12:13 +03:00
|
|
|
this.onChange = (value) => { fn(value == '' ? null : parseFloat(value)); };
|
|
|
|
}
|
|
|
|
|
2018-11-01 15:23:01 -05:00
|
|
|
/**
|
|
|
|
* @description
|
|
|
|
* Registers a function called when the control is touched.
|
|
|
|
*
|
|
|
|
* @param fn The callback function
|
|
|
|
*/
|
2016-10-19 20:12:13 +03:00
|
|
|
registerOnTouched(fn: () => void): void { this.onTouched = fn; }
|
|
|
|
|
2018-11-01 15:23:01 -05:00
|
|
|
/**
|
|
|
|
* Sets the "disabled" property on the range input element.
|
|
|
|
*
|
|
|
|
* @param isDisabled The disabled value
|
|
|
|
*/
|
2016-10-19 20:12:13 +03:00
|
|
|
setDisabledState(isDisabled: boolean): void {
|
2017-06-20 11:35:16 +02:00
|
|
|
this._renderer.setProperty(this._elementRef.nativeElement, 'disabled', isDisabled);
|
2016-10-19 20:12:13 +03:00
|
|
|
}
|
|
|
|
}
|