search.js 4.3 KB

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