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