bpmn.js Modeling API 列表

news/2024/5/18 23:25:10 标签: 前端, 流程图, javascript

原内容位于官方github的examples下,这里给整理出来并部分进行了中文翻译以方便查看,可以借助浏览器的搜索功能进行快速检索,Chrome下搜索快捷键ctrl+F,Mac下搜索快捷键Command+F

Introduction - 介绍

javascript">/**
 * After creating a new instance of bpmn-js you can access any module of bpmn-js using
 * modeler#get.
 *
 * The modules used in this example are:
 *
 * * ElementFactory: Creates new shapes and connections.
 * * ElementRegistry: A registry of all shapes and connections of the diagram.
 * * Modeling: The main module for modeling.
 *
 * We will use these modules to create a new shape, add it to the diagram, and
 * connect it to an existing shape.
 */

// (1) 获取modules
const elementFactory = modeler.get('elementFactory'),
      elementRegistry = modeler.get('elementRegistry'),
      modeling = modeler.get('modeling');

// (2) 根据ID获取Process和shape
const process = elementRegistry.get('Process_1'),
      startEvent = elementRegistry.get('StartEvent_1');

// (3) 新建Shape
const task = elementFactory.createShape({ type: 'bpmn:Task' });

// (4) 添加新的shape到当前流程图
modeling.createShape(task, { x: 400, y: 100 }, process);

// You can now access the new task through the element registry
console.log(elementRegistry.get(task.id)); // Shape { "type": "bpmn:Task", ... }

// (5) 创建连线
modeling.connect(startEvent, task);

Business Objects - 业务对象

javascript">/**
 * Business objects are the model objects that hold all the BPMN-related properties of a shape or
 * connection. They can be accessed through an element's `businessObject` property. Not all model
 * objects are visible as shapes or connections. An event definition for example is a model object
 * that is part of an event shape's business object.
 *
 * The entire BPMN model can be found here: https://github.com/bpmn-io/bpmn-moddle/blob/master/resources/bpmn/json/bpmn.json
 *
 * The modules used in this example are:
 *
 * * BpmnFactory: Creates new business objects.
 * * ElementFactory: Creates new shapes and connections.
 * * ElementRegistry: A registry of all shapes and connections of the diagram.
 * * Modeling: The main module for modeling.
 *
 * We will use these modules to create a new business object representing a shape,
 * add it to the diagram, and connect it to an existing shape.
 */

// (1) 获取modules
const bpmnFactory = modeler.get('bpmnFactory'),
      elementFactory = modeler.get('elementFactory'),
      elementRegistry = modeler.get('elementRegistry'),
      modeling = modeler.get('modeling');

// (2) 根据ID获取Process和shape
const process = elementRegistry.get('Process_1'),
      startEvent = elementRegistry.get('StartEvent_1');

// You can access the start event's business object
console.log(startEvent.businessObject); // { "$type": "bpmn:StartEvent", ... }

// (3) 通过BPMN factory的方式来创建对象
const taskBusinessObject = bpmnFactory.create('bpmn:Task', { id: 'Task_1', name: 'Task' });

// (4) 使用刚刚创建的业务对象创建一个新的shape
const task = elementFactory.createShape({ type: 'bpmn:Task', businessObject: taskBusinessObject });

// (5) 添加新的shape到当前流程图
modeling.createShape(task, { x: 400, y: 100 }, process);

// Using the `id` property we specified you can now access the new task through the element registry
console.log(elementRegistry.get('Task_1')); // Shape { "type": "bpmn:Task", ... }

Creating Shapes - 创建形状

javascript">/**
 * Now that we have created our first shapes let's create some more complex shapes.
 *
 * The modules used in this example are:
 *
 * * BpmnFactory: Creates new business objects.
 * * ElementFactory: Creates new shapes and connections.
 * * ElementRegistry: A registry of all shapes and connections of the diagram.
 * * Modeling: The main module for modeling.
 *
 * We will use these modules to create some more shapes including a BoundaryEvent and
 * a SubProcess.
 */

// (1) 获取modules
const bpmnFactory = modeler.get('bpmnFactory'),
      elementFactory = modeler.get('elementFactory'),
      elementRegistry = modeler.get('elementRegistry'),
      modeling = modeler.get('modeling');

// (2) 根据ID获取Process和shape
const process = elementRegistry.get('Process_1'),
      startEvent = elementRegistry.get('StartEvent_1');

// (3) 创建一个service task类型的shape
const serviceTask = elementFactory.createShape({ type: 'bpmn:ServiceTask' });

// (4) 使用appendShape将新的service task类型的shape添加到现有流程图中
modeling.appendShape(startEvent, serviceTask, { x: 400, y: 100 }, process);

// (5) 创建一个boundary event边界事件类型的shape
const boundaryEvent = elementFactory.createShape({ type: 'bpmn:BoundaryEvent' });

