|
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 The selector locates elements that are suitable for annotation and enables |
|
7 the user to select them. |
|
8 |
|
9 On 'mouseenter' events associated with <p> elements: |
|
10 - if the selector is active and the element is not already annotated |
|
11 - find the nearest ancestor which has an id attribute: this is supposed to |
|
12 make identification of this element more accurate |
|
13 - highlight the element |
|
14 - bind 'click' for the element to send a message back to the add-on, including |
|
15 all the information associated with the anchor. |
|
16 */ |
|
17 |
|
18 var matchedElement = null; |
|
19 var originalBgColor = null; |
|
20 var active = false; |
|
21 |
|
22 function resetMatchedElement() { |
|
23 if (matchedElement) { |
|
24 matchedElement.css('background-color', originalBgColor); |
|
25 matchedElement.unbind('click.annotator'); |
|
26 } |
|
27 } |
|
28 |
|
29 self.on('message', function onMessage(activation) { |
|
30 active = activation; |
|
31 if (!active) { |
|
32 resetMatchedElement(); |
|
33 } |
|
34 }); |
|
35 |
|
36 function getInnerText(element) { |
|
37 // jQuery.text() returns content of <script> tags, we need to ignore those |
|
38 var list = []; |
|
39 element.find("*").andSelf().contents() |
|
40 .filter(function () { |
|
41 return this.nodeType == 3 && this.parentNode.tagName != "SCRIPT"; |
|
42 }) |
|
43 .each(function () { |
|
44 list.push(this.nodeValue); |
|
45 }); |
|
46 return list.join(""); |
|
47 } |
|
48 |
|
49 $('*').mouseenter(function() { |
|
50 if (!active || $(this).hasClass('annotated')) { |
|
51 return; |
|
52 } |
|
53 resetMatchedElement(); |
|
54 ancestor = $(this).closest("[id]"); |
|
55 matchedElement = $(this).first(); |
|
56 originalBgColor = matchedElement.css('background-color'); |
|
57 matchedElement.css('background-color', 'yellow'); |
|
58 matchedElement.bind('click.annotator', function(event) { |
|
59 event.stopPropagation(); |
|
60 event.preventDefault(); |
|
61 self.port.emit('show', |
|
62 [ |
|
63 document.location.toString(), |
|
64 ancestor.attr("id"), |
|
65 getInnerText(matchedElement) |
|
66 ] |
|
67 ); |
|
68 }); |
|
69 }); |
|
70 |
|
71 $('*').mouseout(function() { |
|
72 resetMatchedElement(); |
|
73 }); |