Changeset 4:6979313586cf in OKFNAnnotator (for Zope) for annotator_files/lib/annotator.js


Ignore:
Timestamp:
Aug 27, 2012, 5:05:38 PM (12 years ago)
Author:
casties
Branch:
default
Message:

new version of annotator.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • annotator_files/lib/annotator.js

    r3 r4  
    1 var Annotator, util, _Annotator,
     1// Generated by CoffeeScript 1.3.3
     2var Annotator, g, util, _Annotator, _ref,
    23  __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; },
    3   __hasProp = Object.prototype.hasOwnProperty,
    4   __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; };
     4  __hasProp = {}.hasOwnProperty,
     5  __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; };
    56
    67util = {
     
    1718    })();
    1819  },
     20  maxZIndex: function($elements) {
     21    var all, el;
     22    all = (function() {
     23      var _i, _len, _results;
     24      _results = [];
     25      for (_i = 0, _len = $elements.length; _i < _len; _i++) {
     26        el = $elements[_i];
     27        if ($(el).css('position') === 'static') {
     28          _results.push(-1);
     29        } else {
     30          _results.push(parseInt($(el).css('z-index'), 10) || -1);
     31        }
     32      }
     33      return _results;
     34    })();
     35    return Math.max.apply(Math, all);
     36  },
    1937  mousePosition: function(e, offsetEl) {
    2038    var offset;
     
    4462
    4563  Annotator.prototype.html = {
    46     hl: '<span class="annotator-hl"></span>',
    4764    adder: '<div class="annotator-adder"><button>' + _t('Annotate') + '</button></div>',
    4865    wrapper: '<div class="annotator-wrapper"></div>'
     
    6986  function Annotator(element, options) {
    7087    this.onDeleteAnnotation = __bind(this.onDeleteAnnotation, this);
     88
    7189    this.onEditAnnotation = __bind(this.onEditAnnotation, this);
     90
    7291    this.onAdderClick = __bind(this.onAdderClick, this);
     92
    7393    this.onAdderMousedown = __bind(this.onAdderMousedown, this);
     94
    7495    this.onHighlightMouseover = __bind(this.onHighlightMouseover, this);
     96
    7597    this.checkForEndSelection = __bind(this.checkForEndSelection, this);
     98
    7699    this.checkForStartSelection = __bind(this.checkForStartSelection, this);
     100
    77101    this.clearViewerHideTimer = __bind(this.clearViewerHideTimer, this);
     102
    78103    this.startViewerHideTimer = __bind(this.startViewerHideTimer, this);
     104
    79105    this.showViewer = __bind(this.showViewer, this);
     106
    80107    this.onEditorSubmit = __bind(this.onEditorSubmit, this);
     108
    81109    this.onEditorHide = __bind(this.onEditorHide, this);
     110
    82111    this.showEditor = __bind(this.showEditor, this);
    83     var name, src, _ref;
    84112    Annotator.__super__.constructor.apply(this, arguments);
    85113    this.plugins = {};
    86     if (!Annotator.supported()) return this;
    87     if (!this.options.readOnly) this._setupDocumentEvents();
     114    if (!Annotator.supported()) {
     115      return this;
     116    }
     117    if (!this.options.readOnly) {
     118      this._setupDocumentEvents();
     119    }
    88120    this._setupWrapper()._setupViewer()._setupEditor();
    89     _ref = this.html;
    90     for (name in _ref) {
    91       src = _ref[name];
    92       if (name !== 'wrapper') this[name] = $(src).appendTo(this.wrapper).hide();
    93     }
     121    this._setupDynamicStyle();
     122    this.adder = $(this.html.adder).appendTo(this.wrapper).hide();
    94123  }
    95124
     
    147176  };
    148177
     178  Annotator.prototype._setupDynamicStyle = function() {
     179    var max, sel, style, x;
     180    style = $('#annotator-dynamic-style');
     181    if (!style.length) {
     182      style = $('<style id="annotator-dynamic-style"></style>').appendTo(document.head);
     183    }
     184    sel = '*' + ((function() {
     185      var _i, _len, _ref, _results;
     186      _ref = ['adder', 'outer', 'notice', 'filter'];
     187      _results = [];
     188      for (_i = 0, _len = _ref.length; _i < _len; _i++) {
     189        x = _ref[_i];
     190        _results.push(":not(.annotator-" + x + ")");
     191      }
     192      return _results;
     193    })()).join('');
     194    max = util.maxZIndex($(document.body).find(sel));
     195    max = Math.max(max, 1000);
     196    style.text([".annotator-adder, .annotator-outer, .annotator-notice {", "  z-index: " + (max + 20) + ";", "}", ".annotator-filter {", "  z-index: " + (max + 10) + ";", "}"].join("\n"));
     197    return this;
     198  };
     199
    149200  Annotator.prototype.getSelectedRanges = function() {
    150201    var browserRange, i, normedRange, r, ranges, rangesToIgnore, selection, _i, _len;
     
    154205    if (!selection.isCollapsed) {
    155206      ranges = (function() {
    156         var _ref, _results;
     207        var _i, _ref, _results;
    157208        _results = [];
    158         for (i = 0, _ref = selection.rangeCount; 0 <= _ref ? i < _ref : i > _ref; 0 <= _ref ? i++ : i--) {
     209        for (i = _i = 0, _ref = selection.rangeCount; 0 <= _ref ? _i < _ref : _i > _ref; i = 0 <= _ref ? ++_i : --_i) {
    159210          r = selection.getRangeAt(i);
    160211          browserRange = new Range.BrowserRange(r);
    161212          normedRange = browserRange.normalize().limit(this.wrapper[0]);
    162           if (normedRange === null) rangesToIgnore.push(r);
     213          if (normedRange === null) {
     214            rangesToIgnore.push(r);
     215          }
    163216          _results.push(normedRange);
    164217        }
     
    172225    }
    173226    return $.grep(ranges, function(range) {
    174       if (range) selection.addRange(range.toRange());
     227      if (range) {
     228        selection.addRange(range.toRange());
     229      }
    175230      return range;
    176231    });
     
    185240
    186241  Annotator.prototype.setupAnnotation = function(annotation, fireEvents) {
    187     var normed, normedRanges, r, sniffed, _i, _len;
    188     if (fireEvents == null) fireEvents = true;
     242    var normed, normedRanges, r, root, _i, _j, _len, _len1, _ref;
     243    if (fireEvents == null) {
     244      fireEvents = true;
     245    }
     246    root = this.wrapper[0];
    189247    annotation.ranges || (annotation.ranges = this.selectedRanges);
    190     normedRanges = (function() {
    191       var _i, _len, _ref, _results;
    192       _ref = annotation.ranges;
    193       _results = [];
    194       for (_i = 0, _len = _ref.length; _i < _len; _i++) {
    195         r = _ref[_i];
    196         if (!(r != null)) continue;
    197         sniffed = Range.sniff(r);
    198         _results.push(sniffed.normalize(this.wrapper[0]));
    199       }
    200       return _results;
    201     }).call(this);
    202     normedRanges = $.grep(normedRanges, function(range) {
    203       return range !== null;
    204     });
     248    normedRanges = [];
     249    _ref = annotation.ranges;
     250    for (_i = 0, _len = _ref.length; _i < _len; _i++) {
     251      r = _ref[_i];
     252      try {
     253        normedRanges.push(Range.sniff(r).normalize(root));
     254      } catch (e) {
     255        if (e instanceof Range.RangeError) {
     256          this.publish('rangeNormalizeFail', [annotation, r, e]);
     257        } else {
     258          throw e;
     259        }
     260      }
     261    }
    205262    annotation.quote = [];
    206263    annotation.ranges = [];
    207264    annotation.highlights = [];
    208     for (_i = 0, _len = normedRanges.length; _i < _len; _i++) {
    209       normed = normedRanges[_i];
     265    for (_j = 0, _len1 = normedRanges.length; _j < _len1; _j++) {
     266      normed = normedRanges[_j];
    210267      annotation.quote.push($.trim(normed.text()));
    211268      annotation.ranges.push(normed.serialize(this.wrapper[0], '.annotator-hl'));
     
    214271    annotation.quote = annotation.quote.join(' / ');
    215272    $(annotation.highlights).data('annotation', annotation);
    216     if (fireEvents) this.publish('annotationCreated', [annotation]);
     273    if (fireEvents) {
     274      this.publish('annotationCreated', [annotation]);
     275    }
    217276    return annotation;
    218277  };
     
    238297    var clone, loader,
    239298      _this = this;
    240     if (annotations == null) annotations = [];
     299    if (annotations == null) {
     300      annotations = [];
     301    }
    241302    loader = function(annList) {
    242303      var n, now, _i, _len;
    243       if (annList == null) annList = [];
     304      if (annList == null) {
     305        annList = [];
     306      }
    244307      now = annList.splice(0, 10);
    245308      for (_i = 0, _len = now.length; _i < _len; _i++) {
     
    250313        return setTimeout((function() {
    251314          return loader(annList);
    252         }), 1);
     315        }), 10);
    253316      } else {
    254317        return _this.publish('annotationsLoaded', [clone]);
     
    256319    };
    257320    clone = annotations.slice();
    258     if (annotations.length) loader(annotations);
     321    if (annotations.length) {
     322      loader(annotations);
     323    }
    259324    return this;
    260325  };
     
    268333  };
    269334
    270   Annotator.prototype.highlightRange = function(normedRange) {
    271     var node, white, _i, _len, _ref, _results;
     335  Annotator.prototype.highlightRange = function(normedRange, cssClass) {
     336    var hl, node, white, _i, _len, _ref, _results;
     337    if (cssClass == null) {
     338      cssClass = 'annotator-hl';
     339    }
    272340    white = /^\s*$/;
     341    hl = $("<span class='" + cssClass + "'></span>");
    273342    _ref = normedRange.textNodes();
    274343    _results = [];
     
    276345      node = _ref[_i];
    277346      if (!white.test(node.nodeValue)) {
    278         _results.push($(node).wrapAll(this.hl).parent().show()[0]);
     347        _results.push($(node).wrapAll(hl).parent().show()[0]);
    279348      }
    280349    }
    281350    return _results;
     351  };
     352
     353  Annotator.prototype.highlightRanges = function(normedRanges, cssClass) {
     354    var highlights, r, _i, _len;
     355    if (cssClass == null) {
     356      cssClass = 'annotator-hl';
     357    }
     358    highlights = [];
     359    for (_i = 0, _len = normedRanges.length; _i < _len; _i++) {
     360      r = normedRanges[_i];
     361      $.merge(highlights, this.highlightRange(r, cssClass));
     362    }
     363    return highlights;
    282364  };
    283365
     
    304386    this.editor.element.css(location);
    305387    this.editor.load(annotation);
     388    this.publish('annotationEditorShown', [this.editor, annotation]);
    306389    return this;
    307390  };
     
    348431    var container, range, _i, _len, _ref;
    349432    this.mouseIsDown = false;
    350     if (this.ignoreMouseup) return;
     433    if (this.ignoreMouseup) {
     434      return;
     435    }
    351436    this.selectedRanges = this.getSelectedRanges();
    352437    _ref = this.selectedRanges;
     
    354439      range = _ref[_i];
    355440      container = range.commonAncestor;
    356       if (this.isAnnotator(container)) return;
     441      if ($(container).hasClass('annotator-hl')) {
     442        container = $(container).parents('[class^=annotator-hl]')[0];
     443      }
     444      if (this.isAnnotator(container)) {
     445        return;
     446      }
    357447    }
    358448    if (event && this.selectedRanges.length) {
     
    370460    var annotations;
    371461    this.clearViewerHideTimer();
    372     if (this.mouseIsDown || this.viewer.isShown()) return false;
     462    if (this.mouseIsDown || this.viewer.isShown()) {
     463      return false;
     464    }
    373465    annotations = $(event.target).parents('.annotator-hl').andSelf().map(function() {
    374466      return $(this).data("annotation");
     
    378470
    379471  Annotator.prototype.onAdderMousedown = function(event) {
    380     if (event != null) event.preventDefault();
     472    if (event != null) {
     473      event.preventDefault();
     474    }
    381475    return this.ignoreMouseup = true;
    382476  };
    383477
    384478  Annotator.prototype.onAdderClick = function(event) {
    385     var position;
    386     if (event != null) event.preventDefault();
     479    var highlights, position, r, ranges;
     480    if (event != null) {
     481      event.preventDefault();
     482    }
    387483    position = this.adder.position();
    388484    this.adder.hide();
     485    if (this.selectedRanges && this.selectedRanges.length) {
     486      ranges = (function() {
     487        var _i, _len, _ref, _results;
     488        _ref = this.selectedRanges;
     489        _results = [];
     490        for (_i = 0, _len = _ref.length; _i < _len; _i++) {
     491          r = _ref[_i];
     492          _results.push(Range.sniff(r).normalize());
     493        }
     494        return _results;
     495      }).call(this);
     496      highlights = this.highlightRanges(ranges, 'annotator-hl annotator-hl-temporary');
     497      this.editor.element.one('hide', function() {
     498        var h, _i, _len, _results;
     499        _results = [];
     500        for (_i = 0, _len = highlights.length; _i < _len; _i++) {
     501          h = highlights[_i];
     502          _results.push($(h).replaceWith(h.childNodes));
     503        }
     504        return _results;
     505      });
     506    }
    389507    return this.showEditor(this.createAnnotation(), position);
    390508  };
     
    420538})(Delegator);
    421539
     540g = util.getGlobal();
     541
     542if (!(((_ref = g.document) != null ? _ref.evaluate : void 0) != null)) {
     543  $.getScript('http://assets.annotateit.org/vendor/xpath.min.js');
     544}
     545
     546if (!(g.getSelection != null)) {
     547  $.getScript('http://assets.annotateit.org/vendor/ierange.min.js');
     548}
     549
     550if (!(g.JSON != null)) {
     551  $.getScript('http://assets.annotateit.org/vendor/json2.min.js');
     552}
     553
    422554Annotator.$ = $;
    423555
Note: See TracChangeset for help on using the changeset viewer.