Containers Implementation in Vanilla-Diagram
A container is a node whose symbol has container: true: the basic set's
group, or a host's own (the process-flowchart-js sample's lane).
Other nodes belong to it through their parent field. Positions stay world
px, so the geometry, the routing, the export and the data manager need no
conversion. Every change stays a phase 1 command (move, update,
remove, batch): undoable, announced by vn-diagram-changed, saved as
one change set.
The model
parentnames a container. The model keeps a children index (children, parent id to child ids) besidebyNode, updated on every add, remove and patch;childrenOf(id),descendantsOf(ids)andwithDescendants(ids)read it.- On load, a
parentis dropped, counted asinvalidParents(in the model's report, then the loaded event'sskipped), and named in the load's one warning by its first node, when it names no node, a node whose known symbol isn't a container, or the node itself; and for every node in aparentcycle. That is the Gantt's tree rule (an unknown parent or a cycle makes a root), found the same way, by walking each parent chain once (O(n)); the Gantt's code isn't loaded. A parent whose symbol isn't registered yet is kept, as a port on an unknown symbol is. Aparentofnullis no parent, not counted. - A parent whose symbol registers later. As a container, it holds its
children from then on. As a plain shape, it holds nothing:
childrenOf()reads the symbol registry the diagram givesVanillaDiagramModel.create()and returns no children for a node whose registered symbol isn't a container, so its children act top-level. They don't move, nudge, cut, copy, export or get deleted with it, and a resize doesn't stop at them. The storedparentfield stays (andtoJSON()keeps it) until the next load drops it asinvalidParents, a move of the node without that shape gives it the container under its centre or none at the release, orupdateNode(id, { parent: null })clears it. canHold(parentId, childId, symbols)is the rule: an existing node other than the child, a container when its symbol is known, and not one of the child's descendants.updateNode(id, { parent })refuses what it refuses;parent: nullmakes the node top-level.addNode()drops a parent it refuses.updateNode(id, { symbol })refuses a symbol that isn't a container for a container that holds nodes.- The
basicset'sgroup: a rounded frame, its text box a band along the top (15% of its height, up to 30 px: the text box'smaxHeight) for its label, default 320 x 200, minimum 80 x 60, the four default ports. Without the cap a tall container's band would be most of its top, its label in the middle of it.
Drawing
- A layer,
<g class="vn-diagram-frames">, sits between the grid and the connectors. Containers draw there, depth-first (a container under the containers inside it), siblings byz(_frameOrder()); every other node stays in the nodes layer, above the connectors. In the nodes layer a lane's fill would hide the connectors between its children. bringToFront()andsendToBack()on a container order it among its sibling containers.- A container's group carries
vn-diagram-node--container, which maps the node tokens to--vn-diagram-group-filland--vn-diagram-group-stroke; aswatchorstylestill wins. Its label starts at the band's start, in the page's direction. Forced colours draw the frame inCanvasTextwith no fill. - A container isn't a routing obstacle (12-routing-around-shapes); a connector may end on its ports.
Selection
- A press takes what is on top: a node in the nodes layer, else a connector,
else a container's frame. A container is picked by its border (a 12 screen
px hit stroke,
vn-diagram-frame-hit, as a connector's) and by its label band (vn-diagram-frame-band-hit); its shape takes no pointer. A press on its empty inside acts as on the empty canvas: a rubber band, a pan on touch, a click clears the selection. So a click on a shape in a lane selects the shape. - The rubber band selects containers fully inside it, as any node.
- A double-click on a container's label band edits its text (10-text-editing).
- A container has no rotation handle and
Rskips it; a rotated child is fine.
Moving
- A move (a drag or a nudge burst) of a selection holding containers moves
every descendant too: the
movecommand's ids are the selection plus every descendant, each once. One command, one change set listing every moved node. The guide index leaves out every moved node. updateNode(id, { x, y })moves that node only.
Joining and leaving
- At the release of a move, each moved node that isn't moving with its
container takes the container under its centre (
_containerAt(): the last in drawing order, so the innermost and the topmost, not moving itself) as its parent, or none. The node index finds the nodes at the point and a cached rank of the frames' order picks among the containers, rebuilt after an add, a removal, or a change of a container. A changed parent joins the move as onebatch(labelledcommand.move): one undo step, one change set. - While a move is dragged, the container the pressed node would join shows a
dashed outline in the adorner layer (
vn-diagram-join-outline). - A container can't land in its own descendant: its descendants move with it.
- A palette drop takes the container under its centre (in the
vn-diagram-node-addingdetail'snode.parent); a paste, for each pasted node whose parent wasn't pasted with it.
Resizing
A container's resize (the handles, or Ctrl/Cmd + Shift + arrows) moves only
its frame: the edges the handle moves can't cross its children's bounding box
plus 16 px (_clampContainerBox(); an edge already past it doesn't move
further in), as a box can't go below minSize. The children don't move. A
child resized past its container's frame stays its child: only a move's
release changes membership.
Removing
Removing a container removes every descendant and every connector at them,
as one remove: the remove command adds the descendants of the ids it is
given, so every path does it. _userRemove() (the Delete key,
deleteSelection(), so the toolbar's Delete) and cut() list the
descendants in vn-diagram-removing before they remove. Undo restores the
tree.
The clipboard and the export
- Copying a container copies its descendants too, and the connectors whose
ends are all among the copied nodes;
parentis remapped with the ids, and dropped when the parent isn't copied (11-clipboard-and-export). _buildExport(), sotoSVG()andtoPNG()alike, draws the frames first, then the connectors, then the nodes;{ ids }with a container includes its descendants.
Layout
layout() lays the containers out inside out: each container's children by
the connectors among its descendants, then the container takes their box
plus 16 px each side, its label band (capped at the text box's maxHeight)
kept clear, and is one box at its parent's level
(16-auto-layout).
Performance
A container's move of k descendants is O(k + c) per frame, as any move of k
nodes; the joining target is looked up per drag frame through the node index
(features/groups.feature.js is in DIAGRAM_BUNDLE_FAST_PATH). Parent
validation on load is O(n). diagram-performance.spec.js drags a container
with 20 children at 1,000 nodes within 16ms (about 2ms in Chromium).