ui.search_box_mixin.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /**
  2. * DevExtreme (ui/widget/ui.search_box_mixin.js)
  3. * Version: 19.1.16
  4. * Build date: Tue Oct 18 2022
  5. *
  6. * Copyright (c) 2012 - 2022 Developer Express Inc. ALL RIGHTS RESERVED
  7. * Read about DevExtreme licensing here: https://js.devexpress.com/Licensing/
  8. */
  9. "use strict";
  10. var _renderer = require("../../core/renderer");
  11. var _renderer2 = _interopRequireDefault(_renderer);
  12. var _extend = require("../../core/utils/extend");
  13. var _message = require("../../localization/message");
  14. var _message2 = _interopRequireDefault(_message);
  15. var _text_box = require("../text_box");
  16. var _text_box2 = _interopRequireDefault(_text_box);
  17. var _ui = require("../widget/ui.errors");
  18. var _ui2 = _interopRequireDefault(_ui);
  19. var _deferred = require("../../core/utils/deferred");
  20. function _interopRequireDefault(obj) {
  21. return obj && obj.__esModule ? obj : {
  22. "default": obj
  23. }
  24. }
  25. module.exports = {
  26. _getDefaultOptions: function() {
  27. return (0, _extend.extend)(this.callBase(), {
  28. searchMode: "",
  29. searchExpr: null,
  30. searchValue: "",
  31. searchEnabled: false,
  32. searchEditorOptions: {}
  33. })
  34. },
  35. _initMarkup: function() {
  36. this._renderSearch();
  37. this.callBase()
  38. },
  39. _renderSearch: function() {
  40. var $element = this.$element();
  41. var searchEnabled = this.option("searchEnabled");
  42. var searchBoxClassName = this._addWidgetPrefix("search");
  43. var rootElementClassName = this._addWidgetPrefix("with-search");
  44. if (!searchEnabled) {
  45. $element.removeClass(rootElementClassName);
  46. this._removeSearchBox();
  47. return
  48. }
  49. var editorOptions = this._getSearchEditorOptions();
  50. if (this._searchEditor) {
  51. this._searchEditor.option(editorOptions)
  52. } else {
  53. $element.addClass(rootElementClassName);
  54. this._$searchEditorElement = (0, _renderer2.default)("<div>").addClass(searchBoxClassName).prependTo($element);
  55. this._searchEditor = this._createComponent(this._$searchEditorElement, _text_box2.default, editorOptions)
  56. }
  57. },
  58. _removeSearchBox: function() {
  59. this._$searchEditorElement && this._$searchEditorElement.remove();
  60. delete this._$searchEditorElement;
  61. delete this._searchEditor
  62. },
  63. _getSearchEditorOptions: function() {
  64. var that = this;
  65. var userEditorOptions = that.option("searchEditorOptions");
  66. var searchText = _message2.default.format("Search");
  67. return (0, _extend.extend)({
  68. mode: "search",
  69. placeholder: searchText,
  70. tabIndex: that.option("tabIndex"),
  71. value: that.option("searchValue"),
  72. valueChangeEvent: "input",
  73. inputAttr: {
  74. "aria-label": searchText
  75. },
  76. onValueChanged: function(e) {
  77. var searchTimeout = that.option("searchTimeout");
  78. that._valueChangeDeferred = new _deferred.Deferred;
  79. clearTimeout(that._valueChangeTimeout);
  80. that._valueChangeDeferred.done(function() {
  81. this.option("searchValue", e.value)
  82. }.bind(that));
  83. if (e.event && "input" === e.event.type && searchTimeout) {
  84. that._valueChangeTimeout = setTimeout(function() {
  85. that._valueChangeDeferred.resolve()
  86. }, searchTimeout)
  87. } else {
  88. that._valueChangeDeferred.resolve()
  89. }
  90. }
  91. }, userEditorOptions)
  92. },
  93. _getAriaTarget: function() {
  94. if (this.option("searchEnabled")) {
  95. return this._itemContainer(true)
  96. }
  97. return this.$element()
  98. },
  99. _focusTarget: function() {
  100. if (this.option("searchEnabled")) {
  101. return this._itemContainer(true)
  102. }
  103. return this.callBase()
  104. },
  105. _updateFocusState: function(e, isFocused) {
  106. if (this.option("searchEnabled")) {
  107. this._toggleFocusClass(isFocused, this.$element())
  108. }
  109. this.callBase(e, isFocused)
  110. },
  111. getOperationBySearchMode: function(searchMode) {
  112. return "equals" === searchMode ? "=" : searchMode
  113. },
  114. _cleanAria: function($target) {
  115. this.setAria({
  116. role: null,
  117. activedescendant: null
  118. }, $target);
  119. $target.attr("tabIndex", null)
  120. },
  121. _optionChanged: function(args) {
  122. switch (args.name) {
  123. case "searchEnabled":
  124. case "searchEditorOptions":
  125. this._cleanAria(this.option("searchEnabled") ? this.$element() : this._itemContainer());
  126. this._invalidate();
  127. break;
  128. case "searchExpr":
  129. case "searchMode":
  130. case "searchValue":
  131. if (!this._dataSource) {
  132. _ui2.default.log("W1009");
  133. return
  134. }
  135. if ("searchMode" === args.name) {
  136. this._dataSource.searchOperation(this.getOperationBySearchMode(args.value))
  137. } else {
  138. this._dataSource[args.name](args.value)
  139. }
  140. this._dataSource.load();
  141. break;
  142. case "searchTimeout":
  143. break;
  144. default:
  145. this.callBase(args)
  146. }
  147. },
  148. focus: function() {
  149. if (!this.option("focusedElement") && this.option("searchEnabled")) {
  150. this._searchEditor && this._searchEditor.focus();
  151. return
  152. }
  153. this.callBase()
  154. },
  155. _refresh: function() {
  156. if (this._valueChangeDeferred) {
  157. this._valueChangeDeferred.resolve()
  158. }
  159. this.callBase()
  160. }
  161. };