8c33813b626e866d722ccbb6c5f84d2cc3df10518ebc8c9353767a522ff25434aaf6e9a841565aea6fdbb0ee3675df2df7d3c3cda5c6044b138bbd0422bebb 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. import Hooks from './../../pluginHooks';
  2. import {addClass, removeClass} from './../../helpers/dom/element';
  3. import {registerRenderer, getRenderer} from './../../renderers';
  4. /**
  5. * @private
  6. * @plugin Search
  7. */
  8. function Search(instance) {
  9. this.query = function(queryStr, callback, queryMethod) {
  10. var rowCount = instance.countRows();
  11. var colCount = instance.countCols();
  12. var queryResult = [];
  13. if (!callback) {
  14. callback = Search.global.getDefaultCallback();
  15. }
  16. if (!queryMethod) {
  17. queryMethod = Search.global.getDefaultQueryMethod();
  18. }
  19. for (var rowIndex = 0; rowIndex < rowCount; rowIndex++) {
  20. for (var colIndex = 0; colIndex < colCount; colIndex++) {
  21. var cellData = instance.getDataAtCell(rowIndex, colIndex);
  22. var cellProperties = instance.getCellMeta(rowIndex, colIndex);
  23. var cellCallback = cellProperties.search.callback || callback;
  24. var cellQueryMethod = cellProperties.search.queryMethod || queryMethod;
  25. var testResult = cellQueryMethod(queryStr, cellData);
  26. if (testResult) {
  27. var singleResult = {
  28. row: rowIndex,
  29. col: colIndex,
  30. data: cellData,
  31. };
  32. queryResult.push(singleResult);
  33. }
  34. if (cellCallback) {
  35. cellCallback(instance, rowIndex, colIndex, cellData, testResult);
  36. }
  37. }
  38. }
  39. return queryResult;
  40. };
  41. };
  42. Search.DEFAULT_CALLBACK = function(instance, row, col, data, testResult) {
  43. instance.getCellMeta(row, col).isSearchResult = testResult;
  44. };
  45. Search.DEFAULT_QUERY_METHOD = function(query, value) {
  46. if (typeof query == 'undefined' || query == null || !query.toLowerCase || query.length === 0) {
  47. return false;
  48. }
  49. if (typeof value == 'undefined' || value == null) {
  50. return false;
  51. }
  52. return value.toString().toLowerCase().indexOf(query.toLowerCase()) != -1;
  53. };
  54. Search.DEFAULT_SEARCH_RESULT_CLASS = 'htSearchResult';
  55. Search.global = (function() {
  56. var defaultCallback = Search.DEFAULT_CALLBACK;
  57. var defaultQueryMethod = Search.DEFAULT_QUERY_METHOD;
  58. var defaultSearchResultClass = Search.DEFAULT_SEARCH_RESULT_CLASS;
  59. return {
  60. getDefaultCallback() {
  61. return defaultCallback;
  62. },
  63. setDefaultCallback(newDefaultCallback) {
  64. defaultCallback = newDefaultCallback;
  65. },
  66. getDefaultQueryMethod() {
  67. return defaultQueryMethod;
  68. },
  69. setDefaultQueryMethod(newDefaultQueryMethod) {
  70. defaultQueryMethod = newDefaultQueryMethod;
  71. },
  72. getDefaultSearchResultClass() {
  73. return defaultSearchResultClass;
  74. },
  75. setDefaultSearchResultClass(newSearchResultClass) {
  76. defaultSearchResultClass = newSearchResultClass;
  77. }
  78. };
  79. }());
  80. function SearchCellDecorator(instance, TD, row, col, prop, value, cellProperties) {
  81. var searchResultClass = (cellProperties.search !== null && typeof cellProperties.search == 'object' &&
  82. 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;