2018-10-17 11:38:25 +02:00
|
|
|
/*
|
2020-10-20 23:42:12 +01:00
|
|
|
Copyright 2019 - 2020 The Matrix.org Foundation C.I.C.
|
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";
|
2021-06-29 13:11:58 +01:00
|
|
|
import Resizer, { IConfig } from "../resizer";
|
2020-10-20 11:03:03 +01:00
|
|
|
import Sizer from "../sizer";
|
2018-09-24 16:07:42 +01:00
|
|
|
|
2020-10-20 11:03:03 +01:00
|
|
|
export interface ICollapseConfig extends IConfig {
|
|
|
|
|
toggleSize: number;
|
|
|
|
|
onCollapsed?(collapsed: boolean, id: string, element: HTMLElement): void;
|
2021-03-02 14:02:03 +00:00
|
|
|
isItemCollapsed(element: HTMLElement): boolean;
|
2020-10-20 11:03:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-03-02 14:02:03 +00:00
|
|
|
|
|
|
|
|
get isCollapsed() {
|
|
|
|
|
const isItemCollapsed = this.resizer.config.isItemCollapsed;
|
|
|
|
|
return isItemCollapsed(this.domNode);
|
|
|
|
|
}
|
2019-01-14 20:24:54 +01:00
|
|
|
}
|
2018-09-24 16:07:42 +01:00
|
|
|
|
2020-10-20 11:03:03 +01:00
|
|
|
export default class CollapseDistributor extends FixedDistributor<ICollapseConfig, CollapseItem> {
|
2021-08-24 13:05:46 +02:00
|
|
|
static createItem(resizeHandle: HTMLDivElement, resizer: Resizer<ICollapseConfig>, sizer: Sizer, container?: HTMLElement): CollapseItem {
|
|
|
|
|
return new CollapseItem(resizeHandle, resizer, sizer, container);
|
2019-01-14 20:24:54 +01:00
|
|
|
}
|
|
|
|
|
|
2020-10-20 11:03:03 +01:00
|
|
|
private readonly toggleSize: number;
|
2021-03-02 14:02:03 +00:00
|
|
|
private isCollapsed: boolean;
|
2020-10-20 11:03:03 +01:00
|
|
|
|
|
|
|
|
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;
|
2021-03-02 14:02:03 +00:00
|
|
|
this.isCollapsed = item.isCollapsed;
|
2018-09-24 16:07:42 +01:00
|
|
|
}
|
|
|
|
|
|
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;
|
2021-08-24 13:05:46 +02:00
|
|
|
if (isCollapsedSize !== this.isCollapsed) {
|
|
|
|
|
this.isCollapsed = isCollapsedSize;
|
|
|
|
|
this.item.notifyCollapsed(isCollapsedSize);
|
2018-10-16 18:43:13 +02:00
|
|
|
}
|
|
|
|
|
if (!isCollapsedSize) {
|
2018-11-26 16:42:58 +01:00
|
|
|
super.resize(newSize);
|
2018-10-16 12:26:08 +02:00
|
|
|
}
|
2018-09-24 16:07:42 +01:00
|
|
|
}
|
|
|
|
|
}
|