Team:Imperial College London/balloontipjs

From 2011.igem.org

(Difference between revisions)
(Created page with "//Rich HTML Balloon Tooltip: http://www.dynamicdrive.com/dynamicindex5/balloontooltip.htm //Created: September 10th, 2006 //Updated: May 9th, 10 for window edge bug var disappea...")
Line 1: Line 1:
-
//Rich HTML Balloon Tooltip: http://www.dynamicdrive.com/dynamicindex5/balloontooltip.htm
+
/*Speech Bubbles Tooltip (Initial: Dec 8th, 2010)
-
//Created: September 10th, 2006
+
* This notice must stay intact for usage
-
//Updated: May 9th, 10 for window edge bug
+
* Author: Dynamic Drive at http://www.dynamicdrive.com/
 +
* Visit http://www.dynamicdrive.com/ for full source code
 +
*/
-
var disappeardelay=250  //tooltip disappear delay (in miliseconds)
+
var speechbubbles_tooltip={
-
var verticaloffset=0 //vertical offset of tooltip from anchor link, if any
+
-
var enablearrowhead=1 //0 or 1, to disable or enable the arrow image
+
-
var arrowheadimg=["https://static.igem.org/mediawiki/2011/e/ee/ICL_arrowdown.gif", "https://static.igem.org/mediawiki/2011/d/d5/ICL_arrowup.gif"] //path to down and up arrow images
+
-
var arrowheadheight=11 //height of arrow image (amount to reveal)
+
-
/////No further editting needed
+
loadcontent:function($, selector, options, callback){
 +
var ajaxfriendlyurl=options.url.replace(/^http:\/\/[^\/]+\//i, "http://"+window.location.hostname+"/")
 +
$.ajax({
 +
url: ajaxfriendlyurl, //path to external content
 +
async: true,
 +
error:function(ajaxrequest){
 +
alert('Error fetching Ajax content.<br />Server Response: '+ajaxrequest.responseText)
 +
},
 +
success:function(content){
 +
$(document.body).append(content)
 +
callback(selector)
 +
$(content).remove()
 +
}
 +
})
-
var ie=document.all
+
},
-
var ns6=document.getElementById&&!document.all
+
-
verticaloffset=(enablearrowhead)? verticaloffset+arrowheadheight : verticaloffset
+
-
function getposOffset(what, offsettype){
+
buildtooltip:function($, setting){
-
var totaloffset=(offsettype=="left")? what.offsetLeft : what.offsetTop;
+
var speechtext=(setting.speechid)? $('div#'+setting.speechid).html() : setting.speechtext
-
var parentEl=what.offsetParent;
+
if (speechtext){
-
while (parentEl!=null){
+
$speech=$('<div class="speechbubbles">'+speechtext+'</div>').appendTo(document.body)
-
totaloffset=(offsettype=="left")? totaloffset+parentEl.offsetLeft : totaloffset+parentEl.offsetTop;
+
$speech.addClass('speechbubbles').append('<div class="speechbubbles-arrow-border"></div>\n<div class="speechbubbles-arrow"></div>')
-
parentEl=parentEl.offsetParent;
+
$speech.data('$arrowparts', $speech.find('div.speechbubbles-arrow, div.speechbubbles-arrow-border')) //store ref to the two arrow DIVs within tooltip
-
}
+
var arrowheight=(window.XMLHttpRequest)? $speech.data('$arrowparts').eq(0).outerHeight() : 10
-
return totaloffset;
+
$speech.data('measure', {w:$speech.outerWidth(), h:$speech.outerHeight()+arrowheight, arroww:$speech.data('$arrowparts').eq(0).outerWidth()}) //cache tooltip dimensions
-
}
+
$speech.css({display:'none', visibility:'visible'})
 +
setting.$speech=$speech //remember ref to tooltip
 +
}
 +
return setting.$speech
 +
},
 +
-
function showhide(obj, e){
+
positiontip:function($, $anchor, s, e){
-
dropmenuobj.style.left=dropmenuobj.style.top="-500px"
+
var $speech=s.$speech
-
if (e.type=="mouseover")
+
var $offset=$anchor.offset()
-
obj.visibility="visible"
+
var windowmeasure={w:$(window).width(), h:$(window).height(), left:$(document).scrollLeft(), top:$(document).scrollTop()} //get various window measurements
-
}
+
var anchormeasure={w:$anchor.outerWidth(), h:$anchor.outerHeight(), left:$offset.left, top:$offset.top} //get various anchor link measurements
 +
var speechmeasure={w:$speech.data('measure').w, h:$speech.data('measure').h} //get tooltip measurements
 +
var x=anchormeasure.left
 +
var y=anchormeasure.top+anchormeasure.h
 +
x=(x+speechmeasure.w>windowmeasure.left+windowmeasure.w-3)? x-speechmeasure.w+anchormeasure.w-5 : x //right align tooltip if no space to the right of the anchor
 +
y=(y+speechmeasure.h>windowmeasure.top+windowmeasure.h)? y-speechmeasure.h-anchormeasure.h-10 : y+10 //top align tooltip if no space to the bottom of the anchor
 +
var isrightaligned=x!=anchormeasure.left //Boolean to indicate if tooltip is right aligned
 +
var istopaligned=y!=anchormeasure.top+anchormeasure.h+10 //Boolean to indicate if tooltip is top aligned
 +
$speech.removeClass('downversion').addClass(istopaligned? 'downversion' : '') //add CSS "downversion" class to tooltip if arrow should be pointing down
 +
var arrowpos=(isrightaligned)? speechmeasure.w-(anchormeasure.left+anchormeasure.w-e.pageX)-25 : e.pageX-anchormeasure.left-25 //25 is to move arrow 25px to the left so it's not obscured by cursor
 +
if (arrowpos>speechmeasure.w-25) //if arrow exceeds the width of the tooltip
 +
arrowpos=speechmeasure.w-40 //move it to the left of the cursor
 +
else{
 +
arrowpos=(isrightaligned)? Math.max(anchormeasure.left-x+10, arrowpos) : Math.max(15, arrowpos) //make sure arrow doesn't appear too far to the left of the tooltip
 +
}
 +
$speech.data('$arrowparts').css('left', arrowpos)
 +
var speechcss_before={opacity:0, left:x, top:(istopaligned)? y-speechmeasure.h-10 : y+speechmeasure.h+10}
 +
var speechcss_after={opacity:1, top:y+10}
 +
if (document.all && !window.msPerformance){ //detect IE8 and below
 +
delete speechcss_before.opacity //remove opacity property, as IE8- does not animate this property well with CSS triangles present
 +
delete speechcss_after.opacity
 +
}
 +
$speech.css(speechcss_before).show().animate(speechcss_after)
 +
},
-
function iecompattest(){
 
-
return (document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body
 
-
}
 
-
function clearbrowseredge(obj, whichedge){
+
init:function($, $anchor, options){
-
if (whichedge=="rightedge"){
+
var s={speechtext:$anchor.attr('title'), speechid:$anchor.attr('rel')}
-
edgeoffsetx=0
+
$.extend(s, options)
-
var windowedge=ie && !window.opera? iecompattest().scrollLeft+iecompattest().clientWidth-15 : window.pageXOffset+window.innerWidth-15
+
if (this.buildtooltip($, s)){
-
dropmenuobj.contentmeasure=dropmenuobj.offsetWidth
+
if (s.speechtext) //if title attribute of anchor is defined
-
if (windowedge-dropmenuobj.x < dropmenuobj.contentmeasure)
+
$anchor.attr('title', "") //disable it
-
edgeoffsetx=dropmenuobj.contentmeasure-obj.offsetWidth
+
$anchor.mouseenter(function(e){
-
if (dropmenuobj.x-edgeoffsetx+dropmenuobj.contentmeasure>windowedge)
+
if (s.$speech.queue().length==0){
-
edgeoffsetx=dropmenuobj.x-windowedge+dropmenuobj.contentmeasure
+
clearTimeout(s.hidetimer)
-
return edgeoffsetx
+
speechbubbles_tooltip.positiontip($, $anchor, s, e)
-
}
+
}
-
else{
+
})
-
edgeoffsety=0
+
$anchor.mouseleave(function(e){
-
var topedge=ie && !window.opera? iecompattest().scrollTop : window.pageYOffset
+
s.hidetimer=setTimeout(function(){s.$speech.stop(true,true).hide()}, 200)
-
var windowedge=ie && !window.opera? iecompattest().scrollTop+iecompattest().clientHeight-15 : window.pageYOffset+window.innerHeight-18
+
})
-
dropmenuobj.contentmeasure=dropmenuobj.offsetHeight
+
}
-
if (windowedge-dropmenuobj.y < dropmenuobj.contentmeasure) //move up?
+
}
-
edgeoffsety=dropmenuobj.contentmeasure+obj.offsetHeight+(verticaloffset*2)
+
-
return edgeoffsety
+
-
}
+
-
}
+
-
function displayballoontip(obj, e){ //main ballooon tooltip function
 
-
if (window.event) event.cancelBubble=true
 
-
else if (e.stopPropagation) e.stopPropagation()
 
-
if (typeof dropmenuobj!="undefined") //hide previous tooltip?
 
-
dropmenuobj.style.visibility="hidden"
 
-
clearhidemenu()
 
-
//obj.onmouseout=delayhidemenu
 
-
dropmenuobj=document.getElementById(obj.getAttribute("rel"))
 
-
showhide(dropmenuobj.style, e)
 
-
dropmenuobj.x=getposOffset(obj, "left")
 
-
dropmenuobj.y=getposOffset(obj, "top")+verticaloffset
 
-
dropmenuobj.style.left=dropmenuobj.x-clearbrowseredge(obj, "rightedge")+"px"
 
-
dropmenuobj.style.top=dropmenuobj.y-clearbrowseredge(obj, "bottomedge")+obj.offsetHeight+"px"
 
-
if (enablearrowhead)
 
-
displaytiparrow()
 
-
}
 
-
 
-
function displaytiparrow(){ //function to display optional arrow image associated with tooltip
 
-
tiparrow=document.getElementById("arrowhead")
 
-
tiparrow.src=(edgeoffsety!=0)? arrowheadimg[0] : arrowheadimg[1]
 
-
var ieshadowwidth=(dropmenuobj.filters && dropmenuobj.filters[0])? dropmenuobj.filters[0].Strength-1 : 0
 
-
//modify "left" value depending on whether there's no room on right edge of browser to display it, respectively
 
-
tiparrow.style.left=(edgeoffsetx!=0)? parseInt(dropmenuobj.style.left)+dropmenuobj.offsetWidth-tiparrow.offsetWidth-10+"px" : parseInt(dropmenuobj.style.left)+5+"px"
 
-
//modify "top" value depending on whether there's no room on right edge of browser to display it, respectively
 
-
tiparrow.style.top=(edgeoffsety!=0)? parseInt(dropmenuobj.style.top)+dropmenuobj.offsetHeight-tiparrow.offsetHeight-ieshadowwidth+arrowheadheight+"px" : parseInt(dropmenuobj.style.top)-arrowheadheight+"px"
 
-
tiparrow.style.visibility="visible"
 
-
}
 
-
 
-
function delayhidemenu(){
 
-
delayhide=setTimeout("dropmenuobj.style.visibility='hidden'; dropmenuobj.style.left=0; if (enablearrowhead) tiparrow.style.visibility='hidden'",disappeardelay)
 
-
}
 
-
 
-
function clearhidemenu(){
 
-
if (typeof delayhide!="undefined")
 
-
clearTimeout(delayhide)
 
-
}
 
-
 
-
function reltoelement(linkobj){ //tests if a link has "rel" defined and it's the ID of an element on page
 
-
var relvalue=linkobj.getAttribute("rel")
 
-
return (relvalue!=null && relvalue!="" && document.getElementById(relvalue)!=null && document.getElementById(relvalue).className=="balloonstyle")? true : false
 
-
}
 
-
 
-
function initalizetooltip(){
 
-
var all_links=document.getElementsByTagName("a")
 
-
if (enablearrowhead){
 
-
tiparrow=document.createElement("img")
 
-
tiparrow.setAttribute("src", arrowheadimg[0])
 
-
tiparrow.setAttribute("id", "arrowhead")
 
-
document.body.appendChild(tiparrow)
 
-
}
 
-
for (var i=0; i<all_links.length; i++){
 
-
if (reltoelement(all_links[i])){ //if link has "rel" defined and it's the ID of an element on page
 
-
all_links[i].onmouseover=function(e){
 
-
var evtobj=window.event? window.event : e
 
-
displayballoontip(this, evtobj)
 
-
}
 
-
all_links[i].onmouseout=delayhidemenu
 
-
}
 
-
}
 
}
}
-
if (window.addEventListener)
+
jQuery.fn.speechbubble=function(options){
-
window.addEventListener("load", initalizetooltip, false)
+
var $=jQuery
-
else if (window.attachEvent)
+
function processanchor(selector){
-
window.attachEvent("onload", initalizetooltip)
+
return selector.each(function(){ //return jQuery obj
-
else if (document.getElementById)
+
var $anchor=$(this)
-
window.onload=initalizetooltip
+
speechbubbles_tooltip.init($, $anchor, options)
 +
})
 +
}
 +
if (options && options.url)
 +
speechbubbles_tooltip.loadcontent($, this, options, processanchor)
 +
else
 +
processanchor(this)
 +
};

Revision as of 20:41, 21 September 2011

/*Speech Bubbles Tooltip (Initial: Dec 8th, 2010)

  • This notice must stay intact for usage
  • Author: Dynamic Drive at http://www.dynamicdrive.com/
  • Visit http://www.dynamicdrive.com/ for full source code
  • /

var speechbubbles_tooltip={

loadcontent:function($, selector, options, callback){ var ajaxfriendlyurl=options.url.replace(/^http:\/\/[^\/]+\//i, "http://"+window.location.hostname+"/") $.ajax({ url: ajaxfriendlyurl, //path to external content async: true, error:function(ajaxrequest){ alert('Error fetching Ajax content.
Server Response: '+ajaxrequest.responseText) }, success:function(content){ $(document.body).append(content) callback(selector) $(content).remove() } })

},

buildtooltip:function($, setting){ var speechtext=(setting.speechid)? $('div#'+setting.speechid).html() : setting.speechtext if (speechtext){

$speech=$('
'+speechtext+'
').appendTo(document.body) $speech.addClass('speechbubbles').append('
\n
')

$speech.data('$arrowparts', $speech.find('div.speechbubbles-arrow, div.speechbubbles-arrow-border')) //store ref to the two arrow DIVs within tooltip var arrowheight=(window.XMLHttpRequest)? $speech.data('$arrowparts').eq(0).outerHeight() : 10 $speech.data('measure', {w:$speech.outerWidth(), h:$speech.outerHeight()+arrowheight, arroww:$speech.data('$arrowparts').eq(0).outerWidth()}) //cache tooltip dimensions $speech.css({display:'none', visibility:'visible'}) setting.$speech=$speech //remember ref to tooltip } return setting.$speech },


positiontip:function($, $anchor, s, e){ var $speech=s.$speech var $offset=$anchor.offset() var windowmeasure={w:$(window).width(), h:$(window).height(), left:$(document).scrollLeft(), top:$(document).scrollTop()} //get various window measurements var anchormeasure={w:$anchor.outerWidth(), h:$anchor.outerHeight(), left:$offset.left, top:$offset.top} //get various anchor link measurements var speechmeasure={w:$speech.data('measure').w, h:$speech.data('measure').h} //get tooltip measurements var x=anchormeasure.left var y=anchormeasure.top+anchormeasure.h x=(x+speechmeasure.w>windowmeasure.left+windowmeasure.w-3)? x-speechmeasure.w+anchormeasure.w-5 : x //right align tooltip if no space to the right of the anchor y=(y+speechmeasure.h>windowmeasure.top+windowmeasure.h)? y-speechmeasure.h-anchormeasure.h-10 : y+10 //top align tooltip if no space to the bottom of the anchor var isrightaligned=x!=anchormeasure.left //Boolean to indicate if tooltip is right aligned var istopaligned=y!=anchormeasure.top+anchormeasure.h+10 //Boolean to indicate if tooltip is top aligned $speech.removeClass('downversion').addClass(istopaligned? 'downversion' : ) //add CSS "downversion" class to tooltip if arrow should be pointing down var arrowpos=(isrightaligned)? speechmeasure.w-(anchormeasure.left+anchormeasure.w-e.pageX)-25 : e.pageX-anchormeasure.left-25 //25 is to move arrow 25px to the left so it's not obscured by cursor if (arrowpos>speechmeasure.w-25) //if arrow exceeds the width of the tooltip arrowpos=speechmeasure.w-40 //move it to the left of the cursor else{ arrowpos=(isrightaligned)? Math.max(anchormeasure.left-x+10, arrowpos) : Math.max(15, arrowpos) //make sure arrow doesn't appear too far to the left of the tooltip } $speech.data('$arrowparts').css('left', arrowpos) var speechcss_before={opacity:0, left:x, top:(istopaligned)? y-speechmeasure.h-10 : y+speechmeasure.h+10} var speechcss_after={opacity:1, top:y+10} if (document.all && !window.msPerformance){ //detect IE8 and below delete speechcss_before.opacity //remove opacity property, as IE8- does not animate this property well with CSS triangles present delete speechcss_after.opacity } $speech.css(speechcss_before).show().animate(speechcss_after) },


init:function($, $anchor, options){ var s={speechtext:$anchor.attr('title'), speechid:$anchor.attr('rel')} $.extend(s, options) if (this.buildtooltip($, s)){ if (s.speechtext) //if title attribute of anchor is defined $anchor.attr('title', "") //disable it $anchor.mouseenter(function(e){ if (s.$speech.queue().length==0){ clearTimeout(s.hidetimer) speechbubbles_tooltip.positiontip($, $anchor, s, e) } }) $anchor.mouseleave(function(e){ s.hidetimer=setTimeout(function(){s.$speech.stop(true,true).hide()}, 200) }) } }

}

jQuery.fn.speechbubble=function(options){ var $=jQuery function processanchor(selector){ return selector.each(function(){ //return jQuery obj var $anchor=$(this) speechbubbles_tooltip.init($, $anchor, options) }) } if (options && options.url) speechbubbles_tooltip.loadcontent($, this, options, processanchor) else processanchor(this) };