-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjCal-picker.js
More file actions
190 lines (160 loc) · 7.13 KB
/
jCal-picker.js
File metadata and controls
190 lines (160 loc) · 7.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
function renderCal ( target, cal ) {
// get day to start jCal on
var startCalDate = currDate,
arrDate = $('#arriveCal').data('day'),
depDate = $('#departCal').data('day');
if ( advDate.getTime() > startCalDate.getTime() )
startCalDate = advDate;
if ( arrDate && arrDate.getTime() > startCalDate.getTime() )
startCalDate = arrDate;
/* XXX - we will always focus on the arrival date
if ( depDate && cal == 'departCal' && depDate.getTime() > startCalDate.getTime() )
startCalDate = depDate;
*/
// load the 2mo calendar
$('#' + cal).jCal({
day: startCalDate,
days: 1,
showMonths: 2,
monthSelect: false,
scrollSpeed: 120,
drawBack: function () {
// remove any highlighted text
if ( document.selection && typeof(document.selection.empty) != 'undefined' )
document.selection.empty();
else if ( typeof(window.getSelection) === 'function' && typeof(window.getSelection().removeAllRanges) === 'function' )
window.getSelection().removeAllRanges();
// ensure clicking on the jCal parent or children it will not throw the inputs blur event
$(this._target).unbind('click').bind('click', this._target,
function (e) {
$(e.data).stop();
$(target).focus();
}
);
// select arrival date
if ( arrDate && cal == 'departCal' )
$('#' + cal + ' [id*=' + ( arrDate.getMonth() + 1 ) + '_' + arrDate.getDate() + '_' + arrDate.getFullYear() + ']')
.addClass('selectedDay');
// select days in between
if ( arrDate && depDate ) {
var sDate = new Date ( arrDate.getTime() );
for ( var di = 0, ds = Math.round( ( depDate.getTime() - arrDate.getTime() ) / 86400000 ); di < ds; di++ ) {
var currDay = $('#' + cal + ' [id*=' + ( sDate.getMonth() + 1 ) + '_' + sDate.getDate() + '_' + sDate.getFullYear() + ']');
if ( ! $(currDay).hasClass('selectedDay') )
$(currDay).stop().addClass('selectedDay');
sDate.setDate( sDate.getDate() + 1 );
}
}
// select departure date
if ( depDate && cal == 'arriveCal' )
$('#' + cal + ' [id*=' + ( depDate.getMonth() + 1 ) + '_' + depDate.getDate() + '_' + depDate.getFullYear() + ']')
.addClass('selectedDay');
// draw focus back to the input
$(target).focus();
},
dCheck: function (day) {
if ( day.getTime() < (advDate).getTime() ) // 3day advance
return 'invday';
else
return 'day';
/* XXX - no need to shade those outside
return 'day' +
( ( arrDate && cal == 'departCal' && day.getTime() < arrDate.getTime() )
? ' outDay' : '' ) +
( ( depDate && cal == 'arriveCal' && day.getTime() > depDate.getTime() )
? ' outDay' : '' );
*/
},
callback: function ( day, days, clickedDay ) {
$(target).val( ( day.getMonth() + 1 ) + '/' + day.getDate() + '/' + day.getFullYear() );
// show warning for selecting days outside of itinerary range
/* XXX - no need to fire out of range events
if ( clickedDay && ( $(clickedDay).hasClass('outDay') || $(clickedDay).nextAll('div:lt(' + ( days - 1 ) + ')').hasClass('outDay') ) )
alert('outside of range');
*/
if ( typeof $(this._target).data('day') == 'object' &&
$(this._target).data('day').getTime() == day.getTime() &&
$(this._target).data('days') == days )
return false;
// clear if arrival > departure || departure < arrival
if ( cal == 'arriveCal' && $('#departCal').data('day') && day.getTime() > $('#departCal').data('day').getTime() ) {
$('#depDate').val('');
$('#departCal').data('day','');
}
if ( cal == 'departCal' && $('#arriveCal').data('day') && day.getTime() < $('#arriveCal').data('day').getTime() ) {
$('#arrDate').val('');
$('#arriveCal').data('day','');
}
// hide with valid new day onclick
$(this._target).empty().css('display','none').removeShadow();
return true;
}
});
// hilights from departure date to arrival
if ( cal == 'departCal' && arrDate )
$('#departCal .day').bind('mouseover mouseout click', {_target:this._target,otherDate:arrDate},
function (e) {
var osDate = new Date ( $(this).attr('id').replace(/c[0-9]{1,}d_([0-9]{1,2})_([0-9]{1,2})_([0-9]{4})/, '$1/$2/$3') );
var sDate = new Date ( osDate.getTime() );
for ( var di = Math.round( ( osDate.getTime() - arrDate.getTime() ) / 86400000 ); di >= 0; di-- ) {
var currDay = $(e.data._target).find('[id*=' + ( sDate.getMonth() + 1 ) + '_' + sDate.getDate() + '_' + sDate.getFullYear() + ']');
if ( currDay.length == 0 || $(currDay).hasClass('invday') ) break;
if ( e.type == 'mouseover' ) $(currDay).addClass('overDay');
else if ( e.type == 'mouseout' ) $(currDay).stop().removeClass('overDay');
else if ( e.type == 'click' ) $(currDay).stop().addClass('selectedDay');
sDate.setDate( sDate.getDate() - 1 );
}
}
);
// hilights arrival to set departure date
if ( cal == 'arriveCal' && depDate )
$('#arriveCal .day').bind('mouseover mouseout click', {_target:this._target,otherDate:depDate},
function (e) {
var osDate = new Date ( $(this).attr('id').replace(/c[0-9]{1,}d_([0-9]{1,2})_([0-9]{1,2})_([0-9]{4})/, '$1/$2/$3') );
var sDate = new Date ( osDate.getTime() );
for ( var di = 0, ds = Math.round( ( depDate.getTime() - osDate.getTime() ) / 86400000 ); di <= ds; di++ ) {
var currDay = $(e.data._target).find('[id*=' + ( sDate.getMonth() + 1 ) + '_' + sDate.getDate() + '_' + sDate.getFullYear() + ']');
if ( currDay.length == 0 || $(currDay).hasClass('invday') ) break;
if ( e.type == 'mouseover' ) $(currDay).addClass('overDay');
else if ( e.type == 'mouseout' ) $(currDay).stop().removeClass('overDay');
else if ( e.type == 'click' ) $(currDay).stop().addClass('selectedDay');
sDate.setDate( sDate.getDate() + 1 );
}
}
);
// this is the global blur to hide the current cal
$(target).unbind('blur').bind('blur', {cal:cal},
function ( e ) {
if ( $('#' + e.data.cal + ' .jCalMove').length == 0 ) {
$('#' + e.data.cal).animate({'opacity':1}, 200,
function () {
$('#' + e.data.cal).css('display','none');
$('#' + e.data.cal).empty().removeShadow();
}
);
}
}
);
}
function showCalPicker ( target, cal ) {
if ( $('#' + cal).css('display') == 'none' ) {
// show cal
renderCal(target, cal);
// calculate top positioning
var pickerTop = $(target).position().top + $(target).height() + 8;
// stop previous animations + calculate positioning + show
$('#' + cal).stop().css({left:$(target).position().left + 'px',top:pickerTop + 'px',display:'block'}).dropShadow({color:'#808080'});
// make sure it's always on top
$('#' + cal).css( 'z-index', ( ( parseInt( $(target).css('z-index') ) || 1110 ) + 10 ) );
}
$(document).keyup(function(e){
if (e.keyCode == 27) {
$('#arriveCal, #departCal').css('display','none');
$('#arriveCal, #departCal').empty().removeShadow();
}
});
// ensure focus is on the input
$(target).focus();
// scrollTo - ensure you have the jQuery.ScrollTo plugin installed from http://flesler.blogspot.com/
$.scrollTo( $( '#' + cal ), 500 );
}