aboutsummaryrefslogtreecommitdiff
path: root/src/client/util/History.ts
diff options
context:
space:
mode:
Diffstat (limited to 'src/client/util/History.ts')
-rw-r--r--src/client/util/History.ts45
1 files changed, 22 insertions, 23 deletions
diff --git a/src/client/util/History.ts b/src/client/util/History.ts
index 7dcff9c56..18aee6444 100644
--- a/src/client/util/History.ts
+++ b/src/client/util/History.ts
@@ -1,8 +1,8 @@
-import { Doc } from "../../fields/Doc";
-import { DocServer } from "../DocServer";
import * as qs from 'query-string';
-import { Utils, OmitKeys } from "../../Utils";
-import { CurrentUserUtils } from "./CurrentUserUtils";
+import { Doc } from '../../fields/Doc';
+import { OmitKeys, Utils } from '../../Utils';
+import { DocServer } from '../DocServer';
+import { DashboardView } from '../views/DashboardView';
export namespace HistoryUtil {
export interface DocInitializerList {
@@ -10,7 +10,7 @@ export namespace HistoryUtil {
}
export interface DocUrl {
- type: "doc";
+ type: 'doc';
docId: string;
initializers?: {
[docId: string]: DocInitializerList;
@@ -25,11 +25,11 @@ export namespace HistoryUtil {
// const handlers: ((state: ParsedUrl | null) => void)[] = [];
function onHistory(e: PopStateEvent) {
- if (window.location.pathname !== "/home") {
- const url = e.state as ParsedUrl || parseUrl(window.location);
+ if (window.location.pathname !== '/home') {
+ const url = (e.state as ParsedUrl) || parseUrl(window.location);
if (url) {
switch (url.type) {
- case "doc":
+ case 'doc':
onDocUrl(url);
break;
}
@@ -43,13 +43,13 @@ export namespace HistoryUtil {
let _lastStatePush = 0;
export function pushState(state: ParsedUrl) {
if (Date.now() - _lastStatePush > 1000) {
- history.pushState(state, "", createUrl(state));
+ history.pushState(state, '', createUrl(state));
}
_lastStatePush = Date.now();
}
export function replaceState(state: ParsedUrl) {
- history.replaceState(state, "", createUrl(state));
+ history.replaceState(state, '', createUrl(state));
}
function copyState(state: ParsedUrl): ParsedUrl {
@@ -61,7 +61,7 @@ export namespace HistoryUtil {
if (state) {
state.initializers = state.initializers || {};
}
- return state ?? {initializers:{}};
+ return state ?? { initializers: {} };
}
// export function addHandler(handler: (state: ParsedUrl | null) => void) {
@@ -78,10 +78,10 @@ export namespace HistoryUtil {
const parsers: { [type: string]: (pathname: string[], opts: qs.ParsedQuery) => ParsedUrl | undefined } = {};
const stringifiers: { [type: string]: (state: ParsedUrl) => string } = {};
- type ParserValue = true | "none" | "json" | ((value: string) => any);
+ type ParserValue = true | 'none' | 'json' | ((value: string) => any);
type Parser = {
- [key: string]: ParserValue
+ [key: string]: ParserValue;
};
function addParser(type: string, requiredFields: Parser, optionalFields: Parser, customParser?: (pathname: string[], opts: qs.ParsedQuery, current: ParsedUrl) => ParsedUrl | null | undefined) {
@@ -90,9 +90,9 @@ export namespace HistoryUtil {
return value;
}
if (Array.isArray(value)) {
- } else if (parser === true || parser === "json") {
+ } else if (parser === true || parser === 'json') {
value = JSON.parse(value);
- } else if (parser === "none") {
+ } else if (parser === 'none') {
} else {
value = parser(value);
}
@@ -142,29 +142,28 @@ export namespace HistoryUtil {
}
const queryObj = OmitKeys(state, keys).extract;
const query: any = {};
- Object.keys(queryObj).forEach(key => query[key] = queryObj[key] === null ? null : JSON.stringify(queryObj[key]));
+ Object.keys(queryObj).forEach(key => (query[key] = queryObj[key] === null ? null : JSON.stringify(queryObj[key])));
const queryString = qs.stringify(query);
- return path + (queryString ? `?${queryString}` : "");
+ return path + (queryString ? `?${queryString}` : '');
};
}
- addParser("doc", {}, { readonly: true, initializers: true, nro: true, sharing: true }, (pathname, opts, current) => {
+ addParser('doc', {}, { readonly: true, initializers: true, nro: true, sharing: true }, (pathname, opts, current) => {
if (pathname.length !== 2) return undefined;
current.initializers = current.initializers || {};
const docId = pathname[1];
current.docId = docId;
});
- addStringifier("doc", ["initializers", "readonly", "nro"], (state, current) => {
+ addStringifier('doc', ['initializers', 'readonly', 'nro'], (state, current) => {
return `${current}/${state.docId}`;
});
-
export function parseUrl(location: Location | URL): ParsedUrl | undefined {
const pathname = location.pathname.substring(1);
const search = location.search;
const opts = search.length ? qs.parse(search, { sort: false }) : {};
- const pathnameSplit = pathname.split("/");
+ const pathnameSplit = pathname.split('/');
const type = pathnameSplit[0];
@@ -179,7 +178,7 @@ export namespace HistoryUtil {
if (params.type in stringifiers) {
return stringifiers[params.type](params);
}
- return "";
+ return '';
}
export async function initDoc(id: string, initializer: DocInitializerList) {
@@ -197,7 +196,7 @@ export namespace HistoryUtil {
await Promise.all(Object.keys(init).map(id => initDoc(id, init[id])));
}
if (field instanceof Doc) {
- CurrentUserUtils.openDashboard(field, true);
+ DashboardView.openDashboard(field, true);
}
}