/** * Created by W.X. on 16/7/27. */ import * as React from "react"; import {observer} from "mobx-react/index"; import {autobind} from "core-decorators"; import {DragSource} from "react-dnd"; import {DND_TYPES_EXPORT_ITEM} from "../../../dnd/constants"; import DragSourceCollector = __ReactDnd.DragSourceCollector; import {DesignerHost} from '../../DesignerHost'; import {SelectionService} from '../../behavior/SelectionService'; import {SelectionTypes} from "../../behavior/Enums"; import {KeyStates, Keys} from "../../../common/KeyStates"; import {ExportItemIcon} from "./ExportEnum"; import {ReportItemBase} from "../../../items/ReportItemBase"; interface ExportItemProps { item: ReportItemBase; connectDragSource: Function; connectDragPreview: Function; isDragging: boolean; } @observer class ExportItem extends React.Component { private _host: DesignerHost; private _selectionService: SelectionService; constructor(props:ExportItemProps, context:any) { super(props, context); this._host = DesignerHost.instance(); this._selectionService = this._host.getService(SelectionService); } render(): JSX.Element { const item = this.props.item; const name = item.constructor.name; const designer = this._host.getDesigner(item); if (designer) { const className = 'rd-export-view-item' + (designer.selected ? ' rd-export-view-item-selected' : ''); const iconClass = "itemIcon itemIcon-" + (ExportItemIcon[name] || "default"); const displayName = designer.getDisplayName ? designer.getDisplayName() : item.displayName(); return this.props.connectDragSource(
{displayName}
{designer.locked && ()}
); } else { return null; } } @autobind onDelete(e) { e.stopPropagation(); const item = this.props.item; const parent = item.parent; parent.items.remove(item); this._selectionService.setSelectedItems([item], SelectionTypes.Remove); } @autobind onUnLock(e) { e.stopPropagation(); const item = this.props.item; const designer = this._host.getDesigner(item); designer.locked = false; } @autobind onSelect() { //TODO 这里需要重构 const item = this.props.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._host.designReport.report.setNeedDisplay(); } } const dragSource = { beginDrag(props, monitor, component) { const item = props.item; return { item : item }; } }; const collect: DragSourceCollector = (connect, monitor) => ({ connectDragSource: connect.dragSource(), connectDragPreview: connect.dragPreview(), isDragging: monitor.isDragging() }); // export default ExportItem; export default DragSource(DND_TYPES_EXPORT_ITEM, dragSource, collect)(ExportItem);