123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266 |
- var widgets = require('sdk/widget');
- var pageMod = require('sdk/page-mod');
- var data = require('sdk/self').data;
- var panels = require('sdk/panel');
- var simpleStorage = require('sdk/simple-storage');
- var notifications = require("sdk/notifications");
- var annotatorIsOn = false;
- var selectors = [];
- var matchers = [];
- if (!simpleStorage.storage.annotations)
- simpleStorage.storage.annotations = [];
- function updateMatchers() {
- matchers.forEach(function (matcher) {
- matcher.postMessage(simpleStorage.storage.annotations);
- });
- }
- function Annotation(annotationText, anchor) {
- this.annotationText = annotationText;
- this.url = anchor[0];
- this.ancestorId = anchor[1];
- this.anchorText = anchor[2];
- }
- function handleNewAnnotation(annotationText, anchor) {
- var newAnnotation = new Annotation(annotationText, anchor);
- simpleStorage.storage.annotations.push(newAnnotation);
- updateMatchers();
- }
- function activateSelectors() {
- selectors.forEach(
- function (selector) {
- selector.postMessage(annotatorIsOn);
- });
- }
- function toggleActivation() {
- annotatorIsOn = !annotatorIsOn;
- activateSelectors();
- return annotatorIsOn;
- }
- function detachWorker(worker, workerArray) {
- var index = workerArray.indexOf(worker);
- if(index != -1) {
- workerArray.splice(index, 1);
- }
- }
- exports.main = function() {
- var widget = widgets.Widget({
- id: 'toggle-switch',
- label: 'Annotator',
- contentURL: data.url('widget/pencil-off.png'),
- contentScriptWhen: 'ready',
- contentScriptFile: data.url('widget/widget.js')
- });
- widget.port.on('left-click', function() {
- console.log('activate/deactivate');
- widget.contentURL = toggleActivation() ?
- data.url('widget/pencil-on.png') :
- data.url('widget/pencil-off.png');
- });
- widget.port.on('right-click', function() {
- console.log('show annotation list');
- annotationList.show();
- });
- var selector = pageMod.PageMod({
- include: ['*'],
- contentScriptWhen: 'ready',
- contentScriptFile: [data.url('jquery-1.4.2.min.js'),
- data.url('selector.js')],
- onAttach: function(worker) {
- worker.postMessage(annotatorIsOn);
- selectors.push(worker);
- worker.port.on('show', function(data) {
- annotationEditor.annotationAnchor = data;
- annotationEditor.show();
- });
- worker.on('detach', function () {
- detachWorker(this, selectors);
- });
- }
- });
- var annotationEditor = panels.Panel({
- width: 220,
- height: 220,
- contentURL: data.url('editor/annotation-editor.html'),
- contentScriptFile: data.url('editor/annotation-editor.js'),
- onMessage: function(annotationText) {
- if (annotationText)
- handleNewAnnotation(annotationText, this.annotationAnchor);
- annotationEditor.hide();
- },
- onShow: function() {
- this.postMessage('focus');
- }
- });
- var annotationList = panels.Panel({
- width: 420,
- height: 200,
- contentURL: data.url('list/annotation-list.html'),
- contentScriptFile: [data.url('jquery-1.4.2.min.js'),
- data.url('list/annotation-list.js')],
- contentScriptWhen: 'ready',
- onShow: function() {
- this.postMessage(simpleStorage.storage.annotations);
- },
- onMessage: function(message) {
- require('sdk/tabs').open(message);
- }
- });
- simpleStorage.on("OverQuota", function () {
- notifications.notify({
- title: 'Storage space exceeded',
- text: 'Removing recent annotations'});
- while (simpleStorage.quotaUsage > 1)
- simpleStorage.storage.annotations.pop();
- });
- var matcher = pageMod.PageMod({
- include: ['*'],
- contentScriptWhen: 'ready',
- contentScriptFile: [data.url('jquery-1.4.2.min.js'),
- data.url('matcher.js')],
- onAttach: function(worker) {
- if(simpleStorage.storage.annotations) {
- worker.postMessage(simpleStorage.storage.annotations);
- }
- worker.port.on('show', function(data) {
- annotation.content = data;
- annotation.show();
- });
- worker.port.on('hide', function() {
- annotation.content = null;
- annotation.hide();
- });
- worker.on('detach', function () {
- detachWorker(this, matchers);
- });
- matchers.push(worker);
- }
- });
- var annotation = panels.Panel({
- width: 200,
- height: 180,
- contentURL: data.url('annotation/annotation.html'),
- contentScriptFile: [data.url('jquery-1.4.2.min.js'),
- data.url('annotation/annotation.js')],
- contentScriptWhen: 'ready',
- onShow: function() {
- this.postMessage(this.content);
- }
- });
- }
|