Skip to content
This repository was archived by the owner on Nov 17, 2025. It is now read-only.

Commit ec2d5cd

Browse files
committed
Merge pull request #75 from eekboom/config
Configurable Default Options
2 parents 9b8d4cb + d9c0ffd commit ec2d5cd

File tree

2 files changed

+76
-28
lines changed

2 files changed

+76
-28
lines changed

Readme.md

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ Angular tooltips allows you to use some options via `attribute` data
8181

8282
####Tooltip position
8383
You can set your tooltip to show on `left` or `right` or `top` or `bottom` position
84-
using the `tooltip-side=""` attribute
84+
using the `tooltip-side=""` attribute. Default: `top`
8585
```html
8686
<a href="#" tooltips tooltip-title="tip" tooltip-side="top">Tooltip me</a>
8787
<a href="#" tooltips tooltip-title="tip" tooltip-side="bottom">Tooltip me</a>
@@ -128,8 +128,8 @@ using the `tooltip-view=""` togheter with `tooltip-view-ctrl=""` attribute
128128
```
129129

130130
####Tooltip size
131-
You can set your tooltip size (small || medium || large)
132-
using the `tooltip-size=""` attribute
131+
You can set your tooltip size (small || medium (default) || large)
132+
using the `tooltip-size=""` attribute. Default: `medium`
133133

134134
```html
135135
<a href="#" tooltips tooltip-title="tip" tooltip-size="small">Tooltip me</a>
@@ -138,7 +138,7 @@ using the `tooltip-size=""` attribute
138138
```
139139
####Tooltip speed
140140
You can set the tooltip transition speed ('fast' || 'medium' || 'slow' || int(milliseconds))
141-
using the `tooltip-speed=""` attribute
141+
using the `tooltip-speed=""` attribute. Default: `medium`
142142

143143
```html
144144
<a href="#" tooltips tooltip-speed="fast" tooltip-title="tip">Tooltip fast</a>
@@ -148,22 +148,22 @@ using the `tooltip-speed=""` attribute
148148
```
149149
####Tooltip delay
150150
You can set the tooltip transition delay (ms)
151-
using the `tooltip-delay=""` attribute
151+
using the `tooltip-delay=""` attribute. Default: `0`
152152

153153
```html
154154
<a href="#" tooltips tooltip-delay="800" tooltip-title="tip">Tooltip in 800ms</a>
155155
```
156156
####Tooltip try
157157
If space is not available for tooltip , it will automatically search for a similar alternative position to show. You can set tooltip try (1 || 0)
158-
using the `tooltip-try=""` attribute
158+
using the `tooltip-try=""` attribute. Default: `true`
159159

160160
```html
161161
<a href="#" tooltips tooltip-title="tip" tooltip-try="1">Tooltip me</a>
162162
<a href="#" tooltips tooltip-title="tip" tooltip-try="0">Tooltip me</a>
163163
```
164164
####Tooltip lazy
165-
If you don't want to re-init the tooltip position everytime the tooltip trigger events are fired, you can set tooltip lazy mode (true || false)
166-
using the `tooltip-lazy=""` attribute
165+
If you want to re-init the tooltip position everytime the tooltip trigger events are fired, you can set tooltip lazy mode (true || false)
166+
using the `tooltip-lazy=""` attribute. Default: `true`
167167

168168
```html
169169
<a href="#" tooltips tooltip-lazy="false" tooltip-content="Hi" tooltip-show-trigger="mouseover">
@@ -175,7 +175,8 @@ I will init my position on mouseover only the first time event is fired
175175
```
176176

