Files
ThreadNet-Web/src/resizer/distributors/collapse.ts
T

63 lines
2.0 KiB
TypeScript
Raw Normal View History

2018-10-17 11:38:25 +02:00
/*
2019-01-16 10:29:22 +00:00
Copyright 2019 New Vector Ltd
2018-10-17 11:38:25 +02: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.
*/
2019-01-14 20:33:33 +01:00
import FixedDistributor from "./fixed";
import ResizeItem from "../item";
2020-10-20 11:03:03 +01:00
import Resizer, {IConfig} from "../resizer";
import Sizer from "../sizer";
2020-10-20 11:03:03 +01:00
export interface ICollapseConfig extends IConfig {
toggleSize: number;
onCollapsed?(collapsed: boolean, id: string, element: HTMLElement): void;
}
class CollapseItem extends ResizeItem<ICollapseConfig> {
notifyCollapsed(collapsed: boolean) {
2019-01-14 20:24:54 +01:00
const callback = this.resizer.config.onCollapsed;
if (callback) {
callback(collapsed, this.id, this.domNode);
}
}
}
2020-10-20 11:03:03 +01:00
export default class CollapseDistributor extends FixedDistributor<ICollapseConfig, CollapseItem> {
static createItem(resizeHandle: HTMLDivElement, resizer: Resizer<ICollapseConfig>, sizer: Sizer) {
2019-01-14 20:24:54 +01:00
return new CollapseItem(resizeHandle, resizer, sizer);
}
2020-10-20 11:03:03 +01:00
private readonly toggleSize: number;
private isCollapsed = false;
constructor(item: CollapseItem) {
2018-12-19 23:51:19 +01:00
super(item);
2020-10-20 11:03:03 +01:00
this.toggleSize = item.resizer?.config?.toggleSize;
}
2020-10-20 11:03:03 +01:00
public resize(newSize: number) {
2018-10-16 18:43:13 +02:00
const isCollapsedSize = newSize < this.toggleSize;
if (isCollapsedSize && !this.isCollapsed) {
this.isCollapsed = true;
2019-01-14 20:24:54 +01:00
this.item.notifyCollapsed(true);
2018-10-17 13:36:15 +02:00
} else if (!isCollapsedSize && this.isCollapsed) {
2019-01-14 20:24:54 +01:00
this.item.notifyCollapsed(false);
2018-10-16 18:43:13 +02:00
this.isCollapsed = false;
}
if (!isCollapsedSize) {
super.resize(newSize);
}
}
}