view-find.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*jshint esversion: 6 */
  2. class FindView {
  3. constructor(graphElement, graph) {
  4. this._graphElement = graphElement;
  5. this._graph = graph;
  6. this._contentElement = document.createElement('div');
  7. this._contentElement.setAttribute('class', 'find');
  8. this._searchElement = document.createElement('input');
  9. this._searchElement.setAttribute('id', 'search');
  10. this._searchElement.setAttribute('type', 'text');
  11. this._searchElement.setAttribute('placeholder', 'Search...');
  12. this._searchElement.setAttribute('style', 'width: 100%');
  13. this._searchElement.addEventListener('input', (e) => {
  14. this.update(e.target.value);
  15. this.raise('search-text-changed', e.target.value);
  16. });
  17. this._resultElement = document.createElement('ol');
  18. this._resultElement.addEventListener('click', (e) => {
  19. this.select(e);
  20. });
  21. this._contentElement.appendChild(this._searchElement);
  22. this._contentElement.appendChild(this._resultElement);
  23. }
  24. on(event, callback) {
  25. this._events = this._events || {};
  26. this._events[event] = this._events[event] || [];
  27. this._events[event].push(callback);
  28. }
  29. raise(event, data) {
  30. if (this._events && this._events[event]) {
  31. this._events[event].forEach((callback) => {
  32. callback(this, data);
  33. });
  34. }
  35. }
  36. select(e) {
  37. var selection = [];
  38. var id = e.target.id;
  39. var nodesElement = this._graphElement.getElementById('nodes');
  40. var nodeElement = nodesElement.firstChild;
  41. while (nodeElement) {
  42. if (nodeElement.id == id) {
  43. selection.push(nodeElement);
  44. }
  45. nodeElement = nodeElement.nextSibling;
  46. }
  47. var edgePathsElement = this._graphElement.getElementById('edge-paths');
  48. var edgePathElement = edgePathsElement.firstChild;
  49. while (edgePathElement) {
  50. if (edgePathElement.id == id) {
  51. selection.push(edgePathElement);
  52. }
  53. edgePathElement = edgePathElement.nextSibling;
  54. }
  55. if (selection.length > 0) {
  56. this.raise('select', selection);
  57. }
  58. }
  59. focus(searchText) {
  60. this._searchElement.focus();
  61. this._searchElement.value = '';
  62. this._searchElement.value = searchText;
  63. this.update(searchText);
  64. }
  65. update(searchText) {
  66. while (this._resultElement.lastChild) {
  67. this._resultElement.removeChild(this._resultElement.lastChild);
  68. }
  69. var text = searchText.toLowerCase();
  70. var nodeMatches = {};
  71. var edgeMatches = {};
  72. this._graph.nodes.forEach((node) => {
  73. node.inputs.forEach((input) => {
  74. input.connections.forEach((connection) => {
  75. if (connection.id && connection.id.toLowerCase().indexOf(text) != -1 && !edgeMatches[connection.id]) {
  76. var item = document.createElement('li');
  77. if (!connection.initializer) {
  78. item.innerText = '\u2192 ' + connection.id.split('@').shift();
  79. item.id = 'edge-' + connection.id;
  80. this._resultElement.appendChild(item);
  81. edgeMatches[connection.id] = true;
  82. }
  83. }
  84. });
  85. });
  86. var name = node.name;
  87. if (name && name.toLowerCase().indexOf(text) != -1 && !nodeMatches[name]) {
  88. var item = document.createElement('li');
  89. item.innerText = '\u25A2 ' + node.name;
  90. item.id = 'node-' + node.name;
  91. this._resultElement.appendChild(item);
  92. nodeMatches[node.name] = true;
  93. }
  94. });
  95. this._graph.nodes.forEach((node) => {
  96. node.outputs.forEach((output) => {
  97. output.connections.forEach((connection) => {
  98. if (connection.id && connection.id.toLowerCase().indexOf(text) != -1 && !edgeMatches[connection.id]) {
  99. var item = document.createElement('li');
  100. item.innerText = '\u2192 ' + connection.id.split('@').shift();
  101. item.id = 'edge-' + connection.id;
  102. this._resultElement.appendChild(item);
  103. edgeMatches[connection.id] = true;
  104. }
  105. });
  106. });
  107. });
  108. this._resultElement.style.display = this._resultElement.childNodes.length != 0 ? 'block' : 'none';
  109. }
  110. get content() {
  111. return this._contentElement;
  112. }
  113. }