This repository was archived by the owner on Mar 13, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathcore-tooltip.html
More file actions
217 lines (176 loc) · 6.47 KB
/
core-tooltip.html
File metadata and controls
217 lines (176 loc) · 6.47 KB
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
<!--
Copyright (c) 2015 The Polymer Project Authors. All rights reserved.
This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
Code distributed by Google as part of the polymer project is also
subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
-->
<!--
The `core-tooltip` element creates a hover tooltip centered for the content
it contains. It can be positioned on the top|bottom|left|right of content using
the `position` attribute.
To include HTML in the tooltip, include the `tip` attribute on the relevant
content.
<b>Example</b>:
<core-tooltip label="I'm a tooltip">
<span>Hover over me.</span>
</core-tooltip>
<b>Example</b> - positioning the tooltip to the right:
<core-tooltip label="I'm a tooltip to the right" position="right">
<core-icon-button icon="drawer"></core-icon-button>
</core-tooltip>
<b>Example</b> - no arrow and showing by default:
<core-tooltip label="Tooltip with no arrow and always on" noarrow show>
<img src="image.jpg">
</core-tooltip>
<b>Example</b> - disable the tooltip.
<core-tooltip label="Disabled label never shows" disabled>
...
</core-tooltip>
<b>Example</b> - rich tooltip using the `tip` attribute:
<core-tooltip>
<div>Example of a rich information tooltip</div>
<div tip>
<img src="profile.jpg">Foo <b>Bar</b> - <a href="#">@baz</a>
</div>
</core-tooltip>
By default, the `tip` attribute specifies the HTML content for a rich tooltip.
You can customize this attribute with the `tipAttribute` attribute:
<core-tooltip tipAttribute="htmltooltip">
<div>Example of a rich information tooltip</div>
<div htmltooltip>
...
</div>
</core-tooltip>
@group Polymer Core Elements
@element core-tooltip
@mixins Polymer.CoreFocusable https://github.com/polymer/core-focusable
@mixins Polymer.CoreResizable https://github.com/polymer/core-resizable
@homepage http://www.polymer-project.org/components/core-tooltip/index.html
-->
<link rel="import" href="../polymer/polymer.html">
<link rel="import" href="../core-focusable/core-focusable.html">
<link rel="import" href="../core-resizable/core-resizable.html">
<!-- TODO: would be nice to inherit from label to get .htmlFor, and .control,
but the latter is readonly. -->
<!-- TODO: support off center arrows. -->
<!-- TODO: detect mobile and apply the .large class, instead of manual
control. -->
<!-- TODO: possibly reuse core-overlay. -->
<polymer-element name="core-tooltip" attributes="noarrow position label show tipAttribute" role="tooltip" tabindex="0">
<template>
<link rel="stylesheet" href="core-tooltip.css">
<div id="tooltip" hidden?="{{!hasTooltipContent}}"
class="core-tooltip {{position}} {{ {noarrow: noarrow, show: show && !disabled} | tokenList}}">
<content id="c" select="[{{tipAttribute}}]">{{label}}</content>
</div>
<content></content>
</template>
<script>
(function() {
var proto = {
/**
* A simple string label for the tooltip to display. To display a rich
* HTML tooltip instead, omit `label` and include the `tip` attribute
* on a child node of `core-tooltip`.
*
* @attribute label
* @type string
* @default null
*/
label: null,
eventDelegates: {
'core-resize': 'positionChanged'
},
computed: {
// Indicates whether the tooltip has a set label propety or
// an element with the `tip` attribute.
hasTooltipContent: 'label || !!tipElement'
},
publish: {
/**
* Forces the tooltip to display. If `disabled` is set, this property is ignored.
*
* @attribute show
* @type boolean
* @default false
*/
show: {value: false, reflect: true},
/**
* Positions the tooltip to the top, right, bottom, left of its content.
*
* @attribute position
* @type string
* @default 'bottom'
*/
position: {value: 'bottom', reflect: true},
/**
* If true, the tooltip an arrow pointing towards the content.
*
* @attribute noarrow
* @type boolean
* @default false
*/
noarrow: {value: false, reflect: true}
},
/**
* Customizes the attribute used to specify which content
* is the rich HTML tooltip.
*
* @attribute tipAttribute
* @type string
* @default 'tip'
*/
tipAttribute: 'tip',
attached: function() {
this.updatedChildren();
this.resizableAttachedHandler();
},
detached: function() {
this.resizableDetachedHandler();
},
updatedChildren: function () {
this.tipElement = null;
for (var i = 0, el; el = this.$.c.getDistributedNodes()[i]; ++i) {
if (el.hasAttribute && el.hasAttribute(this.tipAttribute)) {
this.tipElement = el;
break;
}
}
// Job ensures we're not double calling setPosition() on DOM attach.
this.job('positionJob', this.setPosition);
// Monitor children to re-position tooltip when light dom changes.
this.onMutation(this, this.updatedChildren);
},
labelChanged: function(oldVal, newVal) {
this.job('positionJob', this.setPosition);
},
positionChanged: function(oldVal, newVal) {
this.job('positionJob', this.setPosition);
},
setPosition: function() {
var controlWidth = this.clientWidth;
var controlHeight = this.clientHeight;
var toolTipWidth = this.$.tooltip.clientWidth;
var toolTipHeight = this.$.tooltip.clientHeight;
switch (this.position) {
case 'top':
case 'bottom':
this.$.tooltip.style.left = (controlWidth - toolTipWidth) / 2 + 'px';
this.$.tooltip.style.top = null;
break;
case 'left':
case 'right':
this.$.tooltip.style.left = null;
this.$.tooltip.style.top = (controlHeight - toolTipHeight) / 2 + 'px';
break;
}
}
};
Polymer.mixin2(proto, Polymer.CoreFocusable);
Polymer.mixin(proto, Polymer.CoreResizable);
Polymer(proto);
})();
</script>
</polymer-element>