feat(replace): let rules disallow replacement operations

This commit is contained in:
Adam Brengesjö 2015-12-02 16:47:20 +01:00 committed by Nico Rehwaldt
parent 9ac0a9a957
commit 93d4bade22
5 changed files with 98 additions and 6 deletions

View File

@ -43,7 +43,7 @@ var CUSTOM_PROPERTIES = [
* @param {PopupMenu} popupMenu * @param {PopupMenu} popupMenu
* @param {Replace} replace * @param {Replace} replace
*/ */
function BpmnReplace(bpmnFactory, moddle, popupMenu, replace, selection, modeling, eventBus) { function BpmnReplace(bpmnFactory, moddle, popupMenu, replace, selection, modeling, eventBus, rules) {
var self = this, var self = this,
currentElement; currentElement;
@ -224,6 +224,10 @@ function BpmnReplace(bpmnFactory, moddle, popupMenu, replace, selection, modelin
this.getReplaceOptions = function(element) { this.getReplaceOptions = function(element) {
if (!rules.allowed('shape.replace', { element: element })) {
return [];
}
var menuEntries = []; var menuEntries = [];
var businessObject = element.businessObject; var businessObject = element.businessObject;
@ -383,12 +387,10 @@ function BpmnReplace(bpmnFactory, moddle, popupMenu, replace, selection, modelin
// Add entries to replace menu // Add entries to replace menu
forEach(filteredEntries, function(definition) { forEach(filteredEntries, function(definition) {
var entry = addMenuEntry(definition); var entry = addMenuEntry(definition);
menuEntries.push(entry); menuEntries.push(entry);
}); });
} }
function addMenuEntry(definition, action) { function addMenuEntry(definition, action) {
var menuEntry = { var menuEntry = {
@ -446,6 +448,6 @@ function BpmnReplace(bpmnFactory, moddle, popupMenu, replace, selection, modelin
this.replaceElement = replaceElement; this.replaceElement = replaceElement;
} }
BpmnReplace.$inject = [ 'bpmnFactory', 'moddle', 'popupMenu', 'replace', 'selection', 'modeling', 'eventBus' ]; BpmnReplace.$inject = [ 'bpmnFactory', 'moddle', 'popupMenu', 'replace', 'selection', 'modeling', 'eventBus', 'rules' ];
module.exports = BpmnReplace; module.exports = BpmnReplace;

View File

@ -14,7 +14,7 @@ var contextPadModule = require('../../../../lib/features/context-pad'),
modelingModule = require('../../../../lib/features/modeling'), modelingModule = require('../../../../lib/features/modeling'),
popupModule = require('diagram-js/lib/features/popup-menu'), popupModule = require('diagram-js/lib/features/popup-menu'),
replaceModule = require('diagram-js/lib/features/replace'), replaceModule = require('diagram-js/lib/features/replace'),
rulesModule = require('./rules'); rulesModule = require('../../../util/custom-rules');
describe('features - context-pad', function() { describe('features - context-pad', function() {

View File

@ -7,7 +7,8 @@ var TestHelper = require('../../../TestHelper');
var modelingModule = require('../../../../lib/features/modeling'), var modelingModule = require('../../../../lib/features/modeling'),
replaceModule = require('../../../../lib/features/replace'), replaceModule = require('../../../../lib/features/replace'),
moveModule = require('diagram-js/lib/features/move'), moveModule = require('diagram-js/lib/features/move'),
coreModule = require('../../../../lib/core'); coreModule = require('../../../../lib/core'),
customRulesModule = require('../../../util/custom-rules');
var is = require('../../../../lib/util/ModelUtil').is, var is = require('../../../../lib/util/ModelUtil').is,
isExpanded = require('../../../../lib/util/DiUtil').isExpanded, isExpanded = require('../../../../lib/util/DiUtil').isExpanded,
@ -1416,4 +1417,89 @@ describe('features/replace', function() {
})); }));
}); });
describe('getReplaceOptions', function () {
var diagramXML = require('../../../fixtures/bpmn/basic.bpmn');
beforeEach(bootstrapModeler(diagramXML, { modules: testModules.concat([ customRulesModule ]) }));
it('should get replacement options by default', inject(function(elementRegistry, bpmnReplace) {
// given
var startEvent = elementRegistry.get('StartEvent_1');
// when
var options = bpmnReplace.getReplaceOptions(startEvent);
// then
expect(options).to.have.length.above(0);
}));
it('should get replacement options when custom rule returns true', inject(function(elementRegistry, bpmnReplace, customRules) {
// given
var startEvent = elementRegistry.get('StartEvent_1');
customRules.addRule('shape.replace', function () {
return true;
});
// when
var options = bpmnReplace.getReplaceOptions(startEvent);
// then
expect(options).to.have.length.above(0);
}));
it('should get no replacement options when custom rule returns false', inject(function(elementRegistry, bpmnReplace, customRules) {
// given
var startEvent = elementRegistry.get('StartEvent_1');
customRules.addRule('shape.replace', function () {
return false;
});
// when
var options = bpmnReplace.getReplaceOptions(startEvent);
// then
expect(options).to.have.length(0);
}));
it('should provide element to custom rules', inject(function(elementRegistry, bpmnReplace, customRules) {
// given
var startEvent = elementRegistry.get('StartEvent_1');
var actual;
customRules.addRule('shape.replace', function (context) {
actual = context.element;
});
// when
bpmnReplace.getReplaceOptions(startEvent);
// then
expect(actual).to.equal(startEvent);
}));
it('should evaluate rule once', inject(function(elementRegistry, bpmnReplace, customRules) {
// given
var callCount = 0;
var startEvent = elementRegistry.get('StartEvent_1');
customRules.addRule('shape.replace', function () {
callCount++;
});
// when
bpmnReplace.getReplaceOptions(startEvent);
// then
expect(callCount).to.equal(1);
}));
});
}); });

View File

@ -12,4 +12,8 @@ CustomRules.$inject = [ 'eventBus' ];
inherits(CustomRules, RuleProvider); inherits(CustomRules, RuleProvider);
CustomRules.prototype.init = function () {
// placeholder
};
module.exports = CustomRules; module.exports = CustomRules;