Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix css classes on smartplacement, fix correct position after an image was loaded #131

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
dist
node_modules
npm-debug.log
.idea
33 changes: 24 additions & 9 deletions src/tooltipcontroller.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
* @param {Object} options Options object containing settings.
*/
function TooltipController(options) {

// Holds on tooltip controller.
var back_reference = this;

var placementCalculator = new PlacementCalculator(),
tipElement = $('#' + options.popupId);

Expand Down Expand Up @@ -89,7 +93,7 @@ function TooltipController(options) {
element.trigger('powerTipPreRender');

// set tooltip content
tipContent = getTooltipContent(element);
tipContent = getTooltipContent(element, back_reference);
if (tipContent) {
tipElement.empty().append(tipContent);
} else {
Expand All @@ -105,13 +109,8 @@ function TooltipController(options) {

tipElement.data(DATA_MOUSEONTOTIP, options.mouseOnToPopup);

// set tooltip position
if (!options.followMouse) {
positionTipOnElement(element);
session.isFixedTipOpen = true;
} else {
positionTipOnCursor();
}
// set tooltip position
back_reference.updatePosition(element);

// add custom class to tooltip element
tipElement.addClass(options.popupClass);
Expand Down Expand Up @@ -209,6 +208,22 @@ function TooltipController(options) {
});
}

/**
* Updates the position of the tooltip.
*
* @param {jQuery} element
* The element to update.
*/
this.updatePosition = function(element) {
// set tooltip position
if (!options.followMouse) {
positionTipOnElement(element);
session.isFixedTipOpen = true;
} else {
positionTipOnCursor();
}
}

/**
* Moves the tooltip to the users mouse cursor.
* @private
Expand Down Expand Up @@ -302,7 +317,7 @@ function TooltipController(options) {
}

// add placement as class for CSS arrows
tipElement.addClass(finalPlacement);
tipElement.attr('css', finalPlacement);
}

/**
Expand Down
34 changes: 32 additions & 2 deletions src/utility.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,11 @@ function isMouseOver(element) {
* Fetches the tooltip content from the specified element's data attributes.
* @private
* @param {jQuery} element The element to get the tooltip content for.
* @param {TooltipController} controller The tooltip controller.
* @return {(string|jQuery|undefined)} The text/HTML string, jQuery object, or
* undefined if there was no tooltip content for the element.
*/
function getTooltipContent(element) {
function getTooltipContent(element, controller) {
var tipText = element.data(DATA_POWERTIP),
tipObject = element.data(DATA_POWERTIPJQ),
tipTarget = element.data(DATA_POWERTIPTARGET),
Expand All @@ -152,7 +153,36 @@ function getTooltipContent(element) {
}
}

return content;
// Make sure we have a jquery content object.
var real_content = $(content);

// Verify not attach event again.
if (!real_content.hasClass('powertip-image-processed')) {

// Verify content is an image.
if (real_content[0].localName === 'img') {
// Attach load triger, to update positions after an image was successfully loaded.
$(real_content).addClass('powertip-image-processed').on('load', function() {
controller.updatePosition(element);
});
}
}

// Now process all sub images starting, from the content.

// Process only images which aren't yet.
$('img:not(.powertip-image-processed)', real_content).each(function() {

// Mark image as processed and attach event.
$(this).addClass('powertip-image-processed').on('load', function() {

// Reposition the tooltip after an image is successfully loaded.
controller.updatePosition(element);
});
});

// Return real content instead of original content.
return real_content;
}

/**
Expand Down