contentPadProvider.js 10.5 KB
Newer Older
sunhongwei's avatar
sunhongwei 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
import { assign, forEach, isArray } from "min-dash";

import { is } from "bpmn-js/lib/util/ModelUtil";

import { isExpanded, isEventSubProcess } from "bpmn-js/lib/util/DiUtil";

import { isAny } from "bpmn-js/lib/features/modeling/util/ModelingUtil";

import { getChildLanes } from "bpmn-js/lib/features/modeling/util/LaneUtil";

import { hasPrimaryModifier } from "diagram-js/lib/util/Mouse";

/**
 * A provider for BPMN 2.0 elements context pad
 */
export default function ContextPadProvider(
  config,
  injector,
  eventBus,
  contextPad,
  modeling,
  elementFactory,
  connect,
  create,
  popupMenu,
  canvas,
  rules,
  translate,
  elementRegistry
) {
  config = config || {};

  contextPad.registerProvider(this);

  this._contextPad = contextPad;

  this._modeling = modeling;

  this._elementFactory = elementFactory;
  this._connect = connect;
  this._create = create;
  this._popupMenu = popupMenu;
  this._canvas = canvas;
  this._rules = rules;
  this._translate = translate;

  if (config.autoPlace !== false) {
    this._autoPlace = injector.get("autoPlace", false);
  }

  eventBus.on("create.end", 250, function(event) {
chenjiuping's avatar
chenjiuping committed
52 53
    const context = event.context,
      shape = context.shape
sunhongwei's avatar
sunhongwei committed
54 55 56 57 58

    if (!hasPrimaryModifier(event) || !contextPad.isOpen(shape)) {
      return;
    }

chenjiuping's avatar
chenjiuping committed
59
    const entries = contextPad.getEntries(shape)
sunhongwei's avatar
sunhongwei committed
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83

    if (entries.replace) {
      entries.replace.action.click(event, shape);
    }
  });
}

ContextPadProvider.$inject = [
  "config.contextPad",
  "injector",
  "eventBus",
  "contextPad",
  "modeling",
  "elementFactory",
  "connect",
  "create",
  "popupMenu",
  "canvas",
  "rules",
  "translate",
  "elementRegistry"
];

