1 | /* Copyright Mihai Bazon, 2002, 2003 | http://dynarch.com/mishoo/ |
---|
2 | * --------------------------------------------------------------------------- |
---|
3 | * |
---|
4 | * The DHTML Calendar |
---|
5 | * |
---|
6 | * Details and latest version at: |
---|
7 | * http://dynarch.com/mishoo/calendar.epl |
---|
8 | * |
---|
9 | * This script is distributed under the GNU Lesser General Public License. |
---|
10 | * Read the entire license text here: http://www.gnu.org/licenses/lgpl.html |
---|
11 | * |
---|
12 | * This file defines helper functions for setting up the calendar. They are |
---|
13 | * intended to help non-programmers get a working calendar on their site |
---|
14 | * quickly. This script should not be seen as part of the calendar. It just |
---|
15 | * shows you what one can do with the calendar, while in the same time |
---|
16 | * providing a quick and simple method for setting it up. If you need |
---|
17 | * exhaustive customization of the calendar creation process feel free to |
---|
18 | * modify this code to suit your needs (this is recommended and much better |
---|
19 | * than modifying calendar.js itself). |
---|
20 | */ |
---|
21 | |
---|
22 | |
---|
23 | /** |
---|
24 | * This function "patches" an input field (or other element) to use a calendar |
---|
25 | * widget for date selection. |
---|
26 | * |
---|
27 | * The "params" is a single object that can have the following properties: |
---|
28 | * |
---|
29 | * prop. name | description |
---|
30 | * ------------------------------------------------------------------------------------------------- |
---|
31 | * inputField | the ID of an input field to store the date |
---|
32 | * displayArea | the ID of a DIV or other element to show the date |
---|
33 | * button | ID of a button or other element that will trigger the calendar |
---|
34 | * eventName | event that will trigger the calendar, without the "on" prefix (default: "click") |
---|
35 | * ifFormat | date format that will be stored in the input field |
---|
36 | * daFormat | the date format that will be used to display the date in displayArea |
---|
37 | * titleFormat | the format to show the month in the title, default '%B, %Y' |
---|
38 | * singleClick | (true/false) wether the calendar is in single click mode or not (default: true) |
---|
39 | * firstDay | numeric: 0 to 6. "0" means display Sunday first, "1" means display Monday first, etc. |
---|
40 | * disableFirstDowChange| (true/false) disables manual change of first day of week |
---|
41 | * align | alignment (default: "Br"); if you don't know what's this see the calendar documentation |
---|
42 | * range | array with 2 elements. Default: [1900, 2999] -- the range of years available |
---|
43 | * weekNumbers | (true/false) if it's true (default) the calendar will display week numbers |
---|
44 | * flat | null or element ID; if not null the calendar will be a flat calendar having the parent with the given ID |
---|
45 | * flatCallback | function that receives a JS Date object and returns an URL to point the browser to (for flat calendar) |
---|
46 | * flatWeekCallback| gets called if a weeknumber get clicked, params are the cal-object and a date-object representing the start of the week |
---|
47 | * flatWeekTTip | Tooltip for the weeknumber (shown only if flatWeekCallback is set) |
---|
48 | * flatMonthCallback| gets called if a month (title) get clicked, params are the cal-object and a date-object representing the start of the month |
---|
49 | * flatMonthTTip | Tooltip for the month (shown only if flatMonthCallback is set) |
---|
50 | * disableFunc | function that receives a JS Date object and should return true if that date has to be disabled in the calendar |
---|
51 | * onSelect | function that gets called when a date is selected. You don't _have_ to supply this (the default is generally okay) |
---|
52 | * onClose | function that gets called when the calendar is closed. [default] |
---|
53 | * onUpdate | function that gets called after the date is updated in the input field. Receives a reference to the calendar. |
---|
54 | * date | the date that the calendar will be initially displayed to |
---|
55 | * showsTime | default: false; if true the calendar will include a time selector |
---|
56 | * timeFormat | the time format; can be "12" or "24", default is "12" |
---|
57 | * electric | if true (default) then given fields/date areas are updated for each move; otherwise they're updated only on close |
---|
58 | * step | configures the step of the years in drop-down boxes; default: 2 |
---|
59 | * position | configures the calendar absolute position; default: null |
---|
60 | * cache | if "true" (but default: "false") it will reuse the same calendar object, where possible |
---|
61 | * showOthers | if "true" (but default: "false") it will show days from other months too |
---|
62 | * |
---|
63 | * None of them is required, they all have default values. However, if you |
---|
64 | * pass none of "inputField", "displayArea" or "button" you'll get a warning |
---|
65 | * saying "nothing to setup". |
---|
66 | */ |
---|
67 | Calendar.setup = function (params) { |
---|
68 | function param_default(pname, def) { if (typeof params[pname] == "undefined") { params[pname] = def; } }; |
---|
69 | |
---|
70 | param_default("inputField", null); |
---|
71 | param_default("displayArea", null); |
---|
72 | param_default("button", null); |
---|
73 | param_default("eventName", "click"); |
---|
74 | param_default("ifFormat", "%Y/%m/%d"); |
---|
75 | param_default("daFormat", "%Y/%m/%d"); |
---|
76 | param_default("titleFormat", "%B, %Y"); |
---|
77 | param_default("singleClick", true); |
---|
78 | param_default("disableFunc", null); |
---|
79 | param_default("dateStatusFunc", params["disableFunc"]); // takes precedence if both are defined |
---|
80 | param_default("firstDay", 0); // defaults to "Sunday" first |
---|
81 | param_default("disableFirstDowChange", false); |
---|
82 | param_default("align", "Br"); |
---|
83 | param_default("range", [1900, 2999]); |
---|
84 | param_default("weekNumbers", true); |
---|
85 | param_default("flat", null); |
---|
86 | param_default("flatCallback", null); |
---|
87 | param_default("flatWeekCallback",null); |
---|
88 | param_default("flatWeekTTip", null); |
---|
89 | param_default("flatmonthCallback",null); |
---|
90 | param_default("flatmonthTTip", null); |
---|
91 | param_default("onSelect", null); |
---|
92 | param_default("onClose", null); |
---|
93 | param_default("onUpdate", null); |
---|
94 | param_default("date", null); |
---|
95 | param_default("showsTime", false); |
---|
96 | param_default("timeFormat", "24"); |
---|
97 | param_default("electric", true); |
---|
98 | param_default("step", 2); |
---|
99 | param_default("position", null); |
---|
100 | param_default("cache", false); |
---|
101 | param_default("showOthers", false); |
---|
102 | param_default("overMe", false); |
---|
103 | |
---|
104 | var tmp = ["inputField", "displayArea", "button"]; |
---|
105 | for (var i in tmp) { |
---|
106 | if (typeof params[tmp[i]] == "string") { |
---|
107 | params[tmp[i]] = document.getElementById(params[tmp[i]]); |
---|
108 | } |
---|
109 | } |
---|
110 | if (!(params.flat || params.inputField || params.displayArea || params.button)) { |
---|
111 | alert("Calendar.setup:\n Nothing to setup (no fields found). Please check your code"); |
---|
112 | return false; |
---|
113 | } |
---|
114 | |
---|
115 | function onSelect(cal) { |
---|
116 | var p = cal.params; |
---|
117 | var update = (cal.dateClicked || p.electric); |
---|
118 | if (update && p.flat) { |
---|
119 | if (typeof p.flatCallback == "function") |
---|
120 | p.flatCallback(cal); |
---|
121 | else |
---|
122 | alert("No flatCallback given -- doing nothing."); |
---|
123 | return false; |
---|
124 | } |
---|
125 | if (update && p.inputField) { |
---|
126 | p.inputField.value = cal.date.print(p.ifFormat); |
---|
127 | if (typeof p.inputField.onchange == "function") |
---|
128 | p.inputField.onchange(); |
---|
129 | } |
---|
130 | if (update && p.displayArea) |
---|
131 | p.displayArea.innerHTML = cal.date.print(p.daFormat); |
---|
132 | if (update && p.singleClick && cal.dateClicked) |
---|
133 | cal.callCloseHandler(); |
---|
134 | if (update && typeof p.onUpdate == "function") |
---|
135 | p.onUpdate(cal); |
---|
136 | }; |
---|
137 | |
---|
138 | if (params.flat != null) { |
---|
139 | if (typeof params.flat == "string") |
---|
140 | params.flat = document.getElementById(params.flat); |
---|
141 | if (!params.flat) { |
---|
142 | alert("Calendar.setup:\n Flat specified but can't find parent."); |
---|
143 | return false; |
---|
144 | } |
---|
145 | var cal = new Calendar(params.firstDay, params.date, params.onSelect || onSelect); |
---|
146 | cal.showsTime = params.showsTime; |
---|
147 | cal.time24 = (params.timeFormat == "24"); |
---|
148 | cal.params = params; |
---|
149 | cal.setOverMe(params.overMe); |
---|
150 | cal.weekNumbers = params.weekNumbers; |
---|
151 | cal.setRange(params.range[0], params.range[1]); |
---|
152 | cal.setDateStatusHandler(params.dateStatusFunc); |
---|
153 | cal.showsOtherMonths = params.showOthers; |
---|
154 | cal.create(params.flat); |
---|
155 | cal.show(); |
---|
156 | return false; |
---|
157 | } |
---|
158 | |
---|
159 | var triggerEl = params.button || params.displayArea || params.inputField; |
---|
160 | triggerEl["on" + params.eventName] = function() { |
---|
161 | var dateEl = params.inputField || params.displayArea; |
---|
162 | var dateFmt = params.inputField ? params.ifFormat : params.daFormat; |
---|
163 | var mustCreate = false; |
---|
164 | var cal = window.calendar; |
---|
165 | if (!(cal && params.cache)) { |
---|
166 | window.calendar = cal = new Calendar(params.firstDay, |
---|
167 | params.date, |
---|
168 | params.onSelect || onSelect, |
---|
169 | params.onClose || function(cal) { cal.hide(); }); |
---|
170 | cal.showsTime = params.showsTime; |
---|
171 | cal.time24 = (params.timeFormat == "24"); |
---|
172 | cal.weekNumbers = params.weekNumbers; |
---|
173 | mustCreate = true; |
---|
174 | } else { |
---|
175 | if (params.date) |
---|
176 | cal.setDate(params.date); |
---|
177 | cal.hide(); |
---|
178 | } |
---|
179 | cal.setOverMe(params.overMe); |
---|
180 | cal.showsOtherMonths = params.showOthers; |
---|
181 | cal.yearStep = params.step; |
---|
182 | cal.setRange(params.range[0], params.range[1]); |
---|
183 | cal.params = params; |
---|
184 | cal.setDateStatusHandler(params.dateStatusFunc); |
---|
185 | cal.setDateFormat(dateFmt); |
---|
186 | if (mustCreate) |
---|
187 | cal.create(); |
---|
188 | cal.parseDate(dateEl.value || dateEl.innerHTML); |
---|
189 | cal.refresh(); |
---|
190 | if (!params.position) |
---|
191 | cal.showAtElement(params.button || params.displayArea || params.inputField, params.align); |
---|
192 | else |
---|
193 | cal.showAt(params.position[0], params.position[1]); |
---|
194 | return false; |
---|
195 | }; |
---|
196 | }; |
---|