Skip to content

Instantly share code, notes, and snippets.

@pads
Created June 26, 2015 16:00
Show Gist options
  • Save pads/3bbc1c9d45fe84b10977 to your computer and use it in GitHub Desktop.
Save pads/3bbc1c9d45fe84b10977 to your computer and use it in GitHub Desktop.
Angular Bootstrap Dynamic Popover Placement
$tooltipProvider.setTriggers({
// other trigger config
'customOpen':'customClose'
});
angular.module("popover-button-module", [])
.directive("popover-button", ["$parse", "$timeout", "$window", function($parse, $timeout, $window) {
return {
restrict: "A",
link: function popoverButtonLink(scope, button, attributes) {
var id = attributes.id;
var position = attributes.popoverButtonPosition;
var postClickFunction = $parse(attributes.popoverButton);
var popoverIsOpen = false;
if(!id) {
console.error("You must set an ID for the target popover button element");
}
button.bind("customOpen", function popoverButtonOpenHandler() {
popoverIsOpen = true;
$timeout(function adjustPopoverPosition() {
var popover = angular.element(".popover");
if(position) {
// Do positioning here e.g for "top-left", "bottom-right"
}
// The popup is invisible by default so that the user doesn't see the movement
// the above code creates.
popover.addClass("visible");
});
});
button.bind("customClose", function popoverButtonCloseHandler() {
popoverIsOpen = false;
angular.element(".popover").removeClass("visible");
});
button.bind("click", function popoverButtonClickHandler(event) {
if(popoverIsOpen) {
button.triggerHandler("customClose");
} else {
var otherButtons = angular.element("[popover-button]:not('#" + id + "')");
angular.forEach(otherButtons, function closeAllOtherPopovers(value) {
angular.element(value).triggerHandler("customClose");
});
button.triggerHandler("customOpen");
}
if(postClickFunction) {
postClickFunction(scope, { $event: event });
}
});
}
};
}]);
<button id="btn-thumbs-popover"
popover-button="optionalPostClickFunction()"
popover-button-position="top-left"
popover-trigger="customOpen">Open Me</button>
.popover {
visibility: hidden;
}
.popover.visible {
visibility: visible;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment