Skip to content

Commit

Permalink
Add tooltip if any in annotations layer
Browse files Browse the repository at this point in the history
  • Loading branch information
calixteman committed Sep 13, 2020
1 parent b058266 commit e69f692
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 6 deletions.
5 changes: 4 additions & 1 deletion src/core/annotation.js
Original file line number Diff line number Diff line change
Expand Up @@ -1446,6 +1446,7 @@ class ButtonWidgetAnnotation extends WidgetAnnotation {
this.hasFieldFlag(AnnotationFieldFlag.RADIO) &&
!this.hasFieldFlag(AnnotationFieldFlag.PUSHBUTTON);
this.data.pushButton = this.hasFieldFlag(AnnotationFieldFlag.PUSHBUTTON);
this.data.isTooltipOnly = false;

if (this.data.checkBox) {
this._processCheckBox(params);
Expand Down Expand Up @@ -1683,11 +1684,13 @@ class ButtonWidgetAnnotation extends WidgetAnnotation {
}

_processPushButton(params) {
if (!params.dict.has("A")) {
if (!params.dict.has("A") && !this.data.alternativeText) {
warn("Push buttons without action dictionaries are not supported");
return;
}

this.data.isTooltipOnly = !params.dict.has("A");

Catalog.parseDestDictionary({
destDict: params.dict,
resultObj: this.data,
Expand Down
27 changes: 22 additions & 5 deletions src/display/annotation_layer.js
Original file line number Diff line number Diff line change
Expand Up @@ -289,13 +289,15 @@ class AnnotationElement {
}

class LinkAnnotationElement extends AnnotationElement {
constructor(parameters) {
const isRenderable = !!(
constructor(parameters, isRenderable = false) {
const _isRenderable = !!(
parameters.data.url ||
parameters.data.dest ||
parameters.data.action
parameters.data.action ||
isRenderable
);
super(parameters, isRenderable);

super(parameters, _isRenderable);
}

/**
Expand All @@ -322,8 +324,10 @@ class LinkAnnotationElement extends AnnotationElement {
});
} else if (data.action) {
this._bindNamedAction(link, data.action);
} else {
} else if (data.dest) {
this._bindLink(link, data.dest);
} else {
this._bindLink(link, "");
}

this.container.appendChild(link);
Expand Down Expand Up @@ -420,6 +424,10 @@ class WidgetAnnotationElement extends AnnotationElement {
*/
render() {
// Show only the container for unsupported field types.
if (this.data.alternativeText) {
this.container.title = this.data.alternativeText;
}

return this.container;
}
}
Expand Down Expand Up @@ -633,6 +641,10 @@ class RadioButtonWidgetAnnotationElement extends WidgetAnnotationElement {
}

class PushButtonWidgetAnnotationElement extends LinkAnnotationElement {
constructor(parameters) {
super(parameters, parameters.data.isTooltipOnly);
}

/**
* Render the push button widget annotation's HTML element
* in the empty container.
Expand All @@ -647,6 +659,11 @@ class PushButtonWidgetAnnotationElement extends LinkAnnotationElement {
// as performing actions on form fields (resetting, submitting, et cetera).
const container = super.render();
container.className = "buttonWidgetAnnotation pushButton";

if (this.data.alternativeText) {
container.title = this.data.alternativeText;
}

return container;
}
}
Expand Down
43 changes: 43 additions & 0 deletions test/unit/annotation_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2340,6 +2340,49 @@ describe("annotation", function () {
})
.catch(done.fail);
});

it("should handle push buttons", function (done) {
const buttonWidgetRef = Ref.get(124, 0);
buttonWidgetDict.set("Ff", 0x10000);
buttonWidgetDict.set("A", "whatever");

const xref = new XRefMock([
{ ref: buttonWidgetRef, data: buttonWidgetDict },
]);

AnnotationFactory.create(
xref,
buttonWidgetRef,
pdfManagerMock,
idFactoryMock
).then(({ data }) => {
expect(data.annotationType).toEqual(AnnotationType.WIDGET);
expect(data.pushButton).toEqual(true);
done();
}, done.fail);
});

it("should handle push buttons that act as a tooltip only", function (done) {
const buttonWidgetRef = Ref.get(124, 0);
buttonWidgetDict.set("Ff", 0x10000);
buttonWidgetDict.set("TU", "An alternative text");

const xref = new XRefMock([
{ ref: buttonWidgetRef, data: buttonWidgetDict },
]);

AnnotationFactory.create(
xref,
buttonWidgetRef,
pdfManagerMock,
idFactoryMock
).then(({ data }) => {
expect(data.annotationType).toEqual(AnnotationType.WIDGET);
expect(data.pushButton).toEqual(true);
expect(data.alternativeText).toEqual("An alternative text");
done();
}, done.fail);
});
});

describe("ChoiceWidgetAnnotation", function () {
Expand Down

0 comments on commit e69f692

Please sign in to comment.