CustomPalette.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. import PaletteProvider from 'bpmn-js/lib/features/palette/PaletteProvider'
  2. import { assign } from "min-dash"
  3. export default function CustomPalette (palette, create, elementFactory, spaceTool, lassoTool, handTool, globalConnect, translate) {
  4. PaletteProvider.call(this, palette, create, elementFactory, spaceTool, lassoTool, handTool, globalConnect, translate, 2000)
  5. }
  6. const F = function () { } // 核心,利用空对象作为中介;
  7. F.prototype = PaletteProvider.prototype // 核心,将父类的原型赋值给空对象F;
  8. // 利用中介函数重写原型链方法
  9. F.prototype.getPaletteEntries = function () {
  10. const actions = {},
  11. create = this._create,
  12. elementFactory = this._elementFactory,
  13. spaceTool = this._spaceTool,
  14. lassoTool = this._lassoTool,
  15. handTool = this._handTool,
  16. globalConnect = this._globalConnect,
  17. translate = this._translate
  18. function createAction (type, group, className, title, options) {
  19. function createListener (event) {
  20. const shape = elementFactory.createShape(assign({ type: type }, options))
  21. if (options) {
  22. shape.businessObject.di.isExpanded = options.isExpanded
  23. }
  24. create.start(event, shape)
  25. }
  26. const shortType = type.replace(/^bpmn:/, '')
  27. return {
  28. group: group,
  29. className: className,
  30. title: title || translate("Create {type}", { type: shortType }),
  31. action: {
  32. dragstart: createListener,
  33. click: createListener
  34. }
  35. }
  36. }
  37. function createSubprocess (event) {
  38. const subProcess = elementFactory.createShape({
  39. type: 'bpmn:SubProcess',
  40. x: 0,
  41. y: 0,
  42. isExpanded: true
  43. })
  44. const startEvent = elementFactory.createShape({
  45. type: 'bpmn:StartEvent',
  46. x: 40,
  47. y: 82,
  48. parent: subProcess
  49. })
  50. create.start(event, [subProcess, startEvent], {
  51. hints: {
  52. autoSelect: [startEvent]
  53. }
  54. })
  55. }
  56. function createParticipant (event) {
  57. create.start(event, elementFactory.createParticipantShape())
  58. }
  59. assign(actions, {
  60. "hand-tool": {
  61. group: "tools",
  62. className: "bpmn-icon-hand-tool",
  63. title: '激活抓手工具',
  64. // title: translate("Activate the hand tool"),
  65. action: {
  66. click: function (event) {
  67. handTool.activateHand(event)
  68. }
  69. }
  70. },
  71. "lasso-tool": {
  72. group: "tools",
  73. className: "bpmn-icon-lasso-tool",
  74. title: translate("Activate the lasso tool"),
  75. action: {
  76. click: function (event) {
  77. lassoTool.activateSelection(event)
  78. }
  79. }
  80. },
  81. "space-tool": {
  82. group: "tools",
  83. className: "bpmn-icon-space-tool",
  84. title: translate("Activate the create/remove space tool"),
  85. action: {
  86. click: function (event) {
  87. spaceTool.activateSelection(event)
  88. }
  89. }
  90. },
  91. "global-connect-tool": {
  92. group: "tools",
  93. className: "bpmn-icon-connection-multi",
  94. title: translate("Activate the global connect tool"),
  95. action: {
  96. click: function (event) {
  97. globalConnect.toggle(event)
  98. }
  99. }
  100. },
  101. "tool-separator": {
  102. group: "tools",
  103. separator: true
  104. },
  105. "create.start-event": createAction("bpmn:StartEvent", "event", "bpmn-icon-start-event-none", translate("Create StartEvent")),
  106. "create.intermediate-event": createAction(
  107. "bpmn:IntermediateThrowEvent",
  108. "event",
  109. "bpmn-icon-intermediate-event-none",
  110. translate("Create Intermediate/Boundary Event")
  111. ),
  112. "create.end-event": createAction("bpmn:EndEvent", "event", "bpmn-icon-end-event-none", translate("Create EndEvent")),
  113. "create.exclusive-gateway": createAction("bpmn:ExclusiveGateway", "gateway", "bpmn-icon-gateway-none", translate("Create Gateway")),
  114. "create.user-task": createAction("bpmn:UserTask", "activity", "bpmn-icon-user-task", translate("Create User Task")),
  115. "create.data-object": createAction("bpmn:DataObjectReference", "data-object", "bpmn-icon-data-object", translate("Create DataObjectReference")),
  116. "create.data-store": createAction("bpmn:DataStoreReference", "data-store", "bpmn-icon-data-store", translate("Create DataStoreReference")),
  117. "create.subprocess-expanded": {
  118. group: "activity",
  119. className: "bpmn-icon-subprocess-expanded",
  120. title: translate("Create expanded SubProcess"),
  121. action: {
  122. dragstart: createSubprocess,
  123. click: createSubprocess
  124. }
  125. },
  126. "create.participant-expanded": {
  127. group: "collaboration",
  128. className: "bpmn-icon-participant",
  129. title: translate("Create Pool/Participant"),
  130. action: {
  131. dragstart: createParticipant,
  132. click: createParticipant
  133. }
  134. },
  135. "create.group": createAction("bpmn:Group", "artifact", "bpmn-icon-group", translate("Create Group"))
  136. })
  137. return actions
  138. }
  139. CustomPalette.$inject = ["palette", "create", "elementFactory", "spaceTool", "lassoTool", "handTool", "globalConnect", "translate"]
  140. CustomPalette.prototype = new F() // 核心,将 F的实例赋值给子类;
  141. CustomPalette.prototype.constructor = CustomPalette // 修复子类CustomPalette的构造器指向,防止原型链的混乱;