flow.module.js 79 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102
  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( `@keyframes f-animation-open { 0% { transform: scale(.5); opacity: 0; } 100% { transform: scale(1); opacity: 1; }}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 *: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;}f-node { animation: .2s f-animation-open 1 alternate ease-out;}f-tips,f-drop,f-menu,f-menu input,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; margin: 0; padding: 0; border: 0; user-select: none; -webkit-tap-highlight-color: transparent; transition: background 0.2s ease, filter 0.2s ease;}f-element input:read-only { color: #666;}f-element input,f-element textarea { text-transform: initial;}f-element input { transition: background 0.1s ease;}f-element input,f-element select,f-element button,f-element textarea { background-color: #232324d1;}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:after,f-element:before { transition: opacity .17s; opacity: 0; content: '';}f-element[tooltip]:hover:after,f-element[tooltip]:focus-within:after { font-size: 14px !important; display: flex; justify-content: center; position: fixed; margin-left: -7px; width: calc( 100% ); background: #1d1d1de8; border: 1px solid #444444a1; border-radius: 6px; color: #dadada; content: attr( tooltip ); margin-top: -41px; font-size: 16px; padding-top: 3px; padding-bottom: 3px; z-index: 10; opacity: 1; backdrop-filter: blur(4px); white-space: nowrap; overflow: hidden; text-shadow: 1px 1px 0px #0007;}f-element[tooltip]:hover:before,f-element[tooltip]:focus-within:before { border: solid; border-color: #1d1d1de8 transparent; border-width: 12px 6px 0 6px; left: calc( 50% - 6px ); bottom: 30px; position: absolute; opacity: 1; z-index: 11;}f-element[error] { background-color: #ff0000;}f-element[error]:hover:after,f-element[error]:focus-within:after { border: none; background-color: #ff0000bb; filter: drop-shadow( 2px 2px 5px #000 ); color: #fff;}f-element[error]:hover:before,f-element[error]:focus-within:before { border-color: #ff0000bb transparent;}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; 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.input-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; justify-content: flex-end; 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.inputs-disable f-inputs { filter: grayscale(100%); opacity: .5;}f-element.inputs-disable f-inputs input { pointer-events: none;}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.center f-label { text-align: center;}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.title f-toolbar i { font-size: 20px; right: unset; left: 0px;}f-element.input-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 { 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; transition: all .2s;}f-element.input-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-element.invalid > f-io { zoom: 1 !important;}f-element.invalid::after { font-size: 14px !important; display: flex; justify-content: center; align-items:center; margin: auto; position: absolute; width: 100%; height: 100%; background: #bd0b0b77; vertical-align: middle; color: #fff; content: 'Not Compatible'; opacity: .95; backdrop-filter: grayscale(100%); white-space: nowrap; overflow: hidden; left: 0; top: 0; text-transform: initial;}f-element.invalid > f-inputs,f-element.invalid > f-label { opacity: .1;}f-drop { width: 100%; height: 100%; position: sticky; left: 0; top: 0; background: #02358417; text-align: center; justify-content: center; align-items: center; display: flex; box-shadow: inset 0 0 20px 10px #464ace17; pointer-events: none; transition: all .07s; opacity: 0; visibility: hidden;}f-drop.visible { visibility: unset; opacity: unset; transition: all .23s;}f-drop span { opacity: .5; font-size: 40px; text-shadow: 0px 0px 5px #000; font-weight: bold;}f-tooltip { pointer-events: none;}f-tooltip { position: absolute; left: 0; top: 0; background: rgba(0,0,0,.8); backdrop-filter: blur(4px); font-size: 14px; padding: 7px; left: 50%; border-radius: 10px; transform: translateX(-50%); visibility: hidden; pointer-events: none; opacity: 0; transition: all 0.3s ease; z-index: 150; white-space: nowrap;}f-menu.context,f-menu.search { position: absolute;}f-menu.context { width: 170px; z-index: 110;}f-menu.search { bottom: 85px; left: 50%; transform: translateX(-50%); z-index: 10; width: 300px;}f-menu.context f-list { display: block; margin: 0; background: #171717e6; font-size: 12px; border-radius: 6px; backdrop-filter: blur(6px); border: 1px solid #7e7e7e45; box-shadow: 3px 3px 6px rgba(0,0,0,.2); transition: opacity 0.2s ease, transform 0.1s ease;}f-menu.search f-list { margin: 0 6px 0 6px; display: flex; flex-direction: column-reverse; margin-bottom: 5px;}f-menu.context.hidden { visibility: hidden; opacity: 0;}f-menu.context f-item,f-menu.search f-item { display: block; position: relative; margin: 0; padding: 0; white-space: nowrap;}f-menu.search f-item { opacity: 0;}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,f-menu.context f-item.active > f-menu { visibility: unset; transform: unset; opacity: unset;}f-menu.context f-menu { top: 0px; left: calc( 100% - 4px );}f-menu.context f-item button,f-menu.search 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: rgba(45, 45, 48, 0.95); border: 0; color: #ddd; margin: 3px; text-shadow: 1px 1px 0px #0007;}f-menu.context f-item button i,f-menu.search f-item button i { float: left; font-size: 16px;}f-menu.context f-item button span,f-menu.search f-item button span { margin-left: 6px;}f-menu.context f-item:hover > button,f-menu.search f-item:hover > button,f-menu.search f-item.active > button { color: #fff; background-color: rgba(61, 70, 82, 0.98);}f-menu.search f-item:hover,f-menu.search f-item.active { opacity: 1 !important;}f-menu.context f-item button:active { outline: solid 1px rgba( 0, 80, 200, 0.98 );}f-menu.context f-item f-tooltip { margin-left: 85px; top: -50px;}f-menu.search f-item { display: none;}f-menu.search f-item:nth-child(1) { opacity: 1; display: unset;}f-menu.search f-item:nth-child(2) { opacity: .8; display: unset;}f-menu.search f-item:nth-child(3) { opacity: .6; display: unset;}f-menu.search f-item:nth-child(4) { opacity: .4; display: unset;}f-menu.search f-item button { border-radius: 14px;}f-tips { right: 10px; top: 10px; position: absolute; z-index: 100; pointer-events: none; display: flex; flex-direction: column;}f-tips f-tip { width: 450px; font-size: 13px; border-radius: 6px; text-align: center; display: block; height: auto; color: #ffffffe0; margin: 4px; padding: 4px; background: #17171794; border: 1px solid #7e7e7e38; line-height: 100%; backdrop-filter: blur(6px); transition: all 0.2s ease; text-transform: initial; opacity: 0;}f-tips f-tip:nth-child(1) { opacity: 1;}f-tips f-tip:nth-child(2) { opacity: .75;}f-tips f-tip:nth-child(3) { opacity: .25;}f-tips f-tip:nth-child(4) { opacity: .1;}f-tips f-tip.error { background: #b900005e;}f-menu.search input { width: calc( 100% - 28px ); height: 41px; position: absolute; z-index: 10; border-radius: 20px; padding-left: 14px; padding-right: 14px; font-size: 15px; background-color: #17171794; border: 1px solid #7e7e7e45; backdrop-filter: blur(6px); box-shadow: 3px 3px 6px rgb(0 0 0 / 20%); text-transform: initial;}f-menu.circle { position: absolute; left: 40px; bottom: 40px; z-index: 100;}f-menu.circle f-item { align-content: space-around; margin-right: 20px;}f-menu.circle f-item button { width: 47px; height: 47px; font-size: 22px; 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 { margin-top: -60px;}.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-node:not(.selected):hover { filter: drop-shadow(0 0 6px #66666630); } f-element f-toolbar { visibility: hidden; opacity: 0; transition: opacity 0.2s ease; } f-node:hover > f-element f-toolbar { visibility: visible; opacity: 1; } f-element f-io:hover { zoom: 1.4; } f-menu.circle f-item button:hover { background-color: #2a2a2a; } f-menu.search input:hover, f-menu.search input:focus { background-color: #1a1a1a; filter: drop-shadow(0 0 6px #66666630); } f-menu.search input:focus { filter: drop-shadow(0 0 8px #4444dd); } f-menu.circle f-item button:hover > f-tooltip, f-menu.context f-item button:hover > f-tooltip { visibility: visible; opacity: 1; } f-menu.circle f-item button:hover > f-tooltip { margin-top: -50px; } f-menu.context f-item button:hover > f-tooltip { top: -30px; } f-menu.circle f-item button:focus > f-tooltip, f-menu.context f-item button:focus > f-tooltip { visibility: hidden; opacity: 0; }}@media (hover: none) and (pointer: coarse) { body.dragging f-canvas, body.connecting f-canvas { overflow: hidden !important; }}f-canvas { will-change: top, left;}f-node { will-change: transform !important;}` );
  13. const REVISION = '1';
  14. const Styles = {
  15. icons: {
  16. close: '',
  17. unlink: ''
  18. }
  19. };
  20. let _id = 0;
  21. class Serializer extends EventTarget {
  22. constructor() {
  23. super();
  24. this._id = _id ++;
  25. this._serializable = true;
  26. }
  27. get id() {
  28. return this._id;
  29. }
  30. setSerializable( value ) {
  31. this._serializable = value;
  32. return this;
  33. }
  34. getSerializable() {
  35. return this._serializable;
  36. }
  37. serialize( /*data*/ ) {
  38. console.warn( 'Serializer: Abstract function.' );
  39. }
  40. deserialize( /*data*/ ) {
  41. console.warn( 'Serializer: Abstract function.' );
  42. }
  43. toJSON( data = null ) {
  44. let object = null;
  45. const id = this.id;
  46. if ( data !== null ) {
  47. const objects = data.objects;
  48. object = objects[ id ];
  49. if ( object === undefined ) {
  50. object = { objects };
  51. this.serialize( object );
  52. delete object.objects;
  53. objects[ id ] = object;
  54. }
  55. } else {
  56. object = { objects: {} };
  57. this.serialize( object );
  58. }
  59. object.id = id;
  60. object.type = this.constructor.name;
  61. return object;
  62. }
  63. }
  64. class PointerMonitor {
  65. started = false;
  66. constructor() {
  67. this.x = 0;
  68. this.y = 0;
  69. this._onMoveEvent = ( e ) => {
  70. const event = e.touches ? e.touches[ 0 ] : e;
  71. this.x = event.x;
  72. this.y = event.y;
  73. };
  74. }
  75. start() {
  76. if ( this.started ) return;
  77. this.started = true;
  78. window.addEventListener( 'wheel', this._onMoveEvent, true );
  79. window.addEventListener( 'mousedown', this._onMoveEvent, true );
  80. window.addEventListener( 'touchstart', this._onMoveEvent, true );
  81. window.addEventListener( 'mousemove', this._onMoveEvent, true );
  82. window.addEventListener( 'touchmove', this._onMoveEvent, true );
  83. window.addEventListener( 'dragover', this._onMoveEvent, true );
  84. return this;
  85. }
  86. }
  87. const pointer = new PointerMonitor().start();
  88. const draggableDOM = ( dom, callback = null, className = 'dragging' ) => {
  89. let dragData = null;
  90. const getZoom = () => {
  91. let zoomDOM = dom;
  92. while ( zoomDOM && zoomDOM !== document ) {
  93. const zoom = zoomDOM.style.zoom;
  94. if ( zoom ) {
  95. return Number( zoom );
  96. }
  97. zoomDOM = zoomDOM.parentNode;
  98. }
  99. return 1;
  100. };
  101. const onMouseDown = ( e ) => {
  102. const event = e.touches ? e.touches[ 0 ] : e;
  103. e.stopImmediatePropagation();
  104. dragData = {
  105. client: { x: event.clientX, y: event.clientY },
  106. delta: { x: 0, y: 0 },
  107. start: { x: dom.offsetLeft, y: dom.offsetTop },
  108. dragging: false,
  109. isTouch: !! e.touches
  110. };
  111. window.addEventListener( 'mousemove', onGlobalMouseMove );
  112. window.addEventListener( 'mouseup', onGlobalMouseUp );
  113. window.addEventListener( 'touchmove', onGlobalMouseMove );
  114. window.addEventListener( 'touchend', onGlobalMouseUp );
  115. };
  116. const onGlobalMouseMove = ( e ) => {
  117. const { start, delta, client } = dragData;
  118. const event = e.touches ? e.touches[ 0 ] : e;
  119. const zoom = getZoom();
  120. delta.x = ( event.clientX - client.x ) / zoom;
  121. delta.y = ( event.clientY - client.y ) / zoom;
  122. dragData.x = start.x + delta.x;
  123. dragData.y = start.y + delta.y;
  124. if ( dragData.dragging === true ) {
  125. if ( callback !== null ) {
  126. callback( dragData );
  127. } else {
  128. dom.style.cssText += `; left: ${ dragData.x }px; top: ${ dragData.y }px;`;
  129. }
  130. e.stopImmediatePropagation();
  131. } else {
  132. if ( Math.abs( delta.x ) > 2 || Math.abs( delta.y ) > 2 ) {
  133. dragData.dragging = true;
  134. dom.classList.add( 'drag' );
  135. if ( className ) document.body.classList.add( className );
  136. e.stopImmediatePropagation();
  137. }
  138. }
  139. };
  140. const onGlobalMouseUp = ( e ) => {
  141. e.stopImmediatePropagation();
  142. dom.classList.remove( 'drag' );
  143. if ( className ) document.body.classList.remove( className );
  144. window.removeEventListener( 'mousemove', onGlobalMouseMove );
  145. window.removeEventListener( 'mouseup', onGlobalMouseUp );
  146. window.removeEventListener( 'touchmove', onGlobalMouseMove );
  147. window.removeEventListener( 'touchend', onGlobalMouseUp );
  148. if ( callback === null ) {
  149. dom.removeEventListener( 'mousedown', onMouseDown );
  150. dom.removeEventListener( 'touchstart', onMouseDown );
  151. }
  152. dragData.dragging = false;
  153. if ( callback !== null ) {
  154. callback( dragData );
  155. }
  156. };
  157. if ( dom instanceof Event ) {
  158. const e = dom;
  159. dom = e.target;
  160. onMouseDown( e );
  161. } else {
  162. dom.addEventListener( 'mousedown', onMouseDown );
  163. dom.addEventListener( 'touchstart', onMouseDown );
  164. }
  165. };
  166. const dispatchEventList = ( list, ...params ) => {
  167. for ( const callback of list ) {
  168. if ( callback( ...params ) === false ) {
  169. return false;
  170. }
  171. }
  172. return true;
  173. };
  174. const toPX = ( val ) => {
  175. if ( isNaN( val ) === false ) {
  176. val = `${ val }px`;
  177. }
  178. return val;
  179. };
  180. const toHex = ( val ) => {
  181. if ( isNaN( val ) === false ) {
  182. val = `#${ val.toString( 16 ).padStart( 6, '0' ) }`;
  183. }
  184. return val;
  185. };
  186. var Utils = /*#__PURE__*/Object.freeze( {
  187. __proto__: null,
  188. pointer: pointer,
  189. draggableDOM: draggableDOM,
  190. dispatchEventList: dispatchEventList,
  191. toPX: toPX,
  192. toHex: toHex
  193. } );
  194. class Link {
  195. constructor( inputElement = null, outputElement = null ) {
  196. this.inputElement = inputElement;
  197. this.outputElement = outputElement;
  198. }
  199. get lioElement() {
  200. if ( Link.InputDirection === 'left' ) {
  201. return this.outputElement;
  202. } else {
  203. return this.inputElement;
  204. }
  205. }
  206. get rioElement() {
  207. if ( Link.InputDirection === 'left' ) {
  208. return this.inputElement;
  209. } else {
  210. return this.outputElement;
  211. }
  212. }
  213. }
  214. //Link.InputDirection = 'right';
  215. Link.InputDirection = 'left';
  216. let selected = null;
  217. class Element extends Serializer {
  218. constructor( draggable = false ) {
  219. super();
  220. this.isElement = true;
  221. const dom = document.createElement( 'f-element' );
  222. dom.element = this;
  223. const onSelect = ( e ) => {
  224. let element = this;
  225. if ( e.changedTouches && e.changedTouches.length > 0 ) {
  226. const touch = e.changedTouches[ 0 ];
  227. let overDOM = document.elementFromPoint( touch.clientX, touch.clientY );
  228. while ( overDOM && ( ! overDOM.element || ! overDOM.element.isElement ) ) {
  229. overDOM = overDOM.parentNode;
  230. }
  231. element = overDOM ? overDOM.element : null;
  232. }
  233. const type = e.type;
  234. if ( ( type === 'mouseout' ) && selected === element ) {
  235. selected = null;
  236. } else {
  237. selected = element;
  238. }
  239. };
  240. if ( draggable === false ) {
  241. dom.ontouchstart = dom.onmousedown = ( e ) => {
  242. e.stopPropagation();
  243. };
  244. }
  245. dom.addEventListener( 'mouseup', onSelect, true );
  246. dom.addEventListener( 'mouseover', onSelect );
  247. dom.addEventListener( 'mouseout', onSelect );
  248. dom.addEventListener( 'touchmove', onSelect );
  249. dom.addEventListener( 'touchend', onSelect );
  250. this.inputs = [];
  251. this.links = [];
  252. this.dom = dom;
  253. this.lioLength = 0;
  254. this.rioLength = 0;
  255. this.events = {
  256. 'connect': [],
  257. 'connectChildren': [],
  258. 'valid': []
  259. };
  260. this.node = null;
  261. this.style = '';
  262. this.objectCallback = null;
  263. this.enabledInputs = true;
  264. this.visible = true;
  265. this.inputsDOM = dom;
  266. this.disconnectDOM = null;
  267. this.lioDOM = this._createIO( 'lio' );
  268. this.rioDOM = this._createIO( 'rio' );
  269. this.dom.classList.add( `input-${ Link.InputDirection }` );
  270. this.dom.append( this.lioDOM );
  271. this.dom.append( this.rioDOM );
  272. this.addEventListener( 'connect', ( ) => {
  273. dispatchEventList( this.events.connect, this );
  274. } );
  275. this.addEventListener( 'connectChildren', ( ) => {
  276. dispatchEventList( this.events.connectChildren, this );
  277. } );
  278. }
  279. setAttribute( name, value ) {
  280. this.dom.setAttribute( name, value );
  281. return this;
  282. }
  283. onValid( callback ) {
  284. this.events.valid.push( callback );
  285. return this;
  286. }
  287. onConnect( callback, childrens = false ) {
  288. this.events.connect.push( callback );
  289. if ( childrens ) {
  290. this.events.connectChildren.push( callback );
  291. }
  292. return this;
  293. }
  294. setObjectCallback( callback ) {
  295. this.objectCallback = callback;
  296. return this;
  297. }
  298. getObject( output = null ) {
  299. return this.objectCallback ? this.objectCallback( output ) : null;
  300. }
  301. setVisible( value ) {
  302. this.visible = value;
  303. this.dom.style.display = value ? '' : 'none';
  304. return this;
  305. }
  306. getVisible() {
  307. return this.visible;
  308. }
  309. setEnabledInputs( value ) {
  310. const dom = this.dom;
  311. if ( ! this.enabledInputs ) dom.classList.remove( 'inputs-disable' );
  312. if ( ! value ) dom.classList.add( 'inputs-disable' );
  313. this.enabledInputs = value;
  314. return this;
  315. }
  316. getEnabledInputs() {
  317. return this.enabledInputs;
  318. }
  319. setColor( color ) {
  320. this.dom.style[ 'background-color' ] = toHex( color );
  321. return this;
  322. }
  323. setStyle( style ) {
  324. const dom = this.dom;
  325. if ( this.style ) dom.classList.remove( this.style );
  326. if ( style ) dom.classList.add( style );
  327. this.style = style;
  328. return this;
  329. }
  330. setInput( length ) {
  331. if ( Link.InputDirection === 'left' ) {
  332. return this.setLIO( length );
  333. } else {
  334. return this.setRIO( length );
  335. }
  336. }
  337. setInputColor( color ) {
  338. if ( Link.InputDirection === 'left' ) {
  339. return this.setLIOColor( color );
  340. } else {
  341. return this.setRIOColor( color );
  342. }
  343. }
  344. setOutput( length ) {
  345. if ( Link.InputDirection === 'left' ) {
  346. return this.setRIO( length );
  347. } else {
  348. return this.setLIO( length );
  349. }
  350. }
  351. setOutputColor( color ) {
  352. if ( Link.InputDirection === 'left' ) {
  353. return this.setRIOColor( color );
  354. } else {
  355. return this.setLIOColor( color );
  356. }
  357. }
  358. get inputLength() {
  359. if ( Link.InputDirection === 'left' ) {
  360. return this.lioLength;
  361. } else {
  362. return this.rioLength;
  363. }
  364. }
  365. get outputLength() {
  366. if ( Link.InputDirection === 'left' ) {
  367. return this.rioLength;
  368. } else {
  369. return this.lioLength;
  370. }
  371. }
  372. setLIOColor( color ) {
  373. this.lioDOM.style[ 'border-color' ] = toHex( color );
  374. return this;
  375. }
  376. setLIO( length ) {
  377. this.lioLength = length;
  378. this.lioDOM.style.visibility = length > 0 ? '' : 'hidden';
  379. return this;
  380. }
  381. getLIOColor() {
  382. return this.lioDOM.style[ 'border-color' ];
  383. }
  384. setRIOColor( color ) {
  385. this.rioDOM.style[ 'border-color' ] = toHex( color );
  386. return this;
  387. }
  388. getRIOColor() {
  389. return this.rioDOM.style[ 'border-color' ];
  390. }
  391. setRIO( length ) {
  392. this.rioLength = length;
  393. this.rioDOM.style.visibility = length > 0 ? '' : 'hidden';
  394. return this;
  395. }
  396. add( input ) {
  397. this.inputs.push( input );
  398. input.element = this;
  399. this.inputsDOM.append( input.dom );
  400. return this;
  401. }
  402. setHeight( val ) {
  403. this.dom.style.height = toPX( val );
  404. return this;
  405. }
  406. getHeight() {
  407. return this.dom.style.height;
  408. }
  409. connect( element = null ) {
  410. if ( this.disconnectDOM !== null ) {
  411. // remove the current input
  412. this.disconnectDOM.dispatchEvent( new Event( 'disconnect' ) );
  413. }
  414. if ( element !== null ) {
  415. if ( dispatchEventList( this.events.valid, this, element, 'connect' ) === false ) {
  416. return false;
  417. }
  418. const link = new Link( this, element );
  419. this.links.push( link );
  420. if ( this.disconnectDOM === null ) {
  421. this.disconnectDOM = document.createElement( 'f-disconnect' );
  422. this.disconnectDOM.innerHTML = Styles.icons.unlink ? `<i class='${ Styles.icons.unlink }'></i>` : '✖';
  423. this.dom.append( this.disconnectDOM );
  424. const onDisconnect = () => {
  425. this.links = [];
  426. this.dom.removeChild( this.disconnectDOM );
  427. this.disconnectDOM.removeEventListener( 'mousedown', onClick, true );
  428. this.disconnectDOM.removeEventListener( 'touchstart', onClick, true );
  429. this.disconnectDOM.removeEventListener( 'disconnect', onDisconnect, true );
  430. element.removeEventListener( 'connect', onConnect );
  431. element.removeEventListener( 'connectChildren', onConnect );
  432. element.removeEventListener( 'nodeConnect', onConnect );
  433. element.removeEventListener( 'nodeConnectChildren', onConnect );
  434. element.removeEventListener( 'dispose', onDispose );
  435. this.disconnectDOM = null;
  436. };
  437. const onConnect = () => {
  438. this.dispatchEvent( new Event( 'connectChildren' ) );
  439. };
  440. const onDispose = () => {
  441. this.connect();
  442. };
  443. const onClick = ( e ) => {
  444. e.stopPropagation();
  445. this.connect();
  446. };
  447. this.disconnectDOM.addEventListener( 'mousedown', onClick, true );
  448. this.disconnectDOM.addEventListener( 'touchstart', onClick, true );
  449. this.disconnectDOM.addEventListener( 'disconnect', onDisconnect, true );
  450. element.addEventListener( 'connect', onConnect );
  451. element.addEventListener( 'connectChildren', onConnect );
  452. element.addEventListener( 'nodeConnect', onConnect );
  453. element.addEventListener( 'nodeConnectChildren', onConnect );
  454. element.addEventListener( 'dispose', onDispose );
  455. }
  456. }
  457. this.dispatchEvent( new Event( 'connect' ) );
  458. return true;
  459. }
  460. dispose() {
  461. this.dispatchEvent( new Event( 'dispose' ) );
  462. }
  463. serialize( data ) {
  464. const height = this.getHeight();
  465. const inputs = [];
  466. const links = [];
  467. for ( const input of this.inputs ) {
  468. inputs.push( input.toJSON( data ).id );
  469. }
  470. for ( const link of this.links ) {
  471. if ( link.inputElement !== null && link.outputElement !== null ) {
  472. links.push( link.outputElement.toJSON( data ).id );
  473. }
  474. }
  475. if ( this.inputLength > 0 ) data.inputLength = this.inputLength;
  476. if ( this.outputLength > 0 ) data.outputLength = this.outputLength;
  477. if ( inputs.length > 0 ) data.inputs = inputs;
  478. if ( links.length > 0 ) data.links = links;
  479. if ( this.style !== '' ) {
  480. data.style = this.style;
  481. }
  482. if ( height !== '' ) {
  483. data.height = height;
  484. }
  485. }
  486. deserialize( data ) {
  487. if ( data.inputLength !== undefined ) this.setInput( data.inputLength );
  488. if ( data.outputLength !== undefined ) this.setOutput( data.outputLength );
  489. if ( data.inputs !== undefined ) {
  490. const inputs = this.inputs;
  491. if ( inputs.length > 0 ) {
  492. let index = 0;
  493. for ( const id of data.inputs ) {
  494. data.objects[ id ] = inputs[ index ++ ];
  495. }
  496. } else {
  497. for ( const id of data.inputs ) {
  498. this.add( data.objects[ id ] );
  499. }
  500. }
  501. }
  502. if ( data.links !== undefined ) {
  503. for ( const id of data.links ) {
  504. this.connect( data.objects[ id ] );
  505. }
  506. }
  507. if ( data.style !== undefined ) {
  508. this.setStyle( data.style );
  509. }
  510. if ( data.height !== undefined ) {
  511. this.setHeight( data.height );
  512. }
  513. }
  514. getLinkedObject( output = null ) {
  515. const linkedElement = this.getLinkedElement();
  516. return linkedElement ? linkedElement.getObject( output ) : null;
  517. }
  518. getLinkedElement() {
  519. const link = this.getLink();
  520. return link ? link.outputElement : null;
  521. }
  522. getLink() {
  523. return this.links[ 0 ];
  524. }
  525. _createIO( type ) {
  526. const { dom } = this;
  527. const ioDOM = document.createElement( 'f-io' );
  528. ioDOM.style.visibility = 'hidden';
  529. ioDOM.className = type;
  530. const onConnectEvent = ( e ) => {
  531. e.preventDefault();
  532. e.stopPropagation();
  533. selected = null;
  534. const nodeDOM = this.node.dom;
  535. nodeDOM.classList.add( 'io-connect' );
  536. ioDOM.classList.add( 'connect' );
  537. dom.classList.add( 'select' );
  538. const defaultOutput = Link.InputDirection === 'left' ? 'lio' : 'rio';
  539. const link = type === defaultOutput ? new Link( this ) : new Link( null, this );
  540. const previewLink = new Link( link.inputElement, link.outputElement );
  541. this.links.push( link );
  542. draggableDOM( e, ( data ) => {
  543. if ( previewLink.outputElement )
  544. previewLink.outputElement.dom.classList.remove( 'invalid' );
  545. if ( previewLink.inputElement )
  546. previewLink.inputElement.dom.classList.remove( 'invalid' );
  547. previewLink.inputElement = link.inputElement;
  548. previewLink.outputElement = link.outputElement;
  549. if ( type === defaultOutput ) {
  550. previewLink.outputElement = selected;
  551. } else {
  552. previewLink.inputElement = selected;
  553. }
  554. const isInvalid = previewLink.inputElement !== null && previewLink.outputElement !== null &&
  555. previewLink.inputElement.inputLength > 0 && previewLink.outputElement.outputLength > 0 &&
  556. dispatchEventList( previewLink.inputElement.events.valid, previewLink.inputElement, previewLink.outputElement, data.dragging ? 'dragging' : 'dragged' ) === false;
  557. if ( data.dragging && isInvalid ) {
  558. if ( type === defaultOutput ) {
  559. if ( previewLink.outputElement )
  560. previewLink.outputElement.dom.classList.add( 'invalid' );
  561. } else {
  562. if ( previewLink.inputElement )
  563. previewLink.inputElement.dom.classList.add( 'invalid' );
  564. }
  565. return;
  566. }
  567. if ( ! data.dragging ) {
  568. nodeDOM.classList.remove( 'io-connect' );
  569. ioDOM.classList.remove( 'connect' );
  570. dom.classList.remove( 'select' );
  571. this.links.splice( this.links.indexOf( link ), 1 );
  572. if ( selected !== null && ! isInvalid ) {
  573. link.inputElement = previewLink.inputElement;
  574. link.outputElement = previewLink.outputElement;
  575. // check if is an is circular link
  576. if ( link.outputElement.node.isCircular( link.inputElement.node ) ) {
  577. return;
  578. }
  579. //
  580. if ( link.inputElement.inputLength > 0 && link.outputElement.outputLength > 0 ) {
  581. link.inputElement.connect( link.outputElement );
  582. }
  583. }
  584. }
  585. }, 'connecting' );
  586. };
  587. ioDOM.addEventListener( 'mousedown', onConnectEvent, true );
  588. ioDOM.addEventListener( 'touchstart', onConnectEvent, true );
  589. return ioDOM;
  590. }
  591. }
  592. class Input extends Serializer {
  593. constructor( dom ) {
  594. super();
  595. this.isInput = true;
  596. this.dom = dom;
  597. this.element = null;
  598. this.extra = null;
  599. this.tagColor = null;
  600. this.events = {
  601. 'change': [],
  602. 'click': []
  603. };
  604. this.addEventListener( 'change', ( ) => {
  605. dispatchEventList( this.events.change, this );
  606. } );
  607. this.addEventListener( 'click', ( ) => {
  608. dispatchEventList( this.events.click, this );
  609. } );
  610. }
  611. setExtra( value ) {
  612. this.extra = value;
  613. return this;
  614. }
  615. getExtra() {
  616. return this.extra;
  617. }
  618. setTagColor( color ) {
  619. this.tagColor = color;
  620. this.dom.style[ 'border-left' ] = `2px solid ${color}`;
  621. return this;
  622. }
  623. getTagColor() {
  624. return this.tagColor;
  625. }
  626. setToolTip( text ) {
  627. const div = document.createElement( 'f-tooltip' );
  628. div.innerText = text;
  629. this.dom.append( div );
  630. return this;
  631. }
  632. onChange( callback ) {
  633. this.events.change.push( callback );
  634. return this;
  635. }
  636. onClick( callback ) {
  637. this.events.click.push( callback );
  638. return this;
  639. }
  640. setReadOnly( value ) {
  641. this.dom.readOnly = value;
  642. return this;
  643. }
  644. getReadOnly() {
  645. return this.dom.readOnly;
  646. }
  647. setValue( value, dispatch = true ) {
  648. this.dom.value = value;
  649. if ( dispatch ) this.dispatchEvent( new Event( 'change' ) );
  650. return this;
  651. }
  652. getValue() {
  653. return this.dom.value;
  654. }
  655. serialize( data ) {
  656. data.value = this.getValue();
  657. }
  658. deserialize( data ) {
  659. this.setValue( data.value );
  660. }
  661. }
  662. class Node extends Serializer {
  663. constructor() {
  664. super();
  665. this.isNode = true;
  666. const dom = document.createElement( 'f-node' );
  667. const onDown = () => {
  668. const canvas = this.canvas;
  669. if ( canvas !== null ) {
  670. canvas.select( this );
  671. }
  672. };
  673. dom.addEventListener( 'mousedown', onDown, true );
  674. dom.addEventListener( 'touchstart', onDown, true );
  675. this._onConnect = ( e ) => {
  676. const { target } = e;
  677. for ( const element of this.elements ) {
  678. if ( element !== target ) {
  679. element.dispatchEvent( new Event( 'nodeConnect' ) );
  680. }
  681. }
  682. };
  683. this._onConnectChildren = ( e ) => {
  684. const { target } = e;
  685. for ( const element of this.elements ) {
  686. if ( element !== target ) {
  687. element.dispatchEvent( new Event( 'nodeConnectChildren' ) );
  688. }
  689. }
  690. };
  691. this.dom = dom;
  692. this.style = '';
  693. this.canvas = null;
  694. this.elements = [];
  695. this.events = {
  696. 'focus': [],
  697. 'blur': []
  698. };
  699. this.setWidth( 300 ).setPosition( 0, 0 );
  700. }
  701. onFocus( callback ) {
  702. this.events.focus.push( callback );
  703. return this;
  704. }
  705. onBlur( callback ) {
  706. this.events.blur.push( callback );
  707. return this;
  708. }
  709. setStyle( style ) {
  710. const dom = this.dom;
  711. if ( this.style ) dom.classList.remove( this.style );
  712. if ( style ) dom.classList.add( style );
  713. this.style = style;
  714. return this;
  715. }
  716. setPosition( x, y ) {
  717. const dom = this.dom;
  718. dom.style.left = toPX( x );
  719. dom.style.top = toPX( y );
  720. return this;
  721. }
  722. getPosition() {
  723. const dom = this.dom;
  724. return {
  725. x: parseInt( dom.style.left ),
  726. y: parseInt( dom.style.top )
  727. };
  728. }
  729. setWidth( val ) {
  730. this.dom.style.width = toPX( val );
  731. return this;
  732. }
  733. getWidth() {
  734. return parseInt( this.dom.style.width );
  735. }
  736. add( element ) {
  737. this.elements.push( element );
  738. element.node = this;
  739. element.addEventListener( 'connect', this._onConnect );
  740. element.addEventListener( 'connectChildren', this._onConnectChildren );
  741. this.dom.append( element.dom );
  742. return this;
  743. }
  744. remove( element ) {
  745. this.elements.splice( this.elements.indexOf( element ), 1 );
  746. element.node = null;
  747. element.removeEventListener( 'connect', this._onConnect );
  748. element.removeEventListener( 'connectChildren', this._onConnectChildren );
  749. this.dom.removeChild( element.dom );
  750. return this;
  751. }
  752. dispose() {
  753. const canvas = this.canvas;
  754. if ( canvas !== null ) canvas.remove( this );
  755. for ( const element of this.elements ) {
  756. element.dispose();
  757. }
  758. this.dispatchEvent( new Event( 'dispose' ) );
  759. }
  760. isCircular( node ) {
  761. if ( node === this ) return true;
  762. const links = this.getLinks();
  763. for ( const link of links ) {
  764. if ( link.outputElement.node.isCircular( node ) ) {
  765. return true;
  766. }
  767. }
  768. return false;
  769. }
  770. getLinks() {
  771. const links = [];
  772. for ( const element of this.elements ) {
  773. links.push( ...element.links );
  774. }
  775. return links;
  776. }
  777. serialize( data ) {
  778. const { x, y } = this.getPosition();
  779. const elements = [];
  780. for ( const element of this.elements ) {
  781. elements.push( element.toJSON( data ).id );
  782. }
  783. data.x = x;
  784. data.y = y;
  785. data.width = this.getWidth();
  786. data.elements = elements;
  787. if ( this.style !== '' ) {
  788. data.style = this.style;
  789. }
  790. }
  791. deserialize( data ) {
  792. this.setPosition( data.x, data.y );
  793. this.setWidth( data.width );
  794. if ( data.style !== undefined ) {
  795. this.setStyle( data.style );
  796. }
  797. const elements = this.elements;
  798. if ( elements.length > 0 ) {
  799. let index = 0;
  800. for ( const id of data.elements ) {
  801. data.objects[ id ] = elements[ index ++ ];
  802. }
  803. } else {
  804. for ( const id of data.elements ) {
  805. this.add( data.objects[ id ] );
  806. }
  807. }
  808. }
  809. }
  810. class DraggableElement extends Element {
  811. constructor( draggable = true ) {
  812. super( true );
  813. this.draggable = draggable;
  814. const onDrag = ( e ) => {
  815. e.preventDefault();
  816. if ( this.draggable === true ) {
  817. draggableDOM( this.node.dom );
  818. }
  819. };
  820. const { dom } = this;
  821. dom.addEventListener( 'mousedown', onDrag, true );
  822. dom.addEventListener( 'touchstart', onDrag, true );
  823. }
  824. }
  825. class TitleElement extends DraggableElement {
  826. constructor( title, draggable = true ) {
  827. super( draggable );
  828. const { dom } = this;
  829. dom.className = 'title';
  830. const spanDOM = document.createElement( 'span' );
  831. spanDOM.innerText = title;
  832. const iconDOM = document.createElement( 'i' );
  833. const toolbarDOM = document.createElement( 'f-toolbar' );
  834. this.buttons = [];
  835. this.spanDOM = spanDOM;
  836. this.iconDOM = iconDOM;
  837. this.toolbarDOM = toolbarDOM;
  838. dom.append( spanDOM );
  839. dom.append( iconDOM );
  840. dom.append( toolbarDOM );
  841. }
  842. setIcon( value ) {
  843. this.iconDOM.className = value;
  844. return this;
  845. }
  846. getIcon() {
  847. return this.iconDOM.className;
  848. }
  849. setTitle( value ) {
  850. this.spanDOM.innerText = value;
  851. return this;
  852. }
  853. getTitle() {
  854. return this.spanDOM.innerText;
  855. }
  856. addButton( button ) {
  857. this.buttons.push( button );
  858. this.toolbarDOM.append( button.dom );
  859. return this;
  860. }
  861. serialize( data ) {
  862. super.serialize( data );
  863. const title = this.getTitle();
  864. const icon = this.getIcon();
  865. data.title = title;
  866. if ( icon !== '' ) {
  867. data.icon = icon;
  868. }
  869. }
  870. deserialize( data ) {
  871. super.deserialize( data );
  872. this.setTitle( data.title );
  873. if ( data.icon !== undefined ) {
  874. this.setIcon( data.icon );
  875. }
  876. }
  877. }
  878. const drawLine = ( p1x, p1y, p2x, p2y, invert, size, colorA, ctx, colorB = null ) => {
  879. const dx = p2x - p1x;
  880. const dy = p2y - p1y;
  881. const offset = Math.sqrt( ( dx * dx ) + ( dy * dy ) ) * ( invert ? - .3 : .3 );
  882. ctx.beginPath();
  883. ctx.moveTo( p1x, p1y );
  884. ctx.bezierCurveTo(
  885. p1x + offset, p1y,
  886. p2x - offset, p2y,
  887. p2x, p2y
  888. );
  889. if ( colorB !== null && colorA !== colorB ) {
  890. const gradient = ctx.createLinearGradient( p1x, p1y, p2x, p2y );
  891. gradient.addColorStop( 0, colorA );
  892. gradient.addColorStop( 1, colorB );
  893. ctx.strokeStyle = gradient;
  894. } else {
  895. ctx.strokeStyle = colorA;
  896. }
  897. ctx.lineWidth = size;
  898. ctx.stroke();
  899. };
  900. const colors = [
  901. '#ff4444',
  902. '#44ff44',
  903. '#4444ff'
  904. ];
  905. const dropNode = new Node().add( new TitleElement( 'File' ) ).setWidth( 250 );
  906. class Canvas extends Serializer {
  907. constructor() {
  908. super();
  909. const dom = document.createElement( 'f-canvas' );
  910. const contentDOM = document.createElement( 'f-content' );
  911. const areaDOM = document.createElement( 'f-area' );
  912. const dropDOM = document.createElement( 'f-drop' );
  913. const canvas = document.createElement( 'canvas' );
  914. const frontCanvas = document.createElement( 'canvas' );
  915. const context = canvas.getContext( '2d' );
  916. const frontContext = frontCanvas.getContext( '2d' );
  917. this.dom = dom;
  918. this.contentDOM = contentDOM;
  919. this.areaDOM = areaDOM;
  920. this.dropDOM = dropDOM;
  921. this.canvas = canvas;
  922. this.frontCanvas = frontCanvas;
  923. this.context = context;
  924. this.frontContext = frontContext;
  925. this.width = 10000;
  926. this.height = 10000;
  927. this.clientX = 0;
  928. this.clientY = 0;
  929. this.relativeClientX = 0;
  930. this.relativeClientY = 0;
  931. this.zoom = 1;
  932. this.nodes = [];
  933. this.selected = null;
  934. this.updating = false;
  935. this.droppedItems = [];
  936. this.events = {
  937. 'drop': []
  938. };
  939. frontCanvas.className = 'front';
  940. contentDOM.style.left = toPX( this.centerX );
  941. contentDOM.style.top = toPX( this.centerY );
  942. areaDOM.style.width = `calc( 100% + ${ this.width }px )`;
  943. areaDOM.style.height = `calc( 100% + ${ this.height }px )`;
  944. dropDOM.innerHTML = '<span>drop your file</span>';
  945. dom.append( dropDOM );
  946. dom.append( canvas );
  947. dom.append( frontCanvas );
  948. dom.append( contentDOM );
  949. dom.append( areaDOM );
  950. /*
  951. let zoomTouchData = null;
  952. const onZoomStart = () => {
  953. zoomTouchData = null;
  954. };
  955. */
  956. const onZoom = ( e ) => {
  957. if ( e.touches ) {
  958. if ( e.touches.length === 2 ) {
  959. e.preventDefault();
  960. e.stopImmediatePropagation();
  961. /*
  962. const clientX = ( e.touches[ 0 ].clientX + e.touches[ 1 ].clientX ) / 2;
  963. const clientY = ( e.touches[ 0 ].clientY + e.touches[ 1 ].clientY ) / 2;
  964. const distance = Math.hypot(
  965. e.touches[ 0 ].clientX - e.touches[ 1 ].clientX,
  966. e.touches[ 0 ].clientY - e.touches[ 1 ].clientY
  967. );
  968. if ( zoomTouchData === null ) {
  969. zoomTouchData = {
  970. distance
  971. };
  972. }
  973. const delta = ( zoomTouchData.distance - distance );
  974. zoomTouchData.distance = distance;
  975. let zoom = Math.min( Math.max( this.zoom - delta * .01, .5 ), 1.2 );
  976. if ( zoom < .52 ) zoom = .5;
  977. else if ( zoom > .98 ) zoom = 1;
  978. contentDOM.style.left = toPX( this.centerX / zoom );
  979. contentDOM.style.top = toPX( this.centerY / zoom );
  980. contentDOM.style.zoom = this.zoom = zoom;
  981. */
  982. }
  983. } else {
  984. e.preventDefault();
  985. e.stopImmediatePropagation();
  986. /*
  987. const delta = e.deltaY / 100;
  988. const zoom = Math.min( Math.max( this.zoom - delta * .1, .5 ), 1 );
  989. contentDOM.style.left = toPX( this.centerX / zoom );
  990. contentDOM.style.top = toPX( this.centerY / zoom );
  991. contentDOM.style.zoom = this.zoom = zoom;
  992. */
  993. }
  994. };
  995. dom.addEventListener( 'wheel', onZoom );
  996. dom.addEventListener( 'touchmove', onZoom );
  997. //dom.addEventListener( 'touchstart', onZoomStart );
  998. let dropEnterCount = 0;
  999. const dragState = ( enter ) => {
  1000. if ( enter ) {
  1001. if ( dropEnterCount ++ === 0 ) {
  1002. this.droppedItems = [];
  1003. dropDOM.classList.add( 'visible' );
  1004. this.add( dropNode );
  1005. }
  1006. } else if ( -- dropEnterCount === 0 ) {
  1007. dropDOM.classList.remove( 'visible' );
  1008. this.remove( dropNode );
  1009. }
  1010. };
  1011. dom.addEventListener( 'dragenter', () => {
  1012. dragState( true );
  1013. } );
  1014. dom.addEventListener( 'dragleave', () => {
  1015. dragState( false );
  1016. } );
  1017. dom.addEventListener( 'dragover', ( e ) => {
  1018. e.preventDefault();
  1019. const { relativeClientX, relativeClientY } = this;
  1020. const centerNodeX = dropNode.getWidth() / 2;
  1021. dropNode.setPosition( relativeClientX - centerNodeX, relativeClientY - 20 );
  1022. } );
  1023. dom.addEventListener( 'drop', ( e ) => {
  1024. e.preventDefault();
  1025. dragState( false );
  1026. this.droppedItems = e.dataTransfer.items;
  1027. dispatchEventList( this.events.drop, this );
  1028. } );
  1029. draggableDOM( dom, ( data ) => {
  1030. const { delta, isTouch } = data;
  1031. if ( ! isTouch ) {
  1032. if ( data.scrollTop === undefined ) {
  1033. data.scrollLeft = dom.scrollLeft;
  1034. data.scrollTop = dom.scrollTop;
  1035. }
  1036. dom.scrollLeft = data.scrollLeft - delta.x;
  1037. dom.scrollTop = data.scrollTop - delta.y;
  1038. }
  1039. if ( data.dragging ) {
  1040. dom.classList.add( 'grabbing' );
  1041. } else {
  1042. dom.classList.remove( 'grabbing' );
  1043. }
  1044. }, 'dragging-canvas' );
  1045. this._onMoveEvent = ( e ) => {
  1046. const event = e.touches ? e.touches[ 0 ] : e;
  1047. const { zoom, rect } = this;
  1048. this.clientX = event.clientX;
  1049. this.clientY = event.clientY;
  1050. this.relativeClientX = ( ( ( dom.scrollLeft - this.centerX ) + event.clientX ) - rect.left ) / zoom;
  1051. this.relativeClientY = ( ( ( dom.scrollTop - this.centerY ) + event.clientY ) - rect.top ) / zoom;
  1052. };
  1053. this._onContentLoaded = () => {
  1054. this.centralize();
  1055. };
  1056. this._onUpdate = () => {
  1057. this.update();
  1058. };
  1059. this.start();
  1060. }
  1061. get rect() {
  1062. return this.dom.getBoundingClientRect();
  1063. }
  1064. get relativeX() {
  1065. return this.dom.scrollLeft - this.centerX;
  1066. }
  1067. get relativeY() {
  1068. return this.dom.scrollTop - this.centerY;
  1069. }
  1070. get centerX() {
  1071. return this.width / 2;
  1072. }
  1073. get centerY() {
  1074. return this.height / 2;
  1075. }
  1076. onDrop( callback ) {
  1077. this.events.drop.push( callback );
  1078. return this;
  1079. }
  1080. start() {
  1081. this.updating = true;
  1082. document.addEventListener( 'wheel', this._onMoveEvent, true );
  1083. document.addEventListener( 'mousedown', this._onMoveEvent, true );
  1084. document.addEventListener( 'touchstart', this._onMoveEvent, true );
  1085. document.addEventListener( 'mousemove', this._onMoveEvent, true );
  1086. document.addEventListener( 'touchmove', this._onMoveEvent, true );
  1087. document.addEventListener( 'dragover', this._onMoveEvent, true );
  1088. document.addEventListener( 'DOMContentLoaded', this._onContentLoaded );
  1089. requestAnimationFrame( this._onUpdate );
  1090. }
  1091. stop() {
  1092. this.updating = false;
  1093. document.removeEventListener( 'wheel', this._onMoveEvent, true );
  1094. document.removeEventListener( 'mousedown', this._onMoveEvent, true );
  1095. document.removeEventListener( 'touchstart', this._onMoveEvent, true );
  1096. document.removeEventListener( 'mousemove', this._onMoveEvent, true );
  1097. document.removeEventListener( 'touchmove', this._onMoveEvent, true );
  1098. document.removeEventListener( 'dragover', this._onMoveEvent, true );
  1099. document.removeEventListener( 'DOMContentLoaded', this._onContentLoaded );
  1100. }
  1101. add( node ) {
  1102. if ( node.canvas === this ) return;
  1103. this.nodes.push( node );
  1104. node.canvas = this;
  1105. this.contentDOM.append( node.dom );
  1106. return this;
  1107. }
  1108. remove( node ) {
  1109. if ( node === this.selected ) {
  1110. this.select();
  1111. }
  1112. this.unlink( node );
  1113. const nodes = this.nodes;
  1114. nodes.splice( nodes.indexOf( node ), 1 );
  1115. node.canvas = null;
  1116. this.contentDOM.removeChild( node.dom );
  1117. node.dispatchEvent( new Event( 'remove' ) );
  1118. return this;
  1119. }
  1120. clear() {
  1121. const nodes = this.nodes;
  1122. while ( nodes.length > 0 ) {
  1123. this.remove( nodes[ 0 ] );
  1124. }
  1125. return this;
  1126. }
  1127. unlink( node ) {
  1128. const links = this.getLinks();
  1129. for ( const link of links ) {
  1130. if ( link.inputElement && link.outputElement ) {
  1131. if ( link.inputElement.node === node ) {
  1132. link.inputElement.connect();
  1133. } else if ( link.outputElement.node === node ) {
  1134. link.inputElement.connect();
  1135. }
  1136. }
  1137. }
  1138. }
  1139. getLinks() {
  1140. const links = [];
  1141. for ( const node of this.nodes ) {
  1142. links.push( ...node.getLinks() );
  1143. }
  1144. return links;
  1145. }
  1146. centralize() {
  1147. this.dom.scroll( this.centerX, this.centerY );
  1148. return this;
  1149. }
  1150. select( node = null ) {
  1151. if ( node === this.selected ) return;
  1152. const previousNode = this.selected;
  1153. if ( previousNode !== null ) {
  1154. previousNode.dom.classList.remove( 'selected' );
  1155. this.selected = null;
  1156. dispatchEventList( previousNode.events.blur, previousNode );
  1157. }
  1158. if ( node !== null ) {
  1159. node.dom.classList.add( 'selected' );
  1160. this.selected = node;
  1161. dispatchEventList( node.events.focus, node );
  1162. }
  1163. }
  1164. update() {
  1165. if ( this.updating === false ) return;
  1166. requestAnimationFrame( this._onUpdate );
  1167. const { dom, zoom, canvas, frontCanvas, frontContext, context } = this;
  1168. const width = window.innerWidth;
  1169. const height = window.innerHeight;
  1170. const domRect = this.rect;
  1171. if ( canvas.width !== width || canvas.height !== height ) {
  1172. canvas.width = width;
  1173. canvas.height = height;
  1174. frontCanvas.width = width;
  1175. frontCanvas.height = height;
  1176. }
  1177. context.clearRect( 0, 0, width, height );
  1178. frontContext.clearRect( 0, 0, width, height );
  1179. context.globalCompositeOperation = 'lighter';
  1180. frontContext.globalCompositeOperation = 'source-over';
  1181. const links = this.getLinks();
  1182. const aPos = { x: 0, y: 0 };
  1183. const bPos = { x: 0, y: 0 };
  1184. const offsetIORadius = 10;
  1185. let dragging = '';
  1186. for ( const link of links ) {
  1187. const { lioElement, rioElement } = link;
  1188. let draggingLink = '';
  1189. let length = 0;
  1190. if ( lioElement !== null ) {
  1191. const rect = lioElement.dom.getBoundingClientRect();
  1192. length = Math.max( length, lioElement.rioLength );
  1193. aPos.x = rect.x + rect.width;
  1194. aPos.y = rect.y + ( rect.height / 2 );
  1195. } else {
  1196. aPos.x = this.clientX;
  1197. aPos.y = this.clientY;
  1198. draggingLink = 'lio';
  1199. }
  1200. if ( rioElement !== null ) {
  1201. const rect = rioElement.dom.getBoundingClientRect();
  1202. length = Math.max( length, rioElement.lioLength );
  1203. bPos.x = rect.x;
  1204. bPos.y = rect.y + ( rect.height / 2 );
  1205. } else {
  1206. bPos.x = this.clientX;
  1207. bPos.y = this.clientY;
  1208. draggingLink = 'rio';
  1209. }
  1210. dragging = dragging || draggingLink;
  1211. const drawContext = draggingLink ? frontContext : context;
  1212. if ( draggingLink || length === 1 ) {
  1213. let colorA = null,
  1214. colorB = null;
  1215. if ( draggingLink === 'rio' ) {
  1216. colorA = colorB = lioElement.getRIOColor();
  1217. aPos.x += offsetIORadius;
  1218. bPos.x /= zoom;
  1219. bPos.y /= zoom;
  1220. } else if ( draggingLink === 'lio' ) {
  1221. colorA = colorB = rioElement.getLIOColor();
  1222. bPos.x -= offsetIORadius;
  1223. aPos.x /= zoom;
  1224. aPos.y /= zoom;
  1225. } else {
  1226. colorA = lioElement.getRIOColor();
  1227. colorB = rioElement.getLIOColor();
  1228. }
  1229. drawLine(
  1230. aPos.x * zoom, aPos.y * zoom,
  1231. bPos.x * zoom, bPos.y * zoom,
  1232. false, 2, colorA || '#ffffff', drawContext, colorB || '#ffffff'
  1233. );
  1234. } else {
  1235. length = Math.min( length, 4 );
  1236. for ( let i = 0; i < length; i ++ ) {
  1237. const color = colors[ i ] || '#ffffff';
  1238. const marginY = 4;
  1239. const rioLength = Math.min( lioElement.rioLength, length );
  1240. const lioLength = Math.min( rioElement.lioLength, length );
  1241. const colorA = lioElement.getRIOColor() || color;
  1242. const colorB = rioElement.getLIOColor() || color;
  1243. const aCenterY = ( ( rioLength * marginY ) * .5 ) - ( marginY / 2 );
  1244. const bCenterY = ( ( lioLength * marginY ) * .5 ) - ( marginY / 2 );
  1245. const aIndex = Math.min( i, rioLength - 1 );
  1246. const bIndex = Math.min( i, lioLength - 1 );
  1247. const aPosY = ( aIndex * marginY ) - 1;
  1248. const bPosY = ( bIndex * marginY ) - 1;
  1249. drawLine(
  1250. aPos.x * zoom, ( ( aPos.y + aPosY ) - aCenterY ) * zoom,
  1251. bPos.x * zoom, ( ( bPos.y + bPosY ) - bCenterY ) * zoom,
  1252. false, 2, colorA, drawContext, colorB
  1253. );
  1254. }
  1255. }
  1256. }
  1257. context.globalCompositeOperation = 'destination-in';
  1258. context.fillRect( domRect.x, domRect.y, domRect.width, domRect.height );
  1259. if ( dragging !== '' ) {
  1260. dom.classList.add( 'dragging-' + dragging );
  1261. } else {
  1262. dom.classList.remove( 'dragging-lio' );
  1263. dom.classList.remove( 'dragging-rio' );
  1264. }
  1265. }
  1266. serialize( data ) {
  1267. const nodes = [];
  1268. for ( const node of this.nodes ) {
  1269. nodes.push( node.toJSON( data ).id );
  1270. }
  1271. data.nodes = nodes;
  1272. }
  1273. deserialize( data ) {
  1274. for ( const id of data.nodes ) {
  1275. this.add( data.objects[ id ] );
  1276. }
  1277. }
  1278. }
  1279. class ButtonInput extends Input {
  1280. constructor( innterText = '' ) {
  1281. const dom = document.createElement( 'button' );
  1282. const spanDOM = document.createElement( 'span' );
  1283. dom.append( spanDOM );
  1284. const iconDOM = document.createElement( 'i' );
  1285. dom.append( iconDOM );
  1286. super( dom );
  1287. this.spanDOM = spanDOM;
  1288. this.iconDOM = iconDOM;
  1289. spanDOM.innerText = innterText;
  1290. dom.onmouseover = () => {
  1291. this.dispatchEvent( new Event( 'mouseover' ) );
  1292. };
  1293. dom.onclick = dom.ontouchstart = ( e ) => {
  1294. e.preventDefault();
  1295. e.stopPropagation();
  1296. this.dispatchEvent( new Event( 'click' ) );
  1297. };
  1298. }
  1299. setIcon( className ) {
  1300. this.iconDOM.className = className;
  1301. return this;
  1302. }
  1303. setValue( val ) {
  1304. this.spanDOM.innerText = val;
  1305. return this;
  1306. }
  1307. getValue() {
  1308. return this.spanDOM.innerText;
  1309. }
  1310. }
  1311. class ObjectNode extends Node {
  1312. constructor( name, inputLength, callback = null, width = 300 ) {
  1313. super();
  1314. this.setWidth( width );
  1315. const title = new TitleElement( name )
  1316. .setObjectCallback( callback )
  1317. .setSerializable( false )
  1318. .setOutput( inputLength );
  1319. const closeButton = new ButtonInput( Styles.icons.close || '✕' ).onClick( () => {
  1320. this.dispose();
  1321. } ).setIcon( Styles.icons.close );
  1322. title.addButton( closeButton );
  1323. this.add( title );
  1324. this.title = title;
  1325. this.closeButton = closeButton;
  1326. }
  1327. setName( value ) {
  1328. this.title.setTitle( value );
  1329. return this;
  1330. }
  1331. getName() {
  1332. return this.title.getTitle();
  1333. }
  1334. setObjectCallback( callback ) {
  1335. this.title.setObjectCallback( callback );
  1336. return this;
  1337. }
  1338. getObject( callback ) {
  1339. return this.title.getObject( callback );
  1340. }
  1341. setColor( color ) {
  1342. return this.title.setColor( color );
  1343. }
  1344. setOutputColor( color ) {
  1345. return this.title.setOutputColor( color );
  1346. }
  1347. invalidate() {
  1348. this.title.dispatchEvent( new Event( 'connect' ) );
  1349. }
  1350. }
  1351. const ENTER_KEY$1 = 13;
  1352. class StringInput extends Input {
  1353. constructor( value = '' ) {
  1354. const dom = document.createElement( 'input' );
  1355. super( dom );
  1356. dom.type = 'text';
  1357. dom.value = value;
  1358. dom.spellcheck = false;
  1359. dom.autocomplete = 'off';
  1360. dom.onblur = () => {
  1361. this.dispatchEvent( new Event( 'blur' ) );
  1362. };
  1363. dom.onchange = () => {
  1364. this.dispatchEvent( new Event( 'change' ) );
  1365. };
  1366. dom.onkeyup = ( e ) => {
  1367. if ( e.keyCode === ENTER_KEY$1 ) {
  1368. e.target.blur();
  1369. }
  1370. e.stopPropagation();
  1371. this.dispatchEvent( new Event( 'change' ) );
  1372. };
  1373. }
  1374. }
  1375. const ENTER_KEY = 13;
  1376. class NumberInput extends Input {
  1377. constructor( value = 0, min = - Infinity, max = Infinity, step = .01 ) {
  1378. const dom = document.createElement( 'input' );
  1379. super( dom );
  1380. this.min = min;
  1381. this.max = max;
  1382. this.step = step;
  1383. this.integer = false;
  1384. dom.type = 'text';
  1385. dom.className = 'number';
  1386. dom.value = this._getString( value );
  1387. dom.spellcheck = false;
  1388. dom.autocomplete = 'off';
  1389. dom.ondragstart = dom.oncontextmenu = ( e ) => {
  1390. e.preventDefault();
  1391. e.stopPropagation();
  1392. };
  1393. dom.onfocus = dom.onclick = () => {
  1394. dom.select();
  1395. };
  1396. dom.onblur = () => {
  1397. this.dispatchEvent( new Event( 'blur' ) );
  1398. };
  1399. dom.onchange = () => {
  1400. this.dispatchEvent( new Event( 'change' ) );
  1401. };
  1402. dom.onkeydown = ( e ) => {
  1403. if ( e.key.length === 1 && /\d|\./.test( e.key ) !== true ) {
  1404. return false;
  1405. }
  1406. if ( e.keyCode === ENTER_KEY ) {
  1407. e.target.blur();
  1408. }
  1409. e.stopPropagation();
  1410. };
  1411. draggableDOM( dom, ( data ) => {
  1412. const { delta } = data;
  1413. if ( data.value === undefined ) {
  1414. data.value = this.getValue();
  1415. }
  1416. const diff = delta.x - delta.y;
  1417. const value = data.value + ( diff * this.step );
  1418. this.dom.value = this._getString( value.toFixed( this.precision ) );
  1419. this.dispatchEvent( new Event( 'change' ) );
  1420. } );
  1421. }
  1422. setStep( step ) {
  1423. this.step = step;
  1424. return this;
  1425. }
  1426. setRange( min, max, step ) {
  1427. this.min = min;
  1428. this.max = max;
  1429. this.step = step;
  1430. this.dispatchEvent( new Event( 'range' ) );
  1431. return this.setValue( this.getValue() );
  1432. }
  1433. get precision() {
  1434. if ( this.integer === true ) return 0;
  1435. const fract = this.step % 1;
  1436. return fract !== 0 ? fract.toString().split( '.' )[ 1 ].length : 1;
  1437. }
  1438. setValue( val, dispatch = true ) {
  1439. return super.setValue( this._getString( val ), dispatch );
  1440. }
  1441. getValue() {
  1442. return Number( this.dom.value );
  1443. }
  1444. serialize( data ) {
  1445. const { min, max } = this;
  1446. if ( min !== - Infinity && max !== Infinity ) {
  1447. data.min = this.min;
  1448. data.max = this.max;
  1449. data.step = this.step;
  1450. }
  1451. super.serialize( data );
  1452. }
  1453. deserialize( data ) {
  1454. if ( data.min !== undefined ) {
  1455. const { min, max, step } = this;
  1456. this.setRange( min, max, step );
  1457. }
  1458. super.deserialize( data );
  1459. }
  1460. _getString( value ) {
  1461. const num = Math.min( Math.max( Number( value ), this.min ), this.max );
  1462. if ( this.integer === true ) {
  1463. return Math.floor( num );
  1464. } else {
  1465. return num + ( num % 1 ? '' : '.0' );
  1466. }
  1467. }
  1468. }
  1469. const getStep = ( min, max ) => {
  1470. const sensibility = .001;
  1471. return ( max - min ) * sensibility;
  1472. };
  1473. class SliderInput extends Input {
  1474. constructor( value = 0, min = 0, max = 100 ) {
  1475. const dom = document.createElement( 'f-subinputs' );
  1476. super( dom );
  1477. value = Math.min( Math.max( value, min ), max );
  1478. const step = getStep( min, max );
  1479. const rangeDOM = document.createElement( 'input' );
  1480. rangeDOM.type = 'range';
  1481. rangeDOM.min = min;
  1482. rangeDOM.max = max;
  1483. rangeDOM.step = step;
  1484. rangeDOM.value = value;
  1485. const field = new NumberInput( value, min, max, step );
  1486. field.dom.className = 'range-value';
  1487. field.onChange( () => {
  1488. rangeDOM.value = field.getValue();
  1489. } );
  1490. field.addEventListener( 'range', () => {
  1491. rangeDOM.min = field.min;
  1492. rangeDOM.max = field.max;
  1493. rangeDOM.step = field.step;
  1494. rangeDOM.value = field.getValue();
  1495. } );
  1496. dom.append( rangeDOM );
  1497. dom.append( field.dom );
  1498. this.rangeDOM = rangeDOM;
  1499. this.field = field;
  1500. const updateRangeValue = () => {
  1501. let value = Number( rangeDOM.value );
  1502. if ( value !== this.max && value + this.step >= this.max ) {
  1503. // fix not end range fraction
  1504. rangeDOM.value = value = this.max;
  1505. }
  1506. this.field.setValue( value );
  1507. };
  1508. draggableDOM( rangeDOM, () => {
  1509. updateRangeValue();
  1510. this.dispatchEvent( new Event( 'change' ) );
  1511. }, '' );
  1512. }
  1513. get min() {
  1514. return this.field.min;
  1515. }
  1516. get max() {
  1517. return this.field.max;
  1518. }
  1519. get step() {
  1520. return this.field.step;
  1521. }
  1522. setRange( min, max ) {
  1523. this.field.setRange( min, max, getStep( min, max ) );
  1524. this.dispatchEvent( new Event( 'range' ) );
  1525. this.dispatchEvent( new Event( 'change' ) );
  1526. return this;
  1527. }
  1528. setValue( val, dispatch = true ) {
  1529. this.field.setValue( val );
  1530. this.rangeDOM.value = val;
  1531. if ( dispatch ) this.dispatchEvent( new Event( 'change' ) );
  1532. return this;
  1533. }
  1534. getValue() {
  1535. return this.field.getValue();
  1536. }
  1537. serialize( data ) {
  1538. data.min = this.min;
  1539. data.max = this.max;
  1540. super.serialize( data );
  1541. }
  1542. deserialize( data ) {
  1543. const { min, max } = data;
  1544. this.setRange( min, max );
  1545. super.deserialize( data );
  1546. }
  1547. }
  1548. class ColorInput extends Input {
  1549. constructor( value = 0x0099ff ) {
  1550. const dom = document.createElement( 'input' );
  1551. super( dom );
  1552. dom.type = 'color';
  1553. dom.value = toHex( value );
  1554. dom.oninput = () => {
  1555. this.dispatchEvent( new Event( 'change' ) );
  1556. };
  1557. }
  1558. setValue( value, dispatch = true ) {
  1559. return super.setValue( toHex( value ), dispatch );
  1560. }
  1561. getValue() {
  1562. return parseInt( super.getValue().slice( 1 ), 16 );
  1563. }
  1564. }
  1565. class TextInput extends Input {
  1566. constructor( innerText = '' ) {
  1567. const dom = document.createElement( 'textarea' );
  1568. super( dom );
  1569. dom.innerText = innerText;
  1570. }
  1571. setValue( val ) {
  1572. this.dom.innerText = val;
  1573. return this;
  1574. }
  1575. getValue() {
  1576. return this.dom.innerText;
  1577. }
  1578. }
  1579. class LabelElement extends Element {
  1580. constructor( label = '', align = '' ) {
  1581. super();
  1582. this.labelDOM = document.createElement( 'f-label' );
  1583. this.inputsDOM = document.createElement( 'f-inputs' );
  1584. const spanDOM = document.createElement( 'span' );
  1585. const iconDOM = document.createElement( 'i' );
  1586. this.spanDOM = spanDOM;
  1587. this.iconDOM = iconDOM;
  1588. this.labelDOM.append( this.spanDOM );
  1589. this.labelDOM.append( this.iconDOM );
  1590. this.dom.append( this.labelDOM );
  1591. this.dom.append( this.inputsDOM );
  1592. this.serializeLabel = false;
  1593. this.setLabel( label );
  1594. this.setAlign( align );
  1595. }
  1596. setIcon( value ) {
  1597. this.iconDOM.className = value;
  1598. return this;
  1599. }
  1600. getIcon() {
  1601. return this.iconDOM.className;
  1602. }
  1603. setAlign( align ) {
  1604. this.labelDOM.className = align;
  1605. }
  1606. setLabel( val ) {
  1607. this.spanDOM.innerText = val;
  1608. }
  1609. getLabel() {
  1610. return this.spanDOM.innerText;
  1611. }
  1612. serialize( data ) {
  1613. super.serialize( data );
  1614. if ( this.serializeLabel ) {
  1615. const label = this.getLabel();
  1616. const icon = this.getIcon();
  1617. data.label = label;
  1618. if ( icon !== '' ) {
  1619. data.icon = icon;
  1620. }
  1621. }
  1622. }
  1623. deserialize( data ) {
  1624. super.deserialize( data );
  1625. if ( this.serializeLabel ) {
  1626. this.setLabel( data.label );
  1627. if ( data.icon !== undefined ) {
  1628. this.setIcon( data.icon );
  1629. }
  1630. }
  1631. }
  1632. }
  1633. class PanelNode extends Node {
  1634. constructor( title = 'Panel', align = 'top-right' ) {
  1635. super();
  1636. const titleElement = new TitleElement( title );
  1637. this.add( titleElement );
  1638. const collapseButton = new ButtonInput( '🗕' );
  1639. collapseButton.onClick( () => {
  1640. this.setCollapse( ! this.collapsed );
  1641. } );
  1642. titleElement.addButton( collapseButton );
  1643. this.collapseButton = collapseButton;
  1644. this.titleElement = titleElement;
  1645. this.align = align;
  1646. this.collapsed = false;
  1647. this.setAlign( align );
  1648. this.setStyle( 'rouded' );
  1649. }
  1650. setCollapse( value ) {
  1651. const cssClass = 'closed';
  1652. this.dom.classList.remove( cssClass );
  1653. this.collapsed = value;
  1654. this.collapseButton.value = value ? '🗖' : '🗕';
  1655. if ( value === true ) {
  1656. this.dom.classList.add( cssClass );
  1657. }
  1658. return this;
  1659. }
  1660. setAlign( align ) {
  1661. if ( this.align ) this.dom.classList.remove( this.align );
  1662. this.dom.classList.add( align );
  1663. this.align = align;
  1664. return this;
  1665. }
  1666. addInput( inputClass, object, property, ...params ) {
  1667. const value = object[ property ];
  1668. const input = new inputClass( value, ...params );
  1669. input.onChange( () => {
  1670. object[ property ] = input.value;
  1671. } );
  1672. this.add( new LabelElement( property ).add( input ) );
  1673. return input;
  1674. }
  1675. addSlider( object, property, min, max ) {
  1676. return this.addInput( SliderInput, object, property, min, max );
  1677. }
  1678. addNumber( object, property ) {
  1679. return this.addInput( NumberInput, object, property );
  1680. }
  1681. addColor( object, property ) {
  1682. return this.addInput( ColorInput, object, property );
  1683. }
  1684. addString( object, property ) {
  1685. return this.addInput( StringInput, object, property );
  1686. }
  1687. addText( object, property ) {
  1688. const input = this.addInput( TextInput, object, property );
  1689. input.element.setHeight( 70 );
  1690. return input;
  1691. }
  1692. addButton( name ) {
  1693. const input = new ButtonInput( name );
  1694. this.add( new Element().setHeight( 34 ).add( input ) );
  1695. return input;
  1696. }
  1697. }
  1698. class Menu extends EventTarget {
  1699. constructor( className ) {
  1700. super();
  1701. const dom = document.createElement( 'f-menu' );
  1702. dom.className = className + ' hidden';
  1703. const listDOM = document.createElement( 'f-list' );
  1704. dom.append( listDOM );
  1705. this.dom = dom;
  1706. this.listDOM = listDOM;
  1707. this.visible = false;
  1708. this.subMenus = new WeakMap();
  1709. this.domButtons = new WeakMap();
  1710. this.buttons = [];
  1711. this.events = {};
  1712. }
  1713. onContext( callback ) {
  1714. this.events.context.push( callback );
  1715. return this;
  1716. }
  1717. show() {
  1718. this.dom.classList.remove( 'hidden' );
  1719. this.visible = true;
  1720. this.dispatchEvent( new Event( 'show' ) );
  1721. return this;
  1722. }
  1723. hide() {
  1724. this.dom.classList.add( 'hidden' );
  1725. this.dispatchEvent( new Event( 'hide' ) );
  1726. this.visible = false;
  1727. }
  1728. add( button, submenu = null ) {
  1729. const liDOM = document.createElement( 'f-item' );
  1730. if ( submenu !== null ) {
  1731. liDOM.classList.add( 'submenu' );
  1732. liDOM.append( submenu.dom );
  1733. this.subMenus.set( button, submenu );
  1734. button.dom.addEventListener( 'mouseover', () => submenu.show() );
  1735. button.dom.addEventListener( 'mouseout', () => submenu.hide() );
  1736. }
  1737. liDOM.append( button.dom );
  1738. this.buttons.push( button );
  1739. this.listDOM.append( liDOM );
  1740. this.domButtons.set( button, liDOM );
  1741. return this;
  1742. }
  1743. clear() {
  1744. this.buttons = [];
  1745. this.subMenus = new WeakMap();
  1746. this.domButtons = new WeakMap();
  1747. while ( this.listDOM.firstChild ) {
  1748. this.listDOM.firstChild.remove();
  1749. }
  1750. }
  1751. }
  1752. let lastContext = null;
  1753. const onCloseLastContext = ( e ) => {
  1754. if ( lastContext && lastContext.visible === true && e.target.closest( 'f-menu.context' ) === null ) {
  1755. lastContext.hide();
  1756. }
  1757. };
  1758. document.body.addEventListener( 'mousedown', onCloseLastContext, true );
  1759. document.body.addEventListener( 'touchstart', onCloseLastContext, true );
  1760. class ContextMenu extends Menu {
  1761. constructor( target = null ) {
  1762. super( 'context', target );
  1763. this.events.context = [];
  1764. this._lastButtonClick = null;
  1765. this._onButtonClick = ( e = null ) => {
  1766. const button = e ? e.target : null;
  1767. if ( this._lastButtonClick ) {
  1768. this._lastButtonClick.dom.parentElement.classList.remove( 'active' );
  1769. }
  1770. this._lastButtonClick = button;
  1771. if ( button ) {
  1772. if ( this.subMenus.has( button ) ) {
  1773. this.subMenus.get( button )._onButtonClick();
  1774. }
  1775. button.dom.parentElement.classList.add( 'active' );
  1776. }
  1777. };
  1778. this._onButtonMouseOver = ( e ) => {
  1779. const button = e.target;
  1780. if ( this.subMenus.has( button ) && this._lastButtonClick !== button ) {
  1781. this._onButtonClick();
  1782. }
  1783. };
  1784. this.addEventListener( 'context', ( ) => {
  1785. dispatchEventList( this.events.context, this );
  1786. } );
  1787. this.setTarget( target );
  1788. }
  1789. openFrom( dom ) {
  1790. const rect = dom.getBoundingClientRect();
  1791. return this.open( rect.x + ( rect.width / 2 ), rect.y + ( rect.height / 2 ) );
  1792. }
  1793. open( x = pointer.x, y = pointer.y ) {
  1794. if ( lastContext !== null ) {
  1795. lastContext.hide();
  1796. }
  1797. lastContext = this;
  1798. this.setPosition( x, y );
  1799. document.body.append( this.dom );
  1800. return this.show();
  1801. }
  1802. setPosition( x, y ) {
  1803. const dom = this.dom;
  1804. dom.style.left = toPX( x );
  1805. dom.style.top = toPX( y );
  1806. return this;
  1807. }
  1808. setTarget( target = null ) {
  1809. if ( target !== null ) {
  1810. const onContextMenu = ( e ) => {
  1811. e.preventDefault();
  1812. if ( e.pointerType !== 'mouse' || ( e.pageX === 0 && e.pageY === 0 ) ) return;
  1813. this.dispatchEvent( new Event( 'context' ) );
  1814. this.open();
  1815. };
  1816. this.target = target;
  1817. target.addEventListener( 'contextmenu', onContextMenu, false );
  1818. }
  1819. return this;
  1820. }
  1821. show() {
  1822. if ( ! this.opened ) {
  1823. this.dom.style.left = '';
  1824. this.dom.style.transform = '';
  1825. }
  1826. const domRect = this.dom.getBoundingClientRect();
  1827. let offsetX = Math.min( window.innerWidth - ( domRect.x + domRect.width + 10 ), 0 );
  1828. let offsetY = Math.min( window.innerHeight - ( domRect.y + domRect.height + 10 ), 0 );
  1829. if ( this.opened ) {
  1830. if ( offsetX < 0 ) offsetX = - domRect.width;
  1831. if ( offsetY < 0 ) offsetY = - domRect.height;
  1832. this.setPosition( domRect.x + offsetX, domRect.y + offsetY );
  1833. } else {
  1834. // flip submenus
  1835. if ( offsetX < 0 ) this.dom.style.left = '-100%';
  1836. if ( offsetY < 0 ) this.dom.style.transform = 'translateY( calc( 32px - 100% ) )';
  1837. }
  1838. return super.show();
  1839. }
  1840. hide() {
  1841. if ( this.opened ) {
  1842. lastContext = null;
  1843. }
  1844. return super.hide();
  1845. }
  1846. add( button, submenu = null ) {
  1847. button.addEventListener( 'click', this._onButtonClick );
  1848. button.addEventListener( 'mouseover', this._onButtonMouseOver );
  1849. return super.add( button, submenu );
  1850. }
  1851. get opened() {
  1852. return lastContext === this;
  1853. }
  1854. }
  1855. class CircleMenu extends Menu {
  1856. constructor( target = null ) {
  1857. super( 'circle', target );
  1858. }
  1859. }
  1860. class Tips extends EventTarget {
  1861. constructor() {
  1862. super();
  1863. const dom = document.createElement( 'f-tips' );
  1864. this.dom = dom;
  1865. this.time = 0;
  1866. this.duration = 3000;
  1867. }
  1868. message( str ) {
  1869. return this.tip( str );
  1870. }
  1871. error( str ) {
  1872. return this.tip( str, 'error' );
  1873. }
  1874. tip( html, className = '' ) {
  1875. const dom = document.createElement( 'f-tip' );
  1876. dom.className = className;
  1877. dom.innerHTML = html;
  1878. this.dom.prepend( dom );
  1879. //requestAnimationFrame( () => dom.style.opacity = 1 );
  1880. this.time = Math.min( this.time + this.duration, this.duration );
  1881. setTimeout( () => {
  1882. this.time -= this.duration;
  1883. dom.style.opacity = 0;
  1884. setTimeout( () => dom.remove(), 250 );
  1885. }, this.time );
  1886. return this;
  1887. }
  1888. }
  1889. const filterString = ( str ) => {
  1890. return str.trim().toLowerCase().replace( /\s\s+/g, ' ' );
  1891. };
  1892. class Search extends Menu {
  1893. constructor() {
  1894. super( 'search' );
  1895. this.events.submit = [];
  1896. this.events.filter = [];
  1897. const inputDOM = document.createElement( 'input' );
  1898. inputDOM.placeholder = 'Type here';
  1899. let filter = true;
  1900. let filterNeedUpdate = true;
  1901. inputDOM.addEventListener( 'focusout', () => {
  1902. filterNeedUpdate = true;
  1903. this.setValue( '' );
  1904. } );
  1905. inputDOM.onkeydown = ( e ) => {
  1906. const keyCode = e.keyCode;
  1907. if ( keyCode === 38 ) {
  1908. const index = this.filteredIndex;
  1909. if ( this.forceAutoComplete ) {
  1910. this.filteredIndex = index !== null ? ( index + 1 ) % ( this.filtered.length || 1 ) : 0;
  1911. } else {
  1912. this.filteredIndex = index !== null ? Math.min( index + 1, this.filtered.length - 1 ) : 0;
  1913. }
  1914. e.preventDefault();
  1915. filter = false;
  1916. } else if ( keyCode === 40 ) {
  1917. const index = this.filteredIndex;
  1918. if ( this.forceAutoComplete ) {
  1919. this.filteredIndex = index - 1;
  1920. if ( this.filteredIndex === null ) this.filteredIndex = this.filtered.length - 1;
  1921. } else {
  1922. this.filteredIndex = index !== null ? index - 1 : null;
  1923. }
  1924. e.preventDefault();
  1925. filter = false;
  1926. } else if ( keyCode === 13 ) {
  1927. this.value = this.currentFiltered ? this.currentFiltered.button.getValue() : inputDOM.value;
  1928. this.submit();
  1929. e.preventDefault();
  1930. filter = false;
  1931. } else {
  1932. filter = true;
  1933. }
  1934. };
  1935. inputDOM.onkeyup = () => {
  1936. if ( filter ) {
  1937. if ( filterNeedUpdate ) {
  1938. this.dispatchEvent( new Event( 'filter' ) );
  1939. filterNeedUpdate = false;
  1940. }
  1941. this.filter( inputDOM.value );
  1942. }
  1943. };
  1944. this.filtered = [];
  1945. this.currentFiltered = null;
  1946. this.value = '';
  1947. this.forceAutoComplete = false;
  1948. this.dom.append( inputDOM );
  1949. this.inputDOM = inputDOM;
  1950. this.addEventListener( 'filter', ( ) => {
  1951. dispatchEventList( this.events.filter, this );
  1952. } );
  1953. this.addEventListener( 'submit', ( ) => {
  1954. dispatchEventList( this.events.submit, this );
  1955. } );
  1956. }
  1957. submit() {
  1958. this.dispatchEvent( new Event( 'submit' ) );
  1959. return this.setValue( '' );
  1960. }
  1961. setValue( value ) {
  1962. this.inputDOM.value = value;
  1963. this.filter( value );
  1964. return this;
  1965. }
  1966. getValue() {
  1967. return this.value;
  1968. }
  1969. onFilter( callback ) {
  1970. this.events.filter.push( callback );
  1971. return this;
  1972. }
  1973. onSubmit( callback ) {
  1974. this.events.submit.push( callback );
  1975. return this;
  1976. }
  1977. getFilterByButton( button ) {
  1978. for ( const filter of this.filtered ) {
  1979. if ( filter.button === button ) {
  1980. return filter;
  1981. }
  1982. }
  1983. return null;
  1984. }
  1985. add( button ) {
  1986. super.add( button );
  1987. const onDown = () => {
  1988. const filter = this.getFilterByButton( button );
  1989. this.filteredIndex = this.filtered.indexOf( filter );
  1990. this.value = button.getValue();
  1991. this.submit();
  1992. };
  1993. button.dom.addEventListener( 'mousedown', onDown );
  1994. button.dom.addEventListener( 'touchstart', onDown );
  1995. this.domButtons.get( button ).remove();
  1996. return this;
  1997. }
  1998. set filteredIndex( index ) {
  1999. if ( this.currentFiltered ) {
  2000. const buttonDOM = this.domButtons.get( this.currentFiltered.button );
  2001. buttonDOM.classList.remove( 'active' );
  2002. this.currentFiltered = null;
  2003. }
  2004. const filteredItem = this.filtered[ index ];
  2005. if ( filteredItem ) {
  2006. const buttonDOM = this.domButtons.get( filteredItem.button );
  2007. buttonDOM.classList.add( 'active' );
  2008. this.currentFiltered = filteredItem;
  2009. }
  2010. this.updateFilter();
  2011. }
  2012. get filteredIndex() {
  2013. return this.currentFiltered ? this.filtered.indexOf( this.currentFiltered ) : null;
  2014. }
  2015. filter( text ) {
  2016. text = filterString( text );
  2017. const filtered = [];
  2018. for ( const button of this.buttons ) {
  2019. const buttonDOM = this.domButtons.get( button );
  2020. buttonDOM.remove();
  2021. const label = filterString( button.getValue() );
  2022. if ( text && label.includes( text ) === true ) {
  2023. const score = text.length / label.length;
  2024. filtered.push( {
  2025. button,
  2026. score
  2027. } );
  2028. }
  2029. }
  2030. filtered.sort( ( a, b ) => b.score - a.score );
  2031. this.filtered = filtered;
  2032. this.filteredIndex = this.forceAutoComplete ? 0 : null;
  2033. }
  2034. updateFilter() {
  2035. const filteredIndex = Math.min( this.filteredIndex, this.filteredIndex - 3 );
  2036. for ( let i = 0; i < this.filtered.length; i ++ ) {
  2037. const button = this.filtered[ i ].button;
  2038. const buttonDOM = this.domButtons.get( button );
  2039. buttonDOM.remove();
  2040. if ( i >= filteredIndex ) {
  2041. this.listDOM.append( buttonDOM );
  2042. }
  2043. }
  2044. }
  2045. }
  2046. class SelectInput extends Input {
  2047. constructor( options = [], value = null ) {
  2048. const dom = document.createElement( 'select' );
  2049. super( dom );
  2050. dom.onchange = () => {
  2051. this.dispatchEvent( new Event( 'change' ) );
  2052. };
  2053. dom.onmousedown = dom.ontouchstart = () => {
  2054. this.dispatchEvent( new Event( 'click' ) );
  2055. };
  2056. this.setOptions( options, value );
  2057. }
  2058. setOptions( options, value = null ) {
  2059. const dom = this.dom;
  2060. const defaultValue = dom.value;
  2061. let containsDefaultValue = false;
  2062. this.options = options;
  2063. dom.innerHTML = '';
  2064. for ( let index = 0; index < options.length; index ++ ) {
  2065. let opt = options[ index ];
  2066. if ( typeof opt === 'string' ) {
  2067. opt = { name: opt, value: index };
  2068. }
  2069. const option = document.createElement( 'option' );
  2070. option.innerText = opt.name;
  2071. option.value = opt.value;
  2072. if ( containsDefaultValue === false && defaultValue === opt.value ) {
  2073. containsDefaultValue = true;
  2074. }
  2075. dom.append( option );
  2076. }
  2077. dom.value = value !== null ? value : containsDefaultValue ? defaultValue : '';
  2078. return this;
  2079. }
  2080. getOptions() {
  2081. return this._options;
  2082. }
  2083. serialize( data ) {
  2084. data.options = [ ...this.options ];
  2085. super.serialize( data );
  2086. }
  2087. deserialize( data ) {
  2088. const currentOptions = this.options;
  2089. if ( currentOptions.length === 0 ) {
  2090. this.setOptions( data.options );
  2091. }
  2092. super.deserialize( data );
  2093. }
  2094. }
  2095. class ToggleInput extends Input {
  2096. constructor( value = false ) {
  2097. const dom = document.createElement( 'input' );
  2098. super( dom );
  2099. dom.type = 'checkbox';
  2100. dom.className = 'toggle';
  2101. dom.checked = value;
  2102. dom.onclick = () => this.dispatchEvent( new Event( 'click' ) );
  2103. dom.onchange = () => this.dispatchEvent( new Event( 'change' ) );
  2104. }
  2105. setValue( val ) {
  2106. this.dom.checked = val;
  2107. this.dispatchEvent( new Event( 'change' ) );
  2108. return this;
  2109. }
  2110. getValue() {
  2111. return this.dom.checked;
  2112. }
  2113. }
  2114. var Flow = /*#__PURE__*/Object.freeze( {
  2115. __proto__: null,
  2116. Element: Element,
  2117. Input: Input,
  2118. Node: Node,
  2119. Canvas: Canvas,
  2120. Serializer: Serializer,
  2121. Styles: Styles,
  2122. ObjectNode: ObjectNode,
  2123. PanelNode: PanelNode,
  2124. Menu: Menu,
  2125. ContextMenu: ContextMenu,
  2126. CircleMenu: CircleMenu,
  2127. Tips: Tips,
  2128. Search: Search,
  2129. DraggableElement: DraggableElement,
  2130. LabelElement: LabelElement,
  2131. TitleElement: TitleElement,
  2132. ButtonInput: ButtonInput,
  2133. ColorInput: ColorInput,
  2134. NumberInput: NumberInput,
  2135. SelectInput: SelectInput,
  2136. SliderInput: SliderInput,
  2137. StringInput: StringInput,
  2138. TextInput: TextInput,
  2139. ToggleInput: ToggleInput
  2140. } );
  2141. class Loader extends EventTarget {
  2142. constructor( parseType = Loader.DEFAULT ) {
  2143. super();
  2144. this.parseType = parseType;
  2145. this.events = {
  2146. 'load': []
  2147. };
  2148. }
  2149. setParseType( type ) {
  2150. this.parseType = type;
  2151. return this;
  2152. }
  2153. getParseType() {
  2154. return this.parseType;
  2155. }
  2156. onLoad( callback ) {
  2157. this.events.load.push( callback );
  2158. return this;
  2159. }
  2160. async load( url, lib = null ) {
  2161. return await fetch( url )
  2162. .then( response => response.json() )
  2163. .then( result => {
  2164. this.data = this.parse( result, lib );
  2165. dispatchEventList( this.events.load, this );
  2166. return this.data;
  2167. } )
  2168. .catch( err => {
  2169. console.error( 'Loader:', err );
  2170. } );
  2171. }
  2172. parse( json, lib = null ) {
  2173. json = this._parseObjects( json, lib );
  2174. const parseType = this.parseType;
  2175. if ( parseType === Loader.DEFAULT ) {
  2176. const flowObj = new Flow[ json.type ]();
  2177. if ( flowObj.getSerializable() ) {
  2178. flowObj.deserialize( json );
  2179. }
  2180. return flowObj;
  2181. } else if ( parseType === Loader.OBJECTS ) {
  2182. return json;
  2183. }
  2184. }
  2185. _parseObjects( json, lib = null ) {
  2186. json = { ...json };
  2187. const objects = {};
  2188. for ( const id in json.objects ) {
  2189. const obj = json.objects[ id ];
  2190. obj.objects = objects;
  2191. const Class = lib && lib[ obj.type ] ? lib[ obj.type ] : Flow[ obj.type ];
  2192. if ( ! Class ) {
  2193. console.error( `Class "${ obj.type }" not found!` );
  2194. }
  2195. objects[ id ] = new Class();
  2196. }
  2197. const ref = new WeakMap();
  2198. const deserializePass = ( prop = null ) => {
  2199. for ( const id in json.objects ) {
  2200. const newObject = objects[ id ];
  2201. if ( ref.has( newObject ) === false && ( prop === null || newObject[ prop ] === true ) ) {
  2202. ref.set( newObject, true );
  2203. if ( newObject.getSerializable() ) {
  2204. newObject.deserialize( json.objects[ id ] );
  2205. }
  2206. }
  2207. }
  2208. };
  2209. deserializePass( 'isNode' );
  2210. deserializePass( 'isElement' );
  2211. deserializePass( 'isInput' );
  2212. deserializePass();
  2213. json.objects = objects;
  2214. return json;
  2215. }
  2216. }
  2217. Loader.DEFAULT = 'default';
  2218. Loader.OBJECTS = 'objects';
  2219. export { ButtonInput, Canvas, CircleMenu, ColorInput, ContextMenu, DraggableElement, Element, Input, LabelElement, Loader, Menu, Node, NumberInput, ObjectNode, PanelNode, REVISION, Search, SelectInput, Serializer, SliderInput, StringInput, Styles, TextInput, Tips, TitleElement, ToggleInput, Utils };