3 * Copyright 2015 Google Inc. All Rights Reserved.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
22 * Class constructor for Tooltip MDL component.
23 * Implements MDL component design pattern defined at:
24 * https://github.com/jasonmayes/mdl-component-design-pattern
27 * @param {HTMLElement} element The element that will be upgraded.
29 var MaterialTooltip = function MaterialTooltip(element) {
30 this.element_ = element;
32 // Initialize instance.
35 window['MaterialTooltip'] = MaterialTooltip;
38 * Store constants in one place so they can be updated easily.
40 * @enum {string | number}
43 MaterialTooltip.prototype.Constant_ = {
48 * Store strings for class names defined by this component that are used in
49 * JavaScript. This allows us to simply change it in one place should we
50 * decide to modify at a later date.
55 MaterialTooltip.prototype.CssClasses_ = {
56 IS_ACTIVE: 'is-active',
57 BOTTOM: 'mdl-tooltip--bottom',
58 LEFT: 'mdl-tooltip--left',
59 RIGHT: 'mdl-tooltip--right',
60 TOP: 'mdl-tooltip--top'
64 * Handle mouseenter for tooltip.
66 * @param {Event} event The event that fired.
69 MaterialTooltip.prototype.handleMouseEnter_ = function(event) {
70 var props = event.target.getBoundingClientRect();
71 var left = props.left + (props.width / 2);
72 var top = props.top + (props.height / 2);
73 var marginLeft = -1 * (this.element_.offsetWidth / 2);
74 var marginTop = -1 * (this.element_.offsetHeight / 2);
76 if (this.element_.classList.contains(this.CssClasses_.LEFT) || this.element_.classList.contains(this.CssClasses_.RIGHT)) {
77 left = (props.width / 2);
78 if (top + marginTop < 0) {
79 this.element_.style.top = '0';
80 this.element_.style.marginTop = '0';
82 this.element_.style.top = top + 'px';
83 this.element_.style.marginTop = marginTop + 'px';
86 if (left + marginLeft < 0) {
87 this.element_.style.left = '0';
88 this.element_.style.marginLeft = '0';
90 this.element_.style.left = left + 'px';
91 this.element_.style.marginLeft = marginLeft + 'px';
95 if (this.element_.classList.contains(this.CssClasses_.TOP)) {
96 this.element_.style.top = props.top - this.element_.offsetHeight - 10 + 'px';
97 } else if (this.element_.classList.contains(this.CssClasses_.RIGHT)) {
98 this.element_.style.left = props.left + props.width + 10 + 'px';
99 } else if (this.element_.classList.contains(this.CssClasses_.LEFT)) {
100 this.element_.style.left = props.left - this.element_.offsetWidth - 10 + 'px';
102 this.element_.style.top = props.top + props.height + 10 + 'px';
105 this.element_.classList.add(this.CssClasses_.IS_ACTIVE);
109 * Hide tooltip on mouseleave or scroll
113 MaterialTooltip.prototype.hideTooltip_ = function() {
114 this.element_.classList.remove(this.CssClasses_.IS_ACTIVE);
118 * Initialize element.
120 MaterialTooltip.prototype.init = function() {
123 var forElId = this.element_.getAttribute('for') ||
124 this.element_.getAttribute('data-mdl-for');
127 this.forElement_ = document.getElementById(forElId);
130 if (this.forElement_) {
131 // It's left here because it prevents accidental text selection on Android
132 if (!this.forElement_.hasAttribute('tabindex')) {
133 this.forElement_.setAttribute('tabindex', '0');
136 this.boundMouseEnterHandler = this.handleMouseEnter_.bind(this);
137 this.boundMouseLeaveAndScrollHandler = this.hideTooltip_.bind(this);
138 this.forElement_.addEventListener('mouseenter', this.boundMouseEnterHandler, false);
139 this.forElement_.addEventListener('touchend', this.boundMouseEnterHandler, false);
140 this.forElement_.addEventListener('mouseleave', this.boundMouseLeaveAndScrollHandler, false);
141 window.addEventListener('scroll', this.boundMouseLeaveAndScrollHandler, true);
142 window.addEventListener('touchstart', this.boundMouseLeaveAndScrollHandler);
147 // The component registers itself. It can assume componentHandler is available
148 // in the global scope.
149 componentHandler.register({
150 constructor: MaterialTooltip,
151 classAsString: 'MaterialTooltip',
152 cssClass: 'mdl-tooltip'