| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 goog.provide('cvox.ChromeVoxHTMLDateWidget'); | 5 goog.provide('cvox.ChromeVoxHTMLDateWidget'); |
| 6 | 6 |
| 7 goog.require('Msgs'); |
| 8 |
| 7 /** | 9 /** |
| 8 * @fileoverview Gives the user spoken feedback as they interact with the date | 10 * @fileoverview Gives the user spoken feedback as they interact with the date |
| 9 * widget (input type=date). | 11 * widget (input type=date). |
| 10 * | 12 * |
| 11 */ | 13 */ |
| 12 | 14 |
| 13 /** | 15 /** |
| 14 * A class containing the information needed to speak | 16 * A class containing the information needed to speak |
| 15 * a text change event to the user. | 17 * a text change event to the user. |
| 16 * | 18 * |
| 17 * @constructor | 19 * @constructor |
| 18 * @param {Element} dateElem The time widget element. | 20 * @param {Element} dateElem The time widget element. |
| 19 * @param {cvox.TtsInterface} tts The TTS object from ChromeVox. | 21 * @param {cvox.TtsInterface} tts The TTS object from ChromeVox. |
| 20 */ | 22 */ |
| 21 cvox.ChromeVoxHTMLDateWidget = function(dateElem, tts){ | 23 cvox.ChromeVoxHTMLDateWidget = function(dateElem, tts) { |
| 22 var self = this; | 24 var self = this; |
| 23 /** | 25 /** |
| 24 * Currently selected field in the widget. | 26 * Currently selected field in the widget. |
| 25 * @type {number} | 27 * @type {number} |
| 26 * @private | 28 * @private |
| 27 */ | 29 */ |
| 28 this.pos_ = 0; | 30 this.pos_ = 0; |
| 29 var maxpos = 2; | 31 var maxpos = 2; |
| 30 if (dateElem.type == 'month' || dateElem.type == 'week') { | 32 if (dateElem.type == 'month' || dateElem.type == 'week') { |
| 31 maxpos = 1; | 33 maxpos = 1; |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 69 /** | 71 /** |
| 70 * The previous value of the day field. | 72 * The previous value of the day field. |
| 71 * @type {number} | 73 * @type {number} |
| 72 * @private | 74 * @private |
| 73 */ | 75 */ |
| 74 this.pDay_ = -1; | 76 this.pDay_ = -1; |
| 75 | 77 |
| 76 // Use listeners to make this work when running tests inside of ChromeVox. | 78 // Use listeners to make this work when running tests inside of ChromeVox. |
| 77 this.keyListener_ = function(evt) { | 79 this.keyListener_ = function(evt) { |
| 78 self.eventHandler_(evt); | 80 self.eventHandler_(evt); |
| 79 } | 81 }; |
| 80 this.blurListener_ = function(evt) { | 82 this.blurListener_ = function(evt) { |
| 81 self.shutdown(); | 83 self.shutdown(); |
| 82 } | 84 }; |
| 83 | 85 |
| 84 // Ensure we have a reasonable value to start with. | 86 // Ensure we have a reasonable value to start with. |
| 85 if (this.dateElem_.value.length == 0) { | 87 if (this.dateElem_.value.length == 0) { |
| 86 this.forceInitTime_(); | 88 this.forceInitTime_(); |
| 87 } | 89 } |
| 88 | 90 |
| 89 // Move the cursor to the first position so that we are guaranteed to start | 91 // Move the cursor to the first position so that we are guaranteed to start |
| 90 // off at the hours position. | 92 // off at the hours position. |
| 91 for (var i = 0; i < this.maxPos_; i++) { | 93 for (var i = 0; i < this.maxPos_; i++) { |
| 92 var evt = document.createEvent('KeyboardEvent'); | 94 var evt = document.createEvent('KeyboardEvent'); |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 131 monthString = '0' + monthString; // Month format is MM. | 133 monthString = '0' + monthString; // Month format is MM. |
| 132 } | 134 } |
| 133 var dayString = currentDate.getDate() + ''; | 135 var dayString = currentDate.getDate() + ''; |
| 134 | 136 |
| 135 switch (this.dateElem_.type) { | 137 switch (this.dateElem_.type) { |
| 136 case 'month': | 138 case 'month': |
| 137 valueString = yearString + '-' + monthString; | 139 valueString = yearString + '-' + monthString; |
| 138 break; | 140 break; |
| 139 case 'week': | 141 case 'week': |
| 140 // Based on info from: http://www.merlyn.demon.co.uk/weekcalc.htm#WNR | 142 // Based on info from: http://www.merlyn.demon.co.uk/weekcalc.htm#WNR |
| 141 currentDate.setHours(0,0,0); | 143 currentDate.setHours(0, 0, 0); |
| 142 // Set to nearest Thursday: current date + 4 - current day number | 144 // Set to nearest Thursday: current date + 4 - current day number |
| 143 // Make Sunday's day number 7 | 145 // Make Sunday's day number 7 |
| 144 currentDate.setDate( | 146 currentDate.setDate( |
| 145 currentDate.getDate() + 4 - (currentDate.getDay()||7)); | 147 currentDate.getDate() + 4 - (currentDate.getDay() || 7)); |
| 146 // Get first day of year | 148 // Get first day of year |
| 147 var yearStart = new Date(currentDate.getFullYear(),0,1); | 149 var yearStart = new Date(currentDate.getFullYear(), 0, 1); |
| 148 // Calculate full weeks to nearest Thursday | 150 // Calculate full weeks to nearest Thursday |
| 149 var weekString = | 151 var weekString = |
| 150 Math.ceil(( ( (currentDate - yearStart) / 86400000) + 1)/7) + ''; | 152 Math.ceil((((currentDate - yearStart) / 86400000) + 1) / 7) + ''; |
| 151 if (weekString.length < 2) { | 153 if (weekString.length < 2) { |
| 152 weekString = '0' + weekString; // Week format is WXX. | 154 weekString = '0' + weekString; // Week format is WXX. |
| 153 } | 155 } |
| 154 weekString = 'W' + weekString; | 156 weekString = 'W' + weekString; |
| 155 valueString = yearString + '-' + weekString; | 157 valueString = yearString + '-' + weekString; |
| 156 break; | 158 break; |
| 157 default: | 159 default: |
| 158 valueString = yearString + '-' + monthString + '-' + dayString; | 160 valueString = yearString + '-' + monthString + '-' + dayString; |
| 159 break; | 161 break; |
| 160 } | 162 } |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 193 break; | 195 break; |
| 194 } | 196 } |
| 195 }; | 197 }; |
| 196 | 198 |
| 197 /** | 199 /** |
| 198 * Speaks any changes to the control. | 200 * Speaks any changes to the control. |
| 199 * @private | 201 * @private |
| 200 * @param {boolean} shouldSpeakLabel Whether or not to speak the label. | 202 * @param {boolean} shouldSpeakLabel Whether or not to speak the label. |
| 201 */ | 203 */ |
| 202 cvox.ChromeVoxHTMLDateWidget.prototype.update_ = function(shouldSpeakLabel) { | 204 cvox.ChromeVoxHTMLDateWidget.prototype.update_ = function(shouldSpeakLabel) { |
| 203 var splitDate = this.dateElem_.value.split("-"); | 205 var splitDate = this.dateElem_.value.split('-'); |
| 204 if (splitDate.length < 1){ | 206 if (splitDate.length < 1) { |
| 205 this.forceInitTime_(); | 207 this.forceInitTime_(); |
| 206 return; | 208 return; |
| 207 } | 209 } |
| 208 | 210 |
| 209 var year = -1; | 211 var year = -1; |
| 210 var month = -1; | 212 var month = -1; |
| 211 var week = -1; | 213 var week = -1; |
| 212 var day = -1; | 214 var day = -1; |
| 213 | 215 |
| 214 year = parseInt(splitDate[0], 10); | 216 year = parseInt(splitDate[0], 10); |
| 215 | 217 |
| 216 if (this.dateElem_.type == 'week') { | 218 if (this.dateElem_.type == 'week') { |
| 217 week = parseInt(splitDate[1].replace('W', ''), 10); | 219 week = parseInt(splitDate[1].replace('W', ''), 10); |
| 218 } else if (this.dateElem_.type == 'date') { | 220 } else if (this.dateElem_.type == 'date') { |
| 219 month = parseInt(splitDate[1], 10); | 221 month = parseInt(splitDate[1], 10); |
| 220 day = parseInt(splitDate[2], 10); | 222 day = parseInt(splitDate[2], 10); |
| 221 } else { | 223 } else { |
| 222 month = parseInt(splitDate[1], 10); | 224 month = parseInt(splitDate[1], 10); |
| 223 } | 225 } |
| 224 | 226 |
| 225 var changeMessage = '' | 227 var changeMessage = ''; |
| 226 | 228 |
| 227 if (shouldSpeakLabel) { | 229 if (shouldSpeakLabel) { |
| 228 changeMessage = cvox.DomUtil.getName(this.dateElem_, true, true) + '\n'; | 230 changeMessage = cvox.DomUtil.getName(this.dateElem_, true, true) + '\n'; |
| 229 } | 231 } |
| 230 | 232 |
| 231 if (week != this.pWeek_) { | 233 if (week != this.pWeek_) { |
| 232 changeMessage = changeMessage + | 234 changeMessage = changeMessage + |
| 233 cvox.ChromeVox.msgs.getMsg('datewidget_week') + week + '\n'; | 235 Msgs.getMsg('datewidget_week') + week + '\n'; |
| 234 this.pWeek_ = week; | 236 this.pWeek_ = week; |
| 235 } | 237 } |
| 236 | 238 |
| 237 if (month != this.pMonth_) { | 239 if (month != this.pMonth_) { |
| 238 var monthName = ''; | 240 var monthName = ''; |
| 239 switch (month) { | 241 switch (month) { |
| 240 case 1: | 242 case 1: |
| 241 monthName = cvox.ChromeVox.msgs.getMsg('datewidget_january'); | 243 monthName = Msgs.getMsg('datewidget_january'); |
| 242 break; | 244 break; |
| 243 case 2: | 245 case 2: |
| 244 monthName = cvox.ChromeVox.msgs.getMsg('datewidget_february'); | 246 monthName = Msgs.getMsg('datewidget_february'); |
| 245 break; | 247 break; |
| 246 case 3: | 248 case 3: |
| 247 monthName = cvox.ChromeVox.msgs.getMsg('datewidget_march'); | 249 monthName = Msgs.getMsg('datewidget_march'); |
| 248 break; | 250 break; |
| 249 case 4: | 251 case 4: |
| 250 monthName = cvox.ChromeVox.msgs.getMsg('datewidget_april'); | 252 monthName = Msgs.getMsg('datewidget_april'); |
| 251 break; | 253 break; |
| 252 case 5: | 254 case 5: |
| 253 monthName = cvox.ChromeVox.msgs.getMsg('datewidget_may'); | 255 monthName = Msgs.getMsg('datewidget_may'); |
| 254 break; | 256 break; |
| 255 case 6: | 257 case 6: |
| 256 monthName = cvox.ChromeVox.msgs.getMsg('datewidget_june'); | 258 monthName = Msgs.getMsg('datewidget_june'); |
| 257 break; | 259 break; |
| 258 case 7: | 260 case 7: |
| 259 monthName = cvox.ChromeVox.msgs.getMsg('datewidget_july'); | 261 monthName = Msgs.getMsg('datewidget_july'); |
| 260 break; | 262 break; |
| 261 case 8: | 263 case 8: |
| 262 monthName = cvox.ChromeVox.msgs.getMsg('datewidget_august'); | 264 monthName = Msgs.getMsg('datewidget_august'); |
| 263 break; | 265 break; |
| 264 case 9: | 266 case 9: |
| 265 monthName = cvox.ChromeVox.msgs.getMsg('datewidget_september'); | 267 monthName = Msgs.getMsg('datewidget_september'); |
| 266 break; | 268 break; |
| 267 case 10: | 269 case 10: |
| 268 monthName = cvox.ChromeVox.msgs.getMsg('datewidget_october'); | 270 monthName = Msgs.getMsg('datewidget_october'); |
| 269 break; | 271 break; |
| 270 case 11: | 272 case 11: |
| 271 monthName = cvox.ChromeVox.msgs.getMsg('datewidget_november'); | 273 monthName = Msgs.getMsg('datewidget_november'); |
| 272 break; | 274 break; |
| 273 case 12: | 275 case 12: |
| 274 monthName = cvox.ChromeVox.msgs.getMsg('datewidget_december'); | 276 monthName = Msgs.getMsg('datewidget_december'); |
| 275 break; | 277 break; |
| 276 } | 278 } |
| 277 changeMessage = changeMessage + monthName + '\n'; | 279 changeMessage = changeMessage + monthName + '\n'; |
| 278 this.pMonth_ = month; | 280 this.pMonth_ = month; |
| 279 } | 281 } |
| 280 | 282 |
| 281 if (day != this.pDay_) { | 283 if (day != this.pDay_) { |
| 282 changeMessage = changeMessage + day + '\n'; | 284 changeMessage = changeMessage + day + '\n'; |
| 283 this.pDay_ = day; | 285 this.pDay_ = day; |
| 284 } | 286 } |
| (...skipping 26 matching lines...) Expand all Loading... |
| 311 if (((evt.keyCode == 9) && evt.shiftKey) || (evt.keyCode == 37)) { | 313 if (((evt.keyCode == 9) && evt.shiftKey) || (evt.keyCode == 37)) { |
| 312 this.pos_--; | 314 this.pos_--; |
| 313 this.handlePosChange_(); | 315 this.handlePosChange_(); |
| 314 shouldSpeakLabel = true; | 316 shouldSpeakLabel = true; |
| 315 } | 317 } |
| 316 // For all other cases, fall through and let update_ decide if there are any | 318 // For all other cases, fall through and let update_ decide if there are any |
| 317 // changes that need to be spoken. | 319 // changes that need to be spoken. |
| 318 } | 320 } |
| 319 this.update_(shouldSpeakLabel); | 321 this.update_(shouldSpeakLabel); |
| 320 }; | 322 }; |
| OLD | NEW |