Skip to content

Instantly share code, notes, and snippets.

@cdroulers
Created April 13, 2015 12:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cdroulers/afa31c57cc48ea9b6539 to your computer and use it in GitHub Desktop.
Save cdroulers/afa31c57cc48ea9b6539 to your computer and use it in GitHub Desktop.
svgHover.ts
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