ContextPadProvider.prototype.getContextPadEntries = function(element) {
chenjiuping's avatar
chenjiuping committed
84
  const contextPad = this._contextPad,
sunhongwei's avatar
sunhongwei committed
85 86 87 88 89 90 91 92
    modeling = this._modeling,
    elementFactory = this._elementFactory,
    connect = this._connect,
    create = this._create,
    popupMenu = this._popupMenu,
    canvas = this._canvas,
    rules = this._rules,
    autoPlace = this._autoPlace,
chenjiuping's avatar
chenjiuping committed
93
    translate = this._translate
sunhongwei's avatar
sunhongwei committed
94

chenjiuping's avatar
chenjiuping committed
95
  const actions = {}
sunhongwei's avatar
sunhongwei committed
96 97 98 99 100

  if (element.type === "label") {
    return actions;
  }

chenjiuping's avatar
chenjiuping committed
101
  const businessObject = element.businessObject
sunhongwei's avatar
sunhongwei committed
102 103 104 105 106 107 108 109 110 111

  function startConnect(event, element) {
    connect.start(event, element);
  }

  function removeElement() {
    modeling.removeElements([element]);
  }

  function getReplaceMenuPosition(element) {
chenjiuping's avatar
chenjiuping committed
112
    const Y_OFFSET = 5
sunhongwei's avatar
sunhongwei committed
113

chenjiuping's avatar
chenjiuping committed
114 115
    const diagramContainer = canvas.getContainer(),
      pad = contextPad.getPad(element).html
sunhongwei's avatar
sunhongwei committed
116

chenjiuping's avatar
chenjiuping committed
117 118
    const diagramRect = diagramContainer.getBoundingClientRect(),
      padRect = pad.getBoundingClientRect()
sunhongwei's avatar
sunhongwei committed
119

chenjiuping's avatar
chenjiuping committed
120 121
    const top = padRect.top - diagramRect.top
    const left = padRect.left - diagramRect.left
sunhongwei's avatar
sunhongwei committed
122

chenjiuping's avatar
chenjiuping committed
123
    const pos = {
sunhongwei's avatar
sunhongwei committed
124 125
      x: left,
      y: top + padRect.height + Y_OFFSET
chenjiuping's avatar
chenjiuping committed
126
    }
sunhongwei's avatar
sunhongwei committed
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147

    return pos;
  }

  /**
   * Create an append action
   *
   * @param {string} type
   * @param {string} className
   * @param {string} [title]
   * @param {Object} [options]
   *
   * @return {Object} descriptor
   */
  function appendAction(type, className, title, options) {
    if (typeof title !== "string") {
      options = title;
      title = translate("Append {type}", { type: type.replace(/^bpmn:/, "") });
    }

    function appendStart(event, element) {
chenjiuping's avatar
chenjiuping committed
148
      const shape = elementFactory.createShape(assign({ type: type }, options))
sunhongwei's avatar
sunhongwei committed
149 150 151 152 153
      create.start(event, shape, {
        source: element
      });
    }

chenjiuping's avatar
chenjiuping committed
154
    const append = autoPlace
sunhongwei's avatar
sunhongwei committed
155
      ? function(event, element) {
chenjiuping's avatar
chenjiuping committed
156
        const shape = elementFactory.createShape(assign({ type: type }, options))
sunhongwei's avatar
sunhongwei committed
157

chenjiuping's avatar
chenjiuping committed
158 159 160
        autoPlace.append(element, shape)
      }
      : appendStart
sunhongwei's avatar
sunhongwei committed
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184

    return {
      group: "model",
      className: className,
      title: title,
      action: {
        dragstart: appendStart,
        click: append
      }
    };
  }

  function splitLaneHandler(count) {
    return function(event, element) {
      // actual split
      modeling.splitLane(element, count);

      // refresh context pad after split to
      // get rid of split icons
      contextPad.open(element, true);
    };
  }

  if (isAny(businessObject, ["bpmn:Lane", "bpmn:Participant"]) && isExpanded(businessObject)) {
chenjiuping's avatar
chenjiuping committed
185
    const childLanes = getChildLanes(element)
sunhongwei's avatar
sunhongwei committed
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304

    assign(actions, {
      "lane-insert-above": {
        group: "lane-insert-above",
        className: "bpmn-icon-lane-insert-above",
        title: translate("Add Lane above"),
        action: {
          click: function(event, element) {
            modeling.addLane(element, "top");
          }
        }
      }
    });

    if (childLanes.length < 2) {
      if (element.height >= 120) {
        assign(actions, {
          "lane-divide-two": {
            group: "lane-divide",
            className: "bpmn-icon-lane-divide-two",
            title: translate("Divide into two Lanes"),
            action: {
              click: splitLaneHandler(2)
            }
          }
        });
      }

      if (element.height >= 180) {
        assign(actions, {
          "lane-divide-three": {
            group: "lane-divide",
            className: "bpmn-icon-lane-divide-three",
            title: translate("Divide into three Lanes"),
            action: {
              click: splitLaneHandler(3)
            }
          }
        });
      }
    }

    assign(actions, {
      "lane-insert-below": {
        group: "lane-insert-below",
        className: "bpmn-icon-lane-insert-below",
        title: translate("Add Lane below"),
        action: {
          click: function(event, element) {
            modeling.addLane(element, "bottom");
          }
        }
      }
    });
  }

  if (is(businessObject, "bpmn:FlowNode")) {
    if (is(businessObject, "bpmn:EventBasedGateway")) {
      assign(actions, {
        "append.receive-task": appendAction("bpmn:ReceiveTask", "bpmn-icon-receive-task", translate("Append ReceiveTask")),
        "append.message-intermediate-event": appendAction(
          "bpmn:IntermediateCatchEvent",
          "bpmn-icon-intermediate-event-catch-message",
          translate("Append MessageIntermediateCatchEvent"),
          { eventDefinitionType: "bpmn:MessageEventDefinition" }
        ),
        "append.timer-intermediate-event": appendAction(
          "bpmn:IntermediateCatchEvent",
          "bpmn-icon-intermediate-event-catch-timer",
          translate("Append TimerIntermediateCatchEvent"),
          { eventDefinitionType: "bpmn:TimerEventDefinition" }
        ),
        "append.condition-intermediate-event": appendAction(
          "bpmn:IntermediateCatchEvent",
          "bpmn-icon-intermediate-event-catch-condition",
          translate("Append ConditionIntermediateCatchEvent"),
          { eventDefinitionType: "bpmn:ConditionalEventDefinition" }
        ),
        "append.signal-intermediate-event": appendAction(
          "bpmn:IntermediateCatchEvent",
          "bpmn-icon-intermediate-event-catch-signal",
          translate("Append SignalIntermediateCatchEvent"),
          { eventDefinitionType: "bpmn:SignalEventDefinition" }
        )
      });
    } else if (isEventType(businessObject, "bpmn:BoundaryEvent", "bpmn:CompensateEventDefinition")) {
      assign(actions, {
        "append.compensation-activity": appendAction("bpmn:Task", "bpmn-icon-task", translate("Append compensation activity"), {
          isForCompensation: true
        })
      });
    } else if (
      !is(businessObject, "bpmn:EndEvent") &&
      !businessObject.isForCompensation &&
      !isEventType(businessObject, "bpmn:IntermediateThrowEvent", "bpmn:LinkEventDefinition") &&
      !isEventSubProcess(businessObject)
    ) {
      assign(actions, {
        "append.end-event": appendAction("bpmn:EndEvent", "bpmn-icon-end-event-none", translate("Append EndEvent")),
        "append.gateway": appendAction("bpmn:ExclusiveGateway", "bpmn-icon-gateway-none", translate("Append Gateway")),
        "append.append-task": appendAction("bpmn:UserTask", "bpmn-icon-user-task", translate("Append Task")),
        "append.intermediate-event": appendAction(
          "bpmn:IntermediateThrowEvent",
          "bpmn-icon-intermediate-event-none",
          translate("Append Intermediate/Boundary Event")
        )
      });
    }
  }

  if (!popupMenu.isEmpty(element, "bpmn-replace")) {
    // Replace menu entry
    assign(actions, {
      replace: {
        group: "edit",
        className: "bpmn-icon-screw-wrench",
        title: translate("Change type"),
        action: {
          click: function(event, element) {
chenjiuping's avatar
chenjiuping committed
305
            const position = assign(getReplaceMenuPosition(element), {
sunhongwei's avatar
sunhongwei committed
306
              cursor: { x: event.x, y: event.y }
chenjiuping's avatar
chenjiuping committed
307
            })
sunhongwei's avatar
sunhongwei committed
308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352

            popupMenu.open(element, "bpmn-replace", position);
          }
        }
      }
    });
  }

  if (isAny(businessObject, ["bpmn:FlowNode", "bpmn:InteractionNode", "bpmn:DataObjectReference", "bpmn:DataStoreReference"])) {
    assign(actions, {
      "append.text-annotation": appendAction("bpmn:TextAnnotation", "bpmn-icon-text-annotation"),

      connect: {
        group: "connect",
        className: "bpmn-icon-connection-multi",
        title: translate("Connect using " + (businessObject.isForCompensation ? "" : "Sequence/MessageFlow or ") + "Association"),
        action: {
          click: startConnect,
          dragstart: startConnect
        }
      }
    });
  }

  if (isAny(businessObject, ["bpmn:DataObjectReference", "bpmn:DataStoreReference"])) {
    assign(actions, {
      connect: {
        group: "connect",
        className: "bpmn-icon-connection-multi",
        title: translate("Connect using DataInputAssociation"),
        action: {
          click: startConnect,
          dragstart: startConnect
        }
      }
    });
  }

  if (is(businessObject, "bpmn:Group")) {
    assign(actions, {
      "append.text-annotation": appendAction("bpmn:TextAnnotation", "bpmn-icon-text-annotation")
    });
  }

  // delete element entry, only show if allowed by rules
chenjiuping's avatar
chenjiuping committed
353
  let deleteAllowed = rules.allowed('elements.delete', { elements: [element] })
sunhongwei's avatar
sunhongwei committed
354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378

  if (isArray(deleteAllowed)) {
    // was the element returned as a deletion candidate?
    deleteAllowed = deleteAllowed[0] === element;
  }

  if (deleteAllowed) {
    assign(actions, {
      delete: {
        group: "edit",
        className: "bpmn-icon-trash",
        title: translate("Remove"),
        action: {
          click: removeElement
        }
      }
    });
  }

  return actions;
};

// helpers /////////

function isEventType(eventBo, type, definition) {
chenjiuping's avatar
chenjiuping committed
379 380
  const isType = eventBo.$instanceOf(type)
  let isDefinition = false
sunhongwei's avatar
sunhongwei committed
381

chenjiuping's avatar
chenjiuping committed
382
  const definitions = eventBo.eventDefinitions || []
sunhongwei's avatar
sunhongwei committed
383 384 385 386 387 388 389 390
  forEach(definitions, function(def) {
    if (def.$type === definition) {
      isDefinition = true;
    }
  });

  return isType && isDefinition;
}