jquery.tipTip.js
200 lines
| 1 | /* |
| 2 | * TipTip |
| 3 | * Copyright 2010 Drew Wilson |
| 4 | * www.drewwilson.com |
| 5 | * code.drewwilson.com/entry/tiptip-jquery-plugin |
| 6 | * |
| 7 | * Version 1.3.1 - Updated: Mar. 30, 2023 |
| 8 | * |
| 9 | * This is a custom version of TipTip. This file has been locally modified for specific requirements. |
| 10 | * Since the original version is no longer maintained, the changes were not submitted back to the original author. |
| 11 | * |
| 12 | * This Plug-In will create a custom tooltip to replace the default |
| 13 | * browser tooltip. It is extremely lightweight and very smart in |
| 14 | * that it detects the edges of the browser window and will make sure |
| 15 | * the tooltip stays within the current window size. As a result the |
| 16 | * tooltip will adjust itself to be displayed above, below, to the left |
| 17 | * or to the right depending on what is necessary to stay within the |
| 18 | * browser window. It is completely customizable as well via CSS. |
| 19 | * |
| 20 | * This TipTip jQuery plug-in is dual licensed under the MIT and GPL licenses: |
| 21 | * http://www.opensource.org/licenses/mit-license.php |
| 22 | * http://www.gnu.org/licenses/gpl.html |
| 23 | */ |
| 24 | |
| 25 | (function($){ |
| 26 | $.fn.tipTip = function(options) { |
| 27 | var defaults = { |
| 28 | activation: "hover", |
| 29 | keepAlive: false, |
| 30 | maxWidth: "200px", |
| 31 | edgeOffset: 3, |
| 32 | defaultPosition: "bottom", |
| 33 | delay: 400, |
| 34 | fadeIn: 200, |
| 35 | fadeOut: 200, |
| 36 | attribute: "title", |
| 37 | content: false, // HTML or String or callback to fill TipTIp with |
| 38 | enter: function(){}, |
| 39 | exit: function(){} |
| 40 | }; |
| 41 | var opts = $.extend(defaults, options); |
| 42 | |
| 43 | // Setup tip tip elements and render them to the DOM |
| 44 | if($("#tiptip_holder").length <= 0){ |
| 45 | var tiptip_holder = $('<div id="tiptip_holder" style="max-width:'+ opts.maxWidth +';"></div>'); |
| 46 | var tiptip_content = $('<div id="tiptip_content"></div>'); |
| 47 | var tiptip_arrow = $('<div id="tiptip_arrow"></div>'); |
| 48 | $("body").append(tiptip_holder.html(tiptip_content).prepend(tiptip_arrow.html('<div id="tiptip_arrow_inner"></div>'))); |
| 49 | } else { |
| 50 | var tiptip_holder = $("#tiptip_holder"); |
| 51 | var tiptip_content = $("#tiptip_content"); |
| 52 | var tiptip_arrow = $("#tiptip_arrow"); |
| 53 | } |
| 54 | |
| 55 | return this.each(function(){ |
| 56 | var org_elem = $(this); |
| 57 | if(opts.content){ |
| 58 | var org_title = opts.content; |
| 59 | } else { |
| 60 | var org_title = org_elem.attr(opts.attribute); |
| 61 | } |
| 62 | if(org_title != ""){ |
| 63 | if(!opts.content){ |
| 64 | org_elem.removeAttr(opts.attribute); //remove original Attribute |
| 65 | } |
| 66 | var timeout = false; |
| 67 | |
| 68 | if(opts.activation == "hover"){ |
| 69 | org_elem.on( 'mouseenter', function(){ |
| 70 | active_tiptip(); |
| 71 | } ).on( 'mouseleave', function(){ |
| 72 | if(!opts.keepAlive || !tiptip_holder.is(':hover')){ |
| 73 | deactive_tiptip(); |
| 74 | } |
| 75 | }); |
| 76 | if(opts.keepAlive){ |
| 77 | tiptip_holder.on( 'mouseenter', function(){} ).on( 'mouseleave', function(){ |
| 78 | deactive_tiptip(); |
| 79 | }); |
| 80 | } |
| 81 | } else if(opts.activation == "focus"){ |
| 82 | org_elem.on( 'focus', function(){ |
| 83 | active_tiptip(); |
| 84 | }).on( 'blur', function(){ |
| 85 | deactive_tiptip(); |
| 86 | }); |
| 87 | } else if(opts.activation == "click"){ |
| 88 | org_elem.on( 'click', function(){ |
| 89 | active_tiptip(); |
| 90 | return false; |
| 91 | }).on( 'mouseenter', function(){} ).on( 'mouseleave' ,function(){ |
| 92 | if(!opts.keepAlive){ |
| 93 | deactive_tiptip(); |
| 94 | } |
| 95 | }); |
| 96 | if(opts.keepAlive){ |
| 97 | tiptip_holder.on( 'mouseenter', function(){} ).on( 'mouseleave', function(){ |
| 98 | deactive_tiptip(); |
| 99 | }); |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | function active_tiptip(){ |
| 104 | var content = typeof opts.content === 'function' ? opts.content() : org_title; |
| 105 | if (!content) { |
| 106 | return; |
| 107 | } |
| 108 | opts.enter.call(this); |
| 109 | tiptip_content.html(content); |
| 110 | tiptip_holder.hide().css("margin","0"); |
| 111 | tiptip_holder.removeAttr('class'); |
| 112 | tiptip_arrow.removeAttr("style"); |
| 113 | |
| 114 | var top = parseInt(org_elem.offset()['top']); |
| 115 | var left = parseInt(org_elem.offset()['left']); |
| 116 | var org_width = parseInt(org_elem.outerWidth()); |
| 117 | var org_height = parseInt(org_elem.outerHeight()); |
| 118 | var tip_w = tiptip_holder.outerWidth(); |
| 119 | var tip_h = tiptip_holder.outerHeight(); |
| 120 | var w_compare = Math.round((org_width - tip_w) / 2); |
| 121 | var h_compare = Math.round((org_height - tip_h) / 2); |
| 122 | var marg_left = Math.round(left + w_compare); |
| 123 | var marg_top = Math.round(top + org_height + opts.edgeOffset); |
| 124 | var t_class = ""; |
| 125 | var arrow_top = ""; |
| 126 | var arrow_left = Math.round(tip_w - 12) / 2; |
| 127 | |
| 128 | if(opts.defaultPosition == "bottom"){ |
| 129 | t_class = "_bottom"; |
| 130 | } else if(opts.defaultPosition == "top"){ |
| 131 | t_class = "_top"; |
| 132 | } else if(opts.defaultPosition == "left"){ |
| 133 | t_class = "_left"; |
| 134 | } else if(opts.defaultPosition == "right"){ |
| 135 | t_class = "_right"; |
| 136 | } |
| 137 | |
| 138 | var right_compare = (w_compare + left) < parseInt($(window).scrollLeft()); |
| 139 | var left_compare = (tip_w + left) > parseInt($(window).width()); |
| 140 | |
| 141 | if((right_compare && w_compare < 0) || (t_class == "_right" && !left_compare) || (t_class == "_left" && left < (tip_w + opts.edgeOffset + 5))){ |
| 142 | t_class = "_right"; |
| 143 | arrow_top = Math.round(tip_h - 13) / 2; |
| 144 | arrow_left = -12; |
| 145 | marg_left = Math.round(left + org_width + opts.edgeOffset); |
| 146 | marg_top = Math.round(top + h_compare); |
| 147 | } else if((left_compare && w_compare < 0) || (t_class == "_left" && !right_compare)){ |
| 148 | t_class = "_left"; |
| 149 | arrow_top = Math.round(tip_h - 13) / 2; |
| 150 | arrow_left = Math.round(tip_w); |
| 151 | marg_left = Math.round(left - (tip_w + opts.edgeOffset + 5)); |
| 152 | marg_top = Math.round(top + h_compare); |
| 153 | } |
| 154 | |
| 155 | var top_compare = (top + org_height + opts.edgeOffset + tip_h + 8) > parseInt($(window).height() + $(window).scrollTop()); |
| 156 | var bottom_compare = ((top + org_height) - (opts.edgeOffset + tip_h + 8)) < 0; |
| 157 | |
| 158 | if(top_compare || (t_class == "_bottom" && top_compare) || (t_class == "_top" && !bottom_compare)){ |
| 159 | if(t_class == "_top" || t_class == "_bottom"){ |
| 160 | t_class = "_top"; |
| 161 | } else { |
| 162 | t_class = t_class+"_top"; |
| 163 | } |
| 164 | arrow_top = tip_h; |
| 165 | marg_top = Math.round(top - (tip_h + 5 + opts.edgeOffset)); |
| 166 | } else if(bottom_compare | (t_class == "_top" && bottom_compare) || (t_class == "_bottom" && !top_compare)){ |
| 167 | if(t_class == "_top" || t_class == "_bottom"){ |
| 168 | t_class = "_bottom"; |
| 169 | } else { |
| 170 | t_class = t_class+"_bottom"; |
| 171 | } |
| 172 | arrow_top = -12; |
| 173 | marg_top = Math.round(top + org_height + opts.edgeOffset); |
| 174 | } |
| 175 | |
| 176 | if(t_class == "_right_top" || t_class == "_left_top"){ |
| 177 | marg_top = marg_top + 5; |
| 178 | } else if(t_class == "_right_bottom" || t_class == "_left_bottom"){ |
| 179 | marg_top = marg_top - 5; |
| 180 | } |
| 181 | if(t_class == "_left_top" || t_class == "_left_bottom"){ |
| 182 | marg_left = marg_left + 5; |
| 183 | } |
| 184 | tiptip_arrow.css({"margin-left": arrow_left+"px", "margin-top": arrow_top+"px"}); |
| 185 | tiptip_holder.css({"margin-left": marg_left+"px", "margin-top": marg_top+"px"}).attr("class","tip"+t_class); |
| 186 | |
| 187 | if (timeout){ clearTimeout(timeout); } |
| 188 | timeout = setTimeout(function(){ tiptip_holder.stop(true,true).fadeIn(opts.fadeIn); }, opts.delay); |
| 189 | } |
| 190 | |
| 191 | function deactive_tiptip(){ |
| 192 | opts.exit.call(this); |
| 193 | if (timeout){ clearTimeout(timeout); } |
| 194 | tiptip_holder.fadeOut(opts.fadeOut); |
| 195 | } |
| 196 | } |
| 197 | }); |
| 198 | } |
| 199 | })(jQuery); |
| 200 |