import {IObservableArray, action, observable} from "mobx/lib/mobx"; import {EventEmitter2} from "eventemitter2"; import {SelectionTypes} from "./Enums"; import {IServiceProvider, IComponent} from "../../interfaces/Interfaces"; import {DesignerHost} from "../DesignerHost"; import {KeyStates, Keys} from "../../common/KeyStates"; import {ReportItemBase} from "../../items/ReportItemBase"; export class SelectionService { static SelectionChangeEvent:string = 'selectionChangeEvent'; private _host: DesignerHost; private _selection:Array; private _serviceProvider: IServiceProvider; private _events = new EventEmitter2(); private _ignoreEvents = false; // @observable // private _primarySelection: any; constructor(serviceProvider:IServiceProvider) { this._host = DesignerHost.instance(); this._serviceProvider = serviceProvider; this._selection = observable([]); } private get rootComponent(): IComponent { return DesignerHost.instance().designReport.report; } /** * Gets the object that is currently the primary selection. * @returns {any} */ getPrimarySelection():any { // return this._primarySelection; if (this._selection && this._selection.length > 0) { return this._selection[0]; } return null; } getSelectionCount():number { return this._selection.length; } /** * Gets a value indicating whether the component is currently selected. * @param item */ getItemSelected(item):boolean { return this._selection.indexOf(item) >= 0; } /** * Gets a collection of _components that are currently part of the user's selection. * @returns {IObservableArray} */ getSelectedItems():Array { return this._selection.map(item => item); } addEventListener(event:string, listener:EventEmitter2.Listener){ this._events.on(event, listener); } removeEventListener(event:string, listener:EventEmitter2.Listener){ this._events.off(event, listener); } private addSelection(item:ReportItemBase):void { if (this._selection.indexOf(item) === -1) { const designer = this._host.getDesigner(item); if (designer) { designer.selected = true; this._selection.push(item); } } } private removeSelection(item:ReportItemBase):void { const designer = this._host.getDesigner(item); if (designer) { this._host.getDesigner(item).selected = false; const index = this._selection.findIndex(obj => obj === item); if (index >= 0) { this._selection.splice(index, 1); } } } @action /** * 清空当前选中的item */ clear() { //如果已经是空的,直接返回 if(this._selection.length == 0){ return; } this._selection.map( (item:ReportItemBase) => { const designer = this._host.getDesigner(item); if (designer) { this._host.getDesigner(item).selected = false; } }); this._selection.length = 0; this.onSelectionChanged(); } @action replaceSelectedItems(components:Array):void { try { this._ignoreEvents = true; this.clear(); this._ignoreEvents = false; this.setSelectedItems(components); } finally { this._ignoreEvents = false; } } /** * Changes the user's current set of selected _components to the _components * in the given array. If the array is null or doesn't contain any * _components, this will select the top level component in the designer. * @param components * @param selectionType * @private */ @action setSelectedItems(components:Array, selectionType:SelectionTypes = SelectionTypes.Auto) { let fToggle = (selectionType & SelectionTypes.Toggle) == SelectionTypes.Toggle; let fPrimary = (selectionType & SelectionTypes.Primary) == SelectionTypes.Primary; let fAdd = (selectionType & SelectionTypes.Add) == SelectionTypes.Add; let fRemove = (selectionType & SelectionTypes.Remove) == SelectionTypes.Remove; let fReplace = (selectionType & SelectionTypes.Replace) == SelectionTypes.Replace; let fAuto = !(fToggle || fAdd || fRemove || fReplace); // We always want to allow NULL arrays coming in. // if (components == null) { components = []; } // If toggle, replace, remove or add are not specifically specified, infer them from // the state of the modifer keys. This creates the "Auto" selection type // for us by default. // if (fAuto) { fToggle = KeyStates.isDown(Keys.Shift); fAdd = fAdd && KeyStates.isDown(Keys.Shift); // If we are in auto mode, and if we are toggling or adding new controls, then // cancel out the primary flag. // if (fToggle || fAdd) { fPrimary = false; } } // This flag is true if we changed selection and should therefore raise // a selection change event. // let fChanged = false; // Handle the click case // let requestedPrimary = null; if (fPrimary && 1 == components.length) { for(let o of components) { requestedPrimary = o; if (o === null) { throw new Error('_components argument null'); } break; } } let primaryIndex; if (requestedPrimary && this._selection && (primaryIndex = this._selection.indexOf(requestedPrimary)) !== -1) { if (primaryIndex !== 0) { let tmp = this._selection[0]; this._selection[0] = this._selection[primaryIndex]; this._selection[primaryIndex] = tmp; fChanged = true; } } else { // If we are replacing the selection, only remove the ones that are not in our new list. // We also handle the special case here of having a singular component selected that's // already selected. In this case we just move it to the primary selection. // if (!fToggle && !fAdd && !fRemove) { if (this._selection) { let selections = this._selection.map(item => item); // Yucky and N^2, but even with several hundred _components this should be fairly fast // for(let item of selections) { let remove = true; for(let comp of components) { if (comp === null) { throw new Error('_components argument null'); } if (comp === item) { remove = false; break; } } if (remove) { this.removeSelection(item); fChanged = true; } } } } // Now select / toggle the _components. // for(let comp of components) { if (comp === null) { throw new Error('_components argument null'); } if (this._selection && this._selection.indexOf(comp) >= 0) { if (fToggle || fRemove) { this.removeSelection(comp); fChanged = true; } } else if (!fRemove) { this.addSelection(comp); fChanged = true; } } } // Notify that our selection has changed // if (fChanged) { this.onSelectionChanged(); } } private onSelectionChanged() { if (!this._ignoreEvents) { this._events.emit(SelectionService.SelectionChangeEvent); } } }