props.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /**
  2. * Sample plugin.
  3. */
  4. Draw.loadPlugin(function(ui) {
  5. var div = document.createElement('div');
  6. div.style.background = Editor.isDarkMode() ? Editor.darkColor : '#ffffff';
  7. div.style.border = '1px solid gray';
  8. div.style.opacity = '0.8';
  9. div.style.padding = '10px';
  10. div.style.paddingTop = '0px';
  11. div.style.width = '20%';
  12. div.innerHTML = '<p><i>' + mxResources.get('nothingIsSelected') + '</i></p>';
  13. var graph = ui.editor.graph;
  14. if (!ui.editor.isChromelessView())
  15. {
  16. div.style.boxSizing = 'border-box';
  17. div.style.minHeight = '100%';
  18. div.style.width = '100%';
  19. var iiw = window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth;
  20. var dataWindow = new mxWindow('Data', div, iiw - 320, 60, 240, 220, true, true);
  21. dataWindow.destroyOnClose = false;
  22. dataWindow.setMaximizable(false);
  23. dataWindow.setResizable(true);
  24. dataWindow.setScrollable(true);
  25. dataWindow.setClosable(true);
  26. dataWindow.contentWrapper.style.overflowY = 'scroll';
  27. // Adds resource for action
  28. mxResources.parse('extractData=Extract Data');
  29. // Adds action
  30. ui.actions.addAction('extractData...', function()
  31. {
  32. dataWindow.setVisible(!dataWindow.isVisible());
  33. });
  34. var menu = ui.menus.get('extras');
  35. var oldFunct = menu.funct;
  36. menu.funct = function(menu, parent)
  37. {
  38. oldFunct.apply(this, arguments);
  39. ui.menus.addMenuItems(menu, ['-', 'extractData'], parent);
  40. };
  41. }
  42. else
  43. {
  44. div.style.position = 'absolute';
  45. div.style.minWidth = '200px';
  46. div.style.top = '40px';
  47. div.style.right = '20px';
  48. document.body.appendChild(div);
  49. }
  50. // Highlights current cell
  51. var highlight = new mxCellHighlight(graph, '#00ff00', 8);
  52. var ignored = ['label', 'tooltip', 'placeholders'];
  53. function extractData(evt)
  54. {
  55. var result = graph.getDataForCells(graph.getSelectionCells());
  56. if (mxEvent.isShiftDown(evt))
  57. {
  58. console.log(JSON.stringify(result, null, ' '));
  59. }
  60. else
  61. {
  62. console.log(result);
  63. }
  64. };
  65. /**
  66. * Updates the properties panel
  67. */
  68. function cellClicked(cell)
  69. {
  70. // Gets the selection cell
  71. if (cell == null)
  72. {
  73. highlight.highlight(null);
  74. div.innerHTML = '<p><i>' + mxResources.get('nothingIsSelected') + '</i></p>';
  75. }
  76. else
  77. {
  78. var attrs = (cell.value != null) ? cell.value.attributes : null;
  79. if (ui.editor.isChromelessView())
  80. {
  81. highlight.highlight(graph.view.getState(cell));
  82. }
  83. if (attrs != null)
  84. {
  85. var label = Graph.sanitizeHtml(graph.getLabel(cell));
  86. if (label != null && label.length > 0)
  87. {
  88. div.innerHTML = '<h1>' + label + '</h1>';
  89. }
  90. else
  91. {
  92. div.innerText = '';
  93. }
  94. for (var i = 0; i < attrs.length; i++)
  95. {
  96. if (mxUtils.indexOf(ignored, attrs[i].nodeName) < 0 &&
  97. attrs[i].nodeValue.length > 0)
  98. {
  99. // TODO: Add click handler on h2 to output data
  100. var h2 = document.createElement('h2');
  101. mxUtils.write(h2, attrs[i].nodeName);
  102. div.appendChild(h2);
  103. var p = document.createElement('p');
  104. mxUtils.write(p, attrs[i].nodeValue);
  105. div.appendChild(p);
  106. }
  107. }
  108. }
  109. else
  110. {
  111. var label = graph.convertValueToString(cell);
  112. if (label != '')
  113. {
  114. div.innerHTML = '<h1>' + Graph.sanitizeHtml(label) + '</h1>';
  115. }
  116. else
  117. {
  118. div.innerHTML = '<p><i>No data</i></p>';
  119. }
  120. }
  121. if (!ui.editor.isChromelessView())
  122. {
  123. var button = document.createElement('button');
  124. button.setAttribute('title', 'Click or Shift+Click to write data for all selected cells to the browser console');
  125. button.style['float'] = 'none';
  126. mxUtils.write(button, 'Write to Console');
  127. mxEvent.addListener(button, 'click', function(evt)
  128. {
  129. extractData(evt);
  130. });
  131. div.appendChild(button);
  132. }
  133. }
  134. };
  135. if (!ui.editor.isChromelessView())
  136. {
  137. graph.selectionModel.addListener(mxEvent.CHANGE, function(sender, evt)
  138. {
  139. cellClicked(graph.getSelectionCell());
  140. });
  141. graph.model.addListener(mxEvent.CHANGE, function(sender, evt)
  142. {
  143. cellClicked(graph.getSelectionCell());
  144. });
  145. }
  146. else
  147. {
  148. graph.click = function(me)
  149. {
  150. // Async required to enable hyperlinks in labels
  151. window.setTimeout(function()
  152. {
  153. cellClicked(me.getCell());
  154. }, 0);
  155. };
  156. }
  157. });