Rules.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. /**
  2. * A service that provides rules for certain diagram actions.
  3. *
  4. * The default implementation will hook into the {@link CommandStack}
  5. * to perform the actual rule evaluation. Make sure to provide the
  6. * `commandStack` service with this module if you plan to use it.
  7. *
  8. * Together with this implementation you may use the {@link RuleProvider}
  9. * to implement your own rule checkers.
  10. *
  11. * This module is ment to be easily replaced, thus the tiny foot print.
  12. *
  13. * @param {Injector} injector
  14. */
  15. export default function Rules(injector) {
  16. this._commandStack = injector.get('commandStack', false);
  17. }
  18. Rules.$inject = [ 'injector' ];
  19. /**
  20. * Returns whether or not a given modeling action can be executed
  21. * in the specified context.
  22. *
  23. * This implementation will respond with allow unless anyone
  24. * objects.
  25. *
  26. * @param {String} action the action to be checked
  27. * @param {Object} [context] the context to check the action in
  28. *
  29. * @return {Boolean} returns true, false or null depending on whether the
  30. * operation is allowed, not allowed or should be ignored.
  31. */
  32. Rules.prototype.allowed = function(action, context) {
  33. var allowed = true;
  34. var commandStack = this._commandStack;
  35. if (commandStack) {
  36. allowed = commandStack.canExecute(action, context);
  37. }
  38. // map undefined to true, i.e. no rules
  39. return allowed === undefined ? true : allowed;
  40. };