177177
####Tooltip triggers
178-
You can set your tooltip to show/hide on specific event/events, you can use the `tooltip-show-trigger=""` and the `tooltip-hide-trigger=""` attribute for this scope
178+
You can set your tooltip to show/hide on specific event/events, you can use the `tooltip-show-trigger=""` and the `tooltip-hide-trigger=""` attribute for this scope.
179+
Defaults: `mouseover`, `mouseleave`
179180
```html
180181
<a href="#" tooltips tooltip-title="tip" tooltip-show-trigger="click" tooltip-side="top">Show tooltip only on click</a>
181182
<a href="#" tooltips tooltip-title="tip" tooltip-hide-trigger="click" tooltip-side="bottom">Hide tooltip only on click</a>
@@ -184,15 +185,15 @@ You can set your tooltip to show/hide on specific event/events, you can use the
184185

185186
_**Close button**_
186187

187-
If you want to hide on click, you can configure a close button using text or HTML. This allows your users to click the button inside the tooltip instead of clicking on the original trigger.
188+
If you want to hide on click, you can configure a close button using text or HTML. This allows your users to click the button inside the tooltip instead of clicking on the original trigger. Default: `null` (no close button)
188189
```html
189190
<a href="#" tooltips tooltip-title="tip" tooltip-show-trigger="mouseover click" tooltip-hide-trigger="click" tooltip-close-button="x" tooltip-side="left">Show tooltip on click and mouseover and hide tooltip only on click, with option to click on the X</a>
190191
<a href="#" tooltips tooltip-title="tip" tooltip-show-trigger="mouseover click" tooltip-hide-trigger="click" tooltip-close-button='<button type="button">Close Me!</button>' tooltip-side="left">Show tooltip on click and mouseover and hide tooltip only on click, with option to click on HTML button</a>
191192
```
192193

193194
####Tooltip hide trigger target
194195
You can use ```tooltip-hide-target=""``` to specify if the target of the ```tooltip-hide-trigger=""``` is the element or the tooltip itself.
195-
Values are "element" (default) or "tooltip".
196+
Values are "element" or "tooltip". Default: `element`
196197

197198
```html
198199
<a href="#" tooltips tooltip-title="tooltip" tooltip-hide-trigger="click" tooltip-hide-target="tooltip">
@@ -201,7 +202,7 @@ Click on the tooltip to hide tooltip
201202
```
202203

203204
####Tooltip CSS class
204-
You can set a custom CSS class or a set of, using the `tooltip-class=""` attribute:
205+
You can set a custom CSS class or a set of, using the `tooltip-class=""` attribute. Default: `''` (empty string)
205206
```html
206207
<a href="#" tooltips tooltip-class="tooltip-custom tooltip-for-me" tooltip-title="tip" tooltip-side="top">
207208
I will show a tooltip with class="tooltip-custom tooltip-for-me"
@@ -210,13 +211,34 @@ I will show a tooltip with class="tooltip-custom tooltip-for-me"
210211

211212

212213
####Tooltip scroll
213-
In case your tooltip target element moves (either by scrolling or by use of .js) you can enable tooltip positioning check using `tooltip-scroll="true"` attribute. When this is enabled, tooltip element position is checked every 20ms and tooltip position is updated when necessary. This comes extra handy when using close triggers and you need to keep tooltip besides the target element.
214+
In case your tooltip target element moves (either by scrolling or by use of .js) you can enable tooltip positioning check using `tooltip-scroll="true"` attribute. When this is enabled, tooltip element position is checked every 20ms and tooltip position is updated when necessary. This comes extra handy when using close triggers and you need to keep tooltip besides the target element. Default: `false`
214215
```html
215216
<a href="#" tooltips tooltip-title="I move along with my element" tooltip-scroll="true">
216217
I will follow my element movements
217218
</a>
218219
```
219220

221+
## Global Options
222+
Application wide defaults for most of the options can be set using the `tooltipConfigProvider`:
223+
224+
```js
225+
angular
226+
.module('app')
227+
.config(function(tooltipsConfigProvider) {
228+
tooltipsConfigProvider.options({
229+
lazy: false,
230+
size: large
231+
})
232+
});
233+
```
234+
Options that are not specified are kept unchanged.
235+
Option names are the same as attribute names without the "tooltip-" prefix: `scroll`, `showTrigger`, `hideTrigger`,
236+
`hideTarget`, `side`, `size`, `try`, `class`, `speed`, `delay`, `lazy`, `closeButton`
237+
238+
Of course specific tooltips can still overwrite any default using attributes.
239+
240+
Calling `options` method without arguments returns the complete options object.
241+
220242
## Example
221243

222244
###[Live demo](https://720kb.github.io/angular-tooltips)

src/js/angular-tooltips.js

Lines changed: 41 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,34 @@
44
'use strict';
55

