/** * Created by W.X. on 16/7/28. */ import {IObservableArray, observable, action, observe, intercept} from "mobx/lib/mobx"; import {DesignerHost} from '../../DesignerHost'; import {Report} from "../../../items/Report"; import {SelectionService} from '../../behavior/SelectionService'; import {SelectionTypes} from "../../behavior/Enums"; import HistoryService from '../../HistoryService'; import {Unit, UnitType} from "../../../drawing/Unit"; import {autobind} from "core-decorators"; import {checkItemType} from '../../../items/CheckItemType'; import {JSONSchemaWriter} from "../../../schema/JSONSchemaWriter"; import {JSONSchemaLoader} from "../../../schema/loader/JSONSchemaLoader"; import {ReportBandBase} from "../../../items/band/ReportBandBase"; import {ReportItemBase} from "../../../items/ReportItemBase"; import SnackBar from '../../../helper/snackbar'; export class CommandReport { private _selectionService: SelectionService; private _historyService: HistoryService; private _host: DesignerHost; private _items: Array; private _itemObserves: Array = []; @observable private _itemsStatus: Object; private _selectItems: IObservableArray = observable([]); constructor() { this._host = DesignerHost.instance(); this._selectionService = this._host.getService(SelectionService); this._historyService = this._host.historyService; this.initItems(); this.initStatus(); this.initEvents(); } initItems() { this._items = [ {type: 'button', title: '撤销', name: 'undo', onClick: this.undo.bind(this)}, {type: 'button', title: '恢复', name: 'redo', onClick: this.redo.bind(this)}, {type: 'splitLine', title: '|'}, {type: 'button', title: '复用', name: 'copy-paste', onClick: this.itemCopyAndPaste.bind(this)}, {type: 'button', title: '删除', name: 'delete', onClick: this.itemDelete.bind(this)}, {type: 'button', title: '全选', name: 'selectall', usable: true, onClick: this.selectAll.bind(this)}, {type: 'button', title: '锁定', name: 'lock', onClick: this.itemLock.bind(this)}, {type: 'button', title: '解锁', name: 'unlock', onClick: this.itemUnLock.bind(this)}, {type: 'splitLine', title: '|'}, { type: 'buttonGroup', title: '水平', name: 'horizontalAlign', items: [ {title:'left', value:'left'}, {title:'center', value:'center'}, {title:'right', value:'right'}, ], onChange: this.horizontalAlign.bind(this) }, { type: 'buttonGroup', title: '垂直', name: 'verticalAlign', items: [ {title:'top', value:'top'}, {title:'middle', value:'middle'}, {title:'bottom', value:'bottom'}, ], onChange: this.verticalAlign.bind(this) }, {type: 'splitLine', title: '|'}, { type: 'switch', title: '网格', name: 'snapGrid', openLabel: '显示', closeLabel: '隐藏', onChange: this.showSnapGrid }, {type: 'splitLine', title: '|'}, { type: 'select', title: '缩放', name: 'zoom', width: 60, options: [ {title: '75%', value: '0.75'}, {title: '100%', value: '1'}, {title: '150%', value: '1.5'}, {title: '200%', value: '2'}, {title: '400%', value: '4'} ], onChange: this.zoomDesigner } ]; if(this._host.onOpen || this._host.onSave){ this._items.unshift({type: 'splitLine', title: '|'}); if (this._host.onSave) { this._items.unshift({type: 'button', title: '保存', name: 'save', onClick: this.save.bind(this)}); } if (this._host.onOpen) { this._items.unshift({type: 'button', title: '打开', name: 'open', onClick: this.open.bind(this)}); } } } initStatus() { this._itemsStatus = { open: {useable: true}, save: {useable: true}, undo: {useable: false}, redo: {useable: false}, 'copy-paste': {useable: false}, 'delete': {useable: false}, selectall: {useable: true}, lock: {useable: false}, unlock: {useable: false}, verticalAlign: {useable: false}, horizontalAlign: {useable: false}, snapGrid: {useable: true, value: true}, zoom: {useable: true, value: '1'} }; } initEvents() { //监听选择变化 this._selectionService.addEventListener(SelectionService.SelectionChangeEvent, this.onSelectionChange.bind(this)); //监听操作历史的会变化 this.historyListener(); } historyListener() { observe(this._historyService.status, (change) => { this.setItemStatus([change.name], 'useable', change.newValue); }); } get report(): Report { return DesignerHost.instance().designReport.report; } get items(): Array { return this._items; } getItem(itemName: string) { return this['_' + itemName]; } getItemStatus(itemName: string) { const status = this._itemsStatus[itemName]; return status === undefined ? true : status; } @action setItemStatus(item: Array, property: string, value: boolean) { item.map(itemName => { if (this._itemsStatus[itemName][property] !== undefined) { this._itemsStatus[itemName][property] = value; } }); } @autobind showSnapGrid() { const visable = !this._itemsStatus['snapGrid'].value; const designer = DesignerHost.instance().designReport; designer.showSnapGrid = visable; this._itemsStatus['snapGrid'].value = visable; } @autobind zoomDesigner(zoom:number) { const designer = DesignerHost.instance().designReport; designer.zoom = zoom; this._itemsStatus['zoom'].value = zoom; } @action onSelectionChange() { const items = this._selectionService.getSelectedItems(); const len = items.length; //选中的是否都是非容器的Item let itemNum = 0; let needUnlock = 0; let needLock = 0; //是否在同一个band let sameBand: any = ''; //清除上一次的选择,解除对锁定状态的观察 this._selectItems.clear(); this._itemObserves.map(fn => fn()); this._itemObserves = []; items.map(item => { const designer = this._host.getDesigner(item); if (designer) { if (!designer.isParentDesigner()) { itemNum++; } designer.locked ? needUnlock++ : needLock++; this._selectItems.push(item); //判断是否在一个band中 if (sameBand === '') { sameBand = item.parent.className; } else if (sameBand !== false) { if (sameBand !== item.parent.className) { sameBand = false; } } this._itemObserves.push( observe(designer, "_locked", (newValue, oldValue) => { this.checkLockStatus(); }) ); } }); //判断是否选中了detailBand中的dataTable if (len === 1 && checkItemType(items[0], 'TableItem') && checkItemType(items[0].parent, 'DetailBand')) { this.setItemStatus(['copy-paste'], 'useable', false); this.setItemStatus(['delete'], 'useable', true); } else { this.setItemStatus(['copy-paste', 'delete'], 'useable', itemNum === len ? true : false); } this.setItemStatus(['horizontalAlign'], 'useable', !!(itemNum === len && itemNum > 1 && (itemNum > needUnlock + 1))); this.setItemStatus(['verticalAlign'], 'useable', (itemNum > 1 && sameBand && (itemNum > needUnlock + 1)) ? true : false); this.setItemStatus(['lock'], 'useable', needLock > 0 ? true : false); this.setItemStatus(['unlock'], 'useable', needUnlock > 0 ? true : false); } //检查当前选定item的锁定状态,确定锁定、解锁按钮的可用状态 checkLockStatus() { const items = this._selectionService.getSelectedItems(); let needUnlock = 0; let needLock = 0; items.map(item => { const designer = this._host.getDesigner(item); if (designer) { designer.locked ? needUnlock++ : needLock++; } }); this.setItemStatus(['lock'], 'useable', needLock > 0 ? true : false); this.setItemStatus(['unlock'], 'useable', needUnlock > 0 ? true : false); } undo() { this._historyService.undo(); } redo() { this._historyService.redo(); } open() { this._host.onOpen && this._host.onOpen(); } save() { this._host.onSave && this._host.onSave(); } @action itemCopyAndPaste() { const items = this._selectionService.getSelectedItems(); const cloneItems = []; items.map(item => { const parent = item.parent; const cloneItem = item.clone(); const acceptResult = parent.canAcceptItem ? parent.canAcceptItem(cloneItem) : {result: true}; if (acceptResult.result) { cloneItem.left = new Unit(cloneItem.left.pixels + 10, UnitType.Pixel); const designer = DesignerHost.instance().getDesigner(cloneItem); if (designer) { designer.updateMinBounds(); } parent.items.push(cloneItem); cloneItems.push(cloneItem); } else { SnackBar.show(acceptResult.msg); } }); this._selectionService.setSelectedItems(cloneItems, SelectionTypes.Replace); this.report.setNeedDisplay(); this._historyService.snap(); } @action itemDelete() { const items = this._selectionService.getSelectedItems(); items.map(item => { const parent = item.parent; parent.items.remove(item); }); this._selectionService.clear(); this._historyService.snap(); } selectAll() { const selectedItems = this.report.getAllItems() .filter(item => item.parent !== this.report) .map(item => item); this._selectionService.replaceSelectedItems(selectedItems); this.report.setNeedDisplay(); } @action itemLock() { const items = this._selectionService.getSelectedItems(); items.map(item => this._host.getDesigner(item).locked = true); this.setItemStatus(['lock'], 'useable', false); this.setItemStatus(['unlock'], 'useable', true); this.onSelectionChange(); } @action itemUnLock() { const items = this._selectionService.getSelectedItems(); items.map(item => this._host.getDesigner(item).locked = false); this.setItemStatus(['lock'], 'useable', true); this.setItemStatus(['unlock'], 'useable', false); this.onSelectionChange(); } verticalAlign(value) { switch(value){ case 'top': this.align(0, 'top', 'height'); break; case 'middle': this.align(1, 'top', 'height'); break; case 'bottom': this.align(2, 'top', 'height'); break; default: break; } } horizontalAlign(value) { switch(value){ case 'left': this.align(0, 'left', 'width'); break; case 'center': this.align(1, 'left', 'width'); break; case 'right': this.align(2, 'left', 'width'); break; default: break; } } @action align(value, posDirection, sizeDirection) { const items = this._selectionService.getSelectedItems(); const mainItem = items[0]; const mainP = mainItem[posDirection].toPixel(); const mainS = mainItem[sizeDirection].toPixel(); let x: number; if (value === 0) x = mainP; else if (value === 1) x = mainP + mainS / 2; else if (value === 2) x = mainP + mainS; items.map(item => { const isLock = this._host.getDesigner(item).locked; if (item !== mainItem && !isLock) { const itemS = item[sizeDirection].toPixel(); if (value === 0) item[posDirection] = new Unit(x); else if (value === 1) item[posDirection] = new Unit(x - itemS / 2); else if (value === 2) item[posDirection] = new Unit(x - itemS); } }); this._historyService.snap(); } } export default CommandReport;