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.
|
|
|
|
*/
|
|
|
|
|
2018-10-17 14:42:30 +03:00
|
|
|
/**
|
|
|
|
implements DOM/CSS operations for resizing.
|
|
|
|
The sizer determines what CSS mechanism is used for sizing items, like flexbox, ...
|
|
|
|
*/
|
2019-01-14 22:24:54 +03:00
|
|
|
export default class Sizer {
|
2020-10-12 18:47:04 +03:00
|
|
|
constructor(
|
|
|
|
protected readonly container: HTMLElement,
|
|
|
|
protected readonly vertical: boolean,
|
|
|
|
protected readonly reverse: boolean,
|
|
|
|
) {}
|
2018-09-24 18:07:42 +03:00
|
|
|
|
2018-10-17 14:36:15 +03:00
|
|
|
/**
|
|
|
|
@param {Element} item the dom element being resized
|
|
|
|
@return {number} how far the edge of the item is from the edge of the container
|
|
|
|
*/
|
2020-10-12 18:47:04 +03:00
|
|
|
public getItemOffset(item: HTMLElement): number {
|
|
|
|
const offset = (this.vertical ? item.offsetTop : item.offsetLeft) - this.getOffset();
|
2018-09-24 18:07:42 +03:00
|
|
|
if (this.reverse) {
|
|
|
|
return this.getTotalSize() - (offset + this.getItemSize(item));
|
|
|
|
} else {
|
|
|
|
return offset;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-17 14:36:15 +03:00
|
|
|
/**
|
|
|
|
@param {Element} item the dom element being resized
|
|
|
|
@return {number} the width/height of an item in the container
|
|
|
|
*/
|
2020-10-12 18:47:04 +03:00
|
|
|
public getItemSize(item: HTMLElement): number {
|
2018-09-24 18:07:42 +03:00
|
|
|
return this.vertical ? item.offsetHeight : item.offsetWidth;
|
|
|
|
}
|
|
|
|
|
2018-10-17 14:36:15 +03:00
|
|
|
/** @return {number} the width/height of the container */
|
2020-10-12 18:47:04 +03:00
|
|
|
public getTotalSize(): number {
|
2018-09-24 18:07:42 +03:00
|
|
|
return this.vertical ? this.container.offsetHeight : this.container.offsetWidth;
|
|
|
|
}
|
|
|
|
|
2018-10-17 14:36:15 +03:00
|
|
|
/** @return {number} container offset to offsetParent */
|
2020-10-12 18:47:04 +03:00
|
|
|
private getOffset(): number {
|
2018-09-24 18:07:42 +03:00
|
|
|
return this.vertical ? this.container.offsetTop : this.container.offsetLeft;
|
|
|
|
}
|
|
|
|
|
2020-04-21 00:41:58 +03:00
|
|
|
/** @return {number} container offset to document */
|
2020-10-20 13:03:03 +03:00
|
|
|
private getPageOffset(): number {
|
2020-04-21 00:41:58 +03:00
|
|
|
let element = this.container;
|
|
|
|
let offset = 0;
|
|
|
|
while (element) {
|
|
|
|
const pos = this.vertical ? element.offsetTop : element.offsetLeft;
|
|
|
|
offset = offset + pos;
|
2020-10-20 13:03:03 +03:00
|
|
|
element = <HTMLElement>element.offsetParent;
|
2020-04-21 00:41:58 +03:00
|
|
|
}
|
|
|
|
return offset;
|
|
|
|
}
|
|
|
|
|
2020-10-13 14:28:23 +03:00
|
|
|
public getDesiredItemSize(item: HTMLElement) {
|
2018-09-24 18:07:42 +03:00
|
|
|
if (this.vertical) {
|
2020-10-13 14:28:23 +03:00
|
|
|
return item.style.height;
|
2018-09-24 18:07:42 +03:00
|
|
|
} else {
|
2020-10-13 14:28:23 +03:00
|
|
|
return item.style.width;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public setItemSize(item: HTMLElement, size: string) {
|
|
|
|
if (this.vertical) {
|
|
|
|
item.style.height = size;
|
|
|
|
} else {
|
|
|
|
item.style.width = size;
|
2018-09-24 18:07:42 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-12 18:47:04 +03:00
|
|
|
public clearItemSize(item: HTMLElement) {
|
2019-01-10 16:05:00 +03:00
|
|
|
if (this.vertical) {
|
|
|
|
item.style.height = null;
|
|
|
|
} else {
|
|
|
|
item.style.width = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-12 18:47:04 +03:00
|
|
|
public start(item: HTMLElement) {}
|
|
|
|
|
|
|
|
public finish(item: HTMLElement) {}
|
|
|
|
|
2018-10-17 14:36:15 +03:00
|
|
|
/**
|
|
|
|
@param {MouseEvent} event the mouse event
|
|
|
|
@return {number} the distance between the cursor and the edge of the container,
|
|
|
|
along the applicable axis (vertical or horizontal)
|
|
|
|
*/
|
2020-10-12 18:47:04 +03:00
|
|
|
public offsetFromEvent(event: MouseEvent) {
|
2018-09-24 18:07:42 +03:00
|
|
|
const pos = this.vertical ? event.pageY : event.pageX;
|
|
|
|
if (this.reverse) {
|
2020-10-12 18:47:04 +03:00
|
|
|
return (this.getPageOffset() + this.getTotalSize()) - pos;
|
2018-09-24 18:07:42 +03:00
|
|
|
} else {
|
2020-10-12 18:47:04 +03:00
|
|
|
return pos - this.getPageOffset();
|
2018-09-24 18:07:42 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|