flow.module.js 60 KB

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