annotate public/javascripts/calendar/calendar-setup.js @ 8:0c83d98252d9 yuya

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