MCPcopy Index your code
hub / github.com/box/viewer.js

github.com/box/viewer.js @v0.11.1

Chat with this repo
repository ↗ · DeepWiki ↗ · release v0.11.1 ↗ · + Follow
125 symbols 225 edges 81 files 96 documented · 77%
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

Build Status Project Status

Viewer.js

A viewer for documents converted with the Box View API.

Contents

Quick Start

Get the Source

You can find the pre-built development and production source files in the dist/ directory in this repository.

Viewer.js is available on npm and Bower:

npm install viewer
bower install viewer

All stable versions of viewer.js are hosted on CloudFlare's CDN, cdnjs. The unminified versions are available through the following URLs:

//cdnjs.cloudflare.com/ajax/libs/viewer.js/<VERSION>/crocodoc.viewer.css
//cdnjs.cloudflare.com/ajax/libs/viewer.js/<VERSION>/crocodoc.viewer.js

and the minified versions through:

//cdnjs.cloudflare.com/ajax/libs/viewer.js/<VERSION>/crocodoc.viewer.min.css
//cdnjs.cloudflare.com/ajax/libs/viewer.js/<VERSION>/crocodoc.viewer.min.js

For additional information, please see the cdnjs website.

v0.10.8

Development

Production

Loading a Simple Viewer

Crocodoc.createViewer(element, config)

Example

<link rel="stylesheet" href="https://github.com/box/viewer.js/raw/v0.11.1/crocodoc.viewer.min.css" />
<script type="text/javascript" src="https://github.com/box/viewer.js/raw/v0.11.1/jquery.min.js"></script>
<script type="text/javascript" src="https://github.com/box/viewer.js/raw/v0.11.1/crocodoc.viewer.min.js"></script>





<script type="text/javascript">
    var viewer = Crocodoc.createViewer('.viewer', {
        url: 'https://view-api.box.com/1/sessions/3c6abc0dcf35422e8353cf9c27578d5c/assets/'
    });
    viewer.load();
</script>

Logos

As per section 2.6 of our agreement of our API terms, we require that all apps using Box View with the Standard tier conspicuously display a Box logo when displaying Box View documents. We have included an approved logo within this repository. If your Box View plan permits custom branding of the viewer, please refer to Logo Options for instructions on how to build the viewer without the Box logo or with a custom logo.

Documentation

Library Methods

Crocodoc.createViewer(element, config)

Create and return a viewer instance initialized with the given parameters.

  • element the DOM element to initialize the viewer into
    • string: a query selector
    • Element: a DOM Element
    • Object: a jQuery object
  • config the configuration object

Crocodoc.addPlugin(pluginName, creatorFn)

Register a new plugin with the framework. See Plugins for more details.

  • pluginName the name of the plugin
  • creatorFn a function that creates and returns an instance of the plugin (which should be an object) when called

Viewer Config

The only required config parameter is url. All others are optional.

url (required)

The url parameter specifies the base URL where the document assets are located. Viewer.js will look for document assets (including info.json, stylesheet.css, etc) in this path.

layout

The layout parameter specifies the layout mode to use. Default Crocodoc.LAYOUT_VERTICAL. See Setting the Layout Mode for available layouts.

zoom

The zoom parameter specifies the initial zoom level to use. Default Crocodoc.ZOOM_AUTO.

Possible values: * Crocodoc.ZOOM_FIT_WIDTH - zooms to fit the width of the (largest) page within the viewport * Crocodoc.ZOOM_FIT_HEIGHT - zooms to fit the height of the (largest) page within the viewport * Crocodoc.ZOOM_AUTO - zooms to best fit the document within the viewport

page

The page parameter specifies the initial page number to show when the document loads. Default: 1.

enableTextSelection

The enableTextSelection parameter specifies whether or not users can select text. If true, users can select text. Default: true. Note: text selection is not supported in IE 8 see Browser Support for more information.

enableLinks

The enableLinks parameter specifies whether or not hyperlinks are enabled. If true, hyperlinks are enabled. Default: true.

enableDragging

