3 * Copyright(c) 2006-2010 Sencha Inc.
5 * http://www.sencha.com/license
8 * @class Ext.calendar.DateRangeField
9 * @extends Ext.form.Field
10 * <p>A combination field that includes start and end dates and times, as well as an optional all-day checkbox.</p>
12 * @param {Object} config The config object
14 Ext.calendar.DateRangeField = Ext.extend(Ext.form.Field, {
16 * @cfg {String} toText
17 * The text to display in between the date/time fields (defaults to 'to')
21 * @cfg {String} toText
22 * The text to display as the label for the all day checkbox (defaults to 'All day')
24 allDayText: 'All day',
27 onRender: function(ct, position) {
29 this.startDate = new Ext.form.DateField({
30 id: this.id + '-start-date',
36 this.checkDates('date', 'start');
42 this.startTime = new Ext.form.TimeField({
43 id: this.id + '-start-time',
44 hidden: this.showTimes === false,
51 this.checkDates('time', 'start');
57 this.endTime = new Ext.form.TimeField({
58 id: this.id + '-end-time',
59 hidden: this.showTimes === false,
66 this.checkDates('time', 'end');
72 this.endDate = new Ext.form.DateField({
73 id: this.id + '-end-date',
80 this.checkDates('date', 'end');
86 this.allDay = new Ext.form.Checkbox({
87 id: this.id + '-allday',
88 hidden: this.showTimes === false || this.showAllDay === false,
89 boxLabel: this.allDayText,
90 handler: function(chk, checked) {
91 this.startTime.setVisible(!checked);
92 this.endTime.setVisible(!checked);
96 this.toLabel = new Ext.form.Label({
98 id: this.id + '-to-label',
102 this.fieldCt = new Ext.Container({
106 //make sure the container el has the field's id
126 this.fieldCt.ownerCt = this;
127 this.el = this.fieldCt.getEl();
128 this.items = new Ext.util.MixedCollection();
129 this.items.addAll([this.startDate, this.endDate, this.toLabel, this.startTime, this.endTime, this.allDay]);
131 Ext.calendar.DateRangeField.superclass.onRender.call(this, ct, position);
135 checkDates: function(type, startend) {
136 var startField = Ext.getCmp(this.id + '-start-' + type),
137 endField = Ext.getCmp(this.id + '-end-' + type),
138 startValue = this.getDT('start'),
139 endValue = this.getDT('end');
141 if (startValue > endValue) {
142 if (startend == 'start') {
143 endField.setValue(startValue);
145 startField.setValue(endValue);
146 this.checkDates(type, 'start');
149 if (type == 'date') {
150 this.checkDates('time', startend);
155 * Returns an array containing the following values in order:<div class="mdetail-params"><ul>
156 * <li><b><code>DateTime</code></b> : <div class="sub-desc">The start date/time</div></li>
157 * <li><b><code>DateTime</code></b> : <div class="sub-desc">The end date/time</div></li>
158 * <li><b><code>Boolean</code></b> : <div class="sub-desc">True if the dates are all-day, false
159 * if the time values should be used</div></li><ul></div>
160 * @return {Array} The array of return values
162 getValue: function() {
166 this.allDay.getValue()
170 // private getValue helper
171 getDT: function(startend) {
172 var time = this[startend + 'Time'].getValue(),
173 dt = this[startend + 'Date'].getValue();
175 if (Ext.isDate(dt)) {
176 dt = dt.format(this[startend + 'Date'].format);
181 if (time != '' && this[startend + 'Time'].isVisible()) {
182 return Date.parseDate(dt + ' ' + time, this[startend + 'Date'].format + ' ' + this[startend + 'Time'].format);
184 return Date.parseDate(dt, this[startend + 'Date'].format);
189 * Sets the values to use in the date range.
190 * @param {Array/Date/Object} v The value(s) to set into the field. Valid types are as follows:<div class="mdetail-params"><ul>
191 * <li><b><code>Array</code></b> : <div class="sub-desc">An array containing, in order, a start date, end date and all-day flag.
192 * This array should exactly match the return type as specified by {@link #getValue}.</div></li>
193 * <li><b><code>DateTime</code></b> : <div class="sub-desc">A single Date object, which will be used for both the start and
194 * end dates in the range. The all-day flag will be defaulted to false.</div></li>
195 * <li><b><code>Object</code></b> : <div class="sub-desc">An object containing properties for StartDate, EndDate and IsAllDay
196 * as defined in {@link Ext.calendar.EventMappings}.</div></li><ul></div>
198 setValue: function(v) {
199 if (Ext.isArray(v)) {
200 this.setDT(v[0], 'start');
201 this.setDT(v[1], 'end');
202 this.allDay.setValue( !! v[2]);
204 else if (Ext.isDate(v)) {
205 this.setDT(v, 'start');
206 this.setDT(v, 'end');
207 this.allDay.setValue(false);
209 else if (v[Ext.calendar.EventMappings.StartDate.name]) {
211 this.setDT(v[Ext.calendar.EventMappings.StartDate.name], 'start');
212 if (!this.setDT(v[Ext.calendar.EventMappings.EndDate.name], 'end')) {
213 this.setDT(v[Ext.calendar.EventMappings.StartDate.name], 'end');
215 this.allDay.setValue( !! v[Ext.calendar.EventMappings.IsAllDay.name]);
219 // private setValue helper
220 setDT: function(dt, startend) {
221 if (dt && Ext.isDate(dt)) {
222 this[startend + 'Date'].setValue(dt);
223 this[startend + 'Time'].setValue(dt.format(this[startend + 'Time'].format));
229 isDirty: function() {
231 if (this.rendered && !this.disabled) {
232 this.items.each(function(item) {
233 if (item.isDirty()) {
243 onDisable: function() {
244 this.delegateFn('disable');
248 onEnable: function() {
249 this.delegateFn('enable');
254 this.delegateFn('reset');
258 delegateFn: function(fn) {
259 this.items.each(function(item) {
267 beforeDestroy: function() {
268 Ext.destroy(this.fieldCt);
269 Ext.calendar.DateRangeField.superclass.beforeDestroy.call(this);
273 * @method getRawValue
276 getRawValue: Ext.emptyFn,
278 * @method setRawValue
281 setRawValue: Ext.emptyFn
284 Ext.reg('daterangefield', Ext.calendar.DateRangeField);