source: trunk/phpgwapi/js/jscalendar/calendar-setup.js @ 2

Revision 2, 8.9 KB checked in by niltonneto, 17 years ago (diff)

Removida todas as tags usadas pelo CVS ($Id, $Source).
Primeira versão no CVS externo.

  • Property svn:eol-style set to native
  • Property svn:executable set to *
Line 
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 */
67Calendar.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
103        var tmp = ["inputField", "displayArea", "button"];
104        for (var i in tmp) {
105                if (typeof params[tmp[i]] == "string") {
106                        params[tmp[i]] = document.getElementById(params[tmp[i]]);
107                }
108        }
109        if (!(params.flat || params.inputField || params.displayArea || params.button)) {
110                alert("Calendar.setup:\n  Nothing to setup (no fields found).  Please check your code");
111                return false;
112        }
113
114        function onSelect(cal) {
115                var p = cal.params;
116                var update = (cal.dateClicked || p.electric);
117                if (update && p.flat) {
118                        if (typeof p.flatCallback == "function")
119                                p.flatCallback(cal);
120                        else
121                                alert("No flatCallback given -- doing nothing.");
122                        return false;
123                }
124                if (update && p.inputField) {
125                        p.inputField.value = cal.date.print(p.ifFormat);
126                        if (typeof p.inputField.onchange == "function")
127                                p.inputField.onchange();
128                }
129                if (update && p.displayArea)
130                        p.displayArea.innerHTML = cal.date.print(p.daFormat);
131                if (update && p.singleClick && cal.dateClicked)
132                        cal.callCloseHandler();
133                if (update && typeof p.onUpdate == "function")
134                        p.onUpdate(cal);
135        };
136
137        if (params.flat != null) {
138                if (typeof params.flat == "string")
139                        params.flat = document.getElementById(params.flat);
140                if (!params.flat) {
141                        alert("Calendar.setup:\n  Flat specified but can't find parent.");
142                        return false;
143                }
144                var cal = new Calendar(params.firstDay, params.date, params.onSelect || onSelect);
145                cal.showsTime = params.showsTime;
146                cal.time24 = (params.timeFormat == "24");
147                cal.params = params;
148                cal.weekNumbers = params.weekNumbers;
149                cal.setRange(params.range[0], params.range[1]);
150                cal.setDateStatusHandler(params.dateStatusFunc);
151                cal.showsOtherMonths = params.showOthers;
152                cal.create(params.flat);
153                cal.show();
154                return false;
155        }
156
157        var triggerEl = params.button || params.displayArea || params.inputField;
158        triggerEl["on" + params.eventName] = function() {
159                var dateEl = params.inputField || params.displayArea;
160                var dateFmt = params.inputField ? params.ifFormat : params.daFormat;
161                var mustCreate = false;
162                var cal = window.calendar;
163                if (!(cal && params.cache)) {
164                        window.calendar = cal = new Calendar(params.firstDay,
165                                                             params.date,
166                                                             params.onSelect || onSelect,
167                                                             params.onClose || function(cal) { cal.hide(); });
168                        cal.showsTime = params.showsTime;
169                        cal.time24 = (params.timeFormat == "24");
170                        cal.weekNumbers = params.weekNumbers;
171                        mustCreate = true;
172                } else {
173                        if (params.date)
174                                cal.setDate(params.date);
175                        cal.hide();
176                }
177                cal.showsOtherMonths = params.showOthers;
178                cal.yearStep = params.step;
179                cal.setRange(params.range[0], params.range[1]);
180                cal.params = params;
181                cal.setDateStatusHandler(params.dateStatusFunc);
182                cal.setDateFormat(dateFmt);
183                if (mustCreate)
184                        cal.create();
185                cal.parseDate(dateEl.value || dateEl.innerHTML);
186                cal.refresh();
187                if (!params.position)
188                        cal.showAtElement(params.button || params.displayArea || params.inputField, params.align);
189                else
190                        cal.showAt(params.position[0], params.position[1]);
191                return false;
192        };
193};
Note: See TracBrowser for help on using the repository browser.