c878adbfba8788a984b877580723973dde431786.svn-base 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. if (!dojo._hasResource["dijit._Calendar"]) { // _hasResource checks added by
  2. // build. Do not use
  3. // _hasResource directly in your
  4. // code.
  5. dojo._hasResource["dijit._Calendar"] = true;
  6. dojo.provide("dijit._Calendar");
  7. dojo.require("dojo.cldr.supplemental");
  8. dojo.require("dojo.date");
  9. dojo.require("dojo.date.locale");
  10. dojo.require("dijit._Widget");
  11. dojo.require("dijit._Templated");
  12. dojo.declare("dijit._Calendar", [dijit._Widget, dijit._Templated], {
  13. /*
  14. * summary: A simple GUI for choosing a date in the context of a monthly
  15. * calendar.
  16. *
  17. * description: This widget is used internally by other widgets and is
  18. * not accessible as a standalone widget. This widget can't be used in a
  19. * form because it doesn't serialize the date to an <input> field. For a
  20. * form element, use DateTextBox instead.
  21. *
  22. * Note that the parser takes all dates attributes passed in the `RFC
  23. * 3339` format: http://www.faqs.org/rfcs/rfc3339.html
  24. * (2005-06-30T08:05:00-07:00) so that they are serializable and
  25. * locale-independent.
  26. *
  27. * usage: var calendar = new dijit._Calendar({},
  28. * dojo.byId("calendarNode")); -or- <div dojoType="dijit._Calendar"></div>
  29. */
  30. templateString : "<table cellspacing=\"0\" cellpadding=\"0\" class=\"dijitCalendarContainer\">\n\t<thead>\n\t\t<tr class=\"dijitReset dijitCalendarMonthContainer\" valign=\"top\">\n\t\t\t<th class='dijitReset' dojoAttachPoint=\"decrementMonth\">\n\t\t\t\t<span class=\"dijitInline dijitCalendarIncrementControl dijitCalendarDecrease\"><span dojoAttachPoint=\"decreaseArrowNode\" class=\"dijitA11ySideArrow dijitCalendarIncrementControl dijitCalendarDecreaseInner\">-</span></span>\n\t\t\t</th>\n\t\t\t<th class='dijitReset' colspan=\"5\">\n\t\t\t\t<div dojoAttachPoint=\"monthLabelSpacer\" class=\"dijitCalendarMonthLabelSpacer\"></div>\n\t\t\t\t<div dojoAttachPoint=\"monthLabelNode\" class=\"dijitCalendarMonth\"></div>\n\t\t\t</th>\n\t\t\t<th class='dijitReset' dojoAttachPoint=\"incrementMonth\">\n\t\t\t\t<div class=\"dijitInline dijitCalendarIncrementControl dijitCalendarIncrease\"><span dojoAttachPoint=\"increaseArrowNode\" class=\"dijitA11ySideArrow dijitCalendarIncrementControl dijitCalendarIncreaseInner\">+</span></div>\n\t\t\t</th>\n\t\t</tr>\n\t\t<tr>\n\t\t\t<th class=\"dijitReset dijitCalendarDayLabelTemplate\"><span class=\"dijitCalendarDayLabel\"></span></th>\n\t\t</tr>\n\t</thead>\n\t<tbody dojoAttachEvent=\"onclick: _onDayClick\" class=\"dijitReset dijitCalendarBodyContainer\">\n\t\t<tr class=\"dijitReset dijitCalendarWeekTemplate\">\n\t\t\t<td class=\"dijitReset dijitCalendarDateTemplate\"><span class=\"dijitCalendarDateLabel\"></span></td>\n\t\t</tr>\n\t</tbody>\n\t<tfoot class=\"dijitReset dijitCalendarYearContainer\">\n\t\t<tr>\n\t\t\t<td class='dijitReset' valign=\"top\" colspan=\"7\">\n\t\t\t\t<h3 class=\"dijitCalendarYearLabel\">\n\t\t\t\t\t<span dojoAttachPoint=\"previousYearLabelNode\" class=\"dijitInline dijitCalendarPreviousYear\"></span>\n\t\t\t\t\t<span dojoAttachPoint=\"currentYearLabelNode\" class=\"dijitInline dijitCalendarSelectedYear\"></span>\n\t\t\t\t\t<span dojoAttachPoint=\"nextYearLabelNode\" class=\"dijitInline dijitCalendarNextYear\"></span>\n\t\t\t\t</h3>\n\t\t\t</td>\n\t\t</tr>\n\t</tfoot>\n</table>\t\n",
  31. // value: Date
  32. // the currently selected Date
  33. value : allGetServerTime(),
  34. // dayWidth: String
  35. // How to represent the days of the week in the calendar header. See
  36. // dojo.date.locale
  37. dayWidth : "narrow",
  38. setValue : function(/* Date */value) {
  39. // summary: set the current date and update the UI. If the date is
  40. // disabled, the selection will
  41. // not change, but the display will change to the corresponding
  42. // month.
  43. if (!this.value || dojo.date.compare(value, this.value)) {
  44. value = new Date(value);
  45. this.displayMonth = new Date(value);
  46. if (!this.isDisabledDate(value, this.lang)) {
  47. this.value = value;
  48. this.value.setHours(0, 0, 0, 0);
  49. this.onChange(this.value);
  50. }
  51. this._populateGrid();
  52. }
  53. },
  54. _setText : function(node, text) {
  55. while (node.firstChild) {
  56. node.removeChild(node.firstChild);
  57. }
  58. node.appendChild(document.createTextNode(text));
  59. },
  60. _populateGrid : function() {
  61. var month = this.displayMonth;
  62. month.setDate(1);
  63. var firstDay = month.getDay();
  64. var daysInMonth = dojo.date.getDaysInMonth(month);
  65. var daysInPreviousMonth = dojo.date.getDaysInMonth(dojo.date.add(
  66. month, "month", -1));
  67. var today = allGetServerTime();
  68. var selected = this.value;
  69. var dayOffset = dojo.cldr.supplemental.getFirstDayOfWeek(this.lang);
  70. if (dayOffset > firstDay) {
  71. dayOffset -= 7;
  72. }
  73. // Iterate through dates in the calendar and fill in date numbers
  74. // and style info
  75. dojo.query(".dijitCalendarDateTemplate", this.domNode).forEach(
  76. function(template, i) {
  77. i += dayOffset;
  78. var date = new Date(month);
  79. var number, clazz = "dijitCalendar", adj = 0;
  80. if (i < firstDay) {
  81. number = daysInPreviousMonth - firstDay + i + 1;
  82. adj = -1;
  83. clazz += "Previous";
  84. } else if (i >= (firstDay + daysInMonth)) {
  85. number = i - firstDay - daysInMonth + 1;
  86. adj = 1;
  87. clazz += "Next";
  88. } else {
  89. number = i - firstDay + 1;
  90. clazz += "Current";
  91. }
  92. if (adj) {
  93. date = dojo.date.add(date, "month", adj);
  94. }
  95. date.setDate(number);
  96. if (!dojo.date.compare(date, today, "date")) {
  97. clazz = "dijitCalendarCurrentDate " + clazz;
  98. }
  99. if (!dojo.date.compare(date, selected, "date")) {
  100. clazz = "dijitCalendarSelectedDate " + clazz;
  101. }
  102. if (this.isDisabledDate(date, this.lang)) {
  103. clazz = "dijitCalendarDisabledDate " + clazz;
  104. }
  105. template.className = clazz
  106. + "Month dijitCalendarDateTemplate";
  107. template.dijitDateValue = date.valueOf();
  108. var label = dojo.query(".dijitCalendarDateLabel",
  109. template)[0];
  110. this._setText(label, date.getDate());
  111. }, this);
  112. // Fill in localized month name
  113. var monthNames = dojo.date.locale.getNames('months', 'wide',
  114. 'standAlone', this.lang);
  115. this._setText(this.monthLabelNode, monthNames[month.getMonth()]);
  116. // Fill in localized prev/current/next years
  117. var y = month.getFullYear() - 1;
  118. dojo.forEach(["previous", "current", "next"], function(name) {
  119. this._setText(this[name + "YearLabelNode"],
  120. dojo.date.locale.format(new Date(y++, 0), {
  121. selector : 'year',
  122. locale : this.lang
  123. }));
  124. }, this);
  125. // Set up repeating mouse behavior
  126. var _this = this;
  127. var typematic = function(nodeProp, dateProp, adj) {
  128. dijit.typematic.addMouseListener(_this[nodeProp], _this,
  129. function(count) {
  130. if (count >= 0) {
  131. _this._adjustDisplay(dateProp, adj);
  132. }
  133. }, 0.8, 500);
  134. };
  135. typematic("incrementMonth", "month", 1);
  136. typematic("decrementMonth", "month", -1);
  137. typematic("nextYearLabelNode", "year", 1);
  138. typematic("previousYearLabelNode", "year", -1);
  139. },
  140. postCreate : function() {
  141. dijit._Calendar.superclass.postCreate.apply(this);
  142. var cloneClass = dojo.hitch(this, function(clazz, n) {
  143. var template = dojo.query(clazz, this.domNode)[0];
  144. for (var i = 0; i < n; i++) {
  145. template.parentNode.appendChild(template
  146. .cloneNode(true));
  147. }
  148. });
  149. // clone the day label and calendar day templates 6 times to make 7
  150. // columns
  151. cloneClass(".dijitCalendarDayLabelTemplate", 6);
  152. cloneClass(".dijitCalendarDateTemplate", 6);
  153. // now make 6 week rows
  154. cloneClass(".dijitCalendarWeekTemplate", 5);
  155. // insert localized day names in the header
  156. var dayNames = dojo.date.locale.getNames('days', this.dayWidth,
  157. 'standAlone', this.lang);
  158. var dayOffset = dojo.cldr.supplemental.getFirstDayOfWeek(this.lang);
  159. dojo.query(".dijitCalendarDayLabel", this.domNode).forEach(
  160. function(label, i) {
  161. this._setText(label, dayNames[(i + dayOffset) % 7]);
  162. }, this);
  163. // Fill in spacer element with all the month names (invisible) so
  164. // that the maximum width will affect layout
  165. var monthNames = dojo.date.locale.getNames('months', 'wide',
  166. 'standAlone', this.lang);
  167. dojo.forEach(monthNames, function(name) {
  168. var monthSpacer = dojo.doc.createElement("div");
  169. this._setText(monthSpacer, name);
  170. this.monthLabelSpacer.appendChild(monthSpacer);
  171. }, this);
  172. this.value = null;
  173. this.setValue(allGetServerTime());
  174. },
  175. _adjustDisplay : function(/* String */part, /* int */amount) {
  176. this.displayMonth = dojo.date.add(this.displayMonth, part, amount);
  177. this._populateGrid();
  178. },
  179. _onDayClick : function(/* Event */evt) {
  180. var node = evt.target;
  181. dojo.stopEvent(evt);
  182. while (!node.dijitDateValue) {
  183. node = node.parentNode;
  184. }
  185. if (!dojo.hasClass(node, "dijitCalendarDisabledDate")) {
  186. this.setValue(node.dijitDateValue);
  187. this.onValueSelected(this.value);
  188. }
  189. },
  190. onValueSelected : function(/* Date */date) {
  191. // summary: a date cell was selected. It may be the same as the
  192. // previous value.
  193. },
  194. onChange : function(/* Date */date) {
  195. // summary: called only when the selected date has changed
  196. },
  197. isDisabledDate : function(/* Date */dateObject, /* String? */locale) {
  198. // summary:
  199. // May be overridden to disable certain dates in the calendar e.g.
  200. // isDisabledDate=dojo.date.locale.isWeekend
  201. return false; // Boolean
  202. }
  203. });
  204. }