forked from nytimes/document-viewer
-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy pathapi.js
340 lines (278 loc) · 9.81 KB
/
api.js
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
// The API references it's viewer.
DV.Api = function(viewer) {
this.viewer = viewer;
};
// Set up the API class.
DV.Api.prototype = {
// Return the current page of the document.
currentPage : function() {
return this.viewer.models.document.currentPage();
},
// Set the current page of the document.
setCurrentPage : function(page) {
this.viewer.helpers.jump(page - 1);
},
// Register a callback for when the page is changed.
onPageChange : function(callback) {
this.viewer.models.document.onPageChangeCallbacks.push(callback);
},
// Return the page number for one of the three physical page DOM elements, by id:
getPageNumberForId : function(id) {
var page = this.viewer.pageSet.pages[id];
return page.index + 1;
},
// Get the document's canonical schema
getSchema : function() {
return this.viewer.schema.document;
},
// Get the document's canonical ID.
getId : function() {
return this.viewer.schema.document.id;
},
// Get the document's numerical ID.
getModelId : function() {
return parseInt(this.getId(), 10);
},
// Return the current zoom factor of the document.
currentZoom : function() {
var doc = this.viewer.models.document;
return doc.zoomLevel / doc.ZOOM_RANGES[1];
},
// Return the current zoom factor of the document relative to the base zoom.
relativeZoom : function() {
var models = this.viewer.models;
var zoom = this.currentZoom();
return zoom * (models.document.ZOOM_RANGES[1] / models.pages.BASE_WIDTH);
},
// Return the total number of pages in the document.
numberOfPages : function() {
return this.viewer.models.document.totalPages;
},
// Return the name of the contributor, if available.
getContributor : function() {
return this.viewer.schema.document.contributor;
},
// Return the name of the contributing organization, if available.
getContributorOrganization : function() {
return this.viewer.schema.document.contributor_organization;
},
// Change the documents' sections, re-rendering the navigation. "sections"
// should be an array of sections in the canonical format:
// {title: "Chapter 1", pages: "1-12"}
setSections : function(sections) {
sections = DV._.sortBy(sections, function(s){ return s.page; });
this.viewer.schema.data.sections = sections;
this.viewer.models.chapters.loadChapters();
this.redraw();
},
// Get a list of every section in the document.
getSections : function() {
return DV._.clone(this.viewer.schema.data.sections || []);
},
// Get the document's description.
getDescription : function() {
return this.viewer.schema.document.description;
},
// Set the document's description and update the sidebar.
setDescription : function(desc) {
this.viewer.schema.document.description = desc;
this.viewer.$('.DV-description').remove();
this.viewer.$('.DV-navigation').prepend(JST.descriptionContainer({description: desc}));
this.viewer.helpers.displayNavigation();
},
// Get the document's related article url.
getRelatedArticle : function() {
return this.viewer.schema.document.resources.related_article;
},
// Set the document's related article url.
setRelatedArticle : function(url) {
this.viewer.schema.document.resources.related_article = url;
this.viewer.$('.DV-storyLink a').attr({href : url});
this.viewer.$('.DV-storyLink').toggle(!!url);
},
// Get the document's published url.
getPublishedUrl : function() {
return this.viewer.schema.document.resources.published_url;
},
// Set the document's published url.
setPublishedUrl : function(url) {
this.viewer.schema.document.resources.published_url = url;
},
// Get the document's title.
getTitle : function() {
return this.viewer.schema.document.title;
},
// Set the document's title.
setTitle : function(title) {
this.viewer.schema.document.title = title;
document.title = title;
},
getSource : function() {
return this.viewer.schema.document.source;
},
setSource : function(source) {
this.viewer.schema.document.source = source;
},
getPageText : function(pageNumber) {
return this.viewer.schema.text[pageNumber - 1];
},
// Set the page text for the given page of a document in the local cache.
setPageText : function(text, pageNumber) {
this.viewer.schema.text[pageNumber - 1] = text;
},
// Reset all modified page text to the original values from the server cache.
resetPageText : function(overwriteOriginal) {
var self = this;
var pageText = this.viewer.schema.text;
if (overwriteOriginal) {
this.viewer.models.document.originalPageText = {};
} else {
DV._.each(this.viewer.models.document.originalPageText, function(originalPageText, pageNumber) {
pageNumber = parseInt(pageNumber, 10);
if (originalPageText != pageText[pageNumber-1]) {
self.setPageText(originalPageText, pageNumber);
if (pageNumber == self.currentPage()) {
self.viewer.events.loadText();
}
}
});
}
if (this.viewer.openEditor == 'editText') {
this.viewer.$('.DV-textContents').attr('contentEditable', true).addClass('DV-editing');
}
},
// Redraw the UI. Call redraw(true) to also redraw annotations and pages.
redraw : function(redrawAll) {
if (redrawAll) {
this.viewer.models.annotations.renderAnnotations();
this.viewer.models.document.computeOffsets();
}
this.viewer.helpers.renderNavigation();
this.viewer.helpers.renderComponents();
if (redrawAll) {
this.viewer.elements.window.removeClass('DV-coverVisible');
this.viewer.pageSet.buildPages({noNotes : true});
this.viewer.helpers.autoZoomPage();
}
},
getAnnotationsBySortOrder : function() {
return this.viewer.models.annotations.sortAnnotations();
},
getAnnotationsByPageIndex : function(idx) {
return this.viewer.models.annotations.getAnnotations(idx);
},
getAnnotation : function(aid) {
return this.viewer.models.annotations.getAnnotation(aid);
},
setCurrentAnnotation: function(aid) {
var noteModel = this.viewer.models.annotations.getAnnotation(aid);
this.viewer.pageSet.showAnnotation(noteModel);
},
// Add a new annotation to the document, prefilled to any extent.
addAnnotation : function(anno) {
anno = this.viewer.schema.loadAnnotation(anno);
this.viewer.models.annotations.sortAnnotations();
this.redraw(true);
this.viewer.pageSet.showAnnotation(anno, {active: true, edit : true});
return anno;
},
// Register a callback for when an annotation is saved.
onAnnotationSave : function(callback) {
this.viewer.models.annotations.saveCallbacks.push(callback);
},
// Register a callback for when an annotation is deleted.
onAnnotationDelete : function(callback) {
this.viewer.models.annotations.deleteCallbacks.push(callback);
},
setConfirmStateChange : function(callback) {
this.viewer.confirmStateChange = callback;
},
onChangeState : function(callback) {
this.viewer.onStateChangeCallbacks.push(callback);
},
getState : function() {
return this.viewer.state;
},
// set the state. This takes "ViewDocument," "ViewThumbnails", "ViewText"
setState : function(state) {
this.viewer.open(state);
},
resetRemovedPages : function() {
this.viewer.models.document.resetRemovedPages();
},
addPageToRemovedPages : function(page) {
this.viewer.models.document.addPageToRemovedPages(page);
},
removePageFromRemovedPages : function(page) {
this.viewer.models.document.removePageFromRemovedPages(page);
},
resetReorderedPages : function() {
this.viewer.models.document.redrawReorderedPages();
},
reorderPages : function(pageOrder, options) {
var model = this.getModelId();
this.viewer.models.document.reorderPages(model, pageOrder, options);
},
// Request the loading of an external JS file.
loadJS : function(url, callback) {
DV.jQuery.getScript(url, callback);
},
// Set first/last styles for tabs.
roundTabCorners : function() {
var tabs = this.viewer.$('.DV-views > div:visible');
tabs.first().addClass('DV-first');
tabs.last().addClass('DV-last');
},
// Register hooks into DV's hash history
registerHashListener : function(matcher, callback) {
this.viewer.history.register(matcher, callback);
},
// Clobber DV's existing history hooks
clearHashListeners : function() {
this.viewer.history.defaultCallback = null;
this.viewer.history.handlers = [];
},
// Unload the viewer.
unload: function(viewer) {
this.viewer.helpers.unbindEvents();
DV.jQuery('.DV-docViewer', this.viewer.options.container).remove();
this.viewer.helpers.stopCheckTimer();
delete DV.viewers[this.viewer.schema.document.id];
},
// ---------------------- Enter/Leave Edit Modes -----------------------------
enterRemovePagesMode : function() {
this.viewer.openEditor = 'removePages';
},
leaveRemovePagesMode : function() {
this.viewer.openEditor = null;
},
enterAddPagesMode : function() {
this.viewer.openEditor = 'addPages';
},
leaveAddPagesMode : function() {
this.viewer.openEditor = null;
},
enterReplacePagesMode : function() {
this.viewer.openEditor = 'replacePages';
},
leaveReplacePagesMode : function() {
this.viewer.openEditor = null;
},
enterReorderPagesMode : function() {
this.viewer.openEditor = 'reorderPages';
this.viewer.elements.viewer.addClass('DV-reorderPages');
},
leaveReorderPagesMode : function() {
this.resetReorderedPages();
this.viewer.openEditor = null;
this.viewer.elements.viewer.removeClass('DV-reorderPages');
},
enterEditPageTextMode : function() {
this.viewer.openEditor = 'editText';
this.viewer.events.loadText();
},
leaveEditPageTextMode : function() {
this.viewer.openEditor = null;
this.resetPageText();
}
};