2020-01-25 16:56:08 +08:00
|
|
|
import treeService from "./tree.js";
|
2018-03-27 10:29:14 +08:00
|
|
|
import linkService from "./link.js";
|
2020-01-25 21:37:12 +08:00
|
|
|
import treeCache from "./tree_cache.js";
|
2018-03-27 10:29:14 +08:00
|
|
|
|
2018-12-23 03:57:09 +08:00
|
|
|
function setupGlobalTooltip() {
|
|
|
|
$(document).on("mouseenter", "a", mouseEnterHandler);
|
|
|
|
$(document).on("mouseleave", "a", mouseLeaveHandler);
|
2018-08-16 00:22:02 +08:00
|
|
|
|
2019-04-20 15:39:39 +08:00
|
|
|
// close any note tooltip after click, this fixes the problem that sometimes tooltips remained on the screen
|
|
|
|
$(document).on("click", () => $('.note-tooltip').remove());
|
2018-12-23 03:57:09 +08:00
|
|
|
}
|
2018-08-16 00:22:02 +08:00
|
|
|
|
2018-12-23 03:57:09 +08:00
|
|
|
function setupElementTooltip($el) {
|
|
|
|
$el.on('mouseenter', mouseEnterHandler);
|
|
|
|
$el.on('mouseleave', mouseLeaveHandler);
|
|
|
|
}
|
2018-10-07 04:00:43 +08:00
|
|
|
|
2018-12-23 03:57:09 +08:00
|
|
|
async function mouseEnterHandler() {
|
|
|
|
const $link = $(this);
|
2018-07-28 23:59:55 +08:00
|
|
|
|
2019-11-02 03:00:56 +08:00
|
|
|
if ($link.hasClass("no-tooltip-preview")
|
|
|
|
|| $link.hasClass("disabled")
|
|
|
|
|| $link.attr("data-action") === 'note-revision') {
|
2018-12-23 03:57:09 +08:00
|
|
|
return;
|
|
|
|
}
|
2018-11-07 02:35:42 +08:00
|
|
|
|
2018-12-23 03:57:09 +08:00
|
|
|
// this is to avoid showing tooltip from inside CKEditor link editor dialog
|
|
|
|
if ($link.closest(".ck-link-actions").length) {
|
|
|
|
return;
|
|
|
|
}
|
2018-03-27 10:29:14 +08:00
|
|
|
|
2018-12-23 03:57:09 +08:00
|
|
|
let notePath = linkService.getNotePathFromUrl($link.attr("href"));
|
2018-03-27 10:29:14 +08:00
|
|
|
|
2018-12-23 03:57:09 +08:00
|
|
|
if (!notePath) {
|
|
|
|
notePath = $link.attr("data-note-path");
|
|
|
|
}
|
2018-08-22 21:31:36 +08:00
|
|
|
|
2018-12-23 03:57:09 +08:00
|
|
|
if (!notePath) {
|
|
|
|
return;
|
|
|
|
}
|
2018-11-07 02:35:42 +08:00
|
|
|
|
2020-01-25 16:56:08 +08:00
|
|
|
const noteId = treeService.getNoteIdFromNotePath(notePath);
|
2018-11-07 02:35:42 +08:00
|
|
|
|
2020-01-25 21:37:12 +08:00
|
|
|
const note = await treeCache.getNote(noteId);
|
2020-02-02 01:29:18 +08:00
|
|
|
const noteComplement = await treeCache.getNoteComplement(noteId);
|
2018-11-07 02:35:42 +08:00
|
|
|
|
2020-02-01 18:15:58 +08:00
|
|
|
const html = await renderTooltip(note, noteComplement);
|
2018-11-14 21:52:05 +08:00
|
|
|
|
2018-12-23 03:57:09 +08:00
|
|
|
// we need to check if we're still hovering over the element
|
|
|
|
// since the operation to get tooltip content was async, it is possible that
|
|
|
|
// we now create tooltip which won't close because it won't receive mouseleave event
|
|
|
|
if ($(this).is(":hover")) {
|
|
|
|
$(this).tooltip({
|
|
|
|
delay: {"show": 300, "hide": 100},
|
|
|
|
container: 'body',
|
|
|
|
placement: 'auto',
|
|
|
|
trigger: 'manual',
|
|
|
|
boundary: 'window',
|
|
|
|
title: html,
|
2019-04-20 15:39:39 +08:00
|
|
|
html: true,
|
|
|
|
template: '<div class="tooltip note-tooltip" role="tooltip"><div class="arrow"></div><div class="tooltip-inner"></div></div>'
|
2018-12-23 03:57:09 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
$(this).tooltip('show');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function mouseLeaveHandler() {
|
|
|
|
$(this).tooltip('dispose');
|
2018-03-27 10:29:14 +08:00
|
|
|
}
|
|
|
|
|
2020-02-01 18:15:58 +08:00
|
|
|
async function renderTooltip(note, noteComplement) {
|
2020-01-25 21:37:12 +08:00
|
|
|
const attributes = await note.getAttributes();
|
|
|
|
|
2018-08-22 21:31:36 +08:00
|
|
|
let content = '';
|
2018-08-27 19:35:45 +08:00
|
|
|
const promoted = attributes.filter(attr =>
|
|
|
|
(attr.type === 'label-definition' || attr.type === 'relation-definition')
|
|
|
|
&& !attr.name.startsWith("child:")
|
|
|
|
&& attr.value.isPromoted);
|
2018-08-22 21:31:36 +08:00
|
|
|
|
|
|
|
if (promoted.length > 0) {
|
|
|
|
const $table = $("<table>").addClass("promoted-attributes-in-tooltip");
|
|
|
|
|
|
|
|
for (const definitionAttr of promoted) {
|
|
|
|
const definitionType = definitionAttr.type;
|
|
|
|
const valueType = definitionType.substr(0, definitionType.length - 11);
|
|
|
|
|
|
|
|
let valueAttrs = attributes.filter(el => el.name === definitionAttr.name && el.type === valueType);
|
|
|
|
|
|
|
|
for (const valueAttr of valueAttrs) {
|
2018-08-27 19:35:45 +08:00
|
|
|
if (!valueAttr.value) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-08-22 21:31:36 +08:00
|
|
|
let $value = "";
|
|
|
|
|
|
|
|
if (valueType === 'label') {
|
|
|
|
$value = $("<td>").text(valueAttr.value);
|
|
|
|
}
|
|
|
|
else if (valueType === 'relation' && valueAttr.value) {
|
|
|
|
$value = $("<td>").append(await linkService.createNoteLink(valueAttr.value));
|
|
|
|
}
|
|
|
|
|
|
|
|
const $row = $("<tr>")
|
|
|
|
.append($("<th>").text(definitionAttr.name))
|
|
|
|
.append($value);
|
|
|
|
|
|
|
|
$table.append($row);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
content += $table.prop('outerHTML');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (note.type === 'text') {
|
2019-02-07 04:29:23 +08:00
|
|
|
// surround with <div> for a case when note's content is pure text (e.g. "[protected]") which
|
2018-12-23 03:57:09 +08:00
|
|
|
// then fails the jquery non-empty text test
|
2020-02-01 18:15:58 +08:00
|
|
|
content += '<div>' + noteComplement.content + '</div>';
|
2018-08-22 21:31:36 +08:00
|
|
|
}
|
|
|
|
else if (note.type === 'code') {
|
2018-11-14 15:36:19 +08:00
|
|
|
content += $("<pre>")
|
2020-02-01 18:15:58 +08:00
|
|
|
.text(noteComplement.content)
|
2018-11-14 15:36:19 +08:00
|
|
|
.prop('outerHTML');
|
|
|
|
}
|
|
|
|
else if (note.type === 'image') {
|
|
|
|
content += $("<img>")
|
2019-02-23 06:03:20 +08:00
|
|
|
.prop("src", `api/images/${note.noteId}/${note.title}`)
|
2018-11-14 15:36:19 +08:00
|
|
|
.prop('outerHTML');
|
2018-08-22 21:31:36 +08:00
|
|
|
}
|
|
|
|
// other types of notes don't have tooltip preview
|
|
|
|
|
2018-11-14 15:36:19 +08:00
|
|
|
if (!$(content).text().trim() && note.type !== 'image') {
|
2018-11-07 02:35:42 +08:00
|
|
|
return "";
|
2018-08-22 21:31:36 +08:00
|
|
|
}
|
|
|
|
|
2018-11-07 02:35:42 +08:00
|
|
|
return content;
|
2018-08-22 21:31:36 +08:00
|
|
|
}
|
|
|
|
|
2018-03-27 10:29:14 +08:00
|
|
|
export default {
|
2018-12-23 03:57:09 +08:00
|
|
|
setupGlobalTooltip,
|
|
|
|
setupElementTooltip
|
2018-03-27 10:29:14 +08:00
|
|
|
}
|