Subversion Repositories Applications.papyrus

Rev

Details | Last modification | View Log | RSS feed

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