source: OKFNAnnotator (for Zope)/annotator_files/lib/viewer.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: 5.7 KB
Line 
1// Generated by CoffeeScript 1.3.3
2var LinkParser,
3  __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; },
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; };
6
7Annotator.Viewer = (function(_super) {
8
9  __extends(Viewer, _super);
10
11  Viewer.prototype.events = {
12    ".annotator-edit click": "onEditClick",
13    ".annotator-delete click": "onDeleteClick"
14  };
15
16  Viewer.prototype.classes = {
17    hide: 'annotator-hide',
18    showControls: 'annotator-visible'
19  };
20
21  Viewer.prototype.html = {
22    element: "<div class=\"annotator-outer annotator-viewer\">\n  <ul class=\"annotator-widget annotator-listing\"></ul>\n</div>",
23    item: "<li class=\"annotator-annotation annotator-item\">\n  <span class=\"annotator-controls\">\n    <a href=\"#\" title=\"View as webpage\" class=\"annotator-link\">View as webpage</a>\n    <button title=\"Edit\" class=\"annotator-edit\">Edit</button>\n    <button title=\"Delete\" class=\"annotator-delete\">Delete</button>\n  </span>\n</li>"
24  };
25
26  Viewer.prototype.options = {
27    readOnly: false
28  };
29
30  function Viewer(options) {
31    this.onDeleteClick = __bind(this.onDeleteClick, this);
32
33    this.onEditClick = __bind(this.onEditClick, this);
34
35    this.load = __bind(this.load, this);
36
37    this.hide = __bind(this.hide, this);
38
39    this.show = __bind(this.show, this);
40    Viewer.__super__.constructor.call(this, $(this.html.element)[0], options);
41    this.item = $(this.html.item)[0];
42    this.fields = [];
43    this.annotations = [];
44  }
45
46  Viewer.prototype.show = function(event) {
47    var controls,
48      _this = this;
49    util.preventEventDefault(event);
50    controls = this.element.find('.annotator-controls').addClass(this.classes.showControls);
51    setTimeout((function() {
52      return controls.removeClass(_this.classes.showControls);
53    }), 500);
54    this.element.removeClass(this.classes.hide);
55    return this.checkOrientation().publish('show');
56  };
57
58  Viewer.prototype.isShown = function() {
59    return !this.element.hasClass(this.classes.hide);
60  };
61
62  Viewer.prototype.hide = function(event) {
63    util.preventEventDefault(event);
64    this.element.addClass(this.classes.hide);
65    return this.publish('hide');
66  };
67
68  Viewer.prototype.load = function(annotations) {
69    var annotation, controller, controls, del, edit, element, field, item, link, links, list, _i, _j, _len, _len1, _ref, _ref1;
70    this.annotations = annotations || [];
71    list = this.element.find('ul:first').empty();
72    _ref = this.annotations;
73    for (_i = 0, _len = _ref.length; _i < _len; _i++) {
74      annotation = _ref[_i];
75      item = $(this.item).clone().appendTo(list).data('annotation', annotation);
76      controls = item.find('.annotator-controls');
77      link = controls.find('.annotator-link');
78      edit = controls.find('.annotator-edit');
79      del = controls.find('.annotator-delete');
80      links = new LinkParser(annotation.links || []).get('alternate', {
81        'type': 'text/html'
82      });
83      if (links.length === 0 || !(links[0].href != null)) {
84        link.remove();
85      } else {
86        link.attr('href', links[0].href);
87      }
88      if (this.options.readOnly) {
89        edit.remove();
90        del.remove();
91      } else {
92        controller = {
93          showEdit: function() {
94            return edit.removeAttr('disabled');
95          },
96          hideEdit: function() {
97            return edit.attr('disabled', 'disabled');
98          },
99          showDelete: function() {
100            return del.removeAttr('disabled');
101          },
102          hideDelete: function() {
103            return del.attr('disabled', 'disabled');
104          }
105        };
106      }
107      _ref1 = this.fields;
108      for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) {
109        field = _ref1[_j];
110        element = $(field.element).clone().appendTo(item)[0];
111        field.load(element, annotation, controller);
112      }
113    }
114    this.publish('load', [this.annotations]);
115    return this.show();
116  };
117
118  Viewer.prototype.addField = function(options) {
119    var field;
120    field = $.extend({
121      load: function() {}
122    }, options);
123    field.element = $('<div />')[0];
124    this.fields.push(field);
125    field.element;
126    return this;
127  };
128
129  Viewer.prototype.onEditClick = function(event) {
130    return this.onButtonClick(event, 'edit');
131  };
132
133  Viewer.prototype.onDeleteClick = function(event) {
134    return this.onButtonClick(event, 'delete');
135  };
136
137  Viewer.prototype.onButtonClick = function(event, type) {
138    var item;
139    item = $(event.target).parents('.annotator-annotation');
140    return this.publish(type, [item.data('annotation')]);
141  };
142
143  return Viewer;
144
145})(Annotator.Widget);
146
147LinkParser = (function() {
148
149  function LinkParser(data) {
150    this.data = data;
151  }
152
153  LinkParser.prototype.get = function(rel, cond) {
154    var d, k, keys, match, v, _i, _len, _ref, _results;
155    if (cond == null) {
156      cond = {};
157    }
158    cond = $.extend({}, cond, {
159      rel: rel
160    });
161    keys = (function() {
162      var _results;
163      _results = [];
164      for (k in cond) {
165        if (!__hasProp.call(cond, k)) continue;
166        v = cond[k];
167        _results.push(k);
168      }
169      return _results;
170    })();
171    _ref = this.data;
172    _results = [];
173    for (_i = 0, _len = _ref.length; _i < _len; _i++) {
174      d = _ref[_i];
175      match = keys.reduce((function(m, k) {
176        return m && (d[k] === cond[k]);
177      }), true);
178      if (match) {
179        _results.push(d);
180      } else {
181        continue;
182      }
183    }
184    return _results;
185  };
186
187  return LinkParser;
188
189})();
Note: See TracBrowser for help on using the repository browser.