UNPKG

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