Skip to content

Instantly share code, notes, and snippets.

@documentcloud
Created December 18, 2009 22:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save documentcloud/259796 to your computer and use it in GitHub Desktop.
Save documentcloud/259796 to your computer and use it in GitHub Desktop.
/////////////////////////////////////////////////////////////////////////////
// The public DocumentViewer API. Available for other scripts to access:
/////////////////////////////////////////////////////////////////////////////
(function(){
DV.api = {
// Return the current page of the document.
currentPage : function() {
return DV.controller.models.document.currentPage();
},
// Return the current zoom factor of the document.
currentZoom : function() {
var doc = DV.controller.models.document;
return doc.zoomLevel / doc.ZOOM_RANGES[0];
},
// Return the total number of pages in the document.
numberOfPages : function() {
return DV.controller.models.document.totalPages;
},
// 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) {
DV.Schema.data.sections = sections;
DV.controller.models.chapters.loadChapters();
this.redraw();
},
// Get a list of every section in the document.
getSections : function() {
return _.clone(DV.Schema.data.sections || []);
},
// Redraw the UI. Call redraw(true) to also redraw annotations and pages.
redraw : function(redrawAll) {
if (redrawAll) DV.controller.models.annotations.renderAnnotations();
DV.controller.helpers.renderNavigation();
DV.controller.helpers.renderComponents();
if (redrawAll) {
DV.controller.elements.window.removeClass('DV-coverVisible');
DV.controller.pageSet.buildPages();
}
},
// Add a new annotation to the document, prefilled to any extent.
addAnnotation : function(anno) {
anno = DV.Schema.loadAnnotation(anno);
this.redraw(true);
// Super-hacky -- there should be a way to look these up besides the DOM.
DV.controller.helpers.getAnnotationObject($j('#DV-annotation-' + anno.id)).toggle({edit : true});
return anno;
},
// Register a callback for when an annotation is saved.
onAnnotationSave : function(callback) {
DV.controller.models.annotations.saveCallbacks.push(callback);
},
// Register a callback for when an annotation is deleted.
onAnnotationDelete : function(callback) {
DV.controller.models.annotations.deleteCallbacks.push(callback);
}
};
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment