jsPlumb Community Edition Documentation

repository·master·Indexed 18 days ago

https://github.com/jsplumb/community-edition

An 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.

Tokens
63.9K
Snippets
197
Records
287
Agent score
63%

What's inside jsPlumb Community Edition

  1. Overview of the Dynamic Anchors demonstration

    master

    The dynamic-anchors demonstration 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:

    1. ES5 version: Located in the js folder.
    2. TypeScript version: Located in the ts folder, which utilizes Babel and Rollup for packaging.
  2. Explore the Flowchart demonstration

    master

    The flowchart demonstration 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:

    1. TypeScript version (ts): Uses Rollup and Babel for bundling.
    2. 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.

  3. Understand EndpointRepresentation and EndpointFactory

    master

    While Endpoint is the functional instance, EndpointRepresentation<C> is an abstract class representing the visual and spatial state of an endpoint. It tracks properties like x, y, w (width), h (height), and bounds.

    EndpointFactory is the utility used to manage these representations:

    • get(...): Retrieves an EndpointRepresentation.
    • clone(epr): Creates a copy of a representation.
    • compute(...): Calculates the endpoint's position based on anchor points and orientation.
    • registerHandler(eph): Registers a custom EndpointHandler to define how new endpoint types are created and computed.
  4. Core Concepts of jsPlumb Connections

    master

    A Connection in 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 = 2 Endpoints + 1 Connector + zero or more Overlays. Each Endpoint is associated with an Anchor.

  5. What are Interceptors and how to use them

    master

    Interceptors 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:

    1. Global Binding: Use jsPlumbInstance.bind(interceptorName, callback) to create a catch-all handler. For example, a global beforeDrop will trigger for any connection dropped on any endpoint unless that specific endpoint has its own interceptor.
    2. Local Configuration: Pass interceptor callbacks directly into methods like addEndpoint, makeSource, or makeTarget to 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.
  6. Manage connection scopes for drag and drop

    master

    Scopes 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 via jsPlumb.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), or setTargetScope(el, scope) to update an existing configuration.
    • Drag/Drop Options: You can pass scope through dragOptions and dropOptions to 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");
  7. Understand Drag event payloads

    master

    jsPlumb uses several payload interfaces to pass data during drag lifecycle events.

    • DragPayload: The base interface containing:

      • e: The original Event.
      • el: The Element being interacted with.
      • originalPosition: The PointXY where the drag started.
      • pos: The current PointXY position.
      • payload: An optional Record<string, any> for custom data.
    • DragStartPayload: Extends DragPayload and includes dragGroup and dragGroupMemberSpec.

    • DragStopPayload: Extends DragPayload and includes an elements array of DraggedElement objects.

    • DragMovePayload: Extends DragPayload for movement updates.

  8. Understand UINode and UIGroup relationship

    master

    In the @jsplumb/browser-ui package, the UI hierarchy is built using UINode and UIGroup:

    • UINode<E>: Represents a single UI element (the el) managed by jsPlumb. It holds a reference to its parent group and the instance.
    • UIGroup<E>: A specialized UINode that acts as a container for other nodes and groups. It manages the lifecycle and spatial constraints of its children.
  9. Configure Connectors in jsPlumb

    master

    Connectors 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 connector property in the following methods:

    • jsPlumb.connect
    • jsPlumb.addEndpoint(s)
    • jsPlumb.makeSource
    • jsPlumb.makeTarget

    If no connector property is provided, jsPlumb defaults to the Bezier implementation.

  10. What are Overlays in jsPlumb

    master

    Overlays 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.5 is the midpoint). Default is 0.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() and setLocation() methods.

  11. Use Interceptors to control connection behavior

    master

    jsPlumb 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. Returning false cancels the drag. Returning a Record<string, any> allows you to pass additional data to the drag operation.
    • BeforeDropInterceptor: Intercepts a drop operation. Returning false prevents the connection from being established.
    • BeforeDetachInterceptor: Intercepts the detachment of a connection. Returning false prevents 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';
    });