Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.0] Allow passing of DOM nodes to setContent #13469

Merged
merged 2 commits into from Oct 26, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 19 additions & 4 deletions src/component/tooltip/TooltipHTMLContent.ts
Expand Up @@ -17,7 +17,7 @@
* under the License.
*/

import { isString, indexOf, map, each, bind } from 'zrender/src/core/util';
import { isString, indexOf, map, each, bind, isObject, isArray, isDom } from 'zrender/src/core/util';
import { toHex } from 'zrender/src/tool/color';
import { normalizeEvent } from 'zrender/src/core/event';
import { transformLocalCoord } from 'zrender/src/core/dom';
Expand Down Expand Up @@ -365,7 +365,7 @@ class TooltipHTMLContent {
}

setContent(
content: string,
content: string | HTMLElement[],
markers: unknown,
tooltipModel: Model<TooltipOption>,
borderColor?: ZRColor,
Expand All @@ -375,12 +375,27 @@ class TooltipHTMLContent {
return;
}

const el = this.el;

if (isString(arrowPosition) && tooltipModel.get('trigger') === 'item'
&& !shouldTooltipConfine(tooltipModel)) {
content += assembleArrow(tooltipModel.get('backgroundColor'), borderColor, arrowPosition);
}

this.el.innerHTML = content;
if (isString(content)) {
el.innerHTML = content == null ? '' : content;
}
else if (content) {
// Clear previous
el.innerHTML = '';
if (!isArray(content)) {
content = [content];
}
for (let i = 0; i < content.length; i++) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for cleaning this up!
I'm curious, is a for loop with repeated indexing better than a for … of loop? I know that it is more memory efficient in compiled languages where arrays are implemented as contiguous areas of memory, but I had no idea the same was true for Javascript.

if (isDom(content[i]) && content[i].parentNode !== el) {
el.appendChild(content[i]);
}
}
}
}

setEnterable(enterable: boolean) {
Expand Down
5 changes: 4 additions & 1 deletion src/component/tooltip/TooltipRichContent.ts
Expand Up @@ -70,12 +70,15 @@ class TooltipRichContent {
* Set tooltip content
*/
setContent(
content: string,
content: string | HTMLElement[],
markupStyleCreator: TooltipMarkupStyleCreator,
tooltipModel: Model<TooltipOption>,
borderColor: ZRColor,
arrowPosition: TooltipOption['position']
) {
if (zrUtil.isObject(content)) {
throw new Error("Passing DOM nodes as content is not supported in richText tooltip!");
}
if (this.el) {
this._zr.remove(this.el);
}
Expand Down
4 changes: 2 additions & 2 deletions src/component/tooltip/TooltipView.ts
Expand Up @@ -707,7 +707,7 @@ class TooltipView extends ComponentView {

const formatter = tooltipModel.get('formatter');
positionExpr = positionExpr || tooltipModel.get('position');
let html = defaultHtml;
let html: string | HTMLElement[] = defaultHtml;
const nearPoint = this._getNearestPoint(
[x, y],
params,
Expand All @@ -718,7 +718,7 @@ class TooltipView extends ComponentView {
html = formatUtil.formatTpl(formatter, params, true);
}
else if (zrUtil.isFunction(formatter)) {
const callback = bind(function (cbTicket: string, html: string) {
const callback = bind(function (cbTicket: string, html: string | HTMLElement[]) {
if (cbTicket === this._ticket) {
tooltipContent.setContent(html, markupStyleCreator, tooltipModel, nearPoint.color, positionExpr);
this._updatePosition(
Expand Down
4 changes: 2 additions & 2 deletions src/util/types.ts
Expand Up @@ -1053,12 +1053,12 @@ interface TooltipFormatterCallback<T> {
* For sync callback
* params will be an array on axis trigger.
*/
(params: T, asyncTicket: string): string
(params: T, asyncTicket: string): string | HTMLElement[]
/**
* For async callback.
* Returned html string will be a placeholder when callback is not invoked.
*/
(params: T, asyncTicket: string, callback: (cbTicket: string, html: string) => void): string
(params: T, asyncTicket: string, callback: (cbTicket: string, htmlOrDomNodes: string | HTMLElement[]) => void): string | HTMLElement[]
}

type TooltipBuiltinPosition = 'inside' | 'top' | 'left' | 'right' | 'bottom';
Expand Down
75 changes: 75 additions & 0 deletions test/tooltip-domnode.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.