source: main/trunk/greenstone3/web/interfaces/oran/js/annotator/pkg/annotator.filter.min.js@ 24771

Last change on this file since 24771 was 24771, checked in by papitha, 13 years ago

Changes to JS

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