insomnia/app/ui/components/base/modal.js
Gregory Schier 8c0d87168e libcurl network backend (#82)
* Most things are working

* Cookies and lots of improvements

* Hoist regex

* Appveyor test

* Fix appveyor file

* Remove yarn

* Test deps

* Fix

* Remove all git deps

* fix

* Fix

* Add cash rm

* Fix command

* Fix

* Production

* try

* fix

* fix?

* Done

* Remove cache

* tweak

* Shortcut

* Fix

* Attempt

* try

* Try clone depth

* Try again

* back to shallow_clone

* Some debugging

* Another try

* Try something else

* Another

* try curl only

* Another

* fix

* Update node-gyp

* Try again

* Try

* Again

* Some pruning

* Back to the start

* Again

* try config

* Reorder test

* Try without cache

* Update dep

* Try again

* remove shallow_clone

* don't run tests

* Implement CA fetching

* Fix

* Use CAs

* Fix?

* test

* Remove build dir

* Fix dir path?

* Try again

* Aagain

* Try

* Change dep

* Add comment

* Invalidate cache

* Remove yarn

* Fix cookie expiry

* fixed tests

* Some modal fixes and unrelated stuff

* Debounce autocomplete

* Got client certificates working

* Fixed binary files

* Fixed cookies

* Got proxy and debug info working

* Got CA certs working

* Update appveyor build

* Fixed cookie date parsing and overlay z-index

* Test updates

* Autocomplete to bulk header editor

* Small CSS tweak
2017-03-16 10:51:56 -07:00

170 lines
3.7 KiB
JavaScript

import React, {PureComponent, PropTypes} from 'react';
import autobind from 'autobind-decorator';
import classnames from 'classnames';
import {isMac} from '../../../common/constants';
// Keep global z-index reference so that every modal will
// appear over top of an existing one.
let globalZIndex = 1000;
@autobind
class Modal extends PureComponent {
constructor (props) {
super(props);
this.state = {
open: false,
forceRefreshCounter: 0,
zIndex: globalZIndex
};
}
_handleKeyDown (e) {
if (!this.state.open) {
return;
}
// Don't bubble up meta events up past the modal no matter what
// Example: ctrl+Enter to send requests
const isMeta = isMac() ? e.metaKey : e.ctrlKey;
if (isMeta) {
e.stopPropagation();
}
// Don't check for close keys if we don't want them
if (this.props.noEscape) {
return;
}
const closeOnKeyCodes = this.props.closeOnKeyCodes || [];
const pressedEscape = e.keyCode === 27;
const pressedElse = closeOnKeyCodes.find(c => c === e.keyCode);
if (pressedEscape || pressedElse) {
e.preventDefault();
// Pressed escape
this.hide();
}
}
_handleClick (e) {
// Don't check for close keys if we don't want them
if (this.props.noEscape) {
return;
}
// Did we click a close button. Let's check a few parent nodes up as well
// because some buttons might have nested elements. Maybe there is a better
// way to check this?
let target = e.target;
let shouldHide = false;
for (let i = 0; i < 5; i++) {
if (target.hasAttribute('data-close-modal')) {
shouldHide = true;
break;
}
target = target.parentNode;
}
if (shouldHide) {
this.hide();
}
}
_setModalRef (n) {
this._node = n;
}
show () {
const {freshState} = this.props;
const {forceRefreshCounter} = this.state;
this.setState({
open: true,
zIndex: globalZIndex++,
forceRefreshCounter: forceRefreshCounter + (freshState ? 1 : 0)
});
if (this.props.dontFocus) {
return;
}
setTimeout(() => this._node && this._node.focus());
}
toggle () {
if (this.state.open) {
this.hide();
} else {
this.show();
}
}
isOpen () {
return this.state.open;
}
hide () {
this.setState({open: false});
}
componentDidMount () {
this._node.addEventListener('keydown', this._handleKeyDown);
}
componentWillUnmount () {
if (this._node) {
this._node.removeEventListener('keydown', this._handleKeyDown);
}
}
render () {
const {tall, wide, noEscape, className, children} = this.props;
const {open, zIndex, forceRefreshCounter} = this.state;
const classes = classnames(
'modal',
className,
{'modal--open': open},
{'modal--fixed-height': tall},
{'modal--noescape': noEscape},
{'modal--wide': wide},
);
const styles = {};
if (open) {
styles.zIndex = zIndex;
}
return (
<div ref={this._setModalRef}
tabIndex="-1"
className={classes}
style={styles}
onClick={this._handleClick}>
<div className="modal__backdrop overlay" data-close-modal></div>
<div className="modal__content__wrapper">
<div className="modal__content" key={forceRefreshCounter}>
{children}
</div>
</div>
</div>
);
}
}
Modal.propTypes = {
tall: PropTypes.bool,
wide: PropTypes.bool,
noEscape: PropTypes.bool,
dontFocus: PropTypes.bool,
closeOnKeyCodes: PropTypes.array,
freshState: PropTypes.bool,
children: PropTypes.node,
className: PropTypes.string
};
export default Modal;