// (6) 将新边界事件添加到流程图中,并将其附加到serviceTask
modeling.createShape(boundaryEvent, { x: 400, y: 140 }, serviceTask, { attach: true });

// (7) 创建sub process子流程类型的对象
const eventSubProcessBusinessObject = bpmnFactory.create('bpmn:SubProcess', {
  triggeredByEvent: true,
  isExpanded: true
});

// (8) 创建SubProcess子流程shape,并进行属性设置
const eventSubProcess = elementFactory.createShape({
  type: 'bpmn:SubProcess',
  businessObject: eventSubProcessBusinessObject,
  isExpanded: true
});

// (9) 将sub process子流程添加到流程中
modeling.createShape(eventSubProcess, { x: 300, y: 400 }, process);

// (10) 创建eventDefinitionType的计时器启动事件
const timerStartEvent = elementFactory.createShape({
  type: 'bpmn:StartEvent',
  eventDefinitionType: 'bpmn:TimerEventDefinition'
});

// (11) 添加新的timerStartEvent事件到当前流程的子流程中
modeling.createShape(timerStartEvent, { x: 200, y: 400 }, eventSubProcess);

// (12) 创建新的group shape,并指定宽和高
const group = elementFactory.createShape({ type: 'bpmn:Group', width: 400, height: 200 });

// (13) 将group shape添加到现有的流程中
modeling.createShape(group, { x: 325, y: 100 }, process);

// (14) 创建两个shape,并指定坐标
// coordinates, not absolute
const messageStartEvent = elementFactory.createShape({
  type: 'bpmn:StartEvent',
  eventDefinitionType: 'bpmn:MessageEventDefinition',
  x: 0,
  y: 22
});

const userTask = elementFactory.createShape({
  type: 'bpmn:UserTask',
  x: 100,
  y: 0
});

// (15) 一次向流程图中添加多个shape
modeling.createElements([ messageStartEvent, userTask ], { x: 300, y: 600 }, process);

Connecting Shapes - 连线形状

javascript">/**
 * Now let's look at the different ways of connecting shapes to each other.
 *
 * The modules used in this example are:
 *
 * * ElementFactory: Creates new shapes and connections.
 * * ElementRegistry: A registry of all shapes and connections of the diagram.
 * * Modeling: The main module for modeling.
 *
 * We will use these modules to create shapes and connect them on two different ways.
 */

// (1) 获取modules
const elementFactory = modeler.get('elementFactory'),
      elementRegistry = modeler.get('elementRegistry'),
      modeling = modeler.get('modeling');

// (2) 根据ID获取Process和shape
const process = elementRegistry.get('Process_1'),
      startEvent = elementRegistry.get('StartEvent_1');

// (3) 创建一个shape
const task = elementFactory.createShape({ type: 'bpmn:Task' });

// (4) 添加新的shape到流程图
modeling.createShape(task, { x: 400, y: 100 }, process);

// (5) 用connect连接startEvent到新添加的shape
modeling.connect(startEvent, task);

// (6) 创建一个end event类型的shape
const endEvent = elementFactory.createShape({ type: 'bpmn:EndEvent' });

// (7) 添加新的endEvent类型的shape添加到流程图中
modeling.createShape(endEvent, { x: 600, y: 100 }, process);

// (8) 创建一条新的连线连接task shape到endEvent事件
modeling.createConnection(task, endEvent, { type: 'bpmn:SequenceFlow' }, process);

Collaborations - 合作开发

javascript">/**
 * So far we've worked with processes. Let's have a look at collaborations.
 *
 * The modules used in this example are:
 *
 * * ElementFactory: Creates new shapes and connections.
 * * ElementRegistry: A registry of all shapes and connections of the diagram.
 * * Modeling: The main module for modeling.
 *
 * We will use these modules to create Participants, add them to the diagram (thereby
 * turning the process into a collaboration), create lanes and connect participants
 * using Message Flows.
 */

// (1) 获取modules
const elementFactory = modeler.get('elementFactory'),
      elementRegistry = modeler.get('elementRegistry'),
      modeling = modeler.get('modeling');

// (2) 根据ID获取Process和shape
const process = elementRegistry.get('Process_1'),
      startEvent = elementRegistry.get('StartEvent_1');

// (3) 使用createParticipantShape创建一个新的Participant类型是shape
const participant = elementFactory.createParticipantShape({ type: 'bpmn:Participant' });

// (4) 将新参与者添加到流程图中,从而将流程转变为协作流程
modeling.createShape(participant, { x: 400, y: 100 }, process);

// The existing start event is now a child of the participant
console.log(startEvent.parent); // Shape { "type": "bpmn:Participant", ... }

// (5) 创建泳道
const lane = modeling.addLane(participant, 'bottom');

