| 1 |
lars |
1 |
Prado.WebUI.TDatePicker = Class.create();
|
|
|
2 |
Object.extend(Prado.WebUI.TDatePicker,
|
|
|
3 |
{
|
|
|
4 |
/**
|
|
|
5 |
* @return Date the date from drop down list options.
|
|
|
6 |
*/
|
|
|
7 |
getDropDownDate : function(control)
|
|
|
8 |
{
|
|
|
9 |
var now=new Date();
|
|
|
10 |
var year=now.getFullYear();
|
|
|
11 |
var month=now.getMonth();
|
|
|
12 |
var day=1;
|
|
|
13 |
|
|
|
14 |
var month_list = this.getMonthListControl(control);
|
|
|
15 |
var day_list = this.getDayListControl(control);
|
|
|
16 |
var year_list = this.getYearListControl(control);
|
|
|
17 |
|
|
|
18 |
var day = day_list ? $F(day_list) : 1;
|
|
|
19 |
var month = month_list ? $F(month_list) : now.getMonth();
|
|
|
20 |
var year = year_list ? $F(year_list) : now.getFullYear();
|
|
|
21 |
|
|
|
22 |
return new Date(year,month,day, 0, 0, 0);
|
|
|
23 |
},
|
|
|
24 |
|
|
|
25 |
getYearListControl : function(control)
|
|
|
26 |
{
|
|
|
27 |
return $(control.id+"_year");
|
|
|
28 |
},
|
|
|
29 |
|
|
|
30 |
getMonthListControl : function(control)
|
|
|
31 |
{
|
|
|
32 |
return $(control.id+"_month");
|
|
|
33 |
},
|
|
|
34 |
|
|
|
35 |
getDayListControl : function(control)
|
|
|
36 |
{
|
|
|
37 |
return $(control.id+"_day");
|
|
|
38 |
}
|
|
|
39 |
});
|
|
|
40 |
|
|
|
41 |
Prado.WebUI.TDatePicker.prototype =
|
|
|
42 |
{
|
|
|
43 |
MonthNames : [ "January", "February", "March", "April",
|
|
|
44 |
"May", "June", "July", "August",
|
|
|
45 |
"September", "October", "November", "December"
|
|
|
46 |
],
|
|
|
47 |
AbbreviatedMonthNames : ["Jan", "Feb", "Mar", "Apr", "May",
|
|
|
48 |
"Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"],
|
|
|
49 |
|
|
|
50 |
ShortWeekDayNames : ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" ],
|
|
|
51 |
|
|
|
52 |
Format : "yyyy-MM-dd",
|
|
|
53 |
|
|
|
54 |
FirstDayOfWeek : 1, // 0 for sunday
|
|
|
55 |
|
|
|
56 |
ClassName : "",
|
|
|
57 |
|
|
|
58 |
CalendarStyle : "default",
|
|
|
59 |
|
|
|
60 |
FromYear : 2000, UpToYear: 2015,
|
|
|
61 |
|
|
|
62 |
initialize : function(options)
|
|
|
63 |
{
|
|
|
64 |
this.options = options || [];
|
|
|
65 |
this.control = $(options.ID);
|
|
|
66 |
this.dateSlot = new Array(42);
|
|
|
67 |
this.weekSlot = new Array(6);
|
|
|
68 |
this.minimalDaysInFirstWeek = 4;
|
|
|
69 |
this.selectedDate = this.newDate();
|
|
|
70 |
this.positionMode = 'Bottom';
|
|
|
71 |
|
|
|
72 |
//which element to trigger to show the calendar
|
|
|
73 |
if(this.options.Trigger)
|
|
|
74 |
{
|
|
|
75 |
this.trigger = $(this.options.Trigger) ;
|
|
|
76 |
var triggerEvent = this.options.TriggerEvent || "click";
|
|
|
77 |
}
|
|
|
78 |
else
|
|
|
79 |
{
|
|
|
80 |
this.trigger = this.control;
|
|
|
81 |
var triggerEvent = this.options.TriggerEvent || "focus";
|
|
|
82 |
}
|
|
|
83 |
|
|
|
84 |
// Popup position
|
|
|
85 |
if(this.options.PositionMode == 'Top')
|
|
|
86 |
{
|
|
|
87 |
this.positionMode = this.options.PositionMode;
|
|
|
88 |
}
|
|
|
89 |
|
|
|
90 |
Object.extend(this,options);
|
|
|
91 |
|
|
|
92 |
Event.observe(this.trigger, triggerEvent, this.show.bindEvent(this));
|
|
|
93 |
|
|
|
94 |
// Listen to change event if needed
|
|
|
95 |
if (typeof(this.options.OnDateChanged) == "function")
|
|
|
96 |
{
|
|
|
97 |
if(this.options.InputMode == "TextBox")
|
|
|
98 |
{
|
|
|
99 |
Event.observe(this.control, "change", this.onDateChanged.bindEvent(this));
|
|
|
100 |
}
|
|
|
101 |
else
|
|
|
102 |
{
|
|
|
103 |
var day = Prado.WebUI.TDatePicker.getDayListControl(this.control);
|
|
|
104 |
var month = Prado.WebUI.TDatePicker.getMonthListControl(this.control);
|
|
|
105 |
var year = Prado.WebUI.TDatePicker.getYearListControl(this.control);
|
|
|
106 |
Event.observe (day, "change", this.onDateChanged.bindEvent(this));
|
|
|
107 |
Event.observe (month, "change", this.onDateChanged.bindEvent(this));
|
|
|
108 |
Event.observe (year, "change", this.onDateChanged.bindEvent(this));
|
|
|
109 |
|
|
|
110 |
}
|
|
|
111 |
|
|
|
112 |
|
|
|
113 |
}
|
|
|
114 |
|
|
|
115 |
},
|
|
|
116 |
|
|
|
117 |
create : function()
|
|
|
118 |
{
|
|
|
119 |
if(typeof(this._calDiv) != "undefined")
|
|
|
120 |
return;
|
|
|
121 |
|
|
|
122 |
var div;
|
|
|
123 |
var table;
|
|
|
124 |
var tbody;
|
|
|
125 |
var tr;
|
|
|
126 |
var td;
|
|
|
127 |
|
|
|
128 |
// Create the top-level div element
|
|
|
129 |
this._calDiv = document.createElement("div");
|
|
|
130 |
this._calDiv.className = "TDatePicker_"+this.CalendarStyle+" "+this.ClassName;
|
|
|
131 |
this._calDiv.style.display = "none";
|
|
|
132 |
this._calDiv.style.position = "absolute"
|
|
|
133 |
|
|
|
134 |
// header div
|
|
|
135 |
div = document.createElement("div");
|
|
|
136 |
div.className = "calendarHeader";
|
|
|
137 |
this._calDiv.appendChild(div);
|
|
|
138 |
|
|
|
139 |
table = document.createElement("table");
|
|
|
140 |
table.style.cellSpacing = 0;
|
|
|
141 |
div.appendChild(table);
|
|
|
142 |
|
|
|
143 |
tbody = document.createElement("tbody");
|
|
|
144 |
table.appendChild(tbody);
|
|
|
145 |
|
|
|
146 |
tr = document.createElement("tr");
|
|
|
147 |
tbody.appendChild(tr);
|
|
|
148 |
|
|
|
149 |
// Previous Month Button
|
|
|
150 |
td = document.createElement("td");
|
|
|
151 |
var previousMonth = document.createElement("input");
|
|
|
152 |
previousMonth.className = "prevMonthButton button";
|
|
|
153 |
previousMonth.type = "button"
|
|
|
154 |
previousMonth.value = "<<";
|
|
|
155 |
td.appendChild(previousMonth);
|
|
|
156 |
tr.appendChild(td);
|
|
|
157 |
|
|
|
158 |
|
|
|
159 |
|
|
|
160 |
//
|
|
|
161 |
// Create the month drop down
|
|
|
162 |
//
|
|
|
163 |
td = document.createElement("td");
|
|
|
164 |
tr.appendChild(td);
|
|
|
165 |
this._monthSelect = document.createElement("select");
|
|
|
166 |
this._monthSelect.className = "months";
|
|
|
167 |
for (var i = 0 ; i < this.MonthNames.length ; i++) {
|
|
|
168 |
var opt = document.createElement("option");
|
|
|
169 |
opt.innerHTML = this.MonthNames[i];
|
|
|
170 |
opt.value = i;
|
|
|
171 |
if (i == this.selectedDate.getMonth()) {
|
|
|
172 |
opt.selected = true;
|
|
|
173 |
}
|
|
|
174 |
this._monthSelect.appendChild(opt);
|
|
|
175 |
}
|
|
|
176 |
td.appendChild(this._monthSelect);
|
|
|
177 |
|
|
|
178 |
|
|
|
179 |
//
|
|
|
180 |
// Create the year drop down
|
|
|
181 |
//
|
|
|
182 |
td = document.createElement("td");
|
|
|
183 |
td.className = "labelContainer";
|
|
|
184 |
tr.appendChild(td);
|
|
|
185 |
this._yearSelect = document.createElement("select");
|
|
|
186 |
for(var i=this.FromYear; i <= this.UpToYear; ++i) {
|
|
|
187 |
var opt = document.createElement("option");
|
|
|
188 |
opt.innerHTML = i;
|
|
|
189 |
opt.value = i;
|
|
|
190 |
if (i == this.selectedDate.getFullYear()) {
|
|
|
191 |
opt.selected = false;
|
|
|
192 |
}
|
|
|
193 |
this._yearSelect.appendChild(opt);
|
|
|
194 |
}
|
|
|
195 |
td.appendChild(this._yearSelect);
|
|
|
196 |
|
|
|
197 |
|
|
|
198 |
td = document.createElement("td");
|
|
|
199 |
var nextMonth = document.createElement("input");
|
|
|
200 |
nextMonth.className = "nextMonthButton button";
|
|
|
201 |
nextMonth.type = "button";
|
|
|
202 |
nextMonth.value = ">>";
|
|
|
203 |
td.appendChild(nextMonth);
|
|
|
204 |
tr.appendChild(td);
|
|
|
205 |
|
|
|
206 |
// Calendar body
|
|
|
207 |
div = document.createElement("div");
|
|
|
208 |
div.className = "calendarBody";
|
|
|
209 |
this._calDiv.appendChild(div);
|
|
|
210 |
var calendarBody = div;
|
|
|
211 |
|
|
|
212 |
// Create the inside of calendar body
|
|
|
213 |
|
|
|
214 |
var text;
|
|
|
215 |
table = document.createElement("table");
|
|
|
216 |
table.align="center";
|
|
|
217 |
table.className = "grid";
|
|
|
218 |
|
|
|
219 |
div.appendChild(table);
|
|
|
220 |
var thead = document.createElement("thead");
|
|
|
221 |
table.appendChild(thead);
|
|
|
222 |
tr = document.createElement("tr");
|
|
|
223 |
thead.appendChild(tr);
|
|
|
224 |
|
|
|
225 |
for(i=0; i < 7; ++i) {
|
|
|
226 |
td = document.createElement("th");
|
|
|
227 |
text = document.createTextNode(this.ShortWeekDayNames[(i+this.FirstDayOfWeek)%7]);
|
|
|
228 |
td.appendChild(text);
|
|
|
229 |
td.className = "weekDayHead";
|
|
|
230 |
tr.appendChild(td);
|
|
|
231 |
}
|
|
|
232 |
|
|
|
233 |
// Date grid
|
|
|
234 |
tbody = document.createElement("tbody");
|
|
|
235 |
table.appendChild(tbody);
|
|
|
236 |
|
|
|
237 |
for(week=0; week<6; ++week) {
|
|
|
238 |
tr = document.createElement("tr");
|
|
|
239 |
tbody.appendChild(tr);
|
|
|
240 |
|
|
|
241 |
for(day=0; day<7; ++day) {
|
|
|
242 |
td = document.createElement("td");
|
|
|
243 |
td.className = "calendarDate";
|
|
|
244 |
text = document.createTextNode(String.fromCharCode(160));
|
|
|
245 |
td.appendChild(text);
|
|
|
246 |
|
|
|
247 |
tr.appendChild(td);
|
|
|
248 |
var tmp = new Object();
|
|
|
249 |
tmp.tag = "DATE";
|
|
|
250 |
tmp.value = -1;
|
|
|
251 |
tmp.data = text;
|
|
|
252 |
this.dateSlot[(week*7)+day] = tmp;
|
|
|
253 |
|
|
|
254 |
Event.observe(td, "mouseover", this.hover.bindEvent(this));
|
|
|
255 |
Event.observe(td, "mouseout", this.hover.bindEvent(this));
|
|
|
256 |
|
|
|
257 |
}
|
|
|
258 |
}
|
|
|
259 |
|
|
|
260 |
// Calendar Footer
|
|
|
261 |
div = document.createElement("div");
|
|
|
262 |
div.className = "calendarFooter";
|
|
|
263 |
this._calDiv.appendChild(div);
|
|
|
264 |
|
|
|
265 |
var todayButton = document.createElement("input");
|
|
|
266 |
todayButton.type="button";
|
|
|
267 |
todayButton.className = "todayButton";
|
|
|
268 |
var today = this.newDate();
|
|
|
269 |
var buttonText = today.SimpleFormat(this.Format,this);
|
|
|
270 |
todayButton.value = buttonText;
|
|
|
271 |
div.appendChild(todayButton);
|
|
|
272 |
|
|
|
273 |
if(Prado.Browser().ie)
|
|
|
274 |
{
|
|
|
275 |
this.iePopUp = document.createElement('iframe');
|
|
|
276 |
this.iePopUp.src = Prado.WebUI.TDatePicker.spacer;
|
|
|
277 |
this.iePopUp.style.position = "absolute"
|
|
|
278 |
this.iePopUp.scrolling="no"
|
|
|
279 |
this.iePopUp.frameBorder="0"
|
|
|
280 |
this.control.parentNode.appendChild(this.iePopUp);
|
|
|
281 |
}
|
|
|
282 |
|
|
|
283 |
this.control.parentNode.appendChild(this._calDiv);
|
|
|
284 |
|
|
|
285 |
this.update();
|
|
|
286 |
this.updateHeader();
|
|
|
287 |
|
|
|
288 |
this.ieHack(true);
|
|
|
289 |
|
|
|
290 |
// IE55+ extension
|
|
|
291 |
previousMonth.hideFocus = true;
|
|
|
292 |
nextMonth.hideFocus = true;
|
|
|
293 |
todayButton.hideFocus = true;
|
|
|
294 |
// end IE55+ extension
|
|
|
295 |
|
|
|
296 |
// hook up events
|
|
|
297 |
Event.observe(previousMonth, "click", this.prevMonth.bindEvent(this));
|
|
|
298 |
Event.observe(nextMonth, "click", this.nextMonth.bindEvent(this));
|
|
|
299 |
Event.observe(todayButton, "click", this.selectToday.bindEvent(this));
|
|
|
300 |
//Event.observe(clearButton, "click", this.clearSelection.bindEvent(this));
|
|
|
301 |
Event.observe(this._monthSelect, "change", this.monthSelect.bindEvent(this));
|
|
|
302 |
Event.observe(this._yearSelect, "change", this.yearSelect.bindEvent(this));
|
|
|
303 |
|
|
|
304 |
// ie6 extension
|
|
|
305 |
Event.observe(this._calDiv, "mousewheel", this.mouseWheelChange.bindEvent(this));
|
|
|
306 |
|
|
|
307 |
Event.observe(calendarBody, "click", this.selectDate.bindEvent(this));
|
|
|
308 |
|
|
|
309 |
Prado.Element.focus(this.control);
|
|
|
310 |
|
|
|
311 |
},
|
|
|
312 |
|
|
|
313 |
ieHack : function(cleanup)
|
|
|
314 |
{
|
|
|
315 |
// IE hack
|
|
|
316 |
if(this.iePopUp)
|
|
|
317 |
{
|
|
|
318 |
this.iePopUp.style.display = "block";
|
|
|
319 |
this.iePopUp.style.left = (this._calDiv.offsetLeft -1)+ "px";
|
|
|
320 |
this.iePopUp.style.top = (this._calDiv.offsetTop -1 ) + "px";
|
|
|
321 |
this.iePopUp.style.width = Math.abs(this._calDiv.offsetWidth -2)+ "px";
|
|
|
322 |
this.iePopUp.style.height = (this._calDiv.offsetHeight + 1)+ "px";
|
|
|
323 |
if(cleanup) this.iePopUp.style.display = "none";
|
|
|
324 |
}
|
|
|
325 |
},
|
|
|
326 |
|
|
|
327 |
keyPressed : function(ev)
|
|
|
328 |
{
|
|
|
329 |
if(!this.showing) return;
|
|
|
330 |
if (!ev) ev = document.parentWindow.event;
|
|
|
331 |
var kc = ev.keyCode != null ? ev.keyCode : ev.charCode;
|
|
|
332 |
|
|
|
333 |
if(kc == Event.KEY_RETURN || kc == Event.KEY_SPACEBAR || kc == Event.KEY_TAB)
|
|
|
334 |
{
|
|
|
335 |
this.setSelectedDate(this.selectedDate);
|
|
|
336 |
Event.stop(ev);
|
|
|
337 |
this.hide();
|
|
|
338 |
}
|
|
|
339 |
if(kc == Event.KEY_ESC)
|
|
|
340 |
{
|
|
|
341 |
Event.stop(ev); this.hide();
|
|
|
342 |
}
|
|
|
343 |
|
|
|
344 |
var getDaysPerMonth = function (nMonth, nYear)
|
|
|
345 |
{
|
|
|
346 |
nMonth = (nMonth + 12) % 12;
|
|
|
347 |
var days= [31,28,31,30,31,30,31,31,30,31,30,31];
|
|
|
348 |
var res = days[nMonth];
|
|
|
349 |
if (nMonth == 1) //feburary, leap years has 29
|
|
|
350 |
res += nYear % 4 == 0 && !(nYear % 400 == 0) ? 1 : 0;
|
|
|
351 |
return res;
|
|
|
352 |
}
|
|
|
353 |
|
|
|
354 |
if(kc < 37 || kc > 40) return true;
|
|
|
355 |
|
|
|
356 |
var current = this.selectedDate;
|
|
|
357 |
var d = current.valueOf();
|
|
|
358 |
if(kc == Event.KEY_LEFT)
|
|
|
359 |
{
|
|
|
360 |
if(ev.ctrlKey || ev.shiftKey) // -1 month
|
|
|
361 |
{
|
|
|
362 |
current.setDate( Math.min(current.getDate(), getDaysPerMonth(current.getMonth() - 1,current.getFullYear())) ); // no need to catch dec -> jan for the year
|
|
|
363 |
d = current.setMonth( current.getMonth() - 1 );
|
|
|
364 |
}
|
|
|
365 |
else
|
|
|
366 |
d -= 86400000; //-1 day
|
|
|
367 |
}
|
|
|
368 |
else if (kc == Event.KEY_RIGHT)
|
|
|
369 |
{
|
|
|
370 |
if(ev.ctrlKey || ev.shiftKey) // +1 month
|
|
|
371 |
{
|
|
|
372 |
current.setDate( Math.min(current.getDate(), getDaysPerMonth(current.getMonth() + 1,current.getFullYear())) ); // no need to catch dec -> jan for the year
|
|
|
373 |
d = current.setMonth( current.getMonth() + 1 );
|
|
|
374 |
}
|
|
|
375 |
else
|
|
|
376 |
d += 86400000; //+1 day
|
|
|
377 |
}
|
|
|
378 |
else if (kc == Event.KEY_UP)
|
|
|
379 |
{
|
|
|
380 |
if(ev.ctrlKey || ev.shiftKey) //-1 year
|
|
|
381 |
{
|
|
|
382 |
current.setDate( Math.min(current.getDate(), getDaysPerMonth(current.getMonth(),current.getFullYear() - 1)) ); // no need to catch dec -> jan for the year
|
|
|
383 |
d = current.setFullYear( current.getFullYear() - 1 );
|
|
|
384 |
}
|
|
|
385 |
else
|
|
|
386 |
d -= 604800000; // -7 days
|
|
|
387 |
}
|
|
|
388 |
else if (kc == Event.KEY_DOWN)
|
|
|
389 |
{
|
|
|
390 |
if(ev.ctrlKey || ev.shiftKey) // +1 year
|
|
|
391 |
{
|
|
|
392 |
current.setDate( Math.min(current.getDate(), getDaysPerMonth(current.getMonth(),current.getFullYear() + 1)) ); // no need to catch dec -> jan for the year
|
|
|
393 |
d = current.setFullYear( current.getFullYear() + 1 );
|
|
|
394 |
}
|
|
|
395 |
else
|
|
|
396 |
d += 7 * 24 * 61 * 60 * 1000; // +7 days
|
|
|
397 |
}
|
|
|
398 |
this.setSelectedDate(d);
|
|
|
399 |
Event.stop(ev);
|
|
|
400 |
},
|
|
|
401 |
|
|
|
402 |
selectDate : function(ev)
|
|
|
403 |
{
|
|
|
404 |
var el = Event.element(ev);
|
|
|
405 |
while (el.nodeType != 1)
|
|
|
406 |
el = el.parentNode;
|
|
|
407 |
|
|
|
408 |
while (el != null && el.tagName && el.tagName.toLowerCase() != "td")
|
|
|
409 |
el = el.parentNode;
|
|
|
410 |
|
|
|
411 |
// if no td found, return
|
|
|
412 |
if (el == null || el.tagName == null || el.tagName.toLowerCase() != "td")
|
|
|
413 |
return;
|
|
|
414 |
|
|
|
415 |
var d = this.newDate(this.selectedDate);
|
|
|
416 |
var n = Number(el.firstChild.data);
|
|
|
417 |
if (isNaN(n) || n <= 0 || n == null)
|
|
|
418 |
return;
|
|
|
419 |
|
|
|
420 |
d.setDate(n);
|
|
|
421 |
this.setSelectedDate(d);
|
|
|
422 |
this.hide();
|
|
|
423 |
},
|
|
|
424 |
|
|
|
425 |
selectToday : function()
|
|
|
426 |
{
|
|
|
427 |
if(this.selectedDate.toISODate() == this.newDate().toISODate())
|
|
|
428 |
this.hide();
|
|
|
429 |
|
|
|
430 |
this.setSelectedDate(this.newDate());
|
|
|
431 |
},
|
|
|
432 |
|
|
|
433 |
clearSelection : function()
|
|
|
434 |
{
|
|
|
435 |
this.setSelectedDate(this.newDate());
|
|
|
436 |
this.hide();
|
|
|
437 |
},
|
|
|
438 |
|
|
|
439 |
monthSelect : function(ev)
|
|
|
440 |
{
|
|
|
441 |
this.setMonth(Form.Element.getValue(Event.element(ev)));
|
|
|
442 |
},
|
|
|
443 |
|
|
|
444 |
yearSelect : function(ev)
|
|
|
445 |
{
|
|
|
446 |
this.setYear(Form.Element.getValue(Event.element(ev)));
|
|
|
447 |
},
|
|
|
448 |
|
|
|
449 |
// ie6 extension
|
|
|
450 |
mouseWheelChange : function (e)
|
|
|
451 |
{
|
|
|
452 |
if (e == null) e = document.parentWindow.event;
|
|
|
453 |
var n = - e.wheelDelta / 120;
|
|
|
454 |
var d = this.newDate(this.selectedDate);
|
|
|
455 |
var m = d.getMonth() + n;
|
|
|
456 |
this.setMonth(m);
|
|
|
457 |
|
|
|
458 |
return false;
|
|
|
459 |
},
|
|
|
460 |
|
|
|
461 |
// Respond to change event on the textbox or dropdown list
|
|
|
462 |
// This method raises OnDateChanged event on client side if it has been defined
|
|
|
463 |
onDateChanged : function ()
|
|
|
464 |
{
|
|
|
465 |
if (this.options.OnDateChanged)
|
|
|
466 |
{
|
|
|
467 |
var date;
|
|
|
468 |
if (this.options.InputMode == "TextBox")
|
|
|
469 |
{
|
|
|
470 |
date=this.control.value;
|
|
|
471 |
}
|
|
|
472 |
else
|
|
|
473 |
{
|
|
|
474 |
var day = Prado.WebUI.TDatePicker.getDayListControl(this.control).selectedIndex+1;
|
|
|
475 |
var month = Prado.WebUI.TDatePicker.getMonthListControl(this.control).selectedIndex;
|
|
|
476 |
var year = Prado.WebUI.TDatePicker.getYearListControl(this.control).value;
|
|
|
477 |
date=new Date(year, month, day, 0,0,0).SimpleFormat(this.Format, this);
|
|
|
478 |
}
|
|
|
479 |
this.options.OnDateChanged(this, date);
|
|
|
480 |
}
|
|
|
481 |
},
|
|
|
482 |
|
|
|
483 |
onChange : function()
|
|
|
484 |
{
|
|
|
485 |
if(this.options.InputMode == "TextBox")
|
|
|
486 |
{
|
|
|
487 |
this.control.value = this.formatDate();
|
|
|
488 |
Event.fireEvent(this.control, "change");
|
|
|
489 |
}
|
|
|
490 |
else
|
|
|
491 |
{
|
|
|
492 |
var day = Prado.WebUI.TDatePicker.getDayListControl(this.control);
|
|
|
493 |
var month = Prado.WebUI.TDatePicker.getMonthListControl(this.control);
|
|
|
494 |
var year = Prado.WebUI.TDatePicker.getYearListControl(this.control);
|
|
|
495 |
var date = this.selectedDate;
|
|
|
496 |
if(day)
|
|
|
497 |
{
|
|
|
498 |
day.selectedIndex = date.getDate()-1;
|
|
|
499 |
}
|
|
|
500 |
if(month)
|
|
|
501 |
{
|
|
|
502 |
month.selectedIndex = date.getMonth();
|
|
|
503 |
}
|
|
|
504 |
if(year)
|
|
|
505 |
{
|
|
|
506 |
var years = year.options;
|
|
|
507 |
var currentYear = date.getFullYear();
|
|
|
508 |
for(var i = 0; i < years.length; i++)
|
|
|
509 |
years[i].selected = years[i].value.toInteger() == currentYear;
|
|
|
510 |
}
|
|
|
511 |
Event.fireEvent(day || month || year, "change");
|
|
|
512 |
}
|
|
|
513 |
},
|
|
|
514 |
|
|
|
515 |
formatDate : function()
|
|
|
516 |
{
|
|
|
517 |
return this.selectedDate ? this.selectedDate.SimpleFormat(this.Format,this) : '';
|
|
|
518 |
},
|
|
|
519 |
|
|
|
520 |
newDate : function(date)
|
|
|
521 |
{
|
|
|
522 |
if(!date)
|
|
|
523 |
date = new Date();
|
|
|
524 |
if(typeof(date) == "string" || typeof(date) == "number")
|
|
|
525 |
date = new Date(date);
|
|
|
526 |
return new Date(Math.min(Math.max(date.getFullYear(),this.FromYear),this.UpToYear), date.getMonth(), date.getDate(), 0,0,0);
|
|
|
527 |
},
|
|
|
528 |
|
|
|
529 |
setSelectedDate : function(date)
|
|
|
530 |
{
|
|
|
531 |
if (date == null)
|
|
|
532 |
return;
|
|
|
533 |
var old=this.selectedDate;
|
|
|
534 |
this.selectedDate = this.newDate(date);
|
|
|
535 |
var dateChanged=(old - this.selectedDate != 0) || ( this.options.InputMode == "TextBox" && this.control.value != this.formatDate());
|
|
|
536 |
|
|
|
537 |
this.updateHeader();
|
|
|
538 |
this.update();
|
|
|
539 |
if (dateChanged && typeof(this.onChange) == "function")
|
|
|
540 |
this.onChange(this, date);
|
|
|
541 |
},
|
|
|
542 |
|
|
|
543 |
getElement : function()
|
|
|
544 |
{
|
|
|
545 |
return this._calDiv;
|
|
|
546 |
},
|
|
|
547 |
|
|
|
548 |
getSelectedDate : function ()
|
|
|
549 |
{
|
|
|
550 |
return this.selectedDate == null ? null : this.newDate(this.selectedDate);
|
|
|
551 |
},
|
|
|
552 |
|
|
|
553 |
setYear : function(year)
|
|
|
554 |
{
|
|
|
555 |
var d = this.newDate(this.selectedDate);
|
|
|
556 |
d.setFullYear(year);
|
|
|
557 |
this.setSelectedDate(d);
|
|
|
558 |
},
|
|
|
559 |
|
|
|
560 |
setMonth : function (month)
|
|
|
561 |
{
|
|
|
562 |
var d = this.newDate(this.selectedDate);
|
|
|
563 |
d.setDate(Math.min(d.getDate(), this.getDaysPerMonth(month,d.getFullYear())));
|
|
|
564 |
d.setMonth(month);
|
|
|
565 |
this.setSelectedDate(d);
|
|
|
566 |
},
|
|
|
567 |
|
|
|
568 |
nextMonth : function ()
|
|
|
569 |
{
|
|
|
570 |
this.setMonth(this.selectedDate.getMonth()+1);
|
|
|
571 |
},
|
|
|
572 |
|
|
|
573 |
prevMonth : function ()
|
|
|
574 |
{
|
|
|
575 |
this.setMonth(this.selectedDate.getMonth()-1);
|
|
|
576 |
},
|
|
|
577 |
|
|
|
578 |
getDaysPerMonth : function (month, year)
|
|
|
579 |
{
|
|
|
580 |
month = (Number(month)+12) % 12;
|
|
|
581 |
var days = [31,28,31,30,31,30,31,31,30,31,30,31];
|
|
|
582 |
var res = days[month];
|
|
|
583 |
if (month == 1 && ((!(year % 4) && (year % 100)) || !(year % 400))) //feburary, leap years has 29
|
|
|
584 |
res++;
|
|
|
585 |
return res;
|
|
|
586 |
},
|
|
|
587 |
|
|
|
588 |
getDatePickerOffsetHeight : function()
|
|
|
589 |
{
|
|
|
590 |
if(this.options.InputMode == "TextBox")
|
|
|
591 |
return this.control.offsetHeight;
|
|
|
592 |
|
|
|
593 |
var control = Prado.WebUI.TDatePicker.getDayListControl(this.control);
|
|
|
594 |
if(control) return control.offsetHeight;
|
|
|
595 |
|
|
|
596 |
var control = Prado.WebUI.TDatePicker.getMonthListControl(this.control);
|
|
|
597 |
if(control) return control.offsetHeight;
|
|
|
598 |
|
|
|
599 |
var control = Prado.WebUI.TDatePicker.getYearListControl(this.control);
|
|
|
600 |
if(control) return control.offsetHeight;
|
|
|
601 |
return 0;
|
|
|
602 |
},
|
|
|
603 |
|
|
|
604 |
show : function()
|
|
|
605 |
{
|
|
|
606 |
this.create();
|
|
|
607 |
|
|
|
608 |
if(!this.showing)
|
|
|
609 |
{
|
|
|
610 |
var pos = this.control.positionedOffset();
|
|
|
611 |
|
|
|
612 |
pos[1] += this.getDatePickerOffsetHeight();
|
|
|
613 |
this._calDiv.style.top = (pos[1]-1) + "px";
|
|
|
614 |
this._calDiv.style.display = "block";
|
|
|
615 |
this._calDiv.style.left = pos[0] + "px";
|
|
|
616 |
|
|
|
617 |
this.documentClickEvent = this.hideOnClick.bindEvent(this);
|
|
|
618 |
this.documentKeyDownEvent = this.keyPressed.bindEvent(this);
|
|
|
619 |
Event.observe(document.body, "click", this.documentClickEvent);
|
|
|
620 |
var date = this.getDateFromInput();
|
|
|
621 |
if(date)
|
|
|
622 |
{
|
|
|
623 |
this.selectedDate = date;
|
|
|
624 |
this.setSelectedDate(date);
|
|
|
625 |
}
|
|
|
626 |
Event.observe(document,"keydown", this.documentKeyDownEvent);
|
|
|
627 |
this.showing = true;
|
|
|
628 |
|
|
|
629 |
if(this.positionMode=='Top')
|
|
|
630 |
{
|
|
|
631 |
this._calDiv.style.top = ((pos[1]-1) - this.getDatePickerOffsetHeight() - this._calDiv.offsetHeight) + 'px';
|
|
|
632 |
if(Prado.Browser().ie)
|
|
|
633 |
this.iePopup = this._calDiv.style.top;
|
|
|
634 |
}
|
|
|
635 |
this.ieHack(false);
|
|
|
636 |
}
|
|
|
637 |
},
|
|
|
638 |
|
|
|
639 |
getDateFromInput : function()
|
|
|
640 |
{
|
|
|
641 |
if(this.options.InputMode == "TextBox")
|
|
|
642 |
return Date.SimpleParse($F(this.control), this.Format);
|
|
|
643 |
else
|
|
|
644 |
return Prado.WebUI.TDatePicker.getDropDownDate(this.control);
|
|
|
645 |
},
|
|
|
646 |
|
|
|
647 |
//hide the calendar when clicked outside any calendar
|
|
|
648 |
hideOnClick : function(ev)
|
|
|
649 |
{
|
|
|
650 |
if(!this.showing) return;
|
|
|
651 |
var el = Event.element(ev);
|
|
|
652 |
var within = false;
|
|
|
653 |
do
|
|
|
654 |
{
|
|
|
655 |
within = within || (el.className && Element.hasClassName(el, "TDatePicker_"+this.CalendarStyle));
|
|
|
656 |
within = within || el == this.trigger;
|
|
|
657 |
within = within || el == this.control;
|
|
|
658 |
if(within) break;
|
|
|
659 |
el = el.parentNode;
|
|
|
660 |
}
|
|
|
661 |
while(el);
|
|
|
662 |
if(!within) this.hide();
|
|
|
663 |
},
|
|
|
664 |
|
|
|
665 |
|
|
|
666 |
hide : function()
|
|
|
667 |
{
|
|
|
668 |
if(this.showing)
|
|
|
669 |
{
|
|
|
670 |
this._calDiv.style.display = "none";
|
|
|
671 |
if(this.iePopUp)
|
|
|
672 |
this.iePopUp.style.display = "none";
|
|
|
673 |
this.showing = false;
|
|
|
674 |
Event.stopObserving(document.body, "click", this.documentClickEvent);
|
|
|
675 |
Event.stopObserving(document,"keydown", this.documentKeyDownEvent);
|
|
|
676 |
}
|
|
|
677 |
},
|
|
|
678 |
|
|
|
679 |
update : function()
|
|
|
680 |
{
|
|
|
681 |
// Calculate the number of days in the month for the selected date
|
|
|
682 |
var date = this.selectedDate;
|
|
|
683 |
var today = (this.newDate()).toISODate();
|
|
|
684 |
|
|
|
685 |
var selected = date.toISODate();
|
|
|
686 |
var d1 = new Date(date.getFullYear(), date.getMonth(), 1);
|
|
|
687 |
var d2 = new Date(date.getFullYear(), date.getMonth()+1, 1);
|
|
|
688 |
var monthLength = Math.round((d2 - d1) / (24 * 60 * 60 * 1000));
|
|
|
689 |
|
|
|
690 |
// Find out the weekDay index for the first of this month
|
|
|
691 |
var firstIndex = (d1.getDay() - this.FirstDayOfWeek) % 7 ;
|
|
|
692 |
if (firstIndex < 0)
|
|
|
693 |
firstIndex += 7;
|
|
|
694 |
|
|
|
695 |
var index = 0;
|
|
|
696 |
while (index < firstIndex) {
|
|
|
697 |
this.dateSlot[index].value = -1;
|
|
|
698 |
this.dateSlot[index].data.data = String.fromCharCode(160);
|
|
|
699 |
this.dateSlot[index].data.parentNode.className = "empty";
|
|
|
700 |
index++;
|
|
|
701 |
}
|
|
|
702 |
|
|
|
703 |
for (i = 1; i <= monthLength; i++, index++) {
|
|
|
704 |
var slot = this.dateSlot[index];
|
|
|
705 |
var slotNode = slot.data.parentNode;
|
|
|
706 |
slot.value = i;
|
|
|
707 |
slot.data.data = i;
|
|
|
708 |
slotNode.className = "date";
|
|
|
709 |
//slotNode.style.color = "";
|
|
|
710 |
if (d1.toISODate() == today) {
|
|
|
711 |
slotNode.className += " today";
|
|
|
712 |
}
|
|
|
713 |
if (d1.toISODate() == selected) {
|
|
|
714 |
// slotNode.style.color = "blue";
|
|
|
715 |
slotNode.className += " selected";
|
|
|
716 |
}
|
|
|
717 |
d1 = new Date(d1.getFullYear(), d1.getMonth(), d1.getDate()+1);
|
|
|
718 |
}
|
|
|
719 |
|
|
|
720 |
var lastDateIndex = index;
|
|
|
721 |
|
|
|
722 |
while(index < 42) {
|
|
|
723 |
this.dateSlot[index].value = -1;
|
|
|
724 |
this.dateSlot[index].data.data = String.fromCharCode(160);
|
|
|
725 |
this.dateSlot[index].data.parentNode.className = "empty";
|
|
|
726 |
++index;
|
|
|
727 |
}
|
|
|
728 |
|
|
|
729 |
},
|
|
|
730 |
|
|
|
731 |
hover : function(ev)
|
|
|
732 |
{
|
|
|
733 |
if(Event.element(ev).tagName)
|
|
|
734 |
{
|
|
|
735 |
if(ev.type == "mouseover")
|
|
|
736 |
Event.element(ev).addClassName("hover");
|
|
|
737 |
else
|
|
|
738 |
Event.element(ev).removeClassName("hover");
|
|
|
739 |
}
|
|
|
740 |
},
|
|
|
741 |
|
|
|
742 |
updateHeader : function () {
|
|
|
743 |
|
|
|
744 |
var options = this._monthSelect.options;
|
|
|
745 |
var m = this.selectedDate.getMonth();
|
|
|
746 |
for(var i=0; i < options.length; ++i) {
|
|
|
747 |
options[i].selected = false;
|
|
|
748 |
if (options[i].value == m) {
|
|
|
749 |
options[i].selected = true;
|
|
|
750 |
}
|
|
|
751 |
}
|
|
|
752 |
|
|
|
753 |
options = this._yearSelect.options;
|
|
|
754 |
var year = this.selectedDate.getFullYear();
|
|
|
755 |
for(var i=0; i < options.length; ++i) {
|
|
|
756 |
options[i].selected = false;
|
|
|
757 |
if (options[i].value == year) {
|
|
|
758 |
options[i].selected = true;
|
|
|
759 |
}
|
|
|
760 |
}
|
|
|
761 |
|
|
|
762 |
}
|
|
|
763 |
};
|