-
Notifications
You must be signed in to change notification settings - Fork 0
/
srlib.js
1 lines (1 loc) · 11.2 KB
/
srlib.js
1
!function(){define("core/data/container/Drawable",[],function(){function t(t,e){var i=function(){return"xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g,function(t){var e=16*Math.random()|0,i="x"===t?e:3&e|8;return i.toString(16)})};if(this.getId=function(){return e},this.setTime=function(t){this.time=t},this.getTime=function(){return this.time},e)var e=e;else var e=i();t?this.time=t:this.setTime((new Date).getTime())}return t}),define("core/data/container/Point",["./Drawable"],function(t){function e(i,n,s,o){t.call(this,s,o),this.setX=function(t){this.x=t},this.setY=function(t){this.y=t},this.getX=function(){return this.x},this.getY=function(){return this.y},this.distance=function(t,i){if(t instanceof e)var n=t.y,s=t.x;var o=this.x-s,r=this.y-n;return Math.sqrt(o*o+r*r)},this.distanceSquared=function(t,i){if(t instanceof e)var i=t.y,t=t.x;return(this.x-t)*(this.x-t)+(this.y-i)*(this.y-i)},this.x=i,this.y=n}return e.prototype=new t,e.prototype.constructor=e,e}),define("core/data/container/Stroke",["./Drawable","./Point"],function(t,e){function i(e,i,n){t.call(this,i,n),this.setPoints=function(t){this.points=t},this.getPoints=function(){return this.points},this.getNumPoints=function(){return this.points.length},this.getPointAt=function(t){return this.points[t]},this.addPoint=function(t){t instanceof Array?this.points=this.points.concat(t):this.points.push(t)},e?this.points=e:this.points=[]}return i.prototype=new t,i.prototype.constructor=i,i}),define("core/data/container/Substroke",["./Drawable","./Point","./Stroke"],function(t,e,i){function n(e,i,n,s){t.call(this,n,s),this.setParent=function(t){this.parent=t},this.setPoints=function(t){this.points=t},this.getParent=function(){return this.parent},this.getPoints=function(){return this.points},this.getNumPoints=function(){return this.points.length},this.addPoint=function(t){t instanceof Array?this.points=this.points.concat(t):this.points.push(t)},this.parent=e,i?this.points=i:this.points=[]}return n.prototype=new t,n.prototype.constructor=n,n}),define("core/data/container/Shape",["./Drawable","./Point","./Stroke","./Substroke"],function(t,e,i,n){function s(e,i,n,s,o){t.call(this,s,o),this.setSubElements=function(t){this.subElements=t},this.setInterpretation=function(t){this.interpretation=t},this.setConfidence=function(t){this.confidence=t},this.getSubElements=function(){return this.subElements},this.getInterpretation=function(){return this.interpretation},this.getConfidence=function(){return this.confidence},this.addSubElement=function(t){t instanceof Array?this.subElements=this.subElements.concat(t):this.subElements.push(t)},e?this.subElements=e:this.subElements=[],i?this.interpretation=i:this.interpretation="",n?this.confidence=n:this.confidence=0}return s.prototype=new t,s.prototype.constructor=s,s}),define("core/data/container/Sketch",["./Drawable","./Point","./Stroke","./Substroke","./Shape"],function(t,e,i,n,s){function o(e,i,n,s,o,r,h){t.call(this,r,h),this.setDomain=function(t){t instanceof Array?this.domain=t:this.domain=[t]},this.setPoints=function(t){this.points=t},this.setStrokes=function(t){this.strokes=t},this.setSubstrokes=function(t){this.substrokes=t},this.setShapes=function(t){this.shapes=t},this.getDomain=function(){return this.domain},this.getPoints=function(){return this.points},this.getStrokes=function(){return this.strokes},this.getSubstrokes=function(){return this.substrokes},this.getShapes=function(){return this.shapes},this.addDomain=function(t){t instanceof Array?this.domain=this.domain.concat(t):this.domain.push(t)},this.addPoint=function(t){this.points[t.getId()]=t},this.addStroke=function(t){this.strokes[t.getId()]=t},this.addSubstroke=function(t){this.substrokes[t.getId()]=t},this.addShape=function(t){this.shapes[t.getId()]=t},e?e instanceof Array?this.domain=e:this.domain=[e]:this.domain=[],i?this.points=i:this.points={},n?this.strokes=n:this.strokes={},s?this.substrokes=s:this.substrokes={},o?this.shapes=o:this.shapes={}}return o.prototype=new t,o.prototype.constructor=o,o}),define("core/data/container/BoundingBox",["./Point"],function(t){function e(e){this.getBoundsFromPoints=function(t,e,i,n){return this.x=Math.min(t,i),this.y=Math.min(e,n),this.width=Math.abs(i-t),this.height=Math.abs(n-e),[this.x,this.y,this.width,this.height]},this.getBoundsFromStroke=function(t,e){e=e?e:!1;for(var i=Number.POSITIVE_INFINITY,n=Number.POSITIVE_INFINITY,s=Number.NEGATIVE_INFINITY,o=Number.NEGATIVE_INFINITY,r=t.getPoints(),h=r.length,a=0;h>a;a++)r[a].getX()<i&&(i=r[a].getX()),r[a].getY()<n&&(n=r[a].getY()),r[a].getX()>s&&(s=r[a].getX()),r[a].getY()>o&&(o=r[a].getY());return e||this.getBoundsFromPoints(i,n,s,o),[i,n,s,o]},this.getBoundsFromStrokes=function(t){var e=Number.POSITIVE_INFINITY,i=Number.POSITIVE_INFINITY,n=Number.NEGATIVE_INFINITY,s=Number.NEGATIVE_INFINITY;len=t.length;for(var o=0;o<len;o++)[sMinX,sMinY,sMaxX,sMaxY]=this.getBoundsFromStroke(t[o],!0),sMinX<e&&(e=sMinX),sMinY<i&&(i=sMinY),sMaxX>n&&(n=sMaxX),sMaxY>s&&(s=sMaxY);return this.getBoundsFromPoints(e,i,n,s),[e,i,n,s]},this.getMinX=function(){return this.x},this.getMinY=function(){return this.y},this.getMaxX=function(){return this.x+this.width},this.getMaxY=function(){return this.y+this.height},this.getArea=function(){return this.width*this.height},this.getPerimeter=function(){return 2+this.width+2*this.height},this.getTop=function(){return this.getMinY()},this.getBottom=function(){return this.getMaxY()},this.getLeft=function(){return this.getMinX()},this.getRight=function(){return this.getMaxX()},this.isAbove=function(t){return this.getBottom()<this.y},this.isBelow=function(t){return this.getTop()>this.y},this.isLeftOf=function(t){return this.getRight()<this.x},this.isRightOf=function(t){return this.getLeft()>this.x},this.getCenterX=function(){return this.getMinX()+(this.getMaxX()-this.getMinX())/2},this.getCenterY=function(){return this.getMinY()+(this.getMaxY()-this.getMinY())/2},this.getCenterPoint=function(){return new t(this.getCenterX(),this.getCenterY())},this.getTopLeftPoint=function(){return new t(this.getLeft(),this.getTop())},this.getTopCenterPoint=function(){return new t(this.getCenterX(),this.getTop())},this.getTopRightPoint=function(){return new t(this.getRight(),this.getTop())},this.getCenterLeftPoint=function(){return new t(this.getLeft(),this.getCenterY())},this.getCenterRightPoint=function(){return new t(this.getRight(),this.getCenterY())},this.getBottomLeftPoint=function(){return new t(this.getLeft(),this.getBottom())},this.getBottomCenterPoint=function(){return new t(this.getCenterX(),this.getBottom())},this.getBottomRightPoint=function(){return new t(this.getRight(),this.getBottom())},e instanceof Array?e.length>0&&"number"==typeof e[0]?this.getBoundsFromPoints(e[0],e[1],e[2],e[3]):this.getBoundsFromStrokes(e):this.getBoundsFromStroke(e)}return e}),define("core/data/container/main",["require","exports","module","./Drawable","./Point","./Stroke","./Substroke","./Shape","./Sketch","./BoundingBox"],function(t,e){e.Drawable=t("./Drawable"),e.Point=t("./Point"),e.Stroke=t("./Stroke"),e.Substroke=t("./Substroke"),e.Shape=t("./Shape"),e.Sketch=t("./Sketch"),e.BoundingBox=t("./BoundingBox")}),define("core/data/io/Serializer",["../container/Point","../container/Stroke","../container/Substroke","../container/Shape","../container/Sketch"],function(t,e,i,n,s){function o(){this.toSketchMLJSON=function(t){var e={};e.id=t.getId(),e.time=t.getTime().toString(),e.domain=t.getDomain(),e.points=[];var i=t.getPoints();for(var n in i){var s={};s.id=i[n].getId(),s.time=i[n].getTime().toString(),s.x=i[n].getX(),s.y=i[n].getY(),e.points.push(s)}e.strokes=[];var o=t.getStrokes();for(var n in o){var r={};r.id=o[n].getId(),r.time=o[n].getTime().toString(),r.points=[];for(var i=o[n].getPoints(),h=0;h<i.length;h++)r.points.push(i[h].getId());e.strokes.push(r)}e.substrokes=[];var a=t.getSubstrokes();for(var n in a){var r={};r.id=a[n].getId(),r.time=a[n].getTime().toString(),r.parent=a[n].getParent().getId(),r.points=[];for(var i=a[n].getPoints(),h=0;h<i.length;h++)r.points.push(i[h].getId());e.substrokes.push(r)}e.shapes=[];var u=t.getShapes();for(var n in u){var r={};r.id=u[n].getId(),r.time=u[n].getTime().toString(),r.interpretation=u[n].getInterpretation(),r.confidence=u[n].getConfidence().toString(),r.subElements=[];for(var c=u[n].getSubElements(),h=0;h<c.length;h++)r.subElements.push(c[h].getId());e.shapes.push(r)}return JSON.stringify(e)},this.toSketchMLJSONbasic=function(t,s){var o={};if(o.id=t.getId(),o.time=t.getTime(),o.strokes=[],s){if(s instanceof Array)for(var r=t.getShapes(),h=0;h<s.length;h++)for(var a=r[s[h]].getSubElements(),u=0;u<a.length;u++)if(a[u]instanceof n)s.indexOf(a[u].getId())<0&&s.push(a[u].getId());else if(a[u]instanceof e||a[u]instanceof i){var c={};c.id=a[u].getId(),c.time=a[u].getTime().toString(),c.points=[];for(var g=a[u].getPoints(),f=0;f<g.length;f++){var d={};d.id=g[f].getId(),d.time=g[f].getTime().toString(),d.x=g[f].getX(),d.y=g[f].getY(),c.points.push(d)}o.strokes.push(c)}}else{var p=t.getStrokes();for(var m in p){var g=p[m].getPoints(),c={};c.id=p[m].getId(),c.time=p[m].getTime().toString(),c.points=[];for(var h=0;h<g.length;h++){var d={};d.id=g[h].getId(),d.time=g[h].getTime().toString(),d.x=g[h].getX(),d.y=g[h].getY(),c.points.push(d)}o.strokes.push(c)}}return o.strokes=o.strokes.sort(function(t,e){return t.time-e.time}),JSON.stringify(o)}}return o}),define("core/data/io/Deserializer",["../container/Point","../container/Stroke","../container/Substroke","../container/Shape","../container/Sketch"],function(t,e,i,n,s){function o(){this.fromSketchMLJSON=function(o){for(var r=JSON.parse(o),h=r.points,a={},u=0;u<h.length;u++)a[h[u].id]=new t(parseInt(h[u].x),parseInt(h[u].y),parseInt(h[u].time),h[u].id);for(var c=r.strokes,g={},u=0;u<c.length;u++){g[c[u].id]=new e([],parseInt(c[u].time),c[u].id);for(var f=c[u].points,d=0;d<f.length;d++)g[c[u].id].addPoint(a[f[d]])}for(var p=r.substrokes,m={},u=0;u<p.length;u++){m[p[u].id]=new i(g[p[u].parent],[],parseInt(p[u].time),p[u].id);for(var S=p[u].points,d=0;d<S.length;d++)m[p[u].id].addPoint(a[S[d]])}for(var l=r.shapes,x={},u=0;u<l.length;u++)x[l[u].id]=new n(l[u].subElements,l[u].interpretation,parseFloat(l[u].confidence),parseInt(l[u].time),l[u].id);for(var I in x){for(var b=[],k=x[I].getSubElements(),d=0;d<k.length;d++){var v=a[k[d]];v||(v=g[k[d]],v||(v=m[k[d]],v||(v=x[k[d]]))),b.push(v)}x[I].setSubElements(b)}var P=new s(r.domain,a,g,m,x,parseInt(r.time),r.id);return P},this.fromSketchMLJSONbasic=function(i){var n=JSON.parse(i),o=n.id,r=n.time;n=n.strokes;for(var h={},a={},u=0;u<n.length;u++){for(var c=[],g=0;g<n[u].points.length;g++){var f=n[u].points[g];h[f.id]=new t(parseInt(f.x),parseInt(f.y),parseInt(f.time),f.id),c.push(h[f.id])}a[n[u].id]=new e(c,parseInt(n[u].time),n[u].id)}var d=new s([],h,a,{},{},r,o);return d}}return o}),define("core/data/io/main",["require","exports","module","./Serializer","./Deserializer"],function(t,e){e.Serializer=t("./Serializer"),e.Deserializer=t("./Deserializer")}),define("core/data/main",["require","exports","module","./container/main","./io/main"],function(t,e){e.container=t("./container/main"),e.io=t("./io/main")}),define("core/main",["require","exports","module","./data/main"],function(t,e){e.data=t("./data/main")}),define("srlib",["require","exports","module","./core/main"],function(t,e){e.core=t("./core/main")})}(require(["srlib"],function(t){"undefined"!=typeof window&&(window.srlib=t),"undefined"!=typeof global&&(global.srlib=t)}));