|
| 1 | +import {Component, OnInit, Provider, forwardRef,} from "angular2/core"; |
| 2 | +import {A11yHelper} from "../services/a11y-helper.service"; |
| 3 | +import {NG_VALUE_ACCESSOR, ControlValueAccessor} from "angular2/common"; |
| 4 | + |
| 5 | +// #docregion |
| 6 | +const noop = () => { |
| 7 | +}; |
| 8 | + |
| 9 | +const A11Y_CUSTOM_CONTROL_VALUE_ACCESSOR = new Provider( |
| 10 | + NG_VALUE_ACCESSOR, { |
| 11 | + useExisting: forwardRef(() => A11yCustomControl), |
| 12 | + multi: true |
| 13 | + }); |
| 14 | + |
| 15 | +@Component({ |
| 16 | + selector: 'a11y-custom-control', |
| 17 | + templateUrl: './app/form-controls/a11y-custom-control.component.html', |
| 18 | + styleUrls: ['./app/form-controls/a11y-custom-control.component.css'], |
| 19 | + providers: [A11Y_CUSTOM_CONTROL_VALUE_ACCESSOR] |
| 20 | +}) |
| 21 | +export class A11yCustomControl implements OnInit, ControlValueAccessor { |
| 22 | + |
| 23 | + uniqueId:string; |
| 24 | + |
| 25 | + private _innerValue:any = ''; |
| 26 | + outerValue:string = ''; |
| 27 | + |
| 28 | + private _onTouchedCallback:() => void = noop; |
| 29 | + private _onChangeCallback:(_:any) => void = noop; |
| 30 | + |
| 31 | + constructor(private _a11yHelper:A11yHelper) { |
| 32 | + } |
| 33 | + |
| 34 | + onChange(event:any, value:string) { |
| 35 | + if (event.keyCode == 13) { |
| 36 | + event.preventDefault(); |
| 37 | + } |
| 38 | + else { |
| 39 | + this._innerValue = this._a11yHelper.removeHtmlStringBreaks(value); |
| 40 | + this._onChangeCallback(this._innerValue); |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + onBlur(){ |
| 45 | + this._onTouchedCallback(); |
| 46 | + } |
| 47 | + |
| 48 | + writeValue(value:any) { |
| 49 | + if (value != this._innerValue) { |
| 50 | + this._innerValue = value; |
| 51 | + this.outerValue = value; |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + registerOnChange(fn:any) { |
| 56 | + this._onChangeCallback = fn; |
| 57 | + } |
| 58 | + |
| 59 | + registerOnTouched(fn:any) { |
| 60 | + this._onTouchedCallback = fn; |
| 61 | + } |
| 62 | + |
| 63 | + ngOnInit():void { |
| 64 | + this.uniqueId = this._a11yHelper.generateUniqueIdString(); |
| 65 | + } |
| 66 | + |
| 67 | +} |
| 68 | +// #enddocregion |
0 commit comments