customContextPad.js 2.99 KB
Newer Older
wangdanlei's avatar
wangdanlei committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
import { getExtensionValue, editExtension } from './utils'
export default class CustomContextPad {
  constructor(bpmnFactory, config, contextPad, create, elementFactory, injector, translate) {
    this.bpmnFactory = bpmnFactory
    this.create = create
    this.elementFactory = elementFactory
    this.translate = translate
    this.config = config
    if (config.autoPlace !== false) {
      this.autoPlace = injector.get('autoPlace', false)
    }

    contextPad.registerProvider(this)
  }

  getContextPadEntries(element) {
    const {
      autoPlace,
      bpmnFactory,
      elementFactory,
      config
    } = this

    function appendUserTask() {
      return function(event, element) {
        if (autoPlace) {
          const extensionElements = bpmnFactory.create('bpmn:ExtensionElements')
          const Properties = bpmnFactory.create('camunda:Properties')
          const Property = bpmnFactory.create('camunda:Property')
          Property.name = 'relationInfo'
          Property.value = JSON.stringify({
            taskExtInfo: {
              custom: 'userTask'
            }
          })
          Properties.values = [Property]
          extensionElements.values = [Properties]
          const businessObject = bpmnFactory.create('bpmn:UserTask', {
            extensionElements: extensionElements
          })

          const shape = elementFactory.createShape({
            type: 'bpmn:UserTask',
            businessObject: businessObject
          })

          autoPlace.append(element, shape)
        }
      }
    }
    function changeStyle() {
      return function(event, element) {
        const custom = getExtensionValue(element.businessObject, 'custom')
        editExtension(config.modules[0].bpmnjs[1], element, 'custom', custom === 'userTask' ? 'conditionalUserTask' : 'userTask')
      }
    }
    const pad = {}
    // const taskNotify = {
    //   group: 'model',
    //   className: 'icon-custom bpmn-icon-notify',
    //   title: '插入消息任务',
    //   action: {
    //     click: appendTask('msgTask'),
    //     dragstart: appendTaskStart()
    //   }
    // }
    const userTask = {
      group: 'model',
      className: 'icon-custom bpmn-icon-custom-user-task',
      title: '插入',
      action: {
        click: appendUserTask(),
        dragstart: appendUserTask()
      }
    }
    const changeUserTaskStyle = {
      group: 'model',
      className: 'icon-custom bpmn-icon-custom-change-style',
      title: '改变用户任务的样式',
      action: {
        click: changeStyle(),
        dragstart: changeStyle()
      }
    }
    if (element.type !== 'bpmn:EndEvent' && element.type !== 'bpmn:TextAnnotation' && element.type !== 'label' && element.type !== 'bpmn:SequenceFlow') {
      // pad['append.task-notify'] = taskNotify
      pad['append.task-user'] = userTask
      pad['changeUserTaskStyle'] = changeUserTaskStyle
    }
    return pad
  }
}

CustomContextPad.$inject = [
  'bpmnFactory',
  'config',
  'contextPad',
  'create',
  'elementFactory',
  'injector',
  'translate'
]