all files / bpmn-js/lib/features/modeling/behavior/ GroupBehavior.js

98.39% Statements 61/62
92.31% Branches 24/26
100% Functions 13/13
98.39% Lines 61/62
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 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 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                                                              1189×               22× 22×   22×                   10× 10×   10×                                                       14× 105×                       14×   66×   66×               1189×   388× 388×   388×   14× 14× 14×   14×               1189×   82× 82×   82×                 1189× 471× 471× 471×   471×               1189×   85× 85×   85×             1189× 6265×     6265×                              
import inherits from 'inherits';
 
import CommandInterceptor from 'diagram-js/lib/command/CommandInterceptor';
 
import {
  add as collectionAdd,
  remove as collectionRemove
} from 'diagram-js/lib/util/Collections';
 
import {
  getBusinessObject,
  is
} from '../../../util/ModelUtil';
 
import {
  createCategoryValue
} from './util/CategoryUtil';
 
var HIGH_PRIORITY = 2000;
 
 
/**
 * BPMN specific Group behavior
 */
export default function GroupBehavior(
    bpmnFactory,
    canvas,
    elementRegistry,
    eventBus,
    injector,
    moddleCopy
) {
  injector.invoke(CommandInterceptor, this);
 
  /**
   * Gets process definitions
   *
   * @return {ModdleElement} definitions
   */
  function getDefinitions() {
    var rootElement = canvas.getRootElement(),
        businessObject = getBusinessObject(rootElement);
 
    return businessObject.$parent;
  }
 
  /**
   * Removes a referenced category value for a given group shape
   *
   * @param {djs.model.Shape} shape
   */
  function removeReferencedCategoryValue(shape) {
 
    var businessObject = getBusinessObject(shape),
        categoryValue = businessObject.categoryValueRef;
 
    if (!categoryValue) {
      return;
    }
 
    var category = categoryValue.$parent;
 
    Iif (!categoryValue) {
      return;
    }
 
    collectionRemove(category.categoryValue, categoryValue);
 
    // cleanup category if it is empty
    if (category && !category.categoryValue.length) {
      removeCategory(category);
    }
  }
 
  /**
   * Removes a given category from the definitions
   *
   * @param {ModdleElement} category
   */
  function removeCategory(category) {
 
    var definitions = getDefinitions();
 
    collectionRemove(definitions.get('rootElements'), category);
  }
 
  /**
   * Returns all group element in the current registry
   *
   * @return {Array<djs.model.shape>} a list of group shapes
   */
  function getGroupElements() {
    return elementRegistry.filter(function(e) {
      return is(e, 'bpmn:Group');
    });
  }
 
  /**
   * Returns true if given categoryValue is referenced in one of the given elements
   *
   * @param {Array<djs.model.shape>} elements
   * @param {ModdleElement} categoryValue
   * @return {Boolean}
   */
  function isReferenced(elements, categoryValue) {
    return elements.some(function(e) {
 
      var businessObject = getBusinessObject(e);
 
      return businessObject.categoryValueRef
        && businessObject.categoryValueRef === categoryValue;
    });
  }
 
  /**
   * remove referenced category + value when group was deleted
   */
  this.executed('shape.delete', function(event) {
 
    var context = event.context,
        shape = context.shape;
 
    if (is(shape, 'bpmn:Group')) {
 
      var businessObject = getBusinessObject(shape),
          categoryValueRef = businessObject.categoryValueRef,
          groupElements = getGroupElements();
 
      if (!isReferenced(groupElements, categoryValueRef)) {
        removeReferencedCategoryValue(shape);
      }
    }
  });
 
  /**
   * re-attach removed category
   */
  this.reverted('shape.delete', function(event) {
 
    var context = event.context,
        shape = context.shape;
 
    if (is(shape, 'bpmn:Group')) {
 
      var businessObject = getBusinessObject(shape),
          categoryValueRef = businessObject.categoryValueRef,
          definitions = getDefinitions(),
          category = categoryValueRef ? categoryValueRef.$parent : null;
 
      collectionAdd(category.get('categoryValue'), categoryValueRef);
      collectionAdd(definitions.get('rootElements'), category);
    }
  });
 
  /**
   * create new category + value when group was created
   */
  this.execute('shape.create', function(event) {
    var context = event.context,
        shape = context.shape,
        businessObject = getBusinessObject(shape);
 
    if (is(businessObject, 'bpmn:Group') && !businessObject.categoryValueRef) {
 
      var definitions = getDefinitions(),
          categoryValue = createCategoryValue(definitions, bpmnFactory);
 
      // link the reference to the Group
      businessObject.categoryValueRef = categoryValue;
    }
  });
 
 
  this.revert('shape.create', function(event) {
 
    var context = event.context,
        shape = context.shape;
 
    if (is(shape, 'bpmn:Group')) {
      removeReferencedCategoryValue(shape);
 
      delete getBusinessObject(shape).categoryValueRef;
 
    }
  });
 
  // copy bpmn:CategoryValue when copying element
  eventBus.on('moddleCopy.canCopyProperty', HIGH_PRIORITY, function(context) {
    var property = context.property,
        categoryValue;
 
    if (is(property, 'bpmn:CategoryValue')) {
      categoryValue = createCategoryValue(getDefinitions(), bpmnFactory);
 
      // return copy of category
      return moddleCopy.copyElement(property, categoryValue);
    }
  });
 
}
 
GroupBehavior.$inject = [
  'bpmnFactory',
  'canvas',
  'elementRegistry',
  'eventBus',
  'injector',
  'moddleCopy'
];
 
inherits(GroupBehavior, CommandInterceptor);