Changeset 4:6979313586cf in OKFNAnnotator (for Zope) for annotator_files/lib/annotator.js
- Timestamp:
- Aug 27, 2012, 5:05:38 PM (12 years ago)
- Branch:
- default
- 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 2 var Annotator, g, util, _Annotator, _ref, 2 3 __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; }; 5 6 6 7 util = { … … 17 18 })(); 18 19 }, 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 }, 19 37 mousePosition: function(e, offsetEl) { 20 38 var offset; … … 44 62 45 63 Annotator.prototype.html = { 46 hl: '<span class="annotator-hl"></span>',47 64 adder: '<div class="annotator-adder"><button>' + _t('Annotate') + '</button></div>', 48 65 wrapper: '<div class="annotator-wrapper"></div>' … … 69 86 function Annotator(element, options) { 70 87 this.onDeleteAnnotation = __bind(this.onDeleteAnnotation, this); 88 71 89 this.onEditAnnotation = __bind(this.onEditAnnotation, this); 90 72 91 this.onAdderClick = __bind(this.onAdderClick, this); 92 73 93 this.onAdderMousedown = __bind(this.onAdderMousedown, this); 94 74 95 this.onHighlightMouseover = __bind(this.onHighlightMouseover, this); 96 75 97 this.checkForEndSelection = __bind(this.checkForEndSelection, this); 98 76 99 this.checkForStartSelection = __bind(this.checkForStartSelection, this); 100 77 101 this.clearViewerHideTimer = __bind(this.clearViewerHideTimer, this); 102 78 103 this.startViewerHideTimer = __bind(this.startViewerHideTimer, this); 104 79 105 this.showViewer = __bind(this.showViewer, this); 106 80 107 this.onEditorSubmit = __bind(this.onEditorSubmit, this); 108 81 109 this.onEditorHide = __bind(this.onEditorHide, this); 110 82 111 this.showEditor = __bind(this.showEditor, this); 83 var name, src, _ref;84 112 Annotator.__super__.constructor.apply(this, arguments); 85 113 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 } 88 120 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(); 94 123 } 95 124 … … 147 176 }; 148 177 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 149 200 Annotator.prototype.getSelectedRanges = function() { 150 201 var browserRange, i, normedRange, r, ranges, rangesToIgnore, selection, _i, _len; … … 154 205 if (!selection.isCollapsed) { 155 206 ranges = (function() { 156 var _ ref, _results;207 var _i, _ref, _results; 157 208 _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) { 159 210 r = selection.getRangeAt(i); 160 211 browserRange = new Range.BrowserRange(r); 161 212 normedRange = browserRange.normalize().limit(this.wrapper[0]); 162 if (normedRange === null) rangesToIgnore.push(r); 213 if (normedRange === null) { 214 rangesToIgnore.push(r); 215 } 163 216 _results.push(normedRange); 164 217 } … … 172 225 } 173 226 return $.grep(ranges, function(range) { 174 if (range) selection.addRange(range.toRange()); 227 if (range) { 228 selection.addRange(range.toRange()); 229 } 175 230 return range; 176 231 }); … … 185 240 186 241 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]; 189 247 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 } 205 262 annotation.quote = []; 206 263 annotation.ranges = []; 207 264 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]; 210 267 annotation.quote.push($.trim(normed.text())); 211 268 annotation.ranges.push(normed.serialize(this.wrapper[0], '.annotator-hl')); … … 214 271 annotation.quote = annotation.quote.join(' / '); 215 272 $(annotation.highlights).data('annotation', annotation); 216 if (fireEvents) this.publish('annotationCreated', [annotation]); 273 if (fireEvents) { 274 this.publish('annotationCreated', [annotation]); 275 } 217 276 return annotation; 218 277 }; … … 238 297 var clone, loader, 239 298 _this = this; 240 if (annotations == null) annotations = []; 299 if (annotations == null) { 300 annotations = []; 301 } 241 302 loader = function(annList) { 242 303 var n, now, _i, _len; 243 if (annList == null) annList = []; 304 if (annList == null) { 305 annList = []; 306 } 244 307 now = annList.splice(0, 10); 245 308 for (_i = 0, _len = now.length; _i < _len; _i++) { … … 250 313 return setTimeout((function() { 251 314 return loader(annList); 252 }), 1 );315 }), 10); 253 316 } else { 254 317 return _this.publish('annotationsLoaded', [clone]); … … 256 319 }; 257 320 clone = annotations.slice(); 258 if (annotations.length) loader(annotations); 321 if (annotations.length) { 322 loader(annotations); 323 } 259 324 return this; 260 325 }; … … 268 333 }; 269 334 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 } 272 340 white = /^\s*$/; 341 hl = $("<span class='" + cssClass + "'></span>"); 273 342 _ref = normedRange.textNodes(); 274 343 _results = []; … … 276 345 node = _ref[_i]; 277 346 if (!white.test(node.nodeValue)) { 278 _results.push($(node).wrapAll( this.hl).parent().show()[0]);347 _results.push($(node).wrapAll(hl).parent().show()[0]); 279 348 } 280 349 } 281 350 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; 282 364 }; 283 365 … … 304 386 this.editor.element.css(location); 305 387 this.editor.load(annotation); 388 this.publish('annotationEditorShown', [this.editor, annotation]); 306 389 return this; 307 390 }; … … 348 431 var container, range, _i, _len, _ref; 349 432 this.mouseIsDown = false; 350 if (this.ignoreMouseup) return; 433 if (this.ignoreMouseup) { 434 return; 435 } 351 436 this.selectedRanges = this.getSelectedRanges(); 352 437 _ref = this.selectedRanges; … … 354 439 range = _ref[_i]; 355 440 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 } 357 447 } 358 448 if (event && this.selectedRanges.length) { … … 370 460 var annotations; 371 461 this.clearViewerHideTimer(); 372 if (this.mouseIsDown || this.viewer.isShown()) return false; 462 if (this.mouseIsDown || this.viewer.isShown()) { 463 return false; 464 } 373 465 annotations = $(event.target).parents('.annotator-hl').andSelf().map(function() { 374 466 return $(this).data("annotation"); … … 378 470 379 471 Annotator.prototype.onAdderMousedown = function(event) { 380 if (event != null) event.preventDefault(); 472 if (event != null) { 473 event.preventDefault(); 474 } 381 475 return this.ignoreMouseup = true; 382 476 }; 383 477 384 478 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 } 387 483 position = this.adder.position(); 388 484 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 } 389 507 return this.showEditor(this.createAnnotation(), position); 390 508 }; … … 420 538 })(Delegator); 421 539 540 g = util.getGlobal(); 541 542 if (!(((_ref = g.document) != null ? _ref.evaluate : void 0) != null)) { 543 $.getScript('http://assets.annotateit.org/vendor/xpath.min.js'); 544 } 545 546 if (!(g.getSelection != null)) { 547 $.getScript('http://assets.annotateit.org/vendor/ierange.min.js'); 548 } 549 550 if (!(g.JSON != null)) { 551 $.getScript('http://assets.annotateit.org/vendor/json2.min.js'); 552 } 553 422 554 Annotator.$ = $; 423 555
Note: See TracChangeset
for help on using the changeset viewer.