vue項目中使用bpmn為節(jié)點添加顏色的方法
內(nèi)容概述
bpmn是比較方便的繪制流程圖的插件,官方demo https://github.com/bpmn-io/bpmn-js-examples
本文主要包括vue項目中bpmn使用實例、應用技巧、基本知識點總結(jié)和需要注意事項,具有一定的參考價值,需要的朋友可以參考一下。
前情提要
上文我們已經(jīng)實現(xiàn)了在外部更改節(jié)點名。此時又有新玩法:在流程圖中,根據(jù)節(jié)點狀態(tài)為其標記不同顏色。例如:已完成:黃色,正在進行:綠色,本次我們通過兩種方式來實現(xiàn)該需求。效果:
方式1:modeling.setColor
modeling.setColor接受兩個參數(shù):參數(shù)1:節(jié)點實例,可以是單個元素,也可是多個節(jié)點組成的數(shù)組,參數(shù)2:class類
let modeling = this.bpmnModeler.get(’modeling’);modeling.setColor(節(jié)點實例, { stroke: ’green’, fill: ’yellow’});
方式2:Overlay
個人理解,overlay是通過定位方式在元素正上方添加一層帶顏色的蒙板
const $overlayHtml = $(’<div class='highlight-overlay'>’).css({ width: shape.width, height: shape.height});overlays.add(節(jié)點id, { position: {top: 0, left: 0}, html: $overlayHtml});
highlight-overlay:css中聲明好的class類名
overlays.add將創(chuàng)建好的蒙板定位到指定節(jié)點位置,此時節(jié)點id就是目標節(jié)點的唯一身份!
注意事項
上述兩種方式均能實現(xiàn)為節(jié)點添加顏色。但方式2有一點副作用,如果此時你為節(jié)點注冊了點擊事件,在節(jié)點被點擊的時候要做某些處理。此時方式2會使節(jié)點點擊事件失效。
原因:方式2中,此時節(jié)點上方有一層蒙板,并且和節(jié)點等寬等高,相當于節(jié)點被蒙板完全覆蓋。所以點擊節(jié)點的時候,點擊的是蒙版,不是節(jié)點。
不要慌,有解決辦法!此時為蒙板注冊了點擊事件,將點擊節(jié)點要做的操作給蒙板也來一份,哈哈
import $ from ’jquery’; // 引入jquery$('.djs-container').on('click', '.djs-overlays', (e) => { const parentEle = e.target.parentElement.parentElement; const shapeId = parentEle.getAttribute(’data-container-id’); const temp = this.nodeList.filter( (item) => item.id === shapeId )[0]; // 此時temp就是蒙板下方的節(jié)點,要點擊節(jié)點做什么,此刻盡管拿去用 ........});
后續(xù)
上文代碼都是片段,特此附上完整代碼:老規(guī)矩:data中的chart變量流程圖xml文件數(shù)據(jù),由于行數(shù)過多,附在了附件中(點我!點我),使用時,將附件內(nèi)容復制過來,賦值給chart即可運行!
<template> <div class='containerBox'> <div id='container'></div> <div style='margin-left: 200px'>看我!我是點擊的節(jié)點名稱啊~ <span style='color: #eaae00'>{{nodeName}}</span> </div> </div></template><script> import BpmnModeler from ’bpmn-js/lib/Modeler’; import camundaExtension from ’./resources/camunda’; import {tempDetail, saveCanvas} from ’@api/processConfig’; import $ from ’jquery’; export default { name: ’index’, data() { return { containerEl: null, bpmnModeler: null, nodeName: ’’, nodeList: [], // chart變量流程圖xml文件數(shù)據(jù),由于行數(shù)過多,附在了附件中,使用時,將附件整個賦值給chart即可 chart: ’’ }; }, mounted() { this.containerEl = document.getElementById(’container’); this.bpmnModeler = new BpmnModeler({ container: this.containerEl, moddleExtensions: {camunda: camundaExtension} }); this.showChart(); }, methods: { // 流程圖回顯 showChart() { this.bpmnModeler.importXML(this.chart, (err) => { if (!err) { this.addEventBusListener(); this.setNodeColor(); } }); }, setNodeColor() { // 目的:為第一個節(jié)點添加綠色,為第二個節(jié)點添加黃色 // 實現(xiàn)步驟:1、找到頁面里所有節(jié)點 const elementRegistry = this.bpmnModeler.get(’elementRegistry’); this.nodeList = elementRegistry.filter ( (item) => item.type === ’bpmn:UserTask’ || item.type === ’bpmn:ServiceTask’ ); // 此時得到的userTaskList 便是流程圖中所有的節(jié)點的集合 console.log(this.nodeList); // 步驟2 :為節(jié)點添加顏色 // 方式1 :modeling.setColor(參數(shù)1:節(jié)點,可以是單個元素實例,也可是多個節(jié)點組成的數(shù)組,參數(shù)2:class類); let modeling = this.bpmnModeler.get(’modeling’); modeling.setColor(this.nodeList[0], { stroke: ’green’, fill: ’yellow’ }); // 方式2 :添加蒙板 const overlays = this.bpmnModeler.get(’overlays’); const shape = elementRegistry.get(this.nodeList[1].id); if (shape) { const $overlayHtml = $(’<div class='highlight-overlay'>’).css({ width: shape.width, height: shape.height }); overlays.add(this.nodeList[1].id, { position: {top: 0, left: 0}, html: $overlayHtml }); } // 此方法為了解決方式2造成的節(jié)點點擊事件失效問題,如果采用方式1,可忽略此方法 this.overlayClick(); }, overlayClick() { $('.djs-container').on('click', '.djs-overlays', (e) => { const parentEle = e.target.parentElement.parentElement; const shapeId = parentEle.getAttribute(’data-container-id’); const temp = this.nodeList.filter( (item) => item.id === shapeId )[0]; this.nodeName = temp ? temp.businessObject.name : ’’; }); }, addEventBusListener() { const eventBus = this.bpmnModeler.get(’eventBus’); // 為節(jié)點注冊點擊事件,點擊節(jié)點,下方顯示點擊的節(jié)點名稱 eventBus.on(’element.click’, (e) => { const {element} = e; if (!element.parent) return; if (!e || element.type === ’bpmn:Process’) { return false; } else { this.nodeName = element.businessObject.name; } }); } } };</script><style lang='scss'> .containerBox { height: calc(100vh - 220px); position: relative; #container { height: calc(100% - 50px); } .highlight-overlay { background-color: green; opacity: 0.4; border-radius: 10px; } }</style>
到此這篇關于vue項目中使用bpmn為節(jié)點添加顏色的方法的文章就介紹到這了,更多相關vue bpmn節(jié)點顏色內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持好吧啦網(wǎng)!
相關文章:
1. Spring security 自定義過濾器實現(xiàn)Json參數(shù)傳遞并兼容表單參數(shù)(實例代碼)2. Java8內(nèi)存模型PermGen Metaspace實例解析3. python 實現(xiàn)關聯(lián)規(guī)則算法Apriori的示例4. python利用paramiko實現(xiàn)交換機巡檢的示例5. python wsgiref源碼解析6. python學習之plot函數(shù)的使用教程7. 一文搞懂 parseInt()函數(shù)異常行為8. python中用Scrapy實現(xiàn)定時爬蟲的實例講解9. 聊聊python在linux下與windows下導入模塊的區(qū)別說明10. ASP.NET MVC使用正則表達式驗證手機號碼
