import { Component, OnInit, Input, forwardRef, NgModule, TemplateRef, Output, EventEmitter, ViewChild, ViewContainerRef, ElementRef, ContentChild, SimpleChange } from '@angular/core';
import { ControlValueAccessor, NG_VALUE_ACCESSOR, FormsModule } from "@angular/forms";
import { CommonModule, NgForOfContext } from "@angular/common";
import { NgZorroAntdModule } from 'ng-zorro-antd';
import { Subject } from 'rxjs/Rx';
import { ApiService } from '../services/api.service';
export interface Shipper {
name: string;
code: string;
idBak?: string;
mobile?: string;
disabled?: boolean;
}
export const CUSTOM_INPUT_CONTROL_VALUE_ACCESSOR: any = {
provide: NG_VALUE_ACCESSOR,
useExisting: forwardRef(() => ShipperSelectComponent),
multi: true
};
@Component({
selector: `yzt-shipper`,
template: `
`,
styles: [`
.shipper-select{
position: relative;
}
.close-icon {
opacity: 0;
position: absolute;
right: 20px;
top: 50%;
margin-top: -10px;
}
.close-icon:hover {
opacity: 1;
}
.shipper-nz-select:hover +.close-icon {
opacity: 1;
}
:host ::ng-deep .ant-select-disabled .ant-select-selection {
background: #f0f0f0;
color: #2b2b2b;
}
`],
providers: [CUSTOM_INPUT_CONTROL_VALUE_ACCESSOR]
})
export class ShipperSelectComponent implements ControlValueAccessor, OnInit {
private onTouchedCallback: () => () => {};
private onChangeCallback: (_: any) => () => {};
options: Array = [];
// 单选的时候传字符串,多选传数组
_value: string;
_width = "100%";
_content: TemplateRef;
_allowClear = true;
_nzMode = "combobox";
// 下拉过滤含关键字选项,false为不过滤
_filter = false;
currentText = '';
canQuery = true;
keyWordStream = new Subject()
keyWord$: any;
firstNum = 1;
loading = false;
@Input() userCode = "";
@Input() placeholder = "请输入发货人";
@Input() rowsNum = 10;
@Input() valueType = "";
@Input() protocolType = "";
@Input() yztDisabled = false;
set value(v: any) {
if (typeof v === 'string' && v.trim() === '' || !v) {
this.canQuery = true;
this.firstNum = 1;
this.options = [];
this.currentText = '';
this.userCode = '';
this.queryData('', []);
this.onChangeCallback(null);
return;
}
this._value = v;
// 双向绑定获取对象
if (this.valueType === "object") {
const { consignorCode, name } = v;
this.onChangeCallback({ value: consignorCode, label: name });
} else if (this.valueType === "fullObject") {
this.onChangeCallback(v);
} else if (this.valueType === "name") {
this.onChangeCallback(v.name);
} else {
this.onChangeCallback(v.userCode);
}
}
get value(): any {
return this._value;
};
@Input() set width(v: any) {
const width = parseInt(v);
this._width = (Array.from(v)).includes("%") ? `${v}%` : isNaN(width) ? this._width : `${width}px`;
}
@Input() set OptionMode(v) {
this._nzMode = v;
this._allowClear = v === "combobox" ? true : false;
};
@Input() set customTemplate(tpl: TemplateRef) {
if (tpl instanceof TemplateRef) {
this._content = tpl;
}
}
@Output() nzOpenChange: EventEmitter = new EventEmitter();
@Output() outOptions: EventEmitter = new EventEmitter();
constructor(private api: ApiService) {
}
ngOnInit() {
// 限流
this.keyWord$ = this.keyWordStream
.debounceTime(250)
.subscribe(word => {
this.queryData(word, [])
});
}
ngOnChanges(changes: any) {
if (changes.userCode && this.userCode) {
this.firstNum = 1;
this.loading = true;
this.canQuery = true;
this.queryData(this.userCode, [], true);
}
}
ngOnDestroy() {
this.keyWord$.unsubscribe()
}
yztSearchChange(event) {
this.canQuery = true;
this.currentText = event;
this.firstNum = 1;
this.keyWordStream.next(event);
}
yztScrollToBottom() {
this.queryData(this.currentText, this.options);
}
yztOpenChange(event) {
this.nzOpenChange.emit(event)
}
/**
* 仅作清空多选选项
*/
clearSelect($event?: MouseEvent): void {
if ($event) {
$event.preventDefault();
$event.stopPropagation();
}
this.options = [];
this.value = '';
}
// 写入值
writeValue(value: any) {
if (value !== this._value) {
this._value = value;
}
}
// 注册变化处理事件
registerOnChange(fn: any) {
this.onChangeCallback = fn;
}
// 注册触摸事件
registerOnTouched(fn: any) {
this.onTouchedCallback = fn;
}
/**
* 查询数据
* @param $event
*/
queryData(searchText?: string, options?: Array, firstIn: boolean = false) {
if (!this.canQuery) return;
const shipperName = searchText;
let params;
if (this.userCode) {
params = [{ "page": this.firstNum, "size": this.rowsNum }, { key: this.userCode }];
} else {
params = [{ "page": this.firstNum, "size": this.rowsNum }, { key: shipperName, protocolType: this.protocolType }];
}
this.api.getConsignorList(params as any).subscribe(json => {
let content = json && json['content'] || {};
let result = content['content'] || []
this.options = options.concat(result);
this.firstNum += 1;
if (firstIn || this.userCode) {
this.value = this.options[0];
}
this.loading = false;
if (result.length < this.rowsNum) {
this.options.push({ code: "empty", name: "没有更多选项!", disabled: true });
this.canQuery = false;
}
})
}
}
@NgModule({
imports: [
CommonModule,
FormsModule,
NgZorroAntdModule,
],
declarations: [
ShipperSelectComponent
],
exports: [ShipperSelectComponent]
})
export class ShipperSelectModule { }