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
|
|
|
/**
|
|
|
|
distributors translate a moving cursor into
|
|
|
|
CSS/DOM changes by calling the sizer
|
|
|
|
|
|
|
|
they have one method, `resize` that receives
|
|
|
|
the offset from the container edge of where
|
|
|
|
the mouse cursor is.
|
|
|
|
*/
|
2018-09-24 18:07:42 +03:00
|
|
|
class FixedDistributor {
|
2018-10-16 19:42:56 +03:00
|
|
|
constructor(sizer, item, config) {
|
2018-09-24 18:07:42 +03:00
|
|
|
this.sizer = sizer;
|
2018-10-16 17:25:00 +03:00
|
|
|
this.item = item;
|
|
|
|
this.beforeOffset = sizer.getItemOffset(this.item);
|
2018-10-18 16:51:01 +03:00
|
|
|
this.onResized = config && config.onResized;
|
2018-09-24 18:07:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
resize(offset) {
|
|
|
|
const itemSize = offset - this.beforeOffset;
|
|
|
|
this.sizer.setItemSize(this.item, itemSize);
|
2018-10-16 19:42:56 +03:00
|
|
|
if (this.onResized) {
|
|
|
|
this.onResized(itemSize, this.item);
|
|
|
|
}
|
2018-09-24 18:07:42 +03:00
|
|
|
return itemSize;
|
|
|
|
}
|
|
|
|
|
2018-10-16 18:22:12 +03:00
|
|
|
sizeFromOffset(offset) {
|
|
|
|
return offset - this.beforeOffset;
|
2018-09-24 18:07:42 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
class CollapseDistributor extends FixedDistributor {
|
2018-10-16 17:25:00 +03:00
|
|
|
constructor(sizer, item, config) {
|
2018-10-16 19:42:56 +03:00
|
|
|
super(sizer, item, config);
|
2018-10-16 15:56:49 +03:00
|
|
|
this.toggleSize = config && config.toggleSize;
|
|
|
|
this.onCollapsed = config && config.onCollapsed;
|
2018-10-16 19:43:13 +03:00
|
|
|
this.isCollapsed = false;
|
2018-09-24 18:07:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
resize(offset) {
|
2018-10-17 14:36:15 +03:00
|
|
|
const newSize = this.sizeFromOffset(offset);
|
2018-10-16 19:43:13 +03:00
|
|
|
const isCollapsedSize = newSize < this.toggleSize;
|
|
|
|
if (isCollapsedSize && !this.isCollapsed) {
|
|
|
|
this.isCollapsed = true;
|
2018-10-16 15:56:49 +03:00
|
|
|
if (this.onCollapsed) {
|
|
|
|
this.onCollapsed(true, this.item);
|
|
|
|
}
|
2018-10-17 14:36:15 +03:00
|
|
|
} else if (!isCollapsedSize && this.isCollapsed) {
|
2018-10-16 15:56:49 +03:00
|
|
|
if (this.onCollapsed) {
|
|
|
|
this.onCollapsed(false, this.item);
|
|
|
|
}
|
2018-10-16 19:43:13 +03:00
|
|
|
this.isCollapsed = false;
|
|
|
|
}
|
|
|
|
if (!isCollapsedSize) {
|
|
|
|
super.resize(offset);
|
2018-09-24 18:07:42 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
FixedDistributor,
|
|
|
|
CollapseDistributor,
|
|
|
|
};
|