UNPKG

@maxgraph/core

Version:

maxGraph is a fully client side JavaScript diagramming library that uses SVG and HTML for rendering.

252 lines (248 loc) 8.85 kB
"use strict"; /* Copyright 2021-present The maxGraph project Contributors Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.SelectionMixin = void 0; const Cell_js_1 = __importDefault(require("../cell/Cell.js")); const RootChange_js_1 = __importDefault(require("../undoable_changes/RootChange.js")); const ChildChange_js_1 = __importDefault(require("../undoable_changes/ChildChange.js")); // @ts-expect-error The properties of PartialGraph are defined elsewhere. exports.SelectionMixin = { selectionModel: null, getSelectionModel() { return this.selectionModel; }, setSelectionModel(selectionModel) { this.selectionModel = selectionModel; }, /***************************************************************************** * Selection *****************************************************************************/ isCellSelected(cell) { return this.selectionModel.isSelected(cell); }, isSelectionEmpty() { return this.selectionModel.isEmpty(); }, clearSelection() { this.selectionModel.clear(); }, getSelectionCount() { return this.selectionModel.cells.length; }, getSelectionCell() { return this.selectionModel.cells[0]; }, getSelectionCells() { return this.selectionModel.cells.slice(); }, setSelectionCell(cell) { this.selectionModel.setCell(cell); }, setSelectionCells(cells) { this.selectionModel.setCells(cells); }, addSelectionCell(cell) { this.selectionModel.addCell(cell); }, addSelectionCells(cells) { this.selectionModel.addCells(cells); }, removeSelectionCell(cell) { this.selectionModel.removeCell(cell); }, removeSelectionCells(cells) { this.selectionModel.removeCells(cells); }, selectRegion(rect, evt) { const cells = this.getCells(rect.x, rect.y, rect.width, rect.height); this.selectCellsForEvent(cells, evt); return cells; }, selectNextCell() { this.selectCell(true); }, selectPreviousCell() { this.selectCell(); }, selectParentCell() { this.selectCell(false, true); }, selectChildCell() { this.selectCell(false, false, true); }, selectCell(isNext = false, isParent = false, isChild = false) { const cell = this.selectionModel.cells.length > 0 ? this.selectionModel.cells[0] : null; if (this.selectionModel.cells.length > 1) { this.selectionModel.clear(); } const parent = cell ? cell.getParent() : this.getDefaultParent(); const childCount = parent.getChildCount(); if (!cell && childCount > 0) { const child = parent.getChildAt(0); this.setSelectionCell(child); } else if (parent && (!cell || isParent) && this.getView().getState(parent) && parent.getGeometry()) { if (this.getCurrentRoot() !== parent) { this.setSelectionCell(parent); } } else if (cell && isChild) { const tmp = cell.getChildCount(); if (tmp > 0) { const child = cell.getChildAt(0); this.setSelectionCell(child); } } else if (childCount > 0) { let i = parent.getIndex(cell); if (isNext) { i++; const child = parent.getChildAt(i % childCount); this.setSelectionCell(child); } else { i--; const index = i < 0 ? childCount - 1 : i; const child = parent.getChildAt(index); this.setSelectionCell(child); } } }, selectAll(parent, descendants = false) { parent = parent ?? this.getDefaultParent(); const cells = descendants ? parent.filterDescendants((cell) => { return cell !== parent && !!this.getView().getState(cell); }) : parent.getChildren(); this.setSelectionCells(cells); }, selectVertices(parent, selectGroups = false) { this.selectCells(true, false, parent, selectGroups); }, selectEdges(parent) { this.selectCells(false, true, parent); }, selectCells(vertices = false, edges = false, parent, selectGroups = false) { parent = parent ?? this.getDefaultParent(); const filter = (cell) => { const p = cell.getParent(); return (!!this.getView().getState(cell) && (((selectGroups || cell.getChildCount() === 0) && cell.isVertex() && vertices && p && !p.isEdge()) || (cell.isEdge() && edges))); }; const cells = parent.filterDescendants(filter); this.setSelectionCells(cells); }, selectCellForEvent(cell, evt) { const isSelected = this.isCellSelected(cell); if (this.isToggleEvent(evt)) { if (isSelected) { this.removeSelectionCell(cell); } else { this.addSelectionCell(cell); } } else if (!isSelected || this.getSelectionCount() !== 1) { this.setSelectionCell(cell); } }, selectCellsForEvent(cells, evt) { if (this.isToggleEvent(evt)) { this.addSelectionCells(cells); } else { this.setSelectionCells(cells); } }, isSiblingSelected(cell) { const parent = cell.getParent(); const childCount = parent.getChildCount(); for (let i = 0; i < childCount; i += 1) { const child = parent.getChildAt(i); if (cell !== child && this.isCellSelected(child)) { return true; } } return false; }, /***************************************************************************** * Selection state *****************************************************************************/ getSelectionCellsForChanges(changes, ignoreFn = null) { const coveredElements = new Map(); const cells = []; const addCell = (cell) => { if (!coveredElements.has(cell) && this.getDataModel().contains(cell)) { if (cell.isEdge() || cell.isVertex()) { coveredElements.set(cell, true); cells.push(cell); } else { const childCount = cell.getChildCount(); for (let i = 0; i < childCount; i += 1) { addCell(cell.getChildAt(i)); } } } }; for (let i = 0; i < changes.length; i += 1) { const change = changes[i]; if (change.constructor !== RootChange_js_1.default && (!ignoreFn || !ignoreFn(change))) { let cell = null; if (change instanceof ChildChange_js_1.default) { cell = change.child; } else if (change.cell && change.cell instanceof Cell_js_1.default) { cell = change.cell; } if (cell) { addCell(cell); } } } return cells; }, updateSelection() { const cells = this.getSelectionCells(); const removed = []; for (const cell of cells) { if (!this.getDataModel().contains(cell) || !cell.isVisible()) { removed.push(cell); } else { let par = cell.getParent(); while (par && par !== this.getView().currentRoot) { if (par.isCollapsed() || !par.isVisible()) { removed.push(cell); break; } par = par.getParent(); } } } this.removeSelectionCells(removed); }, };