Advertisement
smily03

jQueryUI mods to make tooltip stay open when hovered over

Mar 12th, 2013
1,059
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Tooltip setup function (caller for this function is inside document.ready):
  2.  
  3.  
  4.       function setupTooltips() {
  5.         $(function() {
  6.           $(".ui-tooltip").live("mouseleave",function(){
  7.             setTimeout("$(document).tooltip('enable')",500);$(document).tooltip("disable");
  8.           });
  9.  
  10.           $(document).tooltip({
  11.             position: {
  12.               my: "center bottom",
  13.               at: "center top+5",
  14.               collision: "fit custom",
  15.               using: function( position, feedback ) {
  16.                 $( this ).css( position );
  17.                 $( "<div>" )
  18.                 .addClass( "arrow" )
  19.                 .attr("id","arrow-"+$(this).attr("id"))
  20.                 .addClass( feedback.vertical )
  21.                 .html("<div class='arrowdiv' id='arrowdiv-" + $(this).attr("id") + "'></div>")
  22.                 .appendTo( "#content-" + $(this).attr("id") );
  23.               }
  24.             },
  25.             show: { delay: 300 },
  26.             hide: { delay: 300 },
  27.             content: function() {
  28.               var element = $(this);
  29.               var title = element.attr("title");
  30.               var descr = element.attr("description");
  31.               if (title.indexOf("/") > -1) {
  32.                  var data = $.ajax({
  33.                               url: title,
  34.                               async: false,
  35.                               success: function(data) {
  36.                                 return data;
  37.                               }
  38.                             });
  39.                 return "<div class='ttipContent'><span class='noticeTitle'>" + descr + "</span><br>" + data.responseText + "</div>";
  40.               } else {
  41.                 var tooltip = element.attr("title");
  42.                 if ((tooltip != "") && (tooltip != null)) {
  43.                   return("<div class='ttipContent'>" + tooltip + "</div>");
  44.                 }
  45.               }
  46.             }
  47.           });
  48.         });
  49.       }
  50.  
  51.  
  52. Changes to JqueryUI code itself:
  53.  
  54.  
  55.  
  56. Changes to the position function to add a new "custom" collision handler
  57. ...
  58. //NEW_CUSTOM through next NEW_CUSTOM
  59.         custom: {
  60.           left: function(position, data) {
  61.             var initPos = position.left;
  62.             $.ui.position.flip.left(position, data);
  63.           },
  64.           top: function(position, data) {
  65.             var initPos = position.top;
  66.             $.ui.position.flip.top(position, data);
  67.             if (initPos != position.top) {
  68.               setTimeout("$('#arrow-" + data['elem'].attr('id') + "').addClass('tooltipFlip')",50);
  69.               setTimeout("$('#arrowdiv-" + data['elem'].attr('id') + "').addClass('tooltipFlip')",50);
  70.             }
  71.           }
  72.         }
  73. // NEW_CUSTOM
  74. };
  75. ...
  76.  
  77.  
  78. Changes to the tooltip mouseover code, so that it will determine if the element that spawned the tooltip has a particular class. If it does, it overrides the "events.mouseleave" function, looking at what element the mouse goes to when it leaves the element that spawned the tooltip.
  79.  
  80. ...
  81.                 if ( !event || event.type === "mouseover" ) {
  82. //NEW_CUSTOM through next NEW_CUSTOM, except for noted line of code (which was the original)
  83.                         var myElem = target.attr("id");
  84.                         if ($("#"+myElem).hasClass("dynamicTooltip")) { //Tooltip has dynamic content
  85.  
  86.                                 events.mouseleave = function( event ) {
  87.                                         var myClass = "";
  88.                                         if (event.toElement) {
  89.                                                 var elem = event.toElement;
  90.                                                 myClass = elem["className"];
  91.                                         } else if (event.relatedTarget) {
  92.                                                 var elem = event.relatedTarget;
  93.                                                 myClass = elem["className"];
  94.                                         }
  95.                                         myClass = $.trim(myClass);
  96.                                         if (myClass != "ui-tooltip") {
  97.                                                 var that = this,
  98.                                                 target = $( event ? event.currentTarget : this.element ),
  99.                                                 tooltip = this._find( target );
  100.  
  101.                                                 if ( this.closing ) {
  102.                                                         return;
  103.                                                 }
  104.  
  105.                                                 if ( target.data( "ui-tooltip-title" ) ) {
  106.                                                         target.attr( "title", target.data( "ui-tooltip-title" ) );
  107.                                                 }
  108.  
  109.                                                 removeDescribedBy( target );
  110.  
  111.                                                 tooltip.stop( true );
  112.                                                 this._hide( tooltip, this.options.hide, function() {
  113.                                                         that._removeTooltip( $( this ) );
  114.                                                 });
  115.  
  116.                                                 target.removeData( "tooltip-open" );
  117.                                                 this._off( target, "mouseleave focusout keyup" );
  118.  
  119.                                                 if ( target[0] !== this.element[0] ) {
  120.                                                         this._off( target, "remove" );
  121.                                                 }
  122.                                                 this._off( this.document, "mousemove" );
  123.  
  124.                                                 if ( event && event.type === "mouseleave" ) {
  125.                                                         $.each( this.parents, function( id, parent ) {
  126.                                                                 parent.element.title = parent.title;
  127.                                                                 delete that.parents[ id ];
  128.                                                         });
  129.                                                 }
  130.                                                 this.closing = true;
  131.                                                 this._trigger( "close", event, { tooltip: tooltip } );
  132.                                                 this.closing = false;
  133.                                         }
  134.                                 }
  135.                         } else { //Just a basic tooltip
  136.                                 events.mouseleave = "close"; //The original jQuery code
  137.                         }
  138. //End NEW_CUSTOM
  139.                 }
  140. ...
  141.  
  142.  
  143.  
  144. Changes to the tooltip creation code, adding a wrapper div around the tooltip itself. This is because we have a "beak" on our tooltip that points to the element being hovered over. If we don't have this wrapper div, trying to figure out if the mouse went to the tooltip becomes much more difficult since it could go to the "beak," or to the container of the element being hovered over (if you go from the element to the toolip directly without moving over the "beak" first.)
  145.  
  146. ...
  147.        _tooltip: function( element ) {
  148. //NEW_CUSTOM through next NEW_CUSTOM
  149.  
  150.                var id = "ui-tooltip-" + increments++,
  151.                        tooltip = $( "<div>" )
  152.                                .attr({
  153.                                        id: id,
  154.                                        role: "tooltip"
  155.                                })
  156.                                .addClass( "ui-tooltip " +
  157.                                      ( this.options.tooltipClass || "" ) ),
  158.                        shell = $("<div>")
  159.                                .attr({
  160.                                        id: "shell-" + id
  161.                                })
  162.                                .addClass( "ui-tooltip-shell ui-widget ui-corner-all ui-widget-content " +
  163.                                      ( this.options.tooltipClass || "" ) )
  164.                                .appendTo(tooltip);
  165.  
  166.                        $( "<div>" )
  167.                                .addClass( "ui-tooltip-content" )
  168.                                .attr({
  169.                                        id: "content-" + id
  170.                                })
  171.                                .appendTo( shell );
  172. //NEW_CUSTOM
  173.  
  174. //jQuery Original bits
  175. //              var id = "ui-tooltip-" + increments++,
  176. //                      tooltip = $( "<div>" )
  177. //                              .attr({
  178. //                                      id: id,
  179. //                                      role: "tooltip"
  180. //                              })
  181. //                              .addClass( "ui-tooltip ui-widget ui-corner-all ui-widget-content " +
  182. //                                      ( this.options.tooltipClass || "" ) );
  183. //
  184. //              $( "<div>" )
  185. //                      .addClass( "ui-tooltip-content" )
  186. //                      .appendTo( tooltip );
  187.  
  188.                tooltip.appendTo( this.document[0].body );
  189.                if ( $.fn.bgiframe ) {
  190.                        tooltip.bgiframe();
  191.                }
  192.                this.tooltips[ id ] = element;
  193.                return tooltip;
  194.        },
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement