PageSourceSearch

https://bahai-news.info/javascripts/DV/controllers/api.js

js bahai-news.info collected 2026-10-02 14:22:07 UTC 9,611 bytes, 327 lines download raw bytes

1// The API references it's viewer.
2DV.Api = function(viewer) {
3  this.viewer = viewer;
4};
5
6// Set up the API class.
7DV.Api.prototype = {
8
9  // Return the current page of the document.
10  currentPage : function() {
11    return this.viewer.models.document.currentPage();
12  },
13  
14  // Set the current page of the document.
15  setCurrentPage : function(page) {
16    this.viewer.helpers.jump(page - 1);
17  },
18  
19  // Register a callback for when the page is changed.
20  onPageChange : function(callback) {
21    this.viewer.models.document.onPageChangeCallbacks.push(callback);
22  },
23
24  // Return the page number for one of the three physical page DOM elements, by id:
25  getPageNumberForId : function(id) {
26    var page = this.viewer.pageSet.pages[id];
27    return page.index + 1;
28  },
29
30  // Get the document's canonical schema
31  getSchema : function() {
32    return this.viewer.schema.document;
33  },
34
35  // Get the document's canonical ID.
36  getId : function() {
37    return this.viewer.schema.document.id;
38  },
39
40  // Get the document's numerical ID.
41  getModelId : function() {
42    return parseInt(this.getId(), 10);
43  },
44
45  // Return the current zoom factor of the document.
46  currentZoom : function() {
47    var doc = this.viewer.models.document;
48    return doc.zoomLevel / doc.ZOOM_RANGES[1];
49  },
50
51  // Return the current zoom factor of the document relative to the base zoom.
52  relativeZoom : function() {
53    var models = this.viewer.models;
54    var zoom   = this.currentZoom();
55    return zoom * (models.document.ZOOM_RANGES[1] / models.pages.BASE_WIDTH);
56  },
57
58  // Return the total number of pages in the document.
59  numberOfPages : function() {
60    return this.viewer.models.document.totalPages;
61  },
62
63  // Return the name of the conributor, if available.
64  getContributor : function() {
65    return this.viewer.schema.document.contributor;
66  },
67
68  // Return the name of the contributing organization, if available.
69  getContributorOrganization : function() {
70    return this.viewer.schema.document.contributor_organization;
71  },
72
73  // Change the documents' sections, re-rendering the navigation. "sections"
74  // should be an array of sections in the canonical format:
75  // {title: "Chapter 1", pages: "1-12"}
76  setSections : function(sections) {
77    sections = _.sortBy(sections, function(s){ return s.page; });
78    this.viewer.schema.data.sections = sections;
79    this.viewer.models.chapters.loadChapters();
80    this.redraw();
81  },
82
83  // Get a list of every section in the document.
84  getSections : function() {
85    return _.clone(this.viewer.schema.data.sections || []);
86  },
87
88  // Get the document's description.
89  getDescription : function() {
90    return this.viewer.schema.document.description;
91  },
92
93  // Set the document's description and update the sidebar.
94  setDescription : function(desc) {
95    this.viewer.schema.document.description = desc;
96    this.viewer.$('.DV-description').remove();
97    this.viewer.$('.DV-navigation').prepend(JST.descriptionContainer({description: desc}));
98    this.viewer.helpers.displayNavigation();
99  },
100
101  // Get the document's related article url.
102  getRelatedArticle : function() {
103    return this.viewer.schema.document.resources.related_article;
104  },
105
106  // Set the document's related article url.
107  setRelatedArticle : function(url) {
108    this.viewer.schema.document.resources.related_article = url;
109    this.viewer.$('.DV-storyLink a').attr({href : url});
110    this.viewer.$('.DV-storyLink').toggle(!!url);
111  },
112
113  // Get the document's published url.
114  getPublishedUrl : function() {
115    return this.viewer.schema.document.resources.published_url;
116  },
117
118  // Set the document's published url.
119  setPublishedUrl : function(url) {
120    this.viewer.schema.document.resources.published_url = url;
121  },
122
123  // Get the document's title.
124  getTitle : function() {
125    return this.viewer.schema.document.title;
126  },
127
128  // Set the document's title.
129  setTitle : function(title) {
130    this.viewer.schema.document.title = title;
131    document.title = title;
132  },
133
134  getSource : function() {
135    return this.viewer.schema.document.source;
136  },
137
138  setSource : function(source) {
139    this.viewer.schema.document.source = source;
140  },
141
142  getPageText : function(pageNumber) {
143    return this.viewer.schema.text[pageNumber - 1];
144  },
145
146  // Set the page text for the given page of a document in the local cache.
147  setPageText : function(text, pageNumber) {
148    this.viewer.schema.text[pageNumber - 1] = text;
149  },
150
151  // Reset all modified page text to the original values from the server cache.
152  resetPageText : function(overwriteOriginal) {
153    var self = this;
154    var pageText = this.viewer.schema.text;
155    if (overwriteOriginal) {
156      this.viewer.models.document.originalPageText = {};
157    } else {
158      _.each(this.viewer.models.document.originalPageText, function(originalPageText, pageNumber) {
159        pageNumber = parseInt(pageNumber, 10);
160        if (originalPageText != pageText[pageNumber-1]) {
161          self.setPageText(originalPageText, pageNumber);
162          if (pageNumber == self.currentPage()) {
163            self.viewer.events.loadText();
164          }
165        }
166      });
167    }
168    if (this.viewer.openEditor == 'editText') {
169      this.viewer.$('.DV-textContents').attr('contentEditable', true).addClass('DV-editing');
170    }
171  },
172
173  // Redraw the UI. Call redraw(true) to also redraw annotations and pages.
174  redraw : function(redrawAll) {
175    if (redrawAll) {
176      this.viewer.models.annotations.renderAnnotations();
177      this.viewer.models.document.computeOffsets();
178    }
179    this.viewer.helpers.renderNavigation();
180    this.viewer.helpers.renderComponents();
181    if (redrawAll) {
182      this.viewer.elements.window.removeClass('DV-coverVisible');
183      this.viewer.pageSet.buildPages({noNotes : true});
184      this.viewer.pageSet.reflowPages();
185    }
186  },
187  
188  getAnnotationsBySortOrder : function() {
189    return this.viewer.models.annotations.sortAnnotations();
190  },
191  
192  getAnnotationsByPageIndex : function(idx) {
193    return this.viewer.models.annotations.getAnnotations(idx);
194  },
195  
196  getAnnotation : function(aid) {
197    return this.viewer.models.annotations.getAnnotation(aid);
198  },
199
200  // Add a new annotation to the document, prefilled to any extent.
201  addAnnotation : function(anno) {
202    anno = this.viewer.schema.loadAnnotation(anno);
203    this.viewer.models.annotations.sortAnnotations();
204    this.redraw(true);
205    this.viewer.pageSet.showAnnotation(anno, {active: true, edit : true});
206    return anno;
207  },
208
209  // Register a callback for when an annotation is saved.
210  onAnnotationSave : function(callback) {
211    this.viewer.models.annotations.saveCallbacks.push(callback);
212  },
213
214  // Register a callback for when an annotation is deleted.
215  onAnnotationDelete : function(callback) {
216    this.viewer.models.annotations.deleteCallbacks.push(callback);
217  },
218
219  setConfirmStateChange : function(callback) {
220    this.viewer.confirmStateChange = callback;
221  },
222
223  onChangeState : function(callback) {
224    this.viewer.onStateChangeCallbacks.push(callback);
225  },
226  
227  getState : function() {
228    return this.viewer.state;
229  },
230  
231  // set the state. This takes "ViewDocument," "ViewThumbnails", "ViewText"
232  setState : function(state) {
233    this.viewer.open(state);
234  },
235
236  resetRemovedPages : function() {
237    this.viewer.models.document.resetRemovedPages();
238  },
239
240  addPageToRemovedPages : function(page) {
241    this.viewer.models.document.addPageToRemovedPages(page);
242  },
243
244  removePageFromRemovedPages : function(page) {
245    this.viewer.models.document.removePageFromRemovedPages(page);
246  },
247
248  resetReorderedPages : function() {
249    this.viewer.models.document.redrawReorderedPages();
250  },
251
252  reorderPages : function(pageOrder, options) {
253    var model = this.getModelId();
254    this.viewer.models.document.reorderPages(model, pageOrder, options);
255  },
256
257  // Request the loading of an external JS file.
258  loadJS : function(url, callback) {
259    DV.jQuery.getScript(url, callback);
260  },
261
262  // Set first/last styles for tabs.
263  roundTabCorners : function() {
264    var tabs = this.viewer.$('.DV-views > div:visible');
265    tabs.first().addClass('DV-first');
266    tabs.last().addClass('DV-last');
267  },
268  
269  // Register hooks into DV's hash history
270  registerHashListener : function(matcher, callback) {
271    this.viewer.history.register(matcher, callback);
272  },
273  
274  // Clobber DV's existing history hooks
275  clearHashListeners : function() {
276    this.viewer.history.defaultCallback = null;
277    this.viewer.history.handlers = [];
278  },
279
280  // ---------------------- Enter/Leave Edit Modes -----------------------------
281
282  enterRemovePagesMode : function() {
283    this.viewer.openEditor = 'removePages';
284  },
285
286  leaveRemovePagesMode : function() {
287    this.viewer.openEditor = null;
288  },
289
290  enterAddPagesMode : function() {
291    this.viewer.openEditor = 'addPages';
292  },
293
294  leaveAddPagesMode : function() {
295    this.viewer.openEditor = null;
296  },
297
298  enterReplacePagesMode : function() {
299    this.viewer.openEditor = 'replacePages';
300  },
301
302  leaveReplacePagesMode : function() {
303    this.viewer.openEditor = null;
304  },
305
306  enterReorderPagesMode : function() {
307    this.viewer.openEditor = 'reorderPages';
308    this.viewer.elements.viewer.addClass('DV-reorderPages');
309  },
310
311  leaveReorderPagesMode : function() {
312    this.resetReorderedPages();
313    this.viewer.openEditor = null;
314    this.viewer.elements.viewer.removeClass('DV-reorderPages');
315  },
316
317  enterEditPageTextMode : function() {
318    this.viewer.openEditor = 'editText';
319    this.viewer.events.loadText();
320  },
321
322  leaveEditPageTextMode : function() {
323    this.viewer.openEditor = null;
324    this.resetPageText();
325  }
326
327};

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.