2020-06-04 06:16:53 +03:00
|
|
|
/*
|
|
|
|
Copyright 2020 The Matrix.org Foundation C.I.C.
|
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2020-06-05 22:14:44 +03:00
|
|
|
import { TagID } from "./models";
|
|
|
|
|
2020-06-05 06:21:04 +03:00
|
|
|
const TILE_HEIGHT_PX = 44;
|
2020-06-04 06:16:53 +03:00
|
|
|
|
|
|
|
interface ISerializedListLayout {
|
|
|
|
numTiles: number;
|
2020-06-10 06:12:49 +03:00
|
|
|
showPreviews: boolean;
|
2020-06-04 06:16:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
export class ListLayout {
|
|
|
|
private _n = 0;
|
2020-06-10 06:12:49 +03:00
|
|
|
private _previews = false;
|
2020-06-04 06:16:53 +03:00
|
|
|
|
2020-06-05 22:14:44 +03:00
|
|
|
constructor(public readonly tagId: TagID) {
|
2020-06-04 06:16:53 +03:00
|
|
|
const serialized = localStorage.getItem(this.key);
|
|
|
|
if (serialized) {
|
|
|
|
// We don't use the setters as they cause writes.
|
|
|
|
const parsed = <ISerializedListLayout>JSON.parse(serialized);
|
|
|
|
this._n = parsed.numTiles;
|
2020-06-10 06:12:49 +03:00
|
|
|
this._previews = parsed.showPreviews;
|
2020-06-04 06:16:53 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-10 06:12:49 +03:00
|
|
|
public get showPreviews(): boolean {
|
|
|
|
return this._previews;
|
|
|
|
}
|
|
|
|
|
|
|
|
public set showPreviews(v: boolean) {
|
|
|
|
this._previews = v;
|
|
|
|
this.save();
|
|
|
|
}
|
|
|
|
|
2020-06-04 06:16:53 +03:00
|
|
|
public get tileHeight(): number {
|
|
|
|
return TILE_HEIGHT_PX;
|
|
|
|
}
|
|
|
|
|
|
|
|
private get key(): string {
|
|
|
|
return `mx_sublist_layout_${this.tagId}_boxed`;
|
|
|
|
}
|
|
|
|
|
|
|
|
public get visibleTiles(): number {
|
|
|
|
return Math.max(this._n, this.minVisibleTiles);
|
|
|
|
}
|
|
|
|
|
|
|
|
public set visibleTiles(v: number) {
|
|
|
|
this._n = v;
|
2020-06-10 06:12:49 +03:00
|
|
|
this.save();
|
2020-06-04 06:16:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public get minVisibleTiles(): number {
|
2020-06-10 04:48:31 +03:00
|
|
|
// the .65 comes from the CSS where the show more button is
|
|
|
|
// mathematically 65% of a tile when floating.
|
|
|
|
return 4.65;
|
|
|
|
}
|
|
|
|
|
|
|
|
public calculateTilesToPixelsMin(maxTiles: number, n: number, possiblePadding: number): number {
|
|
|
|
// Only apply the padding if we're about to use maxTiles as we need to
|
|
|
|
// plan for the padding. If we're using n, the padding is already accounted
|
|
|
|
// for by the resizing stuff.
|
|
|
|
let padding = 0;
|
|
|
|
if (maxTiles < n) {
|
|
|
|
padding = possiblePadding;
|
|
|
|
}
|
|
|
|
return this.tilesToPixels(Math.min(maxTiles, n)) + padding;
|
|
|
|
}
|
|
|
|
|
|
|
|
public tilesToPixelsWithPadding(n: number, padding: number): number {
|
|
|
|
return this.tilesToPixels(n) + padding;
|
2020-06-04 06:16:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public tilesToPixels(n: number): number {
|
|
|
|
return n * this.tileHeight;
|
|
|
|
}
|
|
|
|
|
2020-06-04 18:19:03 +03:00
|
|
|
public pixelsToTiles(px: number): number {
|
|
|
|
return px / this.tileHeight;
|
|
|
|
}
|
|
|
|
|
2020-06-10 06:12:49 +03:00
|
|
|
private save() {
|
|
|
|
localStorage.setItem(this.key, JSON.stringify(this.serialize()));
|
|
|
|
}
|
|
|
|
|
2020-06-04 06:16:53 +03:00
|
|
|
private serialize(): ISerializedListLayout {
|
|
|
|
return {
|
|
|
|
numTiles: this.visibleTiles,
|
2020-06-10 06:12:49 +03:00
|
|
|
showPreviews: this.showPreviews,
|
2020-06-04 06:16:53 +03:00
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|