diff annotator_files/lib/annotator.js @ 4:6979313586cf

new version of annotator.
author casties
date Mon, 27 Aug 2012 19:05:38 +0200
parents 6356e78ccf5c
children
line wrap: on
line diff
--- a/annotator_files/lib/annotator.js	Thu Apr 05 19:37:27 2012 +0200
+++ b/annotator_files/lib/annotator.js	Mon Aug 27 19:05:38 2012 +0200
@@ -1,7 +1,8 @@
-var Annotator, util, _Annotator,
+// Generated by CoffeeScript 1.3.3
+var Annotator, g, util, _Annotator, _ref,
   __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; },
-  __hasProp = Object.prototype.hasOwnProperty,
-  __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor; child.__super__ = parent.prototype; return child; };
+  __hasProp = {}.hasOwnProperty,
+  __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };
 
 util = {
   uuid: (function() {
@@ -16,6 +17,23 @@
       return this;
     })();
   },
+  maxZIndex: function($elements) {
+    var all, el;
+    all = (function() {
+      var _i, _len, _results;
+      _results = [];
+      for (_i = 0, _len = $elements.length; _i < _len; _i++) {
+        el = $elements[_i];
+        if ($(el).css('position') === 'static') {
+          _results.push(-1);
+        } else {
+          _results.push(parseInt($(el).css('z-index'), 10) || -1);
+        }
+      }
+      return _results;
+    })();
+    return Math.max.apply(Math, all);
+  },
   mousePosition: function(e, offsetEl) {
     var offset;
     offset = $(offsetEl).offset();
@@ -43,7 +61,6 @@
   };
 
   Annotator.prototype.html = {
-    hl: '<span class="annotator-hl"></span>',
     adder: '<div class="annotator-adder"><button>' + _t('Annotate') + '</button></div>',
     wrapper: '<div class="annotator-wrapper"></div>'
   };
@@ -68,29 +85,41 @@
 
   function Annotator(element, options) {
     this.onDeleteAnnotation = __bind(this.onDeleteAnnotation, this);
+
     this.onEditAnnotation = __bind(this.onEditAnnotation, this);
+
     this.onAdderClick = __bind(this.onAdderClick, this);
+
     this.onAdderMousedown = __bind(this.onAdderMousedown, this);
+
     this.onHighlightMouseover = __bind(this.onHighlightMouseover, this);
+
     this.checkForEndSelection = __bind(this.checkForEndSelection, this);
+
     this.checkForStartSelection = __bind(this.checkForStartSelection, this);
+
     this.clearViewerHideTimer = __bind(this.clearViewerHideTimer, this);
+
     this.startViewerHideTimer = __bind(this.startViewerHideTimer, this);
+
     this.showViewer = __bind(this.showViewer, this);
+
     this.onEditorSubmit = __bind(this.onEditorSubmit, this);
+
     this.onEditorHide = __bind(this.onEditorHide, this);
+
     this.showEditor = __bind(this.showEditor, this);
-    var name, src, _ref;
     Annotator.__super__.constructor.apply(this, arguments);
     this.plugins = {};
-    if (!Annotator.supported()) return this;
-    if (!this.options.readOnly) this._setupDocumentEvents();
+    if (!Annotator.supported()) {
+      return this;
+    }
+    if (!this.options.readOnly) {
+      this._setupDocumentEvents();
+    }
     this._setupWrapper()._setupViewer()._setupEditor();
-    _ref = this.html;
-    for (name in _ref) {
-      src = _ref[name];
-      if (name !== 'wrapper') this[name] = $(src).appendTo(this.wrapper).hide();
-    }
+    this._setupDynamicStyle();
+    this.adder = $(this.html.adder).appendTo(this.wrapper).hide();
   }
 
   Annotator.prototype._setupWrapper = function() {
@@ -146,6 +175,28 @@
     return this;
   };
 
