123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488 |
- import { forEach } from 'min-dash';
- import {
- Base
- } from '../../model';
- import AppendShapeHandler from './cmd/AppendShapeHandler';
- import CreateShapeHandler from './cmd/CreateShapeHandler';
- import DeleteShapeHandler from './cmd/DeleteShapeHandler';
- import MoveShapeHandler from './cmd/MoveShapeHandler';
- import ResizeShapeHandler from './cmd/ResizeShapeHandler';
- import ReplaceShapeHandler from './cmd/ReplaceShapeHandler';
- import ToggleShapeCollapseHandler from './cmd/ToggleShapeCollapseHandler';
- import SpaceToolHandler from './cmd/SpaceToolHandler';
- import CreateLabelHandler from './cmd/CreateLabelHandler';
- import CreateConnectionHandler from './cmd/CreateConnectionHandler';
- import DeleteConnectionHandler from './cmd/DeleteConnectionHandler';
- import MoveConnectionHandler from './cmd/MoveConnectionHandler';
- import LayoutConnectionHandler from './cmd/LayoutConnectionHandler';
- import UpdateWaypointsHandler from './cmd/UpdateWaypointsHandler';
- import ReconnectConnectionHandler from './cmd/ReconnectConnectionHandler';
- import MoveElementsHandler from './cmd/MoveElementsHandler';
- import DeleteElementsHandler from './cmd/DeleteElementsHandler';
- import DistributeElementsHandler from './cmd/DistributeElementsHandler';
- import AlignElementsHandler from './cmd/AlignElementsHandler';
- import UpdateAttachmentHandler from './cmd/UpdateAttachmentHandler';
- import PasteHandler from './cmd/PasteHandler';
- /**
- * The basic modeling entry point.
- *
- * @param {EventBus} eventBus
- * @param {ElementFactory} elementFactory
- * @param {CommandStack} commandStack
- */
- export default function Modeling(eventBus, elementFactory, commandStack) {
- this._eventBus = eventBus;
- this._elementFactory = elementFactory;
- this._commandStack = commandStack;
- var self = this;
- eventBus.on('diagram.init', function() {
- // register modeling handlers
- self.registerHandlers(commandStack);
- });
- }
- Modeling.$inject = [ 'eventBus', 'elementFactory', 'commandStack' ];
- Modeling.prototype.getHandlers = function() {
- return {
- 'shape.append': AppendShapeHandler,
- 'shape.create': CreateShapeHandler,
- 'shape.delete': DeleteShapeHandler,
- 'shape.move': MoveShapeHandler,
- 'shape.resize': ResizeShapeHandler,
- 'shape.replace': ReplaceShapeHandler,
- 'shape.toggleCollapse': ToggleShapeCollapseHandler,
- 'spaceTool': SpaceToolHandler,
- 'label.create': CreateLabelHandler,
- 'connection.create': CreateConnectionHandler,
- 'connection.delete': DeleteConnectionHandler,
- 'connection.move': MoveConnectionHandler,
- 'connection.layout': LayoutConnectionHandler,
- 'connection.updateWaypoints': UpdateWaypointsHandler,
- 'connection.reconnectStart': ReconnectConnectionHandler,
- 'connection.reconnectEnd': ReconnectConnectionHandler,
- 'elements.move': MoveElementsHandler,
- 'elements.delete': DeleteElementsHandler,
- 'elements.distribute': DistributeElementsHandler,
- 'elements.align': AlignElementsHandler,
- 'element.updateAttachment': UpdateAttachmentHandler,
- 'elements.paste': PasteHandler
- };
- };
- /**
- * Register handlers with the command stack
- *
- * @param {CommandStack} commandStack
- */
- Modeling.prototype.registerHandlers = function(commandStack) {
- forEach(this.getHandlers(), function(handler, id) {
- commandStack.registerHandler(id, handler);
- });
- };
- // modeling helpers //////////////////////
- Modeling.prototype.moveShape = function(shape, delta, newParent, newParentIndex, hints) {
- if (typeof newParentIndex === 'object') {
- hints = newParentIndex;
- newParentIndex = null;
- }
- var context = {
- shape: shape,
- delta: delta,
- newParent: newParent,
- newParentIndex: newParentIndex,
- hints: hints || {}
- };
- this._commandStack.execute('shape.move', context);
- };
- /**
- * Update the attachment of the given shape.
- *
- * @param {djs.mode.Base} shape
- * @param {djs.model.Base} [newHost]
- */
- Modeling.prototype.updateAttachment = function(shape, newHost) {
- var context = {
- shape: shape,
- newHost: newHost
- };
- this._commandStack.execute('element.updateAttachment', context);
- };
- /**
- * Move a number of shapes to a new target, either setting it as
- * the new parent or attaching it.
- *
- * @param {Array<djs.mode.Base>} shapes
- * @param {Point} delta
- * @param {djs.model.Base} [target]
- * @param {Object} [hints]
- * @param {Boolean} [hints.attach=false]
- */
- Modeling.prototype.moveElements = function(shapes, delta, target, hints) {
- hints = hints || {};
- var attach = hints.attach;
- var newParent = target,
- newHost;
- if (attach === true) {
- newHost = target;
- newParent = target.parent;
- } else
- if (attach === false) {
- newHost = null;
- }
- var context = {
- shapes: shapes,
- delta: delta,
- newParent: newParent,
- newHost: newHost,
- hints: hints
- };
- this._commandStack.execute('elements.move', context);
- };
- Modeling.prototype.moveConnection = function(connection, delta, newParent, newParentIndex, hints) {
- if (typeof newParentIndex === 'object') {
- hints = newParentIndex;
- newParentIndex = undefined;
- }
- var context = {
- connection: connection,
- delta: delta,
- newParent: newParent,
- newParentIndex: newParentIndex,
- hints: hints || {}
- };
- this._commandStack.execute('connection.move', context);
- };
- Modeling.prototype.layoutConnection = function(connection, hints) {
- var context = {
- connection: connection,
- hints: hints || {}
- };
- this._commandStack.execute('connection.layout', context);
- };
- /**
- * Create connection.
- *
- * @param {djs.model.Base} source
- * @param {djs.model.Base} target
- * @param {Number} [targetIndex]
- * @param {Object|djs.model.Connection} connection
- * @param {djs.model.Base} parent
- * @param {Object} hints
- *
- * @return {djs.model.Connection} the created connection.
- */
- Modeling.prototype.createConnection = function(source, target, parentIndex, connection, parent, hints) {
- if (typeof parentIndex === 'object') {
- hints = parent;
- parent = connection;
- connection = parentIndex;
- parentIndex = undefined;
- }
- connection = this._create('connection', connection);
- var context = {
- source: source,
- target: target,
- parent: parent,
- parentIndex: parentIndex,
- connection: connection,
- hints: hints
- };
- this._commandStack.execute('connection.create', context);
- return context.connection;
- };
- /**
- * Create a shape at the specified position.
- *
- * @param {djs.model.Shape|Object} shape
- * @param {Point} position
- * @param {djs.model.Shape|djs.model.Root} target
- * @param {Number} [parentIndex] position in parents children list
- * @param {Object} [hints]
- * @param {Boolean} [hints.attach] whether to attach to target or become a child
- *
- * @return {djs.model.Shape} the created shape
- */
- Modeling.prototype.createShape = function(shape, position, target, parentIndex, hints) {
- if (typeof parentIndex !== 'number') {
- hints = parentIndex;
- parentIndex = undefined;
- }
- hints = hints || {};
- var attach = hints.attach,
- parent,
- host;
- shape = this._create('shape', shape);
- if (attach) {
- parent = target.parent;
- host = target;
- } else {
- parent = target;
- }
- var context = {
- position: position,
- shape: shape,
- parent: parent,
- parentIndex: parentIndex,
- host: host,
- hints: hints
- };
- this._commandStack.execute('shape.create', context);
- return context.shape;
- };
- Modeling.prototype.createLabel = function(labelTarget, position, label, parent) {
- label = this._create('label', label);
- var context = {
- labelTarget: labelTarget,
- position: position,
- parent: parent || labelTarget.parent,
- shape: label
- };
- this._commandStack.execute('label.create', context);
- return context.shape;
- };
- /**
- * Append shape to given source, drawing a connection
- * between source and the newly created shape.
- *
- * @param {djs.model.Shape} source
- * @param {djs.model.Shape|Object} shape
- * @param {Point} position
- * @param {djs.model.Shape} target
- * @param {Object} [hints]
- * @param {Boolean} [hints.attach]
- * @param {djs.model.Connection|Object} [hints.connection]
- * @param {djs.model.Base} [hints.connectionParent]
- *
- * @return {djs.model.Shape} the newly created shape
- */
- Modeling.prototype.appendShape = function(source, shape, position, target, hints) {
- hints = hints || {};
- shape = this._create('shape', shape);
- var context = {
- source: source,
- position: position,
- target: target,
- shape: shape,
- connection: hints.connection,
- connectionParent: hints.connectionParent,
- attach: hints.attach
- };
- this._commandStack.execute('shape.append', context);
- return context.shape;
- };
- Modeling.prototype.removeElements = function(elements) {
- var context = {
- elements: elements
- };
- this._commandStack.execute('elements.delete', context);
- };
- Modeling.prototype.distributeElements = function(groups, axis, dimension) {
- var context = {
- groups: groups,
- axis: axis,
- dimension: dimension
- };
- this._commandStack.execute('elements.distribute', context);
- };
- Modeling.prototype.removeShape = function(shape, hints) {
- var context = {
- shape: shape,
- hints: hints || {}
- };
- this._commandStack.execute('shape.delete', context);
- };
- Modeling.prototype.removeConnection = function(connection, hints) {
- var context = {
- connection: connection,
- hints: hints || {}
- };
- this._commandStack.execute('connection.delete', context);
- };
- Modeling.prototype.replaceShape = function(oldShape, newShape, hints) {
- var context = {
- oldShape: oldShape,
- newData: newShape,
- hints: hints || {}
- };
- this._commandStack.execute('shape.replace', context);
- return context.newShape;
- };
- Modeling.prototype.pasteElements = function(tree, topParent, position) {
- var context = {
- tree: tree,
- topParent: topParent,
- position: position
- };
- this._commandStack.execute('elements.paste', context);
- };
- Modeling.prototype.alignElements = function(elements, alignment) {
- var context = {
- elements: elements,
- alignment: alignment
- };
- this._commandStack.execute('elements.align', context);
- };
- Modeling.prototype.resizeShape = function(shape, newBounds, minBounds) {
- var context = {
- shape: shape,
- newBounds: newBounds,
- minBounds: minBounds
- };
- this._commandStack.execute('shape.resize', context);
- };
- Modeling.prototype.createSpace = function(movingShapes, resizingShapes, delta, direction) {
- var context = {
- movingShapes: movingShapes,
- resizingShapes: resizingShapes,
- delta: delta,
- direction: direction
- };
- this._commandStack.execute('spaceTool', context);
- };
- Modeling.prototype.updateWaypoints = function(connection, newWaypoints, hints) {
- var context = {
- connection: connection,
- newWaypoints: newWaypoints,
- hints: hints || {}
- };
- this._commandStack.execute('connection.updateWaypoints', context);
- };
- Modeling.prototype.reconnectStart = function(connection, newSource, dockingOrPoints) {
- var context = {
- connection: connection,
- newSource: newSource,
- dockingOrPoints: dockingOrPoints
- };
- this._commandStack.execute('connection.reconnectStart', context);
- };
- Modeling.prototype.reconnectEnd = function(connection, newTarget, dockingOrPoints) {
- var context = {
- connection: connection,
- newTarget: newTarget,
- dockingOrPoints: dockingOrPoints
- };
- this._commandStack.execute('connection.reconnectEnd', context);
- };
- Modeling.prototype.connect = function(source, target, attrs, hints) {
- return this.createConnection(source, target, attrs || {}, source.parent, hints);
- };
- Modeling.prototype._create = function(type, attrs) {
- if (attrs instanceof Base) {
- return attrs;
- } else {
- return this._elementFactory.create(type, attrs);
- }
- };
- Modeling.prototype.toggleCollapse = function(shape, hints) {
- var context = {
- shape: shape,
- hints: hints || {}
- };
- this._commandStack.execute('shape.toggleCollapse', context);
- };
|