UNPKG

rete-connection-plugin

Version:

Rete connection plugin ==== #### Rete.js plugin

8 lines (7 loc) 11.7 kB
/*! * rete-connection-plugin v0.4.0 * (c) 2019 * Released under the ISC license. */ !function(t){"use strict";var a,e=Object.prototype,s=e.hasOwnProperty,n="function"==typeof Symbol?Symbol:{},r=n.iterator||"@@iterator",o=n.asyncIterator||"@@asyncIterator",i=n.toStringTag||"@@toStringTag",c="object"==typeof module,u=t.regeneratorRuntime;if(u)c&&(module.exports=u);else{(u=t.regeneratorRuntime=c?module.exports:{}).wrap=w;var h="suspendedStart",f="suspendedYield",p="executing",d="completed",v={},l={};l[r]=function(){return this};var y=Object.getPrototypeOf,m=y&&y(y(S([])));m&&m!==e&&s.call(m,r)&&(l=m);var g=b.prototype=L.prototype=Object.create(l);E.prototype=g.constructor=b,b.constructor=E,b[i]=E.displayName="GeneratorFunction",u.isGeneratorFunction=function(t){var e="function"==typeof t&&t.constructor;return!!e&&(e===E||"GeneratorFunction"===(e.displayName||e.name))},u.mark=function(t){return Object.setPrototypeOf?Object.setPrototypeOf(t,b):(t.__proto__=b,i in t||(t[i]="GeneratorFunction")),t.prototype=Object.create(g),t},u.awrap=function(t){return{__await:t}},x(C.prototype),C.prototype[o]=function(){return this},u.AsyncIterator=C,u.async=function(t,e,n,o){var r=new C(w(t,e,n,o));return u.isGeneratorFunction(e)?r:r.next().then(function(t){return t.done?t.value:r.next()})},x(g),g[i]="Generator",g[r]=function(){return this},g.toString=function(){return"[object Generator]"},u.keys=function(n){var o=[];for(var t in n)o.push(t);return o.reverse(),function t(){for(;o.length;){var e=o.pop();if(e in n)return t.value=e,t.done=!1,t}return t.done=!0,t}},u.values=S,j.prototype={constructor:j,reset:function(t){if(this.prev=0,this.next=0,this.sent=this._sent=a,this.done=!1,this.delegate=null,this.method="next",this.arg=a,this.tryEntries.forEach(O),!t)for(var e in this)"t"===e.charAt(0)&&s.call(this,e)&&!isNaN(+e.slice(1))&&(this[e]=a)},stop:function(){this.done=!0;var t=this.tryEntries[0].completion;if("throw"===t.type)throw t.arg;return this.rval},dispatchException:function(n){if(this.done)throw n;var o=this;function t(t,e){return i.type="throw",i.arg=n,o.next=t,e&&(o.method="next",o.arg=a),!!e}for(var e=this.tryEntries.length-1;0<=e;--e){var r=this.tryEntries[e],i=r.completion;if("root"===r.tryLoc)return t("end");if(r.tryLoc<=this.prev){var c=s.call(r,"catchLoc"),u=s.call(r,"finallyLoc");if(c&&u){if(this.prev<r.catchLoc)return t(r.catchLoc,!0);if(this.prev<r.finallyLoc)return t(r.finallyLoc)}else if(c){if(this.prev<r.catchLoc)return t(r.catchLoc,!0)}else{if(!u)throw new Error("try statement without catch or finally");if(this.prev<r.finallyLoc)return t(r.finallyLoc)}}}},abrupt:function(t,e){for(var n=this.tryEntries.length-1;0<=n;--n){var o=this.tryEntries[n];if(o.tryLoc<=this.prev&&s.call(o,"finallyLoc")&&this.prev<o.finallyLoc){var r=o;break}}r&&("break"===t||"continue"===t)&&r.tryLoc<=e&&e<=r.finallyLoc&&(r=null);var i=r?r.completion:{};return i.type=t,i.arg=e,r?(this.method="next",this.next=r.finallyLoc,v):this.complete(i)},complete:function(t,e){if("throw"===t.type)throw t.arg;return"break"===t.type||"continue"===t.type?this.next=t.arg:"return"===t.type?(this.rval=this.arg=t.arg,this.method="return",this.next="end"):"normal"===t.type&&e&&(this.next=e),v},finish:function(t){for(var e=this.tryEntries.length-1;0<=e;--e){var n=this.tryEntries[e];if(n.finallyLoc===t)return this.complete(n.completion,n.afterLoc),O(n),v}},catch:function(t){for(var e=this.tryEntries.length-1;0<=e;--e){var n=this.tryEntries[e];if(n.tryLoc===t){var o=n.completion;if("throw"===o.type){var r=o.arg;O(n)}return r}}throw new Error("illegal catch attempt")},delegateYield:function(t,e,n){return this.delegate={iterator:S(t),resultName:e,nextLoc:n},"next"===this.method&&(this.arg=a),v}}}function w(t,e,n,o){var i,c,u,a,r=e&&e.prototype instanceof L?e:L,s=Object.create(r.prototype),l=new j(o||[]);return s._invoke=(i=t,c=n,u=l,a=h,function(t,e){if(a===p)throw new Error("Generator is already running");if(a===d){if("throw"===t)throw e;return N()}for(u.method=t,u.arg=e;;){var n=u.delegate;if(n){var o=P(n,u);if(o){if(o===v)continue;return o}}if("next"===u.method)u.sent=u._sent=u.arg;else if("throw"===u.method){if(a===h)throw a=d,u.arg;u.dispatchException(u.arg)}else"return"===u.method&&u.abrupt("return",u.arg);a=p;var r=k(i,c,u);if("normal"===r.type){if(a=u.done?d:f,r.arg===v)continue;return{value:r.arg,done:u.done}}"throw"===r.type&&(a=d,u.method="throw",u.arg=r.arg)}}),s}function k(t,e,n){try{return{type:"normal",arg:t.call(e,n)}}catch(t){return{type:"throw",arg:t}}}function L(){}function E(){}function b(){}function x(t){["next","throw","return"].forEach(function(e){t[e]=function(t){return this._invoke(e,t)}})}function C(a){var e;this._invoke=function(n,o){function t(){return new Promise(function(t,e){!function e(t,n,o,r){var i=k(a[t],a,n);if("throw"!==i.type){var c=i.arg,u=c.value;return u&&"object"==typeof u&&s.call(u,"__await")?Promise.resolve(u.__await).then(function(t){e("next",t,o,r)},function(t){e("throw",t,o,r)}):Promise.resolve(u).then(function(t){c.value=t,o(c)},r)}r(i.arg)}(n,o,t,e)})}return e=e?e.then(t,t):t()}}function P(t,e){var n=t.iterator[e.method];if(n===a){if(e.delegate=null,"throw"===e.method){if(t.iterator.return&&(e.method="return",e.arg=a,P(t,e),"throw"===e.method))return v;e.method="throw",e.arg=new TypeError("The iterator does not provide a 'throw' method")}return v}var o=k(n,t.iterator,e.arg);if("throw"===o.type)return e.method="throw",e.arg=o.arg,e.delegate=null,v;var r=o.arg;return r?r.done?(e[t.resultName]=r.value,e.next=t.nextLoc,"return"!==e.method&&(e.method="next",e.arg=a),e.delegate=null,v):r:(e.method="throw",e.arg=new TypeError("iterator result is not an object"),e.delegate=null,v)}function _(t){var e={tryLoc:t[0]};1 in t&&(e.catchLoc=t[1]),2 in t&&(e.finallyLoc=t[2],e.afterLoc=t[3]),this.tryEntries.push(e)}function O(t){var e=t.completion||{};e.type="normal",delete e.arg,t.completion=e}function j(t){this.tryEntries=[{tryLoc:"root"}],t.forEach(_,this),this.reset(!0)}function S(e){if(e){var t=e[r];if(t)return t.call(e);if("function"==typeof e.next)return e;if(!isNaN(e.length)){var n=-1,o=function t(){for(;++n<e.length;)if(s.call(e,n))return t.value=e[n],t.done=!1,t;return t.value=a,t.done=!0,t};return o.next=o}}return{next:N}}function N(){return{value:a,done:!0}}}(function(){return this}()||Function("return this")()),function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e(t.ConnectionPlugin={})}(this,function(t){"use strict";function n(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function o(t,e){for(var n=0;n<e.length;n++){var o=e[n];o.enumerable=o.enumerable||!1,o.configurable=!0,"value"in o&&(o.writable=!0),Object.defineProperty(t,o.key,o)}}function r(t,e,n){return e&&o(t.prototype,e),n&&o(t,n),t}function s(t,e){return function(t){if(Array.isArray(t))return t}(t)||function(t,e){var n=[],o=!0,r=!1,i=void 0;try{for(var c,u=t[Symbol.iterator]();!(o=(c=u.next()).done)&&(n.push(c.value),!e||n.length!==e);o=!0);}catch(t){r=!0,i=t}finally{try{o||null==u.return||u.return()}finally{if(r)throw i}}return n}(t,e)||function(){throw new TypeError("Invalid attempt to destructure non-iterable instance")}()}function a(t){return t.toLowerCase().replace(/ /g,"-")}function i(t,e){var n=s(t,4),o=n[0],r=n[1],i=n[2],c=n[3],u=o+Math.abs(i-o)*e,a=i-Math.abs(i-o)*e;return"M ".concat(o," ").concat(r," C ").concat(u," ").concat(r," ").concat(a," ").concat(c," ").concat(i," ").concat(c)}function u(t,e,n){var o={points:e,connection:n,d:""};return t.trigger("connectionpath",o),o.d||i(e,.4)}function l(t){var e=t.el,n=t.d,o=e.querySelector(".connection path");if(!o)throw new Error("Path of connection was broken");o.setAttribute("d",n)}function h(t){var e,n=t.el,o=t.d,r=t.connection,i=r?["input-"+a(r.input.name),"output-"+a(r.output.name),"socket-input-"+a(r.input.socket.name),"socket-output-"+a(r.output.socket.name)]:[],c=document.createElementNS("http://www.w3.org/2000/svg","svg"),u=document.createElementNS("http://www.w3.org/2000/svg","path");(e=c.classList).add.apply(e,["connection"].concat(i)),u.classList.add("main-path"),u.setAttribute("d",o),c.appendChild(u),n.appendChild(c),l({el:n,d:o})}!function(t){if(t&&"undefined"!=typeof window){var e=document.createElement("style");e.setAttribute("type","text/css"),e.innerHTML=t,document.head.appendChild(e)}}(".connection {\n overflow: visible !important; }\n .connection .main-path {\n fill: none;\n stroke-width: 5px;\n stroke: steelblue; }\n");var e=function(){function e(t){n(this,e),this.el=document.createElement("div"),this.editor=t,this._output=null}return r(e,[{key:"reset",value:function(){this.output=null}},{key:"pickOutput",value:function(t){t&&!this.output&&(this.output=t)}},{key:"pickInput",value:function(t){var e=this;if(null===this.output)return t.hasConnection()&&(this.output=t.connections[0].output,editor.removeConnection(t.connections[0])),!0;if(!t.multipleConnections&&t.hasConnection()&&editor.removeConnection(t.connections[0]),!this.output.multipleConnections&&this.output.hasConnection()&&editor.removeConnection(this.output.connections[0]),this.output.connectedTo(t)){var n=t.connections.find(function(t){return t.output===e.output});editor.removeConnection(n)}editor.connect(this.output,t),this.reset()}},{key:"pickConnection",value:function(t){var e=t.output;editor.removeConnection(t),this.output=e}},{key:"getPoints",value:function(){var t=this.editor.view.area.mouse,e=s(this.editor.view.nodes.get(this.output.node).getSocketPosition(this.output),2);return[e[0],e[1],t.x,t.y]}},{key:"updateConnection",value:function(){if(this.output){var t=u(this.editor,this.getPoints());l({el:this.el,d:t})}}},{key:"renderConnection",value:function(){if(this.output){var t=u(this.editor,this.getPoints());h({el:this.el,d:t,connection:null})}}},{key:"output",get:function(){return this._output},set:function(t){var e=this.editor.view.area;null!==(this._output=t)?(e.appendChild(this.el),this.renderConnection()):this.el.parentElement&&(e.removeChild(this.el),this.el.innerHTML="")}}]),e}(),f=function(){function e(t){n(this,e),this.picker=t,this.locked=!1}return r(e,[{key:"act",value:function(){var t=0<arguments.length&&void 0!==arguments[0]?arguments[0]:{},e=t.input,n=t.output;this.locked||(e?this.picker.pickInput(e):n?this.picker.pickOutput(n):this.picker.reset())}},{key:"once",value:function(t){this.act(t),this.prevent()}},{key:"prevent",value:function(){this.locked=!0}},{key:"reset",value:function(){this.locked=!1}}]),e}();var c={name:"connection",install:function(i){i.bind("connectionpath");var c=new e(i),r=new f(c);i.on("rendersocket",function(t){var e=t.el,n=t.input,o=t.output;e._reteConnectionPlugin={input:n,output:o},e.addEventListener("pointerdown",function(t){i.view.container.dispatchEvent(new PointerEvent("pointermove",t)),t.stopPropagation(),r.once(e._reteConnectionPlugin)}),e.addEventListener("click",function(t){t.stopPropagation(),r.reset()})}),window.addEventListener("pointerup",function(t){var e=document.elementFromPoint(t.clientX,t.clientY);r.act(e&&e._reteConnectionPlugin)}),window.addEventListener("pointermove",function(){return r.reset()}),i.on("mousemove",function(){return c.updateConnection()}),i.on("renderconnection",function(t){var e=t.el,n=t.connection,o=t.points,r=u(i,o,n);e.addEventListener("contextmenu",function(t){t.stopPropagation(),t.preventDefault(),c.pickConnection(n)}),h({el:e,d:r,connection:n})}),i.on("updateconnection",function(t){var e=t.el,n=t.connection,o=t.points;l({el:e,connection:n,d:u(i,o,n)})})}};t.default=c,t.defaultPath=i,Object.defineProperty(t,"__esModule",{value:!0})}); //# sourceMappingURL=connection-plugin.min.js.map