UNPKG

24.3 kBJavaScriptView Raw
1/**
2 * ag-grid - Advanced Data Grid / Data Table supporting Javascript / React / AngularJS / Web Components
3 * @version v18.1.2
4 * @link http://www.ag-grid.com/
5 * @license MIT
6 */
7"use strict";
8var __extends = (this && this.__extends) || (function () {
9 var extendStatics = Object.setPrototypeOf ||
10 ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
11 function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
12 return function (d, b) {
13 extendStatics(d, b);
14 function __() { this.constructor = d; }
15 d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
16 };
17})();
18var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
19 var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
20 if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
21 else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
22 return c > 3 && r && Object.defineProperty(target, key, r), r;
23};
24var __metadata = (this && this.__metadata) || function (k, v) {
25 if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
26};
27Object.defineProperty(exports, "__esModule", { value: true });
28var component_1 = require("../widgets/component");
29var componentAnnotations_1 = require("../widgets/componentAnnotations");
30var context_1 = require("../context/context");
31var gridOptionsWrapper_1 = require("../gridOptionsWrapper");
32var utils_1 = require("../utils");
33var FilterConditionType;
34(function (FilterConditionType) {
35 FilterConditionType[FilterConditionType["MAIN"] = 0] = "MAIN";
36 FilterConditionType[FilterConditionType["CONDITION"] = 1] = "CONDITION";
37})(FilterConditionType = exports.FilterConditionType || (exports.FilterConditionType = {}));
38var DEFAULT_TRANSLATIONS = {
39 loadingOoo: 'Loading...',
40 equals: 'Equals',
41 notEqual: 'Not equal',
42 lessThan: 'Less than',
43 greaterThan: 'Greater than',
44 inRange: 'In range',
45 lessThanOrEqual: 'Less than or equals',
46 greaterThanOrEqual: 'Greater than or equals',
47 filterOoo: 'Filter...',
48 contains: 'Contains',
49 notContains: 'Not contains',
50 startsWith: 'Starts with',
51 endsWith: 'Ends with',
52 searchOoo: 'Search...',
53 selectAll: 'Select All',
54 applyFilter: 'Apply Filter',
55 clearFilter: 'Clear Filter'
56};
57/**
58 * T(ype) The type of this filter. ie in DateFilter T=Date
59 * P(arams) The params that this filter can take
60 * M(model getModel/setModel) The object that this filter serializes to
61 * F Floating filter params
62 *
63 * Contains common logic to ALL filters.. Translation, apply and clear button
64 * get/setModel context wiring....
65 */
66var BaseFilter = (function (_super) {
67 __extends(BaseFilter, _super);
68 function BaseFilter() {
69 return _super !== null && _super.apply(this, arguments) || this;
70 }
71 BaseFilter.prototype.init = function (params) {
72 this.filterParams = params;
73 this.defaultFilter = this.filterParams.defaultOption;
74 if (this.filterParams.filterOptions && !this.defaultFilter) {
75 if (this.filterParams.filterOptions.lastIndexOf(BaseFilter.EQUALS) < 0) {
76 this.defaultFilter = this.filterParams.filterOptions[0];
77 }
78 }
79 this.customInit();
80 this.filter = this.defaultFilter;
81 this.filterCondition = this.defaultFilter;
82 this.clearActive = params.clearButton === true;
83 //Allowing for old param property apply, even though is not advertised through the interface
84 this.applyActive = ((params.applyButton === true) || (params.apply === true));
85 this.newRowsActionKeep = params.newRowsAction === 'keep';
86 this.setTemplate(this.generateTemplate());
87 utils_1._.setVisible(this.eApplyButton, this.applyActive);
88 if (this.applyActive) {
89 this.addDestroyableEventListener(this.eApplyButton, "click", this.filterParams.filterChangedCallback);
90 }
91 utils_1._.setVisible(this.eClearButton, this.clearActive);
92 if (this.clearActive) {
93 this.addDestroyableEventListener(this.eClearButton, "click", this.onClearButton.bind(this));
94 }
95 var anyButtonVisible = this.applyActive || this.clearActive;
96 utils_1._.setVisible(this.eButtonsPanel, anyButtonVisible);
97 this.instantiate(this.context);
98 this.initialiseFilterBodyUi(FilterConditionType.MAIN);
99 this.refreshFilterBodyUi(FilterConditionType.MAIN);
100 };
101 BaseFilter.prototype.onClearButton = function () {
102 this.setModel(null);
103 this.onFilterChanged();
104 };
105 BaseFilter.prototype.floatingFilter = function (from) {
106 if (from !== '') {
107 var model = this.modelFromFloatingFilter(from);
108 this.setModel(model);
109 }
110 else {
111 this.resetState();
112 }
113 this.onFilterChanged();
114 };
115 BaseFilter.prototype.onNewRowsLoaded = function () {
116 if (!this.newRowsActionKeep) {
117 this.resetState();
118 }
119 };
120 BaseFilter.prototype.getModel = function () {
121 if (this.isFilterActive()) {
122 if (!this.isFilterConditionActive(FilterConditionType.CONDITION)) {
123 return this.serialize(FilterConditionType.MAIN);
124 }
125 else {
126 return {
127 condition1: this.serialize(FilterConditionType.MAIN),
128 condition2: this.serialize(FilterConditionType.CONDITION),
129 operator: this.conditionValue
130 };
131 }
132 }
133 else {
134 return null;
135 }
136 };
137 BaseFilter.prototype.getNullableModel = function () {
138 if (!this.isFilterConditionActive(FilterConditionType.CONDITION)) {
139 return this.serialize(FilterConditionType.MAIN);
140 }
141 else {
142 return {
143 condition1: this.serialize(FilterConditionType.MAIN),
144 condition2: this.serialize(FilterConditionType.CONDITION),
145 operator: this.conditionValue
146 };
147 }
148 };
149 BaseFilter.prototype.setModel = function (model) {
150 if (model) {
151 if (!model.operator) {
152 this.resetState();
153 this.parse(model, FilterConditionType.MAIN);
154 }
155 else {
156 var asCombinedFilter = model;
157 this.parse((asCombinedFilter).condition1, FilterConditionType.MAIN);
158 this.parse((asCombinedFilter).condition2, FilterConditionType.CONDITION);
159 this.conditionValue = asCombinedFilter.operator;
160 }
161 }
162 else {
163 this.resetState();
164 }
165 this.redrawCondition();
166 this.refreshFilterBodyUi(FilterConditionType.MAIN);
167 this.refreshFilterBodyUi(FilterConditionType.CONDITION);
168 };
169 BaseFilter.prototype.doOnFilterChanged = function (applyNow) {
170 if (applyNow === void 0) { applyNow = false; }
171 this.filterParams.filterModifiedCallback();
172 var requiresApplyAndIsApplying = this.applyActive && applyNow;
173 var notRequiresApply = !this.applyActive;
174 var shouldFilter = notRequiresApply || requiresApplyAndIsApplying;
175 if (shouldFilter) {
176 this.filterParams.filterChangedCallback();
177 }
178 this.refreshFilterBodyUi(FilterConditionType.MAIN);
179 this.refreshFilterBodyUi(FilterConditionType.CONDITION);
180 return shouldFilter;
181 };
182 BaseFilter.prototype.onFilterChanged = function (applyNow) {
183 if (applyNow === void 0) { applyNow = false; }
184 this.doOnFilterChanged(applyNow);
185 this.redrawCondition();
186 this.refreshFilterBodyUi(FilterConditionType.MAIN);
187 this.refreshFilterBodyUi(FilterConditionType.CONDITION);
188 };
189 BaseFilter.prototype.redrawCondition = function () {
190 var _this = this;
191 var filterCondition = this.eFilterBodyWrapper.querySelector('.ag-filter-condition');
192 if (!filterCondition && this.isFilterActive() && this.acceptsBooleanLogic()) {
193 this.eConditionWrapper = utils_1._.loadTemplate(this.createConditionTemplate(FilterConditionType.CONDITION));
194 this.eFilterBodyWrapper.appendChild(this.eConditionWrapper);
195 this.wireQuerySelectors();
196 var _a = this.refreshOperatorUi(), andButton = _a.andButton, orButton = _a.orButton;
197 this.addDestroyableEventListener(andButton, 'change', function () {
198 _this.conditionValue = 'AND';
199 _this.onFilterChanged();
200 });
201 this.addDestroyableEventListener(orButton, 'change', function () {
202 _this.conditionValue = 'OR';
203 _this.onFilterChanged();
204 });
205 this.initialiseFilterBodyUi(FilterConditionType.CONDITION);
206 }
207 else if (filterCondition && !this.isFilterActive()) {
208 this.eFilterBodyWrapper.removeChild(this.eConditionWrapper);
209 this.eConditionWrapper = null;
210 }
211 else {
212 this.refreshFilterBodyUi(FilterConditionType.CONDITION);
213 if (this.eConditionWrapper) {
214 this.refreshOperatorUi();
215 }
216 }
217 };
218 BaseFilter.prototype.refreshOperatorUi = function () {
219 var andButton = this.eConditionWrapper.querySelector('.and');
220 var orButton = this.eConditionWrapper.querySelector('.or');
221 this.conditionValue = this.conditionValue == null ? 'AND' : this.conditionValue;
222 andButton.checked = this.conditionValue === 'AND';
223 orButton.checked = this.conditionValue === 'OR';
224 return { andButton: andButton, orButton: orButton };
225 };
226 BaseFilter.prototype.onFloatingFilterChanged = function (change) {
227 //It has to be of the type FloatingFilterWithApplyChange if it gets here
228 var casted = change;
229 if (casted == null) {
230 this.setModel(null);
231 }
232 else if (!this.isFilterConditionActive(FilterConditionType.CONDITION)) {
233 this.setModel(casted ? casted.model : null);
234 }
235 else {
236 var combinedFilter = {
237 condition1: casted.model,
238 condition2: this.serialize(FilterConditionType.CONDITION),
239 operator: this.conditionValue
240 };
241 this.setModel(combinedFilter);
242 }
243 return this.doOnFilterChanged(casted ? casted.apply : false);
244 };
245 BaseFilter.prototype.generateFilterHeader = function (type) {
246 return '';
247 };
248 BaseFilter.prototype.generateTemplate = function () {
249 var translate = this.translate.bind(this);
250 var mainConditionBody = this.createConditionBody(FilterConditionType.MAIN);
251 var bodyWithBooleanLogic = !this.acceptsBooleanLogic() ?
252 mainConditionBody :
253 this.wrapCondition(mainConditionBody);
254 return "<div>\n <div class='ag-filter-body-wrapper'>" + bodyWithBooleanLogic + "</div>\n <div class=\"ag-filter-apply-panel\" id=\"applyPanel\">\n <button type=\"button\" id=\"clearButton\">" + translate('clearFilter') + "</button>\n <button type=\"button\" id=\"applyButton\">" + translate('applyFilter') + "</button>\n </div>\n </div>";
255 };
256 BaseFilter.prototype.acceptsBooleanLogic = function () {
257 return false;
258 };
259 BaseFilter.prototype.wrapCondition = function (mainCondition) {
260 if (!this.isFilterActive())
261 return mainCondition;
262 return "" + mainCondition + this.createConditionTemplate(FilterConditionType.CONDITION);
263 };
264 BaseFilter.prototype.createConditionTemplate = function (type) {
265 return "<div class=\"ag-filter-condition\">\n <input id=\"andId\" type=\"radio\" class=\"and\" name=\"booleanLogic\" value=\"AND\" checked=\"checked\" /><label style=\"display: inline\" for=\"andId\">AND</label>\n <input id=\"orId\" type=\"radio\" class=\"or\" name=\"booleanLogic\" value=\"OR\" /><label style=\"display: inline\" for=\"orId\">OR</label>\n <div>" + this.createConditionBody(type) + "</div>\n </div>";
266 };
267 BaseFilter.prototype.createConditionBody = function (type) {
268 var body = this.bodyTemplate(type);
269 return this.generateFilterHeader(type) + body;
270 };
271 BaseFilter.prototype.translate = function (toTranslate) {
272 var translate = this.gridOptionsWrapper.getLocaleTextFunc();
273 return translate(toTranslate, DEFAULT_TRANSLATIONS[toTranslate]);
274 };
275 BaseFilter.prototype.getDebounceMs = function (filterParams) {
276 if (filterParams.applyButton && filterParams.debounceMs) {
277 console.warn('ag-Grid: debounceMs is ignored when applyButton = true');
278 return 0;
279 }
280 return filterParams.debounceMs != null ? filterParams.debounceMs : 500;
281 };
282 BaseFilter.EQUALS = 'equals';
283 BaseFilter.NOT_EQUAL = 'notEqual';
284 BaseFilter.LESS_THAN = 'lessThan';
285 BaseFilter.LESS_THAN_OR_EQUAL = 'lessThanOrEqual';
286 BaseFilter.GREATER_THAN = 'greaterThan';
287 BaseFilter.GREATER_THAN_OR_EQUAL = 'greaterThanOrEqual';
288 BaseFilter.IN_RANGE = 'inRange';
289 BaseFilter.CONTAINS = 'contains'; //1;
290 BaseFilter.NOT_CONTAINS = 'notContains'; //1;
291 BaseFilter.STARTS_WITH = 'startsWith'; //4;
292 BaseFilter.ENDS_WITH = 'endsWith'; //5;
293 __decorate([
294 componentAnnotations_1.QuerySelector('#applyPanel'),
295 __metadata("design:type", HTMLElement)
296 ], BaseFilter.prototype, "eButtonsPanel", void 0);
297 __decorate([
298 componentAnnotations_1.QuerySelector('.ag-filter-body-wrapper'),
299 __metadata("design:type", HTMLElement)
300 ], BaseFilter.prototype, "eFilterBodyWrapper", void 0);
301 __decorate([
302 componentAnnotations_1.QuerySelector('#applyButton'),
303 __metadata("design:type", HTMLElement)
304 ], BaseFilter.prototype, "eApplyButton", void 0);
305 __decorate([
306 componentAnnotations_1.QuerySelector('#clearButton'),
307 __metadata("design:type", HTMLElement)
308 ], BaseFilter.prototype, "eClearButton", void 0);
309 __decorate([
310 context_1.Autowired('context'),
311 __metadata("design:type", context_1.Context)
312 ], BaseFilter.prototype, "context", void 0);
313 __decorate([
314 context_1.Autowired('gridOptionsWrapper'),
315 __metadata("design:type", gridOptionsWrapper_1.GridOptionsWrapper)
316 ], BaseFilter.prototype, "gridOptionsWrapper", void 0);
317 return BaseFilter;
318}(component_1.Component));
319exports.BaseFilter = BaseFilter;
320/**
321 * Every filter with a dropdown where the user can specify a comparing type against the filter values
322 */
323var ComparableBaseFilter = (function (_super) {
324 __extends(ComparableBaseFilter, _super);
325 function ComparableBaseFilter() {
326 return _super !== null && _super.apply(this, arguments) || this;
327 }
328 ComparableBaseFilter.prototype.doesFilterPass = function (params) {
329 var mainFilterResult = this.individualFilterPasses(params, FilterConditionType.MAIN);
330 if (this.eTypeConditionSelector == null) {
331 return mainFilterResult;
332 }
333 var auxFilterResult = this.individualFilterPasses(params, FilterConditionType.CONDITION);
334 return this.conditionValue === 'AND' ? mainFilterResult && auxFilterResult : mainFilterResult || auxFilterResult;
335 };
336 ComparableBaseFilter.prototype.init = function (params) {
337 _super.prototype.init.call(this, params);
338 this.suppressAndOrCondition = params.suppressAndOrCondition;
339 };
340 ComparableBaseFilter.prototype.customInit = function () {
341 if (!this.defaultFilter) {
342 this.defaultFilter = this.getDefaultType();
343 }
344 };
345 ComparableBaseFilter.prototype.acceptsBooleanLogic = function () {
346 return this.suppressAndOrCondition !== true;
347 };
348 ComparableBaseFilter.prototype.generateFilterHeader = function (type) {
349 var _this = this;
350 var defaultFilterTypes = this.getApplicableFilterTypes();
351 var restrictedFilterTypes = this.filterParams.filterOptions;
352 var actualFilterTypes = restrictedFilterTypes ? restrictedFilterTypes : defaultFilterTypes;
353 var optionsHtml = actualFilterTypes.map(function (filterType) {
354 var localeFilterName = _this.translate(filterType);
355 return "<option value=\"" + filterType + "\">" + localeFilterName + "</option>";
356 });
357 var readOnly = optionsHtml.length == 1 ? 'disabled' : '';
358 var id = type == FilterConditionType.MAIN ? 'filterType' : 'filterConditionType';
359 return optionsHtml.length <= 0 ?
360 '' :
361 "<div>\n <select class=\"ag-filter-select\" id=\"" + id + "\" " + readOnly + ">\n " + optionsHtml.join('') + "\n </select>\n </div>";
362 };
363 ComparableBaseFilter.prototype.initialiseFilterBodyUi = function (type) {
364 var _this = this;
365 if (type === FilterConditionType.MAIN) {
366 this.setFilterType(this.filter, type);
367 this.addDestroyableEventListener(this.eTypeSelector, "change", function () { return _this.onFilterTypeChanged(type); });
368 }
369 else {
370 this.setFilterType(this.filterCondition, type);
371 this.addDestroyableEventListener(this.eTypeConditionSelector, "change", function () { return _this.onFilterTypeChanged(type); });
372 }
373 };
374 ComparableBaseFilter.prototype.onFilterTypeChanged = function (type) {
375 if (type === FilterConditionType.MAIN) {
376 this.filter = this.eTypeSelector.value;
377 }
378 else {
379 this.filterCondition = this.eTypeConditionSelector.value;
380 }
381 this.refreshFilterBodyUi(type);
382 // we check if filter is active, so that if user changes the type (eg from 'less than' to 'equals'),
383 // well this doesn't matter if the user has no value in the text field, so don't fire 'onFilterChanged'.
384 // this means we don't refresh the grid when the type changes if no value is present.
385 if (this.isFilterActive()) {
386 this.onFilterChanged();
387 }
388 };
389 ComparableBaseFilter.prototype.isFilterActive = function () {
390 var rawFilterValues = this.filterValues(FilterConditionType.MAIN);
391 if (this.filter === BaseFilter.IN_RANGE) {
392 var filterValueArray = rawFilterValues;
393 return filterValueArray[0] != null && filterValueArray[1] != null;
394 }
395 else {
396 return rawFilterValues != null;
397 }
398 };
399 ComparableBaseFilter.prototype.setFilterType = function (filterType, type) {
400 if (type === FilterConditionType.MAIN) {
401 this.filter = filterType;
402 if (!this.eTypeSelector)
403 return;
404 this.eTypeSelector.value = filterType;
405 }
406 else {
407 this.filterCondition = filterType;
408 if (!this.eTypeConditionSelector)
409 return;
410 this.eTypeConditionSelector.value = filterType;
411 }
412 };
413 ComparableBaseFilter.prototype.isFilterConditionActive = function (type) {
414 return this.filterValues(type) != null;
415 };
416 __decorate([
417 componentAnnotations_1.QuerySelector('#filterType'),
418 __metadata("design:type", HTMLSelectElement)
419 ], ComparableBaseFilter.prototype, "eTypeSelector", void 0);
420 __decorate([
421 componentAnnotations_1.QuerySelector('#filterConditionType'),
422 __metadata("design:type", HTMLSelectElement)
423 ], ComparableBaseFilter.prototype, "eTypeConditionSelector", void 0);
424 return ComparableBaseFilter;
425}(BaseFilter));
426exports.ComparableBaseFilter = ComparableBaseFilter;
427/**
428 * Comparable filter with scalar underlying values (ie numbers and dates. Strings are not scalar so have to extend
429 * ComparableBaseFilter)
430 */
431var ScalarBaseFilter = (function (_super) {
432 __extends(ScalarBaseFilter, _super);
433 function ScalarBaseFilter() {
434 return _super !== null && _super.apply(this, arguments) || this;
435 }
436 ScalarBaseFilter.prototype.nullComparator = function (type) {
437 var _this = this;
438 return function (filterValue, gridValue) {
439 if (gridValue == null) {
440 var nullValue = _this.translateNull(type);
441 if (_this.filter === BaseFilter.EQUALS) {
442 return nullValue ? 0 : 1;
443 }
444 if (_this.filter === BaseFilter.GREATER_THAN) {
445 return nullValue ? 1 : -1;
446 }
447 if (_this.filter === BaseFilter.GREATER_THAN_OR_EQUAL) {
448 return nullValue ? 1 : -1;
449 }
450 if (_this.filter === BaseFilter.LESS_THAN_OR_EQUAL) {
451 return nullValue ? -1 : 1;
452 }
453 if (_this.filter === BaseFilter.LESS_THAN) {
454 return nullValue ? -1 : 1;
455 }
456 if (_this.filter === BaseFilter.NOT_EQUAL) {
457 return nullValue ? 1 : 0;
458 }
459 }
460 var actualComparator = _this.comparator();
461 return actualComparator(filterValue, gridValue);
462 };
463 };
464 ScalarBaseFilter.prototype.getDefaultType = function () {
465 return BaseFilter.EQUALS;
466 };
467 ScalarBaseFilter.prototype.translateNull = function (type) {
468 var reducedType = type.indexOf('greater') > -1 ? 'greaterThan' :
469 type.indexOf('lessThan') > -1 ? 'lessThan' :
470 'equals';
471 if (this.filterParams.nullComparator && this.filterParams.nullComparator[reducedType]) {
472 return this.filterParams.nullComparator[reducedType];
473 }
474 return ScalarBaseFilter.DEFAULT_NULL_COMPARATOR[reducedType];
475 };
476 ScalarBaseFilter.prototype.individualFilterPasses = function (params, type) {
477 return this.doIndividualFilterPasses(params, type, type === FilterConditionType.MAIN ? this.filter : this.filterCondition);
478 };
479 ScalarBaseFilter.prototype.doIndividualFilterPasses = function (params, type, filter) {
480 var value = this.filterParams.valueGetter(params.node);
481 var comparator = this.nullComparator(filter);
482 var rawFilterValues = this.filterValues(type);
483 var from = Array.isArray(rawFilterValues) ? rawFilterValues[0] : rawFilterValues;
484 if (from == null) {
485 return type === FilterConditionType.MAIN ? true : this.conditionValue === 'AND';
486 }
487 var compareResult = comparator(from, value);
488 if (filter === BaseFilter.EQUALS) {
489 return compareResult === 0;
490 }
491 if (filter === BaseFilter.GREATER_THAN) {
492 return compareResult > 0;
493 }
494 if (filter === BaseFilter.GREATER_THAN_OR_EQUAL) {
495 return compareResult >= 0;
496 }
497 if (filter === BaseFilter.LESS_THAN_OR_EQUAL) {
498 return compareResult <= 0;
499 }
500 if (filter === BaseFilter.LESS_THAN) {
501 return compareResult < 0;
502 }
503 if (filter === BaseFilter.NOT_EQUAL) {
504 return compareResult != 0;
505 }
506 //From now on the type is a range and rawFilterValues must be an array!
507 var compareToResult = comparator(rawFilterValues[1], value);
508 if (filter === BaseFilter.IN_RANGE) {
509 if (!this.filterParams.inRangeInclusive) {
510 return compareResult > 0 && compareToResult < 0;
511 }
512 else {
513 return compareResult >= 0 && compareToResult <= 0;
514 }
515 }
516 throw new Error('Unexpected type of filter!: ' + filter);
517 };
518 ScalarBaseFilter.DEFAULT_NULL_COMPARATOR = {
519 equals: false,
520 lessThan: false,
521 greaterThan: false
522 };
523 return ScalarBaseFilter;
524}(ComparableBaseFilter));
525exports.ScalarBaseFilter = ScalarBaseFilter;