2018-10-17 12:38:25 +03:00
|
|
|
/*
|
2020-10-21 01:42:12 +03:00
|
|
|
Copyright 2018 - 2020 The Matrix.org Foundation C.I.C.
|
2018-10-17 12:38:25 +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.
|
|
|
|
*/
|
|
|
|
|
2020-10-13 18:11:58 +03:00
|
|
|
import {throttle} from "lodash";
|
|
|
|
|
2020-10-12 18:47:04 +03:00
|
|
|
import FixedDistributor from "./distributors/fixed";
|
|
|
|
import ResizeItem from "./item";
|
2020-10-20 13:03:03 +03:00
|
|
|
import Sizer from "./sizer";
|
2020-10-12 18:47:04 +03:00
|
|
|
|
|
|
|
interface IClassNames {
|
2018-10-16 16:16:10 +03:00
|
|
|
// class on resize-handle
|
2020-10-12 18:47:04 +03:00
|
|
|
handle?: string;
|
2018-10-16 16:16:10 +03:00
|
|
|
// class on resize-handle
|
2020-10-12 18:47:04 +03:00
|
|
|
reverse?: string;
|
2018-10-16 16:16:10 +03:00
|
|
|
// class on resize-handle
|
2020-10-12 18:47:04 +03:00
|
|
|
vertical?: string;
|
2018-10-16 16:16:10 +03:00
|
|
|
// class on container
|
2020-10-12 18:47:04 +03:00
|
|
|
resizing?: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface IConfig {
|
|
|
|
onResizeStart?(): void;
|
|
|
|
onResizeStop?(): void;
|
|
|
|
onResized?(size: number, id: string, element: HTMLElement): void;
|
|
|
|
}
|
|
|
|
|
|
|
|
export default class Resizer<C extends IConfig = IConfig> {
|
|
|
|
private classNames: IClassNames;
|
2018-12-20 01:51:19 +03:00
|
|
|
|
|
|
|
// TODO move vertical/horizontal to config option/container class
|
|
|
|
// as it doesn't make sense to mix them within one container/Resizer
|
2020-10-12 18:47:04 +03:00
|
|
|
constructor(
|
2020-10-13 14:28:23 +03:00
|
|
|
public container: HTMLElement,
|
2020-10-12 18:47:04 +03:00
|
|
|
private readonly distributorCtor: {
|
|
|
|
new(item: ResizeItem): FixedDistributor<C, any>;
|
|
|
|
createItem(resizeHandle: HTMLDivElement, resizer: Resizer, sizer: Sizer): ResizeItem;
|
|
|
|
createSizer(containerElement: HTMLElement, vertical: boolean, reverse: boolean): Sizer;
|
|
|
|
},
|
|
|
|
public readonly config?: C,
|
|
|
|
) {
|
2019-10-08 17:43:57 +03:00
|
|
|
if (!container) {
|
|
|
|
throw new Error("Resizer requires a non-null `container` arg");
|
|
|
|
}
|
2020-10-12 18:47:04 +03:00
|
|
|
|
2018-10-16 16:16:10 +03:00
|
|
|
this.classNames = {
|
|
|
|
handle: "resizer-handle",
|
|
|
|
reverse: "resizer-reverse",
|
|
|
|
vertical: "resizer-vertical",
|
|
|
|
resizing: "resizer-resizing",
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-10-12 18:47:04 +03:00
|
|
|
public setClassNames(classNames: IClassNames) {
|
2018-10-16 16:16:10 +03:00
|
|
|
this.classNames = classNames;
|
|
|
|
}
|
|
|
|
|
2020-10-12 18:47:04 +03:00
|
|
|
public attach() {
|
|
|
|
this.container.addEventListener("mousedown", this.onMouseDown, false);
|
2020-10-13 18:11:58 +03:00
|
|
|
window.addEventListener("resize", this.onResize);
|
2018-10-16 16:16:10 +03:00
|
|
|
}
|
|
|
|
|
2020-10-12 18:47:04 +03:00
|
|
|
public detach() {
|
|
|
|
this.container.removeEventListener("mousedown", this.onMouseDown, false);
|
2020-10-13 18:11:58 +03:00
|
|
|
window.removeEventListener("resize", this.onResize);
|
2018-10-16 16:16:10 +03:00
|
|
|
}
|
|
|
|
|
2018-10-17 14:45:56 +03:00
|
|
|
/**
|
|
|
|
Gives the distributor for a specific resize handle, as if you would have started
|
|
|
|
to drag that handle. Can be used to manipulate the size of an item programmatically.
|
|
|
|
@param {number} handleIndex the index of the resize handle in the container
|
2020-10-20 13:03:03 +03:00
|
|
|
@return {FixedDistributor} a new distributor for the given handle
|
2018-10-17 14:45:56 +03:00
|
|
|
*/
|
2020-10-12 18:47:04 +03:00
|
|
|
public forHandleAt(handleIndex: number): FixedDistributor<C> {
|
|
|
|
const handles = this.getResizeHandles();
|
2018-10-16 18:22:12 +03:00
|
|
|
const handle = handles[handleIndex];
|
2018-11-26 18:46:27 +03:00
|
|
|
if (handle) {
|
2020-10-12 18:47:04 +03:00
|
|
|
const {distributor} = this.createSizerAndDistributor(<HTMLDivElement>handle);
|
2018-11-26 18:46:27 +03:00
|
|
|
return distributor;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-12 18:47:04 +03:00
|
|
|
public forHandleWithId(id: string): FixedDistributor<C> {
|
|
|
|
const handles = this.getResizeHandles();
|
2018-11-26 18:46:27 +03:00
|
|
|
const handle = handles.find((h) => h.getAttribute("data-id") === id);
|
|
|
|
if (handle) {
|
2020-10-12 18:47:04 +03:00
|
|
|
const {distributor} = this.createSizerAndDistributor(<HTMLDivElement>handle);
|
2018-11-26 18:46:27 +03:00
|
|
|
return distributor;
|
|
|
|
}
|
2018-10-16 18:22:12 +03:00
|
|
|
}
|
|
|
|
|
2020-10-12 18:47:04 +03:00
|
|
|
public isReverseResizeHandle(el: HTMLElement): boolean {
|
2018-12-20 01:51:19 +03:00
|
|
|
return el && el.classList.contains(this.classNames.reverse);
|
|
|
|
}
|
|
|
|
|
2020-10-12 18:47:04 +03:00
|
|
|
public isResizeHandle(el: HTMLElement): boolean {
|
2018-10-16 16:16:10 +03:00
|
|
|
return el && el.classList.contains(this.classNames.handle);
|
|
|
|
}
|
|
|
|
|
2020-10-12 18:47:04 +03:00
|
|
|
private onMouseDown = (event: MouseEvent) => {
|
2018-12-18 16:27:10 +03:00
|
|
|
// use closest in case the resize handle contains
|
|
|
|
// child dom nodes that can be the target
|
2020-10-20 13:03:03 +03:00
|
|
|
const resizeHandle = event.target && (<HTMLDivElement>event.target).closest(`.${this.classNames.handle}`);
|
2018-12-18 16:27:10 +03:00
|
|
|
if (!resizeHandle || resizeHandle.parentElement !== this.container) {
|
2018-10-16 16:16:10 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
// prevent starting a drag operation
|
|
|
|
event.preventDefault();
|
2018-11-26 18:46:27 +03:00
|
|
|
|
2018-10-16 16:16:10 +03:00
|
|
|
// mark as currently resizing
|
|
|
|
if (this.classNames.resizing) {
|
|
|
|
this.container.classList.add(this.classNames.resizing);
|
|
|
|
}
|
2020-09-02 14:00:35 +03:00
|
|
|
if (this.config.onResizeStart) {
|
|
|
|
this.config.onResizeStart();
|
|
|
|
}
|
2018-10-16 16:16:10 +03:00
|
|
|
|
2020-10-12 18:47:04 +03:00
|
|
|
const {sizer, distributor} = this.createSizerAndDistributor(<HTMLDivElement>resizeHandle);
|
2019-01-11 19:17:58 +03:00
|
|
|
distributor.start();
|
2018-10-16 16:16:10 +03:00
|
|
|
|
|
|
|
const onMouseMove = (event) => {
|
|
|
|
const offset = sizer.offsetFromEvent(event);
|
2018-11-26 18:42:58 +03:00
|
|
|
distributor.resizeFromContainerOffset(offset);
|
2018-10-16 16:16:10 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
const body = document.body;
|
2019-01-11 20:20:34 +03:00
|
|
|
const finishResize = () => {
|
2018-10-16 16:16:10 +03:00
|
|
|
if (this.classNames.resizing) {
|
|
|
|
this.container.classList.remove(this.classNames.resizing);
|
|
|
|
}
|
2020-10-13 14:28:23 +03:00
|
|
|
distributor.finish();
|
2020-09-02 14:00:35 +03:00
|
|
|
if (this.config.onResizeStop) {
|
|
|
|
this.config.onResizeStop();
|
|
|
|
}
|
2019-01-11 20:20:34 +03:00
|
|
|
body.removeEventListener("mouseup", finishResize, false);
|
|
|
|
document.removeEventListener("mouseleave", finishResize, false);
|
2018-10-16 16:16:10 +03:00
|
|
|
body.removeEventListener("mousemove", onMouseMove, false);
|
|
|
|
};
|
2019-01-11 20:20:34 +03:00
|
|
|
body.addEventListener("mouseup", finishResize, false);
|
|
|
|
document.addEventListener("mouseleave", finishResize, false);
|
2018-10-16 16:16:10 +03:00
|
|
|
body.addEventListener("mousemove", onMouseMove, false);
|
2020-10-12 18:47:04 +03:00
|
|
|
};
|
2018-10-16 17:25:00 +03:00
|
|
|
|
2020-10-13 18:11:58 +03:00
|
|
|
private onResize = throttle(() => {
|
2020-10-15 13:14:48 +03:00
|
|
|
const distributors = this.getDistributors();
|
2020-10-13 18:11:58 +03:00
|
|
|
|
|
|
|
// relax all items if they had any overconstrained flexboxes
|
|
|
|
distributors.forEach(d => d.start());
|
|
|
|
distributors.forEach(d => d.finish());
|
|
|
|
}, 100, {trailing: true, leading: true});
|
|
|
|
|
2020-10-15 13:14:48 +03:00
|
|
|
public getDistributors = () => {
|
|
|
|
return this.getResizeHandles().map(handle => {
|
|
|
|
const {distributor} = this.createSizerAndDistributor(<HTMLDivElement>handle);
|
|
|
|
return distributor;
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2020-10-12 18:47:04 +03:00
|
|
|
private createSizerAndDistributor(
|
|
|
|
resizeHandle: HTMLDivElement,
|
|
|
|
): {sizer: Sizer, distributor: FixedDistributor<any>} {
|
2018-10-16 18:22:12 +03:00
|
|
|
const vertical = resizeHandle.classList.contains(this.classNames.vertical);
|
2018-12-20 01:51:19 +03:00
|
|
|
const reverse = this.isReverseResizeHandle(resizeHandle);
|
2019-01-14 22:24:54 +03:00
|
|
|
const Distributor = this.distributorCtor;
|
|
|
|
const sizer = Distributor.createSizer(this.container, vertical, reverse);
|
|
|
|
const item = Distributor.createItem(resizeHandle, this, sizer);
|
2020-10-12 18:47:04 +03:00
|
|
|
const distributor = new Distributor(item);
|
2018-10-16 18:22:12 +03:00
|
|
|
return {sizer, distributor};
|
|
|
|
}
|
|
|
|
|
2020-10-12 18:47:04 +03:00
|
|
|
private getResizeHandles() {
|
2020-10-15 13:14:48 +03:00
|
|
|
if (!this.container.children) return [];
|
2018-10-16 18:22:12 +03:00
|
|
|
return Array.from(this.container.children).filter(el => {
|
2020-10-12 18:47:04 +03:00
|
|
|
return this.isResizeHandle(<HTMLElement>el);
|
|
|
|
}) as HTMLElement[];
|
2018-10-16 18:22:12 +03:00
|
|
|
}
|
2018-10-16 16:16:10 +03:00
|
|
|
}
|