This repository has been archived on 2022-08-19. You can view files and clone it, but cannot push or open issues or pull requests.
hydrogen-web/src/domain/session/SessionViewModel.js

142 lines
4.8 KiB
JavaScript
Raw Normal View History

2020-08-05 22:08:55 +05:30
/*
Copyright 2020 Bruno Windels <bruno@windels.cloud>
Copyright 2020 The Matrix.org Foundation C.I.C.
2020-08-05 22:08:55 +05:30
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.
*/
import {LeftPanelViewModel} from "./leftpanel/LeftPanelViewModel.js";
import {RoomViewModel} from "./room/RoomViewModel.js";
import {SessionStatusViewModel} from "./SessionStatusViewModel.js";
import {RoomGridViewModel} from "./RoomGridViewModel.js";
import {ViewModel} from "../ViewModel.js";
export class SessionViewModel extends ViewModel {
constructor(options) {
super(options);
const {sessionContainer} = options;
this._sessionContainer = this.track(sessionContainer);
this._sessionStatusViewModel = this.track(new SessionStatusViewModel(this.childOptions({
2020-05-07 03:35:21 +05:30
sync: sessionContainer.sync,
2020-09-17 21:30:00 +05:30
reconnector: sessionContainer.reconnector,
session: sessionContainer.session,
})));
this._leftPanelViewModel = new LeftPanelViewModel(this.childOptions({
rooms: this._sessionContainer.session.rooms,
2020-10-09 23:13:11 +05:30
// this will go over navigation as well
gridEnabled: {
get: () => !!this._gridViewModel,
set: value => this._enabledGrid(value)
}
}));
this._currentRoomViewModel = null;
this._gridViewModel = null;
2020-10-09 23:13:11 +05:30
// this gives us the active room, also in the grid?
this.track(this.navigator.observe("room").subscribe(roomId => {
}));
// this gives us a set of room ids in the grid
this.track(this.navigator.observe("rooms").subscribe(value => {
if (value) {
const roomIds = typeof value === "string" ? value.split(",") : [];
// also update grid
this._enabledGrid(roomIds);
}
}));
}
start() {
this._sessionStatusViewModel.start();
}
2020-10-09 23:13:11 +05:30
get activeSection() {
if (this._currentRoomViewModel) {
return this._currentRoomViewModel.id;
} else if (this._gridViewModel) {
return "roomgrid";
}
return "placeholder";
}
get roomGridViewModel() {
return this._gridViewModel;
}
get leftPanelViewModel() {
return this._leftPanelViewModel;
}
get sessionStatusViewModel() {
return this._sessionStatusViewModel;
}
get roomList() {
return this._roomList;
}
2020-10-09 23:13:11 +05:30
get currentRoomViewModel() {
return this._currentRoomViewModel;
}
2020-10-09 23:13:11 +05:30
// TODO: this should also happen based on URLs
_enabledGrid(enabled) {
if (enabled) {
this._gridViewModel = this.track(new RoomGridViewModel(this.childOptions({width: 3, height: 2})));
// transfer current room
if (this._currentRoomViewModel) {
this.untrack(this._currentRoomViewModel);
2020-10-09 23:13:11 +05:30
this._gridViewModel.setRoomViewModel(this._currentRoomViewModel);
this._currentRoomViewModel = null;
}
} else {
2020-10-09 23:13:11 +05:30
const vm = this._gridViewModel.getAndUntrackFirst();
if (vm) {
this._currentRoomViewModel = this.track(vm);
}
this._gridViewModel = this.disposeTracked(this._gridViewModel);
}
this.emitChange("middlePanelViewType");
}
2020-10-09 23:13:11 +05:30
_openRoom(roomId) {
// already open?
if (this._gridViewModel?.tryFocusRoom(roomId)) {
return;
} else if (this._currentRoomViewModel?.id === roomId) {
return;
2020-10-09 23:13:11 +05:30
}
const room = this._session.rooms.get(roomId);
// not found? close current room and show placeholder
if (!room) {
if (this._gridViewModel) {
this._gridViewModel.setRoomViewModel(null);
} else {
this._currentRoomViewModel = this.disposeTracked(this._currentRoomViewModel);
}
return;
}
const roomVM = new RoomViewModel(this.childOptions({
room,
ownUserId: this._sessionContainer.session.user.id,
}));
roomVM.load();
if (this._gridViewModel) {
2020-10-09 23:13:11 +05:30
this._gridViewModel.setRoomViewModel(roomVM);
} else {
2020-10-09 23:13:11 +05:30
this._currentRoomViewModel = this.disposeTracked(this._currentRoomViewModel);
this._currentRoomViewModel = this.track(roomVM);
this.emitChange("currentRoom");
}
}
}