-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdate-range-picker.js
278 lines (238 loc) · 9.91 KB
/
date-range-picker.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/* eslint-disable ember/jquery-ember-run */
/* eslint-disable ember/no-jquery */
import { action } from '@ember/object';
import { run } from '@ember/runloop';
import { isEmpty } from '@ember/utils';
import Component from '@glimmer/component';
import { tracked } from '@glimmer/tracking';
import $ from 'jquery';
import moment from 'moment';
import layout from './date-range-picker';
const noop = function () {};
export default class DateRangePicker extends Component {
//TODO: get all options from attr.
@tracked layout = layout;
@tracked classNameBindings = ['containerClass', 'au-c-content '];
@tracked attributeBindings = ['start', 'end', 'serverFormat'];
/* OPTIONS */
// Options: Format
@tracked format = this.args.format || 'D MMM, YYYY';
@tracked serverFormat = this.args.serverFormat || 'YYYY-MM-DD';
@tracked direction = this.args.direction || 'ltr'; // Locale setting for text direction
@tracked regional = this.args.regional || 'be-nl'; // Selected region
@tracked separator = this.args.seperator || ' - '; // What character(s) to put between range
// Options: Range
@tracked singleDatePicker = this.args.singleDatePicker || false; // Whether to only select a single date (instead of the default two)
@tracked minDate = this.args.minDate || undefined; // Lowest possible date. If undefined, no limit is set
@tracked maxDate = this.args.maxDate || undefined; // Highest possible date. If undefined, no limit is set
@tracked datelimit = this.args.datelimit || false;
@tracked ranges = this.args.ranges || { // This is not picked up by docs.mjs, and is documented seperately in README.md
Vandaag: [moment().startOf('day'), moment().endOf('day')],
Gisteren: [
moment().subtract(1, 'days').startOf('day'),
moment().subtract(1, 'days').endOf('day'),
],
'Voorbije Week': [moment().subtract(7, 'days'), moment()],
'Voorbije 30 Dagen': [moment().subtract(30, 'days'), moment()],
'Deze Maand': [moment().startOf('month'), moment().endOf('month')],
'Vorige Maand': [
moment().subtract(1, 'month').startOf('month'),
moment().subtract(1, 'month').endOf('month'),
],
};
// Options: Appearance
@tracked firstDay = this.args.firstDay || 0; // What day of the week to start with. 0 = sunday, 1 = monday...
@tracked placeholder = this.args.placeholder || 'Aangepast bereik';
@tracked daysOfWeek = this.args.daysOfWeek || moment.weekdaysMin();
@tracked monthNames = this.args.monthNames || moment.monthsShort();
@tracked cancelLabel = this.args.cancelLabel || 'Terug'; // Text on the cancel button
@tracked applyLabel = this.args.applyLabel || 'Verder'; // Text on the apply button
@tracked customRangeLabel = this.args.customRangeLabel || 'Aangepast bereik';
@tracked showCustomRangeLabel = this.args.showCustomRangeLabel || false;
@tracked fromLabel = this.args.fromLabel || 'Van';
@tracked toLabel = this.args.toLabel || 'Tot';
@tracked showWeekNumbers = this.args.showWeekNumbers || false;
@tracked showDropdowns = this.args.showDropdowns || false;
// Options: Classes
@tracked containerClass = this.args.containerClass || 'form-group au-c-content '; // Class for the container
@tracked inputClass = this.args.inputClass || 'form-control'; // Class for the input
@tracked buttonClasses = this.args.buttonClasses || ['au-c-button']; // Classes for the buttons
@tracked applyClass = this.args.applyClass || 'au-c-button--primary'; // Class for the apply button
@tracked cancelClass = this.args.cancelClass || 'au-c-button--secondary au-u-margin-right-tiny'; // Class for the cancel button
@tracked labelClass = this.args.labelClass || 'au-u-h5'; // Class for all labels/buttons
// Options: TimePicker
@tracked timePicker = this.args.timePicker || false; // Whether to enable the timePicker
@tracked timePicker24Hour = this.args.timePicker24Hour || false;
@tracked timePickerSeconds = this.args.timePickerSeconds || false;
@tracked timePickerIncrement = this.args.timePickerIncrement || undefined;
// Options: Dom behaviour
@tracked autoUpdateInput = this.args.autoUpdateInput || true;
@tracked autoApply = this.args.autoApply || false;
@tracked alwaysShowCalendars = this.args.alwaysShowCalendars || true;
@tracked context = this.args.context || undefined;
@tracked removeDropdownOnDestroy = this.args.removeDropdownOnDestroy || false;
@tracked parentEl = this.args.parentEl || 'body';
// Options: Other
@tracked isInvalidDate = this.args.isInvalidDate || noop;
@tracked isCustomDate = this.args.isCustomDate || noop;
@tracked opens = this.args.opens || null;
@tracked drops = this.args.drops || null;
@tracked linkedCalendars = this.args.linkedCalendars || false;
/* GETTERS */
get inputClasses() {
return this.inputClass
? 'daterangepicker-input ember-text-field ember-view' + this.inputClass
: 'daterangepicker-input ember-text-field ember-view';
}
get start() {
return this.args.start || undefined;
}
get end() {
return this.args.end || undefined;
}
get width() {
if (this.args.width == 'block') return 'au-c-input--block';
else return '';
}
get error() {
if (this.args.error) return 'au-c-input--error';
else return '';
}
get warning() {
if (this.args.warning) return 'au-c-input--warning';
else return '';
}
get disabled() {
if (this.args.disabled) return 'is-disabled';
else return '';
}
get rangeText() {
if (!isEmpty(this.start) && !isEmpty(this.end)) {
return (
moment(this.start, this.serverFormat).format(this.format) +
this.separator +
moment(this.end, this.serverFormat).format(this.format)
);
}
return '';
}
/* WRAPPER CODE: all the code that is meant to interact with the wrapper date-range-picker */
@action didInsertDateInputPicker() {
console.log(this);
$(this).on('cancel', function () {
console.log('meowe');
});
// Init the dropdown when the component is added to the DOM
// daterangepicker(this.getOptions());
$('.daterangepicker-input').daterangepicker(this.getOptions());
this.attachPickerEvents();
}
willDestroy() {
// Remove the hidden dropdown when this component is destroyed
super.willDestroy(...arguments);
super.willDestroy(...arguments);
run.cancel(this._setupTimer);
if (this.removeDropdownOnDestroy) {
$('.daterangepicker').remove();
}
}
@action getOptions() {
// get options to pass to the internal date-range-picker
let momentStartDate = moment(this.start, this.serverFormat);
let momentEndDate = moment(this.end, this.serverFormat);
let startDate = momentStartDate.isValid() ? momentStartDate : undefined;
let endDate = momentEndDate.isValid() ? momentEndDate : undefined;
let momentMinDate = moment(this.minDate, this.serverFormat);
let momentMaxDate = moment(this.maxDate, this.serverFormat);
let minDate = momentMinDate.isValid() ? momentMinDate : undefined;
let maxDate = momentMaxDate.isValid() ? momentMaxDate : undefined;
let showCustomRangeLabel = this.showCustomRangeLabel;
let options = {
isInvalidDate: this.isInvalidDate,
isCustomDate: this.isCustomDate,
alwaysShowCalendars: this.alwaysShowCalendars,
autoUpdateInput: this.autoUpdateInput,
autoApply: this.autoApply,
timePicker: this.timePicker,
buttonClasses: this.buttonClasses,
applyClass: this.applyClass,
cancelClass: this.cancelClass,
singleDatePicker: this.singleDatePicker,
drops: this.drops,
opens: this.opens,
timePicker24Hour: this.timePicker24Hour,
timePickerSeconds: this.timePickerSeconds,
timePickerIncrement: this.timePickerIncrement,
showWeekNumbers: this.showWeekNumbers,
showDropdowns: this.showDropdowns,
showCustomRangeLabel: this.showCustomRangeLabel,
linkedCalendars: this.linkedCalendars,
dateLimit: this.dateLimit,
parentEl: this.parentEl,
};
let localeOptions = {
applyLabel: this.applyLabel,
cancelLabel: this.cancelLabel,
customRangeLabel: this.customRangeLabel,
daysOfWeek: this.daysOfWeek,
regional: this.regional,
direction: this.direction,
firstDay: this.firstDay,
format: this.format,
fromLabel: this.fromLabel,
monthNames: this.monthNames,
separator: this.separator,
toLabel: this.toLabel,
};
const defaultOptions = {
locale: localeOptions,
showCustomRangeLabel: showCustomRangeLabel,
startDate: startDate,
endDate: endDate,
minDate: minDate,
maxDate: maxDate,
};
if (!this.singleDatePicker) {
options.ranges = this.ranges;
}
return { ...options, ...defaultOptions };
}
/* ACTIONS: all the code to run events from au-date-range-picker */
@action hideAction() {
// console.log(...this.args);
return this.start;
}
@action applyAction(picker, start, end) {
console.log(picker);
console.log(start, end);
return start, end;
}
@action attachPickerEvents() {
$('.daterangepicker-input').on('apply.daterangepicker', (_ev, picker) => {
this.handleDateRangePickerEvent('applyAction', picker);
});
$('.daterangepicker-input').on('hide.daterangepicker', (_ev, picker) => {
this.handleDateRangePickerEvent('hideAction', picker);
});
$('.daterangepicker-input').on('cancel.daterangepicker', (_ev, picker) => {
this.handleDateRangePickerEvent('cancelAction', picker);
});
}
@action handleDateRangePickerEvent(actionName, picker) {
let action = this.args[actionName];
let start = picker.startDate.format(this.serverFormat);
let end = picker.endDate.format(this.serverFormat);
if (action) {
// assert(
// `${actionName} for date-range-picker must be a function`,
// typeof action === 'function'
// );
action(this, start, end);
} else {
if (!this.isDestroyed) {
this.start = start;
this.end = end;
}
}
}
}