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