|
| 1 | +vartip=(function(){ |
| 2 | + |
| 3 | +vardestroyTimer=null; |
| 4 | + |
| 5 | +varintervalId; |
| 6 | + |
| 7 | +var_bind=function(){ |
| 8 | +varallTip=helper.eA("[data-tip-options]"); |
| 9 | +for(vari=0;i<allTip.length;i++){ |
| 10 | +varoptions=helper.makeObject(allTip[i].dataset.tipOptions); |
| 11 | +_bind_tip(allTip[i]); |
| 12 | +}; |
| 13 | +}; |
| 14 | + |
| 15 | +var_bind_tip=function(tip){ |
| 16 | +varoptions=helper.makeObject(tip.dataset.tipOptions); |
| 17 | +varaction={ |
| 18 | +focus: function(){ |
| 19 | +tip.addEventListener("focus",function(){ |
| 20 | +if(options.delay){ |
| 21 | +intervalId=setInterval(function(){ |
| 22 | +render(tip); |
| 23 | +},options.delay); |
| 24 | +}else{ |
| 25 | +render(tip); |
| 26 | +}; |
| 27 | +},false); |
| 28 | +tip.addEventListener("blur",function(){ |
| 29 | +destroy(); |
| 30 | +clearInterval(intervalId); |
| 31 | +},false); |
| 32 | +}, |
| 33 | +hover: function(){ |
| 34 | +tip.addEventListener("mouseover",function(){ |
| 35 | +if(options.delay){ |
| 36 | +intervalId=setInterval(function(){ |
| 37 | +render(tip); |
| 38 | +},options.delay); |
| 39 | +}else{ |
| 40 | +render(tip); |
| 41 | +}; |
| 42 | +},false); |
| 43 | +tip.addEventListener("mouseout",function(){ |
| 44 | +destroy(); |
| 45 | +clearInterval(intervalId); |
| 46 | +},false); |
| 47 | +} |
| 48 | +}; |
| 49 | +action[options.state](); |
| 50 | +}; |
| 51 | + |
| 52 | +vardelayDestroy=function(){ |
| 53 | +varallTipBox=helper.eA(".tip-box"); |
| 54 | +for(vari=0;i<allTipBox.length;i++){ |
| 55 | +if(!allTipBox[i].classList.contains("is-opaque")){ |
| 56 | +allTipBox[i].parentElement.removeChild(allTipBox[i]); |
| 57 | +}; |
| 58 | +}; |
| 59 | +}; |
| 60 | + |
| 61 | +vardestroy=function(){ |
| 62 | +varallTipBox=helper.eA(".tip-box"); |
| 63 | +if(allTipBox[0]){ |
| 64 | +for(vari=0;i<allTipBox.length;i++){ |
| 65 | +allTipBox[i].destroy(); |
| 66 | +}; |
| 67 | +}; |
| 68 | +}; |
| 69 | + |
| 70 | +varrender=function(tip){ |
| 71 | +// console.log(tip.getBoundingClientRect()); |
| 72 | +varoptions=helper.makeObject(tip.dataset.tipOptions); |
| 73 | +varbody=helper.e("body"); |
| 74 | +vartipWrapper=document.createElement("div"); |
| 75 | +tipWrapper.setAttribute("class","tip tip-box is-transparent"); |
| 76 | +vartipArrow=document.createElement("span"); |
| 77 | +tipArrow.setAttribute("class","tip-arrow"); |
| 78 | +vartipMessage=document.createElement("p"); |
| 79 | +tipMessage.setAttribute("class","tip-message"); |
| 80 | +tipMessage.textContent=options.message; |
| 81 | +tipWrapper.destroy=function(){ |
| 82 | +if(tipWrapper.classList.contains("is-opaque")){ |
| 83 | +helper.removeClass(tipWrapper,"is-opaque"); |
| 84 | +helper.addClass(tipWrapper,"is-transparent"); |
| 85 | +helper.removeClass(tipWrapper,"tip-intro"); |
| 86 | +helper.addClass(tipWrapper,"tip-outro"); |
| 87 | +}else{ |
| 88 | +tipWrapper.remove(); |
| 89 | +}; |
| 90 | +}; |
| 91 | +tipWrapper.addEventListener("transitionend",function(event,elapsed){ |
| 92 | +if(event.propertyName==="opacity"&&getComputedStyle(this).opacity==0){ |
| 93 | +this.parentElement.removeChild(this); |
| 94 | +}; |
| 95 | +}.bind(tipWrapper),false); |
| 96 | + |
| 97 | +tipWrapper.appendChild(tipMessage); |
| 98 | +tipWrapper.appendChild(tipArrow); |
| 99 | +body.appendChild(tipWrapper); |
| 100 | +tipWrapper.setAttribute("style","width: "+(parseInt(tipWrapper.getBoundingClientRect().width,10)+2)+"px;"); |
| 101 | + |
| 102 | +varwidth=parseInt(tipWrapper.getBoundingClientRect().width); |
| 103 | +vartop=parseInt(tip.getBoundingClientRect().top,10)+parseInt(pageYOffset,10)-parseInt(tipWrapper.getBoundingClientRect().height,10)-parseInt(getComputedStyle(tipWrapper).marginTop,10)-parseInt(getComputedStyle(tipWrapper).marginBottom,10); |
| 104 | +varleft=parseInt(tip.getBoundingClientRect().left,10)+parseInt((tip.getBoundingClientRect().width/2),10)-parseInt(((width+parseInt(getComputedStyle(tipWrapper).marginLeft,10)+parseInt(getComputedStyle(tipWrapper).marginRight,10))/2),10); |
| 105 | + |
| 106 | +tipWrapper.setAttribute("style","width: "+width+"px; top: "+top+"px; left: "+left+"px"); |
| 107 | +varstyle={ |
| 108 | +top: tipWrapper.style.top, |
| 109 | +width: tipWrapper.style.width |
| 110 | +}; |
| 111 | + |
| 112 | +if(tipWrapper.getBoundingClientRect().left<parseInt(getComputedStyle(tipWrapper).marginLeft,10)){ |
| 113 | +// console.log("too far left"); |
| 114 | +tipWrapper.setAttribute("style","width: "+style.width+" top: "+style.top+" left: "+0+"px;"); |
| 115 | +tipArrow.setAttribute("style","left: "+(parseInt(tip.getBoundingClientRect().left,10)+parseInt((tip.getBoundingClientRect().width/2),10)-parseInt(getComputedStyle(tipWrapper).marginLeft,10))+"px;"); |
| 116 | +}elseif(tipWrapper.getBoundingClientRect().right>(document.documentElement.clientWidth-parseInt(getComputedStyle(tipWrapper).marginLeft,10))){ |
| 117 | +// console.log("too far right"); |
| 118 | +tipWrapper.setAttribute("style","width: "+style.width+" top: "+style.top+" left: initial; right: "+0+"px;"); |
| 119 | +tipArrow.setAttribute("style","left: "+(-parseInt(tipWrapper.getBoundingClientRect().left,10)+parseInt(tip.getBoundingClientRect().left,10)+(parseInt((tip.getBoundingClientRect().width),10)/2))+"px;"); |
| 120 | +}; |
| 121 | + |
| 122 | +getComputedStyle(tipWrapper).opacity; |
| 123 | +helper.removeClass(tipWrapper,"is-transparent"); |
| 124 | +helper.addClass(tipWrapper,"is-opaque"); |
| 125 | +helper.addClass(tipWrapper,"tip-intro"); |
| 126 | +}; |
| 127 | + |
| 128 | +varinit=function(){ |
| 129 | +_bind(); |
| 130 | +}; |
| 131 | + |
| 132 | +// exposed methods |
| 133 | +return{ |
| 134 | +destroy: destroy, |
| 135 | +init: init |
| 136 | +}; |
| 137 | + |
| 138 | +})(); |
0 commit comments