CustomPalette.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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: translate("Activate the hand tool"),
  64. action: {
  65. click: function(event) {
  66. handTool.activateHand(event);
  67. }
  68. }
  69. },
  70. "lasso-tool": {
  71. group: "tools",
  72. className: "bpmn-icon-lasso-tool",
  73. title: translate("Activate the lasso tool"),
  74. action: {
  75. click: function(event) {
  76. lassoTool.activateSelection(event);
  77. }
  78. }
  79. },
  80. "space-tool": {
  81. group: "tools",
  82. className: "bpmn-icon-space-tool",
  83. title: translate("Activate the create/remove space tool"),
  84. action: {
  85. click: function(event) {
  86. spaceTool.activateSelection(event);
  87. }
  88. }
  89. },
  90. "global-connect-tool": {
  91. group: "tools",
  92. className: "bpmn-icon-connection-multi",
  93. title: translate("Activate the global connect tool"),
  94. action: {
  95. click: function(event) {
  96. globalConnect.toggle(event);
  97. }
  98. }
  99. },
  100. "tool-separator": {
  101. group: "tools",
  102. separator: true
  103. },
  104. "create.start-event": createAction("bpmn:StartEvent", "event", "bpmn-icon-start-event-none", translate("Create StartEvent")),
  105. "create.intermediate-event": createAction(
  106. "bpmn:IntermediateThrowEvent",
  107. "event",
  108. "bpmn-icon-intermediate-event-none",
  109. translate("Create Intermediate/Boundary Event")
  110. ),
  111. "create.end-event": createAction("bpmn:EndEvent", "event", "bpmn-icon-end-event-none", translate("Create EndEvent")),
  112. "create.exclusive-gateway": createAction("bpmn:ExclusiveGateway", "gateway", "bpmn-icon-gateway-none", translate("Create Gateway")),
  113. "create.user-task": createAction("bpmn:UserTask", "activity", "bpmn-icon-user-task", translate("Create User Task")),
  114. "create.data-object": createAction("bpmn:DataObjectReference", "data-object", "bpmn-icon-data-object", translate("Create DataObjectReference")),
  115. "create.data-store": createAction("bpmn:DataStoreReference", "data-store", "bpmn-icon-data-store", translate("Create DataStoreReference")),
  116. "create.subprocess-expanded": {
  117. group: "activity",
  118. className: "bpmn-icon-subprocess-expanded",
  119. title: translate("Create expanded SubProcess"),
  120. action: {
  121. dragstart: createSubprocess,
  122. click: createSubprocess
  123. }
  124. },
  125. "create.participant-expanded": {
  126. group: "collaboration",
  127. className: "bpmn-icon-participant",
  128. title: translate("Create Pool/Participant"),
  129. action: {
  130. dragstart: createParticipant,
  131. click: createParticipant
  132. }
  133. },
  134. "create.group": createAction("bpmn:Group", "artifact", "bpmn-icon-group", translate("Create Group"))
  135. });
  136. return actions;
  137. };
  138. CustomPalette.$inject = ["palette", "create", "elementFactory", "spaceTool", "lassoTool", "handTool", "globalConnect", "translate"];
  139. CustomPalette.prototype = new F(); // 核心,将 F的实例赋值给子类;
  140. CustomPalette.prototype.constructor = CustomPalette; // 修复子类CustomPalette的构造器指向,防止原型链的混乱;