npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@modoro/ng-flowchart

v2.1.0

Published

Flow chart

Downloads

110

Readme

NgFlowchart

Demo | Npm | Getting started | Wiki

A lightweight Angular Library for building drag and drop flow charts. Chart behavior and steps are customizable. Data can be exported or uploaded in json format.

Inspired by Alyssa X Flowy

Contents

Supported Angular versions

  • Angular 10.2.0+

Change Log

  • 1.0.0-beta
    • Support for nested charts/canvases allowing multiple steps to converge back into one. View StackBlitz
    • Additional Callbacks and hooks for the following events
      • Step method: shouldEvalDropHover
      • Canvas Callback: beforeRender
      • Canvas Callback: afterRender

Current and Upcoming Feature List

Getting started

  1. Install it.
npm i --save @joelwenzel/ng-flowchart
  1. Import it.
    In your app module or module that contains your editor, import NgFlowchartModule.
import { NgFlowchartModule } from '@joelwenzel/ng-flowchart';

@NgModule({
  imports: [
    NgFlowchartModule
  ]
})
export class AppModule { }
  1. Add your canvas directive
<div class="some-container-with-large-dimensions" ngFlowchartCanvas>
</div>
  1. Add the step directives to any elements that you want to drag into your canvas. The directive requires an input, an object containing the templateRef, stepType and optional data. See the wiki for more information.

    NOTE: The steps do not need to be in the same component as the canvas.

<div class="palette">
    <div *ngFor="let item of items" [ngFlowchartStep]="{
      template: stepContent,  //templateRef or Type<NgFlowchartStepComponent>
      type: item.type,  //any unique string. used to classify the step in json
      data: item.data  //optional config data
    }">
        <span>{{item.name}}</span>
    </div>
</div>
<ng-template #stepContent>
    <div class="step-container">
        <div class="step-heading">
            heading
        </div>
        <div class="step-content">
            content
        </div>
    </div>
</ng-template>
  1. The template field on the ngFlowchartStep directive can contain a TemplateRef, as seen above, or a component type extending from NgFlowchartStepComponent.

    For more complex steps that may need to have specific rules or add their own children, you should create a custom step component.

  2. For more features and examples checkout the official documentation

If you enjoy it give it a star

Please star the repo if you liked the library. Your support means everything to me and helps me focus on delivering new features

Chart API

The entire chart content and functionality is available via the NgFlowchart.Flow class.

//In your component.ts containing the chart

@ViewChild(NgFlowchartCanvasDirective)
canvasElement: NgFlowchartCanvasDirective;

ngOnInit() {
    let flow: NgFlowchart.Flow = this.canvasElement.getFlow();
}

The node structure resembles a linked tree with each node having at most 1 parent and n number of children

let flow: NgFlowchart.Flow = this.canvasElement.getFlow();

// returns an array of all direct children of the root
flow.getRoot().children

// the following two lines return the same node
flow.getRoot()
flow.getRoot().children[0].parent

Flow Object Methods

  • getRoot(): NgFlowchartStepComponent

    Returns the root node/step of this flowchart.

  • toJSON(indent?: number): void

    Returns the JSON representation of this flowchart. Optionally specify an indent factor

  • render(pretty?: boolean): void

    Re-renders the flowchart. This should only be needed in rare circumstances. Pretty will attempt to re-center the flow in the canvas

  • getStep(id): NgFlowchartStepComponent

    Returns a step in the flowchart with the given id. Steps are created with the id of the html element id.

  • clear(): void

    Clears the current flow, reseting the canvas. canDeleteStep callbacks will not be called from this method.

  • upload(json: string): Promise

    Clears the existing flow and uploads a new flow from the json string representation.

Step Object Methods and Properties

See the wiki for the full list and descriptions

  • data: any

    The optional config data for the step, passed in the ngFlowchartStep directive

  • children: Array<NgFlowchartStepComponent>

    The array of direct children of this step

  • parent: NgFlowchartStepComponent

    The parent step of this step

  • type: string

    The step type as passed in the ngFlowchartStep directive

  • addChild(pending: NgFlowchart.PendingStep, options?: AddChildOptions)

    Creates and adds a child to this step

  • destroy(recursive = true, checkcallbacks = true)

    Destroys this step component and updates all necessary child and parent relationships

  • removeChild(child: NgFlowchartStepComponent)

    Remove a child from this step. Returns the index at which the child was found or -1 if not found.

