1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
|
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
goog.provide('cvox.ChromeVoxHTMLDateWidget');
/**
* @fileoverview Gives the user spoken feedback as they interact with the date
* widget (input type=date).
*
*/
/**
* A class containing the information needed to speak
* a text change event to the user.
*
* @constructor
* @param {Element} dateElem The time widget element.
* @param {cvox.TtsInterface} tts The TTS object from ChromeVox.
*/
cvox.ChromeVoxHTMLDateWidget = function(dateElem, tts){
var self = this;
/**
* Currently selected field in the widget.
* @type {number}
* @private
*/
this.pos_ = 0;
var maxpos = 2;
if (dateElem.type == 'month' || dateElem.type == 'week') {
maxpos = 1;
}
/**
* The maximum number of fields in the widget.
* @type {number}
* @private
*/
this.maxPos_ = maxpos;
/**
* The HTML node of the widget.
* @type {Node}
* @private
*/
this.dateElem_ = dateElem;
/**
* A handle to the ChromeVox TTS object.
* @type {Object}
* @private
*/
this.dateTts_ = tts;
/**
* The previous value of the year field.
* @type {number}
* @private
*/
this.pYear_ = -1;
/**
* The previous value of the month field.
* @type {number}
* @private
*/
this.pMonth_ = -1;
/**
* The previous value of the week field.
* @type {number}
* @private
*/
this.pWeek_ = -1;
/**
* The previous value of the day field.
* @type {number}
* @private
*/
this.pDay_ = -1;
// Use listeners to make this work when running tests inside of ChromeVox.
this.keyListener_ = function(evt) {
self.eventHandler_(evt);
}
this.blurListener_ = function(evt) {
self.shutdown();
}
// Ensure we have a reasonable value to start with.
if (this.dateElem_.value.length == 0) {
this.forceInitTime_();
}
// Move the cursor to the first position so that we are guaranteed to start
// off at the hours position.
for (var i = 0; i < this.maxPos_; i++) {
var evt = document.createEvent('KeyboardEvent');
evt.initKeyboardEvent(
'keydown', true, true, window, 'Left', 0, false, false, false, false);
this.dateElem_.dispatchEvent(evt);
evt = document.createEvent('KeyboardEvent');
evt.initKeyboardEvent(
'keyup', true, true, window, 'Left', 0, false, false, false, false);
this.dateElem_.dispatchEvent(evt);
}
this.dateElem_.addEventListener('keydown', this.keyListener_, false);
this.dateElem_.addEventListener('keyup', this.keyListener_, false);
this.dateElem_.addEventListener('blur', this.blurListener_, false);
this.update_(true);
};
/**
* Removes the key listeners for the time widget.
*
*/
cvox.ChromeVoxHTMLDateWidget.prototype.shutdown = function() {
this.dateElem_.removeEventListener('blur', this.blurListener_, false);
this.dateElem_.removeEventListener('keydown', this.keyListener_, false);
this.dateElem_.removeEventListener('keyup', this.keyListener_, false);
};
/**
* Forces a sensible default value so that there is something there that can
* be inspected with JS.
* @private
*/
cvox.ChromeVoxHTMLDateWidget.prototype.forceInitTime_ = function() {
var currentDate = new Date();
var valueString = '';
var yearString = currentDate.getFullYear() + '';
// Date.getMonth starts at 0, but the value for the HTML5 date widget needs to
// start at 1.
var monthString = currentDate.getMonth() + 1 + '';
if (monthString.length < 2) {
monthString = '0' + monthString; // Month format is MM.
}
var dayString = currentDate.getDate() + '';
switch (this.dateElem_.type) {
case 'month':
valueString = yearString + '-' + monthString;
break;
case 'week':
// Based on info from: http://www.merlyn.demon.co.uk/weekcalc.htm#WNR
currentDate.setHours(0,0,0);
// Set to nearest Thursday: current date + 4 - current day number
// Make Sunday's day number 7
currentDate.setDate(
currentDate.getDate() + 4 - (currentDate.getDay()||7));
// Get first day of year
var yearStart = new Date(currentDate.getFullYear(),0,1);
// Calculate full weeks to nearest Thursday
var weekString =
Math.ceil(( ( (currentDate - yearStart) / 86400000) + 1)/7) + '';
if (weekString.length < 2) {
weekString = '0' + weekString; // Week format is WXX.
}
weekString = 'W' + weekString;
valueString = yearString + '-' + weekString;
break;
default:
valueString = yearString + '-' + monthString + '-' + dayString;
break;
}
this.dateElem_.setAttribute('value', valueString);
};
/**
* Ensure that the position stays within bounds.
* @private
*/
cvox.ChromeVoxHTMLDateWidget.prototype.handlePosChange_ = function() {
this.pos_ = Math.max(this.pos_, 0);
this.pos_ = Math.min(this.pos_, this.maxPos_);
// TODO (clchen, dtseng): Make this logic i18n once there is a way to
// determine what the date format actually is. For now, assume that:
// date == mm/dd/yyyy
// week == ww/yyyy
// month == mm/yyyy.
switch (this.pos_) {
case 0:
if (this.dateElem_.type == 'week') {
this.pWeek_ = -1;
} else {
this.pMonth_ = -1;
}
break;
case 1:
if (this.dateElem_.type == 'date') {
this.pDay_ = -1;
} else {
this.pYear_ = -1;
}
break;
case 2:
this.pYear_ = -1;
break;
}
};
/**
* Speaks any changes to the control.
* @private
* @param {boolean} shouldSpeakLabel Whether or not to speak the label.
*/
cvox.ChromeVoxHTMLDateWidget.prototype.update_ = function(shouldSpeakLabel) {
var splitDate = this.dateElem_.value.split("-");
if (splitDate.length < 1){
this.forceInitTime_();
return;
}
var year = -1;
var month = -1;
var week = -1;
var day = -1;
year = parseInt(splitDate[0], 10);
if (this.dateElem_.type == 'week') {
week = parseInt(splitDate[1].replace('W', ''), 10);
} else if (this.dateElem_.type == 'date') {
month = parseInt(splitDate[1], 10);
day = parseInt(splitDate[2], 10);
} else {
month = parseInt(splitDate[1], 10);
}
var changeMessage = ''
if (shouldSpeakLabel) {
changeMessage = cvox.DomUtil.getName(this.dateElem_, true, true) + '\n';
}
if (week != this.pWeek_) {
changeMessage = changeMessage +
cvox.ChromeVox.msgs.getMsg('datewidget_week') + week + '\n';
this.pWeek_ = week;
}
if (month != this.pMonth_) {
var monthName = '';
switch (month) {
case 1:
monthName = cvox.ChromeVox.msgs.getMsg('datewidget_january');
break;
case 2:
monthName = cvox.ChromeVox.msgs.getMsg('datewidget_february');
break;
case 3:
monthName = cvox.ChromeVox.msgs.getMsg('datewidget_march');
break;
case 4:
monthName = cvox.ChromeVox.msgs.getMsg('datewidget_april');
break;
case 5:
monthName = cvox.ChromeVox.msgs.getMsg('datewidget_may');
break;
case 6:
monthName = cvox.ChromeVox.msgs.getMsg('datewidget_june');
break;
case 7:
monthName = cvox.ChromeVox.msgs.getMsg('datewidget_july');
break;
case 8:
monthName = cvox.ChromeVox.msgs.getMsg('datewidget_august');
break;
case 9:
monthName = cvox.ChromeVox.msgs.getMsg('datewidget_september');
break;
case 10:
monthName = cvox.ChromeVox.msgs.getMsg('datewidget_october');
break;
case 11:
monthName = cvox.ChromeVox.msgs.getMsg('datewidget_november');
break;
case 12:
monthName = cvox.ChromeVox.msgs.getMsg('datewidget_december');
break;
}
changeMessage = changeMessage + monthName + '\n';
this.pMonth_ = month;
}
if (day != this.pDay_) {
changeMessage = changeMessage + day + '\n';
this.pDay_ = day;
}
if (year != this.pYear_) {
changeMessage = changeMessage + year + '\n';
this.pYear_ = year;
}
if (changeMessage.length > 0) {
this.dateTts_.speak(changeMessage, 0, null);
}
};
/**
* Handles user key events.
* @private
* @param {Event} evt The event to be handled.
*/
cvox.ChromeVoxHTMLDateWidget.prototype.eventHandler_ = function(evt) {
var shouldSpeakLabel = false;
if (evt.type == 'keydown') {
// Handle tab/right arrow
if (((evt.keyCode == 9) && !evt.shiftKey) || (evt.keyCode == 39)) {
this.pos_++;
this.handlePosChange_();
shouldSpeakLabel = true;
}
// Handle shift+tab/left arrow
if (((evt.keyCode == 9) && evt.shiftKey) || (evt.keyCode == 37)) {
this.pos_--;
this.handlePosChange_();
shouldSpeakLabel = true;
}
// For all other cases, fall through and let update_ decide if there are any
// changes that need to be spoken.
}
this.update_(shouldSpeakLabel);
};
|