addon-sdk/source/examples/annotator/data/matcher.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/addon-sdk/source/examples/annotator/data/matcher.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,50 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +/*
     1.9 +Locate anchors for annotations and prepare to display the annotations.
    1.10 +
    1.11 +For each annotation, if its URL matches this page,
    1.12 +- get the ancestor whose ID matches the ID in the anchor
    1.13 +- look for a <p> element whose content contains the anchor text
    1.14 +
    1.15 +That's considered a match. Then we:
    1.16 +- highlight the anchor element
    1.17 +- add an 'annotated' class to tell the selector to skip this element
    1.18 +- embed the annottion text as a new attribute
    1.19 +
    1.20 +For all annotated elements:
    1.21 +- bind 'mouseenter' and 'mouseleave' events to the element, to send 'show'
    1.22 +  and 'hide' messages back to the add-on.
    1.23 +*/
    1.24 +
    1.25 +self.on('message', function onMessage(annotations) {
    1.26 +  annotations.forEach(
    1.27 +    function(annotation) {
    1.28 +      if(annotation.url == document.location.toString()) {
    1.29 +        createAnchor(annotation);
    1.30 +      }
    1.31 +  });
    1.32 +
    1.33 +  $('.annotated').css('border', 'solid 3px yellow');
    1.34 +
    1.35 +  $('.annotated').bind('mouseenter', function(event) {
    1.36 +    self.port.emit('show', $(this).attr('annotation'));
    1.37 +    event.stopPropagation();
    1.38 +    event.preventDefault();
    1.39 +  });
    1.40 +
    1.41 +  $('.annotated').bind('mouseleave', function() {
    1.42 +    self.port.emit('hide');
    1.43 +  });
    1.44 +});
    1.45 +
    1.46 +
    1.47 +function createAnchor(annotation) {
    1.48 +  annotationAnchorAncestor = $('#' + annotation.ancestorId)[0] || document.body;
    1.49 +  annotationAnchor = $(annotationAnchorAncestor).parent().find(
    1.50 +                     ':contains("' + annotation.anchorText + '")').last();
    1.51 +  annotationAnchor.addClass('annotated');
    1.52 +  annotationAnchor.attr('annotation', annotation.annotationText);
    1.53 +}

mercurial