scroller.js 2.76 KB
Newer Older
1 2 3
(function(define, undefined) {
    'use strict';
    define(['jquery', 'underscore', 'annotator_1.2.9'], function($, _, Annotator) {
4 5 6 7
    /**
     * Adds the Scroller Plugin which scrolls to a note with a certain id and
     * opens it.
     **/
8
        Annotator.Plugin.Scroller = function() {
9 10
        // Call the Annotator.Plugin constructor this sets up the element and
        // options properties.
11 12
            Annotator.Plugin.apply(this, arguments);
        };
13

14 15 16 17
        $.extend(Annotator.Plugin.Scroller.prototype, new Annotator.Plugin(), {
            getIdFromLocationHash: function() {
                return window.location.hash.substr(1);
            },
18

19 20
            pluginInit: function() {
                _.bindAll(this, 'onNotesLoaded');
21 22 23
            // If the page URL contains a hash, we could be coming from a click
            // on an anchor in the notes page. In that case, the hash is the id
            // of the note that has to be scrolled to and opened.
24 25 26 27
                if (this.getIdFromLocationHash()) {
                    this.annotator.subscribe('annotationsLoaded', this.onNotesLoaded);
                }
            },
28

29 30 31
            destroy: function() {
                this.annotator.unsubscribe('annotationsLoaded', this.onNotesLoaded);
            },
32

33 34 35 36
            onNotesLoaded: function(notes) {
                var hash = this.getIdFromLocationHash();
                this.annotator.logger.log('Scroller', {
                    'notes:': notes,
Eric Fischer committed
37
                    hash: hash
38 39
                });
                _.each(notes, function(note) {
Eric Fischer committed
40
                    var $highlight, offset;
41
                    if (note.id === hash && note.highlights.length) {
42 43 44 45
                    // Clear the page URL hash, it won't be needed once we've
                    // scrolled and opened the relevant note. And it would
                    // unnecessarily repeat the steps below if we come from
                    // another sequential.
46
                        window.location.hash = '';
Eric Fischer committed
47 48
                        $highlight = $(note.highlights[0]);
                        offset = $highlight.position();
49
                    // Open the note
50
                        this.annotator.showFrozenViewer([note], {
Eric Fischer committed
51 52
                            top: offset.top + 0.5 * $highlight.height(),
                            left: offset.left + 0.5 * $highlight.width()
53
                        });
54
                    // Freeze the viewer
55
                        this.annotator.freezeAll();
56
                    // Scroll to highlight
Eric Fischer committed
57
                        this.scrollIntoView($highlight);
58 59 60
                    }
                }, this);
            },
61

62 63 64 65
            scrollIntoView: function(highlight) {
                highlight.focus();
            }
        });
66 67
    });
}).call(this, define || RequireJS.define);