2018-10-17 12:38:25 +03:00
|
|
|
/*
|
|
|
|
Copyright 2018 New Vector Ltd
|
|
|
|
|
|
|
|
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 {
|
2018-09-24 18:07:42 +03:00
|
|
|
constructor(container, vertical, reverse) {
|
|
|
|
this.container = container;
|
|
|
|
this.reverse = reverse;
|
|
|
|
this.vertical = vertical;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
*/
|
2018-09-24 18:07:42 +03:00
|
|
|
getItemOffset(item) {
|
|
|
|
const offset = (this.vertical ? item.offsetTop : item.offsetLeft) - this._getOffset();
|
|
|
|
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
|
|
|
|
*/
|
2018-09-24 18:07:42 +03:00
|
|
|
getItemSize(item) {
|
|
|
|
return this.vertical ? item.offsetHeight : item.offsetWidth;
|
|
|
|
}
|
|
|
|
|
2018-10-17 14:36:15 +03:00
|
|
|
/** @return {number} the width/height of the container */
|
2018-09-24 18:07:42 +03:00
|
|
|
getTotalSize() {
|
|
|
|
return this.vertical ? this.container.offsetHeight : this.container.offsetWidth;
|
|
|
|
}
|
|
|
|
|
2018-10-17 14:36:15 +03:00
|
|
|
/** @return {number} container offset to offsetParent */
|
2018-09-24 18:07:42 +03:00
|
|
|
_getOffset() {
|
|
|
|
return this.vertical ? this.container.offsetTop : this.container.offsetLeft;
|
|
|
|
}
|
|
|
|
|
2020-04-21 00:41:58 +03:00
|
|
|
/** @return {number} container offset to document */
|
|
|
|
_getPageOffset() {
|
|
|
|
let element = this.container;
|
|
|
|
let offset = 0;
|
|
|
|
while (element) {
|
|
|
|
const pos = this.vertical ? element.offsetTop : element.offsetLeft;
|
|
|
|
offset = offset + pos;
|
|
|
|
element = element.offsetParent;
|
|
|
|
}
|
|
|
|
return offset;
|
|
|
|
}
|
|
|
|
|
2018-09-24 18:07:42 +03:00
|
|
|
setItemSize(item, size) {
|
|
|
|
if (this.vertical) {
|
|
|
|
item.style.height = `${Math.round(size)}px`;
|
|
|
|
} else {
|
|
|
|
item.style.width = `${Math.round(size)}px`;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-10 16:05:00 +03:00
|
|
|
clearItemSize(item) {
|
|
|
|
if (this.vertical) {
|
|
|
|
item.style.height = null;
|
|
|
|
} else {
|
|
|
|
item.style.width = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
*/
|
2018-09-24 18:07:42 +03:00
|
|
|
offsetFromEvent(event) {
|
|
|
|
const pos = this.vertical ? event.pageY : event.pageX;
|
|
|
|
if (this.reverse) {
|
2020-04-21 00:41:58 +03:00
|
|
|
return (this._getPageOffset() + this.getTotalSize()) - pos;
|
2018-09-24 18:07:42 +03:00
|
|
|
} else {
|
2020-04-21 00:41:58 +03:00
|
|
|
return pos - this._getPageOffset();
|
2018-09-24 18:07:42 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|