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-04-22 20:52:56 +02:00
|
|
|
this._session = sessionContainer.session;
|
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({
|
|
|
|
rooms: this._session.rooms,
|
2020-10-07 12:25:03 +02:00
|
|
|
openRoom: this._openRoom.bind(this),
|
|
|
|
gridEnabled: {
|
|
|
|
get: () => !!this._gridViewModel,
|
|
|
|
set: value => this._enabledGrid(value)
|
|
|
|
}
|
2020-10-06 12:22:06 +02:00
|
|
|
}));
|
2020-08-12 17:40:41 +02:00
|
|
|
this._currentRoomTileViewModel = null;
|
2019-02-27 22:50:08 +01:00
|
|
|
this._currentRoomViewModel = null;
|
2020-10-07 12:25:03 +02:00
|
|
|
this._gridViewModel = null;
|
2019-02-27 22:50:08 +01:00
|
|
|
}
|
|
|
|
|
2020-05-05 23:16:51 +02:00
|
|
|
start() {
|
|
|
|
this._sessionStatusViewModel.start();
|
|
|
|
}
|
|
|
|
|
2020-10-07 12:25:03 +02:00
|
|
|
get middlePanelViewType() {
|
|
|
|
if (this._currentRoomViewModel) {
|
|
|
|
return "room";
|
|
|
|
} 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
get currentRoom() {
|
|
|
|
return this._currentRoomViewModel;
|
|
|
|
}
|
|
|
|
|
2020-10-07 12:25:03 +02:00
|
|
|
_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);
|
|
|
|
this._gridViewModel.setRoomViewModel(this._currentRoomViewModel, this._currentRoomTileViewModel);
|
|
|
|
this._currentRoomViewModel = null;
|
|
|
|
this._currentRoomTileViewModel = null;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
const VMs = this._gridViewModel.getAndUntrackFirst();
|
|
|
|
if (VMs) {
|
|
|
|
this._currentRoomViewModel = this.track(VMs.vm);
|
|
|
|
this._currentRoomTileViewModel = VMs.tileVM;
|
|
|
|
this._currentRoomTileViewModel.open();
|
|
|
|
}
|
|
|
|
this._gridViewModel = this.disposeTracked(this._gridViewModel);
|
|
|
|
}
|
|
|
|
this.emitChange("middlePanelViewType");
|
|
|
|
}
|
|
|
|
|
2019-06-26 23:14:39 +02:00
|
|
|
_closeCurrentRoom() {
|
2020-10-07 14:25:32 +02:00
|
|
|
// no closing in grid for now as it is disabled on narrow viewports
|
2020-10-07 12:25:03 +02:00
|
|
|
if (!this._gridViewModel) {
|
|
|
|
this._currentRoomTileViewModel?.close();
|
|
|
|
this._currentRoomViewModel = this.disposeTracked(this._currentRoomViewModel);
|
|
|
|
return true;
|
|
|
|
}
|
2019-06-26 23:14:39 +02:00
|
|
|
}
|
|
|
|
|
2020-08-12 17:40:41 +02:00
|
|
|
_openRoom(room, roomTileVM) {
|
2020-10-07 14:18:35 +02:00
|
|
|
if (this._gridViewModel?.tryFocusRoom(room.id)) {
|
|
|
|
return;
|
2020-10-07 12:25:03 +02:00
|
|
|
} else if (this._currentRoomViewModel?._room.id === room.id) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const roomVM = new RoomViewModel(this.childOptions({
|
2019-06-26 23:14:39 +02:00
|
|
|
room,
|
2019-07-29 10:23:15 +02:00
|
|
|
ownUserId: this._session.user.id,
|
2020-08-27 10:07:47 +02:00
|
|
|
closeCallback: () => {
|
2020-10-07 12:25:03 +02:00
|
|
|
if (this._closeCurrentRoom()) {
|
|
|
|
this.emitChange("currentRoom");
|
|
|
|
}
|
2020-08-27 10:07:47 +02:00
|
|
|
},
|
2020-10-07 12:25:03 +02:00
|
|
|
}));
|
|
|
|
roomVM.load();
|
|
|
|
if (this._gridViewModel) {
|
|
|
|
this._gridViewModel.setRoomViewModel(roomVM, roomTileVM);
|
|
|
|
} else {
|
|
|
|
this._closeCurrentRoom();
|
|
|
|
this._currentRoomTileViewModel = roomTileVM;
|
|
|
|
this._currentRoomViewModel = this.track(roomVM);
|
|
|
|
this.emitChange("currentRoom");
|
|
|
|
}
|
2019-02-27 22:50:08 +01:00
|
|
|
}
|
|
|
|
}
|