Created
April 13, 2015 12:50
-
-
Save cdroulers/afa31c57cc48ea9b6539 to your computer and use it in GitHub Desktop.
svgHover.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
angular.module("shared.ui").directive( | |
"svgHover", | |
[ | |
() => { | |
return { | |
restrict: "A", | |
link: (scope: ng.IScope, element: ng.IAugmentedJQuery) => { | |
function getSvg(svg: HTMLObjectElement): SVGElement { | |
var svgDocument: Document; | |
try { | |
svgDocument = svg.getSVGDocument(); | |
} catch (e) { | |
// IE breaks on the first call. But we can let it go with the setTimeout. | |
} | |
// ReSharper disable once Html.TagNotResolved | |
var svgElement: SVGElement = svgDocument ? <SVGElement>svgDocument.getElementsByTagName("svg")[0] : null; | |
return svgElement; | |
}; | |
function svgMouseOver() { | |
var svgElement = getSvg(<HTMLObjectElement>element.find("object:first")[0]); | |
// Check if 'hovered' is not already set to make sure it is not set twice | |
if (svgElement && svgElement.getAttribute("class") && svgElement.getAttribute("class").indexOf("hovered") === -1) { | |
var currentClass = svgElement.getAttribute("class"); | |
svgElement.setAttribute("class",(currentClass ? currentClass + " " : "") + "hovered"); | |
} | |
}; | |
function svgMouseLeave() { | |
var svgElement = getSvg(<HTMLObjectElement>element.find("object:first")[0]); | |
if (svgElement) { | |
var currentClass = svgElement.getAttribute("class") || ""; | |
svgElement.setAttribute("class", currentClass.replace("hovered", "").trim()); | |
} | |
}; | |
element.hover(svgMouseOver, svgMouseLeave); | |
} | |
}; | |
} | |
]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment