forked from Gregwar/blocks.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblocks.min.js
1 lines (1 loc) · 31.6 KB
/
blocks.min.js
1
"use strict";function arrayRemove(e,t){t=parseInt(t);var n=e[e.length-1];e[e.length-1]=e[t],e[t]=n,e.pop()}function ConnectorImport(e){if(!(e instanceof Array)||e.length<2||e.length>3)throw"Unable to import a connector";return e.length==2&&e.push(null),new Connector(e[0],e[1],e[2])}function IdToConnector(e){var t=e.split("_"),n=t[0],r=t[1],i=null;return t.length==3&&(i=t[2]),new Connector(n,r,i)}function EdgeImport(e,t){if(!1 in t)throw"An edge does not have id";var n=e.getBlockById(t.block1),r=e.getBlockById(t.block2);if(!n||!r)throw"Error while importing an edge, a block did not exists";return new Edge(t.id,n,ConnectorImport(t.connector1),r,ConnectorImport(t.connector2),e)}function BlockImport(e,t){for(var n in e.metas){var r=e.metas[n],i="module"in t?t.module:null;if(r.name==t.type&&r.module==i){var s=new Block(e,r,t.id);return s.x=t.x,s.y=t.y,s.setValues(t.values),s}}throw"Unable to create a block of type "+t.type}"use strict";var Field=function(e){var t=this;this.onUpdate=null,this.value=null,"defaultValue"in e&&(this.value=e.defaultValue),e.unit==undefined?this.unit="":this.unit=e.unit,this.dimension="dimension"in e?e.dimension:null,this.attrs=e.attrs,this.asTitle="asTitle"in e&&e.asTitle;if(e.type==undefined)this.type="string";else{var n=e.type.toLowerCase();n=Types.normalize(n),this.type=n}this.card="card"in e?e.card:"*",this.card=this.parseCardinality(this.card,this.is("output")),this.hide="hide"in e&&e.hide,this.hideLabel="hideLabel"in e&&e.hideLabel,this.name=e.name.toLowerCase(),this.label="label"in e?e.label:e.name,this.dynamicLabel="dynamicLabel"in e?e.dynamicLabel:null,this.choices="choices"in e?e.choices:null,this.extensible="extensible"in e&&e.extensible,this.size=1,this.isArray=this.type.substr(-2)=="[]",this.isArray&&(this.dimension==null&&(this.dimension=this.name),this.type=this.type.substr(0,this.type.length-2)),this.variadic=!!this.dimension,this.defaultValue="defaultValue"in e?e.defaultValue:null};Field.prototype.updated=function(){this.onUpdate&&this.onUpdate()},Field.prototype.getFieldHtml=function(){var e=this.label+":<br/>";if(this.isArray){e+='<div class="fieldsArray">',e+='<div class="pattern">',e+=this.getSingleFieldHtml(""),e+="</div>",e+='<div class="fields">';var t=this.getValue();for(var n in t)e+='<div class="field">',e+=this.getSingleFieldHtml(t[n]),e+="</div>";e+="</div>",e+="</div>"}else e+=this.getSingleFieldHtml();return e+="<br/>",e},Field.prototype.getFieldName=function(){var e=this.name;return this.isArray&&(e+="[]"),e},Field.prototype.getSingleFieldHtml=function(e){var t="";e==undefined&&(e=this.getPrintableValue());if(this.type=="longtext")t+='<textarea name="'+this.getFieldName()+'"></textarea>';else if(this.type=="choice"||this.choices){t+='<select name="'+this.getFieldName()+'">';for(var n in this.choices){var r=this.choices[n],i=r==e?"selected":"";t+="<option "+i+' value="'+r+'">'+r+"</option>"}t+="</select>"}else{var s=this.type=="bool"?"checkbox":"text";t+='<input value="'+e+'" type="'+s+'" name="'+this.getFieldName()+'" />'+this.unit}return t},Field.prototype.getHtml=function(){var e="";return this.hideLabel||(e+="<b>"+this.label+"</b>: "),e+=this.getPrintableValueWithUnit()+"<br/>",e},Field.prototype.getValue=function(){return this.value},Field.prototype.getPrintableValue=function(e){var t=this.getValue();return t instanceof Array&&(e==undefined?t=t.join(", "):t=t[e]),t},Field.prototype.getPrintableValueWithUnit=function(e){var t=this.getPrintableValue(e);return this.unit&&(t+=this.unit),t},Field.prototype.getLabel=function(){return this.label},Field.prototype.setValue=function(e){this.isArray&&!(e instanceof Array)&&(e=e.split(", ")),this.type=="bool"&&(e=!!e),this.value=e},Field.prototype.asDimension=function(){if(this.extensible)return this.size+1;if(this.isArray){var e=this.getValue();if(e instanceof Array)return this.getValue().length;throw"Unable to get the dimension of field "+this.name}return parseInt(this.getValue())},Field.prototype.getDimension=function(e){if(typeof this.dimension=="number")return this.dimension;var t=e.getField(this.dimension);if(!t)throw"Unable to find dimension field "+this.dimension;return t.asDimension()},Field.prototype.is=function(e){return e in this.attrs},Field.prototype.parseCardinality=function(e,t){var n=[0,1];t&&(n=[0,"*"]);if(e!=undefined)if(typeof e!="string")n=[0,e];else{var r=e.split("-");r.length==1?n=[0,r[0]]:n=r}for(var i in n)n[i]!="*"&&(n[i]=parseInt(n[i]));return n},"use strict";var Fields=function(e){var t=this;this.block=e,this.meta=this.block.meta,this.display=!1,this.div=null,this.fields=[];for(var n in this.meta.fields){var r=new Field(this.meta.fields[n]);r.onUpdate=function(){t.block.cssParameters()},this.fields.push(r)}this.inputs=[],this.outputs=[],this.editables=[],this.indexedFields={};for(var n in this.fields){var r=this.fields[n];this.indexedFields[r.name]=r,"editable"in r.attrs&&this.editables.push(r),"input"in r.attrs&&(this.inputs.push(r),r.hide=!0),"output"in r.attrs&&(this.outputs.push(r),r.hide=!0)}};Fields.prototype.getField=function(e){return e=e.toLowerCase(),e in this.indexedFields?this.indexedFields[e]:null},Fields.prototype.show=function(){var e=this,t="<h3>"+this.block.meta.name+"#"+this.block.id+"</h3>";t+='<form class="form">';for(var n in this.editables)t+=this.editables[n].getFieldHtml();t+='<input type="submit" style="display:none" width="0" height="0" />',t+="</form>",t+='<button class="save" href="javascript:void(0);">Save</button>',t+='<button class="close" href="javascript:void(0);">Close</button>',this.div.html(t),this.div.find(".close").click(function(){$.fancybox.close()});var r=this.div.find("form");this.div.find(".save").click(function(){r.find(".pattern").remove(),e.save(r.serializeForm()),$.fancybox.close()}),this.div.find("form").submit(function(){return r.find(".pattern").remove(),e.save($(this).serializeForm()),$.fancybox.close(),!1}),this.div.find("input").dblclick(function(){$(this).select()}),this.handleArrays(),$.fancybox.open(this.div,{wrapCSS:"blocks_js_modal"}),this.display=!0},Fields.prototype.handleArrays=function(){this.div.find(".fieldsArray").each(function(){var e=$(this).find(".pattern").html(),t=$(this).find(".fields"),n='<div class="buttons">';n+='<a class="add" href="#">Add</a> ',n+='<a class="remove" href="#">Remove</a>',n+="</div>",$(this).append(n),$(this).find(".add").click(function(){t.append('<div class="field">'+e+"</div>")}),$(this).find(".remove").click(function(){t.find(".field").last().remove()})})},Fields.prototype.getHtml=function(){var e="";for(var t in this.editables)e+=this.editables[t].getHtml();return e},Fields.prototype.hide=function(){this.div.hide(),this.display=!1},Fields.prototype.save=function(e){var t={};for(var n in e){var r=n,i=!1;r.substr(r.length-2,2)=="[]"&&(r=r.substr(0,r.length-2),i=!0),e[n]==null&&i&&(e[n]=[]),this.getField(r).setValue(e[n])}this.block.render(),this.block.redraw()},Fields.prototype.toggle=function(){this.meta.parametersEditor!=undefined&&typeof this.meta.parametersEditor=="function"?this.meta.parametersEditor(this.block.values,function(e){this.block.updateValues(e),this.block.render(),this.block.redraw()}):this.display?this.hide():this.show()},"use strict";var Segment=function(e,t,n,r){this.x=e,this.y=t,this.dx=n,this.dy=r};Segment.prototype.distance=function(e,t){return Math.sqrt(Math.pow(t.x-e.x,2)+Math.pow(t.y-e.y,2))},Segment.prototype.distanceP=function(e){var t=this.normal();t.x=e.x,t.y=e.y;var n=this.intersection(t);return[n[0],this.distance(t.alpha(n[1]),e)]},Segment.prototype.normal=function(){return new Segment(this.x,this.y,this.dy,-this.dx)},Segment.prototype.intersection=function(e){var t=this.dx,n=-e.dx,r=this.dy,i=-e.dy,s=e.x-this.x,o=e.y-this.y,u=t*i-n*r;if(u==0)return null;var a=(i*s-n*o)/u,f=(-r*s+t*o)/u;return[a,f]},Segment.prototype.alpha=function(e){var t={};return t.x=this.x+this.dx*e,t.y=this.y+this.dy*e,t},"use strict";var Types=function(){this.compatibles={}};Types.normalize=function(e){e=e.toLowerCase();if(e=="check"||e=="bool"||e=="checkbox")e="bool";e=="integer"&&(e="int");if(e=="float"||e=="double")e="number";e=="text"&&(e="string");if(e=="select"||e=="choices"||e=="combobox")e="choice";return e=="textarea"&&(e="longtext"),e},Types.prototype.isCompatible=function(e,t){e=Types.normalize(e),t=Types.normalize(t);if(e==t)return!0;if(e in this.compatibles)for(var n in this.compatibles[e])if(t==this.compatibles[e][n])return!0;return!1},Types.prototype.getCompatibles=function(e){e=Types.normalize(e);var t=[e];if(e in this.compatibles)for(var n in this.compatibles[e])t.push(this.compatibles[e][n]);return t},Types.prototype.addCompatibilityOneWay=function(e,t){e=Types.normalize(e),t=Types.normalize(t),e in this.compatibles||(this.compatibles[e]=[]),this.compatibles[e].push(t)},Types.prototype.addCompatibility=function(e,t){e=Types.normalize(e),t=Types.normalize(t),this.addCompatibilityOneWay(e,t),this.addCompatibilityOneWay(t,e)},"use strict";var Connector=function(e,t,n){this.name=e,this.type=t,this.index=n==null?null:parseInt(n)};Connector.prototype.id=function(){var e=this.name+"_"+this.type;return this.index!=null&&(e+="_"+this.index),e},Connector.prototype.isInput=function(){return this.type=="input"},Connector.prototype.isOutput=function(){return this.type=="output"},Connector.prototype.same=function(e){return this.name==e.name&&this.index==e.index&&this.type==e.type},Connector.prototype.export=function(){var e=[this.name,this.type];return this.index!==null&&e.push(this.index),e},"use strict";var Edge=function(e,t,n,r,i,s){this.blocks=s,this.label=null,this.id=parseInt(e),this.block1=t,this.connector1=n,this.block2=r,this.connector2=i,this.selected=!1,this.defaultSize=3,this.defaultFontSize=10,this.position1=null,this.position2=null,this.segment=null;if(!t.hasConnector(n)||!r.hasConnector(i))throw"Can't create edge because a connector don't exist"};Edge.prototype.isLoopable=function(){return this.block1.isLoopable()||this.block2.isLoopable()},Edge.prototype.fromTo=function(){return[this.block1,this.block2]},Edge.prototype.setLabel=function(e){this.label=e},Edge.prototype.draw=function(e){this.position1=this.block1.linkPositionFor(this.connector1),this.position2=this.block2.linkPositionFor(this.connector2),this.segment=new Segment(this.position1.x,this.position1.y,this.position2.x-this.position1.x,this.position2.y-this.position1.y);var t=this.defaultSize*this.blocks.scale;if(this.selected)var n="rgba(0, 200, 0, 1)";else var n="rgba(255, 200, 0, 1)";e.line(this.position1.x,this.position1.y,this.position2.x,this.position2.y,{stroke:n,strokeWidth:t});var r=(this.position1.x+this.position2.x)/2,i=(this.position1.y+this.position2.y)/2,s=Math.sqrt(Math.pow(this.position1.x-this.position2.x,2)+Math.pow(this.position1.y-this.position2.y,2)),o=30;o=o*Math.PI/180;var u=Math.cos(o),a=Math.sin(o),f=Math.cos(-o),l=Math.sin(-o);if(this.blocks.getOption("orientation",!0)){var c=(this.position1.x-r)*this.blocks.scale*10/(s/2),h=(this.position1.y-i)*this.blocks.scale*10/(s/2),t=this.defaultSize*this.blocks.scale/3;e.line(r,i,r+(c*u-h*a),i+(h*u+c*a),{stroke:n,strokeWidth:t}),e.line(r,i,r+(c*f-h*l),i+(h*f+c*l),{stroke:n,strokeWidth:t})}if(this.label!=null){var p=Math.round(this.defaultFontSize*this.blocks.scale);e.text(r-2*p,i+p/3,this.label,{fontSize:p+"px",fill:"#3a3b01",stroke:"#fff",strokeWidth:2}),e.text(r-2*p,i+p/3,this.label,{fontSize:p+"px",fill:"#3a3b01"})}},Edge.prototype.collide=function(e,t){var n=this.segment.distanceP({x:e,y:t});return n[0]>=0&&n[0]<=1&&n[1]<this.defaultSize*blocks.scale*2?n[0]:!1},Edge.prototype.create=function(){if(this.block1==this.block2)throw"You can't link a block to itself";if(!this.blocks.getOption("canLinkInputs",!1)&&this.connector1.type==this.connector2.type)throw"You have to link an input with an output";if(!this.block1.canLink(this.connector1)||!this.block2.canLink(this.connector2))throw"Can't create such an edge because of the cardinalities";this.block1.addEdge(this.connector1,this),this.block2.addEdge(this.connector2,this),this.block1.render(),this.block2.render()},Edge.prototype.getTypes=function(){return[this.block1.getField(this.connector1.name).type,this.block2.getField(this.connector2.name).type]},Edge.prototype.erase=function(){this.block1.eraseEdge(this.connector1,this),this.block2.eraseEdge(this.connector2,this),this.block1.render(),this.block2.render()},Edge.prototype.same=function(e){return this.block1==e.block1&&this.block2==e.block2&&this.connector1.same(e.connector1)&&this.connector2.same(e.connector2)?!0:this.block1==e.block1&&this.block2==e.block2&&this.connector1.same(e.connector2)&&this.connector2.same(e.connector1)?!0:!1},Edge.prototype.export=function(){return{id:this.id,block1:this.block1.id,connector1:this.connector1.export(),block2:this.block2.id,connector2:this.connector2.export()}},"use strict";var BlocksMessages=function(e,t){var n=this;this.hideTimer=null,this.messages=e,this.width=t,e.click(function(){n.hide()})};BlocksMessages.prototype.show=function(e,t){var n=this;this.hideTimer!=null&&clearTimeout(this.hideTimer);var r="message";t["class"]!=undefined&&(r+=" "+t["class"]);var i='<div class="'+r+'">'+e+"</div>";this.messages.html(i),this.messages.fadeIn(),this.messages.css("margin-left",Math.round((this.width-350)/2)+"px"),this.messages.css("margin-top","20px"),this.hideTimer=setTimeout(function(){n.hide()},5e3)},BlocksMessages.prototype.hide=function(){this.messages.fadeOut(),this.hideTimer=null},"use strict";var BlocksMenu=function(e){var t=this;this.visible=!1,this.position={},this.blocks=e,this.menu=e.div.find(".contextmenu"),this.actions=[{label:"Compact",action:function(e){e.toggleCompact()},icon:"compact"},{label:"Scale",action:function(e){e.perfectScale()},icon:"scale"}],e.div.bind("contextmenu",function(){if(t.visible)t.hide();else{t.position=e.getPosition();var n={};for(var r in e.metas){var i=e.metas[r];i.family in n?n[i.family].push(i):n[i.family]=[i]}var s="";for(var o in t.actions){var u="none";"icon"in t.actions[o]&&(u=t.actions[o].icon),s+='<div rel="'+o+'" class="menuentry menu_action_'+o+'"><div class="menu_icon menu_icon_'+u+'"></div>'+t.actions[o].label+"</div>"}for(var a in n){if(a){var f=a.replace(/[^a-zA-Z]/g,"");s+='<div class="family">',s+='<div class="familyName family_'+a+'"><div class="menu_icon menu_icon_family_'+f+'"></div>'+a+" <span>»</span></div>",s+='<div class="childs">'}for(var r in n[a]){var l=n[a][r];s+='<div class="type type_'+l.name+'" rel="'+l.name+'">'+l.name+"</div>"}a&&(s+="</div>",s+="</div>")}t.menu.find(".types").html(s),t.show(),t.menu.find(".type").click(function(){e.addBlock($(this).attr("rel"),t.position.x,t.position.y),t.hide()}),t.menu.find(".family").each(function(){var e=$(this);$(this).find(".familyName").hover(function(){t.menu.find(".childs").hide(),e.find(".childs").show()})});for(var r in t.actions)t.menu.find(".menu_action_"+r).click(function(){var n=t.actions[$(this).attr("rel")];n.action(e),t.hide()});$(this).find(".types > .type").hover(function(){t.menu.find(".childs").hide()})}return!1})};BlocksMenu.prototype.addAction=function(e,t,n){this.actions.push({label:e,action:t,icon:n})},BlocksMenu.prototype.hide=function(){this.menu.hide(),this.visible=!1},BlocksMenu.prototype.show=function(){this.menu.css("margin-left",5+this.blocks.mouseX+"px"),this.menu.css("margin-top",5+this.blocks.mouseY+"px"),this.menu.show(),this.visible=!0},"use strict";var Meta=function(e){var t=this;this.name=e.name,this.loopable="loopable"in e&&e.loopable,this.size="size"in e?e.size:"normal",this.fields=[];if("fields"in e)for(var n in e.fields){var r=e.fields[n],i="attrs"in r?r.attrs.split(" "):[];r.attrs={};for(var s in i)r.attrs[i[s]]=!0;this.fields.push(r)}for(var n in e.parametersEditor){var o=keys[n];e[o]!=undefined?this[o]=e[o]:this[o]=[]}"module"in e?this.module=e.module:this.module=null,e.family==undefined?this.family="":this.family=e.family,e["class"]!=undefined?this["class"]=e["class"]:this["class"]="",this.description=null,e.description!=undefined&&(this.description=e.description)};"use strict";var History=function(e){var t=this;this.historySize=30,this.blocks=e,this.history=[],this.historyPos=0,this.ctrlDown=!1,$(document).keydown(function(e){e.keyCode==17&&(t.ctrlDown=!0),e.keyCode==90&&t.ctrlDown&&t.restoreLast()}),$(document).keyup(function(e){e.keyCode==17&&(t.ctrlDown=!1)})};History.prototype.save=function(){this.history.push(this.blocks.export()),this.history.length>this.historySize&&this.history.shift()},History.prototype.restoreLast=function(){if(this.history.length){var e=this.history.pop();this.blocks.importData(e)}else alert("Nothing to get")},"use strict";var Block=function(e,t,n){this.blocks=e,this.meta=t,this.defaultFont=10,this.description=null,this.meta.size=="normal"?this.width=150:this.meta.size=="small"?this.width=100:this.width=this.meta.size,this.historySaved=!1,this.id=n,this.hasFocus=!1,this.div=null,this.drag=null,this.lastScale=null,this.fields=new Fields(this),this.x=0,this.y=0,this.ios={},this.focusedConnector=null,this.edges={},this.connectors=[]};Block.prototype.isLoopable=function(){return this.meta.loopable},Block.prototype.setDescription=function(e){this.description=e,this.div.find(".description").html(e)},Block.prototype.updateValues=function(){this.blocks.history.save()},Block.prototype.setValues=function(e){for(var t in e)this.fields.getField(t).setValue(e[t])},Block.prototype.getValues=function(e){var e={};for(var t in this.fields.editables){var n=this.fields.editables[t];e[n.name]=n.getValue()}return e},Block.prototype.getValue=function(e){var t=this.fields.getField(e);return t?t.getValue():null},Block.prototype.htmlentities=function(e){return e=e.replace(/</,"<"),e=e.replace(/>/,">"),e},Block.prototype.setInfos=function(e){this.div.find(".infos").html(e)},Block.prototype.getHtml=function(){var e=this;this.connectors=[];var t=this.meta.name+'<span class="blockId">#'+this.id+"</span>";for(var n in this.fields.fields){var r=this.fields.fields[n];r.asTitle&&(t=r.getPrintableValue())}var i='<div class="parameters"></div>';i+='<div class="blockTitle"><span class="titleText">'+t+'</span><div class="blockicon delete"></div>',i+='<div class="blockicon info"></div>',this.description?i+='<div class="description">'+e.description+"</div>":this.meta.description?i+='<div class="description">'+this.meta.description+"</div>":i+='<div class="description">No description</div>',i+='<div class="blockicon settings"></div></div>',i+='<div class="infos"></div>';for(var n in e.fields.editables){var r=e.fields.editables[n],s=r.getHtml();i&&!r.hide&&!r.asTitle&&!this.blocks.compactMode&&(i+='<div class="parameter">'+s+"</div>")}var o=function(t,n){i+='<div class="'+t+"s "+(e.isLoopable()?"loopable":"")+'">';for(var r in n){var s=n[r];s.extensible&&(s.size=e.maxEntry(s.name));var o=1;s.variadic&&(o=s.getDimension(e.fields));for(var u=0;u<o;u++){var a=s.name.toLowerCase()+"_"+t,f=s.getLabel().replace("#",u+1),l="";s.dynamicLabel!=null?f=String(s.dynamicLabel(e,u)):s&&s.is("editable")&&(l=" ("+s.getPrintableValueWithUnit(s.variadic?u:undefined)+")"),s.variadic&&(a+="_"+u),i+='<div class="'+t+" type_"+s.type+" connector "+a+'" rel="'+a+'"><div class="circle"></div>'+e.htmlentities(f)+l+"</div>",e.connectors.push(a)}}i+="</div>"};return o("input",this.fields.inputs),o("output",this.fields.outputs),i},Block.prototype.render=function(){this.lastScale=null,this.hasFocus=!1,this.div.html(this.getHtml()),this.initListeners(),this.redraw()},Block.prototype.maxEntry=function(e){var t=0;for(var n in this.edges)if(this.edges[n].length){var r=IdToConnector(n);r.name==e&&(t=Math.max(parseInt(r.index)+1,t))}return t},Block.prototype.create=function(e){var t='<div id="block'+this.id+'" class="block '+this.meta["class"]+'"></div>';e.append(t),this.div=e.find("#block"+this.id),this.render()},Block.prototype.redraw=function(e){this.div.css("margin-left",this.blocks.center.x+this.x*this.blocks.scale+"px"),this.div.css("margin-top",this.blocks.center.y+this.y*this.blocks.scale+"px"),this.blocks.showIcons&&this.blocks.scale>.8?this.div.find(".blockicon").show():this.div.find(".blockicon").hide();if(this.lastScale!=this.blocks.scale){this.div.css("font-size",Math.round(this.blocks.scale*this.defaultFont)+"px"),this.div.css("width",Math.round(this.blocks.scale*this.width)+"px");var t=Math.round(12*this.blocks.scale);this.div.find(".circle").css("width",t+"px"),this.div.find(".circle").css("height",t+"px"),this.div.find(".circle").css("background-size",t+"px "+t+"px"),this.div.find(".inputs, .outputs").width(this.div.width()/2-10),this.cssParameters(),this.lastScale=this.blocks.scale}for(var n in this.connectors){var r=this.connectors[n],i=this.div.find("."+r),s=i.find(".circle");s.removeClass("io_active"),s.removeClass("io_selected");if(r in this.edges&&this.edges[r].length){s.addClass("io_active");for(var o in this.edges[r])this.edges[r][o].selected&&s.addClass("io_selected")}}this.fields.div=this.div.find(".parameters"),this.div.removeClass("block_selected"),e&&this.div.addClass("block_selected")},Block.prototype.cssParameters=function(){this.div.find("input").css("font-size",Math.round(this.blocks.scale*this.defaultFont)+"px")},Block.prototype.initListeners=function(){var e=this;e.div.find(".blockTitle").mousedown(function(t){t.which==1&&(e.historySaved=!1,e.drag=[e.blocks.mouseX/e.blocks.scale-e.x,e.blocks.mouseY/e.blocks.scale-e.y])}),e.div.hover(function(){e.hasFocus=!0},function(){e.hasFocus=!1}),e.div.find(".connector").hover(function(){e.focusedConnector=$(this).attr("rel")},function(){e.focusedConnector=null}),$("html").mousemove(function(t){e.drag&&(e.historySaved||(e.blocks.history.save(),e.historySaved=!0),e.x=e.blocks.mouseX/e.blocks.scale-e.drag[0],e.y=e.blocks.mouseY/e.blocks.scale-e.drag[1],e.blocks.redraw())}),$("html").mouseup(function(){e.drag=null}),e.div.find(".connector").mousedown(function(t){t.which==1&&(e.blocks.beginLink(e,$(this).attr("rel")),t.preventDefault())}),e.div.find(".settings").click(function(){e.fields.toggle(),e.cssParameters()}),e.div.find(".delete").click(function(){e.blocks.removeBlock(e.blocks.getBlockId(e))}),e.div.find(".info").hover(function(){e.div.find(".description").show()},function(){e.div.find(".description").hide()})},Block.prototype.linkPositionFor=function(e){var t=e;e instanceof Object&&(t=e.id());try{var n=this.div.find("."+t+" .circle"),r=n.offset().left-this.blocks.div.offset().left+n.width()/2,i=n.offset().top-this.blocks.div.offset().top+n.height()/2}catch(s){throw"Unable to find link position for "+t+" ("+s+")"}return{x:r,y:i}},Block.prototype.canLink=function(e){var t=[],n=e.id();n in this.edges&&(t=this.edges[n]);var r=this.fields.getField(e.name).card;return r[1]=="*"?!0:t.length<r[1]},Block.prototype.addEdge=function(e,t){var n=[],r=e.id();this.edges[r]!=undefined&&(n=this.edges[r]),n.push(t),this.edges[r]=n},Block.prototype.eraseEdge=function(e,t){var n=e.id();if(this.edges[n]!=undefined)for(var r in this.edges[n])if(this.edges[n][r]==t){arrayRemove(this.edges[n],r);break}},Block.prototype.erase=function(){this.div.remove()},Block.prototype.allSuccessors=function(){var e={},t=[this],n=[this.id];e[this.id]=!0;while(t.length>0){var r=t.pop();for(var i in r.edges)for(var s in r.edges[i]){var o=r.edges[i][s];if(o.isLoopable())continue;var u=o.fromTo();if(u[0]==r){var a=u[1];a.id in e||(e[a.id]=!0,t.push(a),n.push(a.id))}}}return n},Block.prototype.export=function(){return{id:this.id,x:this.x,y:this.y,type:this.meta.name,module:this.meta.module,values:this.getValues()}},Block.prototype.getField=function(e){return this.fields.getField(e)},Block.prototype.hasConnector=function(e){var t=this.getField(e.name);return t?t.variadic?e.index!=null&&t.getDimension(this.fields)>=e.index:e.index==null:!1},"use strict";var Blocks=function(e){typeof e!="undefined"?this.options=e:this.options={},this.types=new Types,this.center={},this.scale=1,this.redrawTimeout=null,this.history=null,this.moving=null,this.isReady=!1,this.compactMode=!1,this.menu=null,this.mouseX=0,this.mouseY=0,this.linking=null,this.selectedLink=null,this.selectedSide=null,this.selectedBlock=null,this.div=null,this.context=null,this.metas=[],this.blocks=[],this.edges=[],this.id=1,this.edgeId=1,this.clear=function(){this.edges=[],this.blocks=[],this.id=1,this.edgeId=1,this.div.find(".blocks").html(""),this.redraw()},this.getOption=function(e,t){return e in this.options?this.options[e]:t},this.showIcons=!0};Blocks.prototype.run=function(e){var t=this;$(document).ready(function(){t.div=$(e),t.div.size()||alert("blocks.js: Unable to find "+e),t.div.html('<div class="blocks_js_editor"><div class="messages"></div><div class="contextmenu"><div class="types"></div></div><svg xmlns="http://www.w3.org/2000/svg" version="1.1"></svg><div class="blocks"></div></div>'),t.div.find("svg").css("width","100%"),t.div.find("svg").css("height","100%"),t.context=t.div.find("svg"),t.center.x=t.div.width()/2,t.center.y=t.div.height()/2,t.menu=new BlocksMenu(t),t.messages=new BlocksMessages(t.div.find(".messages"),t.div.width()),t.div[0].addEventListener("mousemove",function(e){t.mouseX=e.pageX-t.div.offset().left,t.mouseY=e.pageY-t.div.offset().top,t.move(e)}),$("html").mouseup(function(e){e.which==1&&t.release()}),t.div.mousedown(function(e){t.canvasClicked()&&e.preventDefault();if(e.which==2||!t.selectedLink&&!t.selectedBlock&&e.which==1)t.moving=[t.mouseX,t.mouseY]}),t.div.mouseup(function(e){if(e.which==2||e.which==1)t.moving=null;e.which==1&&t.menu.hide()}),t.context.svg(),$(document).keydown(function(e){if($("input").is(":focus"))return;e.keyCode==46&&t.deleteEvent()}),t.div.bind("mousewheel",function(e,n,r,i){var s=t.mouseX-t.center.x,o=t.mouseY-t.center.y,u=Math.pow(1.1,i);t.center.x-=s*(u-1),t.center.y-=o*(u-1),t.scale*=u,t.redraw(),e.preventDefault()}),t.history=new History(t),t.isReady||t.postReady()})},Blocks.prototype.postReady=function(){this.isReady=!0;if(this.readyQueue!=undefined)for(var e in this.readyQueue)this.readyQueue[e]()},Blocks.prototype.ready=function(e){if(this.isReady)e();else{var t=[];this.readyQueue!=undefined&&(t=this.readyQueue),t.push(e),this.readyQueue=t}},Blocks.prototype.getPosition=function(){var e={};return e.x=(this.mouseX-this.center.x)/this.scale,e.y=(this.mouseY-this.center.y)/this.scale,e},Blocks.prototype.addBlock=function(e,t,n){for(var r in this.metas){var i=this.metas[r];if(i.name==e){var s=new Block(this,this.metas[r],this.id);s.x=t,s.y=n,s.create(this.div.find(".blocks")),this.history.save(),this.blocks.push(s),this.id++}}},Blocks.prototype.register=function(e){this.metas.push(new Meta(e))},Blocks.prototype.beginLink=function(e,t){this.linking=[e,t],this.highlightTargets()},Blocks.prototype.highlightTargets=function(){var e=this.linking[0],t=IdToConnector(this.linking[1]),n=e.getField(t.name).type;$(".connector").addClass("disabled");var r=this.types.getCompatibles(n);for(var i in r){var s=r[i];$(".connector.type_"+s).removeClass("disabled")}},Blocks.prototype.move=function(){if(this.selectedSide){var e=Math.sqrt(Math.pow(this.mouseX-this.selectedSide[1],2)+Math.pow(this.mouseY-this.selectedSide[2],2));if(e>15){var t=this.edges[this.selectedLink];this.selectedSide[0]==2?this.linking=[t.block1,t.connector1.id()]:this.linking=[t.block2,t.connector2.id()],this.removeEdge(this.selectedLink),this.selectedSide=null,this.highlightTargets(),this.selectedLink!=null&&(this.edges[this.selectedLink].selected=!1),this.selectedLink=null,this.redraw()}}this.moving&&(this.center.x+=this.mouseX-this.moving[0],this.center.y+=this.mouseY-this.moving[1],this.moving=[this.mouseX,this.mouseY],this.redraw()),this.linking&&this.redraw()},Blocks.prototype.canvasClicked=function(){var e=!1;this.selectedBlock=null,this.selectedLink!=null&&(this.edges[this.selectedLink].selected=!1),this.selectedLink=null,this.selectedSide=null;for(var t in this.blocks){var n=this.blocks[t];n.hasFocus&&(this.selectedBlock=t)}if(!this.selectedBlock)for(var t in this.edges){var r=this.edges[t].collide(this.mouseX,this.mouseY);if(r!=0){r<.2?this.selectedSide=[1,this.mouseX,this.mouseY]:r>.8&&(this.selectedSide=[2,this.mouseX,this.mouseY]),this.selectedLink=t,this.edges[t].selected=!0,e=!0;break}}return this.redraw(),e},Blocks.prototype.removeEdge=function(e){this.history.save(),this.edges[e].erase(),arrayRemove(this.edges,e)},Blocks.prototype.getEdgeId=function(e){for(var t in this.edges)if(e==this.edges[t])return t;return!1},Blocks.prototype.removeBlock=function(e){var t=this.blocks[e],n=[];for(var r in this.edges){var i=this.edges[r];i.block1==t||i.block2==t?i.erase():n.push(i)}this.edges=n,t.erase(),arrayRemove(this.blocks,this.selectedBlock),this.redraw()},Blocks.prototype.getBlockId=function(e){for(var t in this.blocks)if(this.blocks[t]==e)return t;return null},Blocks.prototype.getBlockById=function(e){for(var t in this.blocks)if(this.blocks[t].id==e)return this.blocks[t];return null},Blocks.prototype.deleteEvent=function(){this.selectedBlock!=null&&(this.history.save(),this.removeBlock(this.selectedBlock),this.selectedBlock=null),this.selectedLink!=null&&(this.history.save(),this.removeEdge(this.selectedLink),this.selectedLink=null,this.redraw())},Blocks.prototype.doRedraw=function(){for(var e in this.blocks)this.blocks[e].redraw(this.selectedBlock==e);var t=this.context.svg("get");t.clear();for(var e in this.edges)this.edges[e].draw(t);if(this.linking)try{var n=this.linking[0].linkPositionFor(this.linking[1]);t.line(n.x,n.y,this.mouseX,this.mouseY,{stroke:"rgba(0,0,0,0.4)",strokeWidth:3*this.scale})}catch(r){this.linking=null}this.redrawTimeout=null},Blocks.prototype.redraw=function(){var e=this;this.redrawTimeout||(this.redrawTimeout=setTimeout(function(){e.doRedraw()},25))},Blocks.prototype.release=function(){this.linking&&(this.tryEndLink(),this.linking=null),$(".connector").removeClass("disabled"),this.redraw()},Blocks.prototype.tryEndLink=function(){for(var e in this.blocks){var t=this.blocks[e];if(t.hasFocus&&t.focusedConnector){this.endLink(t,t.focusedConnector);break}}},Blocks.prototype.endLink=function(e,t){try{var n=this.edgeId++,r=this.linking[0],i=IdToConnector(this.linking[1]),s=e,o=IdToConnector(t);if(i.isOutput())var u=new Edge(n,r,i,s,o,this);else var u=new Edge(n,s,o,r,i,this);for(var a in this.edges){var f=this.edges[a];if(f.same(u))throw"This edge already exists"}var l=u.fromTo();if(l[1].allSuccessors().indexOf(l[0].id)!=-1)throw"You can not create a loop";this.history.save(),u.create();var c=this.edges.push(u)-1,h=u.getTypes();if(!this.types.isCompatible(h[0],h[1]))throw this.removeEdge(c),"Types "+h[0]+" and "+h[1]+" are not compatible"}catch(p){this.messages.show("Unable to create this edge :\n"+p,{"class":"error"})}this.linking=null,this.selectedBlock=null,this.redraw()},Blocks.prototype.toggleCompact=function(){this.compactMode=!this.compactMode;for(var e in this.blocks)this.blocks[e].render();this.redraw()},Blocks.prototype.export=function(){var e=[],t=[];for(var n in this.blocks)e.push(this.blocks[n].export());for(var n in this.edges)t.push(this.edges[n].export());return{edges:t,blocks:e}},Blocks.prototype.importData=function(e){this.clear(),this.doLoad(e,!1)},Blocks.prototype.load=function(e){this.doLoad(e,!0)},Blocks.prototype.doLoad=function(e,t){var n=this;this.ready(function(){var r=[];n.id=1,n.edgeId=1;for(var i in e.blocks)try{var s=e.blocks[i],o=BlockImport(n,s);n.id=Math.max(n.id,o.id+1),o.create(n.div.find(".blocks")),n.blocks.push(o)}catch(u){r.push("Block #"+i+":"+u)}for(var i in e.edges)try{var s=e.edges[i],a=EdgeImport(n,s);n.edgeId=Math.max(n.edgeId,a.id+1),a.create(),n.edges.push(a)}catch(u){r.push("Edge #"+i+" :"+u)}if(r.length){var f=r.length+" loading errors :<br/>";f+="<ul>";for(var i in r)f+="<li>"+r[i]+"</li>";f+="</ul>",n.messages.show(f,{"class":"error"})}n.redraw(),t&&n.perfectScale()})},Blocks.prototype.perfectScale=function(){if(!this.div)return;var e=null,t=null,n=null,r=null;for(var i in this.blocks){var s=this.blocks[i];e==null?(e=t=s.x,n=r=s.y):(e=Math.min(e,s.x-15),t=Math.max(t,s.x+s.width+18),n=Math.min(n,s.y-15),r=Math.max(r,s.y+115))}var o=this.div.width()/(t-e),u=this.div.height()/(r-n),a=Math.min(o,u);this.scale=a,this.center.x=this.div.width()/2-a*(e+t)/2,this.center.y=this.div.height()/2-a*(n+r)/2,this.redraw()},Blocks.prototype.setLabels=function(e){for(var t in this.edges){var n=this.edges[t];n.id in e&&n.setLabel(e[t])}};