Generating Output JSON

The flowchart can be exported in json format via the Flow object or Step object.

//In your component.ts containing the chart

@ViewChild(NgFlowchartCanvasDirective)
canvasElement: NgFlowchartCanvasDirective;

onButtonClicked() {
    const json = this.canvasElement.getFlow().toJSON(2);
    console.log(json);
}

Here is sample json output for a very basic 3 node chart

{
  "root": {
    "id": "s1608918280530",
    "type": "sample-step",
    "data": {
      "name": "Do Action",
      "inputs": [
        {
          "name": "ACTION",
          "value": "TRANSLATE"
        }
      ]
    },
    "children": [
      {
        "id": "s1608918283650",
        "type": "do-action",
        "data": {
          "name": "Do Action",
          "inputs": [
            {
              "name": "ACTION",
              "value": null
            }
          ]
        },
        "children": []
      },
      {
        "id": "s1608918285174",
        "data": {
          "name": "Notification",
          "type": "notification",
          "inputs": [
            {
              "name": "Address",
              "value": "[email protected]"
            }
          ]
        },
        "children": []
      }
    ]
  }
}

Uploading JSON

The chart flow can be initialized from the same json represenation seen in the Generating Output JSON section.

However, due to the customizable nature of the steps, step types must be registered with the NgFlowchartStepRegistry service so they can be created correctly.

constructor(private stepRegistry: NgFlowchartStepRegistry) {

}

ngAfterViewInit() {
  //created from standared ng-template refs
  this.stepRegistry.registerStep('rest-get', this.normalStepTemplate);
  this.stepRegistry.registerStep('filter', this.normalStepTemplate);

  //created from custom component
  this.stepRegistry.registerStep('router', CustomRouterStepComponent);
}

After registering it is as simple as calling the public method on the flow object with your json.

@ViewChild(NgFlowchartCanvasDirective)
canvas: NgFlowchartCanvasDirective;

showUpload(json: string) {
  this.canvas.getFlow().upload(json);
}

Controlling Behavior

In addition to the Chart API above there are also several hooks and options to make the chart behave the way you want it to.

Options

Options are passed via the ngFlowchartOptions input on the ngFlowchartCanvas directive.

<div
    id="canvas"
    ngFlowchartCanvas
    [ngFlowchartOptions]="{
      stepGap: 40
    }"
></div>
  • stepGap

    The gap (in pixels) between chart steps. Default is 40.

  • hoverDeadzoneRadius

    An inner deadzone radius (in pixels) that will not register the hover icon. The default is 20.

  • isSequential

    Is the flow sequential? If true, then you will not be able to drag parallel steps.

  • rootPosition

    Set the default root position of the chart.

    • TOP_CENTER - Centered on the X and near the top of the Y axis
    • CENTER - Centered on both the x and y axis
    • FREE - Leave the root node wherever dropped
  • centerOnResize

    When a canvas resize is detected, should the flow be re-centered? Default is true

Callbacks

Callbacks are passed via the ngFlowchartCallbacks input on the ngFlowchartCanvas directive.

<div
    id="canvas"
    ngFlowchartCanvas
    [ngFlowchartOptions]="{
      stepGap: 40
    }"
    [ngFlowchartCallbacks]="callbacks"
></div>
//in your component.ts

callbacks: NgFlowchart.Callbacks = {};

constructor() {
  //be sure to bind this to the callback if you want to access this classes context
   this.callbacks.onDropError = this.onDropError;
   this.callbacks.onDropStep = this.onDropStep.bind(this);
   this.callbacks.onMoveError = this.onMoveError;
}

onDropError(error: NgFlowchart.DropError) {
    console.log(error);
    //show some popup
}

onMoveError(error: NgFlowchart.MoveError) {
    console.log(error);
    //show some popup
}

onDropStep(dropEvent: NgFlowchart.DropEvent) {
    this.actions.push(dropEvent);
}
  • onDropError?: (drop: DropError) => void;

    Called whenever a new step fails to drop on the canvas

  • onMoveError?: (drop: MoveError) => void;

    Called whenever an existing canvas step fails to move

  • onDropStep?: (drop: DropEvent) => void;

    Called whenever a new step or existing step is successfully dropped on the canvas

  • beforeRender?: () => void;

    Called before the canvas is about to re-render

  • afterRender?: (drop: DropEvent) => void;

    Called after the canvas completes a re-render

