UNPKG

3.06 kBJavaScriptView Raw
1"use strict";
2/*
3 * Copyright 2016 Palantir Technologies, Inc. All rights reserved.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17Object.defineProperty(exports, "__esModule", { value: true });
18exports.InteractionModeEngine = void 0;
19var TAB_KEY_CODE = 9;
20/* istanbul ignore next */
21/**
22 * A nifty little class that maintains event handlers to add a class to the container element
23 * when entering "mouse mode" (on a `mousedown` event) and remove it when entering "keyboard mode"
24 * (on a `tab` key `keydown` event).
25 */
26var InteractionModeEngine = /** @class */ (function () {
27 function InteractionModeEngine(container, className) {
28 var _this = this;
29 this.container = container;
30 this.className = className;
31 this.isRunning = false;
32 this.handleKeyDown = function (e) {
33 // HACKHACK: https://github.com/palantir/blueprint/issues/4165
34 // eslint-disable-next-line deprecation/deprecation
35 if (e.which === TAB_KEY_CODE) {
36 _this.reset();
37 _this.container.addEventListener("mousedown", _this.handleMouseDown);
38 }
39 };
40 this.handleMouseDown = function () {
41 _this.reset();
42 _this.container.classList.add(_this.className);
43 // HACKHACK: see https://github.com/palantir/blueprint/issues/4342
44 _this.container.addEventListener("keydown", _this.handleKeyDown);
45 };
46 }
47 /** Returns whether the engine is currently running. */
48 InteractionModeEngine.prototype.isActive = function () {
49 return this.isRunning;
50 };
51 /** Enable behavior which applies the given className when in mouse mode. */
52 InteractionModeEngine.prototype.start = function () {
53 this.container.addEventListener("mousedown", this.handleMouseDown);
54 this.isRunning = true;
55 };
56 /** Disable interaction mode behavior and remove className from container. */
57 InteractionModeEngine.prototype.stop = function () {
58 this.reset();
59 this.isRunning = false;
60 };
61 InteractionModeEngine.prototype.reset = function () {
62 this.container.classList.remove(this.className);
63 // HACKHACK: see https://github.com/palantir/blueprint/issues/4342
64 this.container.removeEventListener("keydown", this.handleKeyDown);
65 this.container.removeEventListener("mousedown", this.handleMouseDown);
66 };
67 return InteractionModeEngine;
68}());
69exports.InteractionModeEngine = InteractionModeEngine;
70//# sourceMappingURL=interactionMode.js.map
\No newline at end of file