+  Annotator.prototype._setupDynamicStyle = function() {
+    var max, sel, style, x;
+    style = $('#annotator-dynamic-style');
+    if (!style.length) {
+      style = $('<style id="annotator-dynamic-style"></style>').appendTo(document.head);
+    }
+    sel = '*' + ((function() {
+      var _i, _len, _ref, _results;
+      _ref = ['adder', 'outer', 'notice', 'filter'];
+      _results = [];
+      for (_i = 0, _len = _ref.length; _i < _len; _i++) {
+        x = _ref[_i];
+        _results.push(":not(.annotator-" + x + ")");
+      }
+      return _results;
+    })()).join('');
+    max = util.maxZIndex($(document.body).find(sel));
+    max = Math.max(max, 1000);
+    style.text([".annotator-adder, .annotator-outer, .annotator-notice {", "  z-index: " + (max + 20) + ";", "}", ".annotator-filter {", "  z-index: " + (max + 10) + ";", "}"].join("\n"));
+    return this;
+  };
+
   Annotator.prototype.getSelectedRanges = function() {
     var browserRange, i, normedRange, r, ranges, rangesToIgnore, selection, _i, _len;
     selection = util.getGlobal().getSelection();
@@ -153,13 +204,15 @@
     rangesToIgnore = [];
     if (!selection.isCollapsed) {
       ranges = (function() {
-        var _ref, _results;
+        var _i, _ref, _results;
         _results = [];
-        for (i = 0, _ref = selection.rangeCount; 0 <= _ref ? i < _ref : i > _ref; 0 <= _ref ? i++ : i--) {
+        for (i = _i = 0, _ref = selection.rangeCount; 0 <= _ref ? _i < _ref : _i > _ref; i = 0 <= _ref ? ++_i : --_i) {
           r = selection.getRangeAt(i);
           browserRange = new Range.BrowserRange(r);
           normedRange = browserRange.normalize().limit(this.wrapper[0]);
-          if (normedRange === null) rangesToIgnore.push(r);
+          if (normedRange === null) {
+            rangesToIgnore.push(r);
+          }
           _results.push(normedRange);
         }
         return _results;
@@ -171,7 +224,9 @@
       selection.addRange(r);
     }
     return $.grep(ranges, function(range) {
-      if (range) selection.addRange(range.toRange());
+      if (range) {
+        selection.addRange(range.toRange());
+      }
       return range;
     });
   };
@@ -184,36 +239,40 @@
   };
 
   Annotator.prototype.setupAnnotation = function(annotation, fireEvents) {
-    var normed, normedRanges, r, sniffed, _i, _len;
-    if (fireEvents == null) fireEvents = true;
+    var normed, normedRanges, r, root, _i, _j, _len, _len1, _ref;
+    if (fireEvents == null) {
+      fireEvents = true;
+    }
+    root = this.wrapper[0];
     annotation.ranges || (annotation.ranges = this.selectedRanges);
-    normedRanges = (function() {
-      var _i, _len, _ref, _results;
-      _ref = annotation.ranges;
-      _results = [];
-      for (_i = 0, _len = _ref.length; _i < _len; _i++) {
-        r = _ref[_i];
-        if (!(r != null)) continue;
-        sniffed = Range.sniff(r);
-        _results.push(sniffed.normalize(this.wrapper[0]));
+    normedRanges = [];
+    _ref = annotation.ranges;
+    for (_i = 0, _len = _ref.length; _i < _len; _i++) {
+      r = _ref[_i];
+      try {
+        normedRanges.push(Range.sniff(r).normalize(root));
+      } catch (e) {
+        if (e instanceof Range.RangeError) {
+          this.publish('rangeNormalizeFail', [annotation, r, e]);
+        } else {
+          throw e;
+        }
       }
-      return _results;
-    }).call(this);
-    normedRanges = $.grep(normedRanges, function(range) {
-      return range !== null;
-    });
+    }
     annotation.quote = [];
     annotation.ranges = [];
     annotation.highlights = [];
-    for (_i = 0, _len = normedRanges.length; _i < _len; _i++) {
-      normed = normedRanges[_i];
+    for (_j = 0, _len1 = normedRanges.length; _j < _len1; _j++) {
+      normed = normedRanges[_j];
       annotation.quote.push($.trim(normed.text()));
       annotation.ranges.push(normed.serialize(this.wrapper[0], '.annotator-hl'));
       $.merge(annotation.highlights, this.highlightRange(normed));
     }
     annotation.quote = annotation.quote.join(' / ');
     $(annotation.highlights).data('annotation', annotation);
-    if (fireEvents) this.publish('annotationCreated', [annotation]);
+    if (fireEvents) {
+      this.publish('annotationCreated', [annotation]);
+    }
     return annotation;
   };
 
@@ -237,10 +296,14 @@
   Annotator.prototype.loadAnnotations = function(annotations) {
     var clone, loader,
       _this = this;
-    if (annotations == null) annotations = [];
+    if (annotations == null) {
+      annotations = [];
+    }
     loader = function(annList) {
       var n, now, _i, _len;
-      if (annList == null) annList = [];
+      if (annList == null) {
+        annList = [];
+      }
       now = annList.splice(0, 10);
       for (_i = 0, _len = now.length; _i < _len; _i++) {
         n = now[_i];
@@ -249,13 +312,15 @@
       if (annList.length > 0) {
         return setTimeout((function() {
           return loader(annList);
-        }), 1);
+        }), 10);
       } else {
         return _this.publish('annotationsLoaded', [clone]);
       }
     };
     clone = annotations.slice();
-    if (annotations.length) loader(annotations);
+    if (annotations.length) {
+      loader(annotations);
+    }
     return this;
   };
 
@@ -267,20 +332,37 @@
     }
   };
 
