de6ff591f874a859f1bd7bb85a480520c8382e39
[freeside.git] / httemplate / elements / calendar-setup.js
1 /*  Copyright Mihai Bazon, 2002, 2003  |  http://students.infoiasi.ro/~mishoo
2  * ---------------------------------------------------------------------------
3  *
4  * The DHTML Calendar
5  *
6  * Details and latest version at:
7  * http://students.infoiasi.ro/~mishoo/site/calendar.epl
8  *
9  * Feel free to use this script under the terms of the GNU Lesser General
10  * Public License, as long as you do not remove or alter this notice.
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.
15  */
16
17 // $Id: calendar-setup.js,v 1.2 2003-09-30 08:23:15 ivan Exp $
18
19 /**
20  *  This function "patches" an input field (or other element) to use a calendar
21  *  widget for date selection.
22  *
23  *  The "params" is a single object that can have the following properties:
24  *
25  *    prop. name   | description
26  *  -------------------------------------------------------------------------------------------------
27  *   inputField    | the ID of an input field to store the date
28  *   displayArea   | the ID of a DIV or other element to show the date
29  *   button        | ID of a button or other element that will trigger the calendar
30  *   eventName     | event that will trigger the calendar, without the "on" prefix (default: "click")
31  *   ifFormat      | date format that will be stored in the input field
32  *   daFormat      | the date format that will be used to display the date in displayArea
33  *   singleClick   | (true/false) wether the calendar is in single click mode or not (default: true)
34  *   mondayFirst   | (true/false) if true Monday is the first day of week, Sunday otherwise (default: false)
35  *   align         | alignment (default: "Bl"); if you don't know what's this see the calendar documentation
36  *   range         | array with 2 elements.  Default: [1900, 2999] -- the range of years available
37  *   weekNumbers   | (true/false) if it's true (default) the calendar will display week numbers
38  *   flat          | null or element ID; if not null the calendar will be a flat calendar having the parent with the given ID
39  *   flatCallback  | function that receives a JS Date object and returns an URL to point the browser to (for flat calendar)
40  *   disableFunc   | function that receives a JS Date object and should return true if that date has to be disabled in the calendar
41  *
42  *  None of them is required, they all have default values.  However, if you
43  *  pass none of "inputField", "displayArea" or "button" you'll get a warning
44  *  saying "nothing to setup".
45  */
46 Calendar.setup = function (params) {
47         function param_default(pname, def) { if (typeof params[pname] == "undefined") { params[pname] = def; } };
48
49         param_default("inputField",    null);
50         param_default("displayArea",   null);
51         param_default("button",        null);
52         param_default("eventName",     "click");
53         param_default("ifFormat",      "y/mm/dd");
54         param_default("daFormat",      "y/mm/dd");
55         param_default("singleClick",   true);
56         param_default("disableFunc",   null);
57         param_default("mondayFirst",   false);
58         param_default("align",         "Bl");
59         param_default("range",         [1900, 2999]);
60         param_default("weekNumbers",   true);
61         param_default("flat",          null);
62         param_default("flatCallback",  null);
63
64         var tmp = ["inputField", "displayArea", "button"];
65         for (var i in tmp) {
66                 if (typeof params[tmp[i]] == "string") {
67                         params[tmp[i]] = document.getElementById(params[tmp[i]]);
68                 }
69         }
70         if (!(params.flat || params.inputField || params.displayArea || params.button)) {
71                 alert("Calendar.setup:\n  Nothing to setup (no fields found).  Please check your code");
72                 return false;
73         }
74
75         function onSelect(cal) {
76                 if (cal.params.flat) {
77                         if (typeof cal.params.flatCallback == "function") {
78                                 cal.params.flatCallback(cal);
79                         } else {
80                                 alert("No flatCallback given -- doing nothing.");
81                         }
82                         return false;
83                 }
84                 if (cal.params.inputField) {
85                         cal.params.inputField.value = cal.date.print(cal.params.ifFormat);
86                 }
87                 if (cal.params.displayArea) {
88                         cal.params.displayArea.innerHTML = cal.date.print(cal.params.daFormat);
89                 }
90                 if (cal.params.singleClick && cal.dateClicked) {
91                         cal.callCloseHandler();
92                 }
93         };
94
95         if (params.flat != null) {
96                 params.flat = document.getElementById(params.flat);
97                 if (!params.flat) {
98                         alert("Calendar.setup:\n  Flat specified but can't find parent.");
99                         return false;
100                 }
101                 var cal = new Calendar(params.mondayFirst, null, onSelect);
102                 cal.params = params;
103                 cal.weekNumbers = params.weekNumbers;
104                 cal.setRange(params.range[0], params.range[1]);
105                 cal.setDisabledHandler(params.disableFunc);
106                 cal.create(params.flat);
107                 cal.show();
108                 return false;
109         }
110
111         var triggerEl = params.button || params.displayArea || params.inputField;
112         triggerEl["on" + params.eventName] = function() {
113                 var dateEl = params.inputField || params.displayArea;
114                 var dateFmt = params.inputField ? params.ifFormat : params.daFormat;
115                 var mustCreate = false;
116                 if (!window.calendar) {
117                         window.calendar = new Calendar(params.mondayFirst, null, onSelect, function(cal) { cal.hide(); });
118                         window.calendar.weekNumbers = params.weekNumbers;
119                         mustCreate = true;
120                 } else {
121                         window.calendar.hide();
122                 }
123                 window.calendar.setRange(params.range[0], params.range[1]);
124                 window.calendar.params = params;
125                 window.calendar.setDisabledHandler(params.disableFunc);
126                 window.calendar.setDateFormat(dateFmt);
127                 if (mustCreate) {
128                         window.calendar.create();
129                 }
130                 window.calendar.parseDate(dateEl.value || dateEl.innerHTML);
131                 window.calendar.refresh();
132                 window.calendar.showAtElement(params.displayArea || params.inputField, params.align);
133                 return false;
134         };
135 };