2021-03-24 18:41:55 +01:00
|
|
|
import $ from "jquery";
|
2021-02-25 02:33:15 +01:00
|
|
|
import tippy, {delegate} from "tippy.js";
|
|
|
|
|
2021-06-25 19:52:04 +02:00
|
|
|
import * as message_lists from "./message_lists";
|
2021-03-24 18:41:55 +01:00
|
|
|
import * as reactions from "./reactions";
|
|
|
|
import * as rows from "./rows";
|
2021-06-25 19:52:04 +02:00
|
|
|
import * as timerender from "./timerender";
|
2021-03-24 18:41:55 +01:00
|
|
|
|
2021-02-25 02:33:15 +01:00
|
|
|
// We override the defaults set by tippy library here,
|
|
|
|
// so make sure to check this too after checking tippyjs
|
|
|
|
// documentation for default properties.
|
|
|
|
tippy.setDefaultProps({
|
|
|
|
// We don't want tooltips
|
|
|
|
// to take more space than
|
|
|
|
// mobile widths ever.
|
|
|
|
maxWidth: 300,
|
|
|
|
|
|
|
|
// Some delay to showing / hiding the tooltip makes
|
|
|
|
// it look less forced and more natural.
|
|
|
|
delay: [100, 20],
|
2021-07-07 06:49:36 +02:00
|
|
|
placement: "top",
|
2021-02-25 02:33:15 +01:00
|
|
|
|
|
|
|
// disable animations to make the
|
|
|
|
// tooltips feel snappy
|
|
|
|
animation: false,
|
|
|
|
|
|
|
|
// Show tooltips on long press on touch based
|
|
|
|
// devices.
|
|
|
|
touch: ["hold", 750],
|
|
|
|
|
2021-04-10 19:20:51 +02:00
|
|
|
// This has the side effect of some properties of parent applying to
|
|
|
|
// tooltips.
|
|
|
|
appendTo: "parent",
|
|
|
|
|
2021-02-25 02:33:15 +01:00
|
|
|
// html content is not supported by default
|
|
|
|
// enable it by passing data-tippy-allowHtml="true"
|
|
|
|
// in the tag or a parameter.
|
|
|
|
});
|
|
|
|
|
|
|
|
export function initialize() {
|
2021-06-26 01:33:16 +02:00
|
|
|
// Our default tooltip configuration. For this, one simply needs to:
|
|
|
|
// * Set `class="tippy-zulip-tooltip"` on an element for enable this.
|
|
|
|
// * Set `data-tippy-content="{{t 'Tooltip content' }}"`, often
|
|
|
|
// replacing a `title` attribute on an element that had both.
|
|
|
|
// * Set placement; we typically use `data-tippy-placement="top"`.
|
2021-02-25 02:33:15 +01:00
|
|
|
delegate("body", {
|
|
|
|
target: ".tippy-zulip-tooltip",
|
|
|
|
});
|
2021-06-26 01:33:16 +02:00
|
|
|
|
|
|
|
// The below definitions are for specific tooltips that require
|
|
|
|
// custom JavaScript code or configuration. Note that since the
|
|
|
|
// below specify the target directly, elements using those should
|
|
|
|
// not have the tippy-zulip-tooltip class.
|
2021-03-24 18:41:55 +01:00
|
|
|
|
|
|
|
// message reaction tooltip showing who reacted.
|
2021-06-09 14:35:52 +02:00
|
|
|
let observer;
|
2021-03-24 18:41:55 +01:00
|
|
|
delegate("body", {
|
2021-06-13 21:25:37 +02:00
|
|
|
target: ".message_reaction, .message_reactions .reaction_button",
|
2021-04-08 11:28:45 +02:00
|
|
|
placement: "bottom",
|
2021-03-24 18:41:55 +01:00
|
|
|
onShow(instance) {
|
|
|
|
const elem = $(instance.reference);
|
2021-06-09 14:53:10 +02:00
|
|
|
if (!instance.reference.classList.contains("reaction_button")) {
|
|
|
|
const local_id = elem.attr("data-reaction-id");
|
|
|
|
const message_id = rows.get_message_id(instance.reference);
|
|
|
|
const title = reactions.get_reaction_title_data(message_id, local_id);
|
|
|
|
instance.setContent(title);
|
|
|
|
}
|
2021-06-09 14:35:52 +02:00
|
|
|
|
|
|
|
// Use MutationObserver to check for removal of nodes on which tooltips
|
|
|
|
// are still active.
|
|
|
|
// We target the message table and check for removal of it, it's children
|
|
|
|
// and the reactions individually down in the subtree.
|
|
|
|
const target_node = elem.parents(".message_table.focused_table").get(0);
|
2021-06-23 18:10:35 +02:00
|
|
|
if (!target_node) {
|
|
|
|
// The `reaction` was removed from DOM before we reached here.
|
|
|
|
// In that case, we simply hide the tooltip.
|
|
|
|
// We have to be smart about hiding the instance, so we hide it as soon
|
|
|
|
// as it is displayed.
|
|
|
|
setTimeout(instance.hide, 0);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-06-09 14:35:52 +02:00
|
|
|
const nodes_to_check_for_removal = [
|
|
|
|
elem.parents(".recipient_row").get(0),
|
|
|
|
elem.parents(".message_reactions").get(0),
|
|
|
|
elem.get(0),
|
|
|
|
];
|
|
|
|
const config = {attributes: false, childList: true, subtree: true};
|
|
|
|
|
|
|
|
const callback = function (mutationsList) {
|
|
|
|
for (const mutation of mutationsList) {
|
|
|
|
for (const node of nodes_to_check_for_removal) {
|
|
|
|
// Hide instance if reference is in the removed node list.
|
|
|
|
if (Array.prototype.includes.call(mutation.removedNodes, node)) {
|
|
|
|
instance.hide();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
observer = new MutationObserver(callback);
|
|
|
|
observer.observe(target_node, config);
|
|
|
|
},
|
|
|
|
onHidden(instance) {
|
|
|
|
instance.destroy();
|
2021-06-23 18:10:35 +02:00
|
|
|
if (observer) {
|
|
|
|
observer.disconnect();
|
|
|
|
}
|
2021-03-24 18:41:55 +01:00
|
|
|
},
|
2021-06-09 14:35:52 +02:00
|
|
|
appendTo: () => document.body,
|
2021-03-24 18:41:55 +01:00
|
|
|
});
|
2021-04-15 12:57:12 +02:00
|
|
|
|
|
|
|
delegate("body", {
|
|
|
|
target: ".compose_control_button",
|
|
|
|
// Add some additional delay when they open
|
|
|
|
// so that regular users don't have to see
|
|
|
|
// them unless they want to.
|
|
|
|
delay: [300, 20],
|
|
|
|
});
|
2021-05-13 19:16:36 +02:00
|
|
|
|
|
|
|
delegate("body", {
|
|
|
|
target: ".message_control_button",
|
2021-07-09 00:19:34 +02:00
|
|
|
// This ensures that the tooltip doesn't
|
|
|
|
// hide by the selected message blue border.
|
|
|
|
appendTo: () => document.body,
|
2021-05-13 19:16:36 +02:00
|
|
|
// Add some additional delay when they open
|
|
|
|
// so that regular users don't have to see
|
|
|
|
// them unless they want to.
|
|
|
|
delay: [300, 20],
|
|
|
|
onShow(instance) {
|
|
|
|
// Handle dynamic "starred messages" and "edit" widgets.
|
|
|
|
const elem = $(instance.reference);
|
|
|
|
let content = elem.attr("data-tippy-content");
|
|
|
|
if (content === undefined) {
|
|
|
|
// Tippy cannot get the content for message edit button
|
|
|
|
// as it is dynamically inserted based on editability.
|
|
|
|
// So, we have to manually get the i element to get the
|
|
|
|
// content from it.
|
|
|
|
//
|
|
|
|
// TODO: Change the template structure so logic is unnecessary.
|
2021-05-20 22:28:16 +02:00
|
|
|
const edit_button = elem.find("i.edit_content_button");
|
2021-05-13 19:16:36 +02:00
|
|
|
content = edit_button.attr("data-tippy-content");
|
|
|
|
}
|
2021-07-17 15:47:31 +02:00
|
|
|
|
2021-05-13 19:16:36 +02:00
|
|
|
instance.setContent(content);
|
2021-05-20 22:36:56 +02:00
|
|
|
return true;
|
2021-05-13 19:16:36 +02:00
|
|
|
},
|
|
|
|
});
|
2021-06-12 22:10:34 +02:00
|
|
|
|
|
|
|
delegate("body", {
|
2021-07-19 10:09:18 +02:00
|
|
|
target: ".message_table .message_time",
|
2021-06-12 22:10:34 +02:00
|
|
|
appendTo: () => document.body,
|
2021-06-25 19:52:04 +02:00
|
|
|
onShow(instance) {
|
|
|
|
const time_elem = $(instance.reference);
|
|
|
|
const row = time_elem.closest(".message_row");
|
|
|
|
const message = message_lists.current.get(rows.id(row));
|
|
|
|
const time = new Date(message.timestamp * 1000);
|
2021-06-27 19:16:36 +02:00
|
|
|
instance.setContent(timerender.get_full_datetime(time));
|
2021-06-25 19:52:04 +02:00
|
|
|
},
|
2021-06-12 22:10:34 +02:00
|
|
|
onHidden(instance) {
|
|
|
|
instance.destroy();
|
|
|
|
},
|
|
|
|
});
|
2021-06-23 22:16:15 +02:00
|
|
|
|
|
|
|
delegate("body", {
|
|
|
|
target: ".recipient_row_date > span",
|
|
|
|
appendTo: () => document.body,
|
|
|
|
onHidden(instance) {
|
|
|
|
instance.destroy();
|
|
|
|
},
|
|
|
|
});
|
2021-06-26 20:21:25 +02:00
|
|
|
|
|
|
|
// In case of recipient bar icons, following change
|
|
|
|
// ensures that tooltip doesn't hide behind the message
|
|
|
|
// box or it is not limited by the parent container.
|
|
|
|
delegate("body", {
|
2021-07-06 05:28:25 +02:00
|
|
|
target: [".recipient_bar_icon", ".sidebar-title", "#user_filter_icon"],
|
2021-06-26 20:21:25 +02:00
|
|
|
appendTo: () => document.body,
|
|
|
|
});
|
2021-07-01 19:45:02 +02:00
|
|
|
|
|
|
|
delegate("body", {
|
|
|
|
target: ".rendered_markdown time",
|
|
|
|
allowHTML: true,
|
|
|
|
appendTo: () => document.body,
|
|
|
|
onHidden(instance) {
|
|
|
|
instance.destroy();
|
|
|
|
},
|
|
|
|
});
|
2021-02-25 02:33:15 +01:00
|
|
|
}
|