utility to remove room from path (with our without grid)
which will be used when rejecting an invite
This commit is contained in:
parent
ad5d7fc9f0
commit
9c19fa5c63
2 changed files with 114 additions and 1 deletions
|
@ -147,6 +147,22 @@ class Path {
|
||||||
return this._segments.find(s => s.type === type);
|
return this._segments.find(s => s.type === type);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
replace(segment) {
|
||||||
|
const index = this._segments.findIndex(s => s.type === segment.type);
|
||||||
|
if (index !== -1) {
|
||||||
|
const parent = this._segments[index - 1];
|
||||||
|
if (this._allowsChild(parent, segment)) {
|
||||||
|
const child = this._segments[index + 1];
|
||||||
|
if (!child || this._allowsChild(segment, child)) {
|
||||||
|
const newSegments = this._segments.slice();
|
||||||
|
newSegments[index] = segment;
|
||||||
|
return new Path(newSegments, this._allowsChild);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
get segments() {
|
get segments() {
|
||||||
return this._segments;
|
return this._segments;
|
||||||
}
|
}
|
||||||
|
|
|
@ -43,6 +43,30 @@ function allowsChild(parent, child) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function removeRoomFromPath(path, roomId) {
|
||||||
|
const rooms = path.get("rooms");
|
||||||
|
let roomIdGridIndex = -1;
|
||||||
|
// first delete from rooms segment
|
||||||
|
if (rooms) {
|
||||||
|
roomIdGridIndex = rooms.value.indexOf(roomId);
|
||||||
|
if (roomIdGridIndex !== -1) {
|
||||||
|
const idsWithoutRoom = rooms.value.slice();
|
||||||
|
idsWithoutRoom[roomIdGridIndex] = "";
|
||||||
|
path = path.replace(new Segment("rooms", idsWithoutRoom));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
const room = path.get("room");
|
||||||
|
// then from room (which occurs with or without rooms)
|
||||||
|
if (room && room.value === roomId) {
|
||||||
|
if (roomIdGridIndex !== -1) {
|
||||||
|
path = path.with(new Segment("empty-grid-tile", roomIdGridIndex));
|
||||||
|
} else {
|
||||||
|
path = path.until("session");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return path;
|
||||||
|
}
|
||||||
|
|
||||||
function roomsSegmentWithRoom(rooms, roomId, path) {
|
function roomsSegmentWithRoom(rooms, roomId, path) {
|
||||||
if(!rooms.value.includes(roomId)) {
|
if(!rooms.value.includes(roomId)) {
|
||||||
const emptyGridTile = path.get("empty-grid-tile");
|
const emptyGridTile = path.get("empty-grid-tile");
|
||||||
|
@ -243,6 +267,79 @@ export function tests() {
|
||||||
assert.equal(segments.length, 1);
|
assert.equal(segments.length, 1);
|
||||||
assert.equal(segments[0].type, "session");
|
assert.equal(segments[0].type, "session");
|
||||||
assert.strictEqual(segments[0].value, true);
|
assert.strictEqual(segments[0].value, true);
|
||||||
}
|
},
|
||||||
|
"remove active room from grid path turns it into empty tile": assert => {
|
||||||
|
const nav = new Navigation(allowsChild);
|
||||||
|
const path = nav.pathFrom([
|
||||||
|
new Segment("session", 1),
|
||||||
|
new Segment("rooms", ["a", "b", "c"]),
|
||||||
|
new Segment("room", "b")
|
||||||
|
]);
|
||||||
|
const newPath = removeRoomFromPath(path, "b");
|
||||||
|
assert.equal(newPath.segments.length, 3);
|
||||||
|
assert.equal(newPath.segments[0].type, "session");
|
||||||
|
assert.equal(newPath.segments[0].value, 1);
|
||||||
|
assert.equal(newPath.segments[1].type, "rooms");
|
||||||
|
assert.deepEqual(newPath.segments[1].value, ["a", "", "c"]);
|
||||||
|
assert.equal(newPath.segments[2].type, "empty-grid-tile");
|
||||||
|
assert.equal(newPath.segments[2].value, 1);
|
||||||
|
},
|
||||||
|
"remove inactive room from grid path": assert => {
|
||||||
|
const nav = new Navigation(allowsChild);
|
||||||
|
const path = nav.pathFrom([
|
||||||
|
new Segment("session", 1),
|
||||||
|
new Segment("rooms", ["a", "b", "c"]),
|
||||||
|
new Segment("room", "b")
|
||||||
|
]);
|
||||||
|
const newPath = removeRoomFromPath(path, "a");
|
||||||
|
assert.equal(newPath.segments.length, 3);
|
||||||
|
assert.equal(newPath.segments[0].type, "session");
|
||||||
|
assert.equal(newPath.segments[0].value, 1);
|
||||||
|
assert.equal(newPath.segments[1].type, "rooms");
|
||||||
|
assert.deepEqual(newPath.segments[1].value, ["", "b", "c"]);
|
||||||
|
assert.equal(newPath.segments[2].type, "room");
|
||||||
|
assert.equal(newPath.segments[2].value, "b");
|
||||||
|
},
|
||||||
|
"remove inactive room from grid path with empty tile": assert => {
|
||||||
|
const nav = new Navigation(allowsChild);
|
||||||
|
const path = nav.pathFrom([
|
||||||
|
new Segment("session", 1),
|
||||||
|
new Segment("rooms", ["a", "b", ""]),
|
||||||
|
new Segment("empty-grid-tile", 3)
|
||||||
|
]);
|
||||||
|
const newPath = removeRoomFromPath(path, "b");
|
||||||
|
assert.equal(newPath.segments.length, 3);
|
||||||
|
assert.equal(newPath.segments[0].type, "session");
|
||||||
|
assert.equal(newPath.segments[0].value, 1);
|
||||||
|
assert.equal(newPath.segments[1].type, "rooms");
|
||||||
|
assert.deepEqual(newPath.segments[1].value, ["a", "", ""]);
|
||||||
|
assert.equal(newPath.segments[2].type, "empty-grid-tile");
|
||||||
|
assert.equal(newPath.segments[2].value, 3);
|
||||||
|
},
|
||||||
|
"remove active room": assert => {
|
||||||
|
const nav = new Navigation(allowsChild);
|
||||||
|
const path = nav.pathFrom([
|
||||||
|
new Segment("session", 1),
|
||||||
|
new Segment("room", "b")
|
||||||
|
]);
|
||||||
|
const newPath = removeRoomFromPath(path, "b");
|
||||||
|
assert.equal(newPath.segments.length, 1);
|
||||||
|
assert.equal(newPath.segments[0].type, "session");
|
||||||
|
assert.equal(newPath.segments[0].value, 1);
|
||||||
|
},
|
||||||
|
"remove inactive room doesn't do anything": assert => {
|
||||||
|
const nav = new Navigation(allowsChild);
|
||||||
|
const path = nav.pathFrom([
|
||||||
|
new Segment("session", 1),
|
||||||
|
new Segment("room", "b")
|
||||||
|
]);
|
||||||
|
const newPath = removeRoomFromPath(path, "a");
|
||||||
|
assert.equal(newPath.segments.length, 2);
|
||||||
|
assert.equal(newPath.segments[0].type, "session");
|
||||||
|
assert.equal(newPath.segments[0].value, 1);
|
||||||
|
assert.equal(newPath.segments[1].type, "room");
|
||||||
|
assert.equal(newPath.segments[1].value, "b");
|
||||||
|
},
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Reference in a new issue