DateRange.js
13 KB
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
/**
* @class Ext.form.field.DateRange
* @extends Ext.form.Field
* <p>A combination field that includes start and end dates and times, as well as an optional all-day checkbox.</p>
* @constructor
* @param {Object} config The config object
*/
Ext.define('Ext.calendar.form.field.DateRange', {
extend: 'Ext.form.FieldContainer',
alias: 'widget.daterangefield',
requires: [
'Ext.form.field.Date',
'Ext.form.field.Time',
'Ext.form.Label',
'Ext.form.field.Checkbox',
'Ext.layout.container.Column'
],
/**
* @cfg {String} toText
* The text to display in between the date/time fields (defaults to 'to')
*/
toText: 'to',
/**
* @cfg {String} allDayText
* The text to display as the label for the all day checkbox (defaults to 'All day')
*/
allDayText: 'All day',
/**
* @cfg {String/Boolean} singleLine
* `true` to render the fields all on one line, `false` to break the start date/time and end date/time
* into two stacked rows of fields to preserve horizontal space (defaults to `true`).
*/
singleLine: true,
/**
* @cfg {String} dateFormat
* The date display format used by the date fields (defaults to 'n/j/Y')
*/
dateFormat: 'n/j/Y',
/**
* @cfg {String} timeFormat
* The time display format used by the time fields. By default the DateRange uses the
* {@link Ext.Date.use24HourTime} setting and sets the format to 'g:i A' for 12-hour time (e.g., 1:30 PM)
* or 'G:i' for 24-hour time (e.g., 13:30). This can also be overridden by a static format string if desired.
*/
timeFormat: Ext.Date.use24HourTime ? 'G:i' : 'g:i A',
// private
fieldLayout: {
type: 'hbox',
defaultMargins: { top: 0, right: 5, bottom: 0, left: 0 }
},
// private
initComponent: function() {
var me = this;
me.addCls('ext-dt-range');
if (me.singleLine) {
me.layout = me.fieldLayout;
me.items = me.getFieldConfigs();
}
else {
me.items = [{
xtype: 'container',
layout: me.fieldLayout,
items: [
me.getStartDateConfig(),
me.getStartTimeConfig(),
me.getDateSeparatorConfig()
]
},{
xtype: 'container',
layout: me.fieldLayout,
items: [
me.getEndDateConfig(),
me.getEndTimeConfig(),
me.getAllDayConfig()
]
}];
}
me.callParent(arguments);
me.initRefs();
},
initRefs: function() {
var me = this;
me.startDate = me.down('#' + me.id + '-start-date');
me.startTime = me.down('#' + me.id + '-start-time');
me.endTime = me.down('#' + me.id + '-end-time');
me.endDate = me.down('#' + me.id + '-end-date');
me.allDay = me.down('#' + me.id + '-allday');
me.toLabel = me.down('#' + me.id + '-to-label');
me.startDate.validateOnChange = me.endDate.validateOnChange = false,
me.startDate.isValid = me.endDate.isValid = function() {
var me = this,
valid = Ext.isDate(me.getValue());
if (!valid) {
me.focus();
}
return valid;
};
},
getFieldConfigs: function() {
var me = this;
return [
me.getStartDateConfig(),
me.getStartTimeConfig(),
me.getDateSeparatorConfig(),
me.getEndTimeConfig(),
me.getEndDateConfig(),
me.getAllDayConfig()
];
},
getStartDateConfig: function() {
return {
xtype: 'datefield',
itemId: this.id + '-start-date',
format: this.dateFormat,
width: 100,
listeners: {
'blur': {
fn: function(){
this.onFieldChange('date', 'start');
},
scope: this
}
}
};
},
getStartTimeConfig: function() {
return {
xtype: 'timefield',
itemId: this.id + '-start-time',
hidden: this.showTimes === false,
labelWidth: 0,
hideLabel: true,
width: 90,
format: this.timeFormat,
listeners: {
'select': {
fn: function(){
this.onFieldChange('time', 'start');
},
scope: this
}
}
};
},
getEndDateConfig: function() {
return {
xtype: 'datefield',
itemId: this.id + '-end-date',
format: this.dateFormat,
hideLabel: true,
width: 100,
listeners: {
'blur': {
fn: function(){
this.onFieldChange('date', 'end');
},
scope: this
}
}
};
},
getEndTimeConfig: function() {
return {
xtype: 'timefield',
itemId: this.id + '-end-time',
hidden: this.showTimes === false,
labelWidth: 0,
hideLabel: true,
width: 90,
format: this.timeFormat,
listeners: {
'select': {
fn: function(){
this.onFieldChange('time', 'end');
},
scope: this
}
}
};
},
getDuration: function() {
var me = this,
start = me.getDT('start'),
end = me.getDT('end');
return end.getTime() - start.getTime();
},
getAllDayConfig: function() {
return {
xtype: 'checkbox',
itemId: this.id + '-allday',
hidden: this.showTimes === false || this.showAllDay === false,
boxLabel: this.allDayText,
margins: { top: 2, right: 5, bottom: 0, left: 0 },
handler: this.onAllDayChange,
scope: this
};
},
onAllDayChange: function(chk, checked) {
this.startTime.setVisible(!checked);
this.endTime.setVisible(!checked);
},
getDateSeparatorConfig: function() {
return {
xtype: 'label',
itemId: this.id + '-to-label',
text: this.toText,
margins: { top: 4, right: 5, bottom: 0, left: 0 }
};
},
isSingleLine: function() {
var me = this;
if (me.calculatedSingleLine === undefined) {
if(me.singleLine == 'auto'){
var ownerCtEl = me.ownerCt.getEl(),
w = me.ownerCt.getWidth() - ownerCtEl.getPadding('lr'),
el = ownerCtEl.down('.x-panel-body');
if(el){
w -= el.getPadding('lr');
}
el = ownerCtEl.down('.x-form-item-label')
if(el){
w -= el.getWidth() - el.getPadding('lr');
}
me.calculatedSingleLine = w <= me.singleLineMinWidth ? false : true;
}
else {
me.calculatedSingleLine = me.singleLine !== undefined ? me.singleLine : true;
}
}
return me.calculatedSingleLine;
},
// private
onFieldChange: function(type, startend){
this.checkDates(type, startend);
this.fireEvent('change', this, this.getValue());
},
// private
checkDates: function(type, startend){
var me = this,
startField = me.down('#' + me.id + '-start-' + type),
endField = me.down('#' + me.id + '-end-' + type),
startValue = me.getDT('start'),
endValue = me.getDT('end');
if (!startValue || !endValue) {
return;
}
if(startValue > endValue){
if(startend=='start'){
endField.setValue(startValue);
}else{
startField.setValue(endValue);
me.checkDates(type, 'start');
}
}
if(type=='date'){
me.checkDates('time', startend);
}
},
/**
* Returns an array containing the following values in order:<div class="mdetail-params"><ul>
* <li><b><code>DateTime</code></b> : <div class="sub-desc">The start date/time</div></li>
* <li><b><code>DateTime</code></b> : <div class="sub-desc">The end date/time</div></li>
* <li><b><code>Boolean</code></b> : <div class="sub-desc">True if the dates are all-day, false
* if the time values should be used</div></li><ul></div>
* @return {Array} The array of return values
*/
getValue: function(){
return [
this.getDT('start'),
this.getDT('end'),
this.allDay.getValue()
];
},
// private getValue helper
getDT: function(startend){
var time = this[startend+'Time'].getValue(),
dt = this[startend+'Date'].getValue();
if(Ext.isDate(dt)){
dt = Ext.Date.format(dt, this[startend + 'Date'].format);
}
else{
return null;
};
if(time && time != ''){
time = Ext.Date.format(time, this[startend+'Time'].format);
var val = Ext.Date.parseDate(dt + ' ' + time, this[startend+'Date'].format + ' ' + this[startend+'Time'].format);
return val;
//return Ext.Date.parseDate(dt+' '+time, this[startend+'Date'].format+' '+this[startend+'Time'].format);
}
return Ext.Date.parseDate(dt, this[startend+'Date'].format);
},
/**
* Sets the values to use in the date range.
* @param {Array/Date/Object} v The value(s) to set into the field. Valid types are as follows:<div class="mdetail-params"><ul>
* <li><b><code>Array</code></b> : <div class="sub-desc">An array containing, in order, a start date, end date and all-day flag.
* This array should exactly match the return type as specified by {@link #getValue}.</div></li>
* <li><b><code>DateTime</code></b> : <div class="sub-desc">A single Date object, which will be used for both the start and
* end dates in the range. The all-day flag will be defaulted to false.</div></li>
* <li><b><code>Object</code></b> : <div class="sub-desc">An object containing properties for StartDate, EndDate and IsAllDay
* as defined in {@link Ext.calendar.data.EventMappings}.</div></li><ul></div>
*/
setValue: function(v){
if(!v) {
return;
}
if(Ext.isArray(v)){
this.setDT(v[0], 'start');
this.setDT(v[1], 'end');
this.allDay.setValue(!!v[2]);
}
else if(Ext.isDate(v)){
this.setDT(v, 'start');
this.setDT(v, 'end');
this.allDay.setValue(false);
}
else if(v[Ext.calendar.data.EventMappings.StartDate.name]){ //object
this.setDT(v[Ext.calendar.data.EventMappings.StartDate.name], 'start');
if(!this.setDT(v[Ext.calendar.data.EventMappings.EndDate.name], 'end')){
this.setDT(v[Ext.calendar.data.EventMappings.StartDate.name], 'end');
}
this.allDay.setValue(!!v[Ext.calendar.data.EventMappings.IsAllDay.name]);
}
},
// private setValue helper
setDT: function(dt, startend){
if(dt && Ext.isDate(dt)){
this[startend + 'Date'].setValue(dt);
this[startend + 'Time'].setValue(Ext.Date.format(dt, this[startend + 'Time'].format));
return true;
}
},
// inherited docs
isDirty: function(){
var dirty = false;
if(this.rendered && !this.disabled) {
this.items.each(function(item){
if (item.isDirty()) {
dirty = true;
return false;
}
});
}
return dirty;
},
// private
onDisable : function(){
this.delegateFn('disable');
},
// private
onEnable : function(){
this.delegateFn('enable');
},
// inherited docs
reset : function(){
this.delegateFn('reset');
},
// private
delegateFn : function(fn){
this.items.each(function(item){
if (item[fn]) {
item[fn]();
}
});
},
// private
beforeDestroy: function(){
Ext.destroy(this.fieldCt);
this.callParent(arguments);
},
/**
* @method getRawValue
* @hide
*/
getRawValue : Ext.emptyFn,
/**
* @method setRawValue
* @hide
*/
setRawValue : Ext.emptyFn
});