Last active
August 29, 2015 13:56
-
-
Save Thinkscape/8974923 to your computer and use it in GitHub Desktop.
tooltip-group support for angular-strap
This file contains hidden or 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
module.directive('tooltipGroup', [ | |
var popoverRegistry = {}; | |
var suspend = false; // semaphore used to prevent circular calling of hide triggers | |
function(){ | |
return { | |
restrict : 'A', | |
priority : 1, | |
link : { | |
pre : function ($scope, el, attrs, formCtrl) { | |
// $scope.artifactPickerSelectOptions = artifactPicker.getSelectOptions(); | |
}, | |
post: function ($scope, el, attr) { | |
var groupName = attr.tooltipGroup; | |
// Store the name of the popover group in popover scope | |
$scope.$$childHead.tt_group = groupName; | |
// Init group registry | |
if(!angular.isDefined(popoverRegistry[groupName])){ | |
popoverRegistry[groupName] = []; | |
} | |
// Store the popover in the registry | |
popoverRegistry[groupName].push($scope.$$childHead); | |
// Hide all other popups when this one is opened | |
$scope.$$childHead.$watch('$isShown', function(isOpen){ | |
if (!isOpen || suspend) { | |
return false; | |
} | |
suspend = true; // prevent circular ref | |
// Hide all other tooltips from the same group | |
_.each(popoverRegistry[groupName], function(ttScope){ | |
if(ttScope !== $scope.$$childHead && ttScope.$isShown){ | |
ttScope.$hide(); | |
} | |
}); | |
suspend = false; | |
}); | |
// Remove the tooltip from registry upon destruction | |
$scope.$$childHead.$on('$destroy', function onDestroyTooltip() { | |
_.remove(popoverRegistry[groupName], function(ttScope){ return ttScope === $scope }); | |
}); | |
} | |
} | |
}; | |
} | |
]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment