2018-10-30 20:12:21 +03:00
|
|
|
/*
|
|
|
|
Copyright 2018 New Vector Ltd
|
2019-10-08 17:43:57 +03:00
|
|
|
Copyright 2019 The Matrix.org Foundation C.I.C.
|
2018-10-30 20:12:21 +03:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import React from 'react';
|
|
|
|
import ResizeHandle from '../views/elements/ResizeHandle';
|
|
|
|
import {Resizer, FixedDistributor} from '../../resizer';
|
|
|
|
|
|
|
|
export default class MainSplit extends React.Component {
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
this._setResizeContainerRef = this._setResizeContainerRef.bind(this);
|
2018-12-03 12:43:35 +03:00
|
|
|
this._onResized = this._onResized.bind(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
_onResized(size) {
|
|
|
|
window.localStorage.setItem("mx_rhs_size", size);
|
2019-03-27 18:38:17 +03:00
|
|
|
if (this.props.resizeNotifier) {
|
|
|
|
this.props.resizeNotifier.notifyRightHandleResized();
|
|
|
|
}
|
2018-10-30 20:12:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
_createResizer() {
|
|
|
|
const classNames = {
|
|
|
|
handle: "mx_ResizeHandle",
|
|
|
|
vertical: "mx_ResizeHandle_vertical",
|
2018-10-31 14:20:10 +03:00
|
|
|
reverse: "mx_ResizeHandle_reverse",
|
2018-10-30 20:12:21 +03:00
|
|
|
};
|
|
|
|
const resizer = new Resizer(
|
|
|
|
this.resizeContainer,
|
2018-12-03 12:43:35 +03:00
|
|
|
FixedDistributor,
|
|
|
|
{onResized: this._onResized},
|
|
|
|
);
|
2018-10-30 20:12:21 +03:00
|
|
|
resizer.setClassNames(classNames);
|
2018-12-18 19:12:32 +03:00
|
|
|
let rhsSize = window.localStorage.getItem("mx_rhs_size");
|
2018-10-31 14:08:41 +03:00
|
|
|
if (rhsSize !== null) {
|
2018-12-18 19:12:32 +03:00
|
|
|
rhsSize = parseInt(rhsSize, 10);
|
|
|
|
} else {
|
|
|
|
rhsSize = 350;
|
2018-10-31 14:08:41 +03:00
|
|
|
}
|
2018-12-18 19:12:32 +03:00
|
|
|
resizer.forHandleAt(0).resize(rhsSize);
|
2018-10-31 14:08:41 +03:00
|
|
|
|
|
|
|
resizer.attach();
|
|
|
|
this.resizer = resizer;
|
2018-10-30 20:12:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
_setResizeContainerRef(div) {
|
|
|
|
this.resizeContainer = div;
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
2019-12-06 03:47:18 +03:00
|
|
|
if (this.props.panel) {
|
2018-10-31 14:08:41 +03:00
|
|
|
this._createResizer();
|
|
|
|
}
|
2018-10-30 20:12:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
2018-10-31 14:17:13 +03:00
|
|
|
if (this.resizer) {
|
|
|
|
this.resizer.detach();
|
|
|
|
this.resizer = null;
|
|
|
|
}
|
2018-10-31 14:08:41 +03:00
|
|
|
}
|
|
|
|
|
2019-12-24 05:26:59 +03:00
|
|
|
componentDidUpdate(prevProps) {
|
|
|
|
const wasPanelSet = this.props.panel && !prevProps.panel;
|
|
|
|
const wasPanelCleared = !this.props.panel && prevProps.panel;
|
|
|
|
|
|
|
|
if (this.resizeContainer && wasPanelSet) {
|
|
|
|
// The resizer can only be created when **both** expanded and the panel is
|
|
|
|
// set. Once both are true, the container ref will mount, which is required
|
|
|
|
// for the resizer to work.
|
|
|
|
this._createResizer();
|
|
|
|
} else if (this.resizer && wasPanelCleared) {
|
|
|
|
this.resizer.detach();
|
|
|
|
this.resizer = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-30 20:12:21 +03:00
|
|
|
render() {
|
|
|
|
const bodyView = React.Children.only(this.props.children);
|
|
|
|
const panelView = this.props.panel;
|
|
|
|
|
2020-03-10 23:09:40 +03:00
|
|
|
const hasResizer = !this.props.collapsedRhs && panelView;
|
|
|
|
|
|
|
|
let children;
|
|
|
|
if (hasResizer) {
|
|
|
|
children = <React.Fragment>
|
2018-10-31 14:08:41 +03:00
|
|
|
<ResizeHandle reverse={true} />
|
2018-10-30 20:12:21 +03:00
|
|
|
{ panelView }
|
2020-03-10 23:09:40 +03:00
|
|
|
</React.Fragment>;
|
2018-10-30 20:12:21 +03:00
|
|
|
}
|
2020-03-10 23:09:40 +03:00
|
|
|
|
|
|
|
return <div className="mx_MainSplit" ref={hasResizer ? this._setResizeContainerRef : undefined}>
|
|
|
|
{ bodyView }
|
|
|
|
{ children }
|
|
|
|
</div>;
|
2018-10-30 20:12:21 +03:00
|
|
|
}
|
2018-10-31 14:20:10 +03:00
|
|
|
}
|