2020-09-27 12:36:33 -04:00
|
|
|
// @license magnet:?xt=urn:btih:0b31508aeb0634b347b8270c7bee4d411b5d4109&dn=agpl-3.0.txt AGPLv3-or-later
|
2017-03-14 23:47:02 -04:00
|
|
|
/*
|
2016-05-05 15:10:08 -04:00
|
|
|
* The javascript for friendicas hovercard. Bootstraps popover is needed.
|
2017-03-14 23:47:02 -04:00
|
|
|
*
|
|
|
|
* Much parts of the code are from Hannes Mannerheims <h@nnesmannerhe.im>
|
2016-05-05 15:10:08 -04:00
|
|
|
* qvitter code (https://github.com/hannesmannerheim/qvitter)
|
2017-03-14 23:47:02 -04:00
|
|
|
*
|
2016-05-05 15:10:08 -04:00
|
|
|
* It is licensed under the GNU Affero General Public License <http://www.gnu.org/licenses/>
|
2017-03-14 23:47:02 -04:00
|
|
|
*
|
2016-05-05 15:10:08 -04:00
|
|
|
*/
|
2019-10-16 21:21:49 -04:00
|
|
|
$(document).ready(function () {
|
2021-01-22 08:38:44 -05:00
|
|
|
let $body = $("body");
|
2019-10-29 23:18:17 -04:00
|
|
|
// Prevents normal click action on click hovercard elements
|
2021-01-22 08:38:44 -05:00
|
|
|
$body.on("click", ".userinfo.click-card", function (e) {
|
2019-10-29 23:18:17 -04:00
|
|
|
e.preventDefault();
|
|
|
|
});
|
|
|
|
// This event listener needs to be declared before the one that removes
|
|
|
|
// all cards so that we can stop the immediate propagation of the event
|
|
|
|
// Since the manual popover appears instantly and the hovercard removal is
|
|
|
|
// on a 100ms delay, leaving event propagation immediately hides any click hovercard
|
2021-01-22 08:38:44 -05:00
|
|
|
$body.on("mousedown", ".userinfo.click-card", function (e) {
|
2019-10-29 23:18:17 -04:00
|
|
|
e.stopImmediatePropagation();
|
|
|
|
let timeNow = new Date().getTime();
|
|
|
|
|
|
|
|
let contactUrl = false;
|
|
|
|
let targetElement = $(this);
|
|
|
|
|
|
|
|
// get href-attribute
|
2021-01-22 08:38:44 -05:00
|
|
|
if (targetElement.is("[href]")) {
|
|
|
|
contactUrl = targetElement.attr("href");
|
2019-10-29 23:18:17 -04:00
|
|
|
} else {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// no hovercard for anchor links
|
2021-01-22 08:38:44 -05:00
|
|
|
if (contactUrl.substring(0, 1) === "#") {
|
2019-10-29 23:18:17 -04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
openHovercard(targetElement, contactUrl, timeNow);
|
|
|
|
});
|
|
|
|
|
|
|
|
// hover cards should be removed very easily, e.g. when any of these events happens
|
2021-01-22 08:38:44 -05:00
|
|
|
$body.on("mouseleave touchstart scroll mousedown submit keydown", function (e) {
|
2023-03-21 23:17:26 -04:00
|
|
|
// remove hover card only for desktop user, since on mobile we open the hovercards
|
2019-10-29 23:18:17 -04:00
|
|
|
// by click event insteadof hover
|
|
|
|
removeAllHovercards(e, new Date().getTime());
|
|
|
|
});
|
|
|
|
|
2021-01-22 08:38:44 -05:00
|
|
|
$body
|
|
|
|
.on("mouseover", ".userinfo.hover-card, .wall-item-responses a, .wall-item-bottom .mention a", function (e) {
|
|
|
|
let timeNow = new Date().getTime();
|
|
|
|
removeAllHovercards(e, timeNow);
|
|
|
|
let contactUrl = false;
|
|
|
|
let targetElement = $(this);
|
|
|
|
|
|
|
|
// get href-attribute
|
|
|
|
if (targetElement.is("[href]")) {
|
|
|
|
contactUrl = targetElement.attr("href");
|
|
|
|
} else {
|
|
|
|
return true;
|
|
|
|
}
|
2016-05-07 18:29:33 -04:00
|
|
|
|
2021-01-22 08:38:44 -05:00
|
|
|
// no hover card if the element has the no-hover-card class
|
|
|
|
if (targetElement.hasClass("no-hover-card")) {
|
|
|
|
return true;
|
|
|
|
}
|
2016-05-07 18:29:33 -04:00
|
|
|
|
2021-01-22 08:38:44 -05:00
|
|
|
// no hovercard for anchor links
|
|
|
|
if (contactUrl.substring(0, 1) === "#") {
|
|
|
|
return true;
|
|
|
|
}
|
2016-05-07 18:29:33 -04:00
|
|
|
|
2021-01-22 08:38:44 -05:00
|
|
|
targetElement.attr("data-awaiting-hover-card", timeNow);
|
2016-05-07 18:29:33 -04:00
|
|
|
|
2021-01-22 08:38:44 -05:00
|
|
|
// Delay until the hover-card does appear
|
|
|
|
setTimeout(function () {
|
|
|
|
if (
|
|
|
|
targetElement.is(":hover") &&
|
|
|
|
parseInt(targetElement.attr("data-awaiting-hover-card"), 10) === timeNow &&
|
|
|
|
$(".hovercard").length === 0
|
|
|
|
) {
|
|
|
|
openHovercard(targetElement, contactUrl, timeNow);
|
|
|
|
}
|
|
|
|
}, 500);
|
|
|
|
})
|
|
|
|
.on("mouseleave", ".userinfo.hover-card, .wall-item-responses a, .wall-item-bottom .mention a", function (e) {
|
|
|
|
// action when mouse leaves the hover-card
|
|
|
|
removeAllHovercards(e, new Date().getTime());
|
|
|
|
});
|
2016-05-07 18:29:33 -04:00
|
|
|
|
|
|
|
// if we're hovering a hover card, give it a class, so we don't remove it
|
2021-01-22 08:38:44 -05:00
|
|
|
$body.on("mouseover", ".hovercard", function (e) {
|
|
|
|
$(this).addClass("dont-remove-card");
|
2016-05-07 18:29:33 -04:00
|
|
|
});
|
2019-10-16 21:21:49 -04:00
|
|
|
|
2021-01-22 08:38:44 -05:00
|
|
|
$body.on("mouseleave", ".hovercard", function (e) {
|
|
|
|
$(this).removeClass("dont-remove-card");
|
|
|
|
$(this).popover("hide");
|
2016-05-07 18:29:33 -04:00
|
|
|
});
|
|
|
|
}); // End of $(document).ready
|
2016-05-05 15:10:08 -04:00
|
|
|
|
|
|
|
// removes all hover cards
|
2019-10-16 21:21:49 -04:00
|
|
|
function removeAllHovercards(event, priorTo) {
|
2016-05-05 15:10:08 -04: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-16 21:21:49 -04:00
|
|
|
setTimeout(function () {
|
2021-01-22 08:38:44 -05:00
|
|
|
$.each($(".hovercard"), function () {
|
|
|
|
let title = $(this).attr("data-orig-title");
|
2016-05-06 16:58:26 -04:00
|
|
|
// don't remove card if it was created after removeAllhoverCards() was called
|
2021-01-22 08:38:44 -05:00
|
|
|
if ($(this).data("card-created") < priorTo) {
|
2016-05-06 16:58:26 -04:00
|
|
|
// don't remove it if we're hovering it right now!
|
2021-01-22 08:38:44 -05:00
|
|
|
if (!$(this).hasClass("dont-remove-card")) {
|
|
|
|
let $handle = $('[data-hover-card-active="' + $(this).data("card-created") + '"]');
|
|
|
|
$handle.removeAttr("data-hover-card-active");
|
2019-10-29 23:18:17 -04:00
|
|
|
|
|
|
|
// Restoring the popover handle title
|
2021-01-22 08:38:44 -05:00
|
|
|
let title = $handle.attr("data-orig-title");
|
|
|
|
$handle.attr({ "data-orig-title": "", title: title });
|
2019-10-29 23:18:17 -04:00
|
|
|
|
2021-01-22 08:38:44 -05:00
|
|
|
$(this).popover("hide");
|
2016-05-06 16:58:26 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2019-10-16 21:21:49 -04:00
|
|
|
}, 100);
|
2016-05-05 15:10:08 -04:00
|
|
|
}
|
|
|
|
|
2019-10-29 23:18:17 -04:00
|
|
|
function openHovercard(targetElement, contactUrl, timeNow) {
|
|
|
|
// store the title in a data attribute because Bootstrap
|
|
|
|
// popover destroys the title attribute.
|
2021-01-22 08:38:44 -05:00
|
|
|
let title = targetElement.attr("title");
|
|
|
|
targetElement.attr({ "data-orig-title": title, title: "" });
|
2019-10-29 23:18:17 -04:00
|
|
|
|
2023-03-21 23:16:49 -04:00
|
|
|
// get an additional data attribute if the card is active
|
2021-01-22 08:38:44 -05:00
|
|
|
targetElement.attr("data-hover-card-active", timeNow);
|
2019-10-29 23:18:17 -04:00
|
|
|
// get the whole html content of the hover card and
|
|
|
|
// push it to the bootstrap popover
|
|
|
|
getHoverCardContent(contactUrl, function (data) {
|
|
|
|
if (data) {
|
2021-01-22 08:38:44 -05:00
|
|
|
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
|
|
|
|
let get_position = $(targetElement).offset().top - $(window).scrollTop();
|
|
|
|
if (get_position < 270) {
|
|
|
|
return "bottom";
|
|
|
|
}
|
|
|
|
return "top";
|
|
|
|
},
|
|
|
|
trigger: "manual",
|
|
|
|
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");
|
2019-10-29 23:18:17 -04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-10-16 21:21:49 -04:00
|
|
|
getHoverCardContent.cache = {};
|
|
|
|
|
|
|
|
function getHoverCardContent(contact_url, callback) {
|
|
|
|
let postdata = {
|
|
|
|
url: contact_url,
|
2016-05-05 15:10:08 -04:00
|
|
|
};
|
|
|
|
|
2016-05-06 10:37:04 -04:00
|
|
|
// Normalize and clean the profile so we can use a standardized url
|
|
|
|
// as key for the cache
|
2019-10-16 21:21:49 -04:00
|
|
|
let nurl = cleanContactUrl(contact_url).normalizeLink();
|
2016-05-06 10:37:04 -04:00
|
|
|
|
2019-10-16 21:21:49 -04:00
|
|
|
// If the contact is already in the cache use the cached result instead
|
2016-05-06 10:37:04 -04:00
|
|
|
// of doing a new ajax request
|
2019-10-16 21:21:49 -04:00
|
|
|
if (nurl in getHoverCardContent.cache) {
|
|
|
|
callback(getHoverCardContent.cache[nurl]);
|
2016-05-06 10:37:04 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-05-05 15:10:08 -04:00
|
|
|
$.ajax({
|
2021-01-22 08:38:44 -05:00
|
|
|
url: baseurl + "/contact/hovercard",
|
2016-05-05 15:10:08 -04:00
|
|
|
data: postdata,
|
2019-10-16 21:21:49 -04:00
|
|
|
success: function (data, textStatus, request) {
|
|
|
|
getHoverCardContent.cache[nurl] = data;
|
|
|
|
callback(data);
|
2016-05-05 15:10:08 -04:00
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
2020-09-27 12:36:33 -04:00
|
|
|
// @license-end
|