Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Annotator release 0.0.2

  • Loading branch information...
commit 7ac8b867ec8618b53fb79c170c7bd25469d5bf45 1 parent 4adac3d
@nickstenning nickstenning authored
Showing with 2 additions and 0 deletions.
  1. +1 −0  pkg/annotator.min.css
  2. +1 −0  pkg/annotator.min.js
View
1  pkg/annotator.min.css
@@ -0,0 +1 @@
+.annot-adder img{border:none;}.annot-adder{position:absolute;display:none;}.annot-highlighter{background:rgba(255,255,10,0.3);}.annot-highlighter .hover{border:2px solid #f00;}.annot-creater{position:absolute;width:10em;height:4em;opacity:.95;}.annot-creater textarea{background:#fdf5d8;border:1px solid #e1be94;-moz-border-radius:.3em;-webkit-border-radius:.3em;margin:0;padding:.4em;width:100%;height:100%;}.annot-viewer{position:absolute;width:15em;height:auto;opacity:.95;background:#fdf5d8;border:1px solid #e1be94;-moz-border-radius:.3em;-webkit-border-radius:.3em;margin:0;padding:.2em .4em .4em .4em;}.annot-viewer p{color:#333;font-family:helvetica;font-size:75%;margin:.25em 0 0;}.annot-viewer p+p{border-top:1px solid #E1BE94;padding-top:.25em;}
View
1  pkg/annotator.min.js
@@ -0,0 +1 @@
+$.plugin=function(b,a){$.fn[b]=function(d){var c=Array.prototype.slice.call(arguments,1);return this.each(function(){var e=$.data(this,b);if(e){d&&e[d].apply(e,c)}else{if(typeof a==="function"){e=new a(d,this)}else{function f(){}f.prototype=a;e=new f();e.init(d,this)}$.data(this,b,e)}})}};(function(){var a=false,b=/xyz/.test(function(){xyz})?(/\b_super\b/):(/.*/);this.Class=function(){};Class.extend=function(g){var f=this.prototype;a=true;var e=new this();a=false;for(var d in g){e[d]=typeof g[d]=="function"&&typeof f[d]=="function"&&b.test(g[d])?(function(h,i){return function(){var k=this._super;this._super=f[h];var j=i.apply(this,arguments);this._super=k;return j}})(d,g[d]):g[d]}function c(){if(!a&&this.init){this.init.apply(this,arguments)}}c.prototype=e;c.constructor=c;c.extend=arguments.callee;return c}})();(function($){$.toJSON=function(o){if(typeof(JSON)=="object"&&JSON.stringify){return JSON.stringify(o)}var type=typeof(o);if(o===null){return"null"}if(type=="undefined"){return undefined}if(type=="number"||type=="boolean"){return o+""}if(type=="string"){return $.quoteString(o)}if(type=="object"){if(typeof o.toJSON=="function"){return $.toJSON(o.toJSON())}if(o.constructor===Date){var month=o.getUTCMonth()+1;if(month<10){month="0"+month}var day=o.getUTCDate();if(day<10){day="0"+day}var year=o.getUTCFullYear();var hours=o.getUTCHours();if(hours<10){hours="0"+hours}var minutes=o.getUTCMinutes();if(minutes<10){minutes="0"+minutes}var seconds=o.getUTCSeconds();if(seconds<10){seconds="0"+seconds}var milli=o.getUTCMilliseconds();if(milli<100){milli="0"+milli}if(milli<10){milli="0"+milli}return'"'+year+"-"+month+"-"+day+"T"+hours+":"+minutes+":"+seconds+"."+milli+'Z"'}if(o.constructor===Array){var ret=[];for(var i=0;i<o.length;i++){ret.push($.toJSON(o[i])||"null")}return"["+ret.join(",")+"]"}var pairs=[];for(var k in o){var name;var type=typeof k;if(type=="number"){name='"'+k+'"'}else{if(type=="string"){name=$.quoteString(k)}else{continue}}if(typeof o[k]=="function"){continue}var val=$.toJSON(o[k]);pairs.push(name+":"+val)}return"{"+pairs.join(", ")+"}"}};$.evalJSON=function(src){if(typeof(JSON)=="object"&&JSON.parse){return JSON.parse(src)}return eval("("+src+")")};$.secureEvalJSON=function(src){if(typeof(JSON)=="object"&&JSON.parse){return JSON.parse(src)}var filtered=src;filtered=filtered.replace(/\\["\\\/bfnrtu]/g,"@");filtered=filtered.replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g,"]");filtered=filtered.replace(/(?:^|:|,)(?:\s*\[)+/g,"");if(/^[\],:{}\s]*$/.test(filtered)){return eval("("+src+")")}else{throw new SyntaxError("Error parsing JSON, source is not valid.")}};$.quoteString=function(string){if(string.match(_escapeable)){return'"'+string.replace(_escapeable,function(a){var c=_meta[a];if(typeof c==="string"){return c}c=a.charCodeAt();return"\\u00"+Math.floor(c/16).toString(16)+(c%16).toString(16)})+'"'}return'"'+string+'"'};var _escapeable=/["\\\x00-\x1f\x7f-\x9f]/g;var _meta={"\b":"\\b","\t":"\\t","\n":"\\n","\f":"\\f","\r":"\\r",'"':'\\"',"\\":"\\\\"}})(jQuery);(function(a){this.DelegatorClass=Class.extend({events:{},init:function(){var b=this;a.each(this.events,function(e,d){var c=e.split(" ");b.addDelegatedEvent(c.slice(0,-1).join(" "),c.slice(-1)[0],d)})},addDelegatedEvent:function(e,b,d){var c=this;this.element=this.element||document.body;if(typeof(e)==="string"&&e.replace(/\s+/g,"")===""){e=this.element}a(this.element).bind(b,function(g){for(var f=g.target;f!==c.element.parentNode;f=f.parentNode){if(f===e||a(f).is(e)){return c[d].apply(c,arguments)}}return null})}});a.extend({inject:function(b,d,c){a.each(b,function(e,f){d=c(d,f,e)});return d},flatten:function(b){return a.inject(b,[],function(d,c){return d.concat(a.isArray(c)?a.flatten(c):c)})}});a.fn.textNodes=function(){function b(c){if(c.nodeType!==Node.TEXT_NODE){return a(c).contents().map(function(){return b(this)}).get()}else{return c}}return this.map(function(){return a.flatten(b(this))})};a.fn.xpath=function(b){return this.map(function(){var e="";for(var d=this;d&&d.nodeType==Node.ELEMENT_NODE&&d!==b;d=d.parentNode){var c=a(d.parentNode).children(d.tagName).index(d)+1;c>1?(c="["+c+"]"):(c="");e="/"+d.tagName.toLowerCase()+c+e}return e}).get()}})(jQuery);(function(a){this.Annotator=DelegatorClass.extend({init:function(b,c){this.options=a.extend({classPrefix:"annot",adder:"<div><a href='#'><img src='img/note.png' /></a></div>",creater:"<div><textarea></textarea></div>",highlighter:"<span></span>",viewer:"<div></div>"},b);this.element=c;this.dom={};this.addDelegatedEvent("."+this.options.classPrefix+"-adder img","mousedown","showCreater");this.addDelegatedEvent("."+this.options.classPrefix+"-highlighter","mouseover","updateViewer");this.addDelegatedEvent("."+this.options.classPrefix+"-highlighter","mouseout","updateViewer");this.addDelegatedEvent(this.element,"mouseup","checkForSelection");this._super();var d=this;a.each(["adder","creater","highlighter","viewer"],function(e,f){d.dom[f]=a(d.options[f]).attr({"class":d.options.classPrefix+"-"+f}).appendTo(d.element).hide()})},checkForSelection:function(b){if(this.ignoreMouseup){this.ignoreMouseup=false;return}this.getSelection();if(b&&this.validSelection()){this.dom.adder.show().css({top:b.pageY-25,left:b.pageX+3})}else{this.dom.adder.hide()}},getSelection:function(){this.selection=window.getSelection();this.selectedRanges=[];for(var b=0;b<this.selection.rangeCount;b+=1){this.selectedRanges.push(this.selection.getRangeAt(b))}},validSelection:function(){return this.selection&&this.selection.rangeCount>0&&!this.selection.isCollapsed},createAnnotation:function(b){var d=this,b=b||{},c=[];b.ranges=a.map(b.ranges||this.selectedRanges,function(f){var e,g;if("commonAncestorContainer" in f){e=d.normRange(f);g=d.serializeRange(e)}else{if(("start" in f)&&(typeof f.start=="string")){e=d.deserializeRange(f);g=f}else{e=f;g=d.serializeRange(e)}}c=c.concat(d.highlightRange(e));return g});a(c).data("annotation",b);a(this.element).trigger("annotationCreated",[this,b,c]);return b},normRange:function(b){var d={},c={};a.each(["start","end"],function(e,h){var f,g=b[h+"Container"],i=b[h+"Offset"];if(g.nodeType===Node.ELEMENT_NODE){f=g.childNodes[i];g=f||g.childNodes[i-1];while(g.nodeType!==Node.TEXT_NODE){g=g.firstChild}i=f?0:g.nodeValue.length}d[h]=g;d[h+"Offset"]=i});c.start=(d.startOffset>0)?d.start.splitText(d.startOffset):d.start;if(d.start===d.end){if((d.endOffset-d.startOffset)<c.start.nodeValue.length){c.start.splitText(d.endOffset-d.startOffset)}c.end=c.start}else{if(d.endOffset<d.end.nodeValue.length){d.end.splitText(d.endOffset)}c.end=d.end}c.commonAncestor=b.commonAncestorContainer;while(c.commonAncestor.nodeType!==Node.ELEMENT_NODE){c.commonAncestor=c.commonAncestor.parentNode}return c},serializeRange:function(d){var f=this;var b=function(j,k){var h=a(j).parents(":not(."+f.options.classPrefix+")").eq(0),g=h.xpath(f.element)[0],i=h.textNodes(),l=a.inject(i.slice(0,i.index(j)),0,function(n,m){return n+m.nodeValue.length});return k?[g,l+j.nodeValue.length]:[g,l]},e=b(d.start),c=b(d.end,true);return{start:e[0],end:c[0],startOffset:e[1],endOffset:c[1]}},deserializeRange:function(f){var e=function(j){return document.evaluate(j,document,null,XPathResult.FIRST_ORDERED_NODE_TYPE,null).singleNodeValue};var i=a(this.element).xpath()[0],g=f.start.split("/"),h=f.end.split("/"),d=[],b={};for(var c=0;c<g.length;c+=1){if(g[c]===h[c]){d.push(g[c])}else{break}}b.commonAncestorContainer=e(i+d.join("/"));a.each(["start","end"],function(){var k=this,j=0;a(e(i+f[this])).textNodes().each(function(){if(j+this.nodeValue.length>=f[k+"Offset"]){b[k+"Container"]=this;b[k+"Offset"]=f[k+"Offset"]-j;return false}else{j+=this.nodeValue.length;return true}})});return this.normRange(b)},highlightRange:function(c){var e=this;var d=a(c.commonAncestor).textNodes();var b=[];d.slice(d.index(c.start),d.index(c.end)+1).each(function(){var f=e.dom.highlighter.clone().show();b.push(a(this).wrap(f).parent().get(0))});return b},loadAnnotations:function(b){var c=this;a.each(b,function(){c.createAnnotation(this)})},showCreater:function(b){var c=this;this.dom.creater.css({top:b.pageY,left:b.pageX}).show().find("textarea").focus().bind("keydown",function(d){if(d.keyCode==27){a(this).val("").unbind().parent().hide()}else{if(d.keyCode==13&&!d.shiftKey){a(this).unbind().parent().hide();c.createAnnotation({text:a(this).val()});a(this).val("")}}}).bind("blur",function(d){a(this).val("").unbind().parent().hide()});this.ignoreMouseup=true;this.dom.adder.hide();return false},updateViewer:function(c){if(c.type=="mouseout"){this.dom.viewer.hide()}else{var b=a(c.target).parents("."+this.options.classPrefix+"-highlighter").andSelf();this.dom.viewer.html(a.inject(b,"",function(f,e,d){return f+"<p>"+a(e).data("annotation").text+"</p>"})).css({top:c.pageY+10,left:c.pageX+10}).show()}}});this.AnnotationStore=DelegatorClass.extend({events:{annotationCreated:"annotationCreated"},init:function(b,c){this.options=a.extend({prefix:"/store/annotations",urls:{create:"",read:"/:id",update:"/:id",destroy:"/:id"},annotator:a(c).data("annotator")},b);if(!this.options.annotator){a(c).annotator();this.options.annotator=a(c).data("annotator")}this.element=c;this.annotations=[];this.elementstore={};this.loadAnnotations();this._super()},annotationCreated:function(f,g,b,d){var c=this;if(a.inArray(b,this.annotations)===-1){a.ajax({url:this._urlFor("create"),data:{json:a.toJSON(b)},dataType:"jsonp",type:"PUT",success:function(e){c.registerAnnotation(e,d)}})}else{this.registerAnnotation(b,d)}},registerAnnotation:function(b,c){if(a.inArray(b,this.annotations)===-1){this.annotations.push(b)}this.elementstore[b.id]=c;a(c).data("annotation",b)},loadAnnotation:function(c){var b=this;a.getJSON(this._urlFor("read",c),null,function(d,e){if(e==="success"){b.annotations=c?[d]:d;b.options.annotator.loadAnnotations(b.annotations)}else{throw ('Annotation could not be loaded. [XHR returned "'+e+'"]')}})},loadAnnotations:function(){this.loadAnnotation()},_urlFor:function(c,d){var b=this.options.prefix?this.options.prefix:"/";return b+this.options.urls[c].replace(/:id/,d||"")}});a.plugin("annotator",Annotator);a.plugin("annotationStore",AnnotationStore)})(jQuery);
Please sign in to comment.
Something went wrong with that request. Please try again.