search.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
  2. import Hooks from './../../pluginHooks';
  3. import { addClass, removeClass } from './../../helpers/dom/element';
  4. import { registerRenderer, getRenderer } from './../../renderers';
  5. /**
  6. * @private
  7. * @plugin Search
  8. */
  9. function Search(instance) {
  10. this.query = function (queryStr, callback, queryMethod) {
  11. var rowCount = instance.countRows();
  12. var colCount = instance.countCols();
  13. var queryResult = [];
  14. if (!callback) {
  15. callback = Search.global.getDefaultCallback();
  16. }
  17. if (!queryMethod) {
  18. queryMethod = Search.global.getDefaultQueryMethod();
  19. }
  20. for (var rowIndex = 0; rowIndex < rowCount; rowIndex++) {
  21. for (var colIndex = 0; colIndex < colCount; colIndex++) {
  22. var cellData = instance.getDataAtCell(rowIndex, colIndex);
  23. var cellProperties = instance.getCellMeta(rowIndex, colIndex);
  24. var cellCallback = cellProperties.search.callback || callback;
  25. var cellQueryMethod = cellProperties.search.queryMethod || queryMethod;
  26. var testResult = cellQueryMethod(queryStr, cellData);
  27. if (testResult) {
  28. var singleResult = {
  29. row: rowIndex,
  30. col: colIndex,
  31. data: cellData
  32. };
  33. queryResult.push(singleResult);
  34. }
  35. if (cellCallback) {
  36. cellCallback(instance, rowIndex, colIndex, cellData, testResult);
  37. }
  38. }
  39. }
  40. return queryResult;
  41. };
  42. };
  43. Search.DEFAULT_CALLBACK = function (instance, row, col, data, testResult) {
  44. instance.getCellMeta(row, col).isSearchResult = testResult;
  45. };
  46. Search.DEFAULT_QUERY_METHOD = function (query, value) {
  47. if (typeof query == 'undefined' || query == null || !query.toLowerCase || query.length === 0) {
  48. return false;
  49. }
  50. if (typeof value == 'undefined' || value == null) {
  51. return false;
  52. }
  53. return value.toString().toLowerCase().indexOf(query.toLowerCase()) != -1;
  54. };
  55. Search.DEFAULT_SEARCH_RESULT_CLASS = 'htSearchResult';
  56. Search.global = function () {
  57. var defaultCallback = Search.DEFAULT_CALLBACK;
  58. var defaultQueryMethod = Search.DEFAULT_QUERY_METHOD;
  59. var defaultSearchResultClass = Search.DEFAULT_SEARCH_RESULT_CLASS;
  60. return {
  61. getDefaultCallback: function getDefaultCallback() {
  62. return defaultCallback;
  63. },
  64. setDefaultCallback: function setDefaultCallback(newDefaultCallback) {
  65. defaultCallback = newDefaultCallback;
  66. },
  67. getDefaultQueryMethod: function getDefaultQueryMethod() {
  68. return defaultQueryMethod;
  69. },
  70. setDefaultQueryMethod: function setDefaultQueryMethod(newDefaultQueryMethod) {
  71. defaultQueryMethod = newDefaultQueryMethod;
  72. },
  73. getDefaultSearchResultClass: function getDefaultSearchResultClass() {
  74. return defaultSearchResultClass;
  75. },
  76. setDefaultSearchResultClass: function setDefaultSearchResultClass(newSearchResultClass) {
  77. defaultSearchResultClass = newSearchResultClass;
  78. }
  79. };
  80. }();
  81. function SearchCellDecorator(instance, TD, row, col, prop, value, cellProperties) {
  82. var searchResultClass = cellProperties.search !== null && _typeof(cellProperties.search) == 'object' && cellProperties.search.searchResultClass || Search.global.getDefaultSearchResultClass();
  83. if (cellProperties.isSearchResult) {
  84. addClass(TD, searchResultClass);
  85. } else {
  86. removeClass(TD, searchResultClass);
  87. }
  88. };
  89. var originalBaseRenderer = getRenderer('base');
  90. registerRenderer('base', function (instance, TD, row, col, prop, value, cellProperties) {
  91. originalBaseRenderer.apply(this, arguments);
  92. SearchCellDecorator.apply(this, arguments);
  93. });
  94. function init() {
  95. var instance = this;
  96. var pluginEnabled = !!instance.getSettings().search;
  97. if (pluginEnabled) {
  98. instance.search = new Search(instance);
  99. } else {
  100. delete instance.search;
  101. }
  102. }
  103. Hooks.getSingleton().add('afterInit', init);
  104. Hooks.getSingleton().add('afterUpdateSettings', init);
  105. export default Search;