@blueprintjs/core
Version:
Core styles & components
119 lines (117 loc) • 4.71 kB
JavaScript
/*
* Copyright 2016 Palantir Technologies, Inc. All rights reserved.
* Licensed under the BSD-3 License as modified (the “License”); you may obtain a copy
* of the license at https://github.com/palantir/blueprint/blob/master/LICENSE
* and https://github.com/palantir/blueprint/blob/master/PATENTS
*/
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
var classNames = require("classnames");
var React = require("react");
var ReactDOM = require("react-dom");
var common_1 = require("../../common");
var components_1 = require("../../components");
var hotkey_1 = require("./hotkey");
var hotkeys_1 = require("./hotkeys");
var HotkeysDialog = (function () {
function HotkeysDialog() {
var _this = this;
this.componentProps = {
globalHotkeysGroup: "Global hotkeys",
};
this.hotkeysQueue = [];
this.isDialogShowing = false;
this.timeoutToken = 0;
this.show = function () {
_this.isDialogShowing = true;
_this.render();
};
this.hide = function () {
_this.isDialogShowing = false;
_this.render();
};
}
HotkeysDialog.prototype.render = function () {
if (this.container == null) {
this.container = this.getContainer();
}
ReactDOM.render(this.renderComponent(), this.container);
};
HotkeysDialog.prototype.unmount = function () {
if (this.container != null) {
ReactDOM.unmountComponentAtNode(this.container);
this.container.remove();
delete this.container;
}
};
/**
* Because hotkeys can be registered globally and locally and because
* event ordering cannot be guaranteed, we use this debouncing method to
* allow all hotkey listeners to fire and add their hotkeys to the dialog.
*
* 10msec after the last listener adds their hotkeys, we render the dialog
* and clear the queue.
*/
HotkeysDialog.prototype.enqueueHotkeysForDisplay = function (hotkeys) {
this.hotkeysQueue.push(hotkeys);
// reset timeout for debounce
clearTimeout(this.timeoutToken);
this.timeoutToken = setTimeout(this.show, 10);
};
HotkeysDialog.prototype.isShowing = function () {
return this.isDialogShowing;
};
HotkeysDialog.prototype.getContainer = function () {
if (this.container == null) {
this.container = document.createElement("div");
this.container.classList.add(common_1.Classes.PORTAL);
document.body.appendChild(this.container);
}
return this.container;
};
HotkeysDialog.prototype.renderComponent = function () {
return (React.createElement(components_1.Dialog, tslib_1.__assign({}, this.componentProps, { className: classNames(this.componentProps.className, "pt-hotkey-dialog"), isOpen: this.isDialogShowing, onClose: this.hide }),
React.createElement("div", { className: common_1.Classes.DIALOG_BODY }, this.renderHotkeys())));
};
HotkeysDialog.prototype.renderHotkeys = function () {
var _this = this;
var hotkeys = this.emptyHotkeyQueue();
var elements = hotkeys.map(function (hotkey, index) {
var group = (hotkey.global === true && hotkey.group == null) ?
_this.componentProps.globalHotkeysGroup : hotkey.group;
return React.createElement(hotkey_1.Hotkey, tslib_1.__assign({ key: index }, hotkey, { group: group }));
});
return React.createElement(hotkeys_1.Hotkeys, null, elements);
};
HotkeysDialog.prototype.emptyHotkeyQueue = function () {
// flatten then empty the hotkeys queue
var hotkeys = this.hotkeysQueue.reduce((function (arr, queued) { return arr.concat(queued); }), []);
this.hotkeysQueue.length = 0;
return hotkeys;
};
return HotkeysDialog;
}());
// singleton instance
var HOTKEYS_DIALOG = new HotkeysDialog();
function isHotkeysDialogShowing() {
return HOTKEYS_DIALOG.isShowing();
}
exports.isHotkeysDialogShowing = isHotkeysDialogShowing;
function setHotkeysDialogProps(props) {
for (var key in props) {
if (props.hasOwnProperty(key)) {
HOTKEYS_DIALOG.componentProps[key] = props[key];
}
}
}
exports.setHotkeysDialogProps = setHotkeysDialogProps;
function showHotkeysDialog(hotkeys) {
HOTKEYS_DIALOG.enqueueHotkeysForDisplay(hotkeys);
}
exports.showHotkeysDialog = showHotkeysDialog;
function hideHotkeysDialog() {
HOTKEYS_DIALOG.hide();
}
exports.hideHotkeysDialog = hideHotkeysDialog;
//# sourceMappingURL=hotkeysDialog.js.map