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

new version contains Annotator JS files to be used with FilesystemSite.
author casties
date Thu, 05 Apr 2012 19:37:27 +0200
parents
children 6979313586cf
comparison
equal deleted inserted replaced
2:4c6c8835fc5c 3:6356e78ccf5c
1 var $, gettext, _gettext, _ref, _t;
2
3 gettext = null;
4
5 if (typeof Gettext !== "undefined" && Gettext !== null) {
6 _gettext = new Gettext({
7 domain: "annotator"
8 });
9 gettext = function(msgid) {
10 return _gettext.gettext(msgid);
11 };
12 } else {
13 gettext = function(msgid) {
14 return msgid;
15 };
16 }
17
18 _t = function(msgid) {
19 return gettext(msgid);
20 };
21
22 if (!(typeof jQuery !== "undefined" && jQuery !== null ? (_ref = jQuery.fn) != null ? _ref.jquery : void 0 : void 0)) {
23 console.error(_t("Annotator requires jQuery: have you included lib/vendor/jquery.js?"));
24 }
25
26 if (!(JSON && JSON.parse && JSON.stringify)) {
27 console.error(_t("Annotator requires a JSON implementation: have you included lib/vendor/json2.js?"));
28 }
29
30 $ = jQuery.sub();
31
32 $.flatten = function(array) {
33 var flatten;
34 flatten = function(ary) {
35 var el, flat, _i, _len;
36 flat = [];
37 for (_i = 0, _len = ary.length; _i < _len; _i++) {
38 el = ary[_i];
39 flat = flat.concat(el && $.isArray(el) ? flatten(el) : el);
40 }
41 return flat;
42 };
43 return flatten(array);
44 };
45
46 $.plugin = function(name, object) {
47 return jQuery.fn[name] = function(options) {
48 var args;
49 args = Array.prototype.slice.call(arguments, 1);
50 return this.each(function() {
51 var instance;
52 instance = $.data(this, name);
53 if (instance) {
54 return options && instance[options].apply(instance, args);
55 } else {
56 instance = new object(this, options);
57 return $.data(this, name, instance);
58 }
59 });
60 };
61 };
62
63 $.fn.textNodes = function() {
64 var getTextNodes;
65 getTextNodes = function(node) {
66 var nodes;
67 if (node && node.nodeType !== 3) {
68 nodes = [];
69 if (node.nodeType !== 8) {
70 node = node.lastChild;
71 while (node) {
72 nodes.push(getTextNodes(node));
73 node = node.previousSibling;
74 }
75 }
76 return nodes.reverse();
77 } else {
78 return node;
79 }
80 };
81 return this.map(function() {
82 return $.flatten(getTextNodes(this));
83 });
84 };
85
86 $.fn.xpath = function(relativeRoot) {
87 var jq;
88 jq = this.map(function() {
89 var elem, idx, path;
90 path = '';
91 elem = this;
92 while (elem && elem.nodeType === 1 && elem !== relativeRoot) {
93 idx = $(elem.parentNode).children(elem.tagName).index(elem) + 1;
94 idx = idx > 1 ? "[" + idx + "]" : "";
95 path = "/" + elem.tagName.toLowerCase() + idx + path;
96 elem = elem.parentNode;
97 }
98 return path;
99 });
100 return jq.get();
101 };
102
103 $.escape = function(html) {
104 return html.replace(/&(?!\w+;)/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;');
105 };
106
107 $.fn.escape = function(html) {
108 if (arguments.length) return this.html($.escape(html));
109 return this.html();
110 };