import { Component, OnInit, Input, ViewChild, ViewEncapsulation, ElementRef, AfterViewInit, ChangeDetectorRef, Renderer2, NgZone, EventEmitter, Output, Injector, Optional, LOCALE_ID, InjectFlags, HostBinding, OnChanges, SimpleChanges, OnDestroy } from '@angular/core'; import { fromEvent } from 'rxjs'; import { debounceTime, filter } from 'rxjs/operators'; import { animationFrame } from 'rxjs/internal/scheduler/animationFrame'; import { FarrisSidebarComponent } from '@farris/ui-sidebar'; import { LocaleService } from '@farris/ui-locale'; import { FilterPanelService } from '@farris/ui-filter-panel'; import { QuerySolutionService } from '@farris/ui-filter-condition'; import { FilterService } from './filter.service'; import { ConvertorService } from './service/convertorservice'; import { QueryCondition } from './entity/querycondition'; import { ControlType } from './entity/controltype'; import { TextValue } from './entity/conditionvalue/textvalue'; import { HelpValue } from './entity/conditionvalue/helpvalue'; import { DropDownListValue } from './entity/conditionvalue/dropdownlistvalue'; import { DateRangeValue } from './entity/conditionvalue/daterangevalue'; import { DateRangeTimeValue } from './entity/conditionvalue/daterangetimevalue'; import { NumberRangeValue } from './entity/conditionvalue/numberrangevaue'; import { SearchValue } from './entity/conditionvalue/searchvalue'; import { DateValue } from './entity/conditionvalue/datevalue'; import { CheckboxGroupValue } from './entity/conditionvalue/checkboxgroupvalue'; import { FieldConfig } from './entity/fieldconfig/fieldconfig'; import { FilterHandler, FILTER_HANDLER_TOKEN } from './interface/filterHandler'; import { deepCopy } from './utils'; import { DropDownControl } from './entity/controltype/dropdownlist/dropdowncontrol'; import { CheckboxGroupControl } from './entity/controltype/checkbox/checkboxcontrol'; import { RadioGroupValue } from './entity/conditionvalue/radiovalue'; import { RadioControl } from './entity/controltype/radio/radiocontrol'; import { MonthRangeValue } from './entity/conditionvalue/monthrange'; import { YearRangeValue } from './entity/conditionvalue/yearrange'; import { MonthValue } from './entity/conditionvalue/month'; import { YearValue } from './entity/conditionvalue/yearvalue'; import { InputGroupValue } from './entity/conditionvalue/inputgroup'; import { NumberValue } from './entity/conditionvalue/numbervalue'; import { FlexibleNumberValue } from './entity/conditionvalue/flexiblenumbervalue'; import { FlexibleDateValue } from './entity/conditionvalue/flexibledatevalue'; import ResizeObserver from "resize-observer-polyfill"; @Component({ selector: 'farris-filter', templateUrl: './filter.component.html', encapsulation: ViewEncapsulation.None, providers: [ FilterPanelService ] }) export class FilterComponent implements OnInit, AfterViewInit, OnChanges, OnDestroy { _filterList: Array = []; @Input() get filterList(): Array { return this._filterList; } set filterList(filterList: Array) { this._filterList = filterList || []; this.initFilterList(); } //是否禁用 @Input() disabled = false; @Input() showReminder = false; // 是否启用侧边栏収折-过滤条件在侧边栏显示 @Input() floatFilter = true; // 启用自动宽度 private _enableAutoWidth = false; @Input() set enableAutoWidth(value) { if (value !== this._enableAutoWidth) { if (value) { this.filterEllipsis = false; this.filterExtendShow = false; } this._enableAutoWidth = value; } } get enableAutoWidth() { return this._enableAutoWidth; } @Input() localStorageKey: string = 'farrisSearchRecord'; //初始化值 _defaultValue: FieldConfig[]; @Input() set defaultValue(configs: FieldConfig[]) { this._defaultValue = configs; setTimeout(() => { this.setFilterValue(this._defaultValue); }); } get defaultValue() { return this._defaultValue; } @HostBinding('class.f-filter') cla = true; @Output() query = new EventEmitter(); @Output() searchChange = new EventEmitter(); @Output() resetChange = new EventEmitter(); @Output() clearFilter = new EventEmitter(); @Output() expandFilter = new EventEmitter(); @Output() packupFilter = new EventEmitter(); @Output() filterResize = new EventEmitter(); @ViewChild('filterMain') filterMain: ElementRef; @ViewChild('filterTool') filterTool: ElementRef; @ViewChild('filterWrapper') filterWrapper: ElementRef; @ViewChild('farrissidebar') farrissidebar: FarrisSidebarComponent; @ViewChild('listWrapper') listWrapper: ElementRef; @ViewChild('filterListWrapper') filterListWrapper: ElementRef; currentFilterId; // 展开 filterExtend = false; // 是否显示三个点 filterEllipsis = false; // 是否显示展开收起 filterExtendShow = false; filterExtendText = '展开'; filterExtendIcon = 'f-icon-arrow-chevron-down'; expandText = '展开'; foldText = '收起'; filterWrapperEl; filterToolWidth: number; sidebarOpen = false; floatPanelOpen = false; showExpandTag = false; /** 表单是否允许提交 */ canSubmit = false; /** 表单是否允许提交,用于高级筛选清空后 */ advancedCanSubmit = true; /** 默认展开项是否允许提交 */ showCanSubmit = true; /** 是否显示清空已选 */ filledFilter = false; /** 默认展开的筛选项 */ showFilterList: Array = new Array(); /** 高级筛选 */ extendFilterList: Array = new Array(); extendFilterListStore: Array = new Array(); extendFilterListTemporary: Array = new Array(); /** 已填写筛选数据 */ filledFilterList: Array = new Array(); /** 高级筛选中已填写的 */ filledExtendFilterList: Array = new Array(); /** 没有填写的筛选项 */ // unFilledFilterList:Array = new Array(); /** 高级筛选中必填项 */ extendRequiredList: Array = new Array(); /** 默认筛选中必填项 */ showRequiredList: Array = new Array(); /** 循环表单 */ listFilterConditions: Array = new Array(); /** 默认展开+高级筛选中已填写的 */ loopFilterList: Array = new Array(); /** 用来存储setvalue前数据 */ stagListFilterConditions: Array = new Array(); /** 显示中表单的必填项 */ requiredListFilter: Array = []; localeIds = { 'en': 'en', 'zh-CHS': 'zh-CHS' }; currentLocale = 'zh-CHS'; isControlInline = true; /** * 组件服务接口 */ filterHandler: FilterHandler; filterPanelContainer; showReminderItem: QueryCondition; reminderLeft = 0; // 绑定大小变化响应 private ro: ResizeObserver | null = null; // 记录旧的宽度数据 private containerWidthRecord = 0; // 阈值 private distanceThreshold = 20; constructor( private filterPanelService: FilterPanelService, private filterService: FilterService, private convertorService: ConvertorService, private handler: QuerySolutionService, private cd: ChangeDetectorRef, private render: Renderer2, public ngzone: NgZone, public localeService: LocaleService, @Optional() private injector: Injector ) { if (this.injector) { this.currentLocale = this.injector.get(LOCALE_ID); this.filterHandler = this.injector.get(FILTER_HANDLER_TOKEN, null, InjectFlags.Optional); } } ngOnInit() { this.filterWrapperEl = this.filterListWrapper && this.filterListWrapper.nativeElement; // 绑定事件 this.bindRoEvent(); this.foldText = this.localeService.getValue('filterPanel.fold'); this.expandText = this.localeService.getValue('filterPanel.expand'); this.filterExtendText = this.expandText; if (this.currentLocale === this.localeIds['en']) { this.isControlInline = false; } else { this.isControlInline = true; } } ngOnDestroy() { if (this.ro) { this.ro.unobserve(this.filterWrapperEl); this.ro = null; } } /** * 绑定监控大小的事件 */ private bindRoEvent(){ if(!this.filterWrapper){ return; } this.ngzone.runOutsideAngular(() => { // 这样一直都绑定可能有性能问题 this.ro = new ResizeObserver((entries, observer) => { const tempWidth = entries[0].contentRect.width; if ( Math.abs(tempWidth - this.containerWidthRecord) > this.distanceThreshold ) { if (!this.filterList.length) return; if (this.enableAutoWidth) { return; } this.listEllipsisInit(); this.containerWidthRecord = tempWidth; } }); this.ro.observe(this.filterWrapper.nativeElement); // fromEvent(window, 'resize') // .pipe(debounceTime(50, animationFrame)) // .subscribe((): void => { // this.ngzone.run(() => { // // console.log('resize'); // if (!this.filterList.length) return; // if (this.enableAutoWidth) {return;} // this.listEllipsisInit(); // }); // }); }); } ngOnChanges(changes: SimpleChanges) { // if (changes.filterList && !changes.filterList.isFirstChange()) { // this.initFilterList(); // } } ngAfterViewInit() { } /** * 初始化筛选项 */ initFilterList() { this.listFilterConditions = new Array(); const filterDatas = this.filterService.fieldSchemasConvert(this.filterList); if (filterDatas && filterDatas.length) { this.listFilterConditions = this.convertorService.getSelectedFieldConditions(filterDatas, this.listFilterConditions); } this.stagListFilterConditions = this.listFilterConditions.map(o => ({ ...o })); this.filterListClassify(); setTimeout(() => { if (this.showReminder && this.showFilterList && this.showFilterList.length) { this.showReminderItem = this.showFilterList[0]; if (this.showReminderItem) { let filterlistItems = this.filterListWrapper && this.filterListWrapper.nativeElement.children; if (filterlistItems && filterlistItems.length) { let showRemItem; for (let i = 0; i < filterlistItems.length; i++) { if (this.showReminderItem['id'] == filterlistItems[i].getAttribute('id')) { showRemItem = filterlistItems[i]; } } if (showRemItem) { this.reminderLeft = showRemItem.offsetLeft >= 0 ? showRemItem.offsetLeft : 0; } } } } else { this.showReminderItem = null; } }, 0); this.cd.detectChanges(); } /** 筛选项分类 */ filterListClassify() { this.extendFilterList = []; this.extendFilterListStore = []; this.showFilterList = []; this.filledFilterList = []; this.filledExtendFilterList = []; this.loopFilterList = []; this.extendRequiredList = []; this.showRequiredList = []; // 必填 let requiredList = this.listFilterConditions.filter((listitem) => { return listitem.control.required; }); if (requiredList && requiredList.length) { this.requiredListFilter = requiredList; this.canSubmit = this.canSubmitFilter(this.requiredListFilter); } else { this.requiredListFilter = []; this.canSubmit = true; } // 几种类型分类 this.listFilterConditions.forEach(field => { if (field.control.isExtend && field['filled']) { this.filledExtendFilterList.push(field); } if (field['filled']) { this.filledFilterList.push(field); } // else if(!field.control.isExtend){ // this.unFilledFilterList.push(field); // } if (field.control.isExtend) { this.extendFilterListStore.push(field); } else { this.showFilterList.push(field); } }); if (this.filledFilterList.length) { this.filledFilter = true; } else { this.filledFilter = false; } const list = this.listFilterConditions.filter(item => { return !item.control.isExtend || item['filled'] }); // this.loopFilterList = this.showFilterList.concat(this.filledExtendFilterList); this.loopFilterList = list ? list : []; this.loopFilterList.forEach(item => { this.getValueText(item); }); this.extendFilterList = this.extendFilterListStore.map(o => ({ ...o })); const extendRequiredList = this.extendFilterList.filter((listitem) => { return listitem.control.required; }); if (extendRequiredList && extendRequiredList.length) { this.extendRequiredList = extendRequiredList; } const showrequiredList = this.showFilterList.filter((listitem) => { return listitem.control.required; }); if (showrequiredList && showrequiredList.length) { this.showRequiredList = showrequiredList; } this.showCanSubmit = this.canSubmitFilter(this.showRequiredList); this.advancedCanSubmit = this.canSubmitFilter(this.extendRequiredList); // console.log('已填写',this.filledExtendFilterList); // console.log('显示出来的',this.loopFilterList); // console.log('隐藏部分',this.extendFilterList); this.filterExtendShow = true; // 如果是支持自动宽度,就是不収折,判断涉及赋值时机 if (!this.enableAutoWidth) { setTimeout(() => { this.listEllipsisInit(); }, 0); } } /** * 已填写筛选项点击 * @param event * @param data */ filterClick(event, data, index) { event.stopPropagation(); if (this.disabled) return; if (data) { this.showReminderItem = null; } if (data && this.currentFilterId != data.id) { const panelItem = deepCopy(data); this.currentFilterId = data.id; const { left, top, height, right } = event.currentTarget.getBoundingClientRect(); this.filterPanelContainer = this.filterPanelService.showPanel({ left: left, top: top + height + 2, right: right, item: panelItem, localStorageKey: this.localStorageKey }); this.filterPanelContainer.hidePanel.subscribe(e => { this.filterPanelService.hidePanel(); this.filterPanelContainer = null; this.currentFilterId = null; }); this.filterPanelContainer.clearFilter.subscribe(e => { this.clearFilter.emit(e); }); //提交弹窗内 const _this = this; this.filterPanelContainer.submit.subscribe(e => { e.filled = (e.valueText && e.valueText.length > 0) ? true : false; _this.loopFilterList[index] = e; let panelListIndex = _this.listFilterConditions.findIndex(item => { return item['id'] == e['id']; }); if (panelListIndex >= 0) { _this.listFilterConditions[panelListIndex] = e; } this.searchChange.emit(this.listFilterConditions); this.inSetCurrentFilterConditions(); this.filterListClassify(); // valueText是有需要计算的,所以延后判断是否显示 let findItem = _this.loopFilterList.find(item => { return item['id'] == e['id']; }); if (findItem) { findItem['filled'] = findItem.valueText && findItem.valueText.length > 0 ? true : false; } this.cd.markForCheck(); this.submit('panelFormData', this.listFilterConditions, e); this.filterPanelService.hidePanel(); this.filterPanelContainer = null; this.currentFilterId = null; }); } else { this.filterPanelService.hidePanel(); this.filterPanelContainer = null; this.currentFilterId = null; } } /** * 清空筛选项 */ filterClear(data, index, event) { event.stopPropagation(); this.filterPanelService.hidePanel(); this.currentFilterId = null; this.resetListCondition(data); this.filterListClassify(); this.cd.markForCheck(); this.searchChange.emit(this.listFilterConditions); this.clearFilter.emit(data); this.inSetCurrentFilterConditions(); this.advancedCanSubmit = this.canSubmitFilter(this.extendRequiredList); this.submit('panelFormData', this.listFilterConditions); } /** * 展开高级筛选 */ advancedFilter() { this.showReminderItem = null; this.extendFilterListTemporary = deepCopy(this.extendFilterList); this.farrissidebar.isOpen = true; } listCancel() { this.extendFilterList = deepCopy(this.extendFilterListTemporary); this.farrissidebar.isOpen = false; this.floatPanelOpen = false; } /** * 打开面板高级筛选 */ floatFilterOpen() { this.floatPanelOpen = !this.floatPanelOpen; if (this.floatPanelOpen) { this.extendFilterListTemporary = deepCopy(this.extendFilterList); } } /** * 列表展示初始化 */ listEllipsisInit() { if (this.enableAutoWidth) return; // if(this.filterExtend) return; const filterMainWidth = this.filterMain.nativeElement.offsetWidth; this.filterToolWidth = 0; // filterTool = this.filterTool.nativeElement.offsetWidth; if (this.filterTool) { this.filterToolWidth = this.filterTool.nativeElement.offsetWidth; } // 筛选条件区域最大宽度 let filterListW; if (this.filterExtend) { filterListW = filterMainWidth - 26; } else { // filterListW = filterMainWidth- 48 - this.filterToolWidth - 26; filterListW = filterMainWidth - 48 - this.filterToolWidth - 26; } // const filterWrapper = this.filterListWrapper.nativeElement; // const filterWrapperWidth = filterWrapper.offsetWidth; // console.log(filterWrapperWidth); const filterItems = this.filterWrapperEl ? this.filterWrapperEl.children : []; let filterItemWidth = 0; if (filterItems && filterItems.length) { for (let i = 0, len = filterItems.length; i < len; i++) { this.render.setStyle(filterItems[i], 'display', 'flex'); let width = filterItems[i].offsetWidth; filterItemWidth += width + 8; if (filterItemWidth >= filterListW && !this.filterExtend) { this.render.setStyle(filterItems[i], 'display', 'none'); } else { this.render.setStyle(filterItems[i], 'display', 'flex'); } // console.log(filterItemWidth,filterListW,i); } } // if(filterItemWidth > this.filterWrapperEl.offsetWidth){ if (filterItemWidth >= filterListW) { this.filterEllipsis = true; this.filterExtendShow = true; } else { this.filterEllipsis = false; this.filterExtendShow = false; } if (filterItems.length) this.filterResize.emit(); this.cd.detectChanges(); } /** * 筛选项展开收起切换 */ filterExpand() { this.showReminderItem = null; this.filterExtend = !this.filterExtend; if (this.filterExtend) { this.filterEllipsis = false; this.filterExtendText = this.foldText; this.filterExtendIcon = 'f-icon-arrow-chevron-up'; const filterItems = this.filterWrapperEl.children; if (filterItems && filterItems.length) { for (let i = 0, len = filterItems.length; i < len; i++) { this.render.setStyle(filterItems[i], 'display', 'flex'); } }; this.render.setStyle(this.filterWrapperEl, 'flex-wrap', 'wrap'); this.expandFilter.emit(); this.cd.detectChanges(); } else { this.filterExtendText = this.expandText; this.filterExtendIcon = 'f-icon-arrow-chevron-down'; setTimeout(() => { this.listEllipsisInit(); }, 0); this.packupFilter.emit(); } } /** * 高级筛选表单项搜索变化 * @param $event * @param item */ onSearch($event, item) { if (this.disabled) { return; } this.advancedCanSubmit = this.canSubmitFilter(this.extendRequiredList); // console.log(this.advancedCanSubmit,this.canSubmit); this.searchChange.emit({ item, conditions: this.listFilterConditions, instance: this }); } /** 判断是否所有必填项都填写完毕 */ canSubmitFilter(requirelist) { let formCanSubmit = false; if (!requirelist || !requirelist.length) { formCanSubmit = true; return formCanSubmit; } else { for (let i = 0; i < requirelist.length; i++) { const item = requirelist[i]; if (item.control.getControlType() === ControlType.Text) { const tv = (item.value as TextValue); if (Object.keys(tv).length < 0 || !tv.value) { formCanSubmit = false; return false; } else { formCanSubmit = true; } } else if (item.control.getControlType() === ControlType.Help) { const shv = (item.value as HelpValue); if (Object.keys(shv).length === 0 || !shv.value || !shv.valueField) { formCanSubmit = false; return false; } else { formCanSubmit = true; } } else if (item.control.getControlType() === ControlType.DropDown) { const ddv = (item.value as DropDownListValue); if (Object.keys(ddv).length === 0 || !ddv.value) { formCanSubmit = false; return false; } else { formCanSubmit = true; } } else if (item.control.getControlType() === ControlType.DateRange) { const drv = (item.value as DateRangeValue); if (Object.keys(drv).length === 0 || (!drv.startTime && !drv.endTime)) { formCanSubmit = false; return false; } else { formCanSubmit = true; } } else if (item.control.getControlType() === ControlType.DateRangeTime) { const drv = (item.value as DateRangeTimeValue); if (Object.keys(drv).length === 0 || (!drv.startTime && !drv.endTime)) { formCanSubmit = false; return false; } else { formCanSubmit = true; } } else if (item.control.getControlType() === ControlType.MonthRange) { const mrv = (item.value as MonthRangeValue); if (Object.keys(mrv).length === 0 || (!mrv.startTime && !mrv.endTime)) { formCanSubmit = false; return false; } else { formCanSubmit = true; } } else if (item.control.getControlType() === ControlType.YearRange) { const yrv = (item.value as YearRangeValue); if (Object.keys(yrv).length === 0 || (!yrv.startTime && !yrv.endTime)) { formCanSubmit = false; return false; } else { formCanSubmit = true; } } else if (item.control.getControlType() === ControlType.NumberRange) { const nrv = (item.value as NumberRangeValue); if (Object.keys(nrv).length === 0 || (nrv.startValue === null && nrv.endValue === null)) { formCanSubmit = false; return false; } else { formCanSubmit = true; } } else if (item.control.getControlType() === ControlType.Search) { const sv = (item.value as SearchValue); if (Object.keys(sv).length < 0 || !sv.value) { formCanSubmit = false; return false; } else { formCanSubmit = true; } } else if (item.control.getControlType() === ControlType.Date) { const dv = (item.value as DateValue); if (Object.keys(dv).length < 0 || !dv.value) { formCanSubmit = false; return false; } else { formCanSubmit = true; } } else if (item.control.getControlType() === ControlType.Month) { const mv = (item.value as MonthValue); if (Object.keys(mv).length < 0 || !mv.value) { formCanSubmit = false; return false; } else { formCanSubmit = true; } } else if (item.control.getControlType() === ControlType.Year) { const yv = (item.value as YearValue); if (Object.keys(yv).length < 0 || !yv.value) { formCanSubmit = false; return false; } else { formCanSubmit = true; } } else if (item.control.getControlType() === ControlType.CheckboxGroup) { const cbgv = (item.value as CheckboxGroupValue); if (Object.keys(cbgv).length === 0 || Object.keys(cbgv.value).length === 0) { formCanSubmit = false; return false; } else { formCanSubmit = true; } } else if (item.control.getControlType() === ControlType.Radio) { const radiov = (item.value as RadioGroupValue); if (Object.keys(radiov).length < 0) { formCanSubmit = false; return false; } else { formCanSubmit = true; } } else if (item.control.getControlType() === ControlType.InputGroup) { const sv = (item.value as InputGroupValue); if (Object.keys(sv).length < 0 || !sv.textValue) { formCanSubmit = false; return false; } else { formCanSubmit = true; } } else if (item.control.getControlType() === ControlType.Number) { const nrv = (item.value as NumberValue); if (Object.keys(nrv).length === 0 || (nrv.value == null)) { formCanSubmit = false; return false; } else { formCanSubmit = true; } } else if (item.control.getControlType() === ControlType.FlexibleNumber) { const flexv = (item.value as FlexibleNumberValue); if (flexv.single) { if (Object.keys(flexv).length < 0 || flexv.value == null) { formCanSubmit = false; return false; } else { formCanSubmit = true; } } else { if (Object.keys(flexv).length === 0 || (flexv.startValue == null && flexv.endValue == null)) { formCanSubmit = false; return false; } else { formCanSubmit = true; } } } else if (item.control.getControlType() === ControlType.FlexibleDate) { const flexdatev = (item.value as FlexibleDateValue); if (flexdatev.single) { if (Object.keys(flexdatev).length < 0 || !flexdatev.value) { formCanSubmit = false; return false; } else { formCanSubmit = true; } } else { if (Object.keys(flexdatev).length === 0 || (!flexdatev.startValue && !flexdatev.endValue)) { formCanSubmit = false; return false; } else { formCanSubmit = true; } } } } return formCanSubmit; } } /** 高级筛选点击确定提交 */ listSearch() { if (this.disabled) { return; }; this.extendFilterList.forEach(field => { if ((field.control.getControlType() != ControlType.Date) && (field.control.getControlType() != ControlType.DateRange) && (field.control.getControlType() != ControlType.DateRangeTime) && (field.control.getControlType() != ControlType.FlexibleDate)) { field.valueText = field.value.getValueText(); } // if(field.control.getControlType() == ControlType.Radio){ // console.log(field); // } field.filled = (field.valueText && field.valueText.length > 0) ? true : false; if (field.control.getControlType() == ControlType.Text) { let farrisSearchRecord = field.valueText; let farrisSearchRecordArray = []; let storagelabelCode = field.labelCode; let filterPanelArray = JSON.parse(localStorage.getItem(this.localStorageKey)); if (Array.isArray(filterPanelArray)) { filterPanelArray = null; } if (!filterPanelArray || !filterPanelArray[storagelabelCode] || !filterPanelArray[storagelabelCode].length && farrisSearchRecord && farrisSearchRecord.length) { farrisSearchRecordArray.push(farrisSearchRecord); } else { farrisSearchRecordArray = filterPanelArray[storagelabelCode]; let index = filterPanelArray[storagelabelCode].indexOf(farrisSearchRecord); if (index < 0 && farrisSearchRecord && farrisSearchRecord.length) { farrisSearchRecordArray.unshift(farrisSearchRecord); } farrisSearchRecordArray = farrisSearchRecordArray.slice(0, 6); } if (filterPanelArray == null) filterPanelArray = {}; filterPanelArray[storagelabelCode] = farrisSearchRecordArray; localStorage.setItem(this.localStorageKey, JSON.stringify(filterPanelArray)); } }); //判断高级筛选里是否已修改 let extendAmendedList = this.extendFilterList.filter(item => { return item.filled }); if (extendAmendedList && extendAmendedList.length) { this.showExpandTag = true; } else { this.showExpandTag = false; } this.extendFilterListStore = this.extendFilterList.map(o => ({ ...o })); this.extendFilterListStore.forEach(field => { let extendListIndex = this.listFilterConditions.findIndex(item => { return item['id'] == field['id']; }); if (extendListIndex >= 0) { this.listFilterConditions[extendListIndex] = field; } }); // console.log(this.listFilterConditions); this.searchChange.emit(this.listFilterConditions); this.inSetCurrentFilterConditions(); this.filterListClassify(); this.farrissidebar.isOpen = false; this.floatPanelOpen = false; this.cd.markForCheck(); this.submit('extendFormData', this.listFilterConditions); } /** 提交表单 */ submit(type, filterlist, filterItem = null) { if (this.disabled || !this.showCanSubmit || !this.advancedCanSubmit) { return; } const queryList = this.handler.setFilterConditions(filterlist); this.query.emit({ type: type, data: JSON.stringify(queryList), filterItem }); } /** 设置筛选初始值 */ setFilterValue(fieldConfigs: FieldConfig[]) { this.listFilterConditions = this.stagListFilterConditions.map(o => ({ ...o })); if (this.listFilterConditions && this.listFilterConditions.length) { this.setValues(this.listFilterConditions, fieldConfigs); let listFilter = this.listFilterConditions.map(o => ({ ...o })); this.listFilterConditions = listFilter; this.filterListClassify(); this.searchChange.emit(this.listFilterConditions); this.inSetCurrentFilterConditions(); // this.listSearch(); this.cd.detectChanges(); this.cd.markForCheck(); this.canSubmit = this.canSubmitFilter(this.requiredListFilter); if (!this.canSubmit) { return; } this.submit('showFormData', this.listFilterConditions); } } /** * 清空筛选某一项 * @param labelCode 字段编号唯一标识 */ resetFilterValue(labelCode: string) { if (labelCode && this.listFilterConditions && this.listFilterConditions.length) { let resetCondition = this.listFilterConditions.find((conditions) => { return conditions['fieldCode'] === labelCode; }) if (resetCondition) { this.resetListCondition(resetCondition); this.filterListClassify(); this.cd.markForCheck(); this.searchChange.emit(this.listFilterConditions); this.inSetCurrentFilterConditions(); this.advancedCanSubmit = this.canSubmitFilter(this.extendRequiredList); this.submit('panelFormData', this.listFilterConditions); } } } // 设置value setValues(conditions, fieldConfigs: FieldConfig[]) { let qc: QueryCondition; let controlType: string; if (fieldConfigs && fieldConfigs.length) { fieldConfigs.forEach(v => { qc = conditions.find(c => c.fieldCode == v.labelCode); if (qc) { controlType = this.getControlType(qc); qc.value = this.filterService.setValues(controlType, v.value, qc.control.single); qc.valueText = qc.value.getValueText(); if (v.value) { qc.filled = true; } } }); } } getValue(fieldName: string) { const queryList = this.handler.setFilterConditions(this.listFilterConditions); const item = queryList.find((n: any) => n['FilterField'] === fieldName); if (item) { return item.value; } return undefined; } /** * 获取控件类型 * @param selectedFieldCondition */ getControlType(selectedFieldCondition: QueryCondition): string { if (selectedFieldCondition) { if (selectedFieldCondition.control.getControlType() == ControlType.Text) { return 'input'; } else if (selectedFieldCondition.control.getControlType() == ControlType.Search) { return 'search'; } else if (selectedFieldCondition.control.getControlType() == ControlType.Date) { return 'date'; } else if (selectedFieldCondition.control.getControlType() == ControlType.Month) { return 'month'; } else if (selectedFieldCondition.control.getControlType() == ControlType.Year) { return 'year'; } else if (selectedFieldCondition.control.getControlType() == ControlType.DateRange) { return 'daterange'; } else if (selectedFieldCondition.control.getControlType() == ControlType.MonthRange) { return 'monthrange'; } else if (selectedFieldCondition.control.getControlType() == ControlType.YearRange) { return 'yearrange'; } else if (selectedFieldCondition.control.getControlType() == ControlType.DateRangeTime) { return 'daterangetime'; } else if (selectedFieldCondition.control.getControlType() == ControlType.DropDown) { return 'dropdown'; } else if (selectedFieldCondition.control.getControlType() == ControlType.NumberRange) { return 'number'; } else if (selectedFieldCondition.control.getControlType() == ControlType.Help) { return 'help'; } else if (selectedFieldCondition.control.getControlType() == ControlType.CheckboxGroup) { return 'checkboxgroup'; } else if (selectedFieldCondition.control.getControlType() == ControlType.Radio) { return 'radio'; } else if (selectedFieldCondition.control.getControlType() == ControlType.InputGroup) { return 'input-group'; } else if (selectedFieldCondition.control.getControlType() == ControlType.Number) { return 'singlenumber'; } else if (selectedFieldCondition.control.getControlType() == ControlType.FlexibleNumber) { return 'flexiblenumber'; } else if (selectedFieldCondition.control.getControlType() == ControlType.FlexibleDate) { return 'flexibledate'; } else { return 'input'; } } } /** * 更新上下文中筛选数据 */ inSetCurrentFilterConditions() { if (this.filterHandler) { this.filterHandler.setCurrentFilterConditions(this.listFilterConditions); } } /** 清空条件 */ resetListCondition(listFilterCondition: QueryCondition) { if (listFilterCondition.control.getControlType() == ControlType.Text) { listFilterCondition.value = new TextValue(); } else if (listFilterCondition.control.getControlType() == ControlType.Search) { listFilterCondition.value = new SearchValue(); } else if (listFilterCondition.control.getControlType() == ControlType.Date) { listFilterCondition.value = new DateValue(); } else if (listFilterCondition.control.getControlType() == ControlType.Month) { listFilterCondition.value = new MonthValue(); } else if (listFilterCondition.control.getControlType() == ControlType.Year) { listFilterCondition.value = new YearValue(); } else if (listFilterCondition.control.getControlType() == ControlType.DateRange) { listFilterCondition.value = new DateRangeValue(); } else if (listFilterCondition.control.getControlType() == ControlType.MonthRange) { listFilterCondition.value = new MonthRangeValue(); } else if (listFilterCondition.control.getControlType() == ControlType.YearRange) { listFilterCondition.value = new YearRangeValue(); } else if (listFilterCondition.control.getControlType() == ControlType.DropDown) { listFilterCondition.value = new DropDownListValue(); } else if (listFilterCondition.control.getControlType() == ControlType.NumberRange) { listFilterCondition.value = new NumberRangeValue(); } else if (listFilterCondition.control.getControlType() == ControlType.Help) { listFilterCondition.value = new HelpValue(); } else if (listFilterCondition.control.getControlType() == ControlType.CheckboxGroup) { listFilterCondition.value = new CheckboxGroupValue(); } else if (listFilterCondition.control.getControlType() == ControlType.Radio) { listFilterCondition.value = new RadioGroupValue(); } else if (listFilterCondition.control.getControlType() == ControlType.InputGroup) { listFilterCondition.value = new InputGroupValue(); } else if (listFilterCondition.control.getControlType() == ControlType.Number) { listFilterCondition.value = new NumberValue(); } else if (listFilterCondition.control.getControlType() == ControlType.FlexibleNumber) { listFilterCondition.value = new FlexibleNumberValue({ startValue: null, endValue: null, value: null }, listFilterCondition.control.single); } else if (listFilterCondition.control.getControlType() == ControlType.FlexibleDate) { listFilterCondition.value = new FlexibleDateValue({ startValue: null, endValue: null, value: null }, listFilterCondition.control.single); } else { listFilterCondition.value = new TextValue(); } listFilterCondition.valueText = ''; listFilterCondition.filled = false; // return listFilterCondition; } resetSearch() { if (this.disabled) { return; } if (this.listFilterConditions && this.listFilterConditions.length) { this.listFilterConditions.forEach(listFilterCondition => { this.resetListCondition(listFilterCondition); }); this.listFilterConditions = this.listFilterConditions.map(o => ({ ...o })); this.filterListClassify(); this.showCanSubmit = true; this.submit('panelFormData', this.listFilterConditions); } this.resetChange.emit(); this.showExpandTag = false; } /** * 高级筛选清空筛选 */ resetAdvancedSearch() { if (this.extendFilterList && this.extendFilterList.length) { this.extendFilterList.forEach(listFilterCondition => { this.resetListCondition(listFilterCondition); }) this.extendFilterList = this.extendFilterList.map(o => ({ ...o })); } if (this.extendRequiredList.length) { this.advancedCanSubmit = false; } } /** * valueText 特殊类型处理 * @param item */ getValueText(item: QueryCondition) { // console.log(item.fieldName,item.control.getControlType()); if (item.control.getControlType() === ControlType.DropDown) { const enumValue = (item.control as DropDownControl).enumValues; const value = (item.value as DropDownListValue).value; if (enumValue && enumValue.length) { const texts = enumValue.find(val => { return val['value'] == value; }); if (texts) { item.valueText = texts['name']; } } } else if (item.control.getControlType() === ControlType.CheckboxGroup) { const enumValue = (item.control as CheckboxGroupControl).enumValues; const value = (item.value as CheckboxGroupValue).value; if (enumValue && enumValue.length && value && value['length'] > 0) { if (item.control.boolcheck) { item.valueText = value[0].toString(); if (item.valueText == 'true') { item.valueText = this.localeService.getValue('text.yes'); } else if (item.valueText == 'false') { item.valueText = this.localeService.getValue('text.no'); } } else { let texts = []; for (let i = 0; i <= value['length']; i++) { const enums = enumValue.find(val => { return val['value'] == value[i]; }); if (enums) { texts.push(enums['name']); } } item.valueText = texts.join(','); } } } else if (item.control.getControlType() === ControlType.Radio) { const enumValue = (item.control as RadioControl).enumValues; const value = (item.value as RadioGroupValue).value; if (enumValue && enumValue.length) { const texts = enumValue.find(val => { return val['value'] == value; }); if (texts) { item.valueText = texts['name']; if (item.valueText == 'true') { item.valueText = this.localeService.getValue('text.yes'); } else if (item.valueText == 'false') { item.valueText = this.localeService.getValue('text.no'); } } } } else if (item.control.getControlType() == ControlType.InputGroup) { item.valueText = item.value.getValueText(); } } }