BpmnSearchProvider.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. import {
  2. map,
  3. filter,
  4. sortBy
  5. } from 'min-dash';
  6. import {
  7. getLabel
  8. } from '../label-editing/LabelUtil';
  9. /**
  10. * Provides ability to search through BPMN elements
  11. */
  12. export default function BpmnSearchProvider(elementRegistry, searchPad, canvas) {
  13. this._elementRegistry = elementRegistry;
  14. this._canvas = canvas;
  15. searchPad.registerProvider(this);
  16. }
  17. BpmnSearchProvider.$inject = [
  18. 'elementRegistry',
  19. 'searchPad',
  20. 'canvas'
  21. ];
  22. /**
  23. * Finds all elements that match given pattern
  24. *
  25. * <Result> :
  26. * {
  27. * primaryTokens: <Array<Token>>,
  28. * secondaryTokens: <Array<Token>>,
  29. * element: <Element>
  30. * }
  31. *
  32. * <Token> :
  33. * {
  34. * normal|matched: <string>
  35. * }
  36. *
  37. * @param {string} pattern
  38. * @return {Array<Result>}
  39. */
  40. BpmnSearchProvider.prototype.find = function(pattern) {
  41. var rootElement = this._canvas.getRootElement();
  42. var elements = this._elementRegistry.filter(function(element) {
  43. if (element.labelTarget) {
  44. return false;
  45. }
  46. return true;
  47. });
  48. // do not include root element
  49. elements = filter(elements, function(element) {
  50. return element !== rootElement;
  51. });
  52. elements = map(elements, function(element) {
  53. return {
  54. primaryTokens: matchAndSplit(getLabel(element), pattern),
  55. secondaryTokens: matchAndSplit(element.id, pattern),
  56. element: element
  57. };
  58. });
  59. // exclude non-matched elements
  60. elements = filter(elements, function(element) {
  61. return hasMatched(element.primaryTokens) || hasMatched(element.secondaryTokens);
  62. });
  63. elements = sortBy(elements, function(element) {
  64. return getLabel(element.element) + element.element.id;
  65. });
  66. return elements;
  67. };
  68. function hasMatched(tokens) {
  69. var matched = filter(tokens, function(t) {
  70. return !!t.matched;
  71. });
  72. return matched.length > 0;
  73. }
  74. function matchAndSplit(text, pattern) {
  75. var tokens = [],
  76. originalText = text;
  77. if (!text) {
  78. return tokens;
  79. }
  80. text = text.toLowerCase();
  81. pattern = pattern.toLowerCase();
  82. var i = text.indexOf(pattern);
  83. if (i > -1) {
  84. if (i !== 0) {
  85. tokens.push({
  86. normal: originalText.substr(0, i)
  87. });
  88. }
  89. tokens.push({
  90. matched: originalText.substr(i, pattern.length)
  91. });
  92. if (pattern.length + i < text.length) {
  93. tokens.push({
  94. normal: originalText.substr(pattern.length + i, text.length)
  95. });
  96. }
  97. } else {
  98. tokens.push({
  99. normal: originalText
  100. });
  101. }
  102. return tokens;
  103. }