1 import {Component, EventEmitter, forwardRef, Input, OnInit, Output} from '@angular/core';
2 import {ControlValueAccessor, NG_VALUE_ACCESSOR} from '@angular/forms';
3 import {BooleanFieldValue} from '../core/boolean-field-value';
7 export const PX_TEXT_INPUT_IP_ADDRESS_CONTROL_VALUE_ACCESSOR: any = {
8 provide: NG_VALUE_ACCESSOR,
9 useExisting: forwardRef(() => PlxTextInputIpAddressComponent),
13 selector: 'plx-text-input-ip-address',
16 <plx-text-input #textInputIpAddress type="text" [(ngModel)]="ipValue"
17 [width]="'280px'" [numberShowSpinner]="false" minlength="1"
18 (keyup)="keyUp($event)" (paste)="paste($event)" class="{{sizeClass}}"></plx-text-input>
19 <div class="plx-text-input-error">{{errMsg}}</div>
22 styleUrls: ['text-input.less'],
23 host: {'style': 'display: inline-block;'},
24 providers: [PX_TEXT_INPUT_IP_ADDRESS_CONTROL_VALUE_ACCESSOR]
27 export class PlxTextInputIpAddressComponent implements OnInit, ControlValueAccessor {
28 @Input() lang: string = 'zh'; //zh|en
29 @Input() size: string; //空代表普通尺寸,sm代表小尺寸
30 @Input() ipAddressCheckTip: string = ''; //
32 @Input() @BooleanFieldValue() required: boolean = false;
34 @Input() public ipValue: string;
35 @Output() public ipValueChange: EventEmitter<any> = new EventEmitter<any>();
37 @Input() public ipValueFlg : boolean;
38 @Output() public ipValueFlgChange: EventEmitter<any> = new EventEmitter<any>();
40 private isNull : boolean = true;
42 /** Callback registered via registerOnTouched (ControlValueAccessor) */
43 private _onTouchedCallback: () => void = noop;
44 /** Callback registered via registerOnChange (ControlValueAccessor) */
45 private _onChangeCallback: (_: any) => void = noop;
50 'invalidate': 'IP格式不对',
51 'range': '请输入正确的IPV4地址或IPV6地址',
52 'range-IPV4': '请输入正确的IPV4',
53 'range-IPV6': '请输入正确的IPV6'
56 'empty': 'IP can not be empty',
57 'invalidate': 'IP format is incorrect',
58 'range': 'IP range is IPV4 or IPV6',
59 'range-IPV4': 'IP range is IPV4',
60 'range-IPV6': 'IP range is IPV6'
63 public errMsg: string;
64 public sizeClass: string;
70 if (this.size === 'sm') {
71 this.sizeClass = 'plx-input-sm';
73 this.isNull = this.ipValueFlg;
74 if(this.repIPStr(this.ipValue) === ''&& !this.ipValueFlg){
75 this.ipValueFlg = false;
80 public keyUp(event: any): void {
81 this.setValueToOutside(this.validate());
85 public paste(event: any): void{
87 this.ipValue = event.target.value;
88 this.setValueToOutside(this.validate());
94 this.ipValueChange.emit(this.ipValue);
95 this.ipValueFlgChange.emit(this.ipValueFlg);
98 private setValueToOutside(validateFlg: boolean): void {
99 this.ipValueFlg = validateFlg;
103 value = this.ipValue;
105 if(this.ipValue === "" && !this.isNull){
106 this.ipValueFlg = false;
111 this._onChangeCallback(value);
114 writeValue(value: any): void {
117 this.ipValue = value;
123 registerOnChange(fn: any) {
124 this._onChangeCallback = fn;
127 registerOnTouched(fn: any) {
128 this._onTouchedCallback = fn;
131 public validate(): boolean {
135 this.errMsg = this.errMsgs[this.lang]['empty'];
139 if ((this.ipValue) && (!this.ipValue)) {
140 this.errMsg = this.errMsgs[this.lang]['invalidate'];
143 let blackStr = this.repIPStr(this.ipValue);
144 if(this.ipAddressCheckTip === ''){
145 if(this.ipValue !== '' && blackStr === ''){
146 this.errMsg = this.errMsgs[this.lang]['range'];
150 if(this.ipValue !== '' && this.ipAddressCheckTip !== blackStr) {
151 this.errMsg = this.errMsgs[this.lang]['range-'+ this.ipAddressCheckTip];
158 private repIPStr(value: any): string {
160 var regip4 = /^((25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(25[0-5]|2[0-4]\d|[01]?\d\d?)$/;
161 if (regip4.test(value)) {
164 var regip6 = /^\s*((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}|((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}: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