sonata_jqueryui_js_jquery.ui.tooltip_11.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. /*!
  2. * jQuery UI Tooltip 1.10.4
  3. * http://jqueryui.com
  4. *
  5. * Copyright 2014 jQuery Foundation and other contributors
  6. * Released under the MIT license.
  7. * http://jquery.org/license
  8. *
  9. * http://api.jqueryui.com/tooltip/
  10. *
  11. * Depends:
  12. * jquery.ui.core.js
  13. * jquery.ui.widget.js
  14. * jquery.ui.position.js
  15. */
  16. (function( $ ) {
  17. var increments = 0;
  18. function addDescribedBy( elem, id ) {
  19. var describedby = (elem.attr( "aria-describedby" ) || "").split( /\s+/ );
  20. describedby.push( id );
  21. elem
  22. .data( "ui-tooltip-id", id )
  23. .attr( "aria-describedby", $.trim( describedby.join( " " ) ) );
  24. }
  25. function removeDescribedBy( elem ) {
  26. var id = elem.data( "ui-tooltip-id" ),
  27. describedby = (elem.attr( "aria-describedby" ) || "").split( /\s+/ ),
  28. index = $.inArray( id, describedby );
  29. if ( index !== -1 ) {
  30. describedby.splice( index, 1 );
  31. }
  32. elem.removeData( "ui-tooltip-id" );
  33. describedby = $.trim( describedby.join( " " ) );
  34. if ( describedby ) {
  35. elem.attr( "aria-describedby", describedby );
  36. } else {
  37. elem.removeAttr( "aria-describedby" );
  38. }
  39. }
  40. $.widget( "ui.tooltip", {
  41. version: "1.10.4",
  42. options: {
  43. content: function() {
  44. // support: IE<9, Opera in jQuery <1.7
  45. // .text() can't accept undefined, so coerce to a string
  46. var title = $( this ).attr( "title" ) || "";
  47. // Escape title, since we're going from an attribute to raw HTML
  48. return $( "<a>" ).text( title ).html();
  49. },
  50. hide: true,
  51. // Disabled elements have inconsistent behavior across browsers (#8661)
  52. items: "[title]:not([disabled])",
  53. position: {
  54. my: "left top+15",
  55. at: "left bottom",
  56. collision: "flipfit flip"
  57. },
  58. show: true,
  59. tooltipClass: null,
  60. track: false,
  61. // callbacks
  62. close: null,
  63. open: null
  64. },
  65. _create: function() {
  66. this._on({
  67. mouseover: "open",
  68. focusin: "open"
  69. });
  70. // IDs of generated tooltips, needed for destroy
  71. this.tooltips = {};
  72. // IDs of parent tooltips where we removed the title attribute
  73. this.parents = {};
  74. if ( this.options.disabled ) {
  75. this._disable();
  76. }
  77. },
  78. _setOption: function( key, value ) {
  79. var that = this;
  80. if ( key === "disabled" ) {
  81. this[ value ? "_disable" : "_enable" ]();
  82. this.options[ key ] = value;
  83. // disable element style changes
  84. return;
  85. }
  86. this._super( key, value );
  87. if ( key === "content" ) {
  88. $.each( this.tooltips, function( id, element ) {
  89. that._updateContent( element );
  90. });
  91. }
  92. },
  93. _disable: function() {
  94. var that = this;
  95. // close open tooltips
  96. $.each( this.tooltips, function( id, element ) {
  97. var event = $.Event( "blur" );
  98. event.target = event.currentTarget = element[0];
  99. that.close( event, true );
  100. });
  101. // remove title attributes to prevent native tooltips
  102. this.element.find( this.options.items ).addBack().each(function() {
  103. var element = $( this );
  104. if ( element.is( "[title]" ) ) {
  105. element
  106. .data( "ui-tooltip-title", element.attr( "title" ) )
  107. .attr( "title", "" );
  108. }
  109. });
  110. },
  111. _enable: function() {
  112. // restore title attributes
  113. this.element.find( this.options.items ).addBack().each(function() {
  114. var element = $( this );
  115. if ( element.data( "ui-tooltip-title" ) ) {
  116. element.attr( "title", element.data( "ui-tooltip-title" ) );
  117. }
  118. });
  119. },
  120. open: function( event ) {
  121. var that = this,
  122. target = $( event ? event.target : this.element )
  123. // we need closest here due to mouseover bubbling,
  124. // but always pointing at the same event target
  125. .closest( this.options.items );
  126. // No element to show a tooltip for or the tooltip is already open
  127. if ( !target.length || target.data( "ui-tooltip-id" ) ) {
  128. return;
  129. }
  130. if ( target.attr( "title" ) ) {
  131. target.data( "ui-tooltip-title", target.attr( "title" ) );
  132. }
  133. target.data( "ui-tooltip-open", true );
  134. // kill parent tooltips, custom or native, for hover
  135. if ( event && event.type === "mouseover" ) {
  136. target.parents().each(function() {
  137. var parent = $( this ),
  138. blurEvent;
  139. if ( parent.data( "ui-tooltip-open" ) ) {
  140. blurEvent = $.Event( "blur" );
  141. blurEvent.target = blurEvent.currentTarget = this;
  142. that.close( blurEvent, true );
  143. }
  144. if ( parent.attr( "title" ) ) {
  145. parent.uniqueId();
  146. that.parents[ this.id ] = {
  147. element: this,
  148. title: parent.attr( "title" )
  149. };
  150. parent.attr( "title", "" );
  151. }
  152. });
  153. }
  154. this._updateContent( target, event );
  155. },
  156. _updateContent: function( target, event ) {
  157. var content,
  158. contentOption = this.options.content,
  159. that = this,
  160. eventType = event ? event.type : null;
  161. if ( typeof contentOption === "string" ) {
  162. return this._open( event, target, contentOption );
  163. }
  164. content = contentOption.call( target[0], function( response ) {
  165. // ignore async response if tooltip was closed already
  166. if ( !target.data( "ui-tooltip-open" ) ) {
  167. return;
  168. }
  169. // IE may instantly serve a cached response for ajax requests
  170. // delay this call to _open so the other call to _open runs first
  171. that._delay(function() {
  172. // jQuery creates a special event for focusin when it doesn't
  173. // exist natively. To improve performance, the native event
  174. // object is reused and the type is changed. Therefore, we can't
  175. // rely on the type being correct after the event finished
  176. // bubbling, so we set it back to the previous value. (#8740)
  177. if ( event ) {
  178. event.type = eventType;
  179. }
  180. this._open( event, target, response );
  181. });
  182. });
  183. if ( content ) {
  184. this._open( event, target, content );
  185. }
  186. },
  187. _open: function( event, target, content ) {
  188. var tooltip, events, delayedShow,
  189. positionOption = $.extend( {}, this.options.position );
  190. if ( !content ) {
  191. return;
  192. }
  193. // Content can be updated multiple times. If the tooltip already
  194. // exists, then just update the content and bail.
  195. tooltip = this._find( target );
  196. if ( tooltip.length ) {
  197. tooltip.find( ".ui-tooltip-content" ).html( content );
  198. return;
  199. }
  200. // if we have a title, clear it to prevent the native tooltip
  201. // we have to check first to avoid defining a title if none exists
  202. // (we don't want to cause an element to start matching [title])
  203. //
  204. // We use removeAttr only for key events, to allow IE to export the correct
  205. // accessible attributes. For mouse events, set to empty string to avoid
  206. // native tooltip showing up (happens only when removing inside mouseover).
  207. if ( target.is( "[title]" ) ) {
  208. if ( event && event.type === "mouseover" ) {
  209. target.attr( "title", "" );
  210. } else {
  211. target.removeAttr( "title" );
  212. }
  213. }
  214. tooltip = this._tooltip( target );
  215. addDescribedBy( target, tooltip.attr( "id" ) );
  216. tooltip.find( ".ui-tooltip-content" ).html( content );
  217. function position( event ) {
  218. positionOption.of = event;
  219. if ( tooltip.is( ":hidden" ) ) {
  220. return;
  221. }
  222. tooltip.position( positionOption );
  223. }
  224. if ( this.options.track && event && /^mouse/.test( event.type ) ) {
  225. this._on( this.document, {
  226. mousemove: position
  227. });
  228. // trigger once to override element-relative positioning
  229. position( event );
  230. } else {
  231. tooltip.position( $.extend({
  232. of: target
  233. }, this.options.position ) );
  234. }
  235. tooltip.hide();
  236. this._show( tooltip, this.options.show );
  237. // Handle tracking tooltips that are shown with a delay (#8644). As soon
  238. // as the tooltip is visible, position the tooltip using the most recent
  239. // event.
  240. if ( this.options.show && this.options.show.delay ) {
  241. delayedShow = this.delayedShow = setInterval(function() {
  242. if ( tooltip.is( ":visible" ) ) {
  243. position( positionOption.of );
  244. clearInterval( delayedShow );
  245. }
  246. }, $.fx.interval );
  247. }
  248. this._trigger( "open", event, { tooltip: tooltip } );
  249. events = {
  250. keyup: function( event ) {
  251. if ( event.keyCode === $.ui.keyCode.ESCAPE ) {
  252. var fakeEvent = $.Event(event);
  253. fakeEvent.currentTarget = target[0];
  254. this.close( fakeEvent, true );
  255. }
  256. },
  257. remove: function() {
  258. this._removeTooltip( tooltip );
  259. }
  260. };
  261. if ( !event || event.type === "mouseover" ) {
  262. events.mouseleave = "close";
  263. }
  264. if ( !event || event.type === "focusin" ) {
  265. events.focusout = "close";
  266. }
  267. this._on( true, target, events );
  268. },
  269. close: function( event ) {
  270. var that = this,
  271. target = $( event ? event.currentTarget : this.element ),
  272. tooltip = this._find( target );
  273. // disabling closes the tooltip, so we need to track when we're closing
  274. // to avoid an infinite loop in case the tooltip becomes disabled on close
  275. if ( this.closing ) {
  276. return;
  277. }
  278. // Clear the interval for delayed tracking tooltips
  279. clearInterval( this.delayedShow );
  280. // only set title if we had one before (see comment in _open())
  281. if ( target.data( "ui-tooltip-title" ) ) {
  282. target.attr( "title", target.data( "ui-tooltip-title" ) );
  283. }
  284. removeDescribedBy( target );
  285. tooltip.stop( true );
  286. this._hide( tooltip, this.options.hide, function() {
  287. that._removeTooltip( $( this ) );
  288. });
  289. target.removeData( "ui-tooltip-open" );
  290. this._off( target, "mouseleave focusout keyup" );
  291. // Remove 'remove' binding only on delegated targets
  292. if ( target[0] !== this.element[0] ) {
  293. this._off( target, "remove" );
  294. }
  295. this._off( this.document, "mousemove" );
  296. if ( event && event.type === "mouseleave" ) {
  297. $.each( this.parents, function( id, parent ) {
  298. $( parent.element ).attr( "title", parent.title );
  299. delete that.parents[ id ];
  300. });
  301. }
  302. this.closing = true;
  303. this._trigger( "close", event, { tooltip: tooltip } );
  304. this.closing = false;
  305. },
  306. _tooltip: function( element ) {
  307. var id = "ui-tooltip-" + increments++,
  308. tooltip = $( "<div>" )
  309. .attr({
  310. id: id,
  311. role: "tooltip"
  312. })
  313. .addClass( "ui-tooltip ui-widget ui-corner-all ui-widget-content " +
  314. ( this.options.tooltipClass || "" ) );
  315. $( "<div>" )
  316. .addClass( "ui-tooltip-content" )
  317. .appendTo( tooltip );
  318. tooltip.appendTo( this.document[0].body );
  319. this.tooltips[ id ] = element;
  320. return tooltip;
  321. },
  322. _find: function( target ) {
  323. var id = target.data( "ui-tooltip-id" );
  324. return id ? $( "#" + id ) : $();
  325. },
  326. _removeTooltip: function( tooltip ) {
  327. tooltip.remove();
  328. delete this.tooltips[ tooltip.attr( "id" ) ];
  329. },
  330. _destroy: function() {
  331. var that = this;
  332. // close open tooltips
  333. $.each( this.tooltips, function( id, element ) {
  334. // Delegate to close method to handle common cleanup
  335. var event = $.Event( "blur" );
  336. event.target = event.currentTarget = element[0];
  337. that.close( event, true );
  338. // Remove immediately; destroying an open tooltip doesn't use the
  339. // hide animation
  340. $( "#" + id ).remove();
  341. // Restore the title
  342. if ( element.data( "ui-tooltip-title" ) ) {
  343. element.attr( "title", element.data( "ui-tooltip-title" ) );
  344. element.removeData( "ui-tooltip-title" );
  345. }
  346. });
  347. }
  348. });
  349. }( jQuery ) );