Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
- pat-slideshow-builder
- simplePlaceholder from jquery-ext.
- IE11 is not supported by default anymore. There is a ``polyfills`` bundle, which adds IE11 support for the time being.
- pat tooltip: Remove undocumented "souce: content-html" parameter.
- pat tooltip: Remove undocumented "souce: auto" parameter. This parameter should not be used as it is not explicit enough and would lead to unintuitive behavior.

### Features

Expand Down
56 changes: 19 additions & 37 deletions src/pat/tooltip/tooltip.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,22 +30,14 @@ const all_positions = [
parser.addArgument("position-list", [], all_positions, true);
parser.addArgument("position-policy", "auto", ["auto", "force"]);
parser.addArgument("trigger", "click", ["click", "hover"]);
parser.addArgument("source", "title", [
"auto",
"ajax",
"content",
"content-html",
"title",
]);
parser.addArgument("source", "title", ["ajax", "content", "title"]);
parser.addArgument("ajax-data-type", "html", ["html", "markdown"]);
parser.addArgument("closing", "auto", ["auto", "sticky", "close-button"]);
parser.addArgument("delay");
parser.addArgument("mark-inactive", true);
parser.addArgument("class");
parser.addArgument("target", "body");

// parser.addArgument("height", "auto", ["auto", "max"]);

export default Base.extend({
name: "tooltip",
trigger: ".pat-tooltip, .pat-tooltip-ng",
Expand Down Expand Up @@ -94,7 +86,10 @@ export default Base.extend({
el.classList.add("inactive");
}

if (this.options.trigger === "click") {
if (
this.options.trigger === "click" &&
this.options.source === "ajax"
) {
// prevent default action for "click" and "mouseenter click"
el.addEventListener("click", (event) => {
event.preventDefault();
Expand Down Expand Up @@ -173,26 +168,15 @@ export default Base.extend({
},

source: () => {
if (opts.source === "auto") {
const href = this.el.getAttribute("href");
if (typeof href !== "string") {
log.error(
`href must be specified if 'source' is set to 'auto'`
);
return;
}
if (href.indexOf("#") === 0) {
opts.source = "content";
} else {
opts.source = "ajax";
}
}
let content;
if (opts.source === "title") {
// Tooltip content from title attribute
content = this.el.getAttribute("title");
}
if (["content", "ajax"].includes(opts.source)) {
} else if (opts.source === "content") {
// Tooltiop content from trigger child content.
content = this.el.innerHTML;
tippy_options.allowHTML = true;
} else if (opts.source === "ajax") {
// Tooltiop content from AJAX request.
content = document.createElement("progress");
tippy_options.allowHTML = true;
Expand Down Expand Up @@ -276,7 +260,7 @@ export default Base.extend({
this.tippy.setProps({ trigger: "click" });
}

if (["content", "ajax"].includes(this.options.source)) {
if (this.options.source === "ajax") {
await this._getContent();
}

Expand Down Expand Up @@ -321,7 +305,7 @@ export default Base.extend({
this.tippy.setProps({ trigger: "mouseenter focus" });
}

if (["content", "ajax"].includes(this.options.source)) {
if (this.options.source === "ajax") {
this.tippy.setContent(document.createElement("progress"));
this.ajax_state.canFetch = true;
}
Expand All @@ -334,6 +318,7 @@ export default Base.extend({
const { url, selector, modifier } = this.get_url_parts(
this.el.getAttribute("href")
);
let content;
if (url) {
// Tooltip from remote page.
this.ajax_state = {
Expand All @@ -347,21 +332,18 @@ export default Base.extend({
// TODO: use pat-inject, once it supports async
const response = await fetch(url);
const text = await response.text();
const content = handler(text, url, selector, modifier);
this.tippy.setContent(content);
content = handler(text, url, selector, modifier);
} catch (e) {
log.error(`Error on ajax request ${e}`);
}
this.ajax_state.isFetching = false;
} else if (selector) {
// Tooltip content from current DOM tree.
const content = document.querySelector(selector);
if (!content) {
return;
}
this.tippy.setContent(content[modifier]);
} else {
this.tippy.setContent(this.el.innerHTML);
content = document.querySelector(selector);
content = content ? content[modifier] : undefined;
}
if (content) {
this.tippy.setContent(content);
}
},

Expand Down
Loading