Skip to content
This repository has been archived by the owner on Sep 5, 2024. It is now read-only.

Commit

Permalink
perf(md-tooltip) use MutationObserver instead of watcher if available
Browse files Browse the repository at this point in the history
use watcher on mdVisible only if being used

braces on same line
  • Loading branch information
clshortfuse committed Mar 31, 2016
1 parent 7a650bc commit 1e0d5f6
Showing 1 changed file with 28 additions and 5 deletions.
33 changes: 28 additions & 5 deletions src/components/tooltip/tooltip.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,19 +92,42 @@ function MdTooltipDirective($timeout, $window, $$rAF, $document, $mdUtil, $mdThe
content.css('transform-origin', origin);
}

function onVisibleChanged (isVisible) {
if (isVisible) showTooltip();
else hideTooltip();
}

function configureWatchers () {
scope.$on('$destroy', function() {
scope.visible = false;
element.remove();
angular.element($window).off('resize', debouncedOnResize);
});

scope.$watch('visible', function (isVisible) {
if (isVisible) showTooltip();
else hideTooltip();
});
if (element[0] && 'MutationObserver' in $window) {
var attributeObserver = new MutationObserver(function(mutations) {
mutations
.forEach(function (mutation) {
if (mutation.attributeName === 'md-visible') {
if (!scope.visibleWatcher)
scope.visibleWatcher = scope.$watch('visible', onVisibleChanged );
}
if (mutation.attributeName === 'md-direction') {
updatePosition(scope.direction);
}
});
});

attributeObserver.observe(element[0], { attributes: true});

scope.$watch('direction', updatePosition );
if (attr.hasOwnProperty('mdVisible')) // build watcher only if mdVisible is being used
scope.visibleWatcher = scope.$watch('visible', onVisibleChanged );

}
else { // MutationObserver not supported
scope.visibleWatcher = scope.$watch('visible', onVisibleChanged );
scope.$watch('direction', updatePosition );
}
}

function addAriaLabel () {
Expand Down

0 comments on commit 1e0d5f6

Please sign in to comment.