Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 
 
 

310 righe
7.8 KiB

  1. /* ==========================================================
  2. * bootstrap-twipsy.js v1.4.0
  3. * http://twitter.github.com/bootstrap/javascript.html#twipsy
  4. * Adapted from the original jQuery.tipsy by Jason Frame
  5. * ==========================================================
  6. * Copyright 2011 Twitter, Inc.
  7. *
  8. * Licensed under the Apache License, Version 2.0 (the "License");
  9. * you may not use this file except in compliance with the License.
  10. * You may obtain a copy of the License at
  11. *
  12. * http://www.apache.org/licenses/LICENSE-2.0
  13. *
  14. * Unless required by applicable law or agreed to in writing, software
  15. * distributed under the License is distributed on an "AS IS" BASIS,
  16. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17. * See the License for the specific language governing permissions and
  18. * limitations under the License.
  19. * ========================================================== */
  20. !function( $ ) {
  21. "use strict"
  22. /* CSS TRANSITION SUPPORT (https://gist.github.com/373874)
  23. * ======================================================= */
  24. var transitionEnd
  25. $(document).ready(function () {
  26. $.support.transition = (function () {
  27. var thisBody = document.body || document.documentElement
  28. , thisStyle = thisBody.style
  29. , support = thisStyle.transition !== undefined || thisStyle.WebkitTransition !== undefined || thisStyle.MozTransition !== undefined || thisStyle.MsTransition !== undefined || thisStyle.OTransition !== undefined
  30. return support
  31. })()
  32. // set CSS transition event type
  33. if ( $.support.transition ) {
  34. transitionEnd = "TransitionEnd"
  35. if ( $.browser.webkit ) {
  36. transitionEnd = "webkitTransitionEnd"
  37. } else if ( $.browser.mozilla ) {
  38. transitionEnd = "transitionend"
  39. } else if ( $.browser.opera ) {
  40. transitionEnd = "oTransitionEnd"
  41. }
  42. }
  43. })
  44. /* TWIPSY PUBLIC CLASS DEFINITION
  45. * ============================== */
  46. var Twipsy = function ( element, options ) {
  47. this.$element = $(element)
  48. this.options = options
  49. this.enabled = true
  50. this.fixTitle()
  51. }
  52. Twipsy.prototype = {
  53. show: function() {
  54. var pos
  55. , actualWidth
  56. , actualHeight
  57. , placement
  58. , $tip
  59. , tp
  60. if (this.hasContent() && this.enabled) {
  61. $tip = this.tip()
  62. this.setContent()
  63. if (this.options.animate) {
  64. $tip.addClass('fade')
  65. }
  66. $tip
  67. .remove()
  68. .css({ top: 0, left: 0, display: 'block' })
  69. .prependTo(document.body)
  70. pos = $.extend({}, this.$element.offset(), {
  71. width: this.$element[0].offsetWidth
  72. , height: this.$element[0].offsetHeight
  73. })
  74. actualWidth = $tip[0].offsetWidth
  75. actualHeight = $tip[0].offsetHeight
  76. placement = maybeCall(this.options.placement, this, [ $tip[0], this.$element[0] ])
  77. switch (placement) {
  78. case 'below':
  79. tp = {top: pos.top + pos.height + this.options.offset, left: pos.left + pos.width / 2 - actualWidth / 2}
  80. break
  81. case 'above':
  82. tp = {top: pos.top - actualHeight - this.options.offset, left: pos.left + pos.width / 2 - actualWidth / 2}
  83. break
  84. case 'left':
  85. tp = {top: pos.top + pos.height / 2 - actualHeight / 2, left: pos.left - actualWidth - this.options.offset}
  86. break
  87. case 'right':
  88. tp = {top: pos.top + pos.height / 2 - actualHeight / 2, left: pos.left + pos.width + this.options.offset}
  89. break
  90. }
  91. $tip
  92. .css(tp)
  93. .addClass(placement)
  94. .addClass('in')
  95. }
  96. }
  97. , setContent: function () {
  98. var $tip = this.tip()
  99. $tip.find('.twipsy-inner')[this.options.html ? 'html' : 'text'](this.getTitle())
  100. $tip[0].className = 'twipsy'
  101. }
  102. , hide: function() {
  103. var that = this
  104. , $tip = this.tip()
  105. $tip.removeClass('in')
  106. function removeElement () {
  107. $tip.remove()
  108. }
  109. $.support.transition && this.$tip.hasClass('fade') ?
  110. $tip.bind(transitionEnd, removeElement) :
  111. removeElement()
  112. }
  113. , fixTitle: function() {
  114. var $e = this.$element
  115. if ($e.attr('title') || typeof($e.attr('data-original-title')) != 'string') {
  116. $e.attr('data-original-title', $e.attr('title') || '').removeAttr('title')
  117. }
  118. }
  119. , hasContent: function () {
  120. return this.getTitle()
  121. }
  122. , getTitle: function() {
  123. var title
  124. , $e = this.$element
  125. , o = this.options
  126. this.fixTitle()
  127. if (typeof o.title == 'string') {
  128. title = $e.attr(o.title == 'title' ? 'data-original-title' : o.title)
  129. } else if (typeof o.title == 'function') {
  130. title = o.title.call($e[0])
  131. }
  132. title = ('' + title).replace(/(^\s*|\s*$)/, "")
  133. return title || o.fallback
  134. }
  135. , tip: function() {
  136. if (!this.$tip) {
  137. this.$tip = $('<div class="twipsy" />').html(this.options.template)
  138. }
  139. return this.$tip
  140. }
  141. , validate: function() {
  142. if (!this.$element[0].parentNode) {
  143. this.hide()
  144. this.$element = null
  145. this.options = null
  146. }
  147. }
  148. , enable: function() {
  149. this.enabled = true
  150. }
  151. , disable: function() {
  152. this.enabled = false
  153. }
  154. , toggleEnabled: function() {
  155. this.enabled = !this.enabled
  156. }
  157. }
  158. /* TWIPSY PRIVATE METHODS
  159. * ====================== */
  160. function maybeCall ( thing, ctx, args ) {
  161. return typeof thing == 'function' ? thing.apply(ctx, args) : thing
  162. }
  163. /* TWIPSY PLUGIN DEFINITION
  164. * ======================== */
  165. $.fn.twipsy = function (options) {
  166. $.fn.twipsy.initWith.call(this, options, Twipsy, 'twipsy')
  167. return this
  168. }
  169. $.fn.twipsy.initWith = function (options, Constructor, name) {
  170. var twipsy
  171. , binder
  172. , eventIn
  173. , eventOut
  174. if (options === true) {
  175. return this.data(name)
  176. } else if (typeof options == 'string') {
  177. twipsy = this.data(name)
  178. if (twipsy) {
  179. twipsy[options]()
  180. }
  181. return this
  182. }
  183. options = $.extend({}, $.fn[name].defaults, options)
  184. function get(ele) {
  185. var twipsy = $.data(ele, name)
  186. if (!twipsy) {
  187. twipsy = new Constructor(ele, $.fn.twipsy.elementOptions(ele, options))
  188. $.data(ele, name, twipsy)
  189. }
  190. return twipsy
  191. }
  192. function enter() {
  193. var twipsy = get(this)
  194. twipsy.hoverState = 'in'
  195. if (options.delayIn == 0) {
  196. twipsy.show()
  197. } else {
  198. twipsy.fixTitle()
  199. setTimeout(function() {
  200. if (twipsy.hoverState == 'in') {
  201. twipsy.show()
  202. }
  203. }, options.delayIn)
  204. }
  205. }
  206. function leave() {
  207. var twipsy = get(this)
  208. twipsy.hoverState = 'out'
  209. if (options.delayOut == 0) {
  210. twipsy.hide()
  211. } else {
  212. setTimeout(function() {
  213. if (twipsy.hoverState == 'out') {
  214. twipsy.hide()
  215. }
  216. }, options.delayOut)
  217. }
  218. }
  219. if (!options.live) {
  220. this.each(function() {
  221. get(this)
  222. })
  223. }
  224. if (options.trigger != 'manual') {
  225. binder = options.live ? 'live' : 'bind'
  226. eventIn = options.trigger == 'hover' ? 'mouseenter' : 'focus'
  227. eventOut = options.trigger == 'hover' ? 'mouseleave' : 'blur'
  228. this[binder](eventIn, enter)[binder](eventOut, leave)
  229. }
  230. return this
  231. }
  232. $.fn.twipsy.Twipsy = Twipsy
  233. $.fn.twipsy.defaults = {
  234. animate: true
  235. , delayIn: 0
  236. , delayOut: 0
  237. , fallback: ''
  238. , placement: 'above'
  239. , html: false
  240. , live: false
  241. , offset: 0
  242. , title: 'title'
  243. , trigger: 'hover'
  244. , template: '<div class="twipsy-arrow"></div><div class="twipsy-inner"></div>'
  245. }
  246. $.fn.twipsy.elementOptions = function(ele, options) {
  247. return $.extend({}, options, $(ele).data())
  248. }
  249. }( window.jQuery || window.ender );