calendar-setup.js 8.8 KB

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