devextreme
Version:
HTML5 JavaScript Component Suite for Responsive Web Development
126 lines (124 loc) • 4.89 kB
JavaScript
/**
* DevExtreme (cjs/__internal/grids/new/grid_core/search/utils.js)
* Version: 25.1.3
* Build date: Wed Jun 25 2025
*
* Copyright (c) 2012 - 2025 Developer Express Inc. ALL RIGHTS RESERVED
* Read about DevExtreme licensing here: https://js.devexpress.com/Licensing/
*/
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.splitHighlightedText = exports.createFilterExpression = exports.compareTextPart = exports.calculateSearchFilter = exports.allowSearch = exports.addSearchTextBox = void 0;
var _type = require("../../../../../core/utils/type");
var _message = _interopRequireDefault(require("../../../../../localization/message"));
var _m_utils = _interopRequireDefault(require("../../../../grids/grid_core/m_utils"));
var _common = require("../utils/common");
var _index = require("../utils/parse_value/index");
function _interopRequireDefault(e) {
return e && e.__esModule ? e : {
default: e
}
}
const HIGHLIGHT_SPLIT_SEPARATOR = "<--|--\x3e";
const FILTERING_TIMEOUT = 700;
const CLASS = {
searchPanel: "search-panel"
};
const compareTextPart = (textPart, searchStr, caseSensitive) => caseSensitive ? textPart === searchStr : textPart.toLowerCase() === searchStr.toLowerCase();
exports.compareTextPart = compareTextPart;
const splitHighlightedText = (text, _ref) => {
var _text$match;
let {
enabled: enabled,
searchStr: searchStr,
caseSensitive: caseSensitive
} = _ref;
if (!enabled || !searchStr) {
return null
}
const normalizedSearchStr = searchStr.replace(/\W|_/g, (match => `\\${match}`));
const regExp = new RegExp(normalizedSearchStr, "g" + (caseSensitive ? "" : "i"));
if (!(null !== (_text$match = text.match(regExp)) && void 0 !== _text$match && _text$match.length)) {
return null
}
return text.replace(regExp, (match => `<--|--\x3e${match}<--|--\x3e`)).split("<--|--\x3e").filter((textPart => !!textPart)).map((textPart => ({
type: compareTextPart(textPart, searchStr, caseSensitive) ? "highlighted" : "usual",
text: textPart
})))
};
exports.splitHighlightedText = splitHighlightedText;
const allowSearch = (column, searchVisibleColumnsOnly) => {
const allowSearchByVisibility = !searchVisibleColumnsOnly || column.visible;
const allowSearchByConfig = column.allowSearch;
return allowSearchByVisibility && allowSearchByConfig
};
exports.allowSearch = allowSearch;
const createFilterExpression = (column, filterValue, selectedFilterOperation, target) => {
let result = column.calculateFilterExpression(filterValue, selectedFilterOperation, target);
if ((0, _type.isFunction)(result)) {
result = [result, "=", true]
}
return result
};
exports.createFilterExpression = createFilterExpression;
const calculateSearchFilter = (text, columns, searchVisibleColumnsOnly) => {
const filters = [];
if (!text) {
return null
}
for (const column of columns) {
if (allowSearch(column, searchVisibleColumnsOnly)) {
const filterValue = (0, _index.parseValue)(column, text);
if (void 0 !== filterValue) {
const expression = createFilterExpression(column, filterValue, void 0, "search");
filters.push(expression)
}
}
}
if (0 === filters.length) {
return ["!"]
}
return _m_utils.default.combineFilters(filters, "or")
};
exports.calculateSearchFilter = calculateSearchFilter;
let timer;
const addSearchTextBox = (props, setTextBoxRef) => ({
name: "searchPanel",
showText: "inMenu",
location: "after",
locateInMenu: "auto",
widget: "dxTextBox",
options: {
onContentReady: _ref2 => {
let {
component: component
} = _ref2;
setTextBoxRef(component)
},
onInput: e => {
clearTimeout(timer);
const component = e.component;
const newValue = component._input().val();
timer = setTimeout((() => {
var _props$onValueChanged;
null === (_props$onValueChanged = props.onValueChanged) || void 0 === _props$onValueChanged || _props$onValueChanged.call(props, newValue)
}), 700)
},
value: props.value,
placeholder: props.placeholder,
width: props.width,
inputAttr: {
"aria-label": _message.default.format(`${(0,_common.getName)()}-ariaSearchInGrid`)
},
elementAttr: {
class: (0, _common.addWidgetPrefix)(CLASS.searchPanel)
},
mode: "search",
onDisposing: () => {
clearTimeout(timer)
}
}
});
exports.addSearchTextBox = addSearchTextBox;