layout.js 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /**
  2. * @fileOverview
  3. *
  4. * 布局模块
  5. *
  6. * @author: techird
  7. * @copyright: Baidu FEX, 2014
  8. */
  9. define(function(require, exports, module) {
  10. var kity = require('../core/kity');
  11. var Command = require('../core/command');
  12. var Module = require('../core/module');
  13. /**
  14. * @command Layout
  15. * @description 设置选中节点的布局
  16. * 允许使用的布局可以使用 `kityminder.Minder.getLayoutList()` 查询
  17. * @param {string} name 布局的名称,设置为 null 则使用继承或默认的布局
  18. * @state
  19. * 0: 当前有选中的节点
  20. * -1: 当前没有选中的节点
  21. * @return 返回首个选中节点的布局名称
  22. */
  23. var LayoutCommand = kity.createClass('LayoutCommand', {
  24. base: Command,
  25. execute: function(minder, name) {
  26. var nodes = minder.getSelectedNodes();
  27. nodes.forEach(function(node) {
  28. node.layout(name);
  29. });
  30. },
  31. queryValue: function(minder) {
  32. var node = minder.getSelectedNode();
  33. if (node) {
  34. return node.getData('layout');
  35. }
  36. },
  37. queryState: function(minder) {
  38. return minder.getSelectedNode() ? 0 : -1;
  39. }
  40. });
  41. /**
  42. * @command ResetLayout
  43. * @description 重设选中节点的布局,如果当前没有选中的节点,重设整个脑图的布局
  44. * @state
  45. * 0: 始终可用
  46. * @return 返回首个选中节点的布局名称
  47. */
  48. var ResetLayoutCommand = kity.createClass('ResetLayoutCommand', {
  49. base: Command,
  50. execute: function(minder) {
  51. var nodes = minder.getSelectedNodes();
  52. if (!nodes.length) nodes = [minder.getRoot()];
  53. nodes.forEach(function(node) {
  54. node.traverse(function(child) {
  55. child.resetLayoutOffset();
  56. if (!child.isRoot()) {
  57. child.setData('layout', null);
  58. }
  59. });
  60. });
  61. minder.layout(300);
  62. },
  63. enableReadOnly: true
  64. });
  65. Module.register('LayoutModule', {
  66. commands: {
  67. 'layout': LayoutCommand,
  68. 'resetlayout': ResetLayoutCommand
  69. },
  70. contextmenu: [{
  71. command: 'resetlayout'
  72. }, {
  73. divider: true
  74. }],
  75. commandShortcutKeys: {
  76. 'resetlayout': 'Ctrl+Shift+L'
  77. }
  78. });
  79. });