insomnia/packages/insomnia-app/app/ui/containers/app.js

1179 lines
37 KiB
JavaScript
Raw Normal View History

2018-06-25 17:42:50 +00:00
import React, { PureComponent } from 'react';
2017-08-10 01:56:27 +00:00
import PropTypes from 'prop-types';
import autobind from 'autobind-decorator';
import fs from 'fs';
2018-06-25 17:42:50 +00:00
import { clipboard, ipcRenderer, remote } from 'electron';
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';
2018-06-25 17:42:50 +00:00
import { connect } from 'react-redux';
import { bindActionCreators } from 'redux';
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';
2018-10-17 16:42:33 +00:00
import SettingsModal, { TAB_INDEX_SHORTCUTS } from '../components/modals/settings-modal';
2018-06-25 17:42:50 +00:00
import {
COLLAPSE_SIDEBAR_REMS,
DEFAULT_PANE_HEIGHT,
DEFAULT_PANE_WIDTH,
DEFAULT_SIDEBAR_WIDTH,
MAX_PANE_HEIGHT,
MAX_PANE_WIDTH,
MAX_SIDEBAR_REMS,
MIN_PANE_HEIGHT,
MIN_PANE_WIDTH,
MIN_SIDEBAR_REMS,
PREVIEW_MODE_SOURCE
} from '../../common/constants';
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';
2018-06-25 17:42:50 +00:00
import {
selectActiveCookieJar,
selectActiveOAuth2Token,
selectActiveRequest,
selectActiveRequestMeta,
selectActiveRequestResponses,
selectActiveResponse,
selectActiveWorkspace,
selectActiveWorkspaceClientCertificates,
selectActiveWorkspaceMeta,
selectEntitiesLists,
selectSidebarChildren,
selectUnseenWorkspaces,
selectWorkspaceRequestsAndRequestGroups
} from '../redux/selectors';
import RequestCreateModal from '../components/modals/request-create-modal';
import GenerateCodeModal from '../components/modals/generate-code-modal';
import WorkspaceSettingsModal from '../components/modals/workspace-settings-modal';
import RequestSettingsModal from '../components/modals/request-settings-modal';
import RequestRenderErrorModal from '../components/modals/request-render-error-modal';
import * as network from '../../network/network';
2018-06-25 17:42:50 +00:00
import { debounce, getContentDispositionHeader } from '../../common/misc';
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';
2018-06-25 17:42:50 +00:00
import { getKeys } from '../../templating/utils';
import { showAlert, showModal, showPrompt } from '../components/modals/index';
import { exportHarRequest } from '../../common/har';
import * as hotkeys from '../../common/hotkeys';
2017-11-01 14:13:51 +00:00
import KeydownBinder from '../components/keydown-binder';
import ErrorBoundary from '../components/error-boundary';
import * as plugins from '../../plugins';
import * as templating from '../../templating/index';
import AskModal from '../components/modals/ask-modal';
2018-06-25 17:42:50 +00:00
import { updateMimeType } from '../../models/request';
import MoveRequestGroupModal from '../components/modals/move-request-group-modal';
import * as themes from '../../plugins/misc';
2016-03-20 04:47:43 +00:00
@autobind
class App extends PureComponent {
2018-06-25 17:42:50 +00:00
constructor(props) {
super(props);
this.state = {
showDragOverlay: false,
draggingSidebar: false,
draggingPaneHorizontal: false,
draggingPaneVertical: false,
sidebarWidth: props.sidebarWidth || DEFAULT_SIDEBAR_WIDTH,
paneWidth: props.paneWidth || DEFAULT_PANE_WIDTH,
paneHeight: props.paneHeight || DEFAULT_PANE_HEIGHT
};
2016-11-25 20:55:15 +00:00
this._isMigratingChildren = false;
this._getRenderContextPromiseCache = {};
2018-10-17 16:42:33 +00:00
this._savePaneWidth = debounce(paneWidth => this._updateActiveWorkspaceMeta({ paneWidth }));
this._savePaneHeight = debounce(paneHeight => this._updateActiveWorkspaceMeta({ paneHeight }));
2018-06-25 17:42:50 +00:00
this._saveSidebarWidth = debounce(sidebarWidth =>
this._updateActiveWorkspaceMeta({ sidebarWidth })
);
this._globalKeyMap = null;
}
2018-06-25 17:42:50 +00:00
_setGlobalKeyMap() {
this._globalKeyMap = [
2018-06-25 17:42:50 +00:00
[
hotkeys.SHOW_WORKSPACE_SETTINGS,
() => {
const { activeWorkspace } = this.props;
showModal(WorkspaceSettingsModal, activeWorkspace);
}
],
[
hotkeys.SHOW_REQUEST_SETTINGS,
() => {
if (this.props.activeRequest) {
showModal(RequestSettingsModal, {
request: this.props.activeRequest
});
}
}
],
[
hotkeys.SHOW_QUICK_SWITCHER,
() => {
showModal(RequestSwitcherModal);
}
2018-06-25 17:42:50 +00:00
],
[hotkeys.SEND_REQUEST, this._handleSendShortcut],
[hotkeys.SEND_REQUEST_F5, this._handleSendShortcut],
2018-06-25 17:42:50 +00:00
[
hotkeys.SHOW_ENVIRONMENTS,
() => {
const { activeWorkspace } = this.props;
showModal(WorkspaceEnvironmentsEditModal, activeWorkspace);
2017-11-10 17:03:36 +00:00
}
2018-06-25 17:42:50 +00:00
],
[
hotkeys.SHOW_COOKIES,
() => {
const { activeWorkspace } = this.props;
showModal(CookiesModal, activeWorkspace);
}
],
[
hotkeys.CREATE_REQUEST,
() => {
const { activeRequest, activeWorkspace } = this.props;
2018-10-17 16:42:33 +00:00
const parentId = activeRequest ? activeRequest.parentId : activeWorkspace._id;
2018-06-25 17:42:50 +00:00
this._requestCreate(parentId);
}
],
[
hotkeys.DELETE_REQUEST,
() => {
const { activeRequest } = this.props;
if (!activeRequest) {
return;
}
2017-11-10 17:03:36 +00:00
2018-06-25 17:42:50 +00:00
showModal(AskModal, {
title: 'Delete Request?',
message: `Really delete ${activeRequest.name}?`,
onDone: confirmed => {
if (!confirmed) {
return;
}
models.request.remove(activeRequest);
}
2018-06-25 17:42:50 +00:00
});
}
],
[
hotkeys.CREATE_FOLDER,
() => {
const { activeRequest, activeWorkspace } = this.props;
2018-10-17 16:42:33 +00:00
const parentId = activeRequest ? activeRequest.parentId : activeWorkspace._id;
2018-06-25 17:42:50 +00:00
this._requestGroupCreate(parentId);
}
],
[
hotkeys.GENERATE_CODE,
async () => {
showModal(GenerateCodeModal, this.props.activeRequest);
}
],
[
hotkeys.DUPLICATE_REQUEST,
async () => {
await this._requestDuplicate(this.props.activeRequest);
}
]
];
}
2018-06-25 17:42:50 +00:00
async _handleSendShortcut() {
const { activeRequest, activeEnvironment } = this.props;
await this._handleSendRequestWithEnvironment(
activeRequest ? activeRequest._id : 'n/a',
activeEnvironment ? activeEnvironment._id : 'n/a'
);
}
2018-06-25 17:42:50 +00:00
_setRequestPaneRef(n) {
this._requestPane = n;
}
2018-06-25 17:42:50 +00:00
_setResponsePaneRef(n) {
this._responsePane = n;
}
2016-11-25 20:55:15 +00:00
2018-06-25 17:42:50 +00:00
_setSidebarRef(n) {
this._sidebar = n;
}
2018-06-25 17:42:50 +00:00
_requestGroupCreate(parentId) {
showPrompt({
title: 'New Folder',
defaultValue: 'My Folder',
2016-11-23 20:44:46 +00:00
submitName: 'Create',
label: 'Name',
selectText: true,
2018-05-25 01:40:36 +00:00
onComplete: async name => {
2018-06-25 17:42:50 +00:00
const requestGroup = await models.requestGroup.create({
parentId,
name
});
await models.requestGroupMeta.create({
parentId: requestGroup._id,
collapsed: false
});
}
});
}
2018-06-25 17:42:50 +00:00
_requestCreate(parentId) {
showModal(RequestCreateModal, {
parentId,
onComplete: request => {
this._handleSetActiveRequest(request._id);
}
});
}
2018-06-25 17:42:50 +00:00
async _requestGroupDuplicate(requestGroup) {
models.requestGroup.duplicate(requestGroup);
}
2016-11-28 07:12:17 +00:00
2018-06-25 17:42:50 +00:00
async _requestGroupMove(requestGroup) {
showModal(MoveRequestGroupModal, { requestGroup });
}
2018-06-25 17:42:50 +00:00
async _requestDuplicate(request) {
if (!request) {
return;
}
const newRequest = await models.request.duplicate(request);
await this._handleSetActiveRequest(newRequest._id);
}
2017-02-13 08:12:02 +00:00
2018-06-25 17:42:50 +00:00
async _workspaceDuplicate(callback) {
2017-06-07 00:07:09 +00:00
const workspace = this.props.activeWorkspace;
showPrompt({
title: 'Duplicate Workspace',
2017-06-07 00:07:09 +00:00
defaultValue: `${workspace.name} (Copy)`,
submitName: 'Duplicate',
selectText: true,
onComplete: async name => {
2018-06-25 17:42:50 +00:00
const newWorkspace = await db.duplicate(workspace, { name });
2017-06-07 00:07:09 +00:00
await this.props.handleSetActiveWorkspace(newWorkspace._id);
callback();
}
});
}
2018-06-25 17:42:50 +00:00
async _fetchRenderContext() {
const { activeEnvironment, activeRequest, activeWorkspace } = this.props;
const environmentId = activeEnvironment ? activeEnvironment._id : null;
2018-06-25 20:10:12 +00:00
const ancestors = await db.withAncestors(activeRequest || activeWorkspace, [
models.request.type,
models.requestGroup.type,
models.workspace.type
]);
return render.getRenderContext(activeRequest, environmentId, ancestors);
}
2018-06-25 17:42:50 +00:00
async _handleGetRenderContext() {
const context = await this._fetchRenderContext();
const keys = getKeys(context);
2018-06-25 17:42:50 +00:00
return { context, keys };
}
/**
* Heavily optimized render function
*
* @param text - template to render
* @param contextCacheKey - if rendering multiple times in parallel, set this
* @returns {Promise}
* @private
*/
2018-06-25 17:42:50 +00:00
async _handleRenderText(text, contextCacheKey = null) {
2018-10-17 16:42:33 +00:00
if (!contextCacheKey || !this._getRenderContextPromiseCache[contextCacheKey]) {
const context = this._fetchRenderContext();
// NOTE: We're caching promises here to avoid race conditions
this._getRenderContextPromiseCache[contextCacheKey] = context;
}
// Set timeout to delete the key eventually
2018-10-17 16:42:33 +00:00
setTimeout(() => delete this._getRenderContextPromiseCache[contextCacheKey], 5000);
const context = await this._getRenderContextPromiseCache[contextCacheKey];
return render.render(text, context);
}
2018-06-25 17:42:50 +00:00
_handleGenerateCodeForActiveRequest() {
this._handleGenerateCode(this.props.activeRequest);
}
2018-06-25 17:42:50 +00:00
_handleGenerateCode(request) {
showModal(GenerateCodeModal, request);
}
2016-11-28 07:12:17 +00:00
2018-06-25 17:42:50 +00:00
async _handleCopyAsCurl(request) {
const { activeEnvironment } = this.props;
const environmentId = activeEnvironment ? activeEnvironment._id : 'n/a';
const har = await exportHarRequest(request._id, environmentId);
const snippet = new HTTPSnippet(har);
const cmd = snippet.convert('shell', 'curl');
clipboard.writeText(cmd);
}
2018-06-25 17:42:50 +00:00
async _updateRequestGroupMetaByParentId(requestGroupId, patch) {
2018-10-17 16:42:33 +00:00
const requestGroupMeta = await models.requestGroupMeta.getByParentId(requestGroupId);
if (requestGroupMeta) {
await models.requestGroupMeta.update(requestGroupMeta, patch);
} else {
2018-06-25 17:42:50 +00:00
const newPatch = Object.assign({ parentId: requestGroupId }, patch);
await models.requestGroupMeta.create(newPatch);
}
}
2016-11-25 23:09:17 +00:00
2018-06-25 17:42:50 +00:00
async _updateActiveWorkspaceMeta(patch) {
const workspaceId = this.props.activeWorkspace._id;
2018-10-17 16:42:33 +00:00
const workspaceMeta = await models.workspaceMeta.getOrCreateByParentId(workspaceId);
2017-07-27 22:59:07 +00:00
if (workspaceMeta) {
return models.workspaceMeta.update(workspaceMeta, patch);
} else {
2018-06-25 17:42:50 +00:00
const newPatch = Object.assign({ parentId: workspaceId }, patch);
return models.workspaceMeta.create(newPatch);
}
}
2018-06-25 17:42:50 +00:00
async _updateRequestMetaByParentId(requestId, patch) {
const requestMeta = await models.requestMeta.getByParentId(requestId);
if (requestMeta) {
return models.requestMeta.update(requestMeta, patch);
} else {
2018-06-25 17:42:50 +00:00
const newPatch = Object.assign({ parentId: requestId }, patch);
return models.requestMeta.create(newPatch);
}
}
2018-06-25 17:42:50 +00:00
_handleSetPaneWidth(paneWidth) {
this.setState({ paneWidth });
this._savePaneWidth(paneWidth);
}
2018-06-25 17:42:50 +00:00
_handleSetPaneHeight(paneHeight) {
this.setState({ paneHeight });
this._savePaneHeight(paneHeight);
}
2018-06-25 17:42:50 +00:00
async _handleSetActiveRequest(activeRequestId) {
await this._updateActiveWorkspaceMeta({ activeRequestId });
}
2018-06-25 17:42:50 +00:00
async _handleSetActiveEnvironment(activeEnvironmentId) {
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);
}
2018-06-25 17:42:50 +00:00
_handleSetSidebarWidth(sidebarWidth) {
this.setState({ sidebarWidth });
this._saveSidebarWidth(sidebarWidth);
}
2018-06-25 17:42:50 +00:00
async _handleSetSidebarHidden(sidebarHidden) {
await this._updateActiveWorkspaceMeta({ sidebarHidden });
}
2018-06-25 17:42:50 +00:00
async _handleSetSidebarFilter(sidebarFilter) {
await this._updateActiveWorkspaceMeta({ sidebarFilter });
}
2018-06-25 17:42:50 +00:00
_handleSetRequestGroupCollapsed(requestGroupId, collapsed) {
this._updateRequestGroupMetaByParentId(requestGroupId, { collapsed });
}
2018-06-25 17:42:50 +00:00
_handleSetResponsePreviewMode(requestId, previewMode) {
this._updateRequestMetaByParentId(requestId, { previewMode });
}
2018-06-25 17:42:50 +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;
}
responseFilterHistory.unshift(responseFilter);
2018-06-25 17:42:50 +00:00
await this._updateRequestMetaByParentId(requestId, {
responseFilterHistory
});
}, 2000);
}
2018-06-25 17:42:50 +00:00
async _handleUpdateRequestMimeType(mimeType) {
if (!this.props.activeRequest) {
console.warn('Tried to update request mime-type when no active request');
return null;
}
const requestMeta = await models.requestMeta.getOrCreateByParentId(
this.props.activeRequest._id
);
const savedBody = requestMeta.savedRequestBody;
2018-06-25 17:42:50 +00:00
const saveValue =
typeof mimeType !== 'string' // Switched to No body
? this.props.activeRequest.body
: {}; // Clear saved value in requestMeta
2018-06-25 17:42:50 +00:00
await models.requestMeta.update(requestMeta, {
savedRequestBody: saveValue
});
2018-10-17 16:42:33 +00:00
const newRequest = await updateMimeType(this.props.activeRequest, mimeType, false, savedBody);
// Force it to update, because other editor components (header editor)
// needs to change. Need to wait a delay so the next render can finish
setTimeout(this._wrapper._forceRequestPaneRefresh, 300);
return newRequest;
}
2018-10-17 16:42:33 +00:00
async _handleSendAndDownloadRequestWithEnvironment(requestId, environmentId, dir) {
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) {
this._sendRequestTrackingKey = key;
}
// Start loading
this.props.handleStartLoading(requestId);
try {
const responsePatch = await network.send(requestId, environmentId);
const headers = responsePatch.headers || [];
const header = getContentDispositionHeader(headers);
const nameFromHeader = header ? header.value : null;
if (!responsePatch.bodyPath) {
return;
}
if (responsePatch.statusCode >= 200 && responsePatch.statusCode < 300) {
2018-10-17 16:42:33 +00:00
const extension = mime.extension(responsePatch.contentType) || 'unknown';
2018-06-25 17:42:50 +00:00
const name =
2018-10-17 16:42:33 +00:00
nameFromHeader || `${request.name.replace(/\s/g, '-').toLowerCase()}.${extension}`;
const filename = path.join(dir, name);
const to = fs.createWriteStream(filename);
const readStream = models.response.getBodyStream(responsePatch);
if (!readStream) {
return;
}
readStream.pipe(to);
readStream.on('end', async () => {
responsePatch.error = `Saved to ${filename}`;
await models.response.create(responsePatch);
});
readStream.on('error', async err => {
2018-10-17 16:42:33 +00:00
console.warn('Failed to download request after sending', responsePatch.bodyPath, err);
await models.response.create(responsePatch);
});
}
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>
)
});
}
// Unset active response because we just made a new one
2018-06-25 17:42:50 +00:00
await this._updateRequestMetaByParentId(requestId, {
activeResponseId: null
});
// Stop loading
this.props.handleStopLoading(requestId);
}
2018-06-25 17:42:50 +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-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) {
this._sendRequestTrackingKey = key;
}
this.props.handleStartLoading(requestId);
try {
const responsePatch = await network.send(requestId, environmentId);
await models.response.create(responsePatch);
} catch (err) {
if (err.type === 'render') {
2018-06-25 17:42:50 +00:00
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>
)
});
}
}
// Unset active response because we just made a new one
2018-06-25 17:42:50 +00:00
await this._updateRequestMetaByParentId(requestId, {
activeResponseId: null
});
// Stop loading
this.props.handleStopLoading(requestId);
}
2018-06-25 17:42:50 +00:00
async _handleSetActiveResponse(requestId, activeResponse = null) {
2017-07-17 18:20:38 +00:00
const activeResponseId = activeResponse ? activeResponse._id : null;
2018-06-25 17:42:50 +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
}
}
2018-06-25 17:42:50 +00:00
_requestCreateForWorkspace() {
this._requestCreate(this.props.activeWorkspace._id);
}
2016-11-25 23:09:17 +00:00
2018-06-25 17:42:50 +00:00
_startDragSidebar() {
this.setState({ draggingSidebar: true });
}
2016-06-19 00:08:14 +00:00
2018-06-25 17:42:50 +00:00
_resetDragSidebar() {
// TODO: Remove setTimeout need be not triggering drag on double click
setTimeout(() => this._handleSetSidebarWidth(DEFAULT_SIDEBAR_WIDTH), 50);
}
2016-06-19 00:08:14 +00:00
2018-06-25 17:42:50 +00:00
_startDragPaneHorizontal() {
this.setState({ draggingPaneHorizontal: true });
}
2016-06-19 00:08:14 +00:00
2018-06-25 17:42:50 +00:00
_startDragPaneVertical() {
this.setState({ draggingPaneVertical: true });
}
2018-06-25 17:42:50 +00:00
_resetDragPaneHorizontal() {
// TODO: Remove setTimeout need be not triggering drag on double click
setTimeout(() => this._handleSetPaneWidth(DEFAULT_PANE_WIDTH), 50);
}
2018-06-25 17:42:50 +00:00
_resetDragPaneVertical() {
// TODO: Remove setTimeout need be not triggering drag on double click
setTimeout(() => this._handleSetPaneHeight(DEFAULT_PANE_HEIGHT), 50);
}
2018-06-25 17:42:50 +00:00
_handleMouseMove(e) {
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 /* % */) {
2018-06-25 17:42:50 +00:00
this.setState({ showDragOverlay: true });
}
const requestPane = ReactDOM.findDOMNode(this._requestPane);
const responsePane = ReactDOM.findDOMNode(this._responsePane);
const requestPaneWidth = requestPane.offsetWidth;
const responsePaneWidth = responsePane.offsetWidth;
const pixelOffset = e.clientX - requestPane.offsetLeft;
let paneWidth = pixelOffset / (requestPaneWidth + responsePaneWidth);
paneWidth = Math.min(Math.max(paneWidth, MIN_PANE_WIDTH), MAX_PANE_WIDTH);
this._handleSetPaneWidth(paneWidth);
} 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 /* % */) {
2018-06-25 17:42:50 +00:00
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);
2018-10-17 16:42:33 +00:00
paneHeight = Math.min(Math.max(paneHeight, MIN_PANE_HEIGHT), MAX_PANE_HEIGHT);
this._handleSetPaneHeight(paneHeight);
} else if (this.state.draggingSidebar) {
// 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 */) {
2018-06-25 17:42:50 +00:00
this.setState({ showDragOverlay: true });
}
const currentPixelWidth = ReactDOM.findDOMNode(this._sidebar).offsetWidth;
const ratio = e.clientX / currentPixelWidth;
const width = this.state.sidebarWidth * ratio;
let sidebarWidth = Math.min(width, MAX_SIDEBAR_REMS);
if (sidebarWidth < COLLAPSE_SIDEBAR_REMS) {
sidebarWidth = MIN_SIDEBAR_REMS;
}
this._handleSetSidebarWidth(sidebarWidth);
}
}
2018-06-25 17:42:50 +00:00
_handleMouseUp() {
if (this.state.draggingSidebar) {
2018-06-25 17:42:50 +00:00
this.setState({ draggingSidebar: false, showDragOverlay: false });
}
2016-03-22 05:01:58 +00:00
if (this.state.draggingPaneHorizontal) {
2018-06-25 17:42:50 +00:00
this.setState({ draggingPaneHorizontal: false, showDragOverlay: false });
}
if (this.state.draggingPaneVertical) {
2018-06-25 17:42:50 +00:00
this.setState({ draggingPaneVertical: false, showDragOverlay: false });
2016-06-19 07:21:43 +00:00
}
}
2018-06-25 17:42:50 +00:00
_handleKeyDown(e) {
for (const [definition, callback] of this._globalKeyMap) {
hotkeys.executeHotKey(e, definition, callback);
}
}
2018-06-25 17:42:50 +00:00
_handleToggleMenuBar(hide) {
Merge develop for 5.1.0 (#246) * Add Slack badge to README * Add Twitter badge * Appveyor badge * Fix badges again on README * Fix Twitter badge link * Simplify README.md * Migrate Travis secure items to Travis project settings (#198) * Remove docker linux build (using Travis now) (#199) * Fix travis build * Update Issue and PR templates (#200) * Add template for future pull requests * Format issue template like pull request template * Will not clear selected file if dialog is dismissed (#202) * #183, Body type "Text -> Other" reverts to previous body type (#193) * ISSUE#183 * Adding condition to check mime-type to other * Removing older changes for fixing issue. * Save full response to a file (#207) * Save full response to a file * Add a new button on the response preview pane * Save full response to file when button clicked * Update after PR comments * It's a Response, not a Request * Remove file extension requirement * Implement lazy tag rendering and some fixes (#211) * expanding to 3 decimals (#213) * Update PULL_REQUEST_TEMPLATE.md (#214) * Show build info in console (#216) * Add waiting message in dev mode while webpack compile happens * Switch license from GPL to AGPL (#221) * Default remote URLs to production * Don't use Curl's cookie handling (#220) * Some improvements to the response tag * Add tests for XPath response queries * Refactor conditional element syntax * Add option to toggle Menu Bar showing for Windows/Linux (#225) * Add option to toggle MenuBar showing on Windows/Linux * Extract Toggling Menu Bar functionality to App Container. Default show Menu Bar. Remove tip from Response Pane. * Finalize {% response ... %} Tag (#224) * Some improvements to the response tag * Add tests for XPath response queries * Refactor conditional element syntax * Update nunjucks-tags.js * Better Nunjucks Tag Editor (#234) * Helper to tokenize Nunjucks tag * More granular types * Add tag definitions * Improve editor to be more WYSIWYG * Fixed tests * Added raw response tag * A few improvements to tag editor enum * fix NTLM typo (#244) * Tweaks and fixes for next release (#245)
2017-05-24 16:25:22 +00:00
for (const win of remote.BrowserWindow.getAllWindows()) {
2017-05-24 16:46:18 +00:00
if (win.isMenuBarAutoHide() !== hide) {
Merge develop for 5.1.0 (#246) * Add Slack badge to README * Add Twitter badge * Appveyor badge * Fix badges again on README * Fix Twitter badge link * Simplify README.md * Migrate Travis secure items to Travis project settings (#198) * Remove docker linux build (using Travis now) (#199) * Fix travis build * Update Issue and PR templates (#200) * Add template for future pull requests * Format issue template like pull request template * Will not clear selected file if dialog is dismissed (#202) * #183, Body type "Text -> Other" reverts to previous body type (#193) * ISSUE#183 * Adding condition to check mime-type to other * Removing older changes for fixing issue. * Save full response to a file (#207) * Save full response to a file * Add a new button on the response preview pane * Save full response to file when button clicked * Update after PR comments * It's a Response, not a Request * Remove file extension requirement * Implement lazy tag rendering and some fixes (#211) * expanding to 3 decimals (#213) * Update PULL_REQUEST_TEMPLATE.md (#214) * Show build info in console (#216) * Add waiting message in dev mode while webpack compile happens * Switch license from GPL to AGPL (#221) * Default remote URLs to production * Don't use Curl's cookie handling (#220) * Some improvements to the response tag * Add tests for XPath response queries * Refactor conditional element syntax * Add option to toggle Menu Bar showing for Windows/Linux (#225) * Add option to toggle MenuBar showing on Windows/Linux * Extract Toggling Menu Bar functionality to App Container. Default show Menu Bar. Remove tip from Response Pane. * Finalize {% response ... %} Tag (#224) * Some improvements to the response tag * Add tests for XPath response queries * Refactor conditional element syntax * Update nunjucks-tags.js * Better Nunjucks Tag Editor (#234) * Helper to tokenize Nunjucks tag * More granular types * Add tag definitions * Improve editor to be more WYSIWYG * Fixed tests * Added raw response tag * A few improvements to tag editor enum * fix NTLM typo (#244) * Tweaks and fixes for next release (#245)
2017-05-24 16:25:22 +00:00
win.setAutoHideMenuBar(hide);
win.setMenuBarVisibility(!hide);
}
}
}
2018-06-25 17:42:50 +00:00
async _handleToggleSidebar() {
const sidebarHidden = !this.props.sidebarHidden;
await this._handleSetSidebarHidden(sidebarHidden);
}
2018-06-25 17:42:50 +00:00
_setWrapperRef(n) {
this._wrapper = n;
}
2016-11-09 04:16:38 +00:00
/**
* Update document.title to be "Workspace (Environment) Request"
* @private
*/
2018-06-25 17:42:50 +00:00
_updateDocumentTitle() {
const { activeWorkspace, activeEnvironment, activeRequest } = this.props;
let title = activeWorkspace.name;
if (activeEnvironment) {
title += ` (${activeEnvironment.name})`;
}
if (activeRequest) {
title += ` ${activeRequest.name}`;
}
document.title = title;
}
2018-06-25 17:42:50 +00:00
componentDidUpdate() {
this._updateDocumentTitle();
}
2018-06-25 17:42:50 +00:00
async componentDidMount() {
// Bind mouse and key handlers
document.addEventListener('mouseup', this._handleMouseUp);
document.addEventListener('mousemove', this._handleMouseMove);
this._setGlobalKeyMap();
// Update title
this._updateDocumentTitle();
2017-02-13 08:12:02 +00:00
db.onChange(async changes => {
let needsRefresh = false;
for (const change of changes) {
const [
_, // eslint-disable-line no-unused-vars
doc,
fromSync
] = change;
2018-06-25 17:42:50 +00:00
const { activeRequest } = this.props;
// No active request, so we don't need to force refresh anything
if (!activeRequest) {
return;
}
// 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)
if (doc.type === models.environment.type) {
2017-01-11 21:59:35 +00:00
console.log('[App] Forcing update from environment change', change);
needsRefresh = true;
}
// Force refresh if sync changes the active request
if (fromSync && doc._id === activeRequest._id) {
needsRefresh = true;
2017-01-11 21:59:35 +00:00
console.log('[App] Forcing update from request change', change);
}
}
if (needsRefresh) {
this._wrapper._forceRequestPaneRefresh();
}
});
2016-08-22 20:05:42 +00:00
ipcRenderer.on('toggle-preferences', () => {
showModal(SettingsModal);
});
ipcRenderer.on('reload-plugins', async () => {
2018-07-18 00:48:10 +00:00
const { settings } = this.props;
await plugins.getPlugins(true);
templating.reload();
2018-07-18 00:48:10 +00:00
themes.setTheme(settings.theme);
console.log('[plugins] reloaded');
});
ipcRenderer.on('toggle-preferences-shortcuts', () => {
showModal(SettingsModal, TAB_INDEX_SHORTCUTS);
});
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);
});
2017-11-01 14:13:51 +00:00
// NOTE: This is required for "drop" event to trigger.
2018-06-25 17:42:50 +00:00
document.addEventListener(
'dragover',
e => {
e.preventDefault();
},
false
);
2017-11-01 14:13:51 +00:00
2018-06-25 17:42:50 +00:00
document.addEventListener(
'drop',
async e => {
e.preventDefault();
const { activeWorkspace, handleImportUriToWorkspace } = this.props;
if (!activeWorkspace) {
return;
}
if (e.dataTransfer.files.length === 0) {
console.log('[drag] Ignored drop event because no files present');
return;
}
2017-11-01 14:13:51 +00:00
2018-06-25 17:42:50 +00:00
const file = e.dataTransfer.files[0];
const { path } = file;
const uri = `file://${path}`;
2017-11-01 14:13:51 +00:00
2018-06-25 17:42:50 +00:00
await showAlert({
title: 'Confirm Data Import',
message: (
<span>
Import <code>{path}</code>?
</span>
),
addCancel: true
});
2017-11-01 14:13:51 +00:00
2018-06-25 17:42:50 +00:00
handleImportUriToWorkspace(activeWorkspace._id, uri);
},
false
);
2017-11-01 14:13:51 +00:00
2016-11-25 20:55:15 +00:00
ipcRenderer.on('toggle-sidebar', this._handleToggleSidebar);
Merge develop for 5.1.0 (#246) * Add Slack badge to README * Add Twitter badge * Appveyor badge * Fix badges again on README * Fix Twitter badge link * Simplify README.md * Migrate Travis secure items to Travis project settings (#198) * Remove docker linux build (using Travis now) (#199) * Fix travis build * Update Issue and PR templates (#200) * Add template for future pull requests * Format issue template like pull request template * Will not clear selected file if dialog is dismissed (#202) * #183, Body type "Text -> Other" reverts to previous body type (#193) * ISSUE#183 * Adding condition to check mime-type to other * Removing older changes for fixing issue. * Save full response to a file (#207) * Save full response to a file * Add a new button on the response preview pane * Save full response to file when button clicked * Update after PR comments * It's a Response, not a Request * Remove file extension requirement * Implement lazy tag rendering and some fixes (#211) * expanding to 3 decimals (#213) * Update PULL_REQUEST_TEMPLATE.md (#214) * Show build info in console (#216) * Add waiting message in dev mode while webpack compile happens * Switch license from GPL to AGPL (#221) * Default remote URLs to production * Don't use Curl's cookie handling (#220) * Some improvements to the response tag * Add tests for XPath response queries * Refactor conditional element syntax * Add option to toggle Menu Bar showing for Windows/Linux (#225) * Add option to toggle MenuBar showing on Windows/Linux * Extract Toggling Menu Bar functionality to App Container. Default show Menu Bar. Remove tip from Response Pane. * Finalize {% response ... %} Tag (#224) * Some improvements to the response tag * Add tests for XPath response queries * Refactor conditional element syntax * Update nunjucks-tags.js * Better Nunjucks Tag Editor (#234) * Helper to tokenize Nunjucks tag * More granular types * Add tag definitions * Improve editor to be more WYSIWYG * Fixed tests * Added raw response tag * A few improvements to tag editor enum * fix NTLM typo (#244) * Tweaks and fixes for next release (#245)
2017-05-24 16:25:22 +00:00
// handle this
this._handleToggleMenuBar(this.props.settings.autoHideMenuBar);
// Give it a bit before letting the backend know it's ready
setTimeout(() => ipcRenderer.send('window-ready'), 500);
}
2018-06-25 17:42:50 +00:00
componentWillUnmount() {
// Remove mouse and key handlers
document.removeEventListener('mouseup', this._handleMouseUp);
document.removeEventListener('mousemove', this._handleMouseMove);
}
2018-06-25 17:42:50 +00:00
async _ensureWorkspaceChildren(props) {
const { activeWorkspace, activeCookieJar, environments } = props;
2018-10-17 16:42:33 +00:00
const baseEnvironments = environments.filter(e => e.parentId === activeWorkspace._id);
// Nothing to do
if (baseEnvironments.length && activeCookieJar) {
return;
}
// We already started migrating. Let it finish.
if (this._isMigratingChildren) {
return;
}
// Prevent rendering of everything
this._isMigratingChildren = true;
await db.bufferChanges();
if (baseEnvironments.length === 0) {
2018-06-25 17:42:50 +00:00
await models.environment.create({ parentId: activeWorkspace._id });
2018-10-17 16:42:33 +00:00
console.log(`[app] Created missing base environment for ${activeWorkspace.name}`);
}
if (!activeCookieJar) {
2018-06-25 17:42:50 +00:00
await models.cookieJar.create({
parentId: this.props.activeWorkspace._id
});
2018-10-17 16:42:33 +00:00
console.log(`[app] Created missing cookie jar for ${activeWorkspace.name}`);
}
await db.flushChanges();
// Flush "transaction"
this._isMigratingChildren = false;
}
2018-06-25 17:42:50 +00:00
componentWillReceiveProps(nextProps) {
this._ensureWorkspaceChildren(nextProps);
}
2018-06-25 17:42:50 +00:00
componentWillMount() {
this._ensureWorkspaceChildren(this.props);
}
2018-06-25 17:42:50 +00:00
render() {
if (this._isMigratingChildren) {
console.log('[app] Waiting for migration to complete');
return null;
}
return (
2018-06-25 17:42:50 +00:00
<KeydownBinder
onKeydown={this._handleKeyDown}
2018-10-17 16:42:33 +00:00
key={this.props.activeWorkspace ? this.props.activeWorkspace._id : 'n/a'}>
<div className="app">
<ErrorBoundary showAlert>
<Wrapper
{...this.props}
ref={this._setWrapperRef}
paneWidth={this.state.paneWidth}
paneHeight={this.state.paneHeight}
sidebarWidth={this.state.sidebarWidth}
handleCreateRequestForWorkspace={this._requestCreateForWorkspace}
2018-10-17 16:42:33 +00:00
handleSetRequestGroupCollapsed={this._handleSetRequestGroupCollapsed}
handleActivateRequest={this._handleSetActiveRequest}
handleSetRequestPaneRef={this._setRequestPaneRef}
handleSetResponsePaneRef={this._setResponsePaneRef}
handleSetSidebarRef={this._setSidebarRef}
handleStartDragSidebar={this._startDragSidebar}
handleResetDragSidebar={this._resetDragSidebar}
handleStartDragPaneHorizontal={this._startDragPaneHorizontal}
handleStartDragPaneVertical={this._startDragPaneVertical}
handleResetDragPaneHorizontal={this._resetDragPaneHorizontal}
handleResetDragPaneVertical={this._resetDragPaneVertical}
handleCreateRequest={this._requestCreate}
handleRender={this._handleRenderText}
handleGetRenderContext={this._handleGetRenderContext}
handleDuplicateRequest={this._requestDuplicate}
handleDuplicateRequestGroup={this._requestGroupDuplicate}
handleMoveRequestGroup={this._requestGroupMove}
handleDuplicateWorkspace={this._workspaceDuplicate}
handleCreateRequestGroup={this._requestGroupCreate}
handleGenerateCode={this._handleGenerateCode}
2018-10-17 16:42:33 +00:00
handleGenerateCodeForActiveRequest={this._handleGenerateCodeForActiveRequest}
handleCopyAsCurl={this._handleCopyAsCurl}
handleSetResponsePreviewMode={this._handleSetResponsePreviewMode}
handleSetResponseFilter={this._handleSetResponseFilter}
2018-10-17 16:42:33 +00:00
handleSendRequestWithEnvironment={this._handleSendRequestWithEnvironment}
2018-06-25 17:42:50 +00:00
handleSendAndDownloadRequestWithEnvironment={
this._handleSendAndDownloadRequestWithEnvironment
}
handleSetActiveResponse={this._handleSetActiveResponse}
handleSetActiveRequest={this._handleSetActiveRequest}
handleSetActiveEnvironment={this._handleSetActiveEnvironment}
handleSetSidebarFilter={this._handleSetSidebarFilter}
handleToggleMenuBar={this._handleToggleMenuBar}
handleUpdateRequestMimeType={this._handleUpdateRequestMimeType}
/>
</ErrorBoundary>
<ErrorBoundary showAlert>
2018-06-25 17:42:50 +00:00
<Toast />
</ErrorBoundary>
{/* Block all mouse activity by showing an overlay while dragging */}
2018-10-17 16:42:33 +00:00
{this.state.showDragOverlay ? <div className="blocker-overlay" /> : null}
</div>
</KeydownBinder>
);
}
}
App.propTypes = {
// Required
sidebarWidth: PropTypes.number.isRequired,
paneWidth: PropTypes.number.isRequired,
paneHeight: PropTypes.number.isRequired,
handleCommand: PropTypes.func.isRequired,
Merge develop for 5.1.0 (#246) * Add Slack badge to README * Add Twitter badge * Appveyor badge * Fix badges again on README * Fix Twitter badge link * Simplify README.md * Migrate Travis secure items to Travis project settings (#198) * Remove docker linux build (using Travis now) (#199) * Fix travis build * Update Issue and PR templates (#200) * Add template for future pull requests * Format issue template like pull request template * Will not clear selected file if dialog is dismissed (#202) * #183, Body type "Text -> Other" reverts to previous body type (#193) * ISSUE#183 * Adding condition to check mime-type to other * Removing older changes for fixing issue. * Save full response to a file (#207) * Save full response to a file * Add a new button on the response preview pane * Save full response to file when button clicked * Update after PR comments * It's a Response, not a Request * Remove file extension requirement * Implement lazy tag rendering and some fixes (#211) * expanding to 3 decimals (#213) * Update PULL_REQUEST_TEMPLATE.md (#214) * Show build info in console (#216) * Add waiting message in dev mode while webpack compile happens * Switch license from GPL to AGPL (#221) * Default remote URLs to production * Don't use Curl's cookie handling (#220) * Some improvements to the response tag * Add tests for XPath response queries * Refactor conditional element syntax * Add option to toggle Menu Bar showing for Windows/Linux (#225) * Add option to toggle MenuBar showing on Windows/Linux * Extract Toggling Menu Bar functionality to App Container. Default show Menu Bar. Remove tip from Response Pane. * Finalize {% response ... %} Tag (#224) * Some improvements to the response tag * Add tests for XPath response queries * Refactor conditional element syntax * Update nunjucks-tags.js * Better Nunjucks Tag Editor (#234) * Helper to tokenize Nunjucks tag * More granular types * Add tag definitions * Improve editor to be more WYSIWYG * Fixed tests * Added raw response tag * A few improvements to tag editor enum * fix NTLM typo (#244) * Tweaks and fixes for next release (#245)
2017-05-24 16:25:22 +00:00
settings: PropTypes.object.isRequired,
activeWorkspace: PropTypes.shape({
_id: PropTypes.string.isRequired
}).isRequired,
2017-06-07 00:07:09 +00:00
handleSetActiveWorkspace: PropTypes.func.isRequired,
// Optional
activeRequest: PropTypes.object,
activeEnvironment: PropTypes.shape({
_id: PropTypes.string.isRequired
})
};
2018-06-25 17:42:50 +00:00
function mapStateToProps(state, props) {
const { entities, global } = state;
2018-06-25 17:42:50 +00:00
const { isLoading, loadingRequestIds } = global;
// Entities
2016-11-25 23:09:17 +00:00
const entitiesLists = selectEntitiesLists(state, props);
2018-06-25 17:42:50 +00:00
const { workspaces, environments, requests, requestGroups } = entitiesLists;
2016-11-25 23:09:17 +00:00
const settings = entitiesLists.settings[0];
// Workspace stuff
const workspaceMeta = selectActiveWorkspaceMeta(state, props) || {};
2016-11-25 23:09:17 +00:00
const activeWorkspace = selectActiveWorkspace(state, props);
2018-10-17 16:42:33 +00:00
const activeWorkspaceClientCertificates = selectActiveWorkspaceClientCertificates(state, props);
const sidebarHidden = workspaceMeta.sidebarHidden || false;
const sidebarFilter = workspaceMeta.sidebarFilter || '';
const sidebarWidth = workspaceMeta.sidebarWidth || DEFAULT_SIDEBAR_WIDTH;
const paneWidth = workspaceMeta.paneWidth || DEFAULT_PANE_WIDTH;
const paneHeight = workspaceMeta.paneHeight || DEFAULT_PANE_HEIGHT;
// Request stuff
const requestMeta = selectActiveRequestMeta(state, props) || {};
const activeRequest = selectActiveRequest(state, props);
const responsePreviewMode = requestMeta.previewMode || PREVIEW_MODE_SOURCE;
const responseFilter = requestMeta.responseFilter || '';
const responseFilterHistory = requestMeta.responseFilterHistory || [];
2017-07-17 18:20:38 +00:00
2017-08-22 23:54:31 +00:00
// Cookie Jar
const activeCookieJar = selectActiveCookieJar(state, props);
2017-07-17 18:20:38 +00:00
// Response stuff
2018-10-17 16:42:33 +00:00
const activeRequestResponses = selectActiveRequestResponses(state, props) || [];
2017-07-17 18:20:38 +00:00
const activeResponse = selectActiveResponse(state, props) || null;
// Environment stuff
const activeEnvironmentId = workspaceMeta.activeEnvironmentId;
const activeEnvironment = entities.environments[activeEnvironmentId];
// OAuth2Token stuff
const oAuth2Token = selectActiveOAuth2Token(state, props);
// Find other meta things
2018-10-17 16:42:33 +00:00
const loadStartTime = loadingRequestIds[activeRequest ? activeRequest._id : 'n/a'] || -1;
2016-11-25 23:09:17 +00:00
const sidebarChildren = selectSidebarChildren(state, props);
2018-10-17 16:42:33 +00:00
const workspaceChildren = selectWorkspaceRequestsAndRequestGroups(state, props);
2017-07-27 22:59:07 +00:00
const unseenWorkspaces = selectUnseenWorkspaces(state, props);
return Object.assign({}, state, {
settings,
workspaces,
2017-07-27 22:59:07 +00:00
unseenWorkspaces,
requestGroups,
requests,
oAuth2Token,
isLoading,
loadStartTime,
activeWorkspace,
activeWorkspaceClientCertificates,
activeRequest,
2017-07-17 18:20:38 +00:00
activeRequestResponses,
activeResponse,
2017-08-22 22:30:57 +00:00
activeCookieJar,
sidebarHidden,
sidebarFilter,
sidebarWidth,
paneWidth,
paneHeight,
responsePreviewMode,
responseFilter,
responseFilterHistory,
sidebarChildren,
environments,
activeEnvironment,
workspaceChildren
});
}
2018-06-25 17:42:50 +00:00
function mapDispatchToProps(dispatch) {
const global = bindActionCreators(globalActions, dispatch);
return {
handleStartLoading: global.loadRequestStart,
handleStopLoading: global.loadRequestStop,
handleSetActiveWorkspace: global.setActiveWorkspace,
handleImportFileToWorkspace: global.importFile,
handleImportUriToWorkspace: global.importUri,
handleCommand: global.newCommand,
handleExportFile: global.exportFile,
2017-06-13 20:45:15 +00:00
handleMoveDoc: _moveDoc
};
}
2018-06-25 17:42:50 +00:00
async function _moveDoc(docToMove, parentId, targetId, targetOffset) {
// Nothing to do. We are in the same spot as we started
2017-06-13 20:45:15 +00:00
if (docToMove._id === targetId) {
return;
}
// Don't allow dragging things into itself or children. This will disconnect
// the node from the tree and cause the item to no longer show in the UI.
const descendents = await db.withDescendants(docToMove);
for (const doc of descendents) {
if (doc._id === parentId) {
return;
}
}
2018-06-25 17:42:50 +00:00
function __updateDoc(doc, patch) {
2017-06-13 20:45:15 +00:00
models.getModel(docToMove.type).update(doc, patch);
}
2016-03-20 04:47:43 +00:00
if (targetId === null) {
// We are moving to an empty area. No sorting required
2018-06-25 17:42:50 +00:00
await __updateDoc(docToMove, { parentId });
return;
}
// NOTE: using requestToTarget's parentId so we can switch parents!
2017-06-13 20:45:15 +00:00
let docs = [
2018-06-25 17:42:50 +00:00
...(await models.request.findByParentId(parentId)),
...(await models.requestGroup.findByParentId(parentId))
].sort((a, b) => (a.metaSortKey < b.metaSortKey ? -1 : 1));
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];
2017-06-13 20:45:15 +00:00
if (doc._id === targetId) {
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];
} else {
// We're moving to above
2017-06-13 20:45:15 +00:00
before = docs[i - 1];
after = docs[i];
}
2017-06-13 20:45:15 +00:00
const beforeKey = before ? before.metaSortKey : docs[0].metaSortKey - 100;
2018-10-17 16:42:33 +00:00
const afterKey = after ? after.metaSortKey : docs[docs.length - 1].metaSortKey + 100;
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
2017-11-22 00:07:28 +00:00
console.log(`[app] Recreating Sort Keys ${beforeKey} ${afterKey}`);
await db.bufferChanges(300);
2018-06-25 17:42:50 +00:00
docs.map((r, i) => __updateDoc(r, { metaSortKey: i * 100, parentId }));
} else {
2018-06-25 17:42:50 +00:00
const metaSortKey = afterKey - (afterKey - beforeKey) / 2;
__updateDoc(docToMove, { metaSortKey, parentId });
}
break;
2016-04-16 23:24:57 +00:00
}
2016-03-20 04:47:43 +00:00
}
}
2018-06-25 17:42:50 +00:00
export default connect(
mapStateToProps,
mapDispatchToProps
)(App);