flow.module.js 100 KB

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