Custom Steps

Custom steps can be created if you need any kind of complex logic for specific steps. The example below is a custom step for a router which can be seen elsewhere on this page.

@Component({
  selector: 'app-custom-router-step',
  template: `
  <div class="router-step" #canvasContent>
    <span>{{ data.name }}</span>
    <button class="btn" (click)="onAddRoute()">Add Route</button>
  </div>
  `,
  styleUrls: ['./custom-router-step.component.scss']
})
export class CustomRouterStepComponent extends NgFlowchartStepComponent {

  routes = [];

  canDrop(dropEvent: NgFlowchart.DropTarget): boolean {
    return true;
  }

  getDropPositionsForStep(pendingStep: NgFlowchart.PendingStep): NgFlowchart.DropPosition[] {
    if (pendingStep.template !== RouteStepComponent) {
      return ['ABOVE', 'LEFT', 'RIGHT'];
    }
    else {
      return ['BELOW'];
    }
  }

  onAddRoute() {
    let route = {
      name: 'New Route',
      condition: '',
      sequence: null
    }
    let index = this.routes.push(route);
    route.sequence = index;

    this.addChild({
      template: RouteStepComponent, //another custom step
      type: 'route-step',
      data: route
    }, {
      sibling: true
    });
  }
}

Theming

For the most part, the theme is left to the user given they have complete control over the canvas content via the step templates. However, connectors and drop icons can be styled by overriding a few css classes. These styles should be placed in your root styles.scss or prefix with ::ng-deep

/** The drop icon outer circle */
.ngflowchart-step-wrapper[ngflowchart-drop-hover]::after {
  background: #a3e798 !important;
}

/** The drop icon inner circle */
.ngflowchart-step-wrapper[ngflowchart-drop-hover]::before {
  background: #4cd137 !important;
}

/** The arrow paths and arrow head */
.ngflowchart-arrow {
  & #arrowhead {
    fill: darkgrey;
  }

  & #arrowpath {
    stroke: darkgrey;
  }
}

Storing Step Data

When creating a new step, a data object can be passed to the step. This data object is completely optional but allows you to store/edit configuration data for the step. See Getting Started for passing the data.

This can even be a nested chart

export class AppComponent {
  title = 'workspace';

  @ViewChild(NgFlowchartCanvasDirective)
  canvas: NgFlowchartCanvasDirective;

  async onStepEdit(id: string) {
    let step = this.canvas.getFlow().getStep(id);

    //if data is an object then step.data will return a reference
    //returning and setting an updated version of the data may not be needed
    let updated = await this.showConfigPopup(step.data);
    step.data = updated;
  }
}

Depending on the type of data and complexity, it may be beneficial to create a component for it.

@Component({
  selector: 'app-editable-step',
  template: `
  <div class="editable-step" #canvasContent>
    <span>{{ data.name }}</span>
    <button class="btn" (click)="edit()">Edit</button>
  </div>
  `,
  styleUrls: ['./editable-step.component.scss']
})
export class EditableStepComponent extends NgFlowchartStepComponent {

  edit() {
    let updated = await this.showConfigPopup(this.data);
    this.data = updated;
  }
}

Disabling the Chart

The canvas directive binds directly to the html disabled attribute.

//component.ts
disabled = false;

//component.html
<main>
  <div
    id="canvas"
    ngFlowchartCanvas
    [ngFlowchartOptions]="options"
    [disabled]="disabled"
    
  ></div>
</main>

When the chart is disabled no styling is applied but easily can be by override the following styles:

/** Your base selector may be different */
div#canvas[disabled="true"] {
    opacity: .7;
}

/** Ng deep is only needed if not in the root styles sheet*/
div#canvas[disabled="true"] ::ng-deep.ngflowchart-step-wrapper {
    opacity: .7;
}

FAQ

Undefined variables in a callback

If you are trying to access your component variables and functions inside a callback be sure that you bound the "this" object when assigning the callback.

this.callbacks.onDropStep = this.onDropStep.bind(this);