-  Annotator.prototype.highlightRange = function(normedRange) {
-    var node, white, _i, _len, _ref, _results;
+  Annotator.prototype.highlightRange = function(normedRange, cssClass) {
+    var hl, node, white, _i, _len, _ref, _results;
+    if (cssClass == null) {
+      cssClass = 'annotator-hl';
+    }
     white = /^\s*$/;
+    hl = $("<span class='" + cssClass + "'></span>");
     _ref = normedRange.textNodes();
     _results = [];
     for (_i = 0, _len = _ref.length; _i < _len; _i++) {
       node = _ref[_i];
       if (!white.test(node.nodeValue)) {
-        _results.push($(node).wrapAll(this.hl).parent().show()[0]);
+        _results.push($(node).wrapAll(hl).parent().show()[0]);
       }
     }
     return _results;
   };
 
+  Annotator.prototype.highlightRanges = function(normedRanges, cssClass) {
+    var highlights, r, _i, _len;
+    if (cssClass == null) {
+      cssClass = 'annotator-hl';
+    }
+    highlights = [];
+    for (_i = 0, _len = normedRanges.length; _i < _len; _i++) {
+      r = normedRanges[_i];
+      $.merge(highlights, this.highlightRange(r, cssClass));
+    }
+    return highlights;
+  };
+
   Annotator.prototype.addPlugin = function(name, options) {
     var klass, _base;
     if (this.plugins[name]) {
@@ -303,6 +385,7 @@
   Annotator.prototype.showEditor = function(annotation, location) {
     this.editor.element.css(location);
     this.editor.load(annotation);
+    this.publish('annotationEditorShown', [this.editor, annotation]);
     return this;
   };
 
@@ -347,13 +430,20 @@
   Annotator.prototype.checkForEndSelection = function(event) {
     var container, range, _i, _len, _ref;
     this.mouseIsDown = false;
-    if (this.ignoreMouseup) return;
+    if (this.ignoreMouseup) {
+      return;
+    }
     this.selectedRanges = this.getSelectedRanges();
     _ref = this.selectedRanges;
     for (_i = 0, _len = _ref.length; _i < _len; _i++) {
       range = _ref[_i];
       container = range.commonAncestor;
-      if (this.isAnnotator(container)) return;
+      if ($(container).hasClass('annotator-hl')) {
+        container = $(container).parents('[class^=annotator-hl]')[0];
+      }
+      if (this.isAnnotator(container)) {
+        return;
+      }
     }
     if (event && this.selectedRanges.length) {
       return this.adder.css(util.mousePosition(event, this.wrapper[0])).show();
@@ -369,7 +459,9 @@
   Annotator.prototype.onHighlightMouseover = function(event) {
     var annotations;
     this.clearViewerHideTimer();
-    if (this.mouseIsDown || this.viewer.isShown()) return false;
+    if (this.mouseIsDown || this.viewer.isShown()) {
+      return false;
+    }
     annotations = $(event.target).parents('.annotator-hl').andSelf().map(function() {
       return $(this).data("annotation");
     });
@@ -377,15 +469,41 @@
   };
 
   Annotator.prototype.onAdderMousedown = function(event) {
-    if (event != null) event.preventDefault();
+    if (event != null) {
+      event.preventDefault();
+    }
     return this.ignoreMouseup = true;
   };
 
   Annotator.prototype.onAdderClick = function(event) {
-    var position;
-    if (event != null) event.preventDefault();
+    var highlights, position, r, ranges;
+    if (event != null) {
+      event.preventDefault();
+    }
     position = this.adder.position();
     this.adder.hide();
+    if (this.selectedRanges && this.selectedRanges.length) {
+      ranges = (function() {
+        var _i, _len, _ref, _results;
+        _ref = this.selectedRanges;
+        _results = [];
+        for (_i = 0, _len = _ref.length; _i < _len; _i++) {
+          r = _ref[_i];
+          _results.push(Range.sniff(r).normalize());
+        }
+        return _results;
+      }).call(this);
+      highlights = this.highlightRanges(ranges, 'annotator-hl annotator-hl-temporary');
+      this.editor.element.one('hide', function() {
+        var h, _i, _len, _results;
+        _results = [];
+        for (_i = 0, _len = highlights.length; _i < _len; _i++) {
+          h = highlights[_i];
+          _results.push($(h).replaceWith(h.childNodes));
+        }
+        return _results;
+      });
+    }
     return this.showEditor(this.createAnnotation(), position);
   };
 
@@ -419,6 +537,20 @@
 
 })(Delegator);
 
+g = util.getGlobal();
+
+if (!(((_ref = g.document) != null ? _ref.evaluate : void 0) != null)) {
+  $.getScript('http://assets.annotateit.org/vendor/xpath.min.js');
+}
+
+if (!(g.getSelection != null)) {
+  $.getScript('http://assets.annotateit.org/vendor/ierange.min.js');
+}
+
+if (!(g.JSON != null)) {
+  $.getScript('http://assets.annotateit.org/vendor/json2.min.js');
+}
+
 Annotator.$ = $;
 
 Annotator.Delegator = Delegator;