The enableDragging parameter specifies whether or not dragging is enabled. If true, click-and-drag scrolling/panning will be enabled for this document. Default: false. NOTE: text selection is not fully supported when dragging is enabled. It is recommended that you disable text selection if you plan to enable dragging.

plugins

The plugins parameter allows you to specify a map of plugin names to their respective configs. Plugin names specified in this object will be loaded when the viewer is initialized. See Plugins for more details.

Example:

{
    plugins: {
        // my-plugin will be initialized with the following config
        'my-plugin': {
            foo: 1,
            bar: 2
        }
    }
}

queryParams

The queryParams parameter allows you to specify query string parameters to append to each asset request (eg., info.json or page-1.svg). Can be an object or string. Default: null.

Examples:

// as a string
{
    queryParams: 'hello=world&foo=bar'
}

// as an object
{
    queryParams: {
        hello: 'world',
        foo: ['bar', 'baz']
    }
}

useWindowAsViewport

The useWindowAsViewport parameter allows you to specify whether to use the browser window as the viewport for the document. This is useful when the document should take up the entire browser window (e.g., on mobile devices). Use this option on mobile devices to allow the browser to auto-hide browser chrome when scrolling. Default: false.

dataProviders

The dataProviders parameter allows you override default data providers by specifying names of replacement data providers. See data providers for more info.

Example:

{
    dataProviders: {
        // page-svg data provider will be overridden by the 'my-page-svg' data provider
        'page-svg': 'my-page-svg'
    }
}

Viewer Methods

destroy()

The destroy method removes and cleans up the viewer instance.

on(name, handler)

The on method binds an event handler for the specified event name fired by the viewer object. See Event Handling for available events.

off(name[, handler])

The off method unbinds an event handler for the specified event name and handler fired by the viewer object. If handler is not given, unbinds all event handlers on this viewer object with the given name.

scrollTo(page)

The scrollTo method scrolls the viewer to the specified page. The page argument may be one of the following: * (number) - scroll the the specified page number * Crocodoc.SCROLL_PREVIOUS - scroll to the previous page * Crocodoc.SCROLL_NEXT - scroll to the next page

Examples

// scroll to page 2
viewer.scrollTo(2);

// scroll to the next page
viewer.scrollTo(Crocodoc.SCROLL_NEXT);

zoom(val)

The zoom method sets the current zoom level of the document. Possible values: * Crocodoc.ZOOM_FIT_WIDTH - zooms to fit the width of the (largest) page within the viewport * Crocodoc.ZOOM_FIT_HEIGHT - zooms to fit the height of the (largest) page within the viewport * Crocodoc.ZOOM_AUTO - zooms to best fit the document within the viewport * Crocodoc.ZOOM_IN - zooms in * Crocodoc.ZOOM_OUT - zooms out

Examples

// zoom in
viewer.zoom(Crocodoc.ZOOM_IN);

// zoom to fit width
viewer.zoom(Crocodoc.ZOOM_FIT_WIDTH);

setLayout(mode)

The setLayout method sets the layout mode. See Setting the Layout Mode for available layouts.

Examples

viewer.setLayout(Crocodoc.LAYOUT_PRESENTATION);

Event Handling

The viewer object fires several different events. You can add and remove event listeners using the on and off methods.

Example

// ready event fires when the document metadata has loaded
// and the viewer is ready to be interacted with
viewer.on('ready', function (event) {
    console.log('the viewer is ready, and the document has ' + event.data.numPages + ' pages');
});

