Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / third_party / ot-br-posix / repo / third_party / mdl / repo / src / tooltip / tooltip.js
1 /**
2  * @license
3  * Copyright 2015 Google Inc. All Rights Reserved.
4  *
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  */
17
18 (function() {
19   'use strict';
20
21   /**
22    * Class constructor for Tooltip MDL component.
23    * Implements MDL component design pattern defined at:
24    * https://github.com/jasonmayes/mdl-component-design-pattern
25    *
26    * @constructor
27    * @param {HTMLElement} element The element that will be upgraded.
28    */
29   var MaterialTooltip = function MaterialTooltip(element) {
30     this.element_ = element;
31
32     // Initialize instance.
33     this.init();
34   };
35   window['MaterialTooltip'] = MaterialTooltip;
36
37   /**
38    * Store constants in one place so they can be updated easily.
39    *
40    * @enum {string | number}
41    * @private
42    */
43   MaterialTooltip.prototype.Constant_ = {
44     // None for now.
45   };
46
47   /**
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.
51    *
52    * @enum {string}
53    * @private
54    */
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'
61   };
62
63   /**
64    * Handle mouseenter for tooltip.
65    *
66    * @param {Event} event The event that fired.
67    * @private
68    */
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);
75
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';
81       } else {
82         this.element_.style.top = top + 'px';
83         this.element_.style.marginTop = marginTop + 'px';
84       }
85     } else {
86       if (left + marginLeft < 0) {
87         this.element_.style.left = '0';
88         this.element_.style.marginLeft = '0';
89       } else {
90         this.element_.style.left = left + 'px';
91         this.element_.style.marginLeft = marginLeft + 'px';
92       }
93     }
94
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';
101     } else {
102       this.element_.style.top = props.top + props.height + 10 + 'px';
103     }
104
105     this.element_.classList.add(this.CssClasses_.IS_ACTIVE);
106   };
107
108   /**
109    * Hide tooltip on mouseleave or scroll
110    *
111    * @private
112    */
113   MaterialTooltip.prototype.hideTooltip_ = function() {
114     this.element_.classList.remove(this.CssClasses_.IS_ACTIVE);
115   };
116
117   /**
118    * Initialize element.
119    */
120   MaterialTooltip.prototype.init = function() {
121
122     if (this.element_) {
123       var forElId = this.element_.getAttribute('for') ||
124           this.element_.getAttribute('data-mdl-for');
125
126       if (forElId) {
127         this.forElement_ = document.getElementById(forElId);
128       }
129
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');
134         }
135
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);
143       }
144     }
145   };
146
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'
153   });
154 })();