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
|
// $Id: calendar-setup.js 2070 2014-01-03 01:21:42Z darkviper $
|
23
|
|
24
|
/**
|
25
|
* This function "patches" an input field (or other element) to use a calendar
|
26
|
* widget for date selection.
|
27
|
*
|
28
|
* The "params" is a single object that can have the following properties:
|
29
|
*
|
30
|
* prop. name | description
|
31
|
* -------------------------------------------------------------------------------------------------
|
32
|
* inputField | the ID of an input field to store the date
|
33
|
* displayArea | the ID of a DIV or other element to show the date
|
34
|
* button | ID of a button or other element that will trigger the calendar
|
35
|
* eventName | event that will trigger the calendar, without the "on" prefix (default: "click")
|
36
|
* ifFormat | date format that will be stored in the input field
|
37
|
* daFormat | the date format that will be used to display the date in displayArea
|
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
|
* align | alignment (default: "Br"); if you don't know what's this see the calendar documentation
|
41
|
* range | array with 2 elements. Default: [1900, 2999] -- the range of years available
|
42
|
* weekNumbers | (true/false) if it's true (default) the calendar will display week numbers
|
43
|
* flat | null or element ID; if not null the calendar will be a flat calendar having the parent with the given ID
|
44
|
* flatCallback | function that receives a JS Date object and returns an URL to point the browser to (for flat calendar)
|
45
|
* disableFunc | function that receives a JS Date object and should return true if that date has to be disabled in the calendar
|
46
|
* onSelect | function that gets called when a date is selected. You don't _have_ to supply this (the default is generally okay)
|
47
|
* onClose | function that gets called when the calendar is closed. [default]
|
48
|
* onUpdate | function that gets called after the date is updated in the input field. Receives a reference to the calendar.
|
49
|
* date | the date that the calendar will be initially displayed to
|
50
|
* showsTime | default: false; if true the calendar will include a time selector
|
51
|
* timeFormat | the time format; can be "12" or "24", default is "12"
|
52
|
* electric | if true (default) then given fields/date areas are updated for each move; otherwise they're updated only on close
|
53
|
* step | configures the step of the years in drop-down boxes; default: 2
|
54
|
* position | configures the calendar absolute position; default: null
|
55
|
* cache | if "true" (but default: "false") it will reuse the same calendar object, where possible
|
56
|
* showOthers | if "true" (but default: "false") it will show days from other months too
|
57
|
*
|
58
|
* None of them is required, they all have default values. However, if you
|
59
|
* pass none of "inputField", "displayArea" or "button" you'll get a warning
|
60
|
* saying "nothing to setup".
|
61
|
*/
|
62
|
Calendar.setup = function (params) {
|
63
|
function param_default(pname, def) { if (typeof params[pname] == "undefined") { params[pname] = def; } };
|
64
|
|
65
|
param_default("inputField", null);
|
66
|
param_default("displayArea", null);
|
67
|
param_default("button", null);
|
68
|
param_default("eventName", "click");
|
69
|
param_default("ifFormat", "%Y/%m/%d");
|
70
|
param_default("daFormat", "%Y/%m/%d");
|
71
|
param_default("singleClick", true);
|
72
|
param_default("disableFunc", null);
|
73
|
param_default("dateStatusFunc", params["disableFunc"]); // takes precedence if both are defined
|
74
|
param_default("dateText", null);
|
75
|
param_default("firstDay", null);
|
76
|
param_default("align", "Br");
|
77
|
param_default("range", [1900, 2999]);
|
78
|
param_default("weekNumbers", true);
|
79
|
param_default("flat", null);
|
80
|
param_default("flatCallback", null);
|
81
|
param_default("onSelect", null);
|
82
|
param_default("onClose", null);
|
83
|
param_default("onUpdate", null);
|
84
|
param_default("date", null);
|
85
|
param_default("showsTime", false);
|
86
|
param_default("timeFormat", "24");
|
87
|
param_default("electric", true);
|
88
|
param_default("step", 2);
|
89
|
param_default("position", null);
|
90
|
param_default("cache", false);
|
91
|
param_default("showOthers", false);
|
92
|
param_default("multiple", null);
|
93
|
|
94
|
var tmp = ["inputField", "displayArea", "button"];
|
95
|
for (var i in tmp) {
|
96
|
if (typeof params[tmp[i]] == "string") {
|
97
|
params[tmp[i]] = document.getElementById(params[tmp[i]]);
|
98
|
}
|
99
|
}
|
100
|
if (!(params.flat || params.multiple || params.inputField || params.displayArea || params.button)) {
|
101
|
alert("Calendar.setup:\n Nothing to setup (no fields found). Please check your code");
|
102
|
return false;
|
103
|
}
|
104
|
|
105
|
function onSelect(cal) {
|
106
|
var p = cal.params;
|
107
|
var update = (cal.dateClicked || p.electric);
|
108
|
if (update && p.inputField) {
|
109
|
p.inputField.value = cal.date.print(p.ifFormat);
|
110
|
if (typeof p.inputField.onchange == "function")
|
111
|
p.inputField.onchange();
|
112
|
}
|
113
|
if (update && p.displayArea)
|
114
|
p.displayArea.innerHTML = cal.date.print(p.daFormat);
|
115
|
if (update && typeof p.onUpdate == "function")
|
116
|
p.onUpdate(cal);
|
117
|
if (update && p.flat) {
|
118
|
if (typeof p.flatCallback == "function")
|
119
|
p.flatCallback(cal);
|
120
|
}
|
121
|
if (update && p.singleClick && cal.dateClicked)
|
122
|
cal.callCloseHandler();
|
123
|
};
|
124
|
|
125
|
if (params.flat != null) {
|
126
|
if (typeof params.flat == "string")
|
127
|
params.flat = document.getElementById(params.flat);
|
128
|
if (!params.flat) {
|
129
|
alert("Calendar.setup:\n Flat specified but can't find parent.");
|
130
|
return false;
|
131
|
}
|
132
|
var cal = new Calendar(params.firstDay, params.date, params.onSelect || onSelect);
|
133
|
cal.showsOtherMonths = params.showOthers;
|
134
|
cal.showsTime = params.showsTime;
|
135
|
cal.time24 = (params.timeFormat == "24");
|
136
|
cal.params = params;
|
137
|
cal.weekNumbers = params.weekNumbers;
|
138
|
cal.setRange(params.range[0], params.range[1]);
|
139
|
cal.setDateStatusHandler(params.dateStatusFunc);
|
140
|
cal.getDateText = params.dateText;
|
141
|
if (params.ifFormat) {
|
142
|
cal.setDateFormat(params.ifFormat);
|
143
|
}
|
144
|
if (params.inputField && typeof params.inputField.value == "string") {
|
145
|
cal.parseDate(params.inputField.value);
|
146
|
}
|
147
|
cal.create(params.flat);
|
148
|
cal.show();
|
149
|
return false;
|
150
|
}
|
151
|
|
152
|
var triggerEl = params.button || params.displayArea || params.inputField;
|
153
|
triggerEl["on" + params.eventName] = function() {
|
154
|
var dateEl = params.inputField || params.displayArea;
|
155
|
var dateFmt = params.inputField ? params.ifFormat : params.daFormat;
|
156
|
var mustCreate = false;
|
157
|
var cal = window.calendar;
|
158
|
// if (dateEl)
|
159
|
// params.date = Date.parseDate(dateEl.value || dateEl.innerHTML, dateFmt);
|
160
|
//by nib: checks wether inputfield is empty or not. orig code only checked wether inputfield exists, so param.date was never read!
|
161
|
if (dateEl.value.replace(/^\s+|\s+$/g,"").length != 0 || dateEl.innerHTML.replace(/^\s+|\s+$/g,"").length != 0)
|
162
|
params.date = Date.parseDate(dateEl.value || dateEl.innerHTML, dateFmt);
|
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
|
if (params.multiple) {
|
178
|
cal.multiple = {};
|
179
|
for (var i = params.multiple.length; --i >= 0;) {
|
180
|
var d = params.multiple[i];
|
181
|
var ds = d.print("%Y%m%d");
|
182
|
cal.multiple[ds] = d;
|
183
|
}
|
184
|
}
|
185
|
cal.showsOtherMonths = params.showOthers;
|
186
|
cal.yearStep = params.step;
|
187
|
cal.setRange(params.range[0], params.range[1]);
|
188
|
cal.params = params;
|
189
|
cal.setDateStatusHandler(params.dateStatusFunc);
|
190
|
cal.getDateText = params.dateText;
|
191
|
cal.setDateFormat(dateFmt);
|
192
|
if (mustCreate)
|
193
|
cal.create();
|
194
|
cal.refresh();
|
195
|
if (!params.position)
|
196
|
cal.showAtElement(params.button || params.displayArea || params.inputField, params.align);
|
197
|
else
|
198
|
cal.showAt(params.position[0], params.position[1]);
|
199
|
return false;
|
200
|
};
|
201
|
|
202
|
return cal;
|
203
|
};
|