flow.module.js 62 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306
  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 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-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.setWidth( 300 ).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 parseInt( 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. class DraggableElement extends Element {
  676. constructor( draggable = true ) {
  677. super( true );
  678. this.draggable = draggable;
  679. const onDrag = ( e ) => {
  680. e.preventDefault();
  681. if ( this.draggable === true ) {
  682. draggableDOM( this.node.dom );
  683. }
  684. };
  685. const { dom } = this;
  686. dom.addEventListener( 'mousedown', onDrag, true );
  687. dom.addEventListener( 'touchstart', onDrag, true );
  688. }
  689. }
  690. class TitleElement extends DraggableElement {
  691. constructor( title, draggable = true ) {
  692. super( draggable );
  693. const { dom } = this;
  694. dom.className = 'title';
  695. const spanDOM = document.createElement( 'span' );
  696. spanDOM.innerText = title;
  697. const iconDOM = document.createElement( 'i' );
  698. const toolbarDOM = document.createElement( 'f-toolbar' );
  699. this.buttons = [];
  700. this.spanDOM = spanDOM;
  701. this.iconDOM = iconDOM;
  702. this.toolbarDOM = toolbarDOM;
  703. dom.appendChild( spanDOM );
  704. dom.appendChild( iconDOM );
  705. dom.appendChild( toolbarDOM );
  706. }
  707. setIcon( value ) {
  708. this.iconDOM.className = value;
  709. return this;
  710. }
  711. getIcon() {
  712. return this.iconDOM.className;
  713. }
  714. setTitle( value ) {
  715. this.spanDOM.innerText = value;
  716. return this;
  717. }
  718. getTitle() {
  719. return this.spanDOM.innerText;
  720. }
  721. addButton( button ) {
  722. this.buttons.push( button );
  723. this.toolbarDOM.appendChild( button.dom );
  724. return this;
  725. }
  726. serialize( data ) {
  727. super.serialize( data );
  728. const title = this.getTitle();
  729. const icon = this.getIcon();
  730. data.title = title;
  731. if ( icon !== '' ) {
  732. data.icon = icon;
  733. }
  734. }
  735. deserialize( data ) {
  736. super.deserialize( data );
  737. this.setTitle( data.title );
  738. if ( data.icon !== undefined ) {
  739. this.setIcon( data.icon );
  740. }
  741. }
  742. }
  743. const drawLine = ( p1x, p1y, p2x, p2y, invert, size, color, ctx ) => {
  744. const offset = 100 * ( invert ? - 1 : 1 );
  745. ctx.beginPath();
  746. ctx.moveTo( p1x, p1y );
  747. ctx.bezierCurveTo(
  748. p1x + offset, p1y,
  749. p2x - offset, p2y,
  750. p2x, p2y
  751. );
  752. ctx.lineWidth = size;
  753. ctx.strokeStyle = color;
  754. ctx.stroke();
  755. };
  756. const colors = [
  757. '#ff4444',
  758. '#44ff44',
  759. '#4444ff'
  760. ];
  761. const dropNode = new Node().add( new TitleElement( 'File' ) ).setWidth( 250 );
  762. class Canvas extends Serializer {
  763. constructor() {
  764. super();
  765. const dom = document.createElement( 'f-canvas' );
  766. const contentDOM = document.createElement( 'f-content' );
  767. const areaDOM = document.createElement( 'f-area' );
  768. const dropDOM = document.createElement( 'f-drop' );
  769. const canvas = document.createElement( 'canvas' );
  770. const frontCanvas = document.createElement( 'canvas' );
  771. const context = canvas.getContext( '2d' );
  772. const frontContext = frontCanvas.getContext( '2d' );
  773. this.dom = dom;
  774. this.contentDOM = contentDOM;
  775. this.areaDOM = areaDOM;
  776. this.dropDOM = dropDOM;
  777. this.canvas = canvas;
  778. this.frontCanvas = frontCanvas;
  779. this.context = context;
  780. this.frontContext = frontContext;
  781. this.width = 10000;
  782. this.height = 10000;
  783. this.clientX = 0;
  784. this.clientY = 0;
  785. this.relativeClientX = 0;
  786. this.relativeClientY = 0;
  787. this.zoom = 1;
  788. this.nodes = [];
  789. this.selected = null;
  790. this.updating = false;
  791. this.droppedItems = [];
  792. this.events = {
  793. 'drop': []
  794. };
  795. frontCanvas.className = 'front';
  796. contentDOM.style.left = toPX( this.centerX );
  797. contentDOM.style.top = toPX( this.centerY );
  798. areaDOM.style.width = `calc( 100% + ${ this.width }px )`;
  799. areaDOM.style.height = `calc( 100% + ${ this.height }px )`;
  800. dropDOM.innerHTML = '<span>drop your file</span>';
  801. dom.appendChild( dropDOM );
  802. dom.appendChild( canvas );
  803. dom.appendChild( frontCanvas );
  804. dom.appendChild( contentDOM );
  805. dom.appendChild( areaDOM );
  806. /*
  807. let zoomTouchData = null;
  808. const onZoomStart = () => {
  809. zoomTouchData = null;
  810. };
  811. */
  812. const onZoom = ( e ) => {
  813. if ( e.touches ) {
  814. if ( e.touches.length === 2 ) {
  815. e.preventDefault();
  816. e.stopImmediatePropagation();
  817. /*
  818. const clientX = ( e.touches[ 0 ].clientX + e.touches[ 1 ].clientX ) / 2;
  819. const clientY = ( e.touches[ 0 ].clientY + e.touches[ 1 ].clientY ) / 2;
  820. const distance = Math.hypot(
  821. e.touches[ 0 ].clientX - e.touches[ 1 ].clientX,
  822. e.touches[ 0 ].clientY - e.touches[ 1 ].clientY
  823. );
  824. if ( zoomTouchData === null ) {
  825. zoomTouchData = {
  826. distance
  827. };
  828. }
  829. const delta = ( zoomTouchData.distance - distance );
  830. zoomTouchData.distance = distance;
  831. let zoom = Math.min( Math.max( this.zoom - delta * .01, .5 ), 1.2 );
  832. if ( zoom < .52 ) zoom = .5;
  833. else if ( zoom > .98 ) zoom = 1;
  834. contentDOM.style.left = toPX( this.centerX / zoom );
  835. contentDOM.style.top = toPX( this.centerY / zoom );
  836. contentDOM.style.zoom = this.zoom = zoom;
  837. */
  838. }
  839. } else {
  840. e.preventDefault();
  841. e.stopImmediatePropagation();
  842. /*
  843. const delta = e.deltaY / 100;
  844. const zoom = Math.min( Math.max( this.zoom - delta * .1, .5 ), 1 );
  845. contentDOM.style.left = toPX( this.centerX / zoom );
  846. contentDOM.style.top = toPX( this.centerY / zoom );
  847. contentDOM.style.zoom = this.zoom = zoom;
  848. */
  849. }
  850. };
  851. dom.addEventListener( 'wheel', onZoom );
  852. dom.addEventListener( 'touchmove', onZoom );
  853. //dom.addEventListener( 'touchstart', onZoomStart );
  854. let dropEnterCount = 0;
  855. const dragState = ( enter ) => {
  856. if ( enter ) {
  857. if ( dropEnterCount ++ === 0 ) {
  858. this.droppedItems = [];
  859. dropDOM.classList.add( 'visible' );
  860. this.add( dropNode );
  861. }
  862. } else if ( -- dropEnterCount === 0 ) {
  863. dropDOM.classList.remove( 'visible' );
  864. this.remove( dropNode );
  865. }
  866. };
  867. dom.addEventListener( 'dragenter', () => {
  868. dragState( true );
  869. } );
  870. dom.addEventListener( 'dragleave', () => {
  871. dragState( false );
  872. } );
  873. dom.addEventListener( 'dragover', ( e ) => {
  874. e.preventDefault();
  875. const { relativeClientX, relativeClientY } = this;
  876. const centerNodeX = dropNode.getWidth() / 2;
  877. dropNode.setPosition( relativeClientX - centerNodeX, relativeClientY - 20 );
  878. } );
  879. dom.addEventListener( 'drop', ( e ) => {
  880. e.preventDefault();
  881. dragState( false );
  882. this.droppedItems = e.dataTransfer.items;
  883. dispatchEventList( this.events.drop, this );
  884. } );
  885. draggableDOM( dom, ( data ) => {
  886. const { delta, isTouch } = data;
  887. if ( ! isTouch ) {
  888. if ( data.scrollTop === undefined ) {
  889. data.scrollLeft = dom.scrollLeft;
  890. data.scrollTop = dom.scrollTop;
  891. }
  892. dom.scrollLeft = data.scrollLeft - delta.x;
  893. dom.scrollTop = data.scrollTop - delta.y;
  894. }
  895. if ( data.dragging ) {
  896. dom.classList.add( 'grabbing' );
  897. } else {
  898. dom.classList.remove( 'grabbing' );
  899. }
  900. }, 'dragging-canvas' );
  901. this._onMoveEvent = ( e ) => {
  902. const event = e.touches ? e.touches[ 0 ] : e;
  903. const { zoom, rect } = this;
  904. this.clientX = event.clientX;
  905. this.clientY = event.clientY;
  906. this.relativeClientX = ( ( ( dom.scrollLeft - this.centerX ) + event.clientX ) - rect.left ) / zoom;
  907. this.relativeClientY = ( ( ( dom.scrollTop - this.centerY ) + event.clientY ) - rect.top ) / zoom;
  908. };
  909. this._onContentLoaded = () => {
  910. this.centralize();
  911. };
  912. this._onUpdate = () => {
  913. this.update();
  914. };
  915. this.start();
  916. }
  917. get rect() {
  918. return this.dom.getBoundingClientRect();
  919. }
  920. get relativeX() {
  921. return this.dom.scrollLeft - this.centerX;
  922. }
  923. get relativeY() {
  924. return this.dom.scrollTop - this.centerY;
  925. }
  926. get centerX() {
  927. return this.width / 2;
  928. }
  929. get centerY() {
  930. return this.height / 2;
  931. }
  932. onDrop( callback ) {
  933. this.events.drop.push( callback );
  934. return this;
  935. }
  936. start() {
  937. this.updating = true;
  938. document.addEventListener( 'wheel', this._onMoveEvent, true );
  939. document.addEventListener( 'mousedown', this._onMoveEvent, true );
  940. document.addEventListener( 'touchstart', this._onMoveEvent, true );
  941. document.addEventListener( 'mousemove', this._onMoveEvent, true );
  942. document.addEventListener( 'touchmove', this._onMoveEvent, true );
  943. document.addEventListener( 'dragover', this._onMoveEvent, true );
  944. document.addEventListener( 'DOMContentLoaded', this._onContentLoaded );
  945. requestAnimationFrame( this._onUpdate );
  946. }
  947. stop() {
  948. this.updating = false;
  949. document.removeEventListener( 'wheel', this._onMoveEvent, true );
  950. document.removeEventListener( 'mousedown', this._onMoveEvent, true );
  951. document.removeEventListener( 'touchstart', this._onMoveEvent, true );
  952. document.removeEventListener( 'mousemove', this._onMoveEvent, true );
  953. document.removeEventListener( 'touchmove', this._onMoveEvent, true );
  954. document.removeEventListener( 'dragover', this._onMoveEvent, true );
  955. document.removeEventListener( 'DOMContentLoaded', this._onContentLoaded );
  956. }
  957. add( node ) {
  958. this.nodes.push( node );
  959. node.canvas = this;
  960. this.contentDOM.appendChild( node.dom );
  961. return this;
  962. }
  963. remove( node ) {
  964. if ( node === this.selected ) {
  965. this.select();
  966. }
  967. this.unlink( node );
  968. const nodes = this.nodes;
  969. nodes.splice( nodes.indexOf( node ), 1 );
  970. node.canvas = null;
  971. this.contentDOM.removeChild( node.dom );
  972. return this;
  973. }
  974. clear() {
  975. const nodes = this.nodes;
  976. while ( nodes.length > 0 ) {
  977. this.remove( nodes[ 0 ] );
  978. }
  979. return this;
  980. }
  981. unlink( node ) {
  982. const links = this.getLinks();
  983. for ( const link of links ) {
  984. if ( link.outputElement && link.outputElement.node === node ) {
  985. link.inputElement.connect();
  986. }
  987. }
  988. }
  989. getLinks() {
  990. const links = [];
  991. for ( const node of this.nodes ) {
  992. links.push( ...node.getLinks() );
  993. }
  994. return links;
  995. }
  996. centralize() {
  997. this.dom.scroll( this.centerX, this.centerY );
  998. return this;
  999. }
  1000. select( node = null ) {
  1001. if ( node === this.selected ) return;
  1002. const previousNode = this.selected;
  1003. if ( previousNode !== null ) {
  1004. previousNode.dom.classList.remove( 'selected' );
  1005. this.selected = null;
  1006. dispatchEventList( previousNode.events.blur, previousNode );
  1007. }
  1008. if ( node !== null ) {
  1009. node.dom.classList.add( 'selected' );
  1010. this.selected = node;
  1011. dispatchEventList( node.events.focus, node );
  1012. }
  1013. }
  1014. update() {
  1015. if ( this.updating === false ) return;
  1016. requestAnimationFrame( this._onUpdate );
  1017. const { dom, zoom, canvas, frontCanvas, frontContext, context } = this;
  1018. const width = window.innerWidth;
  1019. const height = window.innerHeight;
  1020. const domRect = this.rect;
  1021. if ( canvas.width !== width || canvas.height !== height ) {
  1022. canvas.width = width;
  1023. canvas.height = height;
  1024. frontCanvas.width = width;
  1025. frontCanvas.height = height;
  1026. }
  1027. context.clearRect( 0, 0, width, height );
  1028. frontContext.clearRect( 0, 0, width, height );
  1029. context.globalCompositeOperation = 'lighter';
  1030. frontContext.globalCompositeOperation = 'source-over';
  1031. const links = this.getLinks();
  1032. const aPos = { x: 0, y: 0 };
  1033. const bPos = { x: 0, y: 0 };
  1034. const offsetIORadius = 10;
  1035. let dragging = '';
  1036. for ( const link of links ) {
  1037. const { lioElement, rioElement } = link;
  1038. let draggingLink = '';
  1039. let length = 0;
  1040. if ( lioElement !== null ) {
  1041. const rect = lioElement.dom.getBoundingClientRect();
  1042. length = Math.max( length, lioElement.rioLength );
  1043. aPos.x = rect.x + rect.width;
  1044. aPos.y = rect.y + ( rect.height / 2 );
  1045. } else {
  1046. aPos.x = this.clientX;
  1047. aPos.y = this.clientY;
  1048. draggingLink = 'lio';
  1049. }
  1050. if ( rioElement !== null ) {
  1051. const rect = rioElement.dom.getBoundingClientRect();
  1052. length = Math.max( length, rioElement.lioLength );
  1053. bPos.x = rect.x;
  1054. bPos.y = rect.y + ( rect.height / 2 );
  1055. } else {
  1056. bPos.x = this.clientX;
  1057. bPos.y = this.clientY;
  1058. draggingLink = 'rio';
  1059. }
  1060. dragging = dragging || draggingLink;
  1061. const drawContext = draggingLink ? frontContext : context;
  1062. if ( draggingLink || length === 1 ) {
  1063. if ( draggingLink === 'rio' ) {
  1064. aPos.x += offsetIORadius;
  1065. bPos.x /= zoom;
  1066. bPos.y /= zoom;
  1067. } else if ( draggingLink === 'lio' ) {
  1068. bPos.x -= offsetIORadius;
  1069. aPos.x /= zoom;
  1070. aPos.y /= zoom;
  1071. }
  1072. drawLine(
  1073. aPos.x * zoom, aPos.y * zoom,
  1074. bPos.x * zoom, bPos.y * zoom,
  1075. false, 2, '#ffffff', drawContext
  1076. );
  1077. } else {
  1078. length = Math.min( length, 4 );
  1079. for ( let i = 0; i < length; i ++ ) {
  1080. const color = colors[ i ] || '#ffffff';
  1081. const marginY = 4;
  1082. const rioLength = Math.min( lioElement.rioLength, length );
  1083. const lioLength = Math.min( rioElement.lioLength, length );
  1084. const aCenterY = ( ( rioLength * marginY ) * .5 ) - ( marginY / 2 );
  1085. const bCenterY = ( ( lioLength * marginY ) * .5 ) - ( marginY / 2 );
  1086. const aIndex = Math.min( i, rioLength - 1 );
  1087. const bIndex = Math.min( i, lioLength - 1 );
  1088. const aPosY = aIndex * marginY;
  1089. const bPosY = bIndex * marginY;
  1090. drawLine(
  1091. aPos.x * zoom, ( ( aPos.y + aPosY ) - aCenterY ) * zoom,
  1092. bPos.x * zoom, ( ( bPos.y + bPosY ) - bCenterY ) * zoom,
  1093. false, 2, color, drawContext
  1094. );
  1095. }
  1096. }
  1097. }
  1098. context.globalCompositeOperation = 'destination-in';
  1099. context.fillRect( domRect.x, domRect.y, domRect.width, domRect.height );
  1100. if ( dragging !== '' ) {
  1101. dom.classList.add( 'dragging-' + dragging );
  1102. } else {
  1103. dom.classList.remove( 'dragging-lio' );
  1104. dom.classList.remove( 'dragging-rio' );
  1105. }
  1106. }
  1107. serialize( data ) {
  1108. const nodes = [];
  1109. for ( const node of this.nodes ) {
  1110. nodes.push( node.toJSON( data ).id );
  1111. }
  1112. data.nodes = nodes;
  1113. }
  1114. deserialize( data ) {
  1115. for ( const id of data.nodes ) {
  1116. this.add( data.objects[ id ] );
  1117. }
  1118. }
  1119. }
  1120. class ButtonInput extends Input {
  1121. constructor( innterText = '' ) {
  1122. const dom = document.createElement( 'button' );
  1123. const spanDOM = document.createElement( 'span' );
  1124. dom.appendChild( spanDOM );
  1125. const iconDOM = document.createElement( 'i' );
  1126. dom.appendChild( iconDOM );
  1127. super( dom );
  1128. this.spanDOM = spanDOM;
  1129. this.iconDOM = iconDOM;
  1130. spanDOM.innerText = innterText;
  1131. dom.onmouseover = () => {
  1132. this.dispatchEvent( new Event( 'mouseover' ) );
  1133. };
  1134. dom.onclick = dom.ontouchstart = ( e ) => {
  1135. e.preventDefault();
  1136. e.stopPropagation();
  1137. this.dispatchEvent( new Event( 'click' ) );
  1138. };
  1139. }
  1140. setIcon( className ) {
  1141. this.iconDOM.className = className;
  1142. return this;
  1143. }
  1144. setValue( val ) {
  1145. this.spanDOM.innerText = val;
  1146. return this;
  1147. }
  1148. getValue() {
  1149. return this.spanDOM.innerText;
  1150. }
  1151. }
  1152. class ObjectNode extends Node {
  1153. constructor( name, inputLength, extra = null, width = 300 ) {
  1154. super();
  1155. this.setWidth( width );
  1156. const title = new TitleElement( name )
  1157. .setExtra( extra )
  1158. .setOutput( inputLength );
  1159. const closeButton = new ButtonInput( '✖' ).onClick( () => {
  1160. this.dispose();
  1161. } );
  1162. title.addButton( closeButton );
  1163. this.add( title );
  1164. this.title = title;
  1165. this.closeButton = closeButton;
  1166. }
  1167. setExtra( value ) {
  1168. this.title.setExtra( value );
  1169. return this;
  1170. }
  1171. getExtra( value ) {
  1172. return this.title.getExtra();
  1173. }
  1174. invalidate() {
  1175. this.title.dispatchEvent( new Event( 'connect' ) );
  1176. }
  1177. }
  1178. const ENTER_KEY$1 = 13;
  1179. class StringInput extends Input {
  1180. constructor( value = '' ) {
  1181. const dom = document.createElement( 'input' );
  1182. super( dom );
  1183. dom.type = 'text';
  1184. dom.value = value;
  1185. dom.spellcheck = false;
  1186. dom.autocomplete = 'off';
  1187. dom.onblur = () => {
  1188. this.dispatchEvent( new Event( 'blur' ) );
  1189. };
  1190. dom.onchange = () => {
  1191. this.dispatchEvent( new Event( 'change' ) );
  1192. };
  1193. dom.onkeyup = ( e ) => {
  1194. if ( e.keyCode === ENTER_KEY$1 ) {
  1195. e.target.blur();
  1196. }
  1197. e.stopPropagation();
  1198. this.dispatchEvent( new Event( 'change' ) );
  1199. };
  1200. }
  1201. }
  1202. const ENTER_KEY = 13;
  1203. class NumberInput extends Input {
  1204. constructor( value = 0, min = - Infinity, max = Infinity, step = .01 ) {
  1205. const dom = document.createElement( 'input' );
  1206. super( dom );
  1207. this.min = min;
  1208. this.max = max;
  1209. this.step = step;
  1210. this.integer = false;
  1211. dom.type = 'text';
  1212. dom.className = 'number';
  1213. dom.value = this._getString( value );
  1214. dom.spellcheck = false;
  1215. dom.autocomplete = 'off';
  1216. dom.ondragstart = dom.oncontextmenu = ( e ) => {
  1217. e.preventDefault();
  1218. e.stopPropagation();
  1219. };
  1220. dom.onfocus = dom.onclick = () => {
  1221. dom.select();
  1222. };
  1223. dom.onblur = () => {
  1224. this.dispatchEvent( new Event( 'blur' ) );
  1225. };
  1226. dom.onchange = () => {
  1227. this.dispatchEvent( new Event( 'change' ) );
  1228. };
  1229. dom.onkeydown = ( e ) => {
  1230. if ( e.key.length === 1 && /\d|\./.test( e.key ) !== true ) {
  1231. return false;
  1232. }
  1233. if ( e.keyCode === ENTER_KEY ) {
  1234. e.target.blur();
  1235. }
  1236. e.stopPropagation();
  1237. };
  1238. draggableDOM( dom, ( data ) => {
  1239. const { delta } = data;
  1240. if ( data.value === undefined ) {
  1241. data.value = this.getValue();
  1242. }
  1243. const diff = delta.x - delta.y;
  1244. const value = data.value + ( diff * this.step );
  1245. this.dom.value = this._getString( value.toFixed( this.precision ) );
  1246. this.dispatchEvent( new Event( 'change' ) );
  1247. } );
  1248. }
  1249. setRange( min, max, step ) {
  1250. this.min = min;
  1251. this.max = max;
  1252. this.step = step;
  1253. this.dispatchEvent( new Event( 'range' ) );
  1254. return this.setValue( this.getValue() );
  1255. }
  1256. get precision() {
  1257. if ( this.integer === true ) return 0;
  1258. const fract = this.step % 1;
  1259. return fract !== 0 ? fract.toString().split( '.' )[ 1 ].length : 1;
  1260. }
  1261. setValue( val, dispatch = true ) {
  1262. return super.setValue( this._getString( val ), dispatch );
  1263. }
  1264. getValue() {
  1265. return Number( this.dom.value );
  1266. }
  1267. serialize( data ) {
  1268. const { min, max } = this;
  1269. if ( min !== - Infinity && max !== Infinity ) {
  1270. data.min = this.min;
  1271. data.max = this.max;
  1272. data.step = this.step;
  1273. }
  1274. super.serialize( data );
  1275. }
  1276. deserialize( data ) {
  1277. if ( data.min !== undefined ) {
  1278. const { min, max, step } = this;
  1279. this.setRange( min, max, step );
  1280. }
  1281. super.deserialize( data );
  1282. }
  1283. _getString( value ) {
  1284. let num = Math.min( Math.max( Number( value ), this.min ), this.max );
  1285. if ( this.integer === true ) {
  1286. return Math.floor( num );
  1287. } else {
  1288. return num + ( num % 1 ? '' : '.0' );
  1289. }
  1290. }
  1291. }
  1292. const getStep = ( min, max ) => {
  1293. const sensibility = .001;
  1294. return ( max - min ) * sensibility;
  1295. };
  1296. class SliderInput extends Input {
  1297. constructor( value = 0, min = 0, max = 100 ) {
  1298. const dom = document.createElement( 'f-subinputs' );
  1299. super( dom );
  1300. value = Math.min( Math.max( value, min ), max );
  1301. const step = getStep( min, max );
  1302. const rangeDOM = document.createElement( 'input' );
  1303. rangeDOM.type = 'range';
  1304. rangeDOM.min = min;
  1305. rangeDOM.max = max;
  1306. rangeDOM.step = step;
  1307. rangeDOM.value = value;
  1308. const field = new NumberInput( value, min, max, step );
  1309. field.dom.className = 'range-value';
  1310. field.onChange( () => {
  1311. rangeDOM.value = field.getValue();
  1312. } );
  1313. field.addEventListener( 'range', () => {
  1314. rangeDOM.min = field.min;
  1315. rangeDOM.max = field.max;
  1316. rangeDOM.step = field.step;
  1317. rangeDOM.value = field.getValue();
  1318. } );
  1319. dom.appendChild( rangeDOM );
  1320. dom.appendChild( field.dom );
  1321. this.rangeDOM = rangeDOM;
  1322. this.field = field;
  1323. const updateRangeValue = () => {
  1324. let value = Number( rangeDOM.value );
  1325. if ( value !== this.max && value + this.step >= this.max ) {
  1326. // fix not end range fraction
  1327. rangeDOM.value = value = this.max;
  1328. }
  1329. this.field.setValue( value );
  1330. };
  1331. draggableDOM( rangeDOM, () => {
  1332. updateRangeValue();
  1333. this.dispatchEvent( new Event( 'change' ) );
  1334. }, '' );
  1335. }
  1336. get min() {
  1337. return this.field.min;
  1338. }
  1339. get max() {
  1340. return this.field.max;
  1341. }
  1342. get step() {
  1343. return this.field.step;
  1344. }
  1345. setRange( min, max ) {
  1346. this.field.setRange( min, max, getStep( min, max ) );
  1347. this.dispatchEvent( new Event( 'range' ) );
  1348. this.dispatchEvent( new Event( 'change' ) );
  1349. return this;
  1350. }
  1351. setValue( val, dispatch = true ) {
  1352. this.field.setValue( val );
  1353. this.rangeDOM.value = val;
  1354. if ( dispatch ) this.dispatchEvent( new Event( 'change' ) );
  1355. return this;
  1356. }
  1357. getValue() {
  1358. return this.field.getValue();
  1359. }
  1360. serialize( data ) {
  1361. data.min = this.min;
  1362. data.max = this.max;
  1363. super.serialize( data );
  1364. }
  1365. deserialize( data ) {
  1366. const { min, max } = data;
  1367. this.setRange( min, max );
  1368. super.deserialize( data );
  1369. }
  1370. }
  1371. class ColorInput extends Input {
  1372. constructor( value = 0x0099ff ) {
  1373. const dom = document.createElement( 'input' );
  1374. super( dom );
  1375. dom.type = 'color';
  1376. dom.value = toHex( value );
  1377. dom.oninput = () => {
  1378. this.dispatchEvent( new Event( 'change' ) );
  1379. };
  1380. }
  1381. setValue( value, dispatch = true ) {
  1382. return super.setValue( toHex( value ), dispatch );
  1383. }
  1384. getValue() {
  1385. return parseInt( super.getValue().substr( 1 ), 16 );
  1386. }
  1387. }
  1388. class TextInput extends Input {
  1389. constructor( innerText = '' ) {
  1390. const dom = document.createElement( 'textarea' );
  1391. super( dom );
  1392. dom.innerText = innerText;
  1393. }
  1394. setValue( val ) {
  1395. this.dom.innerText = val;
  1396. return this;
  1397. }
  1398. getValue() {
  1399. return this.dom.innerText;
  1400. }
  1401. }
  1402. class LabelElement extends Element {
  1403. constructor( label = '', align = '' ) {
  1404. super();
  1405. this.labelDOM = document.createElement( 'f-label' );
  1406. this.inputsDOM = document.createElement( 'f-inputs' );
  1407. const spanDOM = document.createElement( 'span' );
  1408. const iconDOM = document.createElement( 'i' );
  1409. this.spanDOM = spanDOM;
  1410. this.iconDOM = iconDOM;
  1411. this.labelDOM.appendChild( this.spanDOM );
  1412. this.labelDOM.appendChild( this.iconDOM );
  1413. this.dom.appendChild( this.labelDOM );
  1414. this.dom.appendChild( this.inputsDOM );
  1415. this.setLabel( label );
  1416. this.setAlign( align );
  1417. }
  1418. setIcon( value ) {
  1419. this.iconDOM.className = value;
  1420. return this;
  1421. }
  1422. getIcon() {
  1423. return this.iconDOM.className;
  1424. }
  1425. setAlign( align ) {
  1426. this.labelDOM.className = align;
  1427. }
  1428. setLabel( val ) {
  1429. this.spanDOM.innerText = val;
  1430. }
  1431. getLabel() {
  1432. return this.spanDOM.innerText;
  1433. }
  1434. serialize( data ) {
  1435. super.serialize( data );
  1436. const label = this.getLabel();
  1437. const icon = this.getIcon();
  1438. data.label = label;
  1439. if ( icon !== '' ) {
  1440. data.icon = icon;
  1441. }
  1442. }
  1443. deserialize( data ) {
  1444. super.deserialize( data );
  1445. this.setLabel( data.label );
  1446. if ( data.icon !== undefined ) {
  1447. this.setIcon( data.icon );
  1448. }
  1449. }
  1450. }
  1451. class PanelNode extends Node {
  1452. constructor( title = 'Panel', align = 'top-right' ) {
  1453. super();
  1454. const titleElement = new TitleElement( title );
  1455. this.add( titleElement );
  1456. const collapseButton = new ButtonInput( '🗕' );
  1457. collapseButton.onClick( () => {
  1458. this.setCollapse( ! this.collapsed );
  1459. } );
  1460. titleElement.addButton( collapseButton );
  1461. this.collapseButton = collapseButton;
  1462. this.titleElement = titleElement;
  1463. this.align = align;
  1464. this.collapsed = false;
  1465. this.setAlign( align );
  1466. this.setStyle( 'rouded' );
  1467. }
  1468. setCollapse( value ) {
  1469. const cssClass = 'closed';
  1470. this.dom.classList.remove( cssClass );
  1471. this.collapsed = value;
  1472. this.collapseButton.value = value ? '🗖' : '🗕';
  1473. if ( value === true ) {
  1474. this.dom.classList.add( cssClass );
  1475. }
  1476. return this;
  1477. }
  1478. setAlign( align ) {
  1479. if ( this.align ) this.dom.classList.remove( this.align );
  1480. this.dom.classList.add( align );
  1481. this.align = align;
  1482. return this;
  1483. }
  1484. addInput( inputClass, object, property, ...params ) {
  1485. const value = object[ property ];
  1486. const input = new inputClass( value, ...params );
  1487. input.onChange( () => {
  1488. object[ property ] = input.value;
  1489. } );
  1490. this.add( new LabelElement( property ).add( input ) );
  1491. return input;
  1492. }
  1493. addSlider( object, property, min, max ) {
  1494. return this.addInput( SliderInput, object, property, min, max );
  1495. }
  1496. addNumber( object, property ) {
  1497. return this.addInput( NumberInput, object, property );
  1498. }
  1499. addColor( object, property ) {
  1500. return this.addInput( ColorInput, object, property );
  1501. }
  1502. addString( object, property ) {
  1503. return this.addInput( StringInput, object, property );
  1504. }
  1505. addText( object, property ) {
  1506. const input = this.addInput( TextInput, object, property );
  1507. input.element.setHeight( 70 );
  1508. return input;
  1509. }
  1510. addButton( name ) {
  1511. const input = new ButtonInput( name );
  1512. this.add( new Element().setHeight( 34 ).add( input ) );
  1513. return input;
  1514. }
  1515. }
  1516. class Menu extends EventTarget {
  1517. constructor( className, target = null ) {
  1518. super();
  1519. const dom = document.createElement( 'f-menu' );
  1520. dom.className = className + ' hidden';
  1521. this.dom = dom;
  1522. this.visible = false;
  1523. this.subMenus = new WeakMap();
  1524. this.domButtons = new WeakMap();
  1525. this.events = {
  1526. 'context': []
  1527. };
  1528. this.addEventListener( 'context', ( ) => {
  1529. dispatchEventList( this.events.context, this );
  1530. } );
  1531. this._lastButtonClick = null;
  1532. this._onButtonClick = ( e = null ) => {
  1533. const button = e ? e.target : null;
  1534. if ( this._lastButtonClick ) {
  1535. this._lastButtonClick.dom.parentElement.classList.remove( 'active' );
  1536. }
  1537. this._lastButtonClick = button;
  1538. if ( button ) {
  1539. if ( this.subMenus.has( button ) ) {
  1540. this.subMenus.get( button )._onButtonClick();
  1541. }
  1542. button.dom.parentElement.classList.add( 'active' );
  1543. }
  1544. };
  1545. this._onButtonMouseOver = ( e ) => {
  1546. const button = e.target;
  1547. if ( this.subMenus.has( button ) && this._lastButtonClick !== button ) {
  1548. this._onButtonClick();
  1549. }
  1550. };
  1551. this.setTarget( target );
  1552. }
  1553. onContext( callback ) {
  1554. this.events.context.push( callback );
  1555. return this;
  1556. }
  1557. show( x = null, y = null ) {
  1558. this._onButtonClick();
  1559. if ( x !== null && y !== null ) {
  1560. this.setPosition( x, y );
  1561. }
  1562. this.dom.classList.remove( 'hidden' );
  1563. this.visible = true;
  1564. this.dispatchEvent( new Event( 'show' ) );
  1565. return this;
  1566. }
  1567. hide() {
  1568. this.dom.classList.add( 'hidden' );
  1569. this.dispatchEvent( new Event( 'hide' ) );
  1570. this.visible = false;
  1571. }
  1572. setTarget( target = null ) {
  1573. if ( target !== null ) {
  1574. const onContextMenu = ( e ) => {
  1575. e.preventDefault();
  1576. if ( e.pointerType !== 'mouse' || ( e.pageX === 0 && e.pageY === 0 ) ) return;
  1577. const rect = this.target.getBoundingClientRect();
  1578. this.dispatchEvent( new Event( 'context' ) );
  1579. this.show( e.pageX - rect.left, e.pageY - rect.top );
  1580. };
  1581. const onDown = ( e ) => {
  1582. if ( this.visible === true && e.target.closest( 'f-menu' ) === null ) {
  1583. this.hide();
  1584. }
  1585. };
  1586. this.target = target;
  1587. target.addEventListener( 'mousedown', onDown, true );
  1588. target.addEventListener( 'touchstart', onDown, true );
  1589. target.addEventListener( 'contextmenu', onContextMenu, false );
  1590. target.appendChild( this.dom );
  1591. }
  1592. return this;
  1593. }
  1594. add( button, submenu = null ) {
  1595. const liDOM = document.createElement( 'f-item' );
  1596. if ( submenu !== null ) {
  1597. liDOM.classList.add( 'submenu' );
  1598. liDOM.appendChild( submenu.dom );
  1599. this.subMenus.set( button, submenu );
  1600. }
  1601. liDOM.appendChild( button.dom );
  1602. button.addEventListener( 'click', this._onButtonClick );
  1603. button.addEventListener( 'mouseover', this._onButtonMouseOver );
  1604. this.dom.appendChild( liDOM );
  1605. this.domButtons.set( liDOM, button );
  1606. return this;
  1607. }
  1608. setPosition( x, y ) {
  1609. const dom = this.dom;
  1610. dom.style.left = toPX( x );
  1611. dom.style.top = toPX( y );
  1612. return this;
  1613. }
  1614. }
  1615. let lastContext = null;
  1616. class ContextMenu extends Menu {
  1617. constructor( target = null ) {
  1618. super( 'context', target );
  1619. }
  1620. show( x, y ) {
  1621. if ( lastContext !== null ) {
  1622. lastContext.hide();
  1623. }
  1624. lastContext = this;
  1625. return super.show( x, y );
  1626. }
  1627. hide() {
  1628. if ( lastContext === this ) {
  1629. lastContext = null;
  1630. }
  1631. return super.hide();
  1632. }
  1633. }
  1634. class CircleMenu extends Menu {
  1635. constructor( target = null ) {
  1636. super( 'circle', target );
  1637. }
  1638. }
  1639. class SelectInput extends Input {
  1640. constructor( options = [] ) {
  1641. const dom = document.createElement( 'select' );
  1642. super( dom );
  1643. dom.onchange = () => {
  1644. this.dispatchEvent( new Event( 'change' ) );
  1645. };
  1646. this.setOptions( options );
  1647. }
  1648. setOptions( options ) {
  1649. const dom = this.dom;
  1650. this.options = options;
  1651. dom.innerHTML = '';
  1652. for ( let index = 0; index < options.length; index ++ ) {
  1653. let opt = options[ index ];
  1654. if ( typeof opt === 'string' ) {
  1655. opt = { name: opt, value: index };
  1656. }
  1657. const option = document.createElement( 'option' );
  1658. option.innerText = opt.name;
  1659. option.value = opt.value;
  1660. dom.appendChild( option );
  1661. }
  1662. return this;
  1663. }
  1664. getOptions() {
  1665. return this._options;
  1666. }
  1667. serialize( data ) {
  1668. data.options = [ ...this.options ];
  1669. super.serialize( data );
  1670. }
  1671. deserialize( data ) {
  1672. const currentOptions = this.options;
  1673. if ( currentOptions.length > 0 ) {
  1674. this.setOptions( data.options );
  1675. }
  1676. super.deserialize( data );
  1677. }
  1678. }
  1679. class ToggleInput extends Input {
  1680. constructor( value = false ) {
  1681. const dom = document.createElement( 'input' );
  1682. super( dom );
  1683. dom.type = 'checkbox';
  1684. dom.className = 'toggle';
  1685. dom.checked = value;
  1686. }
  1687. setValue( val ) {
  1688. this.dom.checked = val;
  1689. this.dispatchEvent( new Event( 'change' ) );
  1690. return this;
  1691. }
  1692. getValue() {
  1693. return this.dom.checked;
  1694. }
  1695. }
  1696. var Flow = /*#__PURE__*/Object.freeze({
  1697. __proto__: null,
  1698. Element: Element,
  1699. Input: Input,
  1700. Node: Node,
  1701. Canvas: Canvas,
  1702. Serializer: Serializer,
  1703. ObjectNode: ObjectNode,
  1704. PanelNode: PanelNode,
  1705. Menu: Menu,
  1706. ContextMenu: ContextMenu,
  1707. CircleMenu: CircleMenu,
  1708. DraggableElement: DraggableElement,
  1709. LabelElement: LabelElement,
  1710. TitleElement: TitleElement,
  1711. ButtonInput: ButtonInput,
  1712. ColorInput: ColorInput,
  1713. NumberInput: NumberInput,
  1714. SelectInput: SelectInput,
  1715. SliderInput: SliderInput,
  1716. StringInput: StringInput,
  1717. TextInput: TextInput,
  1718. ToggleInput: ToggleInput
  1719. });
  1720. class Loader extends EventTarget {
  1721. constructor( parseType = Loader.DEFAULT ) {
  1722. super();
  1723. this.parseType = parseType;
  1724. this.events = {
  1725. 'load': []
  1726. };
  1727. }
  1728. setParseType( type ) {
  1729. this.parseType = type;
  1730. return this;
  1731. }
  1732. getParseType() {
  1733. return this.parseType;
  1734. }
  1735. onLoad( callback ) {
  1736. this.events.load.push( callback );
  1737. return this;
  1738. }
  1739. async load( url, lib = null ) {
  1740. return await fetch( url )
  1741. .then( response => response.json() )
  1742. .then( result => {
  1743. this.data = this.parse( result, lib );
  1744. dispatchEventList( this.events.load, this );
  1745. return this.data;
  1746. } )
  1747. .catch( err => {
  1748. console.error( 'Loader:', err );
  1749. } );
  1750. }
  1751. parse( json, lib = null ) {
  1752. json = this._parseObjects( json, lib );
  1753. const parseType = this.parseType;
  1754. if ( parseType === Loader.DEFAULT ) {
  1755. const flowObj = new Flow[ json.type ]();
  1756. if ( flowObj.getSerializable() ) {
  1757. flowObj.deserialize( json );
  1758. }
  1759. return flowObj;
  1760. } else if ( parseType === Loader.OBJECTS ) {
  1761. return json;
  1762. }
  1763. }
  1764. _parseObjects( json, lib = null ) {
  1765. json = { ...json };
  1766. const objects = {};
  1767. for ( const id in json.objects ) {
  1768. const obj = json.objects[ id ];
  1769. obj.objects = objects;
  1770. const Class = lib && lib[ obj.type ] ? lib[ obj.type ] : Flow[ obj.type ];
  1771. objects[ id ] = new Class();
  1772. }
  1773. const ref = new WeakMap();
  1774. const deserializePass = ( prop = null ) => {
  1775. for ( const id in json.objects ) {
  1776. const newObject = objects[ id ];
  1777. if ( ref.has( newObject ) === false && ( prop === null || newObject[ prop ] === true ) ) {
  1778. ref.set( newObject, true );
  1779. if ( newObject.getSerializable() ) {
  1780. newObject.deserialize( json.objects[ id ] );
  1781. }
  1782. }
  1783. }
  1784. };
  1785. deserializePass( 'isNode' );
  1786. deserializePass( 'isElement' );
  1787. deserializePass( 'isInput' );
  1788. deserializePass();
  1789. json.objects = objects;
  1790. return json;
  1791. }
  1792. }
  1793. Loader.DEFAULT = 'default';
  1794. Loader.OBJECTS = 'objects';
  1795. 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 };
粤ICP备19079148号