2018-12-20 01:51:19 +03:00
|
|
|
/*
|
2019-01-16 13:29:05 +03:00
|
|
|
Copyright 2019 New Vector Ltd
|
2020-10-12 18:47:04 +03:00
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2020-10-12 18:47:04 +03:00
|
|
|
import Sizer from "./sizer";
|
|
|
|
import Resizer, {IConfig} from "./resizer";
|
|
|
|
|
|
|
|
export default class ResizeItem<C extends IConfig = IConfig> {
|
2020-10-13 14:28:23 +03:00
|
|
|
public readonly domNode: HTMLElement;
|
2020-10-12 18:47:04 +03:00
|
|
|
protected readonly id: string;
|
|
|
|
protected reverse: boolean;
|
|
|
|
|
|
|
|
constructor(
|
|
|
|
handle: HTMLElement,
|
|
|
|
public readonly resizer: Resizer<C>,
|
2020-10-13 14:28:23 +03:00
|
|
|
public readonly sizer: Sizer,
|
2020-10-12 18:47:04 +03:00
|
|
|
) {
|
2019-01-14 22:24:54 +03:00
|
|
|
const id = handle.getAttribute("data-id");
|
|
|
|
const reverse = resizer.isReverseResizeHandle(handle);
|
|
|
|
|
2020-10-12 18:47:04 +03:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-10-12 18:47:04 +03:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-10-12 18:47:04 +03:00
|
|
|
private advance(forwards: boolean) {
|
2018-12-20 01:51:19 +03:00
|
|
|
// opposite direction from fromResizeHandle to get back to handle
|
2020-10-12 18:47:04 +03:00
|
|
|
let handle = <HTMLElement>(this.reverse ?
|
2018-12-20 01:51:19 +03:00
|
|
|
this.domNode.previousElementSibling :
|
2020-10-12 18:47:04 +03:00
|
|
|
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) {
|
2020-10-12 18:47:04 +03:00
|
|
|
handle = <HTMLElement>handle.nextElementSibling;
|
2018-12-20 01:51:19 +03:00
|
|
|
} else {
|
2020-10-12 18:47:04 +03:00
|
|
|
handle = <HTMLElement>handle.previousElementSibling;
|
2018-12-20 01:51:19 +03:00
|
|
|
}
|
2019-01-14 22:24:54 +03:00
|
|
|
} while (handle && !this.resizer.isResizeHandle(handle));
|
|
|
|
|
2018-12-20 01:51:19 +03:00
|
|
|
if (handle) {
|
2020-10-12 18:47:04 +03:00
|
|
|
const nextHandle = this.copyWith(handle, this.resizer, this.sizer);
|
2018-12-20 01:51:19 +03:00
|
|
|
nextHandle.reverse = this.reverse;
|
|
|
|
return nextHandle;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-12 18:47:04 +03:00
|
|
|
public next() {
|
|
|
|
return this.advance(true);
|
2018-12-20 01:51:19 +03:00
|
|
|
}
|
|
|
|
|
2020-10-12 18:47:04 +03:00
|
|
|
public previous() {
|
|
|
|
return this.advance(false);
|
2018-12-20 01:51:19 +03:00
|
|
|
}
|
|
|
|
|
2020-10-12 18:47:04 +03:00
|
|
|
public size() {
|
2018-12-20 01:51:19 +03:00
|
|
|
return this.sizer.getItemSize(this.domNode);
|
|
|
|
}
|
|
|
|
|
2020-10-12 18:47:04 +03:00
|
|
|
public offset() {
|
2018-12-20 01:51:19 +03:00
|
|
|
return this.sizer.getItemOffset(this.domNode);
|
|
|
|
}
|
|
|
|
|
2020-10-12 18:47:04 +03:00
|
|
|
public start() {
|
|
|
|
this.sizer.start(this.domNode);
|
|
|
|
}
|
|
|
|
|
|
|
|
public finish() {
|
|
|
|
this.sizer.finish(this.domNode);
|
|
|
|
}
|
|
|
|
|
2020-10-13 14:28:23 +03:00
|
|
|
public getSize() {
|
|
|
|
return this.sizer.getDesiredItemSize(this.domNode);
|
|
|
|
}
|
|
|
|
|
|
|
|
public setRawSize(size: string) {
|
2018-12-20 01:51:19 +03:00
|
|
|
this.sizer.setItemSize(this.domNode, size);
|
2020-10-13 14:28:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public setSize(size: number) {
|
|
|
|
this.setRawSize(`${Math.round(size)}px`);
|
2019-01-14 22:24:54 +03:00
|
|
|
const callback = this.resizer.config.onResized;
|
2018-12-20 01:51:19 +03:00
|
|
|
if (callback) {
|
|
|
|
callback(size, this.id, this.domNode);
|
|
|
|
}
|
|
|
|
}
|
2019-01-10 16:05:00 +03:00
|
|
|
|
2020-10-12 18:47:04 +03:00
|
|
|
public clearSize() {
|
2019-01-10 16:05:00 +03:00
|
|
|
this.sizer.clearItemSize(this.domNode);
|
2019-01-14 22:24:54 +03:00
|
|
|
const callback = this.resizer.config.onResized;
|
2019-01-10 20:40:26 +03:00
|
|
|
if (callback) {
|
|
|
|
callback(null, this.id, this.domNode);
|
|
|
|
}
|
2019-01-10 16:05:00 +03:00
|
|
|
}
|
2019-01-11 19:17:58 +03:00
|
|
|
|
2020-10-12 18:47:04 +03:00
|
|
|
public first() {
|
2019-01-11 19:17:58 +03:00
|
|
|
const firstHandle = Array.from(this.domNode.parentElement.children).find(el => {
|
2020-10-12 18:47:04 +03:00
|
|
|
return this.resizer.isResizeHandle(<HTMLElement>el);
|
2019-01-11 19:17:58 +03:00
|
|
|
});
|
|
|
|
if (firstHandle) {
|
2020-10-12 18:47:04 +03:00
|
|
|
return this.copyWith(firstHandle, this.resizer, this.sizer);
|
2019-01-11 19:17:58 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-12 18:47:04 +03:00
|
|
|
public last() {
|
2019-01-11 19:17:58 +03:00
|
|
|
const lastHandle = Array.from(this.domNode.parentElement.children).reverse().find(el => {
|
2020-10-12 18:47:04 +03:00
|
|
|
return this.resizer.isResizeHandle(<HTMLElement>el);
|
2019-01-11 19:17:58 +03:00
|
|
|
});
|
|
|
|
if (lastHandle) {
|
2020-10-12 18:47:04 +03:00
|
|
|
return this.copyWith(lastHandle, this.resizer, this.sizer);
|
2019-01-11 19:17:58 +03:00
|
|
|
}
|
|
|
|
}
|
2018-12-20 01:51:19 +03:00
|
|
|
}
|