UNPKG

14.8 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 utils_1 = require("../utils");
31var baseFilter_1 = require("./baseFilter");
32var context_1 = require("../context/context");
33var componentRecipes_1 = require("../components/framework/componentRecipes");
34var DateFilter = (function (_super) {
35 __extends(DateFilter, _super);
36 function DateFilter() {
37 return _super !== null && _super.apply(this, arguments) || this;
38 }
39 DateFilter.prototype.modelFromFloatingFilter = function (from) {
40 return {
41 dateFrom: from,
42 dateTo: this.getDateTo(),
43 type: this.filter,
44 filterType: 'date'
45 };
46 };
47 DateFilter.prototype.getApplicableFilterTypes = function () {
48 return [baseFilter_1.BaseFilter.EQUALS, baseFilter_1.BaseFilter.GREATER_THAN, baseFilter_1.BaseFilter.LESS_THAN, baseFilter_1.BaseFilter.NOT_EQUAL, baseFilter_1.BaseFilter.IN_RANGE];
49 };
50 DateFilter.prototype.bodyTemplate = function (type) {
51 var fromPanelId = type == baseFilter_1.FilterConditionType.MAIN ? "filterDateFromPanel" : "filterDateFromConditionPanel";
52 var toPanelId = type == baseFilter_1.FilterConditionType.MAIN ? "filterDateToPanel" : "filterDateToConditionPanel";
53 return "<div class=\"ag-filter-body\">\n <div class=\"ag-filter-date-from\" id=\"" + fromPanelId + "\">\n </div>\n <div class=\"ag-filter-date-to\" id=\"" + toPanelId + "\">\n </div>\n </div>";
54 };
55 DateFilter.prototype.initialiseFilterBodyUi = function (type) {
56 _super.prototype.initialiseFilterBodyUi.call(this, type);
57 this.createComponents(type);
58 if (type === baseFilter_1.FilterConditionType.MAIN) {
59 this.setDateFrom_date(this.dateFrom, baseFilter_1.FilterConditionType.MAIN);
60 this.setDateTo_date(this.dateTo, baseFilter_1.FilterConditionType.MAIN);
61 this.setFilterType(this.filterCondition, baseFilter_1.FilterConditionType.MAIN);
62 }
63 else {
64 this.setDateFrom_date(this.dateFromCondition, baseFilter_1.FilterConditionType.CONDITION);
65 this.setDateTo_date(this.dateToCondition, baseFilter_1.FilterConditionType.CONDITION);
66 this.setFilterType(this.filterCondition, baseFilter_1.FilterConditionType.CONDITION);
67 }
68 };
69 DateFilter.prototype.createComponents = function (type) {
70 var _this = this;
71 var dateComponentParams = {
72 onDateChanged: function () { _this.onDateChanged(type); },
73 filterParams: this.filterParams
74 };
75 this.componentRecipes.newDateComponent(dateComponentParams).then(function (dateToComponent) {
76 if (type === baseFilter_1.FilterConditionType.MAIN) {
77 _this.dateToComponent = dateToComponent;
78 }
79 else {
80 _this.dateToConditionComponent = dateToComponent;
81 }
82 var dateToElement = dateToComponent.getGui();
83 if (type === baseFilter_1.FilterConditionType.MAIN) {
84 _this.eDateToPanel.appendChild(dateToElement);
85 if (_this.dateToComponent.afterGuiAttached) {
86 _this.dateToComponent.afterGuiAttached();
87 }
88 }
89 else {
90 _this.eDateToConditionPanel.appendChild(dateToElement);
91 if (_this.dateToConditionComponent.afterGuiAttached) {
92 _this.dateToConditionComponent.afterGuiAttached();
93 }
94 }
95 });
96 this.componentRecipes.newDateComponent(dateComponentParams).then(function (dateComponent) {
97 if (type === baseFilter_1.FilterConditionType.MAIN) {
98 _this.dateFromComponent = dateComponent;
99 }
100 else {
101 _this.dateFromConditionComponent = dateComponent;
102 }
103 var dateFromElement = dateComponent.getGui();
104 if (type === baseFilter_1.FilterConditionType.MAIN) {
105 _this.eDateFromPanel.appendChild(dateFromElement);
106 if (_this.dateFromComponent.afterGuiAttached) {
107 _this.dateFromComponent.afterGuiAttached();
108 }
109 }
110 else {
111 _this.eDateFromConditionPanel.appendChild(dateFromElement);
112 if (_this.dateFromConditionComponent.afterGuiAttached) {
113 _this.dateFromConditionComponent.afterGuiAttached();
114 }
115 }
116 });
117 };
118 DateFilter.prototype.onDateChanged = function (type) {
119 if (type === baseFilter_1.FilterConditionType.MAIN) {
120 this.dateFrom = DateFilter.removeTimezone(this.dateFromComponent.getDate());
121 this.dateTo = DateFilter.removeTimezone(this.dateToComponent.getDate());
122 }
123 else {
124 this.dateFromCondition = DateFilter.removeTimezone(this.dateFromComponent.getDate());
125 this.dateToCondition = DateFilter.removeTimezone(this.dateToComponent.getDate());
126 }
127 this.onFilterChanged();
128 };
129 DateFilter.prototype.refreshFilterBodyUi = function (type) {
130 var panel;
131 var filterTypeValue;
132 if (type === baseFilter_1.FilterConditionType.MAIN) {
133 panel = this.eDateToPanel;
134 filterTypeValue = this.filter;
135 }
136 else {
137 panel = this.eDateToConditionPanel;
138 filterTypeValue = this.filterCondition;
139 }
140 if (!panel)
141 return;
142 var visible = filterTypeValue === baseFilter_1.BaseFilter.IN_RANGE;
143 utils_1.Utils.setVisible(panel, visible);
144 };
145 DateFilter.prototype.comparator = function () {
146 return this.filterParams.comparator ? this.filterParams.comparator : this.defaultComparator.bind(this);
147 };
148 DateFilter.prototype.defaultComparator = function (filterDate, cellValue) {
149 //The default comparator assumes that the cellValue is a date
150 var cellAsDate = cellValue;
151 if (cellAsDate < filterDate) {
152 return -1;
153 }
154 if (cellAsDate > filterDate) {
155 return 1;
156 }
157 return cellValue != null ? 0 : -1;
158 };
159 DateFilter.prototype.serialize = function (type) {
160 var dateToComponent = type === baseFilter_1.FilterConditionType.MAIN ? this.dateToComponent : this.dateToConditionComponent;
161 var dateFromComponent = type === baseFilter_1.FilterConditionType.MAIN ? this.dateFromComponent : this.dateFromConditionComponent;
162 var filterType = type === baseFilter_1.FilterConditionType.MAIN ? this.filter : this.filterCondition;
163 return {
164 dateTo: utils_1.Utils.serializeDateToYyyyMmDd(dateToComponent.getDate(), "-"),
165 dateFrom: utils_1.Utils.serializeDateToYyyyMmDd(dateFromComponent.getDate(), "-"),
166 type: filterType ? filterType : this.defaultFilter,
167 filterType: 'date'
168 };
169 };
170 DateFilter.prototype.filterValues = function (type) {
171 if (type === baseFilter_1.FilterConditionType.MAIN) {
172 if (!this.dateFromComponent)
173 return null;
174 return this.filter !== baseFilter_1.BaseFilter.IN_RANGE ?
175 this.dateFromComponent.getDate() :
176 [this.dateFromComponent.getDate(), this.dateToComponent.getDate()];
177 }
178 if (!this.dateFromConditionComponent)
179 return null;
180 return this.filterCondition !== baseFilter_1.BaseFilter.IN_RANGE ?
181 this.dateFromConditionComponent.getDate() :
182 [this.dateFromConditionComponent.getDate(), this.dateToConditionComponent.getDate()];
183 };
184 // not used by ag-Grid, but exposed as part of the filter API for the client if they want it
185 DateFilter.prototype.getDateFrom = function () {
186 return utils_1.Utils.serializeDateToYyyyMmDd(this.dateFromComponent.getDate(), "-");
187 };
188 // not used by ag-Grid, but exposed as part of the filter API for the client if they want it
189 DateFilter.prototype.getDateTo = function () {
190 return utils_1.Utils.serializeDateToYyyyMmDd(this.dateToComponent.getDate(), "-");
191 };
192 // not used by ag-Grid, but exposed as part of the filter API for the client if they want it
193 DateFilter.prototype.getFilterType = function () {
194 return this.filter;
195 };
196 DateFilter.prototype.setDateFrom = function (date, type) {
197 var parsedDate = utils_1.Utils.parseYyyyMmDdToDate(date, "-");
198 this.setDateFrom_date(parsedDate, type);
199 };
200 DateFilter.prototype.setDateFrom_date = function (parsedDate, type) {
201 if (type === baseFilter_1.FilterConditionType.MAIN) {
202 this.dateFrom = parsedDate;
203 if (!this.dateFromComponent)
204 return;
205 this.dateFromComponent.setDate(this.dateFrom);
206 }
207 else {
208 this.dateFromCondition = parsedDate;
209 if (!this.dateFromConditionComponent)
210 return;
211 this.dateFromConditionComponent.setDate(this.dateFromCondition);
212 }
213 };
214 DateFilter.prototype.setDateTo = function (date, type) {
215 var parsedDate = utils_1.Utils.parseYyyyMmDdToDate(date, "-");
216 this.setDateTo_date(parsedDate, type);
217 };
218 DateFilter.prototype.setDateTo_date = function (parsedDate, type) {
219 if (type === baseFilter_1.FilterConditionType.MAIN) {
220 this.dateTo = parsedDate;
221 if (!this.dateToComponent)
222 return;
223 this.dateToComponent.setDate(this.dateTo);
224 }
225 else {
226 this.dateToCondition = parsedDate;
227 if (!this.dateToConditionComponent)
228 return;
229 this.dateToConditionComponent.setDate(this.dateToCondition);
230 }
231 };
232 DateFilter.prototype.resetState = function () {
233 this.setDateFrom(null, baseFilter_1.FilterConditionType.MAIN);
234 this.setDateTo(null, baseFilter_1.FilterConditionType.MAIN);
235 this.setFilterType(this.defaultFilter, baseFilter_1.FilterConditionType.MAIN);
236 this.setDateFrom(null, baseFilter_1.FilterConditionType.CONDITION);
237 this.setDateTo(null, baseFilter_1.FilterConditionType.CONDITION);
238 this.setFilterType(this.defaultFilter, baseFilter_1.FilterConditionType.MAIN);
239 };
240 DateFilter.prototype.parse = function (model, type) {
241 this.setDateFrom(model.dateFrom, type);
242 this.setDateTo(model.dateTo, type);
243 this.setFilterType(model.type, type);
244 };
245 DateFilter.prototype.setType = function (filterType, type) {
246 this.setFilterType(filterType, type);
247 };
248 DateFilter.removeTimezone = function (from) {
249 if (!from) {
250 return null;
251 }
252 return new Date(from.getFullYear(), from.getMonth(), from.getDate());
253 };
254 __decorate([
255 context_1.Autowired('componentRecipes'),
256 __metadata("design:type", componentRecipes_1.ComponentRecipes)
257 ], DateFilter.prototype, "componentRecipes", void 0);
258 __decorate([
259 componentAnnotations_1.QuerySelector('#filterDateFromPanel'),
260 __metadata("design:type", HTMLElement)
261 ], DateFilter.prototype, "eDateFromPanel", void 0);
262 __decorate([
263 componentAnnotations_1.QuerySelector('#filterDateFromConditionPanel'),
264 __metadata("design:type", HTMLElement)
265 ], DateFilter.prototype, "eDateFromConditionPanel", void 0);
266 __decorate([
267 componentAnnotations_1.QuerySelector('#filterDateToPanel'),
268 __metadata("design:type", HTMLElement)
269 ], DateFilter.prototype, "eDateToPanel", void 0);
270 __decorate([
271 componentAnnotations_1.QuerySelector('#filterDateToConditionPanel'),
272 __metadata("design:type", HTMLElement)
273 ], DateFilter.prototype, "eDateToConditionPanel", void 0);
274 return DateFilter;
275}(baseFilter_1.ScalarBaseFilter));
276exports.DateFilter = DateFilter;
277var DefaultDateComponent = (function (_super) {
278 __extends(DefaultDateComponent, _super);
279 function DefaultDateComponent() {
280 return _super.call(this, "<input class=\"ag-filter-filter\" type=\"text\" placeholder=\"yyyy-mm-dd\">") || this;
281 }
282 DefaultDateComponent.prototype.init = function (params) {
283 this.eDateInput = this.getGui();
284 if (utils_1.Utils.isBrowserChrome() || params.filterParams.browserDatePicker) {
285 if (utils_1.Utils.isBrowserIE()) {
286 console.warn('ag-grid: browserDatePicker is specified to true, but it is not supported in IE 11, reverting to plain text date picker');
287 }
288 else {
289 this.eDateInput.type = 'date';
290 }
291 }
292 this.listener = params.onDateChanged;
293 this.addGuiEventListener('input', this.listener);
294 };
295 DefaultDateComponent.prototype.getDate = function () {
296 return utils_1.Utils.parseYyyyMmDdToDate(this.eDateInput.value, "-");
297 };
298 DefaultDateComponent.prototype.setDate = function (date) {
299 this.eDateInput.value = utils_1.Utils.serializeDateToYyyyMmDd(date, "-");
300 };
301 return DefaultDateComponent;
302}(component_1.Component));
303exports.DefaultDateComponent = DefaultDateComponent;