diff --git a/src/ui/filter.js b/src/ui/filter.js index 5e37c4513..29a8bf2ab 100644 --- a/src/ui/filter.js +++ b/src/ui/filter.js @@ -160,10 +160,14 @@ Filter.prototype.updateFilter = function (filter) { return; } - var annotations = this.highlights.map(function () { - return $(this).data('annotation'); - }); - annotations = $.makeArray(annotations); + var annotations = (function (highlights) { + var annotationsById = {}; + highlights.each(function () { + var a = $(this).data('annotation'); + annotationsById[a._local.id] = a; + }); + return $.map(annotationsById, function(a) { return a; }); + }(this.highlights)); for (var i = 0, len = annotations.length; i < len; i++) { var annotation = annotations[i], diff --git a/src/ui/highlighter.js b/src/ui/highlighter.js index 00f72c094..bc1b05a42 100644 --- a/src/ui/highlighter.js +++ b/src/ui/highlighter.js @@ -119,6 +119,15 @@ Highlighter.prototype.drawAll = function (annotations) { return p; }; +// local unique IDs for annotations, used to deduplicate when recovering them +// from highlights. +var id = (function () { + var counter = 0; + return function() { + return '_local_id_' + (++counter); + } +}()); + // Public: Draw highlights for the annotation. // // annotation - An annotation Object for which to draw highlights. @@ -144,6 +153,11 @@ Highlighter.prototype.draw = function (annotation) { if (!hasHighlights) { annotation._local.highlights = []; } + var hasLocalId = (typeof annotation._local.id !== 'undefined' && + annotation._local.id === null); + if (!hasLocalId) { + annotation._local.id = id(); + } for (var j = 0, jlen = normedRanges.length; j < jlen; j++) { var normed = normedRanges[j]; diff --git a/test/spec/ui/filter_spec.js b/test/spec/ui/filter_spec.js index 624297976..79d3f2c4f 100644 --- a/test/spec/ui/filter_spec.js +++ b/test/spec/ui/filter_spec.js @@ -125,10 +125,14 @@ describe('ui.filter.Filter', function () { }; annotations = [{text: 'cat'}, {text: 'dog'}, {text: 'car'}]; $.each(annotations, function(i, annotation) { - $('') - .text(annotation) - .data('annotation', annotation) - .appendTo(element); + this._local = {id: this.text}; + for (var j = 0; j < annotation.text.length; j++) { + var ch = annotation.text[j] + $('') + .text(ch) + .data('annotation', annotation) + .appendTo(element); + } }); plugin.filters = {'text': testFilter}; plugin.highlights = $(element).find('.annotator-hl');