svgedit/dist/editor/system/extensions/ext-connector/ext-connector.js

11 lines
8.3 KiB
JavaScript

System.register([],(function(t,e){"use strict";return{execute:function(){function asyncGeneratorStep(t,e,r,n,o,a,i){try{var s=t[a](i),c=s.value}catch(t){return void r(t)}s.done?e(c):Promise.resolve(c).then(n,o)}
/**
* @file ext-connector.js
*
* @license MIT
*
* @copyright 2010 Alexis Deveria
*
*/
t("default",{name:"connector",init:function init(t){var r=this;return function _asyncToGenerator(t){return function(){var e=this,r=arguments;return new Promise((function(n,o){var a=t.apply(e,r);function _next(t){asyncGeneratorStep(a,n,o,_next,_throw,"next",t)}function _throw(t){asyncGeneratorStep(a,n,o,_next,_throw,"throw",t)}_next(void 0)}))}}(regeneratorRuntime.mark((function _callee(){var n,o,a,i,s,c,u,l,d,g,f,m,h,v,_,b,p,y,x,k,S,A,w,B,E,N,I,M,C,G;return regeneratorRuntime.wrap((function _callee$(j){for(;;)switch(j.prev=j.next){case 0:return I=function _init(){i(b).find("*").each((function(){var t=this.getAttributeNS(_,"connector");if(t){this.setAttribute("class",l.substr(1));var e=t.split(" "),r=o.getStrokedBBox([a(e[0])]),n=o.getStrokedBBox([a(e[1])]);i(this).data("c_start",e[0]).data("c_end",e[1]).data("start_bb",r).data("end_bb",n),o.getEditorNS(!0)}}))},N=function _updateConnectors(t){if(E(t),y.length)for(var e=y.length;e--;){var r=y[e],n=r.connector,a=r.elem,i=r.is_start?"start":"end",s=o.getStrokedBBox([a]);s.x=r.start_x,s.y=r.start_y,d(n,i+"_bb",s),d(n,i+"_off");var c=r.is_start?"end":"start",u=d(n,c+"_bb"),l=u.x+u.width/2,g=u.y+u.height/2,f=k(l,g,s,S(i,n));w(n,r.is_start?0:"end",f.x,f.y,!0);var m=k(f.x,f.y,d(n,c+"_bb"),S(c,n));if(w(n,r.is_start?"end":0,m.x,m.y,!0),navigator.userAgent.includes("AppleWebKit")){for(var h=n.points,v=h.numberOfItems,_=[],b=0;b<v;b++)f=h.getItem(b),_[b]=f.x+","+f.y;n.setAttribute("points",_.join(" "))}}},E=function _findConnectors(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:x,e=i(b).find(l);y=[],e.each((function(){var e;function add(){t.includes(this)&&(e=!0)}var r=[];["start","end"].forEach((function(t,e){var n=d(this,"c_"+t);null==n?(n=document.getElementById(this.attributes["se:connector"].value.split(" ")[e]),d(this,"c_"+t,n.id),d(this,t+"_bb",o.getStrokedBBox([n]))):n=document.getElementById(n),r.push(n)}),this);for(var n=0;n<2;n++){var a=r[n];if(e=!1,i(a).parents().each(add),a&&a.parentNode){if(t.includes(a)||e){var s=o.getStrokedBBox([a]);y.push({elem:a,connector:this,is_start:0===n,start_x:s.x,start_y:s.y})}}else i(this).remove()}}))},B=function _updateLine(t,e){for(var r=y.length;r--;){var n=y[r],o=n.connector,a=n.is_start?"start":"end",i=d(o,a+"_bb");i.x=n.start_x+t,i.y=n.start_y+e,d(o,a+"_bb",i);var s=n.is_start?"end":"start",c=d(o,s+"_bb"),u=c.x+c.width/2,l=c.y+c.height/2,g=k(u,l,i,S(a,o));w(o,n.is_start?0:"end",g.x,g.y,!0);var f=k(g.x,g.y,d(o,s+"_bb"),S(s,o));w(o,n.is_start?"end":0,f.x,f.y,!0)}},w=function _setPoint(t,e,r,n,o){var a=t.points,i=s.createSVGPoint();i.x=r,i.y=n,"end"===e&&(e=a.numberOfItems-1);try{a.replaceItem(i,e)}catch(o){for(var c=t.getAttribute("points").split(" "),u=0;u<c.length;u++)u===e&&(c[u]=r+","+n);t.setAttribute("points",c.join(" "))}if(o){var l=a.getItem(0),d=a.getItem(a.numberOfItems-1);w(t,1,(d.x+l.x)/2,(d.y+l.y)/2)}},A=function _showPanel(t){var e=i("#connector_rules");e.length||(e=i('<style id="connector_rules"></style>').appendTo("head")),e.text(t?"#tool_clone, #tool_topath, #tool_angle, #xy_panel { display: none !important; }":""),i("#connector_panel").toggle(t)},S=function _getOffset(t,e){var r=e.getAttribute("marker-"+t),n=5*e.getAttribute("stroke-width");return r?n:0},k=function _getBBintersect(t,e,r,n){n&&(n-=0,(r=i.extend({},r)).width+=n,r.height+=n,r.x-=n/2,r.y-=n/2);var o,a=r.x+r.width/2,s=r.y+r.height/2,c=t-a,u=e-s;return{x:a+c*(o=Math.abs(u/c)<r.height/r.width?r.width/2/Math.abs(c):u?r.height/2/Math.abs(u):0),y:s+u*o}},o=(n=r).canvas,a=o.getElem,i=t.$,s=t.svgroot,c=o.addSVGElementFromJson,u=t.selectorManager,l=".se_connector",d=i.data,b=t.svgcontent,p=!1,y=[],x=[],function(){var t=o.groupSelectedElements;o.groupSelectedElements=function(){o.removeFromSelection(i(l).toArray());for(var e=arguments.length,r=new Array(e),n=0;n<e;n++)r[n]=arguments[n];return t.apply(this,r)};var e=o.moveSelectedElements;o.moveSelectedElements=function(){for(var t=arguments.length,r=new Array(t),n=0;n<t;n++)r[n]=arguments[n];var o=e.apply(this,r);return N(),o},_=o.getEditorNS()}(),M=[{id:"mode_connect",type:"mode",icon:n.curConfig.imgPath+"cut.png",includeWith:{button:"#tool_line",isDefault:!1,position:1},events:{click:function click(){o.setMode("connector")}}}],j.next=17,e.import("./locale/".concat(n.curPrefs.lang,".js"));case 17:return C=j.sent,G=C.default,j.abrupt("return",{name:G.name,svgicons:"conn.svg",buttons:G.buttons.map((function(t,e){return Object.assign(M[e],t)})),addLangData:function addLangData(t){t.lang;return{data:G.langList}},mouseDown:function mouseDown(t){var e=t.event;g=t.start_x,f=t.start_y;var r=o.getMode(),a=n.curConfig.initStroke;if("connector"===r){if(p)return;var s=e.target,u=i(s).parents();if(-1!==i.inArray(b,u)){var l=i(s).closest("foreignObject");h=l.length?l[0]:s;var v=o.getStrokedBBox([h]),_=v.x+v.width/2,y=v.y+v.height/2;p=!0,m=c({element:"polyline",attr:{id:o.getNextId(),points:_+","+y+" "+_+","+y+" "+g+","+f,stroke:"#"+a.color,"stroke-width":h.stroke_width&&0!==h.stroke_width?h.stroke_width:a.width,fill:"none",opacity:a.opacity,style:"pointer-events:none"}}),d(m,"start_bb",v)}return{started:!0}}"select"===r&&E()},mouseMove:function mouseMove(t){var e=o.getZoom(),r=t.mouse_x/e,n=t.mouse_y/e,a=r-g,i=n-f,s=o.getMode();if("connector"===s&&p){var c=k(r,n,d(m,"start_bb"),S("start",m));g=c.x,f=c.y,w(m,0,c.x,c.y,!0),w(m,"end",r,n,!0)}else if("select"===s){for(var u=x.length;u--;){var l=x[u];l&&d(l,"c_start")&&(o.removeFromSelection([l]),o.getTransformList(l).clear())}y.length&&B(a,i)}},mouseUp:function mouseUp(t){var e=t.event.target;if("connector"===o.getMode()){var r=i(e).closest("foreignObject");r.length&&(e=r[0]);var n=i(e).parents();if(e===h)return{keep:!0,element:null,started:p=!0};if(-1===i.inArray(b,n))return i(m).remove(),{keep:!1,element:null,started:p=!1};v=e;var a=h.id,s=v.id,c=a+" "+s,d=s+" "+a;if(i(b).find(l).filter((function(){var t=this.getAttributeNS(_,"connector");return t===c||t===d})).length)return i(m).remove(),{keep:!1,element:null,started:!1};var y=o.getStrokedBBox([v]),x=k(g,f,y,S("start",m));return w(m,"end",x.x,x.y,!0),i(m).data("c_start",a).data("c_end",s).data("end_bb",y),_=o.getEditorNS(!0),m.setAttributeNS(_,"se:connector",c),m.setAttribute("class",l.substr(1)),m.setAttribute("opacity",1),o.addToSelection([m]),o.moveToBottomSelectedElement(),u.requestSelector(m).showGrips(!1),{keep:!0,element:m,started:p=!1}}},selectedChanged:function selectedChanged(t){if(i(b).find(l).length){"connector"===o.getMode()&&o.setMode("select");for(var e=(x=t.elems).length;e--;){var r=x[e];r&&d(r,"c_start")?(u.requestSelector(r).showGrips(!1),t.selectedElement&&!t.multiselected?A(!0):A(!1)):A(!1)}N()}},elementChanged:function elementChanged(t){var e=t.elems[0];if(e){if("svg"===e.tagName&&"svgcontent"===e.id&&(b=e,I()),e.getAttribute("marker-start")||e.getAttribute("marker-mid")||e.getAttribute("marker-end")){var r=e.getAttribute("marker-start"),n=e.getAttribute("marker-mid"),s=e.getAttribute("marker-end");if(m=e,i(e).data("start_off",Boolean(r)).data("end_off",Boolean(s)),"line"===e.tagName&&n){var u=Number(e.getAttribute("x1")),g=Number(e.getAttribute("x2")),f=Number(e.getAttribute("y1")),h=Number(e.getAttribute("y2")),v=e.id,_=c({element:"polyline",attr:{points:u+","+f+(" "+(u+g)/2+","+(f+h)/2+" ")+g+","+h,stroke:e.getAttribute("stroke"),"stroke-width":e.getAttribute("stroke-width"),"marker-mid":n,fill:"none",opacity:e.getAttribute("opacity")||1}});i(e).after(_).remove(),o.clearSelection(),_.id=v,o.addToSelection([_]),e=_}}if(e.getAttribute("class")===l.substr(1)){var p=a(d(e,"c_start"));N([p])}else N()}},IDsUpdated:function IDsUpdated(t){var e=[];return t.elems.forEach((function(r){"se:connector"in r.attr&&(r.attr["se:connector"]=r.attr["se:connector"].split(" ").map((function(e){return t.changes[e]})).join(" "),/. ./.test(r.attr["se:connector"])||e.push(r.attr.id))})),{remove:e}},toolButtonStateUpdate:function toolButtonStateUpdate(t){t.nostroke&&i("#mode_connect").hasClass("tool_button_current")&&n.clickSelect(),i("#mode_connect").toggleClass("disabled",t.nostroke)}});case 20:case"end":return j.stop()}}),_callee)})))()}})}}}));