source: OKFNAnnotator (for Zope)/annotator_files/lib/plugin/tags.js @ 3:6356e78ccf5c

Last change on this file since 3:6356e78ccf5c was 3:6356e78ccf5c, checked in by casties, 12 years ago

new version contains Annotator JS files to be used with FilesystemSite?.

File size: 3.2 KB
Line 
1var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; },
2  __hasProp = Object.prototype.hasOwnProperty,
3  __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
5Annotator.Plugin.Tags = (function(_super) {
6
7  __extends(Tags, _super);
8
9  function Tags() {
10    this.setAnnotationTags = __bind(this.setAnnotationTags, this);
11    this.updateField = __bind(this.updateField, this);
12    Tags.__super__.constructor.apply(this, arguments);
13  }
14
15  Tags.prototype.options = {
16    parseTags: function(string) {
17      var tags;
18      string = $.trim(string);
19      tags = [];
20      if (string) tags = string.split(/\s+/);
21      return tags;
22    },
23    stringifyTags: function(array) {
24      return array.join(" ");
25    }
26  };
27
28  Tags.prototype.field = null;
29
30  Tags.prototype.input = null;
31
32  Tags.prototype.pluginInit = function() {
33    if (!Annotator.supported()) return;
34    this.field = this.annotator.editor.addField({
35      label: Annotator._t('Add some tags here') + '\u2026',
36      load: this.updateField,
37      submit: this.setAnnotationTags
38    });
39    this.annotator.viewer.addField({
40      load: this.updateViewer
41    });
42    if (this.annotator.plugins.Filter) {
43      this.annotator.plugins.Filter.addFilter({
44        label: Annotator._t('Tag'),
45        property: 'tags',
46        isFiltered: Annotator.Plugin.Tags.filterCallback
47      });
48    }
49    return this.input = $(this.field).find(':input');
50  };
51
52  Tags.prototype.parseTags = function(string) {
53    return this.options.parseTags(string);
54  };
55
56  Tags.prototype.stringifyTags = function(array) {
57    return this.options.stringifyTags(array);
58  };
59
60  Tags.prototype.updateField = function(field, annotation) {
61    var value;
62    value = '';
63    if (annotation.tags) value = this.stringifyTags(annotation.tags);
64    return this.input.val(value);
65  };
66
67  Tags.prototype.setAnnotationTags = function(field, annotation) {
68    return annotation.tags = this.parseTags(this.input.val());
69  };
70
71  Tags.prototype.updateViewer = function(field, annotation) {
72    field = $(field);
73    if (annotation.tags && $.isArray(annotation.tags) && annotation.tags.length) {
74      return field.addClass('annotator-tags').html(function() {
75        var string;
76        return string = $.map(annotation.tags, function(tag) {
77          return '<span class="annotator-tag">' + Annotator.$.escape(tag) + '</span>';
78        }).join(' ');
79      });
80    } else {
81      return field.remove();
82    }
83  };
84
85  return Tags;
86
87})(Annotator.Plugin);
88
89Annotator.Plugin.Tags.filterCallback = function(input, tags) {
90  var keyword, keywords, matches, tag, _i, _j, _len, _len2;
91  if (tags == null) tags = [];
92  matches = 0;
93  keywords = [];
94  if (input) {
95    keywords = input.split(/\s+/g);
96    for (_i = 0, _len = keywords.length; _i < _len; _i++) {
97      keyword = keywords[_i];
98      if (tags.length) {
99        for (_j = 0, _len2 = tags.length; _j < _len2; _j++) {
100          tag = tags[_j];
101          if (tag.indexOf(keyword) !== -1) matches += 1;
102        }
103      }
104    }
105  }
106  return matches === keywords.length;
107};
Note: See TracBrowser for help on using the repository browser.