Skip to content
This repository has been archived by the owner on May 30, 2022. It is now read-only.

Commit

Permalink
fix(macTooltip): Fixed appending multiple tooltips
Browse files Browse the repository at this point in the history
Multiple tooltips get appended to document body when user mouse over the tooltip trigger multiple in a short period of time
  • Loading branch information
adrianlee44 committed Dec 2, 2014
1 parent 2dbfebb commit 8070c26
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 10 deletions.
20 changes: 12 additions & 8 deletions src/directives/tooltip.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,11 @@ angular.module("Mac").directive "macTooltip", [
restrict: "A"

link: (scope, element, attrs) ->
tooltip = null
text = ""
enabled = false
disabled = false
tooltip = null
text = ""
enabled = false
disabled = false
closeDelay = null

defaults =
direction: "top"
Expand All @@ -38,7 +39,7 @@ angular.module("Mac").directive "macTooltip", [
opts = util.extendAttributes "macTooltip", defaults, attrs

showTip = ->
return true if disabled or not text
return true if disabled or not text or tooltip?

tip =
if opts.inside then element else angular.element(document.body)
Expand Down Expand Up @@ -89,12 +90,15 @@ angular.module("Mac").directive "macTooltip", [
return true

removeTip = (delay = 100) ->
if tooltip?
if tooltip? and not closeDelay?
tooltip.removeClass "visible"
$timeout ->

closeDelay = $timeout ->
tooltip?.remove()
tooltip = null
tooltip = null
closeDelay = null
, delay, false

return true

toggle = ->
Expand Down
14 changes: 12 additions & 2 deletions test/unit/tooltip.spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ describe "Mac Tooltip", ->
$timeout = _$timeout_

afterEach ->
tooltip = document.querySelector(".mac-tooltip")
tooltip.parentNode.removeChild tooltip if tooltip?
tooltips = document.querySelectorAll(".mac-tooltip")
for tooltip in tooltips when tooltip?
tooltip.parentNode.removeChild tooltip

describe "Basic Initialization", ->

Expand All @@ -24,6 +25,15 @@ describe "Mac Tooltip", ->

expect(queryTooltip()).not.toBe(null)

it "should only append one tooltip", ->
tip = $compile("<div mac-tooltip='hello world'></div>") $rootScope
$rootScope.$digest()
tip.triggerHandler "mouseenter"
tip.triggerHandler "mouseenter"

tooltip = document.querySelectorAll(".mac-tooltip")
expect(tooltip.length).toBe 1

it "should display the correct message", ->
tip = $compile("<div mac-tooltip='hello world'></div>") $rootScope
$rootScope.$digest()
Expand Down

0 comments on commit 8070c26

Please sign in to comment.