jsPlumb Community Edition Documentation
repository·master·Indexed 18 days ago
https://github.com/jsplumb/community-editionAn open-source library for creating interactive diagrams and visual connectivity in web applications. Version 6.2.10 provides tools for managing connections, anchors, and drag-and-drop interactions via the @jsplumb/browser-ui package. Key features include programmatic connection establishment using jsPlumb.connect, customizable paint and endpoint styles, dynamic anchors, and support for both TypeScript and ES5 workflows.
What's inside jsPlumb Community Edition
- jsPlumb Community Edition is a library that allows developers to visually connect elements on web pages using SVG. It has no external dependencies and is designed for modern browsers that support SVG.
Overview of the Dynamic Anchors demonstration
masterThe
dynamic-anchorsdemonstration showcases how to implement Endpoints with "Dynamic" anchors using jsPlumb Community Edition 6.x.Key features demonstrated include:
- Setting up Endpoints with anchors that support multiple locations.
- Dragging Connections between these dynamic anchors.
The demonstration is provided in two formats:
- ES5 version: Located in the
jsfolder. - TypeScript version: Located in the
tsfolder, which utilizes Babel and Rollup for packaging.
Explore the Flowchart demonstration
masterThe
flowchartdemonstration showcases how to build a simple flowchart using jsPlumb Community Edition (version 6.x). The demonstration is provided in two distinct implementations within the repository:- TypeScript version (
ts): Uses Rollup and Babel for bundling. - Vanilla JavaScript version (
js): A standard JavaScript implementation.
Use these versions to understand how to implement node-and-edge logic for flowcharting applications using the jsPlumb API.
- TypeScript version (
Understand EndpointRepresentation and EndpointFactory
masterWhile
Endpointis the functional instance,EndpointRepresentation<C>is an abstract class representing the visual and spatial state of an endpoint. It tracks properties likex,y,w(width),h(height), andbounds.EndpointFactoryis the utility used to manage these representations:get(...): Retrieves anEndpointRepresentation.clone(epr): Creates a copy of a representation.compute(...): Calculates the endpoint's position based on anchor points and orientation.registerHandler(eph): Registers a customEndpointHandlerto define how new endpoint types are created and computed.
Core Concepts of jsPlumb Connections
masterA
Connectionin jsPlumb is the primary abstraction representing the link between two elements. It is composed of several constituent parts:- Endpoint: The visual representation of one end of a connection. You can create them manually (required for drag-and-drop support) or let jsPlumb create them via
jsPlumb.connect(...). - Anchor: A logical position relative to an element's origin where an Endpoint exists. Anchors have no visual representation and are created automatically based on hints you provide.
- Connector: The visual line (e.g., Bezier curve, straight line, flowchart, or state machine) that joins two Endpoints.
- Overlay: UI components used to decorate a Connector, such as Labels or Arrows.
- Group: A container for a set of elements that can be collapsed, causing all connections to group members to be pooled onto the group container.
Relationship Model: One
Connection= 2Endpoints+ 1Connector+ zero or moreOverlays. EachEndpointis associated with anAnchor.- Endpoint: The visual representation of one end of a connection. You can create them manually (required for drag-and-drop support) or let jsPlumb create them via
What are Interceptors and how to use them
masterInterceptors are specialized event handlers that allow you to abort jsPlumb actions by returning
false. They act as gatekeepers for connection lifecycle events.Registration Methods
You can register interceptors in two ways:
- Global Binding: Use
jsPlumbInstance.bind(interceptorName, callback)to create a catch-all handler. For example, a globalbeforeDropwill trigger for any connection dropped on any endpoint unless that specific endpoint has its own interceptor. - Local Configuration: Pass interceptor callbacks directly into methods like
addEndpoint,makeSource, ormakeTargetto constrain the interceptor to a specific element or endpoint.
Supported Interceptors
beforeDrop: Triggered when a connection is dropped onto a target.beforeDetach: Triggered when a connection is being detached (e.g., dragged off an endpoint into whitespace).beforeDrag: Triggered when a user starts dragging a new connection from an endpoint.beforeStartDetach: Triggered when a user starts dragging an existing connection off an endpoint.
- Global Binding: Use
Manage connection scopes for drag and drop
masterScopes control which draggables can be dropped on which droppables. A draggable can only be dropped on a droppable if they share the same scope.
- Default Scope: Accessible via
jsPlumb.getDefaultScope(). Set it viajsPlumb.setDefaultScope(string). - Multiple Scopes: You can assign multiple scopes to an endpoint or element by providing a space-separated string (similar to CSS classes).
- Setting Scopes: Use
setScope(el, scope),setSourceScope(el, scope), orsetTargetScope(el, scope)to update an existing configuration. - Drag/Drop Options: You can pass
scopethroughdragOptionsanddropOptionsto the underlying library.
// Assigning multiple scopes var options = { scope: "foo bar baz" }; // Providing scope via drag/drop options var options = { dragOptions: { scope: "dragScope" }, dropOptions: { scope: "dropScope" } }; // Changing scope of an existing element jsPlumb.setSourceScope("el1", "newScope");- Default Scope: Accessible via
Understand Drag event payloads
masterjsPlumb uses several payload interfaces to pass data during drag lifecycle events.
DragPayload: The base interface containing:e: The originalEvent.el: TheElementbeing interacted with.originalPosition: ThePointXYwhere the drag started.pos: The currentPointXYposition.payload: An optionalRecord<string, any>for custom data.
DragStartPayload: ExtendsDragPayloadand includesdragGroupanddragGroupMemberSpec.DragStopPayload: ExtendsDragPayloadand includes anelementsarray ofDraggedElementobjects.DragMovePayload: ExtendsDragPayloadfor movement updates.
Understand UINode and UIGroup relationship
masterIn the
@jsplumb/browser-uipackage, the UI hierarchy is built usingUINodeandUIGroup:UINode<E>: Represents a single UI element (theel) managed by jsPlumb. It holds a reference to its parentgroupand theinstance.UIGroup<E>: A specializedUINodethat acts as a container for other nodes and groups. It manages the lifecycle and spatial constraints of its children.
Configure Connectors in jsPlumb
masterConnectors are the lines that join UI elements. jsPlumb provides four implementation types: Bezier (the default), Straight, Flowchart, and State Machine.
You can specify a connector by setting the
connectorproperty in the following methods:jsPlumb.connectjsPlumb.addEndpoint(s)jsPlumb.makeSourcejsPlumb.makeTarget
If no
connectorproperty is provided, jsPlumb defaults to the Bezier implementation.What are Overlays in jsPlumb
masterOverlays are UI elements painted onto Connections or Endpoints, such as Arrows, Labels, or custom DOM elements.
Overlay Location
Location determines where the overlay is placed along the path of a Connector or within an Endpoint:
For Connectors:
- Decimal [0..1]: Proportional travel along the path (e.g.,
0.5is the midpoint). Default is0.5. - Integer > 1: Absolute number of pixels from the start point (source).
- Integer < 0: Absolute number of pixels backwards from the end point (target).
For Endpoints:
- Specified as an
[x, y]array. - Proportional: Decimals in range
0-1(e.g.,[0.5, 0.5]is the center). - Absolute: Decimals greater than
0(e.g.,[5, 0]is 5 pixels from the top-left corner;[-5, 0]is 5 pixels from the bottom-right corner).
All overlays support
getLocation()andsetLocation()methods.- Decimal [0..1]: Proportional travel along the path (e.g.,
Use Interceptors to control connection behavior
masterjsPlumb provides several interceptor types that allow you to programmatically control or modify connection lifecycle events. These are useful for implementing validation logic, such as preventing certain connections or modifying drag parameters.
Key interceptor types include:
BeforeDragInterceptor: Intercepts a drag operation. Returningfalsecancels the drag. Returning aRecord<string, any>allows you to pass additional data to the drag operation.BeforeDropInterceptor: Intercepts a drop operation. Returningfalseprevents the connection from being established.BeforeDetachInterceptor: Intercepts the detachment of a connection. Returningfalseprevents detachment.BeforeStartDetachInterceptor: Intercepts the start of a detachment process.
// Example: Preventing a connection if certain conditions aren't met instance.registerInterceptor('beforeDrop', (params: BeforeDropParams) => { const { connection, targetId, sourceId } = params; // Logic to decide if connection is allowed return targetId !== 'forbidden-zone'; });