Policy 1707 commit to LF
[policy/engine.git] / POLICY-SDK-APP / src / main / webapp / app / policyApp / CSS / bootstrap / js / tooltip.js
1 /* ========================================================================
2  * Bootstrap: tooltip.js v3.3.4
3  * http://getbootstrap.com/javascript/#tooltip
4  * Inspired by the original jQuery.tipsy by Jason Frame
5  * ========================================================================
6  * Copyright 2011-2015 Twitter, Inc.
7  * Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
8  * ======================================================================== */
9
10
11 +function ($) {
12   'use strict';
13
14   // TOOLTIP PUBLIC CLASS DEFINITION
15   // ===============================
16
17   var Tooltip = function (element, options) {
18     this.type       = null
19     this.options    = null
20     this.enabled    = null
21     this.timeout    = null
22     this.hoverState = null
23     this.$element   = null
24
25     this.init('tooltip', element, options)
26   }
27
28   Tooltip.VERSION  = '3.3.4'
29
30   Tooltip.TRANSITION_DURATION = 150
31
32   Tooltip.DEFAULTS = {
33     animation: true,
34     placement: 'top',
35     selector: false,
36     template: '<div class="tooltip" role="tooltip"><div class="tooltip-arrow"></div><div class="tooltip-inner"></div></div>',
37     trigger: 'hover focus',
38     title: '',
39     delay: 0,
40     html: false,
41     container: false,
42     viewport: {
43       selector: 'body',
44       padding: 0
45     }
46   }
47
48   Tooltip.prototype.init = function (type, element, options) {
49     this.enabled   = true
50     this.type      = type
51     this.$element  = $(element)
52     this.options   = this.getOptions(options)
53     this.$viewport = this.options.viewport && $(this.options.viewport.selector || this.options.viewport)
54
55     if (this.$element[0] instanceof document.constructor && !this.options.selector) {
56       throw new Error('`selector` option must be specified when initializing ' + this.type + ' on the window.document object!')
57     }
58
59     var triggers = this.options.trigger.split(' ')
60
61     for (var i = triggers.length; i--;) {
62       var trigger = triggers[i]
63
64       if (trigger == 'click') {
65         this.$element.on('click.' + this.type, this.options.selector, $.proxy(this.toggle, this))
66       } else if (trigger != 'manual') {
67         var eventIn  = trigger == 'hover' ? 'mouseenter' : 'focusin'
68         var eventOut = trigger == 'hover' ? 'mouseleave' : 'focusout'
69
70         this.$element.on(eventIn  + '.' + this.type, this.options.selector, $.proxy(this.enter, this))
71         this.$element.on(eventOut + '.' + this.type, this.options.selector, $.proxy(this.leave, this))
72       }
73     }
74
75     this.options.selector ?
76       (this._options = $.extend({}, this.options, { trigger: 'manual', selector: '' })) :
77       this.fixTitle()
78   }
79
80   Tooltip.prototype.getDefaults = function () {
81     return Tooltip.DEFAULTS
82   }
83
84   Tooltip.prototype.getOptions = function (options) {
85     options = $.extend({}, this.getDefaults(), this.$element.data(), options)
86
87     if (options.delay && typeof options.delay == 'number') {
88       options.delay = {
89         show: options.delay,
90         hide: options.delay
91       }
92     }
93
94     return options
95   }
96
97   Tooltip.prototype.getDelegateOptions = function () {
98     var options  = {}
99     var defaults = this.getDefaults()
100
101     this._options && $.each(this._options, function (key, value) {
102       if (defaults[key] != value) options[key] = value
103     })
104
105     return options
106   }
107
108   Tooltip.prototype.enter = function (obj) {
109     var self = obj instanceof this.constructor ?
110       obj : $(obj.currentTarget).data('bs.' + this.type)
111
112     if (self && self.$tip && self.$tip.is(':visible')) {
113       self.hoverState = 'in'
114       return
115     }
116
117     if (!self) {
118       self = new this.constructor(obj.currentTarget, this.getDelegateOptions())
119       $(obj.currentTarget).data('bs.' + this.type, self)
120     }
121
122     clearTimeout(self.timeout)
123
124     self.hoverState = 'in'
125
126     if (!self.options.delay || !self.options.delay.show) return self.show()
127
128     self.timeout = setTimeout(function () {
129       if (self.hoverState == 'in') self.show()
130     }, self.options.delay.show)
131   }
132
133   Tooltip.prototype.leave = function (obj) {
134     var self = obj instanceof this.constructor ?
135       obj : $(obj.currentTarget).data('bs.' + this.type)
136
137     if (!self) {
138       self = new this.constructor(obj.currentTarget, this.getDelegateOptions())
139       $(obj.currentTarget).data('bs.' + this.type, self)
140     }
141
142     clearTimeout(self.timeout)
143
144     self.hoverState = 'out'
145
146     if (!self.options.delay || !self.options.delay.hide) return self.hide()
147
148     self.timeout = setTimeout(function () {
149       if (self.hoverState == 'out') self.hide()
150     }, self.options.delay.hide)
151   }
152
153   Tooltip.prototype.show = function () {
154     var e = $.Event('show.bs.' + this.type)
155
156     if (this.hasContent() && this.enabled) {
157       this.$element.trigger(e)
158
159       var inDom = $.contains(this.$element[0].ownerDocument.documentElement, this.$element[0])
160       if (e.isDefaultPrevented() || !inDom) return
161       var that = this
162
163       var $tip = this.tip()
164
165       var tipId = this.getUID(this.type)
166
167       this.setContent()
168       $tip.attr('id', tipId)
169       this.$element.attr('aria-describedby', tipId)
170
171       if (this.options.animation) $tip.addClass('fade')
172
173       var placement = typeof this.options.placement == 'function' ?
174         this.options.placement.call(this, $tip[0], this.$element[0]) :
175         this.options.placement
176
177       var autoToken = /\s?auto?\s?/i
178       var autoPlace = autoToken.test(placement)
179       if (autoPlace) placement = placement.replace(autoToken, '') || 'top'
180
181       $tip
182         .detach()
183         .css({ top: 0, left: 0, display: 'block' })
184         .addClass(placement)
185         .data('bs.' + this.type, this)
186
187       this.options.container ? $tip.appendTo(this.options.container) : $tip.insertAfter(this.$element)
188
189       var pos          = this.getPosition()
190       var actualWidth  = $tip[0].offsetWidth
191       var actualHeight = $tip[0].offsetHeight
192
193       if (autoPlace) {
194         var orgPlacement = placement
195         var $container   = this.options.container ? $(this.options.container) : this.$element.parent()
196         var containerDim = this.getPosition($container)
197
198         placement = placement == 'bottom' && pos.bottom + actualHeight > containerDim.bottom ? 'top'    :
199                     placement == 'top'    && pos.top    - actualHeight < containerDim.top    ? 'bottom' :
200                     placement == 'right'  && pos.right  + actualWidth  > containerDim.width  ? 'left'   :
201                     placement == 'left'   && pos.left   - actualWidth  < containerDim.left   ? 'right'  :
202                     placement
203
204         $tip
205           .removeClass(orgPlacement)
206           .addClass(placement)
207       }
208
209       var calculatedOffset = this.getCalculatedOffset(placement, pos, actualWidth, actualHeight)
210
211       this.applyPlacement(calculatedOffset, placement)
212
213       var complete = function () {
214         var prevHoverState = that.hoverState
215         that.$element.trigger('shown.bs.' + that.type)
216         that.hoverState = null
217
218         if (prevHoverState == 'out') that.leave(that)
219       }
220
221       $.support.transition && this.$tip.hasClass('fade') ?
222         $tip
223           .one('bsTransitionEnd', complete)
224           .emulateTransitionEnd(Tooltip.TRANSITION_DURATION) :
225         complete()
226     }
227   }
228
229   Tooltip.prototype.applyPlacement = function (offset, placement) {
230     var $tip   = this.tip()
231     var width  = $tip[0].offsetWidth
232     var height = $tip[0].offsetHeight
233
234     // manually read margins because getBoundingClientRect includes difference
235     var marginTop = parseInt($tip.css('margin-top'), 10)
236     var marginLeft = parseInt($tip.css('margin-left'), 10)
237
238     // we must check for NaN for ie 8/9
239     if (isNaN(marginTop))  marginTop  = 0
240     if (isNaN(marginLeft)) marginLeft = 0
241
242     offset.top  = offset.top  + marginTop
243     offset.left = offset.left + marginLeft
244
245     // $.fn.offset doesn't round pixel values
246     // so we use setOffset directly with our own function B-0
247     $.offset.setOffset($tip[0], $.extend({
248       using: function (props) {
249         $tip.css({
250           top: Math.round(props.top),
251           left: Math.round(props.left)
252         })
253       }
254     }, offset), 0)
255
256     $tip.addClass('in')
257
258     // check to see if placing tip in new offset caused the tip to resize itself
259     var actualWidth  = $tip[0].offsetWidth
260     var actualHeight = $tip[0].offsetHeight
261
262     if (placement == 'top' && actualHeight != height) {
263       offset.top = offset.top + height - actualHeight
264     }
265
266     var delta = this.getViewportAdjustedDelta(placement, offset, actualWidth, actualHeight)
267
268     if (delta.left) offset.left += delta.left
269     else offset.top += delta.top
270
271     var isVertical          = /top|bottom/.test(placement)
272     var arrowDelta          = isVertical ? delta.left * 2 - width + actualWidth : delta.top * 2 - height + actualHeight
273     var arrowOffsetPosition = isVertical ? 'offsetWidth' : 'offsetHeight'
274
275     $tip.offset(offset)
276     this.replaceArrow(arrowDelta, $tip[0][arrowOffsetPosition], isVertical)
277   }
278
279   Tooltip.prototype.replaceArrow = function (delta, dimension, isVertical) {
280     this.arrow()
281       .css(isVertical ? 'left' : 'top', 50 * (1 - delta / dimension) + '%')
282       .css(isVertical ? 'top' : 'left', '')
283   }
284
285   Tooltip.prototype.setContent = function () {
286     var $tip  = this.tip()
287     var title = this.getTitle()
288
289     $tip.find('.tooltip-inner')[this.options.html ? 'html' : 'text'](title)
290     $tip.removeClass('fade in top bottom left right')
291   }
292
293   Tooltip.prototype.hide = function (callback) {
294     var that = this
295     var $tip = $(this.$tip)
296     var e    = $.Event('hide.bs.' + this.type)
297
298     function complete() {
299       if (that.hoverState != 'in') $tip.detach()
300       that.$element
301         .removeAttr('aria-describedby')
302         .trigger('hidden.bs.' + that.type)
303       callback && callback()
304     }
305
306     this.$element.trigger(e)
307
308     if (e.isDefaultPrevented()) return
309
310     $tip.removeClass('in')
311
312     $.support.transition && $tip.hasClass('fade') ?
313       $tip
314         .one('bsTransitionEnd', complete)
315         .emulateTransitionEnd(Tooltip.TRANSITION_DURATION) :
316       complete()
317
318     this.hoverState = null
319
320     return this
321   }
322
323   Tooltip.prototype.fixTitle = function () {
324     var $e = this.$element
325     if ($e.attr('title') || typeof ($e.attr('data-original-title')) != 'string') {
326       $e.attr('data-original-title', $e.attr('title') || '').attr('title', '')
327     }
328   }
329
330   Tooltip.prototype.hasContent = function () {
331     return this.getTitle()
332   }
333
334   Tooltip.prototype.getPosition = function ($element) {
335     $element   = $element || this.$element
336
337     var el     = $element[0]
338     var isBody = el.tagName == 'BODY'
339
340     var elRect    = el.getBoundingClientRect()
341     if (elRect.width == null) {
342       // width and height are missing in IE8, so compute them manually; see https://github.com/twbs/bootstrap/issues/14093
343       elRect = $.extend({}, elRect, { width: elRect.right - elRect.left, height: elRect.bottom - elRect.top })
344     }
345     var elOffset  = isBody ? { top: 0, left: 0 } : $element.offset()
346     var scroll    = { scroll: isBody ? document.documentElement.scrollTop || document.body.scrollTop : $element.scrollTop() }
347     var outerDims = isBody ? { width: $(window).width(), height: $(window).height() } : null
348
349     return $.extend({}, elRect, scroll, outerDims, elOffset)
350   }
351
352   Tooltip.prototype.getCalculatedOffset = function (placement, pos, actualWidth, actualHeight) {
353     return placement == 'bottom' ? { top: pos.top + pos.height,   left: pos.left + pos.width / 2 - actualWidth / 2 } :
354            placement == 'top'    ? { top: pos.top - actualHeight, left: pos.left + pos.width / 2 - actualWidth / 2 } :
355            placement == 'left'   ? { top: pos.top + pos.height / 2 - actualHeight / 2, left: pos.left - actualWidth } :
356         /* placement == 'right' */ { top: pos.top + pos.height / 2 - actualHeight / 2, left: pos.left + pos.width }
357
358   }
359
360   Tooltip.prototype.getViewportAdjustedDelta = function (placement, pos, actualWidth, actualHeight) {
361     var delta = { top: 0, left: 0 }
362     if (!this.$viewport) return delta
363
364     var viewportPadding = this.options.viewport && this.options.viewport.padding || 0
365     var viewportDimensions = this.getPosition(this.$viewport)
366
367     if (/right|left/.test(placement)) {
368       var topEdgeOffset    = pos.top - viewportPadding - viewportDimensions.scroll
369       var bottomEdgeOffset = pos.top + viewportPadding - viewportDimensions.scroll + actualHeight
370       if (topEdgeOffset < viewportDimensions.top) { // top overflow
371         delta.top = viewportDimensions.top - topEdgeOffset
372       } else if (bottomEdgeOffset > viewportDimensions.top + viewportDimensions.height) { // bottom overflow
373         delta.top = viewportDimensions.top + viewportDimensions.height - bottomEdgeOffset
374       }
375     } else {
376       var leftEdgeOffset  = pos.left - viewportPadding
377       var rightEdgeOffset = pos.left + viewportPadding + actualWidth
378       if (leftEdgeOffset < viewportDimensions.left) { // left overflow
379         delta.left = viewportDimensions.left - leftEdgeOffset
380       } else if (rightEdgeOffset > viewportDimensions.width) { // right overflow
381         delta.left = viewportDimensions.left + viewportDimensions.width - rightEdgeOffset
382       }
383     }
384
385     return delta
386   }
387
388   Tooltip.prototype.getTitle = function () {
389     var title
390     var $e = this.$element
391     var o  = this.options
392
393     title = $e.attr('data-original-title')
394       || (typeof o.title == 'function' ? o.title.call($e[0]) :  o.title)
395
396     return title
397   }
398
399   Tooltip.prototype.getUID = function (prefix) {
400     do prefix += ~~(Math.random() * 1000000)
401     while (document.getElementById(prefix))
402     return prefix
403   }
404
405   Tooltip.prototype.tip = function () {
406     return (this.$tip = this.$tip || $(this.options.template))
407   }
408
409   Tooltip.prototype.arrow = function () {
410     return (this.$arrow = this.$arrow || this.tip().find('.tooltip-arrow'))
411   }
412
413   Tooltip.prototype.enable = function () {
414     this.enabled = true
415   }
416
417   Tooltip.prototype.disable = function () {
418     this.enabled = false
419   }
420
421   Tooltip.prototype.toggleEnabled = function () {
422     this.enabled = !this.enabled
423   }
424
425   Tooltip.prototype.toggle = function (e) {
426     var self = this
427     if (e) {
428       self = $(e.currentTarget).data('bs.' + this.type)
429       if (!self) {
430         self = new this.constructor(e.currentTarget, this.getDelegateOptions())
431         $(e.currentTarget).data('bs.' + this.type, self)
432       }
433     }
434
435     self.tip().hasClass('in') ? self.leave(self) : self.enter(self)
436   }
437
438   Tooltip.prototype.destroy = function () {
439     var that = this
440     clearTimeout(this.timeout)
441     this.hide(function () {
442       that.$element.off('.' + that.type).removeData('bs.' + that.type)
443     })
444   }
445
446
447   // TOOLTIP PLUGIN DEFINITION
448   // =========================
449
450   function Plugin(option) {
451     return this.each(function () {
452       var $this   = $(this)
453       var data    = $this.data('bs.tooltip')
454       var options = typeof option == 'object' && option
455
456       if (!data && /destroy|hide/.test(option)) return
457       if (!data) $this.data('bs.tooltip', (data = new Tooltip(this, options)))
458       if (typeof option == 'string') data[option]()
459     })
460   }
461
462   var old = $.fn.tooltip
463
464   $.fn.tooltip             = Plugin
465   $.fn.tooltip.Constructor = Tooltip
466
467
468   // TOOLTIP NO CONFLICT
469   // ===================
470
471   $.fn.tooltip.noConflict = function () {
472     $.fn.tooltip = old
473     return this
474   }
475
476 }(jQuery);