2017-03-15 04:47:02 +01:00
|
|
|
/*
|
2016-05-05 21:10:08 +02:00
|
|
|
* The javascript for friendicas hovercard. Bootstraps popover is needed.
|
2017-03-15 04:47:02 +01:00
|
|
|
*
|
|
|
|
* Much parts of the code are from Hannes Mannerheims <h@nnesmannerhe.im>
|
2016-05-05 21:10:08 +02:00
|
|
|
* qvitter code (https://github.com/hannesmannerheim/qvitter)
|
2017-03-15 04:47:02 +01:00
|
|
|
*
|
2016-05-05 21:10:08 +02:00
|
|
|
* It is licensed under the GNU Affero General Public License <http://www.gnu.org/licenses/>
|
2017-03-15 04:47:02 +01:00
|
|
|
*
|
2016-05-05 21:10:08 +02:00
|
|
|
*/
|
2019-10-17 03:21:49 +02:00
|
|
|
$(document).ready(function () {
|
2016-05-08 00:29:33 +02:00
|
|
|
// Elements with the class "userinfo" will get a hover-card.
|
|
|
|
// Note that this elements does need a href attribute which links to
|
|
|
|
// a valid profile url
|
2019-10-17 03:21:49 +02:00
|
|
|
$("body").on("mouseover", ".userinfo, .wall-item-responses a, .wall-item-bottom .mention a", function (e) {
|
|
|
|
let timeNow = new Date().getTime();
|
|
|
|
removeAllHovercards(e, timeNow);
|
|
|
|
let contact_url = false;
|
|
|
|
let targetElement = $(this);
|
|
|
|
|
|
|
|
// get href-attribute
|
|
|
|
if (targetElement.is('[href]')) {
|
|
|
|
contact_url = targetElement.attr('href');
|
|
|
|
} else {
|
|
|
|
return true;
|
|
|
|
}
|
2016-05-08 00:29:33 +02:00
|
|
|
|
2019-10-17 03:21:49 +02:00
|
|
|
// no hover card if the element has the no-hover-card class
|
|
|
|
if (targetElement.hasClass('no-hover-card')) {
|
|
|
|
return true;
|
|
|
|
}
|
2016-05-08 00:29:33 +02:00
|
|
|
|
2019-10-17 03:21:49 +02:00
|
|
|
// no hovercard for anchor links
|
|
|
|
if (contact_url.substring(0, 1) === '#') {
|
|
|
|
return true;
|
|
|
|
}
|
2016-05-08 00:29:33 +02:00
|
|
|
|
2019-10-17 03:21:49 +02:00
|
|
|
targetElement.attr('data-awaiting-hover-card', timeNow);
|
2016-05-08 00:29:33 +02:00
|
|
|
|
2019-10-17 03:21:49 +02:00
|
|
|
// store the title in an other data attribute beause bootstrap
|
|
|
|
// popover destroys the title.attribute. We can restore it later
|
|
|
|
let title = targetElement.attr("title");
|
|
|
|
targetElement.attr({"data-orig-title": title, title: ""});
|
2016-05-08 00:29:33 +02:00
|
|
|
|
2019-10-17 03:21:49 +02:00
|
|
|
// if the device is a mobile open the hover card by click and not by hover
|
|
|
|
if (typeof is_mobile != "undefined") {
|
|
|
|
targetElement[0].removeAttribute("href");
|
|
|
|
var hctrigger = 'click';
|
|
|
|
} else {
|
|
|
|
var hctrigger = 'manual';
|
|
|
|
}
|
2016-05-28 13:07:24 +02:00
|
|
|
|
2019-10-17 03:21:49 +02:00
|
|
|
// Timeout until the hover-card does appear
|
|
|
|
setTimeout(function () {
|
|
|
|
if (
|
|
|
|
targetElement.is(":hover")
|
|
|
|
&& parseInt(targetElement.attr('data-awaiting-hover-card'), 10) === timeNow
|
|
|
|
&& $('.hovercard').length === 0
|
|
|
|
) { // no card if there already is one open
|
|
|
|
// get an additional data atribute if the card is active
|
|
|
|
targetElement.attr('data-hover-card-active', timeNow);
|
|
|
|
// get the whole html content of the hover card and
|
|
|
|
// push it to the bootstrap popover
|
|
|
|
getHoverCardContent(contact_url, function (data) {
|
|
|
|
if (data) {
|
|
|
|
targetElement.popover({
|
|
|
|
html: true,
|
|
|
|
placement: function () {
|
|
|
|
// Calculate the placement of the the hovercard (if top or bottom)
|
|
|
|
// The placement depence on the distance between window top and the element
|
|
|
|
// which triggers the hover-card
|
|
|
|
var get_position = $(targetElement).offset().top - $(window).scrollTop();
|
|
|
|
if (get_position < 270) {
|
|
|
|
return "bottom";
|
|
|
|
}
|
|
|
|
return "top";
|
|
|
|
},
|
|
|
|
trigger: hctrigger,
|
|
|
|
template: '<div class="popover hovercard" data-card-created="' + timeNow + '"><div class="arrow"></div><div class="popover-content hovercard-content"></div></div>',
|
|
|
|
content: data,
|
|
|
|
container: "body",
|
|
|
|
sanitizeFn: function (content) {
|
|
|
|
return DOMPurify.sanitize(content)
|
|
|
|
},
|
|
|
|
}).popover('show');
|
2016-05-08 00:29:33 +02:00
|
|
|
}
|
2019-10-17 03:21:49 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}, 500);
|
|
|
|
}).on("mouseleave", ".userinfo, .wall-item-responses a, .wall-item-bottom .mention a", function (e) { // action when mouse leaves the hover-card
|
2016-05-06 20:48:12 +02:00
|
|
|
var timeNow = new Date().getTime();
|
2016-05-08 00:29:33 +02:00
|
|
|
// copy the original title to the title atribute
|
|
|
|
var title = $(this).attr("data-orig-title");
|
|
|
|
$(this).attr({"data-orig-title": "", title: title});
|
2019-10-17 03:21:49 +02:00
|
|
|
removeAllHovercards(e, timeNow);
|
2016-05-08 00:29:33 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
// hover cards should be removed very easily, e.g. when any of these events happen
|
2019-10-17 03:21:49 +02:00
|
|
|
$('body').on("mouseleave touchstart scroll click dblclick mousedown mouseup submit keydown keypress keyup", function (e) {
|
2016-05-28 13:07:24 +02:00
|
|
|
// remove hover card only for desktiop user, since on mobile we openen the hovercards
|
|
|
|
// by click event insteadof hover
|
2019-10-17 03:21:49 +02:00
|
|
|
if (typeof is_mobile == "undefined") {
|
2016-05-28 13:07:24 +02:00
|
|
|
var timeNow = new Date().getTime();
|
2019-10-17 03:21:49 +02:00
|
|
|
removeAllHovercards(e, timeNow);
|
|
|
|
}
|
2016-05-08 00:29:33 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
// if we're hovering a hover card, give it a class, so we don't remove it
|
2019-10-17 03:21:49 +02:00
|
|
|
$('body').on('mouseover', '.hovercard', function (e) {
|
2016-05-08 00:29:33 +02:00
|
|
|
$(this).addClass('dont-remove-card');
|
|
|
|
});
|
2019-10-17 03:21:49 +02:00
|
|
|
|
|
|
|
$('body').on('mouseleave', '.hovercard', function (e) {
|
2016-05-08 00:29:33 +02:00
|
|
|
$(this).removeClass('dont-remove-card');
|
|
|
|
$(this).popover("hide");
|
|
|
|
});
|
|
|
|
}); // End of $(document).ready
|
2016-05-05 21:10:08 +02:00
|
|
|
|
|
|
|
// removes all hover cards
|
2019-10-17 03:21:49 +02:00
|
|
|
function removeAllHovercards(event, priorTo) {
|
2016-05-05 21:10:08 +02:00
|
|
|
// don't remove hovercards until after 100ms, so user have time to move the cursor to it (which gives it the dont-remove-card class)
|
2019-10-17 03:21:49 +02:00
|
|
|
setTimeout(function () {
|
|
|
|
$.each($('.hovercard'), function () {
|
2016-05-06 22:58:26 +02:00
|
|
|
var title = $(this).attr("data-orig-title");
|
|
|
|
// don't remove card if it was created after removeAllhoverCards() was called
|
2019-10-17 03:21:49 +02:00
|
|
|
if ($(this).data('card-created') < priorTo) {
|
2016-05-06 22:58:26 +02:00
|
|
|
// don't remove it if we're hovering it right now!
|
2019-10-17 03:21:49 +02:00
|
|
|
if (!$(this).hasClass('dont-remove-card')) {
|
2016-05-06 22:58:26 +02:00
|
|
|
$('[data-hover-card-active="' + $(this).data('card-created') + '"]').removeAttr('data-hover-card-active');
|
|
|
|
$(this).popover("hide");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2019-10-17 03:21:49 +02:00
|
|
|
}, 100);
|
2016-05-05 21:10:08 +02:00
|
|
|
}
|
|
|
|
|
2019-10-17 03:21:49 +02:00
|
|
|
getHoverCardContent.cache = {};
|
|
|
|
|
|
|
|
function getHoverCardContent(contact_url, callback) {
|
|
|
|
let postdata = {
|
|
|
|
url: contact_url,
|
2016-05-05 21:10:08 +02:00
|
|
|
};
|
|
|
|
|
2016-05-06 16:37:04 +02:00
|
|
|
// Normalize and clean the profile so we can use a standardized url
|
|
|
|
// as key for the cache
|
2019-10-17 03:21:49 +02:00
|
|
|
let nurl = cleanContactUrl(contact_url).normalizeLink();
|
2016-05-06 16:37:04 +02:00
|
|
|
|
2019-10-17 03:21:49 +02:00
|
|
|
// If the contact is already in the cache use the cached result instead
|
2016-05-06 16:37:04 +02:00
|
|
|
// of doing a new ajax request
|
2019-10-17 03:21:49 +02:00
|
|
|
if (nurl in getHoverCardContent.cache) {
|
|
|
|
callback(getHoverCardContent.cache[nurl]);
|
2016-05-06 16:37:04 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-05-05 21:10:08 +02:00
|
|
|
$.ajax({
|
2019-10-17 03:21:49 +02:00
|
|
|
url: baseurl + "/contact/hovercard",
|
2016-05-05 21:10:08 +02:00
|
|
|
data: postdata,
|
2019-10-17 03:21:49 +02:00
|
|
|
success: function (data, textStatus, request) {
|
|
|
|
getHoverCardContent.cache[nurl] = data;
|
|
|
|
callback(data);
|
2016-05-05 21:10:08 +02:00
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|