]> git.datanom.net - webcal.git/blob - js/calendar_db.js
Initial upload
[webcal.git] / js / calendar_db.js
1 // Tigra Calendar v4.0.2 (2009-01-12) Database (yyyy-mm-dd)
2 // http://www.softcomplex.com/products/tigra_calendar/
3 // Public Domain Software... You're welcome.
4
5 // default settins
6 var A_TCALDEF = {
7 'months' : ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'],
8 'weekdays' : ['Su', 'Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa'],
9 'yearscroll': true, // show year scroller
10 'weekstart': 0, // first day of week: 0-Su or 1-Mo
11 'centyear' : 70, // 2 digit years less than 'centyear' are in 20xx, othewise in 19xx.
12 'imgpath' : 'img/' // directory with calendar images
13 }
14 // date parsing function
15 function f_tcalParseDate (s_date) {
16
17 var re_date = /^\s*(\d{2,4})\-(\d{1,2})\-(\d{1,2})\s*$/;
18 if (!re_date.exec(s_date))
19 return alert ("Invalid date: '" + s_date + "'.\nAccepted format is yyyy-mm-dd.")
20 var n_day = Number(RegExp.$3),
21 n_month = Number(RegExp.$2),
22 n_year = Number(RegExp.$1);
23
24 if (n_year < 100)
25 n_year += (n_year < this.a_tpl.centyear ? 2000 : 1900);
26 if (n_month < 1 || n_month > 12)
27 return alert ("Invalid month value: '" + n_month + "'.\nAllowed range is 01-12.");
28 var d_numdays = new Date(n_year, n_month, 0);
29 if (n_day > d_numdays.getDate())
30 return alert("Invalid day of month value: '" + n_day + "'.\nAllowed range for selected month is 01 - " + d_numdays.getDate() + ".");
31
32 return new Date (n_year, n_month - 1, n_day);
33 }
34 // date generating function
35 function f_tcalGenerDate (d_date) {
36 return (
37 d_date.getFullYear() + "-"
38 + (d_date.getMonth() < 9 ? '0' : '') + (d_date.getMonth() + 1) + "-"
39 + (d_date.getDate() < 10 ? '0' : '') + d_date.getDate()
40 );
41 }
42
43 // implementation
44 function tcal (a_cfg, a_tpl) {
45
46 // apply default template if not specified
47 if (!a_tpl)
48 a_tpl = A_TCALDEF;
49
50 // register in global collections
51 if (!window.A_TCALS)
52 window.A_TCALS = [];
53 if (!window.A_TCALSIDX)
54 window.A_TCALSIDX = [];
55
56 this.s_id = a_cfg.id ? a_cfg.id : A_TCALS.length;
57 window.A_TCALS[this.s_id] = this;
58 window.A_TCALSIDX[window.A_TCALSIDX.length] = this;
59
60 // assign methods
61 this.f_show = f_tcalShow;
62 this.f_hide = f_tcalHide;
63 this.f_toggle = f_tcalToggle;
64 this.f_update = f_tcalUpdate;
65 this.f_relDate = f_tcalRelDate;
66 this.f_parseDate = f_tcalParseDate;
67 this.f_generDate = f_tcalGenerDate;
68
69 // create calendar icon
70 this.s_iconId = 'tcalico_' + this.s_id;
71 this.e_icon = f_getElement(this.s_iconId);
72 if (!this.e_icon) {
73 document.write('<img src="' + a_tpl.imgpath + 'cal.gif" id="' + this.s_iconId + '" onclick="A_TCALS[\'' + this.s_id + '\'].f_toggle()" class="tcalIcon" alt="Open Calendar" />');
74 this.e_icon = f_getElement(this.s_iconId);
75 }
76 // save received parameters
77 this.a_cfg = a_cfg;
78 this.a_tpl = a_tpl;
79 }
80
81 function f_tcalShow (d_date) {
82
83 // find input field
84 if (!this.a_cfg.controlname)
85 throw("TC: control name is not specified");
86 if (this.a_cfg.formname) {
87 var e_form = document.forms[this.a_cfg.formname];
88 if (!e_form)
89 throw("TC: form '" + this.a_cfg.formname + "' can not be found");
90 this.e_input = e_form.elements[this.a_cfg.controlname];
91 }
92 else
93 this.e_input = f_getElement(this.a_cfg.controlname);
94
95 if (!this.e_input || !this.e_input.tagName || this.e_input.tagName != 'INPUT')
96 throw("TC: element '" + this.a_cfg.controlname + "' does not exist in "
97 + (this.a_cfg.formname ? "form '" + this.a_cfg.controlname + "'" : 'this document'));
98
99 // dynamically create HTML elements if needed
100 this.e_div = f_getElement('tcal');
101 if (!this.e_div) {
102 this.e_div = document.createElement("DIV");
103 this.e_div.id = 'tcal';
104 document.body.appendChild(this.e_div);
105 }
106 this.e_shade = f_getElement('tcalShade');
107 if (!this.e_shade) {
108 this.e_shade = document.createElement("DIV");
109 this.e_shade.id = 'tcalShade';
110 document.body.appendChild(this.e_shade);
111 }
112 this.e_iframe = f_getElement('tcalIF')
113 if (b_ieFix && !this.e_iframe) {
114 this.e_iframe = document.createElement("IFRAME");
115 this.e_iframe.style.filter = 'alpha(opacity=0)';
116 this.e_iframe.id = 'tcalIF';
117 this.e_iframe.src = this.a_tpl.imgpath + 'pixel.gif';
118 document.body.appendChild(this.e_iframe);
119 }
120
121 // hide all calendars
122 f_tcalHideAll();
123
124 // generate HTML and show calendar
125 this.e_icon = f_getElement(this.s_iconId);
126 if (!this.f_update())
127 return;
128
129 this.e_div.style.visibility = 'visible';
130 this.e_shade.style.visibility = 'visible';
131 if (this.e_iframe)
132 this.e_iframe.style.visibility = 'visible';
133
134 // change icon and status
135 this.e_icon.src = this.a_tpl.imgpath + 'no_cal.gif';
136 this.e_icon.title = 'Close Calendar';
137 this.b_visible = true;
138 }
139
140 function f_tcalHide (n_date) {
141 if (n_date) {
142 this.e_input.value = this.f_generDate(new Date(n_date));
143 if (this.e_input.onchange) {
144 this.e_input.onchange(this.e_input);
145 }
146 }
147
148 // no action if not visible
149 if (!this.b_visible)
150 return;
151
152 // hide elements
153 if (this.e_iframe)
154 this.e_iframe.style.visibility = 'hidden';
155 if (this.e_shade)
156 this.e_shade.style.visibility = 'hidden';
157 this.e_div.style.visibility = 'hidden';
158
159 // change icon and status
160 this.e_icon = f_getElement(this.s_iconId);
161 this.e_icon.src = this.a_tpl.imgpath + 'cal.gif';
162 this.e_icon.title = 'Open Calendar';
163 this.b_visible = false;
164 }
165
166 function f_tcalToggle () {
167 return this.b_visible ? this.f_hide() : this.f_show();
168 }
169
170 function f_tcalUpdate (d_date) {
171
172 var d_client = new Date();
173 d_client.setHours(0);
174 d_client.setMinutes(0);
175 d_client.setSeconds(0);
176 d_client.setMilliseconds(0);
177
178 var d_today = this.a_cfg.today ? this.f_parseDate(this.a_cfg.today) : d_client;
179 var d_selected = this.e_input.value == ''
180 ? (this.a_cfg.selected ? this.f_parseDate(this.a_cfg.selected) : d_today)
181 : this.f_parseDate(this.e_input.value);
182
183 // figure out date to display
184 if (!d_date)
185 // selected by default
186 d_date = d_selected;
187 else if (typeof(d_date) == 'number')
188 // get from number
189 d_date = new Date(d_date);
190 else if (typeof(d_date) == 'string')
191 // parse from string
192 this.f_parseDate(d_date);
193
194 if (!d_date) return false;
195
196 // first date to display
197 var d_firstday = new Date(d_date);
198 d_firstday.setDate(1);
199 d_firstday.setDate(1 - (7 + d_firstday.getDay() - this.a_tpl.weekstart) % 7);
200
201 var a_class, s_html = '<table class="ctrl"><tbody><tr>'
202 + (this.a_tpl.yearscroll ? '<td' + this.f_relDate(d_date, -1, 'y') + ' title="Previous Year"><img src="' + this.a_tpl.imgpath + 'prev_year.gif" /></td>' : '')
203 + '<td' + this.f_relDate(d_date, -1) + ' title="Previous Month"><img src="' + this.a_tpl.imgpath + 'prev_mon.gif" /></td><th>'
204 + this.a_tpl.months[d_date.getMonth()] + ' ' + d_date.getFullYear()
205 + '</th><td' + this.f_relDate(d_date, 1) + ' title="Next Month"><img src="' + this.a_tpl.imgpath + 'next_mon.gif" /></td>'
206 + (this.a_tpl.yearscroll ? '<td' + this.f_relDate(d_date, 1, 'y') + ' title="Next Year"><img src="' + this.a_tpl.imgpath + 'next_year.gif" /></td></td>' : '')
207 + '</tr></tbody></table><table><tbody><tr class="wd">';
208
209 // print weekdays titles
210 for (var i = 0; i < 7; i++)
211 s_html += '<th>' + this.a_tpl.weekdays[(this.a_tpl.weekstart + i) % 7] + '</th>';
212 s_html += '</tr>' ;
213
214 // print calendar table
215 var d_current = new Date(d_firstday);
216 while (d_current.getMonth() == d_date.getMonth() ||
217 d_current.getMonth() == d_firstday.getMonth()) {
218
219 // print row heder
220 s_html +='<tr>';
221 for (var n_wday = 0; n_wday < 7; n_wday++) {
222
223 a_class = [];
224 // other month
225 if (d_current.getMonth() != d_date.getMonth())
226 a_class[a_class.length] = 'othermonth';
227 // weekend
228 if (d_current.getDay() == 0 || d_current.getDay() == 6)
229 a_class[a_class.length] = 'weekend';
230 // today
231 if (d_current.valueOf() == d_today.valueOf())
232 a_class[a_class.length] = 'today';
233 // selected
234 if (d_current.valueOf() == d_selected.valueOf())
235 a_class[a_class.length] = 'selected';
236
237 s_html += '<td onclick="A_TCALS[\'' + this.s_id + '\'].f_hide(' + d_current.valueOf() + ')"' + (a_class.length ? ' class="' + a_class.join(' ') + '">' : '>') + d_current.getDate() + '</td>'
238 d_current.setDate(d_current.getDate() + 1);
239 }
240 // print row footer
241 s_html +='</tr>';
242 }
243 s_html +='</tbody></table>';
244
245 // update HTML, positions and sizes
246 this.e_div.innerHTML = s_html;
247
248 var n_width = this.e_div.offsetWidth;
249 var n_height = this.e_div.offsetHeight;
250 var n_top = f_getPosition (this.e_icon, 'Top') + this.e_icon.offsetHeight;
251 var n_left = f_getPosition (this.e_icon, 'Left') - n_width + this.e_icon.offsetWidth;
252 if (n_left < 0) n_left = 0;
253
254 this.e_div.style.left = n_left + 'px';
255 this.e_div.style.top = n_top + 'px';
256
257 this.e_shade.style.width = (n_width + 8) + 'px';
258 this.e_shade.style.left = (n_left - 1) + 'px';
259 this.e_shade.style.top = (n_top - 1) + 'px';
260 this.e_shade.innerHTML = b_ieFix
261 ? '<table><tbody><tr><td rowspan="2" colspan="2" width="6"><img src="' + this.a_tpl.imgpath + 'pixel.gif"></td><td width="7" height="7" style="filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\'' + this.a_tpl.imgpath + 'shade_tr.png\', sizingMethod=\'scale\');"><img src="' + this.a_tpl.imgpath + 'pixel.gif"></td></tr><tr><td height="' + (n_height - 7) + '" style="filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\'' + this.a_tpl.imgpath + 'shade_mr.png\', sizingMethod=\'scale\');"><img src="' + this.a_tpl.imgpath + 'pixel.gif"></td></tr><tr><td width="7" style="filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\'' + this.a_tpl.imgpath + 'shade_bl.png\', sizingMethod=\'scale\');"><img src="' + this.a_tpl.imgpath + 'pixel.gif"></td><td style="filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\'' + this.a_tpl.imgpath + 'shade_bm.png\', sizingMethod=\'scale\');" height="7" align="left"><img src="' + this.a_tpl.imgpath + 'pixel.gif"></td><td style="filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\'' + this.a_tpl.imgpath + 'shade_br.png\', sizingMethod=\'scale\');"><img src="' + this.a_tpl.imgpath + 'pixel.gif"></td></tr><tbody></table>'
262 : '<table><tbody><tr><td rowspan="2" width="6"><img src="' + this.a_tpl.imgpath + 'pixel.gif"></td><td rowspan="2"><img src="' + this.a_tpl.imgpath + 'pixel.gif"></td><td width="7" height="7"><img src="' + this.a_tpl.imgpath + 'shade_tr.png"></td></tr><tr><td background="' + this.a_tpl.imgpath + 'shade_mr.png" height="' + (n_height - 7) + '"><img src="' + this.a_tpl.imgpath + 'pixel.gif"></td></tr><tr><td><img src="' + this.a_tpl.imgpath + 'shade_bl.png"></td><td background="' + this.a_tpl.imgpath + 'shade_bm.png" height="7" align="left"><img src="' + this.a_tpl.imgpath + 'pixel.gif"></td><td><img src="' + this.a_tpl.imgpath + 'shade_br.png"></td></tr><tbody></table>';
263
264 if (this.e_iframe) {
265 this.e_iframe.style.left = n_left + 'px';
266 this.e_iframe.style.top = n_top + 'px';
267 this.e_iframe.style.width = (n_width + 6) + 'px';
268 this.e_iframe.style.height = (n_height + 6) +'px';
269 }
270 return true;
271 }
272
273 function f_getPosition (e_elemRef, s_coord) {
274 var n_pos = 0, n_offset,
275 e_elem = e_elemRef;
276
277 while (e_elem) {
278 n_offset = e_elem["offset" + s_coord];
279 n_pos += n_offset;
280 e_elem = e_elem.offsetParent;
281 }
282 // margin correction in some browsers
283 if (b_ieMac)
284 n_pos += parseInt(document.body[s_coord.toLowerCase() + 'Margin']);
285 else if (b_safari)
286 n_pos -= n_offset;
287
288 e_elem = e_elemRef;
289 while (e_elem != document.body) {
290 n_offset = e_elem["scroll" + s_coord];
291 if (n_offset && e_elem.style.overflow == 'scroll')
292 n_pos -= n_offset;
293 e_elem = e_elem.parentNode;
294 }
295 return n_pos;
296 }
297
298 function f_tcalRelDate (d_date, d_diff, s_units) {
299 var s_units = (s_units == 'y' ? 'FullYear' : 'Month');
300 var d_result = new Date(d_date);
301 d_result['set' + s_units](d_date['get' + s_units]() + d_diff);
302 if (d_result.getDate() != d_date.getDate())
303 d_result.setDate(0);
304 return ' onclick="A_TCALS[\'' + this.s_id + '\'].f_update(' + d_result.valueOf() + ')"';
305 }
306
307 function f_tcalHideAll () {
308 for (var i = 0; i < window.A_TCALSIDX.length; i++)
309 window.A_TCALSIDX[i].f_hide();
310 }
311
312 f_getElement = document.all ?
313 function (s_id) { return document.all[s_id] } :
314 function (s_id) { return document.getElementById(s_id) };
315
316 if (document.addEventListener)
317 window.addEventListener('scroll', f_tcalHideAll, false);
318 if (window.attachEvent)
319 window.attachEvent('onscroll', f_tcalHideAll);
320
321 // global variables
322 var s_userAgent = navigator.userAgent.toLowerCase(),
323 re_webkit = /WebKit\/(\d+)/i;
324 var b_mac = s_userAgent.indexOf('mac') != -1,
325 b_ie5 = s_userAgent.indexOf('msie 5') != -1,
326 b_ie6 = s_userAgent.indexOf('msie 6') != -1 && s_userAgent.indexOf('opera') == -1;
327 var b_ieFix = b_ie5 || b_ie6,
328 b_ieMac = b_mac && b_ie5,
329 b_safari = b_mac && re_webkit.exec(s_userAgent) && Number(RegExp.$1) < 500;
This page took 0.135274 seconds and 6 git commands to generate.