flow.module.js 79 KB

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