1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/addon-sdk/source/examples/annotator/data/selector.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,73 @@ 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 +The selector locates elements that are suitable for annotation and enables 1.10 +the user to select them. 1.11 + 1.12 +On 'mouseenter' events associated with <p> elements: 1.13 +- if the selector is active and the element is not already annotated 1.14 +- find the nearest ancestor which has an id attribute: this is supposed to 1.15 +make identification of this element more accurate 1.16 +- highlight the element 1.17 +- bind 'click' for the element to send a message back to the add-on, including 1.18 +all the information associated with the anchor. 1.19 +*/ 1.20 + 1.21 +var matchedElement = null; 1.22 +var originalBgColor = null; 1.23 +var active = false; 1.24 + 1.25 +function resetMatchedElement() { 1.26 + if (matchedElement) { 1.27 + matchedElement.css('background-color', originalBgColor); 1.28 + matchedElement.unbind('click.annotator'); 1.29 + } 1.30 +} 1.31 + 1.32 +self.on('message', function onMessage(activation) { 1.33 + active = activation; 1.34 + if (!active) { 1.35 + resetMatchedElement(); 1.36 + } 1.37 +}); 1.38 + 1.39 +function getInnerText(element) { 1.40 + // jQuery.text() returns content of <script> tags, we need to ignore those 1.41 + var list = []; 1.42 + element.find("*").andSelf().contents() 1.43 + .filter(function () { 1.44 + return this.nodeType == 3 && this.parentNode.tagName != "SCRIPT"; 1.45 + }) 1.46 + .each(function () { 1.47 + list.push(this.nodeValue); 1.48 + }); 1.49 + return list.join(""); 1.50 +} 1.51 + 1.52 +$('*').mouseenter(function() { 1.53 + if (!active || $(this).hasClass('annotated')) { 1.54 + return; 1.55 + } 1.56 + resetMatchedElement(); 1.57 + ancestor = $(this).closest("[id]"); 1.58 + matchedElement = $(this).first(); 1.59 + originalBgColor = matchedElement.css('background-color'); 1.60 + matchedElement.css('background-color', 'yellow'); 1.61 + matchedElement.bind('click.annotator', function(event) { 1.62 + event.stopPropagation(); 1.63 + event.preventDefault(); 1.64 + self.port.emit('show', 1.65 + [ 1.66 + document.location.toString(), 1.67 + ancestor.attr("id"), 1.68 + getInnerText(matchedElement) 1.69 + ] 1.70 + ); 1.71 + }); 1.72 +}); 1.73 + 1.74 +$('*').mouseout(function() { 1.75 + resetMatchedElement(); 1.76 +});