1 // Copyright (c) 2013 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.
5 cr.define('cr.ui', function() {
7 * An image button that brings up an informative bubble when activated by
10 * @extends {HTMLSpanElement}
11 * @implements {EventListener}
13 var BubbleButton = cr.ui.define('span');
15 BubbleButton.prototype = {
16 __proto__: HTMLSpanElement.prototype,
19 * Decorates the base element to show the proper icon.
21 decorate: function() {
22 this.className = 'bubble-button';
23 this.location = cr.ui.ArrowLocation.TOP_END;
24 this.image = document.createElement('div');
25 this.image.tabIndex = 0;
26 this.image.setAttribute('role', 'button');
27 this.image.addEventListener('click', this);
28 this.image.addEventListener('keydown', this);
29 this.image.addEventListener('mousedown', this);
30 this.appendChild(this.image);
34 * Whether the button is currently showing a bubble.
38 return this.image.classList.contains('showing-bubble');
40 set showingBubble(showing) {
41 this.image.classList.toggle('showing-bubble', showing);
45 * Handle mouse and keyboard events, allowing the user to open and close an
47 * @param {Event} event Mouse or keyboard event.
49 handleEvent: function(event) {
51 // Toggle the bubble on left click. Let any other clicks propagate.
53 if (event.button != 0)
56 // Toggle the bubble when <Return> or <Space> is pressed. Let any other
57 // key presses propagate.
59 switch (event.keyCode) {
67 // Blur focus when a mouse button is pressed, matching the behavior of
68 // other Web UI elements.
70 if (document.activeElement)
71 document.activeElement.blur();
72 event.preventDefault();
76 event.preventDefault();
77 event.stopPropagation();
81 * Abstract method: subclasses should overwrite it. There is no way to mark
82 * method as abstract for Closure Compiler, as of
83 * https://github.com/google/closure-compiler/issues/104.
84 * @type {!Function|undefined}
87 toggleBubble: assertNotReached,
92 BubbleButton: BubbleButton