flow.module.js 63 KB

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