]> git.ipfire.org Git - thirdparty/bootstrap.git/commitdiff
Tooltip positionning on special cases
authorGuillaume Gautreau <guillaume+github@ghusse.com>
Sat, 26 Jan 2013 12:50:26 +0000 (13:50 +0100)
committerGuillaume Gautreau <guillaume+github@ghusse.com>
Sat, 26 Jan 2013 12:50:26 +0000 (13:50 +0100)
js/bootstrap-tooltip.js
less/tooltip.less

index c23d8267a8e7962bf0f4e0808646e6c2e3b3eaf7..0d8a20f5683475f26843f36c663ac552889e8104 100644 (file)
             break
         }
 
-        $tip
-          .offset(tp)
-          .addClass(placement)
-          .addClass('in')
+        this.applyPlacement(tp, placement);
 
         this.$element.trigger('shown')
       }
     }
 
+  , applyPlacement: function(offset, placement){
+    var $tip
+      , width
+      , height
+      , actualWidth
+      , actualHeight
+      , delta
+      , replace = false;
+
+    $tip = this.tip();
+
+    width = $tip[0].offsetWidth;
+    height = $tip[0].offsetHeight;
+
+    $tip
+          .offset(offset)
+          .addClass(placement)
+          .addClass('in');
+
+    actualWidth = $tip[0].offsetWidth;
+    actualHeight = $tip[0].offsetHeight;
+
+    if (placement == "top" && actualHeight != actualWidth){
+      offset.top = offset.top + height - actualHeight;
+      replace = true;
+    }
+
+    if (placement == "bottom" || placement == "top"){
+      delta = 0;
+
+      if (offset.left < 0){
+        delta = -offset.left * 2;
+        offset.left = 0;
+        $tip.offset(offset);
+        actualWidth = $tip[0].offsetWidth;
+        actualHeight = $tip[0].offsetHeight;
+      }
+
+      this.replaceArrow(delta - width + actualWidth, actualWidth, "left");
+    }else{
+      this.replaceArrow(actualHeight - height, actualHeight, "top");
+    }
+
+    if (replace) $tip.offset(offset);
+  }
+
+  , replaceArrow: function(delta, dimension, position){
+    var $arrow = this.arrow();
+
+    if (delta !== 0){
+      $arrow.css(position, 50 * (1 - delta / dimension) + "%");
+    }else{
+      $arrow.css(position, "");
+    }
+  }
+
   , setContent: function () {
       var $tip = this.tip()
         , title = this.getTitle()
       return this.$tip = this.$tip || $(this.options.template)
     }
 
+  , arrow: function(){
+    return this.$arrow = this.$arrow || this.tip().find(".tooltip-arrow");
+  }
+
   , validate: function () {
       if (!this.$element[0].parentNode) {
         this.hide()
index 59b02cd4314c82b75225a105bf23741b9470de84..d5a2bfaba642bacb89a7f7fc1b3c8bc27e7d2870 100644 (file)
@@ -9,15 +9,14 @@
   z-index: @zindexTooltip;
   display: block;
   visibility: visible;
-  padding: 5px;
   font-size: 11px;
   line-height: 1.4;
   .opacity(0);
   &.in     { .opacity(80); }
-  &.top    { margin-top:  -3px; }
-  &.right  { margin-left:  3px; }
-  &.bottom { margin-top:   3px; }
-  &.left   { margin-left: -3px; }
+  &.top    { margin-top:  -3px; padding: 5px 0;}
+  &.right  { margin-left:  3px; padding: 0 5px;}
+  &.bottom { margin-top:   3px; padding: 5px 0;}
+  &.left   { margin-left: -3px; padding: 0 5px;}
 }
 
 // Wrapper for the tooltip content