-
Notifications
You must be signed in to change notification settings - Fork 13
/
publication.ts
208 lines (172 loc) · 5.32 KB
/
publication.ts
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
// ==LICENSE-BEGIN==
// Copyright 2017 European Digital Reading Lab. All rights reserved.
// Licensed to the Readium Foundation under one or more contributor license agreements.
// Use of this source code is governed by a BSD-style license
// that can be found in the LICENSE file exposed on Github (readium) in the project repository.
// ==LICENSE-END==
// import { JsonStringConverter } from "@utils/ta-json-string-converter";
import { LCP } from "@r2-lcp-js/parser/epub/lcp";
import { IZip } from "@utils/zip/zip";
// https://github.com/edcarroll/ta-json
import {
// JsonConverter,
JsonElementType,
JsonObject,
JsonProperty,
OnDeserialized,
} from "ta-json-x";
import { IInternal } from "./internal";
import { Metadata } from "./metadata";
import { Link } from "./publication-link";
// import { IPublicationCollection } from "./publication-collection";
@JsonObject()
export class Publication {
// @JsonConverter(JsonStringConverter)
@JsonProperty("@context")
@JsonElementType(String)
public Context!: string[];
@JsonProperty("metadata")
public Metadata!: Metadata;
@JsonProperty("links")
@JsonElementType(Link)
public Links!: Link[];
@JsonProperty("readingOrder")
@JsonElementType(Link)
public Spine!: Link[];
@JsonProperty("resources")
@JsonElementType(Link)
public Resources!: Link[];
@JsonProperty("toc")
@JsonElementType(Link)
public TOC!: Link[];
@JsonProperty("page-list")
@JsonElementType(Link)
public PageList!: Link[];
@JsonProperty("landmarks")
@JsonElementType(Link)
public Landmarks!: Link[];
@JsonProperty("loi")
@JsonElementType(Link)
public LOI!: Link[];
@JsonProperty("loa")
@JsonElementType(Link)
public LOA!: Link[];
@JsonProperty("lov")
@JsonElementType(Link)
public LOV!: Link[];
@JsonProperty("lot")
@JsonElementType(Link)
public LOT!: Link[];
// // OPDS2
// @JsonProperty("images")
// @JsonElementType(Link)
// public Images!: Link[];
// public OtherLinks: Link[];
// public OtherCollections: IPublicationCollection[];
public LCP: LCP | undefined;
private Internal: IInternal[] | undefined;
public freeDestroy() {
console.log("freeDestroy: Publication");
if (this.Internal) {
const zipInternal = this.findFromInternal("zip");
if (zipInternal) {
const zip = zipInternal.Value as IZip;
zip.freeDestroy();
}
}
}
public findFromInternal(key: string): IInternal | undefined {
if (this.Internal) {
const found = this.Internal.find((internal) => {
return internal.Name === key;
});
if (found) {
return found;
}
}
return undefined;
}
public AddToInternal(key: string, value: any) {
const existing = this.findFromInternal(key);
if (existing) {
existing.Value = value;
} else {
if (!this.Internal) {
this.Internal = [];
}
const internal: IInternal = { Name: key, Value: value };
this.Internal.push(internal);
}
}
// public findLinKByHref(href: string): Link | undefined {
// if (this.Spine) {
// const ll = this.Spine.find((link) => {
// if (link.Href && href.indexOf(link.Href) >= 0) {
// return true;
// }
// return false;
// });
// if (ll) {
// return ll;
// }
// }
// return undefined;
// }
public GetCover(): Link | undefined {
return this.searchLinkByRel("cover");
}
public GetNavDoc(): Link | undefined {
return this.searchLinkByRel("contents");
}
public searchLinkByRel(rel: string): Link | undefined {
if (this.Resources) {
const ll = this.Resources.find((link) => {
return link.HasRel(rel);
});
if (ll) {
return ll;
}
}
if (this.Spine) {
const ll = this.Spine.find((link) => {
return link.HasRel(rel);
});
if (ll) {
return ll;
}
}
if (this.Links) {
const ll = this.Links.find((link) => {
return link.HasRel(rel);
});
if (ll) {
return ll;
}
}
return undefined;
}
public AddLink(typeLink: string, rel: string[], url: string, templated: boolean | undefined) {
const link = new Link();
link.AddRels(rel);
link.Href = url;
link.TypeLink = typeLink;
if (typeof templated !== "undefined") {
link.Templated = templated;
}
if (!this.Links) {
this.Links = [];
}
this.Links.push(link);
}
@OnDeserialized()
// tslint:disable-next-line:no-unused-variable
// @ts-ignore: TS6133 (is declared but its value is never read.)
private _OnDeserialized() {
if (!this.Metadata) {
console.log("Publication.Metadata is not set!");
}
if (!this.Links) {
console.log("Publication.Links is not set!");
}
}
}