flow.module.js 89 KB

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