flow.module.js 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872
  1. /**
  2. * https://github.com/sunag/flow
  3. */
  4. function __flow__addCSS( css ) {
  5. try {
  6. const style = document.createElement( 'style' );
  7. style.setAttribute( 'type', 'text/css' );
  8. style.innerHTML = css;
  9. document.head.appendChild( style );
  10. } catch( e ) {}
  11. }
  12. __flow__addCSS( `f-canvas,f-canvas canvas { position: absolute; top: 0; left: 0; margin: 0; padding: 0; width: 100%; height: 100%; -webkit-touch-callout: none; }f-canvas { overflow: auto; cursor: grab;}f-canvas canvas.front { z-index: 10;}body.dragging f-canvas,body.connecting f-canvas { overflow: hidden !important;}body.dragging *:not(.drag) { pointer-events: none !important;}f-canvas.grabbing * { cursor: grabbing; user-select: none;}f-canvas canvas { position: fixed; overflow: hidden; pointer-events: none;}f-canvas::-webkit-scrollbar { width: 8px; height: 8px;}f-canvas::-webkit-scrollbar-thumb:hover{ background: #014fc5;}f-canvas::-webkit-scrollbar-track { background: #363636;}f-canvas::-webkit-scrollbar-thumb { background-color: #666666; border-radius: 10px; border: 0;}f-canvas f-content,f-canvas f-area { position: absolute; display: block;}f-node { position: absolute; margin: 0; padding: 0; user-select: none; width: 320px; z-index: 1; cursor: auto; filter: drop-shadow(0 0 10px #00000061); backdrop-filter: blur(4px);}f-node.selected { z-index: 2;}f-node.selected,f-canvas.dragging-rio f-node:hover,f-canvas.dragging-lio f-node:hover { filter: drop-shadow(0 0 10px #00000061) drop-shadow(0 0 8px #4444dd);}f-node.closed f-element:not(:first-child) { display: none;}f-node.center { top: 50%; left: 50%; transform: translate( -50%, -50% );}f-node.top-right { top: 0; right: 0;}f-node.top-center { top: 0; left: 50%; transform: translateX( -50% );}f-node.top-left { top: 0; left: 0;}f-node { transition: filter 0.2s ease;}@keyframes f-open { 0% { transform: scale(.5); filter: blur(30px); } 100% { transform: scale(1); filter: unset }}f-node { animation: .2s f-open 1 alternate ease-out;}f-menu,f-menu button,f-element,f-element input,f-element select,f-element button,f-element textarea { font-family: 'Open Sans', sans-serif; font-size: 13px; text-transform: capitalize; color: #eeeeee; outline: solid 0px #000; letter-spacing: .2px; margin: 0; padding: 0; border: 0; user-select: none; -webkit-tap-highlight-color: transparent; transition: background 0.2s ease;}f-element input { transition: background 0.1s ease;}f-element input,f-element select,f-element button,f-element textarea { background-color: #242427;}f-element { position: relative; width: calc( 100% - 14px ); background: rgba(45, 45, 48, 0.95); pointer-events: auto; border-bottom: 2px solid #232323; display: flex; padding-left: 7px; padding-right: 7px; padding-top: 2px; padding-bottom: 2px;}f-element { height: 24px;}f-element input { margin-top: 2px; margin-bottom: 2px; box-shadow: inset 0px 1px 1px rgb(0 0 0 / 20%), 0px 1px 0px rgb(255 255 255 / 5%); margin-left: 2px; margin-right: 2px; width: 100%; padding-left: 4px; padding-right: 4px;}f-element input.number { cursor: col-resize;}f-element input:focus[type='text'], f-element input:focus[type='range'], f-element input:focus[type='color'] { background: rgba( 0, 0, 0, 0.6 ); outline: solid 1px rgba( 0, 80, 200, 0.98 );}f-element input[type='color'] { appearance: none; padding: 0; margin-left: 2px; margin-right: 2px; height: calc( 100% - 4px ); margin-top: 2px; border: none; }f-element input[type='color']::-webkit-color-swatch-wrapper { padding: 2px;}f-element input[type='color']::-webkit-color-swatch { border: none; cursor: alias;}f-element input[type='range'] { appearance: none; width: 100%; overflow: hidden; padding: 0; cursor: ew-resize;}f-element input[type='range']::-webkit-slider-runnable-track { appearance: none; height: 10px; color: #13bba4; margin: 0;}f-element input[type='range']::-webkit-slider-thumb { appearance: none; width: 0; background: #434343; box-shadow: -500px 0 0 500px rgba( 0, 120, 255, 0.98 ); border-radius: 50%; border: 0 !important;}f-element input[type='range']::-webkit-slider-runnable-track { margin-left: -4px; margin-right: -5px;}f-element input[type='checkbox'] { appearance: none; cursor: pointer;}f-element input[type='checkbox'].toggle { height: 20px; width: 45px; border-radius: 16px; display: inline-block; position: relative; margin: 0; margin-top: 2px; background: linear-gradient( 0deg, #292929 0%, #0a0a0ac2 100% ); transition: all 0.2s ease;}f-element input[type='checkbox'].toggle:after { content: ""; position: absolute; top: 2px; left: 2px; width: 16px; height: 16px; border-radius: 50%; background: white; box-shadow: 0 1px 2px rgba(44, 44, 44, 0.2); transition: all 0.2s cubic-bezier(0.5, 0.1, 0.75, 1.35);}f-element input[type='checkbox'].toggle:checked { background: linear-gradient( 0deg, #0177fb 0%, #0177fb 100% );}f-element input[type='checkbox'].toggle:checked:after { transform: translatex(25px);}f-element.auto-height { display: table;}f-element textarea { width: calc( 100% - 18px ); padding-top: 1px; padding-bottom: 3px; padding-left: 3px; padding-right: 8px; margin-top: 2px; margin-left: 2px; height: calc( 100% - 8px ); max-height: 300px; border-radius: 2px; resize: none; box-shadow: inset 0px 1px 1px rgb(0 0 0 / 20%), 0px 1px 0px rgb(255 255 255 / 5%);}f-element.auto-height textarea { resize: auto;}f-element select { width: 100%; margin-top: 2px; margin-bottom: 2px; margin-left: 2px; margin-right: 2px; padding-left: 5px; cursor: pointer; box-shadow: inset 0px 1px 1px rgb(0 0 0 / 20%), 0px 1px 0px rgb(255 255 255 / 5%);}f-element f-toolbar { position: absolute; display: flex; top: 0; width: 100%; height: 100%; align-content: space-around;}f-element.output-right f-toolbar { right: 7px; float: right; justify-content: end;}f-element f-toolbar { margin-top: auto; margin-bottom: auto; margin-left: 3px; margin-right: 3px; font-size: 18px; line-height: 18px;}f-element f-toolbar button { opacity: .7; cursor: pointer; font-size: 14px; width: unset; height: unset; border-radius: unset; border: unset; outline: 0; background-color: unset; box-shadow: unset;}f-element f-toolbar button:hover,f-element f-toolbar button:active { opacity: 1; border: 0; background-color: unset;}f-element input.range-value { width: 60px; text-align: center;}f-menu.context button,f-element button { width: 100%; height: calc( 100% - 4px ); margin-left: 2px; margin-right: 2px; margin-top: 2px; border-radius: 3px; cursor: pointer;}f-element button { box-shadow: inset 1px 1px 1px 0 rgb(255 255 255 / 17%), inset -2px -2px 2px 0 rgb(0 0 0 / 26%);}f-element button:hover { color: #fff; background-color: #2a2a2a;}f-element button:active { border: 1px solid rgba( 0, 120, 255, 0.98 );}f-element f-inputs,f-element f-subinputs { display: flex; width: 100%;}f-element f-inputs { left: 100px; top: 50%; transform: translateY(-50%); position: absolute; width: calc( 100% - 106px ); height: calc( 100% - 4px ); z-index: 1;}f-element f-label,f-element span { margin: auto; text-shadow: 1px 1px 0px #0007;}f-element f-label { padding-left: 4px; white-space: nowrap; position: absolute; top: 50%; transform: translateY(-50%); width: calc( 100% - 20px );}f-element.right f-label { text-align: right;}f-element f-label i { float: left; font-size: 18px; margin-right: 6px;}f-element f-label.center { width: 100%; text-align: center; display: block;}f-element.title { height: 29px; background-color: #3a3a3ab0; background-color: #3b3b43ed; cursor: all-scroll; border-top-left-radius: 6px; border-top-right-radius: 6px;}f-element.blue { background-color: #014fc5;}f-element.red { background-color: #bd0b0b;}f-element.green { background-color: #148d05;}f-element.yellow { background-color: #d6b100;}f-element.title.left { text-align: left; display: inline-grid; justify-content: start;}f-element.title span { text-align: center; font-size: 15px; padding-top: 2px;}f-element.title i { font-size: 18px; position: absolute; right: 10px; top: 50%; transform: translateY(-50%); opacity: .5;}f-element.output-right.title i { left: 10px; right: unset;}f-element.title.left span { text-align: left;}f-element f-io { border: 2px solid #dadada; width: 7px; height: 7px; position: absolute; background: #242427; border-radius: 8px; float: left; left: -7px; top: calc( 50% - 5px ); cursor: alias; box-shadow: 0 0 3px 2px #0000005e; z-index: 1;}f-element f-io.connect,f-canvas.dragging-rio f-element:hover f-io.lio,f-canvas.dragging-lio f-element:hover f-io.rio { border: 2px solid #0177fb; zoom: 1.4;}f-node.io-connect f-io:not(.connect) { border: 2px solid #dadada !important; zoom: 1 !important;}f-element f-io.rio { float: right; right: -7px; left: unset;}f-element f-disconnect { position: absolute; left: -35px; top: 50%; font-size: 22px; transform: translateY( -50% ); filter: drop-shadow(0 0 5px #000); text-shadow: 0px 0px 5px black; cursor: pointer;}f-element.output-right f-disconnect { right: -35px; left: unset;}f-element f-disconnect:hover { color: #ff3300;}f-element textarea::-webkit-scrollbar { width: 6px;}f-element textarea::-webkit-scrollbar-track { background: #111; } f-element textarea::-webkit-scrollbar-thumb { background: #0177fb; }f-element textarea::-webkit-scrollbar-thumb:hover { background: #1187ff; }f-element.small { height: 18px;}f-element.large { height: 36px;}body.connecting f-node:not(.io-connect) f-element:hover,f-element.select { background-color: rgba(61, 70, 82, 0.98);}f-tooltip { pointer-events: none;}f-menu.context { position: absolute; width: 170px; padding: 2px; margin: 0; background: #17171794; z-index: 110; font-size: 12px; border-radius: 6px; backdrop-filter: blur(6px); border: 1px solid #7e7e7e45; box-shadow: 3px 3px 6px rgba(0,0,0,.2);}f-menu.context.hidden { visibility: hidden;}f-menu.context f-item { display: block; position: relative; margin: 0; padding: 0; white-space: nowrap;}f-menu.context f-item.submenu::after { content: ""; position: absolute; right: 6px; top: 50%; -webkit-transform: translateY(-50%); transform: translateY(-50%); border: 5px solid transparent; border-left-color: #808080;}f-menu.context f-item:hover > f-menu { visibility: unset;}f-menu.context f-menu { top: 0px; left: calc( 100% - 4px );}f-menu.context f-item button { overflow: visible; display: block; width: calc( 100% - 6px ); text-align: left; cursor: pointer; white-space: nowrap; padding: 6px 8px; border-radius: 3px; background: #2d2d32; border: 0; color: #ddd; margin: 3px; text-shadow: 1px 1px 0px #0007;}f-menu.context f-item button i { float: left; font-size: 16px;}f-menu.context f-item button span { margin-left: 6px;}f-menu.context f-item:hover > button { color: #fff; background: #313136;}f-menu.context f-item button:active { outline: solid 1px rgba( 0, 80, 200, 0.98 );}f-menu.circle { position: absolute; left: 30px; top: 30px; z-index: 100;}f-menu.circle f-item { display: flex; justify-content: end; align-content: space-around; margin-bottom: 12px;}f-menu.circle f-item button { width: 50px; height: 50px; font-size: 26px; background: #17171794; border-radius: 50%; backdrop-filter: blur(6px); border: 1px solid #7e7e7e45; line-height: 100%; cursor: pointer; box-shadow: 3px 3px 6px rgba(0,0,0,.2);}f-menu.circle f-item f-tooltip { position: absolute; left: 0; top: 0; margin-left: 50px; background: rgba(0,0,0,.8); backdrop-filter: blur(4px); font-size: 14px; padding: 7px; border-radius: 10px; top: 50%; transform: translateY(-50%); visibility: hidden; pointer-events: none; opacity: 0; transition: all 0.3s ease;}f-menu.circle f-item button:hover { background-color: #2a2a2a;}f-menu.circle f-item button:hover > f-tooltip { visibility: visible; transform: translate(10px, -50%); opacity: 1;}.f-rounded f-node f-element,.f-rounded f-node f-element.title.left { border-radius: 10px 5px 10px 5px;}.f-rounded f-node f-element input, .f-rounded f-node f-element select,.f-rounded f-node f-element button,.f-rounded f-node f-element textarea,.f-rounded f-node f-element input[type='checkbox'].toggle,.f-rounded f-node f-element input[type='checkbox'].toggle:after { border-radius: 20px 10px;}.f-rounded f-node f-element input { padding-left: 7px; padding-right: 7px;}.f-rounded f-menu.context,.f-rounded f-menu.context f-item button { border-radius: 20px 10px;}@media (hover: hover) and (pointer: fine) { f-element f-io:hover { border: 2px solid #0177fb; zoom: 1.4; }}f-canvas { will-change: top, left;}f-node { will-change: transform !important;}` );
  13. var Flow = /*#__PURE__*/Object.freeze({
  14. __proto__: null,
  15. get Element () { return Element; },
  16. get Input () { return Input; },
  17. get Node () { return Node; },
  18. get Canvas () { return Canvas; },
  19. get Serializer () { return Serializer; },
  20. get Loader () { return Loader; },
  21. get Utils () { return Utils; },
  22. get PanelNode () { return PanelNode; },
  23. get Menu () { return Menu; },
  24. get ContextMenu () { return ContextMenu; },
  25. get CircleMenu () { return CircleMenu; },
  26. get DraggableElement () { return DraggableElement; },
  27. get LabelElement () { return LabelElement; },
  28. get TitleElement () { return TitleElement; },
  29. get ButtonInput () { return ButtonInput; },
  30. get ColorInput () { return ColorInput; },
  31. get NumberInput () { return NumberInput; },
  32. get SelectInput () { return SelectInput; },
  33. get SliderInput () { return SliderInput; },
  34. get StringInput () { return StringInput; },
  35. get TextInput () { return TextInput; },
  36. get ToggleInput () { return ToggleInput; }
  37. });
  38. let _id = 0;
  39. class Serializer extends EventTarget {
  40. constructor() {
  41. super();
  42. this._id = _id ++;
  43. }
  44. get id() {
  45. return this._id;
  46. }
  47. serialize( /*data*/ ) {
  48. console.warn( 'Serializer: Abstract function.' );
  49. }
  50. deserialize( /*data*/ ) {
  51. console.warn( 'Serializer: Abstract function.' );
  52. }
  53. toJSON( data = null ) {
  54. let object = null;
  55. const id = this.id;
  56. if ( data !== null ) {
  57. const objects = data.objects;
  58. object = objects[ id ];
  59. if ( object === undefined ) {
  60. object = { objects };
  61. this.serialize( object );
  62. delete object.objects;
  63. objects[ id ] = object;
  64. }
  65. } else {
  66. object = { objects: {} };
  67. this.serialize( object );
  68. }
  69. object.id = id;
  70. object.type = this.constructor.name;
  71. return object;
  72. }
  73. }
  74. const draggableDOM = ( dom, callback = null, className = 'dragging' ) => {
  75. let dragData = null;
  76. const getZoom = () => {
  77. let zoomDOM = dom;
  78. while ( zoomDOM && zoomDOM !== document ) {
  79. const zoom = zoomDOM.style.zoom;
  80. if ( zoom ) {
  81. return Number( zoom );
  82. }
  83. zoomDOM = zoomDOM.parentNode;
  84. }
  85. return 1;
  86. };
  87. const onMouseDown = ( e ) => {
  88. const event = e.touches ? e.touches[ 0 ] : e;
  89. e.stopImmediatePropagation();
  90. dragData = {
  91. client: { x: event.clientX, y: event.clientY },
  92. delta: { x: 0, y: 0 },
  93. start: { x: dom.offsetLeft, y: dom.offsetTop },
  94. dragging: false,
  95. isTouch: !! e.touches
  96. };
  97. window.addEventListener( 'mousemove', onGlobalMouseMove );
  98. window.addEventListener( 'mouseup', onGlobalMouseUp );
  99. window.addEventListener( 'touchmove', onGlobalMouseMove );
  100. window.addEventListener( 'touchend', onGlobalMouseUp );
  101. };
  102. const onGlobalMouseMove = ( e ) => {
  103. const { start, delta, client } = dragData;
  104. const event = e.touches ? e.touches[ 0 ] : e;
  105. const zoom = getZoom();
  106. delta.x = ( event.clientX - client.x ) / zoom;
  107. delta.y = ( event.clientY - client.y ) / zoom;
  108. dragData.x = start.x + delta.x;
  109. dragData.y = start.y + delta.y;
  110. if ( dragData.dragging === true ) {
  111. if ( callback !== null ) {
  112. callback( dragData );
  113. } else {
  114. dom.style.cssText += `; left: ${ dragData.x }px; top: ${ dragData.y }px;`;
  115. }
  116. e.stopImmediatePropagation();
  117. } else {
  118. if ( Math.abs( delta.x ) > 1 || Math.abs( delta.y ) > 1 ) {
  119. dragData.dragging = true;
  120. dom.classList.add( 'drag' );
  121. if ( className ) document.body.classList.add( className );
  122. e.stopImmediatePropagation();
  123. }
  124. }
  125. };
  126. const onGlobalMouseUp = ( e ) => {
  127. e.stopImmediatePropagation();
  128. dom.classList.remove( 'drag' );
  129. if ( className ) document.body.classList.remove( className );
  130. window.removeEventListener( 'mousemove', onGlobalMouseMove );
  131. window.removeEventListener( 'mouseup', onGlobalMouseUp );
  132. window.removeEventListener( 'touchmove', onGlobalMouseMove );
  133. window.removeEventListener( 'touchend', onGlobalMouseUp );
  134. if ( callback === null ) {
  135. dom.removeEventListener( 'mousedown', onMouseDown );
  136. dom.removeEventListener( 'touchstart', onMouseDown );
  137. }
  138. dragData.dragging = false;
  139. if ( callback !== null ) {
  140. callback( dragData );
  141. }
  142. };
  143. if ( dom instanceof Event ) {
  144. const e = dom;
  145. dom = e.target;
  146. onMouseDown( e );
  147. } else {
  148. dom.addEventListener( 'mousedown', onMouseDown );
  149. dom.addEventListener( 'touchstart', onMouseDown );
  150. }
  151. };
  152. const dispatchEventList = ( list, ...params ) => {
  153. for ( const callback of list ) {
  154. callback( ...params );
  155. }
  156. };
  157. const toPX = ( val ) => {
  158. if ( isNaN( val ) === false ) {
  159. val = `${ val }px`;
  160. }
  161. return val;
  162. };
  163. const toHex = ( val ) => {
  164. if ( isNaN( val ) === false ) {
  165. val = `#${ val.toString( 16 ).padStart( 6, '0' ) }`;
  166. }
  167. return val;
  168. };
  169. var Utils = /*#__PURE__*/Object.freeze({
  170. __proto__: null,
  171. draggableDOM: draggableDOM,
  172. dispatchEventList: dispatchEventList,
  173. toPX: toPX,
  174. toHex: toHex
  175. });
  176. class Link {
  177. constructor( inputElement = null, outputElement = null ) {
  178. this.inputElement = inputElement;
  179. this.outputElement = outputElement;
  180. }
  181. get lioElement() {
  182. if ( Link.InputDirection === 'left' ) {
  183. return this.outputElement;
  184. } else {
  185. return this.inputElement;
  186. }
  187. }
  188. get rioElement() {
  189. if ( Link.InputDirection === 'left' ) {
  190. return this.inputElement;
  191. } else {
  192. return this.outputElement;
  193. }
  194. }
  195. }
  196. //Link.InputDirection = 'right';
  197. Link.InputDirection = 'left';
  198. let selected = null;
  199. class Element extends Serializer {
  200. constructor( draggable = false ) {
  201. super();
  202. const dom = document.createElement( 'f-element' );
  203. dom.element = this;
  204. const onSelect = ( e ) => {
  205. let element = this;
  206. if ( e.changedTouches && e.changedTouches.length > 0 ) {
  207. const touch = e.changedTouches[ 0 ];
  208. let overDOM = document.elementFromPoint( touch.clientX, touch.clientY );
  209. while ( overDOM && ( ! overDOM.element || ! overDOM.element.isElement ) ) {
  210. overDOM = overDOM.parentNode;
  211. }
  212. element = overDOM ? overDOM.element : null;
  213. }
  214. selected = element;
  215. };
  216. if ( draggable === false ) {
  217. dom.ontouchstart = dom.onmousedown = ( e ) => {
  218. e.stopPropagation();
  219. };
  220. }
  221. dom.addEventListener( 'mouseup', onSelect, true );
  222. dom.addEventListener( 'touchend', onSelect );
  223. this.inputs = [];
  224. this.links = [];
  225. this.dom = dom;
  226. this.lioLength = 0;
  227. this.rioLength = 0;
  228. this.events = {
  229. 'connect': [],
  230. 'connectChildren': []
  231. };
  232. this.node = null;
  233. this.style = '';
  234. this.extra = null;
  235. this.inputsDOM = dom;
  236. this.disconnectDOM = null;
  237. this.lioDOM = this._createIO( 'lio' );
  238. this.rioDOM = this._createIO( 'rio' );
  239. this.dom.classList.add( `output-${ Link.InputDirection }` );
  240. this.dom.appendChild( this.lioDOM );
  241. this.dom.appendChild( this.rioDOM );
  242. this.addEventListener( 'connect', ( ) => {
  243. dispatchEventList( this.events.connect, this );
  244. } );
  245. this.addEventListener( 'connectChildren', ( ) => {
  246. dispatchEventList( this.events.connectChildren, this );
  247. } );
  248. }
  249. onConnect( callback, childrens = false ) {
  250. this.events.connect.push( callback );
  251. if ( childrens ) {
  252. this.events.connectChildren.push( callback );
  253. }
  254. return this;
  255. }
  256. setExtra( value ) {
  257. this.extra = value;
  258. return this;
  259. }
  260. getExtra() {
  261. return this.extra;
  262. }
  263. setStyle( style ) {
  264. const dom = this.dom;
  265. if ( this.style ) dom.classList.remove( this.style );
  266. if ( style ) dom.classList.add( style );
  267. this.style = style;
  268. return this;
  269. }
  270. setInput( length ) {
  271. if ( Link.InputDirection === 'left' ) {
  272. return this.setLIO( length );
  273. } else {
  274. return this.setRIO( length );
  275. }
  276. }
  277. setOutput( length ) {
  278. if ( Link.InputDirection === 'left' ) {
  279. return this.setRIO( length );
  280. } else {
  281. return this.setLIO( length );
  282. }
  283. }
  284. get inputLength() {
  285. if ( Link.InputDirection === 'left' ) {
  286. return this.lioLength;
  287. } else {
  288. return this.rioLength;
  289. }
  290. }
  291. get outputLength() {
  292. if ( Link.InputDirection === 'left' ) {
  293. return this.rioLength;
  294. } else {
  295. return this.lioLength;
  296. }
  297. }
  298. setLIO( length ) {
  299. this.lioLength = length;
  300. this.lioDOM.style.visibility = length > 0 ? '' : 'hidden';
  301. return this;
  302. }
  303. setRIO( length ) {
  304. this.rioLength = length;
  305. this.rioDOM.style.visibility = length > 0 ? '' : 'hidden';
  306. return this;
  307. }
  308. add( input ) {
  309. this.inputs.push( input );
  310. input.element = this;
  311. this.inputsDOM.appendChild( input.dom );
  312. return this;
  313. }
  314. setHeight( val ) {
  315. this.dom.style.height = toPX( val );
  316. return this;
  317. }
  318. getHeight() {
  319. return this.dom.style.height;
  320. }
  321. connect( element = null ) {
  322. if ( this.disconnectDOM !== null ) {
  323. // remove the current input
  324. this.disconnectDOM.dispatchEvent( new Event( 'disconnect' ) );
  325. }
  326. if ( element !== null ) {
  327. const link = new Link( this, element );
  328. this.links.push( link );
  329. if ( this.disconnectDOM === null ) {
  330. this.disconnectDOM = document.createElement( 'f-disconnect' );
  331. this.disconnectDOM.innerText = '✖';
  332. this.dom.appendChild( this.disconnectDOM );
  333. const onDisconnect = () => {
  334. this.links = [];
  335. this.dom.removeChild( this.disconnectDOM );
  336. this.disconnectDOM.removeEventListener( 'mousedown', onClick, true );
  337. this.disconnectDOM.removeEventListener( 'touchstart', onClick, true );
  338. this.disconnectDOM.removeEventListener( 'disconnect', onDisconnect, true );
  339. element.removeEventListener( 'connect', onConnect );
  340. element.removeEventListener( 'connectChildren', onConnect );
  341. element.removeEventListener( 'nodeConnect', onConnect );
  342. element.removeEventListener( 'nodeConnectChildren', onConnect );
  343. element.removeEventListener( 'dispose', onDispose );
  344. this.disconnectDOM = null;
  345. };
  346. const onConnect = ( e ) => {
  347. this.dispatchEvent( new Event( 'connectChildren' ) );
  348. };
  349. const onDispose = () => {
  350. this.connect();
  351. };
  352. const onClick = ( e ) => {
  353. e.stopPropagation();
  354. this.connect();
  355. };
  356. this.disconnectDOM.addEventListener( 'mousedown', onClick, true );
  357. this.disconnectDOM.addEventListener( 'touchstart', onClick, true );
  358. this.disconnectDOM.addEventListener( 'disconnect', onDisconnect, true );
  359. element.addEventListener( 'connect', onConnect );
  360. element.addEventListener( 'connectChildren', onConnect );
  361. element.addEventListener( 'nodeConnect', onConnect );
  362. element.addEventListener( 'nodeConnectChildren', onConnect );
  363. element.addEventListener( 'dispose', onDispose );
  364. }
  365. }
  366. this.dispatchEvent( new Event( 'connect' ) );
  367. return this;
  368. }
  369. dispose() {
  370. this.dispatchEvent( new Event( 'dispose' ) );
  371. }
  372. serialize( data ) {
  373. const height = this.getHeight();
  374. const inputs = [];
  375. const links = [];
  376. for ( const input of this.inputs ) {
  377. inputs.push( input.toJSON( data ).id );
  378. }
  379. for ( const link of this.links ) {
  380. if ( link.inputElement !== null && link.outputElement !== null ) {
  381. links.push( link.outputElement.toJSON( data ).id );
  382. }
  383. }
  384. if ( this.inputLength > 0 ) data.inputLength = this.inputLength;
  385. if ( this.outputLength > 0 ) data.outputLength = this.outputLength;
  386. if ( inputs.length > 0 ) data.inputs = inputs;
  387. if ( links.length > 0 ) data.links = links;
  388. if ( this.style !== '' ) {
  389. data.style = this.style;
  390. }
  391. if ( height !== '' ) {
  392. data.height = height;
  393. }
  394. }
  395. deserialize( data ) {
  396. if ( data.inputLength !== undefined ) this.setInput( data.inputLength );
  397. if ( data.outputLength !== undefined ) this.setOutput( data.outputLength );
  398. if ( data.inputs !== undefined ) {
  399. const inputs = this.inputs;
  400. if ( inputs.length > 0 ) {
  401. let index = 0;
  402. for ( const id of data.inputs ) {
  403. data.objects[ id ] = inputs[ index ++ ];
  404. }
  405. } else {
  406. for ( const id of data.inputs ) {
  407. this.add( data.objects[ id ] );
  408. }
  409. }
  410. }
  411. if ( data.links !== undefined ) {
  412. for ( const id of data.links ) {
  413. this.connect( data.objects[ id ] );
  414. }
  415. }
  416. if ( data.style !== undefined ) {
  417. this.setStyle( data.style );
  418. }
  419. if ( data.height !== undefined ) {
  420. this.setHeight( data.height );
  421. }
  422. }
  423. get linkedExtra() {
  424. const linkedElement = this.linkedElement;
  425. return linkedElement ? linkedElement.getExtra() : null;
  426. }
  427. get linkedElement() {
  428. const link = this.link;
  429. return link ? link.outputElement : null;
  430. }
  431. get link() {
  432. return this.links[ 0 ];
  433. }
  434. _createIO( type ) {
  435. const { dom } = this;
  436. const ioDOM = document.createElement( 'f-io' );
  437. ioDOM.style.visibility = 'hidden';
  438. ioDOM.className = type;
  439. const onConnectEvent = ( e ) => {
  440. e.preventDefault();
  441. e.stopPropagation();
  442. selected = null;
  443. const nodeDOM = this.node.dom;
  444. nodeDOM.classList.add( 'io-connect' );
  445. ioDOM.classList.add( 'connect' );
  446. dom.classList.add( 'select' );
  447. const defaultOutput = Link.InputDirection === 'left' ? 'lio' : 'rio';
  448. const link = type === defaultOutput ? new Link( this ) : new Link( null, this );
  449. this.links.push( link );
  450. draggableDOM( e, ( data ) => {
  451. if ( data.dragging === false ) {
  452. nodeDOM.classList.remove( 'io-connect' );
  453. ioDOM.classList.remove( 'connect' );
  454. dom.classList.remove( 'select' );
  455. this.links.splice( this.links.indexOf( link ), 1 );
  456. if ( selected !== null ) {
  457. if ( type === defaultOutput ) {
  458. link.outputElement = selected;
  459. } else {
  460. link.inputElement = selected;
  461. }
  462. // check if is an is circular link
  463. if ( link.outputElement.node.isCircular( link.inputElement.node ) ) {
  464. return;
  465. }
  466. //
  467. if ( link.inputElement.inputLength > 0 && link.outputElement.outputLength > 0 ) {
  468. link.inputElement.connect( link.outputElement );
  469. }
  470. }
  471. }
  472. }, 'connecting' );
  473. };
  474. ioDOM.addEventListener( 'mousedown', onConnectEvent, true );
  475. ioDOM.addEventListener( 'touchstart', onConnectEvent, true );
  476. return ioDOM;
  477. }
  478. }
  479. Element.prototype.isElement = true;
  480. class Input extends Serializer {
  481. constructor( dom ) {
  482. super();
  483. this.dom = dom;
  484. this.element = null;
  485. this.events = {
  486. 'change': [],
  487. 'click': []
  488. };
  489. this.addEventListener( 'change', ( ) => {
  490. dispatchEventList( this.events.change, this );
  491. } );
  492. this.addEventListener( 'click', ( ) => {
  493. dispatchEventList( this.events.click, this );
  494. } );
  495. }
  496. setToolTip( text ) {
  497. const div = document.createElement( 'f-tooltip' );
  498. div.innerText = text;
  499. this.dom.appendChild( div );
  500. return this;
  501. }
  502. onChange( callback ) {
  503. this.events.change.push( callback );
  504. return this;
  505. }
  506. onClick( callback ) {
  507. this.events.click.push( callback );
  508. return this;
  509. }
  510. setValue( value ) {
  511. this.dom.value = value;
  512. this.dispatchEvent( new Event( 'change' ) );
  513. return this;
  514. }
  515. getValue() {
  516. return this.dom.value;
  517. }
  518. serialize( data ) {
  519. data.value = this.getValue();
  520. }
  521. deserialize( data ) {
  522. this.setValue( data.value );
  523. }
  524. }
  525. Input.prototype.isInput = true;
  526. class Node extends Serializer {
  527. constructor() {
  528. super();
  529. const dom = document.createElement( 'f-node' );
  530. const onDown = () => {
  531. const canvas = this.canvas;
  532. if ( canvas !== null ) {
  533. canvas.select( this );
  534. }
  535. };
  536. dom.addEventListener( 'mousedown', onDown, true );
  537. dom.addEventListener( 'touchstart', onDown, true );
  538. this._onConnect = ( e ) => {
  539. const { target } = e;
  540. for ( const element of this.elements ) {
  541. if ( element !== target ) {
  542. element.dispatchEvent( new Event( 'nodeConnect' ) );
  543. }
  544. }
  545. };
  546. this._onConnectChildren = ( e ) => {
  547. const { target } = e;
  548. for ( const element of this.elements ) {
  549. if ( element !== target ) {
  550. element.dispatchEvent( new Event( 'nodeConnectChildren' ) );
  551. }
  552. }
  553. };
  554. this.dom = dom;
  555. this.style = '';
  556. this.canvas = null;
  557. this.elements = [];
  558. this.setPosition( 0, 0 );
  559. }
  560. setStyle( style ) {
  561. const dom = this.dom;
  562. if ( this.style ) dom.classList.remove( this.style );
  563. if ( style ) dom.classList.add( style );
  564. this.style = style;
  565. return this;
  566. }
  567. setPosition( x, y ) {
  568. const dom = this.dom;
  569. dom.style.left = toPX( x );
  570. dom.style.top = toPX( y );
  571. return this;
  572. }
  573. getPosition() {
  574. const dom = this.dom;
  575. return {
  576. x: parseInt( dom.style.left ),
  577. y: parseInt( dom.style.top )
  578. };
  579. }
  580. setWidth( val ) {
  581. this.dom.style.width = toPX( val );
  582. return this;
  583. }
  584. getWidth() {
  585. return this.dom.style.width;
  586. }
  587. add( element ) {
  588. this.elements.push( element );
  589. element.node = this;
  590. element.addEventListener( 'connect', this._onConnect );
  591. element.addEventListener( 'connectChildren', this._onConnectChildren );
  592. this.dom.appendChild( element.dom );
  593. return this;
  594. }
  595. remove( element ) {
  596. this.elements.splice( this.elements.indexOf( element ), 1 );
  597. element.node = null;
  598. element.removeEventListener( 'connect', this._onConnect );
  599. element.removeEventListener( 'connectChildren', this._onConnectChildren );
  600. this.dom.removeChild( element.dom );
  601. return this;
  602. }
  603. dispose() {
  604. const canvas = this.canvas;
  605. if ( canvas !== null ) canvas.remove( this );
  606. for ( const element of this.elements ) {
  607. element.dispose();
  608. }
  609. this.dispatchEvent( new Event( 'dispose' ) );
  610. }
  611. isCircular( node ) {
  612. if ( node === this ) return true;
  613. const links = this.getLinks();
  614. for ( const link of links ) {
  615. if ( link.outputElement.node.isCircular( node ) ) {
  616. return true;
  617. }
  618. }
  619. return false;
  620. }
  621. getLinks() {
  622. const links = [];
  623. for ( const element of this.elements ) {
  624. links.push( ...element.links );
  625. }
  626. return links;
  627. }
  628. serialize( data ) {
  629. const { x, y, style } = this.getPosition();
  630. const elements = [];
  631. for ( const element of this.elements ) {
  632. elements.push( element.toJSON( data ).id );
  633. }
  634. data.x = x;
  635. data.y = y;
  636. data.width = this.getWidth();
  637. data.elements = elements;
  638. if ( style !== '' ) {
  639. data.style = style;
  640. }
  641. }
  642. deserialize( data ) {
  643. this.setPosition( data.x, data.y );
  644. this.setWidth( data.width );
  645. if ( data.style !== undefined ) {
  646. this.setStyle( data.style );
  647. }
  648. const elements = this.elements;
  649. if ( elements.length > 0 ) {
  650. let index = 0;
  651. for ( const id of data.elements ) {
  652. data.objects[ id ] = elements[ index ++ ];
  653. }
  654. } else {
  655. for ( const id of data.elements ) {
  656. this.add( data.objects[ id ] );
  657. }
  658. }
  659. }
  660. }
  661. Node.prototype.isNode = true;
  662. const drawLine = ( p1x, p1y, p2x, p2y, invert, size, color, ctx ) => {
  663. const offset = 100 * ( invert ? - 1 : 1 );
  664. ctx.beginPath();
  665. ctx.moveTo( p1x, p1y );
  666. ctx.bezierCurveTo(
  667. p1x + offset, p1y,
  668. p2x - offset, p2y,
  669. p2x, p2y
  670. );
  671. ctx.lineWidth = size;
  672. ctx.strokeStyle = color;
  673. ctx.stroke();
  674. };
  675. const colors = [
  676. '#ff4444',
  677. '#44ff44',
  678. '#4444ff'
  679. ];
  680. class Canvas extends Serializer {
  681. constructor() {
  682. super();
  683. const dom = document.createElement( 'f-canvas' );
  684. const contentDOM = document.createElement( 'f-content' );
  685. const areaDOM = document.createElement( 'f-area' );
  686. const canvas = document.createElement( 'canvas' );
  687. const frontCanvas = document.createElement( 'canvas' );
  688. const context = canvas.getContext( '2d' );
  689. const frontContext = frontCanvas.getContext( '2d' );
  690. this.dom = dom;
  691. this.contentDOM = contentDOM;
  692. this.areaDOM = areaDOM;
  693. this.canvas = canvas;
  694. this.frontCanvas = frontCanvas;
  695. this.context = context;
  696. this.frontContext = frontContext;
  697. this.width = 10000;
  698. this.height = 10000;
  699. this.clientX = 0;
  700. this.clientY = 0;
  701. this.relativeClientX = 0;
  702. this.relativeClientY = 0;
  703. this.zoom = 1;
  704. this.nodes = [];
  705. this.selected = null;
  706. this.updating = false;
  707. frontCanvas.className = 'front';
  708. contentDOM.style.left = toPX( this.centerX );
  709. contentDOM.style.top = toPX( this.centerY );
  710. areaDOM.style.width = `calc( 100% + ${ this.width }px )`;
  711. areaDOM.style.height = `calc( 100% + ${ this.height }px )`;
  712. dom.appendChild( canvas );
  713. dom.appendChild( frontCanvas );
  714. dom.appendChild( contentDOM );
  715. dom.appendChild( areaDOM );
  716. /*
  717. let zoomTouchData = null;
  718. const onZoomStart = () => {
  719. zoomTouchData = null;
  720. };
  721. */
  722. const onZoom = ( e ) => {
  723. if ( e.touches ) {
  724. if ( e.touches.length === 2 ) {
  725. e.preventDefault();
  726. e.stopImmediatePropagation();
  727. /*
  728. const clientX = ( e.touches[ 0 ].clientX + e.touches[ 1 ].clientX ) / 2;
  729. const clientY = ( e.touches[ 0 ].clientY + e.touches[ 1 ].clientY ) / 2;
  730. const distance = Math.hypot(
  731. e.touches[ 0 ].clientX - e.touches[ 1 ].clientX,
  732. e.touches[ 0 ].clientY - e.touches[ 1 ].clientY
  733. );
  734. if ( zoomTouchData === null ) {
  735. zoomTouchData = {
  736. distance
  737. };
  738. }
  739. const delta = ( zoomTouchData.distance - distance );
  740. zoomTouchData.distance = distance;
  741. let zoom = Math.min( Math.max( this.zoom - delta * .01, .5 ), 1.2 );
  742. if ( zoom < .52 ) zoom = .5;
  743. else if ( zoom > .98 ) zoom = 1;
  744. contentDOM.style.left = toPX( this.centerX / zoom );
  745. contentDOM.style.top = toPX( this.centerY / zoom );
  746. contentDOM.style.zoom = this.zoom = zoom;
  747. */
  748. }
  749. } else {
  750. e.preventDefault();
  751. e.stopImmediatePropagation();
  752. /*
  753. const delta = e.deltaY / 100;
  754. const zoom = Math.min( Math.max( this.zoom - delta * .1, .5 ), 1 );
  755. contentDOM.style.left = toPX( this.centerX / zoom );
  756. contentDOM.style.top = toPX( this.centerY / zoom );
  757. contentDOM.style.zoom = this.zoom = zoom;
  758. */
  759. }
  760. };
  761. dom.addEventListener( 'wheel', onZoom );
  762. dom.addEventListener( 'touchmove', onZoom );
  763. //dom.addEventListener( 'touchstart', onZoomStart );
  764. draggableDOM( dom, ( data ) => {
  765. const { delta, isTouch } = data;
  766. if ( ! isTouch ) {
  767. if ( data.scrollTop === undefined ) {
  768. data.scrollLeft = dom.scrollLeft;
  769. data.scrollTop = dom.scrollTop;
  770. }
  771. dom.scrollLeft = data.scrollLeft - delta.x;
  772. dom.scrollTop = data.scrollTop - delta.y;
  773. }
  774. if ( data.dragging ) {
  775. dom.classList.add( 'grabbing' );
  776. } else {
  777. dom.classList.remove( 'grabbing' );
  778. }
  779. }, 'dragging-canvas' );
  780. this._onMoveEvent = ( e ) => {
  781. const event = e.touches ? e.touches[ 0 ] : e;
  782. const { zoom, rect } = this;
  783. this.clientX = event.clientX;
  784. this.clientY = event.clientY;
  785. this.relativeClientX = ( ( ( dom.scrollLeft - this.centerX ) + event.clientX ) - rect.left ) / zoom;
  786. this.relativeClientY = ( ( ( dom.scrollTop - this.centerY ) + event.clientY ) - rect.top ) / zoom;
  787. };
  788. this._onContentLoaded = () => {
  789. this.centralize();
  790. };
  791. this._onUpdate = () => {
  792. this.update();
  793. };
  794. this.start();
  795. }
  796. get rect() {
  797. return this.dom.getBoundingClientRect();
  798. }
  799. get relativeX() {
  800. return this.dom.scrollLeft - this.centerX;
  801. }
  802. get relativeY() {
  803. return this.dom.scrollTop - this.centerY;
  804. }
  805. get centerX() {
  806. return this.width / 2;
  807. }
  808. get centerY() {
  809. return this.height / 2;
  810. }
  811. start() {
  812. this.updating = true;
  813. document.addEventListener( 'wheel', this._onMoveEvent, true );
  814. document.addEventListener( 'mousedown', this._onMoveEvent, true );
  815. document.addEventListener( 'touchstart', this._onMoveEvent, true );
  816. document.addEventListener( 'mousemove', this._onMoveEvent, true );
  817. document.addEventListener( 'touchmove', this._onMoveEvent, true );
  818. document.addEventListener( 'DOMContentLoaded', this._onContentLoaded );
  819. requestAnimationFrame( this._onUpdate );
  820. }
  821. stop() {
  822. this.updating = false;
  823. document.removeEventListener( 'wheel', this._onMoveEvent, true );
  824. document.removeEventListener( 'mousedown', this._onMoveEvent, true );
  825. document.removeEventListener( 'touchstart', this._onMoveEvent, true );
  826. document.removeEventListener( 'mousemove', this._onMoveEvent, true );
  827. document.removeEventListener( 'touchmove', this._onMoveEvent, true );
  828. document.removeEventListener( 'DOMContentLoaded', this._onContentLoaded );
  829. }
  830. add( node ) {
  831. this.nodes.push( node );
  832. node.canvas = this;
  833. this.contentDOM.appendChild( node.dom );
  834. return this;
  835. }
  836. remove( node ) {
  837. if ( node === this.selected ) {
  838. this.select();
  839. }
  840. this.unlink( node );
  841. const nodes = this.nodes;
  842. nodes.splice( nodes.indexOf( node ), 1 );
  843. node.canvas = null;
  844. this.contentDOM.removeChild( node.dom );
  845. return this;
  846. }
  847. clear() {
  848. const nodes = this.nodes;
  849. while ( nodes.length > 0 ) {
  850. this.remove( nodes[ 0 ] );
  851. }
  852. return this;
  853. }
  854. unlink( node ) {
  855. const links = this.getLinks();
  856. for ( const link of links ) {
  857. if ( link.outputElement && link.outputElement.node === node ) {
  858. link.inputElement.connect();
  859. }
  860. }
  861. }
  862. getLinks() {
  863. const links = [];
  864. for ( const node of this.nodes ) {
  865. links.push( ...node.getLinks() );
  866. }
  867. return links;
  868. }
  869. centralize() {
  870. this.dom.scroll( this.centerX, this.centerY );
  871. return this;
  872. }
  873. select( node = null ) {
  874. if ( this.selected !== null ) {
  875. this.selected.dom.classList.remove( 'selected' );
  876. }
  877. if ( node !== null ) {
  878. node.dom.classList.add( 'selected' );
  879. }
  880. this.selected = node;
  881. }
  882. update() {
  883. if ( this.updating === false ) return;
  884. requestAnimationFrame( this._onUpdate );
  885. const { dom, zoom, canvas, frontCanvas, frontContext, context } = this;
  886. const width = window.innerWidth;
  887. const height = window.innerHeight;
  888. const domRect = this.rect;
  889. if ( canvas.width !== width || canvas.height !== height ) {
  890. canvas.width = width;
  891. canvas.height = height;
  892. frontCanvas.width = width;
  893. frontCanvas.height = height;
  894. }
  895. context.clearRect( 0, 0, width, height );
  896. frontContext.clearRect( 0, 0, width, height );
  897. context.globalCompositeOperation = 'lighter';
  898. frontContext.globalCompositeOperation = 'source-over';
  899. const links = this.getLinks();
  900. const aPos = { x: 0, y: 0 };
  901. const bPos = { x: 0, y: 0 };
  902. const offsetIORadius = 10;
  903. let dragging = '';
  904. for ( const link of links ) {
  905. const { lioElement, rioElement } = link;
  906. let draggingLink = '';
  907. let length = 0;
  908. if ( lioElement !== null ) {
  909. const rect = lioElement.dom.getBoundingClientRect();
  910. length = Math.max( length, lioElement.rioLength );
  911. aPos.x = rect.x + rect.width;
  912. aPos.y = rect.y + ( rect.height / 2 );
  913. } else {
  914. aPos.x = this.clientX;
  915. aPos.y = this.clientY;
  916. draggingLink = 'lio';
  917. }
  918. if ( rioElement !== null ) {
  919. const rect = rioElement.dom.getBoundingClientRect();
  920. length = Math.max( length, rioElement.lioLength );
  921. bPos.x = rect.x;
  922. bPos.y = rect.y + ( rect.height / 2 );
  923. } else {
  924. bPos.x = this.clientX;
  925. bPos.y = this.clientY;
  926. draggingLink = 'rio';
  927. }
  928. dragging = dragging || draggingLink;
  929. const drawContext = draggingLink ? frontContext : context;
  930. if ( draggingLink || length === 1 ) {
  931. if ( draggingLink === 'rio' ) {
  932. aPos.x += offsetIORadius;
  933. bPos.x /= zoom;
  934. bPos.y /= zoom;
  935. } else if ( draggingLink === 'lio' ) {
  936. bPos.x -= offsetIORadius;
  937. aPos.x /= zoom;
  938. aPos.y /= zoom;
  939. }
  940. drawLine(
  941. aPos.x * zoom, aPos.y * zoom,
  942. bPos.x * zoom, bPos.y * zoom,
  943. false, 2, '#ffffff', drawContext
  944. );
  945. } else {
  946. length = Math.min( length, 4 );
  947. for ( let i = 0; i < length; i ++ ) {
  948. const color = colors[ i ] || '#ffffff';
  949. const marginY = 4;
  950. const rioLength = Math.min( lioElement.rioLength, length );
  951. const lioLength = Math.min( rioElement.lioLength, length );
  952. const aCenterY = ( ( rioLength * marginY ) * .5 ) - ( marginY / 2 );
  953. const bCenterY = ( ( lioLength * marginY ) * .5 ) - ( marginY / 2 );
  954. const aIndex = Math.min( i, rioLength - 1 );
  955. const bIndex = Math.min( i, lioLength - 1 );
  956. const aPosY = aIndex * marginY;
  957. const bPosY = bIndex * marginY;
  958. drawLine(
  959. aPos.x * zoom, ( ( aPos.y + aPosY ) - aCenterY ) * zoom,
  960. bPos.x * zoom, ( ( bPos.y + bPosY ) - bCenterY ) * zoom,
  961. false, 2, color, drawContext
  962. );
  963. }
  964. }
  965. }
  966. context.globalCompositeOperation = 'destination-in';
  967. context.fillRect( domRect.x, domRect.y, domRect.width, domRect.height );
  968. if ( dragging !== '' ) {
  969. dom.classList.add( 'dragging-' + dragging );
  970. } else {
  971. dom.classList.remove( 'dragging-lio' );
  972. dom.classList.remove( 'dragging-rio' );
  973. }
  974. }
  975. serialize( data ) {
  976. const nodes = [];
  977. for ( const node of this.nodes ) {
  978. nodes.push( node.toJSON( data ).id );
  979. }
  980. data.nodes = nodes;
  981. }
  982. deserialize( data ) {
  983. for ( const id of data.nodes ) {
  984. this.add( data.objects[ id ] );
  985. }
  986. }
  987. }
  988. class Loader extends EventTarget {
  989. constructor() {
  990. super();
  991. }
  992. async load( url ) {
  993. return await fetch( url )
  994. .then( response => response.json() )
  995. .then( result => {
  996. return this.parse( result );
  997. } )
  998. .catch( err => {
  999. console.error( 'Loader:', err );
  1000. } );
  1001. }
  1002. static parse( json, lib = null ) {
  1003. json = Loader.parseObjects( json, lib );
  1004. const flowObj = new Flow[ json.type ]();
  1005. flowObj.deserialize( json );
  1006. return flowObj;
  1007. }
  1008. static parseObjects( json, lib = null ) {
  1009. json = { ...json };
  1010. const objects = {};
  1011. for ( const id in json.objects ) {
  1012. const obj = json.objects[ id ];
  1013. obj.objects = objects;
  1014. const Class = lib && lib[ obj.type ] ? lib[ obj.type ] : Flow[ obj.type ];
  1015. objects[ id ] = new Class();
  1016. }
  1017. const ref = new WeakMap();
  1018. const deserializePass = ( prop = null ) => {
  1019. for ( const id in json.objects ) {
  1020. const newObject = objects[ id ];
  1021. if ( ref.has( newObject ) === false && ( prop === null || newObject[ prop ] === true ) ) {
  1022. ref.set( newObject, true );
  1023. newObject.deserialize( json.objects[ id ] );
  1024. }
  1025. }
  1026. };
  1027. deserializePass( 'isNode' );
  1028. deserializePass( 'isElement' );
  1029. deserializePass( 'isInput' );
  1030. deserializePass();
  1031. json.objects = objects;
  1032. return json;
  1033. }
  1034. }
  1035. const ENTER_KEY$1 = 13;
  1036. class StringInput extends Input {
  1037. constructor( value = '' ) {
  1038. const dom = document.createElement( 'input' );
  1039. super( dom );
  1040. dom.type = 'text';
  1041. dom.value = value;
  1042. dom.spellcheck = false;
  1043. dom.autocomplete = 'off';
  1044. dom.onblur = () => {
  1045. this.dispatchEvent( new Event( 'blur' ) );
  1046. };
  1047. dom.onchange = () => {
  1048. this.dispatchEvent( new Event( 'change' ) );
  1049. };
  1050. dom.onkeydown = ( e ) => {
  1051. if ( e.keyCode === ENTER_KEY$1 ) {
  1052. e.target.blur();
  1053. }
  1054. e.stopPropagation();
  1055. };
  1056. }
  1057. }
  1058. const ENTER_KEY = 13;
  1059. class NumberInput extends Input {
  1060. constructor( value = 0, min = - Infinity, max = Infinity, step = .01 ) {
  1061. const dom = document.createElement( 'input' );
  1062. super( dom );
  1063. this.min = min;
  1064. this.max = max;
  1065. this.step = step;
  1066. this.integer = false;
  1067. dom.type = 'text';
  1068. dom.className = 'number';
  1069. dom.value = this._getString( value );
  1070. dom.spellcheck = false;
  1071. dom.autocomplete = 'off';
  1072. dom.onblur = () => {
  1073. this.dispatchEvent( new Event( 'blur' ) );
  1074. };
  1075. dom.onkeydown = ( e ) => {
  1076. if ( e.key.length === 1 && /\d|\/./.test( e.key ) !== true ) {
  1077. return false;
  1078. }
  1079. this.dispatchEvent( new Event( 'change' ) );
  1080. if ( e.keyCode === ENTER_KEY ) {
  1081. e.target.blur();
  1082. }
  1083. e.stopPropagation();
  1084. };
  1085. draggableDOM( dom, ( data ) => {
  1086. const { delta } = data;
  1087. if ( data.value === undefined ) {
  1088. data.value = this.getValue();
  1089. }
  1090. const diff = delta.x - delta.y;
  1091. const value = data.value + ( diff * this.step );
  1092. this.dom.value = this._getString( value.toFixed( this.precision ) );
  1093. this.dispatchEvent( new Event( 'change' ) );
  1094. } );
  1095. }
  1096. setRange( min, max, step ) {
  1097. this.min = min;
  1098. this.max = max;
  1099. this.step = step;
  1100. this.dom.value = this._getString( this.value );
  1101. this.dispatchEvent( new Event( 'range' ) );
  1102. this.dispatchEvent( new Event( 'change' ) );
  1103. return this;
  1104. }
  1105. get precision() {
  1106. if ( this.integer === true ) return 0;
  1107. const fract = this.step % 1;
  1108. return fract !== 0 ? fract.toString().split( '.' )[ 1 ].length : 1;
  1109. }
  1110. setValue( val ) {
  1111. return super.setValue( this._getString( val ) );
  1112. }
  1113. getValue() {
  1114. return Number( this.dom.value );
  1115. }
  1116. serialize( data ) {
  1117. const { min, max } = this;
  1118. if ( min !== - Infinity && max !== Infinity ) {
  1119. data.min = this.min;
  1120. data.max = this.max;
  1121. data.step = this.step;
  1122. }
  1123. super.serialize( data );
  1124. }
  1125. deserialize( data ) {
  1126. if ( data.min !== undefined ) {
  1127. const { min, max, step } = this;
  1128. this.setRange( min, max, step );
  1129. }
  1130. super.deserialize( data );
  1131. }
  1132. _getString( value ) {
  1133. let num = Math.min( Math.max( Number( value ), this.min ), this.max );
  1134. if ( this.integer === true ) {
  1135. return Math.floor( num );
  1136. } else {
  1137. return num + ( num % 1 ? '' : '.0' );
  1138. }
  1139. }
  1140. }
  1141. const getStep = ( min, max ) => {
  1142. const sensibility = .001;
  1143. return ( max - min ) * sensibility;
  1144. };
  1145. class SliderInput extends Input {
  1146. constructor( value = 0, min = 0, max = 100 ) {
  1147. const dom = document.createElement( 'f-subinputs' );
  1148. super( dom );
  1149. value = Math.min( Math.max( value, min ), max );
  1150. const step = getStep( min, max );
  1151. const rangeDOM = document.createElement( 'input' );
  1152. rangeDOM.type = 'range';
  1153. rangeDOM.min = min;
  1154. rangeDOM.max = max;
  1155. rangeDOM.step = step;
  1156. rangeDOM.value = value;
  1157. const field = new NumberInput( value, min, max, step );
  1158. field.dom.className = 'range-value';
  1159. field.onChange( () => {
  1160. rangeDOM.value = field.getValue();
  1161. } );
  1162. field.addEventListener( 'range', () => {
  1163. rangeDOM.min = field.min;
  1164. rangeDOM.max = field.max;
  1165. rangeDOM.step = field.step;
  1166. rangeDOM.value = field.getValue();
  1167. } );
  1168. dom.appendChild( rangeDOM );
  1169. dom.appendChild( field.dom );
  1170. this.min = min;
  1171. this.max = max;
  1172. this.step = step;
  1173. this.rangeDOM = rangeDOM;
  1174. this.field = field;
  1175. const updateRangeValue = () => {
  1176. let value = Number( rangeDOM.value );
  1177. if ( value !== this.max && value + this.step >= this.max ) {
  1178. // fix not end range fraction
  1179. rangeDOM.value = value = this.max;
  1180. }
  1181. this.field.setValue( value );
  1182. };
  1183. draggableDOM( rangeDOM, () => {
  1184. updateRangeValue();
  1185. this.dispatchEvent( new Event( 'change' ) );
  1186. }, '' );
  1187. }
  1188. setRange( min, max ) {
  1189. this.field.setRange( min, max, getStep( min, max ) );
  1190. this.dispatchEvent( new Event( 'range' ) );
  1191. this.dispatchEvent( new Event( 'change' ) );
  1192. return this;
  1193. }
  1194. setValue( val ) {
  1195. this.field.setValue( val );
  1196. this.rangeDOM.value = val;
  1197. this.dispatchEvent( new Event( 'change' ) );
  1198. return this;
  1199. }
  1200. getValue() {
  1201. return this.field.getValue();
  1202. }
  1203. serialize( data ) {
  1204. data.min = this.min;
  1205. data.max = this.max;
  1206. super.serialize( data );
  1207. }
  1208. deserialize( data ) {
  1209. const { min, max } = data;
  1210. this.setRange( min, max );
  1211. super.deserialize( data );
  1212. }
  1213. }
  1214. class ColorInput extends Input {
  1215. constructor( value = 0x0099ff ) {
  1216. const dom = document.createElement( 'input' );
  1217. super( dom );
  1218. dom.type = 'color';
  1219. dom.value = toHex( value );
  1220. dom.oninput = () => {
  1221. this.dispatchEvent( new Event( 'change' ) );
  1222. };
  1223. }
  1224. setValue( value ) {
  1225. return super.setValue( toHex( value ) );
  1226. }
  1227. getValue() {
  1228. return parseInt( super.getValue().substr( 1 ), 16 );
  1229. }
  1230. }
  1231. class TextInput extends Input {
  1232. constructor( innerText = '' ) {
  1233. const dom = document.createElement( 'textarea' );
  1234. super( dom );
  1235. dom.innerText = innerText;
  1236. }
  1237. setValue( val ) {
  1238. this.dom.innerText = val;
  1239. return this;
  1240. }
  1241. getValue() {
  1242. return this.dom.innerText;
  1243. }
  1244. }
  1245. class ButtonInput extends Input {
  1246. constructor( innterText = '' ) {
  1247. const dom = document.createElement( 'button' );
  1248. const spanDOM = document.createElement( 'span' );
  1249. dom.appendChild( spanDOM );
  1250. const iconDOM = document.createElement( 'i' );
  1251. dom.appendChild( iconDOM );
  1252. super( dom );
  1253. this.spanDOM = spanDOM;
  1254. this.iconDOM = iconDOM;
  1255. spanDOM.innerText = innterText;
  1256. dom.onclick = () => {
  1257. this.dispatchEvent( new Event( 'click' ) );
  1258. };
  1259. }
  1260. setIcon( className ) {
  1261. this.iconDOM.className = className;
  1262. return this;
  1263. }
  1264. setValue( val ) {
  1265. this.spanDOM.innerText = val;
  1266. return this;
  1267. }
  1268. getValue() {
  1269. return this.spanDOM.innerText;
  1270. }
  1271. }
  1272. class LabelElement extends Element {
  1273. constructor( label = '', align = '' ) {
  1274. super();
  1275. this.labelDOM = document.createElement( 'f-label' );
  1276. this.inputsDOM = document.createElement( 'f-inputs' );
  1277. const spanDOM = document.createElement( 'span' );
  1278. const iconDOM = document.createElement( 'i' );
  1279. this.spanDOM = spanDOM;
  1280. this.iconDOM = iconDOM;
  1281. this.labelDOM.appendChild( this.spanDOM );
  1282. this.labelDOM.appendChild( this.iconDOM );
  1283. this.dom.appendChild( this.labelDOM );
  1284. this.dom.appendChild( this.inputsDOM );
  1285. this.setLabel( label );
  1286. this.setAlign( align );
  1287. }
  1288. setIcon( value ) {
  1289. this.iconDOM.className = value;
  1290. return this;
  1291. }
  1292. getIcon() {
  1293. return this.iconDOM.className;
  1294. }
  1295. setAlign( align ) {
  1296. this.labelDOM.className = align;
  1297. }
  1298. setLabel( val ) {
  1299. this.spanDOM.innerText = val;
  1300. }
  1301. getLabel() {
  1302. return this.spanDOM.innerText;
  1303. }
  1304. serialize( data ) {
  1305. super.serialize( data );
  1306. const label = this.getLabel();
  1307. const icon = this.getIcon();
  1308. data.label = label;
  1309. if ( icon !== '' ) {
  1310. data.icon = icon;
  1311. }
  1312. }
  1313. deserialize( data ) {
  1314. super.deserialize( data );
  1315. this.setLabel( data.label );
  1316. if ( data.icon !== undefined ) {
  1317. this.setIcon( data.icon );
  1318. }
  1319. }
  1320. }
  1321. class DraggableElement extends Element {
  1322. constructor( draggable = true ) {
  1323. super( true );
  1324. this.draggable = draggable;
  1325. const onDrag = ( e ) => {
  1326. e.preventDefault();
  1327. if ( this.draggable === true ) {
  1328. draggableDOM( this.node.dom );
  1329. }
  1330. };
  1331. const { dom } = this;
  1332. dom.addEventListener( 'mousedown', onDrag, true );
  1333. dom.addEventListener( 'touchstart', onDrag, true );
  1334. }
  1335. }
  1336. class TitleElement extends DraggableElement {
  1337. constructor( title, draggable = true ) {
  1338. super( draggable );
  1339. const { dom } = this;
  1340. dom.className = 'title';
  1341. const spanDOM = document.createElement( 'span' );
  1342. spanDOM.innerText = title;
  1343. const iconDOM = document.createElement( 'i' );
  1344. const toolbarDOM = document.createElement( 'f-toolbar' );
  1345. this.buttons = [];
  1346. this.spanDOM = spanDOM;
  1347. this.iconDOM = iconDOM;
  1348. this.toolbarDOM = toolbarDOM;
  1349. dom.appendChild( spanDOM );
  1350. dom.appendChild( iconDOM );
  1351. dom.appendChild( toolbarDOM );
  1352. }
  1353. setIcon( value ) {
  1354. this.iconDOM.className = value;
  1355. return this;
  1356. }
  1357. getIcon() {
  1358. return this.iconDOM.className;
  1359. }
  1360. setTitle( value ) {
  1361. this.spanDOM.innerText = value;
  1362. return this;
  1363. }
  1364. getTitle() {
  1365. return this.spanDOM.innerText;
  1366. }
  1367. addButton( button ) {
  1368. this.buttons.push( button );
  1369. this.toolbarDOM.appendChild( button.dom );
  1370. return this;
  1371. }
  1372. serialize( data ) {
  1373. super.serialize( data );
  1374. const title = this.getTitle();
  1375. const icon = this.getIcon();
  1376. data.title = title;
  1377. if ( icon !== '' ) {
  1378. data.icon = icon;
  1379. }
  1380. }
  1381. deserialize( data ) {
  1382. super.deserialize( data );
  1383. this.setTitle( data.title );
  1384. if ( data.icon !== undefined ) {
  1385. this.setIcon( data.icon );
  1386. }
  1387. }
  1388. }
  1389. class PanelNode extends Node {
  1390. constructor( title = 'Panel', align = 'top-right' ) {
  1391. super();
  1392. const titleElement = new TitleElement( title );
  1393. this.add( titleElement );
  1394. const collapseButton = new ButtonInput( '🗕' );
  1395. collapseButton.onClick( () => {
  1396. this.setCollapse( ! this.collapsed );
  1397. } );
  1398. titleElement.addButton( collapseButton );
  1399. this.collapseButton = collapseButton;
  1400. this.titleElement = titleElement;
  1401. this.align = align;
  1402. this.collapsed = false;
  1403. this.setAlign( align );
  1404. this.setStyle( 'rouded' );
  1405. }
  1406. setCollapse( value ) {
  1407. const cssClass = 'closed';
  1408. this.dom.classList.remove( cssClass );
  1409. this.collapsed = value;
  1410. this.collapseButton.value = value ? '🗖' : '🗕';
  1411. if ( value === true ) {
  1412. this.dom.classList.add( cssClass );
  1413. }
  1414. return this;
  1415. }
  1416. setAlign( align ) {
  1417. if ( this.align ) this.dom.classList.remove( this.align );
  1418. this.dom.classList.add( align );
  1419. this.align = align;
  1420. return this;
  1421. }
  1422. addInput( inputClass, object, property, ...params ) {
  1423. const value = object[ property ];
  1424. const input = new inputClass( value, ...params );
  1425. input.onChange( () => {
  1426. object[ property ] = input.value;
  1427. } );
  1428. this.add( new LabelElement( property ).add( input ) );
  1429. return input;
  1430. }
  1431. addSlider( object, property, min, max ) {
  1432. return this.addInput( SliderInput, object, property, min, max );
  1433. }
  1434. addNumber( object, property ) {
  1435. return this.addInput( NumberInput, object, property );
  1436. }
  1437. addColor( object, property ) {
  1438. return this.addInput( ColorInput, object, property );
  1439. }
  1440. addString( object, property ) {
  1441. return this.addInput( StringInput, object, property );
  1442. }
  1443. addText( object, property ) {
  1444. const input = this.addInput( TextInput, object, property );
  1445. input.element.setHeight( 70 );
  1446. return input;
  1447. }
  1448. addButton( name ) {
  1449. const input = new ButtonInput( name );
  1450. this.add( new Element().setHeight( 34 ).add( input ) );
  1451. return input;
  1452. }
  1453. }
  1454. class Menu {
  1455. constructor( className, target = null ) {
  1456. const dom = document.createElement( 'f-menu' );
  1457. dom.className = className + ' hidden';
  1458. this.dom = dom;
  1459. this.visible = false;
  1460. this.setTarget( target );
  1461. }
  1462. show( x, y ) {
  1463. this.setPosition( x, y );
  1464. this.dom.classList.remove( 'hidden' );
  1465. this.visible = true;
  1466. return this;
  1467. }
  1468. hide() {
  1469. this.dom.classList.add( 'hidden' );
  1470. this.visible = false;
  1471. }
  1472. setTarget( target = null ) {
  1473. if ( target !== null ) {
  1474. const onContextMenu = ( e ) => {
  1475. e.preventDefault();
  1476. const rect = this.target.getBoundingClientRect();
  1477. this.show( e.pageX - rect.left, e.pageY - rect.top );
  1478. };
  1479. const onDown = ( e ) => {
  1480. if ( this.visible === true && e.target.closest( 'f-menu' ) === null ) {
  1481. this.hide();
  1482. }
  1483. };
  1484. this.target = target;
  1485. target.addEventListener( 'mousedown', onDown, true );
  1486. target.addEventListener( 'touchstart', onDown, true );
  1487. target.addEventListener( 'contextmenu', onContextMenu, false );
  1488. }
  1489. return this;
  1490. }
  1491. add( button, submenu = null ) {
  1492. const liDOM = document.createElement( 'f-item' );
  1493. if ( submenu !== null ) {
  1494. liDOM.classList.add( 'submenu' );
  1495. liDOM.appendChild( submenu.dom );
  1496. }
  1497. liDOM.appendChild( button.dom );
  1498. this.dom.appendChild( liDOM );
  1499. return this;
  1500. }
  1501. setPosition( x, y ) {
  1502. const dom = this.dom;
  1503. dom.style.left = toPX( x );
  1504. dom.style.top = toPX( y );
  1505. return this;
  1506. }
  1507. }
  1508. class ContextMenu extends Menu {
  1509. constructor( target = null ) {
  1510. super( 'context', target );
  1511. }
  1512. }
  1513. class CircleMenu extends Menu {
  1514. constructor( target = null ) {
  1515. super( 'circle', target );
  1516. }
  1517. }
  1518. class SelectInput extends Input {
  1519. constructor( options = [] ) {
  1520. const dom = document.createElement( 'select' );
  1521. super( dom );
  1522. dom.onchange = () => {
  1523. this.dispatchEvent( new Event( 'change' ) );
  1524. };
  1525. this.setOptions( options );
  1526. }
  1527. setOptions( options ) {
  1528. const dom = this.dom;
  1529. this.options = options;
  1530. dom.innerHTML = '';
  1531. for ( let index = 0; index < options.length; index ++ ) {
  1532. let opt = options[ index ];
  1533. if ( typeof opt === 'string' ) {
  1534. opt = { name: opt, value: index };
  1535. }
  1536. const option = document.createElement( 'option' );
  1537. option.innerText = opt.name;
  1538. option.value = opt.value;
  1539. dom.appendChild( option );
  1540. }
  1541. return this;
  1542. }
  1543. getOptions() {
  1544. return this._options;
  1545. }
  1546. serialize( data ) {
  1547. data.options = [ ...this.options ];
  1548. super.serialize( data );
  1549. }
  1550. deserialize( data ) {
  1551. this.setOptions( data.options );
  1552. super.deserialize( data );
  1553. }
  1554. }
  1555. class ToggleInput extends Input {
  1556. constructor( value = false ) {
  1557. const dom = document.createElement( 'input' );
  1558. super( dom );
  1559. dom.type = 'checkbox';
  1560. dom.className = 'toggle';
  1561. dom.checked = value;
  1562. }
  1563. setValue( val ) {
  1564. this.dom.checked = val;
  1565. this.dispatchEvent( new Event( 'change' ) );
  1566. return this;
  1567. }
  1568. getValue() {
  1569. return this.dom.checked;
  1570. }
  1571. }
  1572. export { ButtonInput, Canvas, CircleMenu, ColorInput, ContextMenu, DraggableElement, Element, Input, LabelElement, Loader, Menu, Node, NumberInput, PanelNode, SelectInput, Serializer, SliderInput, StringInput, TextInput, TitleElement, ToggleInput, Utils };