annotate webapp/src/main/webapp/jquery/annotator-dl.js @ 1146:94a9c2379ebb

new shape annotations work now.
author robcast
date Fri, 23 Nov 2012 11:33:08 +0100
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1146
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1 /*
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2 ** Annotator 1.2.5-dev-a4cd304
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
3 ** https://github.com/okfn/annotator/
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
4 **
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
5 ** Copyright 2012 Aron Carroll, Rufus Pollock, and Nick Stenning.
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
6 ** Dual licensed under the MIT and GPLv3 licenses.
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
7 ** https://github.com/okfn/annotator/blob/master/LICENSE
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
8 **
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
9 ** Built at: 2012-11-23 09:46:08Z
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
10 */
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
11
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
12
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
13 (function() {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
14 var $, Annotator, Delegator, LinkParser, Range, base64Decode, base64UrlDecode, createDateFromISO8601, fn, functions, g, gettext, parseToken, util, _Annotator, _gettext, _i, _j, _len, _len1, _ref, _ref1, _t,
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
15 __slice = [].slice,
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
16 __hasProp = {}.hasOwnProperty,
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
17 __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; },
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
18 __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; },
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
19 __indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
20
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
21 gettext = null;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
22
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
23 if (typeof Gettext !== "undefined" && Gettext !== null) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
24 _gettext = new Gettext({
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
25 domain: "annotator"
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
26 });
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
27 gettext = function(msgid) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
28 return _gettext.gettext(msgid);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
29 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
30 } else {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
31 gettext = function(msgid) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
32 return msgid;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
33 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
34 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
35
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
36 _t = function(msgid) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
37 return gettext(msgid);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
38 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
39
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
40 if (!(typeof jQuery !== "undefined" && jQuery !== null ? (_ref = jQuery.fn) != null ? _ref.jquery : void 0 : void 0)) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
41 console.error(_t("Annotator requires jQuery: have you included lib/vendor/jquery.js?"));
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
42 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
43
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
44 if (!(JSON && JSON.parse && JSON.stringify)) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
45 console.error(_t("Annotator requires a JSON implementation: have you included lib/vendor/json2.js?"));
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
46 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
47
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
48 $ = jQuery.sub();
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
49
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
50 $.flatten = function(array) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
51 var flatten;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
52 flatten = function(ary) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
53 var el, flat, _i, _len;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
54 flat = [];
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
55 for (_i = 0, _len = ary.length; _i < _len; _i++) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
56 el = ary[_i];
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
57 flat = flat.concat(el && $.isArray(el) ? flatten(el) : el);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
58 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
59 return flat;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
60 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
61 return flatten(array);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
62 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
63
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
64 $.plugin = function(name, object) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
65 return jQuery.fn[name] = function(options) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
66 var args;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
67 args = Array.prototype.slice.call(arguments, 1);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
68 return this.each(function() {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
69 var instance;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
70 instance = $.data(this, name);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
71 if (instance) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
72 return options && instance[options].apply(instance, args);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
73 } else {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
74 instance = new object(this, options);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
75 return $.data(this, name, instance);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
76 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
77 });
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
78 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
79 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
80
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
81 $.fn.textNodes = function() {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
82 var getTextNodes;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
83 getTextNodes = function(node) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
84 var nodes;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
85 if (node && node.nodeType !== 3) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
86 nodes = [];
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
87 if (node.nodeType !== 8) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
88 node = node.lastChild;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
89 while (node) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
90 nodes.push(getTextNodes(node));
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
91 node = node.previousSibling;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
92 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
93 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
94 return nodes.reverse();
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
95 } else {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
96 return node;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
97 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
98 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
99 return this.map(function() {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
100 return $.flatten(getTextNodes(this));
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
101 });
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
102 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
103
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
104 $.fn.xpath = function(relativeRoot) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
105 var jq;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
106 jq = this.map(function() {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
107 var elem, idx, path;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
108 path = '';
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
109 elem = this;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
110 while (elem && elem.nodeType === 1 && elem !== relativeRoot) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
111 idx = $(elem.parentNode).children(elem.tagName).index(elem) + 1;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
112 idx = "[" + idx + "]";
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
113 path = "/" + elem.tagName.toLowerCase() + idx + path;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
114 elem = elem.parentNode;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
115 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
116 return path;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
117 });
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
118 return jq.get();
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
119 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
120
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
121 $.escape = function(html) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
122 return html.replace(/&(?!\w+;)/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;');
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
123 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
124
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
125 $.fn.escape = function(html) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
126 if (arguments.length) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
127 return this.html($.escape(html));
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
128 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
129 return this.html();
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
130 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
131
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
132 $.fn.reverse = []._reverse || [].reverse;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
133
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
134 functions = ["log", "debug", "info", "warn", "exception", "assert", "dir", "dirxml", "trace", "group", "groupEnd", "groupCollapsed", "time", "timeEnd", "profile", "profileEnd", "count", "clear", "table", "error", "notifyFirebug", "firebug", "userObjects"];
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
135
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
136 if (typeof console !== "undefined" && console !== null) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
137 if (!(console.group != null)) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
138 console.group = function(name) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
139 return console.log("GROUP: ", name);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
140 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
141 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
142 if (!(console.groupCollapsed != null)) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
143 console.groupCollapsed = console.group;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
144 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
145 for (_i = 0, _len = functions.length; _i < _len; _i++) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
146 fn = functions[_i];
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
147 if (!(console[fn] != null)) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
148 console[fn] = function() {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
149 return console.log(_t("Not implemented:") + (" console." + name));
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
150 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
151 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
152 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
153 } else {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
154 this.console = {};
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
155 for (_j = 0, _len1 = functions.length; _j < _len1; _j++) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
156 fn = functions[_j];
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
157 this.console[fn] = function() {};
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
158 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
159 this.console['error'] = function() {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
160 var args;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
161 args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
162 return alert("ERROR: " + (args.join(', ')));
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
163 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
164 this.console['warn'] = function() {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
165 var args;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
166 args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
167 return alert("WARNING: " + (args.join(', ')));
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
168 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
169 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
170
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
171 Delegator = (function() {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
172
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
173 Delegator.prototype.events = {};
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
174
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
175 Delegator.prototype.options = {};
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
176
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
177 Delegator.prototype.element = null;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
178
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
179 function Delegator(element, options) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
180 this.options = $.extend(true, {}, this.options, options);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
181 this.element = $(element);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
182 this.on = this.subscribe;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
183 this.addEvents();
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
184 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
185
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
186 Delegator.prototype.addEvents = function() {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
187 var event, functionName, sel, selector, _k, _ref1, _ref2, _results;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
188 _ref1 = this.events;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
189 _results = [];
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
190 for (sel in _ref1) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
191 functionName = _ref1[sel];
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
192 _ref2 = sel.split(' '), selector = 2 <= _ref2.length ? __slice.call(_ref2, 0, _k = _ref2.length - 1) : (_k = 0, []), event = _ref2[_k++];
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
193 _results.push(this.addEvent(selector.join(' '), event, functionName));
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
194 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
195 return _results;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
196 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
197
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
198 Delegator.prototype.addEvent = function(bindTo, event, functionName) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
199 var closure, isBlankSelector,
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
200 _this = this;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
201 closure = function() {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
202 return _this[functionName].apply(_this, arguments);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
203 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
204 isBlankSelector = typeof bindTo === 'string' && bindTo.replace(/\s+/g, '') === '';
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
205 if (isBlankSelector) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
206 bindTo = this.element;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
207 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
208 if (typeof bindTo === 'string') {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
209 this.element.delegate(bindTo, event, closure);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
210 } else {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
211 if (this.isCustomEvent(event)) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
212 this.subscribe(event, closure);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
213 } else {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
214 $(bindTo).bind(event, closure);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
215 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
216 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
217 return this;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
218 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
219
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
220 Delegator.prototype.isCustomEvent = function(event) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
221 event = event.split('.')[0];
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
222 return $.inArray(event, Delegator.natives) === -1;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
223 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
224
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
225 Delegator.prototype.publish = function() {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
226 this.element.triggerHandler.apply(this.element, arguments);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
227 return this;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
228 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
229
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
230 Delegator.prototype.subscribe = function(event, callback) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
231 var closure;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
232 closure = function() {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
233 return callback.apply(this, [].slice.call(arguments, 1));
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
234 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
235 closure.guid = callback.guid = ($.guid += 1);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
236 this.element.bind(event, closure);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
237 return this;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
238 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
239
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
240 Delegator.prototype.unsubscribe = function() {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
241 this.element.unbind.apply(this.element, arguments);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
242 return this;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
243 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
244
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
245 return Delegator;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
246
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
247 })();
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
248
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
249 Delegator.natives = (function() {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
250 var key, specials, val;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
251 specials = (function() {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
252 var _ref1, _results;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
253 _ref1 = jQuery.event.special;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
254 _results = [];
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
255 for (key in _ref1) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
256 if (!__hasProp.call(_ref1, key)) continue;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
257 val = _ref1[key];
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
258 _results.push(key);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
259 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
260 return _results;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
261 })();
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
262 return "blur focus focusin focusout load resize scroll unload click dblclick\nmousedown mouseup mousemove mouseover mouseout mouseenter mouseleave\nchange select submit keydown keypress keyup error".split(/[^a-z]+/).concat(specials);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
263 })();
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
264
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
265 Range = {};
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
266
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
267 Range.sniff = function(r) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
268 if (r.commonAncestorContainer != null) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
269 return new Range.BrowserRange(r);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
270 } else if (typeof r.start === "string") {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
271 return new Range.SerializedRange(r);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
272 } else if (r.start && typeof r.start === "object") {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
273 return new Range.NormalizedRange(r);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
274 } else {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
275 console.error(_t("Could not sniff range type"));
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
276 return false;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
277 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
278 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
279
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
280 Range.nodeFromXPath = function(xpath, root) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
281 var customResolver, evaluateXPath, namespace, node, segment;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
282 if (root == null) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
283 root = document;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
284 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
285 evaluateXPath = function(xp, nsResolver) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
286 if (nsResolver == null) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
287 nsResolver = null;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
288 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
289 return document.evaluate('.' + xp, root, nsResolver, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
290 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
291 if (!$.isXMLDoc(document.documentElement)) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
292 return evaluateXPath(xpath);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
293 } else {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
294 customResolver = document.createNSResolver(document.ownerDocument === null ? document.documentElement : document.ownerDocument.documentElement);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
295 node = evaluateXPath(xpath, customResolver);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
296 if (!node) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
297 xpath = ((function() {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
298 var _k, _len2, _ref1, _results;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
299 _ref1 = xpath.split('/');
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
300 _results = [];
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
301 for (_k = 0, _len2 = _ref1.length; _k < _len2; _k++) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
302 segment = _ref1[_k];
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
303 if (segment && segment.indexOf(':') === -1) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
304 _results.push(segment.replace(/^([a-z]+)/, 'xhtml:$1'));
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
305 } else {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
306 _results.push(segment);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
307 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
308 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
309 return _results;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
310 })()).join('/');
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
311 namespace = document.lookupNamespaceURI(null);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
312 customResolver = function(ns) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
313 if (ns === 'xhtml') {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
314 return namespace;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
315 } else {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
316 return document.documentElement.getAttribute('xmlns:' + ns);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
317 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
318 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
319 node = evaluateXPath(xpath, customResolver);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
320 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
321 return node;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
322 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
323 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
324
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
325 Range.RangeError = (function(_super) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
326
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
327 __extends(RangeError, _super);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
328
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
329 function RangeError(type, message, parent) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
330 this.type = type;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
331 this.message = message;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
332 this.parent = parent != null ? parent : null;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
333 RangeError.__super__.constructor.call(this, this.message);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
334 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
335
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
336 return RangeError;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
337
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
338 })(Error);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
339
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
340 Range.BrowserRange = (function() {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
341
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
342 function BrowserRange(obj) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
343 this.commonAncestorContainer = obj.commonAncestorContainer;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
344 this.startContainer = obj.startContainer;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
345 this.startOffset = obj.startOffset;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
346 this.endContainer = obj.endContainer;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
347 this.endOffset = obj.endOffset;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
348 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
349
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
350 BrowserRange.prototype.normalize = function(root) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
351 var it, node, nr, offset, p, r, _k, _len2, _ref1;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
352 if (this.tainted) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
353 console.error(_t("You may only call normalize() once on a BrowserRange!"));
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
354 return false;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
355 } else {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
356 this.tainted = true;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
357 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
358 r = {};
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
359 nr = {};
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
360 _ref1 = ['start', 'end'];
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
361 for (_k = 0, _len2 = _ref1.length; _k < _len2; _k++) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
362 p = _ref1[_k];
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
363 node = this[p + 'Container'];
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
364 offset = this[p + 'Offset'];
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
365 if (!((node != null) && (offset != null))) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
366 return false;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
367 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
368 if (node.nodeType === 1) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
369 it = node.childNodes[offset];
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
370 node = it || node.childNodes[offset - 1];
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
371 if (node.nodeType === 1 && !node.firstChild) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
372 it = null;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
373 node = node.previousSibling;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
374 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
375 while (node.nodeType !== 3) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
376 node = node.firstChild;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
377 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
378 offset = it ? 0 : node.nodeValue.length;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
379 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
380 r[p] = node;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
381 r[p + 'Offset'] = offset;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
382 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
383 nr.start = r.startOffset > 0 ? r.start.splitText(r.startOffset) : r.start;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
384 if (r.start === r.end) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
385 if ((r.endOffset - r.startOffset) < nr.start.nodeValue.length) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
386 nr.start.splitText(r.endOffset - r.startOffset);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
387 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
388 nr.end = nr.start;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
389 } else {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
390 if (r.endOffset < r.end.nodeValue.length) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
391 r.end.splitText(r.endOffset);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
392 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
393 nr.end = r.end;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
394 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
395 nr.commonAncestor = this.commonAncestorContainer;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
396 while (nr.commonAncestor.nodeType !== 1) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
397 nr.commonAncestor = nr.commonAncestor.parentNode;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
398 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
399 return new Range.NormalizedRange(nr);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
400 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
401
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
402 BrowserRange.prototype.serialize = function(root, ignoreSelector) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
403 return this.normalize(root).serialize(root, ignoreSelector);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
404 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
405
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
406 return BrowserRange;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
407
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
408 })();
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
409
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
410 Range.NormalizedRange = (function() {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
411
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
412 function NormalizedRange(obj) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
413 this.commonAncestor = obj.commonAncestor;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
414 this.start = obj.start;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
415 this.end = obj.end;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
416 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
417
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
418 NormalizedRange.prototype.normalize = function(root) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
419 return this;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
420 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
421
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
422 NormalizedRange.prototype.limit = function(bounds) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
423 var nodes, parent, startParents, _k, _len2, _ref1;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
424 nodes = $.grep(this.textNodes(), function(node) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
425 return node.parentNode === bounds || $.contains(bounds, node.parentNode);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
426 });
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
427 if (!nodes.length) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
428 return null;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
429 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
430 this.start = nodes[0];
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
431 this.end = nodes[nodes.length - 1];
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
432 startParents = $(this.start).parents();
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
433 _ref1 = $(this.end).parents();
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
434 for (_k = 0, _len2 = _ref1.length; _k < _len2; _k++) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
435 parent = _ref1[_k];
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
436 if (startParents.index(parent) !== -1) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
437 this.commonAncestor = parent;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
438 break;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
439 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
440 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
441 return this;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
442 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
443
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
444 NormalizedRange.prototype.serialize = function(root, ignoreSelector) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
445 var end, serialization, start;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
446 serialization = function(node, isEnd) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
447 var n, nodes, offset, origParent, textNodes, xpath, _k, _len2;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
448 if (ignoreSelector) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
449 origParent = $(node).parents(":not(" + ignoreSelector + ")").eq(0);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
450 } else {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
451 origParent = $(node).parent();
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
452 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
453 xpath = origParent.xpath(root)[0];
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
454 textNodes = origParent.textNodes();
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
455 nodes = textNodes.slice(0, textNodes.index(node));
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
456 offset = 0;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
457 for (_k = 0, _len2 = nodes.length; _k < _len2; _k++) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
458 n = nodes[_k];
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
459 offset += n.nodeValue.length;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
460 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
461 if (isEnd) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
462 return [xpath, offset + node.nodeValue.length];
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
463 } else {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
464 return [xpath, offset];
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
465 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
466 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
467 start = serialization(this.start);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
468 end = serialization(this.end, true);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
469 return new Range.SerializedRange({
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
470 start: start[0],
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
471 end: end[0],
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
472 startOffset: start[1],
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
473 endOffset: end[1]
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
474 });
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
475 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
476
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
477 NormalizedRange.prototype.text = function() {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
478 var node;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
479 return ((function() {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
480 var _k, _len2, _ref1, _results;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
481 _ref1 = this.textNodes();
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
482 _results = [];
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
483 for (_k = 0, _len2 = _ref1.length; _k < _len2; _k++) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
484 node = _ref1[_k];
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
485 _results.push(node.nodeValue);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
486 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
487 return _results;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
488 }).call(this)).join('');
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
489 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
490
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
491 NormalizedRange.prototype.textNodes = function() {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
492 var end, start, textNodes, _ref1;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
493 textNodes = $(this.commonAncestor).textNodes();
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
494 _ref1 = [textNodes.index(this.start), textNodes.index(this.end)], start = _ref1[0], end = _ref1[1];
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
495 return $.makeArray(textNodes.slice(start, end + 1 || 9e9));
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
496 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
497
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
498 NormalizedRange.prototype.toRange = function() {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
499 var range;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
500 range = document.createRange();
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
501 range.setStartBefore(this.start);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
502 range.setEndAfter(this.end);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
503 return range;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
504 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
505
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
506 return NormalizedRange;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
507
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
508 })();
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
509
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
510 Range.SerializedRange = (function() {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
511
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
512 function SerializedRange(obj) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
513 this.start = obj.start;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
514 this.startOffset = obj.startOffset;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
515 this.end = obj.end;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
516 this.endOffset = obj.endOffset;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
517 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
518
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
519 SerializedRange.prototype.normalize = function(root) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
520 var contains, length, node, p, range, tn, _k, _l, _len2, _len3, _ref1, _ref2;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
521 range = {};
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
522 _ref1 = ['start', 'end'];
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
523 for (_k = 0, _len2 = _ref1.length; _k < _len2; _k++) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
524 p = _ref1[_k];
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
525 try {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
526 node = Range.nodeFromXPath(this[p], root);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
527 } catch (e) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
528 throw new Range.RangeError(p, ("Error while finding " + p + " node: " + this[p] + ": ") + e, e);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
529 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
530 if (!node) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
531 throw new Range.RangeError(p, "Couldn't find " + p + " node: " + this[p]);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
532 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
533 length = 0;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
534 _ref2 = $(node).textNodes();
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
535 for (_l = 0, _len3 = _ref2.length; _l < _len3; _l++) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
536 tn = _ref2[_l];
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
537 if (length + tn.nodeValue.length >= this[p + 'Offset']) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
538 range[p + 'Container'] = tn;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
539 range[p + 'Offset'] = this[p + 'Offset'] - length;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
540 break;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
541 } else {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
542 length += tn.nodeValue.length;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
543 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
544 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
545 if (!(range[p + 'Offset'] != null)) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
546 throw new Range.RangeError("" + p + "offset", "Couldn't find offset " + this[p + 'Offset'] + " in element " + this[p]);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
547 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
548 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
549 contains = !(document.compareDocumentPosition != null) ? function(a, b) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
550 return a.contains(b);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
551 } : function(a, b) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
552 return a.compareDocumentPosition(b) & 16;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
553 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
554 $(range.startContainer).parents().reverse().each(function() {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
555 if (contains(this, range.endContainer)) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
556 range.commonAncestorContainer = this;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
557 return false;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
558 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
559 });
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
560 return new Range.BrowserRange(range).normalize(root);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
561 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
562
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
563 SerializedRange.prototype.serialize = function(root, ignoreSelector) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
564 return this.normalize(root).serialize(root, ignoreSelector);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
565 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
566
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
567 SerializedRange.prototype.toObject = function() {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
568 return {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
569 start: this.start,
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
570 startOffset: this.startOffset,
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
571 end: this.end,
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
572 endOffset: this.endOffset
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
573 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
574 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
575
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
576 return SerializedRange;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
577
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
578 })();
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
579
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
580 util = {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
581 uuid: (function() {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
582 var counter;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
583 counter = 0;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
584 return function() {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
585 return counter++;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
586 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
587 })(),
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
588 getGlobal: function() {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
589 return (function() {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
590 return this;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
591 })();
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
592 },
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
593 maxZIndex: function($elements) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
594 var all, el;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
595 all = (function() {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
596 var _k, _len2, _results;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
597 _results = [];
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
598 for (_k = 0, _len2 = $elements.length; _k < _len2; _k++) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
599 el = $elements[_k];
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
600 if ($(el).css('position') === 'static') {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
601 _results.push(-1);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
602 } else {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
603 _results.push(parseInt($(el).css('z-index'), 10) || -1);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
604 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
605 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
606 return _results;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
607 })();
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
608 return Math.max.apply(Math, all);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
609 },
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
610 mousePosition: function(e, offsetEl) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
611 var offset;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
612 offset = $(offsetEl).offset();
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
613 return {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
614 top: e.pageY - offset.top,
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
615 left: e.pageX - offset.left
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
616 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
617 },
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
618 preventEventDefault: function(event) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
619 return event != null ? typeof event.preventDefault === "function" ? event.preventDefault() : void 0 : void 0;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
620 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
621 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
622
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
623 _Annotator = this.Annotator;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
624
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
625 Annotator = (function(_super) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
626
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
627 __extends(Annotator, _super);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
628
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
629 Annotator.prototype.events = {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
630 ".annotator-adder button click": "onAdderClick",
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
631 ".annotator-adder button mousedown": "onAdderMousedown",
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
632 ".annotator-hl mouseover": "onHighlightMouseover",
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
633 ".annotator-hl mouseout": "startViewerHideTimer"
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
634 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
635
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
636 Annotator.prototype.html = {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
637 adder: '<div class="annotator-adder"><button>' + _t('Annotate') + '</button></div>',
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
638 wrapper: '<div class="annotator-wrapper"></div>'
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
639 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
640
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
641 Annotator.prototype.options = {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
642 readOnly: false
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
643 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
644
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
645 Annotator.prototype.plugins = {};
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
646
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
647 Annotator.prototype.editor = null;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
648
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
649 Annotator.prototype.viewer = null;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
650
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
651 Annotator.prototype.selectedRanges = null;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
652
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
653 Annotator.prototype.mouseIsDown = false;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
654
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
655 Annotator.prototype.ignoreMouseup = false;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
656
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
657 Annotator.prototype.viewerHideTimer = null;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
658
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
659 function Annotator(element, options) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
660 this.onDeleteAnnotation = __bind(this.onDeleteAnnotation, this);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
661
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
662 this.onEditAnnotation = __bind(this.onEditAnnotation, this);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
663
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
664 this.onAdderClick = __bind(this.onAdderClick, this);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
665
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
666 this.onAdderMousedown = __bind(this.onAdderMousedown, this);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
667
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
668 this.onHighlightMouseover = __bind(this.onHighlightMouseover, this);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
669
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
670 this.checkForEndSelection = __bind(this.checkForEndSelection, this);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
671
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
672 this.checkForStartSelection = __bind(this.checkForStartSelection, this);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
673
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
674 this.clearViewerHideTimer = __bind(this.clearViewerHideTimer, this);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
675
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
676 this.startViewerHideTimer = __bind(this.startViewerHideTimer, this);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
677
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
678 this.showViewer = __bind(this.showViewer, this);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
679
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
680 this.onEditorSubmit = __bind(this.onEditorSubmit, this);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
681
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
682 this.onEditorHide = __bind(this.onEditorHide, this);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
683
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
684 this.showEditor = __bind(this.showEditor, this);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
685 Annotator.__super__.constructor.apply(this, arguments);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
686 this.plugins = {};
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
687 if (!Annotator.supported()) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
688 return this;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
689 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
690 if (!this.options.readOnly) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
691 this._setupDocumentEvents();
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
692 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
693 this._setupWrapper()._setupViewer()._setupEditor();
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
694 this._setupDynamicStyle();
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
695 this.adder = $(this.html.adder).appendTo(this.wrapper).hide();
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
696 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
697
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
698 Annotator.prototype._setupWrapper = function() {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
699 this.wrapper = $(this.html.wrapper);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
700 this.element.find('script').remove();
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
701 this.element.wrapInner(this.wrapper);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
702 this.wrapper = this.element.find('.annotator-wrapper');
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
703 return this;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
704 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
705
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
706 Annotator.prototype._setupViewer = function() {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
707 var _this = this;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
708 this.viewer = new Annotator.Viewer({
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
709 readOnly: this.options.readOnly
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
710 });
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
711 this.viewer.hide().on("edit", this.onEditAnnotation).on("delete", this.onDeleteAnnotation).addField({
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
712 load: function(field, annotation) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
713 if (annotation.text) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
714 $(field).escape(annotation.text);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
715 } else {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
716 $(field).html("<i>" + (_t('No Comment')) + "</i>");
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
717 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
718 return _this.publish('annotationViewerTextField', [field, annotation]);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
719 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
720 }).element.appendTo(this.wrapper).bind({
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
721 "mouseover": this.clearViewerHideTimer,
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
722 "mouseout": this.startViewerHideTimer
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
723 });
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
724 return this;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
725 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
726
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
727 Annotator.prototype._setupEditor = function() {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
728 this.editor = new Annotator.Editor();
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
729 this.editor.hide().on('hide', this.onEditorHide).on('save', this.onEditorSubmit).addField({
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
730 type: 'textarea',
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
731 label: _t('Comments') + '\u2026',
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
732 load: function(field, annotation) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
733 return $(field).find('textarea').val(annotation.text || '');
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
734 },
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
735 submit: function(field, annotation) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
736 return annotation.text = $(field).find('textarea').val();
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
737 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
738 });
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
739 this.editor.element.appendTo(this.wrapper);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
740 return this;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
741 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
742
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
743 Annotator.prototype._setupDocumentEvents = function() {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
744 $(document).bind({
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
745 "mouseup": this.checkForEndSelection,
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
746 "mousedown": this.checkForStartSelection
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
747 });
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
748 return this;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
749 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
750
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
751 Annotator.prototype._setupDynamicStyle = function() {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
752 var max, sel, style, x;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
753 style = $('#annotator-dynamic-style');
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
754 if (!style.length) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
755 style = $('<style id="annotator-dynamic-style"></style>').appendTo(document.head);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
756 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
757 sel = '*' + ((function() {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
758 var _k, _len2, _ref1, _results;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
759 _ref1 = ['adder', 'outer', 'notice', 'filter'];
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
760 _results = [];
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
761 for (_k = 0, _len2 = _ref1.length; _k < _len2; _k++) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
762 x = _ref1[_k];
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
763 _results.push(":not(.annotator-" + x + ")");
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
764 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
765 return _results;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
766 })()).join('');
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
767 max = util.maxZIndex($(document.body).find(sel));
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
768 max = Math.max(max, 1000);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
769 style.text([".annotator-adder, .annotator-outer, .annotator-notice {", " z-index: " + (max + 20) + ";", "}", ".annotator-filter {", " z-index: " + (max + 10) + ";", "}"].join("\n"));
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
770 return this;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
771 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
772
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
773 Annotator.prototype.getSelectedRanges = function() {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
774 var browserRange, i, normedRange, r, ranges, rangesToIgnore, selection, _k, _len2;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
775 selection = util.getGlobal().getSelection();
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
776 ranges = [];
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
777 rangesToIgnore = [];
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
778 if (!selection.isCollapsed) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
779 ranges = (function() {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
780 var _k, _ref1, _results;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
781 _results = [];
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
782 for (i = _k = 0, _ref1 = selection.rangeCount; 0 <= _ref1 ? _k < _ref1 : _k > _ref1; i = 0 <= _ref1 ? ++_k : --_k) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
783 r = selection.getRangeAt(i);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
784 browserRange = new Range.BrowserRange(r);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
785 normedRange = browserRange.normalize().limit(this.wrapper[0]);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
786 if (normedRange === null) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
787 rangesToIgnore.push(r);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
788 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
789 _results.push(normedRange);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
790 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
791 return _results;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
792 }).call(this);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
793 selection.removeAllRanges();
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
794 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
795 for (_k = 0, _len2 = rangesToIgnore.length; _k < _len2; _k++) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
796 r = rangesToIgnore[_k];
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
797 selection.addRange(r);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
798 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
799 return $.grep(ranges, function(range) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
800 if (range) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
801 selection.addRange(range.toRange());
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
802 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
803 return range;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
804 });
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
805 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
806
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
807 Annotator.prototype.createAnnotation = function() {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
808 var annotation;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
809 annotation = {};
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
810 this.publish('beforeAnnotationCreated', [annotation]);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
811 return annotation;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
812 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
813
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
814 Annotator.prototype.setupAnnotation = function(annotation, fireEvents) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
815 var normed, normedRanges, r, root, _k, _l, _len2, _len3, _ref1;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
816 if (fireEvents == null) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
817 fireEvents = true;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
818 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
819 root = this.wrapper[0];
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
820 annotation.ranges || (annotation.ranges = this.selectedRanges);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
821 normedRanges = [];
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
822 _ref1 = annotation.ranges;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
823 for (_k = 0, _len2 = _ref1.length; _k < _len2; _k++) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
824 r = _ref1[_k];
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
825 try {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
826 normedRanges.push(Range.sniff(r).normalize(root));
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
827 } catch (e) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
828 if (e instanceof Range.RangeError) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
829 this.publish('rangeNormalizeFail', [annotation, r, e]);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
830 } else {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
831 throw e;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
832 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
833 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
834 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
835 annotation.quote = [];
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
836 annotation.ranges = [];
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
837 annotation.highlights = [];
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
838 for (_l = 0, _len3 = normedRanges.length; _l < _len3; _l++) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
839 normed = normedRanges[_l];
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
840 annotation.quote.push($.trim(normed.text()));
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
841 annotation.ranges.push(normed.serialize(this.wrapper[0], '.annotator-hl'));
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
842 $.merge(annotation.highlights, this.highlightRange(normed));
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
843 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
844 annotation.quote = annotation.quote.join(' / ');
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
845 $(annotation.highlights).data('annotation', annotation);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
846 if (fireEvents) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
847 this.publish('annotationCreated', [annotation]);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
848 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
849 return annotation;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
850 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
851
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
852 Annotator.prototype.updateAnnotation = function(annotation) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
853 this.publish('beforeAnnotationUpdated', [annotation]);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
854 this.publish('annotationUpdated', [annotation]);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
855 return annotation;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
856 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
857
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
858 Annotator.prototype.deleteAnnotation = function(annotation) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
859 var h, _k, _len2, _ref1;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
860 _ref1 = annotation.highlights;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
861 for (_k = 0, _len2 = _ref1.length; _k < _len2; _k++) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
862 h = _ref1[_k];
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
863 $(h).replaceWith(h.childNodes);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
864 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
865 this.publish('annotationDeleted', [annotation]);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
866 return annotation;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
867 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
868
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
869 Annotator.prototype.loadAnnotations = function(annotations) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
870 var clone, loader,
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
871 _this = this;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
872 if (annotations == null) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
873 annotations = [];
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
874 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
875 loader = function(annList) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
876 var n, now, _k, _len2;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
877 if (annList == null) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
878 annList = [];
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
879 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
880 now = annList.splice(0, 10);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
881 for (_k = 0, _len2 = now.length; _k < _len2; _k++) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
882 n = now[_k];
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
883 _this.setupAnnotation(n, false);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
884 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
885 if (annList.length > 0) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
886 return setTimeout((function() {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
887 return loader(annList);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
888 }), 10);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
889 } else {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
890 return _this.publish('annotationsLoaded', [clone]);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
891 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
892 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
893 clone = annotations.slice();
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
894 if (annotations.length) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
895 loader(annotations);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
896 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
897 return this;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
898 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
899
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
900 Annotator.prototype.dumpAnnotations = function() {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
901 if (this.plugins['Store']) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
902 return this.plugins['Store'].dumpAnnotations();
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
903 } else {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
904 return console.warn(_t("Can't dump annotations without Store plugin."));
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
905 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
906 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
907
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
908 Annotator.prototype.highlightRange = function(normedRange, cssClass) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
909 var hl, node, white, _k, _len2, _ref1, _results;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
910 if (cssClass == null) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
911 cssClass = 'annotator-hl';
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
912 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
913 white = /^\s*$/;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
914 hl = $("<span class='" + cssClass + "'></span>");
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
915 _ref1 = normedRange.textNodes();
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
916 _results = [];
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
917 for (_k = 0, _len2 = _ref1.length; _k < _len2; _k++) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
918 node = _ref1[_k];
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
919 if (!white.test(node.nodeValue)) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
920 _results.push($(node).wrapAll(hl).parent().show()[0]);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
921 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
922 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
923 return _results;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
924 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
925
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
926 Annotator.prototype.highlightRanges = function(normedRanges, cssClass) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
927 var highlights, r, _k, _len2;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
928 if (cssClass == null) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
929 cssClass = 'annotator-hl';
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
930 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
931 highlights = [];
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
932 for (_k = 0, _len2 = normedRanges.length; _k < _len2; _k++) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
933 r = normedRanges[_k];
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
934 $.merge(highlights, this.highlightRange(r, cssClass));
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
935 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
936 return highlights;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
937 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
938
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
939 Annotator.prototype.addPlugin = function(name, options) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
940 var klass, _base;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
941 if (this.plugins[name]) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
942 console.error(_t("You cannot have more than one instance of any plugin."));
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
943 } else {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
944 klass = Annotator.Plugin[name];
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
945 if (typeof klass === 'function') {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
946 this.plugins[name] = new klass(this.element[0], options);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
947 this.plugins[name].annotator = this;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
948 if (typeof (_base = this.plugins[name]).pluginInit === "function") {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
949 _base.pluginInit();
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
950 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
951 } else {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
952 console.error(_t("Could not load ") + name + _t(" plugin. Have you included the appropriate <script> tag?"));
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
953 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
954 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
955 return this;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
956 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
957
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
958 Annotator.prototype.showEditor = function(annotation, location) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
959 this.editor.element.css(location);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
960 this.editor.load(annotation);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
961 this.publish('annotationEditorShown', [this.editor, annotation]);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
962 return this;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
963 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
964
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
965 Annotator.prototype.onEditorHide = function() {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
966 this.publish('annotationEditorHidden', [this.editor]);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
967 return this.ignoreMouseup = false;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
968 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
969
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
970 Annotator.prototype.onEditorSubmit = function(annotation) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
971 this.publish('annotationEditorSubmit', [this.editor, annotation]);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
972 if (annotation.ranges === void 0) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
973 return this.setupAnnotation(annotation);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
974 } else {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
975 return this.updateAnnotation(annotation);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
976 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
977 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
978
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
979 Annotator.prototype.showViewer = function(annotations, location) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
980 this.viewer.element.css(location);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
981 this.viewer.load(annotations);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
982 return this.publish('annotationViewerShown', [this.viewer, annotations]);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
983 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
984
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
985 Annotator.prototype.startViewerHideTimer = function() {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
986 if (!this.viewerHideTimer) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
987 return this.viewerHideTimer = setTimeout(this.viewer.hide, 250);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
988 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
989 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
990
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
991 Annotator.prototype.clearViewerHideTimer = function() {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
992 clearTimeout(this.viewerHideTimer);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
993 return this.viewerHideTimer = false;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
994 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
995
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
996 Annotator.prototype.checkForStartSelection = function(event) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
997 if (!(event && this.isAnnotator(event.target))) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
998 this.startViewerHideTimer();
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
999 return this.mouseIsDown = true;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1000 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1001 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1002
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1003 Annotator.prototype.checkForEndSelection = function(event) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1004 var container, range, _k, _len2, _ref1;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1005 this.mouseIsDown = false;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1006 if (this.ignoreMouseup) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1007 return;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1008 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1009 this.selectedRanges = this.getSelectedRanges();
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1010 _ref1 = this.selectedRanges;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1011 for (_k = 0, _len2 = _ref1.length; _k < _len2; _k++) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1012 range = _ref1[_k];
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1013 container = range.commonAncestor;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1014 if ($(container).hasClass('annotator-hl')) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1015 container = $(container).parents('[class^=annotator-hl]')[0];
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1016 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1017 if (this.isAnnotator(container)) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1018 return;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1019 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1020 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1021 if (event && this.selectedRanges.length) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1022 return this.adder.css(util.mousePosition(event, this.wrapper[0])).show();
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1023 } else {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1024 return this.adder.hide();
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1025 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1026 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1027
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1028 Annotator.prototype.isAnnotator = function(element) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1029 return !!$(element).parents().andSelf().filter('[class^=annotator-]').not(this.wrapper).length;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1030 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1031
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1032 Annotator.prototype.onHighlightMouseover = function(event) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1033 var annotations;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1034 this.clearViewerHideTimer();
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1035 if (this.mouseIsDown || this.viewer.isShown()) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1036 return false;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1037 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1038 annotations = $(event.target).parents('.annotator-hl').andSelf().map(function() {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1039 return $(this).data("annotation");
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1040 });
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1041 return this.showViewer($.makeArray(annotations), util.mousePosition(event, this.wrapper[0]));
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1042 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1043
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1044 Annotator.prototype.onAdderMousedown = function(event) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1045 if (event != null) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1046 event.preventDefault();
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1047 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1048 return this.ignoreMouseup = true;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1049 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1050
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1051 Annotator.prototype.onAdderClick = function(event) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1052 var highlights, position, r, ranges;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1053 if (event != null) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1054 event.preventDefault();
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1055 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1056 position = this.adder.position();
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1057 this.adder.hide();
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1058 if (this.selectedRanges && this.selectedRanges.length) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1059 ranges = (function() {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1060 var _k, _len2, _ref1, _results;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1061 _ref1 = this.selectedRanges;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1062 _results = [];
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1063 for (_k = 0, _len2 = _ref1.length; _k < _len2; _k++) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1064 r = _ref1[_k];
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1065 _results.push(Range.sniff(r).normalize());
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1066 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1067 return _results;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1068 }).call(this);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1069 highlights = this.highlightRanges(ranges, 'annotator-hl annotator-hl-temporary');
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1070 this.editor.element.one('hide', function() {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1071 var h, _k, _len2, _results;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1072 _results = [];
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1073 for (_k = 0, _len2 = highlights.length; _k < _len2; _k++) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1074 h = highlights[_k];
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1075 _results.push($(h).replaceWith(h.childNodes));
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1076 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1077 return _results;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1078 });
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1079 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1080 return this.showEditor(this.createAnnotation(), position);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1081 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1082
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1083 Annotator.prototype.onEditAnnotation = function(annotation) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1084 var offset;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1085 offset = this.viewer.element.position();
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1086 this.viewer.hide();
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1087 return this.showEditor(annotation, offset);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1088 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1089
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1090 Annotator.prototype.onDeleteAnnotation = function(annotation) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1091 this.viewer.hide();
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1092 return this.deleteAnnotation(annotation);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1093 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1094
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1095 return Annotator;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1096
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1097 })(Delegator);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1098
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1099 Annotator.Plugin = (function(_super) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1100
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1101 __extends(Plugin, _super);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1102
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1103 function Plugin(element, options) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1104 Plugin.__super__.constructor.apply(this, arguments);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1105 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1106
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1107 Plugin.prototype.pluginInit = function() {};
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1108
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1109 return Plugin;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1110
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1111 })(Delegator);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1112
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1113 g = util.getGlobal();
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1114
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1115 if (!(((_ref1 = g.document) != null ? _ref1.evaluate : void 0) != null)) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1116 $.getScript('http://assets.annotateit.org/vendor/xpath.min.js');
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1117 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1118
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1119 if (!(g.getSelection != null)) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1120 $.getScript('http://assets.annotateit.org/vendor/ierange.min.js');
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1121 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1122
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1123 if (!(g.JSON != null)) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1124 $.getScript('http://assets.annotateit.org/vendor/json2.min.js');
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1125 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1126
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1127 Annotator.$ = $;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1128
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1129 Annotator.Delegator = Delegator;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1130
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1131 Annotator.Range = Range;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1132
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1133 Annotator._t = _t;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1134
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1135 Annotator.supported = function() {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1136 return (function() {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1137 return !!this.getSelection;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1138 })();
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1139 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1140
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1141 Annotator.noConflict = function() {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1142 util.getGlobal().Annotator = _Annotator;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1143 return this;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1144 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1145
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1146 $.plugin('annotator', Annotator);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1147
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1148 this.Annotator = Annotator;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1149
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1150 Annotator.Widget = (function(_super) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1151
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1152 __extends(Widget, _super);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1153
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1154 Widget.prototype.classes = {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1155 hide: 'annotator-hide',
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1156 invert: {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1157 x: 'annotator-invert-x',
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1158 y: 'annotator-invert-y'
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1159 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1160 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1161
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1162 function Widget(element, options) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1163 Widget.__super__.constructor.apply(this, arguments);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1164 this.classes = $.extend({}, Annotator.Widget.prototype.classes, this.classes);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1165 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1166
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1167 Widget.prototype.checkOrientation = function() {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1168 var current, offset, viewport, widget, window;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1169 this.resetOrientation();
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1170 window = $(util.getGlobal());
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1171 widget = this.element.children(":first");
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1172 offset = widget.offset();
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1173 viewport = {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1174 top: window.scrollTop(),
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1175 right: window.width() + window.scrollLeft()
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1176 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1177 current = {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1178 top: offset.top,
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1179 right: offset.left + widget.width()
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1180 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1181 if ((current.top - viewport.top) < 0) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1182 this.invertY();
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1183 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1184 if ((current.right - viewport.right) > 0) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1185 this.invertX();
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1186 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1187 return this;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1188 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1189
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1190 Widget.prototype.resetOrientation = function() {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1191 this.element.removeClass(this.classes.invert.x).removeClass(this.classes.invert.y);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1192 return this;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1193 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1194
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1195 Widget.prototype.invertX = function() {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1196 this.element.addClass(this.classes.invert.x);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1197 return this;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1198 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1199
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1200 Widget.prototype.invertY = function() {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1201 this.element.addClass(this.classes.invert.y);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1202 return this;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1203 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1204
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1205 Widget.prototype.isInvertedY = function() {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1206 return this.element.hasClass(this.classes.invert.y);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1207 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1208
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1209 Widget.prototype.isInvertedX = function() {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1210 return this.element.hasClass(this.classes.invert.x);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1211 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1212
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1213 return Widget;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1214
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1215 })(Delegator);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1216
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1217 Annotator.Editor = (function(_super) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1218
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1219 __extends(Editor, _super);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1220
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1221 Editor.prototype.events = {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1222 "form submit": "submit",
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1223 ".annotator-save click": "submit",
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1224 ".annotator-cancel click": "hide",
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1225 ".annotator-cancel mouseover": "onCancelButtonMouseover",
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1226 "textarea keydown": "processKeypress"
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1227 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1228
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1229 Editor.prototype.classes = {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1230 hide: 'annotator-hide',
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1231 focus: 'annotator-focus'
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1232 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1233
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1234 Editor.prototype.html = "<div class=\"annotator-outer annotator-editor\">\n <form class=\"annotator-widget\">\n <ul class=\"annotator-listing\"></ul>\n <div class=\"annotator-controls\">\n <a href=\"#cancel\" class=\"annotator-cancel\">" + _t('Cancel') + "</a>\n<a href=\"#save\" class=\"annotator-save annotator-focus\">" + _t('Save') + "</a>\n </div>\n </form>\n</div>";
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1235
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1236 Editor.prototype.options = {};
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1237
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1238 function Editor(options) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1239 this.onCancelButtonMouseover = __bind(this.onCancelButtonMouseover, this);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1240
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1241 this.processKeypress = __bind(this.processKeypress, this);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1242
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1243 this.submit = __bind(this.submit, this);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1244
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1245 this.load = __bind(this.load, this);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1246
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1247 this.hide = __bind(this.hide, this);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1248
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1249 this.show = __bind(this.show, this);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1250 Editor.__super__.constructor.call(this, $(this.html)[0], options);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1251 this.fields = [];
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1252 this.annotation = {};
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1253 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1254
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1255 Editor.prototype.show = function(event) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1256 util.preventEventDefault(event);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1257 this.element.removeClass(this.classes.hide);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1258 this.element.find('.annotator-save').addClass(this.classes.focus);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1259 this.checkOrientation();
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1260 this.element.find(":input:first").focus();
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1261 this.setupDraggables();
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1262 return this.publish('show');
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1263 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1264
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1265 Editor.prototype.hide = function(event) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1266 util.preventEventDefault(event);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1267 this.element.addClass(this.classes.hide);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1268 return this.publish('hide');
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1269 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1270
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1271 Editor.prototype.load = function(annotation) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1272 var field, _k, _len2, _ref2;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1273 this.annotation = annotation;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1274 this.publish('load', [this.annotation]);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1275 _ref2 = this.fields;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1276 for (_k = 0, _len2 = _ref2.length; _k < _len2; _k++) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1277 field = _ref2[_k];
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1278 field.load(field.element, this.annotation);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1279 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1280 return this.show();
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1281 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1282
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1283 Editor.prototype.submit = function(event) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1284 var field, _k, _len2, _ref2;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1285 util.preventEventDefault(event);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1286 _ref2 = this.fields;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1287 for (_k = 0, _len2 = _ref2.length; _k < _len2; _k++) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1288 field = _ref2[_k];
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1289 field.submit(field.element, this.annotation);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1290 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1291 this.publish('save', [this.annotation]);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1292 return this.hide();
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1293 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1294
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1295 Editor.prototype.addField = function(options) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1296 var element, field, input;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1297 field = $.extend({
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1298 id: 'annotator-field-' + util.uuid(),
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1299 type: 'input',
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1300 label: '',
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1301 load: function() {},
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1302 submit: function() {}
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1303 }, options);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1304 input = null;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1305 element = $('<li class="annotator-item" />');
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1306 field.element = element[0];
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1307 switch (field.type) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1308 case 'textarea':
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1309 input = $('<textarea />');
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1310 break;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1311 case 'input':
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1312 case 'checkbox':
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1313 input = $('<input />');
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1314 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1315 element.append(input);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1316 input.attr({
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1317 id: field.id,
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1318 placeholder: field.label
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1319 });
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1320 if (field.type === 'checkbox') {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1321 input[0].type = 'checkbox';
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1322 element.addClass('annotator-checkbox');
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1323 element.append($('<label />', {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1324 "for": field.id,
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1325 html: field.label
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1326 }));
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1327 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1328 this.element.find('ul:first').append(element);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1329 this.fields.push(field);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1330 return field.element;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1331 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1332
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1333 Editor.prototype.checkOrientation = function() {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1334 var controls, list;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1335 Editor.__super__.checkOrientation.apply(this, arguments);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1336 list = this.element.find('ul');
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1337 controls = this.element.find('.annotator-controls');
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1338 if (this.element.hasClass(this.classes.invert.y)) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1339 controls.insertBefore(list);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1340 } else if (controls.is(':first-child')) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1341 controls.insertAfter(list);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1342 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1343 return this;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1344 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1345
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1346 Editor.prototype.processKeypress = function(event) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1347 if (event.keyCode === 27) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1348 return this.hide();
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1349 } else if (event.keyCode === 13 && !event.shiftKey) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1350 return this.submit();
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1351 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1352 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1353
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1354 Editor.prototype.onCancelButtonMouseover = function() {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1355 return this.element.find('.' + this.classes.focus).removeClass(this.classes.focus);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1356 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1357
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1358 Editor.prototype.setupDraggables = function() {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1359 var classes, controls, cornerItem, editor, mousedown, onMousedown, onMousemove, onMouseup, resize, textarea, throttle,
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1360 _this = this;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1361 this.element.find('.annotator-resize').remove();
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1362 if (this.element.hasClass(this.classes.invert.y)) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1363 cornerItem = this.element.find('.annotator-item:last');
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1364 } else {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1365 cornerItem = this.element.find('.annotator-item:first');
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1366 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1367 if (cornerItem) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1368 $('<span class="annotator-resize"></span>').appendTo(cornerItem);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1369 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1370 mousedown = null;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1371 classes = this.classes;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1372 editor = this.element;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1373 textarea = null;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1374 resize = editor.find('.annotator-resize');
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1375 controls = editor.find('.annotator-controls');
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1376 throttle = false;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1377 onMousedown = function(event) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1378 if (event.target === this) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1379 mousedown = {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1380 element: this,
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1381 top: event.pageY,
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1382 left: event.pageX
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1383 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1384 textarea = editor.find('textarea:first');
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1385 $(window).bind({
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1386 'mouseup.annotator-editor-resize': onMouseup,
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1387 'mousemove.annotator-editor-resize': onMousemove
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1388 });
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1389 return event.preventDefault();
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1390 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1391 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1392 onMouseup = function() {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1393 mousedown = null;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1394 return $(window).unbind('.annotator-editor-resize');
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1395 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1396 onMousemove = function(event) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1397 var diff, directionX, directionY, height, width;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1398 if (mousedown && throttle === false) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1399 diff = {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1400 top: event.pageY - mousedown.top,
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1401 left: event.pageX - mousedown.left
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1402 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1403 if (mousedown.element === resize[0]) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1404 height = textarea.outerHeight();
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1405 width = textarea.outerWidth();
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1406 directionX = editor.hasClass(classes.invert.x) ? -1 : 1;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1407 directionY = editor.hasClass(classes.invert.y) ? 1 : -1;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1408 textarea.height(height + (diff.top * directionY));
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1409 textarea.width(width + (diff.left * directionX));
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1410 if (textarea.outerHeight() !== height) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1411 mousedown.top = event.pageY;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1412 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1413 if (textarea.outerWidth() !== width) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1414 mousedown.left = event.pageX;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1415 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1416 } else if (mousedown.element === controls[0]) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1417 editor.css({
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1418 top: parseInt(editor.css('top'), 10) + diff.top,
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1419 left: parseInt(editor.css('left'), 10) + diff.left
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1420 });
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1421 mousedown.top = event.pageY;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1422 mousedown.left = event.pageX;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1423 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1424 throttle = true;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1425 return setTimeout(function() {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1426 return throttle = false;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1427 }, 1000 / 60);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1428 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1429 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1430 resize.bind('mousedown', onMousedown);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1431 return controls.bind('mousedown', onMousedown);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1432 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1433
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1434 return Editor;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1435
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1436 })(Annotator.Widget);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1437
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1438 Annotator.Viewer = (function(_super) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1439
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1440 __extends(Viewer, _super);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1441
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1442 Viewer.prototype.events = {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1443 ".annotator-edit click": "onEditClick",
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1444 ".annotator-delete click": "onDeleteClick"
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1445 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1446
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1447 Viewer.prototype.classes = {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1448 hide: 'annotator-hide',
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1449 showControls: 'annotator-visible'
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1450 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1451
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1452 Viewer.prototype.html = {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1453 element: "<div class=\"annotator-outer annotator-viewer\">\n <ul class=\"annotator-widget annotator-listing\"></ul>\n</div>",
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1454 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>"
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1455 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1456
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1457 Viewer.prototype.options = {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1458 readOnly: false
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1459 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1460
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1461 function Viewer(options) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1462 this.onDeleteClick = __bind(this.onDeleteClick, this);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1463
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1464 this.onEditClick = __bind(this.onEditClick, this);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1465
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1466 this.load = __bind(this.load, this);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1467
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1468 this.hide = __bind(this.hide, this);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1469
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1470 this.show = __bind(this.show, this);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1471 Viewer.__super__.constructor.call(this, $(this.html.element)[0], options);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1472 this.item = $(this.html.item)[0];
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1473 this.fields = [];
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1474 this.annotations = [];
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1475 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1476
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1477 Viewer.prototype.show = function(event) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1478 var controls,
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1479 _this = this;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1480 util.preventEventDefault(event);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1481 controls = this.element.find('.annotator-controls').addClass(this.classes.showControls);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1482 setTimeout((function() {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1483 return controls.removeClass(_this.classes.showControls);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1484 }), 500);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1485 this.element.removeClass(this.classes.hide);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1486 return this.checkOrientation().publish('show');
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1487 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1488
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1489 Viewer.prototype.isShown = function() {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1490 return !this.element.hasClass(this.classes.hide);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1491 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1492
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1493 Viewer.prototype.hide = function(event) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1494 util.preventEventDefault(event);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1495 this.element.addClass(this.classes.hide);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1496 return this.publish('hide');
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1497 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1498
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1499 Viewer.prototype.load = function(annotations) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1500 var annotation, controller, controls, del, edit, element, field, item, link, links, list, _k, _l, _len2, _len3, _ref2, _ref3;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1501 this.annotations = annotations || [];
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1502 list = this.element.find('ul:first').empty();
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1503 _ref2 = this.annotations;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1504 for (_k = 0, _len2 = _ref2.length; _k < _len2; _k++) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1505 annotation = _ref2[_k];
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1506 item = $(this.item).clone().appendTo(list).data('annotation', annotation);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1507 controls = item.find('.annotator-controls');
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1508 link = controls.find('.annotator-link');
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1509 edit = controls.find('.annotator-edit');
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1510 del = controls.find('.annotator-delete');
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1511 links = new LinkParser(annotation.links || []).get('alternate', {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1512 'type': 'text/html'
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1513 });
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1514 if (links.length === 0 || !(links[0].href != null)) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1515 link.remove();
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1516 } else {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1517 link.attr('href', links[0].href);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1518 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1519 if (this.options.readOnly) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1520 edit.remove();
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1521 del.remove();
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1522 } else {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1523 controller = {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1524 showEdit: function() {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1525 return edit.removeAttr('disabled');
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1526 },
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1527 hideEdit: function() {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1528 return edit.attr('disabled', 'disabled');
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1529 },
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1530 showDelete: function() {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1531 return del.removeAttr('disabled');
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1532 },
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1533 hideDelete: function() {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1534 return del.attr('disabled', 'disabled');
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1535 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1536 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1537 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1538 _ref3 = this.fields;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1539 for (_l = 0, _len3 = _ref3.length; _l < _len3; _l++) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1540 field = _ref3[_l];
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1541 element = $(field.element).clone().appendTo(item)[0];
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1542 field.load(element, annotation, controller);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1543 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1544 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1545 this.publish('load', [this.annotations]);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1546 return this.show();
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1547 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1548
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1549 Viewer.prototype.addField = function(options) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1550 var field;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1551 field = $.extend({
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1552 load: function() {}
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1553 }, options);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1554 field.element = $('<div />')[0];
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1555 this.fields.push(field);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1556 field.element;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1557 return this;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1558 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1559
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1560 Viewer.prototype.onEditClick = function(event) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1561 return this.onButtonClick(event, 'edit');
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1562 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1563
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1564 Viewer.prototype.onDeleteClick = function(event) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1565 return this.onButtonClick(event, 'delete');
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1566 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1567
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1568 Viewer.prototype.onButtonClick = function(event, type) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1569 var item;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1570 item = $(event.target).parents('.annotator-annotation');
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1571 return this.publish(type, [item.data('annotation')]);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1572 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1573
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1574 return Viewer;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1575
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1576 })(Annotator.Widget);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1577
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1578 LinkParser = (function() {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1579
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1580 function LinkParser(data) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1581 this.data = data;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1582 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1583
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1584 LinkParser.prototype.get = function(rel, cond) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1585 var d, k, keys, match, v, _k, _len2, _ref2, _results;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1586 if (cond == null) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1587 cond = {};
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1588 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1589 cond = $.extend({}, cond, {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1590 rel: rel
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1591 });
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1592 keys = (function() {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1593 var _results;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1594 _results = [];
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1595 for (k in cond) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1596 if (!__hasProp.call(cond, k)) continue;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1597 v = cond[k];
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1598 _results.push(k);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1599 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1600 return _results;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1601 })();
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1602 _ref2 = this.data;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1603 _results = [];
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1604 for (_k = 0, _len2 = _ref2.length; _k < _len2; _k++) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1605 d = _ref2[_k];
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1606 match = keys.reduce((function(m, k) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1607 return m && (d[k] === cond[k]);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1608 }), true);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1609 if (match) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1610 _results.push(d);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1611 } else {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1612 continue;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1613 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1614 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1615 return _results;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1616 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1617
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1618 return LinkParser;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1619
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1620 })();
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1621
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1622 Annotator = Annotator || {};
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1623
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1624 Annotator.Notification = (function(_super) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1625
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1626 __extends(Notification, _super);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1627
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1628 Notification.prototype.events = {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1629 "click": "hide"
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1630 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1631
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1632 Notification.prototype.options = {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1633 html: "<div class='annotator-notice'></div>",
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1634 classes: {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1635 show: "annotator-notice-show",
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1636 info: "annotator-notice-info",
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1637 success: "annotator-notice-success",
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1638 error: "annotator-notice-error"
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1639 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1640 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1641
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1642 function Notification(options) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1643 this.hide = __bind(this.hide, this);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1644
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1645 this.show = __bind(this.show, this);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1646 Notification.__super__.constructor.call(this, $(this.options.html).appendTo(document.body)[0], options);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1647 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1648
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1649 Notification.prototype.show = function(message, status) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1650 if (status == null) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1651 status = Annotator.Notification.INFO;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1652 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1653 $(this.element).addClass(this.options.classes.show).addClass(this.options.classes[status]).escape(message || "");
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1654 setTimeout(this.hide, 5000);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1655 return this;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1656 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1657
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1658 Notification.prototype.hide = function() {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1659 $(this.element).removeClass(this.options.classes.show);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1660 return this;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1661 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1662
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1663 return Notification;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1664
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1665 })(Delegator);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1666
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1667 Annotator.Notification.INFO = 'show';
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1668
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1669 Annotator.Notification.SUCCESS = 'success';
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1670
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1671 Annotator.Notification.ERROR = 'error';
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1672
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1673 $(function() {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1674 var notification;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1675 notification = new Annotator.Notification;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1676 Annotator.showNotification = notification.show;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1677 return Annotator.hideNotification = notification.hide;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1678 });
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1679
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1680 Annotator.Plugin.Unsupported = (function(_super) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1681
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1682 __extends(Unsupported, _super);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1683
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1684 function Unsupported() {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1685 return Unsupported.__super__.constructor.apply(this, arguments);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1686 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1687
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1688 Unsupported.prototype.options = {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1689 message: Annotator._t("Sorry your current browser does not support the Annotator")
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1690 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1691
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1692 Unsupported.prototype.pluginInit = function() {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1693 var _this = this;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1694 if (!Annotator.supported()) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1695 return $(function() {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1696 Annotator.showNotification(_this.options.message);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1697 if ((window.XMLHttpRequest === void 0) && (ActiveXObject !== void 0)) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1698 return $('html').addClass('ie6');
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1699 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1700 });
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1701 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1702 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1703
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1704 return Unsupported;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1705
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1706 })(Annotator.Plugin);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1707
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1708 createDateFromISO8601 = function(string) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1709 var d, date, offset, regexp, time, _ref2;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1710 regexp = "([0-9]{4})(-([0-9]{2})(-([0-9]{2})" + "(T([0-9]{2}):([0-9]{2})(:([0-9]{2})(\.([0-9]+))?)?" + "(Z|(([-+])([0-9]{2}):([0-9]{2})))?)?)?)?";
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1711 d = string.match(new RegExp(regexp));
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1712 offset = 0;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1713 date = new Date(d[1], 0, 1);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1714 if (d[3]) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1715 date.setMonth(d[3] - 1);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1716 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1717 if (d[5]) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1718 date.setDate(d[5]);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1719 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1720 if (d[7]) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1721 date.setHours(d[7]);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1722 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1723 if (d[8]) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1724 date.setMinutes(d[8]);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1725 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1726 if (d[10]) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1727 date.setSeconds(d[10]);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1728 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1729 if (d[12]) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1730 date.setMilliseconds(Number("0." + d[12]) * 1000);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1731 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1732 if (d[14]) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1733 offset = (Number(d[16]) * 60) + Number(d[17]);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1734 offset *= (_ref2 = d[15] === '-') != null ? _ref2 : {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1735 1: -1
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1736 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1737 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1738 offset -= date.getTimezoneOffset();
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1739 time = Number(date) + (offset * 60 * 1000);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1740 date.setTime(Number(time));
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1741 return date;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1742 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1743
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1744 base64Decode = function(data) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1745 var ac, b64, bits, dec, h1, h2, h3, h4, i, o1, o2, o3, tmp_arr;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1746 if (typeof atob !== "undefined" && atob !== null) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1747 return atob(data);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1748 } else {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1749 b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1750 i = 0;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1751 ac = 0;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1752 dec = "";
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1753 tmp_arr = [];
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1754 if (!data) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1755 return data;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1756 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1757 data += '';
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1758 while (i < data.length) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1759 h1 = b64.indexOf(data.charAt(i++));
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1760 h2 = b64.indexOf(data.charAt(i++));
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1761 h3 = b64.indexOf(data.charAt(i++));
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1762 h4 = b64.indexOf(data.charAt(i++));
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1763 bits = h1 << 18 | h2 << 12 | h3 << 6 | h4;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1764 o1 = bits >> 16 & 0xff;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1765 o2 = bits >> 8 & 0xff;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1766 o3 = bits & 0xff;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1767 if (h3 === 64) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1768 tmp_arr[ac++] = String.fromCharCode(o1);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1769 } else if (h4 === 64) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1770 tmp_arr[ac++] = String.fromCharCode(o1, o2);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1771 } else {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1772 tmp_arr[ac++] = String.fromCharCode(o1, o2, o3);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1773 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1774 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1775 return tmp_arr.join('');
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1776 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1777 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1778
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1779 base64UrlDecode = function(data) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1780 var i, m, _k, _ref2;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1781 m = data.length % 4;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1782 if (m !== 0) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1783 for (i = _k = 0, _ref2 = 4 - m; 0 <= _ref2 ? _k < _ref2 : _k > _ref2; i = 0 <= _ref2 ? ++_k : --_k) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1784 data += '=';
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1785 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1786 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1787 data = data.replace(/-/g, '+');
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1788 data = data.replace(/_/g, '/');
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1789 return base64Decode(data);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1790 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1791
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1792 parseToken = function(token) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1793 var head, payload, sig, _ref2;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1794 _ref2 = token.split('.'), head = _ref2[0], payload = _ref2[1], sig = _ref2[2];
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1795 return JSON.parse(base64UrlDecode(payload));
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1796 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1797
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1798 Annotator.Plugin.Auth = (function(_super) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1799
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1800 __extends(Auth, _super);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1801
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1802 Auth.prototype.options = {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1803 token: null,
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1804 tokenUrl: '/auth/token',
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1805 autoFetch: true,
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1806 requestMethod: 'GET',
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1807 requestData: null,
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1808 unauthorizedCallback: null
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1809 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1810
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1811 function Auth(element, options) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1812 Auth.__super__.constructor.apply(this, arguments);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1813 this.waitingForToken = [];
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1814 if (this.options.token) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1815 this.setToken(this.options.token);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1816 } else {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1817 this.requestToken();
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1818 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1819 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1820
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1821 Auth.prototype.requestToken = function() {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1822 var _this = this;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1823 this.requestInProgress = true;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1824 return $.ajax({
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1825 url: this.options.tokenUrl,
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1826 dataType: 'text',
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1827 data: this.options.requestData,
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1828 type: this.options.requestMethod,
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1829 xhrFields: {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1830 withCredentials: true
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1831 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1832 }).done(function(data, status, xhr) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1833 return _this.setToken(data);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1834 }).fail(function(xhr, status, err) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1835 var callback, msg;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1836 if (xhr.status === 401) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1837 callback = _this.options.unauthorizedCallback;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1838 if ((callback != null) && callback(_this)) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1839 _this.retryTimeout = setTimeout((function() {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1840 return _this.requestToken();
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1841 }), 1000);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1842 return;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1843 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1844 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1845 msg = Annotator._t("Couldn't get auth token:");
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1846 console.error("" + msg + " " + err, xhr);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1847 return Annotator.showNotification("" + msg + " " + xhr.responseText, Annotator.Notification.ERROR);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1848 }).always(function() {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1849 return _this.requestInProgress = false;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1850 });
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1851 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1852
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1853 Auth.prototype.setToken = function(token) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1854 var _results,
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1855 _this = this;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1856 this.token = token;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1857 this._unsafeToken = parseToken(token);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1858 if (this.haveValidToken()) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1859 if (this.options.autoFetch) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1860 this.refreshTimeout = setTimeout((function() {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1861 return _this.requestToken();
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1862 }), (this.timeToExpiry() - 2) * 1000);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1863 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1864 this.updateHeaders();
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1865 _results = [];
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1866 while (this.waitingForToken.length > 0) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1867 _results.push(this.waitingForToken.pop()(this._unsafeToken));
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1868 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1869 return _results;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1870 } else {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1871 console.warn(Annotator._t("Didn't get a valid token."));
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1872 if (this.options.autoFetch) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1873 console.warn(Annotator._t("Getting a new token in 10s."));
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1874 return setTimeout((function() {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1875 return _this.requestToken();
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1876 }), 10 * 1000);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1877 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1878 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1879 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1880
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1881 Auth.prototype.haveValidToken = function() {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1882 var allFields;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1883 allFields = this._unsafeToken && this._unsafeToken.issuedAt && this._unsafeToken.ttl && this._unsafeToken.consumerKey;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1884 return allFields && this.timeToExpiry() > 0;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1885 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1886
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1887 Auth.prototype.timeToExpiry = function() {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1888 var expiry, issue, now, timeToExpiry;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1889 now = new Date().getTime() / 1000;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1890 issue = createDateFromISO8601(this._unsafeToken.issuedAt).getTime() / 1000;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1891 expiry = issue + this._unsafeToken.ttl;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1892 timeToExpiry = expiry - now;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1893 if (timeToExpiry > 0) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1894 return timeToExpiry;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1895 } else {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1896 return 0;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1897 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1898 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1899
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1900 Auth.prototype.updateHeaders = function() {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1901 var current;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1902 current = this.element.data('annotator:headers');
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1903 return this.element.data('annotator:headers', $.extend(current, {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1904 'x-annotator-auth-token': this.token
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1905 }));
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1906 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1907
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1908 Auth.prototype.withToken = function(callback) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1909 if (!(callback != null)) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1910 return;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1911 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1912 if (this.haveValidToken()) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1913 return callback(this._unsafeToken);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1914 } else {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1915 this.waitingForToken.push(callback);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1916 if (!this.requestInProgress) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1917 return this.requestToken();
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1918 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1919 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1920 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1921
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1922 return Auth;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1923
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1924 })(Annotator.Plugin);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1925
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1926 Annotator.Plugin.Store = (function(_super) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1927
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1928 __extends(Store, _super);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1929
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1930 Store.prototype.events = {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1931 'annotationCreated': 'annotationCreated',
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1932 'annotationDeleted': 'annotationDeleted',
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1933 'annotationUpdated': 'annotationUpdated'
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1934 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1935
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1936 Store.prototype.options = {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1937 prefix: '/store',
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1938 autoFetch: true,
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1939 annotationData: {},
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1940 loadFromSearch: false,
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1941 urls: {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1942 create: '/annotations',
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1943 read: '/annotations/:id',
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1944 update: '/annotations/:id',
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1945 destroy: '/annotations/:id',
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1946 search: '/search'
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1947 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1948 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1949
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1950 function Store(element, options) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1951 this._onError = __bind(this._onError, this);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1952
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1953 this._onLoadAnnotationsFromSearch = __bind(this._onLoadAnnotationsFromSearch, this);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1954
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1955 this._onLoadAnnotations = __bind(this._onLoadAnnotations, this);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1956
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1957 this._getAnnotations = __bind(this._getAnnotations, this);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1958 Store.__super__.constructor.apply(this, arguments);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1959 this.annotations = [];
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1960 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1961
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1962 Store.prototype.pluginInit = function() {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1963 if (!Annotator.supported()) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1964 return;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1965 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1966 if (this.annotator.plugins.Auth) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1967 return this.annotator.plugins.Auth.withToken(this._getAnnotations);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1968 } else {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1969 return this._getAnnotations();
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1970 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1971 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1972
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1973 Store.prototype._getAnnotations = function() {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1974 if (this.options.loadFromSearch) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1975 return this.loadAnnotationsFromSearch(this.options.loadFromSearch);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1976 } else {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1977 return this.loadAnnotations();
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1978 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1979 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1980
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1981 Store.prototype.annotationCreated = function(annotation) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1982 var _this = this;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1983 if (__indexOf.call(this.annotations, annotation) < 0) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1984 this.registerAnnotation(annotation);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1985 return this._apiRequest('create', annotation, function(data) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1986 if (!(data.id != null)) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1987 console.warn(Annotator._t("Warning: No ID returned from server for annotation "), annotation);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1988 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1989 return _this.updateAnnotation(annotation, data);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1990 });
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1991 } else {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1992 return this.updateAnnotation(annotation, {});
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1993 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1994 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1995
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1996 Store.prototype.annotationUpdated = function(annotation) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1997 var _this = this;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1998 if (__indexOf.call(this.annotations, annotation) >= 0) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
1999 return this._apiRequest('update', annotation, (function(data) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2000 return _this.updateAnnotation(annotation, data);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2001 }));
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2002 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2003 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2004
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2005 Store.prototype.annotationDeleted = function(annotation) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2006 var _this = this;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2007 if (__indexOf.call(this.annotations, annotation) >= 0) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2008 return this._apiRequest('destroy', annotation, (function() {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2009 return _this.unregisterAnnotation(annotation);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2010 }));
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2011 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2012 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2013
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2014 Store.prototype.registerAnnotation = function(annotation) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2015 return this.annotations.push(annotation);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2016 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2017
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2018 Store.prototype.unregisterAnnotation = function(annotation) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2019 return this.annotations.splice(this.annotations.indexOf(annotation), 1);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2020 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2021
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2022 Store.prototype.updateAnnotation = function(annotation, data) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2023 if (__indexOf.call(this.annotations, annotation) < 0) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2024 console.error(Annotator._t("Trying to update unregistered annotation!"));
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2025 } else {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2026 $.extend(annotation, data);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2027 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2028 return $(annotation.highlights).data('annotation', annotation);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2029 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2030
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2031 Store.prototype.loadAnnotations = function() {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2032 return this._apiRequest('read', null, this._onLoadAnnotations);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2033 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2034
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2035 Store.prototype._onLoadAnnotations = function(data) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2036 if (data == null) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2037 data = [];
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2038 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2039 this.annotations = data;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2040 return this.annotator.loadAnnotations(data.slice());
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2041 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2042
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2043 Store.prototype.loadAnnotationsFromSearch = function(searchOptions) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2044 return this._apiRequest('search', searchOptions, this._onLoadAnnotationsFromSearch);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2045 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2046
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2047 Store.prototype._onLoadAnnotationsFromSearch = function(data) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2048 if (data == null) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2049 data = {};
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2050 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2051 return this._onLoadAnnotations(data.rows || []);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2052 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2053
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2054 Store.prototype.dumpAnnotations = function() {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2055 var ann, _k, _len2, _ref2, _results;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2056 _ref2 = this.annotations;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2057 _results = [];
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2058 for (_k = 0, _len2 = _ref2.length; _k < _len2; _k++) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2059 ann = _ref2[_k];
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2060 _results.push(JSON.parse(this._dataFor(ann)));
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2061 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2062 return _results;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2063 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2064
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2065 Store.prototype._apiRequest = function(action, obj, onSuccess) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2066 var id, options, request, url;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2067 id = obj && obj.id;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2068 url = this._urlFor(action, id);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2069 options = this._apiRequestOptions(action, obj, onSuccess);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2070 request = $.ajax(url, options);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2071 request._id = id;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2072 request._action = action;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2073 return request;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2074 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2075
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2076 Store.prototype._apiRequestOptions = function(action, obj, onSuccess) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2077 var opts;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2078 opts = {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2079 type: this._methodFor(action),
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2080 headers: this.element.data('annotator:headers'),
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2081 dataType: "json",
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2082 success: onSuccess || function() {},
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2083 error: this._onError
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2084 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2085 if (action === "search") {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2086 opts = $.extend(opts, {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2087 data: obj
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2088 });
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2089 } else {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2090 opts = $.extend(opts, {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2091 data: obj && this._dataFor(obj),
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2092 contentType: "application/json; charset=utf-8"
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2093 });
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2094 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2095 return opts;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2096 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2097
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2098 Store.prototype._urlFor = function(action, id) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2099 var replaceWith, url;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2100 replaceWith = id != null ? '/' + id : '';
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2101 url = this.options.prefix || '/';
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2102 url += this.options.urls[action];
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2103 url = url.replace(/\/:id/, replaceWith);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2104 return url;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2105 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2106
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2107 Store.prototype._methodFor = function(action) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2108 var table;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2109 table = {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2110 'create': 'POST',
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2111 'read': 'GET',
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2112 'update': 'PUT',
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2113 'destroy': 'DELETE',
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2114 'search': 'GET'
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2115 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2116 return table[action];
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2117 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2118
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2119 Store.prototype._dataFor = function(annotation) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2120 var data, highlights;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2121 highlights = annotation.highlights;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2122 delete annotation.highlights;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2123 $.extend(annotation, this.options.annotationData);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2124 data = JSON.stringify(annotation);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2125 if (highlights) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2126 annotation.highlights = highlights;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2127 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2128 return data;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2129 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2130
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2131 Store.prototype._onError = function(xhr) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2132 var action, message;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2133 action = xhr._action;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2134 message = Annotator._t("Sorry we could not ") + action + Annotator._t(" this annotation");
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2135 if (xhr._action === 'search') {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2136 message = Annotator._t("Sorry we could not search the store for annotations");
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2137 } else if (xhr._action === 'read' && !xhr._id) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2138 message = Annotator._t("Sorry we could not ") + action + Annotator._t(" the annotations from the store");
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2139 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2140 switch (xhr.status) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2141 case 401:
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2142 message = Annotator._t("Sorry you are not allowed to ") + action + Annotator._t(" this annotation");
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2143 break;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2144 case 404:
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2145 message = Annotator._t("Sorry we could not connect to the annotations store");
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2146 break;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2147 case 500:
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2148 message = Annotator._t("Sorry something went wrong with the annotation store");
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2149 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2150 Annotator.showNotification(message, Annotator.Notification.ERROR);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2151 return console.error(Annotator._t("API request failed:") + (" '" + xhr.status + "'"));
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2152 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2153
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2154 return Store;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2155
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2156 })(Annotator.Plugin);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2157
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2158 Annotator.Plugin.Permissions = (function(_super) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2159
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2160 __extends(Permissions, _super);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2161
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2162 Permissions.prototype.events = {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2163 'beforeAnnotationCreated': 'addFieldsToAnnotation'
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2164 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2165
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2166 Permissions.prototype.options = {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2167 showViewPermissionsCheckbox: true,
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2168 showEditPermissionsCheckbox: true,
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2169 userId: function(user) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2170 return user;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2171 },
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2172 userString: function(user) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2173 return user;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2174 },
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2175 userAuthorize: function(action, annotation, user) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2176 var token, tokens, _k, _len2;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2177 if (annotation.permissions) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2178 tokens = annotation.permissions[action] || [];
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2179 if (tokens.length === 0) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2180 return true;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2181 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2182 for (_k = 0, _len2 = tokens.length; _k < _len2; _k++) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2183 token = tokens[_k];
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2184 if (this.userId(user) === token) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2185 return true;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2186 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2187 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2188 return false;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2189 } else if (annotation.user) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2190 return user && this.userId(user) === this.userId(annotation.user);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2191 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2192 return true;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2193 },
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2194 user: '',
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2195 permissions: {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2196 'read': [],
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2197 'update': [],
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2198 'delete': [],
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2199 'admin': []
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2200 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2201 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2202
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2203 function Permissions(element, options) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2204 this._setAuthFromToken = __bind(this._setAuthFromToken, this);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2205
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2206 this.updateViewer = __bind(this.updateViewer, this);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2207
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2208 this.updateAnnotationPermissions = __bind(this.updateAnnotationPermissions, this);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2209
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2210 this.updatePermissionsField = __bind(this.updatePermissionsField, this);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2211
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2212 this.addFieldsToAnnotation = __bind(this.addFieldsToAnnotation, this);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2213 Permissions.__super__.constructor.apply(this, arguments);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2214 if (this.options.user) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2215 this.setUser(this.options.user);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2216 delete this.options.user;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2217 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2218 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2219
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2220 Permissions.prototype.pluginInit = function() {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2221 var createCallback, self,
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2222 _this = this;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2223 if (!Annotator.supported()) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2224 return;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2225 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2226 self = this;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2227 createCallback = function(method, type) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2228 return function(field, annotation) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2229 return self[method].call(self, type, field, annotation);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2230 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2231 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2232 if (!this.user && this.annotator.plugins.Auth) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2233 this.annotator.plugins.Auth.withToken(this._setAuthFromToken);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2234 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2235 if (this.options.showViewPermissionsCheckbox === true) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2236 this.annotator.editor.addField({
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2237 type: 'checkbox',
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2238 label: Annotator._t('Allow anyone to <strong>view</strong> this annotation'),
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2239 load: createCallback('updatePermissionsField', 'read'),
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2240 submit: createCallback('updateAnnotationPermissions', 'read')
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2241 });
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2242 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2243 if (this.options.showEditPermissionsCheckbox === true) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2244 this.annotator.editor.addField({
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2245 type: 'checkbox',
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2246 label: Annotator._t('Allow anyone to <strong>edit</strong> this annotation'),
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2247 load: createCallback('updatePermissionsField', 'update'),
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2248 submit: createCallback('updateAnnotationPermissions', 'update')
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2249 });
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2250 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2251 this.annotator.viewer.addField({
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2252 load: this.updateViewer
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2253 });
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2254 if (this.annotator.plugins.Filter) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2255 return this.annotator.plugins.Filter.addFilter({
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2256 label: Annotator._t('User'),
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2257 property: 'user',
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2258 isFiltered: function(input, user) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2259 var keyword, _k, _len2, _ref2;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2260 user = _this.options.userString(user);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2261 if (!(input && user)) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2262 return false;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2263 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2264 _ref2 = input.split(/\s*/);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2265 for (_k = 0, _len2 = _ref2.length; _k < _len2; _k++) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2266 keyword = _ref2[_k];
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2267 if (user.indexOf(keyword) === -1) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2268 return false;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2269 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2270 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2271 return true;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2272 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2273 });
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2274 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2275 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2276
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2277 Permissions.prototype.setUser = function(user) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2278 return this.user = user;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2279 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2280
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2281 Permissions.prototype.addFieldsToAnnotation = function(annotation) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2282 if (annotation) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2283 annotation.permissions = this.options.permissions;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2284 if (this.user) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2285 return annotation.user = this.user;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2286 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2287 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2288 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2289
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2290 Permissions.prototype.authorize = function(action, annotation, user) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2291 if (user === void 0) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2292 user = this.user;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2293 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2294 if (this.options.userAuthorize) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2295 return this.options.userAuthorize.call(this.options, action, annotation, user);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2296 } else {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2297 return true;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2298 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2299 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2300
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2301 Permissions.prototype.updatePermissionsField = function(action, field, annotation) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2302 var input;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2303 field = $(field).show();
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2304 input = field.find('input').removeAttr('disabled');
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2305 if (!this.authorize('admin', annotation)) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2306 field.hide();
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2307 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2308 if (this.authorize(action, annotation || {}, null)) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2309 return input.attr('checked', 'checked');
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2310 } else {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2311 return input.removeAttr('checked');
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2312 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2313 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2314
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2315 Permissions.prototype.updateAnnotationPermissions = function(type, field, annotation) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2316 var dataKey;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2317 if (!annotation.permissions) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2318 annotation.permissions = this.options.permissions;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2319 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2320 dataKey = type + '-permissions';
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2321 if ($(field).find('input').is(':checked')) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2322 return annotation.permissions[type] = [];
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2323 } else {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2324 return annotation.permissions[type] = [this.user];
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2325 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2326 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2327
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2328 Permissions.prototype.updateViewer = function(field, annotation, controls) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2329 var user, username;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2330 field = $(field);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2331 username = this.options.userString(annotation.user);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2332 if (annotation.user && username && typeof username === 'string') {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2333 user = Annotator.$.escape(this.options.userString(annotation.user));
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2334 field.html(user).addClass('annotator-user');
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2335 } else {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2336 field.remove();
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2337 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2338 if (controls) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2339 if (!this.authorize('update', annotation)) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2340 controls.hideEdit();
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2341 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2342 if (!this.authorize('delete', annotation)) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2343 return controls.hideDelete();
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2344 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2345 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2346 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2347
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2348 Permissions.prototype._setAuthFromToken = function(token) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2349 return this.setUser(token.userId);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2350 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2351
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2352 return Permissions;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2353
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2354 })(Annotator.Plugin);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2355
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2356 Annotator.Plugin.Filter = (function(_super) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2357
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2358 __extends(Filter, _super);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2359
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2360 Filter.prototype.events = {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2361 ".annotator-filter-property input focus": "_onFilterFocus",
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2362 ".annotator-filter-property input blur": "_onFilterBlur",
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2363 ".annotator-filter-property input keyup": "_onFilterKeyup",
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2364 ".annotator-filter-previous click": "_onPreviousClick",
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2365 ".annotator-filter-next click": "_onNextClick",
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2366 ".annotator-filter-clear click": "_onClearClick"
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2367 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2368
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2369 Filter.prototype.classes = {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2370 active: 'annotator-filter-active',
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2371 hl: {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2372 hide: 'annotator-hl-filtered',
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2373 active: 'annotator-hl-active'
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2374 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2375 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2376
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2377 Filter.prototype.html = {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2378 element: "<div class=\"annotator-filter\">\n <strong>" + Annotator._t('Navigate:') + "</strong>\n<span class=\"annotator-filter-navigation\">\n <button class=\"annotator-filter-previous\">" + Annotator._t('Previous') + "</button>\n<button class=\"annotator-filter-next\">" + Annotator._t('Next') + "</button>\n</span>\n<strong>" + Annotator._t('Filter by:') + "</strong>\n</div>",
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2379 filter: "<span class=\"annotator-filter-property\">\n <label></label>\n <input/>\n <button class=\"annotator-filter-clear\">" + Annotator._t('Clear') + "</button>\n</span>"
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2380 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2381
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2382 Filter.prototype.options = {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2383 appendTo: 'body',
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2384 filters: [],
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2385 addAnnotationFilter: true,
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2386 isFiltered: function(input, property) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2387 var keyword, _k, _len2, _ref2;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2388 if (!(input && property)) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2389 return false;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2390 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2391 _ref2 = input.split(/\s*/);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2392 for (_k = 0, _len2 = _ref2.length; _k < _len2; _k++) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2393 keyword = _ref2[_k];
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2394 if (property.indexOf(keyword) === -1) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2395 return false;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2396 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2397 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2398 return true;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2399 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2400 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2401
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2402 function Filter(element, options) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2403 this._onPreviousClick = __bind(this._onPreviousClick, this);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2404
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2405 this._onNextClick = __bind(this._onNextClick, this);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2406
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2407 this._onFilterKeyup = __bind(this._onFilterKeyup, this);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2408
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2409 this._onFilterBlur = __bind(this._onFilterBlur, this);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2410
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2411 this._onFilterFocus = __bind(this._onFilterFocus, this);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2412
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2413 this.updateHighlights = __bind(this.updateHighlights, this);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2414
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2415 var _base;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2416 element = $(this.html.element).appendTo((options != null ? options.appendTo : void 0) || this.options.appendTo);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2417 Filter.__super__.constructor.call(this, element, options);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2418 (_base = this.options).filters || (_base.filters = []);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2419 this.filter = $(this.html.filter);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2420 this.filters = [];
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2421 this.current = 0;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2422 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2423
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2424 Filter.prototype.pluginInit = function() {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2425 var filter, _k, _len2, _ref2;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2426 _ref2 = this.options.filters;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2427 for (_k = 0, _len2 = _ref2.length; _k < _len2; _k++) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2428 filter = _ref2[_k];
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2429 this.addFilter(filter);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2430 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2431 this.updateHighlights();
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2432 this._setupListeners()._insertSpacer();
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2433 if (this.options.addAnnotationFilter === true) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2434 return this.addFilter({
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2435 label: Annotator._t('Annotation'),
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2436 property: 'text'
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2437 });
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2438 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2439 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2440
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2441 Filter.prototype._insertSpacer = function() {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2442 var currentMargin, html;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2443 html = $('html');
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2444 currentMargin = parseInt(html.css('padding-top'), 10) || 0;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2445 html.css('padding-top', currentMargin + this.element.outerHeight());
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2446 return this;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2447 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2448
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2449 Filter.prototype._setupListeners = function() {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2450 var event, events, _k, _len2;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2451 events = ['annotationsLoaded', 'annotationCreated', 'annotationUpdated', 'annotationDeleted'];
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2452 for (_k = 0, _len2 = events.length; _k < _len2; _k++) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2453 event = events[_k];
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2454 this.annotator.subscribe(event, this.updateHighlights);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2455 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2456 return this;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2457 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2458
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2459 Filter.prototype.addFilter = function(options) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2460 var f, filter;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2461 filter = $.extend({
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2462 label: '',
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2463 property: '',
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2464 isFiltered: this.options.isFiltered
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2465 }, options);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2466 if (!((function() {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2467 var _k, _len2, _ref2, _results;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2468 _ref2 = this.filters;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2469 _results = [];
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2470 for (_k = 0, _len2 = _ref2.length; _k < _len2; _k++) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2471 f = _ref2[_k];
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2472 if (f.property === filter.property) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2473 _results.push(f);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2474 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2475 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2476 return _results;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2477 }).call(this)).length) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2478 filter.id = 'annotator-filter-' + filter.property;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2479 filter.annotations = [];
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2480 filter.element = this.filter.clone().appendTo(this.element);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2481 filter.element.find('label').html(filter.label).attr('for', filter.id);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2482 filter.element.find('input').attr({
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2483 id: filter.id,
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2484 placeholder: Annotator._t('Filter by ') + filter.label + '\u2026'
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2485 });
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2486 filter.element.find('button').hide();
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2487 filter.element.data('filter', filter);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2488 this.filters.push(filter);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2489 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2490 return this;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2491 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2492
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2493 Filter.prototype.updateFilter = function(filter) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2494 var annotation, annotations, input, property, _k, _len2, _ref2;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2495 filter.annotations = [];
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2496 this.updateHighlights();
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2497 this.resetHighlights();
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2498 input = $.trim(filter.element.find('input').val());
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2499 if (input) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2500 annotations = this.highlights.map(function() {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2501 return $(this).data('annotation');
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2502 });
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2503 _ref2 = $.makeArray(annotations);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2504 for (_k = 0, _len2 = _ref2.length; _k < _len2; _k++) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2505 annotation = _ref2[_k];
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2506 property = annotation[filter.property];
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2507 if (filter.isFiltered(input, property)) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2508 filter.annotations.push(annotation);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2509 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2510 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2511 return this.filterHighlights();
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2512 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2513 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2514
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2515 Filter.prototype.updateHighlights = function() {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2516 this.highlights = this.annotator.element.find('.annotator-hl:visible');
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2517 return this.filtered = this.highlights.not(this.classes.hl.hide);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2518 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2519
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2520 Filter.prototype.filterHighlights = function() {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2521 var activeFilters, annotation, annotations, filtered, highlights, index, uniques, _k, _len2, _ref2;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2522 activeFilters = $.grep(this.filters, function(filter) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2523 return !!filter.annotations.length;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2524 });
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2525 filtered = ((_ref2 = activeFilters[0]) != null ? _ref2.annotations : void 0) || [];
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2526 if (activeFilters.length > 1) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2527 annotations = [];
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2528 $.each(activeFilters, function() {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2529 return $.merge(annotations, this.annotations);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2530 });
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2531 uniques = [];
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2532 filtered = [];
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2533 $.each(annotations, function() {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2534 if ($.inArray(this, uniques) === -1) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2535 return uniques.push(this);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2536 } else {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2537 return filtered.push(this);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2538 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2539 });
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2540 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2541 highlights = this.highlights;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2542 for (index = _k = 0, _len2 = filtered.length; _k < _len2; index = ++_k) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2543 annotation = filtered[index];
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2544 highlights = highlights.not(annotation.highlights);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2545 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2546 highlights.addClass(this.classes.hl.hide);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2547 this.filtered = this.highlights.not(this.classes.hl.hide);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2548 return this;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2549 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2550
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2551 Filter.prototype.resetHighlights = function() {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2552 this.highlights.removeClass(this.classes.hl.hide);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2553 this.filtered = this.highlights;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2554 return this;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2555 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2556
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2557 Filter.prototype._onFilterFocus = function(event) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2558 var input;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2559 input = $(event.target);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2560 input.parent().addClass(this.classes.active);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2561 return input.next('button').show();
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2562 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2563
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2564 Filter.prototype._onFilterBlur = function(event) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2565 var input;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2566 if (!event.target.value) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2567 input = $(event.target);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2568 input.parent().removeClass(this.classes.active);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2569 return input.next('button').hide();
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2570 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2571 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2572
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2573 Filter.prototype._onFilterKeyup = function(event) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2574 var filter;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2575 filter = $(event.target).parent().data('filter');
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2576 if (filter) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2577 return this.updateFilter(filter);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2578 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2579 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2580
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2581 Filter.prototype._findNextHighlight = function(previous) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2582 var active, annotation, current, index, next, offset, operator, resetOffset;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2583 if (!this.highlights.length) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2584 return this;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2585 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2586 offset = previous ? 0 : -1;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2587 resetOffset = previous ? -1 : 0;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2588 operator = previous ? 'lt' : 'gt';
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2589 active = this.highlights.not('.' + this.classes.hl.hide);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2590 current = active.filter('.' + this.classes.hl.active);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2591 if (!current.length) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2592 current = active.eq(offset);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2593 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2594 annotation = current.data('annotation');
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2595 index = active.index(current[0]);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2596 next = active.filter(":" + operator + "(" + index + ")").not(annotation.highlights).eq(resetOffset);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2597 if (!next.length) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2598 next = active.eq(resetOffset);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2599 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2600 return this._scrollToHighlight(next.data('annotation').highlights);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2601 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2602
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2603 Filter.prototype._onNextClick = function(event) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2604 return this._findNextHighlight();
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2605 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2606
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2607 Filter.prototype._onPreviousClick = function(event) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2608 return this._findNextHighlight(true);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2609 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2610
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2611 Filter.prototype._scrollToHighlight = function(highlight) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2612 highlight = $(highlight);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2613 this.highlights.removeClass(this.classes.hl.active);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2614 highlight.addClass(this.classes.hl.active);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2615 return $('html, body').animate({
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2616 scrollTop: highlight.offset().top - (this.element.height() + 20)
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2617 }, 150);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2618 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2619
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2620 Filter.prototype._onClearClick = function(event) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2621 return $(event.target).prev('input').val('').keyup().blur();
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2622 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2623
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2624 return Filter;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2625
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2626 })(Annotator.Plugin);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2627
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2628 Annotator.Plugin.Markdown = (function(_super) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2629
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2630 __extends(Markdown, _super);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2631
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2632 Markdown.prototype.events = {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2633 'annotationViewerTextField': 'updateTextField'
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2634 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2635
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2636 function Markdown(element, options) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2637 this.updateTextField = __bind(this.updateTextField, this);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2638 if ((typeof Showdown !== "undefined" && Showdown !== null ? Showdown.converter : void 0) != null) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2639 Markdown.__super__.constructor.apply(this, arguments);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2640 this.converter = new Showdown.converter();
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2641 } else {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2642 console.error(Annotator._t("To use the Markdown plugin, you must include Showdown into the page first."));
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2643 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2644 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2645
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2646 Markdown.prototype.updateTextField = function(field, annotation) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2647 var text;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2648 text = Annotator.$.escape(annotation.text || '');
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2649 return $(field).html(this.convert(text));
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2650 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2651
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2652 Markdown.prototype.convert = function(text) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2653 return this.converter.makeHtml(text);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2654 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2655
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2656 return Markdown;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2657
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2658 })(Annotator.Plugin);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2659
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2660 Annotator.Plugin.Tags = (function(_super) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2661
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2662 __extends(Tags, _super);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2663
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2664 function Tags() {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2665 this.setAnnotationTags = __bind(this.setAnnotationTags, this);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2666
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2667 this.updateField = __bind(this.updateField, this);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2668 return Tags.__super__.constructor.apply(this, arguments);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2669 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2670
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2671 Tags.prototype.options = {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2672 parseTags: function(string) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2673 var tags;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2674 string = $.trim(string);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2675 tags = [];
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2676 if (string) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2677 tags = string.split(/\s+/);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2678 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2679 return tags;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2680 },
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2681 stringifyTags: function(array) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2682 return array.join(" ");
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2683 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2684 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2685
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2686 Tags.prototype.field = null;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2687
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2688 Tags.prototype.input = null;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2689
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2690 Tags.prototype.pluginInit = function() {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2691 if (!Annotator.supported()) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2692 return;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2693 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2694 this.field = this.annotator.editor.addField({
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2695 label: Annotator._t('Add some tags here') + '\u2026',
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2696 load: this.updateField,
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2697 submit: this.setAnnotationTags
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2698 });
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2699 this.annotator.viewer.addField({
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2700 load: this.updateViewer
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2701 });
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2702 if (this.annotator.plugins.Filter) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2703 this.annotator.plugins.Filter.addFilter({
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2704 label: Annotator._t('Tag'),
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2705 property: 'tags',
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2706 isFiltered: Annotator.Plugin.Tags.filterCallback
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2707 });
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2708 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2709 return this.input = $(this.field).find(':input');
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2710 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2711
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2712 Tags.prototype.parseTags = function(string) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2713 return this.options.parseTags(string);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2714 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2715
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2716 Tags.prototype.stringifyTags = function(array) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2717 return this.options.stringifyTags(array);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2718 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2719
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2720 Tags.prototype.updateField = function(field, annotation) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2721 var value;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2722 value = '';
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2723 if (annotation.tags) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2724 value = this.stringifyTags(annotation.tags);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2725 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2726 return this.input.val(value);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2727 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2728
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2729 Tags.prototype.setAnnotationTags = function(field, annotation) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2730 return annotation.tags = this.parseTags(this.input.val());
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2731 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2732
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2733 Tags.prototype.updateViewer = function(field, annotation) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2734 field = $(field);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2735 if (annotation.tags && $.isArray(annotation.tags) && annotation.tags.length) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2736 return field.addClass('annotator-tags').html(function() {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2737 var string;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2738 return string = $.map(annotation.tags, function(tag) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2739 return '<span class="annotator-tag">' + Annotator.$.escape(tag) + '</span>';
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2740 }).join(' ');
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2741 });
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2742 } else {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2743 return field.remove();
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2744 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2745 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2746
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2747 return Tags;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2748
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2749 })(Annotator.Plugin);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2750
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2751 Annotator.Plugin.Tags.filterCallback = function(input, tags) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2752 var keyword, keywords, matches, tag, _k, _l, _len2, _len3;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2753 if (tags == null) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2754 tags = [];
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2755 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2756 matches = 0;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2757 keywords = [];
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2758 if (input) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2759 keywords = input.split(/\s+/g);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2760 for (_k = 0, _len2 = keywords.length; _k < _len2; _k++) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2761 keyword = keywords[_k];
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2762 if (tags.length) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2763 for (_l = 0, _len3 = tags.length; _l < _len3; _l++) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2764 tag = tags[_l];
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2765 if (tag.indexOf(keyword) !== -1) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2766 matches += 1;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2767 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2768 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2769 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2770 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2771 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2772 return matches === keywords.length;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2773 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2774
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2775 Annotator.Plugin.DigilibIntegrator = (function(_super) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2776
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2777 __extends(DigilibIntegrator, _super);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2778
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2779 function DigilibIntegrator() {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2780 return DigilibIntegrator.__super__.constructor.apply(this, arguments);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2781 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2782
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2783 DigilibIntegrator.prototype.events = {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2784 'annotationDeleted': 'annotationDeleted'
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2785 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2786
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2787 DigilibIntegrator.prototype.options = {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2788 hooks: null
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2789 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2790
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2791 DigilibIntegrator.prototype.pluginInit = function() {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2792 this.annotator.digilib = this.options.hooks;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2793 this.annotator.setupRangeAnnotation = this.annotator.setupAnnotation;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2794 this.annotator.setupAnnotation = this._setupAnnotation;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2795 return this;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2796 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2797
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2798 DigilibIntegrator.prototype._setupAnnotation = function(annotation, fireEvents) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2799 if (fireEvents == null) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2800 fireEvents = true;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2801 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2802 if (this.selectedShapes || (annotation.shapes != null) || (this.selectedAreas != null) || (annotation.areas != null)) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2803 annotation.shapes || (annotation.shapes = this.selectedShapes);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2804 annotation.areas || (annotation.areas = this.selectedAreas);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2805 annotation.highlights = [];
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2806 annotation.ranges = [];
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2807 this.digilib.setupAnnotation(annotation);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2808 if (fireEvents) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2809 this.publish('annotationCreated', [annotation]);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2810 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2811 return annotation;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2812 } else {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2813 return this.setupRangeAnnotation.apply(this, arguments);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2814 }
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2815 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2816
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2817 DigilibIntegrator.prototype.annotationDeleted = function(annotation) {
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2818 return this.options.hooks.annotationDeleted(annotation);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2819 };
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2820
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2821 return DigilibIntegrator;
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2822
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2823 })(Annotator.Plugin);
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2824
94a9c2379ebb new shape annotations work now.
robcast
parents:
diff changeset
2825 }).call(this);