otsdaq_utilities  v2_05_02_indev
CalendarPopup.js
1 // ===================================================================
2 // Author: Ryan Rivera
3 // Use to create a calendar and get a date selectiong
4 // back from user. A handler function is called
5 // if the user selects a date. If the user cancels
6 // nothing is called.
7 //
8 // Usage:
9 //---- function calendarPopup(x, y, calendarHandler)
10 //
11 //
12 // - Calendar pops up at x, y
13 // - Date value returned to calendarHandler as number
14 // representing days since Jan 1, 1970
15 //
16 //
17 // Example JS code:
18 //
19 //---- function myDateHandler(userDate) { alert(userDate); }
20 //---- //open calendar at coordinates (10,10)
21 //---- calendarPopup( 10 , 10 , myDateHandler);
22 //
23 //
24 // Tips:
25 // - override defaults at top of code for personalization
26 // ===================================================================
27 
28 
29 //defaults
30 // can be overridden in user code
31 
32 var calendarPopup_BORDER = "1px solid #aaa";
33 var calendarPopup_BACKGROUND_COLOR = "rgba(0,0,0,0.8)";
34 var calendarPopup_FONT_COLOR = "white";
35 
36 var calendarPopup_Z = 1000000;
37 var calendarPopup_W = 180;
38 var calendarPopup_H = 186;
39 var calendarPopup_CALID = "calendarPopup";
40 var calendarPopup_MIN_YEAR = 2013; //0 indicates current year
41 var calendarPopup_MAX_YEAR = 0; //0 indicates current year
42 
43 var calendarPopup_HEADER_H = 35;
44 var calendarPopup_DATES_H = 110;
45 var calendarPopup_DATES_OFFY = 28;
46 var calendarPopup_DATES_HDR_H = 15;
47 
48 //member variables - do not override
49 var calendarPopup_HANDLER;
50 
51 //function list
52  //calendarPopup(x, y, calendarHandler)
53  //calendarPopupAddHeader()
54  //cancelCalendar()
55  //calendarPopupAddMonthNav()
56  //previousMonth()
57  //nextMonth()
58  //calendarPopupAddDates()
59  //calendarPopupAddFooter()
60  //redrawDates()
61  //handleSelection()
62 
64 
65 //calendarPopup ~
66 // x is integer from left edge of window
67 // y is integer from top edge of window
68 function calendarPopup(x, y, calendarHandler) {
69 
70  calendarPopup_HANDLER = calendarHandler;
71 
72  //remove calendar if already exists
73  cancelCalendar();
74 
75  el = document.createElement("div");
76  el.setAttribute("id", calendarPopup_CALID);
77  el.style.zIndex = calendarPopup_Z;
78  el.style.position = "absolute";
79  el.style.overflow = "hidden";
80  el.style.left = x + "px";
81  el.style.top = y + "px";
82  el.style.width = calendarPopup_W + "px";
83  el.style.height = calendarPopup_H + "px";
84 
85  el.style.backgroundColor = calendarPopup_BACKGROUND_COLOR;
86  el.style.border = calendarPopup_BORDER;
87  el.style.color = calendarPopup_FONT_COLOR;
88 
89  document.body.appendChild(el);
90 
91  calendarPopupAddHeader();
92  calendarPopupAddMonthNav();
93  calendarPopupAddDates();
94  calendarPopupAddFooter();
95 }
96 
97 //calendarPopupAddHeader
98 // add drop down and month display
99 function calendarPopupAddHeader() {
100 
101  var calEl = document.getElementById(calendarPopup_CALID);
102  el = document.createElement("div");
103  el.setAttribute("id", calendarPopup_CALID + "-header");
104  el.style.position = "absolute";
105  el.style.left = 0 + "px";
106  el.style.top = 0 + "px";
107  el.style.width = calendarPopup_W + "px";
108  el.style.height = calendarPopup_HEADER_H + "px";
109  el.style.borderBottom = calendarPopup_BORDER;
110 
111  calEl = calEl.appendChild(el);
112  var subCalEl;
113 
114  //add month select -----------------
115  el = document.createElement("select");
116  el.setAttribute("id", calendarPopup_CALID + "-monthSelect");
117  el.style.color = "black";
118  el.setAttribute("title", "Select a month from the dropdown");
119  el.style.margin = "5px";
120  el.style.cssFloat = "left";
121  el.style.width = 70 + "px";
122  el.onchange = handleSelection;
123 
124  subCalEl = calEl.appendChild(el);
125 
126  el = document.createElement("option");
127  el.style.color = "black";
128  el.text = "Month";
129  subCalEl.add(el,null); //insert at end of list
130 
131  var mDate = parseInt((new Date("Jan 1, 2013")).getTime()); //init to jan, arbitrary year
132  for(var i=0;i<12;++i, mDate += 1000*60*60*24*32) //add 32 days to get all months
133  {
134  el = document.createElement("option");
135  el.style.color = "black";
136  el.text = (new Date(mDate)).toDateString().split(" ")[1];
137  subCalEl.add(el,null); //insert at end of list
138  }
139 
140  //add year select -------------
141  el = document.createElement("select");
142  el.setAttribute("id", calendarPopup_CALID + "-yearSelect");
143  el.style.color = "black";
144  el.setAttribute("title", "Select a month from the dropdown");
145  el.style.margin = "5px";
146  el.style.cssFloat = "left";
147  el.style.width = 70 + "px";
148  el.onchange = handleSelection;
149 
150  subCalEl = calEl.appendChild(el);
151 
152  el = document.createElement("option");
153 
154  var yDate = parseInt((new Date()).getFullYear()); //init to this year
155 
156  var yMin = parseInt(calendarPopup_MIN_YEAR)?parseInt(calendarPopup_MIN_YEAR):yDate;
157  var yMax = parseInt(calendarPopup_MAX_YEAR)?parseInt(calendarPopup_MAX_YEAR):yDate;
158  for(var i=yMin;i<=yMax;++i) //add all years in range and select current year
159  {
160  el = document.createElement("option");
161  el.style.color = "black";
162  el.text = i;
163  subCalEl.add(el,null); //insert at end of list
164  if(yDate == i) el.defaultSelected = true;
165  }
166 
167  //add cancel -------------
168  el = document.createElement("div");
169  el.setAttribute("id", calendarPopup_CALID + "-cancel");
170  el.setAttribute("title", "Cancel date select");
171  el.style.margin = "5px";
172  el.style.cssFloat = "left";
173  el.style.width = 10 + "px";
174  el.style.height = 10 + "px";
175  el.innerHTML = "<b>X</b>";
176  el.style.fontFamily = "arial";
177  el.style.cursor = "pointer";
178 
179  el.onmouseup = cancelCalendar;
180  calEl.appendChild(el);
181 }
182 
183 //cancelCalendar
184 // close calendar do not call handler
185 function cancelCalendar() {
186  var el = document.getElementById(calendarPopup_CALID);
187  if(el)
188  el.parentNode.removeChild(el);
189 }
190 
191 //calendarPopupAddMonthNav
192 function calendarPopupAddMonthNav() {
193 
194  var calEl = document.getElementById(calendarPopup_CALID);
195 
196  el = document.createElement("div");
197  el.setAttribute("id", calendarPopup_CALID + "-monthNav");
198  el.style.position = "absolute";
199  el.style.left = 0 + "px";
200  el.style.top = calendarPopup_HEADER_H + "px";
201  el.style.width = calendarPopup_W + "px";
202 
203  calEl = calEl.appendChild(el);
204 
205  var navW = 25;
206 
207  //add left arrows
208  el = document.createElement("div");
209  el.setAttribute("title", "Prev Month");
210  el.style.margin = "5px";
211  el.style.cssFloat = "left";
212  el.style.width = navW + "px";
213  el.innerHTML = "<b>&lt&lt&lt</b>";
214  el.style.fontFamily = "arial";
215  el.style.cursor = "pointer";
216  el.style.textAlign = "center";
217 
218  el.onmouseup = previousMonth;
219  calEl.appendChild(el);
220 
221  //add current month, year
222  el = document.createElement("div");
223  el.setAttribute("id", calendarPopup_CALID + "-monthYearDisplay");
224  el.style.cssFloat = "left";
225  el.style.width = (calendarPopup_W - navW*2 - 20) + "px";
226  var todayArr = (new Date()).toDateString().split(" ");
227  el.innerHTML = todayArr[1] + " " + todayArr[3]; //init to current month
228  el.style.fontWeight = "800";
229  el.style.textAlign = "center";
230  el.style.cursor = "default";
231  el.style.marginTop = "5px";
232 
233  calEl.appendChild(el);
234 
235  //add right arrows
236  el = document.createElement("div");
237  el.setAttribute("title", "Prev Month");
238  el.style.margin = "5px";
239  el.style.cssFloat = "right";
240  el.style.width = navW + "px";
241  el.innerHTML = "<b>&gt&gt&gt</b>";
242  el.style.fontFamily = "arial";
243  el.style.cursor = "pointer";
244  el.style.textAlign = "center";
245 
246  el.onmouseup = nextMonth;
247  calEl.appendChild(el);
248 
249  //add hidden current month and year displayed
250  el = document.createElement("div");
251  el.setAttribute("id", calendarPopup_CALID + "-currMonth");
252  el.style.display = "none";
253  el.innerHTML = (new Date()).getMonth(); //init to current month
254  calEl.appendChild(el);
255  el = document.createElement("div");
256  el.setAttribute("id", calendarPopup_CALID + "-currYear");
257  el.style.display = "none";
258  el.innerHTML = (new Date()).getFullYear(); //init to current month
259  calEl.appendChild(el);
260 
261 }
262 
263 
264 //previousMonth
265 function previousMonth() {
266  var el = document.getElementById(calendarPopup_CALID + "-currMonth");
267  var newMo = parseInt(el.innerHTML)-1; //subtract 1 from month (0-11)
268  var yel = document.getElementById(calendarPopup_CALID + "-currYear");
269  if(newMo < 0) //decrement year
270  {
271  newMo = 11;
272  yel.innerHTML = parseInt(yel.innerHTML)-1;
273  }
274  el.innerHTML = newMo;
275  el = document.getElementById(calendarPopup_CALID + "-monthYearDisplay");
276  var dayArr = (new Date(yel.innerHTML,newMo,1)).toDateString().split(" ");
277  el.innerHTML = dayArr[1] + " " + dayArr[3]; //display updated month
278  redrawDates();
279 }
280 
281 //nextMonth
282 function nextMonth() {
283  var el = document.getElementById(calendarPopup_CALID + "-currMonth");
284  var newMo = parseInt(el.innerHTML)+1; //add 1 from month (0-11)
285  var yel = document.getElementById(calendarPopup_CALID + "-currYear");
286  if(newMo > 11) //increment year
287  {
288  newMo = 0;
289  yel.innerHTML = parseInt(yel.innerHTML)+1;
290  }
291  el.innerHTML = newMo;
292  el = document.getElementById(calendarPopup_CALID + "-monthYearDisplay");
293  var dayArr = (new Date(yel.innerHTML,newMo,1)).toDateString().split(" ");
294  el.innerHTML = dayArr[1] + " " + dayArr[3]; //display updated month
295  redrawDates();
296 }
297 
298 //calendarPopupAddDates
299 function calendarPopupAddDates() {
300  var calEl = document.getElementById(calendarPopup_CALID);
301 
302  el = document.createElement("div");
303  el.setAttribute("id", calendarPopup_CALID + "-dates");
304  el.style.position = "absolute";
305  el.style.left = 0 + "px";
306  el.style.top = calendarPopup_HEADER_H + calendarPopup_DATES_OFFY +
307  calendarPopup_DATES_HDR_H + "px";
308  el.style.width = calendarPopup_W + "px";
309  el.style.height = calendarPopup_DATES_H + "px";
310  el.style.paddingTop = "2px";
311 
312  calEl.appendChild(el);
313 
314  el = document.createElement("div");
315  el.setAttribute("id", calendarPopup_CALID + "-datesHeader");
316  el.style.position = "absolute";
317  el.style.left = 0 + "px";
318  el.style.top = calendarPopup_HEADER_H + calendarPopup_DATES_OFFY + "px";
319  el.style.width = calendarPopup_W + "px";
320  el.style.height = calendarPopup_DATES_HDR_H + "px";
321  el.style.borderBottom = '1px solid #aaa';
322 
323  calEl = calEl.appendChild(el);
324 
325  var w = parseInt(calendarPopup_W/7);
326  var firstMargin = parseInt((calendarPopup_W - w*7)/2); //acount for fractional acccumulation of error
327  var x = 0;
328  var day = new Date("Jun 9, 2013"); //any Sunday
329  for(var i=0;i<7;++i, day = new Date(day.getTime() + 1000*60*60*24)) //draw day headers, day by day
330  {
331  el = document.createElement("div");
332  el.setAttribute("class", calendarPopup_CALID + "-dayHdr");
333  el.style.cssFloat = "left";
334  el.style.width = w + "px";
335  el.style.height = calendarPopup_DATES_HDR_H + "px";
336  el.style.color = "white";
337  el.style.textAlign = "center";
338  if(!i) el.style.marginLeft = firstMargin + "px";
339  el.innerHTML = day.toDateString()[0]; //get first letter of day
340  calEl.appendChild(el);
341  }
342 
343  redrawDates();
344 }
345 
346 //calendarPopupAddFooter
347 function calendarPopupAddFooter() {
348 
349  var calEl = document.getElementById(calendarPopup_CALID);
350 }
351 
352 //redrawDates
353 function redrawDates() {
354  var calEl = document.getElementById(calendarPopup_CALID + "-dates");
355  calEl.innerHTML = ""; //clear old dates
356 
357  //draw 6 weeks of dates always
358 
359  //start from 1st of the curr month/year
360  var mo = document.getElementById(calendarPopup_CALID + "-currMonth").innerHTML;
361  var day = new Date(
362  document.getElementById(calendarPopup_CALID + "-currYear").innerHTML,
363  mo,1);
364  //walk back dayOne until it is a Sunday (day 0)
365  while(day.getDay()) day = new Date(day.getTime() - 1000*60*60*24); //subtract a day
366 
367  //now have first day in dates, loop through 7*6
368  var w = parseInt(calendarPopup_W/7);
369  var h = parseInt(calendarPopup_DATES_H/6);
370  var x = 0, y = 0;
371  var firstMargin = parseInt((calendarPopup_W - w*7)/2); //acount for fractional acccumulation of error
372 
373  var todayDate = (new Date()).getDate(); //to color today
374  var todayMonth = (new Date()).getMonth(); //to color today
375  var todayYear = (new Date()).getFullYear(); //to color today
376  for(var i=0;i<7*6;++i, day = new Date(day.getTime() + 1000*60*60*24)) //draw dates, day by day
377  {
378  el = document.createElement("div");
379  el.setAttribute("class", calendarPopup_CALID + "-day");
380  //store in ID, days since Jan 1, 1970
381  el.setAttribute("id", calendarPopup_CALID + "-day-" + parseInt(day.getTime()/(1000*60*60*24)));
382 
383  el.style.cssFloat = "left";
384  el.style.width = w + "px";
385  el.style.height = h + "px";
386  el.style.textAlign = "center";
387  el.style.cursor = "pointer";
388  el.style.color = mo == day.getMonth()?"white":"gray";
389  el.style.backgroundColor = (
390  todayYear == day.getFullYear() &&
391  todayMonth == day.getMonth() &&
392  todayDate == day.getDate())?"#458":"auto";
393 
394  if(!(i%7)) el.style.marginLeft = firstMargin + "px";
395  el.innerHTML = day.getDate(); //get day number
396 
397  el.onmouseup = selectDate;
398 
399  calEl.appendChild(el);
400  }
401 
402  //add hover color for day
403  var head = document.getElementsByTagName('head')[0];
404  var style = document.createElement('style');
405  var declarations = document.createTextNode("." + calendarPopup_CALID + "-day:hover { background-color: #A10 }");
406  style.type = 'text/css';
407  if (style.styleSheet) {
408  style.styleSheet.cssText = declarations.nodeValue;
409  } else {
410  style.appendChild(declarations);
411  }
412 
413  head.appendChild(style);
414 }
415 
416 
417 //handleSelection
418 // handle selection change from month or year drop down
419 function handleSelection() {
420  var el = document.getElementById(calendarPopup_CALID + "-monthSelect");
421  var newMo = el.value;
422  el = document.getElementById(calendarPopup_CALID + "-monthYearDisplay"); //display updated month
423  el.innerHTML = newMo;
424  newMo = (new Date(newMo + " 1 2013")).getMonth(); //get new month in 0-11 format
425  el = document.getElementById(calendarPopup_CALID + "-currMonth");
426  el.innerHTML = newMo;
427 
428  el = document.getElementById(calendarPopup_CALID + "-yearSelect");
429  newMo = el.value;
430  el = document.getElementById(calendarPopup_CALID + "-monthYearDisplay"); //display updated month
431  el.innerHTML += " " + newMo;
432  el = document.getElementById(calendarPopup_CALID + "-currYear");
433  el.innerHTML = newMo;
434 
435  redrawDates();
436 }
437 
438 function selectDate() {
439  calendarPopup_HANDLER(parseInt(this.id.split("-")[2])+1); //pass days since Jan 1, 1970
440  cancelCalendar();
441 }
442 
443 
444 
445 
446 
447 
448 
449 
450 
451