Mercurial > hg > OKFNAnnotator
diff 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 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/annotator_files/lib/viewer.js Thu Apr 05 19:37:27 2012 +0200 @@ -0,0 +1,181 @@ +var LinkParser, + __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }, + __hasProp = Object.prototype.hasOwnProperty, + __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; }; + +Annotator.Viewer = (function(_super) { + + __extends(Viewer, _super); + + Viewer.prototype.events = { + ".annotator-edit click": "onEditClick", + ".annotator-delete click": "onDeleteClick" + }; + + Viewer.prototype.classes = { + hide: 'annotator-hide', + showControls: 'annotator-visible' + }; + + Viewer.prototype.html = { + element: "<div class=\"annotator-outer annotator-viewer\">\n <ul class=\"annotator-widget annotator-listing\"></ul>\n</div>", + 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>" + }; + + Viewer.prototype.options = { + readOnly: false + }; + + function Viewer(options) { + this.onDeleteClick = __bind(this.onDeleteClick, this); + this.onEditClick = __bind(this.onEditClick, this); + this.load = __bind(this.load, this); + this.hide = __bind(this.hide, this); + this.show = __bind(this.show, this); Viewer.__super__.constructor.call(this, $(this.html.element)[0], options); + this.item = $(this.html.item)[0]; + this.fields = []; + this.annotations = []; + } + + Viewer.prototype.show = function(event) { + var controls, + _this = this; + util.preventEventDefault(event); + controls = this.element.find('.annotator-controls').addClass(this.classes.showControls); + setTimeout((function() { + return controls.removeClass(_this.classes.showControls); + }), 500); + this.element.removeClass(this.classes.hide); + return this.checkOrientation().publish('show'); + }; + + Viewer.prototype.isShown = function() { + return !this.element.hasClass(this.classes.hide); + }; + + Viewer.prototype.hide = function(event) { + util.preventEventDefault(event); + this.element.addClass(this.classes.hide); + return this.publish('hide'); + }; + + Viewer.prototype.load = function(annotations) { + var annotation, controller, controls, del, edit, element, field, item, link, links, list, _i, _j, _len, _len2, _ref, _ref2; + this.annotations = annotations || []; + list = this.element.find('ul:first').empty(); + _ref = this.annotations; + for (_i = 0, _len = _ref.length; _i < _len; _i++) { + annotation = _ref[_i]; + item = $(this.item).clone().appendTo(list).data('annotation', annotation); + controls = item.find('.annotator-controls'); + link = controls.find('.annotator-link'); + edit = controls.find('.annotator-edit'); + del = controls.find('.annotator-delete'); + links = new LinkParser(annotation.links || []).get('alternate', { + 'type': 'text/html' + }); + if (links.length === 0 || !(links[0].href != null)) { + link.remove(); + } else { + link.attr('href', links[0].href); + } + if (this.options.readOnly) { + edit.remove(); + del.remove(); + } else { + controller = { + showEdit: function() { + return edit.removeAttr('disabled'); + }, + hideEdit: function() { + return edit.attr('disabled', 'disabled'); + }, + showDelete: function() { + return del.removeAttr('disabled'); + }, + hideDelete: function() { + return del.attr('disabled', 'disabled'); + } + }; + } + _ref2 = this.fields; + for (_j = 0, _len2 = _ref2.length; _j < _len2; _j++) { + field = _ref2[_j]; + element = $(field.element).clone().appendTo(item)[0]; + field.load(element, annotation, controller); + } + } + this.publish('load', [this.annotations]); + return this.show(); + }; + + Viewer.prototype.addField = function(options) { + var field; + field = $.extend({ + load: function() {} + }, options); + field.element = $('<div />')[0]; + this.fields.push(field); + field.element; + return this; + }; + + Viewer.prototype.onEditClick = function(event) { + return this.onButtonClick(event, 'edit'); + }; + + Viewer.prototype.onDeleteClick = function(event) { + return this.onButtonClick(event, 'delete'); + }; + + Viewer.prototype.onButtonClick = function(event, type) { + var item; + item = $(event.target).parents('.annotator-annotation'); + return this.publish(type, [item.data('annotation')]); + }; + + return Viewer; + +})(Annotator.Widget); + +LinkParser = (function() { + + function LinkParser(data) { + this.data = data; + } + + LinkParser.prototype.get = function(rel, cond) { + var d, k, keys, match, v, _i, _len, _ref, _results; + if (cond == null) cond = {}; + cond = $.extend({}, cond, { + rel: rel + }); + keys = (function() { + var _results; + _results = []; + for (k in cond) { + if (!__hasProp.call(cond, k)) continue; + v = cond[k]; + _results.push(k); + } + return _results; + })(); + _ref = this.data; + _results = []; + for (_i = 0, _len = _ref.length; _i < _len; _i++) { + d = _ref[_i]; + match = keys.reduce((function(m, k) { + return m && (d[k] === cond[k]); + }), true); + if (match) { + _results.push(d); + } else { + continue; + } + } + return _results; + }; + + return LinkParser; + +})();