2020-10-06 18:03:12 +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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import {Segment} from "./Navigation.js";
|
|
|
|
|
|
|
|
export class URLRouter {
|
2020-10-12 17:49:06 +02:00
|
|
|
constructor({history, navigation, parseUrlPath, stringifyPath}) {
|
2020-10-06 18:03:12 +02:00
|
|
|
this._subscription = null;
|
2020-10-08 18:22:36 +02:00
|
|
|
this._history = history;
|
2020-10-06 18:03:12 +02:00
|
|
|
this._navigation = navigation;
|
2020-10-12 17:49:06 +02:00
|
|
|
this._parseUrlPath = parseUrlPath;
|
|
|
|
this._stringifyPath = stringifyPath;
|
2020-10-06 18:03:12 +02:00
|
|
|
}
|
|
|
|
|
2020-10-09 09:56:01 +02:00
|
|
|
attach() {
|
2020-10-08 18:22:36 +02:00
|
|
|
this._subscription = this._history.subscribe(url => {
|
2020-10-12 17:49:06 +02:00
|
|
|
const redirectedUrl = this.applyUrl(url);
|
|
|
|
if (redirectedUrl !== url) {
|
|
|
|
this._history.replaceUrl(redirectedUrl);
|
|
|
|
}
|
2020-10-06 18:03:12 +02:00
|
|
|
});
|
2020-10-09 17:00:19 +02:00
|
|
|
this.applyUrl(this._history.get());
|
2020-10-07 09:40:51 +02:00
|
|
|
}
|
|
|
|
|
2020-10-12 17:49:06 +02:00
|
|
|
dispose() {
|
2020-10-06 18:03:12 +02:00
|
|
|
this._subscription = this._subscription();
|
|
|
|
}
|
|
|
|
|
2020-10-12 17:49:06 +02:00
|
|
|
applyUrl(url) {
|
|
|
|
const urlPath = this._history.urlAsPath(url)
|
2020-10-12 18:31:55 +02:00
|
|
|
const navPath = this._navigation.pathFrom(this._parseUrlPath(urlPath, this._navigation.path));
|
2020-10-12 17:49:06 +02:00
|
|
|
this._navigation.applyPath(navPath);
|
|
|
|
return this._history.pathAsUrl(this._stringifyPath(navPath));
|
2020-10-06 18:03:12 +02:00
|
|
|
}
|
|
|
|
|
2020-10-09 17:00:19 +02:00
|
|
|
get history() {
|
|
|
|
return this._history;
|
2020-10-08 18:22:36 +02:00
|
|
|
}
|
|
|
|
|
2020-10-12 17:49:06 +02:00
|
|
|
urlForSegments(segments) {
|
|
|
|
let path = this._navigation.path;
|
|
|
|
for (const segment of segments) {
|
|
|
|
path = path.with(segment);
|
|
|
|
if (!path) {
|
|
|
|
return;
|
|
|
|
}
|
2020-10-06 18:03:12 +02:00
|
|
|
}
|
2020-10-12 17:49:06 +02:00
|
|
|
return this.urlForPath(path);
|
|
|
|
}
|
|
|
|
|
|
|
|
urlForSegment(type, value) {
|
|
|
|
return this.urlForSegments([this._navigation.segment(type, value)]);
|
2020-10-06 18:03:12 +02:00
|
|
|
}
|
|
|
|
|
2020-10-14 14:53:52 +02:00
|
|
|
urlUntilSegment(type) {
|
|
|
|
return this.urlForPath(this._navigation.path.until(type));
|
|
|
|
}
|
|
|
|
|
2020-10-06 18:03:12 +02:00
|
|
|
urlForPath(path) {
|
2020-10-12 17:49:06 +02:00
|
|
|
return this.history.pathAsUrl(this._stringifyPath(path));
|
|
|
|
}
|
|
|
|
|
|
|
|
openRoomActionUrl(roomId) {
|
|
|
|
// not a segment to navigation knowns about, so append it manually
|
|
|
|
const urlPath = `${this._stringifyPath(this._navigation.path.until("session"))}/open-room/${roomId}`;
|
2020-10-08 18:22:36 +02:00
|
|
|
return this._history.pathAsUrl(urlPath);
|
2020-10-06 18:03:12 +02:00
|
|
|
}
|
2020-10-12 17:49:06 +02:00
|
|
|
|
|
|
|
disableGridUrl() {
|
2020-10-12 18:31:55 +02:00
|
|
|
let path = this._navigation.path.until("session");
|
|
|
|
const room = this._navigation.path.get("room");
|
|
|
|
if (room) {
|
|
|
|
path = path.with(room);
|
|
|
|
}
|
|
|
|
return this.urlForPath(path);
|
2020-10-12 17:49:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
enableGridUrl() {
|
|
|
|
let path = this._navigation.path.until("session");
|
2020-10-12 18:31:55 +02:00
|
|
|
const room = this._navigation.path.get("room");
|
2020-10-12 17:49:06 +02:00
|
|
|
if (room) {
|
|
|
|
path = path.with(this._navigation.segment("rooms", [room.value]));
|
|
|
|
path = path.with(room);
|
|
|
|
} else {
|
|
|
|
path = path.with(this._navigation.segment("rooms", []));
|
|
|
|
path = path.with(this._navigation.segment("empty-grid-tile", 0));
|
|
|
|
}
|
|
|
|
return this.urlForPath(path);
|
|
|
|
}
|
2020-10-06 18:03:12 +02:00
|
|
|
}
|