|
| 1 | +/* |
| 2 | +Copyright 2015, 2016 OpenMarket Ltd |
| 3 | +Copyright 2017, 2018 Vector Creations Ltd |
| 4 | +Copyright 2020 The Matrix.org Foundation C.I.C. |
| 5 | +
|
| 6 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +you may not use this file except in compliance with the License. |
| 8 | +You may obtain a copy of the License at |
| 9 | +
|
| 10 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +
|
| 12 | +Unless required by applicable law or agreed to in writing, software |
| 13 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +See the License for the specific language governing permissions and |
| 16 | +limitations under the License. |
| 17 | +*/ |
| 18 | + |
| 19 | +import * as React from "react"; |
| 20 | +import { _t } from "../../../languageHandler"; |
| 21 | +import { Layout } from '../../../resizer/distributors/roomsublist2'; |
| 22 | +import { RovingTabIndexProvider } from "../../../accessibility/RovingTabIndex"; |
| 23 | +import { ResizeNotifier } from "../../../utils/ResizeNotifier"; |
| 24 | +import RoomListStore from "../../../stores/room-list/RoomListStore2"; |
| 25 | + |
| 26 | +interface IProps { |
| 27 | + onKeyDown: (ev: React.KeyboardEvent) => void; |
| 28 | + onFocus: (ev: React.FocusEvent) => void; |
| 29 | + onBlur: (ev: React.FocusEvent) => void; |
| 30 | + resizeNotifier: ResizeNotifier; |
| 31 | + collapsed: boolean; |
| 32 | + searchFilter: string; |
| 33 | +} |
| 34 | + |
| 35 | +interface IState { |
| 36 | +} |
| 37 | + |
| 38 | +// TODO: Actually write stub |
| 39 | +export class RoomSublist2 extends React.Component<any, any> { |
| 40 | + public setHeight(size: number) { |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +export default class RoomList2 extends React.Component<IProps, IState> { |
| 45 | + private sublistRefs: { [tagId: string]: React.RefObject<RoomSublist2> } = {}; |
| 46 | + private sublistSizes: { [tagId: string]: number } = {}; |
| 47 | + private sublistCollapseStates: { [tagId: string]: boolean } = {}; |
| 48 | + private unfilteredLayout: Layout; |
| 49 | + private filteredLayout: Layout; |
| 50 | + |
| 51 | + constructor(props: IProps) { |
| 52 | + super(props); |
| 53 | + |
| 54 | + this.loadSublistSizes(); |
| 55 | + this.prepareLayouts(); |
| 56 | + } |
| 57 | + |
| 58 | + public componentDidMount(): void { |
| 59 | + RoomListStore.instance.addListener(() => { |
| 60 | + console.log(RoomListStore.instance.orderedLists); |
| 61 | + }); |
| 62 | + } |
| 63 | + |
| 64 | + private loadSublistSizes() { |
| 65 | + const sizesJson = window.localStorage.getItem("mx_roomlist_sizes"); |
| 66 | + if (sizesJson) this.sublistSizes = JSON.parse(sizesJson); |
| 67 | + |
| 68 | + const collapsedJson = window.localStorage.getItem("mx_roomlist_collapsed"); |
| 69 | + if (collapsedJson) this.sublistCollapseStates = JSON.parse(collapsedJson); |
| 70 | + } |
| 71 | + |
| 72 | + private saveSublistSizes() { |
| 73 | + window.localStorage.setItem("mx_roomlist_sizes", JSON.stringify(this.sublistSizes)); |
| 74 | + window.localStorage.setItem("mx_roomlist_collapsed", JSON.stringify(this.sublistCollapseStates)); |
| 75 | + } |
| 76 | + |
| 77 | + private prepareLayouts() { |
| 78 | + this.unfilteredLayout = new Layout((tagId: string, height: number) => { |
| 79 | + const sublist = this.sublistRefs[tagId]; |
| 80 | + if (sublist) sublist.current.setHeight(height); |
| 81 | + |
| 82 | + // TODO: Check overflow |
| 83 | + |
| 84 | + // Don't store a height for collapsed sublists |
| 85 | + if (!this.sublistCollapseStates[tagId]) { |
| 86 | + this.sublistSizes[tagId] = height; |
| 87 | + this.saveSublistSizes(); |
| 88 | + } |
| 89 | + }, this.sublistSizes, this.sublistCollapseStates, { |
| 90 | + allowWhitespace: false, |
| 91 | + handleHeight: 1, |
| 92 | + }); |
| 93 | + |
| 94 | + this.filteredLayout = new Layout((tagId: string, height: number) => { |
| 95 | + const sublist = this.sublistRefs[tagId]; |
| 96 | + if (sublist) sublist.current.setHeight(height); |
| 97 | + }, null, null, { |
| 98 | + allowWhitespace: false, |
| 99 | + handleHeight: 0, |
| 100 | + }); |
| 101 | + } |
| 102 | + |
| 103 | + private collectSublistRef(tagId: string, ref: React.RefObject<RoomSublist2>) { |
| 104 | + if (!ref) { |
| 105 | + delete this.sublistRefs[tagId]; |
| 106 | + } else { |
| 107 | + this.sublistRefs[tagId] = ref; |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + public render() { |
| 112 | + return ( |
| 113 | + <RovingTabIndexProvider handleHomeEnd={true} onKeyDown={this.props.onKeyDown}> |
| 114 | + {({onKeyDownHandler}) => ( |
| 115 | + <div |
| 116 | + onFocus={this.props.onFocus} |
| 117 | + onBlur={this.props.onBlur} |
| 118 | + onKeyDown={onKeyDownHandler} |
| 119 | + className="mx_RoomList" |
| 120 | + role="tree" |
| 121 | + aria-label={_t("Rooms")} |
| 122 | + // Firefox sometimes makes this element focusable due to |
| 123 | + // overflow:scroll;, so force it out of tab order. |
| 124 | + tabIndex={-1} |
| 125 | + >{_t("TODO")}</div> |
| 126 | + )} |
| 127 | + </RovingTabIndexProvider> |
| 128 | + ); |
| 129 | + } |
| 130 | +} |
0 commit comments