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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
|
// 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.
/**
* @fileoverview Watches for events in the browser such as focus changes.
*
*/
goog.provide('cvox.ChromeVoxEventWatcher');
goog.provide('cvox.ChromeVoxEventWatcherUtil');
goog.require('cvox.ActiveIndicator');
goog.require('cvox.ApiImplementation');
goog.require('cvox.AriaUtil');
goog.require('cvox.ChromeVox');
goog.require('cvox.ChromeVoxEditableTextBase');
goog.require('cvox.ChromeVoxEventSuspender');
goog.require('cvox.ChromeVoxHTMLDateWidget');
goog.require('cvox.ChromeVoxHTMLMediaWidget');
goog.require('cvox.ChromeVoxHTMLTimeWidget');
goog.require('cvox.ChromeVoxKbHandler');
goog.require('cvox.ChromeVoxUserCommands');
goog.require('cvox.DomUtil');
goog.require('cvox.Focuser');
goog.require('cvox.History');
goog.require('cvox.LiveRegions');
goog.require('cvox.Memoize');
goog.require('cvox.NavigationSpeaker');
goog.require('cvox.PlatformFilter'); // TODO: Find a better place for this.
goog.require('cvox.PlatformUtil');
goog.require('cvox.QueueMode');
goog.require('cvox.TextHandlerInterface');
goog.require('cvox.UserEventDetail');
/**
* @constructor
*/
cvox.ChromeVoxEventWatcher = function() {
};
/**
* The maximum amount of time to wait before processing events.
* A max time is needed so that even if a page is constantly updating,
* events will still go through.
* @const
* @type {number}
* @private
*/
cvox.ChromeVoxEventWatcher.MAX_WAIT_TIME_MS_ = 50;
/**
* As long as the MAX_WAIT_TIME_ has not been exceeded, the event processor
* will wait this long after the last event was received before starting to
* process events.
* @const
* @type {number}
* @private
*/
cvox.ChromeVoxEventWatcher.WAIT_TIME_MS_ = 10;
/**
* Maximum number of live regions that we will attempt to process.
* @const
* @type {number}
* @private
*/
cvox.ChromeVoxEventWatcher.MAX_LIVE_REGIONS_ = 5;
/**
* Whether or not ChromeVox should echo keys.
* It is useful to turn this off in case the system is already echoing keys (for
* example, in Android).
*
* @type {boolean}
*/
cvox.ChromeVoxEventWatcher.shouldEchoKeys = true;
/**
* Whether or not the next utterance should flush all previous speech.
* Immediately after a key down or user action, we make the next speech
* flush, but otherwise it's better to do a category flush, so if a single
* user action generates both a focus change and a live region change,
* both get spoken.
* @type {boolean}
*/
cvox.ChromeVoxEventWatcher.shouldFlushNextUtterance = false;
/**
* Inits the event watcher and adds listeners.
* @param {!Document|!Window} doc The DOM document to add event listeners to.
*/
cvox.ChromeVoxEventWatcher.init = function(doc) {
/**
* @type {Object}
*/
cvox.ChromeVoxEventWatcher.lastFocusedNode = null;
/**
* @type {Object}
*/
cvox.ChromeVoxEventWatcher.announcedMouseOverNode = null;
/**
* @type {Object}
*/
cvox.ChromeVoxEventWatcher.pendingMouseOverNode = null;
/**
* @type {number?}
*/
cvox.ChromeVoxEventWatcher.mouseOverTimeoutId = null;
/**
* @type {string?}
*/
cvox.ChromeVoxEventWatcher.lastFocusedNodeValue = null;
/**
* @type {Object}
*/
cvox.ChromeVoxEventWatcher.eventToEat = null;
/**
* @type {Element}
*/
cvox.ChromeVoxEventWatcher.currentTextControl = null;
/**
* @type {cvox.ChromeVoxEditableTextBase}
*/
cvox.ChromeVoxEventWatcher.currentTextHandler = null;
/**
* Array of event listeners we've added so we can unregister them if needed.
* @type {Array}
* @private
*/
cvox.ChromeVoxEventWatcher.listeners_ = [];
/**
* The mutation observer we use to listen for live regions.
* @type {MutationObserver}
* @private
*/
cvox.ChromeVoxEventWatcher.mutationObserver_ = null;
/**
* Whether or not mouse hover events should trigger focusing.
* @type {boolean}
*/
cvox.ChromeVoxEventWatcher.focusFollowsMouse = false;
/**
* The delay before a mouseover triggers focusing or announcing anything.
* @type {number}
*/
cvox.ChromeVoxEventWatcher.mouseoverDelayMs = 500;
/**
* Array of events that need to be processed.
* @type {Array<Event>}
* @private
*/
cvox.ChromeVoxEventWatcher.events_ = new Array();
/**
* The time when the last event was received.
* @type {number}
*/
cvox.ChromeVoxEventWatcher.lastEventTime = 0;
/**
* The timestamp for the first unprocessed event.
* @type {number}
*/
cvox.ChromeVoxEventWatcher.firstUnprocessedEventTime = -1;
/**
* Whether or not queue processing is scheduled to run.
* @type {boolean}
* @private
*/
cvox.ChromeVoxEventWatcher.queueProcessingScheduled_ = false;
/**
* A list of callbacks to be called when the EventWatcher has
* completed processing all events in its queue.
* @type {Array<function()>}
* @private
*/
cvox.ChromeVoxEventWatcher.readyCallbacks_ = new Array();
/**
* tracks whether we've received two or more key up's while pass through mode
* is active.
* @type {boolean}
* @private
*/
cvox.ChromeVoxEventWatcher.secondPassThroughKeyUp_ = false;
/**
* Whether or not the ChromeOS Search key (keyCode == 91) is being held.
*
* We must track this manually because on ChromeOS, the Search key being held
* down does not cause keyEvent.metaKey to be set.
*
* TODO (clchen, dmazzoni): Refactor this since there are edge cases
* where manually tracking key down and key up can fail (such as when
* the user switches tabs before letting go of the key being held).
*
* @type {boolean}
*/
cvox.ChromeVox.searchKeyHeld = false;
/**
* The mutation observer that listens for changes to text controls
* that might not send other events.
* @type {MutationObserver}
* @private
*/
cvox.ChromeVoxEventWatcher.textMutationObserver_ = null;
cvox.ChromeVoxEventWatcher.addEventListeners_(doc);
};
/**
* Stores state variables in a provided object.
*
* @param {Object} store The object.
*/
cvox.ChromeVoxEventWatcher.storeOn = function(store) {
store['searchKeyHeld'] = cvox.ChromeVox.searchKeyHeld;
};
/**
* Updates the object with state variables from an earlier storeOn call.
*
* @param {Object} store The object.
*/
cvox.ChromeVoxEventWatcher.readFrom = function(store) {
cvox.ChromeVox.searchKeyHeld = store['searchKeyHeld'];
};
/**
* Adds an event to the events queue and updates the time when the last
* event was received.
*
* @param {Event} evt The event to be added to the events queue.
*/
cvox.ChromeVoxEventWatcher.addEvent = function(evt) {
// Don't add any events to the events queue if ChromeVox is inactive or the
// document isn't focused.
if (!cvox.ChromeVox.isActive || !cvox.ChromeVox.documentHasFocus()) {
if (evt.type == 'focus') {
// If it's a focus event, update the active indicator so that it
// properly shows and hides as focus moves to iframe and webview
// elements.
cvox.ChromeVox.navigationManager.activeIndicator.syncToNode(evt.target);
}
return;
}
cvox.ChromeVoxEventWatcher.events_.push(evt);
cvox.ChromeVoxEventWatcher.lastEventTime = new Date().getTime();
if (cvox.ChromeVoxEventWatcher.firstUnprocessedEventTime == -1) {
cvox.ChromeVoxEventWatcher.firstUnprocessedEventTime = new Date().getTime();
}
if (!cvox.ChromeVoxEventWatcher.queueProcessingScheduled_) {
cvox.ChromeVoxEventWatcher.queueProcessingScheduled_ = true;
window.setTimeout(cvox.ChromeVoxEventWatcher.processQueue_,
cvox.ChromeVoxEventWatcher.WAIT_TIME_MS_);
}
};
/**
* Adds a callback to be called when the event watcher has finished
* processing all pending events.
* @param {Function} cb The callback.
*/
cvox.ChromeVoxEventWatcher.addReadyCallback = function(cb) {
cvox.ChromeVoxEventWatcher.readyCallbacks_.push(cb);
cvox.ChromeVoxEventWatcher.maybeCallReadyCallbacks_();
};
/**
* Returns whether or not there are pending events.
* @return {boolean} Whether or not there are pending events.
* @private
*/
cvox.ChromeVoxEventWatcher.hasPendingEvents_ = function() {
return cvox.ChromeVoxEventWatcher.firstUnprocessedEventTime != -1 ||
cvox.ChromeVoxEventWatcher.queueProcessingScheduled_;
};
/**
* A bit used to make sure only one ready callback is pending at a time.
* @private
*/
cvox.ChromeVoxEventWatcher.readyCallbackRunning_ = false;
/**
* Checks if the event watcher has pending events. If not, call the oldest
* readyCallback in a loop until exhausted or until there are pending events.
* @private
*/
cvox.ChromeVoxEventWatcher.maybeCallReadyCallbacks_ = function() {
if (!cvox.ChromeVoxEventWatcher.readyCallbackRunning_) {
cvox.ChromeVoxEventWatcher.readyCallbackRunning_ = true;
window.setTimeout(function() {
cvox.ChromeVoxEventWatcher.readyCallbackRunning_ = false;
if (!cvox.ChromeVoxEventWatcher.hasPendingEvents_() &&
!cvox.ChromeVoxEventWatcher.queueProcessingScheduled_ &&
cvox.ChromeVoxEventWatcher.readyCallbacks_.length > 0) {
cvox.ChromeVoxEventWatcher.readyCallbacks_.shift()();
cvox.ChromeVoxEventWatcher.maybeCallReadyCallbacks_();
}
}, 5);
}
};
/**
* Add all of our event listeners to the document.
* @param {!Document|!Window} doc The DOM document to add event listeners to.
* @private
*/
cvox.ChromeVoxEventWatcher.addEventListeners_ = function(doc) {
// We always need key down listeners to intercept activate/deactivate.
cvox.ChromeVoxEventWatcher.addEventListener_(doc,
'keydown', cvox.ChromeVoxEventWatcher.keyDownEventWatcher, true);
// If ChromeVox isn't active, skip all other event listeners.
if (!cvox.ChromeVox.isActive || cvox.ChromeVox.entireDocumentIsHidden) {
return;
}
cvox.ChromeVoxEventWatcher.addEventListener_(doc,
'keypress', cvox.ChromeVoxEventWatcher.keyPressEventWatcher, true);
cvox.ChromeVoxEventWatcher.addEventListener_(doc,
'keyup', cvox.ChromeVoxEventWatcher.keyUpEventWatcher, true);
// Listen for our own events to handle public user commands if the web app
// doesn't do it for us.
cvox.ChromeVoxEventWatcher.addEventListener_(doc,
cvox.UserEventDetail.Category.JUMP,
cvox.ChromeVoxUserCommands.handleChromeVoxUserEvent,
false);
cvox.ChromeVoxEventWatcher.addEventListener_(doc,
'focus', cvox.ChromeVoxEventWatcher.focusEventWatcher, true);
cvox.ChromeVoxEventWatcher.addEventListener_(doc,
'blur', cvox.ChromeVoxEventWatcher.blurEventWatcher, true);
cvox.ChromeVoxEventWatcher.addEventListener_(doc,
'change', cvox.ChromeVoxEventWatcher.changeEventWatcher, true);
cvox.ChromeVoxEventWatcher.addEventListener_(doc,
'copy', cvox.ChromeVoxEventWatcher.clipboardEventWatcher, true);
cvox.ChromeVoxEventWatcher.addEventListener_(doc,
'cut', cvox.ChromeVoxEventWatcher.clipboardEventWatcher, true);
cvox.ChromeVoxEventWatcher.addEventListener_(doc,
'paste', cvox.ChromeVoxEventWatcher.clipboardEventWatcher, true);
cvox.ChromeVoxEventWatcher.addEventListener_(doc,
'select', cvox.ChromeVoxEventWatcher.selectEventWatcher, true);
// TODO(dtseng): Experimental, see:
// https://developers.google.com/chrome/whitepapers/pagevisibility
cvox.ChromeVoxEventWatcher.addEventListener_(doc, 'webkitvisibilitychange',
cvox.ChromeVoxEventWatcher.visibilityChangeWatcher, true);
cvox.ChromeVoxEventWatcher.events_ = new Array();
cvox.ChromeVoxEventWatcher.queueProcessingScheduled_ = false;
// Handle mouse events directly without going into the events queue.
cvox.ChromeVoxEventWatcher.addEventListener_(doc,
'mouseover', cvox.ChromeVoxEventWatcher.mouseOverEventWatcher, true);
cvox.ChromeVoxEventWatcher.addEventListener_(doc,
'mouseout', cvox.ChromeVoxEventWatcher.mouseOutEventWatcher, true);
// With the exception of non-Android, click events go through the event queue.
cvox.ChromeVoxEventWatcher.addEventListener_(doc,
'click', cvox.ChromeVoxEventWatcher.mouseClickEventWatcher, true);
cvox.ChromeVoxEventWatcher.mutationObserver_ =
new window.WebKitMutationObserver(
cvox.ChromeVoxEventWatcher.mutationHandler);
var observerTarget = null;
if (doc.documentElement) {
observerTarget = doc.documentElement;
} else if (doc.document && doc.document.documentElement) {
observerTarget = doc.document.documentElement;
}
if (observerTarget) {
cvox.ChromeVoxEventWatcher.mutationObserver_.observe(
observerTarget,
/** @type {!MutationObserverInit} */ ({
childList: true,
attributes: true,
characterData: true,
subtree: true,
attributeOldValue: true,
characterDataOldValue: true
}));
}
};
/**
* Remove all registered event watchers.
* @param {!Document|!Window} doc The DOM document to add event listeners to.
*/
cvox.ChromeVoxEventWatcher.cleanup = function(doc) {
for (var i = 0; i < cvox.ChromeVoxEventWatcher.listeners_.length; i++) {
var listener = cvox.ChromeVoxEventWatcher.listeners_[i];
doc.removeEventListener(
listener.type, listener.listener, listener.useCapture);
}
cvox.ChromeVoxEventWatcher.listeners_ = [];
if (cvox.ChromeVoxEventWatcher.currentDateHandler) {
cvox.ChromeVoxEventWatcher.currentDateHandler.shutdown();
}
if (cvox.ChromeVoxEventWatcher.currentTimeHandler) {
cvox.ChromeVoxEventWatcher.currentTimeHandler.shutdown();
}
if (cvox.ChromeVoxEventWatcher.currentMediaHandler) {
cvox.ChromeVoxEventWatcher.currentMediaHandler.shutdown();
}
if (cvox.ChromeVoxEventWatcher.mutationObserver_) {
cvox.ChromeVoxEventWatcher.mutationObserver_.disconnect();
}
cvox.ChromeVoxEventWatcher.mutationObserver_ = null;
};
/**
* Add one event listener and save the data so it can be removed later.
* @param {!Document|!Window} doc The DOM document to add event listeners to.
* @param {string} type The event type.
* @param {EventListener|function(Event):(boolean|undefined)} listener
* The function to be called when the event is fired.
* @param {boolean} useCapture Whether this listener should capture events
* before they're sent to targets beneath it in the DOM tree.
* @private
*/
cvox.ChromeVoxEventWatcher.addEventListener_ = function(doc, type,
listener, useCapture) {
cvox.ChromeVoxEventWatcher.listeners_.push(
{'type': type, 'listener': listener, 'useCapture': useCapture});
doc.addEventListener(type, listener, useCapture);
};
/**
* Return the last focused node.
* @return {Object} The last node that was focused.
*/
cvox.ChromeVoxEventWatcher.getLastFocusedNode = function() {
return cvox.ChromeVoxEventWatcher.lastFocusedNode;
};
/**
* Sets the last focused node.
* @param {Element} element The last focused element.
*
* @private
*/
cvox.ChromeVoxEventWatcher.setLastFocusedNode_ = function(element) {
cvox.ChromeVoxEventWatcher.lastFocusedNode = element;
cvox.ChromeVoxEventWatcher.lastFocusedNodeValue = !element ? null :
cvox.DomUtil.getControlValueAndStateString(element);
};
/**
* Called when there's any mutation of the document. We use this to
* handle live region updates.
* @param {Array<MutationRecord>} mutations The mutations.
* @return {boolean} True if the default action should be performed.
*/
cvox.ChromeVoxEventWatcher.mutationHandler = function(mutations) {
if (cvox.ChromeVoxEventSuspender.areEventsSuspended()) {
return true;
}
cvox.ChromeVox.navigationManager.updateIndicatorIfChanged();
cvox.LiveRegions.processMutations(
mutations,
function(assertive, navDescriptions) {
var evt = new window.Event('LiveRegion');
evt.navDescriptions = navDescriptions;
evt.assertive = assertive;
cvox.ChromeVoxEventWatcher.addEvent(evt);
return true;
});
};
/**
* Handles mouseclick events.
* Mouseclick events are only triggered if the user touches the mouse;
* we use it to determine whether or not we should bother trying to sync to a
* selection.
* @param {Event} evt The mouseclick event to process.
* @return {boolean} True if the default action should be performed.
*/
cvox.ChromeVoxEventWatcher.mouseClickEventWatcher = function(evt) {
if (evt.fromCvox) {
return true;
}
if (cvox.ChromeVox.host.mustRedispatchClickEvent()) {
cvox.ChromeVoxUserCommands.wasMouseClicked = true;
evt.stopPropagation();
evt.preventDefault();
// Since the click event was caught and we are re-dispatching it, we also
// need to refocus the current node because the current node has already
// been blurred by the window getting the click event in the first place.
// Failing to restore focus before clicking can cause odd problems such as
// the soft IME not coming up in Android (it only shows up if the click
// happens in a focused text field).
cvox.Focuser.setFocus(cvox.ChromeVox.navigationManager.getCurrentNode());
cvox.ChromeVox.tts.speak(
Msgs.getMsg('element_clicked'),
cvox.ChromeVoxEventWatcher.queueMode_(),
cvox.AbstractTts.PERSONALITY_ANNOTATION);
var targetNode = cvox.ChromeVox.navigationManager.getCurrentNode();
// If the targetNode has a defined onclick function, just call it directly
// rather than try to generate a click event and dispatching it.
// While both work equally well on standalone Chrome, when dealing with
// embedded WebViews, generating a click event and sending it is not always
// reliable since the framework may swallow the event.
cvox.DomUtil.clickElem(targetNode, false, true);
return false;
} else {
cvox.ChromeVoxEventWatcher.addEvent(evt);
}
cvox.ChromeVoxUserCommands.wasMouseClicked = true;
return true;
};
/**
* Handles mouseover events.
* Mouseover events are only triggered if the user touches the mouse, so
* for users who only use the keyboard, this will have no effect.
*
* @param {Event} evt The mouseover event to process.
* @return {boolean} True if the default action should be performed.
*/
cvox.ChromeVoxEventWatcher.mouseOverEventWatcher = function(evt) {
// Chrome simulates the meta key for mouse events generated from
// touch exploration.
var isTouchEvent = (evt.metaKey);
var mouseoverDelayMs = cvox.ChromeVoxEventWatcher.mouseoverDelayMs;
if (isTouchEvent) {
mouseoverDelayMs = 0;
} else if (!cvox.ChromeVoxEventWatcher.focusFollowsMouse) {
return true;
}
if (cvox.DomUtil.isDescendantOfNode(
cvox.ChromeVoxEventWatcher.announcedMouseOverNode, evt.target)) {
return true;
}
if (evt.target == cvox.ChromeVoxEventWatcher.pendingMouseOverNode) {
return true;
}
cvox.ChromeVoxEventWatcher.pendingMouseOverNode = evt.target;
if (cvox.ChromeVoxEventWatcher.mouseOverTimeoutId) {
window.clearTimeout(cvox.ChromeVoxEventWatcher.mouseOverTimeoutId);
cvox.ChromeVoxEventWatcher.mouseOverTimeoutId = null;
}
if (evt.target.tagName && (evt.target.tagName == 'BODY')) {
cvox.ChromeVoxEventWatcher.pendingMouseOverNode = null;
cvox.ChromeVoxEventWatcher.announcedMouseOverNode = null;
return true;
}
// Only focus and announce if the mouse stays over the same target
// for longer than the given delay.
cvox.ChromeVoxEventWatcher.mouseOverTimeoutId = window.setTimeout(
function() {
cvox.ChromeVoxEventWatcher.mouseOverTimeoutId = null;
if (evt.target != cvox.ChromeVoxEventWatcher.pendingMouseOverNode) {
return;
}
cvox.Memoize.scope(function() {
cvox.ChromeVoxEventWatcher.shouldFlushNextUtterance = true;
cvox.ChromeVox.navigationManager.stopReading(true);
var target = /** @type {Node} */(evt.target);
cvox.Focuser.setFocus(target);
cvox.ApiImplementation.syncToNode(
target, true, cvox.ChromeVoxEventWatcher.queueMode_());
cvox.ChromeVoxEventWatcher.announcedMouseOverNode = target;
});
}, mouseoverDelayMs);
return true;
};
/**
* Handles mouseout events.
*
* @param {Event} evt The mouseout event to process.
* @return {boolean} True if the default action should be performed.
*/
cvox.ChromeVoxEventWatcher.mouseOutEventWatcher = function(evt) {
if (evt.target == cvox.ChromeVoxEventWatcher.pendingMouseOverNode) {
cvox.ChromeVoxEventWatcher.pendingMouseOverNode = null;
if (cvox.ChromeVoxEventWatcher.mouseOverTimeoutId) {
window.clearTimeout(cvox.ChromeVoxEventWatcher.mouseOverTimeoutId);
cvox.ChromeVoxEventWatcher.mouseOverTimeoutId = null;
}
}
return true;
};
/**
* Watches for focus events.
*
* @param {Event} evt The focus event to add to the queue.
* @return {boolean} True if the default action should be performed.
*/
cvox.ChromeVoxEventWatcher.focusEventWatcher = function(evt) {
// First remove any dummy spans. We create dummy spans in UserCommands in
// order to sync the browser's default tab action with the user's current
// navigation position.
cvox.ChromeVoxUserCommands.removeTabDummySpan();
if (!cvox.ChromeVoxEventSuspender.areEventsSuspended()) {
cvox.ChromeVoxEventWatcher.addEvent(evt);
} else if (evt.target && evt.target.nodeType == Node.ELEMENT_NODE) {
cvox.ChromeVoxEventWatcher.setLastFocusedNode_(
/** @type {Element} */(evt.target));
}
return true;
};
/**
* Handles for focus events passed to it from the events queue.
*
* @param {Event} evt The focus event to handle.
*/
cvox.ChromeVoxEventWatcher.focusHandler = function(evt) {
if (!cvox.ChromeVox.documentHasFocus()) {
return;
}
if (evt.target &&
evt.target.hasAttribute &&
evt.target.getAttribute('aria-hidden') == 'true' &&
evt.target.getAttribute('chromevoxignoreariahidden') != 'true') {
cvox.ChromeVoxEventWatcher.setLastFocusedNode_(null);
cvox.ChromeVoxEventWatcher.setUpTextHandler();
return;
}
if (evt.target && evt.target != window) {
var target = /** @type {Element} */(evt.target);
var parentControl = cvox.DomUtil.getSurroundingControl(target);
if (parentControl &&
parentControl == cvox.ChromeVoxEventWatcher.lastFocusedNode) {
cvox.ChromeVoxEventWatcher.handleControlChanged(target);
return;
}
if (parentControl) {
cvox.ChromeVoxEventWatcher.setLastFocusedNode_(
/** @type {Element} */(parentControl));
} else {
cvox.ChromeVoxEventWatcher.setLastFocusedNode_(target);
}
var queueMode = cvox.ChromeVoxEventWatcher.queueMode_();
if (cvox.ChromeVoxEventWatcher.getInitialVisibility() ||
cvox.ChromeVoxEventWatcher.handleDialogFocus(target)) {
queueMode = cvox.QueueMode.QUEUE;
}
if (cvox.ChromeVox.navigationManager.clearPageSel(true)) {
queueMode = cvox.QueueMode.QUEUE;
}
// Navigate to this control so that it will be the same for focus as for
// regular navigation.
cvox.ApiImplementation.syncToNode(
target, !document.webkitHidden, queueMode);
if ((evt.target.constructor == HTMLVideoElement) ||
(evt.target.constructor == HTMLAudioElement)) {
cvox.ChromeVoxEventWatcher.setUpMediaHandler_();
return;
}
if (evt.target.hasAttribute) {
var inputType = evt.target.getAttribute('type');
switch (inputType) {
case 'time':
cvox.ChromeVoxEventWatcher.setUpTimeHandler_();
return;
case 'date':
case 'month':
case 'week':
cvox.ChromeVoxEventWatcher.setUpDateHandler_();
return;
}
}
cvox.ChromeVoxEventWatcher.setUpTextHandler();
} else {
cvox.ChromeVoxEventWatcher.setLastFocusedNode_(null);
}
return;
};
/**
* Watches for blur events.
*
* @param {Event} evt The blur event to add to the queue.
* @return {boolean} True if the default action should be performed.
*/
cvox.ChromeVoxEventWatcher.blurEventWatcher = function(evt) {
window.setTimeout(function() {
if (!document.activeElement) {
cvox.ChromeVoxEventWatcher.setLastFocusedNode_(null);
cvox.ChromeVoxEventWatcher.addEvent(evt);
}
}, 0);
return true;
};
/**
* Watches for key down events.
*
* @param {Event} evt The keydown event to add to the queue.
* @return {boolean} True if the default action should be performed.
*/
cvox.ChromeVoxEventWatcher.keyDownEventWatcher = function(evt) {
return /** @type {boolean} */ (cvox.Memoize.scope(
cvox.ChromeVoxEventWatcher.doKeyDownEventWatcher_.bind(this, evt)));
};
/**
* Implementation of |keyDownEventWatcher|.
*
* @param {Event} evt The keydown event to add to the queue.
* @return {boolean} True if the default action should be performed.
* @private
*/
cvox.ChromeVoxEventWatcher.doKeyDownEventWatcher_ = function(evt) {
cvox.ChromeVoxEventWatcher.shouldFlushNextUtterance = true;
if (cvox.ChromeVox.passThroughMode) {
return true;
}
if (cvox.ChromeVox.isChromeOS && evt.keyCode == 91) {
cvox.ChromeVox.searchKeyHeld = true;
}
// Store some extra ChromeVox-specific properties in the event.
evt.searchKeyHeld =
cvox.ChromeVox.searchKeyHeld && cvox.ChromeVox.isActive;
evt.stickyMode = cvox.ChromeVox.isStickyModeOn() && cvox.ChromeVox.isActive;
evt.keyPrefix = cvox.ChromeVox.keyPrefixOn && cvox.ChromeVox.isActive;
cvox.ChromeVox.keyPrefixOn = false;
cvox.ChromeVoxEventWatcher.eventToEat = null;
if (!cvox.ChromeVoxKbHandler.basicKeyDownActionsListener(evt) ||
cvox.ChromeVoxEventWatcher.handleControlAction(evt)) {
// Swallow the event immediately to prevent the arrow keys
// from driving controls on the web page.
evt.preventDefault();
evt.stopPropagation();
// Also mark this as something to be swallowed when the followup
// keypress/keyup counterparts to this event show up later.
cvox.ChromeVoxEventWatcher.eventToEat = evt;
return false;
}
cvox.ChromeVoxEventWatcher.addEvent(evt);
return true;
};
/**
* Watches for key up events.
*
* @param {Event} evt The event to add to the queue.
* @return {boolean} True if the default action should be performed.
* @this {cvox.ChromeVoxEventWatcher}
*/
cvox.ChromeVoxEventWatcher.keyUpEventWatcher = function(evt) {
if (evt.keyCode == 91) {
cvox.ChromeVox.searchKeyHeld = false;
}
if (cvox.ChromeVox.passThroughMode) {
if (!evt.ctrlKey && !evt.altKey && !evt.metaKey && !evt.shiftKey &&
!cvox.ChromeVox.searchKeyHeld) {
// Only reset pass through on the second key up without modifiers since
// the first one is from the pass through shortcut itself.
if (this.secondPassThroughKeyUp_) {
this.secondPassThroughKeyUp_ = false;
cvox.ChromeVox.passThroughMode = false;
} else {
this.secondPassThroughKeyUp_ = true;
}
}
return true;
}
if (cvox.ChromeVoxEventWatcher.eventToEat &&
evt.keyCode == cvox.ChromeVoxEventWatcher.eventToEat.keyCode) {
evt.stopPropagation();
evt.preventDefault();
return false;
}
cvox.ChromeVoxEventWatcher.addEvent(evt);
return true;
};
/**
* Watches for key press events.
*
* @param {Event} evt The event to add to the queue.
* @return {boolean} True if the default action should be performed.
*/
cvox.ChromeVoxEventWatcher.keyPressEventWatcher = function(evt) {
var url = document.location.href;
// Use ChromeVox.typingEcho as default value.
var speakChar = cvox.TypingEcho.shouldSpeakChar(cvox.ChromeVox.typingEcho);
if (typeof cvox.ChromeVox.keyEcho[url] !== 'undefined') {
speakChar = cvox.ChromeVox.keyEcho[url];
}
// Directly handle typed characters here while key echo is on. This
// skips potentially costly computations (especially for content editable).
// This is done deliberately for the sake of responsiveness and in some cases
// (e.g. content editable), to have characters echoed properly.
if (cvox.ChromeVoxEditableTextBase.eventTypingEcho && (speakChar &&
cvox.DomPredicates.editTextPredicate([document.activeElement])) &&
document.activeElement.type !== 'password') {
cvox.ChromeVox.tts.speak(String.fromCharCode(evt.charCode),
cvox.QueueMode.FLUSH);
}
cvox.ChromeVoxEventWatcher.addEvent(evt);
if (cvox.ChromeVoxEventWatcher.eventToEat &&
evt.keyCode == cvox.ChromeVoxEventWatcher.eventToEat.keyCode) {
evt.preventDefault();
evt.stopPropagation();
return false;
}
return true;
};
/**
* Watches for change events.
*
* @param {Event} evt The event to add to the queue.
* @return {boolean} True if the default action should be performed.
*/
cvox.ChromeVoxEventWatcher.changeEventWatcher = function(evt) {
cvox.ChromeVoxEventWatcher.addEvent(evt);
return true;
};
// TODO(dtseng): ChromeVoxEditableText interrupts cut and paste announcements.
/**
* Watches for cut, copy, and paste events.
*
* @param {Event} evt The event to process.
* @return {boolean} True if the default action should be performed.
*/
cvox.ChromeVoxEventWatcher.clipboardEventWatcher = function(evt) {
// Don't announce anything unless this document has focus and the
// editable element that's the target of the clipboard event is visible.
var targetNode = /** @type {Node} */(evt.target);
if (!cvox.ChromeVox.documentHasFocus() ||
!targetNode ||
!cvox.DomUtil.isVisible(targetNode) ||
cvox.AriaUtil.isHidden(targetNode)) {
return true;
}
cvox.ChromeVox.tts.speak(Msgs.getMsg(evt.type).toLowerCase(),
cvox.QueueMode.QUEUE);
var text = '';
switch (evt.type) {
case 'paste':
text = evt.clipboardData.getData('text');
break;
case 'copy':
case 'cut':
text = window.getSelection().toString();
break;
}
cvox.ChromeVox.tts.speak(text, cvox.QueueMode.QUEUE);
cvox.ChromeVox.navigationManager.clearPageSel();
return true;
};
/**
* Handles change events passed to it from the events queue.
*
* @param {Event} evt The event to handle.
*/
cvox.ChromeVoxEventWatcher.changeHandler = function(evt) {
if (cvox.ChromeVoxEventWatcher.setUpTextHandler()) {
return;
}
if (document.activeElement == evt.target) {
cvox.ChromeVoxEventWatcher.handleControlChanged(document.activeElement);
}
};
/**
* Watches for select events.
*
* @param {Event} evt The event to add to the queue.
* @return {boolean} True if the default action should be performed.
*/
cvox.ChromeVoxEventWatcher.selectEventWatcher = function(evt) {
cvox.ChromeVoxEventWatcher.addEvent(evt);
return true;
};
/**
* Listens for WebKit visibility change events.
*/
cvox.ChromeVoxEventWatcher.visibilityChangeWatcher = function() {
cvox.ChromeVoxEventWatcher.initialVisibility = !document.webkitHidden;
if (document.webkitHidden) {
cvox.ChromeVox.navigationManager.stopReading(true);
}
};
/**
* Gets the initial visibility of the page.
* @return {boolean} True if the page is visible and this is the first request
* for visibility state.
*/
cvox.ChromeVoxEventWatcher.getInitialVisibility = function() {
var ret = cvox.ChromeVoxEventWatcher.initialVisibility;
cvox.ChromeVoxEventWatcher.initialVisibility = false;
return ret;
};
/**
* Speaks the text of one live region.
* @param {boolean} assertive True if it's an assertive live region.
* @param {Array<cvox.NavDescription>} messages An array of navDescriptions
* representing the description of the live region changes.
* @private
*/
cvox.ChromeVoxEventWatcher.speakLiveRegion_ = function(
assertive, messages) {
var queueMode = cvox.ChromeVoxEventWatcher.queueMode_();
var descSpeaker = new cvox.NavigationSpeaker();
descSpeaker.speakDescriptionArray(messages, queueMode, null);
};
/**
* Sets up the text handler.
* @return {boolean} True if an editable text control has focus.
*/
cvox.ChromeVoxEventWatcher.setUpTextHandler = function() {
var currentFocus = document.activeElement;
if (currentFocus &&
currentFocus.hasAttribute &&
currentFocus.getAttribute('aria-hidden') == 'true' &&
currentFocus.getAttribute('chromevoxignoreariahidden') != 'true') {
currentFocus = null;
}
if (currentFocus != cvox.ChromeVoxEventWatcher.currentTextControl) {
if (cvox.ChromeVoxEventWatcher.currentTextControl) {
cvox.ChromeVoxEventWatcher.currentTextControl.removeEventListener(
'input', cvox.ChromeVoxEventWatcher.changeEventWatcher, false);
cvox.ChromeVoxEventWatcher.currentTextControl.removeEventListener(
'click', cvox.ChromeVoxEventWatcher.changeEventWatcher, false);
if (cvox.ChromeVoxEventWatcher.textMutationObserver_) {
cvox.ChromeVoxEventWatcher.textMutationObserver_.disconnect();
cvox.ChromeVoxEventWatcher.textMutationObserver_ = null;
}
}
cvox.ChromeVoxEventWatcher.currentTextControl = null;
if (cvox.ChromeVoxEventWatcher.currentTextHandler) {
cvox.ChromeVoxEventWatcher.currentTextHandler.teardown();
cvox.ChromeVoxEventWatcher.currentTextHandler = null;
}
if (currentFocus == null) {
return false;
}
if (currentFocus.constructor == HTMLInputElement &&
cvox.DomUtil.isInputTypeText(currentFocus) &&
cvox.ChromeVoxEventWatcher.shouldEchoKeys) {
cvox.ChromeVoxEventWatcher.currentTextControl = currentFocus;
cvox.ChromeVoxEventWatcher.currentTextHandler =
new cvox.ChromeVoxEditableHTMLInput(currentFocus, cvox.ChromeVox.tts);
} else if ((currentFocus.constructor == HTMLTextAreaElement) &&
cvox.ChromeVoxEventWatcher.shouldEchoKeys) {
cvox.ChromeVoxEventWatcher.currentTextControl = currentFocus;
cvox.ChromeVoxEventWatcher.currentTextHandler =
new cvox.ChromeVoxEditableTextArea(currentFocus, cvox.ChromeVox.tts);
} else if (currentFocus.isContentEditable ||
currentFocus.getAttribute('role') == 'textbox') {
cvox.ChromeVoxEventWatcher.currentTextControl = currentFocus;
cvox.ChromeVoxEventWatcher.currentTextHandler =
new cvox.ChromeVoxEditableContentEditable(currentFocus,
cvox.ChromeVox.tts);
}
if (cvox.ChromeVoxEventWatcher.currentTextControl) {
cvox.ChromeVoxEventWatcher.currentTextControl.addEventListener(
'input', cvox.ChromeVoxEventWatcher.changeEventWatcher, false);
cvox.ChromeVoxEventWatcher.currentTextControl.addEventListener(
'click', cvox.ChromeVoxEventWatcher.changeEventWatcher, false);
if (window.WebKitMutationObserver) {
cvox.ChromeVoxEventWatcher.textMutationObserver_ =
new window.WebKitMutationObserver(
cvox.ChromeVoxEventWatcher.onTextMutation);
cvox.ChromeVoxEventWatcher.textMutationObserver_.observe(
cvox.ChromeVoxEventWatcher.currentTextControl,
/** @type {!MutationObserverInit} */ ({
childList: true,
attributes: true,
subtree: true,
attributeOldValue: false,
characterDataOldValue: false
}));
}
if (!cvox.ChromeVoxEventSuspender.areEventsSuspended()) {
cvox.ChromeVox.navigationManager.updateSel(
cvox.CursorSelection.fromNode(
cvox.ChromeVoxEventWatcher.currentTextControl));
}
}
return (null != cvox.ChromeVoxEventWatcher.currentTextHandler);
}
};
/**
* Speaks updates to editable text controls as needed.
*
* @param {boolean} isKeypress Was this change triggered by a keypress?
* @return {boolean} True if an editable text control has focus.
*/
cvox.ChromeVoxEventWatcher.handleTextChanged = function(isKeypress) {
if (cvox.ChromeVoxEventWatcher.currentTextHandler) {
var handler = cvox.ChromeVoxEventWatcher.currentTextHandler;
var shouldFlush = false;
if (isKeypress && cvox.ChromeVoxEventWatcher.shouldFlushNextUtterance) {
shouldFlush = true;
cvox.ChromeVoxEventWatcher.shouldFlushNextUtterance = false;
}
handler.update(shouldFlush);
cvox.ChromeVoxEventWatcher.shouldFlushNextUtterance = false;
return true;
}
return false;
};
/**
* Called when an editable text control has focus, because many changes
* to a text box don't ever generate events - e.g. if the page's javascript
* changes the contents of the text box after some delay, or if it's
* contentEditable or a generic div with role="textbox".
*/
cvox.ChromeVoxEventWatcher.onTextMutation = function() {
if (cvox.ChromeVoxEventWatcher.currentTextHandler) {
window.setTimeout(function() {
cvox.ChromeVoxEventWatcher.handleTextChanged(false);
}, cvox.ChromeVoxEventWatcher.MAX_WAIT_TIME_MS_);
}
};
/**
* Speaks updates to other form controls as needed.
* @param {Element} control The target control.
*/
cvox.ChromeVoxEventWatcher.handleControlChanged = function(control) {
var newValue = cvox.DomUtil.getControlValueAndStateString(control);
var parentControl = cvox.DomUtil.getSurroundingControl(control);
var announceChange = false;
if (control != cvox.ChromeVoxEventWatcher.lastFocusedNode &&
(parentControl == null ||
parentControl != cvox.ChromeVoxEventWatcher.lastFocusedNode)) {
cvox.ChromeVoxEventWatcher.setLastFocusedNode_(control);
} else if (newValue == cvox.ChromeVoxEventWatcher.lastFocusedNodeValue) {
return;
}
cvox.ChromeVoxEventWatcher.lastFocusedNodeValue = newValue;
if (cvox.DomPredicates.checkboxPredicate([control]) ||
cvox.DomPredicates.radioPredicate([control])) {
// Always announce changes to checkboxes and radio buttons.
announceChange = true;
// Play earcons for checkboxes and radio buttons
if (control.checked) {
cvox.ChromeVox.earcons.playEarcon(cvox.Earcon.CHECK_ON);
} else {
cvox.ChromeVox.earcons.playEarcon(cvox.Earcon.CHECK_OFF);
}
}
if (control.tagName == 'SELECT') {
announceChange = true;
}
if (control.tagName == 'INPUT') {
switch (control.type) {
case 'color':
case 'datetime':
case 'datetime-local':
case 'range':
announceChange = true;
break;
default:
break;
}
}
// Always announce changes for anything with an ARIA role.
if (control.hasAttribute && control.hasAttribute('role')) {
announceChange = true;
}
var activeDescendant = cvox.AriaUtil.getActiveDescendant(control);
if ((parentControl &&
parentControl != control &&
document.activeElement == control)) {
// Sync ChromeVox to the newly selected control.
cvox.ApiImplementation.syncToNode(
activeDescendant || control, true,
cvox.ChromeVoxEventWatcher.queueMode_());
announceChange = false;
} else if (activeDescendant) {
cvox.ChromeVox.navigationManager.updateSelToArbitraryNode(
activeDescendant,
true);
announceChange = true;
}
if (announceChange && !cvox.ChromeVoxEventSuspender.areEventsSuspended()) {
cvox.ChromeVox.tts.speak(newValue,
cvox.ChromeVoxEventWatcher.queueMode_(),
null);
cvox.ChromeVox.braille.write(cvox.NavBraille.fromText(newValue));
}
};
/**
* Handle actions on form controls triggered by key presses.
* @param {Object} evt The event.
* @return {boolean} True if this key event was handled.
*/
cvox.ChromeVoxEventWatcher.handleControlAction = function(evt) {
// Ignore the control action if ChromeVox is not active.
if (!cvox.ChromeVox.isActive) {
return false;
}
var control = evt.target;
if (control.tagName == 'SELECT' && (control.size <= 1) &&
(evt.keyCode == 13 || evt.keyCode == 32)) { // Enter or Space
// TODO (dmazzoni, clchen): Remove this workaround once accessibility
// APIs make browser based popups accessible.
//
// Do nothing, but eat this keystroke when the SELECT control
// has a dropdown style since if we don't, it will generate
// a browser popup menu which is not accessible.
// List style SELECT controls are fine and don't need this workaround.
evt.preventDefault();
evt.stopPropagation();
return true;
}
if (control.tagName == 'INPUT' && control.type == 'range') {
var value = parseFloat(control.value);
var step;
if (control.step && control.step > 0.0) {
step = control.step;
} else if (control.min && control.max) {
var range = (control.max - control.min);
if (range > 2 && range < 31) {
step = 1;
} else {
step = (control.max - control.min) / 10;
}
} else {
step = 1;
}
if (evt.keyCode == 37 || evt.keyCode == 38) { // left or up
value -= step;
} else if (evt.keyCode == 39 || evt.keyCode == 40) { // right or down
value += step;
}
if (control.max && value > control.max) {
value = control.max;
}
if (control.min && value < control.min) {
value = control.min;
}
control.value = value;
}
return false;
};
/**
* When an element receives focus, see if we've entered or left a dialog
* and return a string describing the event.
*
* @param {Element} target The element that just received focus.
* @return {boolean} True if an announcement was spoken.
*/
cvox.ChromeVoxEventWatcher.handleDialogFocus = function(target) {
var dialog = target;
var role = '';
while (dialog) {
if (dialog.hasAttribute) {
role = dialog.getAttribute('role');
if (role == 'dialog' || role == 'alertdialog') {
break;
}
}
dialog = dialog.parentElement;
}
if (dialog == cvox.ChromeVox.navigationManager.currentDialog) {
return false;
}
if (cvox.ChromeVox.navigationManager.currentDialog && !dialog) {
if (!cvox.DomUtil.isDescendantOfNode(
document.activeElement,
cvox.ChromeVox.navigationManager.currentDialog)) {
cvox.ChromeVox.navigationManager.currentDialog = null;
cvox.ChromeVoxEventWatcher.speakAnnotationWithCategory_(
Msgs.getMsg('exiting_dialog'),
cvox.TtsCategory.NAV);
return true;
}
} else {
if (dialog) {
cvox.ChromeVox.navigationManager.currentDialog = dialog;
cvox.ChromeVoxEventWatcher.speakAnnotationWithCategory_(
Msgs.getMsg('entering_dialog'),
cvox.TtsCategory.NAV);
if (role == 'alertdialog') {
var dialogDescArray =
cvox.DescriptionUtil.getFullDescriptionsFromChildren(null, dialog);
var descSpeaker = new cvox.NavigationSpeaker();
descSpeaker.speakDescriptionArray(dialogDescArray,
cvox.QueueMode.QUEUE,
null);
}
return true;
}
}
return false;
};
/**
* Speak the given text with the annotation personality and the given
* speech queue utterance category.
* @param {string} text The text to speak.
* @param {string} category The category of text, used by the speech queue
* when flushing all speech from the same category while leaving other
* speech in the queue.
* @private
*/
cvox.ChromeVoxEventWatcher.speakAnnotationWithCategory_ = function(
text, category) {
var properties = {};
var src = cvox.AbstractTts.PERSONALITY_ANNOTATION;
for (var key in src) {
properties[key] = src[key];
}
properties['category'] = category;
cvox.ChromeVox.tts.speak(
text,
cvox.ChromeVoxEventWatcher.queueMode_(),
properties);
};
/**
* Returns true if we should wait to process events.
* @param {number} lastFocusTimestamp The timestamp of the last focus event.
* @param {number} firstTimestamp The timestamp of the first event.
* @param {number} currentTime The current timestamp.
* @return {boolean} True if we should wait to process events.
*/
cvox.ChromeVoxEventWatcherUtil.shouldWaitToProcess = function(
lastFocusTimestamp, firstTimestamp, currentTime) {
var timeSinceFocusEvent = currentTime - lastFocusTimestamp;
var timeSinceFirstEvent = currentTime - firstTimestamp;
return timeSinceFocusEvent < cvox.ChromeVoxEventWatcher.WAIT_TIME_MS_ &&
timeSinceFirstEvent < cvox.ChromeVoxEventWatcher.MAX_WAIT_TIME_MS_;
};
/**
* Returns the queue mode to use for the next utterance spoken as
* a result of an event or navigation. The first utterance that's spoken
* after an explicit user action like a key press will flush, and
* subsequent events will return a category flush.
* @return {cvox.QueueMode} The queue mode.
* @private
*/
cvox.ChromeVoxEventWatcher.queueMode_ = function() {
if (cvox.ChromeVoxEventWatcher.shouldFlushNextUtterance) {
cvox.ChromeVoxEventWatcher.shouldFlushNextUtterance = false;
return cvox.QueueMode.FLUSH;
}
return cvox.QueueMode.CATEGORY_FLUSH;
};
/**
* Processes the events queue.
*
* @private
*/
cvox.ChromeVoxEventWatcher.processQueue_ = function() {
cvox.Memoize.scope(cvox.ChromeVoxEventWatcher.doProcessQueue_);
};
/**
* Implementation of |processQueue_|.
*
* @private
*/
cvox.ChromeVoxEventWatcher.doProcessQueue_ = function() {
// Return now if there are no events in the queue.
if (cvox.ChromeVoxEventWatcher.events_.length == 0) {
return;
}
// Look for the most recent focus event and delete any preceding event
// that applied to whatever was focused previously.
var events = cvox.ChromeVoxEventWatcher.events_;
var lastFocusIndex = -1;
var lastFocusTimestamp = 0;
var evt;
var i;
for (i = 0; evt = events[i]; i++) {
if (evt.type == 'focus') {
lastFocusIndex = i;
lastFocusTimestamp = evt.timeStamp;
}
}
cvox.ChromeVoxEventWatcher.events_ = [];
for (i = 0; evt = events[i]; i++) {
var prevEvt = events[i - 1] || {};
if ((i >= lastFocusIndex || evt.type == 'LiveRegion') &&
(prevEvt.type != 'focus' || evt.type != 'change')) {
cvox.ChromeVoxEventWatcher.events_.push(evt);
}
}
cvox.ChromeVoxEventWatcher.events_.sort(function(a, b) {
if (b.type != 'LiveRegion' && a.type == 'LiveRegion') {
return 1;
}
return -1;
});
// If the most recent focus event was very recent, wait for things to
// settle down before processing events, unless the max wait time has
// passed.
var currentTime = new Date().getTime();
if (lastFocusIndex >= 0 &&
cvox.ChromeVoxEventWatcherUtil.shouldWaitToProcess(
lastFocusTimestamp,
cvox.ChromeVoxEventWatcher.firstUnprocessedEventTime,
currentTime)) {
window.setTimeout(cvox.ChromeVoxEventWatcher.processQueue_,
cvox.ChromeVoxEventWatcher.WAIT_TIME_MS_);
return;
}
// Process the remaining events in the queue, in order.
for (i = 0; evt = cvox.ChromeVoxEventWatcher.events_[i]; i++) {
cvox.ChromeVoxEventWatcher.handleEvent_(evt);
}
cvox.ChromeVoxEventWatcher.events_ = new Array();
cvox.ChromeVoxEventWatcher.firstUnprocessedEventTime = -1;
cvox.ChromeVoxEventWatcher.queueProcessingScheduled_ = false;
cvox.ChromeVoxEventWatcher.maybeCallReadyCallbacks_();
};
/**
* Handle events from the queue by routing them to their respective handlers.
*
* @private
* @param {Event} evt The event to be handled.
*/
cvox.ChromeVoxEventWatcher.handleEvent_ = function(evt) {
switch (evt.type) {
case 'keydown':
case 'input':
cvox.ChromeVoxEventWatcher.setUpTextHandler();
if (cvox.ChromeVoxEventWatcher.currentTextControl) {
cvox.ChromeVoxEventWatcher.handleTextChanged(true);
var editableText = /** @type {cvox.ChromeVoxEditableTextBase} */
(cvox.ChromeVoxEventWatcher.currentTextHandler);
if (editableText && editableText.lastChangeDescribed) {
break;
}
}
// We're either not on a text control, or we are on a text control but no
// text change was described. Let's try describing the state instead.
cvox.ChromeVoxEventWatcher.handleControlChanged(document.activeElement);
break;
case 'keyup':
// Some controls change only after key up.
cvox.ChromeVoxEventWatcher.handleControlChanged(document.activeElement);
break;
case 'keypress':
cvox.ChromeVoxEventWatcher.setUpTextHandler();
break;
case 'click':
cvox.ApiImplementation.syncToNode(/** @type {Node} */(evt.target), true);
break;
case 'focus':
cvox.ChromeVoxEventWatcher.focusHandler(evt);
break;
case 'blur':
cvox.ChromeVoxEventWatcher.setUpTextHandler();
break;
case 'change':
cvox.ChromeVoxEventWatcher.changeHandler(evt);
break;
case 'select':
cvox.ChromeVoxEventWatcher.setUpTextHandler();
break;
case 'LiveRegion':
cvox.ChromeVoxEventWatcher.speakLiveRegion_(
evt.assertive, evt.navDescriptions);
break;
}
};
/**
* Sets up the time handler.
* @return {boolean} True if a time control has focus.
* @private
*/
cvox.ChromeVoxEventWatcher.setUpTimeHandler_ = function() {
var currentFocus = document.activeElement;
if (currentFocus &&
currentFocus.hasAttribute &&
currentFocus.getAttribute('aria-hidden') == 'true' &&
currentFocus.getAttribute('chromevoxignoreariahidden') != 'true') {
currentFocus = null;
}
if (currentFocus.constructor == HTMLInputElement &&
currentFocus.type && (currentFocus.type == 'time')) {
cvox.ChromeVoxEventWatcher.currentTimeHandler =
new cvox.ChromeVoxHTMLTimeWidget(currentFocus, cvox.ChromeVox.tts);
} else {
cvox.ChromeVoxEventWatcher.currentTimeHandler = null;
}
return (null != cvox.ChromeVoxEventWatcher.currentTimeHandler);
};
/**
* Sets up the media (video/audio) handler.
* @return {boolean} True if a media control has focus.
* @private
*/
cvox.ChromeVoxEventWatcher.setUpMediaHandler_ = function() {
var currentFocus = document.activeElement;
if (currentFocus &&
currentFocus.hasAttribute &&
currentFocus.getAttribute('aria-hidden') == 'true' &&
currentFocus.getAttribute('chromevoxignoreariahidden') != 'true') {
currentFocus = null;
}
if ((currentFocus.constructor == HTMLVideoElement) ||
(currentFocus.constructor == HTMLAudioElement)) {
cvox.ChromeVoxEventWatcher.currentMediaHandler =
new cvox.ChromeVoxHTMLMediaWidget(currentFocus, cvox.ChromeVox.tts);
} else {
cvox.ChromeVoxEventWatcher.currentMediaHandler = null;
}
return (null != cvox.ChromeVoxEventWatcher.currentMediaHandler);
};
/**
* Sets up the date handler.
* @return {boolean} True if a date control has focus.
* @private
*/
cvox.ChromeVoxEventWatcher.setUpDateHandler_ = function() {
var currentFocus = document.activeElement;
if (currentFocus &&
currentFocus.hasAttribute &&
currentFocus.getAttribute('aria-hidden') == 'true' &&
currentFocus.getAttribute('chromevoxignoreariahidden') != 'true') {
currentFocus = null;
}
if (currentFocus.constructor == HTMLInputElement &&
currentFocus.type &&
((currentFocus.type == 'date') ||
(currentFocus.type == 'month') ||
(currentFocus.type == 'week'))) {
cvox.ChromeVoxEventWatcher.currentDateHandler =
new cvox.ChromeVoxHTMLDateWidget(currentFocus, cvox.ChromeVox.tts);
} else {
cvox.ChromeVoxEventWatcher.currentDateHandler = null;
}
return (null != cvox.ChromeVoxEventWatcher.currentDateHandler);
};
|