]> git.pond.sub.org Git - eow/blob - static/dojo-release-1.1.1/dijit/_Calendar.js
add Dojo 1.1.1
[eow] / static / dojo-release-1.1.1 / dijit / _Calendar.js
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");
4
5 dojo.require("dojo.cldr.supplemental");
6 dojo.require("dojo.date");
7 dojo.require("dojo.date.locale");
8
9 dojo.require("dijit._Widget");
10 dojo.require("dijit._Templated");
11
12 dojo.declare(
13         "dijit._Calendar",
14         [dijit._Widget, dijit._Templated],
15         {
16         //      
17         //      summary:
18         //              A simple GUI for choosing a date in the context of a monthly calendar.
19         //
20         //      description:
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.
26         //
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.
30         //
31         //      example:
32         //      |       var calendar = new dijit._Calendar({}, dojo.byId("calendarNode"));
33         //
34         //      example:
35         //      |       <div dojoType="dijit._Calendar"></div>
36         //      
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",
38
39                 // value: Date
40                 //      the currently selected Date
41                 value: new Date(),
42
43                 // dayWidth: String
44                 //      How to represent the days of the week in the calendar header. See dojo.date.locale
45                 dayWidth: "narrow",
46
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)){
54                                         this.value = value;
55                                         this.value.setHours(0,0,0,0);
56                                         this.onChange(this.value);
57                                 }
58                                 this._populateGrid();
59                         }
60                 },
61
62                 _setText: function(node, text){
63                         while(node.firstChild){
64                                 node.removeChild(node.firstChild);
65                         }
66                         node.appendChild(dojo.doc.createTextNode(text));
67                 },
68
69                 _populateGrid: function(){
70                         var month = this.displayMonth;
71                         month.setDate(1);
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;
77
78                         var dayOffset = dojo.cldr.supplemental.getFirstDayOfWeek(this.lang);
79                         if(dayOffset > firstDay){ dayOffset -= 7; }
80
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){
83                                 i += dayOffset;
84                                 var date = new Date(month);
85                                 var number, clazz = "dijitCalendar", adj = 0;
86
87                                 if(i < firstDay){
88                                         number = daysInPreviousMonth - firstDay + i + 1;
89                                         adj = -1;
90                                         clazz += "Previous";
91                                 }else if(i >= (firstDay + daysInMonth)){
92                                         number = i - firstDay - daysInMonth + 1;
93                                         adj = 1;
94                                         clazz += "Next";
95                                 }else{
96                                         number = i - firstDay + 1;
97                                         clazz += "Current";
98                                 }
99
100                                 if(adj){
101                                         date = dojo.date.add(date, "month", adj);
102                                 }
103                                 date.setDate(number);
104
105                                 if(!dojo.date.compare(date, today, "date")){
106                                         clazz = "dijitCalendarCurrentDate " + clazz;
107                                 }
108
109                                 if(!dojo.date.compare(date, selected, "date")){
110                                         clazz = "dijitCalendarSelectedDate " + clazz;
111                                 }
112
113                                 if(this.isDisabledDate(date, this.lang)){
114                                         clazz = "dijitCalendarDisabledDate " + clazz;
115                                 }
116
117                                 var clazz2 = this.getClassForDate(date, this.lang);
118                                 if(clazz2){
119                                         clazz += clazz2 + " " + clazz;
120                                 }
121
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());
126                         }, this);
127
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()]);
131
132                         // Fill in localized prev/current/next years
133                         var y = month.getFullYear() - 1;
134                         var d = new Date();
135                         dojo.forEach(["previous", "current", "next"], function(name){
136                                 d.setFullYear(y++);
137                                 this._setText(this[name+"YearLabelNode"],
138                                         dojo.date.locale.format(d, {selector:'year', locale:this.lang}));
139                         }, this);
140
141                         // Set up repeating mouse behavior
142                         var _this = this;
143                         var typematic = function(nodeProp, dateProp, adj){
144                                 dijit.typematic.addMouseListener(_this[nodeProp], _this, function(count){
145                                         if(count >= 0){ _this._adjustDisplay(dateProp, adj); }
146                                 }, 0.8, 500);
147                         };
148                         typematic("incrementMonth", "month", 1);
149                         typematic("decrementMonth", "month", -1);
150                         typematic("nextYearLabelNode", "year", 1);
151                         typematic("previousYearLabelNode", "year", -1);
152                 },
153
154                 goToToday: function(){
155                         this.setValue(new Date());
156                 },
157
158                 postCreate: function(){
159                         this.inherited(arguments);
160                         
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));
165                                 }
166                         });
167
168                         // clone the day label and calendar day templates 6 times to make 7 columns
169                         cloneClass(".dijitCalendarDayLabelTemplate", 6);
170                         cloneClass(".dijitCalendarDateTemplate", 6);
171
172                         // now make 6 week rows
173                         cloneClass(".dijitCalendarWeekTemplate", 5);
174
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]);
180                         }, this);
181
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);
188                         }, this);
189
190                         this.value = null;
191                         this.setValue(new Date());
192                 },
193
194                 _adjustDisplay: function(/*String*/part, /*int*/amount){
195                         this.displayMonth = dojo.date.add(this.displayMonth, part, amount);
196                         this._populateGrid();
197                 },
198
199                 _onDayClick: function(/*Event*/evt){
200                         var node = evt.target;
201                         dojo.stopEvent(evt);
202                         while(!node.dijitDateValue){
203                                 node = node.parentNode;
204                         }
205                         if(!dojo.hasClass(node, "dijitCalendarDisabledDate")){
206                                 this.setValue(node.dijitDateValue);
207                                 this.onValueSelected(this.value);
208                         }
209                 },
210
211                 onValueSelected: function(/*Date*/date){
212                         // summary: a date cell was selected.  It may be the same as the previous value.
213                 },
214
215                 onChange: function(/*Date*/date){
216                         // summary: called only when the selected date has changed
217                 },
218
219                 isDisabledDate: function(/*Date*/dateObject, /*String?*/locale){
220                         // summary:
221                         //      May be overridden to disable certain dates in the calendar e.g. `isDisabledDate=dojo.date.locale.isWeekend`
222 /*=====
223                         return false; // Boolean
224 =====*/
225                 },
226
227                 getClassForDate: function(/*Date*/dateObject, /*String?*/locale){
228                         // summary:
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.
231
232 /*=====
233                         return ""; // String
234 =====*/
235                 }
236         }
237 );
238
239 }