-
Notifications
You must be signed in to change notification settings - Fork 418
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
5 changed files
with
810 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
import { | ||
asTRBL, | ||
getMid | ||
} from '../../layout/LayoutUtil'; | ||
|
||
import { DEFAULT_DISTANCE } from './AutoPlaceUtil'; | ||
|
||
var LOW_PRIORITY = 100; | ||
|
||
|
||
/** | ||
* A service that places elements connected to existing ones | ||
* to an appropriate position in an _automated_ fashion. | ||
* | ||
* @param {EventBus} eventBus | ||
* @param {Modeling} modeling | ||
*/ | ||
export default function AutoPlace(eventBus, modeling) { | ||
|
||
eventBus.on('autoPlace', LOW_PRIORITY, function(context) { | ||
var shape = context.shape, | ||
source = context.source; | ||
|
||
return getNewShapePosition(source, shape); | ||
}); | ||
|
||
/** | ||
* Append shape to source at appropriate position. | ||
* | ||
* @param {djs.model.Shape} source | ||
* @param {djs.model.Shape} shape | ||
* | ||
* @return {djs.model.Shape} appended shape | ||
*/ | ||
this.append = function(source, shape, hints) { | ||
|
||
eventBus.fire('autoPlace.start', { | ||
source: source, | ||
shape: shape | ||
}); | ||
|
||
// allow others to provide the position | ||
var position = eventBus.fire('autoPlace', { | ||
source: source, | ||
shape: shape | ||
}); | ||
|
||
var newShape = modeling.appendShape(source, shape, position, source.parent, hints); | ||
|
||
eventBus.fire('autoPlace.end', { | ||
source: source, | ||
shape: newShape | ||
}); | ||
|
||
return newShape; | ||
}; | ||
|
||
} | ||
|
||
AutoPlace.$inject = [ | ||
'eventBus', | ||
'modeling' | ||
]; | ||
|
||
// helpers ////////// | ||
|
||
/** | ||
* Find the new position for the target element to | ||
* connect to source. | ||
* | ||
* @param {djs.model.Shape} source | ||
* @param {djs.model.Shape} element | ||
* @param {Object} [hints] | ||
* @param {Object} [hints.defaultDistance] | ||
* | ||
* @returns {Point} | ||
*/ | ||
export function getNewShapePosition(source, element, hints) { | ||
if (!hints) { | ||
hints = {}; | ||
} | ||
|
||
var distance = hints.defaultDistance || DEFAULT_DISTANCE; | ||
|
||
var sourceMid = getMid(source), | ||
sourceTrbl = asTRBL(source); | ||
|
||
// simply put element right next to source | ||
return { | ||
x: sourceTrbl.right + distance + element.width / 2, | ||
y: sourceMid.y | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
/** | ||
* Select element after auto placement. | ||
* | ||
* @param {EventBus} eventBus | ||
* @param {Selection} selection | ||
*/ | ||
export default function AutoPlaceSelectionBehavior(eventBus, selection) { | ||
|
||
eventBus.on('autoPlace.end', 500, function(e) { | ||
selection.select(e.shape); | ||
}); | ||
|
||
} | ||
|
||
AutoPlaceSelectionBehavior.$inject = [ | ||
'eventBus', | ||
'selection' | ||
]; |
Oops, something went wrong.