import.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /**
  2. * Plugin for Freemind import.
  3. * See https://github.com/jiangxin/freemind-mmx/tree/master/freemind
  4. */
  5. Draw.loadPlugin(function(ui) {
  6. var graph = ui.editor.graph;
  7. // Adds resource for action
  8. mxResources.parse('importFreemind=Freemind');
  9. // Parses Freemind data
  10. function importFreemindData(data)
  11. {
  12. // Gets the default parent for inserting new cells. This
  13. // is normally the first child of the root (ie. layer 0).
  14. var parent = graph.getDefaultParent();
  15. var cells = [];
  16. // Makes the import one undoable edit
  17. graph.getModel().beginUpdate();
  18. try
  19. {
  20. // Gets point for free space in the graph for insert
  21. var pt = graph.getFreeInsertPoint();
  22. //
  23. // TODO: Import freemind data at pt.x/pt.y like so...
  24. //
  25. cells.push(graph.insertVertex(parent, null, data, pt.x, pt.y, 80, 30));
  26. // Applies current styles to new cells (might not be needed)
  27. graph.fireEvent(new mxEventObject('cellsInserted', 'cells', cells));
  28. }
  29. finally
  30. {
  31. graph.getModel().endUpdate();
  32. }
  33. // Selects new cells and scrolls into view
  34. graph.setSelectionCells(cells);
  35. graph.scrollCellToVisible(graph.getSelectionCell());
  36. };
  37. // Adds action
  38. ui.actions.addAction('importFreemind...', function()
  39. {
  40. // Only modern browsers for now. We'll move the import
  41. // code above to the main codebase later
  42. if (Graph.fileSupport && !mxClient.IS_IE && !mxClient.IS_IE11)
  43. {
  44. var input = document.createElement('input');
  45. input.setAttribute('type', 'file');
  46. mxEvent.addListener(input, 'change', function()
  47. {
  48. if (input.files != null)
  49. {
  50. // Only one file for now...
  51. var reader = new FileReader();
  52. reader.onload = function(e)
  53. {
  54. importFreemindData(e.target.result);
  55. };
  56. reader.readAsText(input.files[0]);
  57. }
  58. });
  59. input.click();
  60. }
  61. });
  62. // Adds menu
  63. ui.menubar.addMenu('Import', function(menu, parent)
  64. {
  65. ui.menus.addMenuItem(menu, 'importFreemind');
  66. });
  67. // Moves import menu to before help menu
  68. ui.menubar.container.insertBefore(ui.menubar.container.lastChild,
  69. ui.menubar.container.lastChild.previousSibling.previousSibling.previousSibling);
  70. });