number.js 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /**
  2. * Sample plugin.
  3. */
  4. Draw.loadPlugin(function(ui) {
  5. var graph = ui.editor.graph;
  6. var enabled = true;
  7. var counter = 0;
  8. // Creates the shape for the shape number and puts it into the draw pane
  9. var redrawShape = graph.cellRenderer.redrawShape;
  10. graph.cellRenderer.redrawShape = function(state, force, rendering)
  11. {
  12. var result = redrawShape.apply(this, arguments);
  13. if (result && enabled && graph.model.isVertex(state.cell))
  14. {
  15. if (state.shape != null && state.secondLabel == null)
  16. {
  17. var value = '<div style="padding:2px;border:1px solid gray;background:yellow;border-radius:2px;">' + (++counter) + '</div>';
  18. state.secondLabel = new mxText(value, new mxRectangle(),
  19. mxConstants.ALIGN_LEFT, mxConstants.ALIGN_BOTTOM);
  20. // Styles the label
  21. state.secondLabel.size = 12;
  22. state.secondLabel.dialect = state.shape.dialect;
  23. state.secondLabel.dialect = mxConstants.DIALECT_STRICTHTML;
  24. graph.cellRenderer.initializeLabel(state, state.secondLabel);
  25. }
  26. }
  27. if (state.secondLabel != null)
  28. {
  29. var scale = graph.getView().getScale();
  30. var bounds = new mxRectangle(state.x + state.width - 4 * scale, state.y + 4 * scale, 0, 0);
  31. state.secondLabel.state = state;
  32. state.secondLabel.scale = scale;
  33. state.secondLabel.bounds = bounds;
  34. state.secondLabel.redraw();
  35. }
  36. return result;
  37. };
  38. // Destroys the shape number
  39. var destroy = graph.cellRenderer.destroy;
  40. graph.cellRenderer.destroy = function(state)
  41. {
  42. destroy.apply(this, arguments);
  43. if (state.secondLabel != null)
  44. {
  45. state.secondLabel.destroy();
  46. state.secondLabel = null;
  47. }
  48. };
  49. graph.cellRenderer.getShapesForState = function(state)
  50. {
  51. return [state.shape, state.text, state.secondLabel, state.control];
  52. };
  53. var validate = graph.view.validate;
  54. graph.view.validate = function()
  55. {
  56. counter = 0;
  57. validate.apply(this, arguments);
  58. };
  59. // Extends View menu
  60. mxResources.parse('number=Number');
  61. // Adds action
  62. var action = ui.actions.addAction('number...', function()
  63. {
  64. enabled = !enabled;
  65. graph.refresh();
  66. });
  67. action.setToggleAction(true);
  68. action.setSelectedCallback(function() { return enabled; });
  69. var menu = ui.menus.get('view');
  70. var oldFunct = menu.funct;
  71. menu.funct = function(menu, parent)
  72. {
  73. oldFunct.apply(this, arguments);
  74. ui.menus.addMenuItems(menu, ['-', 'number'], parent);
  75. };
  76. // Forces refresh if file was loaded before plugin
  77. if (ui.getCurrentFile() != null)
  78. {
  79. graph.refresh();
  80. }
  81. });