From d56e14c6db007c88a70a16df43825d7982de1381 Mon Sep 17 00:00:00 2001 From: Spencer Alger Date: Tue, 30 Sep 2014 18:58:57 -0700 Subject: [PATCH] [vis/tooltip] modify the placement if we can't swap direction --- src/kibana/components/vislib/lib/tooltip.js | 42 ++++++++++++++++----- 1 file changed, 32 insertions(+), 10 deletions(-) diff --git a/src/kibana/components/vislib/lib/tooltip.js b/src/kibana/components/vislib/lib/tooltip.js index eedfa99639f96..56e35342644ff 100644 --- a/src/kibana/components/vislib/lib/tooltip.js +++ b/src/kibana/components/vislib/lib/tooltip.js @@ -63,7 +63,7 @@ define(function (require) { Tooltip.prototype.getTooltipPlacement = function (event) { var self = this; - var OFFSET = 15; + var OFFSET = 10; var chart = $(self.el).find('.' + self.containerClass); if (!chart.size()) return; @@ -82,22 +82,44 @@ define(function (require) { west: event.clientX - tipWidth - OFFSET }; - var eastOverEdge = left.east + tipWidth > chartPos.right; - var westOverEdge = left.west + tipWidth < chartPos.left; - // the placements if we were to place the tip north or south var top = { south: event.clientY + OFFSET, north: event.clientY - tipHeight - OFFSET }; - var northOverEdge = top.north + tipHeight < chartPos.top; - var southOverEdge = top.south + tipHeight > chartPos.bottom; - - return { - top: (southOverEdge && !northOverEdge) ? top.north : top.south, - left: (eastOverEdge && !westOverEdge) ? left.west : left.east + // number of pixels that the toolip would overflow it's far + // side, if we placed it that way. (negative === no overflow) + var overflow = { + north: chartPos.top - top.north, + east: (left.east + tipWidth) - chartPos.right, + south: (top.south + tipHeight) - chartPos.bottom, + west: chartPos.left - left.west }; + + var placement = {}; + + if (overflow.south > 0) { + if (overflow.north < 0) { + placement.top = top.north; + } else { + placement.top = top.south - overflow.south; + } + } else { + placement.top = top.south; + } + + if (overflow.east > 0) { + if (overflow.west < 0) { + placement.left = left.west; + } else { + placement.left = left.east - overflow.east; + } + } else { + placement.left = left.east; + } + + return placement; }; return Tooltip;