Convert GrabToPan to a standard class

This code is the last piece[1] of the viewer that's not using standard `class`es, and by converting this code we get rid of some now unneeded boilerplate code (slightly reducing the size of the *built* `web/viewer.js` file).
Note that while this code was originally imported from a separate repository, it was last sync-ed with upstream *five years* ago which is why this re-factoring should be OK as far as I'm concerned (and we've done some other clean-up since then as well).

---
[1] Technically the `web/debugger.js` file is left as well, however that code is first of all not bundled in the *built* `web/viewer.js` file and secondly it's not even loaded by default either.
This commit is contained in:
Jonas Jenwald 2021-11-09 13:00:11 +01:00
parent ed6af0f844
commit 08d56c67ae

View File

@ -14,15 +14,19 @@
* limitations under the License. * limitations under the License.
*/ */
// Class name of element which can be grabbed.
const CSS_CLASS_GRAB = "grab-to-pan-grab";
class GrabToPan {
/** /**
* Construct a GrabToPan instance for a given HTML element. * Construct a GrabToPan instance for a given HTML element.
* @param options.element {Element} * @param {Element} options.element
* @param options.ignoreTarget {function} optional. See `ignoreTarget(node)` * @param {function} [options.ignoreTarget] - See `ignoreTarget(node)`.
* @param options.onActiveChanged {function(boolean)} optional. Called * @param {function(boolean)} [options.onActiveChanged] - Called when
* when grab-to-pan is (de)activated. The first argument is a boolean that * grab-to-pan is (de)activated. The first argument is a boolean that
* shows whether grab-to-pan is activated. * shows whether grab-to-pan is activated.
*/ */
function GrabToPan(options) { constructor(options) {
this.element = options.element; this.element = options.element;
this.document = options.element.ownerDocument; this.document = options.element.ownerDocument;
if (typeof options.ignoreTarget === "function") { if (typeof options.ignoreTarget === "function") {
@ -35,76 +39,66 @@ function GrabToPan(options) {
this.activate = this.activate.bind(this); this.activate = this.activate.bind(this);
this.deactivate = this.deactivate.bind(this); this.deactivate = this.deactivate.bind(this);
this.toggle = this.toggle.bind(this); this.toggle = this.toggle.bind(this);
this._onmousedown = this._onmousedown.bind(this); this._onMouseDown = this.#onMouseDown.bind(this);
this._onmousemove = this._onmousemove.bind(this); this._onMouseMove = this.#onMouseMove.bind(this);
this._endPan = this._endPan.bind(this); this._endPan = this.#endPan.bind(this);
// This overlay will be inserted in the document when the mouse moves during // This overlay will be inserted in the document when the mouse moves during
// a grab operation, to ensure that the cursor has the desired appearance. // a grab operation, to ensure that the cursor has the desired appearance.
const overlay = (this.overlay = document.createElement("div")); const overlay = (this.overlay = document.createElement("div"));
overlay.className = "grab-to-pan-grabbing"; overlay.className = "grab-to-pan-grabbing";
} }
GrabToPan.prototype = {
/**
* Class name of element which can be grabbed
*/
CSS_CLASS_GRAB: "grab-to-pan-grab",
/** /**
* Bind a mousedown event to the element to enable grab-detection. * Bind a mousedown event to the element to enable grab-detection.
*/ */
activate: function GrabToPan_activate() { activate() {
if (!this.active) { if (!this.active) {
this.active = true; this.active = true;
this.element.addEventListener("mousedown", this._onmousedown, true); this.element.addEventListener("mousedown", this._onMouseDown, true);
this.element.classList.add(this.CSS_CLASS_GRAB); this.element.classList.add(CSS_CLASS_GRAB);
if (this.onActiveChanged) {
this.onActiveChanged(true); this.onActiveChanged?.(true);
} }
} }
},
/** /**
* Removes all events. Any pending pan session is immediately stopped. * Removes all events. Any pending pan session is immediately stopped.
*/ */
deactivate: function GrabToPan_deactivate() { deactivate() {
if (this.active) { if (this.active) {
this.active = false; this.active = false;
this.element.removeEventListener("mousedown", this._onmousedown, true); this.element.removeEventListener("mousedown", this._onMouseDown, true);
this._endPan(); this._endPan();
this.element.classList.remove(this.CSS_CLASS_GRAB); this.element.classList.remove(CSS_CLASS_GRAB);
if (this.onActiveChanged) {
this.onActiveChanged(false);
}
}
},
toggle: function GrabToPan_toggle() { this.onActiveChanged?.(false);
}
}
toggle() {
if (this.active) { if (this.active) {
this.deactivate(); this.deactivate();
} else { } else {
this.activate(); this.activate();
} }
}, }
/** /**
* Whether to not pan if the target element is clicked. * Whether to not pan if the target element is clicked.
* Override this method to change the default behaviour. * Override this method to change the default behaviour.
* *
* @param node {Element} The target of the event * @param {Element} node - The target of the event.
* @returns {boolean} Whether to not react to the click event. * @returns {boolean} Whether to not react to the click event.
*/ */
ignoreTarget: function GrabToPan_ignoreTarget(node) { ignoreTarget(node) {
// Check whether the clicked element is, a child of, an input element/link. // Check whether the clicked element is, a child of, an input element/link.
return node.matches( return node.matches(
"a[href], a[href] *, input, textarea, button, button *, select, option" "a[href], a[href] *, input, textarea, button, button *, select, option"
); );
}, }
/** #onMouseDown(event) {
* @private
*/
_onmousedown: function GrabToPan__onmousedown(event) {
if (event.button !== 0 || this.ignoreTarget(event.target)) { if (event.button !== 0 || this.ignoreTarget(event.target)) {
return; return;
} }
@ -122,7 +116,7 @@ GrabToPan.prototype = {
this.scrollTopStart = this.element.scrollTop; this.scrollTopStart = this.element.scrollTop;
this.clientXStart = event.clientX; this.clientXStart = event.clientX;
this.clientYStart = event.clientY; this.clientYStart = event.clientY;
this.document.addEventListener("mousemove", this._onmousemove, true); this.document.addEventListener("mousemove", this._onMouseMove, true);
this.document.addEventListener("mouseup", this._endPan, true); this.document.addEventListener("mouseup", this._endPan, true);
// When a scroll event occurs before a mousemove, assume that the user // When a scroll event occurs before a mousemove, assume that the user
// dragged a scrollbar (necessary for Opera Presto, Safari and IE) // dragged a scrollbar (necessary for Opera Presto, Safari and IE)
@ -135,12 +129,9 @@ GrabToPan.prototype = {
if (focusedElement && !focusedElement.contains(event.target)) { if (focusedElement && !focusedElement.contains(event.target)) {
focusedElement.blur(); focusedElement.blur();
} }
}, }
/** #onMouseMove(event) {
* @private
*/
_onmousemove: function GrabToPan__onmousemove(event) {
this.element.removeEventListener("scroll", this._endPan, true); this.element.removeEventListener("scroll", this._endPan, true);
if (!(event.buttons & 1)) { if (!(event.buttons & 1)) {
// The left mouse button is released. // The left mouse button is released.
@ -164,18 +155,15 @@ GrabToPan.prototype = {
if (!this.overlay.parentNode) { if (!this.overlay.parentNode) {
document.body.appendChild(this.overlay); document.body.appendChild(this.overlay);
} }
}, }
/** #endPan() {
* @private
*/
_endPan: function GrabToPan__endPan() {
this.element.removeEventListener("scroll", this._endPan, true); this.element.removeEventListener("scroll", this._endPan, true);
this.document.removeEventListener("mousemove", this._onmousemove, true); this.document.removeEventListener("mousemove", this._onMouseMove, true);
this.document.removeEventListener("mouseup", this._endPan, true); this.document.removeEventListener("mouseup", this._endPan, true);
// Note: ChildNode.remove doesn't throw if the parentNode is undefined. // Note: ChildNode.remove doesn't throw if the parentNode is undefined.
this.overlay.remove(); this.overlay.remove();
}, }
}; }
export { GrabToPan }; export { GrabToPan };