// (6) 创建两个镶嵌的泳道
modeling.splitLane(lane, 2);

// (7) 创建另一个participant类型的shape
const collapsedParticipant = elementFactory
  .createParticipantShape({ type: 'bpmn:Participant', isExpanded: false });

// (8) 添加participant到流程图中
modeling.createShape(collapsedParticipant, { x: 300, y: 500 }, process);

// (9) 通过message消息连接两个participant
modeling.connect(collapsedParticipant, participant);

Editing Elements - 编辑元素

javascript">/**
 * Now, let's have a look at how you can edit existing elements.
 *
 * The modules used in this example are:
 *
 * * BpmnFactory: Creates new business objects.
 * * ElementRegistry: A registry of all shapes and connections of the diagram.
 * * Modeling: The main module for modeling.
 *
 * We will use these modules to update the properties of two existing shapes.
 */

// (1) 获取modules
const bpmnFactory = modeler.get('bpmnFactory'),
      elementRegistry = modeler.get('elementRegistry'),
      modeling = modeler.get('modeling');

// (2) 获取shapes
const startEvent = elementRegistry.get('StartEvent_1'),
      exclusiveGateway = elementRegistry.get('ExclusiveGateway_1'),
      sequenceFlow = elementRegistry.get('SequenceFlow_3'),
      task = elementRegistry.get('Task_1');

// (3) 通过updateProperties来更改start event的name属性
modeling.updateProperties(startEvent, { name: 'Foo' });

// (4) 更改网关的defaultFlow属性
modeling.updateProperties(exclusiveGateway, {
  default: sequenceFlow.businessObject
});

// (5) 将任务更改为multi-instance
const multiInstanceLoopCharacteristics = bpmnFactory.create('bpmn:MultiInstanceLoopCharacteristics');

modeling.updateProperties(task, {
  loopCharacteristics: multiInstanceLoopCharacteristics
});

http://www.niftyadmin.cn/n/5422949.html

相关文章

python-0001-安装虚拟环境

版本 软件版本python3.6django2.2.5sqlite33.45.1pycharm2023.3.4 升级sqlite3 下载地址:https://download.csdn.net/download/qq_41833259/88944701 升级命令: tar -zxvf sqlite-autoconf-3399999.tar.gz cd sqlite-autoconf-3399999 ./configure m…

#stm32外设总结电容触摸按键

BS8116A-3 IRQ 外部中断请求 NMOS输出内部上拉 SCL SDA IIC通信接口 VDD 供电电压2.2-5.5V Ct电容: 0~25 pF 电容越大灵敏度越低 1、 软件使用流程 初始化 将IIC的两个引脚初始化为复用开漏模式 按键引脚设置上拉输入 下降沿触发外部中断 void KEY_Init(void) {//uint8_t …

1、设计模式之认识设计模式

设计模式是一种经过验证的、被广泛应用的解决特定问题的软件设计方案,它提供了一种在软件设计中反复使用的解决方案。设计模式通常描述了一个问题的情境、解决方案和解决方案的优点和缺点。设计模式不是一种具体的编程语言特性或库,而是一种通用的设计思…

STM32存储左右互搏 SPI总线读写SD/MicroSD/TF卡

STM32存储左右互搏 SPI总线读写SD/MicroSD/TF卡 SD/MicroSD/TF卡是基于FLASH的一种常见非易失存储单元,由接口协议电路和FLASH构成。市面上由不同尺寸和不同容量的卡,手机领域用的TF卡实际就是MicroSD卡,尺寸比SD卡小,而电路和协…

win7粘滞键漏洞密码破解

前提: 1.不借助U盘等工具。 2.已将win7登录账户为test,密码为123456 1、将电脑开机关机几次,进入以下界面 2、然后点击启动修复(推荐),进入以下界面 3、接着就进入到以下界面,然后点击查看问题详细信息 …

【面试】Redis

为什么要用缓存? 使用缓存的目的就是提升读写性能。在实际的业务场景下,更多的是为了提升读性能,带来更好的性能和并发量。Redis的读写性能比MySQL好的多,我们就可以把MySQL中的热点数据缓存到Redis,提升读取性能&…

html5cssjs代码 008 名画欣赏

html5&css&js代码 008 名画欣赏 一、代码二、解释 这段HTML代码定义了一个网页,展示了名画欣赏的内容。主要包括页面的标题、样式定义和主体内容。其中,样式定义使用了CSS来控制页面的布局和外观,主体内容使用了结构化的HTML标签来展…

深入理解Java中的线程安全List:CopyOnWriteArrayList原理和应用

码到三十五 : 个人主页 心中有诗画,指尖舞代码,目光览世界,步履越千山,人间尽值得 ! 在Java并发编程中,线程安全的数据结构是至关重要的。其中,CopyOnWriteArrayList是一个线程安全的ArrayLis…