| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149 |
- import {
- getMid
- } from '../../layout/LayoutUtil';
- import {
- isNil,
- isObject
- } from 'min-dash';
- export default function Connect(eventBus, dragging, modeling, rules) {
- // rules
- function canConnect(source, target) {
- return rules.allowed('connection.create', {
- source: source,
- target: target
- });
- }
- function canConnectReverse(source, target) {
- return canConnect(target, source);
- }
- // event handlers
- eventBus.on('connect.hover', function(event) {
- var context = event.context,
- start = context.start,
- hover = event.hover,
- canExecute;
- // cache hover state
- context.hover = hover;
- canExecute = context.canExecute = canConnect(start, hover);
- // ignore hover
- if (isNil(canExecute)) {
- return;
- }
- if (canExecute !== false) {
- context.source = start;
- context.target = hover;
- return;
- }
- canExecute = context.canExecute = canConnectReverse(start, hover);
- // ignore hover
- if (isNil(canExecute)) {
- return;
- }
- if (canExecute !== false) {
- context.source = hover;
- context.target = start;
- }
- });
- eventBus.on([ 'connect.out', 'connect.cleanup' ], function(event) {
- var context = event.context;
- context.hover = null;
- context.source = null;
- context.target = null;
- context.canExecute = false;
- });
- eventBus.on('connect.end', function(event) {
- var context = event.context,
- canExecute = context.canExecute,
- connectionStart = context.connectionStart,
- connectionEnd = {
- x: event.x,
- y: event.y
- },
- source = context.source,
- target = context.target;
- if (!canExecute) {
- return false;
- }
- var attrs = null,
- hints = {
- connectionStart: isReverse(context) ? connectionEnd : connectionStart,
- connectionEnd: isReverse(context) ? connectionStart : connectionEnd
- };
- if (isObject(canExecute)) {
- attrs = canExecute;
- }
- context.connection = modeling.connect(source, target, attrs, hints);
- });
- // API
- /**
- * Start connect operation.
- *
- * @param {DOMEvent} event
- * @param {djs.model.Base} start
- * @param {Point} [connectionStart]
- * @param {boolean} [autoActivate=false]
- */
- this.start = function(event, start, connectionStart, autoActivate) {
- if (!isObject(connectionStart)) {
- autoActivate = connectionStart;
- connectionStart = getMid(start);
- }
- dragging.init(event, 'connect', {
- autoActivate: autoActivate,
- data: {
- shape: start,
- context: {
- start: start,
- connectionStart: connectionStart
- }
- }
- });
- };
- }
- Connect.$inject = [
- 'eventBus',
- 'dragging',
- 'modeling',
- 'rules'
- ];
- // helpers //////////
- export function isReverse(context) {
- var hover = context.hover,
- source = context.source,
- target = context.target;
- return hover && source && hover === source && source !== target;
- }
|