Chris@0: /* Copyright Mihai Bazon, 2002, 2003 | http://dynarch.com/mishoo/ Chris@0: * --------------------------------------------------------------------------- Chris@0: * Chris@0: * The DHTML Calendar Chris@0: * Chris@0: * Details and latest version at: Chris@0: * http://dynarch.com/mishoo/calendar.epl Chris@0: * Chris@0: * This script is distributed under the GNU Lesser General Public License. Chris@0: * Read the entire license text here: http://www.gnu.org/licenses/lgpl.html Chris@0: * Chris@0: * This file defines helper functions for setting up the calendar. They are Chris@0: * intended to help non-programmers get a working calendar on their site Chris@0: * quickly. This script should not be seen as part of the calendar. It just Chris@0: * shows you what one can do with the calendar, while in the same time Chris@0: * providing a quick and simple method for setting it up. If you need Chris@0: * exhaustive customization of the calendar creation process feel free to Chris@0: * modify this code to suit your needs (this is recommended and much better Chris@0: * than modifying calendar.js itself). Chris@0: */ Chris@0: Chris@0: // $Id: calendar-setup.js,v 1.25 2005/03/07 09:51:33 mishoo Exp $ Chris@0: Chris@0: /** Chris@0: * This function "patches" an input field (or other element) to use a calendar Chris@0: * widget for date selection. Chris@0: * Chris@0: * The "params" is a single object that can have the following properties: Chris@0: * Chris@0: * prop. name | description Chris@0: * ------------------------------------------------------------------------------------------------- Chris@0: * inputField | the ID of an input field to store the date Chris@0: * displayArea | the ID of a DIV or other element to show the date Chris@0: * button | ID of a button or other element that will trigger the calendar Chris@0: * eventName | event that will trigger the calendar, without the "on" prefix (default: "click") Chris@0: * ifFormat | date format that will be stored in the input field Chris@0: * daFormat | the date format that will be used to display the date in displayArea Chris@0: * singleClick | (true/false) wether the calendar is in single click mode or not (default: true) Chris@0: * firstDay | numeric: 0 to 6. "0" means display Sunday first, "1" means display Monday first, etc. Chris@0: * align | alignment (default: "Br"); if you don't know what's this see the calendar documentation Chris@0: * range | array with 2 elements. Default: [1900, 2999] -- the range of years available Chris@0: * weekNumbers | (true/false) if it's true (default) the calendar will display week numbers Chris@0: * flat | null or element ID; if not null the calendar will be a flat calendar having the parent with the given ID Chris@0: * flatCallback | function that receives a JS Date object and returns an URL to point the browser to (for flat calendar) Chris@0: * disableFunc | function that receives a JS Date object and should return true if that date has to be disabled in the calendar Chris@0: * onSelect | function that gets called when a date is selected. You don't _have_ to supply this (the default is generally okay) Chris@0: * onClose | function that gets called when the calendar is closed. [default] Chris@0: * onUpdate | function that gets called after the date is updated in the input field. Receives a reference to the calendar. Chris@0: * date | the date that the calendar will be initially displayed to Chris@0: * showsTime | default: false; if true the calendar will include a time selector Chris@0: * timeFormat | the time format; can be "12" or "24", default is "12" Chris@0: * electric | if true (default) then given fields/date areas are updated for each move; otherwise they're updated only on close Chris@0: * step | configures the step of the years in drop-down boxes; default: 2 Chris@0: * position | configures the calendar absolute position; default: null Chris@0: * cache | if "true" (but default: "false") it will reuse the same calendar object, where possible Chris@0: * showOthers | if "true" (but default: "false") it will show days from other months too Chris@0: * Chris@0: * None of them is required, they all have default values. However, if you Chris@0: * pass none of "inputField", "displayArea" or "button" you'll get a warning Chris@0: * saying "nothing to setup". Chris@0: */ Chris@0: Calendar.setup = function (params) { Chris@0: function param_default(pname, def) { if (typeof params[pname] == "undefined") { params[pname] = def; } }; Chris@0: Chris@0: param_default("inputField", null); Chris@0: param_default("displayArea", null); Chris@0: param_default("button", null); Chris@0: param_default("eventName", "click"); Chris@0: param_default("ifFormat", "%Y/%m/%d"); Chris@0: param_default("daFormat", "%Y/%m/%d"); Chris@0: param_default("singleClick", true); Chris@0: param_default("disableFunc", null); Chris@0: param_default("dateStatusFunc", params["disableFunc"]); // takes precedence if both are defined Chris@0: param_default("dateText", null); Chris@0: param_default("firstDay", null); Chris@0: param_default("align", "Br"); Chris@0: param_default("range", [1900, 2999]); Chris@0: param_default("weekNumbers", true); Chris@0: param_default("flat", null); Chris@0: param_default("flatCallback", null); Chris@0: param_default("onSelect", null); Chris@0: param_default("onClose", null); Chris@0: param_default("onUpdate", null); Chris@0: param_default("date", null); Chris@0: param_default("showsTime", false); Chris@0: param_default("timeFormat", "24"); Chris@0: param_default("electric", true); Chris@0: param_default("step", 2); Chris@0: param_default("position", null); Chris@0: param_default("cache", false); Chris@0: param_default("showOthers", false); Chris@0: param_default("multiple", null); Chris@0: Chris@0: var tmp = ["inputField", "displayArea", "button"]; Chris@0: for (var i in tmp) { Chris@0: if (typeof params[tmp[i]] == "string") { Chris@0: params[tmp[i]] = document.getElementById(params[tmp[i]]); Chris@0: } Chris@0: } Chris@0: if (!(params.flat || params.multiple || params.inputField || params.displayArea || params.button)) { Chris@0: alert("Calendar.setup:\n Nothing to setup (no fields found). Please check your code"); Chris@0: return false; Chris@0: } Chris@0: Chris@0: function onSelect(cal) { Chris@0: var p = cal.params; Chris@0: var update = (cal.dateClicked || p.electric); Chris@0: if (update && p.inputField) { Chris@0: p.inputField.value = cal.date.print(p.ifFormat); Chris@0: if (typeof p.inputField.onchange == "function") Chris@0: p.inputField.onchange(); Chris@0: } Chris@0: if (update && p.displayArea) Chris@0: p.displayArea.innerHTML = cal.date.print(p.daFormat); Chris@0: if (update && typeof p.onUpdate == "function") Chris@0: p.onUpdate(cal); Chris@0: if (update && p.flat) { Chris@0: if (typeof p.flatCallback == "function") Chris@0: p.flatCallback(cal); Chris@0: } Chris@0: if (update && p.singleClick && cal.dateClicked) Chris@0: cal.callCloseHandler(); Chris@0: }; Chris@0: Chris@0: if (params.flat != null) { Chris@0: if (typeof params.flat == "string") Chris@0: params.flat = document.getElementById(params.flat); Chris@0: if (!params.flat) { Chris@0: alert("Calendar.setup:\n Flat specified but can't find parent."); Chris@0: return false; Chris@0: } Chris@0: var cal = new Calendar(params.firstDay, params.date, params.onSelect || onSelect); Chris@0: cal.showsOtherMonths = params.showOthers; Chris@0: cal.showsTime = params.showsTime; Chris@0: cal.time24 = (params.timeFormat == "24"); Chris@0: cal.params = params; Chris@0: cal.weekNumbers = params.weekNumbers; Chris@0: cal.setRange(params.range[0], params.range[1]); Chris@0: cal.setDateStatusHandler(params.dateStatusFunc); Chris@0: cal.getDateText = params.dateText; Chris@0: if (params.ifFormat) { Chris@0: cal.setDateFormat(params.ifFormat); Chris@0: } Chris@0: if (params.inputField && typeof params.inputField.value == "string") { Chris@0: cal.parseDate(params.inputField.value); Chris@0: } Chris@0: cal.create(params.flat); Chris@0: cal.show(); Chris@0: return false; Chris@0: } Chris@0: Chris@0: var triggerEl = params.button || params.displayArea || params.inputField; Chris@0: triggerEl["on" + params.eventName] = function() { Chris@0: var dateEl = params.inputField || params.displayArea; Chris@0: var dateFmt = params.inputField ? params.ifFormat : params.daFormat; Chris@0: var mustCreate = false; Chris@0: var cal = window.calendar; Chris@0: if (dateEl) Chris@0: params.date = Date.parseDate(dateEl.value || dateEl.innerHTML, dateFmt); Chris@0: if (!(cal && params.cache)) { Chris@0: window.calendar = cal = new Calendar(params.firstDay, Chris@0: params.date, Chris@0: params.onSelect || onSelect, Chris@0: params.onClose || function(cal) { cal.hide(); }); Chris@0: cal.showsTime = params.showsTime; Chris@0: cal.time24 = (params.timeFormat == "24"); Chris@0: cal.weekNumbers = params.weekNumbers; Chris@0: mustCreate = true; Chris@0: } else { Chris@0: if (params.date) Chris@0: cal.setDate(params.date); Chris@0: cal.hide(); Chris@0: } Chris@0: if (params.multiple) { Chris@0: cal.multiple = {}; Chris@0: for (var i = params.multiple.length; --i >= 0;) { Chris@0: var d = params.multiple[i]; Chris@0: var ds = d.print("%Y%m%d"); Chris@0: cal.multiple[ds] = d; Chris@0: } Chris@0: } Chris@0: cal.showsOtherMonths = params.showOthers; Chris@0: cal.yearStep = params.step; Chris@0: cal.setRange(params.range[0], params.range[1]); Chris@0: cal.params = params; Chris@0: cal.setDateStatusHandler(params.dateStatusFunc); Chris@0: cal.getDateText = params.dateText; Chris@0: cal.setDateFormat(dateFmt); Chris@0: if (mustCreate) Chris@0: cal.create(); Chris@0: cal.refresh(); Chris@0: if (!params.position) Chris@0: cal.showAtElement(params.button || params.displayArea || params.inputField); Chris@0: else Chris@0: cal.showAt(params.position[0], params.position[1]); Chris@0: return false; Chris@0: }; Chris@0: Chris@0: return cal; Chris@0: };