Debian Codegraph @

dijit/Calendar.js

raw symbols SCIP
1 define([
2 "dojo/_base/array", // array.map
3 "dojo/date",
4 "dojo/date/locale",
5 "dojo/_base/declare", // declare
6 "dojo/dom-attr", // domAttr.get
7 "dojo/dom-class", // domClass.add domClass.contains domClass.remove domClass.toggle
8 "dojo/dom-construct", // create
9 "dojo/_base/kernel", // kernel.deprecated
10 "dojo/keys", // keys
11 "dojo/_base/lang", // lang.hitch
12 "dojo/on",
13 "dojo/sniff", // has("ie")
14 "./CalendarLite",
15 "./_Widget",
16 "./_CssStateMixin",
17 "./_TemplatedMixin",
18 "./form/DropDownButton"
19 ], function(array, date, local, declare, domAttr, domClass, domConstruct, kernel, keys, lang, on, has,
20 CalendarLite, _Widget, _CssStateMixin, _TemplatedMixin, DropDownButton){
21
22 // module:
23 // dijit/Calendar
24
25 // _Widget for deprecated methods like setAttribute()
26 var Calendar = declare("dijit.Calendar", [CalendarLite, _Widget, _CssStateMixin], {
27 // summary:
28 // A simple GUI for choosing a date in the context of a monthly calendar.
29 //
30 // description:
31 // See CalendarLite for general description. Calendar extends CalendarLite, adding:
32 //
33 // - month drop down list
34 // - keyboard navigation
35 // - CSS classes for hover/mousepress on date, month, and year nodes
36 // - support of deprecated methods (will be removed in 2.0)
37
38 baseClass: "dijitCalendar",
39
40 // Set node classes for various mouse events, see dijit._CssStateMixin for more details
41 cssStateNodes: {
42 "decrementMonth": "dijitCalendarArrow",
43 "incrementMonth": "dijitCalendarArrow",
44 "previousYearLabelNode": "dijitCalendarPreviousYear",
45 "nextYearLabelNode": "dijitCalendarNextYear"
46 },
47
48 setValue: function(/*Date*/ value){
49 // summary:
50 // Deprecated. Use set('value', ...) instead.
51 // tags:
52 // deprecated
53 kernel.deprecated("dijit.Calendar:setValue() is deprecated. Use set('value', ...) instead.", "", "2.0");
54 this.set('value', value);
55 },
56
57 _createMonthWidget: function(){
58 // summary:
59 // Creates the drop down button that displays the current month and lets user pick a new one
60
61 return new Calendar._MonthDropDownButton({
62 id: this.id + "_mddb",
63 tabIndex: -1,
64 onMonthSelect: lang.hitch(this, "_onMonthSelect"),
65 lang: this.lang,
66 dateLocaleModule: this.dateLocaleModule
67 }, this.monthNode);
68 },
69
70 postCreate: function(){
71 this.inherited(arguments);
72
73 // Events specific to Calendar, not used in CalendarLite
74 this.own(
75 on(this.domNode, "keydown", lang.hitch(this, "_onKeyDown")),
76 on(this.dateRowsNode, "mouseover", lang.hitch(this, "_onDayMouseOver")),
77 on(this.dateRowsNode, "mouseout", lang.hitch(this, "_onDayMouseOut")),
78 on(this.dateRowsNode, "mousedown", lang.hitch(this, "_onDayMouseDown")),
79 on(this.dateRowsNode, "mouseup", lang.hitch(this, "_onDayMouseUp"))
80 );
81 },
82
83 _onMonthSelect: function(/*Number*/ newMonth){
84 // summary:
85 // Handler for when user selects a month from the drop down list
86 // tags:
87 // protected
88
89 // move to selected month, bounding by the number of days in the month
90 // (ex: jan 31 --> feb 28, not feb 31)
91 var date = new this.dateClassObj(this.currentFocus);
92 date.setDate(1);
93 date.setMonth(newMonth);
94 var daysInMonth = this.dateModule.getDaysInMonth(date);
95 var currentDate = this.currentFocus.getDate();
96 date.setDate(Math.min(currentDate, daysInMonth));
97 this._setCurrentFocusAttr(date);
98 },
99
100 _onDayMouseOver: function(/*Event*/ evt){
101 // summary:
102 // Handler for mouse over events on days, sets hovered style
103 // tags:
104 // protected
105
106 // event can occur on <td> or the <span> inside the td,
107 // set node to the <td>.
108 var node =
109 domClass.contains(evt.target, "dijitCalendarDateLabel") ?
110 evt.target.parentNode :
111 evt.target;
112
113 if(node && (
114 (node.dijitDateValue && !domClass.contains(node, "dijitCalendarDisabledDate"))
115 || node == this.previousYearLabelNode || node == this.nextYearLabelNode
116 )){
117 domClass.add(node, "dijitCalendarHoveredDate");
118 this._currentNode = node;
119 }
120 },
121
122 _onDayMouseOut: function(/*Event*/ evt){
123 // summary:
124 // Handler for mouse out events on days, clears hovered style
125 // tags:
126 // protected
127
128 if(!this._currentNode){
129 return;
130 }
131
132 // if mouse out occurs moving from <td> to <span> inside <td>, ignore it
133 if(evt.relatedTarget && evt.relatedTarget.parentNode == this._currentNode){
134 return;
135 }
136 var cls = "dijitCalendarHoveredDate";
137 if(domClass.contains(this._currentNode, "dijitCalendarActiveDate")){
138 cls += " dijitCalendarActiveDate";
139 }
140 domClass.remove(this._currentNode, cls);
141 this._currentNode = null;
142 },
143
144 _onDayMouseDown: function(/*Event*/ evt){
145 var node = evt.target.parentNode;
146 if(node && node.dijitDateValue && !domClass.contains(node, "dijitCalendarDisabledDate")){
147 domClass.add(node, "dijitCalendarActiveDate");
148 this._currentNode = node;
149 }
150 },
151
152 _onDayMouseUp: function(/*Event*/ evt){
153 var node = evt.target.parentNode;
154 if(node && node.dijitDateValue){
155 domClass.remove(node, "dijitCalendarActiveDate");
156 }
157 },
158
159 handleKey: function(/*Event*/ evt){
160 // summary:
161 // Provides keyboard navigation of calendar.
162 // description:
163 // Called from _onKeyDown() to handle keydown on a stand alone Calendar,
164 // and also from `dijit/form/_DateTimeTextBox` to pass a keydown event
165 // from the `dijit/form/DateTextBox` to be handled in this widget
166 // returns:
167 // False if the key was recognized as a navigation key,
168 // to indicate that the event was handled by Calendar and shouldn't be propagated
169 // tags:
170 // protected
171 var increment = -1,
172 interval,
173 newValue = this.currentFocus;
174 switch(evt.keyCode){
175 case keys.RIGHT_ARROW:
176 increment = 1;
177 //fallthrough...
178 case keys.LEFT_ARROW:
179 interval = "day";
180 if(!this.isLeftToRight()){
181 increment *= -1;
182 }
183 break;
184 case keys.DOWN_ARROW:
185 increment = 1;
186 //fallthrough...
187 case keys.UP_ARROW:
188 interval = "week";
189 break;
190 case keys.PAGE_DOWN:
191 increment = 1;
192 //fallthrough...
193 case keys.PAGE_UP:
194 interval = evt.ctrlKey || evt.altKey ? "year" : "month";
195 break;
196 case keys.END:
197 // go to the next month
198 newValue = this.dateModule.add(newValue, "month", 1);
199 // subtract a day from the result when we're done
200 interval = "day";
201 //fallthrough...
202 case keys.HOME:
203 newValue = new this.dateClassObj(newValue);
204 newValue.setDate(1);
205 break;
206 default:
207 return true;
208 }
209
210 if(interval){
211 newValue = this.dateModule.add(newValue, interval, increment);
212 }
213
214 this._setCurrentFocusAttr(newValue);
215
216 return false;
217 },
218
219 _onKeyDown: function(/*Event*/ evt){
220 // summary:
221 // For handling keydown events on a stand alone calendar
222 if(!this.handleKey(evt)){
223 evt.stopPropagation();
224 evt.preventDefault();
225 }
226 },
227
228 onValueSelected: function(/*Date*/ /*===== date =====*/){
229 // summary:
230 // Deprecated. Notification that a date cell was selected. It may be the same as the previous value.
231 // description:
232 // Formerly used by `dijit/form/_DateTimeTextBox` (and thus `dijit/form/DateTextBox`)
233 // to get notification when the user has clicked a date. Now onExecute() (above) is used.
234 // tags:
235 // protected
236 },
237
238 onChange: function(value){
239 this.onValueSelected(value); // remove in 2.0
240 },
241
242 getClassForDate: function(/*===== dateObject, locale =====*/){
243 // summary:
244 // May be overridden to return CSS classes to associate with the date entry for the given dateObject,
245 // for example to indicate a holiday in specified locale.
246 // dateObject: Date
247 // locale: String?
248 // tags:
249 // extension
250
251 /*=====
252 return ""; // String
253 =====*/
254 }
255 });
256
257 Calendar._MonthDropDownButton = declare("dijit.Calendar._MonthDropDownButton", DropDownButton, {
258 // summary:
259 // DropDownButton for the current month. Displays name of current month
260 // and a list of month names in the drop down
261
262 onMonthSelect: function(){
263 },
264
265 postCreate: function(){
266 this.inherited(arguments);
267 this.dropDown = new Calendar._MonthDropDown({
268 id: this.id + "_mdd", //do not change this id because it is referenced in the template
269 onChange: this.onMonthSelect
270 });
271 },
272 _setMonthAttr: function(month){
273 // summary:
274 // Set the current month to display as a label
275 var monthNames = this.dateLocaleModule.getNames('months', 'wide', 'standAlone', this.lang, month);
276 this.dropDown.set("months", monthNames);
277
278 // Set name of current month and also fill in spacer element with all the month names
279 // (invisible) so that the maximum width will affect layout. But not on IE6 because then
280 // the center <TH> overlaps the right <TH> (due to a browser bug).
281 this.containerNode.innerHTML =
282 (has("ie") == 6 ? "" : "<div class='dijitSpacer'>" + this.dropDown.domNode.innerHTML + "</div>") +
283 "<div class='dijitCalendarMonthLabel dijitCalendarCurrentMonthLabel'>" + monthNames[month.getMonth()] + "</div>";
284 }
285 });
286
287 Calendar._MonthDropDown = declare("dijit.Calendar._MonthDropDown", [_Widget, _TemplatedMixin, _CssStateMixin], {
288 // summary:
289 // The list-of-months drop down from the MonthDropDownButton
290
291 // months: String[]
292 // List of names of months, possibly w/some undefined entries for Hebrew leap months
293 // (ex: ["January", "February", undefined, "April", ...])
294 months: [],
295
296 baseClass: "dijitCalendarMonthMenu dijitMenu",
297
298 templateString: "<div data-dojo-attach-event='ondijitclick:_onClick'></div>",
299
300 _setMonthsAttr: function(/*String[]*/ months){
301 this.domNode.innerHTML = "";
302 array.forEach(months, function(month, idx){
303 var div = domConstruct.create("div", {
304 className: "dijitCalendarMonthLabel",
305 month: idx,
306 innerHTML: month
307 }, this.domNode);
308 div._cssState = "dijitCalendarMonthLabel"; // trigger _CSSStateMixin magic; property, not attribute.
309 }, this);
310 },
311
312 _onClick: function(/*Event*/ evt){
313 this.onChange(domAttr.get(evt.target, "month"));
314 },
315
316 onChange: function(/*Number*/ /*===== month =====*/){
317 // summary:
318 // Callback when month is selected from drop down
319 }
320 });
321
322 return Calendar;
323 });