forked from andresgutgon/bootstrap-tooltip-extension
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bootstrap-tooltip-extension.js
177 lines (154 loc) · 5.43 KB
/
bootstrap-tooltip-extension.js
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
/* ===========================================================
* bootstrap-tooltip-extension.js v0.0.2
* https://github.com/andresgutgon/bootstrap-tooltip-extension
* ===========================================================
*
* This file extends bootstrap-tooltip.js that add
* More tooltip positions 'bottom-left', 'bottom-right', 'top-left' and 'top-right'
*
* =========================================================== */
(function ($) {
var old
, TooltipExtension = function (element, options) {
this.init('tooltip', element, options);
};
TooltipExtension.prototype = $.extend({}, $.fn.tooltip.Constructor.prototype, {
constructor: TooltipExtension
, show: function () {
var $tip
, pos
, actualWidth
, actualHeight
, placement
, moveArrow
, tp
, e = $.Event('show');
if (this.hasContent() && this.enabled) {
this.$element.trigger(e);
if (e.isDefaultPrevented()) { return; }
$tip = this.tip();
this.setContent();
if (this.options.animation) {
$tip.addClass('fade');
}
placement = typeof this.options.placement === 'function' ?
this.options.placement.call(this, $tip[0], this.$element[0]) :
this.options.placement;
$tip
.detach()
.css({ top: 0, left: 0, display: 'block' });
if (this.options.container) {
$tip.appendTo(this.options.container);
} else {
$tip.insertAfter(this.$element);
}
pos = this.getPosition();
actualWidth = $tip[0].offsetWidth;
actualHeight = $tip[0].offsetHeight;
switch (placement) {
case 'bottom':
tp = {top: pos.top + pos.height, left: pos.left + pos.width / 2 - actualWidth / 2};
break;
case 'top':
tp = {top: pos.top - actualHeight, left: pos.left + pos.width / 2 - actualWidth / 2};
break;
case 'left':
tp = {top: pos.top + pos.height / 2 - actualHeight / 2, left: pos.left - actualWidth};
break;
case 'right':
tp = {top: pos.top + pos.height / 2 - actualHeight / 2, left: pos.left + pos.width};
break;
// Extra positions. This are not part of bootstrap
// Extrange:
// I've to make top position 10px smaller in 'top-left' and 'top-right'
// And 10px bigger in 'bottom-left' and 'bottom-right'
// This should behave like 'top' and 'bottom'. But they don't.
case 'bottom-left':
tp = {top: pos.top + pos.height + 10, left: pos.left};
if (this.$element.outerWidth() <= 18) { // if button is small move tooltip left
tp.left -= 4;
}
break;
case 'bottom-right':
tp = {top: pos.top + pos.height + 10, left: pos.left + pos.width - actualWidth};
if (this.$element.outerWidth() <= 18) { // if button is small move tooltip left
tp.left += 4;
}
break;
case 'top-left':
tp = {top: pos.top - actualHeight - 10, left: pos.left };
if (this.$element.outerWidth() <= 18) { // if button is small move tooltip left
tp.left -= 4;
}
break;
case 'top-right':
tp = {top: pos.top - actualHeight - 10, left: pos.left + pos.width - actualWidth};
if (this.$element.outerWidth() <= 18) { // if button is small move tooltip left
tp.left += 4;
}
break;
}
this.applyPlacement(tp, placement);
if (this.options.moveArrow) {
this.moveArrow();
}
this.$element.trigger('shown');
}
}
/**
* Calculate arrow position relative to button width.
*/
, moveArrow: function () {
var placement = this.options.placement
, button = this.$element
, template = $(this.options.template)
, $arrow = this.tip().find(".tooltip-arrow, .arrow")
, arrow_width = parseInt($arrow.css("width"), 10) // This is needed we get here Ex.: '18px'
, new_arrow_position = (button.outerWidth() / 2) - (arrow_width / 2);
switch (placement) {
case 'bottom-left':
$arrow.css("left", new_arrow_position);
break;
case 'bottom-right':
$arrow.css("right", new_arrow_position);
break;
case 'top-left':
$arrow.css("left", new_arrow_position);
break;
case 'top-right':
$arrow.css("right", new_arrow_position);
break;
}
}
});
/* TOOLTIP EXTRA PLUGIN DEFINITION
* ========================= */
old = $.fn.tooltip;
$.fn.tooltip = function (option) {
return this.each(function () {
var $this = $(this)
, data = $this.data('tooltip')
, options = typeof option === 'object' && option;
if (!data) { $this.data('tooltip', (data = new TooltipExtension(this, options))); }
if (typeof option === 'string') { data[option](); }
});
};
$.fn.tooltip.Constructor = TooltipExtension;
$.fn.tooltip.defaults = {
animation: true
, placement: 'top'
, selector: false
, template: '<div class="tooltip"><div class="tooltip-arrow"></div><div class="tooltip-inner"></div></div>'
, trigger: 'hover focus'
, title: ''
, delay: 0
, html: false
, container: false
};
/* TOOLTIP EXTRA NO CONFLICT
* =================== */
$.fn.tooltip.noConflict = function () {
$.fn.tooltip = old;
return this;
};
})(window.jQuery);