Tweak the LinkAnnotationElement._bindJSAction and WidgetAnnotationElement.{_setEventListener, _setEventListeners} methods

- Update the `LinkAnnotationElement._bindJSAction` call-site to actually agree with the JSDocs, by passing in the `data`.

 - Prevent the links created by `LinkAnnotationElement._bindJSAction` from being displayed with empty hashes; compare with e.g. `LinkAnnotationElement. _bindNamedAction`.

 - The overall indentation-level in `WidgetAnnotationElement._setEventListener` can be reduced slightly by using early returns, which improves the overall readability of this method a bit. (We're also able to avoid unnecessary `in` usage here.)

 - The code can also be made *slightly* more efficient overall, by moving the `this.data.actions` check into `WidgetAnnotationElement._setEventListeners` instead. This way we can avoid useless `this._setEventListener`-calls when there are no actions present.
This commit is contained in:
Jonas Jenwald 2020-12-17 13:08:13 +01:00
parent 6dc39cb873
commit e2b6d79dee

View File

@ -399,7 +399,7 @@ class LinkAnnotationElement extends AnnotationElement {
this.enableScripting &&
this.hasJSActions
) {
this._bindJSAction(link);
this._bindJSAction(link, data);
} else {
this._bindLink(link, "");
}
@ -465,9 +465,8 @@ class LinkAnnotationElement extends AnnotationElement {
* @param {Object} data
* @memberof LinkAnnotationElement
*/
_bindJSAction(link) {
link.href = this.linkService.getAnchorUrl("#");
const { data } = this;
_bindJSAction(link, data) {
link.href = this.linkService.getAnchorUrl("");
const map = new Map([
["Action", "onclick"],
["MouseUp", "onmouseup"],
@ -546,40 +545,44 @@ class WidgetAnnotationElement extends AnnotationElement {
}
_setEventListener(element, baseName, eventName, valueGetter) {
if (this.data.actions && eventName.replace(" ", "") in this.data.actions) {
if (baseName.includes("mouse")) {
// Mouse events
element.addEventListener(baseName, event => {
window.dispatchEvent(
new CustomEvent("dispatchEventInSandbox", {
detail: {
id: this.data.id,
name: eventName,
value: valueGetter(event),
shift: event.shiftKey,
modifier: this._getKeyModifier(event),
},
})
);
});
} else {
// Non mouse event
element.addEventListener(baseName, event => {
window.dispatchEvent(
new CustomEvent("dispatchEventInSandbox", {
detail: {
id: this.data.id,
name: eventName,
value: event.target.checked,
},
})
);
});
}
if (this.data.actions[eventName.replace(" ", "")] === undefined) {
return;
}
if (baseName.includes("mouse")) {
// Mouse events
element.addEventListener(baseName, event => {
window.dispatchEvent(
new CustomEvent("dispatchEventInSandbox", {
detail: {
id: this.data.id,
name: eventName,
value: valueGetter(event),
shift: event.shiftKey,
modifier: this._getKeyModifier(event),
},
})
);
});
} else {
// Non mouse event
element.addEventListener(baseName, event => {
window.dispatchEvent(
new CustomEvent("dispatchEventInSandbox", {
detail: {
id: this.data.id,
name: eventName,
value: event.target.checked,
},
})
);
});
}
}
_setEventListeners(element, names, getter) {
if (!this.data.actions) {
return;
}
for (const [baseName, eventName] of names) {
this._setEventListener(element, baseName, eventName, getter);
}
@ -997,8 +1000,8 @@ class RadioButtonWidgetAnnotationElement extends WidgetAnnotationElement {
element.setAttribute("id", id);
element.addEventListener("change", function (event) {
const target = event.target;
for (const radio of document.getElementsByName(event.target.name)) {
const { target } = event;
for (const radio of document.getElementsByName(target.name)) {
if (radio !== target) {
storage.setValue(radio.getAttribute("id"), { value: false });
}