2020-08-05 18:38:55 +02:00
|
|
|
/*
|
|
|
|
Copyright 2020 Bruno Windels <bruno@windels.cloud>
|
2020-10-06 12:22:06 +02:00
|
|
|
Copyright 2020 The Matrix.org Foundation C.I.C.
|
2020-08-05 18:38:55 +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.
|
|
|
|
*/
|
|
|
|
|
2020-10-06 12:22:06 +02:00
|
|
|
import {LeftPanelViewModel} from "./leftpanel/LeftPanelViewModel.js";
|
2020-04-20 21:26:39 +02:00
|
|
|
import {RoomViewModel} from "./room/RoomViewModel.js";
|
2020-05-05 23:17:27 +02:00
|
|
|
import {SessionStatusViewModel} from "./SessionStatusViewModel.js";
|
2020-10-07 12:25:03 +02:00
|
|
|
import {RoomGridViewModel} from "./RoomGridViewModel.js";
|
2020-05-04 19:23:11 +02:00
|
|
|
import {ViewModel} from "../ViewModel.js";
|
2019-02-27 22:50:08 +01:00
|
|
|
|
2020-05-04 19:23:11 +02:00
|
|
|
export class SessionViewModel extends ViewModel {
|
|
|
|
constructor(options) {
|
|
|
|
super(options);
|
2020-05-05 23:17:27 +02:00
|
|
|
const {sessionContainer} = options;
|
2020-10-09 17:01:22 +02:00
|
|
|
this._sessionContainer = this.track(sessionContainer);
|
2020-05-05 23:16:51 +02:00
|
|
|
this._sessionStatusViewModel = this.track(new SessionStatusViewModel(this.childOptions({
|
2020-05-07 00:05:21 +02:00
|
|
|
sync: sessionContainer.sync,
|
2020-09-17 18:00:00 +02:00
|
|
|
reconnector: sessionContainer.reconnector,
|
|
|
|
session: sessionContainer.session,
|
2020-05-05 23:16:51 +02:00
|
|
|
})));
|
2020-10-06 12:22:06 +02:00
|
|
|
this._leftPanelViewModel = new LeftPanelViewModel(this.childOptions({
|
2020-10-12 17:49:06 +02:00
|
|
|
rooms: this._sessionContainer.session.rooms
|
2020-10-06 12:22:06 +02:00
|
|
|
}));
|
2019-02-27 22:50:08 +01:00
|
|
|
this._currentRoomViewModel = null;
|
2020-10-07 12:25:03 +02:00
|
|
|
this._gridViewModel = null;
|
2020-10-12 17:49:06 +02:00
|
|
|
this._setupNavigation();
|
|
|
|
}
|
2020-10-09 19:43:11 +02:00
|
|
|
|
2020-10-12 17:49:06 +02:00
|
|
|
_setupNavigation() {
|
|
|
|
const gridRooms = this.navigation.observe("rooms");
|
2020-10-09 19:43:11 +02:00
|
|
|
// this gives us a set of room ids in the grid
|
2020-10-12 17:49:06 +02:00
|
|
|
this.track(gridRooms.subscribe(roomIds => {
|
|
|
|
this._updateGrid(roomIds);
|
|
|
|
}));
|
|
|
|
if (gridRooms.get()) {
|
|
|
|
this._updateGrid(gridRooms.get());
|
|
|
|
}
|
|
|
|
|
|
|
|
const currentRoomId = this.navigation.observe("room");
|
|
|
|
// this gives us the active room
|
|
|
|
this.track(currentRoomId.subscribe(roomId => {
|
|
|
|
if (!this._gridViewModel) {
|
|
|
|
this._openRoom(roomId);
|
2020-10-09 19:43:11 +02:00
|
|
|
}
|
|
|
|
}));
|
2020-10-12 17:49:06 +02:00
|
|
|
if (currentRoomId.get() && !this._gridViewModel) {
|
|
|
|
this._openRoom(currentRoomId.get());
|
|
|
|
}
|
2019-02-27 22:50:08 +01:00
|
|
|
}
|
|
|
|
|
2020-10-13 14:43:31 +02:00
|
|
|
get id() {
|
|
|
|
return this._sessionContainer.sessionId;
|
|
|
|
}
|
|
|
|
|
2020-05-05 23:16:51 +02:00
|
|
|
start() {
|
|
|
|
this._sessionStatusViewModel.start();
|
|
|
|
}
|
|
|
|
|
2020-10-09 19:43:11 +02:00
|
|
|
get activeSection() {
|
2020-10-07 12:25:03 +02:00
|
|
|
if (this._currentRoomViewModel) {
|
2020-10-07 14:36:08 +02:00
|
|
|
return this._currentRoomViewModel.id;
|
2020-10-07 12:25:03 +02:00
|
|
|
} else if (this._gridViewModel) {
|
|
|
|
return "roomgrid";
|
|
|
|
}
|
|
|
|
return "placeholder";
|
|
|
|
}
|
|
|
|
|
|
|
|
get roomGridViewModel() {
|
|
|
|
return this._gridViewModel;
|
|
|
|
}
|
|
|
|
|
2020-10-06 12:22:06 +02:00
|
|
|
get leftPanelViewModel() {
|
|
|
|
return this._leftPanelViewModel;
|
|
|
|
}
|
|
|
|
|
2020-05-05 23:16:51 +02:00
|
|
|
get sessionStatusViewModel() {
|
|
|
|
return this._sessionStatusViewModel;
|
2019-06-16 10:54:16 +02:00
|
|
|
}
|
|
|
|
|
2019-02-27 22:50:08 +01:00
|
|
|
get roomList() {
|
|
|
|
return this._roomList;
|
|
|
|
}
|
|
|
|
|
2020-10-09 19:43:11 +02:00
|
|
|
get currentRoomViewModel() {
|
2019-02-27 22:50:08 +01:00
|
|
|
return this._currentRoomViewModel;
|
|
|
|
}
|
|
|
|
|
2020-10-12 17:49:06 +02:00
|
|
|
_updateGrid(roomIds) {
|
|
|
|
const changed = !(this._gridViewModel && roomIds);
|
|
|
|
const currentRoomId = this.navigation.path.get("room");
|
|
|
|
if (roomIds) {
|
|
|
|
if (!this._gridViewModel) {
|
|
|
|
this._gridViewModel = this.track(new RoomGridViewModel(this.childOptions({
|
|
|
|
width: 3,
|
|
|
|
height: 2,
|
|
|
|
createRoomViewModel: roomId => this._createRoomViewModel(roomId),
|
|
|
|
})));
|
2020-10-13 13:11:19 +02:00
|
|
|
if (this._gridViewModel.initializeRoomIdsAndTransferVM(roomIds, this._currentRoomViewModel)) {
|
|
|
|
this._currentRoomViewModel = this.untrack(this._currentRoomViewModel);
|
|
|
|
} else if (this._currentRoomViewModel) {
|
|
|
|
this._currentRoomViewModel = this.disposeTracked(this._currentRoomViewModel);
|
2020-10-12 17:49:06 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
this._gridViewModel.setRoomIds(roomIds);
|
2020-10-07 12:25:03 +02:00
|
|
|
}
|
2020-10-12 17:49:06 +02:00
|
|
|
} else if (this._gridViewModel && !roomIds) {
|
|
|
|
if (currentRoomId) {
|
|
|
|
const vm = this._gridViewModel.releaseRoomViewModel(currentRoomId.value);
|
2020-10-13 13:12:30 +02:00
|
|
|
if (vm) {
|
|
|
|
this._currentRoomViewModel = this.track(vm);
|
|
|
|
} else {
|
|
|
|
this._currentRoomViewModel = this.track(this._createRoomViewModel(currentRoomId.value));
|
|
|
|
}
|
2020-10-07 12:25:03 +02:00
|
|
|
}
|
|
|
|
this._gridViewModel = this.disposeTracked(this._gridViewModel);
|
|
|
|
}
|
2020-10-12 17:49:06 +02:00
|
|
|
if (changed) {
|
|
|
|
this.emitChange("middlePanelViewType");
|
|
|
|
}
|
2020-10-07 12:25:03 +02:00
|
|
|
}
|
|
|
|
|
2020-10-12 17:49:06 +02:00
|
|
|
_createRoomViewModel(roomId) {
|
2020-10-12 18:31:55 +02:00
|
|
|
const room = this._sessionContainer.session.rooms.get(roomId);
|
2020-10-09 19:43:11 +02:00
|
|
|
if (!room) {
|
2020-10-12 17:49:06 +02:00
|
|
|
return null;
|
2020-10-07 12:25:03 +02:00
|
|
|
}
|
|
|
|
const roomVM = new RoomViewModel(this.childOptions({
|
2019-06-26 23:14:39 +02:00
|
|
|
room,
|
2020-10-09 17:01:22 +02:00
|
|
|
ownUserId: this._sessionContainer.session.user.id,
|
2020-10-07 12:25:03 +02:00
|
|
|
}));
|
|
|
|
roomVM.load();
|
2020-10-12 17:49:06 +02:00
|
|
|
return roomVM;
|
|
|
|
}
|
|
|
|
|
|
|
|
_openRoom(roomId) {
|
2020-10-13 13:12:49 +02:00
|
|
|
if (!roomId) {
|
|
|
|
if (this._currentRoomViewModel) {
|
|
|
|
this._currentRoomViewModel = this.disposeTracked(this._currentRoomViewModel);
|
|
|
|
this.emitChange("currentRoom");
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2020-10-12 17:49:06 +02:00
|
|
|
// already open?
|
|
|
|
if (this._currentRoomViewModel?.id === roomId) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this._currentRoomViewModel = this.disposeTracked(this._currentRoomViewModel);
|
|
|
|
const roomVM = this._createRoomViewModel(roomId);
|
|
|
|
if (roomVM) {
|
|
|
|
this._currentRoomViewModel = this.track(roomVM);
|
2020-10-07 12:25:03 +02:00
|
|
|
this.emitChange("currentRoom");
|
|
|
|
}
|
2019-02-27 22:50:08 +01:00
|
|
|
}
|
|
|
|
}
|