OLD | NEW |
| (Empty) |
1 <!-- | |
2 Copyright (c) 2014 The Polymer Project Authors. All rights reserved. | |
3 This code may only be used under the BSD style license found at http://polymer.g
ithub.io/LICENSE.txt | |
4 The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt | |
5 The complete set of contributors may be found at http://polymer.github.io/CONTRI
BUTORS.txt | |
6 Code distributed by Google as part of the polymer project is also | |
7 subject to an additional IP rights grant found at http://polymer.github.io/PATEN
TS.txt | |
8 --> | |
9 | |
10 <!-- | |
11 The `core-tooltip` element creates a hover tooltip centered for the content | |
12 it contains. It can be positioned on the top|bottom|left|right of content using | |
13 the `position` attribute. | |
14 | |
15 To include HTML in the tooltip, include the `tip` attribute on the relevant | |
16 content. | |
17 | |
18 <b>Example</b>: | |
19 | |
20 <core-tooltip label="I'm a tooltip"> | |
21 <span>Hover over me.</span> | |
22 </core-tooltip> | |
23 | |
24 <b>Example</b> - positioning the tooltip to the right: | |
25 | |
26 <core-tooltip label="I'm a tooltip to the right" position="right"> | |
27 <core-icon-button icon="drawer"></core-icon-button> | |
28 </core-tooltip> | |
29 | |
30 <b>Example</b> - no arrow and showing by default: | |
31 | |
32 <core-tooltip label="Tooltip with no arrow and always on" noarrow show> | |
33 <img src="image.jpg"> | |
34 </core-tooltip> | |
35 | |
36 <b>Example</b> - disable the tooltip. | |
37 | |
38 <core-tooltip label="Disabled label never shows" disabled> | |
39 ... | |
40 </core-tooltip> | |
41 | |
42 <b>Example</b> - rich tooltip using the `tip` attribute: | |
43 | |
44 <core-tooltip> | |
45 <div>Example of a rich information tooltip</div> | |
46 <div tip> | |
47 <img src="profile.jpg">Foo <b>Bar</b> - <a href="#">@baz</a> | |
48 </div> | |
49 </core-tooltip> | |
50 | |
51 By default, the `tip` attribute specifies the HTML content for a rich tooltip. | |
52 You can customize this attribute with the `tipAttribute` attribute: | |
53 | |
54 <core-tooltip tipAttribute="htmltooltip"> | |
55 <div>Example of a rich information tooltip</div> | |
56 <div htmltooltip> | |
57 ... | |
58 </div> | |
59 </core-tooltip> | |
60 | |
61 @group Polymer Core Elements | |
62 @element core-tooltip | |
63 @extends paper-focusable | |
64 @homepage http://www.polymer-project.org/components/core-tooltip/index.html | |
65 --> | |
66 | |
67 <link rel="import" href="../polymer/polymer.html"> | |
68 <link rel="import" href="../paper-focusable/paper-focusable.html"> | |
69 | |
70 <!-- TODO: would be nice to inherit from label to get .htmlFor, and .control, | |
71 but the latter is readonly. --> | |
72 <!-- TODO: support off center arrows. --> | |
73 <!-- TODO: detect mobile and apply the .large class, instead of manual | |
74 control. --> | |
75 <!-- TODO: possibly reuse core-overlay. --> | |
76 <polymer-element name="core-tooltip" extends="paper-focusable" attributes="noarr
ow position label show tipAttribute" role="tooltip"> | |
77 <template> | |
78 | |
79 <link rel="stylesheet" href="core-tooltip.css"> | |
80 <div id="tooltip" hidden?="{{!hasTooltipContent}}" | |
81 class="core-tooltip {{position}} {{ {noarrow: noarrow, show: show && !dis
abled} | tokenList}}"> | |
82 <content id="c" select="[{{tipAttribute}}]">{{label}}</content> | |
83 </div> | |
84 | |
85 <content></content> | |
86 | |
87 </template> | |
88 <script> | |
89 | |
90 Polymer({ | |
91 | |
92 /** | |
93 * A simple string label for the tooltip to display. To display a rich | |
94 * HTML tooltip instead, omit `label` and include the `tip` attribute | |
95 * on a child node of `core-tooltip`. | |
96 * | |
97 * @attribute label | |
98 * @type string | |
99 * @default null | |
100 */ | |
101 label: null, | |
102 | |
103 computed: { | |
104 // Indicates whether the tooltip has a set label propety or | |
105 // an element with the `tip` attribute. | |
106 hasTooltipContent: 'label || !!tipElement' | |
107 }, | |
108 | |
109 publish: { | |
110 /** | |
111 * Forces the tooltip to display. If `disabled` is set, this property is i
gnored. | |
112 * | |
113 * @attribute show | |
114 * @type boolean | |
115 * @default false | |
116 */ | |
117 show: {value: false, reflect: true}, | |
118 | |
119 /** | |
120 * Positions the tooltip to the top, right, bottom, left of its content. | |
121 * | |
122 * @attribute position | |
123 * @type string | |
124 * @default 'bottom' | |
125 */ | |
126 position: {value: 'bottom', reflect: true}, | |
127 | |
128 /** | |
129 * If true, the tooltip an arrow pointing towards the content. | |
130 * | |
131 * @attribute noarrow | |
132 * @type boolean | |
133 * @default false | |
134 */ | |
135 noarrow: {value: false, reflect: true} | |
136 }, | |
137 | |
138 /** | |
139 * Customizes the attribute used to specify which content | |
140 * is the rich HTML tooltip. | |
141 * | |
142 * @attribute tipAttribute | |
143 * @type string | |
144 * @default 'tip' | |
145 */ | |
146 tipAttribute: 'tip', | |
147 | |
148 attached: function() { | |
149 this.updatedChildren(); | |
150 }, | |
151 | |
152 updatedChildren: function () { | |
153 this.tipElement = null; | |
154 | |
155 for (var i = 0, el; el = this.$.c.getDistributedNodes()[i]; ++i) { | |
156 if (el.hasAttribute && el.hasAttribute('tip')) { | |
157 this.tipElement = el; | |
158 break; | |
159 } | |
160 } | |
161 | |
162 // Job ensures we're not double calling setPosition() on DOM attach. | |
163 this.job('positionJob', this.setPosition); | |
164 | |
165 // Monitor children to re-position tooltip when light dom changes. | |
166 this.onMutation(this, this.updatedChildren); | |
167 }, | |
168 | |
169 labelChanged: function(oldVal, newVal) { | |
170 this.job('positionJob', this.setPosition); | |
171 }, | |
172 | |
173 positionChanged: function(oldVal, newVal) { | |
174 this.job('positionJob', this.setPosition); | |
175 }, | |
176 | |
177 setPosition: function() { | |
178 var controlWidth = this.clientWidth; | |
179 var controlHeight = this.clientHeight; | |
180 var toolTipWidth = this.$.tooltip.clientWidth; | |
181 var toolTipHeight = this.$.tooltip.clientHeight; | |
182 | |
183 switch (this.position) { | |
184 case 'top': | |
185 case 'bottom': | |
186 this.$.tooltip.style.left = (controlWidth - toolTipWidth) / 2 + 'px'; | |
187 this.$.tooltip.style.top = null; | |
188 break; | |
189 case 'left': | |
190 case 'right': | |
191 this.$.tooltip.style.left = null; | |
192 this.$.tooltip.style.top = (controlHeight - toolTipHeight) / 2 + 'px'; | |
193 break; | |
194 } | |
195 } | |
196 }); | |
197 | |
198 </script> | |
199 </polymer-element> | |
OLD | NEW |