2020-10-07 12:23:02 +02: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.
|
|
|
|
*/
|
|
|
|
|
2021-06-03 17:33:02 +02:00
|
|
|
import {ViewModel} from "../ViewModel.js";
|
2021-07-13 09:35:54 +02:00
|
|
|
import {addPanelIfNeeded} from "../navigation/index.js";
|
2020-10-07 12:23:02 +02:00
|
|
|
|
2020-10-13 13:11:19 +02:00
|
|
|
function dedupeSparse(roomIds) {
|
|
|
|
return roomIds.map((id, idx) => {
|
|
|
|
if (roomIds.slice(0, idx).includes(id)) {
|
|
|
|
return undefined;
|
|
|
|
} else {
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-10-07 12:23:02 +02:00
|
|
|
export class RoomGridViewModel extends ViewModel {
|
|
|
|
constructor(options) {
|
|
|
|
super(options);
|
2020-10-12 17:49:06 +02:00
|
|
|
|
2020-10-07 12:23:02 +02:00
|
|
|
this._width = options.width;
|
|
|
|
this._height = options.height;
|
2021-05-07 13:10:35 +02:00
|
|
|
this._createRoomViewModelObservable = options.createRoomViewModelObservable;
|
2020-10-07 12:23:02 +02:00
|
|
|
this._selectedIndex = 0;
|
2021-05-07 13:10:35 +02:00
|
|
|
this._viewModelsObservables = [];
|
2020-10-12 17:49:06 +02:00
|
|
|
this._setupNavigation();
|
|
|
|
}
|
|
|
|
|
|
|
|
_setupNavigation() {
|
|
|
|
const focusTileIndex = this.navigation.observe("empty-grid-tile");
|
|
|
|
this.track(focusTileIndex.subscribe(index => {
|
|
|
|
if (typeof index === "number") {
|
|
|
|
this._setFocusIndex(index);
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
if (typeof focusTileIndex.get() === "number") {
|
|
|
|
this._selectedIndex = focusTileIndex.get();
|
|
|
|
}
|
|
|
|
|
2020-10-12 18:31:55 +02:00
|
|
|
const focusedRoom = this.navigation.observe("room");
|
2020-10-12 17:49:06 +02:00
|
|
|
this.track(focusedRoom.subscribe(roomId => {
|
|
|
|
if (roomId) {
|
2020-10-13 13:11:19 +02:00
|
|
|
// as the room will be in the "rooms" observable
|
2021-05-07 13:10:35 +02:00
|
|
|
// (monitored by the parent vmo) as well,
|
2020-10-13 13:11:19 +02:00
|
|
|
// we only change the focus here and trust
|
2021-05-07 13:10:35 +02:00
|
|
|
// setRoomIds to have created the vmo already
|
2020-10-13 13:11:19 +02:00
|
|
|
this._setFocusRoom(roomId);
|
2020-10-12 17:49:06 +02:00
|
|
|
}
|
|
|
|
}));
|
2020-10-13 13:11:19 +02:00
|
|
|
// initial focus for a room is set by initializeRoomIdsAndTransferVM
|
2020-10-07 12:23:02 +02:00
|
|
|
}
|
|
|
|
|
2020-10-08 14:17:43 +02:00
|
|
|
roomViewModelAt(i) {
|
2021-05-07 13:10:35 +02:00
|
|
|
return this._viewModelsObservables[i]?.get();
|
2020-10-07 12:23:02 +02:00
|
|
|
}
|
|
|
|
|
2020-10-08 14:17:43 +02:00
|
|
|
get focusIndex() {
|
|
|
|
return this._selectedIndex;
|
2020-10-07 12:23:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
get width() {
|
|
|
|
return this._width;
|
|
|
|
}
|
|
|
|
|
|
|
|
get height() {
|
|
|
|
return this._height;
|
|
|
|
}
|
|
|
|
|
2021-06-15 11:04:26 +02:00
|
|
|
_switchToRoom(roomId) {
|
|
|
|
let path = this.navigation.path.until("rooms");
|
2021-06-24 10:18:53 +02:00
|
|
|
path = path.with(this.navigation.segment("room", roomId));
|
2021-07-13 09:35:54 +02:00
|
|
|
path = addPanelIfNeeded(this.navigation, "details", path);
|
|
|
|
path = addPanelIfNeeded(this.navigation, "members", path);
|
2021-06-15 11:04:26 +02:00
|
|
|
this.navigation.applyPath(path);
|
|
|
|
}
|
|
|
|
|
2020-10-12 17:49:06 +02:00
|
|
|
focusTile(index) {
|
|
|
|
if (index === this._selectedIndex) {
|
|
|
|
return;
|
|
|
|
}
|
2021-05-07 13:10:35 +02:00
|
|
|
const vmo = this._viewModelsObservables[index];
|
|
|
|
if (vmo) {
|
2021-06-15 11:04:26 +02:00
|
|
|
this._switchToRoom(vmo.id);
|
2020-10-12 17:49:06 +02:00
|
|
|
} else {
|
2020-10-16 12:46:14 +02:00
|
|
|
this.navigation.push("empty-grid-tile", index);
|
2020-10-12 17:49:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-13 13:11:19 +02:00
|
|
|
/** called from SessionViewModel */
|
|
|
|
initializeRoomIdsAndTransferVM(roomIds, existingRoomVM) {
|
|
|
|
roomIds = dedupeSparse(roomIds);
|
|
|
|
let transfered = false;
|
|
|
|
if (existingRoomVM) {
|
|
|
|
const index = roomIds.indexOf(existingRoomVM.id);
|
|
|
|
if (index !== -1) {
|
2021-05-07 13:10:35 +02:00
|
|
|
this._viewModelsObservables[index] = this.track(existingRoomVM);
|
|
|
|
existingRoomVM.subscribe(viewModel => this._refreshRoomViewModel(viewModel));
|
2020-10-13 13:11:19 +02:00
|
|
|
transfered = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this.setRoomIds(roomIds);
|
|
|
|
// now all view models exist, set the focus to the selected room
|
|
|
|
const focusedRoom = this.navigation.path.get("room");
|
|
|
|
if (focusedRoom) {
|
2021-05-07 13:10:35 +02:00
|
|
|
const index = this._viewModelsObservables.findIndex(vmo => vmo && vmo.id === focusedRoom.value);
|
2020-10-13 13:11:19 +02:00
|
|
|
if (index !== -1) {
|
|
|
|
this._selectedIndex = index;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return transfered;
|
|
|
|
}
|
|
|
|
|
2020-10-12 17:49:06 +02:00
|
|
|
/** called from SessionViewModel */
|
|
|
|
setRoomIds(roomIds) {
|
2020-10-13 13:11:19 +02:00
|
|
|
roomIds = dedupeSparse(roomIds);
|
2020-10-12 17:49:06 +02:00
|
|
|
let changed = false;
|
|
|
|
const len = this._height * this._width;
|
|
|
|
for (let i = 0; i < len; i += 1) {
|
|
|
|
const newId = roomIds[i];
|
2021-05-07 13:10:35 +02:00
|
|
|
const vmo = this._viewModelsObservables[i];
|
2020-10-13 13:11:19 +02:00
|
|
|
// did anything change?
|
2021-05-07 13:10:35 +02:00
|
|
|
if ((!vmo && newId) || (vmo && vmo.id !== newId)) {
|
|
|
|
if (vmo) {
|
|
|
|
this._viewModelsObservables[i] = this.disposeTracked(vmo);
|
2020-10-13 13:11:19 +02:00
|
|
|
}
|
2020-10-12 17:49:06 +02:00
|
|
|
if (newId) {
|
2021-05-07 13:10:35 +02:00
|
|
|
const vmo = this._createRoomViewModelObservable(newId);
|
|
|
|
this._viewModelsObservables[i] = this.track(vmo);
|
|
|
|
vmo.subscribe(viewModel => this._refreshRoomViewModel(viewModel));
|
|
|
|
vmo.initialize();
|
2020-10-12 17:49:06 +02:00
|
|
|
}
|
|
|
|
changed = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (changed) {
|
|
|
|
this.emitChange();
|
|
|
|
}
|
2020-10-13 13:11:19 +02:00
|
|
|
return changed;
|
2020-10-12 17:49:06 +02:00
|
|
|
}
|
2021-05-07 13:10:35 +02:00
|
|
|
|
|
|
|
_refreshRoomViewModel(viewModel) {
|
|
|
|
this.emitChange();
|
|
|
|
viewModel?.focus();
|
|
|
|
}
|
2021-05-31 18:44:43 +02:00
|
|
|
|
2020-10-12 17:49:06 +02:00
|
|
|
/** called from SessionViewModel */
|
|
|
|
releaseRoomViewModel(roomId) {
|
2021-05-07 13:10:35 +02:00
|
|
|
const index = this._viewModelsObservables.findIndex(vmo => vmo && vmo.id === roomId);
|
2020-10-12 17:49:06 +02:00
|
|
|
if (index !== -1) {
|
2021-05-07 13:10:35 +02:00
|
|
|
const vmo = this._viewModelsObservables[index];
|
|
|
|
this.untrack(vmo);
|
|
|
|
vmo.unsubscribeAll();
|
|
|
|
this._viewModelsObservables[index] = null;
|
|
|
|
return vmo;
|
2020-10-12 17:49:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_setFocusIndex(idx) {
|
|
|
|
if (idx === this._selectedIndex || idx >= (this._width * this._height)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this._selectedIndex = idx;
|
2021-05-07 13:10:35 +02:00
|
|
|
const vmo = this._viewModelsObservables[this._selectedIndex];
|
|
|
|
vmo?.get()?.focus();
|
2020-10-13 13:11:19 +02:00
|
|
|
this.emitChange("focusIndex");
|
2020-10-12 17:49:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
_setFocusRoom(roomId) {
|
2021-05-07 13:10:35 +02:00
|
|
|
const index = this._viewModelsObservables.findIndex(vmo => vmo?.id === roomId);
|
2020-10-07 14:18:35 +02:00
|
|
|
if (index >= 0) {
|
2020-10-12 17:49:06 +02:00
|
|
|
this._setFocusIndex(index);
|
2020-10-07 14:18:35 +02:00
|
|
|
}
|
2020-10-07 12:23:02 +02:00
|
|
|
}
|
2020-10-13 13:11:19 +02:00
|
|
|
}
|
2020-10-12 17:49:06 +02:00
|
|
|
|
2021-06-03 17:33:02 +02:00
|
|
|
import {createNavigation} from "../navigation/index.js";
|
|
|
|
import {ObservableValue} from "../../observable/ObservableValue.js";
|
2021-05-07 13:10:35 +02:00
|
|
|
|
2021-06-03 17:33:02 +02:00
|
|
|
export function tests() {
|
2020-10-13 13:11:19 +02:00
|
|
|
class RoomVMMock {
|
|
|
|
constructor(id) {
|
|
|
|
this.id = id;
|
|
|
|
this.disposed = false;
|
|
|
|
this.focused = false;
|
|
|
|
}
|
|
|
|
dispose() {
|
|
|
|
this.disposed = true;
|
|
|
|
}
|
|
|
|
focus() {
|
|
|
|
this.focused = true;
|
2020-10-07 12:23:02 +02:00
|
|
|
}
|
|
|
|
}
|
2020-10-13 13:11:19 +02:00
|
|
|
|
2021-05-07 13:10:35 +02:00
|
|
|
class RoomViewModelObservableMock extends ObservableValue {
|
2021-06-03 17:33:02 +02:00
|
|
|
async initialize() {}
|
2021-05-07 13:10:35 +02:00
|
|
|
dispose() { this.get()?.dispose(); }
|
|
|
|
get id() { return this.get()?.id; }
|
|
|
|
}
|
|
|
|
|
2020-10-13 13:11:19 +02:00
|
|
|
function createNavigationForRoom(rooms, room) {
|
|
|
|
const navigation = createNavigation();
|
|
|
|
navigation.applyPath(navigation.pathFrom([
|
|
|
|
navigation.segment("session", "1"),
|
|
|
|
navigation.segment("rooms", rooms),
|
|
|
|
navigation.segment("room", room),
|
|
|
|
]));
|
|
|
|
return navigation;
|
|
|
|
}
|
|
|
|
|
|
|
|
function createNavigationForEmptyTile(rooms, idx) {
|
|
|
|
const navigation = createNavigation();
|
|
|
|
navigation.applyPath(navigation.pathFrom([
|
|
|
|
navigation.segment("session", "1"),
|
|
|
|
navigation.segment("rooms", rooms),
|
|
|
|
navigation.segment("empty-grid-tile", idx),
|
|
|
|
]));
|
|
|
|
return navigation;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
"initialize with duplicate set of rooms": assert => {
|
|
|
|
const navigation = createNavigationForRoom(["c", "a", "b", undefined, "a"], "a");
|
|
|
|
const gridVM = new RoomGridViewModel({
|
2021-05-07 13:10:35 +02:00
|
|
|
createRoomViewModelObservable: id => new RoomViewModelObservableMock(new RoomVMMock(id)),
|
2020-10-13 13:11:19 +02:00
|
|
|
navigation,
|
|
|
|
width: 3,
|
|
|
|
height: 2,
|
|
|
|
});
|
|
|
|
gridVM.initializeRoomIdsAndTransferVM(navigation.path.get("rooms").value);
|
|
|
|
assert.equal(gridVM.focusIndex, 1);
|
|
|
|
assert.equal(gridVM.roomViewModelAt(0).id, "c");
|
|
|
|
assert.equal(gridVM.roomViewModelAt(1).id, "a");
|
|
|
|
assert.equal(gridVM.roomViewModelAt(2).id, "b");
|
|
|
|
assert.equal(gridVM.roomViewModelAt(3), undefined);
|
|
|
|
assert.equal(gridVM.roomViewModelAt(4), undefined);
|
|
|
|
assert.equal(gridVM.roomViewModelAt(5), undefined);
|
|
|
|
},
|
|
|
|
"transfer room view model": assert => {
|
|
|
|
const navigation = createNavigationForRoom(["a"], "a");
|
|
|
|
const gridVM = new RoomGridViewModel({
|
2021-05-07 13:10:35 +02:00
|
|
|
createRoomViewModelObservable: () => assert.fail("no vms should be created"),
|
2020-10-13 13:11:19 +02:00
|
|
|
navigation,
|
|
|
|
width: 3,
|
|
|
|
height: 2,
|
|
|
|
});
|
2021-05-07 13:10:35 +02:00
|
|
|
const existingRoomVM = new RoomViewModelObservableMock(new RoomVMMock("a"));
|
2020-10-13 13:11:19 +02:00
|
|
|
const transfered = gridVM.initializeRoomIdsAndTransferVM(navigation.path.get("rooms").value, existingRoomVM);
|
|
|
|
assert.equal(transfered, true);
|
|
|
|
assert.equal(gridVM.focusIndex, 0);
|
|
|
|
assert.equal(gridVM.roomViewModelAt(0).id, "a");
|
|
|
|
},
|
|
|
|
"reject transfer for non-matching room view model": assert => {
|
|
|
|
const navigation = createNavigationForRoom(["a"], "a");
|
|
|
|
const gridVM = new RoomGridViewModel({
|
2021-05-07 13:10:35 +02:00
|
|
|
createRoomViewModelObservable: id => new RoomViewModelObservableMock(new RoomVMMock(id)),
|
2020-10-13 13:11:19 +02:00
|
|
|
navigation,
|
|
|
|
width: 3,
|
|
|
|
height: 2,
|
|
|
|
});
|
2021-05-07 13:10:35 +02:00
|
|
|
const existingRoomVM = new RoomViewModelObservableMock(new RoomVMMock("f"));
|
2020-10-13 13:11:19 +02:00
|
|
|
const transfered = gridVM.initializeRoomIdsAndTransferVM(navigation.path.get("rooms").value, existingRoomVM);
|
|
|
|
assert.equal(transfered, false);
|
|
|
|
assert.equal(gridVM.focusIndex, 0);
|
|
|
|
assert.equal(gridVM.roomViewModelAt(0).id, "a");
|
|
|
|
},
|
|
|
|
"created & released room view model is not disposed": assert => {
|
|
|
|
const navigation = createNavigationForRoom(["a"], "a");
|
|
|
|
const gridVM = new RoomGridViewModel({
|
2021-05-07 13:10:35 +02:00
|
|
|
createRoomViewModelObservable: id => new RoomViewModelObservableMock(new RoomVMMock(id)),
|
2020-10-13 13:11:19 +02:00
|
|
|
navigation,
|
|
|
|
width: 3,
|
|
|
|
height: 2,
|
|
|
|
});
|
|
|
|
const transfered = gridVM.initializeRoomIdsAndTransferVM(navigation.path.get("rooms").value);
|
|
|
|
assert.equal(transfered, false);
|
|
|
|
const releasedVM = gridVM.releaseRoomViewModel("a");
|
|
|
|
gridVM.dispose();
|
2021-05-07 13:10:35 +02:00
|
|
|
assert.equal(releasedVM.get().disposed, false);
|
2020-10-13 13:11:19 +02:00
|
|
|
},
|
|
|
|
"transfered & released room view model is not disposed": assert => {
|
|
|
|
const navigation = createNavigationForRoom([undefined, "a"], "a");
|
|
|
|
const gridVM = new RoomGridViewModel({
|
2021-05-07 13:10:35 +02:00
|
|
|
createRoomViewModelObservable: () => assert.fail("no vms should be created"),
|
2020-10-13 13:11:19 +02:00
|
|
|
navigation,
|
|
|
|
width: 3,
|
|
|
|
height: 2,
|
|
|
|
});
|
2021-05-07 13:10:35 +02:00
|
|
|
const existingRoomVM = new RoomViewModelObservableMock(new RoomVMMock("a"));
|
2020-10-13 13:11:19 +02:00
|
|
|
const transfered = gridVM.initializeRoomIdsAndTransferVM(navigation.path.get("rooms").value, existingRoomVM);
|
|
|
|
assert.equal(transfered, true);
|
|
|
|
const releasedVM = gridVM.releaseRoomViewModel("a");
|
|
|
|
gridVM.dispose();
|
2021-05-07 13:10:35 +02:00
|
|
|
assert.equal(releasedVM.get().disposed, false);
|
2020-10-13 13:11:19 +02:00
|
|
|
},
|
|
|
|
"try release non-existing room view model is": assert => {
|
|
|
|
const navigation = createNavigationForEmptyTile([undefined, "b"], 3);
|
|
|
|
const gridVM = new RoomGridViewModel({
|
2021-05-07 13:10:35 +02:00
|
|
|
createRoomViewModelObservable: id => new RoomViewModelObservableMock(new RoomVMMock(id)),
|
2020-10-13 13:11:19 +02:00
|
|
|
navigation,
|
|
|
|
width: 3,
|
|
|
|
height: 2,
|
|
|
|
});
|
|
|
|
gridVM.initializeRoomIdsAndTransferVM(navigation.path.get("rooms").value);
|
|
|
|
const releasedVM = gridVM.releaseRoomViewModel("c");
|
|
|
|
assert(!releasedVM);
|
|
|
|
},
|
|
|
|
"initial focus is set to empty tile": assert => {
|
|
|
|
const navigation = createNavigationForEmptyTile(["a"], 1);
|
|
|
|
const gridVM = new RoomGridViewModel({
|
2021-05-07 13:10:35 +02:00
|
|
|
createRoomViewModelObservable: id => new RoomViewModelObservableMock(new RoomVMMock(id)),
|
2020-10-13 13:11:19 +02:00
|
|
|
navigation,
|
|
|
|
width: 3,
|
|
|
|
height: 2,
|
|
|
|
});
|
|
|
|
gridVM.initializeRoomIdsAndTransferVM(navigation.path.get("rooms").value);
|
|
|
|
assert.equal(gridVM.focusIndex, 1);
|
|
|
|
assert.equal(gridVM.roomViewModelAt(0).id, "a");
|
|
|
|
},
|
|
|
|
"change room ids after creation": assert => {
|
|
|
|
const navigation = createNavigationForRoom(["a", "b"], "a");
|
|
|
|
const gridVM = new RoomGridViewModel({
|
2021-05-07 13:10:35 +02:00
|
|
|
createRoomViewModelObservable: id => new RoomViewModelObservableMock(new RoomVMMock(id)),
|
2020-10-13 13:11:19 +02:00
|
|
|
navigation,
|
|
|
|
width: 3,
|
|
|
|
height: 2,
|
|
|
|
});
|
|
|
|
navigation.observe("rooms").subscribe(roomIds => {
|
|
|
|
gridVM.setRoomIds(roomIds);
|
|
|
|
});
|
|
|
|
gridVM.initializeRoomIdsAndTransferVM(navigation.path.get("rooms").value);
|
|
|
|
const oldA = gridVM.roomViewModelAt(0);
|
|
|
|
const oldB = gridVM.roomViewModelAt(1);
|
|
|
|
assert.equal(oldA.id, "a");
|
|
|
|
assert.equal(oldB.id, "b");
|
|
|
|
navigation.applyPath(navigation.path
|
|
|
|
.with(navigation.segment("rooms", ["b", "c", "b"]))
|
|
|
|
.with(navigation.segment("room", "c"))
|
|
|
|
);
|
|
|
|
assert.equal(oldA.disposed, true);
|
|
|
|
assert.equal(oldB.disposed, true);
|
|
|
|
assert.equal(gridVM.focusIndex, 1);
|
|
|
|
assert.equal(gridVM.roomViewModelAt(0).id, "b");
|
|
|
|
assert.equal(gridVM.roomViewModelAt(0).disposed, false);
|
|
|
|
assert.equal(gridVM.roomViewModelAt(1).id, "c");
|
|
|
|
assert.equal(gridVM.roomViewModelAt(1).focused, true);
|
|
|
|
assert.equal(gridVM.roomViewModelAt(2), undefined);
|
|
|
|
}
|
|
|
|
};
|
2020-10-07 12:23:02 +02:00
|
|
|
}
|