66
angular.module('720kb.tooltips', [])
7-
.directive('tooltips', ['$window', '$compile', '$interpolate', '$interval',
8-
function manageDirective($window, $compile, $interpolate, $interval) {
7+
.provider('tooltipsConfig', function TooltipsConfigProvider() {
8+
var options = {
9+
'scroll': false
10+
, 'showTrigger': 'mouseover'
11+
, 'hideTrigger': 'mouseleave'
12+
, 'hideTarget': 'element'
13+
, 'side': 'top'
14+
, 'size': 'medium'
15+
, 'try': true
16+
, 'class': ''
17+
, 'speed': 'medium'
18+
, 'delay': 0
19+
, 'lazy': true
20+
, 'closeButton': null
21+
};
22+
this.options = function optionsAccessor() {
23+
if (arguments.length === 1) {
24+
angular.extend(options, arguments[0]);
25+
}
26+
return angular.copy(options);
27+
};
28+
29+
this.$get = function getTooltipsConfig() {
30+
return options;
31+
};
32+
})
33+
.directive('tooltips', ['$window', '$compile', '$interpolate', '$interval', 'tooltipsConfig',
34+
function manageDirective($window, $compile, $interpolate, $interval, tooltipsConfig) {
935

1036
var TOOLTIP_SMALL_MARGIN = 8 //px
1137
, TOOLTIP_MEDIUM_MARGIN = 9 //px
@@ -33,22 +59,22 @@
3359
, positionInterval
3460
, oldBoundingRect
3561
, title = attr.tooltipTitle || attr.title || ''
36-
, tooltipScroll = attr.tooltipScroll || false
62+
, tooltipScroll = attr.tooltipScroll || tooltipsConfig.scroll
3763
, content = attr.tooltipContent || ''
3864
, html = attr.tooltipHtml || ''
39-
, showTriggers = attr.tooltipShowTrigger || 'mouseover'
40-
, hideTriggers = attr.tooltipHideTrigger || 'mouseleave'
41-
, hideTarget = typeof attr.tooltipHideTarget !== 'undefined' && attr.tooltipHideTarget !== null ? attr.tooltipHideTarget : 'element'
42-
, originSide = attr.tooltipSide || 'top'
65+
, showTriggers = attr.tooltipShowTrigger || tooltipsConfig.showTrigger
66+
, hideTriggers = attr.tooltipHideTrigger || tooltipsConfig.hideTrigger
67+
, hideTarget = typeof attr.tooltipHideTarget !== 'undefined' && attr.tooltipHideTarget !== null ? attr.tooltipHideTarget : tooltipsConfig.hideTarget
68+
, originSide = attr.tooltipSide || tooltipsConfig.side
4369
, side = originSide
44-
, size = attr.tooltipSize || 'medium'
45-
, tryPosition = typeof attr.tooltipTry !== 'undefined' && attr.tooltipTry !== null ? $scope.$eval(attr.tooltipTry) : true
46-
, className = attr.tooltipClass || ''
47-
, speed = (attr.tooltipSpeed || 'medium').toLowerCase()
48-
, delay = attr.tooltipDelay || 0
49-
, lazyMode = typeof attr.tooltipLazy !== 'undefined' && attr.tooltipLazy !== null ? $scope.$eval(attr.tooltipLazy) : true
50-
, hasCloseButton = typeof attr.tooltipCloseButton !== 'undefined' && attr.tooltipCloseButton !== null
51-
, closeButtonContent = attr.tooltipCloseButton || ''
70+
, size = attr.tooltipSize || tooltipsConfig.size
71+
, tryPosition = typeof attr.tooltipTry !== 'undefined' && attr.tooltipTry !== null ? $scope.$eval(attr.tooltipTry) : tooltipsConfig.try
72+
, className = attr.tooltipClass || tooltipsConfig.class
73+
, speed = (attr.tooltipSpeed || tooltipsConfig.speed).toLowerCase()
74+
, delay = attr.tooltipDelay || tooltipsConfig.delay
75+
, lazyMode = typeof attr.tooltipLazy !== 'undefined' && attr.tooltipLazy !== null ? $scope.$eval(attr.tooltipLazy) : tooltipsConfig.lazy
76+
, closeButtonContent = attr.tooltipCloseButton || tooltipsConfig.closeButton
77+
, hasCloseButton = typeof closeButtonContent !== 'undefined' && closeButtonContent !== null
5278
, htmlTemplate = '<div class="_720kb-tooltip ' + CSS_PREFIX + size + '">';
5379

5480
if (hideTarget !== 'element' && hideTarget !== 'tooltip') {

0 commit comments

Comments
 (0)