CustomPalette.js 5.5 KB

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