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
3// found in the LICENSE file.
4
5/**
6 * @fileoverview A widget hosting an HTML snippet.
7 */
8
9goog.provide('cvox.OverlayWidget');
10
11goog.require('cvox.DomUtil');
12goog.require('cvox.SearchWidget');
13
14
15/**
16 * @param {string} snippet The HTML snippet to render.
17 * @constructor
18 * @extends {cvox.SearchWidget}
19 */
20cvox.OverlayWidget = function(snippet) {
21  goog.base(this);
22  this.snippet_ = snippet;
23};
24goog.inherits(cvox.OverlayWidget, cvox.SearchWidget);
25
26
27/**
28 * @override
29 */
30cvox.OverlayWidget.prototype.show = function() {
31  goog.base(this, 'show');
32  var host = document.createElement('DIV');
33  host.innerHTML = this.snippet_;
34
35  // Position the overlay over the current ChromeVox selection.
36  var hitPoint = cvox.DomUtil.elementToPoint(
37      cvox.ChromeVox.navigationManager.getCurrentNode());
38  host.style.position = 'absolute';
39  host.style.left = hitPoint.x;
40  host.style.top = hitPoint.y;
41
42  document.body.appendChild(host);
43  cvox.ChromeVox.navigationManager.updateSelToArbitraryNode(host);
44  this.host_ = host;
45};
46
47
48/**
49 * @override
50 */
51cvox.OverlayWidget.prototype.hide = function(opt_noSync) {
52  this.host_.remove();
53  goog.base(this, 'hide');
54};
55
56
57/**
58 * @override
59 */
60cvox.OverlayWidget.prototype.onKeyDown = function(evt) {
61  // Allow the base class to handle all keys first.
62  goog.base(this, 'onKeyDown', evt);
63
64  // Do not interfere with any key that exits the widget.
65  if (evt.keyCode == 13 || evt.keyCode == 27) { // Enter or escape.
66    return true;
67  }
68
69  // Bound navigation within the snippet for any other key.
70  var r = cvox.ChromeVox.navigationManager.isReversed();
71  if (!cvox.DomUtil.isDescendantOfNode(
72      cvox.ChromeVox.navigationManager.getCurrentNode(), this.host_)) {
73    if (r) {
74      cvox.ChromeVox.navigationManager.syncToBeginning();
75    } else {
76      cvox.ChromeVox.navigationManager.updateSelToArbitraryNode(this.host_);
77    }
78    this.onNavigate();
79    cvox.ChromeVox.navigationManager.speakDescriptionArray(
80        cvox.ChromeVox.navigationManager.getDescription(), 0, null);
81  }
82};
83