aron / annotator.offline.js

Offline storage for the Annotator
http://github.com/aron/annotator.offline.js
MIT License
43 stars 9 forks source link

Offline Annotator Plugin

A plugin for the OKFN Annotator that stores all created annotations in local storage if it's supported by the current browser. The plugin will also fire "online" and "offline" events when the browsers connectivity changes.

There is a demo available online.

Usage

The plugin requires the annotator.js and annotator.css to be included on the page. See the annotator Getting Started guide for instructions then simply include the annotator.offline.js file in your page. This can be downloaded from the GitHub download page.

<link rel="stylesheet" href="https://github.com/aron/annotator.offline.js/blob/master/annotator.css" />
<script src="https://github.com/aron/annotator.offline.js/raw/master/jquery.js"></script>
<script src="https://github.com/aron/annotator.offline.js/raw/master/annotator.js"></script>
<script src="https://github.com/aron/annotator.offline.js/raw/master/annotator.offline.js"></script>

Then set up the annotator as usual calling "addPlugin".

jQuery('#content').annotator().annotator("addPlugin", "Offline", {
  online: function (plugin) {
    // Do something when the browser is online.
  },
  offline: function (plugin) {
    // Do something when the browser goes offline.
  }
});

Options

Loading Annotations Conditionally

If you have a single page application (such as an ebook reader) you may wish for finer grained control over which annotations are loaded. To do this you can use a combination of setAnnotationData and shouldLoadAnnotation options:

jQuery('#content').annotator().annotator("addPlugin", "Offline", {
  setAnnotationData: function (ann) {
    // Add page specific data to the annotation on creation.
    if (!ann.page) {
      ann.page = getCurrentPage(); // getCurrentPage() would return the current page number
    }
  },
  shouldLoadAnnotation: function (ann) {
    // Return true if the annotation should be loaded into the current view.
    return ann.page === getCurrentPage();
  }
});

API

There are various events available on the Offline plugin that can be used in your own code.

Events

Events can be subscribed to on the annotator or offline plugin object.

// Get the annotator by calling .data() on the selector it was called on.
var annotator = $("#content").data("annotator");

// Sync with server when online.
annotator.subscribe("online", function (plugin) {
  syncAnnotationsWithServer(plugin.annotations());
});

// Convert timestamp to Date object on load.
annotator.subscribe("beforeAnnotationLoaded", function (annotation) {
  var date = new Date();
  date.setTime(Date.parse(annotation.timestamp));
  annotation.timestamp = date;
});

Development

If you're interested in developing the plugin. You can install the developer dependancies by running the following command in the base directory:

$ npm install .

Development requires node and npm binaries to be intalled on your system. It was developed with node --version 0.6.6 and npm --version 1.1.0 -beta-4. Details on installation can be found on the node website.

Then visit http://localhost:8000 in your browser.

There is a Cakefile containing useful commands included.

$ cake serve # serves the directory at http://localhost:8000 (requires python)
$ cake test  # opens the test suite in your browser
$ cake watch # compiles .coffee files into lib/*.js when they change
$ cake build # creates a production pkg/annotator.offline.js file
$ cake pkg   # creates a zip file of production files

Repositories

The development branch should always contain the latest version of the plugin but it is not guaranteed to be in working order. The master branch should always have the latest stable code and each release can be found under an appropriately versioned tag.

Testing

Unit tests are located in the test/ directory and can be run by visiting http://localhost:8000/test/index.html in your browser.

Frameworks

The plugin uses the following libraries for development:

License

This plugin was commissioned and open sourced by Compendio.

Copyright 2012, Compendio Bildungsmedien AG Neunbrunnenstrasse 50 8050 Zürich www.compendio.ch

Released under the MIT license