-
Notifications
You must be signed in to change notification settings - Fork 4
/
CatalogPage.tsx
127 lines (115 loc) · 3.58 KB
/
CatalogPage.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/* eslint-disable */
import * as React from "react";
import * as ReactDOM from "react-dom";
const OPDSCatalog = require("@thepalaceproject/web-opds-client");
import * as PropTypes from "prop-types";
import { ActionsProvider } from "@thepalaceproject/web-opds-client/lib/components/context/ActionsContext";
import BookDetailsContainer from "./BookDetailsContainer";
import Header from "./Header";
import Footer from "./Footer";
import computeBreadcrumbs from "../computeBreadcrumbs";
import EntryPointsContainer from "./EntryPointsContainer";
import WelcomePage from "./WelcomePage";
import DataFetcher from "@thepalaceproject/web-opds-client/lib/DataFetcher";
import ActionsCreator from "@thepalaceproject/web-opds-client/lib/actions";
import { adapter } from "@thepalaceproject/web-opds-client/lib/OPDSDataAdapter";
import title from "../utils/title";
export interface CatalogPageProps extends React.Props<CatalogPage> {
params: {
collectionUrl: string;
bookUrl: string;
tab: string;
};
}
/** Extracts URL parameters and renders the OPDS Web Client for the appropriate catalog
and book, using the admin interface header and book details wrapper with extra tabs. */
export default class CatalogPage extends React.Component<CatalogPageProps, {}> {
static childContextTypes: React.ValidationMap<any> = {
tab: PropTypes.string,
library: PropTypes.func,
};
getChildContext() {
return {
tab: this.props.params.tab,
library: () =>
this.getLibrary(
this.props.params.collectionUrl,
this.props.params.bookUrl
),
};
}
render(): JSX.Element {
if (!this.hasLibrary()) {
return <WelcomePage />;
}
let { collectionUrl, bookUrl } = this.props.params;
collectionUrl = this.expandCollectionUrl(collectionUrl) || null;
bookUrl = this.expandBookUrl(bookUrl) || null;
let pageTitleTemplate = (collectionTitle, bookTitle) => {
let details = bookTitle || collectionTitle;
return title(details);
};
const fetcher = new DataFetcher({ adapter });
const actions = new ActionsCreator(fetcher);
return (
<>
<ActionsProvider actions={actions} fetcher={fetcher}>
<OPDSCatalog
collectionUrl={collectionUrl}
bookUrl={bookUrl}
BookDetailsContainer={BookDetailsContainer}
Header={Header}
pageTitleTemplate={pageTitleTemplate}
computeBreadcrumbs={computeBreadcrumbs}
CollectionContainer={EntryPointsContainer}
allLanguageSearch={true}
/>
</ActionsProvider>
<Footer />
</>
);
}
getLibrary(collectionUrl, bookUrl): string {
if (collectionUrl) {
let urlParts = collectionUrl.split("/");
if (urlParts.length > 0) {
return urlParts[0];
}
}
if (bookUrl) {
let urlParts = bookUrl.split("/");
if (urlParts.length > 0) {
return urlParts[0];
}
}
return null;
}
hasLibrary(): boolean {
let library = this.getLibrary(
this.props.params.collectionUrl,
this.props.params.bookUrl
);
return !!library;
}
expandCollectionUrl(url: string): string {
return url
? `${document.location.origin}/${url}${
url.includes("?") ? "&" : "?"
}max_cache_age=0`
: url;
}
expandBookUrl(url: string): string {
if (url) {
const library = this.getLibrary(null, url);
return (
document.location.origin +
"/" +
library +
"/works/" +
url.replace(library + "/", "")
);
} else {
return url;
}
}
}