note.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /**
  2. * @fileOverview
  3. *
  4. * 支持节点详细信息(HTML)格式
  5. *
  6. * @author: techird
  7. * @copyright: Baidu FEX, 2014
  8. */
  9. define(function(require, exports, module) {
  10. var kity = require('../core/kity');
  11. var utils = require('../core/utils');
  12. var Minder = require('../core/minder');
  13. var MinderNode = require('../core/node');
  14. var Command = require('../core/command');
  15. var Module = require('../core/module');
  16. var Renderer = require('../core/render');
  17. Module.register('NoteModule', function() {
  18. var NOTE_PATH = 'M9,9H3V8h6L9,9L9,9z M9,7H3V6h6V7z M9,5H3V4h6V5z M8.5,11H2V2h8v7.5 M9,12l2-2V1H1v11';
  19. /**
  20. * @command Note
  21. * @description 设置节点的备注信息
  22. * @param {string} note 要设置的备注信息,设置为 null 则移除备注信息
  23. * @state
  24. * 0: 当前有选中的节点
  25. * -1: 当前没有选中的节点
  26. */
  27. var NoteCommand = kity.createClass('NoteCommand', {
  28. base: Command,
  29. execute: function(minder, note) {
  30. var node = minder.getSelectedNode();
  31. node.setData('note', note);
  32. node.render();
  33. node.getMinder().layout(300);
  34. },
  35. queryState: function(minder) {
  36. return minder.getSelectedNodes().length === 1 ? 0 : -1;
  37. },
  38. queryValue: function(minder) {
  39. var node = minder.getSelectedNode();
  40. return node && node.getData('note');
  41. }
  42. });
  43. var NoteIcon = kity.createClass('NoteIcon', {
  44. base: kity.Group,
  45. constructor: function() {
  46. this.callBase();
  47. this.width = 16;
  48. this.height = 17;
  49. this.rect = new kity.Rect(16, 17, 0.5, -8.5, 2).fill('transparent');
  50. this.path = new kity.Path().setPathData(NOTE_PATH).setTranslate(2.5, -6.5);
  51. this.addShapes([this.rect, this.path]);
  52. this.on('mouseover', function() {
  53. this.rect.fill('rgba(255, 255, 200, .8)');
  54. }).on('mouseout', function() {
  55. this.rect.fill('transparent');
  56. });
  57. this.setStyle('cursor', 'pointer');
  58. }
  59. });
  60. var NoteIconRenderer = kity.createClass('NoteIconRenderer', {
  61. base: Renderer,
  62. create: function(node) {
  63. var icon = new NoteIcon();
  64. icon.on('mousedown', function(e) {
  65. e.preventDefault();
  66. node.getMinder().fire('editnoterequest');
  67. });
  68. icon.on('mouseover', function() {
  69. node.getMinder().fire('shownoterequest', {node: node, icon: icon});
  70. });
  71. icon.on('mouseout', function() {
  72. node.getMinder().fire('hidenoterequest', {node: node, icon: icon});
  73. });
  74. return icon;
  75. },
  76. shouldRender: function(node) {
  77. return node.getData('note');
  78. },
  79. update: function(icon, node, box) {
  80. var x = box.right + node.getStyle('space-left');
  81. var y = box.cy;
  82. icon.path.fill(node.getStyle('color'));
  83. icon.setTranslate(x, y);
  84. return new kity.Box(x, Math.round(y - icon.height / 2), icon.width, icon.height);
  85. }
  86. });
  87. return {
  88. renderers: {
  89. right: NoteIconRenderer
  90. },
  91. commands: {
  92. 'note': NoteCommand
  93. }
  94. };
  95. });
  96. });