element-web/src/resizer/item.ts

126 lines
3.8 KiB
TypeScript
Raw Normal View History

2018-12-20 01:51:19 +03:00
/*
Copyright 2019 New Vector Ltd
Copyright 2020 The Matrix.org Foundation C.I.C.
2018-12-20 01:51:19 +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 Sizer from "./sizer";
import Resizer, {IConfig} from "./resizer";
export default class ResizeItem<C extends IConfig = IConfig> {
protected readonly domNode: HTMLElement;
protected readonly id: string;
protected reverse: boolean;
constructor(
handle: HTMLElement,
public readonly resizer: Resizer<C>,
protected readonly sizer: Sizer,
) {
const id = handle.getAttribute("data-id");
const reverse = resizer.isReverseResizeHandle(handle);
this.domNode = <HTMLElement>(reverse ? handle.nextElementSibling : handle.previousElementSibling);
2018-12-20 01:51:19 +03:00
this.id = id;
this.reverse = reverse;
this.resizer = resizer;
this.sizer = sizer;
}
private copyWith(handle: Element, resizer: Resizer, sizer: Sizer) {
const Ctor = this.constructor as typeof ResizeItem;
return new Ctor(<HTMLElement>handle, resizer, sizer);
2018-12-20 01:51:19 +03:00
}
private advance(forwards: boolean) {
2018-12-20 01:51:19 +03:00
// opposite direction from fromResizeHandle to get back to handle
let handle = <HTMLElement>(this.reverse ?
2018-12-20 01:51:19 +03:00
this.domNode.previousElementSibling :
this.domNode.nextElementSibling);
2018-12-20 01:51:19 +03:00
const moveNext = forwards !== this.reverse; // xor
// iterate at least once to avoid infinite loop
do {
if (moveNext) {
handle = <HTMLElement>handle.nextElementSibling;
2018-12-20 01:51:19 +03:00
} else {
handle = <HTMLElement>handle.previousElementSibling;
2018-12-20 01:51:19 +03:00
}
} while (handle && !this.resizer.isResizeHandle(handle));
2018-12-20 01:51:19 +03:00
if (handle) {
const nextHandle = this.copyWith(handle, this.resizer, this.sizer);
2018-12-20 01:51:19 +03:00
nextHandle.reverse = this.reverse;
return nextHandle;
}
}
public next() {
return this.advance(true);
2018-12-20 01:51:19 +03:00
}
public previous() {
return this.advance(false);
2018-12-20 01:51:19 +03:00
}
public size() {
2018-12-20 01:51:19 +03:00
return this.sizer.getItemSize(this.domNode);
}
public offset() {
2018-12-20 01:51:19 +03:00
return this.sizer.getItemOffset(this.domNode);
}
public start() {
this.sizer.start(this.domNode);
}
public finish() {
this.sizer.finish(this.domNode);
}
public setSize(size: number) {
2018-12-20 01:51:19 +03:00
this.sizer.setItemSize(this.domNode, size);
const callback = this.resizer.config.onResized;
2018-12-20 01:51:19 +03:00
if (callback) {
callback(size, this.id, this.domNode);
}
}
public clearSize() {
this.sizer.clearItemSize(this.domNode);
const callback = this.resizer.config.onResized;
2019-01-10 20:40:26 +03:00
if (callback) {
callback(null, this.id, this.domNode);
}
}
public first() {
const firstHandle = Array.from(this.domNode.parentElement.children).find(el => {
return this.resizer.isResizeHandle(<HTMLElement>el);
});
if (firstHandle) {
return this.copyWith(firstHandle, this.resizer, this.sizer);
}
}
public last() {
const lastHandle = Array.from(this.domNode.parentElement.children).reverse().find(el => {
return this.resizer.isResizeHandle(<HTMLElement>el);
});
if (lastHandle) {
return this.copyWith(lastHandle, this.resizer, this.sizer);
}
}
2018-12-20 01:51:19 +03:00
}