CreateRules.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. import inherits from 'inherits';
  2. import RuleProvider from 'lib/features/rules/RuleProvider';
  3. export default function CreateRules(eventBus) {
  4. RuleProvider.call(this, eventBus);
  5. }
  6. CreateRules.$inject = [ 'eventBus' ];
  7. inherits(CreateRules, RuleProvider);
  8. CreateRules.prototype.init = function() {
  9. this.addRule('shape.attach', function(context) {
  10. var target = context.target;
  11. // can attach to host
  12. if (/host/.test(target.id)) {
  13. return 'attach';
  14. }
  15. return false;
  16. });
  17. this.addRule('connection.create', function(context) {
  18. var source = context.source,
  19. target = context.target,
  20. hints = context.hints;
  21. expect(source.parent).to.exist;
  22. expect(target.parent).not.to.exist;
  23. expect(hints).to.have.keys([
  24. 'targetParent',
  25. 'targetAttach'
  26. ]);
  27. // can connect from parent or child
  28. return /parent|child/.test(source.id);
  29. });
  30. this.addRule('shape.create', function(context) {
  31. var target = context.target;
  32. if (/ignore/.test(target.id)) {
  33. return null;
  34. }
  35. // can create on parent or root,
  36. // no connect, no attach
  37. if (/parent|root/.test(target.id)) {
  38. return true;
  39. }
  40. return false;
  41. });
  42. };