aboutsummaryrefslogtreecommitdiff
path: root/tools/addon-sdk-1.12/examples/annotator/data/selector.js
blob: f42dbfa5cd22211472f65f2e2ac45f3fd9410bc3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

/*
The selector locates elements that are suitable for annotation and enables
the user to select them.

On 'mouseenter' events associated with <p> elements:
- if the selector is active and the element is not already annotated
- find the nearest ancestor which has an id attribute: this is supposed to
make identification of this element more accurate
- highlight the element
- bind 'click' for the element to send a message back to the add-on, including
all the information associated with the anchor.
*/

var matchedElement = null;
var originalBgColor = null;
var active = false;

function resetMatchedElement() {
  if (matchedElement) {
    matchedElement.css('background-color', originalBgColor);
    matchedElement.unbind('click.annotator');
  }
}

self.on('message', function onMessage(activation) {
  active = activation;
  if (!active) {
    resetMatchedElement();
  }
});

function getInnerText(element) {
  // jQuery.text() returns content of <script> tags, we need to ignore those
  var list = [];
  element.find("*").andSelf().contents()
    .filter(function () {
      return this.nodeType == 3 && this.parentNode.tagName != "SCRIPT";
    })
    .each(function () {
      list.push(this.nodeValue);
    });
  return list.join("");
}

$('*').mouseenter(function() {
  if (!active || $(this).hasClass('annotated')) {
    return;
  }
  resetMatchedElement();
  ancestor = $(this).closest("[id]");
  matchedElement = $(this).first();
  originalBgColor = matchedElement.css('background-color');
  matchedElement.css('background-color', 'yellow');
  matchedElement.bind('click.annotator', function(event) {
    event.stopPropagation();
    event.preventDefault();
    self.port.emit('show',
      [
        document.location.toString(),
        ancestor.attr("id"),
        getInnerText(matchedElement)
      ]
   );
  });
});

$('*').mouseout(function() {
  resetMatchedElement();
});