comparison annotator_files/lib/viewer.js @ 3:6356e78ccf5c

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