UNPKG

5.91 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 */
16import { __assign } from "tslib";
17/**
18 * @fileoverview This component is DEPRECATED, and the code is frozen.
19 * All changes & bugfixes should be made to HotkeysDialog2 instead.
20 */
21/* eslint-disable deprecation/deprecation */
22import classNames from "classnames";
23import * as React from "react";
24import * as ReactDOM from "react-dom";
25import { Classes } from "../common";
26import { Dialog, Hotkey, Hotkeys } from "../components";
27/**
28 * The delay before showing or hiding the dialog. Should be long enough to
29 * allow all registered hotkey listeners to execute first.
30 */
31var DELAY_IN_MS = 10;
32/** @deprecated use HotkeysDialog2 */
33var HotkeysDialogLegacy = /** @class */ (function () {
34 function HotkeysDialogLegacy() {
35 var _this = this;
36 this.componentProps = {
37 globalHotkeysGroup: "Global hotkeys",
38 };
39 this.container = null;
40 this.hotkeysQueue = [];
41 this.isDialogShowing = false;
42 this.show = function () {
43 _this.isDialogShowing = true;
44 _this.render();
45 };
46 this.hide = function () {
47 _this.isDialogShowing = false;
48 _this.render();
49 };
50 }
51 HotkeysDialogLegacy.prototype.render = function () {
52 if (this.container == null) {
53 this.container = this.getContainer();
54 }
55 ReactDOM.render(this.renderComponent(), this.container);
56 };
57 HotkeysDialogLegacy.prototype.unmount = function () {
58 if (this.container != null) {
59 ReactDOM.unmountComponentAtNode(this.container);
60 this.container.remove();
61 this.container = null;
62 }
63 };
64 /**
65 * Because hotkeys can be registered globally and locally and because
66 * event ordering cannot be guaranteed, we use this debouncing method to
67 * allow all hotkey listeners to fire and add their hotkeys to the dialog.
68 *
69 * 10msec after the last listener adds their hotkeys, we render the dialog
70 * and clear the queue.
71 */
72 HotkeysDialogLegacy.prototype.enqueueHotkeysForDisplay = function (hotkeys) {
73 this.hotkeysQueue.push(hotkeys);
74 // reset timeout for debounce
75 window.clearTimeout(this.showTimeoutToken);
76 this.showTimeoutToken = window.setTimeout(this.show, DELAY_IN_MS);
77 };
78 HotkeysDialogLegacy.prototype.hideAfterDelay = function () {
79 window.clearTimeout(this.hideTimeoutToken);
80 this.hideTimeoutToken = window.setTimeout(this.hide, DELAY_IN_MS);
81 };
82 HotkeysDialogLegacy.prototype.isShowing = function () {
83 return this.isDialogShowing;
84 };
85 HotkeysDialogLegacy.prototype.getContainer = function () {
86 if (this.container == null) {
87 this.container = document.createElement("div");
88 this.container.classList.add(Classes.PORTAL);
89 document.body.appendChild(this.container);
90 }
91 return this.container;
92 };
93 HotkeysDialogLegacy.prototype.renderComponent = function () {
94 return (React.createElement(Dialog, __assign({}, this.componentProps, { className: classNames(Classes.HOTKEY_DIALOG, this.componentProps.className), isOpen: this.isDialogShowing, onClose: this.hide }),
95 React.createElement("div", { className: Classes.DIALOG_BODY }, this.renderHotkeys())));
96 };
97 HotkeysDialogLegacy.prototype.renderHotkeys = function () {
98 var _this = this;
99 var hotkeys = this.emptyHotkeyQueue();
100 var elements = hotkeys.map(function (hotkey, index) {
101 var group = hotkey.global === true && hotkey.group == null ? _this.componentProps.globalHotkeysGroup : hotkey.group;
102 return React.createElement(Hotkey, __assign({ key: index }, hotkey, { group: group }));
103 });
104 return React.createElement(Hotkeys, null, elements);
105 };
106 HotkeysDialogLegacy.prototype.emptyHotkeyQueue = function () {
107 // flatten then empty the hotkeys queue
108 var hotkeys = this.hotkeysQueue.reduce(function (arr, queued) { return arr.concat(queued); }, []);
109 this.hotkeysQueue.length = 0;
110 return hotkeys;
111 };
112 return HotkeysDialogLegacy;
113}());
114// singleton instance
115var HOTKEYS_DIALOG = new HotkeysDialogLegacy();
116/** @deprecated use HotkeysProvider */
117export function isHotkeysDialogShowing() {
118 return HOTKEYS_DIALOG.isShowing();
119}
120/** @deprecated use HotkeysProvider */
121export function setHotkeysDialogProps(props) {
122 for (var key in props) {
123 if (props.hasOwnProperty(key)) {
124 HOTKEYS_DIALOG.componentProps[key] = props[key];
125 }
126 }
127}
128/** @deprecated use HotkeysProvider */
129export function showHotkeysDialog(hotkeys) {
130 HOTKEYS_DIALOG.enqueueHotkeysForDisplay(hotkeys);
131}
132/** @deprecated use HotkeysProvider */
133export function hideHotkeysDialog() {
134 HOTKEYS_DIALOG.hide();
135}
136/**
137 * Use this function instead of `hideHotkeysDialog` if you need to ensure that all hotkey listeners
138 * have time to execute with the dialog in a consistent open state. This can avoid flickering the
139 * dialog between open and closed states as successive listeners fire.
140 *
141 * @deprecated use HotkeysProvider
142 */
143export function hideHotkeysDialogAfterDelay() {
144 HOTKEYS_DIALOG.hideAfterDelay();
145}
146//# sourceMappingURL=hotkeysDialogLegacy.js.map
\No newline at end of file