2017-08-10 01:56:27 +00:00
|
|
|
|
import React, {PureComponent} from 'react';
|
|
|
|
|
import PropTypes from 'prop-types';
|
2017-03-03 01:44:07 +00:00
|
|
|
|
import autobind from 'autobind-decorator';
|
2017-02-01 20:21:14 +00:00
|
|
|
|
import fs from 'fs';
|
2017-06-29 18:31:22 +00:00
|
|
|
|
import {clipboard, ipcRenderer, remote} from 'electron';
|
2017-05-03 17:48:23 +00:00
|
|
|
|
import {parse as urlParse} from 'url';
|
2017-08-16 02:17:35 +00:00
|
|
|
|
import HTTPSnippet from 'insomnia-httpsnippet';
|
2016-07-19 16:15:03 +00:00
|
|
|
|
import ReactDOM from 'react-dom';
|
2016-07-19 16:59:26 +00:00
|
|
|
|
import {connect} from 'react-redux';
|
2016-07-16 02:06:10 +00:00
|
|
|
|
import {bindActionCreators} from 'redux';
|
2017-03-28 22:45:23 +00:00
|
|
|
|
import {showModal} from '../components/modals';
|
2017-03-08 05:52:17 +00:00
|
|
|
|
import Wrapper from '../components/wrapper';
|
|
|
|
|
import WorkspaceEnvironmentsEditModal from '../components/modals/workspace-environments-edit-modal';
|
|
|
|
|
import Toast from '../components/toast';
|
|
|
|
|
import CookiesModal from '../components/modals/cookies-modal';
|
|
|
|
|
import RequestSwitcherModal from '../components/modals/request-switcher-modal';
|
|
|
|
|
import ChangelogModal from '../components/modals/changelog-modal';
|
2017-08-04 16:54:11 +00:00
|
|
|
|
import SettingsModal, {TAB_INDEX_SHORTCUTS} from '../components/modals/settings-modal';
|
2017-04-21 04:30:52 +00:00
|
|
|
|
import {COLLAPSE_SIDEBAR_REMS, DEFAULT_PANE_HEIGHT, DEFAULT_PANE_WIDTH, DEFAULT_SIDEBAR_WIDTH, getAppVersion, isMac, MAX_PANE_HEIGHT, MAX_PANE_WIDTH, MAX_SIDEBAR_REMS, MIN_PANE_HEIGHT, MIN_PANE_WIDTH, MIN_SIDEBAR_REMS, PREVIEW_MODE_SOURCE} from '../../common/constants';
|
2016-11-16 17:18:39 +00:00
|
|
|
|
import * as globalActions from '../redux/modules/global';
|
2016-11-10 05:56:23 +00:00
|
|
|
|
import * as db from '../../common/database';
|
|
|
|
|
import * as models from '../../models';
|
2017-04-24 22:43:42 +00:00
|
|
|
|
import {trackEvent} from '../../analytics';
|
2017-07-27 22:59:07 +00:00
|
|
|
|
import {selectActiveOAuth2Token, selectActiveRequest, selectActiveRequestMeta, selectActiveRequestResponses, selectActiveResponse, selectActiveWorkspace, selectActiveWorkspaceMeta, selectEntitiesLists, selectSidebarChildren, selectUnseenWorkspaces, selectWorkspaceRequestsAndRequestGroups} from '../redux/selectors';
|
2017-03-08 05:52:17 +00:00
|
|
|
|
import RequestCreateModal from '../components/modals/request-create-modal';
|
|
|
|
|
import GenerateCodeModal from '../components/modals/generate-code-modal';
|
|
|
|
|
import WorkspaceSettingsModal from '../components/modals/workspace-settings-modal';
|
2017-03-28 22:45:23 +00:00
|
|
|
|
import RequestSettingsModal from '../components/modals/request-settings-modal';
|
|
|
|
|
import RequestRenderErrorModal from '../components/modals/request-render-error-modal';
|
2017-03-16 17:51:56 +00:00
|
|
|
|
import * as network from '../../network/network';
|
2016-12-01 01:50:59 +00:00
|
|
|
|
import {debounce} from '../../common/misc';
|
2017-02-01 20:21:14 +00:00
|
|
|
|
import * as mime from 'mime-types';
|
|
|
|
|
import * as path from 'path';
|
2017-02-13 08:12:02 +00:00
|
|
|
|
import * as render from '../../common/render';
|
2017-03-12 00:31:23 +00:00
|
|
|
|
import {getKeys} from '../../templating/utils';
|
2017-07-17 22:37:24 +00:00
|
|
|
|
import {showAlert, showPrompt} from '../components/modals/index';
|
2017-06-29 18:27:29 +00:00
|
|
|
|
import {exportHar} from '../../common/har';
|
2016-08-15 17:04:36 +00:00
|
|
|
|
|
2017-01-27 01:00:27 +00:00
|
|
|
|
const KEY_ENTER = 13;
|
|
|
|
|
const KEY_COMMA = 188;
|
|
|
|
|
const KEY_D = 68;
|
|
|
|
|
const KEY_E = 69;
|
|
|
|
|
const KEY_K = 75;
|
|
|
|
|
const KEY_L = 76;
|
|
|
|
|
const KEY_N = 78;
|
|
|
|
|
const KEY_P = 80;
|
2017-06-07 21:51:24 +00:00
|
|
|
|
const KEY_R = 82;
|
|
|
|
|
const KEY_F5 = 116;
|
2016-03-20 04:47:43 +00:00
|
|
|
|
|
2017-03-03 01:44:07 +00:00
|
|
|
|
@autobind
|
2017-02-28 21:32:23 +00:00
|
|
|
|
class App extends PureComponent {
|
2016-12-01 01:50:59 +00:00
|
|
|
|
constructor (props) {
|
|
|
|
|
super(props);
|
2017-03-03 01:44:07 +00:00
|
|
|
|
|
2016-12-01 01:50:59 +00:00
|
|
|
|
this.state = {
|
2017-03-28 22:45:23 +00:00
|
|
|
|
showDragOverlay: false,
|
2016-12-01 01:50:59 +00:00
|
|
|
|
draggingSidebar: false,
|
2017-03-28 22:45:23 +00:00
|
|
|
|
draggingPaneHorizontal: false,
|
|
|
|
|
draggingPaneVertical: false,
|
2016-12-01 01:50:59 +00:00
|
|
|
|
sidebarWidth: props.sidebarWidth || DEFAULT_SIDEBAR_WIDTH,
|
2017-03-28 22:45:23 +00:00
|
|
|
|
paneWidth: props.paneWidth || DEFAULT_PANE_WIDTH,
|
|
|
|
|
paneHeight: props.paneHeight || DEFAULT_PANE_HEIGHT
|
2016-12-01 01:50:59 +00:00
|
|
|
|
};
|
2016-11-25 20:55:15 +00:00
|
|
|
|
|
2017-03-12 00:31:23 +00:00
|
|
|
|
this._getRenderContextPromiseCache = {};
|
2017-02-27 21:00:13 +00:00
|
|
|
|
|
2017-03-03 01:44:07 +00:00
|
|
|
|
this._savePaneWidth = debounce(paneWidth => this._updateActiveWorkspaceMeta({paneWidth}));
|
2017-03-28 22:45:23 +00:00
|
|
|
|
this._savePaneHeight = debounce(paneHeight => this._updateActiveWorkspaceMeta({paneHeight}));
|
2017-03-03 01:44:07 +00:00
|
|
|
|
this._saveSidebarWidth = debounce(sidebarWidth => this._updateActiveWorkspaceMeta({sidebarWidth}));
|
|
|
|
|
|
2017-03-03 21:10:35 +00:00
|
|
|
|
this._globalKeyMap = null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_setGlobalKeyMap () {
|
2017-01-27 01:00:27 +00:00
|
|
|
|
this._globalKeyMap = [
|
|
|
|
|
{ // Show Workspace Settings
|
|
|
|
|
meta: true,
|
|
|
|
|
shift: true,
|
2017-03-28 22:45:23 +00:00
|
|
|
|
alt: false,
|
2017-01-27 01:00:27 +00:00
|
|
|
|
key: KEY_COMMA,
|
|
|
|
|
callback: () => {
|
|
|
|
|
const {activeWorkspace} = this.props;
|
2017-03-28 22:45:23 +00:00
|
|
|
|
showModal(WorkspaceSettingsModal, activeWorkspace);
|
2017-01-27 01:00:27 +00:00
|
|
|
|
trackEvent('HotKey', 'Workspace Settings');
|
|
|
|
|
}
|
2017-03-28 22:45:23 +00:00
|
|
|
|
}, {
|
|
|
|
|
meta: true,
|
|
|
|
|
shift: true,
|
|
|
|
|
alt: true,
|
|
|
|
|
key: KEY_COMMA,
|
|
|
|
|
callback: () => {
|
|
|
|
|
if (this.props.activeRequest) {
|
2017-06-16 20:29:22 +00:00
|
|
|
|
showModal(RequestSettingsModal, {request: this.props.activeRequest});
|
2017-03-28 22:45:23 +00:00
|
|
|
|
trackEvent('HotKey', 'Request Settings');
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-01-27 01:00:27 +00:00
|
|
|
|
}, {
|
|
|
|
|
meta: true,
|
|
|
|
|
shift: false,
|
2017-03-28 22:45:23 +00:00
|
|
|
|
alt: false,
|
2017-01-27 01:00:27 +00:00
|
|
|
|
key: KEY_P,
|
|
|
|
|
callback: () => {
|
2017-03-28 22:45:23 +00:00
|
|
|
|
showModal(RequestSwitcherModal);
|
2017-01-27 01:00:27 +00:00
|
|
|
|
trackEvent('HotKey', 'Quick Switcher');
|
|
|
|
|
}
|
|
|
|
|
}, {
|
|
|
|
|
meta: true,
|
|
|
|
|
shift: false,
|
2017-03-28 22:45:23 +00:00
|
|
|
|
alt: false,
|
2017-06-07 21:51:24 +00:00
|
|
|
|
key: [KEY_ENTER, KEY_R],
|
|
|
|
|
callback: this._handleSendShortcut
|
|
|
|
|
}, {
|
|
|
|
|
meta: false,
|
|
|
|
|
shift: false,
|
|
|
|
|
alt: false,
|
|
|
|
|
key: [KEY_F5],
|
|
|
|
|
callback: this._handleSendShortcut
|
2017-01-27 01:00:27 +00:00
|
|
|
|
}, {
|
|
|
|
|
meta: true,
|
|
|
|
|
shift: false,
|
2017-03-28 22:45:23 +00:00
|
|
|
|
alt: false,
|
2017-01-27 01:00:27 +00:00
|
|
|
|
key: KEY_E,
|
|
|
|
|
callback: () => {
|
|
|
|
|
const {activeWorkspace} = this.props;
|
2017-03-28 22:45:23 +00:00
|
|
|
|
showModal(WorkspaceEnvironmentsEditModal, activeWorkspace);
|
2017-01-27 01:00:27 +00:00
|
|
|
|
trackEvent('HotKey', 'Environments');
|
|
|
|
|
}
|
|
|
|
|
}, {
|
|
|
|
|
meta: true,
|
|
|
|
|
shift: false,
|
2017-03-28 22:45:23 +00:00
|
|
|
|
alt: false,
|
2017-01-27 01:00:27 +00:00
|
|
|
|
key: KEY_L,
|
|
|
|
|
callback: () => {
|
|
|
|
|
const node = document.body.querySelector('.urlbar input');
|
|
|
|
|
node && node.focus();
|
|
|
|
|
trackEvent('HotKey', 'Url');
|
|
|
|
|
}
|
|
|
|
|
}, {
|
|
|
|
|
meta: true,
|
|
|
|
|
shift: false,
|
2017-03-28 22:45:23 +00:00
|
|
|
|
alt: false,
|
2017-01-27 01:00:27 +00:00
|
|
|
|
key: KEY_K,
|
|
|
|
|
callback: () => {
|
|
|
|
|
const {activeWorkspace} = this.props;
|
2017-03-28 22:45:23 +00:00
|
|
|
|
showModal(CookiesModal, activeWorkspace);
|
2017-01-27 01:00:27 +00:00
|
|
|
|
trackEvent('HotKey', 'Cookies');
|
|
|
|
|
}
|
|
|
|
|
}, {
|
|
|
|
|
meta: true,
|
|
|
|
|
shift: false,
|
2017-03-28 22:45:23 +00:00
|
|
|
|
alt: false,
|
2017-01-27 01:00:27 +00:00
|
|
|
|
key: KEY_N,
|
2017-06-09 21:42:19 +00:00
|
|
|
|
callback: () => {
|
2017-01-27 01:00:27 +00:00
|
|
|
|
const {activeRequest, activeWorkspace} = this.props;
|
|
|
|
|
const parentId = activeRequest ? activeRequest.parentId : activeWorkspace._id;
|
2017-06-09 21:42:19 +00:00
|
|
|
|
this._requestCreate(parentId);
|
2017-01-27 01:00:27 +00:00
|
|
|
|
trackEvent('HotKey', 'Request Create');
|
|
|
|
|
}
|
|
|
|
|
}, {
|
|
|
|
|
meta: true,
|
|
|
|
|
shift: false,
|
2017-03-28 22:45:23 +00:00
|
|
|
|
alt: false,
|
2017-01-27 01:00:27 +00:00
|
|
|
|
key: KEY_D,
|
2017-03-03 21:10:35 +00:00
|
|
|
|
callback: async () => {
|
|
|
|
|
await this._requestDuplicate(this.props.activeRequest);
|
2017-01-27 01:00:27 +00:00
|
|
|
|
trackEvent('HotKey', 'Request Duplicate');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
];
|
|
|
|
|
}
|
2016-07-19 16:59:26 +00:00
|
|
|
|
|
2017-06-07 21:51:24 +00:00
|
|
|
|
async _handleSendShortcut () {
|
|
|
|
|
const {activeRequest, activeEnvironment} = this.props;
|
|
|
|
|
await this._handleSendRequestWithEnvironment(
|
|
|
|
|
activeRequest ? activeRequest._id : 'n/a',
|
|
|
|
|
activeEnvironment ? activeEnvironment._id : 'n/a',
|
|
|
|
|
);
|
|
|
|
|
trackEvent('HotKey', 'Send');
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-03 01:44:07 +00:00
|
|
|
|
_setRequestPaneRef (n) {
|
|
|
|
|
this._requestPane = n;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_setResponsePaneRef (n) {
|
|
|
|
|
this._responsePane = n;
|
|
|
|
|
}
|
2016-11-25 20:55:15 +00:00
|
|
|
|
|
2017-03-03 01:44:07 +00:00
|
|
|
|
_setSidebarRef (n) {
|
|
|
|
|
this._sidebar = n;
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-28 22:45:23 +00:00
|
|
|
|
_isDragging () {
|
|
|
|
|
return (
|
|
|
|
|
this.state.draggingPaneHorizontal ||
|
|
|
|
|
this.state.draggingPaneVertical ||
|
|
|
|
|
this.state.draggingSidebar
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-01 02:04:27 +00:00
|
|
|
|
_requestGroupCreate (parentId) {
|
|
|
|
|
showPrompt({
|
2017-07-20 01:55:40 +00:00
|
|
|
|
title: 'New Folder',
|
2016-08-15 17:04:36 +00:00
|
|
|
|
defaultValue: 'My Folder',
|
2016-11-23 20:44:46 +00:00
|
|
|
|
submitName: 'Create',
|
2016-11-29 20:55:31 +00:00
|
|
|
|
label: 'Name',
|
2017-06-01 02:04:27 +00:00
|
|
|
|
selectText: true,
|
|
|
|
|
onComplete: name => {
|
|
|
|
|
models.requestGroup.create({parentId, name});
|
|
|
|
|
}
|
2016-08-15 17:04:36 +00:00
|
|
|
|
});
|
2017-03-03 01:44:07 +00:00
|
|
|
|
}
|
2016-08-15 17:04:36 +00:00
|
|
|
|
|
2017-06-09 21:42:19 +00:00
|
|
|
|
_requestCreate (parentId) {
|
|
|
|
|
showModal(RequestCreateModal, {
|
|
|
|
|
parentId,
|
|
|
|
|
onComplete: request => {
|
|
|
|
|
this._handleSetActiveRequest(request._id);
|
|
|
|
|
}
|
|
|
|
|
});
|
2017-03-03 01:44:07 +00:00
|
|
|
|
}
|
2016-04-17 20:35:35 +00:00
|
|
|
|
|
2017-03-03 01:44:07 +00:00
|
|
|
|
async _requestGroupDuplicate (requestGroup) {
|
2016-12-01 01:50:59 +00:00
|
|
|
|
models.requestGroup.duplicate(requestGroup);
|
2017-03-03 01:44:07 +00:00
|
|
|
|
}
|
2016-11-28 07:12:17 +00:00
|
|
|
|
|
2017-03-03 01:44:07 +00:00
|
|
|
|
async _requestDuplicate (request) {
|
2016-11-27 21:42:38 +00:00
|
|
|
|
if (!request) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const newRequest = await models.request.duplicate(request);
|
2017-03-03 20:09:08 +00:00
|
|
|
|
await this._handleSetActiveRequest(newRequest._id);
|
2017-03-03 01:44:07 +00:00
|
|
|
|
}
|
2017-02-13 08:12:02 +00:00
|
|
|
|
|
2017-06-07 00:07:09 +00:00
|
|
|
|
async _workspaceDuplicate (callback) {
|
|
|
|
|
const workspace = this.props.activeWorkspace;
|
|
|
|
|
showPrompt({
|
2017-07-20 01:55:40 +00:00
|
|
|
|
title: 'Duplicate Workspace',
|
2017-06-07 00:07:09 +00:00
|
|
|
|
defaultValue: `${workspace.name} (Copy)`,
|
|
|
|
|
submitName: 'Duplicate',
|
|
|
|
|
selectText: true,
|
|
|
|
|
onComplete: async name => {
|
|
|
|
|
const newWorkspace = await db.duplicate(workspace, {name});
|
|
|
|
|
await this.props.handleSetActiveWorkspace(newWorkspace._id);
|
|
|
|
|
callback();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-30 18:52:07 +00:00
|
|
|
|
async _fetchRenderContext () {
|
2017-03-12 00:31:23 +00:00
|
|
|
|
const {activeEnvironment, activeRequest} = this.props;
|
|
|
|
|
const environmentId = activeEnvironment ? activeEnvironment._id : null;
|
2017-07-11 01:05:54 +00:00
|
|
|
|
return render.getRenderContext(activeRequest, environmentId, null);
|
2017-03-12 00:31:23 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async _handleGetRenderContext () {
|
|
|
|
|
const context = await this._fetchRenderContext();
|
|
|
|
|
const keys = getKeys(context);
|
|
|
|
|
return {context, keys};
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-27 21:00:13 +00:00
|
|
|
|
/**
|
|
|
|
|
* Heavily optimized render function
|
|
|
|
|
*
|
|
|
|
|
* @param text - template to render
|
|
|
|
|
* @param contextCacheKey - if rendering multiple times in parallel, set this
|
|
|
|
|
* @returns {Promise}
|
|
|
|
|
* @private
|
|
|
|
|
*/
|
2017-04-11 21:20:01 +00:00
|
|
|
|
async _handleRenderText (text, contextCacheKey = null) {
|
2017-03-12 00:31:23 +00:00
|
|
|
|
if (!contextCacheKey || !this._getRenderContextPromiseCache[contextCacheKey]) {
|
|
|
|
|
const context = this._fetchRenderContext();
|
2017-02-27 21:00:13 +00:00
|
|
|
|
|
|
|
|
|
// NOTE: We're caching promises here to avoid race conditions
|
2017-03-12 00:31:23 +00:00
|
|
|
|
this._getRenderContextPromiseCache[contextCacheKey] = context;
|
2017-02-27 21:00:13 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Set timeout to delete the key eventually
|
2017-03-12 00:31:23 +00:00
|
|
|
|
setTimeout(() => delete this._getRenderContextPromiseCache[contextCacheKey], 5000);
|
2017-02-27 21:00:13 +00:00
|
|
|
|
|
2017-03-12 00:31:23 +00:00
|
|
|
|
const context = await this._getRenderContextPromiseCache[contextCacheKey];
|
2017-04-11 21:20:01 +00:00
|
|
|
|
return render.render(text, context);
|
2017-03-03 01:44:07 +00:00
|
|
|
|
}
|
2016-11-27 21:42:38 +00:00
|
|
|
|
|
2017-03-03 01:44:07 +00:00
|
|
|
|
_handleGenerateCodeForActiveRequest () {
|
2017-02-01 20:21:14 +00:00
|
|
|
|
this._handleGenerateCode(this.props.activeRequest);
|
2017-03-03 01:44:07 +00:00
|
|
|
|
}
|
2017-02-01 20:21:14 +00:00
|
|
|
|
|
2017-03-03 01:44:07 +00:00
|
|
|
|
_handleGenerateCode (request) {
|
2017-02-01 20:21:14 +00:00
|
|
|
|
showModal(GenerateCodeModal, request);
|
2017-03-03 01:44:07 +00:00
|
|
|
|
}
|
2016-11-28 07:12:17 +00:00
|
|
|
|
|
2017-06-29 18:27:29 +00:00
|
|
|
|
async _handleCopyAsCurl (request) {
|
|
|
|
|
const {activeEnvironment} = this.props;
|
|
|
|
|
const environmentId = activeEnvironment ? activeEnvironment._id : 'n/a';
|
|
|
|
|
const har = await exportHar(request._id, environmentId);
|
|
|
|
|
const snippet = new HTTPSnippet(har);
|
|
|
|
|
const cmd = snippet.convert('shell', 'curl');
|
|
|
|
|
clipboard.writeText(cmd);
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-03 01:44:07 +00:00
|
|
|
|
async _updateRequestGroupMetaByParentId (requestGroupId, patch) {
|
2016-12-01 01:50:59 +00:00
|
|
|
|
const requestGroupMeta = await models.requestGroupMeta.getByParentId(requestGroupId);
|
|
|
|
|
if (requestGroupMeta) {
|
|
|
|
|
await models.requestGroupMeta.update(requestGroupMeta, patch);
|
|
|
|
|
} else {
|
2016-12-01 03:54:26 +00:00
|
|
|
|
const newPatch = Object.assign({parentId: requestGroupId}, patch);
|
|
|
|
|
await models.requestGroupMeta.create(newPatch);
|
2016-12-01 01:50:59 +00:00
|
|
|
|
}
|
2017-03-03 01:44:07 +00:00
|
|
|
|
}
|
2016-11-25 23:09:17 +00:00
|
|
|
|
|
2017-03-03 01:44:07 +00:00
|
|
|
|
async _updateActiveWorkspaceMeta (patch) {
|
2016-12-01 01:50:59 +00:00
|
|
|
|
const workspaceId = this.props.activeWorkspace._id;
|
2017-07-27 22:59:07 +00:00
|
|
|
|
const workspaceMeta = await models.workspaceMeta.getOrCreateByParentId(workspaceId);
|
|
|
|
|
if (workspaceMeta) {
|
|
|
|
|
return models.workspaceMeta.update(workspaceMeta, patch);
|
2016-12-01 01:50:59 +00:00
|
|
|
|
} else {
|
2016-12-01 03:54:26 +00:00
|
|
|
|
const newPatch = Object.assign({parentId: workspaceId}, patch);
|
2017-06-08 02:42:16 +00:00
|
|
|
|
return models.workspaceMeta.create(newPatch);
|
2016-12-01 01:50:59 +00:00
|
|
|
|
}
|
2017-03-03 01:44:07 +00:00
|
|
|
|
}
|
2016-12-01 01:50:59 +00:00
|
|
|
|
|
2017-03-03 01:44:07 +00:00
|
|
|
|
async _updateRequestMetaByParentId (requestId, patch) {
|
2016-12-01 01:50:59 +00:00
|
|
|
|
const requestMeta = await models.requestMeta.getByParentId(requestId);
|
|
|
|
|
if (requestMeta) {
|
2017-06-08 02:42:16 +00:00
|
|
|
|
return models.requestMeta.update(requestMeta, patch);
|
2016-12-01 01:50:59 +00:00
|
|
|
|
} else {
|
2016-12-01 03:54:26 +00:00
|
|
|
|
const newPatch = Object.assign({parentId: requestId}, patch);
|
2017-06-08 02:42:16 +00:00
|
|
|
|
return models.requestMeta.create(newPatch);
|
2016-12-01 01:50:59 +00:00
|
|
|
|
}
|
2017-03-03 01:44:07 +00:00
|
|
|
|
}
|
2016-12-01 01:50:59 +00:00
|
|
|
|
|
2017-03-03 01:44:07 +00:00
|
|
|
|
_handleSetPaneWidth (paneWidth) {
|
2016-12-01 01:50:59 +00:00
|
|
|
|
this.setState({paneWidth});
|
|
|
|
|
this._savePaneWidth(paneWidth);
|
2017-03-03 01:44:07 +00:00
|
|
|
|
}
|
2016-12-01 01:50:59 +00:00
|
|
|
|
|
2017-03-28 22:45:23 +00:00
|
|
|
|
_handleSetPaneHeight (paneHeight) {
|
|
|
|
|
this.setState({paneHeight});
|
|
|
|
|
this._savePaneHeight(paneHeight);
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-03 01:44:07 +00:00
|
|
|
|
async _handleSetActiveRequest (activeRequestId) {
|
2017-02-13 08:12:02 +00:00
|
|
|
|
await this._updateActiveWorkspaceMeta({activeRequestId});
|
2017-03-03 20:09:08 +00:00
|
|
|
|
}
|
2016-12-01 01:50:59 +00:00
|
|
|
|
|
2017-03-03 01:44:07 +00:00
|
|
|
|
async _handleSetActiveEnvironment (activeEnvironmentId) {
|
2017-02-13 08:12:02 +00:00
|
|
|
|
await this._updateActiveWorkspaceMeta({activeEnvironmentId});
|
2017-03-29 02:21:49 +00:00
|
|
|
|
|
|
|
|
|
// Give it time to update and re-render
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
this._wrapper._forceRequestPaneRefresh();
|
|
|
|
|
}, 100);
|
2017-03-03 01:44:07 +00:00
|
|
|
|
}
|
2016-12-01 01:50:59 +00:00
|
|
|
|
|
2017-03-03 01:44:07 +00:00
|
|
|
|
_handleSetSidebarWidth (sidebarWidth) {
|
2016-12-01 01:50:59 +00:00
|
|
|
|
this.setState({sidebarWidth});
|
|
|
|
|
this._saveSidebarWidth(sidebarWidth);
|
2017-03-03 01:44:07 +00:00
|
|
|
|
}
|
2016-12-01 01:50:59 +00:00
|
|
|
|
|
2017-03-03 01:44:07 +00:00
|
|
|
|
async _handleSetSidebarHidden (sidebarHidden) {
|
2017-02-13 08:12:02 +00:00
|
|
|
|
await this._updateActiveWorkspaceMeta({sidebarHidden});
|
2017-03-03 20:09:08 +00:00
|
|
|
|
}
|
2016-12-01 01:50:59 +00:00
|
|
|
|
|
2017-03-03 01:44:07 +00:00
|
|
|
|
async _handleSetSidebarFilter (sidebarFilter) {
|
2017-02-13 08:12:02 +00:00
|
|
|
|
await this._updateActiveWorkspaceMeta({sidebarFilter});
|
2017-03-03 01:44:07 +00:00
|
|
|
|
}
|
2016-12-01 01:50:59 +00:00
|
|
|
|
|
2017-03-03 01:44:07 +00:00
|
|
|
|
_handleSetRequestGroupCollapsed (requestGroupId, collapsed) {
|
2016-12-01 01:50:59 +00:00
|
|
|
|
this._updateRequestGroupMetaByParentId(requestGroupId, {collapsed});
|
2017-03-03 01:44:07 +00:00
|
|
|
|
}
|
2016-12-01 01:50:59 +00:00
|
|
|
|
|
2017-03-03 01:44:07 +00:00
|
|
|
|
_handleSetResponsePreviewMode (requestId, previewMode) {
|
2016-12-01 01:50:59 +00:00
|
|
|
|
this._updateRequestMetaByParentId(requestId, {previewMode});
|
2017-03-03 01:44:07 +00:00
|
|
|
|
}
|
2016-12-01 01:50:59 +00:00
|
|
|
|
|
2017-06-08 02:42:16 +00:00
|
|
|
|
async _handleSetResponseFilter (requestId, responseFilter) {
|
|
|
|
|
await this._updateRequestMetaByParentId(requestId, {responseFilter});
|
|
|
|
|
|
|
|
|
|
clearTimeout(this._responseFilterHistorySaveTimeout);
|
|
|
|
|
this._responseFilterHistorySaveTimeout = setTimeout(async () => {
|
|
|
|
|
const meta = await models.requestMeta.getByParentId(requestId);
|
|
|
|
|
const responseFilterHistory = meta.responseFilterHistory.slice(0, 10);
|
|
|
|
|
|
|
|
|
|
// Already in history?
|
|
|
|
|
if (responseFilterHistory.includes(responseFilter)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-08 02:53:46 +00:00
|
|
|
|
// Blank?
|
|
|
|
|
if (!responseFilter) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-08 02:42:16 +00:00
|
|
|
|
responseFilterHistory.unshift(responseFilter);
|
|
|
|
|
await this._updateRequestMetaByParentId(requestId, {responseFilterHistory});
|
|
|
|
|
}, 2000);
|
2017-03-03 01:44:07 +00:00
|
|
|
|
}
|
2016-12-01 01:50:59 +00:00
|
|
|
|
|
2017-03-03 01:44:07 +00:00
|
|
|
|
async _handleSendAndDownloadRequestWithEnvironment (requestId, environmentId, dir) {
|
2017-02-01 20:21:14 +00:00
|
|
|
|
const request = await models.request.getById(requestId);
|
|
|
|
|
if (!request) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// NOTE: Since request is by far the most popular event, we will throttle
|
|
|
|
|
// it so that we only track it if the request has changed since the last one
|
|
|
|
|
const key = request._id;
|
|
|
|
|
if (this._sendRequestTrackingKey !== key) {
|
|
|
|
|
trackEvent('Request', 'Send and Download');
|
|
|
|
|
this._sendRequestTrackingKey = key;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Start loading
|
|
|
|
|
this.props.handleStartLoading(requestId);
|
|
|
|
|
|
|
|
|
|
try {
|
2017-06-30 03:30:22 +00:00
|
|
|
|
const {response: responsePatch, bodyBuffer} = await network.send(requestId, environmentId);
|
2017-02-01 20:21:14 +00:00
|
|
|
|
if (responsePatch.statusCode >= 200 && responsePatch.statusCode < 300) {
|
|
|
|
|
const extension = mime.extension(responsePatch.contentType) || '';
|
|
|
|
|
const name = request.name.replace(/\s/g, '-').toLowerCase();
|
|
|
|
|
const filename = path.join(dir, `${name}.${extension}`);
|
2017-06-30 03:30:22 +00:00
|
|
|
|
const partialResponse = Object.assign({}, responsePatch);
|
|
|
|
|
await models.response.create(partialResponse, `Saved to ${filename}`);
|
|
|
|
|
fs.writeFile(filename, bodyBuffer, err => {
|
|
|
|
|
if (err) {
|
|
|
|
|
console.warn('Failed to download request after sending', err);
|
|
|
|
|
}
|
2017-02-01 20:21:14 +00:00
|
|
|
|
});
|
|
|
|
|
} else {
|
2017-06-30 03:30:22 +00:00
|
|
|
|
await models.response.create(responsePatch, bodyBuffer);
|
2017-02-01 20:21:14 +00:00
|
|
|
|
}
|
2017-07-17 22:37:24 +00:00
|
|
|
|
} catch (err) {
|
|
|
|
|
showAlert({
|
|
|
|
|
title: 'Unexpected Request Failure',
|
|
|
|
|
message: (
|
|
|
|
|
<div>
|
|
|
|
|
<p>The request failed due to an unhandled error:</p>
|
|
|
|
|
<code className="wide selectable">
|
|
|
|
|
<pre>{err.message}</pre>
|
|
|
|
|
</code>
|
|
|
|
|
</div>
|
|
|
|
|
)
|
|
|
|
|
});
|
2017-02-01 20:21:14 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Unset active response because we just made a new one
|
|
|
|
|
await this._updateRequestMetaByParentId(requestId, {activeResponseId: null});
|
|
|
|
|
|
|
|
|
|
// Stop loading
|
|
|
|
|
this.props.handleStopLoading(requestId);
|
2017-03-03 01:44:07 +00:00
|
|
|
|
}
|
2017-02-01 20:21:14 +00:00
|
|
|
|
|
2017-03-03 01:44:07 +00:00
|
|
|
|
async _handleSendRequestWithEnvironment (requestId, environmentId) {
|
2016-12-05 22:42:40 +00:00
|
|
|
|
const request = await models.request.getById(requestId);
|
|
|
|
|
if (!request) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2016-12-01 01:50:59 +00:00
|
|
|
|
|
2016-12-05 22:42:40 +00:00
|
|
|
|
// NOTE: Since request is by far the most popular event, we will throttle
|
|
|
|
|
// it so that we only track it if the request has changed since the last noe
|
|
|
|
|
const key = `${request._id}::${request.modified}`;
|
|
|
|
|
if (this._sendRequestTrackingKey !== key) {
|
|
|
|
|
trackEvent('Request', 'Send');
|
|
|
|
|
this._sendRequestTrackingKey = key;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.props.handleStartLoading(requestId);
|
2016-12-01 01:50:59 +00:00
|
|
|
|
|
|
|
|
|
try {
|
2017-06-30 03:30:22 +00:00
|
|
|
|
const {response: responsePatch, bodyBuffer} = await network.send(requestId, environmentId);
|
|
|
|
|
await models.response.create(responsePatch, bodyBuffer);
|
2017-03-28 22:45:23 +00:00
|
|
|
|
} catch (err) {
|
|
|
|
|
if (err.type === 'render') {
|
|
|
|
|
showModal(RequestRenderErrorModal, {request, error: err});
|
2017-07-17 22:37:24 +00:00
|
|
|
|
} else {
|
|
|
|
|
showAlert({
|
|
|
|
|
title: 'Unexpected Request Failure',
|
|
|
|
|
message: (
|
|
|
|
|
<div>
|
|
|
|
|
<p>The request failed due to an unhandled error:</p>
|
|
|
|
|
<code className="wide selectable">
|
|
|
|
|
<pre>{err.message}</pre>
|
|
|
|
|
</code>
|
|
|
|
|
</div>
|
|
|
|
|
)
|
|
|
|
|
});
|
2017-03-28 22:45:23 +00:00
|
|
|
|
}
|
2016-12-01 01:50:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Unset active response because we just made a new one
|
|
|
|
|
await this._updateRequestMetaByParentId(requestId, {activeResponseId: null});
|
|
|
|
|
|
|
|
|
|
// Stop loading
|
|
|
|
|
this.props.handleStopLoading(requestId);
|
2017-03-03 01:44:07 +00:00
|
|
|
|
}
|
2016-12-01 01:50:59 +00:00
|
|
|
|
|
2017-07-17 18:20:38 +00:00
|
|
|
|
async _handleSetActiveResponse (requestId, activeResponse = null) {
|
|
|
|
|
const activeResponseId = activeResponse ? activeResponse._id : null;
|
2017-06-12 21:49:46 +00:00
|
|
|
|
await this._updateRequestMetaByParentId(requestId, {activeResponseId});
|
|
|
|
|
|
|
|
|
|
let response;
|
|
|
|
|
if (activeResponseId) {
|
|
|
|
|
response = await models.response.getById(activeResponseId);
|
|
|
|
|
} else {
|
|
|
|
|
response = await models.response.getLatestForRequest(requestId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const requestVersionId = response ? response.requestVersionId : 'n/a';
|
|
|
|
|
const request = await models.requestVersion.restore(requestVersionId);
|
|
|
|
|
|
|
|
|
|
if (request) {
|
|
|
|
|
// Refresh app to reflect changes. Using timeout because we need to
|
|
|
|
|
// wait for the request update to propagate.
|
|
|
|
|
setTimeout(() => this._wrapper._forceRequestPaneRefresh(), 500);
|
|
|
|
|
} else {
|
|
|
|
|
// Couldn't restore request. That's okay
|
|
|
|
|
}
|
2017-03-03 01:44:07 +00:00
|
|
|
|
}
|
2016-12-01 01:50:59 +00:00
|
|
|
|
|
2017-03-03 01:44:07 +00:00
|
|
|
|
_requestCreateForWorkspace () {
|
2016-12-01 01:50:59 +00:00
|
|
|
|
this._requestCreate(this.props.activeWorkspace._id);
|
2017-03-03 01:44:07 +00:00
|
|
|
|
}
|
2016-11-25 23:09:17 +00:00
|
|
|
|
|
2017-03-03 01:44:07 +00:00
|
|
|
|
_startDragSidebar () {
|
2016-12-05 22:42:40 +00:00
|
|
|
|
trackEvent('Sidebar', 'Drag');
|
2017-03-03 20:09:08 +00:00
|
|
|
|
this.setState({draggingSidebar: true});
|
2017-03-03 01:44:07 +00:00
|
|
|
|
}
|
2016-06-19 00:08:14 +00:00
|
|
|
|
|
2017-03-03 01:44:07 +00:00
|
|
|
|
_resetDragSidebar () {
|
2016-12-05 22:42:40 +00:00
|
|
|
|
trackEvent('Sidebar', 'Drag');
|
2016-07-06 22:11:37 +00:00
|
|
|
|
// TODO: Remove setTimeout need be not triggering drag on double click
|
2016-12-01 01:50:59 +00:00
|
|
|
|
setTimeout(() => this._handleSetSidebarWidth(DEFAULT_SIDEBAR_WIDTH), 50);
|
2017-03-03 01:44:07 +00:00
|
|
|
|
}
|
2016-06-19 00:08:14 +00:00
|
|
|
|
|
2017-03-28 22:45:23 +00:00
|
|
|
|
_startDragPaneHorizontal () {
|
2016-11-23 19:33:24 +00:00
|
|
|
|
trackEvent('App Pane', 'Drag Start');
|
2017-03-28 22:45:23 +00:00
|
|
|
|
this.setState({draggingPaneHorizontal: true});
|
2017-03-03 01:44:07 +00:00
|
|
|
|
}
|
2016-06-19 00:08:14 +00:00
|
|
|
|
|
2017-03-28 22:45:23 +00:00
|
|
|
|
_startDragPaneVertical () {
|
|
|
|
|
trackEvent('App Pane', 'Drag Start Vertical');
|
|
|
|
|
this.setState({draggingPaneVertical: true});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_resetDragPaneHorizontal () {
|
2016-12-05 22:42:40 +00:00
|
|
|
|
trackEvent('App Pane', 'Drag Reset');
|
2016-07-06 22:11:37 +00:00
|
|
|
|
// TODO: Remove setTimeout need be not triggering drag on double click
|
2016-12-01 01:50:59 +00:00
|
|
|
|
setTimeout(() => this._handleSetPaneWidth(DEFAULT_PANE_WIDTH), 50);
|
2017-03-03 01:44:07 +00:00
|
|
|
|
}
|
2016-07-06 22:11:37 +00:00
|
|
|
|
|
2017-03-28 22:45:23 +00:00
|
|
|
|
_resetDragPaneVertical () {
|
|
|
|
|
trackEvent('App Pane', 'Drag Reset Vertical');
|
|
|
|
|
// TODO: Remove setTimeout need be not triggering drag on double click
|
|
|
|
|
setTimeout(() => this._handleSetPaneHeight(DEFAULT_PANE_HEIGHT), 50);
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-03 01:44:07 +00:00
|
|
|
|
_handleMouseMove (e) {
|
2017-03-28 22:45:23 +00:00
|
|
|
|
if (this.state.draggingPaneHorizontal) {
|
|
|
|
|
// Only pop the overlay after we've moved it a bit (so we don't block doubleclick);
|
|
|
|
|
const distance = this.props.paneWidth - this.state.paneWidth;
|
|
|
|
|
if (!this.state.showDragOverlay && Math.abs(distance) > 0.02 /* % */) {
|
|
|
|
|
this.setState({showDragOverlay: true});
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-03 04:32:45 +00:00
|
|
|
|
const requestPane = ReactDOM.findDOMNode(this._requestPane);
|
|
|
|
|
const responsePane = ReactDOM.findDOMNode(this._responsePane);
|
2016-07-06 20:18:26 +00:00
|
|
|
|
|
|
|
|
|
const requestPaneWidth = requestPane.offsetWidth;
|
|
|
|
|
const responsePaneWidth = responsePane.offsetWidth;
|
2017-03-12 00:31:23 +00:00
|
|
|
|
|
2016-07-06 20:18:26 +00:00
|
|
|
|
const pixelOffset = e.clientX - requestPane.offsetLeft;
|
|
|
|
|
let paneWidth = pixelOffset / (requestPaneWidth + responsePaneWidth);
|
|
|
|
|
paneWidth = Math.min(Math.max(paneWidth, MIN_PANE_WIDTH), MAX_PANE_WIDTH);
|
2017-03-12 00:31:23 +00:00
|
|
|
|
|
2016-12-01 01:50:59 +00:00
|
|
|
|
this._handleSetPaneWidth(paneWidth);
|
2017-03-28 22:45:23 +00:00
|
|
|
|
} else if (this.state.draggingPaneVertical) {
|
|
|
|
|
// Only pop the overlay after we've moved it a bit (so we don't block doubleclick);
|
|
|
|
|
const distance = this.props.paneHeight - this.state.paneHeight;
|
|
|
|
|
if (!this.state.showDragOverlay && Math.abs(distance) > 0.02 /* % */) {
|
|
|
|
|
this.setState({showDragOverlay: true});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const requestPane = ReactDOM.findDOMNode(this._requestPane);
|
|
|
|
|
const responsePane = ReactDOM.findDOMNode(this._responsePane);
|
|
|
|
|
|
|
|
|
|
const requestPaneHeight = requestPane.offsetHeight;
|
|
|
|
|
const responsePaneHeight = responsePane.offsetHeight;
|
|
|
|
|
|
|
|
|
|
const pixelOffset = e.clientY - requestPane.offsetTop;
|
|
|
|
|
let paneHeight = pixelOffset / (requestPaneHeight + responsePaneHeight);
|
|
|
|
|
paneHeight = Math.min(Math.max(paneHeight, MIN_PANE_HEIGHT), MAX_PANE_HEIGHT);
|
|
|
|
|
|
|
|
|
|
this._handleSetPaneHeight(paneHeight);
|
2016-07-06 20:18:26 +00:00
|
|
|
|
} else if (this.state.draggingSidebar) {
|
2017-03-28 22:45:23 +00:00
|
|
|
|
// Only pop the overlay after we've moved it a bit (so we don't block doubleclick);
|
|
|
|
|
const distance = this.props.sidebarWidth - this.state.sidebarWidth;
|
|
|
|
|
if (!this.state.showDragOverlay && Math.abs(distance) > 2 /* ems */) {
|
|
|
|
|
this.setState({showDragOverlay: true});
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-03 04:32:45 +00:00
|
|
|
|
const currentPixelWidth = ReactDOM.findDOMNode(this._sidebar).offsetWidth;
|
2016-07-06 20:18:26 +00:00
|
|
|
|
const ratio = e.clientX / currentPixelWidth;
|
2016-12-01 01:50:59 +00:00
|
|
|
|
const width = this.state.sidebarWidth * ratio;
|
2017-03-12 00:31:23 +00:00
|
|
|
|
|
2017-04-21 04:30:52 +00:00
|
|
|
|
let sidebarWidth = Math.min(width, MAX_SIDEBAR_REMS);
|
|
|
|
|
|
|
|
|
|
if (sidebarWidth < COLLAPSE_SIDEBAR_REMS) {
|
|
|
|
|
sidebarWidth = MIN_SIDEBAR_REMS;
|
|
|
|
|
}
|
2017-03-12 00:31:23 +00:00
|
|
|
|
|
2016-12-01 01:50:59 +00:00
|
|
|
|
this._handleSetSidebarWidth(sidebarWidth);
|
2016-07-06 20:18:26 +00:00
|
|
|
|
}
|
2017-03-03 01:44:07 +00:00
|
|
|
|
}
|
2016-07-06 20:18:26 +00:00
|
|
|
|
|
2017-03-03 01:44:07 +00:00
|
|
|
|
_handleMouseUp () {
|
2016-07-06 20:18:26 +00:00
|
|
|
|
if (this.state.draggingSidebar) {
|
2017-03-28 22:45:23 +00:00
|
|
|
|
this.setState({draggingSidebar: false, showDragOverlay: false});
|
2016-07-06 20:18:26 +00:00
|
|
|
|
}
|
2016-03-22 05:01:58 +00:00
|
|
|
|
|
2017-03-28 22:45:23 +00:00
|
|
|
|
if (this.state.draggingPaneHorizontal) {
|
|
|
|
|
this.setState({draggingPaneHorizontal: false, showDragOverlay: false});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (this.state.draggingPaneVertical) {
|
|
|
|
|
this.setState({draggingPaneVertical: false, showDragOverlay: false});
|
2016-06-19 07:21:43 +00:00
|
|
|
|
}
|
2017-03-03 01:44:07 +00:00
|
|
|
|
}
|
2016-07-06 20:18:26 +00:00
|
|
|
|
|
2017-03-03 01:44:07 +00:00
|
|
|
|
_handleKeyDown (e) {
|
2017-01-27 01:00:27 +00:00
|
|
|
|
const isMetaPressed = isMac() ? e.metaKey : e.ctrlKey;
|
2017-03-28 22:45:23 +00:00
|
|
|
|
const isAltPressed = isMac() ? e.ctrlKey : e.altKey;
|
2017-01-27 01:00:27 +00:00
|
|
|
|
const isShiftPressed = e.shiftKey;
|
|
|
|
|
|
2017-03-28 22:45:23 +00:00
|
|
|
|
for (const {meta, shift, alt, key, callback} of this._globalKeyMap) {
|
2017-06-07 21:51:24 +00:00
|
|
|
|
const keys = Array.isArray(key) ? key : [key];
|
|
|
|
|
for (const key of keys) {
|
|
|
|
|
if ((alt && !isAltPressed) || (!alt && isAltPressed)) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2017-01-27 01:00:27 +00:00
|
|
|
|
|
2017-06-07 21:51:24 +00:00
|
|
|
|
if ((meta && !isMetaPressed) || (!meta && isMetaPressed)) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2017-03-28 22:45:23 +00:00
|
|
|
|
|
2017-06-07 21:51:24 +00:00
|
|
|
|
if ((shift && !isShiftPressed) || (!shift && isShiftPressed)) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2017-01-27 01:00:27 +00:00
|
|
|
|
|
2017-06-07 21:51:24 +00:00
|
|
|
|
if (key !== e.keyCode) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2017-01-27 01:00:27 +00:00
|
|
|
|
|
2017-06-07 21:51:24 +00:00
|
|
|
|
callback();
|
|
|
|
|
}
|
2017-01-27 01:00:27 +00:00
|
|
|
|
}
|
2017-03-03 01:44:07 +00:00
|
|
|
|
}
|
2017-01-27 01:00:27 +00:00
|
|
|
|
|
2017-05-24 16:25:22 +00:00
|
|
|
|
_handleToggleMenuBar (hide) {
|
|
|
|
|
for (const win of remote.BrowserWindow.getAllWindows()) {
|
2017-05-24 16:46:18 +00:00
|
|
|
|
if (win.isMenuBarAutoHide() !== hide) {
|
2017-05-24 16:25:22 +00:00
|
|
|
|
win.setAutoHideMenuBar(hide);
|
|
|
|
|
win.setMenuBarVisibility(!hide);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-03 01:44:07 +00:00
|
|
|
|
async _handleToggleSidebar () {
|
2016-12-01 01:50:59 +00:00
|
|
|
|
const sidebarHidden = !this.props.sidebarHidden;
|
2017-02-27 21:00:13 +00:00
|
|
|
|
await this._handleSetSidebarHidden(sidebarHidden);
|
2016-12-01 01:50:59 +00:00
|
|
|
|
trackEvent('Sidebar', 'Toggle Visibility', sidebarHidden ? 'Hide' : 'Show');
|
2017-03-03 01:44:07 +00:00
|
|
|
|
}
|
2016-07-06 20:18:26 +00:00
|
|
|
|
|
2017-03-03 01:44:07 +00:00
|
|
|
|
_setWrapperRef (n) {
|
|
|
|
|
this._wrapper = n;
|
|
|
|
|
}
|
2016-11-09 04:16:38 +00:00
|
|
|
|
|
2017-03-12 09:03:57 +00:00
|
|
|
|
/**
|
|
|
|
|
* Update document.title to be "Workspace (Environment) – Request"
|
|
|
|
|
* @private
|
|
|
|
|
*/
|
|
|
|
|
_updateDocumentTitle () {
|
|
|
|
|
const {
|
|
|
|
|
activeWorkspace,
|
|
|
|
|
activeEnvironment,
|
|
|
|
|
activeRequest
|
|
|
|
|
} = this.props;
|
|
|
|
|
|
|
|
|
|
let title = activeWorkspace.name;
|
|
|
|
|
|
|
|
|
|
if (activeEnvironment) {
|
|
|
|
|
title += ` (${activeEnvironment.name})`;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (activeRequest) {
|
|
|
|
|
title += ` – ${activeRequest.name}`;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
document.title = title;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
componentDidUpdate () {
|
|
|
|
|
this._updateDocumentTitle();
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-02 20:57:00 +00:00
|
|
|
|
async componentDidMount () {
|
2017-01-27 01:00:27 +00:00
|
|
|
|
// Bind mouse and key handlers
|
2016-07-06 20:18:26 +00:00
|
|
|
|
document.addEventListener('mouseup', this._handleMouseUp);
|
|
|
|
|
document.addEventListener('mousemove', this._handleMouseMove);
|
2017-01-27 01:00:27 +00:00
|
|
|
|
document.addEventListener('keydown', this._handleKeyDown);
|
2017-03-03 21:10:35 +00:00
|
|
|
|
this._setGlobalKeyMap();
|
2016-07-25 22:27:29 +00:00
|
|
|
|
|
2017-03-12 09:03:57 +00:00
|
|
|
|
// Update title
|
|
|
|
|
this._updateDocumentTitle();
|
2016-07-25 22:27:29 +00:00
|
|
|
|
|
|
|
|
|
// Update Stats Object
|
2016-11-10 01:15:27 +00:00
|
|
|
|
const {lastVersion, launches} = await models.stats.get();
|
2016-10-02 20:57:00 +00:00
|
|
|
|
const firstLaunch = !lastVersion;
|
|
|
|
|
if (firstLaunch) {
|
|
|
|
|
// TODO: Show a welcome message
|
2016-12-07 17:57:01 +00:00
|
|
|
|
trackEvent('General', 'First Launch', getAppVersion(), {nonInteraction: true});
|
2016-10-02 20:57:00 +00:00
|
|
|
|
} else if (lastVersion !== getAppVersion()) {
|
2016-12-07 17:57:01 +00:00
|
|
|
|
trackEvent('General', 'Updated', getAppVersion(), {nonInteraction: true});
|
2016-11-07 20:24:38 +00:00
|
|
|
|
showModal(ChangelogModal);
|
2016-12-05 22:42:40 +00:00
|
|
|
|
} else {
|
2016-12-07 17:57:01 +00:00
|
|
|
|
trackEvent('General', 'Launched', getAppVersion(), {nonInteraction: true});
|
2016-10-02 20:57:00 +00:00
|
|
|
|
}
|
2016-07-25 22:27:29 +00:00
|
|
|
|
|
2017-02-13 08:12:02 +00:00
|
|
|
|
db.onChange(async changes => {
|
2016-10-24 23:30:37 +00:00
|
|
|
|
for (const change of changes) {
|
2017-03-03 20:09:08 +00:00
|
|
|
|
const [
|
|
|
|
|
_, // eslint-disable-line no-unused-vars
|
|
|
|
|
doc,
|
|
|
|
|
fromSync
|
|
|
|
|
] = change;
|
2017-03-12 09:03:57 +00:00
|
|
|
|
|
2016-11-16 17:18:39 +00:00
|
|
|
|
const {activeRequest} = this.props;
|
2016-11-07 20:24:38 +00:00
|
|
|
|
|
2016-12-03 05:15:38 +00:00
|
|
|
|
// No active request, so we don't need to force refresh anything
|
2016-11-07 20:24:38 +00:00
|
|
|
|
if (!activeRequest) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-03 05:15:38 +00:00
|
|
|
|
// Force refresh if environment changes
|
2017-01-13 19:21:03 +00:00
|
|
|
|
// TODO: Only do this for environments in this workspace (not easy because they're nested)
|
2016-12-03 05:15:38 +00:00
|
|
|
|
if (doc.type === models.environment.type) {
|
2017-01-11 21:59:35 +00:00
|
|
|
|
console.log('[App] Forcing update from environment change', change);
|
2017-02-28 21:32:23 +00:00
|
|
|
|
this._wrapper._forceRequestPaneRefresh();
|
2016-10-24 23:30:37 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-12-03 05:15:38 +00:00
|
|
|
|
// Force refresh if sync changes the active request
|
|
|
|
|
if (fromSync && doc._id === activeRequest._id) {
|
2017-02-28 21:32:23 +00:00
|
|
|
|
this._wrapper._forceRequestPaneRefresh();
|
2017-01-11 21:59:35 +00:00
|
|
|
|
console.log('[App] Forcing update from request change', change);
|
2016-12-03 05:15:38 +00:00
|
|
|
|
}
|
2016-10-24 23:30:37 +00:00
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2016-11-10 01:15:27 +00:00
|
|
|
|
models.stats.update({
|
2016-10-02 20:57:00 +00:00
|
|
|
|
launches: launches + 1,
|
|
|
|
|
lastLaunch: Date.now(),
|
|
|
|
|
lastVersion: getAppVersion()
|
2016-07-25 22:27:29 +00:00
|
|
|
|
});
|
2016-07-27 20:07:50 +00:00
|
|
|
|
|
2016-08-22 20:05:42 +00:00
|
|
|
|
ipcRenderer.on('toggle-preferences', () => {
|
2017-03-28 22:45:23 +00:00
|
|
|
|
showModal(SettingsModal);
|
2016-09-12 20:04:15 +00:00
|
|
|
|
});
|
|
|
|
|
|
2017-08-04 16:54:11 +00:00
|
|
|
|
ipcRenderer.on('toggle-preferences-shortcuts', () => {
|
|
|
|
|
showModal(SettingsModal, TAB_INDEX_SHORTCUTS);
|
|
|
|
|
});
|
|
|
|
|
|
2017-05-03 17:48:23 +00:00
|
|
|
|
ipcRenderer.on('run-command', (e, commandUri) => {
|
|
|
|
|
const parsed = urlParse(commandUri, true);
|
|
|
|
|
|
|
|
|
|
const command = `${parsed.hostname}${parsed.pathname}`;
|
|
|
|
|
const args = JSON.parse(JSON.stringify(parsed.query));
|
|
|
|
|
args.workspaceId = args.workspaceId || this.props.activeWorkspace._id;
|
|
|
|
|
|
|
|
|
|
this.props.handleCommand(command, args);
|
|
|
|
|
});
|
|
|
|
|
|
2016-11-23 19:59:26 +00:00
|
|
|
|
ipcRenderer.on('toggle-changelog', () => {
|
2017-03-28 22:45:23 +00:00
|
|
|
|
showModal(ChangelogModal);
|
2016-11-23 19:59:26 +00:00
|
|
|
|
});
|
|
|
|
|
|
2016-11-25 20:55:15 +00:00
|
|
|
|
ipcRenderer.on('toggle-sidebar', this._handleToggleSidebar);
|
2017-05-03 17:48:23 +00:00
|
|
|
|
|
|
|
|
|
process.nextTick(() => ipcRenderer.send('app-ready'));
|
2017-05-24 16:25:22 +00:00
|
|
|
|
|
|
|
|
|
// handle this
|
|
|
|
|
this._handleToggleMenuBar(this.props.settings.autoHideMenuBar);
|
2016-07-06 20:18:26 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-07-07 20:10:55 +00:00
|
|
|
|
componentWillUnmount () {
|
2017-01-27 01:00:27 +00:00
|
|
|
|
// Remove mouse and key handlers
|
2016-07-06 20:18:26 +00:00
|
|
|
|
document.removeEventListener('mouseup', this._handleMouseUp);
|
|
|
|
|
document.removeEventListener('mousemove', this._handleMouseMove);
|
2017-01-27 01:00:27 +00:00
|
|
|
|
document.removeEventListener('keydown', this._handleKeyDown);
|
2016-07-06 20:18:26 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-07-07 20:10:55 +00:00
|
|
|
|
render () {
|
2016-11-16 17:18:39 +00:00
|
|
|
|
return (
|
|
|
|
|
<div className="app">
|
|
|
|
|
<Wrapper
|
2016-12-01 01:50:59 +00:00
|
|
|
|
{...this.props}
|
2016-11-26 00:37:59 +00:00
|
|
|
|
ref={this._setWrapperRef}
|
2016-12-01 01:50:59 +00:00
|
|
|
|
paneWidth={this.state.paneWidth}
|
2017-03-28 22:45:23 +00:00
|
|
|
|
paneHeight={this.state.paneHeight}
|
2016-12-01 01:50:59 +00:00
|
|
|
|
sidebarWidth={this.state.sidebarWidth}
|
2016-11-25 23:09:17 +00:00
|
|
|
|
handleCreateRequestForWorkspace={this._requestCreateForWorkspace}
|
2016-12-01 01:50:59 +00:00
|
|
|
|
handleSetRequestGroupCollapsed={this._handleSetRequestGroupCollapsed}
|
|
|
|
|
handleActivateRequest={this._handleSetActiveRequest}
|
2016-11-25 20:55:15 +00:00
|
|
|
|
handleSetRequestPaneRef={this._setRequestPaneRef}
|
|
|
|
|
handleSetResponsePaneRef={this._setResponsePaneRef}
|
|
|
|
|
handleSetSidebarRef={this._setSidebarRef}
|
|
|
|
|
handleStartDragSidebar={this._startDragSidebar}
|
|
|
|
|
handleResetDragSidebar={this._resetDragSidebar}
|
2017-03-28 22:45:23 +00:00
|
|
|
|
handleStartDragPaneHorizontal={this._startDragPaneHorizontal}
|
|
|
|
|
handleStartDragPaneVertical={this._startDragPaneVertical}
|
|
|
|
|
handleResetDragPaneHorizontal={this._resetDragPaneHorizontal}
|
|
|
|
|
handleResetDragPaneVertical={this._resetDragPaneVertical}
|
2016-11-25 20:55:15 +00:00
|
|
|
|
handleCreateRequest={this._requestCreate}
|
2017-02-13 08:12:02 +00:00
|
|
|
|
handleRender={this._handleRenderText}
|
2017-03-12 00:31:23 +00:00
|
|
|
|
handleGetRenderContext={this._handleGetRenderContext}
|
2016-11-27 21:42:38 +00:00
|
|
|
|
handleDuplicateRequest={this._requestDuplicate}
|
2016-11-28 07:12:17 +00:00
|
|
|
|
handleDuplicateRequestGroup={this._requestGroupDuplicate}
|
2017-06-07 00:07:09 +00:00
|
|
|
|
handleDuplicateWorkspace={this._workspaceDuplicate}
|
2016-11-25 20:55:15 +00:00
|
|
|
|
handleCreateRequestGroup={this._requestGroupCreate}
|
2016-11-28 07:12:17 +00:00
|
|
|
|
handleGenerateCode={this._handleGenerateCode}
|
2017-02-01 20:21:14 +00:00
|
|
|
|
handleGenerateCodeForActiveRequest={this._handleGenerateCodeForActiveRequest}
|
2017-06-29 18:27:29 +00:00
|
|
|
|
handleCopyAsCurl={this._handleCopyAsCurl}
|
2016-12-01 01:50:59 +00:00
|
|
|
|
handleSetResponsePreviewMode={this._handleSetResponsePreviewMode}
|
|
|
|
|
handleSetResponseFilter={this._handleSetResponseFilter}
|
|
|
|
|
handleSendRequestWithEnvironment={this._handleSendRequestWithEnvironment}
|
2017-02-01 20:21:14 +00:00
|
|
|
|
handleSendAndDownloadRequestWithEnvironment={this._handleSendAndDownloadRequestWithEnvironment}
|
2016-12-01 01:50:59 +00:00
|
|
|
|
handleSetActiveResponse={this._handleSetActiveResponse}
|
|
|
|
|
handleSetActiveRequest={this._handleSetActiveRequest}
|
|
|
|
|
handleSetActiveEnvironment={this._handleSetActiveEnvironment}
|
|
|
|
|
handleSetSidebarFilter={this._handleSetSidebarFilter}
|
2017-05-24 16:25:22 +00:00
|
|
|
|
handleToggleMenuBar={this._handleToggleMenuBar}
|
2016-11-16 17:18:39 +00:00
|
|
|
|
/>
|
|
|
|
|
<Toast/>
|
2017-03-28 22:45:23 +00:00
|
|
|
|
|
|
|
|
|
{/* Block all mouse activity by showing an overlay while dragging */}
|
|
|
|
|
{this.state.showDragOverlay ? <div className="blocker-overlay"></div> : null}
|
2016-11-16 17:18:39 +00:00
|
|
|
|
</div>
|
2017-03-03 20:09:08 +00:00
|
|
|
|
);
|
2016-11-16 17:18:39 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2016-07-06 20:18:26 +00:00
|
|
|
|
|
2017-03-08 05:52:17 +00:00
|
|
|
|
App.propTypes = {
|
|
|
|
|
// Required
|
|
|
|
|
sidebarWidth: PropTypes.number.isRequired,
|
|
|
|
|
paneWidth: PropTypes.number.isRequired,
|
2017-03-28 22:45:23 +00:00
|
|
|
|
paneHeight: PropTypes.number.isRequired,
|
2017-05-03 17:48:23 +00:00
|
|
|
|
handleCommand: PropTypes.func.isRequired,
|
2017-05-24 16:25:22 +00:00
|
|
|
|
settings: PropTypes.object.isRequired,
|
2017-03-08 05:52:17 +00:00
|
|
|
|
activeWorkspace: PropTypes.shape({
|
|
|
|
|
_id: PropTypes.string.isRequired
|
|
|
|
|
}).isRequired,
|
2017-06-07 00:07:09 +00:00
|
|
|
|
handleSetActiveWorkspace: PropTypes.func.isRequired,
|
2017-03-08 05:52:17 +00:00
|
|
|
|
|
|
|
|
|
// Optional
|
2017-03-12 00:31:23 +00:00
|
|
|
|
activeRequest: PropTypes.object,
|
|
|
|
|
activeEnvironment: PropTypes.shape({
|
|
|
|
|
_id: PropTypes.string.isRequired
|
|
|
|
|
})
|
2017-03-08 05:52:17 +00:00
|
|
|
|
};
|
|
|
|
|
|
2016-11-25 23:09:17 +00:00
|
|
|
|
function mapStateToProps (state, props) {
|
2016-11-16 17:18:39 +00:00
|
|
|
|
const {
|
|
|
|
|
entities,
|
2016-12-01 01:50:59 +00:00
|
|
|
|
global
|
2016-11-16 17:18:39 +00:00
|
|
|
|
} = state;
|
|
|
|
|
|
|
|
|
|
const {
|
|
|
|
|
isLoading,
|
2017-03-03 20:09:08 +00:00
|
|
|
|
loadingRequestIds
|
2016-11-16 17:18:39 +00:00
|
|
|
|
} = global;
|
|
|
|
|
|
|
|
|
|
// Entities
|
2016-11-25 23:09:17 +00:00
|
|
|
|
const entitiesLists = selectEntitiesLists(state, props);
|
|
|
|
|
const {
|
|
|
|
|
workspaces,
|
|
|
|
|
environments,
|
|
|
|
|
requests,
|
|
|
|
|
requestGroups
|
|
|
|
|
} = entitiesLists;
|
|
|
|
|
|
|
|
|
|
const settings = entitiesLists.settings[0];
|
2016-11-16 17:18:39 +00:00
|
|
|
|
|
|
|
|
|
// Workspace stuff
|
2016-12-01 01:50:59 +00:00
|
|
|
|
const workspaceMeta = selectActiveWorkspaceMeta(state, props) || {};
|
2016-11-25 23:09:17 +00:00
|
|
|
|
const activeWorkspace = selectActiveWorkspace(state, props);
|
2016-12-01 01:50:59 +00:00
|
|
|
|
const sidebarHidden = workspaceMeta.sidebarHidden || false;
|
|
|
|
|
const sidebarFilter = workspaceMeta.sidebarFilter || '';
|
|
|
|
|
const sidebarWidth = workspaceMeta.sidebarWidth || DEFAULT_SIDEBAR_WIDTH;
|
|
|
|
|
const paneWidth = workspaceMeta.paneWidth || DEFAULT_PANE_WIDTH;
|
2017-03-28 22:45:23 +00:00
|
|
|
|
const paneHeight = workspaceMeta.paneHeight || DEFAULT_PANE_HEIGHT;
|
2016-11-16 17:18:39 +00:00
|
|
|
|
|
|
|
|
|
// Request stuff
|
2016-12-01 01:50:59 +00:00
|
|
|
|
const requestMeta = selectActiveRequestMeta(state, props) || {};
|
|
|
|
|
const activeRequest = selectActiveRequest(state, props);
|
|
|
|
|
const responsePreviewMode = requestMeta.previewMode || PREVIEW_MODE_SOURCE;
|
|
|
|
|
const responseFilter = requestMeta.responseFilter || '';
|
2017-06-08 02:42:16 +00:00
|
|
|
|
const responseFilterHistory = requestMeta.responseFilterHistory || [];
|
2017-07-17 18:20:38 +00:00
|
|
|
|
|
|
|
|
|
// Response stuff
|
|
|
|
|
const activeRequestResponses = selectActiveRequestResponses(state, props) || [];
|
|
|
|
|
const activeResponse = selectActiveResponse(state, props) || null;
|
2016-11-27 21:42:38 +00:00
|
|
|
|
|
2016-11-16 17:18:39 +00:00
|
|
|
|
// Environment stuff
|
2016-12-01 01:50:59 +00:00
|
|
|
|
const activeEnvironmentId = workspaceMeta.activeEnvironmentId;
|
2016-11-16 17:18:39 +00:00
|
|
|
|
const activeEnvironment = entities.environments[activeEnvironmentId];
|
|
|
|
|
|
2017-03-23 22:10:42 +00:00
|
|
|
|
// OAuth2Token stuff
|
|
|
|
|
const oAuth2Token = selectActiveOAuth2Token(state, props);
|
|
|
|
|
|
2016-11-16 17:18:39 +00:00
|
|
|
|
// Find other meta things
|
2016-12-01 01:50:59 +00:00
|
|
|
|
const loadStartTime = loadingRequestIds[activeRequest ? activeRequest._id : 'n/a'] || -1;
|
2016-11-25 23:09:17 +00:00
|
|
|
|
const sidebarChildren = selectSidebarChildren(state, props);
|
2016-11-26 07:33:55 +00:00
|
|
|
|
const workspaceChildren = selectWorkspaceRequestsAndRequestGroups(state, props);
|
2017-07-27 22:59:07 +00:00
|
|
|
|
const unseenWorkspaces = selectUnseenWorkspaces(state, props);
|
2016-11-16 17:18:39 +00:00
|
|
|
|
|
|
|
|
|
return Object.assign({}, state, {
|
2017-03-03 20:09:08 +00:00
|
|
|
|
settings,
|
|
|
|
|
workspaces,
|
2017-07-27 22:59:07 +00:00
|
|
|
|
unseenWorkspaces,
|
2017-03-03 20:09:08 +00:00
|
|
|
|
requestGroups,
|
|
|
|
|
requests,
|
2017-03-23 22:10:42 +00:00
|
|
|
|
oAuth2Token,
|
2017-03-03 20:09:08 +00:00
|
|
|
|
isLoading,
|
|
|
|
|
loadStartTime,
|
|
|
|
|
activeWorkspace,
|
|
|
|
|
activeRequest,
|
2017-07-17 18:20:38 +00:00
|
|
|
|
activeRequestResponses,
|
|
|
|
|
activeResponse,
|
2017-03-03 20:09:08 +00:00
|
|
|
|
sidebarHidden,
|
|
|
|
|
sidebarFilter,
|
|
|
|
|
sidebarWidth,
|
|
|
|
|
paneWidth,
|
2017-03-28 22:45:23 +00:00
|
|
|
|
paneHeight,
|
2017-03-03 20:09:08 +00:00
|
|
|
|
responsePreviewMode,
|
|
|
|
|
responseFilter,
|
2017-06-08 02:42:16 +00:00
|
|
|
|
responseFilterHistory,
|
2017-03-03 20:09:08 +00:00
|
|
|
|
sidebarChildren,
|
|
|
|
|
environments,
|
|
|
|
|
activeEnvironment,
|
|
|
|
|
workspaceChildren
|
2017-03-03 21:10:35 +00:00
|
|
|
|
});
|
2016-11-16 17:18:39 +00:00
|
|
|
|
}
|
2016-07-06 20:18:26 +00:00
|
|
|
|
|
2016-11-16 17:18:39 +00:00
|
|
|
|
function mapDispatchToProps (dispatch) {
|
2016-12-01 01:50:59 +00:00
|
|
|
|
const global = bindActionCreators(globalActions, dispatch);
|
2016-04-26 07:29:24 +00:00
|
|
|
|
|
2016-11-16 17:18:39 +00:00
|
|
|
|
return {
|
2016-12-01 01:50:59 +00:00
|
|
|
|
handleStartLoading: global.loadRequestStart,
|
|
|
|
|
handleStopLoading: global.loadRequestStop,
|
|
|
|
|
|
|
|
|
|
handleSetActiveWorkspace: global.setActiveWorkspace,
|
|
|
|
|
handleImportFileToWorkspace: global.importFile,
|
2017-05-03 17:48:23 +00:00
|
|
|
|
handleImportUriToWorkspace: global.importUri,
|
|
|
|
|
handleCommand: global.newCommand,
|
2016-12-01 01:50:59 +00:00
|
|
|
|
handleExportFile: global.exportFile,
|
2017-06-13 20:45:15 +00:00
|
|
|
|
handleMoveDoc: _moveDoc
|
2016-11-16 17:18:39 +00:00
|
|
|
|
};
|
|
|
|
|
}
|
2016-03-22 03:22:45 +00:00
|
|
|
|
|
2017-06-13 20:45:15 +00:00
|
|
|
|
async function _moveDoc (docToMove, parentId, targetId, targetOffset) {
|
|
|
|
|
if (docToMove._id === targetId) {
|
|
|
|
|
// Nothing to do. We are in the same spot as we started
|
2016-11-16 17:18:39 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
2016-04-26 07:29:24 +00:00
|
|
|
|
|
2017-06-13 20:45:15 +00:00
|
|
|
|
function __updateDoc (doc, patch) {
|
|
|
|
|
models.getModel(docToMove.type).update(doc, patch);
|
2016-11-16 17:18:39 +00:00
|
|
|
|
}
|
2016-03-20 04:47:43 +00:00
|
|
|
|
|
2016-11-16 17:18:39 +00:00
|
|
|
|
if (targetId === null) {
|
|
|
|
|
// We are moving to an empty area. No sorting required
|
2017-06-13 20:45:15 +00:00
|
|
|
|
await __updateDoc(docToMove, {parentId});
|
2016-11-16 17:18:39 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// NOTE: using requestToTarget's parentId so we can switch parents!
|
2017-06-13 20:45:15 +00:00
|
|
|
|
let docs = [
|
|
|
|
|
...await models.request.findByParentId(parentId),
|
|
|
|
|
...await models.requestGroup.findByParentId(parentId)
|
|
|
|
|
].sort((a, b) => a.metaSortKey < b.metaSortKey ? -1 : 1);
|
2016-11-16 17:18:39 +00:00
|
|
|
|
|
2017-06-13 20:45:15 +00:00
|
|
|
|
// Find the index of doc B so we can re-order and save everything
|
|
|
|
|
for (let i = 0; i < docs.length; i++) {
|
|
|
|
|
const doc = docs[i];
|
2016-11-16 17:18:39 +00:00
|
|
|
|
|
2017-06-13 20:45:15 +00:00
|
|
|
|
if (doc._id === targetId) {
|
2016-11-16 17:18:39 +00:00
|
|
|
|
let before, after;
|
|
|
|
|
if (targetOffset < 0) {
|
|
|
|
|
// We're moving to below
|
2017-06-13 20:45:15 +00:00
|
|
|
|
before = docs[i];
|
|
|
|
|
after = docs[i + 1];
|
2016-11-16 17:18:39 +00:00
|
|
|
|
} else {
|
|
|
|
|
// We're moving to above
|
2017-06-13 20:45:15 +00:00
|
|
|
|
before = docs[i - 1];
|
|
|
|
|
after = docs[i];
|
2016-11-16 17:18:39 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-06-13 20:45:15 +00:00
|
|
|
|
const beforeKey = before ? before.metaSortKey : docs[0].metaSortKey - 100;
|
|
|
|
|
const afterKey = after ? after.metaSortKey : docs[docs.length - 1].metaSortKey + 100;
|
2016-11-16 17:18:39 +00:00
|
|
|
|
|
|
|
|
|
if (Math.abs(afterKey - beforeKey) < 0.000001) {
|
|
|
|
|
// If sort keys get too close together, we need to redistribute the list. This is
|
|
|
|
|
// not performant at all (need to update all siblings in DB), but it is extremely rare
|
|
|
|
|
// anyway
|
|
|
|
|
console.log(`-- Recreating Sort Keys ${beforeKey} ${afterKey} --`);
|
|
|
|
|
|
|
|
|
|
db.bufferChanges(300);
|
2017-06-13 20:45:15 +00:00
|
|
|
|
docs.map((r, i) => __updateDoc(r, {metaSortKey: i * 100, parentId}));
|
2016-11-16 17:18:39 +00:00
|
|
|
|
} else {
|
2017-03-03 20:09:08 +00:00
|
|
|
|
const metaSortKey = afterKey - ((afterKey - beforeKey) / 2);
|
2017-06-13 20:45:15 +00:00
|
|
|
|
__updateDoc(docToMove, {metaSortKey, parentId});
|
2016-11-16 17:18:39 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
break;
|
2016-04-16 23:24:57 +00:00
|
|
|
|
}
|
2016-03-20 04:47:43 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-03 01:44:07 +00:00
|
|
|
|
export default connect(mapStateToProps, mapDispatchToProps)(App);
|