Viewer Events

  • asseterror Triggered if any asset fails to load. Event properties:
    • error - the error message
    • resource - the url of the resource that failed to load
    • status - the http status code
  • destroy Triggered when the document viewer is purposely destroyed with the destroy method.
  • fail Triggered if the document fails to load. Event properties:
    • error - the error details
  • ready Triggered as soon as a document becomes viewable. Event properties:
    • page - the current page
    • numPages - total number of pages in the document
  • resize Triggered when the viewer is resized. Event properties:
    • width - the viewport width
    • height - the viewport height
  • scrollstart Triggered when the user starts scrolling. Event properties:
    • scrollTop - the scrollTop position of the viewport
    • scrollLeft - the scrollLeft position of the viewport
  • scrollend Triggered when the user stops scrolling (or when the content stops moving if there is a momentum effect). Event properties:
    • scrollTop - the scrollTop position of the viewport
    • scrollLeft - the scrollLeft position of the viewport
  • zoom Triggered when the zoom value changes. Event properties:
    • zoom - current zoom value
    • zoomMode - current zoom mode (string or null)
    • canZoomOut - whether the viewer is able to zoom out (boolean)
    • canZoomIn - whether the viewer is able to zoom in (boolean)

Page Events

  • pagefocus Triggered whenever a new page is scrolled into view. Event properties:
    • page - page number
    • numPages - total number of pages in the document
    • visiblePages - an array of page numbers that are currently (fully or partially) visible in the viewport
  • pageload Triggered whenever a page is loaded. Event properties:
    • page - page number
  • pageunload Triggered whenever a page is unloaded to improve performance. Event properties:
    • page - page number
  • pagefail Triggered if a page fails to load. Event properties:
    • error - the error details
    • page - page number of the failed page

Setting the Layout Mode

Built-in Layout Modes

You can set a layout initially via the configuration object:

var viewer = Crocodoc.createViewer('.viewer', {
    url: 'https://view-api.box.com/1/sessions/<session_id>/assets/',
    layout: Crocodoc.LAYOUT_HORIZONTAL
});

Or via the setLayout method:

viewer.setLayout(Crocodoc.LAYOUT_PRESENTATION);

The currently supported layouts are: * Crocodoc.LAYOUT_VERTICAL Pages are scrolled vertically and arranged into one or more columns depending upon the current zoom. * Crocodoc.LAYOUT_VERTICAL_SINGLE_COLUMN Pages are scrolled vertically and arranged into a single column even when zoomed out. * Crocodoc.LAYOUT_HORIZONTAL Pages within the viewer are horizontally and arranged into a single row. * Crocodoc.LAYOUT_PRESENTATION One page is shown at a time with no scrolling. Custom transitions may be used to switch between pages. * Crocodoc.LAYOUT_PRESENTATION_TWO_PAGE Two pages are shown at a time, side by side, with no scrolling. Custom transitions may be used to switch between pages.

Other Layout Modes

The above list are the modes that are included as part of the bundled viewer.js library. Here are some other layout modes that can be included alongside the library to add new layout functionality:

  • 'vertical-two-page' Behaves like Crocodoc.LAYOUT_PRESENTATION_TWO_PAGE for zooming and Crocodoc.LAYOUT_VERTICAL for scrolling (gist).
  • 'presentation-vertical' Behaves like Crocodoc.LAYOUT_PRESENTATION for page layout (e.g., one page visible at a time)

Core symbols most depended-on inside this repo

getStyle
called by 5
test/js/utilities/common-test.js
getFileInfoString
called by 4
Gruntfile.js
queuePagesToLoadInOrder
called by 4
src/js/components/lazy-loader.js
buildEventData
called by 3
src/js/components/scroller.js
getVendorCSSPropertyName
called by 3
src/js/utilities/support.js
getData
called by 3
plugins/realtime/realtime.js
handleScroll
called by 2
src/js/components/scroller.js
loadTextLayerHTML
called by 2
src/js/components/page-text.js

Shape

Function 125

Languages

TypeScript100%

Modules by API surface

src/js/utilities/ajax.js11 symbols
src/js/components/lazy-loader.js11 symbols
plugins/realtime/realtime.js8 symbols
plugins/fullscreen/fullscreen.js8 symbols
src/js/components/viewer-base.js7 symbols
src/js/components/scroller.js7 symbols
src/js/components/page-svg.js7 symbols
src/js/components/controller-paged.js7 symbols
examples/realtime/server.js6 symbols
src/js/components/resizer.js4 symbols
src/js/components/page-text.js4 symbols
examples/page-content-flip/page-content.js4 symbols

For agents

$ claude mcp add viewer.js \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact