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

changeset 0
6474c204b198
equal deleted inserted replaced
-1:000000000000 0:92e1dd7d6e38
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5 /*
6 Locate anchors for annotations and prepare to display the annotations.
7
8 For each annotation, if its URL matches this page,
9 - get the ancestor whose ID matches the ID in the anchor
10 - look for a <p> element whose content contains the anchor text
11
12 That's considered a match. Then we:
13 - highlight the anchor element
14 - add an 'annotated' class to tell the selector to skip this element
15 - embed the annottion text as a new attribute
16
17 For all annotated elements:
18 - bind 'mouseenter' and 'mouseleave' events to the element, to send 'show'
19 and 'hide' messages back to the add-on.
20 */
21
22 self.on('message', function onMessage(annotations) {
23 annotations.forEach(
24 function(annotation) {
25 if(annotation.url == document.location.toString()) {
26 createAnchor(annotation);
27 }
28 });
29
30 $('.annotated').css('border', 'solid 3px yellow');
31
32 $('.annotated').bind('mouseenter', function(event) {
33 self.port.emit('show', $(this).attr('annotation'));
34 event.stopPropagation();
35 event.preventDefault();
36 });
37
38 $('.annotated').bind('mouseleave', function() {
39 self.port.emit('hide');
40 });
41 });
42
43
44 function createAnchor(annotation) {
45 annotationAnchorAncestor = $('#' + annotation.ancestorId)[0] || document.body;
46 annotationAnchor = $(annotationAnchorAncestor).parent().find(
47 ':contains("' + annotation.anchorText + '")').last();
48 annotationAnchor.addClass('annotated');
49 annotationAnchor.attr('annotation', annotation.annotationText);
50 }

mercurial