source: OKFNAnnotator (for Zope)/annotator_files/lib/plugin/filter.js @ 4:6979313586cf

Last change on this file since 4:6979313586cf was 4:6979313586cf, checked in by casties, 12 years ago

new version of annotator.

File size: 9.2 KB
Line 
1// Generated by CoffeeScript 1.3.3
2var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; },
3  __hasProp = {}.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; };
5
6Annotator.Plugin.Filter = (function(_super) {
7
8  __extends(Filter, _super);
9
10  Filter.prototype.events = {
11    ".annotator-filter-property input focus": "_onFilterFocus",
12    ".annotator-filter-property input blur": "_onFilterBlur",
13    ".annotator-filter-property input keyup": "_onFilterKeyup",
14    ".annotator-filter-previous click": "_onPreviousClick",
15    ".annotator-filter-next click": "_onNextClick",
16    ".annotator-filter-clear click": "_onClearClick"
17  };
18
19  Filter.prototype.classes = {
20    active: 'annotator-filter-active',
21    hl: {
22      hide: 'annotator-hl-filtered',
23      active: 'annotator-hl-active'
24    }
25  };
26
27  Filter.prototype.html = {
28    element: "<div class=\"annotator-filter\">\n  <strong>" + Annotator._t('Navigate:') + "</strong>\n<span class=\"annotator-filter-navigation\">\n  <button class=\"annotator-filter-previous\">" + Annotator._t('Previous') + "</button>\n<button class=\"annotator-filter-next\">" + Annotator._t('Next') + "</button>\n</span>\n<strong>" + Annotator._t('Filter by:') + "</strong>\n</div>",
29    filter: "<span class=\"annotator-filter-property\">\n  <label></label>\n  <input/>\n  <button class=\"annotator-filter-clear\">" + Annotator._t('Clear') + "</button>\n</span>"
30  };
31
32  Filter.prototype.options = {
33    appendTo: 'body',
34    filters: [],
35    addAnnotationFilter: true,
36    isFiltered: function(input, property) {
37      var keyword, _i, _len, _ref;
38      if (!(input && property)) {
39        return false;
40      }
41      _ref = input.split(/\s*/);
42      for (_i = 0, _len = _ref.length; _i < _len; _i++) {
43        keyword = _ref[_i];
44        if (property.indexOf(keyword) === -1) {
45          return false;
46        }
47      }
48      return true;
49    }
50  };
51
52  function Filter(element, options) {
53    this._onPreviousClick = __bind(this._onPreviousClick, this);
54
55    this._onNextClick = __bind(this._onNextClick, this);
56
57    this._onFilterKeyup = __bind(this._onFilterKeyup, this);
58
59    this._onFilterBlur = __bind(this._onFilterBlur, this);
60
61    this._onFilterFocus = __bind(this._onFilterFocus, this);
62
63    this.updateHighlights = __bind(this.updateHighlights, this);
64
65    var _base;
66    element = $(this.html.element).appendTo((options != null ? options.appendTo : void 0) || this.options.appendTo);
67    Filter.__super__.constructor.call(this, element, options);
68    (_base = this.options).filters || (_base.filters = []);
69    this.filter = $(this.html.filter);
70    this.filters = [];
71    this.current = 0;
72  }
73
74  Filter.prototype.pluginInit = function() {
75    var filter, _i, _len, _ref;
76    _ref = this.options.filters;
77    for (_i = 0, _len = _ref.length; _i < _len; _i++) {
78      filter = _ref[_i];
79      this.addFilter(filter);
80    }
81    this.updateHighlights();
82    this._setupListeners()._insertSpacer();
83    if (this.options.addAnnotationFilter === true) {
84      return this.addFilter({
85        label: Annotator._t('Annotation'),
86        property: 'text'
87      });
88    }
89  };
90
91  Filter.prototype._insertSpacer = function() {
92    var currentMargin, html;
93    html = $('html');
94    currentMargin = parseInt(html.css('padding-top'), 10) || 0;
95    html.css('padding-top', currentMargin + this.element.outerHeight());
96    return this;
97  };
98
99  Filter.prototype._setupListeners = function() {
100    var event, events, _i, _len;
101    events = ['annotationsLoaded', 'annotationCreated', 'annotationUpdated', 'annotationDeleted'];
102    for (_i = 0, _len = events.length; _i < _len; _i++) {
103      event = events[_i];
104      this.annotator.subscribe(event, this.updateHighlights);
105    }
106    return this;
107  };
108
109  Filter.prototype.addFilter = function(options) {
110    var f, filter;
111    filter = $.extend({
112      label: '',
113      property: '',
114      isFiltered: this.options.isFiltered
115    }, options);
116    if (!((function() {
117      var _i, _len, _ref, _results;
118      _ref = this.filters;
119      _results = [];
120      for (_i = 0, _len = _ref.length; _i < _len; _i++) {
121        f = _ref[_i];
122        if (f.property === filter.property) {
123          _results.push(f);
124        }
125      }
126      return _results;
127    }).call(this)).length) {
128      filter.id = 'annotator-filter-' + filter.property;
129      filter.annotations = [];
130      filter.element = this.filter.clone().appendTo(this.element);
131      filter.element.find('label').html(filter.label).attr('for', filter.id);
132      filter.element.find('input').attr({
133        id: filter.id,
134        placeholder: Annotator._t('Filter by ') + filter.label + '\u2026'
135      });
136      filter.element.find('button').hide();
137      filter.element.data('filter', filter);
138      this.filters.push(filter);
139    }
140    return this;
141  };
142
143  Filter.prototype.updateFilter = function(filter) {
144    var annotation, annotations, input, property, _i, _len, _ref;
145    filter.annotations = [];
146    this.updateHighlights();
147    this.resetHighlights();
148    input = $.trim(filter.element.find('input').val());
149    if (input) {
150      annotations = this.highlights.map(function() {
151        return $(this).data('annotation');
152      });
153      _ref = $.makeArray(annotations);
154      for (_i = 0, _len = _ref.length; _i < _len; _i++) {
155        annotation = _ref[_i];
156        property = annotation[filter.property];
157        if (filter.isFiltered(input, property)) {
158          filter.annotations.push(annotation);
159        }
160      }
161      return this.filterHighlights();
162    }
163  };
164
165  Filter.prototype.updateHighlights = function() {
166    this.highlights = this.annotator.element.find('.annotator-hl:visible');
167    return this.filtered = this.highlights.not(this.classes.hl.hide);
168  };
169
170  Filter.prototype.filterHighlights = function() {
171    var activeFilters, annotation, annotations, filtered, highlights, index, uniques, _i, _len, _ref;
172    activeFilters = $.grep(this.filters, function(filter) {
173      return !!filter.annotations.length;
174    });
175    filtered = ((_ref = activeFilters[0]) != null ? _ref.annotations : void 0) || [];
176    if (activeFilters.length > 1) {
177      annotations = [];
178      $.each(activeFilters, function() {
179        return $.merge(annotations, this.annotations);
180      });
181      uniques = [];
182      filtered = [];
183      $.each(annotations, function() {
184        if ($.inArray(this, uniques) === -1) {
185          return uniques.push(this);
186        } else {
187          return filtered.push(this);
188        }
189      });
190    }
191    highlights = this.highlights;
192    for (index = _i = 0, _len = filtered.length; _i < _len; index = ++_i) {
193      annotation = filtered[index];
194      highlights = highlights.not(annotation.highlights);
195    }
196    highlights.addClass(this.classes.hl.hide);
197    this.filtered = this.highlights.not(this.classes.hl.hide);
198    return this;
199  };
200
201  Filter.prototype.resetHighlights = function() {
202    this.highlights.removeClass(this.classes.hl.hide);
203    this.filtered = this.highlights;
204    return this;
205  };
206
207  Filter.prototype._onFilterFocus = function(event) {
208    var input;
209    input = $(event.target);
210    input.parent().addClass(this.classes.active);
211    return input.next('button').show();
212  };
213
214  Filter.prototype._onFilterBlur = function(event) {
215    var input;
216    if (!event.target.value) {
217      input = $(event.target);
218      input.parent().removeClass(this.classes.active);
219      return input.next('button').hide();
220    }
221  };
222
223  Filter.prototype._onFilterKeyup = function(event) {
224    var filter;
225    filter = $(event.target).parent().data('filter');
226    if (filter) {
227      return this.updateFilter(filter);
228    }
229  };
230
231  Filter.prototype._findNextHighlight = function(previous) {
232    var active, annotation, current, index, next, offset, operator, resetOffset;
233    if (!this.highlights.length) {
234      return this;
235    }
236    offset = previous ? 0 : -1;
237    resetOffset = previous ? -1 : 0;
238    operator = previous ? 'lt' : 'gt';
239    active = this.highlights.not('.' + this.classes.hl.hide);
240    current = active.filter('.' + this.classes.hl.active);
241    if (!current.length) {
242      current = active.eq(offset);
243    }
244    annotation = current.data('annotation');
245    index = active.index(current[0]);
246    next = active.filter(":" + operator + "(" + index + ")").not(annotation.highlights).eq(resetOffset);
247    if (!next.length) {
248      next = active.eq(resetOffset);
249    }
250    return this._scrollToHighlight(next.data('annotation').highlights);
251  };
252
253  Filter.prototype._onNextClick = function(event) {
254    return this._findNextHighlight();
255  };
256
257  Filter.prototype._onPreviousClick = function(event) {
258    return this._findNextHighlight(true);
259  };
260
261  Filter.prototype._scrollToHighlight = function(highlight) {
262    highlight = $(highlight);
263    this.highlights.removeClass(this.classes.hl.active);
264    highlight.addClass(this.classes.hl.active);
265    return $('html, body').animate({
266      scrollTop: highlight.offset().top - (this.element.height() + 20)
267    }, 150);
268  };
269
270  Filter.prototype._onClearClick = function(event) {
271    return $(event.target).prev('input').val('').keyup().blur();
272  };
273
274  return Filter;
275
276})(Annotator.Plugin);
Note: See TracBrowser for help on using the repository browser.