/** * Created by W.X. on 16/8/11. */ import * as React from "react"; import * as ReactDOM from "react-dom"; import {autobind} from "core-decorators"; import {observer} from "mobx-react/index"; import {DropTarget} from "react-dnd"; import DropTargetCollector = __ReactDnd.DropTargetCollector; import DropTargetMonitor = __ReactDnd.DropTargetMonitor; import DropTargetConnector = __ReactDnd.DropTargetConnector; import {DND_TYPES_EXPORT_ITEM} from "../../../dnd/constants"; import DNDManager from "../../../dnd/DndManager"; import {DesignerHost} from '../../DesignerHost'; import {Report} from "../../../items/Report"; import {SelectionService} from '../../behavior/SelectionService'; import {SelectionTypes} from "../../behavior/Enums"; import {KeyStates, Keys} from "../../../common/KeyStates"; import {ExportGroupColor} from "./ExportEnum"; import {Popover, PopoverAnimationVertical} from 'material-ui/Popover'; import Menu from 'material-ui/Menu'; import MenuItem from 'material-ui/MenuItem'; import {ReportItemBase} from "../../../items/ReportItemBase"; interface ExportBandItemProps { connectDropTarget: Function; data : { type : string, item : any, group ?: any, groupIndex ?: number, detailIndex ?: number, maxIndex ?: number } } @observer class ExportBandItem extends React.Component { private _host: DesignerHost; private _selectionService: SelectionService; constructor(props:ExportBandItemProps, context:any) { super(props, context); this._host = DesignerHost.instance(); this._selectionService = this._host.getService(SelectionService); this.state = { isExt:false } } get report():Report { return this._host.designReport.report; } render(): JSX.Element { const {item, type, group, groupIndex} = this.props.data; const groupName = group ? group.name+"-" : ""; const style = group && { background :(ExportGroupColor[groupIndex+1] || ExportGroupColor[0]) }; if(type === "blank-band"){ return (
{groupName} {item}
); } else{ const bandName = item.constructor.name; const designer = this._host.getDesigner(item); if (!designer) { return null; } const className = 'rd-export-view-item' + (designer.selected ? ' rd-export-view-item-selected' : ''); return this.props.connectDropTarget(
{groupName} {(item as ReportItemBase).displayName()}
{designer.locked && ()} {this.renderExtBtn(bandName)} {this.renderExtMenu(bandName)}
); } } renderExtBtn(bandName:string){ const {group} = this.props.data; const detailAmount = this.report.detailBandCount; if(group || (bandName === "DetailBand" && detailAmount>1)){ return (); } else{ return (); } } renderExtMenu(bandName:string){ const {group, groupIndex, detailIndex, maxIndex} = this.props.data; const detailAmount = this.report.detailBandCount; if(group || (bandName === "DetailBand" && detailAmount>1)){ const index = group ? groupIndex : detailIndex; const isSwap = bandName === "GroupFooterBand" ? true :false; return ( { (index > 0) && } { (index < maxIndex) && } } onTouchTap={this.onDelete} /> { group && } onTouchTap={this.onDeleteGroup} /> } ); } return; } @autobind onAddBnd(e){ e.stopPropagation(); const report = this.report; const {item, group} = this.props.data; if(group){ report.addGroupBand(item, group); } else{ report.addBand(item); } } @autobind onExtOpen(e){ e.stopPropagation(); this.setState({ isExt: true, anchorEl: e.currentTarget }); } @autobind onExtClose(){ this.setState({isExt: false}); } @autobind onDelete(e) { e.stopPropagation(); const item = this.props.data.item; this.onExtClose(); this.report.delBand(item); this._selectionService.setSelectedItems([item], SelectionTypes.Remove); } @autobind onDeleteGroup(e){ e.stopPropagation(); const item = this.props.data.item; const group = item.group; this.onExtClose(); this.report.delGroup(group); this._selectionService.setSelectedItems([group.groupHeader, group.groupFooter], SelectionTypes.Remove); } @autobind onUnLock(e) { e.stopPropagation(); const item = this.props.data.item; const designer = this._host.getDesigner(item); designer.locked = false; } @autobind onSelect() { //TODO 这里需要重构 const item = this.props.data.item; const isSelected = this._host.getDesigner(item).selected; //判断shift键是否按下,按下执行多选 if (KeyStates.isDown(Keys.Shift)) { if (isSelected) { this._selectionService.setSelectedItems([item], SelectionTypes.Remove); } else { const selectionItems = this._selectionService.getSelectedItems(); selectionItems.push(item); this._selectionService.setSelectedItems(selectionItems, SelectionTypes.Replace); } } //非shift键按下单选模式 else { const selectionItems = this._selectionService.getSelectedItems(); //如果当前item是唯一选中的item,则设置为未选中,否则都视为单选当前Item const type = (selectionItems.length === 1 && selectionItems[0] === item) ? SelectionTypes.Remove : SelectionTypes.Replace; this._selectionService.setSelectedItems([item], type); } this.report.setNeedDisplay(); } @autobind onMoveUp(){ const {type,item} = this.props.data; this.onExtClose(); if(type === "DetailBand"){ this.report.exchangeDetailBand(item,"up"); } else{ this.report.exchangeGroup(item,"up"); } } @autobind onMoveDown(){ const {type,item} = this.props.data; this.onExtClose(); if(type === "DetailBand"){ this.report.exchangeDetailBand(item,"down"); } else{ this.report.exchangeGroup(item,"down"); } } } // export default ExportBandItem; const dndHandler = { exportItemDrop(props, monitor?:DropTargetMonitor, component?: any){ const item = (monitor.getItem() as any).item; // item.parent.items.remove(item); const parent = component.props.data.item; // parent.items.push(item); DesignerHost.instance().designReport.report.moveItem(parent,item); }, exportItemHover(props, monitor?:DropTargetMonitor, component?: any){ } }; const dropTarget = { drop(props, monitor, component) { if (monitor.didDrop()) { return; } return dndHandler.exportItemDrop(props, monitor, component); }, hover(props, monitor, component) { const clientOffset: {x: number, y: number} = monitor.getClientOffset(); const clientRect: ClientRect = ReactDOM.findDOMNode(component).getBoundingClientRect(); const isJustOverThisOne = monitor.isOver({shallow: true}); const canDrop = monitor.canDrop(); dndHandler.exportItemHover(props, monitor, component); } }; const collect: DropTargetCollector = (connect, monitor) => ({ connectDropTarget: connect.dropTarget(), isOver: monitor.isOver(), isOverCurrent: monitor.isOver({shallow: true}), canDrop: monitor.canDrop(), }); export default DropTarget(DND_TYPES_EXPORT_ITEM, dropTarget, collect)(ExportBandItem);