UNPKG

2.6 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 */
16/* istanbul ignore next */
17/**
18 * A nifty little class that maintains event handlers to add a class to the container element
19 * when entering "mouse mode" (on a `mousedown` event) and remove it when entering "keyboard mode"
20 * (on a `tab` key `keydown` event).
21 */
22var InteractionModeEngine = /** @class */ (function () {
23 function InteractionModeEngine(container, className) {
24 var _this = this;
25 this.container = container;
26 this.className = className;
27 this.isRunning = false;
28 this.handleKeyDown = function (e) {
29 if (e.key === "Tab") {
30 _this.reset();
31 _this.container.addEventListener("mousedown", _this.handleMouseDown);
32 }
33 };
34 this.handleMouseDown = function () {
35 _this.reset();
36 _this.container.classList.add(_this.className);
37 _this.container.addEventListener("keydown", _this.handleKeyDown);
38 };
39 }
40 /** Returns whether the engine is currently running. */
41 InteractionModeEngine.prototype.isActive = function () {
42 return this.isRunning;
43 };
44 /** Enable behavior which applies the given className when in mouse mode. */
45 InteractionModeEngine.prototype.start = function () {
46 this.container.addEventListener("mousedown", this.handleMouseDown);
47 this.isRunning = true;
48 };
49 /** Disable interaction mode behavior and remove className from container. */
50 InteractionModeEngine.prototype.stop = function () {
51 this.reset();
52 this.isRunning = false;
53 };
54 InteractionModeEngine.prototype.reset = function () {
55 this.container.classList.remove(this.className);
56 this.container.removeEventListener("keydown", this.handleKeyDown);
57 this.container.removeEventListener("mousedown", this.handleMouseDown);
58 };
59 return InteractionModeEngine;
60}());
61export { InteractionModeEngine };
62//# sourceMappingURL=interactionMode.js.map
\No newline at end of file