-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(replace): add moddle extension for canCopyProperty
Related to zeebe-io/zeebe-modeler#248
- Loading branch information
Showing
4 changed files
with
380 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
'use strict'; | ||
|
||
var isFunction = require('min-dash').isFunction, | ||
isObject = require('min-dash').isObject, | ||
some = require('min-dash').some; | ||
|
||
var WILDCARD = '*'; | ||
|
||
|
||
function ZeebeModdleExtension(eventBus) { | ||
|
||
var self = this; | ||
|
||
eventBus.on('moddleCopy.canCopyProperty', function(context) { | ||
var property = context.property, | ||
parent = context.parent; | ||
|
||
return self.canCopyProperty(property, parent); | ||
}); | ||
} | ||
|
||
ZeebeModdleExtension.$inject = [ 'eventBus' ]; | ||
|
||
ZeebeModdleExtension.prototype.canCopyProperty = function(property, parent) { | ||
|
||
// check if property is allowed in parent | ||
if (isObject(property) && !isAllowedInParent(property, parent)) { | ||
|
||
return false; | ||
} | ||
}; | ||
|
||
module.exports = ZeebeModdleExtension; | ||
|
||
// helpers ////////// | ||
|
||
function is(element, type) { | ||
return element && isFunction(element.$instanceOf) && element.$instanceOf(type); | ||
} | ||
|
||
function getParent(element, type) { | ||
if (!type) { | ||
return element.$parent; | ||
} | ||
|
||
if (is(element, type)) { | ||
return element; | ||
} | ||
|
||
if (!element.$parent) { | ||
return; | ||
} | ||
|
||
return getParent(element.$parent, type); | ||
} | ||
|
||
function isAllowedInParent(property, parent) { | ||
|
||
// (1) find property descriptor | ||
var descriptor = property.$type && property.$model.getTypeDescriptor(property.$type); | ||
|
||
var allowedIn = descriptor && descriptor.meta && descriptor.meta.allowedIn; | ||
|
||
if (!allowedIn || isWildcard(allowedIn)) { | ||
return true; | ||
} | ||
|
||
// (2) check if property has parent of allowed type | ||
return some(allowedIn, function(type) { | ||
return getParent(parent, type); | ||
}); | ||
} | ||
|
||
function isWildcard(allowedIn) { | ||
return allowedIn.indexOf(WILDCARD) !== -1; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
'use strict'; | ||
|
||
module.exports = { | ||
__init__: [ 'zeebeModdleExtension' ], | ||
zeebeModdleExtension: [ 'type', require('./extension') ] | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.