Skip to content

Commit 5a6817e

Browse files
author
Billy He
committed
Merge pull request #5 from qiushihe/coordinator
Refactor FlowTip.Coordinator
2 parents cd44061 + daeb8b0 commit 5a6817e

28 files changed

Lines changed: 1926 additions & 3403 deletions

Makefile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
all: flowtip.js
2+
3+
flowtip.js: flowtip.coffee coordinator.coffee
4+
cat flowtip.coffee coordinator.coffee | coffee -c -s > flowtip.js
5+
cp flowtip.js pages/flowtip.js
6+

README.md

Lines changed: 365 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,368 @@
22

33
*A flexible, adaptable and easy to use tooltip positioning library.*
44

5-
See [this page](http://qiushihe.github.io/flowtip) for more information.
5+
* [Website](http://qiushihe.github.io/flowtip)
6+
* [Interactive Demo](http://qiushihe.github.io/flowtip/demo.html)
7+
* [Github Repo](https://github.com/qiushihe/flowtip)
8+
9+
## Dependencies
10+
11+
* [jQuery](http://jquery.com)
12+
* [Unerscore.js](http://underscorejs.org)
13+
* [CoffeeScript](http://coffeescript.org) (development only)
14+
15+
## Glossaries
16+
17+
The documentation and source of this library refers to several terms that may require
18+
clarification:
19+
20+
**Tooltip**: An instance of **FlowTip** object.
21+
22+
**Root**: The **root** of a tooltip refers to the element that contains both the tooltip's
23+
"content" element, and "tail" element.
24+
25+
**Content (element)**: The container element for the tooltip's content.
26+
27+
**Tail**: The tail of the tooltip. It's usually visualized as a pointy "nub" of sort.
28+
29+
**Target**: The target is the thing the tooltip points to.
30+
31+
**Region**: There are four different regions a tooltip could be in at any given time. The four
32+
regions are "top", "bottom", "left" and "right". Region is described relative to the target of
33+
the tooltip. For example, region "top" means the tooltip would appear above the target, and
34+
region "right" means the tooltip would appear to the right side of the target.
35+
36+
**Pivot**: The pivot is a imaginary line that is aligned to the target and the tooltip's root is
37+
then in term aligned to the pivot.
38+
39+
## Basic Usage
40+
41+
Creates an instance of the **FlowTip** object:
42+
43+
var tooltip = new FlowTip();
44+
45+
An instance of the **FlowTip** can be created with options (see **Attributes** section below):
46+
47+
var myFlowTip = new FlowTip({
48+
region: "bottom"
49+
className: "my-tip"
50+
hasTail: false
51+
})
52+
53+
At this point, the tooltip is not yet "attached" to a target. The tooltip's target can be set by
54+
calling `setTarget`:
55+
56+
tooltip.setTarget(tooltipTarget);
57+
58+
... where `tooltipTarget` may be a DOM object or a jQuery selection.
59+
60+
To make the tooltip visible, call `show`:
61+
62+
tooltip.show();
63+
64+
This will render the tooltip is it's not already rendered, and position the tooltip in the
65+
appropriate region (see `region` in **Attributes** section below) with proper alignments
66+
(see **Alignments** section below).
67+
68+
It's important to note that instances of **FlowTip** does **not** automatically re-position
69+
themselves when their targets move. The responsibility of detecting target movement lies outside
70+
the scope of this library. To update the tooltip's position against its target:
71+
72+
tooltip.reposition();
73+
74+
## Attributes
75+
76+
### `className`: **String**
77+
78+
_Default value: ""_
79+
80+
Additional class name(s) for the container of the tooltip.
81+
82+
### `contentClassName`: **String**
83+
84+
_Default value: ""_
85+
86+
Additional class name(s) for the tooltip's content.
87+
88+
### `tailClassName`: **String**
89+
90+
_Default value: ""_
91+
92+
Additional class name(s) for the tooltip's tail.
93+
94+
### `appendTo`: **Element**
95+
96+
_Default value: null_
97+
98+
The element within which the tooltip will be inserted into. Can be set or updated by calling
99+
`setAppendTo`. Default value is the document's `body` and the tooltip would thus be free to appear
100+
and move anywhere on the page and edge detection will only be performed on the edge of the page.
101+
102+
If `appendTo` is set to an element then edge detection will be performed on the edge of
103+
the element instead.
104+
105+
### `tooltipContent`: **String** or **Element**
106+
107+
_Default value: null_
108+
109+
The content of the tooltip. May be specified as (HTML) string or DOM element. Can be set original
110+
updated by calling `setTooltipContent`.
111+
112+
### `region`: **String**
113+
114+
_Default value: top_
115+
116+
The preferred region in which the tooltip will appear at first relative to its target. Possibly
117+
values are: `top`, `bottom`, `left` and `right`.
118+
119+
### `topDisabled`, `bottomDisabled`, `leftDisabled` and `rightDisabled`: **Boolean**
120+
121+
_Default value: false_
122+
123+
When set to `true`, the specified region will become unavailable for the various edge detection
124+
algorithms.
125+
126+
### `hideInDisabledRegions`: **Boolean**
127+
128+
_Default value: false_
129+
130+
When set to `true`, and when the only suitable regions for the tooltip are disabled, the tooltip
131+
will be hidden. When set to `false`, and when the only suitable regions for the tooltip are
132+
disabled, the tooltip will be placed in its original region.
133+
134+
### `persevere`: **Boolean**
135+
136+
_Default value: false_
137+
138+
If set to `true`, the tooltip will revert back to its preferred region whenever there is enough
139+
space in that region. If set to `false`, the tooltip will remain in the region edge detection
140+
puts it in, until edge detection changes it again.
141+
142+
### `hasTail`: **Boolean**
143+
144+
_Default value: true_
145+
146+
Controls if the tooltip's tail's visibility. When set to `false` the tooltip's tail will be hidden
147+
and ignored for all positioning calculations.
148+
149+
### `width`: **Integer**
150+
151+
_Default value: null_
152+
153+
Width of the tooltip's root. If set to `null`, the tooltip will expand to fit its content's width.
154+
155+
### `height`: **Integer** or the string "auto"
156+
157+
_Default value: auto_
158+
159+
Height of the tooltip's root. If set to `null`, the tooltip will expand to fit its content's
160+
height. If set to `"auto"`, the tooltip's content will be relatively positioned to allow possible
161+
scrolling.
162+
163+
### `minWidth`: **String**
164+
165+
_Default value: null_
166+
167+
In the form of `42px`, or other forms accepted by CSS min-width.
168+
169+
### `minHeight`: **String**
170+
171+
_Default value: null_
172+
173+
In the form of `42px`, or other forms accepted by CSS max-width.
174+
175+
### `maxWidth`: **String**
176+
177+
_Default value: null_
178+
179+
In the form of `42px`, or other forms accepted by CSS min-height.
180+
181+
### `maxHeight`:**String**
182+
183+
_Default value: null_
184+
185+
In the form of `42px`, or other forms accepted by CSS max-height.
186+
187+
### `tailWidth`: **Integer**
188+
189+
_Default value: 20_
190+
191+
Width of the tail element.
192+
193+
### `tailHeight`: **Integer**
194+
195+
_Default value: 10_
196+
197+
Height of the tail element.
198+
199+
### `targetOffset`: **Integer**
200+
201+
_Default value: 10_
202+
203+
The distance between the tooltip's root (or tip of the tail, see `targetOffsetFrom` below) and
204+
the target's edge.
205+
206+
### `targetOffsetFrom`: **String**
207+
208+
_Default value: root_
209+
210+
Possible values are `root` and `tail`. When set to `root`, `targetOffset` will be calculated
211+
against the edge of the tooltip's root; When set to `tail`, `targetOffset` will be calculated
212+
against the tip of the tail.
213+
214+
### `edgeOffset`: **Integer**
215+
216+
_Default value: 30_
217+
218+
The distance between the tooltip's root's edge and the edge of the boundary representative the
219+
tooltip's `appendTo` element. When this distance is smaller than `edgeOffset`, edge detection
220+
will place the tooltip in an opposite region (i.e. flipping the tooltip).
221+
222+
### `rotationOffset`: **Integer**
223+
224+
_Default value: 30_
225+
226+
The distance between the target's edge and the edge of the boundary representative the tooltip's
227+
`appendTo` element. When this distance is smaller than `rotationOffset`, edge detection will
228+
place the tooltip in an adjacent region (i.e. rotating the tooltip).
229+
230+
### `targetAlign`: **String**
231+
232+
_Default value: center_
233+
234+
Possible values are `center` and `edge`. When set to `center`, the pivot will be center aligned
235+
against the target. When set to `edge`, one of the pivot's edge will be aligned against the pivot.
236+
See `targetAlignOffset` for the exact mechanics of the two values. This value can also be
237+
specified on a per-region basis via `[top|bottom|left|right]TargetAlign`, and if a region's
238+
specific value is absence, the value from `targetAlign` will be used.
239+
240+
### `targetAlignOffset`: **Integer**
241+
242+
_Default value: 0_
243+
244+
If `targetAlign` is set to `center`, this value controls the clockwise distance from the center
245+
of the target the pivot will shift. When this value is positive, the pivot will shift clockwise,
246+
and counter-clockwise when this value is negative.
247+
248+
If `targetAlign` is set to `edge`, the pivot will shift away from one of the target's edges.
249+
When this value is positive, the clockwise edge will be used, and the counter-clockwise edge
250+
will be used when this value is negative. The absolute value of this value controls the amount
251+
of shifting.
252+
253+
### `rootAlign`: **String**
254+
255+
_Default value: center_
256+
257+
Possible values are `center` and `edge`. When set to `center`, the tooltip's root will be center
258+
aligned against the pivot. When set to `edge`, one of the tooltip's root's edge will be aligned
259+
against the pivot. See `rootAlignOffset` for the exact mechanics of the two values. This value
260+
can also be specified on a per-region basis via `[top|bottom|left|right]RootAlign`, and if a
261+
region's specific value is absence, the value from `rootAlign` will be used.
262+
263+
### `rootAlignOffset`: **Integer**
264+
265+
_Default value: 0_
266+
267+
If `rootAlign` is set to `center`, this value controls the clockwise distance from the pivot the
268+
tooltip's root will shift. When this value is positive, the root will shift clockwise, and
269+
counter-clockwise when this value is negative.
270+
271+
If `rootAlign` is set to `edge`, one of the tooltip's root's edge will be aligned against the
272+
pivot. When this value is positive, the clockwise edge will be used, and the counter-clockwise
273+
edge will be used when this value is negative. The absolute value of this value controls the
274+
amount of shifting.
275+
276+
## Public Methods
277+
278+
### `constructor`
279+
280+
Accepts a hash of options. See **Attributes**.
281+
282+
### `render`
283+
284+
Render the tooltip's root (if not already rendered), content and insert the tooltip into the DOM.
285+
If called repeatedly, only re-render the tooltip's content.
286+
287+
### `setAppendTo`
288+
289+
Set the tooltip's containing element. If the tooltip has already been rendered, the tooltip will
290+
be moved/inserted into the new containing element.
291+
292+
### `setTarget`
293+
294+
Set the tooltip's target. The target is the element to which the tooltip will be pointing at.
295+
296+
### `setClientRectTarget`
297+
298+
Set the tooltip's target to be a ClientRect instead of a html element. The target is the
299+
ClientRect to which the tooltip will be pointing at.
300+
301+
### `setTooltipContent`
302+
303+
Set the tooltip's content. If `render` is set to `true` for `options`, the `render()` method will
304+
be called to re-render the content.
305+
306+
### `reposition`
307+
308+
Perform edge detection and position calculations to update the tooltip's position.
309+
310+
### `show`
311+
312+
Show the tooltip. Also update the tooltip's `visible` attribute.
313+
314+
### `hide`
315+
316+
Hide the tooltip. Also update the tooltip's `visible` attribute.
317+
318+
### `trigger`
319+
320+
Trigger an event from the root of the tooltip.
321+
322+
### `destroy`
323+
324+
Remove the tooltip's root from DOM.
325+
326+
## Edge Detection
327+
328+
There are three strategies for detection: **flip**, **rotate** and **squeeze**.
329+
330+
### Flip
331+
332+
The tooltip will be flipped when the root of the tooltip gets too close to the edge of the
333+
boundary. Adjust `edgeOffset` to control the amount of space allowed. The tooltip will be
334+
flipped horizontally when in the left and right regions, and vertically when in the top and
335+
bottom regions.
336+
337+
### Rotate
338+
339+
The tooltip will be rotated to an adjacent region if the target gets too close to the edge of
340+
the boundary. Adjust `rotationOffset` to control the amount of space allowed. For example, the
341+
tooltip will be rotated to the top region if the tooltip is currently in the left or right
342+
region, and the target gets too close to the bottom edge of the boundary.
343+
344+
### Squeeze
345+
346+
The tooltip will be squeezed to an adjacent region if the root of the tooltip gets too close to
347+
the edge of the boundary on both sides. For example, if the tooltip is in left or right region,
348+
and neither region has enough space, the tooltip will be squeezed to the top region.
349+
350+
## Alignments
351+
352+
Tool tip's alignments are divided into **root alignment** and **target alignment**, each with
353+
a corresponding **offset** attribute that controls the direction of the alignment and offset
354+
amount.
355+
356+
### Target Alignment
357+
358+
Target alignment refers to the alignment of the pivot relative to the target of the tooltip. See
359+
`targetAlign` and `targetAlignOffset`.
360+
361+
### Root Alignment
362+
363+
Root alignment refers to the alignment of the tooltip's root relative to the pivot. See
364+
`rootAlign` and `rootAlignOffset`.
365+
366+
## Coordinator
367+
368+
_TODO: Add documentation for `FlowTip.Coordinator`_
369+

0 commit comments

Comments
 (0)