2021-03-24 18:41:55 +01:00
|
|
|
import $ from "jquery";
|
2022-04-23 08:59:40 +02:00
|
|
|
import _ from "lodash";
|
2021-02-25 02:33:15 +01:00
|
|
|
import tippy, {delegate} from "tippy.js";
|
|
|
|
|
2022-03-17 23:35:27 +01:00
|
|
|
import render_message_inline_image_tooltip from "../templates/message_inline_image_tooltip.hbs";
|
2022-04-23 08:59:40 +02:00
|
|
|
import render_narrow_to_compose_recipients_tooltip from "../templates/narrow_to_compose_recipients_tooltip.hbs";
|
2022-03-17 23:35:27 +01:00
|
|
|
|
2022-04-23 08:59:40 +02:00
|
|
|
import * as common from "./common";
|
|
|
|
import * as compose_state from "./compose_state";
|
2021-12-10 09:07:42 +01:00
|
|
|
import {$t} from "./i18n";
|
2021-06-25 19:52:04 +02:00
|
|
|
import * as message_lists from "./message_lists";
|
2022-04-23 08:59:40 +02:00
|
|
|
import * as narrow_state from "./narrow_state";
|
2021-12-10 09:07:42 +01:00
|
|
|
import * as popover_menus from "./popover_menus";
|
2021-03-24 18:41:55 +01:00
|
|
|
import * as reactions from "./reactions";
|
2022-04-23 08:59:40 +02:00
|
|
|
import * as recent_topics_util from "./recent_topics_util";
|
2021-03-24 18:41:55 +01:00
|
|
|
import * as rows from "./rows";
|
2021-06-25 19:52:04 +02:00
|
|
|
import * as timerender from "./timerender";
|
2022-03-17 23:35:27 +01:00
|
|
|
import {parse_html} from "./ui_util";
|
2021-03-24 18:41:55 +01:00
|
|
|
|
2022-03-02 23:06:33 +01:00
|
|
|
// For tooltips without data-tippy-content, we use the HTML content of
|
|
|
|
// a <template> whose id is given by data-tooltip-template-id.
|
|
|
|
function get_tooltip_content(reference) {
|
|
|
|
if ("tooltipTemplateId" in reference.dataset) {
|
|
|
|
const template = document.querySelector(
|
|
|
|
`template#${CSS.escape(reference.dataset.tooltipTemplateId)}`,
|
|
|
|
);
|
|
|
|
return template.content.cloneNode(true);
|
|
|
|
}
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2022-07-28 06:55:12 +02:00
|
|
|
// Defining observer outside ensures that at max only one observer is active at all times.
|
|
|
|
let observer;
|
|
|
|
function hide_tooltip_if_reference_removed(
|
|
|
|
target_node,
|
|
|
|
config,
|
|
|
|
instance,
|
|
|
|
nodes_to_check_for_removal,
|
|
|
|
) {
|
|
|
|
// Use MutationObserver to check for removal of nodes on which tooltips
|
|
|
|
// are still active.
|
|
|
|
if (!target_node) {
|
|
|
|
// The tooltip reference 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;
|
|
|
|
}
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
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",
|
|
|
|
|
2022-03-02 23:06:33 +01:00
|
|
|
// To add a text tooltip, override this by setting data-tippy-content.
|
|
|
|
// To add an HTML tooltip, set data-tooltip-template-id to the id of a <template>.
|
|
|
|
// Or, override this with a function returning string (text) or DocumentFragment (HTML).
|
|
|
|
content: get_tooltip_content,
|
2021-02-25 02:33:15 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
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) {
|
2022-01-25 11:36:19 +01:00
|
|
|
const $elem = $(instance.reference);
|
2021-06-09 14:53:10 +02:00
|
|
|
if (!instance.reference.classList.contains("reaction_button")) {
|
2022-01-25 11:36:19 +01:00
|
|
|
const local_id = $elem.attr("data-reaction-id");
|
2021-06-09 14:53:10 +02:00
|
|
|
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
|
|
|
|
2022-07-28 06:55:12 +02:00
|
|
|
const config = {attributes: false, childList: true, subtree: true};
|
|
|
|
const target = $elem.parents(".message_table.focused_table").get(0);
|
2021-06-09 14:35:52 +02:00
|
|
|
const nodes_to_check_for_removal = [
|
2022-01-25 11:36:19 +01:00
|
|
|
$elem.parents(".recipient_row").get(0),
|
|
|
|
$elem.parents(".message_reactions").get(0),
|
|
|
|
$elem.get(0),
|
2021-06-09 14:35:52 +02:00
|
|
|
];
|
2022-07-28 06:55:12 +02:00
|
|
|
hide_tooltip_if_reference_removed(target, config, instance, nodes_to_check_for_removal);
|
2021-06-09 14:35:52 +02:00
|
|
|
},
|
|
|
|
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-12-28 00:37:04 +01:00
|
|
|
// This ensures that the upload files tooltip
|
|
|
|
// doesn't hide behind the left sidebar.
|
|
|
|
appendTo: () => document.body,
|
2021-04-15 12:57:12 +02:00
|
|
|
});
|
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.
|
2022-01-25 11:36:19 +01:00
|
|
|
const $elem = $(instance.reference);
|
|
|
|
let content = $elem.attr("data-tippy-content");
|
2021-05-13 19:16:36 +02:00
|
|
|
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.
|
2022-01-25 11:36:19 +01:00
|
|
|
const $edit_button = $elem.find("i.edit_content_button");
|
|
|
|
content = $edit_button.attr("data-tippy-content");
|
2021-05-13 19:16:36 +02:00
|
|
|
}
|
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
|
|
|
|
2022-01-25 08:05:18 +01:00
|
|
|
$("body").on("blur", ".message_control_button", (e) => {
|
|
|
|
// Remove tooltip when user is trying to tab through all the icons.
|
|
|
|
// If user tabs slowly, tooltips are displayed otherwise they are
|
2022-02-08 00:13:33 +01:00
|
|
|
// destroyed before they can be displayed.
|
2022-01-25 08:05:18 +01:00
|
|
|
e.currentTarget._tippy.destroy();
|
|
|
|
});
|
|
|
|
|
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) {
|
2022-01-25 11:36:19 +01:00
|
|
|
const $time_elem = $(instance.reference);
|
|
|
|
const $row = $time_elem.closest(".message_row");
|
|
|
|
const message = message_lists.current.get(rows.id($row));
|
2021-06-25 19:52:04 +02:00
|
|
|
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", {
|
2022-02-14 17:43:32 +01:00
|
|
|
target: [
|
|
|
|
".recipient_bar_icon",
|
|
|
|
".sidebar-title",
|
|
|
|
"#user_filter_icon",
|
|
|
|
"#scroll-to-bottom-button-clickable-area",
|
2022-09-19 07:44:21 +02:00
|
|
|
".code_external_link",
|
2022-02-14 17:43:32 +01:00
|
|
|
],
|
2021-06-26 20:21:25 +02:00
|
|
|
appendTo: () => document.body,
|
|
|
|
});
|
2021-07-01 19:45:02 +02:00
|
|
|
|
2022-03-02 23:06:33 +01:00
|
|
|
delegate("body", {
|
|
|
|
target: ".rendered_markdown time",
|
|
|
|
content: timerender.get_markdown_time_tooltip,
|
|
|
|
appendTo: () => document.body,
|
|
|
|
onHidden(instance) {
|
|
|
|
instance.destroy();
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2021-11-20 11:51:35 +01:00
|
|
|
delegate("body", {
|
|
|
|
target: "#stream-specific-notify-table .unmute_stream",
|
|
|
|
appendTo: () => document.body,
|
|
|
|
});
|
|
|
|
|
2021-07-01 19:45:02 +02:00
|
|
|
delegate("body", {
|
2022-02-01 14:05:16 +01:00
|
|
|
target: [
|
|
|
|
".rendered_markdown .copy_codeblock",
|
|
|
|
"#compose_top_right [data-tippy-content]",
|
2022-03-02 23:06:33 +01:00
|
|
|
"#compose_top_right [data-tooltip-template-id]",
|
2022-02-01 14:05:16 +01:00
|
|
|
],
|
2021-07-01 19:45:02 +02:00
|
|
|
appendTo: () => document.body,
|
|
|
|
onHidden(instance) {
|
|
|
|
instance.destroy();
|
|
|
|
},
|
|
|
|
});
|
2021-12-10 09:07:42 +01:00
|
|
|
|
2022-04-23 08:59:40 +02:00
|
|
|
delegate("body", {
|
|
|
|
target: ".narrow_to_compose_recipients",
|
|
|
|
appendTo: () => document.body,
|
|
|
|
content() {
|
|
|
|
const narrow_filter = narrow_state.filter();
|
|
|
|
let display_current_view;
|
|
|
|
if (!recent_topics_util.is_visible()) {
|
|
|
|
if (narrow_filter === undefined) {
|
|
|
|
display_current_view = $t({defaultMessage: "Currently viewing all messages."});
|
|
|
|
} else if (
|
|
|
|
_.isEqual(narrow_filter.sorted_term_types(), ["stream"]) &&
|
|
|
|
compose_state.get_message_type() === "stream" &&
|
|
|
|
narrow_filter.operands("stream")[0] === compose_state.stream_name()
|
|
|
|
) {
|
|
|
|
display_current_view = $t({
|
|
|
|
defaultMessage: "Currently viewing the entire stream.",
|
|
|
|
});
|
|
|
|
} else if (
|
|
|
|
_.isEqual(narrow_filter.sorted_term_types(), ["is-private"]) &&
|
|
|
|
compose_state.get_message_type() === "private"
|
|
|
|
) {
|
|
|
|
display_current_view = $t({
|
|
|
|
defaultMessage: "Currently viewing all private messages.",
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const shortcut_html = (common.has_mac_keyboard() ? "⌘" : "Ctrl") + " + .";
|
|
|
|
return parse_html(
|
|
|
|
render_narrow_to_compose_recipients_tooltip({shortcut_html, display_current_view}),
|
|
|
|
);
|
|
|
|
},
|
|
|
|
onHidden(instance) {
|
|
|
|
instance.destroy();
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2021-12-10 09:07:42 +01:00
|
|
|
delegate("body", {
|
|
|
|
target: [".enter_sends_true", ".enter_sends_false"],
|
|
|
|
content: $t({defaultMessage: "Change send shortcut"}),
|
|
|
|
onShow() {
|
|
|
|
// Don't show tooltip if the popover is displayed.
|
|
|
|
if (popover_menus.compose_enter_sends_popover_displayed) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
},
|
2022-02-08 05:22:16 +01:00
|
|
|
appendTo: () => document.body,
|
2021-12-10 09:07:42 +01:00
|
|
|
});
|
2022-03-17 23:35:27 +01:00
|
|
|
|
|
|
|
delegate("body", {
|
|
|
|
target: ".message_inline_image > a > img",
|
|
|
|
appendTo: () => document.body,
|
|
|
|
// Add a short delay so the user can mouseover several inline images without
|
|
|
|
// tooltips showing and hiding rapidly
|
|
|
|
delay: [300, 20],
|
|
|
|
onShow(instance) {
|
|
|
|
// Some message_inline_images aren't actually images with a title,
|
|
|
|
// for example youtube videos, so we default to the actual href
|
|
|
|
const title =
|
|
|
|
$(instance.reference).parent().attr("aria-label") ||
|
|
|
|
$(instance.reference).parent().attr("href");
|
|
|
|
instance.setContent(parse_html(render_message_inline_image_tooltip({title})));
|
2022-07-28 06:56:16 +02:00
|
|
|
|
|
|
|
const target_node = $(instance.reference)
|
|
|
|
.parents(".message_table.focused_table")
|
|
|
|
.get(0);
|
|
|
|
const config = {attributes: false, childList: true, subtree: false};
|
|
|
|
const nodes_to_check_for_removal = [
|
|
|
|
$(instance.reference).parents(".message_inline_image").get(0),
|
|
|
|
];
|
|
|
|
hide_tooltip_if_reference_removed(
|
|
|
|
target_node,
|
|
|
|
config,
|
|
|
|
instance,
|
|
|
|
nodes_to_check_for_removal,
|
|
|
|
);
|
2022-03-17 23:35:27 +01:00
|
|
|
},
|
|
|
|
onHidden(instance) {
|
|
|
|
instance.destroy();
|
|
|
|
},
|
|
|
|
});
|
2022-03-18 00:38:16 +01:00
|
|
|
|
|
|
|
delegate("body", {
|
|
|
|
target: ".image-info-wrapper > .image-description > .title",
|
|
|
|
appendTo: () => document.body,
|
|
|
|
onShow(instance) {
|
|
|
|
const title = $(instance.reference).attr("aria-label");
|
|
|
|
const filename = $(instance.reference).prop("data-filename");
|
|
|
|
const $markup = $("<span>").text(title);
|
|
|
|
if (title !== filename) {
|
|
|
|
// If the image title is the same as the filename, there's no reason
|
|
|
|
// to show this next line.
|
|
|
|
const second_line = $t({defaultMessage: "File name: {filename}"}, {filename});
|
|
|
|
$markup.append($("<br>"), $("<span>").text(second_line));
|
|
|
|
}
|
|
|
|
instance.setContent($markup[0]);
|
|
|
|
},
|
|
|
|
onHidden(instance) {
|
|
|
|
instance.destroy();
|
|
|
|
},
|
|
|
|
});
|
2022-03-16 05:50:25 +01:00
|
|
|
|
|
|
|
delegate("body", {
|
|
|
|
// Configure tooltips for the stream_sorter_toggle buttons.
|
|
|
|
|
|
|
|
// TODO: Ideally, we'd extend this to be a common mechanism for
|
|
|
|
// tab switchers, with the strings living in a more normal configuration
|
|
|
|
// location.
|
|
|
|
target: ".stream_sorter_toggle .ind-tab [data-tippy-content]",
|
|
|
|
|
|
|
|
// Adjust their placement to `bottom`.
|
|
|
|
placement: "bottom",
|
|
|
|
|
|
|
|
// Avoid inheriting `position: relative` CSS on the stream sorter widget.
|
|
|
|
appendTo: () => document.body,
|
|
|
|
});
|
2022-08-17 14:16:15 +02:00
|
|
|
|
|
|
|
delegate("body", {
|
|
|
|
// This tooltip appears on the "Summary" checkboxes in
|
|
|
|
// settings > custom profile fields, when at the limit of 2
|
|
|
|
// fields with display_in_profile_summary enabled.
|
|
|
|
target: [
|
|
|
|
"#profile-field-settings .display_in_profile_summary_tooltip",
|
|
|
|
"#edit-custom-profile-field-form-modal .display_in_profile_summary_tooltip",
|
|
|
|
"#add-new-custom-profile-field-form .display_in_profile_summary_tooltip",
|
|
|
|
],
|
|
|
|
content: $t({
|
|
|
|
defaultMessage: "Only 2 custom profile fields can be displayed in the profile summary.",
|
|
|
|
}),
|
|
|
|
appendTo: () => document.body,
|
|
|
|
onTrigger(instance) {
|
|
|
|
// Sometimes just removing class is not enough to destroy/remove tooltip, especially in
|
|
|
|
// "Add a new custom profile field" form, so here we are manually calling `destroy()`.
|
|
|
|
if (!instance.reference.classList.contains("display_in_profile_summary_tooltip")) {
|
|
|
|
instance.destroy();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
2021-02-25 02:33:15 +01:00
|
|
|
}
|