The communications platform that puts data protection first.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Rocket.Chat/packages/rocketchat-tooltip/rocketchat-tooltip.js

87 lines
1.5 KiB

10 years ago
/* globals Blaze, RocketChat */
RocketChat.tooltip = {
source: null,
initiated: false,
opened: false,
10 years ago
init() {
if (this.initiated) {
return;
}
this.initiated = true;
Blaze.render(Template.rocketchatTooltip, document.body);
},
showElement(element, source) {
10 years ago
if (this.opened) {
return;
}
if (this.timeout) {
clearTimeout(this.timeout);
}
this.timeout = setTimeout(() => {
this.timeout = null;
this.source = source;
$('.tooltip .content').empty().append($(element).clone().show());
10 years ago
this.setPosition().addClass('show');
this.opened = true;
}, 300);
},
10 years ago
hide() {
if (this.timeout) {
clearTimeout(this.timeout);
}
if (this.opened) {
$('.tooltip').removeClass('show');
$('.tooltip .content').empty();
10 years ago
this.opened = false;
}
},
10 years ago
setPosition() {
const sourcePos = $(this.source).offset();
10 years ago
const sourceWidth = $(this.source).outerWidth();
10 years ago
const tip = $('.tooltip');
let top = sourcePos.top - tip.outerHeight() - 5;
10 years ago
let left = sourcePos.left;
left = left + (sourceWidth / 2) - (tip.outerWidth() / 2);
10 years ago
if (left < 0) {
$('.tooltip .tooltip-arrow').css({
'margin-left': `${ left - 5 }px`
});
10 years ago
left = 0;
} else {
$('.tooltip .tooltip-arrow').css({
'margin-left': ''
});
10 years ago
}
if (top < 0) {
top = sourcePos.top + $(this.source).outerHeight() + 5;
tip.addClass('bellow');
} else {
tip.removeClass('bellow');
}
return tip
10 years ago
.css({
top: `${ top }px`,
left: `${ left }px`
10 years ago
});
}
10 years ago
};