1 if(!dojo._hasResource["dijit._Calendar"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
2 dojo._hasResource["dijit._Calendar"] = true;
3 dojo.provide("dijit._Calendar");
5 dojo.require("dojo.cldr.supplemental");
6 dojo.require("dojo.date");
7 dojo.require("dojo.date.locale");
9 dojo.require("dijit._Widget");
10 dojo.require("dijit._Templated");
14 [dijit._Widget, dijit._Templated],
18 // A simple GUI for choosing a date in the context of a monthly calendar.
21 // A simple GUI for choosing a date in the context of a monthly calendar.
22 // This widget is used internally by other widgets and is not accessible
23 // as a standalone widget.
24 // This widget can't be used in a form because it doesn't serialize the date to an
25 // `<input>` field. For a form element, use dijit.form.DateTextBox instead.
27 // Note that the parser takes all dates attributes passed in the
28 // [RFC 3339 format](http://www.faqs.org/rfcs/rfc3339.html), e.g. `2005-06-30T08:05:00-07:00`
29 // so that they are serializable and locale-independent.
32 // | var calendar = new dijit._Calendar({}, dojo.byId("calendarNode"));
35 // | <div dojoType="dijit._Calendar"></div>
37 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<div class=\"dijitInline dijitCalendarIncrementControl dijitCalendarDecrease\"><span dojoAttachPoint=\"decreaseArrowNode\" class=\"dijitA11ySideArrow dijitCalendarIncrementControl dijitCalendarDecreaseInner\">-</span></div>\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=\"dijitCalendarMonthLabel\"></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",
40 // the currently selected Date
44 // How to represent the days of the week in the calendar header. See dojo.date.locale
47 setValue: function(/*Date*/ value){
48 // summary: set the current date and update the UI. If the date is disabled, the selection will
49 // not change, but the display will change to the corresponding month.
50 if(!this.value || dojo.date.compare(value, this.value)){
51 value = new Date(value);
52 this.displayMonth = new Date(value);
53 if(!this.isDisabledDate(value, this.lang)){
55 this.value.setHours(0,0,0,0);
56 this.onChange(this.value);
62 _setText: function(node, text){
63 while(node.firstChild){
64 node.removeChild(node.firstChild);
66 node.appendChild(dojo.doc.createTextNode(text));
69 _populateGrid: function(){
70 var month = this.displayMonth;
72 var firstDay = month.getDay();
73 var daysInMonth = dojo.date.getDaysInMonth(month);
74 var daysInPreviousMonth = dojo.date.getDaysInMonth(dojo.date.add(month, "month", -1));
75 var today = new Date();
76 var selected = this.value;
78 var dayOffset = dojo.cldr.supplemental.getFirstDayOfWeek(this.lang);
79 if(dayOffset > firstDay){ dayOffset -= 7; }
81 // Iterate through dates in the calendar and fill in date numbers and style info
82 dojo.query(".dijitCalendarDateTemplate", this.domNode).forEach(function(template, i){
84 var date = new Date(month);
85 var number, clazz = "dijitCalendar", adj = 0;
88 number = daysInPreviousMonth - firstDay + i + 1;
91 }else if(i >= (firstDay + daysInMonth)){
92 number = i - firstDay - daysInMonth + 1;
96 number = i - firstDay + 1;
101 date = dojo.date.add(date, "month", adj);
103 date.setDate(number);
105 if(!dojo.date.compare(date, today, "date")){
106 clazz = "dijitCalendarCurrentDate " + clazz;
109 if(!dojo.date.compare(date, selected, "date")){
110 clazz = "dijitCalendarSelectedDate " + clazz;
113 if(this.isDisabledDate(date, this.lang)){
114 clazz = "dijitCalendarDisabledDate " + clazz;
117 var clazz2 = this.getClassForDate(date, this.lang);
119 clazz += clazz2 + " " + clazz;
122 template.className = clazz + "Month dijitCalendarDateTemplate";
123 template.dijitDateValue = date.valueOf();
124 var label = dojo.query(".dijitCalendarDateLabel", template)[0];
125 this._setText(label, date.getDate());
128 // Fill in localized month name
129 var monthNames = dojo.date.locale.getNames('months', 'wide', 'standAlone', this.lang);
130 this._setText(this.monthLabelNode, monthNames[month.getMonth()]);
132 // Fill in localized prev/current/next years
133 var y = month.getFullYear() - 1;
135 dojo.forEach(["previous", "current", "next"], function(name){
137 this._setText(this[name+"YearLabelNode"],
138 dojo.date.locale.format(d, {selector:'year', locale:this.lang}));
141 // Set up repeating mouse behavior
143 var typematic = function(nodeProp, dateProp, adj){
144 dijit.typematic.addMouseListener(_this[nodeProp], _this, function(count){
145 if(count >= 0){ _this._adjustDisplay(dateProp, adj); }
148 typematic("incrementMonth", "month", 1);
149 typematic("decrementMonth", "month", -1);
150 typematic("nextYearLabelNode", "year", 1);
151 typematic("previousYearLabelNode", "year", -1);
154 goToToday: function(){
155 this.setValue(new Date());
158 postCreate: function(){
159 this.inherited(arguments);
161 var cloneClass = dojo.hitch(this, function(clazz, n){
162 var template = dojo.query(clazz, this.domNode)[0];
163 for(var i=0; i<n; i++){
164 template.parentNode.appendChild(template.cloneNode(true));
168 // clone the day label and calendar day templates 6 times to make 7 columns
169 cloneClass(".dijitCalendarDayLabelTemplate", 6);
170 cloneClass(".dijitCalendarDateTemplate", 6);
172 // now make 6 week rows
173 cloneClass(".dijitCalendarWeekTemplate", 5);
175 // insert localized day names in the header
176 var dayNames = dojo.date.locale.getNames('days', this.dayWidth, 'standAlone', this.lang);
177 var dayOffset = dojo.cldr.supplemental.getFirstDayOfWeek(this.lang);
178 dojo.query(".dijitCalendarDayLabel", this.domNode).forEach(function(label, i){
179 this._setText(label, dayNames[(i + dayOffset) % 7]);
182 // Fill in spacer element with all the month names (invisible) so that the maximum width will affect layout
183 var monthNames = dojo.date.locale.getNames('months', 'wide', 'standAlone', this.lang);
184 dojo.forEach(monthNames, function(name){
185 var monthSpacer = dojo.doc.createElement("div");
186 this._setText(monthSpacer, name);
187 this.monthLabelSpacer.appendChild(monthSpacer);
191 this.setValue(new Date());
194 _adjustDisplay: function(/*String*/part, /*int*/amount){
195 this.displayMonth = dojo.date.add(this.displayMonth, part, amount);
196 this._populateGrid();
199 _onDayClick: function(/*Event*/evt){
200 var node = evt.target;
202 while(!node.dijitDateValue){
203 node = node.parentNode;
205 if(!dojo.hasClass(node, "dijitCalendarDisabledDate")){
206 this.setValue(node.dijitDateValue);
207 this.onValueSelected(this.value);
211 onValueSelected: function(/*Date*/date){
212 // summary: a date cell was selected. It may be the same as the previous value.
215 onChange: function(/*Date*/date){
216 // summary: called only when the selected date has changed
219 isDisabledDate: function(/*Date*/dateObject, /*String?*/locale){
221 // May be overridden to disable certain dates in the calendar e.g. `isDisabledDate=dojo.date.locale.isWeekend`
223 return false; // Boolean
227 getClassForDate: function(/*Date*/dateObject, /*String?*/locale){
229 // May be overridden to return CSS classes to associate with the date entry for the given dateObject,
230 // for example to indicate a holiday in specified locale.