find.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /**
  2. * Explore plugin.
  3. */
  4. Draw.loadPlugin(function(ui)
  5. {
  6. var div = document.createElement('div');
  7. div.style.userSelect = 'none';
  8. div.style.overflow = 'hidden';
  9. div.style.padding = '10px';
  10. div.style.height = '100%';
  11. var graph = ui.editor.graph;
  12. var lastFound = null;
  13. mxUtils.write(div, 'Find:');
  14. mxUtils.br(div);
  15. var searchInput = document.createElement('input');
  16. searchInput.setAttribute('type', 'text');
  17. searchInput.style.marginTop = '4px';
  18. searchInput.style.width = '170px';
  19. div.appendChild(searchInput);
  20. var tmp = document.createElement('div');
  21. function search(next)
  22. {
  23. var cells = graph.model.getDescendants(graph.model.getRoot());
  24. var search = searchInput.value.toLowerCase();
  25. var active = !next || lastFound == null;
  26. var firstMatch = null;
  27. if (search.length > 0)
  28. {
  29. for (var i = 0; i < cells.length; i++)
  30. {
  31. var state = graph.view.getState(cells[i]);
  32. if (state != null && (active || firstMatch == null) &&
  33. graph.model.isVertex(state.cell) || graph.model.isEdge(state.cell))
  34. {
  35. if (graph.isHtmlLabel(state.cell))
  36. {
  37. tmp.innerHTML = graph.getLabel(state.cell);
  38. label = mxUtils.extractTextWithWhitespace([tmp]);
  39. }
  40. else
  41. {
  42. label = graph.getLabel(state.cell);
  43. }
  44. label = mxUtils.trim(label.replace(/[\x00-\x1F\x7F-\x9F]|\s+/g, ' ')).toLowerCase();
  45. if (label.substring(0, search.length) == search)
  46. {
  47. if (active)
  48. {
  49. firstMatch = state;
  50. break;
  51. }
  52. else if (firstMatch == null)
  53. {
  54. firstMatch = state;
  55. }
  56. }
  57. }
  58. active = active || state == lastFound;
  59. }
  60. }
  61. if (firstMatch != null)
  62. {
  63. lastFound = firstMatch;
  64. graph.setSelectionCell(lastFound.cell);
  65. graph.scrollCellToVisible(lastFound.cell);
  66. }
  67. else
  68. {
  69. graph.clearSelection();
  70. }
  71. return search.length == 0 || firstMatch != null;
  72. };
  73. mxUtils.br(div);
  74. var resetBtn = mxUtils.button(mxResources.get('reset'), function()
  75. {
  76. searchInput.value = '';
  77. searchInput.style.backgroundColor = '';
  78. lastFound = null;
  79. searchInput.focus();
  80. });
  81. resetBtn.style.marginTop = '8px';
  82. resetBtn.style.marginRight = '4px';
  83. resetBtn.style.padding = '4px';
  84. div.appendChild(resetBtn);
  85. var btn = mxUtils.button('Find Again', function()
  86. {
  87. searchInput.style.backgroundColor = search(true) ? '' : '#ffcfcf';
  88. });
  89. btn.style.marginTop = '8px';
  90. btn.style.padding = '4px';
  91. div.appendChild(btn);
  92. mxEvent.addListener(searchInput, 'keyup', function(evt)
  93. {
  94. searchInput.style.backgroundColor = search(evt.keyCode == 13) ? '' : '#ffcfcf';
  95. });
  96. var wnd = new mxWindow('Find', div, document.body.offsetWidth - 300, 140, 200, 120, true, true);
  97. wnd.destroyOnClose = false;
  98. wnd.setMaximizable(false);
  99. wnd.setResizable(false);
  100. wnd.setClosable(true);
  101. // Extends Extras menu
  102. mxResources.parse('find=Find');
  103. // Adds action
  104. ui.actions.addAction('find...', function()
  105. {
  106. wnd.setVisible(!wnd.isVisible());
  107. if (wnd.isVisible())
  108. {
  109. searchInput.focus();
  110. if (mxClient.IS_FF || document.documentMode >= 5 || mxClient.IS_QUIRKS)
  111. {
  112. searchInput.select();
  113. }
  114. else
  115. {
  116. document.execCommand('selectAll', false, null);
  117. }
  118. }
  119. else
  120. {
  121. graph.container.focus();
  122. }
  123. }, null, null, 'Ctrl+Space');
  124. var menu = ui.menus.get('edit');
  125. var oldFunct = menu.funct;
  126. menu.funct = function(menu, parent)
  127. {
  128. oldFunct.apply(this, arguments);
  129. ui.menus.addMenuItems(menu, ['-', 'find'], parent);
  130. };
  131. var findAction = ui.actions.get('find');
  132. var keyHandlerKeyDown = ui.keyHandler.keyDown;
  133. ui.keyHandler.keyDown = function(evt)
  134. {
  135. if (evt.keyCode == 32 && mxEvent.isControlDown(evt))
  136. {
  137. findAction.funct();
  138. mxEvent.consume(evt);
  139. }
  140. else
  141. {
  142. return keyHandlerKeyDown.apply(this, arguments);
  143. }
  144. };
  145. });