jsplumb.js 665 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171
  1. /**
  2. * jsBezier
  3. *
  4. * Copyright (c) 2010 - 2017 jsPlumb (hello@jsplumbtoolkit.com)
  5. *
  6. * licensed under the MIT license.
  7. *
  8. * a set of Bezier curve functions that deal with Beziers, used by jsPlumb, and perhaps useful for other people. These functions work with Bezier
  9. * curves of arbitrary degree.
  10. *
  11. * - functions are all in the 'jsBezier' namespace.
  12. *
  13. * - all input points should be in the format {x:.., y:..}. all output points are in this format too.
  14. *
  15. * - all input curves should be in the format [ {x:.., y:..}, {x:.., y:..}, {x:.., y:..}, {x:.., y:..} ]
  16. *
  17. * - 'location' as used as an input here refers to a decimal in the range 0-1 inclusive, which indicates a point some proportion along the length
  18. * of the curve. location as output has the same format and meaning.
  19. *
  20. *
  21. * Function List:
  22. * --------------
  23. *
  24. * distanceFromCurve(point, curve)
  25. *
  26. * Calculates the distance that the given point lies from the given Bezier. Note that it is computed relative to the center of the Bezier,
  27. * so if you have stroked the curve with a wide pen you may wish to take that into account! The distance returned is relative to the values
  28. * of the curve and the point - it will most likely be pixels.
  29. *
  30. * gradientAtPoint(curve, location)
  31. *
  32. * Calculates the gradient to the curve at the given location, as a decimal between 0 and 1 inclusive.
  33. *
  34. * gradientAtPointAlongCurveFrom (curve, location)
  35. *
  36. * Calculates the gradient at the point on the given curve that is 'distance' units from location.
  37. *
  38. * nearestPointOnCurve(point, curve)
  39. *
  40. * Calculates the nearest point to the given point on the given curve. The return value of this is a JS object literal, containing both the
  41. *point's coordinates and also the 'location' of the point (see above), for example: { point:{x:551,y:150}, location:0.263365 }.
  42. *
  43. * pointOnCurve(curve, location)
  44. *
  45. * Calculates the coordinates of the point on the given Bezier curve at the given location.
  46. *
  47. * pointAlongCurveFrom(curve, location, distance)
  48. *
  49. * Calculates the coordinates of the point on the given curve that is 'distance' units from location. 'distance' should be in the same coordinate
  50. * space as that used to construct the Bezier curve. For an HTML Canvas usage, for example, distance would be a measure of pixels.
  51. *
  52. * locationAlongCurveFrom(curve, location, distance)
  53. *
  54. * Calculates the location on the given curve that is 'distance' units from location. 'distance' should be in the same coordinate
  55. * space as that used to construct the Bezier curve. For an HTML Canvas usage, for example, distance would be a measure of pixels.
  56. *
  57. * perpendicularToCurveAt(curve, location, length, distance)
  58. *
  59. * Calculates the perpendicular to the given curve at the given location. length is the length of the line you wish for (it will be centered
  60. * on the point at 'location'). distance is optional, and allows you to specify a point along the path from the given location as the center of
  61. * the perpendicular returned. The return value of this is an array of two points: [ {x:...,y:...}, {x:...,y:...} ].
  62. *
  63. *
  64. */
  65. (function() {
  66. var root = this;
  67. if(typeof Math.sgn == "undefined") {
  68. Math.sgn = function(x) { return x == 0 ? 0 : x > 0 ? 1 :-1; };
  69. }
  70. var Vectors = {
  71. subtract : function(v1, v2) { return {x:v1.x - v2.x, y:v1.y - v2.y }; },
  72. dotProduct : function(v1, v2) { return (v1.x * v2.x) + (v1.y * v2.y); },
  73. square : function(v) { return Math.sqrt((v.x * v.x) + (v.y * v.y)); },
  74. scale : function(v, s) { return {x:v.x * s, y:v.y * s }; }
  75. },
  76. maxRecursion = 64,
  77. flatnessTolerance = Math.pow(2.0,-maxRecursion-1);
  78. /**
  79. * Calculates the distance that the point lies from the curve.
  80. *
  81. * @param point a point in the form {x:567, y:3342}
  82. * @param curve a Bezier curve in the form [{x:..., y:...}, {x:..., y:...}, {x:..., y:...}, {x:..., y:...}]. note that this is currently
  83. * hardcoded to assume cubiz beziers, but would be better off supporting any degree.
  84. * @return a JS object literal containing location and distance, for example: {location:0.35, distance:10}. Location is analogous to the location
  85. * argument you pass to the pointOnPath function: it is a ratio of distance travelled along the curve. Distance is the distance in pixels from
  86. * the point to the curve.
  87. */
  88. var _distanceFromCurve = function(point, curve) {
  89. var candidates = [],
  90. w = _convertToBezier(point, curve),
  91. degree = curve.length - 1, higherDegree = (2 * degree) - 1,
  92. numSolutions = _findRoots(w, higherDegree, candidates, 0),
  93. v = Vectors.subtract(point, curve[0]), dist = Vectors.square(v), t = 0.0;
  94. for (var i = 0; i < numSolutions; i++) {
  95. v = Vectors.subtract(point, _bezier(curve, degree, candidates[i], null, null));
  96. var newDist = Vectors.square(v);
  97. if (newDist < dist) {
  98. dist = newDist;
  99. t = candidates[i];
  100. }
  101. }
  102. v = Vectors.subtract(point, curve[degree]);
  103. newDist = Vectors.square(v);
  104. if (newDist < dist) {
  105. dist = newDist;
  106. t = 1.0;
  107. }
  108. return {location:t, distance:dist};
  109. };
  110. /**
  111. * finds the nearest point on the curve to the given point.
  112. */
  113. var _nearestPointOnCurve = function(point, curve) {
  114. var td = _distanceFromCurve(point, curve);
  115. return {point:_bezier(curve, curve.length - 1, td.location, null, null), location:td.location};
  116. };
  117. var _convertToBezier = function(point, curve) {
  118. var degree = curve.length - 1, higherDegree = (2 * degree) - 1,
  119. c = [], d = [], cdTable = [], w = [],
  120. z = [ [1.0, 0.6, 0.3, 0.1], [0.4, 0.6, 0.6, 0.4], [0.1, 0.3, 0.6, 1.0] ];
  121. for (var i = 0; i <= degree; i++) c[i] = Vectors.subtract(curve[i], point);
  122. for (var i = 0; i <= degree - 1; i++) {
  123. d[i] = Vectors.subtract(curve[i+1], curve[i]);
  124. d[i] = Vectors.scale(d[i], 3.0);
  125. }
  126. for (var row = 0; row <= degree - 1; row++) {
  127. for (var column = 0; column <= degree; column++) {
  128. if (!cdTable[row]) cdTable[row] = [];
  129. cdTable[row][column] = Vectors.dotProduct(d[row], c[column]);
  130. }
  131. }
  132. for (i = 0; i <= higherDegree; i++) {
  133. if (!w[i]) w[i] = [];
  134. w[i].y = 0.0;
  135. w[i].x = parseFloat(i) / higherDegree;
  136. }
  137. var n = degree, m = degree-1;
  138. for (var k = 0; k <= n + m; k++) {
  139. var lb = Math.max(0, k - m),
  140. ub = Math.min(k, n);
  141. for (i = lb; i <= ub; i++) {
  142. var j = k - i;
  143. w[i+j].y += cdTable[j][i] * z[j][i];
  144. }
  145. }
  146. return w;
  147. };
  148. /**
  149. * counts how many roots there are.
  150. */
  151. var _findRoots = function(w, degree, t, depth) {
  152. var left = [], right = [],
  153. left_count, right_count,
  154. left_t = [], right_t = [];
  155. switch (_getCrossingCount(w, degree)) {
  156. case 0 : {
  157. return 0;
  158. }
  159. case 1 : {
  160. if (depth >= maxRecursion) {
  161. t[0] = (w[0].x + w[degree].x) / 2.0;
  162. return 1;
  163. }
  164. if (_isFlatEnough(w, degree)) {
  165. t[0] = _computeXIntercept(w, degree);
  166. return 1;
  167. }
  168. break;
  169. }
  170. }
  171. _bezier(w, degree, 0.5, left, right);
  172. left_count = _findRoots(left, degree, left_t, depth+1);
  173. right_count = _findRoots(right, degree, right_t, depth+1);
  174. for (var i = 0; i < left_count; i++) t[i] = left_t[i];
  175. for (var i = 0; i < right_count; i++) t[i+left_count] = right_t[i];
  176. return (left_count+right_count);
  177. };
  178. var _getCrossingCount = function(curve, degree) {
  179. var n_crossings = 0, sign, old_sign;
  180. sign = old_sign = Math.sgn(curve[0].y);
  181. for (var i = 1; i <= degree; i++) {
  182. sign = Math.sgn(curve[i].y);
  183. if (sign != old_sign) n_crossings++;
  184. old_sign = sign;
  185. }
  186. return n_crossings;
  187. };
  188. var _isFlatEnough = function(curve, degree) {
  189. var error,
  190. intercept_1, intercept_2, left_intercept, right_intercept,
  191. a, b, c, det, dInv, a1, b1, c1, a2, b2, c2;
  192. a = curve[0].y - curve[degree].y;
  193. b = curve[degree].x - curve[0].x;
  194. c = curve[0].x * curve[degree].y - curve[degree].x * curve[0].y;
  195. var max_distance_above, max_distance_below;
  196. max_distance_above = max_distance_below = 0.0;
  197. for (var i = 1; i < degree; i++) {
  198. var value = a * curve[i].x + b * curve[i].y + c;
  199. if (value > max_distance_above)
  200. max_distance_above = value;
  201. else if (value < max_distance_below)
  202. max_distance_below = value;
  203. }
  204. a1 = 0.0; b1 = 1.0; c1 = 0.0; a2 = a; b2 = b;
  205. c2 = c - max_distance_above;
  206. det = a1 * b2 - a2 * b1;
  207. dInv = 1.0/det;
  208. intercept_1 = (b1 * c2 - b2 * c1) * dInv;
  209. a2 = a; b2 = b; c2 = c - max_distance_below;
  210. det = a1 * b2 - a2 * b1;
  211. dInv = 1.0/det;
  212. intercept_2 = (b1 * c2 - b2 * c1) * dInv;
  213. left_intercept = Math.min(intercept_1, intercept_2);
  214. right_intercept = Math.max(intercept_1, intercept_2);
  215. error = right_intercept - left_intercept;
  216. return (error < flatnessTolerance)? 1 : 0;
  217. };
  218. var _computeXIntercept = function(curve, degree) {
  219. var XLK = 1.0, YLK = 0.0,
  220. XNM = curve[degree].x - curve[0].x, YNM = curve[degree].y - curve[0].y,
  221. XMK = curve[0].x - 0.0, YMK = curve[0].y - 0.0,
  222. det = XNM*YLK - YNM*XLK, detInv = 1.0/det,
  223. S = (XNM*YMK - YNM*XMK) * detInv;
  224. return 0.0 + XLK * S;
  225. };
  226. var _bezier = function(curve, degree, t, left, right) {
  227. var temp = [[]];
  228. for (var j =0; j <= degree; j++) temp[0][j] = curve[j];
  229. for (var i = 1; i <= degree; i++) {
  230. for (var j =0 ; j <= degree - i; j++) {
  231. if (!temp[i]) temp[i] = [];
  232. if (!temp[i][j]) temp[i][j] = {};
  233. temp[i][j].x = (1.0 - t) * temp[i-1][j].x + t * temp[i-1][j+1].x;
  234. temp[i][j].y = (1.0 - t) * temp[i-1][j].y + t * temp[i-1][j+1].y;
  235. }
  236. }
  237. if (left != null)
  238. for (j = 0; j <= degree; j++) left[j] = temp[j][0];
  239. if (right != null)
  240. for (j = 0; j <= degree; j++) right[j] = temp[degree-j][j];
  241. return (temp[degree][0]);
  242. };
  243. var _curveFunctionCache = {};
  244. var _getCurveFunctions = function(order) {
  245. var fns = _curveFunctionCache[order];
  246. if (!fns) {
  247. fns = [];
  248. var f_term = function() { return function(t) { return Math.pow(t, order); }; },
  249. l_term = function() { return function(t) { return Math.pow((1-t), order); }; },
  250. c_term = function(c) { return function(t) { return c; }; },
  251. t_term = function() { return function(t) { return t; }; },
  252. one_minus_t_term = function() { return function(t) { return 1-t; }; },
  253. _termFunc = function(terms) {
  254. return function(t) {
  255. var p = 1;
  256. for (var i = 0; i < terms.length; i++) p = p * terms[i](t);
  257. return p;
  258. };
  259. };
  260. fns.push(new f_term()); // first is t to the power of the curve order
  261. for (var i = 1; i < order; i++) {
  262. var terms = [new c_term(order)];
  263. for (var j = 0 ; j < (order - i); j++) terms.push(new t_term());
  264. for (var j = 0 ; j < i; j++) terms.push(new one_minus_t_term());
  265. fns.push(new _termFunc(terms));
  266. }
  267. fns.push(new l_term()); // last is (1-t) to the power of the curve order
  268. _curveFunctionCache[order] = fns;
  269. }
  270. return fns;
  271. };
  272. /**
  273. * calculates a point on the curve, for a Bezier of arbitrary order.
  274. * @param curve an array of control points, eg [{x:10,y:20}, {x:50,y:50}, {x:100,y:100}, {x:120,y:100}]. For a cubic bezier this should have four points.
  275. * @param location a decimal indicating the distance along the curve the point should be located at. this is the distance along the curve as it travels, taking the way it bends into account. should be a number from 0 to 1, inclusive.
  276. */
  277. var _pointOnPath = function(curve, location) {
  278. var cc = _getCurveFunctions(curve.length - 1),
  279. _x = 0, _y = 0;
  280. for (var i = 0; i < curve.length ; i++) {
  281. _x = _x + (curve[i].x * cc[i](location));
  282. _y = _y + (curve[i].y * cc[i](location));
  283. }
  284. return {x:_x, y:_y};
  285. };
  286. var _dist = function(p1,p2) {
  287. return Math.sqrt(Math.pow(p1.x - p2.x, 2) + Math.pow(p1.y - p2.y, 2));
  288. };
  289. var _isPoint = function(curve) {
  290. return curve[0].x === curve[1].x && curve[0].y === curve[1].y;
  291. };
  292. /**
  293. * finds the point that is 'distance' along the path from 'location'. this method returns both the x,y location of the point and also
  294. * its 'location' (proportion of travel along the path); the method below - _pointAlongPathFrom - calls this method and just returns the
  295. * point.
  296. */
  297. var _pointAlongPath = function(curve, location, distance) {
  298. if (_isPoint(curve)) {
  299. return {
  300. point:curve[0],
  301. location:location
  302. };
  303. }
  304. var prev = _pointOnPath(curve, location),
  305. tally = 0,
  306. curLoc = location,
  307. direction = distance > 0 ? 1 : -1,
  308. cur = null;
  309. while (tally < Math.abs(distance)) {
  310. curLoc += (0.005 * direction);
  311. cur = _pointOnPath(curve, curLoc);
  312. tally += _dist(cur, prev);
  313. prev = cur;
  314. }
  315. return {point:cur, location:curLoc};
  316. };
  317. var _length = function(curve) {
  318. var d = new Date().getTime();
  319. if (_isPoint(curve)) return 0;
  320. var prev = _pointOnPath(curve, 0),
  321. tally = 0,
  322. curLoc = 0,
  323. direction = 1,
  324. cur = null;
  325. while (curLoc < 1) {
  326. curLoc += (0.005 * direction);
  327. cur = _pointOnPath(curve, curLoc);
  328. tally += _dist(cur, prev);
  329. prev = cur;
  330. }
  331. console.log("length", new Date().getTime() - d);
  332. return tally;
  333. };
  334. /**
  335. * finds the point that is 'distance' along the path from 'location'.
  336. */
  337. var _pointAlongPathFrom = function(curve, location, distance) {
  338. return _pointAlongPath(curve, location, distance).point;
  339. };
  340. /**
  341. * finds the location that is 'distance' along the path from 'location'.
  342. */
  343. var _locationAlongPathFrom = function(curve, location, distance) {
  344. return _pointAlongPath(curve, location, distance).location;
  345. };
  346. /**
  347. * returns the gradient of the curve at the given location, which is a decimal between 0 and 1 inclusive.
  348. *
  349. * thanks // http://bimixual.org/AnimationLibrary/beziertangents.html
  350. */
  351. var _gradientAtPoint = function(curve, location) {
  352. var p1 = _pointOnPath(curve, location),
  353. p2 = _pointOnPath(curve.slice(0, curve.length - 1), location),
  354. dy = p2.y - p1.y, dx = p2.x - p1.x;
  355. return dy === 0 ? Infinity : Math.atan(dy / dx);
  356. };
  357. /**
  358. returns the gradient of the curve at the point which is 'distance' from the given location.
  359. if this point is greater than location 1, the gradient at location 1 is returned.
  360. if this point is less than location 0, the gradient at location 0 is returned.
  361. */
  362. var _gradientAtPointAlongPathFrom = function(curve, location, distance) {
  363. var p = _pointAlongPath(curve, location, distance);
  364. if (p.location > 1) p.location = 1;
  365. if (p.location < 0) p.location = 0;
  366. return _gradientAtPoint(curve, p.location);
  367. };
  368. /**
  369. * calculates a line that is 'length' pixels long, perpendicular to, and centered on, the path at 'distance' pixels from the given location.
  370. * if distance is not supplied, the perpendicular for the given location is computed (ie. we set distance to zero).
  371. */
  372. var _perpendicularToPathAt = function(curve, location, length, distance) {
  373. distance = distance == null ? 0 : distance;
  374. var p = _pointAlongPath(curve, location, distance),
  375. m = _gradientAtPoint(curve, p.location),
  376. _theta2 = Math.atan(-1 / m),
  377. y = length / 2 * Math.sin(_theta2),
  378. x = length / 2 * Math.cos(_theta2);
  379. return [{x:p.point.x + x, y:p.point.y + y}, {x:p.point.x - x, y:p.point.y - y}];
  380. };
  381. /**
  382. * Calculates all intersections of the given line with the given curve.
  383. * @param x1
  384. * @param y1
  385. * @param x2
  386. * @param y2
  387. * @param curve
  388. * @returns {Array}
  389. */
  390. var _lineIntersection = function(x1, y1, x2, y2, curve) {
  391. var a = y2 - y1,
  392. b = x1 - x2,
  393. c = (x1 * (y1 - y2)) + (y1 * (x2-x1)),
  394. coeffs = _computeCoefficients(curve),
  395. p = [
  396. (a*coeffs[0][0]) + (b * coeffs[1][0]),
  397. (a*coeffs[0][1])+(b*coeffs[1][1]),
  398. (a*coeffs[0][2])+(b*coeffs[1][2]),
  399. (a*coeffs[0][3])+(b*coeffs[1][3]) + c
  400. ],
  401. r = _cubicRoots.apply(null, p),
  402. intersections = [];
  403. if (r != null) {
  404. for (var i = 0; i < 3; i++) {
  405. var t = r[i],
  406. t2 = Math.pow(t, 2),
  407. t3 = Math.pow(t, 3),
  408. x = [
  409. (coeffs[0][0] * t3) + (coeffs[0][1] * t2) + (coeffs[0][2] * t) + coeffs[0][3],
  410. (coeffs[1][0] * t3) + (coeffs[1][1] * t2) + (coeffs[1][2] * t) + coeffs[1][3]
  411. ];
  412. // check bounds of the line
  413. var s;
  414. if ((x2 - x1) !== 0) {
  415. s = (x[0] - x1) / (x2 - x1);
  416. }
  417. else {
  418. s = (x[1] - y1) / (y2 - y1);
  419. }
  420. if (t >= 0 && t <= 1.0 && s >= 0 && s <= 1.0) {
  421. intersections.push(x);
  422. }
  423. }
  424. }
  425. return intersections;
  426. };
  427. /**
  428. * Calculates all intersections of the given box with the given curve.
  429. * @param x X position of top left corner of box
  430. * @param y Y position of top left corner of box
  431. * @param w width of box
  432. * @param h height of box
  433. * @param curve
  434. * @returns {Array}
  435. */
  436. var _boxIntersection = function(x, y, w, h, curve) {
  437. var i = [];
  438. i.push.apply(i, _lineIntersection(x, y, x + w, y, curve));
  439. i.push.apply(i, _lineIntersection(x + w, y, x + w, y + h, curve));
  440. i.push.apply(i, _lineIntersection(x + w, y + h, x, y + h, curve));
  441. i.push.apply(i, _lineIntersection(x, y + h, x, y, curve));
  442. return i;
  443. };
  444. /**
  445. * Calculates all intersections of the given bounding box with the given curve.
  446. * @param boundingBox Bounding box, in { x:.., y:..., w:..., h:... } format.
  447. * @param curve
  448. * @returns {Array}
  449. */
  450. var _boundingBoxIntersection = function(boundingBox, curve) {
  451. var i = [];
  452. i.push.apply(i, _lineIntersection(boundingBox.x, boundingBox.y, boundingBox.x + boundingBox.w, boundingBox.y, curve));
  453. i.push.apply(i, _lineIntersection(boundingBox.x + boundingBox.w, boundingBox.y, boundingBox.x + boundingBox.w, boundingBox.y + boundingBox.h, curve));
  454. i.push.apply(i, _lineIntersection(boundingBox.x + boundingBox.w, boundingBox.y + boundingBox.h, boundingBox.x, boundingBox.y + boundingBox.h, curve));
  455. i.push.apply(i, _lineIntersection(boundingBox.x, boundingBox.y + boundingBox.h, boundingBox.x, boundingBox.y, curve));
  456. return i;
  457. };
  458. function _computeCoefficientsForAxis(curve, axis) {
  459. return [
  460. -(curve[0][axis]) + (3*curve[1][axis]) + (-3 * curve[2][axis]) + curve[3][axis],
  461. (3*(curve[0][axis])) - (6*(curve[1][axis])) + (3*(curve[2][axis])),
  462. -3*curve[0][axis] + 3*curve[1][axis],
  463. curve[0][axis]
  464. ];
  465. }
  466. function _computeCoefficients(curve)
  467. {
  468. return [
  469. _computeCoefficientsForAxis(curve, "x"),
  470. _computeCoefficientsForAxis(curve, "y")
  471. ];
  472. }
  473. function sgn(x) {
  474. return x < 0 ? -1 : x > 0 ? 1 : 0;
  475. }
  476. function _cubicRoots(a, b, c, d) {
  477. var A = b / a,
  478. B = c / a,
  479. C = d / a,
  480. Q = (3*B - Math.pow(A, 2))/9,
  481. R = (9*A*B - 27*C - 2*Math.pow(A, 3))/54,
  482. D = Math.pow(Q, 3) + Math.pow(R, 2),
  483. S,
  484. T,
  485. t = [];
  486. if (D >= 0) // complex or duplicate roots
  487. {
  488. S = sgn(R + Math.sqrt(D))*Math.pow(Math.abs(R + Math.sqrt(D)),(1/3));
  489. T = sgn(R - Math.sqrt(D))*Math.pow(Math.abs(R - Math.sqrt(D)),(1/3));
  490. t[0] = -A/3 + (S + T);
  491. t[1] = -A/3 - (S + T)/2;
  492. t[2] = -A/3 - (S + T)/2;
  493. /*discard complex roots*/
  494. if (Math.abs(Math.sqrt(3)*(S - T)/2) !== 0) {
  495. t[1] = -1;
  496. t[2] = -1;
  497. }
  498. }
  499. else // distinct real roots
  500. {
  501. var th = Math.acos(R/Math.sqrt(-Math.pow(Q, 3)));
  502. t[0] = 2*Math.sqrt(-Q)*Math.cos(th/3) - A/3;
  503. t[1] = 2*Math.sqrt(-Q)*Math.cos((th + 2*Math.PI)/3) - A/3;
  504. t[2] = 2*Math.sqrt(-Q)*Math.cos((th + 4*Math.PI)/3) - A/3;
  505. }
  506. // discard out of spec roots
  507. for (var i = 0; i < 3; i++) {
  508. if (t[i] < 0 || t[i] > 1.0) {
  509. t[i] = -1;
  510. }
  511. }
  512. return t;
  513. }
  514. var jsBezier = this.jsBezier = {
  515. distanceFromCurve : _distanceFromCurve,
  516. gradientAtPoint : _gradientAtPoint,
  517. gradientAtPointAlongCurveFrom : _gradientAtPointAlongPathFrom,
  518. nearestPointOnCurve : _nearestPointOnCurve,
  519. pointOnCurve : _pointOnPath,
  520. pointAlongCurveFrom : _pointAlongPathFrom,
  521. perpendicularToCurveAt : _perpendicularToPathAt,
  522. locationAlongCurveFrom:_locationAlongPathFrom,
  523. getLength:_length,
  524. lineIntersection:_lineIntersection,
  525. boxIntersection:_boxIntersection,
  526. boundingBoxIntersection:_boundingBoxIntersection,
  527. version:"0.9.0"
  528. };
  529. if (typeof exports !== "undefined") {
  530. exports.jsBezier = jsBezier;
  531. }
  532. }).call(typeof window !== 'undefined' ? window : this);
  533. /**
  534. * Biltong v0.4.0
  535. *
  536. * Various geometry functions written as part of jsPlumb and perhaps useful for others.
  537. *
  538. * Copyright (c) 2017 jsPlumb
  539. * https://jsplumbtoolkit.com
  540. *
  541. * Permission is hereby granted, free of charge, to any person
  542. * obtaining a copy of this software and associated documentation
  543. * files (the "Software"), to deal in the Software without
  544. * restriction, including without limitation the rights to use,
  545. * copy, modify, merge, publish, distribute, sublicense, and/or sell
  546. * copies of the Software, and to permit persons to whom the
  547. * Software is furnished to do so, subject to the following
  548. * conditions:
  549. *
  550. * The above copyright notice and this permission notice shall be
  551. * included in all copies or substantial portions of the Software.
  552. *
  553. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  554. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  555. * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  556. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  557. * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  558. * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  559. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  560. * OTHER DEALINGS IN THE SOFTWARE.
  561. */
  562. ;(function() {
  563. "use strict";
  564. var root = this;
  565. var Biltong = root.Biltong = {
  566. version:"0.4.0"
  567. };
  568. if (typeof exports !== "undefined") {
  569. exports.Biltong = Biltong;
  570. }
  571. var _isa = function(a) { return Object.prototype.toString.call(a) === "[object Array]"; },
  572. _pointHelper = function(p1, p2, fn) {
  573. p1 = _isa(p1) ? p1 : [p1.x, p1.y];
  574. p2 = _isa(p2) ? p2 : [p2.x, p2.y];
  575. return fn(p1, p2);
  576. },
  577. /**
  578. * @name Biltong.gradient
  579. * @function
  580. * @desc Calculates the gradient of a line between the two points.
  581. * @param {Point} p1 First point, either as a 2 entry array or object with `left` and `top` properties.
  582. * @param {Point} p2 Second point, either as a 2 entry array or object with `left` and `top` properties.
  583. * @return {Float} The gradient of a line between the two points.
  584. */
  585. _gradient = Biltong.gradient = function(p1, p2) {
  586. return _pointHelper(p1, p2, function(_p1, _p2) {
  587. if (_p2[0] == _p1[0])
  588. return _p2[1] > _p1[1] ? Infinity : -Infinity;
  589. else if (_p2[1] == _p1[1])
  590. return _p2[0] > _p1[0] ? 0 : -0;
  591. else
  592. return (_p2[1] - _p1[1]) / (_p2[0] - _p1[0]);
  593. });
  594. },
  595. /**
  596. * @name Biltong.normal
  597. * @function
  598. * @desc Calculates the gradient of a normal to a line between the two points.
  599. * @param {Point} p1 First point, either as a 2 entry array or object with `left` and `top` properties.
  600. * @param {Point} p2 Second point, either as a 2 entry array or object with `left` and `top` properties.
  601. * @return {Float} The gradient of a normal to a line between the two points.
  602. */
  603. _normal = Biltong.normal = function(p1, p2) {
  604. return -1 / _gradient(p1, p2);
  605. },
  606. /**
  607. * @name Biltong.lineLength
  608. * @function
  609. * @desc Calculates the length of a line between the two points.
  610. * @param {Point} p1 First point, either as a 2 entry array or object with `left` and `top` properties.
  611. * @param {Point} p2 Second point, either as a 2 entry array or object with `left` and `top` properties.
  612. * @return {Float} The length of a line between the two points.
  613. */
  614. _lineLength = Biltong.lineLength = function(p1, p2) {
  615. return _pointHelper(p1, p2, function(_p1, _p2) {
  616. return Math.sqrt(Math.pow(_p2[1] - _p1[1], 2) + Math.pow(_p2[0] - _p1[0], 2));
  617. });
  618. },
  619. /**
  620. * @name Biltong.quadrant
  621. * @function
  622. * @desc Calculates the quadrant in which the angle between the two points lies.
  623. * @param {Point} p1 First point, either as a 2 entry array or object with `left` and `top` properties.
  624. * @param {Point} p2 Second point, either as a 2 entry array or object with `left` and `top` properties.
  625. * @return {Integer} The quadrant - 1 for upper right, 2 for lower right, 3 for lower left, 4 for upper left.
  626. */
  627. _quadrant = Biltong.quadrant = function(p1, p2) {
  628. return _pointHelper(p1, p2, function(_p1, _p2) {
  629. if (_p2[0] > _p1[0]) {
  630. return (_p2[1] > _p1[1]) ? 2 : 1;
  631. }
  632. else if (_p2[0] == _p1[0]) {
  633. return _p2[1] > _p1[1] ? 2 : 1;
  634. }
  635. else {
  636. return (_p2[1] > _p1[1]) ? 3 : 4;
  637. }
  638. });
  639. },
  640. /**
  641. * @name Biltong.theta
  642. * @function
  643. * @desc Calculates the angle between the two points.
  644. * @param {Point} p1 First point, either as a 2 entry array or object with `left` and `top` properties.
  645. * @param {Point} p2 Second point, either as a 2 entry array or object with `left` and `top` properties.
  646. * @return {Float} The angle between the two points.
  647. */
  648. _theta = Biltong.theta = function(p1, p2) {
  649. return _pointHelper(p1, p2, function(_p1, _p2) {
  650. var m = _gradient(_p1, _p2),
  651. t = Math.atan(m),
  652. s = _quadrant(_p1, _p2);
  653. if ((s == 4 || s== 3)) t += Math.PI;
  654. if (t < 0) t += (2 * Math.PI);
  655. return t;
  656. });
  657. },
  658. /**
  659. * @name Biltong.intersects
  660. * @function
  661. * @desc Calculates whether or not the two rectangles intersect.
  662. * @param {Rectangle} r1 First rectangle, as a js object in the form `{x:.., y:.., w:.., h:..}`
  663. * @param {Rectangle} r2 Second rectangle, as a js object in the form `{x:.., y:.., w:.., h:..}`
  664. * @return {Boolean} True if the rectangles intersect, false otherwise.
  665. */
  666. _intersects = Biltong.intersects = function(r1, r2) {
  667. var x1 = r1.x, x2 = r1.x + r1.w, y1 = r1.y, y2 = r1.y + r1.h,
  668. a1 = r2.x, a2 = r2.x + r2.w, b1 = r2.y, b2 = r2.y + r2.h;
  669. return ( (x1 <= a1 && a1 <= x2) && (y1 <= b1 && b1 <= y2) ) ||
  670. ( (x1 <= a2 && a2 <= x2) && (y1 <= b1 && b1 <= y2) ) ||
  671. ( (x1 <= a1 && a1 <= x2) && (y1 <= b2 && b2 <= y2) ) ||
  672. ( (x1 <= a2 && a1 <= x2) && (y1 <= b2 && b2 <= y2) ) ||
  673. ( (a1 <= x1 && x1 <= a2) && (b1 <= y1 && y1 <= b2) ) ||
  674. ( (a1 <= x2 && x2 <= a2) && (b1 <= y1 && y1 <= b2) ) ||
  675. ( (a1 <= x1 && x1 <= a2) && (b1 <= y2 && y2 <= b2) ) ||
  676. ( (a1 <= x2 && x1 <= a2) && (b1 <= y2 && y2 <= b2) );
  677. },
  678. /**
  679. * @name Biltong.encloses
  680. * @function
  681. * @desc Calculates whether or not r2 is completely enclosed by r1.
  682. * @param {Rectangle} r1 First rectangle, as a js object in the form `{x:.., y:.., w:.., h:..}`
  683. * @param {Rectangle} r2 Second rectangle, as a js object in the form `{x:.., y:.., w:.., h:..}`
  684. * @param {Boolean} [allowSharedEdges=false] If true, the concept of enclosure allows for one or more edges to be shared by the two rectangles.
  685. * @return {Boolean} True if r1 encloses r2, false otherwise.
  686. */
  687. _encloses = Biltong.encloses = function(r1, r2, allowSharedEdges) {
  688. var x1 = r1.x, x2 = r1.x + r1.w, y1 = r1.y, y2 = r1.y + r1.h,
  689. a1 = r2.x, a2 = r2.x + r2.w, b1 = r2.y, b2 = r2.y + r2.h,
  690. c = function(v1, v2, v3, v4) { return allowSharedEdges ? v1 <= v2 && v3>= v4 : v1 < v2 && v3 > v4; };
  691. return c(x1,a1,x2,a2) && c(y1,b1,y2,b2);
  692. },
  693. _segmentMultipliers = [null, [1, -1], [1, 1], [-1, 1], [-1, -1] ],
  694. _inverseSegmentMultipliers = [null, [-1, -1], [-1, 1], [1, 1], [1, -1] ],
  695. /**
  696. * @name Biltong.pointOnLine
  697. * @function
  698. * @desc Calculates a point on the line from `fromPoint` to `toPoint` that is `distance` units along the length of the line.
  699. * @param {Point} p1 First point, either as a 2 entry array or object with `left` and `top` properties.
  700. * @param {Point} p2 Second point, either as a 2 entry array or object with `left` and `top` properties.
  701. * @return {Point} Point on the line, in the form `{ x:..., y:... }`.
  702. */
  703. _pointOnLine = Biltong.pointOnLine = function(fromPoint, toPoint, distance) {
  704. var m = _gradient(fromPoint, toPoint),
  705. s = _quadrant(fromPoint, toPoint),
  706. segmentMultiplier = distance > 0 ? _segmentMultipliers[s] : _inverseSegmentMultipliers[s],
  707. theta = Math.atan(m),
  708. y = Math.abs(distance * Math.sin(theta)) * segmentMultiplier[1],
  709. x = Math.abs(distance * Math.cos(theta)) * segmentMultiplier[0];
  710. return { x:fromPoint.x + x, y:fromPoint.y + y };
  711. },
  712. /**
  713. * @name Biltong.perpendicularLineTo
  714. * @function
  715. * @desc Calculates a line of length `length` that is perpendicular to the line from `fromPoint` to `toPoint` and passes through `toPoint`.
  716. * @param {Point} p1 First point, either as a 2 entry array or object with `left` and `top` properties.
  717. * @param {Point} p2 Second point, either as a 2 entry array or object with `left` and `top` properties.
  718. * @return {Line} Perpendicular line, in the form `[ { x:..., y:... }, { x:..., y:... } ]`.
  719. */
  720. _perpendicularLineTo = Biltong.perpendicularLineTo = function(fromPoint, toPoint, length) {
  721. var m = _gradient(fromPoint, toPoint),
  722. theta2 = Math.atan(-1 / m),
  723. y = length / 2 * Math.sin(theta2),
  724. x = length / 2 * Math.cos(theta2);
  725. return [{x:toPoint.x + x, y:toPoint.y + y}, {x:toPoint.x - x, y:toPoint.y - y}];
  726. };
  727. }).call(typeof window !== 'undefined' ? window : this);
  728. ;
  729. (function () {
  730. "use strict";
  731. /**
  732. * Creates a Touch object.
  733. * @param view
  734. * @param target
  735. * @param pageX
  736. * @param pageY
  737. * @param screenX
  738. * @param screenY
  739. * @param clientX
  740. * @param clientY
  741. * @returns {Touch}
  742. * @private
  743. */
  744. function _touch(view, target, pageX, pageY, screenX, screenY, clientX, clientY) {
  745. return new Touch({
  746. target:target,
  747. identifier:_uuid(),
  748. pageX: pageX,
  749. pageY: pageY,
  750. screenX: screenX,
  751. screenY: screenY,
  752. clientX: clientX || screenX,
  753. clientY: clientY || screenY
  754. });
  755. }
  756. /**
  757. * Create a synthetic touch list from the given list of Touch objects.
  758. * @returns {Array}
  759. * @private
  760. */
  761. function _touchList() {
  762. var list = [];
  763. Array.prototype.push.apply(list, arguments);
  764. list.item = function(index) { return this[index]; };
  765. return list;
  766. }
  767. /**
  768. * Create a Touch object and then insert it into a synthetic touch list, returning the list.s
  769. * @param view
  770. * @param target
  771. * @param pageX
  772. * @param pageY
  773. * @param screenX
  774. * @param screenY
  775. * @param clientX
  776. * @param clientY
  777. * @returns {Array}
  778. * @private
  779. */
  780. function _touchAndList(view, target, pageX, pageY, screenX, screenY, clientX, clientY) {
  781. return _touchList(_touch.apply(null, arguments));
  782. }
  783. var root = this,
  784. matchesSelector = function (el, selector, ctx) {
  785. ctx = ctx || el.parentNode;
  786. var possibles = ctx.querySelectorAll(selector);
  787. for (var i = 0; i < possibles.length; i++) {
  788. if (possibles[i] === el) {
  789. return true;
  790. }
  791. }
  792. return false;
  793. },
  794. _gel = function (el) {
  795. return (typeof el == "string" || el.constructor === String) ? document.getElementById(el) : el;
  796. },
  797. _t = function (e) {
  798. return e.srcElement || e.target;
  799. },
  800. //
  801. // gets path info for the given event - the path from target to obj, in the event's bubble chain. if doCompute
  802. // is false we just return target for the path.
  803. //
  804. _pi = function(e, target, obj, doCompute) {
  805. if (!doCompute) return { path:[target], end:1 };
  806. else if (typeof e.path !== "undefined" && e.path.indexOf) {
  807. return { path: e.path, end: e.path.indexOf(obj) };
  808. } else {
  809. var out = { path:[], end:-1 }, _one = function(el) {
  810. out.path.push(el);
  811. if (el === obj) {
  812. out.end = out.path.length - 1;
  813. }
  814. else if (el.parentNode != null) {
  815. _one(el.parentNode)
  816. }
  817. };
  818. _one(target);
  819. return out;
  820. }
  821. },
  822. _d = function (l, fn) {
  823. for (var i = 0, j = l.length; i < j; i++) {
  824. if (l[i] == fn) break;
  825. }
  826. if (i < l.length) l.splice(i, 1);
  827. },
  828. guid = 1,
  829. //
  830. // this function generates a guid for every handler, sets it on the handler, then adds
  831. // it to the associated object's map of handlers for the given event. this is what enables us
  832. // to unbind all events of some type, or all events (the second of which can be requested by the user,
  833. // but it also used by Mottle when an element is removed.)
  834. _store = function (obj, event, fn) {
  835. var g = guid++;
  836. obj.__ta = obj.__ta || {};
  837. obj.__ta[event] = obj.__ta[event] || {};
  838. // store each handler with a unique guid.
  839. obj.__ta[event][g] = fn;
  840. // set the guid on the handler.
  841. fn.__tauid = g;
  842. return g;
  843. },
  844. _unstore = function (obj, event, fn) {
  845. obj.__ta && obj.__ta[event] && delete obj.__ta[event][fn.__tauid];
  846. // a handler might have attached extra functions, so we unbind those too.
  847. if (fn.__taExtra) {
  848. for (var i = 0; i < fn.__taExtra.length; i++) {
  849. _unbind(obj, fn.__taExtra[i][0], fn.__taExtra[i][1]);
  850. }
  851. fn.__taExtra.length = 0;
  852. }
  853. // a handler might have attached an unstore callback
  854. fn.__taUnstore && fn.__taUnstore();
  855. },
  856. _curryChildFilter = function (children, obj, fn, evt) {
  857. if (children == null) return fn;
  858. else {
  859. var c = children.split(","),
  860. _fn = function (e) {
  861. _fn.__tauid = fn.__tauid;
  862. var t = _t(e), target = t; // t is the target element on which the event occurred. it is the
  863. // element we will wish to pass to any callbacks.
  864. var pathInfo = _pi(e, t, obj, children != null)
  865. if (pathInfo.end != -1) {
  866. for (var p = 0; p < pathInfo.end; p++) {
  867. target = pathInfo.path[p];
  868. for (var i = 0; i < c.length; i++) {
  869. if (matchesSelector(target, c[i], obj)) {
  870. fn.apply(target, arguments);
  871. }
  872. }
  873. }
  874. }
  875. };
  876. registerExtraFunction(fn, evt, _fn);
  877. return _fn;
  878. }
  879. },
  880. //
  881. // registers an 'extra' function on some event listener function we were given - a function that we
  882. // created and bound to the element as part of our housekeeping, and which we want to unbind and remove
  883. // whenever the given function is unbound.
  884. registerExtraFunction = function (fn, evt, newFn) {
  885. fn.__taExtra = fn.__taExtra || [];
  886. fn.__taExtra.push([evt, newFn]);
  887. },
  888. DefaultHandler = function (obj, evt, fn, children) {
  889. if (isTouchDevice && touchMap[evt]) {
  890. var tfn = _curryChildFilter(children, obj, fn, touchMap[evt]);
  891. _bind(obj, touchMap[evt], tfn , fn);
  892. }
  893. if (evt === "focus" && obj.getAttribute("tabindex") == null) {
  894. obj.setAttribute("tabindex", "1");
  895. }
  896. _bind(obj, evt, _curryChildFilter(children, obj, fn, evt), fn);
  897. },
  898. SmartClickHandler = function (obj, evt, fn, children) {
  899. if (obj.__taSmartClicks == null) {
  900. var down = function (e) {
  901. obj.__tad = _pageLocation(e);
  902. },
  903. up = function (e) {
  904. obj.__tau = _pageLocation(e);
  905. },
  906. click = function (e) {
  907. if (obj.__tad && obj.__tau && obj.__tad[0] === obj.__tau[0] && obj.__tad[1] === obj.__tau[1]) {
  908. for (var i = 0; i < obj.__taSmartClicks.length; i++)
  909. obj.__taSmartClicks[i].apply(_t(e), [ e ]);
  910. }
  911. };
  912. DefaultHandler(obj, "mousedown", down, children);
  913. DefaultHandler(obj, "mouseup", up, children);
  914. DefaultHandler(obj, "click", click, children);
  915. obj.__taSmartClicks = [];
  916. }
  917. // store in the list of callbacks
  918. obj.__taSmartClicks.push(fn);
  919. // the unstore function removes this function from the object's listener list for this type.
  920. fn.__taUnstore = function () {
  921. _d(obj.__taSmartClicks, fn);
  922. };
  923. },
  924. _tapProfiles = {
  925. "tap": {touches: 1, taps: 1},
  926. "dbltap": {touches: 1, taps: 2},
  927. "contextmenu": {touches: 2, taps: 1}
  928. },
  929. TapHandler = function (clickThreshold, dblClickThreshold) {
  930. return function (obj, evt, fn, children) {
  931. // if event is contextmenu, for devices which are mouse only, we want to
  932. // use the default bind.
  933. if (evt == "contextmenu" && isMouseDevice)
  934. DefaultHandler(obj, evt, fn, children);
  935. else {
  936. // the issue here is that this down handler gets registered only for the
  937. // child nodes in the first registration. in fact it should be registered with
  938. // no child selector and then on down we should cycle through the registered
  939. // functions to see if one of them matches. on mouseup we should execute ALL of
  940. // the functions whose children are either null or match the element.
  941. if (obj.__taTapHandler == null) {
  942. var tt = obj.__taTapHandler = {
  943. tap: [],
  944. dbltap: [],
  945. contextmenu: [],
  946. down: false,
  947. taps: 0,
  948. downSelectors: []
  949. };
  950. var down = function (e) {
  951. var target = _t(e), pathInfo = _pi(e, target, obj, children != null), finished = false;
  952. for (var p = 0; p < pathInfo.end; p++) {
  953. if (finished) return;
  954. target = pathInfo.path[p];
  955. for (var i = 0; i < tt.downSelectors.length; i++) {
  956. if (tt.downSelectors[i] == null || matchesSelector(target, tt.downSelectors[i], obj)) {
  957. tt.down = true;
  958. setTimeout(clearSingle, clickThreshold);
  959. setTimeout(clearDouble, dblClickThreshold);
  960. finished = true;
  961. break; // we only need one match on mousedown
  962. }
  963. }
  964. }
  965. },
  966. up = function (e) {
  967. if (tt.down) {
  968. var target = _t(e), currentTarget, pathInfo;
  969. tt.taps++;
  970. var tc = _touchCount(e);
  971. for (var eventId in _tapProfiles) {
  972. if (_tapProfiles.hasOwnProperty(eventId)) {
  973. var p = _tapProfiles[eventId];
  974. if (p.touches === tc && (p.taps === 1 || p.taps === tt.taps)) {
  975. for (var i = 0; i < tt[eventId].length; i++) {
  976. pathInfo = _pi(e, target, obj, tt[eventId][i][1] != null);
  977. for (var pLoop = 0; pLoop < pathInfo.end; pLoop++) {
  978. currentTarget = pathInfo.path[pLoop];
  979. // this is a single event registration handler.
  980. if (tt[eventId][i][1] == null || matchesSelector(currentTarget, tt[eventId][i][1], obj)) {
  981. tt[eventId][i][0].apply(currentTarget, [ e ]);
  982. break;
  983. }
  984. }
  985. }
  986. }
  987. }
  988. }
  989. }
  990. },
  991. clearSingle = function () {
  992. tt.down = false;
  993. },
  994. clearDouble = function () {
  995. tt.taps = 0;
  996. };
  997. DefaultHandler(obj, "mousedown", down);
  998. DefaultHandler(obj, "mouseup", up);
  999. }
  1000. // add this child selector (it can be null, that's fine).
  1001. obj.__taTapHandler.downSelectors.push(children);
  1002. obj.__taTapHandler[evt].push([fn, children]);
  1003. // the unstore function removes this function from the object's listener list for this type.
  1004. fn.__taUnstore = function () {
  1005. _d(obj.__taTapHandler[evt], fn);
  1006. };
  1007. }
  1008. };
  1009. },
  1010. meeHelper = function (type, evt, obj, target) {
  1011. for (var i in obj.__tamee[type]) {
  1012. if (obj.__tamee[type].hasOwnProperty(i)) {
  1013. obj.__tamee[type][i].apply(target, [ evt ]);
  1014. }
  1015. }
  1016. },
  1017. MouseEnterExitHandler = function () {
  1018. var activeElements = [];
  1019. return function (obj, evt, fn, children) {
  1020. if (!obj.__tamee) {
  1021. // __tamee holds a flag saying whether the mouse is currently "in" the element, and a list of
  1022. // both mouseenter and mouseexit functions.
  1023. obj.__tamee = { over: false, mouseenter: [], mouseexit: [] };
  1024. // register over and out functions
  1025. var over = function (e) {
  1026. var t = _t(e);
  1027. if ((children == null && (t == obj && !obj.__tamee.over)) || (matchesSelector(t, children, obj) && (t.__tamee == null || !t.__tamee.over))) {
  1028. meeHelper("mouseenter", e, obj, t);
  1029. t.__tamee = t.__tamee || {};
  1030. t.__tamee.over = true;
  1031. activeElements.push(t);
  1032. }
  1033. },
  1034. out = function (e) {
  1035. var t = _t(e);
  1036. // is the current target one of the activeElements? and is the
  1037. // related target NOT a descendant of it?
  1038. for (var i = 0; i < activeElements.length; i++) {
  1039. if (t == activeElements[i] && !matchesSelector((e.relatedTarget || e.toElement), "*", t)) {
  1040. t.__tamee.over = false;
  1041. activeElements.splice(i, 1);
  1042. meeHelper("mouseexit", e, obj, t);
  1043. }
  1044. }
  1045. };
  1046. _bind(obj, "mouseover", _curryChildFilter(children, obj, over, "mouseover"), over);
  1047. _bind(obj, "mouseout", _curryChildFilter(children, obj, out, "mouseout"), out);
  1048. }
  1049. fn.__taUnstore = function () {
  1050. delete obj.__tamee[evt][fn.__tauid];
  1051. };
  1052. _store(obj, evt, fn);
  1053. obj.__tamee[evt][fn.__tauid] = fn;
  1054. };
  1055. },
  1056. isTouchDevice = "ontouchstart" in document.documentElement || navigator.maxTouchPoints,
  1057. isMouseDevice = "onmousedown" in document.documentElement,
  1058. touchMap = { "mousedown": "touchstart", "mouseup": "touchend", "mousemove": "touchmove" },
  1059. touchstart = "touchstart", touchend = "touchend", touchmove = "touchmove",
  1060. iev = (function () {
  1061. var rv = -1;
  1062. if (navigator.appName == 'Microsoft Internet Explorer') {
  1063. var ua = navigator.userAgent,
  1064. re = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
  1065. if (re.exec(ua) != null)
  1066. rv = parseFloat(RegExp.$1);
  1067. }
  1068. return rv;
  1069. })(),
  1070. isIELT9 = iev > -1 && iev < 9,
  1071. _genLoc = function (e, prefix) {
  1072. if (e == null) return [ 0, 0 ];
  1073. var ts = _touches(e), t = _getTouch(ts, 0);
  1074. return [t[prefix + "X"], t[prefix + "Y"]];
  1075. },
  1076. _pageLocation = function (e) {
  1077. if (e == null) return [ 0, 0 ];
  1078. if (isIELT9) {
  1079. return [ e.clientX + document.documentElement.scrollLeft, e.clientY + document.documentElement.scrollTop ];
  1080. }
  1081. else {
  1082. return _genLoc(e, "page");
  1083. }
  1084. },
  1085. _screenLocation = function (e) {
  1086. return _genLoc(e, "screen");
  1087. },
  1088. _clientLocation = function (e) {
  1089. return _genLoc(e, "client");
  1090. },
  1091. _getTouch = function (touches, idx) {
  1092. return touches.item ? touches.item(idx) : touches[idx];
  1093. },
  1094. _touches = function (e) {
  1095. return e.touches && e.touches.length > 0 ? e.touches :
  1096. e.changedTouches && e.changedTouches.length > 0 ? e.changedTouches :
  1097. e.targetTouches && e.targetTouches.length > 0 ? e.targetTouches :
  1098. [ e ];
  1099. },
  1100. _touchCount = function (e) {
  1101. return _touches(e).length;
  1102. },
  1103. //http://www.quirksmode.org/blog/archives/2005/10/_and_the_winner_1.html
  1104. _bind = function (obj, type, fn, originalFn) {
  1105. _store(obj, type, fn);
  1106. originalFn.__tauid = fn.__tauid;
  1107. if (obj.addEventListener)
  1108. obj.addEventListener(type, fn, false);
  1109. else if (obj.attachEvent) {
  1110. var key = type + fn.__tauid;
  1111. obj["e" + key] = fn;
  1112. // TODO look at replacing with .call(..)
  1113. obj[key] = function () {
  1114. obj["e" + key] && obj["e" + key](window.event);
  1115. };
  1116. obj.attachEvent("on" + type, obj[key]);
  1117. }
  1118. },
  1119. _unbind = function (obj, type, fn) {
  1120. if (fn == null) return;
  1121. _each(obj, function () {
  1122. var _el = _gel(this);
  1123. _unstore(_el, type, fn);
  1124. // it has been bound if there is a tauid. otherwise it was not bound and we can ignore it.
  1125. if (fn.__tauid != null) {
  1126. if (_el.removeEventListener) {
  1127. _el.removeEventListener(type, fn, false);
  1128. if (isTouchDevice && touchMap[type]) _el.removeEventListener(touchMap[type], fn, false);
  1129. }
  1130. else if (this.detachEvent) {
  1131. var key = type + fn.__tauid;
  1132. _el[key] && _el.detachEvent("on" + type, _el[key]);
  1133. _el[key] = null;
  1134. _el["e" + key] = null;
  1135. }
  1136. }
  1137. // if a touch event was also registered, deregister now.
  1138. if (fn.__taTouchProxy) {
  1139. _unbind(obj, fn.__taTouchProxy[1], fn.__taTouchProxy[0]);
  1140. }
  1141. });
  1142. },
  1143. _each = function (obj, fn) {
  1144. if (obj == null) return;
  1145. // if a list (or list-like), use it. if a string, get a list
  1146. // by running the string through querySelectorAll. else, assume
  1147. // it's an Element.
  1148. // obj.top is "unknown" in IE8.
  1149. obj = (typeof Window !== "undefined" && (typeof obj.top !== "unknown" && obj == obj.top)) ? [ obj ] :
  1150. (typeof obj !== "string") && (obj.tagName == null && obj.length != null) ? obj :
  1151. typeof obj === "string" ? document.querySelectorAll(obj)
  1152. : [ obj ];
  1153. for (var i = 0; i < obj.length; i++)
  1154. fn.apply(obj[i]);
  1155. },
  1156. _uuid = function () {
  1157. return ('xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
  1158. var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
  1159. return v.toString(16);
  1160. }));
  1161. };
  1162. /**
  1163. * Mottle offers support for abstracting out the differences
  1164. * between touch and mouse devices, plus "smart click" functionality
  1165. * (don't fire click if the mouse has moved between mousedown and mouseup),
  1166. * and synthesized click/tap events.
  1167. * @class Mottle
  1168. * @constructor
  1169. * @param {Object} params Constructor params
  1170. * @param {Number} [params.clickThreshold=250] Threshold, in milliseconds beyond which a touchstart followed by a touchend is not considered to be a click.
  1171. * @param {Number} [params.dblClickThreshold=450] Threshold, in milliseconds beyond which two successive tap events are not considered to be a click.
  1172. * @param {Boolean} [params.smartClicks=false] If true, won't fire click events if the mouse has moved between mousedown and mouseup. Note that this functionality
  1173. * requires that Mottle consume the mousedown event, and so may not be viable in all use cases.
  1174. */
  1175. root.Mottle = function (params) {
  1176. params = params || {};
  1177. var clickThreshold = params.clickThreshold || 250,
  1178. dblClickThreshold = params.dblClickThreshold || 450,
  1179. mouseEnterExitHandler = new MouseEnterExitHandler(),
  1180. tapHandler = new TapHandler(clickThreshold, dblClickThreshold),
  1181. _smartClicks = params.smartClicks,
  1182. _doBind = function (obj, evt, fn, children) {
  1183. if (fn == null) return;
  1184. _each(obj, function () {
  1185. var _el = _gel(this);
  1186. if (_smartClicks && evt === "click")
  1187. SmartClickHandler(_el, evt, fn, children);
  1188. else if (evt === "tap" || evt === "dbltap" || evt === "contextmenu") {
  1189. tapHandler(_el, evt, fn, children);
  1190. }
  1191. else if (evt === "mouseenter" || evt == "mouseexit")
  1192. mouseEnterExitHandler(_el, evt, fn, children);
  1193. else
  1194. DefaultHandler(_el, evt, fn, children);
  1195. });
  1196. };
  1197. /**
  1198. * Removes an element from the DOM, and deregisters all event handlers for it. You should use this
  1199. * to ensure you don't leak memory.
  1200. * @method remove
  1201. * @param {String|Element} el Element, or id of the element, to remove.
  1202. * @return {Mottle} The current Mottle instance; you can chain this method.
  1203. */
  1204. this.remove = function (el) {
  1205. _each(el, function () {
  1206. var _el = _gel(this);
  1207. if (_el.__ta) {
  1208. for (var evt in _el.__ta) {
  1209. if (_el.__ta.hasOwnProperty(evt)) {
  1210. for (var h in _el.__ta[evt]) {
  1211. if (_el.__ta[evt].hasOwnProperty(h))
  1212. _unbind(_el, evt, _el.__ta[evt][h]);
  1213. }
  1214. }
  1215. }
  1216. }
  1217. _el.parentNode && _el.parentNode.removeChild(_el);
  1218. });
  1219. return this;
  1220. };
  1221. /**
  1222. * Register an event handler, optionally as a delegate for some set of descendant elements. Note
  1223. * that this method takes either 3 or 4 arguments - if you supply 3 arguments it is assumed you have
  1224. * omitted the `children` parameter, and that the event handler should be bound directly to the given element.
  1225. * @method on
  1226. * @param {Element[]|Element|String} el Either an Element, or a CSS spec for a list of elements, or an array of Elements.
  1227. * @param {String} [children] Comma-delimited list of selectors identifying allowed children.
  1228. * @param {String} event Event ID.
  1229. * @param {Function} fn Event handler function.
  1230. * @return {Mottle} The current Mottle instance; you can chain this method.
  1231. */
  1232. this.on = function (el, event, children, fn) {
  1233. var _el = arguments[0],
  1234. _c = arguments.length == 4 ? arguments[2] : null,
  1235. _e = arguments[1],
  1236. _f = arguments[arguments.length - 1];
  1237. _doBind(_el, _e, _f, _c);
  1238. return this;
  1239. };
  1240. /**
  1241. * Cancel delegate event handling for the given function. Note that unlike with 'on' you do not supply
  1242. * a list of child selectors here: it removes event delegation from all of the child selectors for which the
  1243. * given function was registered (if any).
  1244. * @method off
  1245. * @param {Element[]|Element|String} el Element - or ID of element - from which to remove event listener.
  1246. * @param {String} event Event ID.
  1247. * @param {Function} fn Event handler function.
  1248. * @return {Mottle} The current Mottle instance; you can chain this method.
  1249. */
  1250. this.off = function (el, event, fn) {
  1251. _unbind(el, event, fn);
  1252. return this;
  1253. };
  1254. /**
  1255. * Triggers some event for a given element.
  1256. * @method trigger
  1257. * @param {Element} el Element for which to trigger the event.
  1258. * @param {String} event Event ID.
  1259. * @param {Event} originalEvent The original event. Should be optional of course, but currently is not, due
  1260. * to the jsPlumb use case that caused this method to be added.
  1261. * @param {Object} [payload] Optional object to set as `payload` on the generated event; useful for message passing.
  1262. * @return {Mottle} The current Mottle instance; you can chain this method.
  1263. */
  1264. this.trigger = function (el, event, originalEvent, payload) {
  1265. // MouseEvent undefined in old IE; that's how we know it's a mouse event. A fine Microsoft paradox.
  1266. var originalIsMouse = isMouseDevice && (typeof MouseEvent === "undefined" || originalEvent == null || originalEvent.constructor === MouseEvent);
  1267. var eventToBind = (isTouchDevice && !isMouseDevice && touchMap[event]) ? touchMap[event] : event,
  1268. bindingAMouseEvent = !(isTouchDevice && !isMouseDevice && touchMap[event]);
  1269. var pl = _pageLocation(originalEvent), sl = _screenLocation(originalEvent), cl = _clientLocation(originalEvent);
  1270. _each(el, function () {
  1271. var _el = _gel(this), evt;
  1272. originalEvent = originalEvent || {
  1273. screenX: sl[0],
  1274. screenY: sl[1],
  1275. clientX: cl[0],
  1276. clientY: cl[1]
  1277. };
  1278. var _decorate = function (_evt) {
  1279. if (payload) _evt.payload = payload;
  1280. };
  1281. var eventGenerators = {
  1282. "TouchEvent": function (evt) {
  1283. var touchList = _touchAndList(window, _el, 0, pl[0], pl[1], sl[0], sl[1], cl[0], cl[1]),
  1284. init = evt.initTouchEvent || evt.initEvent;
  1285. init(eventToBind, true, true, window, null, sl[0], sl[1],
  1286. cl[0], cl[1], false, false, false, false,
  1287. touchList, touchList, touchList, 1, 0);
  1288. },
  1289. "MouseEvents": function (evt) {
  1290. evt.initMouseEvent(eventToBind, true, true, window, 0,
  1291. sl[0], sl[1],
  1292. cl[0], cl[1],
  1293. false, false, false, false, 1, _el);
  1294. }
  1295. };
  1296. if (document.createEvent) {
  1297. var ite = !bindingAMouseEvent && !originalIsMouse && (isTouchDevice && touchMap[event]),
  1298. evtName = ite ? "TouchEvent" : "MouseEvents";
  1299. evt = document.createEvent(evtName);
  1300. eventGenerators[evtName](evt);
  1301. _decorate(evt);
  1302. _el.dispatchEvent(evt);
  1303. }
  1304. else if (document.createEventObject) {
  1305. evt = document.createEventObject();
  1306. evt.eventType = evt.eventName = eventToBind;
  1307. evt.screenX = sl[0];
  1308. evt.screenY = sl[1];
  1309. evt.clientX = cl[0];
  1310. evt.clientY = cl[1];
  1311. _decorate(evt);
  1312. _el.fireEvent('on' + eventToBind, evt);
  1313. }
  1314. });
  1315. return this;
  1316. }
  1317. };
  1318. /**
  1319. * Static method to assist in 'consuming' an element: uses `stopPropagation` where available, or sets
  1320. * `e.returnValue=false` where it is not.
  1321. * @method Mottle.consume
  1322. * @param {Event} e Event to consume
  1323. * @param {Boolean} [doNotPreventDefault=false] If true, does not call `preventDefault()` on the event.
  1324. */
  1325. root.Mottle.consume = function (e, doNotPreventDefault) {
  1326. if (e.stopPropagation)
  1327. e.stopPropagation();
  1328. else
  1329. e.returnValue = false;
  1330. if (!doNotPreventDefault && e.preventDefault)
  1331. e.preventDefault();
  1332. };
  1333. /**
  1334. * Gets the page location corresponding to the given event. For touch events this means get the page location of the first touch.
  1335. * @method Mottle.pageLocation
  1336. * @param {Event} e Event to get page location for.
  1337. * @return {Number[]} [left, top] for the given event.
  1338. */
  1339. root.Mottle.pageLocation = _pageLocation;
  1340. /**
  1341. * Forces touch events to be turned "on". Useful for testing: even if you don't have a touch device, you can still
  1342. * trigger a touch event when this is switched on and it will be captured and acted on.
  1343. * @method setForceTouchEvents
  1344. * @param {Boolean} value If true, force touch events to be on.
  1345. */
  1346. root.Mottle.setForceTouchEvents = function (value) {
  1347. isTouchDevice = value;
  1348. };
  1349. /**
  1350. * Forces mouse events to be turned "on". Useful for testing: even if you don't have a mouse, you can still
  1351. * trigger a mouse event when this is switched on and it will be captured and acted on.
  1352. * @method setForceMouseEvents
  1353. * @param {Boolean} value If true, force mouse events to be on.
  1354. */
  1355. root.Mottle.setForceMouseEvents = function (value) {
  1356. isMouseDevice = value;
  1357. };
  1358. root.Mottle.version = "0.8.0";
  1359. if (typeof exports !== "undefined") {
  1360. exports.Mottle = root.Mottle;
  1361. }
  1362. }).call(typeof window === "undefined" ? this : window);
  1363. /**
  1364. drag/drop functionality for use with jsPlumb but with
  1365. no knowledge of jsPlumb. supports multiple scopes (separated by whitespace), dragging
  1366. multiple elements, constrain to parent, drop filters, drag start filters, custom
  1367. css classes.
  1368. a lot of the functionality of this script is expected to be plugged in:
  1369. addClass
  1370. removeClass
  1371. addEvent
  1372. removeEvent
  1373. getPosition
  1374. setPosition
  1375. getSize
  1376. indexOf
  1377. intersects
  1378. the name came from here:
  1379. http://mrsharpoblunto.github.io/foswig.js/
  1380. copyright 2016 jsPlumb
  1381. */
  1382. ;(function() {
  1383. "use strict";
  1384. var root = this;
  1385. var _suggest = function(list, item, head) {
  1386. if (list.indexOf(item) === -1) {
  1387. head ? list.unshift(item) : list.push(item);
  1388. return true;
  1389. }
  1390. return false;
  1391. };
  1392. var _vanquish = function(list, item) {
  1393. var idx = list.indexOf(item);
  1394. if (idx !== -1) list.splice(idx, 1);
  1395. };
  1396. var _difference = function(l1, l2) {
  1397. var d = [];
  1398. for (var i = 0; i < l1.length; i++) {
  1399. if (l2.indexOf(l1[i]) === -1)
  1400. d.push(l1[i]);
  1401. }
  1402. return d;
  1403. };
  1404. var _isString = function(f) {
  1405. return f == null ? false : (typeof f === "string" || f.constructor === String);
  1406. };
  1407. var getOffsetRect = function (elem) {
  1408. // (1)
  1409. var box = elem.getBoundingClientRect(),
  1410. body = document.body,
  1411. docElem = document.documentElement,
  1412. // (2)
  1413. scrollTop = window.pageYOffset || docElem.scrollTop || body.scrollTop,
  1414. scrollLeft = window.pageXOffset || docElem.scrollLeft || body.scrollLeft,
  1415. // (3)
  1416. clientTop = docElem.clientTop || body.clientTop || 0,
  1417. clientLeft = docElem.clientLeft || body.clientLeft || 0,
  1418. // (4)
  1419. top = box.top + scrollTop - clientTop,
  1420. left = box.left + scrollLeft - clientLeft;
  1421. return { top: Math.round(top), left: Math.round(left) };
  1422. };
  1423. var matchesSelector = function(el, selector, ctx) {
  1424. ctx = ctx || el.parentNode;
  1425. var possibles = ctx.querySelectorAll(selector);
  1426. for (var i = 0; i < possibles.length; i++) {
  1427. if (possibles[i] === el)
  1428. return true;
  1429. }
  1430. return false;
  1431. };
  1432. var findDelegateElement = function(parentElement, childElement, selector) {
  1433. if (matchesSelector(childElement, selector, parentElement)) {
  1434. return childElement;
  1435. } else {
  1436. var currentParent = childElement.parentNode;
  1437. while (currentParent != null && currentParent !== parentElement) {
  1438. if (matchesSelector(currentParent, selector, parentElement)) {
  1439. return currentParent;
  1440. } else {
  1441. currentParent = currentParent.parentNode;
  1442. }
  1443. }
  1444. }
  1445. };
  1446. /**
  1447. * Finds all elements matching the given selector, for the given parent. In order to support "scoped root" selectors,
  1448. * ie. things like "> .someClass", that is .someClass elements that are direct children of `parentElement`, we have to
  1449. * jump through a small hoop here: when a delegate draggable is registered, we write a `katavorio-draggable` attribute
  1450. * on the element on which the draggable is registered. Then when this method runs, we grab the value of that attribute and
  1451. * prepend it as part of the selector we're looking for. So "> .someClass" ends up being written as
  1452. * "[katavorio-draggable='...' > .someClass]", which works with querySelectorAll.
  1453. *
  1454. * @param availableSelectors
  1455. * @param parentElement
  1456. * @param childElement
  1457. * @returns {*}
  1458. */
  1459. var findMatchingSelector = function(availableSelectors, parentElement, childElement) {
  1460. var el = null;
  1461. var draggableId = parentElement.getAttribute("katavorio-draggable"),
  1462. prefix = draggableId != null ? "[katavorio-draggable='" + draggableId + "'] " : "";
  1463. for (var i = 0; i < availableSelectors.length; i++) {
  1464. el = findDelegateElement(parentElement, childElement, prefix + availableSelectors[i].selector);
  1465. if (el != null) {
  1466. if (availableSelectors[i].filter) {
  1467. var matches = matchesSelector(childElement, availableSelectors[i].filter, el),
  1468. exclude = availableSelectors[i].filterExclude === true;
  1469. if ( (exclude && !matches) || matches) {
  1470. return null;
  1471. }
  1472. }
  1473. return [ availableSelectors[i], el ];
  1474. }
  1475. }
  1476. return null;
  1477. };
  1478. var iev = (function() {
  1479. var rv = -1;
  1480. if (navigator.appName === 'Microsoft Internet Explorer') {
  1481. var ua = navigator.userAgent,
  1482. re = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
  1483. if (re.exec(ua) != null)
  1484. rv = parseFloat(RegExp.$1);
  1485. }
  1486. return rv;
  1487. })(),
  1488. DEFAULT_GRID_X = 10,
  1489. DEFAULT_GRID_Y = 10,
  1490. isIELT9 = iev > -1 && iev < 9,
  1491. isIE9 = iev === 9,
  1492. _pl = function(e) {
  1493. if (isIELT9) {
  1494. return [ e.clientX + document.documentElement.scrollLeft, e.clientY + document.documentElement.scrollTop ];
  1495. }
  1496. else {
  1497. var ts = _touches(e), t = _getTouch(ts, 0);
  1498. // for IE9 pageX might be null if the event was synthesized. We try for pageX/pageY first,
  1499. // falling back to clientX/clientY if necessary. In every other browser we want to use pageX/pageY.
  1500. return isIE9 ? [t.pageX || t.clientX, t.pageY || t.clientY] : [t.pageX, t.pageY];
  1501. }
  1502. },
  1503. _getTouch = function(touches, idx) { return touches.item ? touches.item(idx) : touches[idx]; },
  1504. _touches = function(e) {
  1505. return e.touches && e.touches.length > 0 ? e.touches :
  1506. e.changedTouches && e.changedTouches.length > 0 ? e.changedTouches :
  1507. e.targetTouches && e.targetTouches.length > 0 ? e.targetTouches :
  1508. [ e ];
  1509. },
  1510. _classes = {
  1511. delegatedDraggable:"katavorio-delegated-draggable", // elements that are the delegated drag handler for a bunch of other elements
  1512. draggable:"katavorio-draggable", // draggable elements
  1513. droppable:"katavorio-droppable", // droppable elements
  1514. drag : "katavorio-drag", // elements currently being dragged
  1515. selected:"katavorio-drag-selected", // elements in current drag selection
  1516. active : "katavorio-drag-active", // droppables that are targets of a currently dragged element
  1517. hover : "katavorio-drag-hover", // droppables over which a matching drag element is hovering
  1518. noSelect : "katavorio-drag-no-select", // added to the body to provide a hook to suppress text selection
  1519. ghostProxy:"katavorio-ghost-proxy", // added to a ghost proxy element in use when a drag has exited the bounds of its parent.
  1520. clonedDrag:"katavorio-clone-drag" // added to a node that is a clone of an element created at the start of a drag
  1521. },
  1522. _defaultScope = "katavorio-drag-scope",
  1523. _events = [ "stop", "start", "drag", "drop", "over", "out", "beforeStart" ],
  1524. _devNull = function() {},
  1525. _true = function() { return true; },
  1526. _foreach = function(l, fn, from) {
  1527. for (var i = 0; i < l.length; i++) {
  1528. if (l[i] != from)
  1529. fn(l[i]);
  1530. }
  1531. },
  1532. _setDroppablesActive = function(dd, val, andHover, drag) {
  1533. _foreach(dd, function(e) {
  1534. e.setActive(val);
  1535. if (val) e.updatePosition();
  1536. if (andHover) e.setHover(drag, val);
  1537. });
  1538. },
  1539. _each = function(obj, fn) {
  1540. if (obj == null) return;
  1541. obj = !_isString(obj) && (obj.tagName == null && obj.length != null) ? obj : [ obj ];
  1542. for (var i = 0; i < obj.length; i++)
  1543. fn.apply(obj[i], [ obj[i] ]);
  1544. },
  1545. _consume = function(e) {
  1546. if (e.stopPropagation) {
  1547. e.stopPropagation();
  1548. e.preventDefault();
  1549. }
  1550. else {
  1551. e.returnValue = false;
  1552. }
  1553. },
  1554. _defaultInputFilterSelector = "input,textarea,select,button,option",
  1555. //
  1556. // filters out events on all input elements, like textarea, checkbox, input, select.
  1557. _inputFilter = function(e, el, _katavorio) {
  1558. var t = e.srcElement || e.target;
  1559. return !matchesSelector(t, _katavorio.getInputFilterSelector(), el);
  1560. };
  1561. var Super = function(el, params, css, scope) {
  1562. this.params = params || {};
  1563. this.el = el;
  1564. this.params.addClass(this.el, this._class);
  1565. this.uuid = _uuid();
  1566. var enabled = true;
  1567. this.setEnabled = function(e) { enabled = e; };
  1568. this.isEnabled = function() { return enabled; };
  1569. this.toggleEnabled = function() { enabled = !enabled; };
  1570. this.setScope = function(scopes) {
  1571. this.scopes = scopes ? scopes.split(/\s+/) : [ scope ];
  1572. };
  1573. this.addScope = function(scopes) {
  1574. var m = {};
  1575. _each(this.scopes, function(s) { m[s] = true;});
  1576. _each(scopes ? scopes.split(/\s+/) : [], function(s) { m[s] = true;});
  1577. this.scopes = [];
  1578. for (var i in m) this.scopes.push(i);
  1579. };
  1580. this.removeScope = function(scopes) {
  1581. var m = {};
  1582. _each(this.scopes, function(s) { m[s] = true;});
  1583. _each(scopes ? scopes.split(/\s+/) : [], function(s) { delete m[s];});
  1584. this.scopes = [];
  1585. for (var i in m) this.scopes.push(i);
  1586. };
  1587. this.toggleScope = function(scopes) {
  1588. var m = {};
  1589. _each(this.scopes, function(s) { m[s] = true;});
  1590. _each(scopes ? scopes.split(/\s+/) : [], function(s) {
  1591. if (m[s]) delete m[s];
  1592. else m[s] = true;
  1593. });
  1594. this.scopes = [];
  1595. for (var i in m) this.scopes.push(i);
  1596. };
  1597. this.setScope(params.scope);
  1598. this.k = params.katavorio;
  1599. return params.katavorio;
  1600. };
  1601. var TRUE = function() { return true; };
  1602. var FALSE = function() { return false; };
  1603. var Drag = function(el, params, css, scope) {
  1604. this._class = css.draggable;
  1605. var k = Super.apply(this, arguments);
  1606. this.rightButtonCanDrag = this.params.rightButtonCanDrag;
  1607. var downAt = [0,0], posAtDown = null, pagePosAtDown = null, pageDelta = [0,0], moving = false, initialScroll = [0,0],
  1608. consumeStartEvent = this.params.consumeStartEvent !== false,
  1609. dragEl = this.el,
  1610. clone = this.params.clone,
  1611. scroll = this.params.scroll,
  1612. _multipleDrop = params.multipleDrop !== false,
  1613. isConstrained = false,
  1614. //useGhostProxy = params.ghostProxy === true ? TRUE : params.ghostProxy && typeof params.ghostProxy === "function" ? params.ghostProxy : FALSE,
  1615. useGhostProxy,
  1616. ghostProxy,// = function(el) { return el.cloneNode(true); },
  1617. elementToDrag = null,
  1618. availableSelectors = [],
  1619. activeSelectorParams = null, // which, if any, selector config is currently active.
  1620. ghostProxyParent = params.ghostProxyParent,
  1621. currentParentPosition,
  1622. ghostParentPosition,
  1623. ghostDx,
  1624. ghostDy;
  1625. if (params.ghostProxy === true) {
  1626. useGhostProxy = TRUE;
  1627. } else {
  1628. if (params.ghostProxy && typeof params.ghostProxy === "function") {
  1629. useGhostProxy = params.ghostProxy;
  1630. } else {
  1631. useGhostProxy = function(container, dragEl) {
  1632. if (activeSelectorParams && activeSelectorParams.useGhostProxy) {
  1633. return activeSelectorParams.useGhostProxy(container, dragEl);
  1634. } else {
  1635. return false;
  1636. }
  1637. }
  1638. }
  1639. }
  1640. if (params.makeGhostProxy) {
  1641. ghostProxy = params.makeGhostProxy;
  1642. } else {
  1643. ghostProxy = function(el) {
  1644. if (activeSelectorParams && activeSelectorParams.makeGhostProxy) {
  1645. return activeSelectorParams.makeGhostProxy(el);
  1646. } else {
  1647. return el.cloneNode(true);
  1648. }
  1649. };
  1650. }
  1651. // if an initial selector was provided, push the entire set of params as a selector config.
  1652. if (params.selector) {
  1653. var draggableId = el.getAttribute("katavorio-draggable");
  1654. if (draggableId == null) {
  1655. draggableId = "" + new Date().getTime();
  1656. el.setAttribute("katavorio-draggable", draggableId);
  1657. }
  1658. availableSelectors.push(params);
  1659. }
  1660. var snapThreshold = params.snapThreshold,
  1661. _snap = function(pos, gridX, gridY, thresholdX, thresholdY) {
  1662. var _dx = Math.floor(pos[0] / gridX),
  1663. _dxl = gridX * _dx,
  1664. _dxt = _dxl + gridX,
  1665. _x = Math.abs(pos[0] - _dxl) <= thresholdX ? _dxl : Math.abs(_dxt - pos[0]) <= thresholdX ? _dxt : pos[0];
  1666. var _dy = Math.floor(pos[1] / gridY),
  1667. _dyl = gridY * _dy,
  1668. _dyt = _dyl + gridY,
  1669. _y = Math.abs(pos[1] - _dyl) <= thresholdY ? _dyl : Math.abs(_dyt - pos[1]) <= thresholdY ? _dyt : pos[1];
  1670. return [ _x, _y];
  1671. };
  1672. this.posses = [];
  1673. this.posseRoles = {};
  1674. this.toGrid = function(pos) {
  1675. if (this.params.grid == null) {
  1676. return pos;
  1677. }
  1678. else {
  1679. var tx = this.params.grid ? this.params.grid[0] / 2 : snapThreshold ? snapThreshold : DEFAULT_GRID_X / 2,
  1680. ty = this.params.grid ? this.params.grid[1] / 2 : snapThreshold ? snapThreshold : DEFAULT_GRID_Y / 2;
  1681. return _snap(pos, this.params.grid[0], this.params.grid[1], tx, ty);
  1682. }
  1683. };
  1684. this.snap = function(x, y) {
  1685. if (dragEl == null) return;
  1686. x = x || (this.params.grid ? this.params.grid[0] : DEFAULT_GRID_X);
  1687. y = y || (this.params.grid ? this.params.grid[1] : DEFAULT_GRID_Y);
  1688. var p = this.params.getPosition(dragEl),
  1689. tx = this.params.grid ? this.params.grid[0] / 2 : snapThreshold,
  1690. ty = this.params.grid ? this.params.grid[1] / 2 : snapThreshold,
  1691. snapped = _snap(p, x, y, tx, ty);
  1692. this.params.setPosition(dragEl, snapped);
  1693. return snapped;
  1694. };
  1695. this.setUseGhostProxy = function(val) {
  1696. useGhostProxy = val ? TRUE : FALSE;
  1697. };
  1698. var constrain;
  1699. var negativeFilter = function(pos) {
  1700. return (params.allowNegative === false) ? [ Math.max (0, pos[0]), Math.max(0, pos[1]) ] : pos;
  1701. };
  1702. var _setConstrain = function(value) {
  1703. constrain = typeof value === "function" ? value : value ? function(pos, dragEl, _constrainRect, _size) {
  1704. return negativeFilter([
  1705. Math.max(0, Math.min(_constrainRect.w - _size[0], pos[0])),
  1706. Math.max(0, Math.min(_constrainRect.h - _size[1], pos[1]))
  1707. ]);
  1708. }.bind(this) : function(pos) { return negativeFilter(pos); };
  1709. }.bind(this);
  1710. _setConstrain(typeof this.params.constrain === "function" ? this.params.constrain : (this.params.constrain || this.params.containment));
  1711. /**
  1712. * Sets whether or not the Drag is constrained. A value of 'true' means constrain to parent bounds; a function
  1713. * will be executed and returns true if the position is allowed.
  1714. * @param value
  1715. */
  1716. this.setConstrain = function(value) {
  1717. _setConstrain(value);
  1718. };
  1719. /* private */ var _doConstrain = function(pos, dragEl, _constrainRect, _size) {
  1720. if (activeSelectorParams != null && activeSelectorParams.constrain && typeof activeSelectorParams.constrain === "function") {
  1721. return activeSelectorParams.constrain(pos, dragEl, _constrainRect, _size);
  1722. } else {
  1723. return constrain(pos, dragEl, _constrainRect, _size);
  1724. }
  1725. };
  1726. var revertFunction;
  1727. /**
  1728. * Sets a function to call on drag stop, which, if it returns true, indicates that the given element should
  1729. * revert to its position before the previous drag.
  1730. * @param fn
  1731. */
  1732. this.setRevert = function(fn) {
  1733. revertFunction = fn;
  1734. };
  1735. if (this.params.revert) {
  1736. revertFunction = this.params.revert;
  1737. }
  1738. var _assignId = function(obj) {
  1739. if (typeof obj === "function") {
  1740. obj._katavorioId = _uuid();
  1741. return obj._katavorioId;
  1742. } else {
  1743. return obj;
  1744. }
  1745. },
  1746. // a map of { spec -> [ fn, exclusion ] } entries.
  1747. _filters = {},
  1748. _testFilter = function(e) {
  1749. for (var key in _filters) {
  1750. var f = _filters[key];
  1751. var rv = f[0](e);
  1752. if (f[1]) rv = !rv;
  1753. if (!rv) return false;
  1754. }
  1755. return true;
  1756. },
  1757. _setFilter = this.setFilter = function(f, _exclude) {
  1758. if (f) {
  1759. var key = _assignId(f);
  1760. _filters[key] = [
  1761. function(e) {
  1762. var t = e.srcElement || e.target, m;
  1763. if (_isString(f)) {
  1764. m = matchesSelector(t, f, el);
  1765. }
  1766. else if (typeof f === "function") {
  1767. m = f(e, el);
  1768. }
  1769. return m;
  1770. },
  1771. _exclude !== false
  1772. ];
  1773. }
  1774. },
  1775. _addFilter = this.addFilter = _setFilter,
  1776. _removeFilter = this.removeFilter = function(f) {
  1777. var key = typeof f === "function" ? f._katavorioId : f;
  1778. delete _filters[key];
  1779. };
  1780. this.clearAllFilters = function() {
  1781. _filters = {};
  1782. };
  1783. this.canDrag = this.params.canDrag || _true;
  1784. var constrainRect,
  1785. matchingDroppables = [],
  1786. intersectingDroppables = [];
  1787. this.addSelector = function(params) {
  1788. if (params.selector) {
  1789. availableSelectors.push(params);
  1790. }
  1791. };
  1792. this.downListener = function(e) {
  1793. if (e.defaultPrevented) { return; }
  1794. var isNotRightClick = this.rightButtonCanDrag || (e.which !== 3 && e.button !== 2);
  1795. if (isNotRightClick && this.isEnabled() && this.canDrag()) {
  1796. var _f = _testFilter(e) && _inputFilter(e, this.el, this.k);
  1797. if (_f) {
  1798. activeSelectorParams = null;
  1799. elementToDrag = null;
  1800. // if (selector) {
  1801. // elementToDrag = findDelegateElement(this.el, e.target || e.srcElement, selector);
  1802. // if(elementToDrag == null) {
  1803. // return;
  1804. // }
  1805. // }
  1806. if (availableSelectors.length > 0) {
  1807. var match = findMatchingSelector(availableSelectors, this.el, e.target || e.srcElement);
  1808. if (match != null) {
  1809. activeSelectorParams = match[0];
  1810. elementToDrag = match[1];
  1811. }
  1812. // elementToDrag = findDelegateElement(this.el, e.target || e.srcElement, selector);
  1813. if(elementToDrag == null) {
  1814. return;
  1815. }
  1816. }
  1817. else {
  1818. elementToDrag = this.el;
  1819. }
  1820. if (clone) {
  1821. dragEl = elementToDrag.cloneNode(true);
  1822. this.params.addClass(dragEl, _classes.clonedDrag);
  1823. dragEl.setAttribute("id", null);
  1824. dragEl.style.position = "absolute";
  1825. if (this.params.parent != null) {
  1826. var p = this.params.getPosition(this.el);
  1827. dragEl.style.left = p[0] + "px";
  1828. dragEl.style.top = p[1] + "px";
  1829. this.params.parent.appendChild(dragEl);
  1830. } else {
  1831. // the clone node is added to the body; getOffsetRect gives us a value
  1832. // relative to the body.
  1833. var b = getOffsetRect(elementToDrag);
  1834. dragEl.style.left = b.left + "px";
  1835. dragEl.style.top = b.top + "px";
  1836. document.body.appendChild(dragEl);
  1837. }
  1838. } else {
  1839. dragEl = elementToDrag;
  1840. }
  1841. consumeStartEvent && _consume(e);
  1842. downAt = _pl(e);
  1843. if (dragEl && dragEl.parentNode)
  1844. {
  1845. initialScroll = [dragEl.parentNode.scrollLeft, dragEl.parentNode.scrollTop];
  1846. }
  1847. //
  1848. this.params.bind(document, "mousemove", this.moveListener);
  1849. this.params.bind(document, "mouseup", this.upListener);
  1850. k.markSelection(this);
  1851. k.markPosses(this);
  1852. this.params.addClass(document.body, css.noSelect);
  1853. _dispatch("beforeStart", {el:this.el, pos:posAtDown, e:e, drag:this});
  1854. }
  1855. else if (this.params.consumeFilteredEvents) {
  1856. _consume(e);
  1857. }
  1858. }
  1859. }.bind(this);
  1860. this.moveListener = function(e) {
  1861. if (downAt) {
  1862. if (!moving) {
  1863. var _continue = _dispatch("start", {el:this.el, pos:posAtDown, e:e, drag:this});
  1864. if (_continue !== false) {
  1865. if (!downAt) {
  1866. return;
  1867. }
  1868. this.mark(true);
  1869. moving = true;
  1870. } else {
  1871. this.abort();
  1872. }
  1873. }
  1874. // it is possible that the start event caused the drag to be aborted. So we check
  1875. // again that we are currently dragging.
  1876. if (downAt) {
  1877. intersectingDroppables.length = 0;
  1878. var pos = _pl(e), dx = pos[0] - downAt[0], dy = pos[1] - downAt[1],
  1879. z = this.params.ignoreZoom ? 1 : k.getZoom();
  1880. if (dragEl && dragEl.parentNode)
  1881. {
  1882. dx += dragEl.parentNode.scrollLeft - initialScroll[0];
  1883. dy += dragEl.parentNode.scrollTop - initialScroll[1];
  1884. }
  1885. dx /= z;
  1886. dy /= z;
  1887. this.moveBy(dx, dy, e);
  1888. k.updateSelection(dx, dy, this);
  1889. k.updatePosses(dx, dy, this);
  1890. }
  1891. }
  1892. }.bind(this);
  1893. this.upListener = function(e) {
  1894. if (downAt) {
  1895. downAt = null;
  1896. this.params.unbind(document, "mousemove", this.moveListener);
  1897. this.params.unbind(document, "mouseup", this.upListener);
  1898. this.params.removeClass(document.body, css.noSelect);
  1899. this.unmark(e);
  1900. k.unmarkSelection(this, e);
  1901. k.unmarkPosses(this, e);
  1902. this.stop(e);
  1903. k.notifyPosseDragStop(this, e);
  1904. moving = false;
  1905. intersectingDroppables.length = 0;
  1906. if (clone) {
  1907. dragEl && dragEl.parentNode && dragEl.parentNode.removeChild(dragEl);
  1908. dragEl = null;
  1909. } else {
  1910. if (revertFunction && revertFunction(dragEl, this.params.getPosition(dragEl)) === true) {
  1911. this.params.setPosition(dragEl, posAtDown);
  1912. _dispatch("revert", dragEl);
  1913. }
  1914. }
  1915. }
  1916. }.bind(this);
  1917. this.getFilters = function() { return _filters; };
  1918. this.abort = function() {
  1919. if (downAt != null) {
  1920. this.upListener();
  1921. }
  1922. };
  1923. /**
  1924. * Returns the element that was last dragged. This may be some original element from the DOM, or if `clone` is
  1925. * set, then its actually a copy of some original DOM element. In some client calls to this method, it is the
  1926. * actual element that was dragged that is desired. In others, it is the original DOM element that the user
  1927. * wishes to get - in which case, pass true for `retrieveOriginalElement`.
  1928. *
  1929. * @returns {*}
  1930. */
  1931. this.getDragElement = function(retrieveOriginalElement) {
  1932. return retrieveOriginalElement ? elementToDrag || this.el : dragEl || this.el;
  1933. };
  1934. var listeners = {"start":[], "drag":[], "stop":[], "over":[], "out":[], "beforeStart":[], "revert":[] };
  1935. if (params.events.start) listeners.start.push(params.events.start);
  1936. if (params.events.beforeStart) listeners.beforeStart.push(params.events.beforeStart);
  1937. if (params.events.stop) listeners.stop.push(params.events.stop);
  1938. if (params.events.drag) listeners.drag.push(params.events.drag);
  1939. if (params.events.revert) listeners.revert.push(params.events.revert);
  1940. this.on = function(evt, fn) {
  1941. if (listeners[evt]) listeners[evt].push(fn);
  1942. };
  1943. this.off = function(evt, fn) {
  1944. if (listeners[evt]) {
  1945. var l = [];
  1946. for (var i = 0; i < listeners[evt].length; i++) {
  1947. if (listeners[evt][i] !== fn) l.push(listeners[evt][i]);
  1948. }
  1949. listeners[evt] = l;
  1950. }
  1951. };
  1952. var _dispatch = function(evt, value) {
  1953. var result = null;
  1954. if (activeSelectorParams && activeSelectorParams[evt]) {
  1955. result = activeSelectorParams[evt](value);
  1956. } else if (listeners[evt]) {
  1957. for (var i = 0; i < listeners[evt].length; i++) {
  1958. try {
  1959. var v = listeners[evt][i](value);
  1960. if (v != null) {
  1961. result = v;
  1962. }
  1963. }
  1964. catch (e) { }
  1965. }
  1966. }
  1967. return result;
  1968. };
  1969. this.notifyStart = function(e) {
  1970. _dispatch("start", {el:this.el, pos:this.params.getPosition(dragEl), e:e, drag:this});
  1971. };
  1972. this.stop = function(e, force) {
  1973. if (force || moving) {
  1974. var positions = [],
  1975. sel = k.getSelection(),
  1976. dPos = this.params.getPosition(dragEl);
  1977. if (sel.length > 0) {
  1978. for (var i = 0; i < sel.length; i++) {
  1979. var p = this.params.getPosition(sel[i].el);
  1980. positions.push([ sel[i].el, { left: p[0], top: p[1] }, sel[i] ]);
  1981. }
  1982. }
  1983. else {
  1984. positions.push([ dragEl, {left:dPos[0], top:dPos[1]}, this ]);
  1985. }
  1986. _dispatch("stop", {
  1987. el: dragEl,
  1988. pos: ghostProxyOffsets || dPos,
  1989. finalPos:dPos,
  1990. e: e,
  1991. drag: this,
  1992. selection:positions
  1993. });
  1994. }
  1995. };
  1996. this.mark = function(andNotify) {
  1997. posAtDown = this.params.getPosition(dragEl);
  1998. pagePosAtDown = this.params.getPosition(dragEl, true);
  1999. pageDelta = [pagePosAtDown[0] - posAtDown[0], pagePosAtDown[1] - posAtDown[1]];
  2000. this.size = this.params.getSize(dragEl);
  2001. matchingDroppables = k.getMatchingDroppables(this);
  2002. _setDroppablesActive(matchingDroppables, true, false, this);
  2003. this.params.addClass(dragEl, this.params.dragClass || css.drag);
  2004. var cs;
  2005. if (this.params.getConstrainingRectangle) {
  2006. cs = this.params.getConstrainingRectangle(dragEl)
  2007. } else {
  2008. cs = this.params.getSize(dragEl.parentNode);
  2009. }
  2010. constrainRect = {w: cs[0], h: cs[1]};
  2011. ghostDx = 0;
  2012. ghostDy = 0;
  2013. if (andNotify) {
  2014. k.notifySelectionDragStart(this);
  2015. }
  2016. };
  2017. var ghostProxyOffsets;
  2018. this.unmark = function(e, doNotCheckDroppables) {
  2019. _setDroppablesActive(matchingDroppables, false, true, this);
  2020. if (isConstrained && useGhostProxy(elementToDrag, dragEl)) {
  2021. ghostProxyOffsets = [dragEl.offsetLeft - ghostDx, dragEl.offsetTop - ghostDy];
  2022. dragEl.parentNode.removeChild(dragEl);
  2023. dragEl = elementToDrag;
  2024. }
  2025. else {
  2026. ghostProxyOffsets = null;
  2027. }
  2028. this.params.removeClass(dragEl, this.params.dragClass || css.drag);
  2029. matchingDroppables.length = 0;
  2030. isConstrained = false;
  2031. if (!doNotCheckDroppables) {
  2032. if (intersectingDroppables.length > 0 && ghostProxyOffsets) {
  2033. params.setPosition(elementToDrag, ghostProxyOffsets);
  2034. }
  2035. intersectingDroppables.sort(_rankSort);
  2036. for (var i = 0; i < intersectingDroppables.length; i++) {
  2037. var retVal = intersectingDroppables[i].drop(this, e);
  2038. if (retVal === true) break;
  2039. }
  2040. }
  2041. };
  2042. this.moveBy = function(dx, dy, e) {
  2043. intersectingDroppables.length = 0;
  2044. var desiredLoc = this.toGrid([posAtDown[0] + dx, posAtDown[1] + dy]),
  2045. cPos = _doConstrain(desiredLoc, dragEl, constrainRect, this.size);
  2046. // if we should use a ghost proxy...
  2047. if (useGhostProxy(this.el, dragEl)) {
  2048. // and the element has been dragged outside of its parent bounds
  2049. if (desiredLoc[0] !== cPos[0] || desiredLoc[1] !== cPos[1]) {
  2050. // ...if ghost proxy not yet created
  2051. if (!isConstrained) {
  2052. // create it
  2053. var gp = ghostProxy(elementToDrag);
  2054. params.addClass(gp, _classes.ghostProxy);
  2055. if (ghostProxyParent) {
  2056. ghostProxyParent.appendChild(gp);
  2057. // find offset between drag el's parent the ghost parent
  2058. currentParentPosition = params.getPosition(elementToDrag.parentNode, true);
  2059. ghostParentPosition = params.getPosition(params.ghostProxyParent, true);
  2060. ghostDx = currentParentPosition[0] - ghostParentPosition[0];
  2061. ghostDy = currentParentPosition[1] - ghostParentPosition[1];
  2062. } else {
  2063. elementToDrag.parentNode.appendChild(gp);
  2064. }
  2065. // the ghost proxy is the drag element
  2066. dragEl = gp;
  2067. // set this flag so we dont recreate the ghost proxy
  2068. isConstrained = true;
  2069. }
  2070. // now the drag position can be the desired position, as the ghost proxy can support it.
  2071. cPos = desiredLoc;
  2072. }
  2073. else {
  2074. // if the element is not outside of its parent bounds, and ghost proxy is in place,
  2075. if (isConstrained) {
  2076. // remove the ghost proxy from the dom
  2077. dragEl.parentNode.removeChild(dragEl);
  2078. // reset the drag element to the original element
  2079. dragEl = elementToDrag;
  2080. // clear this flag.
  2081. isConstrained = false;
  2082. currentParentPosition = null;
  2083. ghostParentPosition = null;
  2084. ghostDx = 0;
  2085. ghostDy = 0;
  2086. }
  2087. }
  2088. }
  2089. var rect = { x:cPos[0], y:cPos[1], w:this.size[0], h:this.size[1]},
  2090. pageRect = { x:rect.x + pageDelta[0], y:rect.y + pageDelta[1], w:rect.w, h:rect.h},
  2091. focusDropElement = null;
  2092. this.params.setPosition(dragEl, [cPos[0] + ghostDx, cPos[1] + ghostDy]);
  2093. for (var i = 0; i < matchingDroppables.length; i++) {
  2094. var r2 = { x:matchingDroppables[i].pagePosition[0], y:matchingDroppables[i].pagePosition[1], w:matchingDroppables[i].size[0], h:matchingDroppables[i].size[1]};
  2095. if (this.params.intersects(pageRect, r2) && (_multipleDrop || focusDropElement == null || focusDropElement === matchingDroppables[i].el) && matchingDroppables[i].canDrop(this)) {
  2096. if (!focusDropElement) focusDropElement = matchingDroppables[i].el;
  2097. intersectingDroppables.push(matchingDroppables[i]);
  2098. matchingDroppables[i].setHover(this, true, e);
  2099. }
  2100. else if (matchingDroppables[i].isHover()) {
  2101. matchingDroppables[i].setHover(this, false, e);
  2102. }
  2103. }
  2104. _dispatch("drag", {el:this.el, pos:cPos, e:e, drag:this});
  2105. /* test to see if the parent needs to be scrolled (future)
  2106. if (scroll) {
  2107. var pnsl = dragEl.parentNode.scrollLeft, pnst = dragEl.parentNode.scrollTop;
  2108. console.log("scroll!", pnsl, pnst);
  2109. }*/
  2110. };
  2111. this.destroy = function() {
  2112. this.params.unbind(this.el, "mousedown", this.downListener);
  2113. this.params.unbind(document, "mousemove", this.moveListener);
  2114. this.params.unbind(document, "mouseup", this.upListener);
  2115. this.downListener = null;
  2116. this.upListener = null;
  2117. this.moveListener = null;
  2118. };
  2119. // init:register mousedown, and perhaps set a filter
  2120. this.params.bind(this.el, "mousedown", this.downListener);
  2121. // if handle provided, use that. otherwise, try to set a filter.
  2122. // note that a `handle` selector always results in filterExclude being set to false, ie.
  2123. // the selector defines the handle element(s).
  2124. if (this.params.handle)
  2125. _setFilter(this.params.handle, false);
  2126. else
  2127. _setFilter(this.params.filter, this.params.filterExclude);
  2128. };
  2129. var Drop = function(el, params, css, scope) {
  2130. this._class = css.droppable;
  2131. this.params = params || {};
  2132. this.rank = params.rank || 0;
  2133. this._activeClass = this.params.activeClass || css.active;
  2134. this._hoverClass = this.params.hoverClass || css.hover;
  2135. Super.apply(this, arguments);
  2136. var hover = false;
  2137. this.allowLoopback = this.params.allowLoopback !== false;
  2138. this.setActive = function(val) {
  2139. this.params[val ? "addClass" : "removeClass"](this.el, this._activeClass);
  2140. };
  2141. this.updatePosition = function() {
  2142. this.position = this.params.getPosition(this.el);
  2143. this.pagePosition = this.params.getPosition(this.el, true);
  2144. this.size = this.params.getSize(this.el);
  2145. };
  2146. this.canDrop = this.params.canDrop || function(drag) {
  2147. return true;
  2148. };
  2149. this.isHover = function() { return hover; };
  2150. this.setHover = function(drag, val, e) {
  2151. // if turning off hover but this was not the drag that caused the hover, ignore.
  2152. if (val || this.el._katavorioDragHover == null || this.el._katavorioDragHover === drag.el._katavorio) {
  2153. this.params[val ? "addClass" : "removeClass"](this.el, this._hoverClass);
  2154. this.el._katavorioDragHover = val ? drag.el._katavorio : null;
  2155. if (hover !== val) {
  2156. this.params.events[val ? "over" : "out"]({el: this.el, e: e, drag: drag, drop: this});
  2157. }
  2158. hover = val;
  2159. }
  2160. };
  2161. /**
  2162. * A drop event. `drag` is the corresponding Drag object, which may be a Drag for some specific element, or it
  2163. * may be a Drag on some element acting as a delegate for elements contained within it.
  2164. * @param drag
  2165. * @param event
  2166. * @returns {*}
  2167. */
  2168. this.drop = function(drag, event) {
  2169. return this.params.events["drop"]({ drag:drag, e:event, drop:this });
  2170. };
  2171. this.destroy = function() {
  2172. this._class = null;
  2173. this._activeClass = null;
  2174. this._hoverClass = null;
  2175. hover = null;
  2176. };
  2177. };
  2178. var _uuid = function() {
  2179. return ('xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
  2180. var r = Math.random()*16|0, v = c === 'x' ? r : (r&0x3|0x8);
  2181. return v.toString(16);
  2182. }));
  2183. };
  2184. var _rankSort = function(a,b) {
  2185. return a.rank < b.rank ? 1 : a.rank > b.rank ? -1 : 0;
  2186. };
  2187. var _gel = function(el) {
  2188. if (el == null) return null;
  2189. el = (typeof el === "string" || el.constructor === String) ? document.getElementById(el) : el;
  2190. if (el == null) return null;
  2191. el._katavorio = el._katavorio || _uuid();
  2192. return el;
  2193. };
  2194. root.Katavorio = function(katavorioParams) {
  2195. var _selection = [],
  2196. _selectionMap = {};
  2197. this._dragsByScope = {};
  2198. this._dropsByScope = {};
  2199. var _zoom = 1,
  2200. _reg = function(obj, map) {
  2201. _each(obj, function(_obj) {
  2202. for(var i = 0; i < _obj.scopes.length; i++) {
  2203. map[_obj.scopes[i]] = map[_obj.scopes[i]] || [];
  2204. map[_obj.scopes[i]].push(_obj);
  2205. }
  2206. });
  2207. },
  2208. _unreg = function(obj, map) {
  2209. var c = 0;
  2210. _each(obj, function(_obj) {
  2211. for(var i = 0; i < _obj.scopes.length; i++) {
  2212. if (map[_obj.scopes[i]]) {
  2213. var idx = katavorioParams.indexOf(map[_obj.scopes[i]], _obj);
  2214. if (idx !== -1) {
  2215. map[_obj.scopes[i]].splice(idx, 1);
  2216. c++;
  2217. }
  2218. }
  2219. }
  2220. });
  2221. return c > 0 ;
  2222. },
  2223. _getMatchingDroppables = this.getMatchingDroppables = function(drag) {
  2224. var dd = [], _m = {};
  2225. for (var i = 0; i < drag.scopes.length; i++) {
  2226. var _dd = this._dropsByScope[drag.scopes[i]];
  2227. if (_dd) {
  2228. for (var j = 0; j < _dd.length; j++) {
  2229. if (_dd[j].canDrop(drag) && !_m[_dd[j].uuid] && (_dd[j].allowLoopback || _dd[j].el !== drag.el)) {
  2230. _m[_dd[j].uuid] = true;
  2231. dd.push(_dd[j]);
  2232. }
  2233. }
  2234. }
  2235. }
  2236. dd.sort(_rankSort);
  2237. return dd;
  2238. },
  2239. _prepareParams = function(p) {
  2240. p = p || {};
  2241. var _p = {
  2242. events:{}
  2243. }, i;
  2244. for (i in katavorioParams) _p[i] = katavorioParams[i];
  2245. for (i in p) _p[i] = p[i];
  2246. // events
  2247. for (i = 0; i < _events.length; i++) {
  2248. _p.events[_events[i]] = p[_events[i]] || _devNull;
  2249. }
  2250. _p.katavorio = this;
  2251. return _p;
  2252. }.bind(this),
  2253. _mistletoe = function(existingDrag, params) {
  2254. for (var i = 0; i < _events.length; i++) {
  2255. if (params[_events[i]]) {
  2256. existingDrag.on(_events[i], params[_events[i]]);
  2257. }
  2258. }
  2259. }.bind(this),
  2260. _css = {},
  2261. overrideCss = katavorioParams.css || {},
  2262. _scope = katavorioParams.scope || _defaultScope;
  2263. // prepare map of css classes based on defaults frst, then optional overrides
  2264. for (var i in _classes) _css[i] = _classes[i];
  2265. for (var i in overrideCss) _css[i] = overrideCss[i];
  2266. var inputFilterSelector = katavorioParams.inputFilterSelector || _defaultInputFilterSelector;
  2267. /**
  2268. * Gets the selector identifying which input elements to filter from drag events.
  2269. * @method getInputFilterSelector
  2270. * @return {String} Current input filter selector.
  2271. */
  2272. this.getInputFilterSelector = function() { return inputFilterSelector; };
  2273. /**
  2274. * Sets the selector identifying which input elements to filter from drag events.
  2275. * @method setInputFilterSelector
  2276. * @param {String} selector Input filter selector to set.
  2277. * @return {Katavorio} Current instance; method may be chained.
  2278. */
  2279. this.setInputFilterSelector = function(selector) {
  2280. inputFilterSelector = selector;
  2281. return this;
  2282. };
  2283. /**
  2284. * Either makes the given element draggable, or identifies it as an element inside which some identified list
  2285. * of elements may be draggable.
  2286. * @param el
  2287. * @param params
  2288. * @returns {Array}
  2289. */
  2290. this.draggable = function(el, params) {
  2291. var o = [];
  2292. _each(el, function (_el) {
  2293. _el = _gel(_el);
  2294. if (_el != null) {
  2295. if (_el._katavorioDrag == null) {
  2296. var p = _prepareParams(params);
  2297. _el._katavorioDrag = new Drag(_el, p, _css, _scope);
  2298. _reg(_el._katavorioDrag, this._dragsByScope);
  2299. o.push(_el._katavorioDrag);
  2300. katavorioParams.addClass(_el, p.selector ? _css.delegatedDraggable : _css.draggable);
  2301. }
  2302. else {
  2303. _mistletoe(_el._katavorioDrag, params);
  2304. }
  2305. }
  2306. }.bind(this));
  2307. return o;
  2308. };
  2309. this.droppable = function(el, params) {
  2310. var o = [];
  2311. _each(el, function(_el) {
  2312. _el = _gel(_el);
  2313. if (_el != null) {
  2314. var drop = new Drop(_el, _prepareParams(params), _css, _scope);
  2315. _el._katavorioDrop = _el._katavorioDrop || [];
  2316. _el._katavorioDrop.push(drop);
  2317. _reg(drop, this._dropsByScope);
  2318. o.push(drop);
  2319. katavorioParams.addClass(_el, _css.droppable);
  2320. }
  2321. }.bind(this));
  2322. return o;
  2323. };
  2324. /**
  2325. * @name Katavorio#select
  2326. * @function
  2327. * @desc Adds an element to the current selection (for multiple node drag)
  2328. * @param {Element|String} DOM element - or id of the element - to add.
  2329. */
  2330. this.select = function(el) {
  2331. _each(el, function() {
  2332. var _el = _gel(this);
  2333. if (_el && _el._katavorioDrag) {
  2334. if (!_selectionMap[_el._katavorio]) {
  2335. _selection.push(_el._katavorioDrag);
  2336. _selectionMap[_el._katavorio] = [ _el, _selection.length - 1 ];
  2337. katavorioParams.addClass(_el, _css.selected);
  2338. }
  2339. }
  2340. });
  2341. return this;
  2342. };
  2343. /**
  2344. * @name Katavorio#deselect
  2345. * @function
  2346. * @desc Removes an element from the current selection (for multiple node drag)
  2347. * @param {Element|String} DOM element - or id of the element - to remove.
  2348. */
  2349. this.deselect = function(el) {
  2350. _each(el, function() {
  2351. var _el = _gel(this);
  2352. if (_el && _el._katavorio) {
  2353. var e = _selectionMap[_el._katavorio];
  2354. if (e) {
  2355. var _s = [];
  2356. for (var i = 0; i < _selection.length; i++)
  2357. if (_selection[i].el !== _el) _s.push(_selection[i]);
  2358. _selection = _s;
  2359. delete _selectionMap[_el._katavorio];
  2360. katavorioParams.removeClass(_el, _css.selected);
  2361. }
  2362. }
  2363. });
  2364. return this;
  2365. };
  2366. this.deselectAll = function() {
  2367. for (var i in _selectionMap) {
  2368. var d = _selectionMap[i];
  2369. katavorioParams.removeClass(d[0], _css.selected);
  2370. }
  2371. _selection.length = 0;
  2372. _selectionMap = {};
  2373. };
  2374. this.markSelection = function(drag) {
  2375. _foreach(_selection, function(e) { e.mark(); }, drag);
  2376. };
  2377. this.markPosses = function(drag) {
  2378. if (drag.posses) {
  2379. _each(drag.posses, function(p) {
  2380. if (drag.posseRoles[p] && _posses[p]) {
  2381. _foreach(_posses[p].members, function (d) {
  2382. d.mark();
  2383. }, drag);
  2384. }
  2385. })
  2386. }
  2387. };
  2388. this.unmarkSelection = function(drag, event) {
  2389. _foreach(_selection, function(e) { e.unmark(event); }, drag);
  2390. };
  2391. this.unmarkPosses = function(drag, event) {
  2392. if (drag.posses) {
  2393. _each(drag.posses, function(p) {
  2394. if (drag.posseRoles[p] && _posses[p]) {
  2395. _foreach(_posses[p].members, function (d) {
  2396. d.unmark(event, true);
  2397. }, drag);
  2398. }
  2399. });
  2400. }
  2401. };
  2402. this.getSelection = function() { return _selection.slice(0); };
  2403. this.updateSelection = function(dx, dy, drag) {
  2404. _foreach(_selection, function(e) { e.moveBy(dx, dy); }, drag);
  2405. };
  2406. var _posseAction = function(fn, drag) {
  2407. if (drag.posses) {
  2408. _each(drag.posses, function(p) {
  2409. if (drag.posseRoles[p] && _posses[p]) {
  2410. _foreach(_posses[p].members, function (e) {
  2411. fn(e);
  2412. }, drag);
  2413. }
  2414. });
  2415. }
  2416. };
  2417. this.updatePosses = function(dx, dy, drag) {
  2418. _posseAction(function(e) { e.moveBy(dx, dy); }, drag);
  2419. };
  2420. this.notifyPosseDragStop = function(drag, evt) {
  2421. _posseAction(function(e) { e.stop(evt, true); }, drag);
  2422. };
  2423. this.notifySelectionDragStop = function(drag, evt) {
  2424. _foreach(_selection, function(e) { e.stop(evt, true); }, drag);
  2425. };
  2426. this.notifySelectionDragStart = function(drag, evt) {
  2427. _foreach(_selection, function(e) { e.notifyStart(evt);}, drag);
  2428. };
  2429. this.setZoom = function(z) { _zoom = z; };
  2430. this.getZoom = function() { return _zoom; };
  2431. // does the work of changing scopes
  2432. var _scopeManip = function(kObj, scopes, map, fn) {
  2433. _each(kObj, function(_kObj) {
  2434. _unreg(_kObj, map); // deregister existing scopes
  2435. _kObj[fn](scopes); // set scopes
  2436. _reg(_kObj, map); // register new ones
  2437. });
  2438. };
  2439. _each([ "set", "add", "remove", "toggle"], function(v) {
  2440. this[v + "Scope"] = function(el, scopes) {
  2441. _scopeManip(el._katavorioDrag, scopes, this._dragsByScope, v + "Scope");
  2442. _scopeManip(el._katavorioDrop, scopes, this._dropsByScope, v + "Scope");
  2443. }.bind(this);
  2444. this[v + "DragScope"] = function(el, scopes) {
  2445. _scopeManip(el.constructor === Drag ? el : el._katavorioDrag, scopes, this._dragsByScope, v + "Scope");
  2446. }.bind(this);
  2447. this[v + "DropScope"] = function(el, scopes) {
  2448. _scopeManip(el.constructor === Drop ? el : el._katavorioDrop, scopes, this._dropsByScope, v + "Scope");
  2449. }.bind(this);
  2450. }.bind(this));
  2451. this.snapToGrid = function(x, y) {
  2452. for (var s in this._dragsByScope) {
  2453. _foreach(this._dragsByScope[s], function(d) { d.snap(x, y); });
  2454. }
  2455. };
  2456. this.getDragsForScope = function(s) { return this._dragsByScope[s]; };
  2457. this.getDropsForScope = function(s) { return this._dropsByScope[s]; };
  2458. var _destroy = function(el, type, map) {
  2459. el = _gel(el);
  2460. if (el[type]) {
  2461. // remove from selection, if present.
  2462. var selIdx = _selection.indexOf(el[type]);
  2463. if (selIdx >= 0) {
  2464. _selection.splice(selIdx, 1);
  2465. }
  2466. if (_unreg(el[type], map)) {
  2467. _each(el[type], function(kObj) { kObj.destroy() });
  2468. }
  2469. delete el[type];
  2470. }
  2471. };
  2472. var _removeListener = function(el, type, evt, fn) {
  2473. el = _gel(el);
  2474. if (el[type]) {
  2475. el[type].off(evt, fn);
  2476. }
  2477. };
  2478. this.elementRemoved = function(el) {
  2479. if (el["_katavorioDrag"]) {
  2480. this.destroyDraggable(el);
  2481. }
  2482. if (el["_katavorioDrop"]) {
  2483. this.destroyDroppable(el);
  2484. }
  2485. };
  2486. /**
  2487. * Either completely remove drag functionality from the given element, or remove a specific event handler. If you
  2488. * call this method with a single argument - the element - all drag functionality is removed from it. Otherwise, if
  2489. * you provide an event name and listener function, this function is de-registered (if found).
  2490. * @param el Element to update
  2491. * @param {string} [evt] Optional event name to unsubscribe
  2492. * @param {Function} [fn] Optional function to unsubscribe
  2493. */
  2494. this.destroyDraggable = function(el, evt, fn) {
  2495. if (arguments.length === 1) {
  2496. _destroy(el, "_katavorioDrag", this._dragsByScope);
  2497. } else {
  2498. _removeListener(el, "_katavorioDrag", evt, fn);
  2499. }
  2500. };
  2501. /**
  2502. * Either completely remove drop functionality from the given element, or remove a specific event handler. If you
  2503. * call this method with a single argument - the element - all drop functionality is removed from it. Otherwise, if
  2504. * you provide an event name and listener function, this function is de-registered (if found).
  2505. * @param el Element to update
  2506. * @param {string} [evt] Optional event name to unsubscribe
  2507. * @param {Function} [fn] Optional function to unsubscribe
  2508. */
  2509. this.destroyDroppable = function(el, evt, fn) {
  2510. if (arguments.length === 1) {
  2511. _destroy(el, "_katavorioDrop", this._dropsByScope);
  2512. } else {
  2513. _removeListener(el, "_katavorioDrop", evt, fn);
  2514. }
  2515. };
  2516. this.reset = function() {
  2517. this._dragsByScope = {};
  2518. this._dropsByScope = {};
  2519. _selection = [];
  2520. _selectionMap = {};
  2521. _posses = {};
  2522. };
  2523. // ----- groups
  2524. var _posses = {};
  2525. var _processOneSpec = function(el, _spec, dontAddExisting) {
  2526. var posseId = _isString(_spec) ? _spec : _spec.id;
  2527. var active = _isString(_spec) ? true : _spec.active !== false;
  2528. var posse = _posses[posseId] || (function() {
  2529. var g = {name:posseId, members:[]};
  2530. _posses[posseId] = g;
  2531. return g;
  2532. })();
  2533. _each(el, function(_el) {
  2534. if (_el._katavorioDrag) {
  2535. if (dontAddExisting && _el._katavorioDrag.posseRoles[posse.name] != null) return;
  2536. _suggest(posse.members, _el._katavorioDrag);
  2537. _suggest(_el._katavorioDrag.posses, posse.name);
  2538. _el._katavorioDrag.posseRoles[posse.name] = active;
  2539. }
  2540. });
  2541. return posse;
  2542. };
  2543. /**
  2544. * Add the given element to the posse with the given id, creating the group if it at first does not exist.
  2545. * @method addToPosse
  2546. * @param {Element} el Element to add.
  2547. * @param {String...|Object...} spec Variable args parameters. Each argument can be a either a String, indicating
  2548. * the ID of a Posse to which the element should be added as an active participant, or an Object containing
  2549. * `{ id:"posseId", active:false/true}`. In the latter case, if `active` is not provided it is assumed to be
  2550. * true.
  2551. * @returns {Posse|Posse[]} The Posse(s) to which the element(s) was/were added.
  2552. */
  2553. this.addToPosse = function(el, spec) {
  2554. var posses = [];
  2555. for (var i = 1; i < arguments.length; i++) {
  2556. posses.push(_processOneSpec(el, arguments[i]));
  2557. }
  2558. return posses.length === 1 ? posses[0] : posses;
  2559. };
  2560. /**
  2561. * Sets the posse(s) for the element with the given id, creating those that do not yet exist, and removing from
  2562. * the element any current Posses that are not specified by this method call. This method will not change the
  2563. * active/passive state if it is given a posse in which the element is already a member.
  2564. * @method setPosse
  2565. * @param {Element} el Element to set posse(s) on.
  2566. * @param {String...|Object...} spec Variable args parameters. Each argument can be a either a String, indicating
  2567. * the ID of a Posse to which the element should be added as an active participant, or an Object containing
  2568. * `{ id:"posseId", active:false/true}`. In the latter case, if `active` is not provided it is assumed to be
  2569. * true.
  2570. * @returns {Posse|Posse[]} The Posse(s) to which the element(s) now belongs.
  2571. */
  2572. this.setPosse = function(el, spec) {
  2573. var posses = [];
  2574. for (var i = 1; i < arguments.length; i++) {
  2575. posses.push(_processOneSpec(el, arguments[i], true).name);
  2576. }
  2577. _each(el, function(_el) {
  2578. if (_el._katavorioDrag) {
  2579. var diff = _difference(_el._katavorioDrag.posses, posses);
  2580. var p = [];
  2581. Array.prototype.push.apply(p, _el._katavorioDrag.posses);
  2582. for (var i = 0; i < diff.length; i++) {
  2583. this.removeFromPosse(_el, diff[i]);
  2584. }
  2585. }
  2586. }.bind(this));
  2587. return posses.length === 1 ? posses[0] : posses;
  2588. };
  2589. /**
  2590. * Remove the given element from the given posse(s).
  2591. * @method removeFromPosse
  2592. * @param {Element} el Element to remove.
  2593. * @param {String...} posseId Varargs parameter: one value for each posse to remove the element from.
  2594. */
  2595. this.removeFromPosse = function(el, posseId) {
  2596. if (arguments.length < 2) throw new TypeError("No posse id provided for remove operation");
  2597. for(var i = 1; i < arguments.length; i++) {
  2598. posseId = arguments[i];
  2599. _each(el, function (_el) {
  2600. if (_el._katavorioDrag && _el._katavorioDrag.posses) {
  2601. var d = _el._katavorioDrag;
  2602. _each(posseId, function (p) {
  2603. _vanquish(_posses[p].members, d);
  2604. _vanquish(d.posses, p);
  2605. delete d.posseRoles[p];
  2606. });
  2607. }
  2608. });
  2609. }
  2610. };
  2611. /**
  2612. * Remove the given element from all Posses to which it belongs.
  2613. * @method removeFromAllPosses
  2614. * @param {Element|Element[]} el Element to remove from Posses.
  2615. */
  2616. this.removeFromAllPosses = function(el) {
  2617. _each(el, function(_el) {
  2618. if (_el._katavorioDrag && _el._katavorioDrag.posses) {
  2619. var d = _el._katavorioDrag;
  2620. _each(d.posses, function(p) {
  2621. _vanquish(_posses[p].members, d);
  2622. });
  2623. d.posses.length = 0;
  2624. d.posseRoles = {};
  2625. }
  2626. });
  2627. };
  2628. /**
  2629. * Changes the participation state for the element in the Posse with the given ID.
  2630. * @param {Element|Element[]} el Element(s) to change state for.
  2631. * @param {String} posseId ID of the Posse to change element state for.
  2632. * @param {Boolean} state True to make active, false to make passive.
  2633. */
  2634. this.setPosseState = function(el, posseId, state) {
  2635. var posse = _posses[posseId];
  2636. if (posse) {
  2637. _each(el, function(_el) {
  2638. if (_el._katavorioDrag && _el._katavorioDrag.posses) {
  2639. _el._katavorioDrag.posseRoles[posse.name] = state;
  2640. }
  2641. });
  2642. }
  2643. };
  2644. };
  2645. root.Katavorio.version = "1.0.0";
  2646. if (typeof exports !== "undefined") {
  2647. exports.Katavorio = root.Katavorio;
  2648. }
  2649. }).call(typeof window !== 'undefined' ? window : this);
  2650. (function() {
  2651. var root = this;
  2652. root.jsPlumbUtil = root.jsPlumbUtil || {};
  2653. var jsPlumbUtil = root.jsPlumbUtil;
  2654. if (typeof exports !=='undefined') { exports.jsPlumbUtil = jsPlumbUtil;}
  2655. /**
  2656. * Tests if the given object is an Array.
  2657. * @param a
  2658. */
  2659. function isArray(a) {
  2660. return Object.prototype.toString.call(a) === "[object Array]";
  2661. }
  2662. jsPlumbUtil.isArray = isArray;
  2663. /**
  2664. * Tests if the given object is a Number.
  2665. * @param n
  2666. */
  2667. function isNumber(n) {
  2668. return Object.prototype.toString.call(n) === "[object Number]";
  2669. }
  2670. jsPlumbUtil.isNumber = isNumber;
  2671. function isString(s) {
  2672. return typeof s === "string";
  2673. }
  2674. jsPlumbUtil.isString = isString;
  2675. function isBoolean(s) {
  2676. return typeof s === "boolean";
  2677. }
  2678. jsPlumbUtil.isBoolean = isBoolean;
  2679. function isNull(s) {
  2680. return s == null;
  2681. }
  2682. jsPlumbUtil.isNull = isNull;
  2683. function isObject(o) {
  2684. return o == null ? false : Object.prototype.toString.call(o) === "[object Object]";
  2685. }
  2686. jsPlumbUtil.isObject = isObject;
  2687. function isDate(o) {
  2688. return Object.prototype.toString.call(o) === "[object Date]";
  2689. }
  2690. jsPlumbUtil.isDate = isDate;
  2691. function isFunction(o) {
  2692. return Object.prototype.toString.call(o) === "[object Function]";
  2693. }
  2694. jsPlumbUtil.isFunction = isFunction;
  2695. function isNamedFunction(o) {
  2696. return isFunction(o) && o.name != null && o.name.length > 0;
  2697. }
  2698. jsPlumbUtil.isNamedFunction = isNamedFunction;
  2699. function isEmpty(o) {
  2700. for (var i in o) {
  2701. if (o.hasOwnProperty(i)) {
  2702. return false;
  2703. }
  2704. }
  2705. return true;
  2706. }
  2707. jsPlumbUtil.isEmpty = isEmpty;
  2708. function clone(a) {
  2709. if (isString(a)) {
  2710. return "" + a;
  2711. }
  2712. else if (isBoolean(a)) {
  2713. return !!a;
  2714. }
  2715. else if (isDate(a)) {
  2716. return new Date(a.getTime());
  2717. }
  2718. else if (isFunction(a)) {
  2719. return a;
  2720. }
  2721. else if (isArray(a)) {
  2722. var b = [];
  2723. for (var i = 0; i < a.length; i++) {
  2724. b.push(clone(a[i]));
  2725. }
  2726. return b;
  2727. }
  2728. else if (isObject(a)) {
  2729. var c = {};
  2730. for (var j in a) {
  2731. c[j] = clone(a[j]);
  2732. }
  2733. return c;
  2734. }
  2735. else {
  2736. return a;
  2737. }
  2738. }
  2739. jsPlumbUtil.clone = clone;
  2740. function merge(a, b, collations, overwrites) {
  2741. // first change the collations array - if present - into a lookup table, because its faster.
  2742. var cMap = {}, ar, i, oMap = {};
  2743. collations = collations || [];
  2744. overwrites = overwrites || [];
  2745. for (i = 0; i < collations.length; i++) {
  2746. cMap[collations[i]] = true;
  2747. }
  2748. for (i = 0; i < overwrites.length; i++) {
  2749. oMap[overwrites[i]] = true;
  2750. }
  2751. var c = clone(a);
  2752. for (i in b) {
  2753. if (c[i] == null || oMap[i]) {
  2754. c[i] = b[i];
  2755. }
  2756. else if (isString(b[i]) || isBoolean(b[i])) {
  2757. if (!cMap[i]) {
  2758. c[i] = b[i]; // if we dont want to collate, just copy it in.
  2759. }
  2760. else {
  2761. ar = [];
  2762. // if c's object is also an array we can keep its values.
  2763. ar.push.apply(ar, isArray(c[i]) ? c[i] : [c[i]]);
  2764. ar.push.apply(ar, isBoolean(b[i]) ? b[i] : [b[i]]);
  2765. c[i] = ar;
  2766. }
  2767. }
  2768. else {
  2769. if (isArray(b[i])) {
  2770. ar = [];
  2771. // if c's object is also an array we can keep its values.
  2772. if (isArray(c[i])) {
  2773. ar.push.apply(ar, c[i]);
  2774. }
  2775. ar.push.apply(ar, b[i]);
  2776. c[i] = ar;
  2777. }
  2778. else if (isObject(b[i])) {
  2779. // overwrite c's value with an object if it is not already one.
  2780. if (!isObject(c[i])) {
  2781. c[i] = {};
  2782. }
  2783. for (var j in b[i]) {
  2784. c[i][j] = b[i][j];
  2785. }
  2786. }
  2787. }
  2788. }
  2789. return c;
  2790. }
  2791. jsPlumbUtil.merge = merge;
  2792. function replace(inObj, path, value) {
  2793. if (inObj == null) {
  2794. return;
  2795. }
  2796. var q = inObj, t = q;
  2797. path.replace(/([^\.])+/g, function (term, lc, pos, str) {
  2798. var array = term.match(/([^\[0-9]+){1}(\[)([0-9+])/), last = pos + term.length >= str.length, _getArray = function () {
  2799. return t[array[1]] || (function () {
  2800. t[array[1]] = [];
  2801. return t[array[1]];
  2802. })();
  2803. };
  2804. if (last) {
  2805. // set term = value on current t, creating term as array if necessary.
  2806. if (array) {
  2807. _getArray()[array[3]] = value;
  2808. }
  2809. else {
  2810. t[term] = value;
  2811. }
  2812. }
  2813. else {
  2814. // set to current t[term], creating t[term] if necessary.
  2815. if (array) {
  2816. var a_1 = _getArray();
  2817. t = a_1[array[3]] || (function () {
  2818. a_1[array[3]] = {};
  2819. return a_1[array[3]];
  2820. })();
  2821. }
  2822. else {
  2823. t = t[term] || (function () {
  2824. t[term] = {};
  2825. return t[term];
  2826. })();
  2827. }
  2828. }
  2829. return "";
  2830. });
  2831. return inObj;
  2832. }
  2833. jsPlumbUtil.replace = replace;
  2834. //
  2835. // chain a list of functions, supplied by [ object, method name, args ], and return on the first
  2836. // one that returns the failValue. if none return the failValue, return the successValue.
  2837. //
  2838. function functionChain(successValue, failValue, fns) {
  2839. for (var i = 0; i < fns.length; i++) {
  2840. var o = fns[i][0][fns[i][1]].apply(fns[i][0], fns[i][2]);
  2841. if (o === failValue) {
  2842. return o;
  2843. }
  2844. }
  2845. return successValue;
  2846. }
  2847. jsPlumbUtil.functionChain = functionChain;
  2848. /**
  2849. *
  2850. * Take the given model and expand out any parameters. 'functionPrefix' is optional, and if present, helps jsplumb figure out what to do if a value is a Function.
  2851. * if you do not provide it (and doNotExpandFunctions is null, or false), jsplumb will run the given values through any functions it finds, and use the function's
  2852. * output as the value in the result. if you do provide the prefix, only functions that are named and have this prefix
  2853. * will be executed; other functions will be passed as values to the output.
  2854. *
  2855. * @param model
  2856. * @param values
  2857. * @param functionPrefix
  2858. * @param doNotExpandFunctions
  2859. * @returns {any}
  2860. */
  2861. function populate(model, values, functionPrefix, doNotExpandFunctions) {
  2862. // for a string, see if it has parameter matches, and if so, try to make the substitutions.
  2863. var getValue = function (fromString) {
  2864. var matches = fromString.match(/(\${.*?})/g);
  2865. if (matches != null) {
  2866. for (var i = 0; i < matches.length; i++) {
  2867. var val = values[matches[i].substring(2, matches[i].length - 1)] || "";
  2868. if (val != null) {
  2869. fromString = fromString.replace(matches[i], val);
  2870. }
  2871. }
  2872. }
  2873. return fromString;
  2874. };
  2875. // process one entry.
  2876. var _one = function (d) {
  2877. if (d != null) {
  2878. if (isString(d)) {
  2879. return getValue(d);
  2880. }
  2881. else if (isFunction(d) && !doNotExpandFunctions && (functionPrefix == null || (d.name || "").indexOf(functionPrefix) === 0)) {
  2882. return d(values);
  2883. }
  2884. else if (isArray(d)) {
  2885. var r = [];
  2886. for (var i = 0; i < d.length; i++) {
  2887. r.push(_one(d[i]));
  2888. }
  2889. return r;
  2890. }
  2891. else if (isObject(d)) {
  2892. var s = {};
  2893. for (var j in d) {
  2894. s[j] = _one(d[j]);
  2895. }
  2896. return s;
  2897. }
  2898. else {
  2899. return d;
  2900. }
  2901. }
  2902. };
  2903. return _one(model);
  2904. }
  2905. jsPlumbUtil.populate = populate;
  2906. /**
  2907. * Find the index of a given object in an array.
  2908. * @param a The array to search
  2909. * @param f The function to run on each element. Return true if the element matches.
  2910. * @returns {number} -1 if not found, otherwise the index in the array.
  2911. */
  2912. function findWithFunction(a, f) {
  2913. if (a) {
  2914. for (var i = 0; i < a.length; i++) {
  2915. if (f(a[i])) {
  2916. return i;
  2917. }
  2918. }
  2919. }
  2920. return -1;
  2921. }
  2922. jsPlumbUtil.findWithFunction = findWithFunction;
  2923. /**
  2924. * Remove some element from an array by matching each element in the array against some predicate function. Note that this
  2925. * is an in-place removal; the array is altered.
  2926. * @param a The array to search
  2927. * @param f The function to run on each element. Return true if the element matches.
  2928. * @returns {boolean} true if removed, false otherwise.
  2929. */
  2930. function removeWithFunction(a, f) {
  2931. var idx = findWithFunction(a, f);
  2932. if (idx > -1) {
  2933. a.splice(idx, 1);
  2934. }
  2935. return idx !== -1;
  2936. }
  2937. jsPlumbUtil.removeWithFunction = removeWithFunction;
  2938. /**
  2939. * Remove some element from an array by simple lookup in the array for the given element. Note that this
  2940. * is an in-place removal; the array is altered.
  2941. * @param l The array to search
  2942. * @param v The value to remove.
  2943. * @returns {boolean} true if removed, false otherwise.
  2944. */
  2945. function remove(l, v) {
  2946. var idx = l.indexOf(v);
  2947. if (idx > -1) {
  2948. l.splice(idx, 1);
  2949. }
  2950. return idx !== -1;
  2951. }
  2952. jsPlumbUtil.remove = remove;
  2953. /**
  2954. * Add some element to the given array, unless it is determined that it is already in the array.
  2955. * @param list The array to add the element to.
  2956. * @param item The item to add.
  2957. * @param hashFunction A function to use to determine if the given item already exists in the array.
  2958. */
  2959. function addWithFunction(list, item, hashFunction) {
  2960. if (findWithFunction(list, hashFunction) === -1) {
  2961. list.push(item);
  2962. }
  2963. }
  2964. jsPlumbUtil.addWithFunction = addWithFunction;
  2965. /**
  2966. * Add some element to a list that is contained in a map of lists.
  2967. * @param map The map of [ key -> list ] entries
  2968. * @param key The name of the list to insert into
  2969. * @param value The value to insert
  2970. * @param insertAtStart Whether or not to insert at the start; defaults to false.
  2971. */
  2972. function addToList(map, key, value, insertAtStart) {
  2973. var l = map[key];
  2974. if (l == null) {
  2975. l = [];
  2976. map[key] = l;
  2977. }
  2978. l[insertAtStart ? "unshift" : "push"](value);
  2979. return l;
  2980. }
  2981. jsPlumbUtil.addToList = addToList;
  2982. /**
  2983. * Add an item to a list, unless it is already in the list. The test for pre-existence is a simple list lookup.
  2984. * If you want to do something more complex, perhaps #addWithFunction might help.
  2985. * @param list List to add the item to
  2986. * @param item Item to add
  2987. * @param insertAtHead Whether or not to insert at the start; defaults to false.
  2988. */
  2989. function suggest(list, item, insertAtHead) {
  2990. if (list.indexOf(item) === -1) {
  2991. if (insertAtHead) {
  2992. list.unshift(item);
  2993. }
  2994. else {
  2995. list.push(item);
  2996. }
  2997. return true;
  2998. }
  2999. return false;
  3000. }
  3001. jsPlumbUtil.suggest = suggest;
  3002. /**
  3003. * Extends the given obj (which can be an array) with the given constructor function, prototype functions, and class members, any of which may be null.
  3004. * @param child
  3005. * @param parent
  3006. * @param _protoFn
  3007. */
  3008. function extend(child, parent, _protoFn) {
  3009. var i;
  3010. parent = isArray(parent) ? parent : [parent];
  3011. var _copyProtoChain = function (focus) {
  3012. var proto = focus.__proto__;
  3013. while (proto != null) {
  3014. if (proto.prototype != null) {
  3015. for (var j in proto.prototype) {
  3016. if (proto.prototype.hasOwnProperty(j) && !child.prototype.hasOwnProperty(j)) {
  3017. child.prototype[j] = proto.prototype[j];
  3018. }
  3019. }
  3020. proto = proto.prototype.__proto__;
  3021. }
  3022. else {
  3023. proto = null;
  3024. }
  3025. }
  3026. };
  3027. for (i = 0; i < parent.length; i++) {
  3028. for (var j in parent[i].prototype) {
  3029. if (parent[i].prototype.hasOwnProperty(j) && !child.prototype.hasOwnProperty(j)) {
  3030. child.prototype[j] = parent[i].prototype[j];
  3031. }
  3032. }
  3033. _copyProtoChain(parent[i]);
  3034. }
  3035. var _makeFn = function (name, protoFn) {
  3036. return function () {
  3037. for (i = 0; i < parent.length; i++) {
  3038. if (parent[i].prototype[name]) {
  3039. parent[i].prototype[name].apply(this, arguments);
  3040. }
  3041. }
  3042. return protoFn.apply(this, arguments);
  3043. };
  3044. };
  3045. var _oneSet = function (fns) {
  3046. for (var k in fns) {
  3047. child.prototype[k] = _makeFn(k, fns[k]);
  3048. }
  3049. };
  3050. if (arguments.length > 2) {
  3051. for (i = 2; i < arguments.length; i++) {
  3052. _oneSet(arguments[i]);
  3053. }
  3054. }
  3055. return child;
  3056. }
  3057. jsPlumbUtil.extend = extend;
  3058. var lut = [];
  3059. for (var i = 0; i < 256; i++) {
  3060. lut[i] = (i < 16 ? '0' : '') + (i).toString(16);
  3061. }
  3062. function uuid() {
  3063. var d0 = Math.random() * 0xffffffff | 0;
  3064. var d1 = Math.random() * 0xffffffff | 0;
  3065. var d2 = Math.random() * 0xffffffff | 0;
  3066. var d3 = Math.random() * 0xffffffff | 0;
  3067. return lut[d0 & 0xff] + lut[d0 >> 8 & 0xff] + lut[d0 >> 16 & 0xff] + lut[d0 >> 24 & 0xff] + '-' +
  3068. lut[d1 & 0xff] + lut[d1 >> 8 & 0xff] + '-' + lut[d1 >> 16 & 0x0f | 0x40] + lut[d1 >> 24 & 0xff] + '-' +
  3069. lut[d2 & 0x3f | 0x80] + lut[d2 >> 8 & 0xff] + '-' + lut[d2 >> 16 & 0xff] + lut[d2 >> 24 & 0xff] +
  3070. lut[d3 & 0xff] + lut[d3 >> 8 & 0xff] + lut[d3 >> 16 & 0xff] + lut[d3 >> 24 & 0xff];
  3071. }
  3072. jsPlumbUtil.uuid = uuid;
  3073. /**
  3074. * Trim a string.
  3075. * @param s String to trim
  3076. * @returns the String with leading and trailing whitespace removed.
  3077. */
  3078. function fastTrim(s) {
  3079. if (s == null) {
  3080. return null;
  3081. }
  3082. var str = s.replace(/^\s\s*/, ''), ws = /\s/, i = str.length;
  3083. while (ws.test(str.charAt(--i))) {
  3084. }
  3085. return str.slice(0, i + 1);
  3086. }
  3087. jsPlumbUtil.fastTrim = fastTrim;
  3088. function each(obj, fn) {
  3089. obj = obj.length == null || typeof obj === "string" ? [obj] : obj;
  3090. for (var i = 0; i < obj.length; i++) {
  3091. fn(obj[i]);
  3092. }
  3093. }
  3094. jsPlumbUtil.each = each;
  3095. function map(obj, fn) {
  3096. var o = [];
  3097. for (var i = 0; i < obj.length; i++) {
  3098. o.push(fn(obj[i]));
  3099. }
  3100. return o;
  3101. }
  3102. jsPlumbUtil.map = map;
  3103. function mergeWithParents(type, map, parentAttribute) {
  3104. parentAttribute = parentAttribute || "parent";
  3105. var _def = function (id) {
  3106. return id ? map[id] : null;
  3107. };
  3108. var _parent = function (def) {
  3109. return def ? _def(def[parentAttribute]) : null;
  3110. };
  3111. var _one = function (parent, def) {
  3112. if (parent == null) {
  3113. return def;
  3114. }
  3115. else {
  3116. var overrides = ["anchor", "anchors", "cssClass", "connector", "paintStyle", "hoverPaintStyle", "endpoint", "endpoints"];
  3117. if (def.mergeStrategy === "override") {
  3118. Array.prototype.push.apply(overrides, ["events", "overlays"]);
  3119. }
  3120. var d_1 = merge(parent, def, [], overrides);
  3121. return _one(_parent(parent), d_1);
  3122. }
  3123. };
  3124. var _getDef = function (t) {
  3125. if (t == null) {
  3126. return {};
  3127. }
  3128. if (typeof t === "string") {
  3129. return _def(t);
  3130. }
  3131. else if (t.length) {
  3132. var done = false, i = 0, _dd = void 0;
  3133. while (!done && i < t.length) {
  3134. _dd = _getDef(t[i]);
  3135. if (_dd) {
  3136. done = true;
  3137. }
  3138. else {
  3139. i++;
  3140. }
  3141. }
  3142. return _dd;
  3143. }
  3144. };
  3145. var d = _getDef(type);
  3146. if (d) {
  3147. return _one(_parent(d), d);
  3148. }
  3149. else {
  3150. return {};
  3151. }
  3152. }
  3153. jsPlumbUtil.mergeWithParents = mergeWithParents;
  3154. jsPlumbUtil.logEnabled = true;
  3155. function log() {
  3156. var args = [];
  3157. for (var _i = 0; _i < arguments.length; _i++) {
  3158. args[_i] = arguments[_i];
  3159. }
  3160. if (jsPlumbUtil.logEnabled && typeof console !== "undefined") {
  3161. try {
  3162. var msg = arguments[arguments.length - 1];
  3163. console.log(msg);
  3164. }
  3165. catch (e) {
  3166. }
  3167. }
  3168. }
  3169. jsPlumbUtil.log = log;
  3170. /**
  3171. * Wraps one function with another, creating a placeholder for the
  3172. * wrapped function if it was null. this is used to wrap the various
  3173. * drag/drop event functions - to allow jsPlumb to be notified of
  3174. * important lifecycle events without imposing itself on the user's
  3175. * drag/drop functionality.
  3176. * @method jsPlumbUtil.wrap
  3177. * @param {Function} wrappedFunction original function to wrap; may be null.
  3178. * @param {Function} newFunction function to wrap the original with.
  3179. * @param {Object} [returnOnThisValue] Optional. Indicates that the wrappedFunction should
  3180. * not be executed if the newFunction returns a value matching 'returnOnThisValue'.
  3181. * note that this is a simple comparison and only works for primitives right now.
  3182. */
  3183. function wrap(wrappedFunction, newFunction, returnOnThisValue) {
  3184. return function () {
  3185. var r = null;
  3186. try {
  3187. if (newFunction != null) {
  3188. r = newFunction.apply(this, arguments);
  3189. }
  3190. }
  3191. catch (e) {
  3192. log("jsPlumb function failed : " + e);
  3193. }
  3194. if ((wrappedFunction != null) && (returnOnThisValue == null || (r !== returnOnThisValue))) {
  3195. try {
  3196. r = wrappedFunction.apply(this, arguments);
  3197. }
  3198. catch (e) {
  3199. log("wrapped function failed : " + e);
  3200. }
  3201. }
  3202. return r;
  3203. };
  3204. }
  3205. jsPlumbUtil.wrap = wrap;
  3206. var EventGenerator = /** @class */ (function () {
  3207. function EventGenerator() {
  3208. var _this = this;
  3209. this._listeners = {};
  3210. this.eventsSuspended = false;
  3211. this.tick = false;
  3212. // this is a list of events that should re-throw any errors that occur during their dispatch.
  3213. this.eventsToDieOn = { "ready": true };
  3214. this.queue = [];
  3215. this.bind = function (event, listener, insertAtStart) {
  3216. var _one = function (evt) {
  3217. addToList(_this._listeners, evt, listener, insertAtStart);
  3218. listener.__jsPlumb = listener.__jsPlumb || {};
  3219. listener.__jsPlumb[uuid()] = evt;
  3220. };
  3221. if (typeof event === "string") {
  3222. _one(event);
  3223. }
  3224. else if (event.length != null) {
  3225. for (var i = 0; i < event.length; i++) {
  3226. _one(event[i]);
  3227. }
  3228. }
  3229. return _this;
  3230. };
  3231. this.fire = function (event, value, originalEvent) {
  3232. if (!this.tick) {
  3233. this.tick = true;
  3234. if (!this.eventsSuspended && this._listeners[event]) {
  3235. var l = this._listeners[event].length, i = 0, _gone = false, ret = null;
  3236. if (!this.shouldFireEvent || this.shouldFireEvent(event, value, originalEvent)) {
  3237. while (!_gone && i < l && ret !== false) {
  3238. // doing it this way rather than catching and then possibly re-throwing means that an error propagated by this
  3239. // method will have the whole call stack available in the debugger.
  3240. if (this.eventsToDieOn[event]) {
  3241. this._listeners[event][i].apply(this, [value, originalEvent]);
  3242. }
  3243. else {
  3244. try {
  3245. ret = this._listeners[event][i].apply(this, [value, originalEvent]);
  3246. }
  3247. catch (e) {
  3248. log("jsPlumb: fire failed for event " + event + " : " + e);
  3249. }
  3250. }
  3251. i++;
  3252. if (this._listeners == null || this._listeners[event] == null) {
  3253. _gone = true;
  3254. }
  3255. }
  3256. }
  3257. }
  3258. this.tick = false;
  3259. this._drain();
  3260. }
  3261. else {
  3262. this.queue.unshift(arguments);
  3263. }
  3264. return this;
  3265. };
  3266. this._drain = function () {
  3267. var n = _this.queue.pop();
  3268. if (n) {
  3269. _this.fire.apply(_this, n);
  3270. }
  3271. };
  3272. this.unbind = function (eventOrListener, listener) {
  3273. if (arguments.length === 0) {
  3274. this._listeners = {};
  3275. }
  3276. else if (arguments.length === 1) {
  3277. if (typeof eventOrListener === "string") {
  3278. delete this._listeners[eventOrListener];
  3279. }
  3280. else if (eventOrListener.__jsPlumb) {
  3281. var evt = void 0;
  3282. for (var i in eventOrListener.__jsPlumb) {
  3283. evt = eventOrListener.__jsPlumb[i];
  3284. remove(this._listeners[evt] || [], eventOrListener);
  3285. }
  3286. }
  3287. }
  3288. else if (arguments.length === 2) {
  3289. remove(this._listeners[eventOrListener] || [], listener);
  3290. }
  3291. return this;
  3292. };
  3293. this.getListener = function (forEvent) {
  3294. return _this._listeners[forEvent];
  3295. };
  3296. this.setSuspendEvents = function (val) {
  3297. _this.eventsSuspended = val;
  3298. };
  3299. this.isSuspendEvents = function () {
  3300. return _this.eventsSuspended;
  3301. };
  3302. this.silently = function (fn) {
  3303. _this.setSuspendEvents(true);
  3304. try {
  3305. fn();
  3306. }
  3307. catch (e) {
  3308. log("Cannot execute silent function " + e);
  3309. }
  3310. _this.setSuspendEvents(false);
  3311. };
  3312. this.cleanupListeners = function () {
  3313. for (var i in _this._listeners) {
  3314. _this._listeners[i] = null;
  3315. }
  3316. };
  3317. }
  3318. return EventGenerator;
  3319. }());
  3320. jsPlumbUtil.EventGenerator = EventGenerator;
  3321. function rotatePoint(point, center, rotation) {
  3322. var radial = [point[0] - center[0], point[1] - center[1]], cr = Math.cos(rotation / 360 * Math.PI * 2), sr = Math.sin(rotation / 360 * Math.PI * 2);
  3323. return [
  3324. (radial[0] * cr) - (radial[1] * sr) + center[0],
  3325. (radial[1] * cr) + (radial[0] * sr) + center[1],
  3326. cr,
  3327. sr
  3328. ];
  3329. }
  3330. jsPlumbUtil.rotatePoint = rotatePoint;
  3331. function rotateAnchorOrientation(orientation, rotation) {
  3332. var r = rotatePoint(orientation, [0, 0], rotation);
  3333. return [
  3334. Math.round(r[0]),
  3335. Math.round(r[1])
  3336. ];
  3337. }
  3338. jsPlumbUtil.rotateAnchorOrientation = rotateAnchorOrientation;
  3339. }).call(typeof window !== 'undefined' ? window : this);
  3340. /*
  3341. * This file contains utility functions that run in browsers only.
  3342. *
  3343. * Copyright (c) 2010 - 2020 jsPlumb (hello@jsplumbtoolkit.com)
  3344. *
  3345. * https://jsplumbtoolkit.com
  3346. * https://github.com/jsplumb/jsplumb
  3347. *
  3348. * Dual licensed under the MIT and GPL2 licenses.
  3349. */
  3350. ;(function() {
  3351. "use strict";
  3352. var root = this;
  3353. root.jsPlumbUtil.matchesSelector = function(el, selector, ctx) {
  3354. ctx = ctx || el.parentNode;
  3355. var possibles = ctx.querySelectorAll(selector);
  3356. for (var i = 0; i < possibles.length; i++) {
  3357. if (possibles[i] === el) {
  3358. return true;
  3359. }
  3360. }
  3361. return false;
  3362. };
  3363. root.jsPlumbUtil.consume = function(e, doNotPreventDefault) {
  3364. if (e.stopPropagation) {
  3365. e.stopPropagation();
  3366. }
  3367. else {
  3368. e.returnValue = false;
  3369. }
  3370. if (!doNotPreventDefault && e.preventDefault){
  3371. e.preventDefault();
  3372. }
  3373. };
  3374. /*
  3375. * Function: sizeElement
  3376. * Helper to size and position an element. You would typically use
  3377. * this when writing your own Connector or Endpoint implementation.
  3378. *
  3379. * Parameters:
  3380. * x - [int] x position for the element origin
  3381. * y - [int] y position for the element origin
  3382. * w - [int] width of the element
  3383. * h - [int] height of the element
  3384. *
  3385. */
  3386. root.jsPlumbUtil.sizeElement = function(el, x, y, w, h) {
  3387. if (el) {
  3388. el.style.height = h + "px";
  3389. el.height = h;
  3390. el.style.width = w + "px";
  3391. el.width = w;
  3392. el.style.left = x + "px";
  3393. el.style.top = y + "px";
  3394. }
  3395. };
  3396. }).call(typeof window !== 'undefined' ? window : this);
  3397. /*
  3398. * This file contains the code for working with scrollable lists.
  3399. *
  3400. * Copyright (c) 2010 - 2020 jsPlumb (hello@jsplumbtoolkit.com)
  3401. *
  3402. * https://jsplumbtoolkit.com
  3403. * https://github.com/jsplumb/jsplumb
  3404. *
  3405. * Dual licensed under the MIT and GPL2 licenses.
  3406. */
  3407. ;(function() {
  3408. var DEFAULT_OPTIONS = {
  3409. deriveAnchor:function(edge, index, ep, conn) {
  3410. return {
  3411. top:["TopRight", "TopLeft"],
  3412. bottom:["BottomRight", "BottomLeft"]
  3413. }[edge][index];
  3414. }
  3415. };
  3416. var root = this;
  3417. var ListManager = function(jsPlumbInstance, params) {
  3418. this.count = 0;
  3419. this.instance = jsPlumbInstance;
  3420. this.lists = {};
  3421. this.options = params || {};
  3422. this.instance.addList = function(el, options) {
  3423. return this.listManager.addList(el, options);
  3424. };
  3425. this.instance.removeList = function(el) {
  3426. this.listManager.removeList(el);
  3427. };
  3428. this.instance.bind("manageElement", function(p) {
  3429. //look for [jtk-scrollable-list] elements and attach scroll listeners if necessary
  3430. var scrollableLists = this.instance.getSelector(p.el, "[jtk-scrollable-list]");
  3431. for (var i = 0; i < scrollableLists.length; i++) {
  3432. this.addList(scrollableLists[i]);
  3433. }
  3434. }.bind(this));
  3435. this.instance.bind("unmanageElement", function(p) {
  3436. this.removeList(p.el);
  3437. });
  3438. this.instance.bind("connection", function(c, evt) {
  3439. if (evt == null) {
  3440. // not added by mouse. look for an ancestor of the source and/or target element that is a scrollable list, and run
  3441. // its scroll method.
  3442. this._maybeUpdateParentList(c.source);
  3443. this._maybeUpdateParentList(c.target);
  3444. }
  3445. }.bind(this));
  3446. };
  3447. root.jsPlumbListManager = ListManager;
  3448. ListManager.prototype = {
  3449. addList : function(el, options) {
  3450. var dp = this.instance.extend({}, DEFAULT_OPTIONS);
  3451. this.instance.extend(dp, this.options);
  3452. options = this.instance.extend(dp, options || {});
  3453. var id = [this.instance.getInstanceIndex(), this.count++].join("_");
  3454. this.lists[id] = new List(this.instance, el, options, id);
  3455. },
  3456. removeList:function(el) {
  3457. var list = this.lists[el._jsPlumbList];
  3458. if (list) {
  3459. list.destroy();
  3460. delete this.lists[el._jsPlumbList];
  3461. }
  3462. },
  3463. _maybeUpdateParentList:function (el) {
  3464. var parent = el.parentNode, container = this.instance.getContainer();
  3465. while(parent != null && parent !== container) {
  3466. if (parent._jsPlumbList != null && this.lists[parent._jsPlumbList] != null) {
  3467. parent._jsPlumbScrollHandler();
  3468. return
  3469. }
  3470. parent = parent.parentNode;
  3471. }
  3472. }
  3473. };
  3474. var List = function(instance, el, options, id) {
  3475. el["_jsPlumbList"] = id;
  3476. //
  3477. // Derive an anchor to use for the current situation. In contrast to the way we derive an endpoint, here we use `anchor` from the options, if present, as
  3478. // our first choice, and then `deriveAnchor` as our next choice. There is a default `deriveAnchor` implementation that uses TopRight/TopLeft for top and
  3479. // BottomRight/BottomLeft for bottom.
  3480. //
  3481. // edge - "top" or "bottom"
  3482. // index - 0 when endpoint is connection source, 1 when endpoint is connection target
  3483. // ep - the endpoint that is being proxied
  3484. // conn - the connection that is being proxied
  3485. //
  3486. function deriveAnchor(edge, index, ep, conn) {
  3487. return options.anchor ? options.anchor : options.deriveAnchor(edge, index, ep, conn);
  3488. }
  3489. //
  3490. // Derive an endpoint to use for the current situation. We'll use a `deriveEndpoint` function passed in to the options as our first choice,
  3491. // followed by `endpoint` (an endpoint spec) from the options, and failing either of those we just use the `type` of the endpoint that is being proxied.
  3492. //
  3493. // edge - "top" or "bottom"
  3494. // index - 0 when endpoint is connection source, 1 when endpoint is connection target
  3495. // endpoint - the endpoint that is being proxied
  3496. // connection - the connection that is being proxied
  3497. //
  3498. function deriveEndpoint(edge, index, ep, conn) {
  3499. return options.deriveEndpoint ? options.deriveEndpoint(edge, index, ep, conn) : options.endpoint ? options.endpoint : ep.type;
  3500. }
  3501. //
  3502. // look for a parent of the given scrollable list that is draggable, and then update the child offsets for it. this should not
  3503. // be necessary in the delegated drag stuff from the upcoming 3.0.0 release.
  3504. //
  3505. function _maybeUpdateDraggable(el) {
  3506. var parent = el.parentNode, container = instance.getContainer();
  3507. while(parent != null && parent !== container) {
  3508. if (instance.hasClass(parent, "jtk-managed")) {
  3509. instance.recalculateOffsets(parent);
  3510. return
  3511. }
  3512. parent = parent.parentNode;
  3513. }
  3514. }
  3515. var scrollHandler = function(e) {
  3516. var children = instance.getSelector(el, ".jtk-managed");
  3517. var elId = instance.getId(el);
  3518. for (var i = 0; i < children.length; i++) {
  3519. if (children[i].offsetTop < el.scrollTop) {
  3520. if (!children[i]._jsPlumbProxies) {
  3521. children[i]._jsPlumbProxies = children[i]._jsPlumbProxies || [];
  3522. instance.select({source: children[i]}).each(function (c) {
  3523. instance.proxyConnection(c, 0, el, elId, function () {
  3524. return deriveEndpoint("top", 0, c.endpoints[0], c);
  3525. }, function () {
  3526. return deriveAnchor("top", 0, c.endpoints[0], c);
  3527. });
  3528. children[i]._jsPlumbProxies.push([c, 0]);
  3529. });
  3530. instance.select({target: children[i]}).each(function (c) {
  3531. instance.proxyConnection(c, 1, el, elId, function () {
  3532. return deriveEndpoint("top", 1, c.endpoints[1], c);
  3533. }, function () {
  3534. return deriveAnchor("top", 1, c.endpoints[1], c);
  3535. });
  3536. children[i]._jsPlumbProxies.push([c, 1]);
  3537. });
  3538. }
  3539. }
  3540. //
  3541. else if (children[i].offsetTop + children[i].offsetHeight > el.scrollTop + el.offsetHeight) {
  3542. if (!children[i]._jsPlumbProxies) {
  3543. children[i]._jsPlumbProxies = children[i]._jsPlumbProxies || [];
  3544. instance.select({source: children[i]}).each(function (c) {
  3545. instance.proxyConnection(c, 0, el, elId, function () {
  3546. return deriveEndpoint("bottom", 0, c.endpoints[0], c);
  3547. }, function () {
  3548. return deriveAnchor("bottom", 0, c.endpoints[0], c);
  3549. });
  3550. children[i]._jsPlumbProxies.push([c, 0]);
  3551. });
  3552. instance.select({target: children[i]}).each(function (c) {
  3553. instance.proxyConnection(c, 1, el, elId, function () {
  3554. return deriveEndpoint("bottom", 1, c.endpoints[1], c);
  3555. }, function () {
  3556. return deriveAnchor("bottom", 1, c.endpoints[1], c);
  3557. });
  3558. children[i]._jsPlumbProxies.push([c, 1]);
  3559. });
  3560. }
  3561. } else if (children[i]._jsPlumbProxies) {
  3562. for (var j = 0; j < children[i]._jsPlumbProxies.length; j++) {
  3563. instance.unproxyConnection(children[i]._jsPlumbProxies[j][0], children[i]._jsPlumbProxies[j][1], elId);
  3564. }
  3565. delete children[i]._jsPlumbProxies;
  3566. }
  3567. instance.revalidate(children[i]);
  3568. }
  3569. _maybeUpdateDraggable(el);
  3570. };
  3571. instance.setAttribute(el, "jtk-scrollable-list", "true");
  3572. el._jsPlumbScrollHandler = scrollHandler;
  3573. instance.on(el, "scroll", scrollHandler);
  3574. scrollHandler(); // run it once; there may be connections already.
  3575. this.destroy = function() {
  3576. instance.off(el, "scroll", scrollHandler);
  3577. delete el._jsPlumbScrollHandler;
  3578. var children = instance.getSelector(el, ".jtk-managed");
  3579. var elId = instance.getId(el);
  3580. for (var i = 0; i < children.length; i++) {
  3581. if (children[i]._jsPlumbProxies) {
  3582. for (var j = 0; j < children[i]._jsPlumbProxies.length; j++) {
  3583. instance.unproxyConnection(children[i]._jsPlumbProxies[j][0], children[i]._jsPlumbProxies[j][1], elId);
  3584. }
  3585. delete children[i]._jsPlumbProxies;
  3586. }
  3587. }
  3588. };
  3589. };
  3590. }).call(typeof window !== 'undefined' ? window : this);
  3591. /*
  3592. * This file contains the core code.
  3593. *
  3594. * Copyright (c) 2010 - 2020 jsPlumb (hello@jsplumbtoolkit.com)
  3595. *
  3596. * https://jsplumbtoolkit.com
  3597. * https://github.com/jsplumb/jsplumb
  3598. *
  3599. * Dual licensed under the MIT and GPL2 licenses.
  3600. */
  3601. ;(function () {
  3602. "use strict";
  3603. var root = this;
  3604. var _ju = root.jsPlumbUtil,
  3605. // helper method to update the hover style whenever it, or paintStyle, changes.
  3606. // we use paintStyle as the foundation and merge hoverPaintStyle over the
  3607. // top.
  3608. _updateHoverStyle = function (component) {
  3609. if (component._jsPlumb.paintStyle && component._jsPlumb.hoverPaintStyle) {
  3610. var mergedHoverStyle = {};
  3611. jsPlumb.extend(mergedHoverStyle, component._jsPlumb.paintStyle);
  3612. jsPlumb.extend(mergedHoverStyle, component._jsPlumb.hoverPaintStyle);
  3613. delete component._jsPlumb.hoverPaintStyle;
  3614. // we want the fill of paintStyle to override a gradient, if possible.
  3615. if (mergedHoverStyle.gradient && component._jsPlumb.paintStyle.fill) {
  3616. delete mergedHoverStyle.gradient;
  3617. }
  3618. component._jsPlumb.hoverPaintStyle = mergedHoverStyle;
  3619. }
  3620. },
  3621. events = ["tap", "dbltap", "click", "dblclick", "mouseover", "mouseout", "mousemove", "mousedown", "mouseup", "contextmenu" ],
  3622. eventFilters = { "mouseout": "mouseleave", "mouseexit": "mouseleave" },
  3623. _updateAttachedElements = function (component, state, timestamp, sourceElement) {
  3624. var affectedElements = component.getAttachedElements();
  3625. if (affectedElements) {
  3626. for (var i = 0, j = affectedElements.length; i < j; i++) {
  3627. if (!sourceElement || sourceElement !== affectedElements[i]) {
  3628. affectedElements[i].setHover(state, true, timestamp); // tell the attached elements not to inform their own attached elements.
  3629. }
  3630. }
  3631. }
  3632. },
  3633. _splitType = function (t) {
  3634. return t == null ? null : t.split(" ");
  3635. },
  3636. _mapType = function(map, obj, typeId) {
  3637. for (var i in obj) {
  3638. map[i] = typeId;
  3639. }
  3640. },
  3641. _each = function(fn, obj) {
  3642. obj = _ju.isArray(obj) || (obj.length != null && !_ju.isString(obj)) ? obj : [ obj ];
  3643. for (var i = 0; i < obj.length; i++) {
  3644. try {
  3645. fn.apply(obj[i], [ obj[i] ]);
  3646. }
  3647. catch (e) {
  3648. _ju.log(".each iteration failed : " + e);
  3649. }
  3650. }
  3651. },
  3652. _applyTypes = function (component, params, doNotRepaint) {
  3653. if (component.getDefaultType) {
  3654. var td = component.getTypeDescriptor(), map = {};
  3655. var defType = component.getDefaultType();
  3656. var o = _ju.merge({}, defType);
  3657. _mapType(map, defType, "__default");
  3658. for (var i = 0, j = component._jsPlumb.types.length; i < j; i++) {
  3659. var tid = component._jsPlumb.types[i];
  3660. if (tid !== "__default") {
  3661. var _t = component._jsPlumb.instance.getType(tid, td);
  3662. if (_t != null) {
  3663. var overrides = ["anchor", "anchors", "connector", "paintStyle", "hoverPaintStyle", "endpoint", "endpoints", "connectorOverlays", "connectorStyle", "connectorHoverStyle", "endpointStyle", "endpointHoverStyle"];
  3664. var collations = [ ];
  3665. if (_t.mergeStrategy === "override") {
  3666. Array.prototype.push.apply(overrides, ["events", "overlays", "cssClass"]);
  3667. } else {
  3668. collations.push("cssClass");
  3669. }
  3670. o = _ju.merge(o, _t, collations, overrides);
  3671. _mapType(map, _t, tid);
  3672. }
  3673. }
  3674. }
  3675. if (params) {
  3676. o = _ju.populate(o, params, "_");
  3677. }
  3678. component.applyType(o, doNotRepaint, map);
  3679. if (!doNotRepaint) {
  3680. component.repaint();
  3681. }
  3682. }
  3683. },
  3684. // ------------------------------ BEGIN jsPlumbUIComponent --------------------------------------------
  3685. jsPlumbUIComponent = root.jsPlumbUIComponent = function (params) {
  3686. _ju.EventGenerator.apply(this, arguments);
  3687. var self = this,
  3688. a = arguments,
  3689. idPrefix = self.idPrefix,
  3690. id = idPrefix + (new Date()).getTime();
  3691. this._jsPlumb = {
  3692. instance: params._jsPlumb,
  3693. parameters: params.parameters || {},
  3694. paintStyle: null,
  3695. hoverPaintStyle: null,
  3696. paintStyleInUse: null,
  3697. hover: false,
  3698. beforeDetach: params.beforeDetach,
  3699. beforeDrop: params.beforeDrop,
  3700. overlayPlacements: [],
  3701. hoverClass: params.hoverClass || params._jsPlumb.Defaults.HoverClass,
  3702. types: [],
  3703. typeCache:{}
  3704. };
  3705. this.cacheTypeItem = function(key, item, typeId) {
  3706. this._jsPlumb.typeCache[typeId] = this._jsPlumb.typeCache[typeId] || {};
  3707. this._jsPlumb.typeCache[typeId][key] = item;
  3708. };
  3709. this.getCachedTypeItem = function(key, typeId) {
  3710. return this._jsPlumb.typeCache[typeId] ? this._jsPlumb.typeCache[typeId][key] : null;
  3711. };
  3712. this.getId = function () {
  3713. return id;
  3714. };
  3715. // ----------------------------- default type --------------------------------------------
  3716. var o = params.overlays || [], oo = {};
  3717. if (this.defaultOverlayKeys) {
  3718. for (var i = 0; i < this.defaultOverlayKeys.length; i++) {
  3719. Array.prototype.push.apply(o, this._jsPlumb.instance.Defaults[this.defaultOverlayKeys[i]] || []);
  3720. }
  3721. for (i = 0; i < o.length; i++) {
  3722. // if a string, convert to object representation so that we can store the typeid on it.
  3723. // also assign an id.
  3724. var fo = jsPlumb.convertToFullOverlaySpec(o[i]);
  3725. oo[fo[1].id] = fo;
  3726. }
  3727. }
  3728. var _defaultType = {
  3729. overlays:oo,
  3730. parameters: params.parameters || {},
  3731. scope: params.scope || this._jsPlumb.instance.getDefaultScope()
  3732. };
  3733. this.getDefaultType = function() {
  3734. return _defaultType;
  3735. };
  3736. this.appendToDefaultType = function(obj) {
  3737. for (var i in obj) {
  3738. _defaultType[i] = obj[i];
  3739. }
  3740. };
  3741. // ----------------------------- end default type --------------------------------------------
  3742. // all components can generate events
  3743. if (params.events) {
  3744. for (var evtName in params.events) {
  3745. self.bind(evtName, params.events[evtName]);
  3746. }
  3747. }
  3748. // all components get this clone function.
  3749. // TODO issue 116 showed a problem with this - it seems 'a' that is in
  3750. // the clone function's scope is shared by all invocations of it, the classic
  3751. // JS closure problem. for now, jsPlumb does a version of this inline where
  3752. // it used to call clone. but it would be nice to find some time to look
  3753. // further at this.
  3754. this.clone = function () {
  3755. var o = Object.create(this.constructor.prototype);
  3756. this.constructor.apply(o, a);
  3757. return o;
  3758. }.bind(this);
  3759. // user can supply a beforeDetach callback, which will be executed before a detach
  3760. // is performed; returning false prevents the detach.
  3761. this.isDetachAllowed = function (connection) {
  3762. var r = true;
  3763. if (this._jsPlumb.beforeDetach) {
  3764. try {
  3765. r = this._jsPlumb.beforeDetach(connection);
  3766. }
  3767. catch (e) {
  3768. _ju.log("jsPlumb: beforeDetach callback failed", e);
  3769. }
  3770. }
  3771. return r;
  3772. };
  3773. // user can supply a beforeDrop callback, which will be executed before a dropped
  3774. // connection is confirmed. user can return false to reject connection.
  3775. this.isDropAllowed = function (sourceId, targetId, scope, connection, dropEndpoint, source, target) {
  3776. var r = this._jsPlumb.instance.checkCondition("beforeDrop", {
  3777. sourceId: sourceId,
  3778. targetId: targetId,
  3779. scope: scope,
  3780. connection: connection,
  3781. dropEndpoint: dropEndpoint,
  3782. source: source, target: target
  3783. });
  3784. if (this._jsPlumb.beforeDrop) {
  3785. try {
  3786. r = this._jsPlumb.beforeDrop({
  3787. sourceId: sourceId,
  3788. targetId: targetId,
  3789. scope: scope,
  3790. connection: connection,
  3791. dropEndpoint: dropEndpoint,
  3792. source: source, target: target
  3793. });
  3794. }
  3795. catch (e) {
  3796. _ju.log("jsPlumb: beforeDrop callback failed", e);
  3797. }
  3798. }
  3799. return r;
  3800. };
  3801. var domListeners = [];
  3802. // sets the component associated with listener events. for instance, an overlay delegates
  3803. // its events back to a connector. but if the connector is swapped on the underlying connection,
  3804. // then this component must be changed. This is called by setConnector in the Connection class.
  3805. this.setListenerComponent = function (c) {
  3806. for (var i = 0; i < domListeners.length; i++) {
  3807. domListeners[i][3] = c;
  3808. }
  3809. };
  3810. };
  3811. var _removeTypeCssHelper = function (component, typeIndex) {
  3812. var typeId = component._jsPlumb.types[typeIndex],
  3813. type = component._jsPlumb.instance.getType(typeId, component.getTypeDescriptor());
  3814. if (type != null && type.cssClass && component.canvas) {
  3815. component._jsPlumb.instance.removeClass(component.canvas, type.cssClass);
  3816. }
  3817. };
  3818. _ju.extend(root.jsPlumbUIComponent, _ju.EventGenerator, {
  3819. getParameter: function (name) {
  3820. return this._jsPlumb.parameters[name];
  3821. },
  3822. setParameter: function (name, value) {
  3823. this._jsPlumb.parameters[name] = value;
  3824. },
  3825. getParameters: function () {
  3826. return this._jsPlumb.parameters;
  3827. },
  3828. setParameters: function (p) {
  3829. this._jsPlumb.parameters = p;
  3830. },
  3831. getClass:function() {
  3832. return jsPlumb.getClass(this.canvas);
  3833. },
  3834. hasClass:function(clazz) {
  3835. return jsPlumb.hasClass(this.canvas, clazz);
  3836. },
  3837. addClass: function (clazz) {
  3838. jsPlumb.addClass(this.canvas, clazz);
  3839. },
  3840. removeClass: function (clazz) {
  3841. jsPlumb.removeClass(this.canvas, clazz);
  3842. },
  3843. updateClasses: function (classesToAdd, classesToRemove) {
  3844. jsPlumb.updateClasses(this.canvas, classesToAdd, classesToRemove);
  3845. },
  3846. setType: function (typeId, params, doNotRepaint) {
  3847. this.clearTypes();
  3848. this._jsPlumb.types = _splitType(typeId) || [];
  3849. _applyTypes(this, params, doNotRepaint);
  3850. },
  3851. getType: function () {
  3852. return this._jsPlumb.types;
  3853. },
  3854. reapplyTypes: function (params, doNotRepaint) {
  3855. _applyTypes(this, params, doNotRepaint);
  3856. },
  3857. hasType: function (typeId) {
  3858. return this._jsPlumb.types.indexOf(typeId) !== -1;
  3859. },
  3860. addType: function (typeId, params, doNotRepaint) {
  3861. var t = _splitType(typeId), _cont = false;
  3862. if (t != null) {
  3863. for (var i = 0, j = t.length; i < j; i++) {
  3864. if (!this.hasType(t[i])) {
  3865. this._jsPlumb.types.push(t[i]);
  3866. _cont = true;
  3867. }
  3868. }
  3869. if (_cont) {
  3870. _applyTypes(this, params, doNotRepaint);
  3871. }
  3872. }
  3873. },
  3874. removeType: function (typeId, params, doNotRepaint) {
  3875. var t = _splitType(typeId), _cont = false, _one = function (tt) {
  3876. var idx = this._jsPlumb.types.indexOf(tt);
  3877. if (idx !== -1) {
  3878. // remove css class if necessary
  3879. _removeTypeCssHelper(this, idx);
  3880. this._jsPlumb.types.splice(idx, 1);
  3881. return true;
  3882. }
  3883. return false;
  3884. }.bind(this);
  3885. if (t != null) {
  3886. for (var i = 0, j = t.length; i < j; i++) {
  3887. _cont = _one(t[i]) || _cont;
  3888. }
  3889. if (_cont) {
  3890. _applyTypes(this, params, doNotRepaint);
  3891. }
  3892. }
  3893. },
  3894. clearTypes: function (params, doNotRepaint) {
  3895. var i = this._jsPlumb.types.length;
  3896. for (var j = 0; j < i; j++) {
  3897. _removeTypeCssHelper(this, 0);
  3898. this._jsPlumb.types.splice(0, 1);
  3899. }
  3900. _applyTypes(this, params, doNotRepaint);
  3901. },
  3902. toggleType: function (typeId, params, doNotRepaint) {
  3903. var t = _splitType(typeId);
  3904. if (t != null) {
  3905. for (var i = 0, j = t.length; i < j; i++) {
  3906. var idx = this._jsPlumb.types.indexOf(t[i]);
  3907. if (idx !== -1) {
  3908. _removeTypeCssHelper(this, idx);
  3909. this._jsPlumb.types.splice(idx, 1);
  3910. }
  3911. else {
  3912. this._jsPlumb.types.push(t[i]);
  3913. }
  3914. }
  3915. _applyTypes(this, params, doNotRepaint);
  3916. }
  3917. },
  3918. applyType: function (t, doNotRepaint) {
  3919. this.setPaintStyle(t.paintStyle, doNotRepaint);
  3920. this.setHoverPaintStyle(t.hoverPaintStyle, doNotRepaint);
  3921. if (t.parameters) {
  3922. for (var i in t.parameters) {
  3923. this.setParameter(i, t.parameters[i]);
  3924. }
  3925. }
  3926. this._jsPlumb.paintStyleInUse = this.getPaintStyle();
  3927. },
  3928. setPaintStyle: function (style, doNotRepaint) {
  3929. // this._jsPlumb.paintStyle = jsPlumb.extend({}, style);
  3930. // TODO figure out if we want components to clone paintStyle so as not to share it.
  3931. this._jsPlumb.paintStyle = style;
  3932. this._jsPlumb.paintStyleInUse = this._jsPlumb.paintStyle;
  3933. _updateHoverStyle(this);
  3934. if (!doNotRepaint) {
  3935. this.repaint();
  3936. }
  3937. },
  3938. getPaintStyle: function () {
  3939. return this._jsPlumb.paintStyle;
  3940. },
  3941. setHoverPaintStyle: function (style, doNotRepaint) {
  3942. //this._jsPlumb.hoverPaintStyle = jsPlumb.extend({}, style);
  3943. // TODO figure out if we want components to clone paintStyle so as not to share it.
  3944. this._jsPlumb.hoverPaintStyle = style;
  3945. _updateHoverStyle(this);
  3946. if (!doNotRepaint) {
  3947. this.repaint();
  3948. }
  3949. },
  3950. getHoverPaintStyle: function () {
  3951. return this._jsPlumb.hoverPaintStyle;
  3952. },
  3953. destroy: function (force) {
  3954. if (force || this.typeId == null) {
  3955. this.cleanupListeners(); // this is on EventGenerator
  3956. this.clone = null;
  3957. this._jsPlumb = null;
  3958. }
  3959. },
  3960. isHover: function () {
  3961. return this._jsPlumb.hover;
  3962. },
  3963. setHover: function (hover, ignoreAttachedElements, timestamp) {
  3964. // while dragging, we ignore these events. this keeps the UI from flashing and
  3965. // swishing and whatevering.
  3966. if (this._jsPlumb && !this._jsPlumb.instance.currentlyDragging && !this._jsPlumb.instance.isHoverSuspended()) {
  3967. this._jsPlumb.hover = hover;
  3968. var method = hover ? "addClass" : "removeClass";
  3969. if (this.canvas != null) {
  3970. if (this._jsPlumb.instance.hoverClass != null) {
  3971. this._jsPlumb.instance[method](this.canvas, this._jsPlumb.instance.hoverClass);
  3972. }
  3973. if (this._jsPlumb.hoverClass != null) {
  3974. this._jsPlumb.instance[method](this.canvas, this._jsPlumb.hoverClass);
  3975. }
  3976. }
  3977. if (this._jsPlumb.hoverPaintStyle != null) {
  3978. this._jsPlumb.paintStyleInUse = hover ? this._jsPlumb.hoverPaintStyle : this._jsPlumb.paintStyle;
  3979. if (!this._jsPlumb.instance.isSuspendDrawing()) {
  3980. timestamp = timestamp || jsPlumbUtil.uuid();
  3981. this.repaint({timestamp: timestamp, recalc: false});
  3982. }
  3983. }
  3984. // get the list of other affected elements, if supported by this component.
  3985. // for a connection, its the endpoints. for an endpoint, its the connections! surprise.
  3986. if (this.getAttachedElements && !ignoreAttachedElements) {
  3987. _updateAttachedElements(this, hover, jsPlumbUtil.uuid(), this);
  3988. }
  3989. }
  3990. }
  3991. });
  3992. // ------------------------------ END jsPlumbUIComponent --------------------------------------------
  3993. var _jsPlumbInstanceIndex = 0,
  3994. getInstanceIndex = function () {
  3995. var i = _jsPlumbInstanceIndex + 1;
  3996. _jsPlumbInstanceIndex++;
  3997. return i;
  3998. };
  3999. var jsPlumbInstance = root.jsPlumbInstance = function (_defaults) {
  4000. this.version = "2.15.6";
  4001. this.Defaults = {
  4002. Anchor: "Bottom",
  4003. Anchors: [ null, null ],
  4004. ConnectionsDetachable: true,
  4005. ConnectionOverlays: [ ],
  4006. Connector: "Bezier",
  4007. Container: null,
  4008. DoNotThrowErrors: false,
  4009. DragOptions: { },
  4010. DropOptions: { },
  4011. Endpoint: "Dot",
  4012. EndpointOverlays: [ ],
  4013. Endpoints: [ null, null ],
  4014. EndpointStyle: { fill: "#456" },
  4015. EndpointStyles: [ null, null ],
  4016. EndpointHoverStyle: null,
  4017. EndpointHoverStyles: [ null, null ],
  4018. HoverPaintStyle: null,
  4019. LabelStyle: { color: "black" },
  4020. ListStyle: { },
  4021. LogEnabled: false,
  4022. Overlays: [ ],
  4023. MaxConnections: 1,
  4024. PaintStyle: { "stroke-width": 4, stroke: "#456" },
  4025. ReattachConnections: false,
  4026. RenderMode: "svg",
  4027. Scope: "jsPlumb_DefaultScope"
  4028. };
  4029. if (_defaults) {
  4030. jsPlumb.extend(this.Defaults, _defaults);
  4031. }
  4032. this.logEnabled = this.Defaults.LogEnabled;
  4033. this._connectionTypes = {};
  4034. this._endpointTypes = {};
  4035. _ju.EventGenerator.apply(this);
  4036. var _currentInstance = this,
  4037. _instanceIndex = getInstanceIndex(),
  4038. _bb = _currentInstance.bind,
  4039. _initialDefaults = {},
  4040. _zoom = 1,
  4041. _info = function (el) {
  4042. if (el == null) {
  4043. return null;
  4044. }
  4045. else if (el.nodeType === 3 || el.nodeType === 8) {
  4046. return { el:el, text:true };
  4047. }
  4048. else {
  4049. var _el = _currentInstance.getElement(el);
  4050. return { el: _el, id: (_ju.isString(el) && _el == null) ? el : _getId(_el) };
  4051. }
  4052. };
  4053. this.getInstanceIndex = function () {
  4054. return _instanceIndex;
  4055. };
  4056. // CONVERTED
  4057. this.setZoom = function (z, repaintEverything) {
  4058. _zoom = z;
  4059. _currentInstance.fire("zoom", _zoom);
  4060. if (repaintEverything) {
  4061. _currentInstance.repaintEverything();
  4062. }
  4063. return true;
  4064. };
  4065. // CONVERTED
  4066. this.getZoom = function () {
  4067. return _zoom;
  4068. };
  4069. for (var i in this.Defaults) {
  4070. _initialDefaults[i] = this.Defaults[i];
  4071. }
  4072. var _container, _containerDelegations = [];
  4073. this.unbindContainer = function() {
  4074. if (_container != null && _containerDelegations.length > 0) {
  4075. for (var i = 0; i < _containerDelegations.length; i++) {
  4076. _currentInstance.off(_container, _containerDelegations[i][0], _containerDelegations[i][1]);
  4077. }
  4078. }
  4079. };
  4080. this.setContainer = function (c) {
  4081. this.unbindContainer();
  4082. // get container as dom element.
  4083. c = this.getElement(c);
  4084. // move existing connections and endpoints, if any.
  4085. this.select().each(function (conn) {
  4086. conn.moveParent(c);
  4087. });
  4088. this.selectEndpoints().each(function (ep) {
  4089. ep.moveParent(c);
  4090. });
  4091. // set container.
  4092. var previousContainer = _container;
  4093. _container = c;
  4094. _containerDelegations.length = 0;
  4095. var eventAliases = {
  4096. "endpointclick":"endpointClick",
  4097. "endpointdblclick":"endpointDblClick"
  4098. };
  4099. var _oneDelegateHandler = function (id, e, componentType) {
  4100. var t = e.srcElement || e.target,
  4101. jp = (t && t.parentNode ? t.parentNode._jsPlumb : null) || (t ? t._jsPlumb : null) || (t && t.parentNode && t.parentNode.parentNode ? t.parentNode.parentNode._jsPlumb : null);
  4102. if (jp) {
  4103. jp.fire(id, jp, e);
  4104. var alias = componentType ? eventAliases[componentType + id] || id : id;
  4105. // jsplumb also fires every event coming from components/overlays. That's what the test for `jp.component` is for.
  4106. _currentInstance.fire(alias, jp.component || jp, e);
  4107. }
  4108. };
  4109. var _addOneDelegate = function(eventId, selector, fn) {
  4110. _containerDelegations.push([eventId, fn]);
  4111. _currentInstance.on(_container, eventId, selector, fn);
  4112. };
  4113. // delegate one event on the container to jsplumb elements. it might be possible to
  4114. // abstract this out: each of endpoint, connection and overlay could register themselves with
  4115. // jsplumb as "component types" or whatever, and provide a suitable selector. this would be
  4116. // done by the renderer (although admittedly from 2.0 onwards we're not supporting vml anymore)
  4117. var _oneDelegate = function (id) {
  4118. // connections.
  4119. _addOneDelegate(id, ".jtk-connector", function (e) {
  4120. _oneDelegateHandler(id, e);
  4121. });
  4122. // endpoints. note they can have an enclosing div, or not.
  4123. _addOneDelegate(id, ".jtk-endpoint", function (e) {
  4124. _oneDelegateHandler(id, e, "endpoint");
  4125. });
  4126. // overlays
  4127. _addOneDelegate(id, ".jtk-overlay", function (e) {
  4128. _oneDelegateHandler(id, e);
  4129. });
  4130. };
  4131. for (var i = 0; i < events.length; i++) {
  4132. _oneDelegate(events[i]);
  4133. }
  4134. // managed elements
  4135. for (var elId in managedElements) {
  4136. var el = managedElements[elId].el;
  4137. if (el.parentNode === previousContainer) {
  4138. previousContainer.removeChild(el);
  4139. _container.appendChild(el);
  4140. }
  4141. }
  4142. };
  4143. this.getContainer = function () {
  4144. return _container;
  4145. };
  4146. this.bind = function (event, fn) {
  4147. if ("ready" === event && initialized) {
  4148. fn();
  4149. }
  4150. else {
  4151. _bb.apply(_currentInstance, [event, fn]);
  4152. }
  4153. };
  4154. _currentInstance.importDefaults = function (d) {
  4155. for (var i in d) {
  4156. _currentInstance.Defaults[i] = d[i];
  4157. }
  4158. if (d.Container) {
  4159. _currentInstance.setContainer(d.Container);
  4160. }
  4161. return _currentInstance;
  4162. };
  4163. _currentInstance.restoreDefaults = function () {
  4164. _currentInstance.Defaults = jsPlumb.extend({}, _initialDefaults);
  4165. return _currentInstance;
  4166. };
  4167. var log = null,
  4168. initialized = false,
  4169. // TODO remove from window scope
  4170. connections = [],
  4171. // map of element id -> endpoint lists. an element can have an arbitrary
  4172. // number of endpoints on it, and not all of them have to be connected
  4173. // to anything.
  4174. endpointsByElement = {},
  4175. endpointsByUUID = {},
  4176. managedElements = {},
  4177. offsets = {},
  4178. offsetTimestamps = {},
  4179. draggableStates = {},
  4180. connectionBeingDragged = false,
  4181. sizes = [],
  4182. _suspendDrawing = false,
  4183. _suspendedAt = null,
  4184. DEFAULT_SCOPE = this.Defaults.Scope,
  4185. _curIdStamp = 1,
  4186. _idstamp = function () {
  4187. return "" + _curIdStamp++;
  4188. },
  4189. //
  4190. // appends an element to some other element, which is calculated as follows:
  4191. //
  4192. // 1. if Container exists, use that element.
  4193. // 2. if the 'parent' parameter exists, use that.
  4194. // 3. otherwise just use the root element.
  4195. //
  4196. //
  4197. _appendElement = function (el, parent) {
  4198. if (_container) {
  4199. _container.appendChild(el);
  4200. }
  4201. else if (!parent) {
  4202. this.appendToRoot(el);
  4203. }
  4204. else {
  4205. this.getElement(parent).appendChild(el);
  4206. }
  4207. }.bind(this),
  4208. //
  4209. // Draws an endpoint and its connections. this is the main entry point into drawing connections as well
  4210. // as endpoints, since jsPlumb is endpoint-centric under the hood.
  4211. //
  4212. // @param element element to draw (of type library specific element object)
  4213. // @param ui UI object from current library's event system. optional.
  4214. // @param timestamp timestamp for this paint cycle. used to speed things up a little by cutting down the amount of offset calculations we do.
  4215. // @param clearEdits defaults to false; indicates that mouse edits for connectors should be cleared
  4216. ///
  4217. _draw = function (element, ui, timestamp, clearEdits) {
  4218. var drawResult = { c:[], e:[] };
  4219. if (!_suspendDrawing) {
  4220. element = _currentInstance.getElement(element);
  4221. if (element != null) {
  4222. var id = _getId(element),
  4223. repaintEls = element.querySelectorAll(".jtk-managed");
  4224. if (timestamp == null) {
  4225. timestamp = jsPlumbUtil.uuid();
  4226. }
  4227. // update the offset of everything _before_ we try to draw anything.
  4228. var o = _updateOffset({elId: id, offset: ui, recalc: false, timestamp: timestamp});
  4229. for (var i = 0; i < repaintEls.length; i++) {
  4230. _updateOffset({
  4231. elId: repaintEls[i].getAttribute("id"),
  4232. // offset: {
  4233. // left: o.o.left + repaintEls[i].offset.left,
  4234. // top: o.o.top + repaintEls[i].offset.top
  4235. // },
  4236. recalc: true,
  4237. timestamp: timestamp
  4238. });
  4239. }
  4240. var d2 = _currentInstance.router.redraw(id, ui, timestamp, null, clearEdits);
  4241. Array.prototype.push.apply(drawResult.c, d2.c);
  4242. Array.prototype.push.apply(drawResult.e, d2.e);
  4243. if (repaintEls) {
  4244. for (var j = 0; j < repaintEls.length; j++) {
  4245. d2 = _currentInstance.router.redraw(repaintEls[j].getAttribute("id"), null, timestamp, null, clearEdits, true);
  4246. Array.prototype.push.apply(drawResult.c, d2.c);
  4247. Array.prototype.push.apply(drawResult.e, d2.e);
  4248. }
  4249. }
  4250. }
  4251. }
  4252. return drawResult;
  4253. },
  4254. //
  4255. // gets an Endpoint by uuid.
  4256. //
  4257. _getEndpoint = function (uuid) {
  4258. return endpointsByUUID[uuid];
  4259. },
  4260. /**
  4261. * inits a draggable if it's not already initialised.
  4262. * TODO: somehow abstract this to the adapter, because the concept of "draggable" has no
  4263. * place on the server.
  4264. */
  4265. _scopeMatch = function (e1, e2) {
  4266. var s1 = e1.scope.split(/\s/), s2 = e2.scope.split(/\s/);
  4267. for (var i = 0; i < s1.length; i++) {
  4268. for (var j = 0; j < s2.length; j++) {
  4269. if (s2[j] === s1[i]) {
  4270. return true;
  4271. }
  4272. }
  4273. }
  4274. return false;
  4275. },
  4276. _mergeOverrides = function (def, values) {
  4277. var m = jsPlumb.extend({}, def);
  4278. for (var i in values) {
  4279. if (values[i]) {
  4280. m[i] = values[i];
  4281. }
  4282. }
  4283. return m;
  4284. },
  4285. /*
  4286. * prepares a final params object that can be passed to _newConnection, taking into account defaults, events, etc.
  4287. */
  4288. _prepareConnectionParams = function (params, referenceParams) {
  4289. var _p = jsPlumb.extend({ }, params);
  4290. if (referenceParams) {
  4291. jsPlumb.extend(_p, referenceParams);
  4292. }
  4293. // hotwire endpoints passed as source or target to sourceEndpoint/targetEndpoint, respectively.
  4294. if (_p.source) {
  4295. if (_p.source.endpoint) {
  4296. _p.sourceEndpoint = _p.source;
  4297. }
  4298. else {
  4299. _p.source = _currentInstance.getElement(_p.source);
  4300. }
  4301. }
  4302. if (_p.target) {
  4303. if (_p.target.endpoint) {
  4304. _p.targetEndpoint = _p.target;
  4305. }
  4306. else {
  4307. _p.target = _currentInstance.getElement(_p.target);
  4308. }
  4309. }
  4310. // test for endpoint uuids to connect
  4311. if (params.uuids) {
  4312. _p.sourceEndpoint = _getEndpoint(params.uuids[0]);
  4313. _p.targetEndpoint = _getEndpoint(params.uuids[1]);
  4314. }
  4315. // now ensure that if we do have Endpoints already, they're not full.
  4316. // source:
  4317. if (_p.sourceEndpoint && _p.sourceEndpoint.isFull()) {
  4318. _ju.log(_currentInstance, "could not add connection; source endpoint is full");
  4319. return;
  4320. }
  4321. // target:
  4322. if (_p.targetEndpoint && _p.targetEndpoint.isFull()) {
  4323. _ju.log(_currentInstance, "could not add connection; target endpoint is full");
  4324. return;
  4325. }
  4326. // if source endpoint mandates connection type and nothing specified in our params, use it.
  4327. if (!_p.type && _p.sourceEndpoint) {
  4328. _p.type = _p.sourceEndpoint.connectionType;
  4329. }
  4330. // copy in any connectorOverlays that were specified on the source endpoint.
  4331. // it doesnt copy target endpoint overlays. i'm not sure if we want it to or not.
  4332. if (_p.sourceEndpoint && _p.sourceEndpoint.connectorOverlays) {
  4333. _p.overlays = _p.overlays || [];
  4334. for (var i = 0, j = _p.sourceEndpoint.connectorOverlays.length; i < j; i++) {
  4335. _p.overlays.push(_p.sourceEndpoint.connectorOverlays[i]);
  4336. }
  4337. }
  4338. // scope
  4339. if (_p.sourceEndpoint && _p.sourceEndpoint.scope) {
  4340. _p.scope = _p.sourceEndpoint.scope;
  4341. }
  4342. // pointer events
  4343. if (!_p["pointer-events"] && _p.sourceEndpoint && _p.sourceEndpoint.connectorPointerEvents) {
  4344. _p["pointer-events"] = _p.sourceEndpoint.connectorPointerEvents;
  4345. }
  4346. var _addEndpoint = function (el, def, idx) {
  4347. var params = _mergeOverrides(def, {
  4348. anchor: _p.anchors ? _p.anchors[idx] : _p.anchor,
  4349. endpoint: _p.endpoints ? _p.endpoints[idx] : _p.endpoint,
  4350. paintStyle: _p.endpointStyles ? _p.endpointStyles[idx] : _p.endpointStyle,
  4351. hoverPaintStyle: _p.endpointHoverStyles ? _p.endpointHoverStyles[idx] : _p.endpointHoverStyle
  4352. });
  4353. return _currentInstance.addEndpoint(el, params);
  4354. };
  4355. // check for makeSource/makeTarget specs.
  4356. var _oneElementDef = function (type, idx, defs, matchType) {
  4357. if (_p[type] && !_p[type].endpoint && !_p[type + "Endpoint"] && !_p.newConnection) {
  4358. var tid = _getId(_p[type]), tep = defs[tid];
  4359. tep = tep ? tep[matchType] : null;
  4360. if (tep) {
  4361. // if not enabled, return.
  4362. if (!tep.enabled) {
  4363. return false;
  4364. }
  4365. var epDef = jsPlumb.extend({}, tep.def);
  4366. delete epDef.label;
  4367. var newEndpoint = tep.endpoint != null && tep.endpoint._jsPlumb ? tep.endpoint : _addEndpoint(_p[type], epDef, idx);
  4368. if (newEndpoint.isFull()) {
  4369. return false;
  4370. }
  4371. _p[type + "Endpoint"] = newEndpoint;
  4372. if (!_p.scope && epDef.scope) {
  4373. _p.scope = epDef.scope;
  4374. } // provide scope if not already provided and endpoint def has one.
  4375. if (tep.uniqueEndpoint) {
  4376. if (!tep.endpoint) {
  4377. tep.endpoint = newEndpoint;
  4378. newEndpoint.setDeleteOnEmpty(false);
  4379. }
  4380. else {
  4381. newEndpoint.finalEndpoint = tep.endpoint;
  4382. }
  4383. } else {
  4384. newEndpoint.setDeleteOnEmpty(true);
  4385. }
  4386. //
  4387. // copy in connector overlays if present on the source definition.
  4388. //
  4389. if (idx === 0 && tep.def.connectorOverlays) {
  4390. _p.overlays = _p.overlays || [];
  4391. Array.prototype.push.apply(_p.overlays, tep.def.connectorOverlays);
  4392. }
  4393. }
  4394. }
  4395. };
  4396. if (_oneElementDef("source", 0, this.sourceEndpointDefinitions, _p.type || "default") === false) {
  4397. return;
  4398. }
  4399. if (_oneElementDef("target", 1, this.targetEndpointDefinitions, _p.type || "default") === false) {
  4400. return;
  4401. }
  4402. // last, ensure scopes match
  4403. if (_p.sourceEndpoint && _p.targetEndpoint) {
  4404. if (!_scopeMatch(_p.sourceEndpoint, _p.targetEndpoint)) {
  4405. _p = null;
  4406. }
  4407. }
  4408. return _p;
  4409. }.bind(_currentInstance),
  4410. _newConnection = function (params) {
  4411. var connectionFunc = _currentInstance.Defaults.ConnectionType || _currentInstance.getDefaultConnectionType();
  4412. params._jsPlumb = _currentInstance;
  4413. params.newConnection = _newConnection;
  4414. params.newEndpoint = _newEndpoint;
  4415. params.endpointsByUUID = endpointsByUUID;
  4416. params.endpointsByElement = endpointsByElement;
  4417. params.finaliseConnection = _finaliseConnection;
  4418. params.id = "con_" + _idstamp();
  4419. var con = new connectionFunc(params);
  4420. // if the connection is draggable, then maybe we need to tell the target endpoint to init the
  4421. // dragging code. it won't run again if it already configured to be draggable.
  4422. if (con.isDetachable()) {
  4423. con.endpoints[0].initDraggable("_jsPlumbSource");
  4424. con.endpoints[1].initDraggable("_jsPlumbTarget");
  4425. }
  4426. return con;
  4427. },
  4428. //
  4429. // adds the connection to the backing model, fires an event if necessary and then redraws
  4430. //
  4431. _finaliseConnection = _currentInstance.finaliseConnection = function (jpc, params, originalEvent, doInformAnchorManager) {
  4432. params = params || {};
  4433. // add to list of connections (by scope).
  4434. if (!jpc.suspendedEndpoint) {
  4435. connections.push(jpc);
  4436. }
  4437. jpc.pending = null;
  4438. // turn off isTemporarySource on the source endpoint (only viable on first draw)
  4439. jpc.endpoints[0].isTemporarySource = false;
  4440. // always inform the anchor manager
  4441. // except that if jpc has a suspended endpoint it's not true to say the
  4442. // connection is new; it has just (possibly) moved. the question is whether
  4443. // to make that call here or in the anchor manager. i think perhaps here.
  4444. if (doInformAnchorManager !== false) {
  4445. _currentInstance.router.newConnection(jpc);
  4446. }
  4447. // force a paint
  4448. _draw(jpc.source);
  4449. // fire an event
  4450. if (!params.doNotFireConnectionEvent && params.fireEvent !== false) {
  4451. var eventArgs = {
  4452. connection: jpc,
  4453. source: jpc.source, target: jpc.target,
  4454. sourceId: jpc.sourceId, targetId: jpc.targetId,
  4455. sourceEndpoint: jpc.endpoints[0], targetEndpoint: jpc.endpoints[1]
  4456. };
  4457. _currentInstance.fire("connection", eventArgs, originalEvent);
  4458. }
  4459. },
  4460. /*
  4461. factory method to prepare a new endpoint. this should always be used instead of creating Endpoints
  4462. manually, since this method attaches event listeners and an id.
  4463. */
  4464. _newEndpoint = function (params, id) {
  4465. var endpointFunc = _currentInstance.Defaults.EndpointType || jsPlumb.Endpoint;
  4466. var _p = jsPlumb.extend({}, params);
  4467. //delete _p.label; // not supported by endpoint.
  4468. _p._jsPlumb = _currentInstance;
  4469. _p.newConnection = _newConnection;
  4470. _p.newEndpoint = _newEndpoint;
  4471. _p.endpointsByUUID = endpointsByUUID;
  4472. _p.endpointsByElement = endpointsByElement;
  4473. _p.fireDetachEvent = fireDetachEvent;
  4474. _p.elementId = id || _getId(_p.source);
  4475. var ep = new endpointFunc(_p);
  4476. ep.id = "ep_" + _idstamp();
  4477. _manage(_p.elementId, _p.source);
  4478. if (!jsPlumb.headless) {
  4479. _currentInstance.getDragManager().endpointAdded(_p.source, id);
  4480. }
  4481. return ep;
  4482. },
  4483. /*
  4484. * performs the given function operation on all the connections found
  4485. * for the given element id; this means we find all the endpoints for
  4486. * the given element, and then for each endpoint find the connectors
  4487. * connected to it. then we pass each connection in to the given
  4488. * function.
  4489. */
  4490. _operation = function (elId, func, endpointFunc) {
  4491. var endpoints = endpointsByElement[elId];
  4492. if (endpoints && endpoints.length) {
  4493. for (var i = 0, ii = endpoints.length; i < ii; i++) {
  4494. for (var j = 0, jj = endpoints[i].connections.length; j < jj; j++) {
  4495. var retVal = func(endpoints[i].connections[j]);
  4496. // if the function passed in returns true, we exit.
  4497. // most functions return false.
  4498. if (retVal) {
  4499. return;
  4500. }
  4501. }
  4502. if (endpointFunc) {
  4503. endpointFunc(endpoints[i]);
  4504. }
  4505. }
  4506. }
  4507. },
  4508. _setDraggable = function (element, draggable) {
  4509. return jsPlumb.each(element, function (el) {
  4510. if (_currentInstance.isDragSupported(el)) {
  4511. draggableStates[_currentInstance.getAttribute(el, "id")] = draggable;
  4512. _currentInstance.setElementDraggable(el, draggable);
  4513. }
  4514. });
  4515. },
  4516. /*
  4517. * private method to do the business of hiding/showing.
  4518. *
  4519. * @param el
  4520. * either Id of the element in question or a library specific
  4521. * object for the element.
  4522. * @param state
  4523. * String specifying a value for the css 'display' property
  4524. * ('block' or 'none').
  4525. */
  4526. _setVisible = function (el, state, alsoChangeEndpoints) {
  4527. state = state === "block";
  4528. var endpointFunc = null;
  4529. if (alsoChangeEndpoints) {
  4530. endpointFunc = function (ep) {
  4531. ep.setVisible(state, true, true);
  4532. };
  4533. }
  4534. var info = _info(el);
  4535. _operation(info.id, function (jpc) {
  4536. if (state && alsoChangeEndpoints) {
  4537. // this test is necessary because this functionality is new, and i wanted to maintain backwards compatibility.
  4538. // this block will only set a connection to be visible if the other endpoint in the connection is also visible.
  4539. var oidx = jpc.sourceId === info.id ? 1 : 0;
  4540. if (jpc.endpoints[oidx].isVisible()) {
  4541. jpc.setVisible(true);
  4542. }
  4543. }
  4544. else { // the default behaviour for show, and what always happens for hide, is to just set the visibility without getting clever.
  4545. jpc.setVisible(state);
  4546. }
  4547. }, endpointFunc);
  4548. },
  4549. /**
  4550. * private method to do the business of toggling hiding/showing.
  4551. */
  4552. _toggleVisible = function (elId, changeEndpoints) {
  4553. var endpointFunc = null;
  4554. if (changeEndpoints) {
  4555. endpointFunc = function (ep) {
  4556. var state = ep.isVisible();
  4557. ep.setVisible(!state);
  4558. };
  4559. }
  4560. _operation(elId, function (jpc) {
  4561. var state = jpc.isVisible();
  4562. jpc.setVisible(!state);
  4563. }, endpointFunc);
  4564. },
  4565. // TODO comparison performance
  4566. _getCachedData = function (elId) {
  4567. var o = offsets[elId];
  4568. if (!o) {
  4569. return _updateOffset({elId: elId});
  4570. }
  4571. else {
  4572. return {o: o, s: sizes[elId]};
  4573. }
  4574. },
  4575. /**
  4576. * gets an id for the given element, creating and setting one if
  4577. * necessary. the id is of the form
  4578. *
  4579. * jsPlumb_<instance index>_<index in instance>
  4580. *
  4581. * where "index in instance" is a monotonically increasing integer that starts at 0,
  4582. * for each instance. this method is used not only to assign ids to elements that do not
  4583. * have them but also to connections and endpoints.
  4584. */
  4585. _getId = function (element, uuid, doNotCreateIfNotFound) {
  4586. if (_ju.isString(element)) {
  4587. return element;
  4588. }
  4589. if (element == null) {
  4590. return null;
  4591. }
  4592. var id = _currentInstance.getAttribute(element, "id");
  4593. if (!id || id === "undefined") {
  4594. // check if fixed uuid parameter is given
  4595. if (arguments.length === 2 && arguments[1] !== undefined) {
  4596. id = uuid;
  4597. }
  4598. else if (arguments.length === 1 || (arguments.length === 3 && !arguments[2])) {
  4599. id = "jsPlumb_" + _instanceIndex + "_" + _idstamp();
  4600. }
  4601. if (!doNotCreateIfNotFound) {
  4602. _currentInstance.setAttribute(element, "id", id);
  4603. }
  4604. }
  4605. return id;
  4606. };
  4607. this.setConnectionBeingDragged = function (v) {
  4608. connectionBeingDragged = v;
  4609. };
  4610. this.isConnectionBeingDragged = function () {
  4611. return connectionBeingDragged;
  4612. };
  4613. /**
  4614. * Returns a map of all the elements this jsPlumbInstance is currently managing.
  4615. * @returns {Object} Map of [id-> {el, endpoint[], connection, position}] information.
  4616. */
  4617. this.getManagedElements = function() {
  4618. return managedElements;
  4619. };
  4620. this.connectorClass = "jtk-connector";
  4621. this.connectorOutlineClass = "jtk-connector-outline";
  4622. this.connectedClass = "jtk-connected";
  4623. this.hoverClass = "jtk-hover";
  4624. this.endpointClass = "jtk-endpoint";
  4625. this.endpointConnectedClass = "jtk-endpoint-connected";
  4626. this.endpointFullClass = "jtk-endpoint-full";
  4627. this.endpointDropAllowedClass = "jtk-endpoint-drop-allowed";
  4628. this.endpointDropForbiddenClass = "jtk-endpoint-drop-forbidden";
  4629. this.overlayClass = "jtk-overlay";
  4630. this.draggingClass = "jtk-dragging";// CONVERTED
  4631. this.elementDraggingClass = "jtk-element-dragging";// CONVERTED
  4632. this.sourceElementDraggingClass = "jtk-source-element-dragging"; // CONVERTED
  4633. this.targetElementDraggingClass = "jtk-target-element-dragging";// CONVERTED
  4634. this.endpointAnchorClassPrefix = "jtk-endpoint-anchor";
  4635. this.hoverSourceClass = "jtk-source-hover";
  4636. this.hoverTargetClass = "jtk-target-hover";
  4637. this.dragSelectClass = "jtk-drag-select";
  4638. this.Anchors = {};
  4639. this.Connectors = { "svg": {} };
  4640. this.Endpoints = { "svg": {} };
  4641. this.Overlays = { "svg": {} } ;
  4642. this.ConnectorRenderers = {};
  4643. this.SVG = "svg";
  4644. // --------------------------- jsPlumbInstance public API ---------------------------------------------------------
  4645. this.addEndpoint = function (el, params, referenceParams) {
  4646. referenceParams = referenceParams || {};
  4647. var p = jsPlumb.extend({}, referenceParams);
  4648. jsPlumb.extend(p, params);
  4649. p.endpoint = p.endpoint || _currentInstance.Defaults.Endpoint;
  4650. p.paintStyle = p.paintStyle || _currentInstance.Defaults.EndpointStyle;
  4651. var results = [],
  4652. inputs = (_ju.isArray(el) || (el.length != null && !_ju.isString(el))) ? el : [ el ];
  4653. for (var i = 0, j = inputs.length; i < j; i++) {
  4654. p.source = _currentInstance.getElement(inputs[i]);
  4655. _ensureContainer(p.source);
  4656. var id = _getId(p.source), e = _newEndpoint(p, id);
  4657. // ensure element is managed. force a recalc if drawing not suspended, to ensure the cached value is fresh
  4658. var myOffset = _manage(id, p.source, null, !_suspendDrawing).info.o;
  4659. _ju.addToList(endpointsByElement, id, e);
  4660. if (!_suspendDrawing) {
  4661. e.paint({
  4662. anchorLoc: e.anchor.compute(
  4663. { xy: [ myOffset.left, myOffset.top ],
  4664. wh: sizes[id],
  4665. element: e,
  4666. timestamp: _suspendedAt,
  4667. rotation:this.getRotation(id)
  4668. }),
  4669. timestamp: _suspendedAt
  4670. });
  4671. }
  4672. results.push(e);
  4673. }
  4674. return results.length === 1 ? results[0] : results;
  4675. };
  4676. this.addEndpoints = function (el, endpoints, referenceParams) {
  4677. var results = [];
  4678. for (var i = 0, j = endpoints.length; i < j; i++) {
  4679. var e = _currentInstance.addEndpoint(el, endpoints[i], referenceParams);
  4680. if (_ju.isArray(e)) {
  4681. Array.prototype.push.apply(results, e);
  4682. }
  4683. else {
  4684. results.push(e);
  4685. }
  4686. }
  4687. return results;
  4688. };
  4689. this.animate = function (el, properties, options) {
  4690. if (!this.animationSupported) {
  4691. return false;
  4692. }
  4693. options = options || {};
  4694. var del = _currentInstance.getElement(el),
  4695. id = _getId(del),
  4696. stepFunction = jsPlumb.animEvents.step,
  4697. completeFunction = jsPlumb.animEvents.complete;
  4698. options[stepFunction] = _ju.wrap(options[stepFunction], function () {
  4699. _currentInstance.revalidate(id);
  4700. });
  4701. // onComplete repaints, just to make sure everything looks good at the end of the animation.
  4702. options[completeFunction] = _ju.wrap(options[completeFunction], function () {
  4703. _currentInstance.revalidate(id);
  4704. });
  4705. _currentInstance.doAnimate(del, properties, options);
  4706. };
  4707. /**
  4708. * checks for a listener for the given condition, executing it if found, passing in the given value.
  4709. * condition listeners would have been attached using "bind" (which is, you could argue, now overloaded, since
  4710. * firing click events etc is a bit different to what this does). i thought about adding a "bindCondition"
  4711. * or something, but decided against it, for the sake of simplicity. jsPlumb will never fire one of these
  4712. * condition events anyway.
  4713. */
  4714. this.checkCondition = function (conditionName, args) {
  4715. var l = _currentInstance.getListener(conditionName),
  4716. r = true;
  4717. if (l && l.length > 0) {
  4718. var values = Array.prototype.slice.call(arguments, 1);
  4719. try {
  4720. for (var i = 0, j = l.length; i < j; i++) {
  4721. r = r && l[i].apply(l[i], values);
  4722. }
  4723. }
  4724. catch (e) {
  4725. _ju.log(_currentInstance, "cannot check condition [" + conditionName + "]" + e);
  4726. }
  4727. }
  4728. return r;
  4729. };
  4730. this.connect = function (params, referenceParams) {
  4731. // prepare a final set of parameters to create connection with
  4732. var _p = _prepareConnectionParams(params, referenceParams), jpc;
  4733. // TODO probably a nicer return value if the connection was not made. _prepareConnectionParams
  4734. // will return null (and log something) if either endpoint was full. what would be nicer is to
  4735. // create a dedicated 'error' object.
  4736. if (_p) {
  4737. if (_p.source == null && _p.sourceEndpoint == null) {
  4738. _ju.log("Cannot establish connection - source does not exist");
  4739. return;
  4740. }
  4741. if (_p.target == null && _p.targetEndpoint == null) {
  4742. _ju.log("Cannot establish connection - target does not exist");
  4743. return;
  4744. }
  4745. _ensureContainer(_p.source);
  4746. // create the connection. it is not yet registered
  4747. jpc = _newConnection(_p);
  4748. // now add it the model, fire an event, and redraw
  4749. _finaliseConnection(jpc, _p);
  4750. }
  4751. return jpc;
  4752. };
  4753. var stTypes = [
  4754. { el: "source", elId: "sourceId", epDefs: "sourceEndpointDefinitions" },
  4755. { el: "target", elId: "targetId", epDefs: "targetEndpointDefinitions" }
  4756. ];
  4757. var _set = function (c, el, idx, doNotRepaint) {
  4758. var ep, _st = stTypes[idx], cId = c[_st.elId], cEl = c[_st.el], sid, sep,
  4759. oldEndpoint = c.endpoints[idx];
  4760. var evtParams = {
  4761. index: idx,
  4762. originalSourceId: idx === 0 ? cId : c.sourceId,
  4763. newSourceId: c.sourceId,
  4764. originalTargetId: idx === 1 ? cId : c.targetId,
  4765. newTargetId: c.targetId,
  4766. connection: c
  4767. };
  4768. if (el.constructor === jsPlumb.Endpoint) {
  4769. ep = el;
  4770. ep.addConnection(c);
  4771. el = ep.element;
  4772. }
  4773. else {
  4774. sid = _getId(el);
  4775. sep = this[_st.epDefs][sid];
  4776. if (sid === c[_st.elId]) {
  4777. ep = null; // dont change source/target if the element is already the one given.
  4778. }
  4779. else if (sep) {
  4780. for (var t in sep) {
  4781. if (!sep[t].enabled) {
  4782. return;
  4783. }
  4784. ep = sep[t].endpoint != null && sep[t].endpoint._jsPlumb ? sep[t].endpoint : this.addEndpoint(el, sep[t].def);
  4785. if (sep[t].uniqueEndpoint) {
  4786. sep[t].endpoint = ep;
  4787. }
  4788. ep.addConnection(c);
  4789. }
  4790. }
  4791. else {
  4792. ep = c.makeEndpoint(idx === 0, el, sid);
  4793. }
  4794. }
  4795. if (ep != null) {
  4796. oldEndpoint.detachFromConnection(c);
  4797. c.endpoints[idx] = ep;
  4798. c[_st.el] = ep.element;
  4799. c[_st.elId] = ep.elementId;
  4800. evtParams[idx === 0 ? "newSourceId" : "newTargetId"] = ep.elementId;
  4801. fireMoveEvent(evtParams);
  4802. if (!doNotRepaint) {
  4803. c.repaint();
  4804. }
  4805. }
  4806. evtParams.element = el;
  4807. return evtParams;
  4808. }.bind(this);
  4809. this.setSource = function (connection, el, doNotRepaint) {
  4810. var p = _set(connection, el, 0, doNotRepaint);
  4811. this.router.sourceOrTargetChanged(p.originalSourceId, p.newSourceId, connection, p.el, 0);
  4812. };
  4813. this.setTarget = function (connection, el, doNotRepaint) {
  4814. var p = _set(connection, el, 1, doNotRepaint);
  4815. this.router.sourceOrTargetChanged(p.originalTargetId, p.newTargetId, connection, p.el, 1);
  4816. };
  4817. this.deleteEndpoint = function (object, dontUpdateHover, deleteAttachedObjects) {
  4818. var endpoint = (typeof object === "string") ? endpointsByUUID[object] : object;
  4819. if (endpoint) {
  4820. _currentInstance.deleteObject({ endpoint: endpoint, dontUpdateHover: dontUpdateHover, deleteAttachedObjects:deleteAttachedObjects });
  4821. }
  4822. return _currentInstance;
  4823. };
  4824. this.deleteEveryEndpoint = function () {
  4825. var _is = _currentInstance.setSuspendDrawing(true);
  4826. for (var id in endpointsByElement) {
  4827. var endpoints = endpointsByElement[id];
  4828. if (endpoints && endpoints.length) {
  4829. for (var i = 0, j = endpoints.length; i < j; i++) {
  4830. _currentInstance.deleteEndpoint(endpoints[i], true);
  4831. }
  4832. }
  4833. }
  4834. endpointsByElement = {};
  4835. managedElements = {};
  4836. endpointsByUUID = {};
  4837. offsets = {};
  4838. offsetTimestamps = {};
  4839. _currentInstance.router.reset();
  4840. var dm = _currentInstance.getDragManager();
  4841. if (dm) {
  4842. dm.reset();
  4843. }
  4844. if (!_is) {
  4845. _currentInstance.setSuspendDrawing(false);
  4846. }
  4847. return _currentInstance;
  4848. };
  4849. var fireDetachEvent = function (jpc, doFireEvent, originalEvent) {
  4850. // may have been given a connection, or in special cases, an object
  4851. var connType = _currentInstance.Defaults.ConnectionType || _currentInstance.getDefaultConnectionType(),
  4852. argIsConnection = jpc.constructor === connType,
  4853. params = argIsConnection ? {
  4854. connection: jpc,
  4855. source: jpc.source, target: jpc.target,
  4856. sourceId: jpc.sourceId, targetId: jpc.targetId,
  4857. sourceEndpoint: jpc.endpoints[0], targetEndpoint: jpc.endpoints[1]
  4858. } : jpc;
  4859. if (doFireEvent) {
  4860. _currentInstance.fire("connectionDetached", params, originalEvent);
  4861. }
  4862. // always fire this. used by internal jsplumb stuff.
  4863. _currentInstance.fire("internal.connectionDetached", params, originalEvent);
  4864. _currentInstance.router.connectionDetached(params);
  4865. };
  4866. var fireMoveEvent = _currentInstance.fireMoveEvent = function (params, evt) {
  4867. _currentInstance.fire("connectionMoved", params, evt);
  4868. };
  4869. this.unregisterEndpoint = function (endpoint) {
  4870. if (endpoint._jsPlumb.uuid) {
  4871. endpointsByUUID[endpoint._jsPlumb.uuid] = null;
  4872. }
  4873. _currentInstance.router.deleteEndpoint(endpoint);
  4874. // TODO at least replace this with a removeWithFunction call.
  4875. for (var e in endpointsByElement) {
  4876. var endpoints = endpointsByElement[e];
  4877. if (endpoints) {
  4878. var newEndpoints = [];
  4879. for (var i = 0, j = endpoints.length; i < j; i++) {
  4880. if (endpoints[i] !== endpoint) {
  4881. newEndpoints.push(endpoints[i]);
  4882. }
  4883. }
  4884. endpointsByElement[e] = newEndpoints;
  4885. }
  4886. if (endpointsByElement[e].length < 1) {
  4887. delete endpointsByElement[e];
  4888. }
  4889. }
  4890. };
  4891. var IS_DETACH_ALLOWED = "isDetachAllowed";
  4892. var BEFORE_DETACH = "beforeDetach";
  4893. var CHECK_CONDITION = "checkCondition";
  4894. /**
  4895. * Deletes a Connection.
  4896. * @method deleteConnection
  4897. * @param connection Connection to delete
  4898. * @param {Object} [params] Optional delete parameters
  4899. * @param {Boolean} [params.fireEvent=true] If false, a connection detached event will not be fired. Otherwise one will.
  4900. * @param {Boolean} [params.force=false] If true, the connection will be deleted even if a beforeDetach interceptor tries to stop the deletion.
  4901. * @returns {Boolean} True if the connection was deleted, false otherwise.
  4902. */
  4903. this.deleteConnection = function(connection, params) {
  4904. if (connection != null) {
  4905. params = params || {};
  4906. if (params.force || _ju.functionChain(true, false, [
  4907. [ connection.endpoints[0], IS_DETACH_ALLOWED, [ connection ] ],
  4908. [ connection.endpoints[1], IS_DETACH_ALLOWED, [ connection ] ],
  4909. [ connection, IS_DETACH_ALLOWED, [ connection ] ],
  4910. [ _currentInstance, CHECK_CONDITION, [ BEFORE_DETACH, connection ] ]
  4911. ])) {
  4912. connection.setHover(false);
  4913. fireDetachEvent(connection, !connection.pending && params.fireEvent !== false, params.originalEvent);
  4914. connection.endpoints[0].detachFromConnection(connection);
  4915. connection.endpoints[1].detachFromConnection(connection);
  4916. _ju.removeWithFunction(connections, function (_c) {
  4917. return connection.id === _c.id;
  4918. });
  4919. connection.cleanup();
  4920. connection.destroy();
  4921. return true;
  4922. }
  4923. }
  4924. return false;
  4925. };
  4926. /**
  4927. * Remove all Connections from all elements, but leaves Endpoints in place ((unless a connection is set to auto delete its Endpoints).
  4928. * @method deleteEveryConnection
  4929. * @param {Object} [params] optional params object for the call
  4930. * @param {Boolean} [params.fireEvent=true] Whether or not to fire detach events
  4931. * @param {Boolean} [params.forceDetach=false] If true, this call will ignore any `beforeDetach` interceptors.
  4932. * @returns {Number} The number of connections that were deleted.
  4933. */
  4934. this.deleteEveryConnection = function (params) {
  4935. params = params || {};
  4936. var count = connections.length, deletedCount = 0;
  4937. _currentInstance.batch(function () {
  4938. for (var i = 0; i < count; i++) {
  4939. deletedCount += _currentInstance.deleteConnection(connections[0], params) ? 1 : 0;
  4940. }
  4941. });
  4942. return deletedCount;
  4943. };
  4944. /**
  4945. * Removes all an element's Connections.
  4946. * @method deleteConnectionsForElement
  4947. * @param {Object} el Either the id of the element, or a selector for the element.
  4948. * @param {Object} [params] Optional parameters.
  4949. * @param {Boolean} [params.fireEvent=true] Whether or not to fire the detach event.
  4950. * @param {Boolean} [params.forceDetach=false] If true, this call will ignore any `beforeDetach` interceptors.
  4951. * @return {jsPlumbInstance} The current jsPlumb instance.
  4952. */
  4953. this.deleteConnectionsForElement = function (el, params) {
  4954. params = params || {};
  4955. el = _currentInstance.getElement(el);
  4956. var id = _getId(el), endpoints = endpointsByElement[id];
  4957. if (endpoints && endpoints.length) {
  4958. for (var i = 0, j = endpoints.length; i < j; i++) {
  4959. endpoints[i].deleteEveryConnection(params);
  4960. }
  4961. }
  4962. return _currentInstance;
  4963. };
  4964. /// not public. but of course its exposed. how to change this.
  4965. this.deleteObject = function (params) {
  4966. var result = {
  4967. endpoints: {},
  4968. connections: {},
  4969. endpointCount: 0,
  4970. connectionCount: 0
  4971. },
  4972. deleteAttachedObjects = params.deleteAttachedObjects !== false;
  4973. var unravelConnection = function (connection) {
  4974. if (connection != null && result.connections[connection.id] == null) {
  4975. if (!params.dontUpdateHover && connection._jsPlumb != null) {
  4976. connection.setHover(false);
  4977. }
  4978. result.connections[connection.id] = connection;
  4979. result.connectionCount++;
  4980. }
  4981. };
  4982. var unravelEndpoint = function (endpoint) {
  4983. if (endpoint != null && result.endpoints[endpoint.id] == null) {
  4984. if (!params.dontUpdateHover && endpoint._jsPlumb != null) {
  4985. endpoint.setHover(false);
  4986. }
  4987. result.endpoints[endpoint.id] = endpoint;
  4988. result.endpointCount++;
  4989. if (deleteAttachedObjects) {
  4990. for (var i = 0; i < endpoint.connections.length; i++) {
  4991. var c = endpoint.connections[i];
  4992. unravelConnection(c);
  4993. }
  4994. }
  4995. }
  4996. };
  4997. if (params.connection) {
  4998. unravelConnection(params.connection);
  4999. }
  5000. else {
  5001. unravelEndpoint(params.endpoint);
  5002. }
  5003. // loop through connections
  5004. for (var i in result.connections) {
  5005. var c = result.connections[i];
  5006. if (c._jsPlumb) {
  5007. _ju.removeWithFunction(connections, function (_c) {
  5008. return c.id === _c.id;
  5009. });
  5010. fireDetachEvent(c, params.fireEvent === false ? false : !c.pending, params.originalEvent);
  5011. var doNotCleanup = params.deleteAttachedObjects == null ? null : !params.deleteAttachedObjects;
  5012. c.endpoints[0].detachFromConnection(c, null, doNotCleanup);
  5013. c.endpoints[1].detachFromConnection(c, null, doNotCleanup);
  5014. c.cleanup(true);
  5015. c.destroy(true);
  5016. }
  5017. }
  5018. // loop through endpoints
  5019. for (var j in result.endpoints) {
  5020. var e = result.endpoints[j];
  5021. if (e._jsPlumb) {
  5022. _currentInstance.unregisterEndpoint(e);
  5023. // FIRE some endpoint deleted event?
  5024. e.cleanup(true);
  5025. e.destroy(true);
  5026. }
  5027. }
  5028. return result;
  5029. };
  5030. // helpers for select/selectEndpoints
  5031. var _setOperation = function (list, func, args, selector) {
  5032. for (var i = 0, j = list.length; i < j; i++) {
  5033. list[i][func].apply(list[i], args);
  5034. }
  5035. return selector(list);
  5036. },
  5037. _getOperation = function (list, func, args) {
  5038. var out = [];
  5039. for (var i = 0, j = list.length; i < j; i++) {
  5040. out.push([ list[i][func].apply(list[i], args), list[i] ]);
  5041. }
  5042. return out;
  5043. },
  5044. setter = function (list, func, selector) {
  5045. return function () {
  5046. return _setOperation(list, func, arguments, selector);
  5047. };
  5048. },
  5049. getter = function (list, func) {
  5050. return function () {
  5051. return _getOperation(list, func, arguments);
  5052. };
  5053. },
  5054. prepareList = function (input, doNotGetIds) {
  5055. var r = [];
  5056. if (input) {
  5057. if (typeof input === 'string') {
  5058. if (input === "*") {
  5059. return input;
  5060. }
  5061. r.push(input);
  5062. }
  5063. else {
  5064. if (doNotGetIds) {
  5065. r = input;
  5066. }
  5067. else {
  5068. if (input.length) {
  5069. for (var i = 0, j = input.length; i < j; i++) {
  5070. r.push(_info(input[i]).id);
  5071. }
  5072. }
  5073. else {
  5074. r.push(_info(input).id);
  5075. }
  5076. }
  5077. }
  5078. }
  5079. return r;
  5080. },
  5081. filterList = function (list, value, missingIsFalse) {
  5082. if (list === "*") {
  5083. return true;
  5084. }
  5085. return list.length > 0 ? list.indexOf(value) !== -1 : !missingIsFalse;
  5086. };
  5087. // get some connections, specifying source/target/scope
  5088. this.getConnections = function (options, flat) {
  5089. if (!options) {
  5090. options = {};
  5091. } else if (options.constructor === String) {
  5092. options = { "scope": options };
  5093. }
  5094. var scope = options.scope || _currentInstance.getDefaultScope(),
  5095. scopes = prepareList(scope, true),
  5096. sources = prepareList(options.source),
  5097. targets = prepareList(options.target),
  5098. results = (!flat && scopes.length > 1) ? {} : [],
  5099. _addOne = function (scope, obj) {
  5100. if (!flat && scopes.length > 1) {
  5101. var ss = results[scope];
  5102. if (ss == null) {
  5103. ss = results[scope] = [];
  5104. }
  5105. ss.push(obj);
  5106. } else {
  5107. results.push(obj);
  5108. }
  5109. };
  5110. for (var j = 0, jj = connections.length; j < jj; j++) {
  5111. var c = connections[j],
  5112. sourceId = c.proxies && c.proxies[0] ? c.proxies[0].originalEp.elementId : c.sourceId,
  5113. targetId = c.proxies && c.proxies[1] ? c.proxies[1].originalEp.elementId : c.targetId;
  5114. if (filterList(scopes, c.scope) && filterList(sources, sourceId) && filterList(targets, targetId)) {
  5115. _addOne(c.scope, c);
  5116. }
  5117. }
  5118. return results;
  5119. };
  5120. var _curryEach = function (list, executor) {
  5121. return function (f) {
  5122. for (var i = 0, ii = list.length; i < ii; i++) {
  5123. f(list[i]);
  5124. }
  5125. return executor(list);
  5126. };
  5127. },
  5128. _curryGet = function (list) {
  5129. return function (idx) {
  5130. return list[idx];
  5131. };
  5132. };
  5133. var _makeCommonSelectHandler = function (list, executor) {
  5134. var out = {
  5135. length: list.length,
  5136. each: _curryEach(list, executor),
  5137. get: _curryGet(list)
  5138. },
  5139. setters = ["setHover", "removeAllOverlays", "setLabel", "addClass", "addOverlay", "removeOverlay",
  5140. "removeOverlays", "showOverlay", "hideOverlay", "showOverlays", "hideOverlays", "setPaintStyle",
  5141. "setHoverPaintStyle", "setSuspendEvents", "setParameter", "setParameters", "setVisible",
  5142. "repaint", "addType", "toggleType", "removeType", "removeClass", "setType", "bind", "unbind" ],
  5143. getters = ["getLabel", "getOverlay", "isHover", "getParameter", "getParameters", "getPaintStyle",
  5144. "getHoverPaintStyle", "isVisible", "hasType", "getType", "isSuspendEvents" ],
  5145. i, ii;
  5146. for (i = 0, ii = setters.length; i < ii; i++) {
  5147. out[setters[i]] = setter(list, setters[i], executor);
  5148. }
  5149. for (i = 0, ii = getters.length; i < ii; i++) {
  5150. out[getters[i]] = getter(list, getters[i]);
  5151. }
  5152. return out;
  5153. };
  5154. var _makeConnectionSelectHandler = function (list) {
  5155. var common = _makeCommonSelectHandler(list, _makeConnectionSelectHandler);
  5156. return jsPlumb.extend(common, {
  5157. // setters
  5158. setDetachable: setter(list, "setDetachable", _makeConnectionSelectHandler),
  5159. setReattach: setter(list, "setReattach", _makeConnectionSelectHandler),
  5160. setConnector: setter(list, "setConnector", _makeConnectionSelectHandler),
  5161. delete: function () {
  5162. for (var i = 0, ii = list.length; i < ii; i++) {
  5163. _currentInstance.deleteConnection(list[i]);
  5164. }
  5165. },
  5166. // getters
  5167. isDetachable: getter(list, "isDetachable"),
  5168. isReattach: getter(list, "isReattach")
  5169. });
  5170. };
  5171. var _makeEndpointSelectHandler = function (list) {
  5172. var common = _makeCommonSelectHandler(list, _makeEndpointSelectHandler);
  5173. return jsPlumb.extend(common, {
  5174. setEnabled: setter(list, "setEnabled", _makeEndpointSelectHandler),
  5175. setAnchor: setter(list, "setAnchor", _makeEndpointSelectHandler),
  5176. isEnabled: getter(list, "isEnabled"),
  5177. deleteEveryConnection: function () {
  5178. for (var i = 0, ii = list.length; i < ii; i++) {
  5179. list[i].deleteEveryConnection();
  5180. }
  5181. },
  5182. "delete": function () {
  5183. for (var i = 0, ii = list.length; i < ii; i++) {
  5184. _currentInstance.deleteEndpoint(list[i]);
  5185. }
  5186. }
  5187. });
  5188. };
  5189. this.select = function (params) {
  5190. params = params || {};
  5191. params.scope = params.scope || "*";
  5192. return _makeConnectionSelectHandler(params.connections || _currentInstance.getConnections(params, true));
  5193. };
  5194. this.selectEndpoints = function (params) {
  5195. params = params || {};
  5196. params.scope = params.scope || "*";
  5197. var noElementFilters = !params.element && !params.source && !params.target,
  5198. elements = noElementFilters ? "*" : prepareList(params.element),
  5199. sources = noElementFilters ? "*" : prepareList(params.source),
  5200. targets = noElementFilters ? "*" : prepareList(params.target),
  5201. scopes = prepareList(params.scope, true);
  5202. var ep = [];
  5203. for (var el in endpointsByElement) {
  5204. var either = filterList(elements, el, true),
  5205. source = filterList(sources, el, true),
  5206. sourceMatchExact = sources !== "*",
  5207. target = filterList(targets, el, true),
  5208. targetMatchExact = targets !== "*";
  5209. // if they requested 'either' then just match scope. otherwise if they requested 'source' (not as a wildcard) then we have to match only endpoints that have isSource set to to true, and the same thing with isTarget.
  5210. if (either || source || target) {
  5211. inner:
  5212. for (var i = 0, ii = endpointsByElement[el].length; i < ii; i++) {
  5213. var _ep = endpointsByElement[el][i];
  5214. if (filterList(scopes, _ep.scope, true)) {
  5215. var noMatchSource = (sourceMatchExact && sources.length > 0 && !_ep.isSource),
  5216. noMatchTarget = (targetMatchExact && targets.length > 0 && !_ep.isTarget);
  5217. if (noMatchSource || noMatchTarget) {
  5218. continue inner;
  5219. }
  5220. ep.push(_ep);
  5221. }
  5222. }
  5223. }
  5224. }
  5225. return _makeEndpointSelectHandler(ep);
  5226. };
  5227. // get all connections managed by the instance of jsplumb.
  5228. this.getAllConnections = function () {
  5229. return connections;
  5230. };
  5231. this.getDefaultScope = function () {
  5232. return DEFAULT_SCOPE;
  5233. };
  5234. // get an endpoint by uuid.
  5235. this.getEndpoint = _getEndpoint;
  5236. /**
  5237. * Gets the list of Endpoints for a given element.
  5238. * @method getEndpoints
  5239. * @param {String|Element|Selector} el The element to get endpoints for.
  5240. * @return {Endpoint[]} An array of Endpoints for the specified element.
  5241. */
  5242. this.getEndpoints = function (el) {
  5243. return endpointsByElement[_info(el).id] || [];
  5244. };
  5245. // gets the default endpoint type. used when subclassing. see wiki.
  5246. this.getDefaultEndpointType = function () {
  5247. return jsPlumb.Endpoint;
  5248. };
  5249. // gets the default connection type. used when subclassing. see wiki.
  5250. this.getDefaultConnectionType = function () {
  5251. return jsPlumb.Connection;
  5252. };
  5253. /*
  5254. * Gets an element's id, creating one if necessary. really only exposed
  5255. * for the lib-specific functionality to access; would be better to pass
  5256. * the current instance into the lib-specific code (even though this is
  5257. * a static call. i just don't want to expose it to the public API).
  5258. */
  5259. this.getId = _getId;
  5260. this.draw = _draw;
  5261. this.info = _info;
  5262. this.appendElement = _appendElement;
  5263. var _hoverSuspended = false;
  5264. this.isHoverSuspended = function () {
  5265. return _hoverSuspended;
  5266. };
  5267. this.setHoverSuspended = function (s) {
  5268. _hoverSuspended = s;
  5269. };
  5270. // set an element's connections to be hidden
  5271. this.hide = function (el, changeEndpoints) {
  5272. _setVisible(el, "none", changeEndpoints);
  5273. return _currentInstance;
  5274. };
  5275. // exposed for other objects to use to get a unique id.
  5276. this.idstamp = _idstamp;
  5277. // ensure that, if the current container exists, it is a DOM element and not a selector.
  5278. // if it does not exist and `candidate` is supplied, the offset parent of that element will be set as the Container.
  5279. // this is used to do a better default behaviour for the case that the user has not set a container:
  5280. // addEndpoint, makeSource, makeTarget and connect all call this method with the offsetParent of the
  5281. // element in question (for connect it is the source element). So if no container is set, it is inferred
  5282. // to be the offsetParent of the first element the user tries to connect.
  5283. var _ensureContainer = function (candidate) {
  5284. if (!_container && candidate) {
  5285. var can = _currentInstance.getElement(candidate);
  5286. if (can.offsetParent) {
  5287. _currentInstance.setContainer(can.offsetParent);
  5288. }
  5289. }
  5290. };
  5291. var _getContainerFromDefaults = function () {
  5292. if (_currentInstance.Defaults.Container) {
  5293. _currentInstance.setContainer(_currentInstance.Defaults.Container);
  5294. }
  5295. };
  5296. // check if a given element is managed or not. if not, add to our map. if drawing is not suspended then
  5297. // we'll also stash its dimensions; otherwise we'll do this in a lazy way through updateOffset.
  5298. var _manage = _currentInstance.manage = function (id, element, _transient, _recalc) {
  5299. if (!managedElements[id]) {
  5300. managedElements[id] = {
  5301. el: element,
  5302. endpoints: [],
  5303. connections: [],
  5304. rotation: 0
  5305. };
  5306. managedElements[id].info = _updateOffset({ elId: id, timestamp: _suspendedAt });
  5307. _currentInstance.addClass(element, "jtk-managed");
  5308. if (!_transient) {
  5309. _currentInstance.fire("manageElement", { id:id, info:managedElements[id].info, el:element });
  5310. }
  5311. } else {
  5312. if (_recalc) {
  5313. managedElements[id].info = _updateOffset({ elId: id, timestamp: _suspendedAt, recalc:true });
  5314. }
  5315. }
  5316. return managedElements[id];
  5317. };
  5318. this.unmanage = function(id) {
  5319. if (managedElements[id]) {
  5320. var el = managedElements[id].el;
  5321. _currentInstance.removeClass(el, "jtk-managed");
  5322. delete managedElements[id];
  5323. _currentInstance.fire("unmanageElement", {id:id, el:el});
  5324. }
  5325. };
  5326. this.rotate = function(elId, amountInDegrees, doNotRedraw) {
  5327. if (managedElements[elId]) {
  5328. managedElements[elId].rotation = amountInDegrees;
  5329. managedElements[elId].el.style.transform="rotate(" + amountInDegrees + "deg)";
  5330. managedElements[elId].el.style.transformOrigin="center center";
  5331. if (doNotRedraw !== true) {
  5332. return this.revalidate(elId);
  5333. }
  5334. }
  5335. return {
  5336. c:[], e:[]
  5337. };
  5338. };
  5339. this.getRotation = function(elementId) {
  5340. return managedElements[elementId] ? managedElements[elementId].rotation || 0 : 0;
  5341. };
  5342. /**
  5343. * updates the offset and size for a given element, and stores the
  5344. * values. if 'offset' is not null we use that (it would have been
  5345. * passed in from a drag call) because it's faster; but if it is null,
  5346. * or if 'recalc' is true in order to force a recalculation, we get the current values.
  5347. * @method updateOffset
  5348. */
  5349. var _updateOffset = function (params) {
  5350. var timestamp = params.timestamp, recalc = params.recalc, offset = params.offset, elId = params.elId, s;
  5351. if (_suspendDrawing && !timestamp) {
  5352. timestamp = _suspendedAt;
  5353. }
  5354. if (!recalc) {
  5355. if (timestamp && timestamp === offsetTimestamps[elId]) {
  5356. return {o: params.offset || offsets[elId], s: sizes[elId]};
  5357. }
  5358. }
  5359. if (recalc || (!offset && offsets[elId] == null)) { // if forced repaint or no offset available, we recalculate.
  5360. // get the current size and offset, and store them
  5361. s = managedElements[elId] ? managedElements[elId].el : null;
  5362. if (s != null) {
  5363. sizes[elId] = _currentInstance.getSize(s);
  5364. offsets[elId] = _currentInstance.getOffset(s);
  5365. offsetTimestamps[elId] = timestamp;
  5366. }
  5367. } else {
  5368. offsets[elId] = offset || offsets[elId];
  5369. if (sizes[elId] == null) {
  5370. s = managedElements[elId].el;
  5371. if (s != null) {
  5372. sizes[elId] = _currentInstance.getSize(s);
  5373. }
  5374. }
  5375. offsetTimestamps[elId] = timestamp;
  5376. }
  5377. if (offsets[elId] && !offsets[elId].right) {
  5378. offsets[elId].right = offsets[elId].left + sizes[elId][0];
  5379. offsets[elId].bottom = offsets[elId].top + sizes[elId][1];
  5380. offsets[elId].width = sizes[elId][0];
  5381. offsets[elId].height = sizes[elId][1];
  5382. offsets[elId].centerx = offsets[elId].left + (offsets[elId].width / 2);
  5383. offsets[elId].centery = offsets[elId].top + (offsets[elId].height / 2);
  5384. }
  5385. return {o: offsets[elId], s: sizes[elId]};
  5386. };
  5387. this.updateOffset = _updateOffset;
  5388. /**
  5389. * callback from the current library to tell us to prepare ourselves (attach
  5390. * mouse listeners etc; can't do that until the library has provided a bind method)
  5391. */
  5392. this.init = function () {
  5393. if (!initialized) {
  5394. _getContainerFromDefaults();
  5395. _currentInstance.router = new root.jsPlumb.DefaultRouter(_currentInstance);
  5396. _currentInstance.anchorManager = _currentInstance.router.anchorManager;
  5397. initialized = true;
  5398. _currentInstance.fire("ready", _currentInstance);
  5399. }
  5400. }.bind(this);
  5401. this.log = log;
  5402. this.jsPlumbUIComponent = jsPlumbUIComponent;
  5403. /*
  5404. * Creates an anchor with the given params.
  5405. *
  5406. *
  5407. * Returns: The newly created Anchor.
  5408. * Throws: an error if a named anchor was not found.
  5409. */
  5410. this.makeAnchor = function () {
  5411. var pp, _a = function (t, p) {
  5412. if (root.jsPlumb.Anchors[t]) {
  5413. return new root.jsPlumb.Anchors[t](p);
  5414. }
  5415. if (!_currentInstance.Defaults.DoNotThrowErrors) {
  5416. throw { msg: "jsPlumb: unknown anchor type '" + t + "'" };
  5417. }
  5418. };
  5419. if (arguments.length === 0) {
  5420. return null;
  5421. }
  5422. var specimen = arguments[0], elementId = arguments[1], jsPlumbInstance = arguments[2], newAnchor = null;
  5423. // if it appears to be an anchor already...
  5424. if (specimen.compute && specimen.getOrientation) {
  5425. return specimen;
  5426. } //TODO hazy here about whether it should be added or is already added somehow.
  5427. // is it the name of an anchor type?
  5428. else if (typeof specimen === "string") {
  5429. newAnchor = _a(arguments[0], {elementId: elementId, jsPlumbInstance: _currentInstance});
  5430. }
  5431. // is it an array? it will be one of:
  5432. // an array of [spec, params] - this defines a single anchor, which may be dynamic, but has parameters.
  5433. // an array of arrays - this defines some dynamic anchors
  5434. // an array of numbers - this defines a single anchor.
  5435. else if (_ju.isArray(specimen)) {
  5436. if (_ju.isArray(specimen[0]) || _ju.isString(specimen[0])) {
  5437. // if [spec, params] format
  5438. if (specimen.length === 2 && _ju.isObject(specimen[1])) {
  5439. // if first arg is a string, its a named anchor with params
  5440. if (_ju.isString(specimen[0])) {
  5441. pp = root.jsPlumb.extend({elementId: elementId, jsPlumbInstance: _currentInstance}, specimen[1]);
  5442. newAnchor = _a(specimen[0], pp);
  5443. }
  5444. // otherwise first arg is array, second is params. we treat as a dynamic anchor, which is fine
  5445. // even if the first arg has only one entry. you could argue all anchors should be implicitly dynamic in fact.
  5446. else {
  5447. pp = root.jsPlumb.extend({elementId: elementId, jsPlumbInstance: _currentInstance, anchors: specimen[0]}, specimen[1]);
  5448. newAnchor = new root.jsPlumb.DynamicAnchor(pp);
  5449. }
  5450. }
  5451. else {
  5452. newAnchor = new jsPlumb.DynamicAnchor({anchors: specimen, selector: null, elementId: elementId, jsPlumbInstance: _currentInstance});
  5453. }
  5454. }
  5455. else {
  5456. var anchorParams = {
  5457. x: specimen[0], y: specimen[1],
  5458. orientation: (specimen.length >= 4) ? [ specimen[2], specimen[3] ] : [0, 0],
  5459. offsets: (specimen.length >= 6) ? [ specimen[4], specimen[5] ] : [ 0, 0 ],
  5460. elementId: elementId,
  5461. jsPlumbInstance: _currentInstance,
  5462. cssClass: specimen.length === 7 ? specimen[6] : null
  5463. };
  5464. newAnchor = new root.jsPlumb.Anchor(anchorParams);
  5465. newAnchor.clone = function () {
  5466. return new root.jsPlumb.Anchor(anchorParams);
  5467. };
  5468. }
  5469. }
  5470. if (!newAnchor.id) {
  5471. newAnchor.id = "anchor_" + _idstamp();
  5472. }
  5473. return newAnchor;
  5474. };
  5475. /**
  5476. * makes a list of anchors from the given list of types or coords, eg
  5477. * ["TopCenter", "RightMiddle", "BottomCenter", [0, 1, -1, -1] ]
  5478. */
  5479. this.makeAnchors = function (types, elementId, jsPlumbInstance) {
  5480. var r = [];
  5481. for (var i = 0, ii = types.length; i < ii; i++) {
  5482. if (typeof types[i] === "string") {
  5483. r.push(root.jsPlumb.Anchors[types[i]]({elementId: elementId, jsPlumbInstance: jsPlumbInstance}));
  5484. }
  5485. else if (_ju.isArray(types[i])) {
  5486. r.push(_currentInstance.makeAnchor(types[i], elementId, jsPlumbInstance));
  5487. }
  5488. }
  5489. return r;
  5490. };
  5491. /**
  5492. * Makes a dynamic anchor from the given list of anchors (which may be in shorthand notation as strings or dimension arrays, or Anchor
  5493. * objects themselves) and the given, optional, anchorSelector function (jsPlumb uses a default if this is not provided; most people will
  5494. * not need to provide this - i think).
  5495. */
  5496. this.makeDynamicAnchor = function (anchors, anchorSelector) {
  5497. return new root.jsPlumb.DynamicAnchor({anchors: anchors, selector: anchorSelector, elementId: null, jsPlumbInstance: _currentInstance});
  5498. };
  5499. // --------------------- makeSource/makeTarget ----------------------------------------------
  5500. this.targetEndpointDefinitions = {};
  5501. this.sourceEndpointDefinitions = {};
  5502. var selectorFilter = function (evt, _el, selector, _instance, negate) {
  5503. var t = evt.target || evt.srcElement, ok = false,
  5504. sel = _instance.getSelector(_el, selector);
  5505. for (var j = 0; j < sel.length; j++) {
  5506. if (sel[j] === t) {
  5507. ok = true;
  5508. break;
  5509. }
  5510. }
  5511. return negate ? !ok : ok;
  5512. };
  5513. var _makeElementDropHandler = function (elInfo, p, dropOptions, isSource, isTarget) {
  5514. var proxyComponent = new jsPlumbUIComponent(p);
  5515. var _drop = p._jsPlumb.EndpointDropHandler({
  5516. jsPlumb: _currentInstance,
  5517. enabled: function () {
  5518. return elInfo.def.enabled;
  5519. },
  5520. isFull: function () {
  5521. var targetCount = _currentInstance.select({target: elInfo.id}).length;
  5522. return elInfo.def.maxConnections > 0 && targetCount >= elInfo.def.maxConnections;
  5523. },
  5524. element: elInfo.el,
  5525. elementId: elInfo.id,
  5526. isSource: isSource,
  5527. isTarget: isTarget,
  5528. addClass: function (clazz) {
  5529. _currentInstance.addClass(elInfo.el, clazz);
  5530. },
  5531. removeClass: function (clazz) {
  5532. _currentInstance.removeClass(elInfo.el, clazz);
  5533. },
  5534. onDrop: function (jpc) {
  5535. var source = jpc.endpoints[0];
  5536. source.anchor.locked = false;
  5537. },
  5538. isDropAllowed: function () {
  5539. return proxyComponent.isDropAllowed.apply(proxyComponent, arguments);
  5540. },
  5541. isRedrop:function(jpc) {
  5542. return (jpc.suspendedElement != null && jpc.suspendedEndpoint != null && jpc.suspendedEndpoint.element === elInfo.el);
  5543. },
  5544. getEndpoint: function (jpc) {
  5545. // make a new Endpoint for the target, or get it from the cache if uniqueEndpoint
  5546. // is set. if its a redrop the new endpoint will be immediately cleaned up.
  5547. var newEndpoint = elInfo.def.endpoint;
  5548. // if no cached endpoint, or there was one but it has been cleaned up
  5549. // (ie. detached), create a new one
  5550. if (newEndpoint == null || newEndpoint._jsPlumb == null) {
  5551. var eps = _currentInstance.deriveEndpointAndAnchorSpec(jpc.getType().join(" "), true);
  5552. var pp = eps.endpoints ? root.jsPlumb.extend(p, {
  5553. endpoint:elInfo.def.def.endpoint || eps.endpoints[1]
  5554. }) :p;
  5555. if (eps.anchors) {
  5556. pp = root.jsPlumb.extend(pp, {
  5557. anchor:elInfo.def.def.anchor || eps.anchors[1]
  5558. });
  5559. }
  5560. newEndpoint = _currentInstance.addEndpoint(elInfo.el, pp);
  5561. newEndpoint._mtNew = true;
  5562. }
  5563. if (p.uniqueEndpoint) {
  5564. elInfo.def.endpoint = newEndpoint;
  5565. }
  5566. newEndpoint.setDeleteOnEmpty(true);
  5567. // if connection is detachable, init the new endpoint to be draggable, to support that happening.
  5568. if (jpc.isDetachable()) {
  5569. newEndpoint.initDraggable();
  5570. }
  5571. // if the anchor has a 'positionFinder' set, then delegate to that function to find
  5572. // out where to locate the anchor.
  5573. if (newEndpoint.anchor.positionFinder != null) {
  5574. var dropPosition = _currentInstance.getUIPosition(arguments, _currentInstance.getZoom()),
  5575. elPosition = _currentInstance.getOffset(elInfo.el),
  5576. elSize = _currentInstance.getSize(elInfo.el),
  5577. ap = dropPosition == null ? [0,0] : newEndpoint.anchor.positionFinder(dropPosition, elPosition, elSize, newEndpoint.anchor.constructorParams);
  5578. newEndpoint.anchor.x = ap[0];
  5579. newEndpoint.anchor.y = ap[1];
  5580. // now figure an orientation for it..kind of hard to know what to do actually. probably the best thing i can do is to
  5581. // support specifying an orientation in the anchor's spec. if one is not supplied then i will make the orientation
  5582. // be what will cause the most natural link to the source: it will be pointing at the source, but it needs to be
  5583. // specified in one axis only, and so how to make that choice? i think i will use whichever axis is the one in which
  5584. // the target is furthest away from the source.
  5585. }
  5586. return newEndpoint;
  5587. },
  5588. maybeCleanup: function (ep) {
  5589. if (ep._mtNew && ep.connections.length === 0) {
  5590. _currentInstance.deleteObject({endpoint: ep});
  5591. }
  5592. else {
  5593. delete ep._mtNew;
  5594. }
  5595. }
  5596. });
  5597. // wrap drop events as needed and initialise droppable
  5598. var dropEvent = root.jsPlumb.dragEvents.drop;
  5599. dropOptions.scope = dropOptions.scope || (p.scope || _currentInstance.Defaults.Scope);
  5600. dropOptions[dropEvent] = _ju.wrap(dropOptions[dropEvent], _drop, true);
  5601. dropOptions.rank = p.rank || 0;
  5602. // if target, return true from the over event. this will cause katavorio to stop setting drops to hover
  5603. // if multipleDrop is set to false.
  5604. if (isTarget) {
  5605. dropOptions[root.jsPlumb.dragEvents.over] = function () { return true; };
  5606. }
  5607. // vanilla jsplumb only
  5608. if (p.allowLoopback === false) {
  5609. dropOptions.canDrop = function (_drag) {
  5610. var de = _drag.getDragElement()._jsPlumbRelatedElement;
  5611. return de !== elInfo.el;
  5612. };
  5613. }
  5614. _currentInstance.initDroppable(elInfo.el, dropOptions, "internal");
  5615. return _drop;
  5616. };
  5617. // see API docs
  5618. this.makeTarget = function (el, params, referenceParams) {
  5619. // put jsplumb ref into params without altering the params passed in
  5620. var p = root.jsPlumb.extend({_jsPlumb: this}, referenceParams);
  5621. root.jsPlumb.extend(p, params);
  5622. var maxConnections = p.maxConnections || -1,
  5623. _doOne = function (el) {
  5624. // get the element's id and store the endpoint definition for it. jsPlumb.connect calls will look for one of these,
  5625. // and use the endpoint definition if found.
  5626. // decode the info for this element (id and element)
  5627. var elInfo = _info(el),
  5628. elid = elInfo.id,
  5629. dropOptions = root.jsPlumb.extend({}, p.dropOptions || {}),
  5630. type = p.connectionType || "default";
  5631. this.targetEndpointDefinitions[elid] = this.targetEndpointDefinitions[elid] || {};
  5632. _ensureContainer(elid);
  5633. // if this is a group and the user has not mandated a rank, set to -1 so that Nodes takes
  5634. // precedence.
  5635. if (elInfo.el._isJsPlumbGroup && dropOptions.rank == null) {
  5636. dropOptions.rank = -1;
  5637. }
  5638. // store the definition
  5639. var _def = {
  5640. def: root.jsPlumb.extend({}, p),
  5641. uniqueEndpoint: p.uniqueEndpoint,
  5642. maxConnections: maxConnections,
  5643. enabled: true
  5644. };
  5645. if (p.createEndpoint) {
  5646. _def.uniqueEndpoint = true;
  5647. _def.endpoint = _currentInstance.addEndpoint(el, _def.def);
  5648. _def.endpoint.setDeleteOnEmpty(false);
  5649. }
  5650. elInfo.def = _def;
  5651. this.targetEndpointDefinitions[elid][type] = _def;
  5652. _makeElementDropHandler(elInfo, p, dropOptions, p.isSource === true, true);
  5653. // stash the definition on the drop
  5654. elInfo.el._katavorioDrop[elInfo.el._katavorioDrop.length - 1].targetDef = _def;
  5655. }.bind(this);
  5656. // make an array if only given one element
  5657. var inputs = el.length && el.constructor !== String ? el : [ el ];
  5658. // register each one in the list.
  5659. for (var i = 0, ii = inputs.length; i < ii; i++) {
  5660. _doOne(inputs[i]);
  5661. }
  5662. return this;
  5663. };
  5664. // see api docs
  5665. this.unmakeTarget = function (el, doNotClearArrays) {
  5666. var info = _info(el);
  5667. _currentInstance.destroyDroppable(info.el, "internal");
  5668. if (!doNotClearArrays) {
  5669. delete this.targetEndpointDefinitions[info.id];
  5670. }
  5671. return this;
  5672. };
  5673. // see api docs
  5674. this.makeSource = function (el, params, referenceParams) {
  5675. var p = root.jsPlumb.extend({_jsPlumb: this}, referenceParams);
  5676. root.jsPlumb.extend(p, params);
  5677. var type = p.connectionType || "default";
  5678. var aae = _currentInstance.deriveEndpointAndAnchorSpec(type);
  5679. p.endpoint = p.endpoint || aae.endpoints[0];
  5680. p.anchor = p.anchor || aae.anchors[0];
  5681. var maxConnections = p.maxConnections || -1,
  5682. onMaxConnections = p.onMaxConnections,
  5683. _doOne = function (elInfo) {
  5684. // get the element's id and store the endpoint definition for it. jsPlumb.connect calls will look for one of these,
  5685. // and use the endpoint definition if found.
  5686. var elid = elInfo.id,
  5687. _del = this.getElement(elInfo.el);
  5688. this.sourceEndpointDefinitions[elid] = this.sourceEndpointDefinitions[elid] || {};
  5689. _ensureContainer(elid);
  5690. var _def = {
  5691. def:root.jsPlumb.extend({}, p),
  5692. uniqueEndpoint: p.uniqueEndpoint,
  5693. maxConnections: maxConnections,
  5694. enabled: true
  5695. };
  5696. if (p.createEndpoint) {
  5697. _def.uniqueEndpoint = true;
  5698. _def.endpoint = _currentInstance.addEndpoint(el, _def.def);
  5699. _def.endpoint.setDeleteOnEmpty(false);
  5700. }
  5701. this.sourceEndpointDefinitions[elid][type] = _def;
  5702. elInfo.def = _def;
  5703. var stopEvent = root.jsPlumb.dragEvents.stop,
  5704. dragEvent = root.jsPlumb.dragEvents.drag,
  5705. dragOptions = root.jsPlumb.extend({ }, p.dragOptions || {}),
  5706. existingDrag = dragOptions.drag,
  5707. existingStop = dragOptions.stop,
  5708. ep = null,
  5709. endpointAddedButNoDragYet = false;
  5710. // set scope if its not set in dragOptions but was passed in in params
  5711. dragOptions.scope = dragOptions.scope || p.scope;
  5712. dragOptions[dragEvent] = _ju.wrap(dragOptions[dragEvent], function () {
  5713. if (existingDrag) {
  5714. existingDrag.apply(this, arguments);
  5715. }
  5716. endpointAddedButNoDragYet = false;
  5717. });
  5718. dragOptions[stopEvent] = _ju.wrap(dragOptions[stopEvent], function () {
  5719. if (existingStop) {
  5720. existingStop.apply(this, arguments);
  5721. }
  5722. this.currentlyDragging = false;
  5723. if (ep._jsPlumb != null) { // if not cleaned up...
  5724. // reset the anchor to the anchor that was initially provided. the one we were using to drag
  5725. // the connection was just a placeholder that was located at the place the user pressed the
  5726. // mouse button to initiate the drag.
  5727. var anchorDef = p.anchor || this.Defaults.Anchor,
  5728. oldAnchor = ep.anchor,
  5729. oldConnection = ep.connections[0];
  5730. var newAnchor = this.makeAnchor(anchorDef, elid, this),
  5731. _el = ep.element;
  5732. // if the anchor has a 'positionFinder' set, then delegate to that function to find
  5733. // out where to locate the anchor. issue 117.
  5734. if (newAnchor.positionFinder != null) {
  5735. var elPosition = _currentInstance.getOffset(_el),
  5736. elSize = this.getSize(_el),
  5737. dropPosition = { left: elPosition.left + (oldAnchor.x * elSize[0]), top: elPosition.top + (oldAnchor.y * elSize[1]) },
  5738. ap = newAnchor.positionFinder(dropPosition, elPosition, elSize, newAnchor.constructorParams);
  5739. newAnchor.x = ap[0];
  5740. newAnchor.y = ap[1];
  5741. }
  5742. ep.setAnchor(newAnchor, true);
  5743. ep.repaint();
  5744. this.repaint(ep.elementId);
  5745. if (oldConnection != null) {
  5746. this.repaint(oldConnection.targetId);
  5747. }
  5748. }
  5749. }.bind(this));
  5750. // when the user presses the mouse, add an Endpoint, if we are enabled.
  5751. var mouseDownListener = function (e) {
  5752. // on right mouse button, abort.
  5753. if (e.which === 3 || e.button === 2) {
  5754. return;
  5755. }
  5756. elid = this.getId(this.getElement(elInfo.el)); // elid might have changed since this method was called to configure the element.
  5757. // TODO store def on element.
  5758. var def = this.sourceEndpointDefinitions[elid][type];
  5759. // if disabled, return.
  5760. if (!def.enabled) {
  5761. return;
  5762. }
  5763. // if a filter was given, run it, and return if it says no.
  5764. if (p.filter) {
  5765. var r = _ju.isString(p.filter) ? selectorFilter(e, elInfo.el, p.filter, this, p.filterExclude) : p.filter(e, elInfo.el);
  5766. if (r === false) {
  5767. return;
  5768. }
  5769. }
  5770. // if maxConnections reached
  5771. var sourceCount = this.select({source: elid}).length;
  5772. if (def.maxConnections >= 0 && (sourceCount >= def.maxConnections)) {
  5773. if (onMaxConnections) {
  5774. onMaxConnections({
  5775. element: elInfo.el,
  5776. maxConnections: maxConnections
  5777. }, e);
  5778. }
  5779. return false;
  5780. }
  5781. // find the position on the element at which the mouse was pressed; this is where the endpoint
  5782. // will be located.
  5783. var elxy = root.jsPlumb.getPositionOnElement(e, _del, _zoom);
  5784. // we need to override the anchor in here, and force 'isSource', but we don't want to mess with
  5785. // the params passed in, because after a connection is established we're going to reset the endpoint
  5786. // to have the anchor we were given.
  5787. var tempEndpointParams = {};
  5788. root.jsPlumb.extend(tempEndpointParams, def.def);
  5789. tempEndpointParams.isTemporarySource = true;
  5790. tempEndpointParams.anchor = [ elxy[0], elxy[1] , 0, 0];
  5791. tempEndpointParams.dragOptions = dragOptions;
  5792. if (def.def.scope) {
  5793. tempEndpointParams.scope = def.def.scope;
  5794. }
  5795. ep = this.addEndpoint(elid, tempEndpointParams);
  5796. endpointAddedButNoDragYet = true;
  5797. ep.setDeleteOnEmpty(true);
  5798. // if unique endpoint and it's already been created, push it onto the endpoint we create. at the end
  5799. // of a successful connection we'll switch to that endpoint.
  5800. // TODO this is the same code as the programmatic endpoints create on line 1050 ish
  5801. if (def.uniqueEndpoint) {
  5802. if (!def.endpoint) {
  5803. def.endpoint = ep;
  5804. ep.setDeleteOnEmpty(false);
  5805. }
  5806. else {
  5807. ep.finalEndpoint = def.endpoint;
  5808. }
  5809. }
  5810. var _delTempEndpoint = function () {
  5811. // this mouseup event is fired only if no dragging occurred, by jquery and yui, but for mootools
  5812. // it is fired even if dragging has occurred, in which case we would blow away a perfectly
  5813. // legitimate endpoint, were it not for this check. the flag is set after adding an
  5814. // endpoint and cleared in a drag listener we set in the dragOptions above.
  5815. _currentInstance.off(ep.canvas, "mouseup", _delTempEndpoint);
  5816. _currentInstance.off(elInfo.el, "mouseup", _delTempEndpoint);
  5817. if (endpointAddedButNoDragYet) {
  5818. endpointAddedButNoDragYet = false;
  5819. _currentInstance.deleteEndpoint(ep);
  5820. }
  5821. };
  5822. _currentInstance.on(ep.canvas, "mouseup", _delTempEndpoint);
  5823. _currentInstance.on(elInfo.el, "mouseup", _delTempEndpoint);
  5824. // optionally check for attributes to extract from the source element
  5825. var payload = {};
  5826. if (def.def.extract) {
  5827. for (var att in def.def.extract) {
  5828. var v = (e.srcElement || e.target).getAttribute(att);
  5829. if (v) {
  5830. payload[def.def.extract[att]] = v;
  5831. }
  5832. }
  5833. }
  5834. // and then trigger its mousedown event, which will kick off a drag, which will start dragging
  5835. // a new connection from this endpoint.
  5836. _currentInstance.trigger(ep.canvas, "mousedown", e, payload);
  5837. _ju.consume(e);
  5838. }.bind(this);
  5839. this.on(elInfo.el, "mousedown", mouseDownListener);
  5840. _def.trigger = mouseDownListener;
  5841. // if a filter was provided, set it as a dragFilter on the element,
  5842. // to prevent the element drag function from kicking in when we want to
  5843. // drag a new connection
  5844. if (p.filter && (_ju.isString(p.filter) || _ju.isFunction(p.filter))) {
  5845. _currentInstance.setDragFilter(elInfo.el, p.filter);
  5846. }
  5847. var dropOptions = root.jsPlumb.extend({}, p.dropOptions || {});
  5848. _makeElementDropHandler(elInfo, p, dropOptions, true, p.isTarget === true);
  5849. }.bind(this);
  5850. var inputs = el.length && el.constructor !== String ? el : [ el ];
  5851. for (var i = 0, ii = inputs.length; i < ii; i++) {
  5852. _doOne(_info(inputs[i]));
  5853. }
  5854. return this;
  5855. };
  5856. // see api docs
  5857. this.unmakeSource = function (el, connectionType, doNotClearArrays) {
  5858. var info = _info(el);
  5859. _currentInstance.destroyDroppable(info.el, "internal");
  5860. var eldefs = this.sourceEndpointDefinitions[info.id];
  5861. if (eldefs) {
  5862. for (var def in eldefs) {
  5863. if (connectionType == null || connectionType === def) {
  5864. var mouseDownListener = eldefs[def].trigger;
  5865. if (mouseDownListener) {
  5866. _currentInstance.off(info.el, "mousedown", mouseDownListener);
  5867. }
  5868. if (!doNotClearArrays) {
  5869. delete this.sourceEndpointDefinitions[info.id][def];
  5870. }
  5871. }
  5872. }
  5873. }
  5874. return this;
  5875. };
  5876. // see api docs
  5877. this.unmakeEverySource = function () {
  5878. for (var i in this.sourceEndpointDefinitions) {
  5879. _currentInstance.unmakeSource(i, null, true);
  5880. }
  5881. this.sourceEndpointDefinitions = {};
  5882. return this;
  5883. };
  5884. var _getScope = function (el, types, connectionType) {
  5885. types = _ju.isArray(types) ? types : [ types ];
  5886. var id = _getId(el);
  5887. connectionType = connectionType || "default";
  5888. for (var i = 0; i < types.length; i++) {
  5889. var eldefs = this[types[i]][id];
  5890. if (eldefs && eldefs[connectionType]) {
  5891. return eldefs[connectionType].def.scope || this.Defaults.Scope;
  5892. }
  5893. }
  5894. }.bind(this);
  5895. var _setScope = function (el, scope, types, connectionType) {
  5896. types = _ju.isArray(types) ? types : [ types ];
  5897. var id = _getId(el);
  5898. connectionType = connectionType || "default";
  5899. for (var i = 0; i < types.length; i++) {
  5900. var eldefs = this[types[i]][id];
  5901. if (eldefs && eldefs[connectionType]) {
  5902. eldefs[connectionType].def.scope = scope;
  5903. }
  5904. }
  5905. }.bind(this);
  5906. this.getScope = function (el, scope) {
  5907. return _getScope(el, [ "sourceEndpointDefinitions", "targetEndpointDefinitions" ]);
  5908. };
  5909. this.getSourceScope = function (el) {
  5910. return _getScope(el, "sourceEndpointDefinitions");
  5911. };
  5912. this.getTargetScope = function (el) {
  5913. return _getScope(el, "targetEndpointDefinitions");
  5914. };
  5915. this.setScope = function (el, scope, connectionType) {
  5916. this.setSourceScope(el, scope, connectionType);
  5917. this.setTargetScope(el, scope, connectionType);
  5918. };
  5919. this.setSourceScope = function (el, scope, connectionType) {
  5920. _setScope(el, scope, "sourceEndpointDefinitions", connectionType);
  5921. // we get the source scope during the mousedown event, but we also want to set this.
  5922. this.setDragScope(el, scope);
  5923. };
  5924. this.setTargetScope = function (el, scope, connectionType) {
  5925. _setScope(el, scope, "targetEndpointDefinitions", connectionType);
  5926. this.setDropScope(el, scope);
  5927. };
  5928. // see api docs
  5929. this.unmakeEveryTarget = function () {
  5930. for (var i in this.targetEndpointDefinitions) {
  5931. _currentInstance.unmakeTarget(i, true);
  5932. }
  5933. this.targetEndpointDefinitions = {};
  5934. return this;
  5935. };
  5936. // does the work of setting a source enabled or disabled.
  5937. var _setEnabled = function (type, el, state, toggle, connectionType) {
  5938. var a = type === "source" ? this.sourceEndpointDefinitions : this.targetEndpointDefinitions,
  5939. originalState, info, newState;
  5940. connectionType = connectionType || "default";
  5941. // a selector or an array
  5942. if (el.length && !_ju.isString(el)) {
  5943. originalState = [];
  5944. for (var i = 0, ii = el.length; i < ii; i++) {
  5945. info = _info(el[i]);
  5946. if (a[info.id] && a[info.id][connectionType]) {
  5947. originalState[i] = a[info.id][connectionType].enabled;
  5948. newState = toggle ? !originalState[i] : state;
  5949. a[info.id][connectionType].enabled = newState;
  5950. _currentInstance[newState ? "removeClass" : "addClass"](info.el, "jtk-" + type + "-disabled");
  5951. }
  5952. }
  5953. }
  5954. // otherwise a DOM element or a String ID.
  5955. else {
  5956. info = _info(el);
  5957. var id = info.id;
  5958. if (a[id] && a[id][connectionType]) {
  5959. originalState = a[id][connectionType].enabled;
  5960. newState = toggle ? !originalState : state;
  5961. a[id][connectionType].enabled = newState;
  5962. _currentInstance[newState ? "removeClass" : "addClass"](info.el, "jtk-" + type + "-disabled");
  5963. }
  5964. }
  5965. return originalState;
  5966. }.bind(this);
  5967. var _first = function (el, fn) {
  5968. if (el != null) {
  5969. if (_ju.isString(el) || !el.length) {
  5970. return fn.apply(this, [el]);
  5971. } else if (el.length) {
  5972. return fn.apply(this, [el[0]]);
  5973. }
  5974. }
  5975. }.bind(this);
  5976. this.toggleSourceEnabled = function (el, connectionType) {
  5977. _setEnabled("source", el, null, true, connectionType);
  5978. return this.isSourceEnabled(el, connectionType);
  5979. };
  5980. this.setSourceEnabled = function (el, state, connectionType) {
  5981. return _setEnabled("source", el, state, null, connectionType);
  5982. };
  5983. this.isSource = function (el, connectionType) {
  5984. connectionType = connectionType || "default";
  5985. return _first(el, function (_el) {
  5986. var eldefs = this.sourceEndpointDefinitions[_info(_el).id];
  5987. return eldefs != null && eldefs[connectionType] != null;
  5988. }.bind(this));
  5989. };
  5990. this.isSourceEnabled = function (el, connectionType) {
  5991. connectionType = connectionType || "default";
  5992. return _first(el, function (_el) {
  5993. var sep = this.sourceEndpointDefinitions[_info(_el).id];
  5994. return sep && sep[connectionType] && sep[connectionType].enabled === true;
  5995. }.bind(this));
  5996. };
  5997. this.toggleTargetEnabled = function (el, connectionType) {
  5998. _setEnabled("target", el, null, true, connectionType);
  5999. return this.isTargetEnabled(el, connectionType);
  6000. };
  6001. this.isTarget = function (el, connectionType) {
  6002. connectionType = connectionType || "default";
  6003. return _first(el, function (_el) {
  6004. var eldefs = this.targetEndpointDefinitions[_info(_el).id];
  6005. return eldefs != null && eldefs[connectionType] != null;
  6006. }.bind(this));
  6007. };
  6008. this.isTargetEnabled = function (el, connectionType) {
  6009. connectionType = connectionType || "default";
  6010. return _first(el, function (_el) {
  6011. var tep = this.targetEndpointDefinitions[_info(_el).id];
  6012. return tep && tep[connectionType] && tep[connectionType].enabled === true;
  6013. }.bind(this));
  6014. };
  6015. this.setTargetEnabled = function (el, state, connectionType) {
  6016. return _setEnabled("target", el, state, null, connectionType);
  6017. };
  6018. // --------------------- end makeSource/makeTarget ----------------------------------------------
  6019. this.ready = function (fn) {
  6020. _currentInstance.bind("ready", fn);
  6021. };
  6022. var _elEach = function(el, fn) {
  6023. // support both lists...
  6024. if (typeof el === 'object' && el.length) {
  6025. for (var i = 0, ii = el.length; i < ii; i++) {
  6026. fn(el[i]);
  6027. }
  6028. }
  6029. else {// ...and single strings or elements.
  6030. fn(el);
  6031. }
  6032. return _currentInstance;
  6033. };
  6034. // repaint some element's endpoints and connections
  6035. this.repaint = function (el, ui, timestamp) {
  6036. return _elEach(el, function(_el) {
  6037. _draw(_el, ui, timestamp);
  6038. });
  6039. };
  6040. this.revalidate = function (el, timestamp, isIdAlready) {
  6041. var elId = isIdAlready ? el : _currentInstance.getId(el);
  6042. _currentInstance.updateOffset({ elId: elId, recalc: true, timestamp:timestamp });
  6043. var dm = _currentInstance.getDragManager();
  6044. if (dm) {
  6045. dm.updateOffsets(elId);
  6046. }
  6047. return _draw(el, null, timestamp);
  6048. };
  6049. // repaint every endpoint and connection.
  6050. this.repaintEverything = function () {
  6051. // TODO this timestamp causes continuous anchors to not repaint properly.
  6052. // fix this. do not just take out the timestamp. it runs a lot faster with
  6053. // the timestamp included.
  6054. var timestamp = jsPlumbUtil.uuid(), elId;
  6055. for (elId in endpointsByElement) {
  6056. _currentInstance.updateOffset({ elId: elId, recalc: true, timestamp: timestamp });
  6057. }
  6058. for (elId in endpointsByElement) {
  6059. _draw(elId, null, timestamp);
  6060. }
  6061. return this;
  6062. };
  6063. this.removeAllEndpoints = function (el, recurse, affectedElements) {
  6064. affectedElements = affectedElements || [];
  6065. var _one = function (_el) {
  6066. var info = _info(_el),
  6067. ebe = endpointsByElement[info.id],
  6068. i, ii;
  6069. if (ebe) {
  6070. affectedElements.push(info);
  6071. for (i = 0, ii = ebe.length; i < ii; i++) {
  6072. _currentInstance.deleteEndpoint(ebe[i], false);
  6073. }
  6074. }
  6075. delete endpointsByElement[info.id];
  6076. if (recurse) {
  6077. if (info.el && info.el.nodeType !== 3 && info.el.nodeType !== 8) {
  6078. for (i = 0, ii = info.el.childNodes.length; i < ii; i++) {
  6079. _one(info.el.childNodes[i]);
  6080. }
  6081. }
  6082. }
  6083. };
  6084. _one(el);
  6085. return this;
  6086. };
  6087. var _doRemove = function(info, affectedElements) {
  6088. _currentInstance.removeAllEndpoints(info.id, true, affectedElements);
  6089. var dm = _currentInstance.getDragManager();
  6090. var _one = function(_info) {
  6091. if (dm) {
  6092. dm.elementRemoved(_info.id);
  6093. }
  6094. _currentInstance.router.elementRemoved(_info.id);
  6095. if (_currentInstance.isSource(_info.el)) {
  6096. _currentInstance.unmakeSource(_info.el);
  6097. }
  6098. if (_currentInstance.isTarget(_info.el)) {
  6099. _currentInstance.unmakeTarget(_info.el);
  6100. }
  6101. _currentInstance.destroyDraggable(_info.el);
  6102. _currentInstance.destroyDroppable(_info.el);
  6103. delete _currentInstance.floatingConnections[_info.id];
  6104. delete managedElements[_info.id];
  6105. delete offsets[_info.id];
  6106. if (_info.el) {
  6107. _currentInstance.removeElement(_info.el);
  6108. _info.el._jsPlumb = null;
  6109. }
  6110. };
  6111. // remove all affected child elements
  6112. for (var ae = 1; ae < affectedElements.length; ae++) {
  6113. _one(affectedElements[ae]);
  6114. }
  6115. // and always remove the requested one from the dom.
  6116. _one(info);
  6117. };
  6118. /**
  6119. * Remove the given element, including cleaning up all endpoints registered for it.
  6120. * This is exposed in the public API but also used internally by jsPlumb when removing the
  6121. * element associated with a connection drag.
  6122. */
  6123. this.remove = function (el, doNotRepaint) {
  6124. var info = _info(el), affectedElements = [];
  6125. if (info.text && info.el.parentNode) {
  6126. info.el.parentNode.removeChild(info.el);
  6127. }
  6128. else if (info.id) {
  6129. _currentInstance.batch(function () {
  6130. _doRemove(info, affectedElements);
  6131. }, doNotRepaint === true);
  6132. }
  6133. return _currentInstance;
  6134. };
  6135. this.empty = function (el, doNotRepaint) {
  6136. var affectedElements = [];
  6137. var _one = function(el, dontRemoveFocus) {
  6138. var info = _info(el);
  6139. if (info.text) {
  6140. info.el.parentNode.removeChild(info.el);
  6141. }
  6142. else if (info.el) {
  6143. while(info.el.childNodes.length > 0) {
  6144. _one(info.el.childNodes[0]);
  6145. }
  6146. if (!dontRemoveFocus) {
  6147. _doRemove(info, affectedElements);
  6148. }
  6149. }
  6150. };
  6151. _currentInstance.batch(function() {
  6152. _one(el, true);
  6153. }, doNotRepaint === false);
  6154. return _currentInstance;
  6155. };
  6156. this.reset = function (doNotUnbindInstanceEventListeners) {
  6157. _currentInstance.silently(function() {
  6158. _hoverSuspended = false;
  6159. _currentInstance.removeAllGroups();
  6160. _currentInstance.removeGroupManager();
  6161. _currentInstance.deleteEveryEndpoint();
  6162. if (!doNotUnbindInstanceEventListeners) {
  6163. _currentInstance.unbind();
  6164. }
  6165. this.targetEndpointDefinitions = {};
  6166. this.sourceEndpointDefinitions = {};
  6167. connections.length = 0;
  6168. if (this.doReset) {
  6169. this.doReset();
  6170. }
  6171. }.bind(this));
  6172. };
  6173. this.destroy = function() {
  6174. this.reset();
  6175. _container = null;
  6176. _containerDelegations = null;
  6177. };
  6178. var _clearObject = function (obj) {
  6179. if (obj.canvas && obj.canvas.parentNode) {
  6180. obj.canvas.parentNode.removeChild(obj.canvas);
  6181. }
  6182. obj.cleanup();
  6183. obj.destroy();
  6184. };
  6185. this.clear = function () {
  6186. _currentInstance.select().each(_clearObject);
  6187. _currentInstance.selectEndpoints().each(_clearObject);
  6188. endpointsByElement = {};
  6189. endpointsByUUID = {};
  6190. };
  6191. this.setDefaultScope = function (scope) {
  6192. DEFAULT_SCOPE = scope;
  6193. return _currentInstance;
  6194. };
  6195. this.deriveEndpointAndAnchorSpec = function(type, dontPrependDefault) {
  6196. var bits = ((dontPrependDefault ? "" : "default ") + type).split(/[\s]/), eps = null, ep = null, a = null, as = null;
  6197. for (var i = 0; i < bits.length; i++) {
  6198. var _t = _currentInstance.getType(bits[i], "connection");
  6199. if (_t) {
  6200. if (_t.endpoints) {
  6201. eps = _t.endpoints;
  6202. }
  6203. if (_t.endpoint) {
  6204. ep = _t.endpoint;
  6205. }
  6206. if (_t.anchors) {
  6207. as = _t.anchors;
  6208. }
  6209. if (_t.anchor) {
  6210. a = _t.anchor;
  6211. }
  6212. }
  6213. }
  6214. return { endpoints: eps ? eps : [ ep, ep ], anchors: as ? as : [a, a ]};
  6215. };
  6216. // sets the id of some element, changing whatever we need to to keep track.
  6217. this.setId = function (el, newId, doNotSetAttribute) {
  6218. //
  6219. var id;
  6220. if (_ju.isString(el)) {
  6221. id = el;
  6222. }
  6223. else {
  6224. el = this.getElement(el);
  6225. id = this.getId(el);
  6226. }
  6227. var sConns = this.getConnections({source: id, scope: '*'}, true),
  6228. tConns = this.getConnections({target: id, scope: '*'}, true);
  6229. newId = "" + newId;
  6230. if (!doNotSetAttribute) {
  6231. el = this.getElement(id);
  6232. this.setAttribute(el, "id", newId);
  6233. }
  6234. else {
  6235. el = this.getElement(newId);
  6236. }
  6237. endpointsByElement[newId] = endpointsByElement[id] || [];
  6238. for (var i = 0, ii = endpointsByElement[newId].length; i < ii; i++) {
  6239. endpointsByElement[newId][i].setElementId(newId);
  6240. endpointsByElement[newId][i].setReferenceElement(el);
  6241. }
  6242. delete endpointsByElement[id];
  6243. this.sourceEndpointDefinitions[newId] = this.sourceEndpointDefinitions[id];
  6244. delete this.sourceEndpointDefinitions[id];
  6245. this.targetEndpointDefinitions[newId] = this.targetEndpointDefinitions[id];
  6246. delete this.targetEndpointDefinitions[id];
  6247. this.router.changeId(id, newId);
  6248. var dm = this.getDragManager();
  6249. if (dm) {
  6250. dm.changeId(id, newId);
  6251. }
  6252. managedElements[newId] = managedElements[id];
  6253. delete managedElements[id];
  6254. var _conns = function (list, epIdx, type) {
  6255. for (var i = 0, ii = list.length; i < ii; i++) {
  6256. list[i].endpoints[epIdx].setElementId(newId);
  6257. list[i].endpoints[epIdx].setReferenceElement(el);
  6258. list[i][type + "Id"] = newId;
  6259. list[i][type] = el;
  6260. }
  6261. };
  6262. _conns(sConns, 0, "source");
  6263. _conns(tConns, 1, "target");
  6264. this.repaint(newId);
  6265. };
  6266. this.setDebugLog = function (debugLog) {
  6267. log = debugLog;
  6268. };
  6269. this.setSuspendDrawing = function (val, repaintAfterwards) {
  6270. var curVal = _suspendDrawing;
  6271. _suspendDrawing = val;
  6272. if (val) {
  6273. _suspendedAt = new Date().getTime();
  6274. } else {
  6275. _suspendedAt = null;
  6276. }
  6277. if (repaintAfterwards) {
  6278. this.repaintEverything();
  6279. }
  6280. return curVal;
  6281. };
  6282. // returns whether or not drawing is currently suspended.
  6283. this.isSuspendDrawing = function () {
  6284. return _suspendDrawing;
  6285. };
  6286. // return timestamp for when drawing was suspended.
  6287. this.getSuspendedAt = function () {
  6288. return _suspendedAt;
  6289. };
  6290. this.batch = function (fn, doNotRepaintAfterwards) {
  6291. var _wasSuspended = this.isSuspendDrawing();
  6292. if (!_wasSuspended) {
  6293. this.setSuspendDrawing(true);
  6294. }
  6295. try {
  6296. fn();
  6297. }
  6298. catch (e) {
  6299. _ju.log("Function run while suspended failed", e);
  6300. }
  6301. if (!_wasSuspended) {
  6302. this.setSuspendDrawing(false, !doNotRepaintAfterwards);
  6303. }
  6304. };
  6305. this.doWhileSuspended = this.batch;
  6306. this.getCachedData = _getCachedData;
  6307. this.show = function (el, changeEndpoints) {
  6308. _setVisible(el, "block", changeEndpoints);
  6309. return _currentInstance;
  6310. };
  6311. // TODO: update this method to return the current state.
  6312. this.toggleVisible = _toggleVisible;
  6313. this.addListener = this.bind;
  6314. var floatingConnections = [];
  6315. this.registerFloatingConnection = function(info, conn, ep) {
  6316. floatingConnections[info.id] = conn;
  6317. // only register for the target endpoint; we will not be dragging the source at any time
  6318. // before this connection is either discarded or made into a permanent connection.
  6319. _ju.addToList(endpointsByElement, info.id, ep);
  6320. };
  6321. this.getFloatingConnectionFor = function(id) {
  6322. return floatingConnections[id];
  6323. };
  6324. this.listManager = new root.jsPlumbListManager(this, this.Defaults.ListStyle);
  6325. };
  6326. _ju.extend(root.jsPlumbInstance, _ju.EventGenerator, {
  6327. setAttribute: function (el, a, v) {
  6328. this.setAttribute(el, a, v);
  6329. },
  6330. getAttribute: function (el, a) {
  6331. return this.getAttribute(root.jsPlumb.getElement(el), a);
  6332. },
  6333. convertToFullOverlaySpec: function(spec) {
  6334. if (_ju.isString(spec)) {
  6335. spec = [ spec, { } ];
  6336. }
  6337. spec[1].id = spec[1].id || _ju.uuid();
  6338. return spec;
  6339. },
  6340. registerConnectionType: function (id, type) {
  6341. this._connectionTypes[id] = root.jsPlumb.extend({}, type);
  6342. if (type.overlays) {
  6343. var to = {};
  6344. for (var i = 0; i < type.overlays.length; i++) {
  6345. // if a string, convert to object representation so that we can store the typeid on it.
  6346. // also assign an id.
  6347. var fo = this.convertToFullOverlaySpec(type.overlays[i]);
  6348. to[fo[1].id] = fo;
  6349. }
  6350. this._connectionTypes[id].overlays = to;
  6351. }
  6352. },
  6353. registerConnectionTypes: function (types) {
  6354. for (var i in types) {
  6355. this.registerConnectionType(i, types[i]);
  6356. }
  6357. },
  6358. registerEndpointType: function (id, type) {
  6359. this._endpointTypes[id] = root.jsPlumb.extend({}, type);
  6360. if (type.overlays) {
  6361. var to = {};
  6362. for (var i = 0; i < type.overlays.length; i++) {
  6363. // if a string, convert to object representation so that we can store the typeid on it.
  6364. // also assign an id.
  6365. var fo = this.convertToFullOverlaySpec(type.overlays[i]);
  6366. to[fo[1].id] = fo;
  6367. }
  6368. this._endpointTypes[id].overlays = to;
  6369. }
  6370. },
  6371. registerEndpointTypes: function (types) {
  6372. for (var i in types) {
  6373. this.registerEndpointType(i, types[i]);
  6374. }
  6375. },
  6376. getType: function (id, typeDescriptor) {
  6377. return typeDescriptor === "connection" ? this._connectionTypes[id] : this._endpointTypes[id];
  6378. },
  6379. setIdChanged: function (oldId, newId) {
  6380. this.setId(oldId, newId, true);
  6381. },
  6382. // set parent: change the parent for some node and update all the registrations we need to.
  6383. setParent: function (el, newParent) {
  6384. var _dom = this.getElement(el),
  6385. _id = this.getId(_dom),
  6386. _pdom = this.getElement(newParent),
  6387. _pid = this.getId(_pdom),
  6388. dm = this.getDragManager();
  6389. _dom.parentNode.removeChild(_dom);
  6390. _pdom.appendChild(_dom);
  6391. if (dm) {
  6392. dm.setParent(_dom, _id, _pdom, _pid);
  6393. }
  6394. },
  6395. extend: function (o1, o2, names) {
  6396. var i;
  6397. if (names) {
  6398. for (i = 0; i < names.length; i++) {
  6399. o1[names[i]] = o2[names[i]];
  6400. }
  6401. }
  6402. else {
  6403. for (i in o2) {
  6404. o1[i] = o2[i];
  6405. }
  6406. }
  6407. return o1;
  6408. },
  6409. floatingConnections: {},
  6410. getFloatingAnchorIndex: function (jpc) {
  6411. return jpc.endpoints[0].isFloating() ? 0 : jpc.endpoints[1].isFloating() ? 1 : -1;
  6412. },
  6413. proxyConnection :function(connection, index, proxyEl, proxyElId, endpointGenerator, anchorGenerator) {
  6414. var proxyEp,
  6415. originalElementId = connection.endpoints[index].elementId,
  6416. originalEndpoint = connection.endpoints[index];
  6417. connection.proxies = connection.proxies || [];
  6418. if(connection.proxies[index]) {
  6419. proxyEp = connection.proxies[index].ep;
  6420. }else {
  6421. proxyEp = this.addEndpoint(proxyEl, {
  6422. endpoint:endpointGenerator(connection, index),
  6423. anchor:anchorGenerator(connection, index),
  6424. parameters:{
  6425. isProxyEndpoint:true
  6426. }
  6427. });
  6428. }
  6429. proxyEp.setDeleteOnEmpty(true);
  6430. // for this index, stash proxy info: the new EP, the original EP.
  6431. connection.proxies[index] = { ep:proxyEp, originalEp: originalEndpoint };
  6432. // and advise the anchor manager
  6433. if (index === 0) {
  6434. this.router.sourceOrTargetChanged(originalElementId, proxyElId, connection, proxyEl, 0);
  6435. }
  6436. else {
  6437. this.router.sourceOrTargetChanged(originalElementId, proxyElId, connection, proxyEl, 1);
  6438. }
  6439. // detach the original EP from the connection.
  6440. originalEndpoint.detachFromConnection(connection, null, true);
  6441. // set the proxy as the new ep
  6442. proxyEp.connections = [ connection ];
  6443. connection.endpoints[index] = proxyEp;
  6444. originalEndpoint.setVisible(false);
  6445. connection.setVisible(true);
  6446. this.revalidate(proxyEl);
  6447. },
  6448. unproxyConnection : function(connection, index, proxyElId) {
  6449. // if connection cleaned up, no proxies, or none for this end of the connection, abort.
  6450. if (connection._jsPlumb == null || connection.proxies == null || connection.proxies[index] == null) {
  6451. return;
  6452. }
  6453. var originalElement = connection.proxies[index].originalEp.element,
  6454. originalElementId = connection.proxies[index].originalEp.elementId;
  6455. connection.endpoints[index] = connection.proxies[index].originalEp;
  6456. // and advise the anchor manager
  6457. if (index === 0) {
  6458. // TODO why are there two differently named methods? Why is there not one method that says "some end of this
  6459. // connection changed (you give the index), and here's the new element and element id."
  6460. this.router.sourceOrTargetChanged(proxyElId, originalElementId, connection, originalElement, 0);
  6461. }
  6462. else {
  6463. this.router.sourceOrTargetChanged(proxyElId, originalElementId, connection, originalElement, 1);
  6464. }
  6465. // detach the proxy EP from the connection (which will cause it to be removed as we no longer need it)
  6466. connection.proxies[index].ep.detachFromConnection(connection, null);
  6467. connection.proxies[index].originalEp.addConnection(connection);
  6468. if(connection.isVisible()) {
  6469. connection.proxies[index].originalEp.setVisible(true);
  6470. }
  6471. // cleanup
  6472. delete connection.proxies[index];
  6473. }
  6474. });
  6475. // --------------------- static instance + module registration -------------------------------------------
  6476. // create static instance and assign to window if window exists.
  6477. var jsPlumb = new jsPlumbInstance();
  6478. // register on 'root' (lets us run on server or browser)
  6479. root.jsPlumb = jsPlumb;
  6480. // add 'getInstance' method to static instance
  6481. jsPlumb.getInstance = function (_defaults, overrideFns) {
  6482. var j = new jsPlumbInstance(_defaults);
  6483. if (overrideFns) {
  6484. for (var ovf in overrideFns) {
  6485. j[ovf] = overrideFns[ovf];
  6486. }
  6487. }
  6488. j.init();
  6489. return j;
  6490. };
  6491. jsPlumb.each = function (spec, fn) {
  6492. if (spec == null) {
  6493. return;
  6494. }
  6495. if (typeof spec === "string") {
  6496. fn(jsPlumb.getElement(spec));
  6497. }
  6498. else if (spec.length != null) {
  6499. for (var i = 0; i < spec.length; i++) {
  6500. fn(jsPlumb.getElement(spec[i]));
  6501. }
  6502. }
  6503. else {
  6504. fn(spec);
  6505. } // assume it's an element.
  6506. };
  6507. // CommonJS
  6508. if (typeof exports !== 'undefined') {
  6509. exports.jsPlumb = jsPlumb;
  6510. }
  6511. // --------------------- end static instance + AMD registration -------------------------------------------
  6512. }).call(typeof window !== 'undefined' ? window : this);
  6513. /*
  6514. * Copyright (c) 2010 - 2020 jsPlumb (hello@jsplumbtoolkit.com)
  6515. *
  6516. * https://jsplumbtoolkit.com
  6517. * https://github.com/jsplumb/jsplumb
  6518. *
  6519. * Dual licensed under the MIT and GPL2 licenses.
  6520. */
  6521. ;(function() {
  6522. "use strict";
  6523. var root = this, _jp = root.jsPlumb, _ju = root.jsPlumbUtil;
  6524. // ------------------------------ BEGIN OverlayCapablejsPlumbUIComponent --------------------------------------------
  6525. var _internalLabelOverlayId = "__label",
  6526. // this is a shortcut helper method to let people add a label as
  6527. // overlay.
  6528. _makeLabelOverlay = function (component, params) {
  6529. var _params = {
  6530. cssClass: params.cssClass,
  6531. labelStyle: component.labelStyle,
  6532. id: _internalLabelOverlayId,
  6533. component: component,
  6534. _jsPlumb: component._jsPlumb.instance // TODO not necessary, since the instance can be accessed through the component.
  6535. },
  6536. mergedParams = _jp.extend(_params, params);
  6537. return new _jp.Overlays[component._jsPlumb.instance.getRenderMode()].Label(mergedParams);
  6538. },
  6539. _processOverlay = function (component, o) {
  6540. var _newOverlay = null;
  6541. if (_ju.isArray(o)) { // this is for the shorthand ["Arrow", { width:50 }] syntax
  6542. // there's also a three arg version:
  6543. // ["Arrow", { width:50 }, {location:0.7}]
  6544. // which merges the 3rd arg into the 2nd.
  6545. var type = o[0],
  6546. // make a copy of the object so as not to mess up anyone else's reference...
  6547. p = _jp.extend({component: component, _jsPlumb: component._jsPlumb.instance}, o[1]);
  6548. if (o.length === 3) {
  6549. _jp.extend(p, o[2]);
  6550. }
  6551. _newOverlay = new _jp.Overlays[component._jsPlumb.instance.getRenderMode()][type](p);
  6552. } else if (o.constructor === String) {
  6553. _newOverlay = new _jp.Overlays[component._jsPlumb.instance.getRenderMode()][o]({component: component, _jsPlumb: component._jsPlumb.instance});
  6554. } else {
  6555. _newOverlay = o;
  6556. }
  6557. _newOverlay.id = _newOverlay.id || _ju.uuid();
  6558. component.cacheTypeItem("overlay", _newOverlay, _newOverlay.id);
  6559. component._jsPlumb.overlays[_newOverlay.id] = _newOverlay;
  6560. return _newOverlay;
  6561. };
  6562. _jp.OverlayCapableJsPlumbUIComponent = function (params) {
  6563. root.jsPlumbUIComponent.apply(this, arguments);
  6564. this._jsPlumb.overlays = {};
  6565. this._jsPlumb.overlayPositions = {};
  6566. if (params.label) {
  6567. this.getDefaultType().overlays[_internalLabelOverlayId] = ["Label", {
  6568. label: params.label,
  6569. location: params.labelLocation || this.defaultLabelLocation || 0.5,
  6570. labelStyle: params.labelStyle || this._jsPlumb.instance.Defaults.LabelStyle,
  6571. id:_internalLabelOverlayId
  6572. }];
  6573. }
  6574. this.setListenerComponent = function (c) {
  6575. if (this._jsPlumb) {
  6576. for (var i in this._jsPlumb.overlays) {
  6577. this._jsPlumb.overlays[i].setListenerComponent(c);
  6578. }
  6579. }
  6580. };
  6581. };
  6582. _jp.OverlayCapableJsPlumbUIComponent.applyType = function (component, t) {
  6583. if (t.overlays) {
  6584. // loop through the ones in the type. if already present on the component,
  6585. // dont remove or re-add.
  6586. var keep = {}, i;
  6587. for (i in t.overlays) {
  6588. var existing = component._jsPlumb.overlays[t.overlays[i][1].id];
  6589. if (existing) {
  6590. // maybe update from data, if there were parameterised values for instance.
  6591. existing.updateFrom(t.overlays[i][1]);
  6592. keep[t.overlays[i][1].id] = true;
  6593. existing.reattach(component._jsPlumb.instance, component);
  6594. }
  6595. else {
  6596. var c = component.getCachedTypeItem("overlay", t.overlays[i][1].id);
  6597. if (c != null) {
  6598. c.reattach(component._jsPlumb.instance, component);
  6599. c.setVisible(true);
  6600. // maybe update from data, if there were parameterised values for instance.
  6601. c.updateFrom(t.overlays[i][1]);
  6602. component._jsPlumb.overlays[c.id] = c;
  6603. }
  6604. else {
  6605. c = component.addOverlay(t.overlays[i], true);
  6606. }
  6607. keep[c.id] = true;
  6608. }
  6609. }
  6610. // now loop through the full overlays and remove those that we dont want to keep
  6611. for (i in component._jsPlumb.overlays) {
  6612. if (keep[component._jsPlumb.overlays[i].id] == null) {
  6613. component.removeOverlay(component._jsPlumb.overlays[i].id, true); // remove overlay but dont clean it up.
  6614. // that would remove event listeners etc; overlays are never discarded by the types stuff, they are
  6615. // just detached/reattached.
  6616. }
  6617. }
  6618. }
  6619. };
  6620. _ju.extend(_jp.OverlayCapableJsPlumbUIComponent, root.jsPlumbUIComponent, {
  6621. setHover: function (hover, ignoreAttachedElements) {
  6622. if (this._jsPlumb && !this._jsPlumb.instance.isConnectionBeingDragged()) {
  6623. for (var i in this._jsPlumb.overlays) {
  6624. this._jsPlumb.overlays[i][hover ? "addClass" : "removeClass"](this._jsPlumb.instance.hoverClass);
  6625. }
  6626. }
  6627. },
  6628. addOverlay: function (overlay, doNotRepaint) {
  6629. var o = _processOverlay(this, overlay);
  6630. if (this.getData && o.type === "Label" && _ju.isArray(overlay)) {
  6631. //
  6632. // component data might contain label location - look for it here.
  6633. var d = this.getData(), p = overlay[1];
  6634. if (d) {
  6635. var locationAttribute = p.labelLocationAttribute || "labelLocation";
  6636. var loc = d ? d[locationAttribute] : null;
  6637. if (loc) {
  6638. o.loc = loc;
  6639. }
  6640. }
  6641. }
  6642. if (!doNotRepaint) {
  6643. this.repaint();
  6644. }
  6645. return o;
  6646. },
  6647. getOverlay: function (id) {
  6648. return this._jsPlumb.overlays[id];
  6649. },
  6650. getOverlays: function () {
  6651. return this._jsPlumb.overlays;
  6652. },
  6653. hideOverlay: function (id) {
  6654. var o = this.getOverlay(id);
  6655. if (o) {
  6656. o.hide();
  6657. }
  6658. },
  6659. hideOverlays: function () {
  6660. for (var i in this._jsPlumb.overlays) {
  6661. this._jsPlumb.overlays[i].hide();
  6662. }
  6663. },
  6664. showOverlay: function (id) {
  6665. var o = this.getOverlay(id);
  6666. if (o) {
  6667. o.show();
  6668. }
  6669. },
  6670. showOverlays: function () {
  6671. for (var i in this._jsPlumb.overlays) {
  6672. this._jsPlumb.overlays[i].show();
  6673. }
  6674. },
  6675. removeAllOverlays: function (doNotRepaint) {
  6676. for (var i in this._jsPlumb.overlays) {
  6677. if (this._jsPlumb.overlays[i].cleanup) {
  6678. this._jsPlumb.overlays[i].cleanup();
  6679. }
  6680. }
  6681. this._jsPlumb.overlays = {};
  6682. this._jsPlumb.overlayPositions = null;
  6683. this._jsPlumb.overlayPlacements= {};
  6684. if (!doNotRepaint) {
  6685. this.repaint();
  6686. }
  6687. },
  6688. removeOverlay: function (overlayId, dontCleanup) {
  6689. var o = this._jsPlumb.overlays[overlayId];
  6690. if (o) {
  6691. o.setVisible(false);
  6692. if (!dontCleanup && o.cleanup) {
  6693. o.cleanup();
  6694. }
  6695. delete this._jsPlumb.overlays[overlayId];
  6696. if (this._jsPlumb.overlayPositions) {
  6697. delete this._jsPlumb.overlayPositions[overlayId];
  6698. }
  6699. if (this._jsPlumb.overlayPlacements) {
  6700. delete this._jsPlumb.overlayPlacements[overlayId];
  6701. }
  6702. }
  6703. },
  6704. removeOverlays: function () {
  6705. for (var i = 0, j = arguments.length; i < j; i++) {
  6706. this.removeOverlay(arguments[i]);
  6707. }
  6708. },
  6709. moveParent: function (newParent) {
  6710. if (this.bgCanvas) {
  6711. this.bgCanvas.parentNode.removeChild(this.bgCanvas);
  6712. newParent.appendChild(this.bgCanvas);
  6713. }
  6714. if (this.canvas && this.canvas.parentNode) {
  6715. this.canvas.parentNode.removeChild(this.canvas);
  6716. newParent.appendChild(this.canvas);
  6717. for (var i in this._jsPlumb.overlays) {
  6718. if (this._jsPlumb.overlays[i].isAppendedAtTopLevel) {
  6719. var el = this._jsPlumb.overlays[i].getElement();
  6720. el.parentNode.removeChild(el);
  6721. newParent.appendChild(el);
  6722. }
  6723. }
  6724. }
  6725. },
  6726. getLabel: function () {
  6727. var lo = this.getOverlay(_internalLabelOverlayId);
  6728. return lo != null ? lo.getLabel() : null;
  6729. },
  6730. getLabelOverlay: function () {
  6731. return this.getOverlay(_internalLabelOverlayId);
  6732. },
  6733. setLabel: function (l) {
  6734. var lo = this.getOverlay(_internalLabelOverlayId);
  6735. if (!lo) {
  6736. var params = l.constructor === String || l.constructor === Function ? { label: l } : l;
  6737. lo = _makeLabelOverlay(this, params);
  6738. this._jsPlumb.overlays[_internalLabelOverlayId] = lo;
  6739. }
  6740. else {
  6741. if (l.constructor === String || l.constructor === Function) {
  6742. lo.setLabel(l);
  6743. }
  6744. else {
  6745. if (l.label) {
  6746. lo.setLabel(l.label);
  6747. }
  6748. if (l.location) {
  6749. lo.setLocation(l.location);
  6750. }
  6751. }
  6752. }
  6753. if (!this._jsPlumb.instance.isSuspendDrawing()) {
  6754. this.repaint();
  6755. }
  6756. },
  6757. cleanup: function (force) {
  6758. for (var i in this._jsPlumb.overlays) {
  6759. this._jsPlumb.overlays[i].cleanup(force);
  6760. this._jsPlumb.overlays[i].destroy(force);
  6761. }
  6762. if (force) {
  6763. this._jsPlumb.overlays = {};
  6764. this._jsPlumb.overlayPositions = null;
  6765. }
  6766. },
  6767. setVisible: function (v) {
  6768. this[v ? "showOverlays" : "hideOverlays"]();
  6769. },
  6770. setAbsoluteOverlayPosition: function (overlay, xy) {
  6771. this._jsPlumb.overlayPositions[overlay.id] = xy;
  6772. },
  6773. getAbsoluteOverlayPosition: function (overlay) {
  6774. return this._jsPlumb.overlayPositions ? this._jsPlumb.overlayPositions[overlay.id] : null;
  6775. },
  6776. _clazzManip:function(action, clazz, dontUpdateOverlays) {
  6777. if (!dontUpdateOverlays) {
  6778. for (var i in this._jsPlumb.overlays) {
  6779. this._jsPlumb.overlays[i][action + "Class"](clazz);
  6780. }
  6781. }
  6782. },
  6783. addClass:function(clazz, dontUpdateOverlays) {
  6784. this._clazzManip("add", clazz, dontUpdateOverlays);
  6785. },
  6786. removeClass:function(clazz, dontUpdateOverlays) {
  6787. this._clazzManip("remove", clazz, dontUpdateOverlays);
  6788. }
  6789. });
  6790. // ------------------------------ END OverlayCapablejsPlumbUIComponent --------------------------------------------
  6791. }).call(typeof window !== 'undefined' ? window : this);
  6792. /*
  6793. * This file contains the code for Endpoints.
  6794. *
  6795. * Copyright (c) 2010 - 2020 jsPlumb (hello@jsplumbtoolkit.com)
  6796. *
  6797. * https://jsplumbtoolkit.com
  6798. * https://github.com/jsplumb/jsplumb
  6799. *
  6800. * Dual licensed under the MIT and GPL2 licenses.
  6801. */
  6802. ;(function () {
  6803. "use strict";
  6804. var root = this, _jp = root.jsPlumb, _ju = root.jsPlumbUtil;
  6805. // create the drag handler for a connection
  6806. var _makeConnectionDragHandler = function (endpoint, placeholder, _jsPlumb) {
  6807. var stopped = false;
  6808. return {
  6809. drag: function () {
  6810. if (stopped) {
  6811. stopped = false;
  6812. return true;
  6813. }
  6814. if (placeholder.element) {
  6815. var _ui = _jsPlumb.getUIPosition(arguments, _jsPlumb.getZoom());
  6816. if (_ui != null) {
  6817. _jsPlumb.setPosition(placeholder.element, _ui);
  6818. }
  6819. _jsPlumb.repaint(placeholder.element, _ui);
  6820. // always repaint the source endpoint, because only continuous/dynamic anchors cause the endpoint
  6821. // to be repainted, so static anchors need to be told (or the endpoint gets dragged around)
  6822. endpoint.paint({anchorPoint:endpoint.anchor.getCurrentLocation({element:endpoint})});
  6823. }
  6824. },
  6825. stopDrag: function () {
  6826. stopped = true;
  6827. }
  6828. };
  6829. };
  6830. // creates a placeholder div for dragging purposes, adds it, and pre-computes its offset.
  6831. var _makeDraggablePlaceholder = function (placeholder, _jsPlumb, ipco, ips) {
  6832. var n = _jsPlumb.createElement("div", { position : "absolute" });
  6833. _jsPlumb.appendElement(n);
  6834. var id = _jsPlumb.getId(n);
  6835. _jsPlumb.setPosition(n, ipco);
  6836. n.style.width = ips[0] + "px";
  6837. n.style.height = ips[1] + "px";
  6838. _jsPlumb.manage(id, n, true); // TRANSIENT MANAGE
  6839. // create and assign an id, and initialize the offset.
  6840. placeholder.id = id;
  6841. placeholder.element = n;
  6842. };
  6843. // create a floating endpoint (for drag connections)
  6844. var _makeFloatingEndpoint = function (paintStyle, referenceAnchor, endpoint, referenceCanvas, sourceElement, _jsPlumb, _newEndpoint, scope) {
  6845. var floatingAnchor = new _jp.FloatingAnchor({ reference: referenceAnchor, referenceCanvas: referenceCanvas, jsPlumbInstance: _jsPlumb });
  6846. return _newEndpoint({
  6847. paintStyle: paintStyle,
  6848. endpoint: endpoint,
  6849. anchor: floatingAnchor,
  6850. source: sourceElement,
  6851. scope: scope
  6852. });
  6853. };
  6854. var typeParameters = [ "connectorStyle", "connectorHoverStyle", "connectorOverlays",
  6855. "connector", "connectionType", "connectorClass", "connectorHoverClass" ];
  6856. // a helper function that tries to find a connection to the given element, and returns it if so. if elementWithPrecedence is null,
  6857. // or no connection to it is found, we return the first connection in our list.
  6858. var findConnectionToUseForDynamicAnchor = function (ep, elementWithPrecedence) {
  6859. var idx = 0;
  6860. if (elementWithPrecedence != null) {
  6861. for (var i = 0; i < ep.connections.length; i++) {
  6862. if (ep.connections[i].sourceId === elementWithPrecedence || ep.connections[i].targetId === elementWithPrecedence) {
  6863. idx = i;
  6864. break;
  6865. }
  6866. }
  6867. }
  6868. return ep.connections[idx];
  6869. };
  6870. _jp.Endpoint = function (params) {
  6871. var _jsPlumb = params._jsPlumb,
  6872. _newConnection = params.newConnection,
  6873. _newEndpoint = params.newEndpoint;
  6874. this.idPrefix = "_jsplumb_e_";
  6875. this.defaultLabelLocation = [ 0.5, 0.5 ];
  6876. this.defaultOverlayKeys = ["Overlays", "EndpointOverlays"];
  6877. _jp.OverlayCapableJsPlumbUIComponent.apply(this, arguments);
  6878. // TYPE
  6879. this.appendToDefaultType({
  6880. connectionType:params.connectionType,
  6881. maxConnections: params.maxConnections == null ? this._jsPlumb.instance.Defaults.MaxConnections : params.maxConnections, // maximum number of connections this endpoint can be the source of.,
  6882. paintStyle: params.endpointStyle || params.paintStyle || params.style || this._jsPlumb.instance.Defaults.EndpointStyle || _jp.Defaults.EndpointStyle,
  6883. hoverPaintStyle: params.endpointHoverStyle || params.hoverPaintStyle || this._jsPlumb.instance.Defaults.EndpointHoverStyle || _jp.Defaults.EndpointHoverStyle,
  6884. connectorStyle: params.connectorStyle,
  6885. connectorHoverStyle: params.connectorHoverStyle,
  6886. connectorClass: params.connectorClass,
  6887. connectorHoverClass: params.connectorHoverClass,
  6888. connectorOverlays: params.connectorOverlays,
  6889. connector: params.connector,
  6890. connectorTooltip: params.connectorTooltip
  6891. });
  6892. // END TYPE
  6893. this._jsPlumb.enabled = !(params.enabled === false);
  6894. this._jsPlumb.visible = true;
  6895. this.element = _jp.getElement(params.source);
  6896. this._jsPlumb.uuid = params.uuid;
  6897. this._jsPlumb.floatingEndpoint = null;
  6898. var inPlaceCopy = null;
  6899. if (this._jsPlumb.uuid) {
  6900. params.endpointsByUUID[this._jsPlumb.uuid] = this;
  6901. }
  6902. this.elementId = params.elementId;
  6903. this.dragProxy = params.dragProxy;
  6904. this._jsPlumb.connectionCost = params.connectionCost;
  6905. this._jsPlumb.connectionsDirected = params.connectionsDirected;
  6906. this._jsPlumb.currentAnchorClass = "";
  6907. this._jsPlumb.events = {};
  6908. var deleteOnEmpty = params.deleteOnEmpty === true;
  6909. this.setDeleteOnEmpty = function(d) {
  6910. deleteOnEmpty = d;
  6911. };
  6912. var _updateAnchorClass = function () {
  6913. // stash old, get new
  6914. var oldAnchorClass = _jsPlumb.endpointAnchorClassPrefix + "-" + this._jsPlumb.currentAnchorClass;
  6915. this._jsPlumb.currentAnchorClass = this.anchor.getCssClass();
  6916. var anchorClass = _jsPlumb.endpointAnchorClassPrefix + (this._jsPlumb.currentAnchorClass ? "-" + this._jsPlumb.currentAnchorClass : "");
  6917. this.removeClass(oldAnchorClass);
  6918. this.addClass(anchorClass);
  6919. // add and remove at the same time to reduce the number of reflows.
  6920. _jp.updateClasses(this.element, anchorClass, oldAnchorClass);
  6921. }.bind(this);
  6922. this.prepareAnchor = function(anchorParams) {
  6923. var a = this._jsPlumb.instance.makeAnchor(anchorParams, this.elementId, _jsPlumb);
  6924. a.bind("anchorChanged", function (currentAnchor) {
  6925. this.fire("anchorChanged", {endpoint: this, anchor: currentAnchor});
  6926. _updateAnchorClass();
  6927. }.bind(this));
  6928. return a;
  6929. };
  6930. this.setPreparedAnchor = function(anchor, doNotRepaint) {
  6931. this._jsPlumb.instance.continuousAnchorFactory.clear(this.elementId);
  6932. this.anchor = anchor;
  6933. _updateAnchorClass();
  6934. if (!doNotRepaint) {
  6935. this._jsPlumb.instance.repaint(this.elementId);
  6936. }
  6937. return this;
  6938. };
  6939. this.setAnchor = function (anchorParams, doNotRepaint) {
  6940. var a = this.prepareAnchor(anchorParams);
  6941. this.setPreparedAnchor(a, doNotRepaint);
  6942. return this;
  6943. };
  6944. var internalHover = function (state) {
  6945. if (this.connections.length > 0) {
  6946. for (var i = 0; i < this.connections.length; i++) {
  6947. this.connections[i].setHover(state, false);
  6948. }
  6949. }
  6950. else {
  6951. this.setHover(state);
  6952. }
  6953. }.bind(this);
  6954. this.bind("mouseover", function () {
  6955. internalHover(true);
  6956. });
  6957. this.bind("mouseout", function () {
  6958. internalHover(false);
  6959. });
  6960. // ANCHOR MANAGER
  6961. if (!params._transient) { // in place copies, for example, are transient. they will never need to be retrieved during a paint cycle, because they dont move, and then they are deleted.
  6962. this._jsPlumb.instance.router.addEndpoint(this, this.elementId);
  6963. }
  6964. this.prepareEndpoint = function(ep, typeId) {
  6965. var _e = function (t, p) {
  6966. var rm = _jsPlumb.getRenderMode();
  6967. if (_jp.Endpoints[rm][t]) {
  6968. return new _jp.Endpoints[rm][t](p);
  6969. }
  6970. if (!_jsPlumb.Defaults.DoNotThrowErrors) {
  6971. throw { msg: "jsPlumb: unknown endpoint type '" + t + "'" };
  6972. }
  6973. };
  6974. var endpointArgs = {
  6975. _jsPlumb: this._jsPlumb.instance,
  6976. cssClass: params.cssClass,
  6977. container: params.container,
  6978. tooltip: params.tooltip,
  6979. connectorTooltip: params.connectorTooltip,
  6980. endpoint: this
  6981. };
  6982. var endpoint;
  6983. if (_ju.isString(ep)) {
  6984. endpoint = _e(ep, endpointArgs);
  6985. }
  6986. else if (_ju.isArray(ep)) {
  6987. endpointArgs = _ju.merge(ep[1], endpointArgs);
  6988. endpoint = _e(ep[0], endpointArgs);
  6989. }
  6990. else {
  6991. endpoint = ep.clone();
  6992. }
  6993. // assign a clone function using a copy of endpointArgs. this is used when a drag starts: the endpoint that was dragged is cloned,
  6994. // and the clone is left in its place while the original one goes off on a magical journey.
  6995. // the copy is to get around a closure problem, in which endpointArgs ends up getting shared by
  6996. // the whole world.
  6997. //var argsForClone = jsPlumb.extend({}, endpointArgs);
  6998. endpoint.clone = function () {
  6999. // TODO this, and the code above, can be refactored to be more dry.
  7000. if (_ju.isString(ep)) {
  7001. return _e(ep, endpointArgs);
  7002. }
  7003. else if (_ju.isArray(ep)) {
  7004. endpointArgs = _ju.merge(ep[1], endpointArgs);
  7005. return _e(ep[0], endpointArgs);
  7006. }
  7007. }.bind(this);
  7008. endpoint.typeId = typeId;
  7009. return endpoint;
  7010. };
  7011. this.setEndpoint = function(ep, doNotRepaint) {
  7012. var _ep = this.prepareEndpoint(ep);
  7013. this.setPreparedEndpoint(_ep, true);
  7014. };
  7015. this.setPreparedEndpoint = function (ep, doNotRepaint) {
  7016. if (this.endpoint != null) {
  7017. this.endpoint.cleanup();
  7018. this.endpoint.destroy();
  7019. }
  7020. this.endpoint = ep;
  7021. this.type = this.endpoint.type;
  7022. this.canvas = this.endpoint.canvas;
  7023. };
  7024. _jp.extend(this, params, typeParameters);
  7025. this.isSource = params.isSource || false;
  7026. this.isTemporarySource = params.isTemporarySource || false;
  7027. this.isTarget = params.isTarget || false;
  7028. this.connections = params.connections || [];
  7029. this.connectorPointerEvents = params["connector-pointer-events"];
  7030. this.scope = params.scope || _jsPlumb.getDefaultScope();
  7031. this.timestamp = null;
  7032. this.reattachConnections = params.reattach || _jsPlumb.Defaults.ReattachConnections;
  7033. this.connectionsDetachable = _jsPlumb.Defaults.ConnectionsDetachable;
  7034. if (params.connectionsDetachable === false || params.detachable === false) {
  7035. this.connectionsDetachable = false;
  7036. }
  7037. this.dragAllowedWhenFull = params.dragAllowedWhenFull !== false;
  7038. if (params.onMaxConnections) {
  7039. this.bind("maxConnections", params.onMaxConnections);
  7040. }
  7041. //
  7042. // add a connection. not part of public API.
  7043. //
  7044. this.addConnection = function (connection) {
  7045. this.connections.push(connection);
  7046. this[(this.connections.length > 0 ? "add" : "remove") + "Class"](_jsPlumb.endpointConnectedClass);
  7047. this[(this.isFull() ? "add" : "remove") + "Class"](_jsPlumb.endpointFullClass);
  7048. };
  7049. this.detachFromConnection = function (connection, idx, doNotCleanup) {
  7050. idx = idx == null ? this.connections.indexOf(connection) : idx;
  7051. if (idx >= 0) {
  7052. this.connections.splice(idx, 1);
  7053. this[(this.connections.length > 0 ? "add" : "remove") + "Class"](_jsPlumb.endpointConnectedClass);
  7054. this[(this.isFull() ? "add" : "remove") + "Class"](_jsPlumb.endpointFullClass);
  7055. }
  7056. if (!doNotCleanup && deleteOnEmpty && this.connections.length === 0) {
  7057. _jsPlumb.deleteObject({
  7058. endpoint: this,
  7059. fireEvent: false,
  7060. deleteAttachedObjects: doNotCleanup !== true
  7061. });
  7062. }
  7063. };
  7064. this.deleteEveryConnection = function(params) {
  7065. var c = this.connections.length;
  7066. for (var i = 0; i < c; i++) {
  7067. _jsPlumb.deleteConnection(this.connections[0], params);
  7068. }
  7069. };
  7070. this.detachFrom = function (targetEndpoint, fireEvent, originalEvent) {
  7071. var c = [];
  7072. for (var i = 0; i < this.connections.length; i++) {
  7073. if (this.connections[i].endpoints[1] === targetEndpoint || this.connections[i].endpoints[0] === targetEndpoint) {
  7074. c.push(this.connections[i]);
  7075. }
  7076. }
  7077. for (var j = 0, count = c.length; j < count; j++) {
  7078. _jsPlumb.deleteConnection(c[0]);
  7079. }
  7080. return this;
  7081. };
  7082. this.getElement = function () {
  7083. return this.element;
  7084. };
  7085. this.setElement = function (el) {
  7086. var parentId = this._jsPlumb.instance.getId(el),
  7087. curId = this.elementId;
  7088. // remove the endpoint from the list for the current endpoint's element
  7089. _ju.removeWithFunction(params.endpointsByElement[this.elementId], function (e) {
  7090. return e.id === this.id;
  7091. }.bind(this));
  7092. this.element = _jp.getElement(el);
  7093. this.elementId = _jsPlumb.getId(this.element);
  7094. _jsPlumb.router.rehomeEndpoint(this, curId, this.element);
  7095. _jsPlumb.dragManager.endpointAdded(this.element);
  7096. _ju.addToList(params.endpointsByElement, parentId, this);
  7097. return this;
  7098. };
  7099. /**
  7100. * private but must be exposed.
  7101. */
  7102. this.makeInPlaceCopy = function () {
  7103. var loc = this.anchor.getCurrentLocation({element: this}),
  7104. o = this.anchor.getOrientation(this),
  7105. acc = this.anchor.getCssClass(),
  7106. inPlaceAnchor = {
  7107. bind: function () {
  7108. },
  7109. compute: function () {
  7110. return [ loc[0], loc[1] ];
  7111. },
  7112. getCurrentLocation: function () {
  7113. return [ loc[0], loc[1] ];
  7114. },
  7115. getOrientation: function () {
  7116. return o;
  7117. },
  7118. getCssClass: function () {
  7119. return acc;
  7120. }
  7121. };
  7122. return _newEndpoint({
  7123. dropOptions: params.dropOptions,
  7124. anchor: inPlaceAnchor,
  7125. source: this.element,
  7126. paintStyle: this.getPaintStyle(),
  7127. endpoint: params.hideOnDrag ? "Blank" : this.endpoint,
  7128. _transient: true,
  7129. scope: this.scope,
  7130. reference:this
  7131. });
  7132. };
  7133. /**
  7134. * returns a connection from the pool; used when dragging starts. just gets the head of the array if it can.
  7135. */
  7136. this.connectorSelector = function () {
  7137. return this.connections[0];
  7138. };
  7139. this.setStyle = this.setPaintStyle;
  7140. this.paint = function (params) {
  7141. params = params || {};
  7142. var timestamp = params.timestamp, recalc = !(params.recalc === false);
  7143. if (!timestamp || this.timestamp !== timestamp) {
  7144. var info = _jsPlumb.updateOffset({ elId: this.elementId, timestamp: timestamp });
  7145. var xy = params.offset ? params.offset.o : info.o;
  7146. if (xy != null) {
  7147. var ap = params.anchorPoint, connectorPaintStyle = params.connectorPaintStyle;
  7148. if (ap == null) {
  7149. var wh = params.dimensions || info.s,
  7150. anchorParams = { xy: [ xy.left, xy.top ], wh: wh, element: this, timestamp: timestamp };
  7151. if (recalc && this.anchor.isDynamic && this.connections.length > 0) {
  7152. var c = findConnectionToUseForDynamicAnchor(this, params.elementWithPrecedence),
  7153. oIdx = c.endpoints[0] === this ? 1 : 0,
  7154. oId = oIdx === 0 ? c.sourceId : c.targetId,
  7155. oInfo = _jsPlumb.getCachedData(oId),
  7156. oOffset = oInfo.o, oWH = oInfo.s;
  7157. anchorParams.index = oIdx === 0 ? 1 : 0;
  7158. anchorParams.connection = c;
  7159. anchorParams.txy = [ oOffset.left, oOffset.top ];
  7160. anchorParams.twh = oWH;
  7161. anchorParams.tElement = c.endpoints[oIdx];
  7162. anchorParams.tRotation = _jsPlumb.getRotation(oId);
  7163. } else if (this.connections.length > 0) {
  7164. anchorParams.connection = this.connections[0];
  7165. }
  7166. anchorParams.rotation = _jsPlumb.getRotation(this.elementId);
  7167. ap = this.anchor.compute(anchorParams);
  7168. }
  7169. this.endpoint.compute(ap, this.anchor.getOrientation(this), this._jsPlumb.paintStyleInUse, connectorPaintStyle || this.paintStyleInUse);
  7170. this.endpoint.paint(this._jsPlumb.paintStyleInUse, this.anchor);
  7171. this.timestamp = timestamp;
  7172. // paint overlays
  7173. for (var i in this._jsPlumb.overlays) {
  7174. if (this._jsPlumb.overlays.hasOwnProperty(i)) {
  7175. var o = this._jsPlumb.overlays[i];
  7176. if (o.isVisible()) {
  7177. this._jsPlumb.overlayPlacements[i] = o.draw(this.endpoint, this._jsPlumb.paintStyleInUse);
  7178. o.paint(this._jsPlumb.overlayPlacements[i]);
  7179. }
  7180. }
  7181. }
  7182. }
  7183. }
  7184. };
  7185. this.getTypeDescriptor = function () {
  7186. return "endpoint";
  7187. };
  7188. this.isVisible = function () {
  7189. return this._jsPlumb.visible;
  7190. };
  7191. this.repaint = this.paint;
  7192. var draggingInitialised = false;
  7193. this.initDraggable = function () {
  7194. // is this a connection source? we make it draggable and have the
  7195. // drag listener maintain a connection with a floating endpoint.
  7196. if (!draggingInitialised && _jp.isDragSupported(this.element)) {
  7197. var placeholderInfo = { id: null, element: null },
  7198. jpc = null,
  7199. existingJpc = false,
  7200. existingJpcParams = null,
  7201. _dragHandler = _makeConnectionDragHandler(this, placeholderInfo, _jsPlumb),
  7202. dragOptions = params.dragOptions || {},
  7203. defaultOpts = {},
  7204. startEvent = _jp.dragEvents.start,
  7205. stopEvent = _jp.dragEvents.stop,
  7206. dragEvent = _jp.dragEvents.drag,
  7207. beforeStartEvent = _jp.dragEvents.beforeStart,
  7208. payload;
  7209. // respond to beforeStart from katavorio; this will have, optionally, a payload of attribute values
  7210. // that were placed there by the makeSource mousedown listener.
  7211. var beforeStart = function(beforeStartParams) {
  7212. payload = beforeStartParams.e.payload || {};
  7213. };
  7214. var start = function (startParams) {
  7215. // ------------- first, get a connection to drag. this may be null, in which case we are dragging a new one.
  7216. jpc = this.connectorSelector();
  7217. // -------------------------------- now a bunch of tests about whether or not to proceed -------------------------
  7218. var _continue = true;
  7219. // if not enabled, return
  7220. if (!this.isEnabled()) {
  7221. _continue = false;
  7222. }
  7223. // if no connection and we're not a source - or temporarily a source, as is the case with makeSource - return.
  7224. if (jpc == null && !this.isSource && !this.isTemporarySource) {
  7225. _continue = false;
  7226. }
  7227. // otherwise if we're full and not allowed to drag, also return false.
  7228. if (this.isSource && this.isFull() && !(jpc != null && this.dragAllowedWhenFull)) {
  7229. _continue = false;
  7230. }
  7231. // if the connection was setup as not detachable or one of its endpoints
  7232. // was setup as connectionsDetachable = false, or Defaults.ConnectionsDetachable
  7233. // is set to false...
  7234. if (jpc != null && !jpc.isDetachable(this)) {
  7235. // .. and the endpoint is full
  7236. if (this.isFull()) {
  7237. _continue = false;
  7238. } else {
  7239. // otherwise, if not full, set the connection to null, and we will now proceed
  7240. // to drag a new connection.
  7241. jpc = null;
  7242. }
  7243. }
  7244. var beforeDrag = _jsPlumb.checkCondition(jpc == null ? "beforeDrag" : "beforeStartDetach", {
  7245. endpoint:this,
  7246. source:this.element,
  7247. sourceId:this.elementId,
  7248. connection:jpc
  7249. });
  7250. if (beforeDrag === false) {
  7251. _continue = false;
  7252. }
  7253. // else we might have been given some data. we'll pass it in to a new connection as 'data'.
  7254. // here we also merge in the optional payload we were given on mousedown.
  7255. else if (typeof beforeDrag === "object") {
  7256. _jp.extend(beforeDrag, payload || {});
  7257. }
  7258. else {
  7259. // or if no beforeDrag data, maybe use the payload on its own.
  7260. beforeDrag = payload || {};
  7261. }
  7262. if (_continue === false) {
  7263. // this is for mootools and yui. returning false from this causes jquery to stop drag.
  7264. // the events are wrapped in both mootools and yui anyway, but i don't think returning
  7265. // false from the start callback would stop a drag.
  7266. if (_jsPlumb.stopDrag) {
  7267. _jsPlumb.stopDrag(this.canvas);
  7268. }
  7269. _dragHandler.stopDrag();
  7270. return false;
  7271. }
  7272. // ---------------------------------------------------------------------------------------------------------------------
  7273. // ok to proceed.
  7274. // clear hover for all connections for this endpoint before continuing.
  7275. for (var i = 0; i < this.connections.length; i++) {
  7276. this.connections[i].setHover(false);
  7277. }
  7278. this.addClass("endpointDrag");
  7279. _jsPlumb.setConnectionBeingDragged(true);
  7280. // if we're not full but there was a connection, make it null. we'll create a new one.
  7281. if (jpc && !this.isFull() && this.isSource) {
  7282. jpc = null;
  7283. }
  7284. _jsPlumb.updateOffset({ elId: this.elementId });
  7285. // ---------------- make the element we will drag around, and position it -----------------------------
  7286. var ipco = this._jsPlumb.instance.getOffset(this.canvas),
  7287. canvasElement = this.canvas,
  7288. ips = this._jsPlumb.instance.getSize(this.canvas);
  7289. _makeDraggablePlaceholder(placeholderInfo, _jsPlumb, ipco, ips);
  7290. // store the id of the dragging div and the source element. the drop function will pick these up.
  7291. _jsPlumb.setAttributes(this.canvas, {
  7292. "dragId": placeholderInfo.id,
  7293. "elId": this.elementId
  7294. });
  7295. // ------------------- create an endpoint that will be our floating endpoint ------------------------------------
  7296. var endpointToFloat = this.dragProxy || this.endpoint;
  7297. if (this.dragProxy == null && this.connectionType != null) {
  7298. var aae = this._jsPlumb.instance.deriveEndpointAndAnchorSpec(this.connectionType);
  7299. if (aae.endpoints[1]) {
  7300. endpointToFloat = aae.endpoints[1];
  7301. }
  7302. }
  7303. var centerAnchor = this._jsPlumb.instance.makeAnchor("Center");
  7304. centerAnchor.isFloating = true;
  7305. this._jsPlumb.floatingEndpoint = _makeFloatingEndpoint(this.getPaintStyle(), centerAnchor, endpointToFloat, this.canvas, placeholderInfo.element, _jsPlumb, _newEndpoint, this.scope);
  7306. var _savedAnchor = this._jsPlumb.floatingEndpoint.anchor;
  7307. if (jpc == null) {
  7308. this.setHover(false, false);
  7309. // create a connection. one end is this endpoint, the other is a floating endpoint.
  7310. jpc = _newConnection({
  7311. sourceEndpoint: this,
  7312. targetEndpoint: this._jsPlumb.floatingEndpoint,
  7313. source: this.element, // for makeSource with parent option. ensure source element is represented correctly.
  7314. target: placeholderInfo.element,
  7315. anchors: [ this.anchor, this._jsPlumb.floatingEndpoint.anchor ],
  7316. paintStyle: params.connectorStyle, // this can be null. Connection will use the default.
  7317. hoverPaintStyle: params.connectorHoverStyle,
  7318. connector: params.connector, // this can also be null. Connection will use the default.
  7319. overlays: params.connectorOverlays,
  7320. type: this.connectionType,
  7321. cssClass: this.connectorClass,
  7322. hoverClass: this.connectorHoverClass,
  7323. scope:params.scope,
  7324. data:beforeDrag
  7325. });
  7326. jpc.pending = true;
  7327. jpc.addClass(_jsPlumb.draggingClass);
  7328. this._jsPlumb.floatingEndpoint.addClass(_jsPlumb.draggingClass);
  7329. this._jsPlumb.floatingEndpoint.anchor = _savedAnchor;
  7330. // fire an event that informs that a connection is being dragged
  7331. _jsPlumb.fire("connectionDrag", jpc);
  7332. // register the new connection on the drag manager. This connection, at this point, is 'pending',
  7333. // and has as its target a temporary element (the 'placeholder'). If the connection subsequently
  7334. // becomes established, the anchor manager is informed that the target of the connection has
  7335. // changed.
  7336. _jsPlumb.router.newConnection(jpc);
  7337. } else {
  7338. existingJpc = true;
  7339. jpc.setHover(false);
  7340. // new anchor idx
  7341. var anchorIdx = jpc.endpoints[0].id === this.id ? 0 : 1;
  7342. this.detachFromConnection(jpc, null, true); // detach from the connection while dragging is occurring. but dont cleanup automatically.
  7343. // store the original scope (issue 57)
  7344. var dragScope = _jsPlumb.getDragScope(canvasElement);
  7345. _jsPlumb.setAttribute(this.canvas, "originalScope", dragScope);
  7346. // fire an event that informs that a connection is being dragged. we do this before
  7347. // replacing the original target with the floating element info.
  7348. _jsPlumb.fire("connectionDrag", jpc);
  7349. // now we replace ourselves with the temporary div we created above:
  7350. if (anchorIdx === 0) {
  7351. existingJpcParams = [ jpc.source, jpc.sourceId, canvasElement, dragScope ];
  7352. _jsPlumb.router.sourceOrTargetChanged(jpc.endpoints[anchorIdx].elementId, placeholderInfo.id, jpc, placeholderInfo.element, 0);
  7353. } else {
  7354. existingJpcParams = [ jpc.target, jpc.targetId, canvasElement, dragScope ];
  7355. _jsPlumb.router.sourceOrTargetChanged(jpc.endpoints[anchorIdx].elementId, placeholderInfo.id, jpc, placeholderInfo.element, 1);
  7356. }
  7357. // store the original endpoint and assign the new floating endpoint for the drag.
  7358. jpc.suspendedEndpoint = jpc.endpoints[anchorIdx];
  7359. // PROVIDE THE SUSPENDED ELEMENT, BE IT A SOURCE OR TARGET (ISSUE 39)
  7360. jpc.suspendedElement = jpc.endpoints[anchorIdx].getElement();
  7361. jpc.suspendedElementId = jpc.endpoints[anchorIdx].elementId;
  7362. jpc.suspendedElementType = anchorIdx === 0 ? "source" : "target";
  7363. jpc.suspendedEndpoint.setHover(false);
  7364. this._jsPlumb.floatingEndpoint.referenceEndpoint = jpc.suspendedEndpoint;
  7365. jpc.endpoints[anchorIdx] = this._jsPlumb.floatingEndpoint;
  7366. jpc.addClass(_jsPlumb.draggingClass);
  7367. this._jsPlumb.floatingEndpoint.addClass(_jsPlumb.draggingClass);
  7368. }
  7369. _jsPlumb.registerFloatingConnection(placeholderInfo, jpc, this._jsPlumb.floatingEndpoint);
  7370. // tell jsplumb about it
  7371. _jsPlumb.currentlyDragging = true;
  7372. }.bind(this);
  7373. var stop = function () {
  7374. _jsPlumb.setConnectionBeingDragged(false);
  7375. if (jpc && jpc.endpoints != null) {
  7376. // get the actual drop event (decode from library args to stop function)
  7377. var originalEvent = _jsPlumb.getDropEvent(arguments);
  7378. // unlock the other endpoint (if it is dynamic, it would have been locked at drag start)
  7379. var idx = _jsPlumb.getFloatingAnchorIndex(jpc);
  7380. jpc.endpoints[idx === 0 ? 1 : 0].anchor.locked = false;
  7381. jpc.removeClass(_jsPlumb.draggingClass);
  7382. // if we have the floating endpoint then the connection has not been dropped
  7383. // on another endpoint. If it is a new connection we throw it away. If it is an
  7384. // existing connection we check to see if we should reattach it, throwing it away
  7385. // if not.
  7386. if (this._jsPlumb && (jpc.deleteConnectionNow || jpc.endpoints[idx] === this._jsPlumb.floatingEndpoint)) {
  7387. // 6a. if the connection was an existing one...
  7388. if (existingJpc && jpc.suspendedEndpoint) {
  7389. // fix for issue35, thanks Sylvain Gizard: when firing the detach event make sure the
  7390. // floating endpoint has been replaced.
  7391. if (idx === 0) {
  7392. jpc.floatingElement = jpc.source;
  7393. jpc.floatingId = jpc.sourceId;
  7394. jpc.floatingEndpoint = jpc.endpoints[0];
  7395. jpc.floatingIndex = 0;
  7396. jpc.source = existingJpcParams[0];
  7397. jpc.sourceId = existingJpcParams[1];
  7398. } else {
  7399. // keep a copy of the floating element; the anchor manager will want to clean up.
  7400. jpc.floatingElement = jpc.target;
  7401. jpc.floatingId = jpc.targetId;
  7402. jpc.floatingEndpoint = jpc.endpoints[1];
  7403. jpc.floatingIndex = 1;
  7404. jpc.target = existingJpcParams[0];
  7405. jpc.targetId = existingJpcParams[1];
  7406. }
  7407. var fe = this._jsPlumb.floatingEndpoint; // store for later removal.
  7408. // restore the original scope (issue 57)
  7409. _jsPlumb.setDragScope(existingJpcParams[2], existingJpcParams[3]);
  7410. jpc.endpoints[idx] = jpc.suspendedEndpoint;
  7411. // if the connection should be reattached, or the other endpoint refuses detach, then
  7412. // reset the connection to its original state
  7413. if (jpc.isReattach() || jpc._forceReattach || jpc._forceDetach || !_jsPlumb.deleteConnection(jpc, {originalEvent: originalEvent})) {
  7414. jpc.setHover(false);
  7415. jpc._forceDetach = null;
  7416. jpc._forceReattach = null;
  7417. this._jsPlumb.floatingEndpoint.detachFromConnection(jpc);
  7418. jpc.suspendedEndpoint.addConnection(jpc);
  7419. // TODO this code is duplicated in lots of places...and there is nothing external
  7420. // in the code; it all refers to the connection itself. we could add a
  7421. // `checkSanity(connection)` method to anchorManager that did this.
  7422. if (idx === 1) {
  7423. _jsPlumb.router.sourceOrTargetChanged(jpc.floatingId, jpc.targetId, jpc, jpc.target, idx);
  7424. }
  7425. else {
  7426. _jsPlumb.router.sourceOrTargetChanged(jpc.floatingId, jpc.sourceId, jpc, jpc.source, idx);
  7427. }
  7428. _jsPlumb.repaint(existingJpcParams[1]);
  7429. }
  7430. else {
  7431. _jsPlumb.deleteObject({endpoint: fe});
  7432. }
  7433. }
  7434. }
  7435. // makeTargets sets this flag, to tell us we have been replaced and should delete this object.
  7436. if (this.deleteAfterDragStop) {
  7437. _jsPlumb.deleteObject({endpoint: this});
  7438. }
  7439. else {
  7440. if (this._jsPlumb) {
  7441. this.paint({recalc: false});
  7442. }
  7443. }
  7444. // although the connection is no longer valid, there are use cases where this is useful.
  7445. _jsPlumb.fire("connectionDragStop", jpc, originalEvent);
  7446. // fire this event to give people more fine-grained control (connectionDragStop fires a lot)
  7447. if (jpc.pending) {
  7448. _jsPlumb.fire("connectionAborted", jpc, originalEvent);
  7449. }
  7450. // tell jsplumb that dragging is finished.
  7451. _jsPlumb.currentlyDragging = false;
  7452. jpc.suspendedElement = null;
  7453. jpc.suspendedEndpoint = null;
  7454. jpc = null;
  7455. }
  7456. // if no endpoints, jpc already cleaned up. but still we want to ensure we're reset properly.
  7457. // remove the element associated with the floating endpoint
  7458. // (and its associated floating endpoint and visual artefacts)
  7459. if (placeholderInfo && placeholderInfo.element) {
  7460. _jsPlumb.remove(placeholderInfo.element, false, false);
  7461. }
  7462. // remove the inplace copy
  7463. if (inPlaceCopy) {
  7464. _jsPlumb.deleteObject({endpoint: inPlaceCopy});
  7465. }
  7466. if (this._jsPlumb) {
  7467. // make our canvas visible (TODO: hand off to library; we should not know about DOM)
  7468. this.canvas.style.visibility = "visible";
  7469. // unlock our anchor
  7470. this.anchor.locked = false;
  7471. // clear floating anchor.
  7472. this._jsPlumb.floatingEndpoint = null;
  7473. }
  7474. }.bind(this);
  7475. dragOptions = _jp.extend(defaultOpts, dragOptions);
  7476. dragOptions.scope = this.scope || dragOptions.scope;
  7477. dragOptions[beforeStartEvent] = _ju.wrap(dragOptions[beforeStartEvent], beforeStart, false);
  7478. dragOptions[startEvent] = _ju.wrap(dragOptions[startEvent], start, false);
  7479. // extracted drag handler function so can be used by makeSource
  7480. dragOptions[dragEvent] = _ju.wrap(dragOptions[dragEvent], _dragHandler.drag);
  7481. dragOptions[stopEvent] = _ju.wrap(dragOptions[stopEvent], stop);
  7482. dragOptions.multipleDrop = false;
  7483. dragOptions.canDrag = function () {
  7484. return this.isSource || this.isTemporarySource || (this.connections.length > 0 && this.connectionsDetachable !== false);
  7485. }.bind(this);
  7486. _jsPlumb.initDraggable(this.canvas, dragOptions, "internal");
  7487. this.canvas._jsPlumbRelatedElement = this.element;
  7488. draggingInitialised = true;
  7489. }
  7490. };
  7491. var ep = params.endpoint || this._jsPlumb.instance.Defaults.Endpoint || _jp.Defaults.Endpoint;
  7492. this.setEndpoint(ep, true);
  7493. var anchorParamsToUse = params.anchor ? params.anchor : params.anchors ? params.anchors : (_jsPlumb.Defaults.Anchor || "Top");
  7494. this.setAnchor(anchorParamsToUse, true);
  7495. // finally, set type if it was provided
  7496. var type = [ "default", (params.type || "")].join(" ");
  7497. this.addType(type, params.data, true);
  7498. this.canvas = this.endpoint.canvas;
  7499. this.canvas._jsPlumb = this;
  7500. this.initDraggable();
  7501. // pulled this out into a function so we can reuse it for the inPlaceCopy canvas; you can now drop detached connections
  7502. // back onto the endpoint you detached it from.
  7503. var _initDropTarget = function (canvas, isTransient, endpoint, referenceEndpoint) {
  7504. if (_jp.isDropSupported(this.element)) {
  7505. var dropOptions = params.dropOptions || _jsPlumb.Defaults.DropOptions || _jp.Defaults.DropOptions;
  7506. dropOptions = _jp.extend({}, dropOptions);
  7507. dropOptions.scope = dropOptions.scope || this.scope;
  7508. var dropEvent = _jp.dragEvents.drop,
  7509. overEvent = _jp.dragEvents.over,
  7510. outEvent = _jp.dragEvents.out,
  7511. _ep = this,
  7512. drop = _jsPlumb.EndpointDropHandler({
  7513. getEndpoint: function () {
  7514. return _ep;
  7515. },
  7516. jsPlumb: _jsPlumb,
  7517. enabled: function () {
  7518. return endpoint != null ? endpoint.isEnabled() : true;
  7519. },
  7520. isFull: function () {
  7521. return endpoint.isFull();
  7522. },
  7523. element: this.element,
  7524. elementId: this.elementId,
  7525. isSource: this.isSource,
  7526. isTarget: this.isTarget,
  7527. addClass: function (clazz) {
  7528. _ep.addClass(clazz);
  7529. },
  7530. removeClass: function (clazz) {
  7531. _ep.removeClass(clazz);
  7532. },
  7533. isDropAllowed: function () {
  7534. return _ep.isDropAllowed.apply(_ep, arguments);
  7535. },
  7536. reference:referenceEndpoint,
  7537. isRedrop:function(jpc, dhParams) {
  7538. return jpc.suspendedEndpoint && dhParams.reference && (jpc.suspendedEndpoint.id === dhParams.reference.id);
  7539. }
  7540. });
  7541. dropOptions[dropEvent] = _ju.wrap(dropOptions[dropEvent], drop, true);
  7542. dropOptions[overEvent] = _ju.wrap(dropOptions[overEvent], function () {
  7543. var draggable = _jp.getDragObject(arguments),
  7544. id = _jsPlumb.getAttribute(_jp.getElement(draggable), "dragId"),
  7545. _jpc = _jsPlumb.getFloatingConnectionFor(id);//_jsPlumb.floatingConnections[id];
  7546. if (_jpc != null) {
  7547. var idx = _jsPlumb.getFloatingAnchorIndex(_jpc);
  7548. // here we should fire the 'over' event if we are a target and this is a new connection,
  7549. // or we are the same as the floating endpoint.
  7550. var _cont = (this.isTarget && idx !== 0) || (_jpc.suspendedEndpoint && this.referenceEndpoint && this.referenceEndpoint.id === _jpc.suspendedEndpoint.id);
  7551. if (_cont) {
  7552. var bb = _jsPlumb.checkCondition("checkDropAllowed", {
  7553. sourceEndpoint: _jpc.endpoints[idx],
  7554. targetEndpoint: this,
  7555. connection: _jpc
  7556. });
  7557. this[(bb ? "add" : "remove") + "Class"](_jsPlumb.endpointDropAllowedClass);
  7558. this[(bb ? "remove" : "add") + "Class"](_jsPlumb.endpointDropForbiddenClass);
  7559. _jpc.endpoints[idx].anchor.over(this.anchor, this);
  7560. }
  7561. }
  7562. }.bind(this));
  7563. dropOptions[outEvent] = _ju.wrap(dropOptions[outEvent], function () {
  7564. var draggable = _jp.getDragObject(arguments),
  7565. id = draggable == null ? null : _jsPlumb.getAttribute(_jp.getElement(draggable), "dragId"),
  7566. _jpc = id ? _jsPlumb.getFloatingConnectionFor(id) : null;
  7567. if (_jpc != null) {
  7568. var idx = _jsPlumb.getFloatingAnchorIndex(_jpc);
  7569. var _cont = (this.isTarget && idx !== 0) || (_jpc.suspendedEndpoint && this.referenceEndpoint && this.referenceEndpoint.id === _jpc.suspendedEndpoint.id);
  7570. if (_cont) {
  7571. this.removeClass(_jsPlumb.endpointDropAllowedClass);
  7572. this.removeClass(_jsPlumb.endpointDropForbiddenClass);
  7573. _jpc.endpoints[idx].anchor.out();
  7574. }
  7575. }
  7576. }.bind(this));
  7577. _jsPlumb.initDroppable(canvas, dropOptions, "internal", isTransient);
  7578. }
  7579. }.bind(this);
  7580. // Initialise the endpoint's canvas as a drop target. The drop handler will take care of the logic of whether
  7581. // something can actually be dropped.
  7582. if (!this.anchor.isFloating) {
  7583. _initDropTarget(this.canvas, !(params._transient || this.anchor.isFloating), this, params.reference);
  7584. }
  7585. return this;
  7586. };
  7587. _ju.extend(_jp.Endpoint, _jp.OverlayCapableJsPlumbUIComponent, {
  7588. setVisible: function (v, doNotChangeConnections, doNotNotifyOtherEndpoint) {
  7589. this._jsPlumb.visible = v;
  7590. if (this.canvas) {
  7591. this.canvas.style.display = v ? "block" : "none";
  7592. }
  7593. this[v ? "showOverlays" : "hideOverlays"]();
  7594. if (!doNotChangeConnections) {
  7595. for (var i = 0; i < this.connections.length; i++) {
  7596. this.connections[i].setVisible(v);
  7597. if (!doNotNotifyOtherEndpoint) {
  7598. var oIdx = this === this.connections[i].endpoints[0] ? 1 : 0;
  7599. // only change the other endpoint if this is its only connection.
  7600. if (this.connections[i].endpoints[oIdx].connections.length === 1) {
  7601. this.connections[i].endpoints[oIdx].setVisible(v, true, true);
  7602. }
  7603. }
  7604. }
  7605. }
  7606. },
  7607. getAttachedElements: function () {
  7608. return this.connections;
  7609. },
  7610. applyType: function (t, doNotRepaint) {
  7611. this.setPaintStyle(t.endpointStyle || t.paintStyle, doNotRepaint);
  7612. this.setHoverPaintStyle(t.endpointHoverStyle || t.hoverPaintStyle, doNotRepaint);
  7613. if (t.maxConnections != null) {
  7614. this._jsPlumb.maxConnections = t.maxConnections;
  7615. }
  7616. if (t.scope) {
  7617. this.scope = t.scope;
  7618. }
  7619. _jp.extend(this, t, typeParameters);
  7620. if (t.cssClass != null && this.canvas) {
  7621. this._jsPlumb.instance.addClass(this.canvas, t.cssClass);
  7622. }
  7623. _jp.OverlayCapableJsPlumbUIComponent.applyType(this, t);
  7624. },
  7625. isEnabled: function () {
  7626. return this._jsPlumb.enabled;
  7627. },
  7628. setEnabled: function (e) {
  7629. this._jsPlumb.enabled = e;
  7630. },
  7631. cleanup: function () {
  7632. var anchorClass = this._jsPlumb.instance.endpointAnchorClassPrefix + (this._jsPlumb.currentAnchorClass ? "-" + this._jsPlumb.currentAnchorClass : "");
  7633. _jp.removeClass(this.element, anchorClass);
  7634. this.anchor = null;
  7635. this.endpoint.cleanup(true);
  7636. this.endpoint.destroy();
  7637. this.endpoint = null;
  7638. // drag/drop
  7639. this._jsPlumb.instance.destroyDraggable(this.canvas, "internal");
  7640. this._jsPlumb.instance.destroyDroppable(this.canvas, "internal");
  7641. },
  7642. setHover: function (h) {
  7643. if (this.endpoint && this._jsPlumb && !this._jsPlumb.instance.isConnectionBeingDragged()) {
  7644. this.endpoint.setHover(h);
  7645. }
  7646. },
  7647. isFull: function () {
  7648. return this._jsPlumb.maxConnections === 0 ? true : !(this.isFloating() || this._jsPlumb.maxConnections < 0 || this.connections.length < this._jsPlumb.maxConnections);
  7649. },
  7650. /**
  7651. * private but needs to be exposed.
  7652. */
  7653. isFloating: function () {
  7654. return this.anchor != null && this.anchor.isFloating;
  7655. },
  7656. isConnectedTo: function (endpoint) {
  7657. var found = false;
  7658. if (endpoint) {
  7659. for (var i = 0; i < this.connections.length; i++) {
  7660. if (this.connections[i].endpoints[1] === endpoint || this.connections[i].endpoints[0] === endpoint) {
  7661. found = true;
  7662. break;
  7663. }
  7664. }
  7665. }
  7666. return found;
  7667. },
  7668. getConnectionCost: function () {
  7669. return this._jsPlumb.connectionCost;
  7670. },
  7671. setConnectionCost: function (c) {
  7672. this._jsPlumb.connectionCost = c;
  7673. },
  7674. areConnectionsDirected: function () {
  7675. return this._jsPlumb.connectionsDirected;
  7676. },
  7677. setConnectionsDirected: function (b) {
  7678. this._jsPlumb.connectionsDirected = b;
  7679. },
  7680. setElementId: function (_elId) {
  7681. this.elementId = _elId;
  7682. this.anchor.elementId = _elId;
  7683. },
  7684. setReferenceElement: function (_el) {
  7685. this.element = _jp.getElement(_el);
  7686. },
  7687. setDragAllowedWhenFull: function (allowed) {
  7688. this.dragAllowedWhenFull = allowed;
  7689. },
  7690. equals: function (endpoint) {
  7691. return this.anchor.equals(endpoint.anchor);
  7692. },
  7693. getUuid: function () {
  7694. return this._jsPlumb.uuid;
  7695. },
  7696. computeAnchor: function (params) {
  7697. return this.anchor.compute(params);
  7698. }
  7699. });
  7700. root.jsPlumbInstance.prototype.EndpointDropHandler = function (dhParams) {
  7701. return function (e) {
  7702. var _jsPlumb = dhParams.jsPlumb;
  7703. // remove the classes that are added dynamically. drop is neither forbidden nor allowed now that
  7704. // the drop is finishing.
  7705. dhParams.removeClass(_jsPlumb.endpointDropAllowedClass);
  7706. dhParams.removeClass(_jsPlumb.endpointDropForbiddenClass);
  7707. var originalEvent = _jsPlumb.getDropEvent(arguments),
  7708. draggable = _jsPlumb.getDragObject(arguments),
  7709. id = _jsPlumb.getAttribute(draggable, "dragId"),
  7710. elId = _jsPlumb.getAttribute(draggable, "elId"),
  7711. scope = _jsPlumb.getAttribute(draggable, "originalScope"),
  7712. jpc = _jsPlumb.getFloatingConnectionFor(id);
  7713. // if no active connection, bail.
  7714. if (jpc == null) {
  7715. return;
  7716. }
  7717. // calculate if this is an existing connection.
  7718. var existingConnection = jpc.suspendedEndpoint != null;
  7719. // if suspended endpoint exists but has been cleaned up, bail. This means it's an existing connection
  7720. // that has been detached and will shortly be discarded.
  7721. if (existingConnection && jpc.suspendedEndpoint._jsPlumb == null) {
  7722. return;
  7723. }
  7724. // get the drop endpoint. for a normal connection this is just the one that would replace the currently
  7725. // floating endpoint. for a makeTarget this is a new endpoint that is created on drop. But we leave that to
  7726. // the handler to figure out.
  7727. var _ep = dhParams.getEndpoint(jpc);
  7728. // If we're not given an endpoint to use, bail.
  7729. if (_ep == null) {
  7730. return;
  7731. }
  7732. // if this is a drop back where the connection came from, mark it force reattach and
  7733. // return; the stop handler will reattach. without firing an event.
  7734. if (dhParams.isRedrop(jpc, dhParams)) {
  7735. jpc._forceReattach = true;
  7736. jpc.setHover(false);
  7737. if (dhParams.maybeCleanup) {
  7738. dhParams.maybeCleanup(_ep);
  7739. }
  7740. return;
  7741. }
  7742. // ensure we dont bother trying to drop sources on non-source eps, and same for target.
  7743. var idx = _jsPlumb.getFloatingAnchorIndex(jpc);
  7744. if ((idx === 0 && !dhParams.isSource)|| (idx === 1 && !dhParams.isTarget)){
  7745. if (dhParams.maybeCleanup) {
  7746. dhParams.maybeCleanup(_ep);
  7747. }
  7748. return;
  7749. }
  7750. if (dhParams.onDrop) {
  7751. dhParams.onDrop(jpc);
  7752. }
  7753. // restore the original scope if necessary (issue 57)
  7754. if (scope) {
  7755. _jsPlumb.setDragScope(draggable, scope);
  7756. }
  7757. // if the target of the drop is full, fire an event (we abort below)
  7758. // makeTarget: keep.
  7759. var isFull = dhParams.isFull(e);
  7760. if (isFull) {
  7761. _ep.fire("maxConnections", {
  7762. endpoint: this,
  7763. connection: jpc,
  7764. maxConnections: _ep._jsPlumb.maxConnections
  7765. }, originalEvent);
  7766. }
  7767. //
  7768. // if endpoint enabled, not full, and matches the index of the floating endpoint...
  7769. if (!isFull && dhParams.enabled()) {
  7770. var _doContinue = true;
  7771. // before testing for beforeDrop, reset the connection's source/target to be the actual DOM elements
  7772. // involved (that is, stash any temporary stuff used for dragging. but we need to keep it around in
  7773. // order that the anchor manager can clean things up properly).
  7774. if (idx === 0) {
  7775. jpc.floatingElement = jpc.source;
  7776. jpc.floatingId = jpc.sourceId;
  7777. jpc.floatingEndpoint = jpc.endpoints[0];
  7778. jpc.floatingIndex = 0;
  7779. jpc.source = dhParams.element;
  7780. jpc.sourceId = _jsPlumb.getId(dhParams.element);
  7781. } else {
  7782. jpc.floatingElement = jpc.target;
  7783. jpc.floatingId = jpc.targetId;
  7784. jpc.floatingEndpoint = jpc.endpoints[1];
  7785. jpc.floatingIndex = 1;
  7786. jpc.target = dhParams.element;
  7787. jpc.targetId = _jsPlumb.getId(dhParams.element);
  7788. }
  7789. // if this is an existing connection and detach is not allowed we won't continue. The connection's
  7790. // endpoints have been reinstated; everything is back to how it was.
  7791. if (existingConnection && jpc.suspendedEndpoint.id !== _ep.id) {
  7792. if (!jpc.isDetachAllowed(jpc) || !jpc.endpoints[idx].isDetachAllowed(jpc) || !jpc.suspendedEndpoint.isDetachAllowed(jpc) || !_jsPlumb.checkCondition("beforeDetach", jpc)) {
  7793. _doContinue = false;
  7794. }
  7795. }
  7796. // ------------ wrap the execution path in a function so we can support asynchronous beforeDrop
  7797. var continueFunction = function (optionalData) {
  7798. // remove this jpc from the current endpoint, which is a floating endpoint that we will
  7799. // subsequently discard.
  7800. jpc.endpoints[idx].detachFromConnection(jpc);
  7801. // if there's a suspended endpoint, detach it from the connection.
  7802. if (jpc.suspendedEndpoint) {
  7803. jpc.suspendedEndpoint.detachFromConnection(jpc);
  7804. }
  7805. jpc.endpoints[idx] = _ep;
  7806. _ep.addConnection(jpc);
  7807. // copy our parameters in to the connection:
  7808. var params = _ep.getParameters();
  7809. for (var aParam in params) {
  7810. jpc.setParameter(aParam, params[aParam]);
  7811. }
  7812. if (!existingConnection) {
  7813. // if not an existing connection and
  7814. if (params.draggable) {
  7815. _jsPlumb.initDraggable(this.element, dhParams.dragOptions, "internal", _jsPlumb);
  7816. }
  7817. }
  7818. else {
  7819. var suspendedElementId = jpc.suspendedEndpoint.elementId;
  7820. _jsPlumb.fireMoveEvent({
  7821. index: idx,
  7822. originalSourceId: idx === 0 ? suspendedElementId : jpc.sourceId,
  7823. newSourceId: idx === 0 ? _ep.elementId : jpc.sourceId,
  7824. originalTargetId: idx === 1 ? suspendedElementId : jpc.targetId,
  7825. newTargetId: idx === 1 ? _ep.elementId : jpc.targetId,
  7826. originalSourceEndpoint: idx === 0 ? jpc.suspendedEndpoint : jpc.endpoints[0],
  7827. newSourceEndpoint: idx === 0 ? _ep : jpc.endpoints[0],
  7828. originalTargetEndpoint: idx === 1 ? jpc.suspendedEndpoint : jpc.endpoints[1],
  7829. newTargetEndpoint: idx === 1 ? _ep : jpc.endpoints[1],
  7830. connection: jpc
  7831. }, originalEvent);
  7832. }
  7833. if (idx === 1) {
  7834. _jsPlumb.router.sourceOrTargetChanged(jpc.floatingId, jpc.targetId, jpc, jpc.target, 1);
  7835. }
  7836. else {
  7837. _jsPlumb.router.sourceOrTargetChanged(jpc.floatingId, jpc.sourceId, jpc, jpc.source, 0);
  7838. }
  7839. // when makeSource has uniqueEndpoint:true, we want to create connections with new endpoints
  7840. // that are subsequently deleted. So makeSource sets `finalEndpoint`, which is the Endpoint to
  7841. // which the connection should be attached. The `detachFromConnection` call below results in the
  7842. // temporary endpoint being cleaned up.
  7843. if (jpc.endpoints[0].finalEndpoint) {
  7844. var _toDelete = jpc.endpoints[0];
  7845. _toDelete.detachFromConnection(jpc);
  7846. jpc.endpoints[0] = jpc.endpoints[0].finalEndpoint;
  7847. jpc.endpoints[0].addConnection(jpc);
  7848. }
  7849. // if optionalData was given, merge it onto the connection's data.
  7850. if (_ju.isObject(optionalData)) {
  7851. jpc.mergeData(optionalData);
  7852. }
  7853. // finalise will inform the anchor manager and also add to
  7854. // connectionsByScope if necessary.
  7855. _jsPlumb.finaliseConnection(jpc, null, originalEvent, false);
  7856. jpc.setHover(false);
  7857. // SP continuous anchor flush
  7858. _jsPlumb.revalidate(jpc.endpoints[0].element);
  7859. }.bind(this);
  7860. var dontContinueFunction = function () {
  7861. // otherwise just put it back on the endpoint it was on before the drag.
  7862. if (jpc.suspendedEndpoint) {
  7863. jpc.endpoints[idx] = jpc.suspendedEndpoint;
  7864. jpc.setHover(false);
  7865. jpc._forceDetach = true;
  7866. if (idx === 0) {
  7867. jpc.source = jpc.suspendedEndpoint.element;
  7868. jpc.sourceId = jpc.suspendedEndpoint.elementId;
  7869. } else {
  7870. jpc.target = jpc.suspendedEndpoint.element;
  7871. jpc.targetId = jpc.suspendedEndpoint.elementId;
  7872. }
  7873. jpc.suspendedEndpoint.addConnection(jpc);
  7874. // TODO checkSanity
  7875. if (idx === 1) {
  7876. _jsPlumb.router.sourceOrTargetChanged(jpc.floatingId, jpc.targetId, jpc, jpc.target, 1);
  7877. }
  7878. else {
  7879. _jsPlumb.router.sourceOrTargetChanged(jpc.floatingId, jpc.sourceId, jpc, jpc.source, 0);
  7880. }
  7881. _jsPlumb.repaint(jpc.sourceId);
  7882. jpc._forceDetach = false;
  7883. }
  7884. };
  7885. // --------------------------------------
  7886. // now check beforeDrop. this will be available only on Endpoints that are setup to
  7887. // have a beforeDrop condition (although, secretly, under the hood all Endpoints and
  7888. // the Connection have them, because they are on jsPlumbUIComponent. shhh!), because
  7889. // it only makes sense to have it on a target endpoint.
  7890. _doContinue = _doContinue && dhParams.isDropAllowed(jpc.sourceId, jpc.targetId, jpc.scope, jpc, _ep);// && jpc.pending;
  7891. if (_doContinue) {
  7892. continueFunction(_doContinue);
  7893. return true;
  7894. }
  7895. else {
  7896. dontContinueFunction();
  7897. }
  7898. }
  7899. if (dhParams.maybeCleanup) {
  7900. dhParams.maybeCleanup(_ep);
  7901. }
  7902. _jsPlumb.currentlyDragging = false;
  7903. };
  7904. };
  7905. }).call(typeof window !== 'undefined' ? window : this);
  7906. /*
  7907. * This file contains the code for Connections.
  7908. *
  7909. * Copyright (c) 2010 - 2020 jsPlumb (hello@jsplumbtoolkit.com)
  7910. *
  7911. * https://jsplumbtoolkit.com
  7912. * https://github.com/jsplumb/jsplumb
  7913. *
  7914. * Dual licensed under the MIT and GPL2 licenses.
  7915. */
  7916. ;
  7917. (function () {
  7918. "use strict";
  7919. var root = this,
  7920. _jp = root.jsPlumb,
  7921. _ju = root.jsPlumbUtil;
  7922. var makeConnector = function (_jsPlumb, renderMode, connectorName, connectorArgs, forComponent) {
  7923. // first make sure we have a cache for the specified renderer
  7924. _jp.Connectors[renderMode] = _jp.Connectors[renderMode] || {};
  7925. // now see if the one we want exists; if not we will try to make it
  7926. if (_jp.Connectors[renderMode][connectorName] == null) {
  7927. if (_jp.Connectors[connectorName] == null) {
  7928. if (!_jsPlumb.Defaults.DoNotThrowErrors) {
  7929. throw new TypeError("jsPlumb: unknown connector type '" + connectorName + "'");
  7930. } else {
  7931. return null;
  7932. }
  7933. }
  7934. _jp.Connectors[renderMode][connectorName] = function() {
  7935. _jp.Connectors[connectorName].apply(this, arguments);
  7936. _jp.ConnectorRenderers[renderMode].apply(this, arguments);
  7937. };
  7938. _ju.extend(_jp.Connectors[renderMode][connectorName], [ _jp.Connectors[connectorName], _jp.ConnectorRenderers[renderMode]]);
  7939. }
  7940. return new _jp.Connectors[renderMode][connectorName](connectorArgs, forComponent);
  7941. },
  7942. _makeAnchor = function (anchorParams, elementId, _jsPlumb) {
  7943. return (anchorParams) ? _jsPlumb.makeAnchor(anchorParams, elementId, _jsPlumb) : null;
  7944. },
  7945. _updateConnectedClass = function (conn, element, _jsPlumb, remove) {
  7946. if (element != null) {
  7947. element._jsPlumbConnections = element._jsPlumbConnections || {};
  7948. if (remove) {
  7949. delete element._jsPlumbConnections[conn.id];
  7950. }
  7951. else {
  7952. element._jsPlumbConnections[conn.id] = true;
  7953. }
  7954. if (_ju.isEmpty(element._jsPlumbConnections)) {
  7955. _jsPlumb.removeClass(element, _jsPlumb.connectedClass);
  7956. }
  7957. else {
  7958. _jsPlumb.addClass(element, _jsPlumb.connectedClass);
  7959. }
  7960. }
  7961. };
  7962. _jp.Connection = function (params) {
  7963. var _newEndpoint = params.newEndpoint;
  7964. this.id = params.id;
  7965. this.connector = null;
  7966. this.idPrefix = "_jsplumb_c_";
  7967. this.defaultLabelLocation = 0.5;
  7968. this.defaultOverlayKeys = ["Overlays", "ConnectionOverlays"];
  7969. // if a new connection is the result of moving some existing connection, params.previousConnection
  7970. // will have that Connection in it. listeners for the jsPlumbConnection event can look for that
  7971. // member and take action if they need to.
  7972. this.previousConnection = params.previousConnection;
  7973. this.source = _jp.getElement(params.source);
  7974. this.target = _jp.getElement(params.target);
  7975. _jp.OverlayCapableJsPlumbUIComponent.apply(this, arguments);
  7976. // sourceEndpoint and targetEndpoint override source/target, if they are present. but
  7977. // source is not overridden if the Endpoint has declared it is not the final target of a connection;
  7978. // instead we use the source that the Endpoint declares will be the final source element.
  7979. if (params.sourceEndpoint) {
  7980. this.source = params.sourceEndpoint.getElement();
  7981. this.sourceId = params.sourceEndpoint.elementId;
  7982. } else {
  7983. this.sourceId = this._jsPlumb.instance.getId(this.source);
  7984. }
  7985. if (params.targetEndpoint) {
  7986. this.target = params.targetEndpoint.getElement();
  7987. this.targetId = params.targetEndpoint.elementId;
  7988. } else {
  7989. this.targetId = this._jsPlumb.instance.getId(this.target);
  7990. }
  7991. this.scope = params.scope; // scope may have been passed in to the connect call. if it wasn't, we will pull it from the source endpoint, after having initialised the endpoints.
  7992. this.endpoints = [];
  7993. this.endpointStyles = [];
  7994. var _jsPlumb = this._jsPlumb.instance;
  7995. _jsPlumb.manage(this.sourceId, this.source);
  7996. _jsPlumb.manage(this.targetId, this.target);
  7997. this._jsPlumb.visible = true;
  7998. this._jsPlumb.params = {
  7999. cssClass: params.cssClass,
  8000. container: params.container,
  8001. "pointer-events": params["pointer-events"],
  8002. editorParams: params.editorParams,
  8003. overlays: params.overlays
  8004. };
  8005. this._jsPlumb.lastPaintedAt = null;
  8006. // listen to mouseover and mouseout events passed from the container delegate.
  8007. this.bind("mouseover", function () {
  8008. this.setHover(true);
  8009. }.bind(this));
  8010. this.bind("mouseout", function () {
  8011. this.setHover(false);
  8012. }.bind(this));
  8013. // INITIALISATION CODE
  8014. this.makeEndpoint = function (isSource, el, elId, ep, definition) {
  8015. elId = elId || this._jsPlumb.instance.getId(el);
  8016. return this.prepareEndpoint(_jsPlumb, _newEndpoint, this, ep, isSource ? 0 : 1, params, el, elId, definition);
  8017. };
  8018. // if type given, get the endpoint definitions mapping to that type from the jsplumb instance, and use those.
  8019. // we apply types at the end of this constructor but endpoints are only honoured in a type definition at
  8020. // create time.
  8021. if (params.type) {
  8022. params.endpoints = params.endpoints || this._jsPlumb.instance.deriveEndpointAndAnchorSpec(params.type).endpoints;
  8023. }
  8024. var eS = this.makeEndpoint(true, this.source, this.sourceId, params.sourceEndpoint),
  8025. eT = this.makeEndpoint(false, this.target, this.targetId, params.targetEndpoint);
  8026. if (eS) {
  8027. _ju.addToList(params.endpointsByElement, this.sourceId, eS);
  8028. }
  8029. if (eT) {
  8030. _ju.addToList(params.endpointsByElement, this.targetId, eT);
  8031. }
  8032. // if scope not set, set it to be the scope for the source endpoint.
  8033. if (!this.scope) {
  8034. this.scope = this.endpoints[0].scope;
  8035. }
  8036. // if explicitly told to (or not to) delete endpoints when empty, override endpoint's preferences
  8037. if (params.deleteEndpointsOnEmpty != null) {
  8038. this.endpoints[0].setDeleteOnEmpty(params.deleteEndpointsOnEmpty);
  8039. this.endpoints[1].setDeleteOnEmpty(params.deleteEndpointsOnEmpty);
  8040. }
  8041. // -------------------------- DEFAULT TYPE ---------------------------------------------
  8042. // DETACHABLE
  8043. var _detachable = _jsPlumb.Defaults.ConnectionsDetachable;
  8044. if (params.detachable === false) {
  8045. _detachable = false;
  8046. }
  8047. if (this.endpoints[0].connectionsDetachable === false) {
  8048. _detachable = false;
  8049. }
  8050. if (this.endpoints[1].connectionsDetachable === false) {
  8051. _detachable = false;
  8052. }
  8053. // REATTACH
  8054. var _reattach = params.reattach || this.endpoints[0].reattachConnections || this.endpoints[1].reattachConnections || _jsPlumb.Defaults.ReattachConnections;
  8055. this.appendToDefaultType({
  8056. detachable: _detachable,
  8057. reattach: _reattach,
  8058. paintStyle:this.endpoints[0].connectorStyle || this.endpoints[1].connectorStyle || params.paintStyle || _jsPlumb.Defaults.PaintStyle || _jp.Defaults.PaintStyle,
  8059. hoverPaintStyle:this.endpoints[0].connectorHoverStyle || this.endpoints[1].connectorHoverStyle || params.hoverPaintStyle || _jsPlumb.Defaults.HoverPaintStyle || _jp.Defaults.HoverPaintStyle
  8060. });
  8061. var _suspendedAt = _jsPlumb.getSuspendedAt();
  8062. if (!_jsPlumb.isSuspendDrawing()) {
  8063. // paint the endpoints
  8064. var myInfo = _jsPlumb.getCachedData(this.sourceId),
  8065. myOffset = myInfo.o, myWH = myInfo.s,
  8066. otherInfo = _jsPlumb.getCachedData(this.targetId),
  8067. otherOffset = otherInfo.o,
  8068. otherWH = otherInfo.s,
  8069. initialTimestamp = _suspendedAt || jsPlumbUtil.uuid(),
  8070. anchorLoc = this.endpoints[0].anchor.compute({
  8071. xy: [ myOffset.left, myOffset.top ], wh: myWH, element: this.endpoints[0],
  8072. elementId: this.endpoints[0].elementId,
  8073. txy: [ otherOffset.left, otherOffset.top ], twh: otherWH, tElement: this.endpoints[1],
  8074. timestamp: initialTimestamp,
  8075. rotation:_jsPlumb.getRotation(this.endpoints[0].elementId)
  8076. });
  8077. this.endpoints[0].paint({ anchorLoc: anchorLoc, timestamp: initialTimestamp });
  8078. anchorLoc = this.endpoints[1].anchor.compute({
  8079. xy: [ otherOffset.left, otherOffset.top ], wh: otherWH, element: this.endpoints[1],
  8080. elementId: this.endpoints[1].elementId,
  8081. txy: [ myOffset.left, myOffset.top ], twh: myWH, tElement: this.endpoints[0],
  8082. timestamp: initialTimestamp,
  8083. rotation:_jsPlumb.getRotation(this.endpoints[1].elementId)
  8084. });
  8085. this.endpoints[1].paint({ anchorLoc: anchorLoc, timestamp: initialTimestamp });
  8086. }
  8087. this.getTypeDescriptor = function () {
  8088. return "connection";
  8089. };
  8090. this.getAttachedElements = function () {
  8091. return this.endpoints;
  8092. };
  8093. this.isDetachable = function (ep) {
  8094. return this._jsPlumb.detachable === false ? false : ep != null ? ep.connectionsDetachable === true : this._jsPlumb.detachable === true;
  8095. };
  8096. this.setDetachable = function (detachable) {
  8097. this._jsPlumb.detachable = detachable === true;
  8098. };
  8099. this.isReattach = function () {
  8100. return this._jsPlumb.reattach === true || this.endpoints[0].reattachConnections === true || this.endpoints[1].reattachConnections === true;
  8101. };
  8102. this.setReattach = function (reattach) {
  8103. this._jsPlumb.reattach = reattach === true;
  8104. };
  8105. // END INITIALISATION CODE
  8106. // COST + DIRECTIONALITY
  8107. // if cost not supplied, try to inherit from source endpoint
  8108. this._jsPlumb.cost = params.cost || this.endpoints[0].getConnectionCost();
  8109. this._jsPlumb.directed = params.directed;
  8110. // inherit directed flag if set no source endpoint
  8111. if (params.directed == null) {
  8112. this._jsPlumb.directed = this.endpoints[0].areConnectionsDirected();
  8113. }
  8114. // END COST + DIRECTIONALITY
  8115. // PARAMETERS
  8116. // merge all the parameters objects into the connection. parameters set
  8117. // on the connection take precedence; then source endpoint params, then
  8118. // finally target endpoint params.
  8119. var _p = _jp.extend({}, this.endpoints[1].getParameters());
  8120. _jp.extend(_p, this.endpoints[0].getParameters());
  8121. _jp.extend(_p, this.getParameters());
  8122. this.setParameters(_p);
  8123. // END PARAMETERS
  8124. // PAINTING
  8125. this.setConnector(this.endpoints[0].connector || this.endpoints[1].connector || params.connector || _jsPlumb.Defaults.Connector || _jp.Defaults.Connector, true);
  8126. var data = params.data == null || !_ju.isObject(params.data) ? {} : params.data;
  8127. this.getData = function() { return data; };
  8128. this.setData = function(d) { data = d || {}; };
  8129. this.mergeData = function(d) { data = _jp.extend(data, d); };
  8130. // the very last thing we do is apply types, if there are any.
  8131. var _types = [ "default", this.endpoints[0].connectionType, this.endpoints[1].connectionType, params.type ].join(" ");
  8132. if (/[^\s]/.test(_types)) {
  8133. this.addType(_types, params.data, true);
  8134. }
  8135. this.updateConnectedClass();
  8136. // END PAINTING
  8137. };
  8138. _ju.extend(_jp.Connection, _jp.OverlayCapableJsPlumbUIComponent, {
  8139. applyType: function (t, doNotRepaint, typeMap) {
  8140. var _connector = null;
  8141. if (t.connector != null) {
  8142. _connector = this.getCachedTypeItem("connector", typeMap.connector);
  8143. if (_connector == null) {
  8144. _connector = this.prepareConnector(t.connector, typeMap.connector);
  8145. this.cacheTypeItem("connector", _connector, typeMap.connector);
  8146. }
  8147. this.setPreparedConnector(_connector);
  8148. }
  8149. // none of these things result in the creation of objects so can be ignored.
  8150. if (t.detachable != null) {
  8151. this.setDetachable(t.detachable);
  8152. }
  8153. if (t.reattach != null) {
  8154. this.setReattach(t.reattach);
  8155. }
  8156. if (t.scope) {
  8157. this.scope = t.scope;
  8158. }
  8159. if (t.cssClass != null && this.canvas) {
  8160. this._jsPlumb.instance.addClass(this.canvas, t.cssClass);
  8161. }
  8162. var _anchors = null;
  8163. // this also results in the creation of objects.
  8164. if (t.anchor) {
  8165. // note that even if the param was anchor, we store `anchors`.
  8166. _anchors = this.getCachedTypeItem("anchors", typeMap.anchor);
  8167. if (_anchors == null) {
  8168. _anchors = [ this._jsPlumb.instance.makeAnchor(t.anchor), this._jsPlumb.instance.makeAnchor(t.anchor) ];
  8169. this.cacheTypeItem("anchors", _anchors, typeMap.anchor);
  8170. }
  8171. }
  8172. else if (t.anchors) {
  8173. _anchors = this.getCachedTypeItem("anchors", typeMap.anchors);
  8174. if (_anchors == null) {
  8175. _anchors = [
  8176. this._jsPlumb.instance.makeAnchor(t.anchors[0]),
  8177. this._jsPlumb.instance.makeAnchor(t.anchors[1])
  8178. ];
  8179. this.cacheTypeItem("anchors", _anchors, typeMap.anchors);
  8180. }
  8181. }
  8182. if (_anchors != null) {
  8183. this.endpoints[0].anchor = _anchors[0];
  8184. this.endpoints[1].anchor = _anchors[1];
  8185. if (this.endpoints[1].anchor.isDynamic) {
  8186. this._jsPlumb.instance.repaint(this.endpoints[1].elementId);
  8187. }
  8188. }
  8189. _jp.OverlayCapableJsPlumbUIComponent.applyType(this, t);
  8190. },
  8191. addClass: function (c, informEndpoints) {
  8192. if (informEndpoints) {
  8193. this.endpoints[0].addClass(c);
  8194. this.endpoints[1].addClass(c);
  8195. if (this.suspendedEndpoint) {
  8196. this.suspendedEndpoint.addClass(c);
  8197. }
  8198. }
  8199. if (this.connector) {
  8200. this.connector.addClass(c);
  8201. }
  8202. },
  8203. removeClass: function (c, informEndpoints) {
  8204. if (informEndpoints) {
  8205. this.endpoints[0].removeClass(c);
  8206. this.endpoints[1].removeClass(c);
  8207. if (this.suspendedEndpoint) {
  8208. this.suspendedEndpoint.removeClass(c);
  8209. }
  8210. }
  8211. if (this.connector) {
  8212. this.connector.removeClass(c);
  8213. }
  8214. },
  8215. isVisible: function () {
  8216. return this._jsPlumb.visible;
  8217. },
  8218. setVisible: function (v) {
  8219. this._jsPlumb.visible = v;
  8220. if (this.connector) {
  8221. this.connector.setVisible(v);
  8222. }
  8223. this.repaint();
  8224. },
  8225. cleanup: function () {
  8226. this.updateConnectedClass(true);
  8227. this.endpoints = null;
  8228. this.source = null;
  8229. this.target = null;
  8230. if (this.connector != null) {
  8231. this.connector.cleanup(true);
  8232. this.connector.destroy(true);
  8233. }
  8234. this.connector = null;
  8235. },
  8236. updateConnectedClass:function(remove) {
  8237. if (this._jsPlumb) {
  8238. _updateConnectedClass(this, this.source, this._jsPlumb.instance, remove);
  8239. _updateConnectedClass(this, this.target, this._jsPlumb.instance, remove);
  8240. }
  8241. },
  8242. setHover: function (state) {
  8243. if (this.connector && this._jsPlumb && !this._jsPlumb.instance.isConnectionBeingDragged()) {
  8244. this.connector.setHover(state);
  8245. root.jsPlumb[state ? "addClass" : "removeClass"](this.source, this._jsPlumb.instance.hoverSourceClass);
  8246. root.jsPlumb[state ? "addClass" : "removeClass"](this.target, this._jsPlumb.instance.hoverTargetClass);
  8247. }
  8248. },
  8249. getUuids:function() {
  8250. return [ this.endpoints[0].getUuid(), this.endpoints[1].getUuid() ];
  8251. },
  8252. getCost: function () {
  8253. return this._jsPlumb ? this._jsPlumb.cost : -Infinity;
  8254. },
  8255. setCost: function (c) {
  8256. this._jsPlumb.cost = c;
  8257. },
  8258. isDirected: function () {
  8259. return this._jsPlumb.directed;
  8260. },
  8261. getConnector: function () {
  8262. return this.connector;
  8263. },
  8264. prepareConnector:function(connectorSpec, typeId) {
  8265. var connectorArgs = {
  8266. _jsPlumb: this._jsPlumb.instance,
  8267. cssClass: this._jsPlumb.params.cssClass,
  8268. container: this._jsPlumb.params.container,
  8269. "pointer-events": this._jsPlumb.params["pointer-events"]
  8270. },
  8271. renderMode = this._jsPlumb.instance.getRenderMode(),
  8272. connector;
  8273. if (_ju.isString(connectorSpec)) {
  8274. connector = makeConnector(this._jsPlumb.instance, renderMode, connectorSpec, connectorArgs, this);
  8275. } // lets you use a string as shorthand.
  8276. else if (_ju.isArray(connectorSpec)) {
  8277. if (connectorSpec.length === 1) {
  8278. connector = makeConnector(this._jsPlumb.instance, renderMode, connectorSpec[0], connectorArgs, this);
  8279. }
  8280. else {
  8281. connector = makeConnector(this._jsPlumb.instance, renderMode, connectorSpec[0], _ju.merge(connectorSpec[1], connectorArgs), this);
  8282. }
  8283. }
  8284. if (typeId != null) {
  8285. connector.typeId = typeId;
  8286. }
  8287. return connector;
  8288. },
  8289. setPreparedConnector: function(connector, doNotRepaint, doNotChangeListenerComponent, typeId) {
  8290. if (this.connector !== connector) {
  8291. var previous, previousClasses = "";
  8292. // the connector will not be cleaned up if it was set as part of a type, because `typeId` will be set on it
  8293. // and we havent passed in `true` for "force" here.
  8294. if (this.connector != null) {
  8295. previous = this.connector;
  8296. previousClasses = previous.getClass();
  8297. this.connector.cleanup();
  8298. this.connector.destroy();
  8299. }
  8300. this.connector = connector;
  8301. if (typeId) {
  8302. this.cacheTypeItem("connector", connector, typeId);
  8303. }
  8304. this.canvas = this.connector.canvas;
  8305. this.bgCanvas = this.connector.bgCanvas;
  8306. this.connector.reattach(this._jsPlumb.instance);
  8307. // put classes from prior connector onto the canvas
  8308. this.addClass(previousClasses);
  8309. // new: instead of binding listeners per connector, we now just have one delegate on the container.
  8310. // so for that handler we set the connection as the '_jsPlumb' member of the canvas element, and
  8311. // bgCanvas, if it exists, which it does right now in the VML renderer, so it won't from v 2.0.0 onwards.
  8312. if (this.canvas) {
  8313. this.canvas._jsPlumb = this;
  8314. }
  8315. if (this.bgCanvas) {
  8316. this.bgCanvas._jsPlumb = this;
  8317. }
  8318. if (previous != null) {
  8319. var o = this.getOverlays();
  8320. for (var i = 0; i < o.length; i++) {
  8321. if (o[i].transfer) {
  8322. o[i].transfer(this.connector);
  8323. }
  8324. }
  8325. }
  8326. if (!doNotChangeListenerComponent) {
  8327. this.setListenerComponent(this.connector);
  8328. }
  8329. if (!doNotRepaint) {
  8330. this.repaint();
  8331. }
  8332. }
  8333. },
  8334. setConnector: function (connectorSpec, doNotRepaint, doNotChangeListenerComponent, typeId) {
  8335. var connector = this.prepareConnector(connectorSpec, typeId);
  8336. this.setPreparedConnector(connector, doNotRepaint, doNotChangeListenerComponent, typeId);
  8337. },
  8338. paint: function (params) {
  8339. if (!this._jsPlumb.instance.isSuspendDrawing() && this._jsPlumb.visible) {
  8340. params = params || {};
  8341. var timestamp = params.timestamp,
  8342. // if the moving object is not the source we must transpose the two references.
  8343. swap = false,
  8344. tId = swap ? this.sourceId : this.targetId, sId = swap ? this.targetId : this.sourceId,
  8345. tIdx = swap ? 0 : 1, sIdx = swap ? 1 : 0;
  8346. if (timestamp == null || timestamp !== this._jsPlumb.lastPaintedAt) {
  8347. var sourceInfo = this._jsPlumb.instance.updateOffset({elId:sId}).o,
  8348. targetInfo = this._jsPlumb.instance.updateOffset({elId:tId}).o,
  8349. sE = this.endpoints[sIdx], tE = this.endpoints[tIdx];
  8350. var sAnchorP = sE.anchor.getCurrentLocation(
  8351. {
  8352. xy: [sourceInfo.left, sourceInfo.top],
  8353. wh: [sourceInfo.width, sourceInfo.height],
  8354. element: sE,
  8355. timestamp: timestamp,
  8356. rotation:this._jsPlumb.instance.getRotation(this.sourceId)
  8357. }),
  8358. tAnchorP = tE.anchor.getCurrentLocation({
  8359. xy: [targetInfo.left, targetInfo.top],
  8360. wh: [targetInfo.width, targetInfo.height],
  8361. element: tE,
  8362. timestamp: timestamp,
  8363. rotation:this._jsPlumb.instance.getRotation(this.targetId)
  8364. });
  8365. this.connector.resetBounds();
  8366. this.connector.compute({
  8367. sourcePos: sAnchorP,
  8368. targetPos: tAnchorP,
  8369. sourceOrientation:sE.anchor.getOrientation(sE),
  8370. targetOrientation:tE.anchor.getOrientation(tE),
  8371. sourceEndpoint: this.endpoints[sIdx],
  8372. targetEndpoint: this.endpoints[tIdx],
  8373. "stroke-width": this._jsPlumb.paintStyleInUse.strokeWidth,
  8374. sourceInfo: sourceInfo,
  8375. targetInfo: targetInfo
  8376. });
  8377. var overlayExtents = { minX: Infinity, minY: Infinity, maxX: -Infinity, maxY: -Infinity };
  8378. // compute overlays. we do this first so we can get their placements, and adjust the
  8379. // container if needs be (if an overlay would be clipped)
  8380. for (var i in this._jsPlumb.overlays) {
  8381. if (this._jsPlumb.overlays.hasOwnProperty(i)) {
  8382. var o = this._jsPlumb.overlays[i];
  8383. if (o.isVisible()) {
  8384. this._jsPlumb.overlayPlacements[i] = o.draw(this.connector, this._jsPlumb.paintStyleInUse, this.getAbsoluteOverlayPosition(o));
  8385. overlayExtents.minX = Math.min(overlayExtents.minX, this._jsPlumb.overlayPlacements[i].minX);
  8386. overlayExtents.maxX = Math.max(overlayExtents.maxX, this._jsPlumb.overlayPlacements[i].maxX);
  8387. overlayExtents.minY = Math.min(overlayExtents.minY, this._jsPlumb.overlayPlacements[i].minY);
  8388. overlayExtents.maxY = Math.max(overlayExtents.maxY, this._jsPlumb.overlayPlacements[i].maxY);
  8389. }
  8390. }
  8391. }
  8392. var lineWidth = parseFloat(this._jsPlumb.paintStyleInUse.strokeWidth || 1) / 2,
  8393. outlineWidth = parseFloat(this._jsPlumb.paintStyleInUse.strokeWidth || 0),
  8394. extents = {
  8395. xmin: Math.min(this.connector.bounds.minX - (lineWidth + outlineWidth), overlayExtents.minX),
  8396. ymin: Math.min(this.connector.bounds.minY - (lineWidth + outlineWidth), overlayExtents.minY),
  8397. xmax: Math.max(this.connector.bounds.maxX + (lineWidth + outlineWidth), overlayExtents.maxX),
  8398. ymax: Math.max(this.connector.bounds.maxY + (lineWidth + outlineWidth), overlayExtents.maxY)
  8399. };
  8400. // paint the connector.
  8401. this.connector.paintExtents = extents;
  8402. this.connector.paint(this._jsPlumb.paintStyleInUse, null, extents);
  8403. // and then the overlays
  8404. for (var j in this._jsPlumb.overlays) {
  8405. if (this._jsPlumb.overlays.hasOwnProperty(j)) {
  8406. var p = this._jsPlumb.overlays[j];
  8407. if (p.isVisible()) {
  8408. p.paint(this._jsPlumb.overlayPlacements[j], extents);
  8409. }
  8410. }
  8411. }
  8412. }
  8413. this._jsPlumb.lastPaintedAt = timestamp;
  8414. }
  8415. },
  8416. repaint: function (params) {
  8417. var p = jsPlumb.extend(params || {}, {});
  8418. p.elId = this.sourceId;
  8419. this.paint(p);
  8420. },
  8421. prepareEndpoint: function (_jsPlumb, _newEndpoint, conn, existing, index, params, element, elementId, definition) {
  8422. var e;
  8423. if (existing) {
  8424. conn.endpoints[index] = existing;
  8425. existing.addConnection(conn);
  8426. } else {
  8427. if (!params.endpoints) {
  8428. params.endpoints = [ null, null ];
  8429. }
  8430. var ep = definition || params.endpoints[index] || params.endpoint || _jsPlumb.Defaults.Endpoints[index] || _jp.Defaults.Endpoints[index] || _jsPlumb.Defaults.Endpoint || _jp.Defaults.Endpoint;
  8431. if (!params.endpointStyles) {
  8432. params.endpointStyles = [ null, null ];
  8433. }
  8434. if (!params.endpointHoverStyles) {
  8435. params.endpointHoverStyles = [ null, null ];
  8436. }
  8437. var es = params.endpointStyles[index] || params.endpointStyle || _jsPlumb.Defaults.EndpointStyles[index] || _jp.Defaults.EndpointStyles[index] || _jsPlumb.Defaults.EndpointStyle || _jp.Defaults.EndpointStyle;
  8438. // Endpoints derive their fill from the connector's stroke, if no fill was specified.
  8439. if (es.fill == null && params.paintStyle != null) {
  8440. es.fill = params.paintStyle.stroke;
  8441. }
  8442. if (es.outlineStroke == null && params.paintStyle != null) {
  8443. es.outlineStroke = params.paintStyle.outlineStroke;
  8444. }
  8445. if (es.outlineWidth == null && params.paintStyle != null) {
  8446. es.outlineWidth = params.paintStyle.outlineWidth;
  8447. }
  8448. var ehs = params.endpointHoverStyles[index] || params.endpointHoverStyle || _jsPlumb.Defaults.EndpointHoverStyles[index] || _jp.Defaults.EndpointHoverStyles[index] || _jsPlumb.Defaults.EndpointHoverStyle || _jp.Defaults.EndpointHoverStyle;
  8449. // endpoint hover fill style is derived from connector's hover stroke style
  8450. if (params.hoverPaintStyle != null) {
  8451. if (ehs == null) {
  8452. ehs = {};
  8453. }
  8454. if (ehs.fill == null) {
  8455. ehs.fill = params.hoverPaintStyle.stroke;
  8456. }
  8457. }
  8458. var a = params.anchors ? params.anchors[index] :
  8459. params.anchor ? params.anchor :
  8460. _makeAnchor(_jsPlumb.Defaults.Anchors[index], elementId, _jsPlumb) ||
  8461. _makeAnchor(_jp.Defaults.Anchors[index], elementId, _jsPlumb) ||
  8462. _makeAnchor(_jsPlumb.Defaults.Anchor, elementId, _jsPlumb) ||
  8463. _makeAnchor(_jp.Defaults.Anchor, elementId, _jsPlumb),
  8464. u = params.uuids ? params.uuids[index] : null;
  8465. e = _newEndpoint({
  8466. paintStyle: es, hoverPaintStyle: ehs, endpoint: ep, connections: [ conn ],
  8467. uuid: u, anchor: a, source: element, scope: params.scope,
  8468. reattach: params.reattach || _jsPlumb.Defaults.ReattachConnections,
  8469. detachable: params.detachable || _jsPlumb.Defaults.ConnectionsDetachable
  8470. });
  8471. if (existing == null) {
  8472. e.setDeleteOnEmpty(true);
  8473. }
  8474. conn.endpoints[index] = e;
  8475. if (params.drawEndpoints === false) {
  8476. e.setVisible(false, true, true);
  8477. }
  8478. }
  8479. return e;
  8480. },
  8481. replaceEndpoint:function(idx, endpointDef) {
  8482. var current = this.endpoints[idx],
  8483. elId = current.elementId,
  8484. ebe = this._jsPlumb.instance.getEndpoints(elId),
  8485. _idx = ebe.indexOf(current),
  8486. _new = this.makeEndpoint(idx === 0, current.element, elId, null, endpointDef);
  8487. this.endpoints[idx] = _new;
  8488. ebe.splice(_idx, 1, _new);
  8489. this._jsPlumb.instance.deleteObject({endpoint:current, deleteAttachedObjects:false});
  8490. this._jsPlumb.instance.fire("endpointReplaced", {previous:current, current:_new});
  8491. this._jsPlumb.instance.router.sourceOrTargetChanged(this.endpoints[1].elementId, this.endpoints[1].elementId, this, this.endpoints[1].element, 1);
  8492. }
  8493. }); // END Connection class
  8494. }).call(typeof window !== 'undefined' ? window : this);
  8495. /*
  8496. * This file contains the code for creating and manipulating anchors.
  8497. *
  8498. * Copyright (c) 2010 - 2020 jsPlumb (hello@jsplumbtoolkit.com)
  8499. *
  8500. * https://jsplumbtoolkit.com
  8501. * https://github.com/jsplumb/jsplumb
  8502. *
  8503. * Dual licensed under the MIT and GPL2 licenses.
  8504. */
  8505. ;
  8506. (function () {
  8507. "use strict";
  8508. var root = this,
  8509. _ju = root.jsPlumbUtil,
  8510. _jp = root.jsPlumb;
  8511. //
  8512. // manages anchors for all elements.
  8513. //
  8514. _jp.AnchorManager = function (params) {
  8515. var _amEndpoints = {},
  8516. continuousAnchorLocations = {},
  8517. continuousAnchorOrientations = {},
  8518. connectionsByElementId = {},
  8519. self = this,
  8520. anchorLists = {},
  8521. jsPlumbInstance = params.jsPlumbInstance,
  8522. floatingConnections = {},
  8523. // used by placeAnchors function
  8524. placeAnchorsOnLine = function (desc, elementDimensions, elementPosition, connections, horizontal, otherMultiplier, reverse, rotation) {
  8525. var a = [], step = elementDimensions[horizontal ? 0 : 1] / (connections.length + 1);
  8526. for (var i = 0; i < connections.length; i++) {
  8527. var val = (i + 1) * step, other = otherMultiplier * elementDimensions[horizontal ? 1 : 0];
  8528. if (reverse) {
  8529. val = elementDimensions[horizontal ? 0 : 1] - val;
  8530. }
  8531. var dx = (horizontal ? val : other), x = elementPosition.left + dx, xp = dx / elementDimensions[0],
  8532. dy = (horizontal ? other : val), y = elementPosition.top + dy, yp = dy / elementDimensions[1];
  8533. if (rotation !== 0) {
  8534. var rotated = jsPlumbUtil.rotatePoint([x, y], [elementPosition.centerx, elementPosition.centery], rotation);
  8535. x = rotated[0];
  8536. y = rotated[1];
  8537. }
  8538. a.push([ x, y, xp, yp, connections[i][1], connections[i][2] ]);
  8539. }
  8540. return a;
  8541. },
  8542. rightAndBottomSort = function(a, b) {
  8543. return b[0][0] - a[0][0];
  8544. },
  8545. // used by edgeSortFunctions
  8546. leftAndTopSort = function (a, b) {
  8547. var p1 = a[0][0] < 0 ? -Math.PI - a[0][0] : Math.PI - a[0][0],
  8548. p2 = b[0][0] < 0 ? -Math.PI - b[0][0] : Math.PI - b[0][0];
  8549. return p1 - p2;
  8550. },
  8551. // used by placeAnchors
  8552. edgeSortFunctions = {
  8553. "top":leftAndTopSort,
  8554. "right": rightAndBottomSort,
  8555. "bottom": rightAndBottomSort,
  8556. "left": leftAndTopSort
  8557. },
  8558. // used by placeAnchors
  8559. _sortHelper = function (_array, _fn) {
  8560. return _array.sort(_fn);
  8561. },
  8562. // used by AnchorManager.redraw
  8563. placeAnchors = function (elementId, _anchorLists) {
  8564. var cd = jsPlumbInstance.getCachedData(elementId), sS = cd.s, sO = cd.o,
  8565. placeSomeAnchors = function (desc, elementDimensions, elementPosition, unsortedConnections, isHorizontal, otherMultiplier, orientation) {
  8566. if (unsortedConnections.length > 0) {
  8567. var sc = _sortHelper(unsortedConnections, edgeSortFunctions[desc]), // puts them in order based on the target element's pos on screen
  8568. reverse = desc === "right" || desc === "top",
  8569. rotation = jsPlumbInstance.getRotation(elementId),
  8570. anchors = placeAnchorsOnLine(desc, elementDimensions,
  8571. elementPosition, sc,
  8572. isHorizontal, otherMultiplier, reverse, rotation);
  8573. // takes a computed anchor position and adjusts it for parent offset and scroll, then stores it.
  8574. var _setAnchorLocation = function (endpoint, anchorPos) {
  8575. continuousAnchorLocations[endpoint.id] = [ anchorPos[0], anchorPos[1], anchorPos[2], anchorPos[3] ];
  8576. continuousAnchorOrientations[endpoint.id] = orientation;
  8577. };
  8578. for (var i = 0; i < anchors.length; i++) {
  8579. var c = anchors[i][4], weAreSource = c.endpoints[0].elementId === elementId, weAreTarget = c.endpoints[1].elementId === elementId;
  8580. if (weAreSource) {
  8581. _setAnchorLocation(c.endpoints[0], anchors[i]);
  8582. }
  8583. if (weAreTarget) {
  8584. _setAnchorLocation(c.endpoints[1], anchors[i]);
  8585. }
  8586. }
  8587. }
  8588. };
  8589. placeSomeAnchors("bottom", sS, sO, _anchorLists.bottom, true, 1, [0, 1]);
  8590. placeSomeAnchors("top", sS, sO, _anchorLists.top, true, 0, [0, -1]);
  8591. placeSomeAnchors("left", sS, sO, _anchorLists.left, false, 0, [-1, 0]);
  8592. placeSomeAnchors("right", sS, sO, _anchorLists.right, false, 1, [1, 0]);
  8593. };
  8594. this.reset = function () {
  8595. _amEndpoints = {};
  8596. connectionsByElementId = {};
  8597. anchorLists = {};
  8598. };
  8599. this.addFloatingConnection = function (key, conn) {
  8600. floatingConnections[key] = conn;
  8601. };
  8602. this.newConnection = function (conn) {
  8603. var sourceId = conn.sourceId, targetId = conn.targetId,
  8604. ep = conn.endpoints,
  8605. doRegisterTarget = true,
  8606. registerConnection = function (otherIndex, otherEndpoint, otherAnchor, elId, c) {
  8607. if ((sourceId === targetId) && otherAnchor.isContinuous) {
  8608. // remove the target endpoint's canvas. we dont need it.
  8609. conn._jsPlumb.instance.removeElement(ep[1].canvas);
  8610. doRegisterTarget = false;
  8611. }
  8612. _ju.addToList(connectionsByElementId, elId, [c, otherEndpoint, otherAnchor.constructor === _jp.DynamicAnchor]);
  8613. };
  8614. registerConnection(0, ep[0], ep[0].anchor, targetId, conn);
  8615. if (doRegisterTarget) {
  8616. registerConnection(1, ep[1], ep[1].anchor, sourceId, conn);
  8617. }
  8618. };
  8619. var removeEndpointFromAnchorLists = function (endpoint) {
  8620. (function (list, eId) {
  8621. if (list) { // transient anchors dont get entries in this list.
  8622. var f = function (e) {
  8623. return e[4] === eId;
  8624. };
  8625. _ju.removeWithFunction(list.top, f);
  8626. _ju.removeWithFunction(list.left, f);
  8627. _ju.removeWithFunction(list.bottom, f);
  8628. _ju.removeWithFunction(list.right, f);
  8629. }
  8630. })(anchorLists[endpoint.elementId], endpoint.id);
  8631. };
  8632. this.connectionDetached = function (connInfo, doNotRedraw) {
  8633. var connection = connInfo.connection || connInfo,
  8634. sourceId = connInfo.sourceId,
  8635. targetId = connInfo.targetId,
  8636. ep = connection.endpoints,
  8637. removeConnection = function (otherIndex, otherEndpoint, otherAnchor, elId, c) {
  8638. _ju.removeWithFunction(connectionsByElementId[elId], function (_c) {
  8639. return _c[0].id === c.id;
  8640. });
  8641. };
  8642. removeConnection(1, ep[1], ep[1].anchor, sourceId, connection);
  8643. removeConnection(0, ep[0], ep[0].anchor, targetId, connection);
  8644. if (connection.floatingId) {
  8645. removeConnection(connection.floatingIndex, connection.floatingEndpoint, connection.floatingEndpoint.anchor, connection.floatingId, connection);
  8646. removeEndpointFromAnchorLists(connection.floatingEndpoint);
  8647. }
  8648. // remove from anchorLists
  8649. removeEndpointFromAnchorLists(connection.endpoints[0]);
  8650. removeEndpointFromAnchorLists(connection.endpoints[1]);
  8651. if (!doNotRedraw) {
  8652. self.redraw(connection.sourceId);
  8653. if (connection.targetId !== connection.sourceId) {
  8654. self.redraw(connection.targetId);
  8655. }
  8656. }
  8657. };
  8658. this.addEndpoint = function (endpoint, elementId) {
  8659. _ju.addToList(_amEndpoints, elementId, endpoint);
  8660. };
  8661. this.changeId = function (oldId, newId) {
  8662. connectionsByElementId[newId] = connectionsByElementId[oldId];
  8663. _amEndpoints[newId] = _amEndpoints[oldId];
  8664. delete connectionsByElementId[oldId];
  8665. delete _amEndpoints[oldId];
  8666. };
  8667. this.getConnectionsFor = function (elementId) {
  8668. return connectionsByElementId[elementId] || [];
  8669. };
  8670. this.getEndpointsFor = function (elementId) {
  8671. return _amEndpoints[elementId] || [];
  8672. };
  8673. this.deleteEndpoint = function (endpoint) {
  8674. _ju.removeWithFunction(_amEndpoints[endpoint.elementId], function (e) {
  8675. return e.id === endpoint.id;
  8676. });
  8677. removeEndpointFromAnchorLists(endpoint);
  8678. };
  8679. this.elementRemoved = function (elementId) {
  8680. delete floatingConnections[elementId];
  8681. delete _amEndpoints[elementId];
  8682. _amEndpoints[elementId] = [];
  8683. };
  8684. // updates the given anchor list by either updating an existing anchor's info, or adding it. this function
  8685. // also removes the anchor from its previous list, if the edge it is on has changed.
  8686. // all connections found along the way (those that are connected to one of the faces this function
  8687. // operates on) are added to the connsToPaint list, as are their endpoints. in this way we know to repaint
  8688. // them wthout having to calculate anything else about them.
  8689. var _updateAnchorList = function (lists, theta, order, conn, aBoolean, otherElId, idx, reverse, edgeId, elId, connsToPaint, endpointsToPaint) {
  8690. // first try to find the exact match, but keep track of the first index of a matching element id along the way.s
  8691. var exactIdx = -1,
  8692. firstMatchingElIdx = -1,
  8693. endpoint = conn.endpoints[idx],
  8694. endpointId = endpoint.id,
  8695. oIdx = [1, 0][idx],
  8696. values = [
  8697. [ theta, order ],
  8698. conn,
  8699. aBoolean,
  8700. otherElId,
  8701. endpointId
  8702. ],
  8703. listToAddTo = lists[edgeId],
  8704. listToRemoveFrom = endpoint._continuousAnchorEdge ? lists[endpoint._continuousAnchorEdge] : null,
  8705. i,
  8706. candidate;
  8707. if (listToRemoveFrom) {
  8708. var rIdx = _ju.findWithFunction(listToRemoveFrom, function (e) {
  8709. return e[4] === endpointId;
  8710. });
  8711. if (rIdx !== -1) {
  8712. listToRemoveFrom.splice(rIdx, 1);
  8713. // get all connections from this list
  8714. for (i = 0; i < listToRemoveFrom.length; i++) {
  8715. candidate = listToRemoveFrom[i][1];
  8716. _ju.addWithFunction(connsToPaint, candidate, function (c) {
  8717. return c.id === candidate.id;
  8718. });
  8719. _ju.addWithFunction(endpointsToPaint, listToRemoveFrom[i][1].endpoints[idx], function (e) {
  8720. return e.id === candidate.endpoints[idx].id;
  8721. });
  8722. _ju.addWithFunction(endpointsToPaint, listToRemoveFrom[i][1].endpoints[oIdx], function (e) {
  8723. return e.id === candidate.endpoints[oIdx].id;
  8724. });
  8725. }
  8726. }
  8727. }
  8728. for (i = 0; i < listToAddTo.length; i++) {
  8729. candidate = listToAddTo[i][1];
  8730. if (params.idx === 1 && listToAddTo[i][3] === otherElId && firstMatchingElIdx === -1) {
  8731. firstMatchingElIdx = i;
  8732. }
  8733. _ju.addWithFunction(connsToPaint, candidate, function (c) {
  8734. return c.id === candidate.id;
  8735. });
  8736. _ju.addWithFunction(endpointsToPaint, listToAddTo[i][1].endpoints[idx], function (e) {
  8737. return e.id === candidate.endpoints[idx].id;
  8738. });
  8739. _ju.addWithFunction(endpointsToPaint, listToAddTo[i][1].endpoints[oIdx], function (e) {
  8740. return e.id === candidate.endpoints[oIdx].id;
  8741. });
  8742. }
  8743. if (exactIdx !== -1) {
  8744. listToAddTo[exactIdx] = values;
  8745. }
  8746. else {
  8747. var insertIdx = reverse ? firstMatchingElIdx !== -1 ? firstMatchingElIdx : 0 : listToAddTo.length; // of course we will get this from having looked through the array shortly.
  8748. listToAddTo.splice(insertIdx, 0, values);
  8749. }
  8750. // store this for next time.
  8751. endpoint._continuousAnchorEdge = edgeId;
  8752. };
  8753. //
  8754. // Notification that the connection given has changed source/target from the originalId to the newId.
  8755. //
  8756. // For a change of source this involves:
  8757. // 1. removing the connection from the list of connections stored for the originalId
  8758. // 2. updating the source information for the target of the connection
  8759. // 3. re-registering the connection in connectionsByElementId with the newId
  8760. //
  8761. // For a change of target this means find the entry in an endpoint's list for this connection and update its target endpoint
  8762. // with the current target in the connection.
  8763. //
  8764. this.sourceOrTargetChanged = function (originalId, newId, connection, newElement, anchorIndex) {
  8765. if (anchorIndex === 0) {
  8766. if (originalId !== newId) {
  8767. connection.sourceId = newId;
  8768. connection.source = newElement;
  8769. // remove the entry that points from the old source to the target
  8770. _ju.removeWithFunction(connectionsByElementId[originalId], function (info) {
  8771. return info[0].id === connection.id;
  8772. });
  8773. // find entry for target and update it
  8774. var tIdx = _ju.findWithFunction(connectionsByElementId[connection.targetId], function (i) {
  8775. return i[0].id === connection.id;
  8776. });
  8777. if (tIdx > -1) {
  8778. connectionsByElementId[connection.targetId][tIdx][0] = connection;
  8779. connectionsByElementId[connection.targetId][tIdx][1] = connection.endpoints[0];
  8780. connectionsByElementId[connection.targetId][tIdx][2] = connection.endpoints[0].anchor.constructor === _jp.DynamicAnchor;
  8781. }
  8782. // add entry for new source
  8783. _ju.addToList(connectionsByElementId, newId, [connection, connection.endpoints[1], connection.endpoints[1].anchor.constructor === _jp.DynamicAnchor]);
  8784. // TODO SP not final on this yet. when a user drags an existing connection and it turns into a self
  8785. // loop, then this code hides the target endpoint (by removing it from the DOM) But I think this should
  8786. // occur only if the anchor is Continuous
  8787. if (connection.endpoints[1].anchor.isContinuous) {
  8788. if (connection.source === connection.target) {
  8789. connection._jsPlumb.instance.removeElement(connection.endpoints[1].canvas);
  8790. } else {
  8791. if (connection.endpoints[1].canvas.parentNode == null) {
  8792. connection._jsPlumb.instance.appendElement(connection.endpoints[1].canvas);
  8793. }
  8794. }
  8795. }
  8796. connection.updateConnectedClass();
  8797. }
  8798. } else if (anchorIndex === 1) {
  8799. var sourceElId = connection.endpoints[0].elementId;
  8800. connection.target = newElement;
  8801. connection.targetId = newId;
  8802. var sIndex = _ju.findWithFunction(connectionsByElementId[sourceElId], function (i) {
  8803. return i[0].id === connection.id;
  8804. }),
  8805. tIndex = _ju.findWithFunction(connectionsByElementId[originalId], function (i) {
  8806. return i[0].id === connection.id;
  8807. });
  8808. // update or add data for source
  8809. if (sIndex !== -1) {
  8810. connectionsByElementId[sourceElId][sIndex][0] = connection;
  8811. connectionsByElementId[sourceElId][sIndex][1] = connection.endpoints[1];
  8812. connectionsByElementId[sourceElId][sIndex][2] = connection.endpoints[1].anchor.constructor === _jp.DynamicAnchor;
  8813. }
  8814. // remove entry for previous target (if there)
  8815. if (tIndex > -1) {
  8816. connectionsByElementId[originalId].splice(tIndex, 1);
  8817. // add entry for new target
  8818. _ju.addToList(connectionsByElementId, newId, [connection, connection.endpoints[0], connection.endpoints[0].anchor.constructor === _jp.DynamicAnchor]);
  8819. }
  8820. connection.updateConnectedClass();
  8821. }
  8822. };
  8823. //
  8824. // moves the given endpoint from `currentId` to `element`.
  8825. // This involves:
  8826. //
  8827. // 1. changing the key in _amEndpoints under which the endpoint is stored
  8828. // 2. changing the source or target values in all of the endpoint's connections
  8829. // 3. changing the array in connectionsByElementId in which the endpoint's connections
  8830. // are stored (done by either sourceChanged or updateOtherEndpoint)
  8831. //
  8832. this.rehomeEndpoint = function (ep, currentId, element) {
  8833. var eps = _amEndpoints[currentId] || [],
  8834. elementId = jsPlumbInstance.getId(element);
  8835. if (elementId !== currentId) {
  8836. var idx = eps.indexOf(ep);
  8837. if (idx > -1) {
  8838. var _ep = eps.splice(idx, 1)[0];
  8839. self.add(_ep, elementId);
  8840. }
  8841. }
  8842. for (var i = 0; i < ep.connections.length; i++) {
  8843. if (ep.connections[i].sourceId === currentId) {
  8844. self.sourceOrTargetChanged(currentId, ep.elementId, ep.connections[i], ep.element, 0);
  8845. }
  8846. else if (ep.connections[i].targetId === currentId) {
  8847. self.sourceOrTargetChanged(currentId, ep.elementId, ep.connections[i], ep.element, 1);
  8848. }
  8849. }
  8850. };
  8851. this.redraw = function (elementId, ui, timestamp, offsetToUI, clearEdits, doNotRecalcEndpoint) {
  8852. var connectionsToPaint = [],
  8853. endpointsToPaint = [],
  8854. anchorsToUpdate = [];
  8855. if (!jsPlumbInstance.isSuspendDrawing()) {
  8856. // get all the endpoints for this element
  8857. var ep = _amEndpoints[elementId] || [],
  8858. endpointConnections = connectionsByElementId[elementId] || [];
  8859. timestamp = timestamp || jsPlumbUtil.uuid();
  8860. // offsetToUI are values that would have been calculated in the dragManager when registering
  8861. // an endpoint for an element that had a parent (somewhere in the hierarchy) that had been
  8862. // registered as draggable.
  8863. offsetToUI = offsetToUI || {left: 0, top: 0};
  8864. if (ui) {
  8865. ui = {
  8866. left: ui.left + offsetToUI.left,
  8867. top: ui.top + offsetToUI.top
  8868. };
  8869. }
  8870. // valid for one paint cycle.
  8871. var myOffset = jsPlumbInstance.updateOffset({ elId: elementId, offset: ui, recalc: false, timestamp: timestamp }),
  8872. orientationCache = {};
  8873. // actually, first we should compute the orientation of this element to all other elements to which
  8874. // this element is connected with a continuous anchor (whether both ends of the connection have
  8875. // a continuous anchor or just one)
  8876. for (var i = 0; i < endpointConnections.length; i++) {
  8877. var conn = endpointConnections[i][0],
  8878. sourceId = conn.sourceId,
  8879. targetId = conn.targetId,
  8880. sourceContinuous = conn.endpoints[0].anchor.isContinuous,
  8881. targetContinuous = conn.endpoints[1].anchor.isContinuous;
  8882. if (sourceContinuous || targetContinuous) {
  8883. var oKey = sourceId + "_" + targetId,
  8884. o = orientationCache[oKey],
  8885. oIdx = conn.sourceId === elementId ? 1 : 0,
  8886. targetRotation = jsPlumbInstance.getRotation(targetId),
  8887. sourceRotation = jsPlumbInstance.getRotation(sourceId);
  8888. if (sourceContinuous && !anchorLists[sourceId]) {
  8889. anchorLists[sourceId] = { top: [], right: [], bottom: [], left: [] };
  8890. }
  8891. if (targetContinuous && !anchorLists[targetId]) {
  8892. anchorLists[targetId] = { top: [], right: [], bottom: [], left: [] };
  8893. }
  8894. if (elementId !== targetId) {
  8895. jsPlumbInstance.updateOffset({ elId: targetId, timestamp: timestamp });
  8896. }
  8897. if (elementId !== sourceId) {
  8898. jsPlumbInstance.updateOffset({ elId: sourceId, timestamp: timestamp });
  8899. }
  8900. var td = jsPlumbInstance.getCachedData(targetId),
  8901. sd = jsPlumbInstance.getCachedData(sourceId);
  8902. if (targetId === sourceId && (sourceContinuous || targetContinuous)) {
  8903. // here we may want to improve this by somehow determining the face we'd like
  8904. // to put the connector on. ideally, when drawing, the face should be calculated
  8905. // by determining which face is closest to the point at which the mouse button
  8906. // was released. for now, we're putting it on the top face.
  8907. _updateAnchorList( anchorLists[sourceId], -Math.PI / 2, 0, conn, false, targetId, 0, false, "top", sourceId, connectionsToPaint, endpointsToPaint);
  8908. _updateAnchorList( anchorLists[targetId], -Math.PI / 2, 0, conn, false, sourceId, 1, false, "top", targetId, connectionsToPaint, endpointsToPaint);
  8909. }
  8910. else {
  8911. if (!o) {
  8912. o = this.calculateOrientation(sourceId, targetId, sd.o, td.o, conn.endpoints[0].anchor, conn.endpoints[1].anchor, conn, sourceRotation, targetRotation);
  8913. orientationCache[oKey] = o;
  8914. // this would be a performance enhancement, but the computed angles need to be clamped to
  8915. //the (-PI/2 -> PI/2) range in order for the sorting to work properly.
  8916. /* orientationCache[oKey2] = {
  8917. orientation:o.orientation,
  8918. a:[o.a[1], o.a[0]],
  8919. theta:o.theta + Math.PI,
  8920. theta2:o.theta2 + Math.PI
  8921. };*/
  8922. }
  8923. if (sourceContinuous) {
  8924. _updateAnchorList(anchorLists[sourceId], o.theta, 0, conn, false, targetId, 0, false, o.a[0], sourceId, connectionsToPaint, endpointsToPaint);
  8925. }
  8926. if (targetContinuous) {
  8927. _updateAnchorList(anchorLists[targetId], o.theta2, -1, conn, true, sourceId, 1, true, o.a[1], targetId, connectionsToPaint, endpointsToPaint);
  8928. }
  8929. }
  8930. if (sourceContinuous) {
  8931. _ju.addWithFunction(anchorsToUpdate, sourceId, function (a) {
  8932. return a === sourceId;
  8933. });
  8934. }
  8935. if (targetContinuous) {
  8936. _ju.addWithFunction(anchorsToUpdate, targetId, function (a) {
  8937. return a === targetId;
  8938. });
  8939. }
  8940. _ju.addWithFunction(connectionsToPaint, conn, function (c) {
  8941. return c.id === conn.id;
  8942. });
  8943. if ((sourceContinuous && oIdx === 0) || (targetContinuous && oIdx === 1)) {
  8944. _ju.addWithFunction(endpointsToPaint, conn.endpoints[oIdx], function (e) {
  8945. return e.id === conn.endpoints[oIdx].id;
  8946. });
  8947. }
  8948. }
  8949. }
  8950. // place Endpoints whose anchors are continuous but have no Connections
  8951. for (i = 0; i < ep.length; i++) {
  8952. if (ep[i].connections.length === 0 && ep[i].anchor.isContinuous) {
  8953. if (!anchorLists[elementId]) {
  8954. anchorLists[elementId] = { top: [], right: [], bottom: [], left: [] };
  8955. }
  8956. _updateAnchorList(anchorLists[elementId], -Math.PI / 2, 0, {endpoints: [ep[i], ep[i]], paint: function () {
  8957. }}, false, elementId, 0, false, ep[i].anchor.getDefaultFace(), elementId, connectionsToPaint, endpointsToPaint);
  8958. _ju.addWithFunction(anchorsToUpdate, elementId, function (a) {
  8959. return a === elementId;
  8960. });
  8961. }
  8962. }
  8963. // now place all the continuous anchors we need to;
  8964. for (i = 0; i < anchorsToUpdate.length; i++) {
  8965. placeAnchors(anchorsToUpdate[i], anchorLists[anchorsToUpdate[i]]);
  8966. }
  8967. // now that continuous anchors have been placed, paint all the endpoints for this element
  8968. for (i = 0; i < ep.length; i++) {
  8969. ep[i].paint({ timestamp: timestamp, offset: myOffset, dimensions: myOffset.s, recalc: doNotRecalcEndpoint !== true });
  8970. }
  8971. // ... and any other endpoints we came across as a result of the continuous anchors.
  8972. for (i = 0; i < endpointsToPaint.length; i++) {
  8973. var cd = jsPlumbInstance.getCachedData(endpointsToPaint[i].elementId);
  8974. //endpointsToPaint[i].paint({ timestamp: timestamp, offset: cd, dimensions: cd.s });
  8975. endpointsToPaint[i].paint({ timestamp: null, offset: cd, dimensions: cd.s });
  8976. }
  8977. // paint all the standard and "dynamic connections", which are connections whose other anchor is
  8978. // static and therefore does need to be recomputed; we make sure that happens only one time.
  8979. // TODO we could have compiled a list of these in the first pass through connections; might save some time.
  8980. for (i = 0; i < endpointConnections.length; i++) {
  8981. var otherEndpoint = endpointConnections[i][1];
  8982. if (otherEndpoint.anchor.constructor === _jp.DynamicAnchor) {
  8983. otherEndpoint.paint({ elementWithPrecedence: elementId, timestamp: timestamp });
  8984. _ju.addWithFunction(connectionsToPaint, endpointConnections[i][0], function (c) {
  8985. return c.id === endpointConnections[i][0].id;
  8986. });
  8987. // all the connections for the other endpoint now need to be repainted
  8988. for (var k = 0; k < otherEndpoint.connections.length; k++) {
  8989. if (otherEndpoint.connections[k] !== endpointConnections[i][0]) {
  8990. _ju.addWithFunction(connectionsToPaint, otherEndpoint.connections[k], function (c) {
  8991. return c.id === otherEndpoint.connections[k].id;
  8992. });
  8993. }
  8994. }
  8995. } else {
  8996. _ju.addWithFunction(connectionsToPaint, endpointConnections[i][0], function (c) {
  8997. return c.id === endpointConnections[i][0].id;
  8998. });
  8999. }
  9000. }
  9001. // paint current floating connection for this element, if there is one.
  9002. var fc = floatingConnections[elementId];
  9003. if (fc) {
  9004. fc.paint({timestamp: timestamp, recalc: false, elId: elementId});
  9005. }
  9006. // paint all the connections
  9007. for (i = 0; i < connectionsToPaint.length; i++) {
  9008. connectionsToPaint[i].paint({elId: elementId, timestamp: null, recalc: false, clearEdits: clearEdits});
  9009. }
  9010. }
  9011. return {
  9012. c:connectionsToPaint,
  9013. e:endpointsToPaint
  9014. };
  9015. };
  9016. var ContinuousAnchor = function (anchorParams) {
  9017. _ju.EventGenerator.apply(this);
  9018. this.type = "Continuous";
  9019. this.isDynamic = true;
  9020. this.isContinuous = true;
  9021. var faces = anchorParams.faces || ["top", "right", "bottom", "left"],
  9022. clockwise = !(anchorParams.clockwise === false),
  9023. availableFaces = { },
  9024. opposites = { "top": "bottom", "right": "left", "left": "right", "bottom": "top" },
  9025. clockwiseOptions = { "top": "right", "right": "bottom", "left": "top", "bottom": "left" },
  9026. antiClockwiseOptions = { "top": "left", "right": "top", "left": "bottom", "bottom": "right" },
  9027. secondBest = clockwise ? clockwiseOptions : antiClockwiseOptions,
  9028. lastChoice = clockwise ? antiClockwiseOptions : clockwiseOptions,
  9029. cssClass = anchorParams.cssClass || "",
  9030. _currentFace = null, _lockedFace = null, X_AXIS_FACES = ["left", "right"], Y_AXIS_FACES = ["top", "bottom"],
  9031. _lockedAxis = null;
  9032. for (var i = 0; i < faces.length; i++) {
  9033. availableFaces[faces[i]] = true;
  9034. }
  9035. this.getDefaultFace = function () {
  9036. return faces.length === 0 ? "top" : faces[0];
  9037. };
  9038. this.isRelocatable = function() { return true; };
  9039. this.isSnapOnRelocate = function() { return true; };
  9040. // if the given edge is supported, returns it. otherwise looks for a substitute that _is_
  9041. // supported. if none supported we also return the request edge.
  9042. this.verifyEdge = function (edge) {
  9043. if (availableFaces[edge]) {
  9044. return edge;
  9045. }
  9046. else if (availableFaces[opposites[edge]]) {
  9047. return opposites[edge];
  9048. }
  9049. else if (availableFaces[secondBest[edge]]) {
  9050. return secondBest[edge];
  9051. }
  9052. else if (availableFaces[lastChoice[edge]]) {
  9053. return lastChoice[edge];
  9054. }
  9055. return edge; // we have to give them something.
  9056. };
  9057. this.isEdgeSupported = function (edge) {
  9058. return _lockedAxis == null ?
  9059. (_lockedFace == null ? availableFaces[edge] === true : _lockedFace === edge)
  9060. : _lockedAxis.indexOf(edge) !== -1;
  9061. };
  9062. this.setCurrentFace = function(face, overrideLock) {
  9063. _currentFace = face;
  9064. // if currently locked, and the user wants to override, do that.
  9065. if (overrideLock && _lockedFace != null) {
  9066. _lockedFace = _currentFace;
  9067. }
  9068. };
  9069. this.getCurrentFace = function() { return _currentFace; };
  9070. this.getSupportedFaces = function() {
  9071. var af = [];
  9072. for (var k in availableFaces) {
  9073. if (availableFaces[k]) {
  9074. af.push(k);
  9075. }
  9076. }
  9077. return af;
  9078. };
  9079. this.lock = function() {
  9080. _lockedFace = _currentFace;
  9081. };
  9082. this.unlock = function() {
  9083. _lockedFace = null;
  9084. };
  9085. this.isLocked = function() {
  9086. return _lockedFace != null;
  9087. };
  9088. this.lockCurrentAxis = function() {
  9089. if (_currentFace != null) {
  9090. _lockedAxis = (_currentFace === "left" || _currentFace === "right") ? X_AXIS_FACES : Y_AXIS_FACES;
  9091. }
  9092. };
  9093. this.unlockCurrentAxis = function() {
  9094. _lockedAxis = null;
  9095. };
  9096. this.compute = function (params) {
  9097. return continuousAnchorLocations[params.element.id] || [0, 0];
  9098. };
  9099. this.getCurrentLocation = function (params) {
  9100. return continuousAnchorLocations[params.element.id] || [0, 0];
  9101. };
  9102. this.getOrientation = function (endpoint) {
  9103. return continuousAnchorOrientations[endpoint.id] || [0, 0];
  9104. };
  9105. this.getCssClass = function () {
  9106. return cssClass;
  9107. };
  9108. };
  9109. // continuous anchors
  9110. jsPlumbInstance.continuousAnchorFactory = {
  9111. get: function (params) {
  9112. return new ContinuousAnchor(params);
  9113. },
  9114. clear: function (elementId) {
  9115. delete continuousAnchorLocations[elementId];
  9116. }
  9117. };
  9118. };
  9119. _jp.AnchorManager.prototype.calculateOrientation = function (sourceId,
  9120. targetId,
  9121. sd,
  9122. td,
  9123. sourceAnchor,
  9124. targetAnchor,
  9125. connection,
  9126. sourceRotation,
  9127. targetRotation) {
  9128. var Orientation = { HORIZONTAL: "horizontal", VERTICAL: "vertical", DIAGONAL: "diagonal", IDENTITY: "identity" },
  9129. axes = ["left", "top", "right", "bottom"];
  9130. if (sourceId === targetId) {
  9131. return {
  9132. orientation: Orientation.IDENTITY,
  9133. a: ["top", "top"]
  9134. };
  9135. }
  9136. // since we only support rotation around the center of an element these two lines don't have to take rotation
  9137. // into account.
  9138. var theta = Math.atan2((td.centery - sd.centery), (td.centerx - sd.centerx)),
  9139. theta2 = Math.atan2((sd.centery - td.centery), (sd.centerx - td.centerx));
  9140. // --------------------------------------------------------------------------------------
  9141. // improved face calculation. get midpoints of each face for source and target, then put in an array with all combinations of
  9142. // source/target faces. sort this array by distance between midpoints. the entry at index 0 is our preferred option. we can
  9143. // go through the array one by one until we find an entry in which each requested face is supported.
  9144. var candidates = [], midpoints = { };
  9145. (function (types, dim) {
  9146. for (var i = 0; i < types.length; i++) {
  9147. midpoints[types[i]] = {
  9148. "left": [ dim[i][0].left, dim[i][0].centery ],
  9149. "right": [ dim[i][0].right, dim[i][0].centery ],
  9150. "top": [ dim[i][0].centerx, dim[i][0].top ],
  9151. "bottom": [ dim[i][0].centerx , dim[i][0].bottom]
  9152. };
  9153. if (dim[i][1] !== 0) {
  9154. for (var axis in midpoints[types[i]]) {
  9155. midpoints[types[i]][axis] = jsPlumbUtil.rotatePoint(midpoints[types[i]][axis], [dim[i][0].centerx, dim[i][0].centery], dim[i][1]);
  9156. }
  9157. }
  9158. }
  9159. })([ "source", "target" ], [ [sd, sourceRotation], [td, targetRotation] ]);
  9160. for (var sf = 0; sf < axes.length; sf++) {
  9161. for (var tf = 0; tf < axes.length; tf++) {
  9162. candidates.push({
  9163. source: axes[sf],
  9164. target: axes[tf],
  9165. dist: Biltong.lineLength(midpoints.source[axes[sf]], midpoints.target[axes[tf]])
  9166. });
  9167. }
  9168. }
  9169. candidates.sort(function (a, b) {
  9170. return a.dist < b.dist ? -1 : a.dist > b.dist ? 1 : 0;
  9171. });
  9172. // now go through this list and try to get an entry that satisfies both (there will be one, unless one of the anchors
  9173. // declares no available faces)
  9174. var sourceEdge = candidates[0].source, targetEdge = candidates[0].target;
  9175. for (var i = 0; i < candidates.length; i++) {
  9176. if (sourceAnchor.isContinuous && sourceAnchor.locked) {
  9177. sourceEdge = sourceAnchor.getCurrentFace();
  9178. }
  9179. else if (!sourceAnchor.isContinuous || sourceAnchor.isEdgeSupported(candidates[i].source)) {
  9180. sourceEdge = candidates[i].source;
  9181. }
  9182. else {
  9183. sourceEdge = null;
  9184. }
  9185. if (targetAnchor.isContinuous && targetAnchor.locked) {
  9186. targetEdge = targetAnchor.getCurrentFace();
  9187. }
  9188. else if (!targetAnchor.isContinuous || targetAnchor.isEdgeSupported(candidates[i].target)) {
  9189. targetEdge = candidates[i].target;
  9190. }
  9191. else {
  9192. targetEdge = null;
  9193. }
  9194. if (sourceEdge != null && targetEdge != null) {
  9195. break;
  9196. }
  9197. }
  9198. if (sourceAnchor.isContinuous) {
  9199. sourceAnchor.setCurrentFace(sourceEdge);
  9200. }
  9201. if (targetAnchor.isContinuous) {
  9202. targetAnchor.setCurrentFace(targetEdge);
  9203. }
  9204. // --------------------------------------------------------------------------------------
  9205. return {
  9206. a: [ sourceEdge, targetEdge ],
  9207. theta: theta,
  9208. theta2: theta2
  9209. };
  9210. };
  9211. /**
  9212. * Anchors model a position on some element at which an Endpoint may be located. They began as a first class citizen of jsPlumb, ie. a user
  9213. * was required to create these themselves, but over time this has been replaced by the concept of referring to them either by name (eg. "TopMiddle"),
  9214. * or by an array describing their coordinates (eg. [ 0, 0.5, 0, -1 ], which is the same as "TopMiddle"). jsPlumb now handles all of the
  9215. * creation of Anchors without user intervention.
  9216. */
  9217. _jp.Anchor = function (params) {
  9218. this.x = params.x || 0;
  9219. this.y = params.y || 0;
  9220. this.elementId = params.elementId;
  9221. this.cssClass = params.cssClass || "";
  9222. this.orientation = params.orientation || [ 0, 0 ];
  9223. this.lastReturnValue = null;
  9224. this.offsets = params.offsets || [ 0, 0 ];
  9225. this.timestamp = null;
  9226. this._unrotatedOrientation = [
  9227. this.orientation[0],
  9228. this.orientation[1]
  9229. ];
  9230. this.relocatable = params.relocatable !== false;
  9231. this.snapOnRelocate = params.snapOnRelocate !== false;
  9232. this.locked = false;
  9233. _ju.EventGenerator.apply(this);
  9234. this.compute = function (params) {
  9235. var xy = params.xy, wh = params.wh, timestamp = params.timestamp;
  9236. if (timestamp && timestamp === this.timestamp) {
  9237. return this.lastReturnValue;
  9238. }
  9239. // unrotated position
  9240. var candidate = [ xy[0] + (this.x * wh[0]) + this.offsets[0], xy[1] + (this.y * wh[1]) + this.offsets[1], this.x, this.y ];
  9241. // if rotation set, adjust position.
  9242. var rotation = params.rotation;
  9243. if (rotation != null && rotation !== 0) {
  9244. var c2 = jsPlumbUtil.rotatePoint(candidate, [xy[0] + (wh[0] / 2), xy[1] + (wh[1] / 2) ], rotation);
  9245. // rotate the orientation values too. for rotations that are not multiples of 90 degrees, this will result in values that are not in the set
  9246. // [0, -1, 1 ], and in that case the connector paint may not be perfect. need some evidence from real world usage.
  9247. this.orientation[0] = Math.round((this._unrotatedOrientation[0] * c2[2]) - (this._unrotatedOrientation[1] * c2[3]));
  9248. this.orientation[1] = Math.round((this._unrotatedOrientation[1] * c2[2]) + (this._unrotatedOrientation[0] * c2[3]));
  9249. this.lastReturnValue = [c2[0], c2[1], this.x, this.y];
  9250. } else {
  9251. // if rotation not set (or 0), ensure orientation is original value
  9252. this.orientation[0] = this._unrotatedOrientation[0];
  9253. this.orientation[1] = this._unrotatedOrientation[1];
  9254. this.lastReturnValue = candidate;
  9255. }
  9256. this.timestamp = timestamp;
  9257. return this.lastReturnValue;
  9258. };
  9259. this.getCurrentLocation = function (params) {
  9260. params = params || {};
  9261. return (this.lastReturnValue == null || (params.timestamp != null && this.timestamp !== params.timestamp)) ? this.compute(params) : this.lastReturnValue;
  9262. };
  9263. this.setPosition = function(x, y, ox, oy, overrideLock) {
  9264. if (!this.locked || overrideLock) {
  9265. this.x = x;
  9266. this.y = y;
  9267. this.orientation = [ ox, oy ];
  9268. this.lastReturnValue = null;
  9269. }
  9270. };
  9271. };
  9272. _ju.extend(_jp.Anchor, _ju.EventGenerator, {
  9273. equals: function (anchor) {
  9274. if (!anchor) {
  9275. return false;
  9276. }
  9277. var ao = anchor.getOrientation(),
  9278. o = this.getOrientation();
  9279. return this.x === anchor.x && this.y === anchor.y && this.offsets[0] === anchor.offsets[0] && this.offsets[1] === anchor.offsets[1] && o[0] === ao[0] && o[1] === ao[1];
  9280. },
  9281. getOrientation: function () {
  9282. return this.orientation;
  9283. },
  9284. getCssClass: function () {
  9285. return this.cssClass;
  9286. }
  9287. });
  9288. /**
  9289. * An Anchor that floats. its orientation is computed dynamically from
  9290. * its position relative to the anchor it is floating relative to. It is used when creating
  9291. * a connection through drag and drop.
  9292. *
  9293. * TODO FloatingAnchor could totally be refactored to extend Anchor just slightly.
  9294. */
  9295. _jp.FloatingAnchor = function (params) {
  9296. _jp.Anchor.apply(this, arguments);
  9297. // this is the anchor that this floating anchor is referenced to for
  9298. // purposes of calculating the orientation.
  9299. var ref = params.reference,
  9300. // the canvas this refers to.
  9301. refCanvas = params.referenceCanvas,
  9302. size = _jp.getSize(refCanvas),
  9303. // these are used to store the current relative position of our
  9304. // anchor wrt the reference anchor. they only indicate
  9305. // direction, so have a value of 1 or -1 (or, very rarely, 0). these
  9306. // values are written by the compute method, and read
  9307. // by the getOrientation method.
  9308. xDir = 0, yDir = 0,
  9309. // temporary member used to store an orientation when the floating
  9310. // anchor is hovering over another anchor.
  9311. orientation = null,
  9312. _lastResult = null;
  9313. // clear from parent. we want floating anchor orientation to always be computed.
  9314. this.orientation = null;
  9315. // set these to 0 each; they are used by certain types of connectors in the loopback case,
  9316. // when the connector is trying to clear the element it is on. but for floating anchor it's not
  9317. // very important.
  9318. this.x = 0;
  9319. this.y = 0;
  9320. this.isFloating = true;
  9321. this.compute = function (params) {
  9322. var xy = params.xy,
  9323. result = [ xy[0] + (size[0] / 2), xy[1] + (size[1] / 2) ]; // return origin of the element. we may wish to improve this so that any object can be the drag proxy.
  9324. _lastResult = result;
  9325. return result;
  9326. };
  9327. this.getOrientation = function (_endpoint) {
  9328. if (orientation) {
  9329. return orientation;
  9330. }
  9331. else {
  9332. var o = ref.getOrientation(_endpoint);
  9333. // here we take into account the orientation of the other
  9334. // anchor: if it declares zero for some direction, we declare zero too. this might not be the most awesome. perhaps we can come
  9335. // up with a better way. it's just so that the line we draw looks like it makes sense. maybe this wont make sense.
  9336. return [ Math.abs(o[0]) * xDir * -1,
  9337. Math.abs(o[1]) * yDir * -1 ];
  9338. }
  9339. };
  9340. /**
  9341. * notification the endpoint associated with this anchor is hovering
  9342. * over another anchor; we want to assume that anchor's orientation
  9343. * for the duration of the hover.
  9344. */
  9345. this.over = function (anchor, endpoint) {
  9346. orientation = anchor.getOrientation(endpoint);
  9347. };
  9348. /**
  9349. * notification the endpoint associated with this anchor is no
  9350. * longer hovering over another anchor; we should resume calculating
  9351. * orientation as we normally do.
  9352. */
  9353. this.out = function () {
  9354. orientation = null;
  9355. };
  9356. this.getCurrentLocation = function (params) {
  9357. return _lastResult == null ? this.compute(params) : _lastResult;
  9358. };
  9359. };
  9360. _ju.extend(_jp.FloatingAnchor, _jp.Anchor);
  9361. var _convertAnchor = function (anchor, jsPlumbInstance, elementId) {
  9362. return anchor.constructor === _jp.Anchor ? anchor : jsPlumbInstance.makeAnchor(anchor, elementId, jsPlumbInstance);
  9363. };
  9364. /*
  9365. * A DynamicAnchor is an Anchor that contains a list of other Anchors, which it cycles
  9366. * through at compute time to find the one that is located closest to
  9367. * the center of the target element, and returns that Anchor's compute
  9368. * method result. this causes endpoints to follow each other with
  9369. * respect to the orientation of their target elements, which is a useful
  9370. * feature for some applications.
  9371. *
  9372. */
  9373. _jp.DynamicAnchor = function (params) {
  9374. _jp.Anchor.apply(this, arguments);
  9375. this.isDynamic = true;
  9376. this.anchors = [];
  9377. this.elementId = params.elementId;
  9378. this.jsPlumbInstance = params.jsPlumbInstance;
  9379. for (var i = 0; i < params.anchors.length; i++) {
  9380. this.anchors[i] = _convertAnchor(params.anchors[i], this.jsPlumbInstance, this.elementId);
  9381. }
  9382. this.getAnchors = function () {
  9383. return this.anchors;
  9384. };
  9385. var _curAnchor = this.anchors.length > 0 ? this.anchors[0] : null,
  9386. _lastAnchor = _curAnchor,
  9387. self = this,
  9388. // helper method to calculate the distance between the centers of the two elements.
  9389. _distance = function (anchor, cx, cy, xy, wh, r, tr) {
  9390. var ax = xy[0] + (anchor.x * wh[0]), ay = xy[1] + (anchor.y * wh[1]),
  9391. acx = xy[0] + (wh[0] / 2), acy = xy[1] + (wh[1] / 2);
  9392. if(r != null && r !== 0) {
  9393. var rotated = jsPlumbUtil.rotatePoint([ax,ay], [acx, acy], r);
  9394. ax = rotated[0];
  9395. ay = rotated[1];
  9396. }
  9397. return (Math.sqrt(Math.pow(cx - ax, 2) + Math.pow(cy - ay, 2)) +
  9398. Math.sqrt(Math.pow(acx - ax, 2) + Math.pow(acy - ay, 2)));
  9399. },
  9400. // default method uses distance between element centers. you can provide your own method in the dynamic anchor
  9401. // constructor (and also to jsPlumb.makeDynamicAnchor). the arguments to it are:
  9402. // xy - xy loc of the anchor's element
  9403. // wh - anchor's element's dimensions
  9404. // txy - xy loc of the element of the other anchor in the connection
  9405. // twh - dimensions of the element of the other anchor in the connection.
  9406. // r - the rotation of the anchor's element
  9407. // tr - the rotation of the target anchor's element. currently unused; a placeholder for possible future refactoring.
  9408. // anchors - the list of selectable anchors
  9409. _anchorSelector = params.selector || function (xy, wh, txy, twh, r, tr, anchors) {
  9410. var cx = txy[0] + (twh[0] / 2), cy = txy[1] + (twh[1] / 2);
  9411. var minIdx = -1, minDist = Infinity;
  9412. for (var i = 0; i < anchors.length; i++) {
  9413. var d = _distance(anchors[i], cx, cy, xy, wh, r, tr);
  9414. if (d < minDist) {
  9415. minIdx = i + 0;
  9416. minDist = d;
  9417. }
  9418. }
  9419. return anchors[minIdx];
  9420. };
  9421. this.compute = function (params) {
  9422. var xy = params.xy, wh = params.wh, txy = params.txy, twh = params.twh, r = params.rotation, tr = params.tRotation;
  9423. this.timestamp = params.timestamp;
  9424. // if anchor is locked or an opposite element was not given, we
  9425. // maintain our state. anchor will be locked
  9426. // if it is the source of a drag and drop.
  9427. if (this.locked || txy == null || twh == null) {
  9428. this.lastReturnValue = _curAnchor.compute(params);
  9429. return this.lastReturnValue;
  9430. }
  9431. else {
  9432. params.timestamp = null; // otherwise clear this, i think. we want the anchor to compute.
  9433. }
  9434. _curAnchor = _anchorSelector(xy, wh, txy, twh, r, tr, this.anchors);
  9435. this.x = _curAnchor.x;
  9436. this.y = _curAnchor.y;
  9437. if (_curAnchor !== _lastAnchor) {
  9438. this.fire("anchorChanged", _curAnchor);
  9439. }
  9440. _lastAnchor = _curAnchor;
  9441. this.lastReturnValue = _curAnchor.compute(params);
  9442. return this.lastReturnValue;
  9443. };
  9444. this.getCurrentLocation = function (params) {
  9445. return _curAnchor != null ? _curAnchor.getCurrentLocation(params) : null;
  9446. };
  9447. this.getOrientation = function (_endpoint) {
  9448. return _curAnchor != null ? _curAnchor.getOrientation(_endpoint) : [ 0, 0 ];
  9449. };
  9450. this.over = function (anchor, endpoint) {
  9451. if (_curAnchor != null) {
  9452. _curAnchor.over(anchor, endpoint);
  9453. }
  9454. };
  9455. this.out = function () {
  9456. if (_curAnchor != null) {
  9457. _curAnchor.out();
  9458. }
  9459. };
  9460. this.setAnchor = function(a) {
  9461. _curAnchor = a;
  9462. };
  9463. this.getCssClass = function () {
  9464. return (_curAnchor && _curAnchor.getCssClass()) || "";
  9465. };
  9466. /**
  9467. * Attempt to match an anchor with the given coordinates and then set it.
  9468. * @param coords
  9469. * @returns true if matching anchor found, false otherwise.
  9470. */
  9471. this.setAnchorCoordinates = function(coords) {
  9472. var idx = jsPlumbUtil.findWithFunction(this.anchors, function(a) {
  9473. return a.x === coords[0] && a.y === coords[1];
  9474. });
  9475. if (idx !== -1) {
  9476. this.setAnchor(this.anchors[idx]);
  9477. return true;
  9478. } else {
  9479. return false;
  9480. }
  9481. };
  9482. };
  9483. _ju.extend(_jp.DynamicAnchor, _jp.Anchor);
  9484. // -------- basic anchors ------------------
  9485. var _curryAnchor = function (x, y, ox, oy, type, fnInit) {
  9486. _jp.Anchors[type] = function (params) {
  9487. var a = params.jsPlumbInstance.makeAnchor([ x, y, ox, oy, 0, 0 ], params.elementId, params.jsPlumbInstance);
  9488. a.type = type;
  9489. if (fnInit) {
  9490. fnInit(a, params);
  9491. }
  9492. return a;
  9493. };
  9494. };
  9495. _curryAnchor(0.5, 0, 0, -1, "TopCenter");
  9496. _curryAnchor(0.5, 1, 0, 1, "BottomCenter");
  9497. _curryAnchor(0, 0.5, -1, 0, "LeftMiddle");
  9498. _curryAnchor(1, 0.5, 1, 0, "RightMiddle");
  9499. _curryAnchor(0.5, 0, 0, -1, "Top");
  9500. _curryAnchor(0.5, 1, 0, 1, "Bottom");
  9501. _curryAnchor(0, 0.5, -1, 0, "Left");
  9502. _curryAnchor(1, 0.5, 1, 0, "Right");
  9503. _curryAnchor(0.5, 0.5, 0, 0, "Center");
  9504. _curryAnchor(1, 0, 0, -1, "TopRight");
  9505. _curryAnchor(1, 1, 0, 1, "BottomRight");
  9506. _curryAnchor(0, 0, 0, -1, "TopLeft");
  9507. _curryAnchor(0, 1, 0, 1, "BottomLeft");
  9508. // ------- dynamic anchors -------------------
  9509. // default dynamic anchors chooses from Top, Right, Bottom, Left
  9510. _jp.Defaults.DynamicAnchors = function (params) {
  9511. return params.jsPlumbInstance.makeAnchors(["TopCenter", "RightMiddle", "BottomCenter", "LeftMiddle"], params.elementId, params.jsPlumbInstance);
  9512. };
  9513. // default dynamic anchors bound to name 'AutoDefault'
  9514. _jp.Anchors.AutoDefault = function (params) {
  9515. var a = params.jsPlumbInstance.makeDynamicAnchor(_jp.Defaults.DynamicAnchors(params));
  9516. a.type = "AutoDefault";
  9517. return a;
  9518. };
  9519. // ------- continuous anchors -------------------
  9520. var _curryContinuousAnchor = function (type, faces) {
  9521. _jp.Anchors[type] = function (params) {
  9522. var a = params.jsPlumbInstance.makeAnchor(["Continuous", { faces: faces }], params.elementId, params.jsPlumbInstance);
  9523. a.type = type;
  9524. return a;
  9525. };
  9526. };
  9527. _jp.Anchors.Continuous = function (params) {
  9528. return params.jsPlumbInstance.continuousAnchorFactory.get(params);
  9529. };
  9530. _curryContinuousAnchor("ContinuousLeft", ["left"]);
  9531. _curryContinuousAnchor("ContinuousTop", ["top"]);
  9532. _curryContinuousAnchor("ContinuousBottom", ["bottom"]);
  9533. _curryContinuousAnchor("ContinuousRight", ["right"]);
  9534. // ------- position assign anchors -------------------
  9535. // this anchor type lets you assign the position at connection time.
  9536. _curryAnchor(0, 0, 0, 0, "Assign", function (anchor, params) {
  9537. // find what to use as the "position finder". the user may have supplied a String which represents
  9538. // the id of a position finder in jsPlumb.AnchorPositionFinders, or the user may have supplied the
  9539. // position finder as a function. we find out what to use and then set it on the anchor.
  9540. var pf = params.position || "Fixed";
  9541. anchor.positionFinder = pf.constructor === String ? params.jsPlumbInstance.AnchorPositionFinders[pf] : pf;
  9542. // always set the constructor params; the position finder might need them later (the Grid one does,
  9543. // for example)
  9544. anchor.constructorParams = params;
  9545. });
  9546. // these are the default anchor positions finders, which are used by the makeTarget function. supplying
  9547. // a position finder argument to that function allows you to specify where the resulting anchor will
  9548. // be located
  9549. root.jsPlumbInstance.prototype.AnchorPositionFinders = {
  9550. "Fixed": function (dp, ep, es) {
  9551. return [ (dp.left - ep.left) / es[0], (dp.top - ep.top) / es[1] ];
  9552. },
  9553. "Grid": function (dp, ep, es, params) {
  9554. var dx = dp.left - ep.left, dy = dp.top - ep.top,
  9555. gx = es[0] / (params.grid[0]), gy = es[1] / (params.grid[1]),
  9556. mx = Math.floor(dx / gx), my = Math.floor(dy / gy);
  9557. return [ ((mx * gx) + (gx / 2)) / es[0], ((my * gy) + (gy / 2)) / es[1] ];
  9558. }
  9559. };
  9560. // ------- perimeter anchors -------------------
  9561. _jp.Anchors.Perimeter = function (params) {
  9562. params = params || {};
  9563. var anchorCount = params.anchorCount || 60,
  9564. shape = params.shape;
  9565. if (!shape) {
  9566. throw new Error("no shape supplied to Perimeter Anchor type");
  9567. }
  9568. var _circle = function () {
  9569. var r = 0.5, step = Math.PI * 2 / anchorCount, current = 0, a = [];
  9570. for (var i = 0; i < anchorCount; i++) {
  9571. var x = r + (r * Math.sin(current)),
  9572. y = r + (r * Math.cos(current));
  9573. a.push([ x, y, 0, 0 ]);
  9574. current += step;
  9575. }
  9576. return a;
  9577. },
  9578. _path = function (segments) {
  9579. var anchorsPerFace = anchorCount / segments.length, a = [],
  9580. _computeFace = function (x1, y1, x2, y2, fractionalLength, ox, oy) {
  9581. anchorsPerFace = anchorCount * fractionalLength;
  9582. var dx = (x2 - x1) / anchorsPerFace, dy = (y2 - y1) / anchorsPerFace;
  9583. for (var i = 0; i < anchorsPerFace; i++) {
  9584. a.push([
  9585. x1 + (dx * i),
  9586. y1 + (dy * i),
  9587. ox == null ? 0 : ox,
  9588. oy == null ? 0 : oy
  9589. ]);
  9590. }
  9591. };
  9592. for (var i = 0; i < segments.length; i++) {
  9593. _computeFace.apply(null, segments[i]);
  9594. }
  9595. return a;
  9596. },
  9597. _shape = function (faces) {
  9598. var s = [];
  9599. for (var i = 0; i < faces.length; i++) {
  9600. s.push([faces[i][0], faces[i][1], faces[i][2], faces[i][3], 1 / faces.length, faces[i][4], faces[i][5]]);
  9601. }
  9602. return _path(s);
  9603. },
  9604. _rectangle = function () {
  9605. return _shape([
  9606. [ 0, 0, 1, 0, 0, -1 ],
  9607. [ 1, 0, 1, 1, 1, 0 ],
  9608. [ 1, 1, 0, 1, 0, 1 ],
  9609. [ 0, 1, 0, 0, -1, 0 ]
  9610. ]);
  9611. };
  9612. var _shapes = {
  9613. "Circle": _circle,
  9614. "Ellipse": _circle,
  9615. "Diamond": function () {
  9616. return _shape([
  9617. [ 0.5, 0, 1, 0.5 ],
  9618. [ 1, 0.5, 0.5, 1 ],
  9619. [ 0.5, 1, 0, 0.5 ],
  9620. [ 0, 0.5, 0.5, 0 ]
  9621. ]);
  9622. },
  9623. "Rectangle": _rectangle,
  9624. "Square": _rectangle,
  9625. "Triangle": function () {
  9626. return _shape([
  9627. [ 0.5, 0, 1, 1 ],
  9628. [ 1, 1, 0, 1 ],
  9629. [ 0, 1, 0.5, 0]
  9630. ]);
  9631. },
  9632. "Path": function (params) {
  9633. var points = params.points, p = [], tl = 0;
  9634. for (var i = 0; i < points.length - 1; i++) {
  9635. var l = Math.sqrt(Math.pow(points[i][2] - points[i][0]) + Math.pow(points[i][3] - points[i][1]));
  9636. tl += l;
  9637. p.push([points[i][0], points[i][1], points[i + 1][0], points[i + 1][1], l]);
  9638. }
  9639. for (var j = 0; j < p.length; j++) {
  9640. p[j][4] = p[j][4] / tl;
  9641. }
  9642. return _path(p);
  9643. }
  9644. },
  9645. _rotate = function (points, amountInDegrees) {
  9646. var o = [], theta = amountInDegrees / 180 * Math.PI;
  9647. for (var i = 0; i < points.length; i++) {
  9648. var _x = points[i][0] - 0.5,
  9649. _y = points[i][1] - 0.5;
  9650. o.push([
  9651. 0.5 + ((_x * Math.cos(theta)) - (_y * Math.sin(theta))),
  9652. 0.5 + ((_x * Math.sin(theta)) + (_y * Math.cos(theta))),
  9653. points[i][2],
  9654. points[i][3]
  9655. ]);
  9656. }
  9657. return o;
  9658. };
  9659. if (!_shapes[shape]) {
  9660. throw new Error("Shape [" + shape + "] is unknown by Perimeter Anchor type");
  9661. }
  9662. var da = _shapes[shape](params);
  9663. if (params.rotation) {
  9664. da = _rotate(da, params.rotation);
  9665. }
  9666. var a = params.jsPlumbInstance.makeDynamicAnchor(da);
  9667. a.type = "Perimeter";
  9668. return a;
  9669. };
  9670. }).call(typeof window !== 'undefined' ? window : this);
  9671. /*
  9672. * Default router. Defers to an AnchorManager for placement of anchors, and connector paint routines for paths.
  9673. * Currently this is a placeholder and acts as a facade to the pre-existing anchor manager. The Toolkit edition
  9674. * will make use of concept to provide more advanced routing.
  9675. *
  9676. * Copyright (c) 2010 - 2020 jsPlumb (hello@jsplumbtoolkit.com)
  9677. *
  9678. * https://jsplumbtoolkit.com
  9679. * https://github.com/jsplumb/jsplumb
  9680. *
  9681. * Dual licensed under the MIT and GPL2 licenses.
  9682. */
  9683. ;
  9684. (function () {
  9685. "use strict";
  9686. var root = this,
  9687. _ju = root.jsPlumbUtil,
  9688. _jp = root.jsPlumb;
  9689. _jp.DefaultRouter = function(jsPlumbInstance) {
  9690. this.jsPlumbInstance = jsPlumbInstance;
  9691. this.anchorManager = new _jp.AnchorManager({jsPlumbInstance:jsPlumbInstance});
  9692. this.sourceOrTargetChanged = function (originalId, newId, connection, newElement, anchorIndex) {
  9693. this.anchorManager.sourceOrTargetChanged(originalId, newId, connection, newElement, anchorIndex);
  9694. };
  9695. this.reset = function() {
  9696. this.anchorManager.reset();
  9697. };
  9698. this.changeId = function (oldId, newId) {
  9699. this.anchorManager.changeId(oldId, newId);
  9700. };
  9701. this.elementRemoved = function (elementId) {
  9702. this.anchorManager.elementRemoved(elementId);
  9703. };
  9704. this.newConnection = function (conn) {
  9705. this.anchorManager.newConnection(conn);
  9706. };
  9707. this.connectionDetached = function (connInfo, doNotRedraw) {
  9708. this.anchorManager.connectionDetached(connInfo, doNotRedraw);
  9709. };
  9710. this.redraw = function (elementId, ui, timestamp, offsetToUI, clearEdits, doNotRecalcEndpoint) {
  9711. return this.anchorManager.redraw(elementId, ui, timestamp, offsetToUI, clearEdits, doNotRecalcEndpoint);
  9712. };
  9713. this.deleteEndpoint = function (endpoint) {
  9714. this.anchorManager.deleteEndpoint(endpoint);
  9715. };
  9716. this.rehomeEndpoint = function (ep, currentId, element) {
  9717. this.anchorManager.rehomeEndpoint(ep, currentId, element);
  9718. };
  9719. this.addEndpoint = function (endpoint, elementId) {
  9720. this.anchorManager.addEndpoint(endpoint, elementId);
  9721. };
  9722. };
  9723. }).call(typeof window !== 'undefined' ? window : this);
  9724. /*
  9725. * This file contains the default Connectors, Endpoint and Overlay definitions.
  9726. *
  9727. * Copyright (c) 2010 - 2020 jsPlumb (hello@jsplumbtoolkit.com)
  9728. *
  9729. * https://jsplumbtoolkit.com
  9730. * https://github.com/jsplumb/jsplumb
  9731. *
  9732. * Dual licensed under the MIT and GPL2 licenses.
  9733. */
  9734. ;
  9735. (function () {
  9736. "use strict";
  9737. var root = this, _jp = root.jsPlumb, _ju = root.jsPlumbUtil, _jg = root.Biltong;
  9738. _jp.Segments = {
  9739. /*
  9740. * Class: AbstractSegment
  9741. * A Connector is made up of 1..N Segments, each of which has a Type, such as 'Straight', 'Arc',
  9742. * 'Bezier'. This is new from 1.4.2, and gives us a lot more flexibility when drawing connections: things such
  9743. * as rounded corners for flowchart connectors, for example, or a straight line stub for Bezier connections, are
  9744. * much easier to do now.
  9745. *
  9746. * A Segment is responsible for providing coordinates for painting it, and also must be able to report its length.
  9747. *
  9748. */
  9749. AbstractSegment: function (params) {
  9750. this.params = params;
  9751. /**
  9752. * Function: findClosestPointOnPath
  9753. * Finds the closest point on this segment to the given [x, y],
  9754. * returning both the x and y of the point plus its distance from
  9755. * the supplied point, and its location along the length of the
  9756. * path inscribed by the segment. This implementation returns
  9757. * Infinity for distance and null values for everything else;
  9758. * subclasses are expected to override.
  9759. */
  9760. this.findClosestPointOnPath = function (x, y) {
  9761. return {
  9762. d: Infinity,
  9763. x: null,
  9764. y: null,
  9765. l: null
  9766. };
  9767. };
  9768. this.getBounds = function () {
  9769. return {
  9770. minX: Math.min(params.x1, params.x2),
  9771. minY: Math.min(params.y1, params.y2),
  9772. maxX: Math.max(params.x1, params.x2),
  9773. maxY: Math.max(params.y1, params.y2)
  9774. };
  9775. };
  9776. /**
  9777. * Computes the list of points on the segment that intersect the given line.
  9778. * @method lineIntersection
  9779. * @param {number} x1
  9780. * @param {number} y1
  9781. * @param {number} x2
  9782. * @param {number} y2
  9783. * @returns {Array<[number, number]>}
  9784. */
  9785. this.lineIntersection = function(x1, y1, x2, y2) {
  9786. return [];
  9787. };
  9788. /**
  9789. * Computes the list of points on the segment that intersect the box with the given origin and size.
  9790. * @method boxIntersection
  9791. * @param {number} x1
  9792. * @param {number} y1
  9793. * @param {number} w
  9794. * @param {number} h
  9795. * @returns {Array<[number, number]>}
  9796. */
  9797. this.boxIntersection = function(x, y, w, h) {
  9798. var a = [];
  9799. a.push.apply(a, this.lineIntersection(x, y, x + w, y));
  9800. a.push.apply(a, this.lineIntersection(x + w, y, x + w, y + h));
  9801. a.push.apply(a, this.lineIntersection(x + w, y + h, x, y + h));
  9802. a.push.apply(a, this.lineIntersection(x, y + h, x, y));
  9803. return a;
  9804. };
  9805. /**
  9806. * Computes the list of points on the segment that intersect the given bounding box, which is an object of the form { x:.., y:.., w:.., h:.. }.
  9807. * @method lineIntersection
  9808. * @param {BoundingRectangle} box
  9809. * @returns {Array<[number, number]>}
  9810. */
  9811. this.boundingBoxIntersection = function(box) {
  9812. return this.boxIntersection(box.x, box.y, box.w, box.y);
  9813. };
  9814. },
  9815. Straight: function (params) {
  9816. var _super = _jp.Segments.AbstractSegment.apply(this, arguments),
  9817. length, m, m2, x1, x2, y1, y2,
  9818. _recalc = function () {
  9819. length = Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2));
  9820. m = _jg.gradient({x: x1, y: y1}, {x: x2, y: y2});
  9821. m2 = -1 / m;
  9822. };
  9823. this.type = "Straight";
  9824. this.getLength = function () {
  9825. return length;
  9826. };
  9827. this.getGradient = function () {
  9828. return m;
  9829. };
  9830. this.getCoordinates = function () {
  9831. return { x1: x1, y1: y1, x2: x2, y2: y2 };
  9832. };
  9833. this.setCoordinates = function (coords) {
  9834. x1 = coords.x1;
  9835. y1 = coords.y1;
  9836. x2 = coords.x2;
  9837. y2 = coords.y2;
  9838. _recalc();
  9839. };
  9840. this.setCoordinates({x1: params.x1, y1: params.y1, x2: params.x2, y2: params.y2});
  9841. this.getBounds = function () {
  9842. return {
  9843. minX: Math.min(x1, x2),
  9844. minY: Math.min(y1, y2),
  9845. maxX: Math.max(x1, x2),
  9846. maxY: Math.max(y1, y2)
  9847. };
  9848. };
  9849. /**
  9850. * returns the point on the segment's path that is 'location' along the length of the path, where 'location' is a decimal from
  9851. * 0 to 1 inclusive. for the straight line segment this is simple maths.
  9852. */
  9853. this.pointOnPath = function (location, absolute) {
  9854. if (location === 0 && !absolute) {
  9855. return { x: x1, y: y1 };
  9856. }
  9857. else if (location === 1 && !absolute) {
  9858. return { x: x2, y: y2 };
  9859. }
  9860. else {
  9861. var l = absolute ? location > 0 ? location : length + location : location * length;
  9862. return _jg.pointOnLine({x: x1, y: y1}, {x: x2, y: y2}, l);
  9863. }
  9864. };
  9865. /**
  9866. * returns the gradient of the segment at the given point - which for us is constant.
  9867. */
  9868. this.gradientAtPoint = function (_) {
  9869. return m;
  9870. };
  9871. /**
  9872. * returns the point on the segment's path that is 'distance' along the length of the path from 'location', where
  9873. * 'location' is a decimal from 0 to 1 inclusive, and 'distance' is a number of pixels.
  9874. * this hands off to jsPlumbUtil to do the maths, supplying two points and the distance.
  9875. */
  9876. this.pointAlongPathFrom = function (location, distance, absolute) {
  9877. var p = this.pointOnPath(location, absolute),
  9878. farAwayPoint = distance <= 0 ? {x: x1, y: y1} : {x: x2, y: y2 };
  9879. /*
  9880. location == 1 ? {
  9881. x:x1 + ((x2 - x1) * 10),
  9882. y:y1 + ((y1 - y2) * 10)
  9883. } :
  9884. */
  9885. if (distance <= 0 && Math.abs(distance) > 1) {
  9886. distance *= -1;
  9887. }
  9888. return _jg.pointOnLine(p, farAwayPoint, distance);
  9889. };
  9890. // is c between a and b?
  9891. var within = function (a, b, c) {
  9892. return c >= Math.min(a, b) && c <= Math.max(a, b);
  9893. };
  9894. // find which of a and b is closest to c
  9895. var closest = function (a, b, c) {
  9896. return Math.abs(c - a) < Math.abs(c - b) ? a : b;
  9897. };
  9898. /**
  9899. Function: findClosestPointOnPath
  9900. Finds the closest point on this segment to [x,y]. See
  9901. notes on this method in AbstractSegment.
  9902. */
  9903. this.findClosestPointOnPath = function (x, y) {
  9904. var out = {
  9905. d: Infinity,
  9906. x: null,
  9907. y: null,
  9908. l: null,
  9909. x1: x1,
  9910. x2: x2,
  9911. y1: y1,
  9912. y2: y2
  9913. };
  9914. if (m === 0) {
  9915. out.y = y1;
  9916. out.x = within(x1, x2, x) ? x : closest(x1, x2, x);
  9917. }
  9918. else if (m === Infinity || m === -Infinity) {
  9919. out.x = x1;
  9920. out.y = within(y1, y2, y) ? y : closest(y1, y2, y);
  9921. }
  9922. else {
  9923. // closest point lies on normal from given point to this line.
  9924. var b = y1 - (m * x1),
  9925. b2 = y - (m2 * x),
  9926. // y1 = m.x1 + b and y1 = m2.x1 + b2
  9927. // so m.x1 + b = m2.x1 + b2
  9928. // x1(m - m2) = b2 - b
  9929. // x1 = (b2 - b) / (m - m2)
  9930. _x1 = (b2 - b) / (m - m2),
  9931. _y1 = (m * _x1) + b;
  9932. out.x = within(x1, x2, _x1) ? _x1 : closest(x1, x2, _x1);//_x1;
  9933. out.y = within(y1, y2, _y1) ? _y1 : closest(y1, y2, _y1);//_y1;
  9934. }
  9935. var fractionInSegment = _jg.lineLength([ out.x, out.y ], [ x1, y1 ]);
  9936. out.d = _jg.lineLength([x, y], [out.x, out.y]);
  9937. out.l = fractionInSegment / length;
  9938. return out;
  9939. };
  9940. var _pointLiesBetween = function(q, p1, p2) {
  9941. return (p2 > p1) ? (p1 <= q && q <= p2) : (p1 >= q && q >= p2);
  9942. }, _plb = _pointLiesBetween;
  9943. /**
  9944. * Calculates all intersections of the given line with this segment.
  9945. * @param _x1
  9946. * @param _y1
  9947. * @param _x2
  9948. * @param _y2
  9949. * @returns {Array}
  9950. */
  9951. this.lineIntersection = function(_x1, _y1, _x2, _y2) {
  9952. var m2 = Math.abs(_jg.gradient({x: _x1, y: _y1}, {x: _x2, y: _y2})),
  9953. m1 = Math.abs(m),
  9954. b = m1 === Infinity ? x1 : y1 - (m1 * x1),
  9955. out = [],
  9956. b2 = m2 === Infinity ? _x1 : _y1 - (m2 * _x1);
  9957. // if lines parallel, no intersection
  9958. if (m2 !== m1) {
  9959. // perpendicular, segment horizontal
  9960. if(m2 === Infinity && m1 === 0) {
  9961. if (_plb(_x1, x1, x2) && _plb(y1, _y1, _y2)) {
  9962. out = [ _x1, y1 ]; // we return X on the incident line and Y from the segment
  9963. }
  9964. } else if(m2 === 0 && m1 === Infinity) {
  9965. // perpendicular, segment vertical
  9966. if(_plb(_y1, y1, y2) && _plb(x1, _x1, _x2)) {
  9967. out = [x1, _y1]; // we return X on the segment and Y from the incident line
  9968. }
  9969. } else {
  9970. var X, Y;
  9971. if (m2 === Infinity) {
  9972. // test line is a vertical line. where does it cross the segment?
  9973. X = _x1;
  9974. if (_plb(X, x1, x2)) {
  9975. Y = (m1 * _x1) + b;
  9976. if (_plb(Y, _y1, _y2)) {
  9977. out = [ X, Y ];
  9978. }
  9979. }
  9980. } else if (m2 === 0) {
  9981. Y = _y1;
  9982. // test line is a horizontal line. where does it cross the segment?
  9983. if (_plb(Y, y1, y2)) {
  9984. X = (_y1 - b) / m1;
  9985. if (_plb(X, _x1, _x2)) {
  9986. out = [ X, Y ];
  9987. }
  9988. }
  9989. } else {
  9990. // mX + b = m2X + b2
  9991. // mX - m2X = b2 - b
  9992. // X(m - m2) = b2 - b
  9993. // X = (b2 - b) / (m - m2)
  9994. // Y = mX + b
  9995. X = (b2 - b) / (m1 - m2);
  9996. Y = (m1 * X) + b;
  9997. if(_plb(X, x1, x2) && _plb(Y, y1, y2)) {
  9998. out = [ X, Y];
  9999. }
  10000. }
  10001. }
  10002. }
  10003. return out;
  10004. };
  10005. /**
  10006. * Calculates all intersections of the given box with this segment. By default this method simply calls `lineIntersection` with each of the four
  10007. * faces of the box; subclasses can override this if they think there's a faster way to compute the entire box at once.
  10008. * @param x X position of top left corner of box
  10009. * @param y Y position of top left corner of box
  10010. * @param w width of box
  10011. * @param h height of box
  10012. * @returns {Array}
  10013. */
  10014. this.boxIntersection = function(x, y, w, h) {
  10015. var a = [];
  10016. a.push.apply(a, this.lineIntersection(x, y, x + w, y));
  10017. a.push.apply(a, this.lineIntersection(x + w, y, x + w, y + h));
  10018. a.push.apply(a, this.lineIntersection(x + w, y + h, x, y + h));
  10019. a.push.apply(a, this.lineIntersection(x, y + h, x, y));
  10020. return a;
  10021. };
  10022. /**
  10023. * Calculates all intersections of the given bounding box with this segment. By default this method simply calls `lineIntersection` with each of the four
  10024. * faces of the box; subclasses can override this if they think there's a faster way to compute the entire box at once.
  10025. * @param box Bounding box, in { x:.., y:..., w:..., h:... } format.
  10026. * @returns {Array}
  10027. */
  10028. this.boundingBoxIntersection = function(box) {
  10029. return this.boxIntersection(box.x, box.y, box.w, box.h);
  10030. };
  10031. },
  10032. /*
  10033. Arc Segment. You need to supply:
  10034. r - radius
  10035. cx - center x for the arc
  10036. cy - center y for the arc
  10037. ac - whether the arc is anticlockwise or not. default is clockwise.
  10038. and then either:
  10039. startAngle - startAngle for the arc.
  10040. endAngle - endAngle for the arc.
  10041. or:
  10042. x1 - x for start point
  10043. y1 - y for start point
  10044. x2 - x for end point
  10045. y2 - y for end point
  10046. */
  10047. Arc: function (params) {
  10048. var _super = _jp.Segments.AbstractSegment.apply(this, arguments),
  10049. _calcAngle = function (_x, _y) {
  10050. return _jg.theta([params.cx, params.cy], [_x, _y]);
  10051. },
  10052. _calcAngleForLocation = function (segment, location) {
  10053. if (segment.anticlockwise) {
  10054. var sa = segment.startAngle < segment.endAngle ? segment.startAngle + TWO_PI : segment.startAngle,
  10055. s = Math.abs(sa - segment.endAngle);
  10056. return sa - (s * location);
  10057. }
  10058. else {
  10059. var ea = segment.endAngle < segment.startAngle ? segment.endAngle + TWO_PI : segment.endAngle,
  10060. ss = Math.abs(ea - segment.startAngle);
  10061. return segment.startAngle + (ss * location);
  10062. }
  10063. },
  10064. TWO_PI = 2 * Math.PI;
  10065. this.radius = params.r;
  10066. this.anticlockwise = params.ac;
  10067. this.type = "Arc";
  10068. if (params.startAngle && params.endAngle) {
  10069. this.startAngle = params.startAngle;
  10070. this.endAngle = params.endAngle;
  10071. this.x1 = params.cx + (this.radius * Math.cos(params.startAngle));
  10072. this.y1 = params.cy + (this.radius * Math.sin(params.startAngle));
  10073. this.x2 = params.cx + (this.radius * Math.cos(params.endAngle));
  10074. this.y2 = params.cy + (this.radius * Math.sin(params.endAngle));
  10075. }
  10076. else {
  10077. this.startAngle = _calcAngle(params.x1, params.y1);
  10078. this.endAngle = _calcAngle(params.x2, params.y2);
  10079. this.x1 = params.x1;
  10080. this.y1 = params.y1;
  10081. this.x2 = params.x2;
  10082. this.y2 = params.y2;
  10083. }
  10084. if (this.endAngle < 0) {
  10085. this.endAngle += TWO_PI;
  10086. }
  10087. if (this.startAngle < 0) {
  10088. this.startAngle += TWO_PI;
  10089. }
  10090. // segment is used by vml
  10091. //this.segment = _jg.quadrant([this.x1, this.y1], [this.x2, this.y2]);
  10092. // we now have startAngle and endAngle as positive numbers, meaning the
  10093. // absolute difference (|d|) between them is the sweep (s) of this arc, unless the
  10094. // arc is 'anticlockwise' in which case 's' is given by 2PI - |d|.
  10095. var ea = this.endAngle < this.startAngle ? this.endAngle + TWO_PI : this.endAngle;
  10096. this.sweep = Math.abs(ea - this.startAngle);
  10097. if (this.anticlockwise) {
  10098. this.sweep = TWO_PI - this.sweep;
  10099. }
  10100. var circumference = 2 * Math.PI * this.radius,
  10101. frac = this.sweep / TWO_PI,
  10102. length = circumference * frac;
  10103. this.getLength = function () {
  10104. return length;
  10105. };
  10106. this.getBounds = function () {
  10107. return {
  10108. minX: params.cx - params.r,
  10109. maxX: params.cx + params.r,
  10110. minY: params.cy - params.r,
  10111. maxY: params.cy + params.r
  10112. };
  10113. };
  10114. var VERY_SMALL_VALUE = 0.0000000001,
  10115. gentleRound = function (n) {
  10116. var f = Math.floor(n), r = Math.ceil(n);
  10117. if (n - f < VERY_SMALL_VALUE) {
  10118. return f;
  10119. }
  10120. else if (r - n < VERY_SMALL_VALUE) {
  10121. return r;
  10122. }
  10123. return n;
  10124. };
  10125. /**
  10126. * returns the point on the segment's path that is 'location' along the length of the path, where 'location' is a decimal from
  10127. * 0 to 1 inclusive.
  10128. */
  10129. this.pointOnPath = function (location, absolute) {
  10130. if (location === 0) {
  10131. return { x: this.x1, y: this.y1, theta: this.startAngle };
  10132. }
  10133. else if (location === 1) {
  10134. return { x: this.x2, y: this.y2, theta: this.endAngle };
  10135. }
  10136. if (absolute) {
  10137. location = location / length;
  10138. }
  10139. var angle = _calcAngleForLocation(this, location),
  10140. _x = params.cx + (params.r * Math.cos(angle)),
  10141. _y = params.cy + (params.r * Math.sin(angle));
  10142. return { x: gentleRound(_x), y: gentleRound(_y), theta: angle };
  10143. };
  10144. /**
  10145. * returns the gradient of the segment at the given point.
  10146. */
  10147. this.gradientAtPoint = function (location, absolute) {
  10148. var p = this.pointOnPath(location, absolute);
  10149. var m = _jg.normal([ params.cx, params.cy ], [p.x, p.y ]);
  10150. if (!this.anticlockwise && (m === Infinity || m === -Infinity)) {
  10151. m *= -1;
  10152. }
  10153. return m;
  10154. };
  10155. this.pointAlongPathFrom = function (location, distance, absolute) {
  10156. var p = this.pointOnPath(location, absolute),
  10157. arcSpan = distance / circumference * 2 * Math.PI,
  10158. dir = this.anticlockwise ? -1 : 1,
  10159. startAngle = p.theta + (dir * arcSpan),
  10160. startX = params.cx + (this.radius * Math.cos(startAngle)),
  10161. startY = params.cy + (this.radius * Math.sin(startAngle));
  10162. return {x: startX, y: startY};
  10163. };
  10164. // TODO: lineIntersection
  10165. },
  10166. Bezier: function (params) {
  10167. this.curve = [
  10168. { x: params.x1, y: params.y1},
  10169. { x: params.cp1x, y: params.cp1y },
  10170. { x: params.cp2x, y: params.cp2y },
  10171. { x: params.x2, y: params.y2 }
  10172. ];
  10173. var _isPoint = function(c) {
  10174. return c[0].x === c[1].x && c[0].y === c[1].y;
  10175. };
  10176. var _dist = function(p1, p2 ) {
  10177. return Math.sqrt(Math.pow(p1.x - p2.x, 2) + Math.pow(p1.y - p2.y, 2));
  10178. };
  10179. var _compute = function(loc) {
  10180. var EMPTY_POINT = {x:0, y:0};
  10181. if (loc === 0) {
  10182. return this.curve[0];
  10183. }
  10184. var degree = this.curve.length - 1;
  10185. if (loc === 1) {
  10186. return this.curve[degree];
  10187. }
  10188. var o = this.curve;
  10189. var s = 1 - loc;
  10190. if (degree === 0) {
  10191. return this.curve[0];
  10192. }
  10193. if (degree === 1) {
  10194. return {
  10195. x: s * o[0].x + loc * o[1].x,
  10196. y: s * o[0].y + loc * o[1].y
  10197. };
  10198. }
  10199. if (degree < 4) {
  10200. var l = s * s, h = loc * loc, u = 0, m, g, f;
  10201. if (degree === 2) {
  10202. o = [o[0], o[1], o[2], EMPTY_POINT];
  10203. m = l;
  10204. g = 2 * (s * loc);
  10205. f = h;
  10206. } else if (degree === 3) {
  10207. m = l * s;
  10208. g = 3 * (l * loc);
  10209. f = 3 * (s * h);
  10210. u = loc * h;
  10211. }
  10212. return {
  10213. x: m * o[0].x + g * o[1].x + f * o[2].x + u * o[3].x,
  10214. y: m * o[0].y + g * o[1].y + f * o[2].y + u * o[3].y
  10215. };
  10216. } else {
  10217. return EMPTY_POINT; // not supported.
  10218. }
  10219. }.bind(this);
  10220. var _getLUT = function(steps) {
  10221. var out = [];
  10222. steps--;
  10223. for (var n = 0; n <= steps; n++) {
  10224. out.push(_compute(n / steps));
  10225. }
  10226. return out;
  10227. };
  10228. var _computeLength = function() {
  10229. if (_isPoint(this.curve)) {
  10230. this.length = 0;
  10231. }
  10232. var steps = 16;
  10233. var lut = _getLUT(steps);
  10234. this.length = 0;
  10235. for (var i = 0; i < steps - 1; i++) {
  10236. var a = lut[i], b = lut[i + 1];
  10237. this.length += _dist(a, b);
  10238. }
  10239. }.bind(this);
  10240. var _super = _jp.Segments.AbstractSegment.apply(this, arguments);
  10241. // although this is not a strictly rigorous determination of bounds
  10242. // of a bezier curve, it works for the types of curves that this segment
  10243. // type produces.
  10244. this.bounds = {
  10245. minX: Math.min(params.x1, params.x2, params.cp1x, params.cp2x),
  10246. minY: Math.min(params.y1, params.y2, params.cp1y, params.cp2y),
  10247. maxX: Math.max(params.x1, params.x2, params.cp1x, params.cp2x),
  10248. maxY: Math.max(params.y1, params.y2, params.cp1y, params.cp2y)
  10249. };
  10250. this.type = "Bezier";
  10251. _computeLength();
  10252. var _translateLocation = function (_curve, location, absolute) {
  10253. if (absolute) {
  10254. location = root.jsBezier.locationAlongCurveFrom(_curve, location > 0 ? 0 : 1, location);
  10255. }
  10256. return location;
  10257. };
  10258. /**
  10259. * returns the point on the segment's path that is 'location' along the length of the path, where 'location' is a decimal from
  10260. * 0 to 1 inclusive.
  10261. */
  10262. this.pointOnPath = function (location, absolute) {
  10263. location = _translateLocation(this.curve, location, absolute);
  10264. return root.jsBezier.pointOnCurve(this.curve, location);
  10265. };
  10266. /**
  10267. * returns the gradient of the segment at the given point.
  10268. */
  10269. this.gradientAtPoint = function (location, absolute) {
  10270. location = _translateLocation(this.curve, location, absolute);
  10271. return root.jsBezier.gradientAtPoint(this.curve, location);
  10272. };
  10273. this.pointAlongPathFrom = function (location, distance, absolute) {
  10274. location = _translateLocation(this.curve, location, absolute);
  10275. return root.jsBezier.pointAlongCurveFrom(this.curve, location, distance);
  10276. };
  10277. this.getLength = function () {
  10278. return this.length;
  10279. };
  10280. this.getBounds = function () {
  10281. return this.bounds;
  10282. };
  10283. this.findClosestPointOnPath = function (x, y) {
  10284. var p = root.jsBezier.nearestPointOnCurve({x:x,y:y}, this.curve);
  10285. return {
  10286. d:Math.sqrt(Math.pow(p.point.x - x, 2) + Math.pow(p.point.y - y, 2)),
  10287. x:p.point.x,
  10288. y:p.point.y,
  10289. l:1 - p.location,
  10290. s:this
  10291. };
  10292. };
  10293. this.lineIntersection = function(x1, y1, x2, y2) {
  10294. return root.jsBezier.lineIntersection(x1, y1, x2, y2, this.curve);
  10295. };
  10296. }
  10297. };
  10298. _jp.SegmentRenderer = {
  10299. getPath: function (segment, isFirstSegment) {
  10300. return ({
  10301. "Straight": function (isFirstSegment) {
  10302. var d = segment.getCoordinates();
  10303. return (isFirstSegment ? "M " + d.x1 + " " + d.y1 + " " : "") + "L " + d.x2 + " " + d.y2;
  10304. },
  10305. "Bezier": function (isFirstSegment) {
  10306. var d = segment.params;
  10307. return (isFirstSegment ? "M " + d.x2 + " " + d.y2 + " " : "") +
  10308. "C " + d.cp2x + " " + d.cp2y + " " + d.cp1x + " " + d.cp1y + " " + d.x1 + " " + d.y1;
  10309. },
  10310. "Arc": function (isFirstSegment) {
  10311. var d = segment.params,
  10312. laf = segment.sweep > Math.PI ? 1 : 0,
  10313. sf = segment.anticlockwise ? 0 : 1;
  10314. return (isFirstSegment ? "M" + segment.x1 + " " + segment.y1 + " " : "") + "A " + segment.radius + " " + d.r + " 0 " + laf + "," + sf + " " + segment.x2 + " " + segment.y2;
  10315. }
  10316. })[segment.type](isFirstSegment);
  10317. }
  10318. };
  10319. /*
  10320. Class: UIComponent
  10321. Superclass for Connector and AbstractEndpoint.
  10322. */
  10323. var AbstractComponent = function () {
  10324. this.resetBounds = function () {
  10325. this.bounds = { minX: Infinity, minY: Infinity, maxX: -Infinity, maxY: -Infinity };
  10326. };
  10327. this.resetBounds();
  10328. };
  10329. /*
  10330. * Class: Connector
  10331. * Superclass for all Connectors; here is where Segments are managed. This is exposed on jsPlumb just so it
  10332. * can be accessed from other files. You should not try to instantiate one of these directly.
  10333. *
  10334. * When this class is asked for a pointOnPath, or gradient etc, it must first figure out which segment to dispatch
  10335. * that request to. This is done by keeping track of the total connector length as segments are added, and also
  10336. * their cumulative ratios to the total length. Then when the right segment is found it is a simple case of dispatching
  10337. * the request to it (and adjusting 'location' so that it is relative to the beginning of that segment.)
  10338. */
  10339. _jp.Connectors.AbstractConnector = function (params) {
  10340. AbstractComponent.apply(this, arguments);
  10341. var segments = [],
  10342. totalLength = 0,
  10343. segmentProportions = [],
  10344. segmentProportionalLengths = [],
  10345. stub = params.stub || 0,
  10346. sourceStub = _ju.isArray(stub) ? stub[0] : stub,
  10347. targetStub = _ju.isArray(stub) ? stub[1] : stub,
  10348. gap = params.gap || 0,
  10349. sourceGap = _ju.isArray(gap) ? gap[0] : gap,
  10350. targetGap = _ju.isArray(gap) ? gap[1] : gap,
  10351. userProvidedSegments = null,
  10352. paintInfo = null;
  10353. this.getPathData = function() {
  10354. var p = "";
  10355. for (var i = 0; i < segments.length; i++) {
  10356. p += _jp.SegmentRenderer.getPath(segments[i], i === 0);
  10357. p += " ";
  10358. }
  10359. return p;
  10360. };
  10361. /**
  10362. * Function: findSegmentForPoint
  10363. * Returns the segment that is closest to the given [x,y],
  10364. * null if nothing found. This function returns a JS
  10365. * object with:
  10366. *
  10367. * d - distance from segment
  10368. * l - proportional location in segment
  10369. * x - x point on the segment
  10370. * y - y point on the segment
  10371. * s - the segment itself.
  10372. * connectorLocation - the location on the connector of the point, expressed as a decimal between 0 and 1 inclusive.
  10373. */
  10374. this.findSegmentForPoint = function (x, y) {
  10375. var out = { d: Infinity, s: null, x: null, y: null, l: null };
  10376. for (var i = 0; i < segments.length; i++) {
  10377. var _s = segments[i].findClosestPointOnPath(x, y);
  10378. if (_s.d < out.d) {
  10379. out.d = _s.d;
  10380. out.l = _s.l;
  10381. out.x = _s.x;
  10382. out.y = _s.y;
  10383. out.s = segments[i];
  10384. out.x1 = _s.x1;
  10385. out.x2 = _s.x2;
  10386. out.y1 = _s.y1;
  10387. out.y2 = _s.y2;
  10388. out.index = i;
  10389. out.connectorLocation = segmentProportions[i][0] + (_s.l * (segmentProportions[i][1] - segmentProportions[i][0]));
  10390. }
  10391. }
  10392. return out;
  10393. };
  10394. this.lineIntersection = function(x1, y1, x2, y2) {
  10395. var out = [];
  10396. for (var i = 0; i < segments.length; i++) {
  10397. out.push.apply(out, segments[i].lineIntersection(x1, y1, x2, y2));
  10398. }
  10399. return out;
  10400. };
  10401. this.boxIntersection = function(x, y, w, h) {
  10402. var out = [];
  10403. for (var i = 0; i < segments.length; i++) {
  10404. out.push.apply(out, segments[i].boxIntersection(x, y, w, h));
  10405. }
  10406. return out;
  10407. };
  10408. this.boundingBoxIntersection = function(box) {
  10409. var out = [];
  10410. for (var i = 0; i < segments.length; i++) {
  10411. out.push.apply(out, segments[i].boundingBoxIntersection(box));
  10412. }
  10413. return out;
  10414. };
  10415. var _updateSegmentProportions = function () {
  10416. var curLoc = 0;
  10417. for (var i = 0; i < segments.length; i++) {
  10418. var sl = segments[i].getLength();
  10419. segmentProportionalLengths[i] = sl / totalLength;
  10420. segmentProportions[i] = [curLoc, (curLoc += (sl / totalLength)) ];
  10421. }
  10422. },
  10423. /**
  10424. * returns [segment, proportion of travel in segment, segment index] for the segment
  10425. * that contains the point which is 'location' distance along the entire path, where
  10426. * 'location' is a decimal between 0 and 1 inclusive. in this connector type, paths
  10427. * are made up of a list of segments, each of which contributes some fraction to
  10428. * the total length.
  10429. * From 1.3.10 this also supports the 'absolute' property, which lets us specify a location
  10430. * as the absolute distance in pixels, rather than a proportion of the total path.
  10431. */
  10432. _findSegmentForLocation = function (location, absolute) {
  10433. var idx, i, inSegmentProportion;
  10434. if (absolute) {
  10435. location = location > 0 ? location / totalLength : (totalLength + location) / totalLength;
  10436. }
  10437. // if location 1 we know its the last segment
  10438. if (location === 1) {
  10439. idx = segments.length - 1;
  10440. inSegmentProportion = 1;
  10441. } else if (location === 0) {
  10442. // if location 0 we know its the first segment
  10443. inSegmentProportion = 0;
  10444. idx = 0;
  10445. } else {
  10446. // if location >= 0.5, traverse backwards (of course not exact, who knows the segment proportions. but
  10447. // an educated guess at least)
  10448. if (location >= 0.5) {
  10449. idx = 0;
  10450. inSegmentProportion = 0;
  10451. for (i = segmentProportions.length - 1; i > -1; i--) {
  10452. if (segmentProportions[i][1] >= location && segmentProportions[i][0] <= location) {
  10453. idx = i;
  10454. inSegmentProportion = (location - segmentProportions[i][0]) / segmentProportionalLengths[i];
  10455. break;
  10456. }
  10457. }
  10458. } else {
  10459. idx = segmentProportions.length - 1;
  10460. inSegmentProportion = 1;
  10461. for (i = 0; i < segmentProportions.length; i++) {
  10462. if (segmentProportions[i][1] >= location) {
  10463. idx = i;
  10464. inSegmentProportion = (location - segmentProportions[i][0]) / segmentProportionalLengths[i];
  10465. break;
  10466. }
  10467. }
  10468. }
  10469. }
  10470. return { segment: segments[idx], proportion: inSegmentProportion, index: idx };
  10471. },
  10472. _addSegment = function (conn, type, params) {
  10473. if (params.x1 === params.x2 && params.y1 === params.y2) {
  10474. return;
  10475. }
  10476. var s = new _jp.Segments[type](params);
  10477. segments.push(s);
  10478. totalLength += s.getLength();
  10479. conn.updateBounds(s);
  10480. },
  10481. _clearSegments = function () {
  10482. totalLength = segments.length = segmentProportions.length = segmentProportionalLengths.length = 0;
  10483. };
  10484. this.setSegments = function (_segs) {
  10485. userProvidedSegments = [];
  10486. totalLength = 0;
  10487. for (var i = 0; i < _segs.length; i++) {
  10488. userProvidedSegments.push(_segs[i]);
  10489. totalLength += _segs[i].getLength();
  10490. }
  10491. };
  10492. this.getLength = function() {
  10493. return totalLength;
  10494. };
  10495. var _prepareCompute = function (params) {
  10496. this.strokeWidth = params.strokeWidth;
  10497. var segment = _jg.quadrant(params.sourcePos, params.targetPos),
  10498. swapX = params.targetPos[0] < params.sourcePos[0],
  10499. swapY = params.targetPos[1] < params.sourcePos[1],
  10500. lw = params.strokeWidth || 1,
  10501. so = params.sourceEndpoint.anchor.getOrientation(params.sourceEndpoint),
  10502. to = params.targetEndpoint.anchor.getOrientation(params.targetEndpoint),
  10503. x = swapX ? params.targetPos[0] : params.sourcePos[0],
  10504. y = swapY ? params.targetPos[1] : params.sourcePos[1],
  10505. w = Math.abs(params.targetPos[0] - params.sourcePos[0]),
  10506. h = Math.abs(params.targetPos[1] - params.sourcePos[1]);
  10507. // if either anchor does not have an orientation set, we derive one from their relative
  10508. // positions. we fix the axis to be the one in which the two elements are further apart, and
  10509. // point each anchor at the other element. this is also used when dragging a new connection.
  10510. if (so[0] === 0 && so[1] === 0 || to[0] === 0 && to[1] === 0) {
  10511. var index = w > h ? 0 : 1, oIndex = [1, 0][index];
  10512. so = [];
  10513. to = [];
  10514. so[index] = params.sourcePos[index] > params.targetPos[index] ? -1 : 1;
  10515. to[index] = params.sourcePos[index] > params.targetPos[index] ? 1 : -1;
  10516. so[oIndex] = 0;
  10517. to[oIndex] = 0;
  10518. }
  10519. var sx = swapX ? w + (sourceGap * so[0]) : sourceGap * so[0],
  10520. sy = swapY ? h + (sourceGap * so[1]) : sourceGap * so[1],
  10521. tx = swapX ? targetGap * to[0] : w + (targetGap * to[0]),
  10522. ty = swapY ? targetGap * to[1] : h + (targetGap * to[1]),
  10523. oProduct = ((so[0] * to[0]) + (so[1] * to[1]));
  10524. var result = {
  10525. sx: sx, sy: sy, tx: tx, ty: ty, lw: lw,
  10526. xSpan: Math.abs(tx - sx),
  10527. ySpan: Math.abs(ty - sy),
  10528. mx: (sx + tx) / 2,
  10529. my: (sy + ty) / 2,
  10530. so: so, to: to, x: x, y: y, w: w, h: h,
  10531. segment: segment,
  10532. startStubX: sx + (so[0] * sourceStub),
  10533. startStubY: sy + (so[1] * sourceStub),
  10534. endStubX: tx + (to[0] * targetStub),
  10535. endStubY: ty + (to[1] * targetStub),
  10536. isXGreaterThanStubTimes2: Math.abs(sx - tx) > (sourceStub + targetStub),
  10537. isYGreaterThanStubTimes2: Math.abs(sy - ty) > (sourceStub + targetStub),
  10538. opposite: oProduct === -1,
  10539. perpendicular: oProduct === 0,
  10540. orthogonal: oProduct === 1,
  10541. sourceAxis: so[0] === 0 ? "y" : "x",
  10542. points: [x, y, w, h, sx, sy, tx, ty ],
  10543. stubs:[sourceStub, targetStub]
  10544. };
  10545. result.anchorOrientation = result.opposite ? "opposite" : result.orthogonal ? "orthogonal" : "perpendicular";
  10546. return result;
  10547. };
  10548. this.getSegments = function () {
  10549. return segments;
  10550. };
  10551. this.updateBounds = function (segment) {
  10552. var segBounds = segment.getBounds();
  10553. this.bounds.minX = Math.min(this.bounds.minX, segBounds.minX);
  10554. this.bounds.maxX = Math.max(this.bounds.maxX, segBounds.maxX);
  10555. this.bounds.minY = Math.min(this.bounds.minY, segBounds.minY);
  10556. this.bounds.maxY = Math.max(this.bounds.maxY, segBounds.maxY);
  10557. };
  10558. var dumpSegmentsToConsole = function () {
  10559. console.log("SEGMENTS:");
  10560. for (var i = 0; i < segments.length; i++) {
  10561. console.log(segments[i].type, segments[i].getLength(), segmentProportions[i]);
  10562. }
  10563. };
  10564. this.pointOnPath = function (location, absolute) {
  10565. var seg = _findSegmentForLocation(location, absolute);
  10566. return seg.segment && seg.segment.pointOnPath(seg.proportion, false) || [0, 0];
  10567. };
  10568. this.gradientAtPoint = function (location, absolute) {
  10569. var seg = _findSegmentForLocation(location, absolute);
  10570. return seg.segment && seg.segment.gradientAtPoint(seg.proportion, false) || 0;
  10571. };
  10572. this.pointAlongPathFrom = function (location, distance, absolute) {
  10573. var seg = _findSegmentForLocation(location, absolute);
  10574. // TODO what happens if this crosses to the next segment?
  10575. return seg.segment && seg.segment.pointAlongPathFrom(seg.proportion, distance, false) || [0, 0];
  10576. };
  10577. this.compute = function (params) {
  10578. paintInfo = _prepareCompute.call(this, params);
  10579. _clearSegments();
  10580. this._compute(paintInfo, params);
  10581. this.x = paintInfo.points[0];
  10582. this.y = paintInfo.points[1];
  10583. this.w = paintInfo.points[2];
  10584. this.h = paintInfo.points[3];
  10585. this.segment = paintInfo.segment;
  10586. _updateSegmentProportions();
  10587. };
  10588. return {
  10589. addSegment: _addSegment,
  10590. prepareCompute: _prepareCompute,
  10591. sourceStub: sourceStub,
  10592. targetStub: targetStub,
  10593. maxStub: Math.max(sourceStub, targetStub),
  10594. sourceGap: sourceGap,
  10595. targetGap: targetGap,
  10596. maxGap: Math.max(sourceGap, targetGap)
  10597. };
  10598. };
  10599. _ju.extend(_jp.Connectors.AbstractConnector, AbstractComponent);
  10600. // ********************************* END OF CONNECTOR TYPES *******************************************************************
  10601. // ********************************* ENDPOINT TYPES *******************************************************************
  10602. _jp.Endpoints.AbstractEndpoint = function (params) {
  10603. AbstractComponent.apply(this, arguments);
  10604. var compute = this.compute = function (anchorPoint, orientation, endpointStyle, connectorPaintStyle) {
  10605. var out = this._compute.apply(this, arguments);
  10606. this.x = out[0];
  10607. this.y = out[1];
  10608. this.w = out[2];
  10609. this.h = out[3];
  10610. this.bounds.minX = this.x;
  10611. this.bounds.minY = this.y;
  10612. this.bounds.maxX = this.x + this.w;
  10613. this.bounds.maxY = this.y + this.h;
  10614. return out;
  10615. };
  10616. return {
  10617. compute: compute,
  10618. cssClass: params.cssClass
  10619. };
  10620. };
  10621. _ju.extend(_jp.Endpoints.AbstractEndpoint, AbstractComponent);
  10622. /**
  10623. * Class: Endpoints.Dot
  10624. * A round endpoint, with default radius 10 pixels.
  10625. */
  10626. /**
  10627. * Function: Constructor
  10628. *
  10629. * Parameters:
  10630. *
  10631. * radius - radius of the endpoint. defaults to 10 pixels.
  10632. */
  10633. _jp.Endpoints.Dot = function (params) {
  10634. this.type = "Dot";
  10635. var _super = _jp.Endpoints.AbstractEndpoint.apply(this, arguments);
  10636. params = params || {};
  10637. this.radius = params.radius || 10;
  10638. this.defaultOffset = 0.5 * this.radius;
  10639. this.defaultInnerRadius = this.radius / 3;
  10640. this._compute = function (anchorPoint, orientation, endpointStyle, connectorPaintStyle) {
  10641. this.radius = endpointStyle.radius || this.radius;
  10642. var x = anchorPoint[0] - this.radius,
  10643. y = anchorPoint[1] - this.radius,
  10644. w = this.radius * 2,
  10645. h = this.radius * 2;
  10646. if (endpointStyle.stroke) {
  10647. var lw = endpointStyle.strokeWidth || 1;
  10648. x -= lw;
  10649. y -= lw;
  10650. w += (lw * 2);
  10651. h += (lw * 2);
  10652. }
  10653. return [ x, y, w, h, this.radius ];
  10654. };
  10655. };
  10656. _ju.extend(_jp.Endpoints.Dot, _jp.Endpoints.AbstractEndpoint);
  10657. _jp.Endpoints.Rectangle = function (params) {
  10658. this.type = "Rectangle";
  10659. var _super = _jp.Endpoints.AbstractEndpoint.apply(this, arguments);
  10660. params = params || {};
  10661. this.width = params.width || 20;
  10662. this.height = params.height || 20;
  10663. this._compute = function (anchorPoint, orientation, endpointStyle, connectorPaintStyle) {
  10664. var width = endpointStyle.width || this.width,
  10665. height = endpointStyle.height || this.height,
  10666. x = anchorPoint[0] - (width / 2),
  10667. y = anchorPoint[1] - (height / 2);
  10668. return [ x, y, width, height];
  10669. };
  10670. };
  10671. _ju.extend(_jp.Endpoints.Rectangle, _jp.Endpoints.AbstractEndpoint);
  10672. var DOMElementEndpoint = function (params) {
  10673. _jp.jsPlumbUIComponent.apply(this, arguments);
  10674. this._jsPlumb.displayElements = [];
  10675. };
  10676. _ju.extend(DOMElementEndpoint, _jp.jsPlumbUIComponent, {
  10677. getDisplayElements: function () {
  10678. return this._jsPlumb.displayElements;
  10679. },
  10680. appendDisplayElement: function (el) {
  10681. this._jsPlumb.displayElements.push(el);
  10682. }
  10683. });
  10684. /**
  10685. * Class: Endpoints.Image
  10686. * Draws an image as the Endpoint.
  10687. */
  10688. /**
  10689. * Function: Constructor
  10690. *
  10691. * Parameters:
  10692. *
  10693. * src - location of the image to use.
  10694. TODO: multiple references to self. not sure quite how to get rid of them entirely. perhaps self = null in the cleanup
  10695. function will suffice
  10696. TODO this class still might leak memory.
  10697. */
  10698. _jp.Endpoints.Image = function (params) {
  10699. this.type = "Image";
  10700. DOMElementEndpoint.apply(this, arguments);
  10701. _jp.Endpoints.AbstractEndpoint.apply(this, arguments);
  10702. var _onload = params.onload,
  10703. src = params.src || params.url,
  10704. clazz = params.cssClass ? " " + params.cssClass : "";
  10705. this._jsPlumb.img = new Image();
  10706. this._jsPlumb.ready = false;
  10707. this._jsPlumb.initialized = false;
  10708. this._jsPlumb.deleted = false;
  10709. this._jsPlumb.widthToUse = params.width;
  10710. this._jsPlumb.heightToUse = params.height;
  10711. this._jsPlumb.endpoint = params.endpoint;
  10712. this._jsPlumb.img.onload = function () {
  10713. if (this._jsPlumb != null) {
  10714. this._jsPlumb.ready = true;
  10715. this._jsPlumb.widthToUse = this._jsPlumb.widthToUse || this._jsPlumb.img.width;
  10716. this._jsPlumb.heightToUse = this._jsPlumb.heightToUse || this._jsPlumb.img.height;
  10717. if (_onload) {
  10718. _onload(this);
  10719. }
  10720. }
  10721. }.bind(this);
  10722. /*
  10723. Function: setImage
  10724. Sets the Image to use in this Endpoint.
  10725. Parameters:
  10726. img - may be a URL or an Image object
  10727. onload - optional; a callback to execute once the image has loaded.
  10728. */
  10729. this._jsPlumb.endpoint.setImage = function (_img, onload) {
  10730. var s = _img.constructor === String ? _img : _img.src;
  10731. _onload = onload;
  10732. this._jsPlumb.img.src = s;
  10733. if (this.canvas != null) {
  10734. this.canvas.setAttribute("src", this._jsPlumb.img.src);
  10735. }
  10736. }.bind(this);
  10737. this._jsPlumb.endpoint.setImage(src, _onload);
  10738. this._compute = function (anchorPoint, orientation, endpointStyle, connectorPaintStyle) {
  10739. this.anchorPoint = anchorPoint;
  10740. if (this._jsPlumb.ready) {
  10741. return [anchorPoint[0] - this._jsPlumb.widthToUse / 2, anchorPoint[1] - this._jsPlumb.heightToUse / 2,
  10742. this._jsPlumb.widthToUse, this._jsPlumb.heightToUse];
  10743. }
  10744. else {
  10745. return [0, 0, 0, 0];
  10746. }
  10747. };
  10748. this.canvas = _jp.createElement("img", {
  10749. position:"absolute",
  10750. margin:0,
  10751. padding:0,
  10752. outline:0
  10753. }, this._jsPlumb.instance.endpointClass + clazz);
  10754. if (this._jsPlumb.widthToUse) {
  10755. this.canvas.setAttribute("width", this._jsPlumb.widthToUse);
  10756. }
  10757. if (this._jsPlumb.heightToUse) {
  10758. this.canvas.setAttribute("height", this._jsPlumb.heightToUse);
  10759. }
  10760. this._jsPlumb.instance.appendElement(this.canvas);
  10761. this.actuallyPaint = function (d, style, anchor) {
  10762. if (!this._jsPlumb.deleted) {
  10763. if (!this._jsPlumb.initialized) {
  10764. this.canvas.setAttribute("src", this._jsPlumb.img.src);
  10765. this.appendDisplayElement(this.canvas);
  10766. this._jsPlumb.initialized = true;
  10767. }
  10768. var x = this.anchorPoint[0] - (this._jsPlumb.widthToUse / 2),
  10769. y = this.anchorPoint[1] - (this._jsPlumb.heightToUse / 2);
  10770. _ju.sizeElement(this.canvas, x, y, this._jsPlumb.widthToUse, this._jsPlumb.heightToUse);
  10771. }
  10772. };
  10773. this.paint = function (style, anchor) {
  10774. if (this._jsPlumb != null) { // may have been deleted
  10775. if (this._jsPlumb.ready) {
  10776. this.actuallyPaint(style, anchor);
  10777. }
  10778. else {
  10779. root.setTimeout(function () {
  10780. this.paint(style, anchor);
  10781. }.bind(this), 200);
  10782. }
  10783. }
  10784. };
  10785. };
  10786. _ju.extend(_jp.Endpoints.Image, [ DOMElementEndpoint, _jp.Endpoints.AbstractEndpoint ], {
  10787. cleanup: function (force) {
  10788. if (force) {
  10789. this._jsPlumb.deleted = true;
  10790. if (this.canvas) {
  10791. this.canvas.parentNode.removeChild(this.canvas);
  10792. }
  10793. this.canvas = null;
  10794. }
  10795. }
  10796. });
  10797. /*
  10798. * Class: Endpoints.Blank
  10799. * An Endpoint that paints nothing (visible) on the screen. Supports cssClass and hoverClass parameters like all Endpoints.
  10800. */
  10801. _jp.Endpoints.Blank = function (params) {
  10802. var _super = _jp.Endpoints.AbstractEndpoint.apply(this, arguments);
  10803. this.type = "Blank";
  10804. DOMElementEndpoint.apply(this, arguments);
  10805. this._compute = function (anchorPoint, orientation, endpointStyle, connectorPaintStyle) {
  10806. return [anchorPoint[0], anchorPoint[1], 10, 0];
  10807. };
  10808. var clazz = params.cssClass ? " " + params.cssClass : "";
  10809. this.canvas = _jp.createElement("div", {
  10810. display: "block",
  10811. width: "1px",
  10812. height: "1px",
  10813. background: "transparent",
  10814. position: "absolute"
  10815. }, this._jsPlumb.instance.endpointClass + clazz);
  10816. this._jsPlumb.instance.appendElement(this.canvas);
  10817. this.paint = function (style, anchor) {
  10818. _ju.sizeElement(this.canvas, this.x, this.y, this.w, this.h);
  10819. };
  10820. };
  10821. _ju.extend(_jp.Endpoints.Blank, [_jp.Endpoints.AbstractEndpoint, DOMElementEndpoint], {
  10822. cleanup: function () {
  10823. if (this.canvas && this.canvas.parentNode) {
  10824. this.canvas.parentNode.removeChild(this.canvas);
  10825. }
  10826. }
  10827. });
  10828. /*
  10829. * Class: Endpoints.Triangle
  10830. * A triangular Endpoint.
  10831. */
  10832. /*
  10833. * Function: Constructor
  10834. *
  10835. * Parameters:
  10836. *
  10837. * width width of the triangle's base. defaults to 55 pixels.
  10838. * height height of the triangle from base to apex. defaults to 55 pixels.
  10839. */
  10840. _jp.Endpoints.Triangle = function (params) {
  10841. this.type = "Triangle";
  10842. _jp.Endpoints.AbstractEndpoint.apply(this, arguments);
  10843. var self = this;
  10844. params = params || { };
  10845. params.width = params.width || 55;
  10846. params.height = params.height || 55;
  10847. this.width = params.width;
  10848. this.height = params.height;
  10849. this._compute = function (anchorPoint, orientation, endpointStyle, connectorPaintStyle) {
  10850. var width = endpointStyle.width || self.width,
  10851. height = endpointStyle.height || self.height,
  10852. x = anchorPoint[0] - (width / 2),
  10853. y = anchorPoint[1] - (height / 2);
  10854. return [ x, y, width, height ];
  10855. };
  10856. };
  10857. // ********************************* END OF ENDPOINT TYPES *******************************************************************
  10858. // ********************************* OVERLAY DEFINITIONS ***********************************************************************
  10859. var AbstractOverlay = _jp.Overlays.AbstractOverlay = function (params) {
  10860. this.visible = true;
  10861. this.isAppendedAtTopLevel = true;
  10862. this.component = params.component;
  10863. this.loc = params.location == null ? 0.5 : params.location;
  10864. this.endpointLoc = params.endpointLocation == null ? [ 0.5, 0.5] : params.endpointLocation;
  10865. this.visible = params.visible !== false;
  10866. };
  10867. AbstractOverlay.prototype = {
  10868. cleanup: function (force) {
  10869. if (force) {
  10870. this.component = null;
  10871. this.canvas = null;
  10872. this.endpointLoc = null;
  10873. }
  10874. },
  10875. reattach:function(instance, component) { },
  10876. setVisible: function (val) {
  10877. this.visible = val;
  10878. this.component.repaint();
  10879. },
  10880. isVisible: function () {
  10881. return this.visible;
  10882. },
  10883. hide: function () {
  10884. this.setVisible(false);
  10885. },
  10886. show: function () {
  10887. this.setVisible(true);
  10888. },
  10889. incrementLocation: function (amount) {
  10890. this.loc += amount;
  10891. this.component.repaint();
  10892. },
  10893. setLocation: function (l) {
  10894. this.loc = l;
  10895. this.component.repaint();
  10896. },
  10897. getLocation: function () {
  10898. return this.loc;
  10899. },
  10900. updateFrom:function() { }
  10901. };
  10902. /*
  10903. * Class: Overlays.Arrow
  10904. *
  10905. * An arrow overlay, defined by four points: the head, the two sides of the tail, and a 'foldback' point at some distance along the length
  10906. * of the arrow that lines from each tail point converge into. The foldback point is defined using a decimal that indicates some fraction
  10907. * of the length of the arrow and has a default value of 0.623. A foldback point value of 1 would mean that the arrow had a straight line
  10908. * across the tail.
  10909. */
  10910. /*
  10911. * @constructor
  10912. *
  10913. * @param {Object} params Constructor params.
  10914. * @param {Number} [params.length] Distance in pixels from head to tail baseline. default 20.
  10915. * @param {Number} [params.width] Width in pixels of the tail baseline. default 20.
  10916. * @param {String} [params.fill] Style to use when filling the arrow. defaults to "black".
  10917. * @param {String} [params.stroke] Style to use when stroking the arrow. defaults to null, which means the arrow is not stroked.
  10918. * @param {Number} [params.stroke-width] Line width to use when stroking the arrow. defaults to 1, but only used if stroke is not null.
  10919. * @param {Number} [params.foldback] Distance (as a decimal from 0 to 1 inclusive) along the length of the arrow marking the point the tail points should fold back to. defaults to 0.623.
  10920. * @param {Number} [params.location] Distance (as a decimal from 0 to 1 inclusive) marking where the arrow should sit on the connector. defaults to 0.5.
  10921. * @param {NUmber} [params.direction] Indicates the direction the arrow points in. valid values are -1 and 1; 1 is default.
  10922. */
  10923. _jp.Overlays.Arrow = function (params) {
  10924. this.type = "Arrow";
  10925. AbstractOverlay.apply(this, arguments);
  10926. this.isAppendedAtTopLevel = false;
  10927. params = params || {};
  10928. var self = this;
  10929. this.length = params.length || 20;
  10930. this.width = params.width || 20;
  10931. this.id = params.id;
  10932. this.direction = (params.direction || 1) < 0 ? -1 : 1;
  10933. var paintStyle = params.paintStyle || { "stroke-width": 1 },
  10934. // how far along the arrow the lines folding back in come to. default is 62.3%.
  10935. foldback = params.foldback || 0.623;
  10936. this.computeMaxSize = function () {
  10937. return self.width * 1.5;
  10938. };
  10939. this.elementCreated = function(p, component) {
  10940. this.path = p;
  10941. if (params.events) {
  10942. for (var i in params.events) {
  10943. _jp.on(p, i, params.events[i]);
  10944. }
  10945. }
  10946. };
  10947. this.draw = function (component, currentConnectionPaintStyle) {
  10948. var hxy, mid, txy, tail, cxy;
  10949. if (component.pointAlongPathFrom) {
  10950. if (_ju.isString(this.loc) || this.loc > 1 || this.loc < 0) {
  10951. var l = parseInt(this.loc, 10),
  10952. fromLoc = this.loc < 0 ? 1 : 0;
  10953. hxy = component.pointAlongPathFrom(fromLoc, l, false);
  10954. mid = component.pointAlongPathFrom(fromLoc, l - (this.direction * this.length / 2), false);
  10955. txy = _jg.pointOnLine(hxy, mid, this.length);
  10956. }
  10957. else if (this.loc === 1) {
  10958. hxy = component.pointOnPath(this.loc);
  10959. mid = component.pointAlongPathFrom(this.loc, -(this.length));
  10960. txy = _jg.pointOnLine(hxy, mid, this.length);
  10961. if (this.direction === -1) {
  10962. var _ = txy;
  10963. txy = hxy;
  10964. hxy = _;
  10965. }
  10966. }
  10967. else if (this.loc === 0) {
  10968. txy = component.pointOnPath(this.loc);
  10969. mid = component.pointAlongPathFrom(this.loc, this.length);
  10970. hxy = _jg.pointOnLine(txy, mid, this.length);
  10971. if (this.direction === -1) {
  10972. var __ = txy;
  10973. txy = hxy;
  10974. hxy = __;
  10975. }
  10976. }
  10977. else {
  10978. hxy = component.pointAlongPathFrom(this.loc, this.direction * this.length / 2);
  10979. mid = component.pointOnPath(this.loc);
  10980. txy = _jg.pointOnLine(hxy, mid, this.length);
  10981. }
  10982. tail = _jg.perpendicularLineTo(hxy, txy, this.width);
  10983. cxy = _jg.pointOnLine(hxy, txy, foldback * this.length);
  10984. var d = { hxy: hxy, tail: tail, cxy: cxy },
  10985. stroke = paintStyle.stroke || currentConnectionPaintStyle.stroke,
  10986. fill = paintStyle.fill || currentConnectionPaintStyle.stroke,
  10987. lineWidth = paintStyle.strokeWidth || currentConnectionPaintStyle.strokeWidth;
  10988. return {
  10989. component: component,
  10990. d: d,
  10991. "stroke-width": lineWidth,
  10992. stroke: stroke,
  10993. fill: fill,
  10994. minX: Math.min(hxy.x, tail[0].x, tail[1].x),
  10995. maxX: Math.max(hxy.x, tail[0].x, tail[1].x),
  10996. minY: Math.min(hxy.y, tail[0].y, tail[1].y),
  10997. maxY: Math.max(hxy.y, tail[0].y, tail[1].y)
  10998. };
  10999. }
  11000. else {
  11001. return {component: component, minX: 0, maxX: 0, minY: 0, maxY: 0};
  11002. }
  11003. };
  11004. };
  11005. _ju.extend(_jp.Overlays.Arrow, AbstractOverlay, {
  11006. updateFrom:function(d) {
  11007. this.length = d.length || this.length;
  11008. this.width = d.width|| this.width;
  11009. this.direction = d.direction != null ? d.direction : this.direction;
  11010. this.foldback = d.foldback|| this.foldback;
  11011. },
  11012. cleanup:function() {
  11013. if (this.path && this.path.parentNode) {
  11014. this.path.parentNode.removeChild(this.path);
  11015. }
  11016. }
  11017. });
  11018. /*
  11019. * Class: Overlays.PlainArrow
  11020. *
  11021. * A basic arrow. This is in fact just one instance of the more generic case in which the tail folds back on itself to some
  11022. * point along the length of the arrow: in this case, that foldback point is the full length of the arrow. so it just does
  11023. * a 'call' to Arrow with foldback set appropriately.
  11024. */
  11025. /*
  11026. * Function: Constructor
  11027. * See <Overlays.Arrow> for allowed parameters for this overlay.
  11028. */
  11029. _jp.Overlays.PlainArrow = function (params) {
  11030. params = params || {};
  11031. var p = _jp.extend(params, {foldback: 1});
  11032. _jp.Overlays.Arrow.call(this, p);
  11033. this.type = "PlainArrow";
  11034. };
  11035. _ju.extend(_jp.Overlays.PlainArrow, _jp.Overlays.Arrow);
  11036. /*
  11037. * Class: Overlays.Diamond
  11038. *
  11039. * A diamond. Like PlainArrow, this is a concrete case of the more generic case of the tail points converging on some point...it just
  11040. * happens that in this case, that point is greater than the length of the the arrow.
  11041. *
  11042. * this could probably do with some help with positioning...due to the way it reuses the Arrow paint code, what Arrow thinks is the
  11043. * center is actually 1/4 of the way along for this guy. but we don't have any knowledge of pixels at this point, so we're kind of
  11044. * stuck when it comes to helping out the Arrow class. possibly we could pass in a 'transpose' parameter or something. the value
  11045. * would be -l/4 in this case - move along one quarter of the total length.
  11046. */
  11047. /*
  11048. * Function: Constructor
  11049. * See <Overlays.Arrow> for allowed parameters for this overlay.
  11050. */
  11051. _jp.Overlays.Diamond = function (params) {
  11052. params = params || {};
  11053. var l = params.length || 40,
  11054. p = _jp.extend(params, {length: l / 2, foldback: 2});
  11055. _jp.Overlays.Arrow.call(this, p);
  11056. this.type = "Diamond";
  11057. };
  11058. _ju.extend(_jp.Overlays.Diamond, _jp.Overlays.Arrow);
  11059. var _getDimensions = function (component, forceRefresh) {
  11060. if (component._jsPlumb.cachedDimensions == null || forceRefresh) {
  11061. component._jsPlumb.cachedDimensions = component.getDimensions();
  11062. }
  11063. return component._jsPlumb.cachedDimensions;
  11064. };
  11065. // abstract superclass for overlays that add an element to the DOM.
  11066. var AbstractDOMOverlay = function (params) {
  11067. _jp.jsPlumbUIComponent.apply(this, arguments);
  11068. AbstractOverlay.apply(this, arguments);
  11069. // hand off fired events to associated component.
  11070. var _f = this.fire;
  11071. this.fire = function () {
  11072. _f.apply(this, arguments);
  11073. if (this.component) {
  11074. this.component.fire.apply(this.component, arguments);
  11075. }
  11076. };
  11077. this.detached=false;
  11078. this.id = params.id;
  11079. this._jsPlumb.div = null;
  11080. this._jsPlumb.initialised = false;
  11081. this._jsPlumb.component = params.component;
  11082. this._jsPlumb.cachedDimensions = null;
  11083. this._jsPlumb.create = params.create;
  11084. this._jsPlumb.initiallyInvisible = params.visible === false;
  11085. this.getElement = function () {
  11086. if (this._jsPlumb.div == null) {
  11087. var div = this._jsPlumb.div = _jp.getElement(this._jsPlumb.create(this._jsPlumb.component));
  11088. div.style.position = "absolute";
  11089. jsPlumb.addClass(div, this._jsPlumb.instance.overlayClass + " " +
  11090. (this.cssClass ? this.cssClass :
  11091. params.cssClass ? params.cssClass : ""));
  11092. this._jsPlumb.instance.appendElement(div);
  11093. this._jsPlumb.instance.getId(div);
  11094. this.canvas = div;
  11095. // in IE the top left corner is what it placed at the desired location. This will not
  11096. // be fixed. IE8 is not going to be supported for much longer.
  11097. var ts = "translate(-50%, -50%)";
  11098. div.style.webkitTransform = ts;
  11099. div.style.mozTransform = ts;
  11100. div.style.msTransform = ts;
  11101. div.style.oTransform = ts;
  11102. div.style.transform = ts;
  11103. // write the related component into the created element
  11104. div._jsPlumb = this;
  11105. if (params.visible === false) {
  11106. div.style.display = "none";
  11107. }
  11108. }
  11109. return this._jsPlumb.div;
  11110. };
  11111. this.draw = function (component, currentConnectionPaintStyle, absolutePosition) {
  11112. var td = _getDimensions(this);
  11113. if (td != null && td.length === 2) {
  11114. var cxy = { x: 0, y: 0 };
  11115. // absolutePosition would have been set by a call to connection.setAbsoluteOverlayPosition.
  11116. if (absolutePosition) {
  11117. cxy = { x: absolutePosition[0], y: absolutePosition[1] };
  11118. }
  11119. else if (component.pointOnPath) {
  11120. var loc = this.loc, absolute = false;
  11121. if (_ju.isString(this.loc) || this.loc < 0 || this.loc > 1) {
  11122. loc = parseInt(this.loc, 10);
  11123. absolute = true;
  11124. }
  11125. cxy = component.pointOnPath(loc, absolute); // a connection
  11126. }
  11127. else {
  11128. var locToUse = this.loc.constructor === Array ? this.loc : this.endpointLoc;
  11129. cxy = { x: locToUse[0] * component.w,
  11130. y: locToUse[1] * component.h };
  11131. }
  11132. var minx = cxy.x - (td[0] / 2),
  11133. miny = cxy.y - (td[1] / 2);
  11134. return {
  11135. component: component,
  11136. d: { minx: minx, miny: miny, td: td, cxy: cxy },
  11137. minX: minx,
  11138. maxX: minx + td[0],
  11139. minY: miny,
  11140. maxY: miny + td[1]
  11141. };
  11142. }
  11143. else {
  11144. return {minX: 0, maxX: 0, minY: 0, maxY: 0};
  11145. }
  11146. };
  11147. };
  11148. _ju.extend(AbstractDOMOverlay, [_jp.jsPlumbUIComponent, AbstractOverlay], {
  11149. getDimensions: function () {
  11150. return [1,1];
  11151. },
  11152. setVisible: function (state) {
  11153. if (this._jsPlumb.div) {
  11154. this._jsPlumb.div.style.display = state ? "block" : "none";
  11155. // if initially invisible, dimensions are 0,0 and never get updated
  11156. if (state && this._jsPlumb.initiallyInvisible) {
  11157. _getDimensions(this, true);
  11158. this.component.repaint();
  11159. this._jsPlumb.initiallyInvisible = false;
  11160. }
  11161. }
  11162. },
  11163. /*
  11164. * Function: clearCachedDimensions
  11165. * Clears the cached dimensions for the label. As a performance enhancement, label dimensions are
  11166. * cached from 1.3.12 onwards. The cache is cleared when you change the label text, of course, but
  11167. * there are other reasons why the text dimensions might change - if you make a change through CSS, for
  11168. * example, you might change the font size. in that case you should explicitly call this method.
  11169. */
  11170. clearCachedDimensions: function () {
  11171. this._jsPlumb.cachedDimensions = null;
  11172. },
  11173. cleanup: function (force) {
  11174. if (force) {
  11175. if (this._jsPlumb.div != null) {
  11176. this._jsPlumb.div._jsPlumb = null;
  11177. this._jsPlumb.instance.removeElement(this._jsPlumb.div);
  11178. }
  11179. }
  11180. else {
  11181. // if not a forced cleanup, just detach child from parent for now.
  11182. if (this._jsPlumb && this._jsPlumb.div && this._jsPlumb.div.parentNode) {
  11183. this._jsPlumb.div.parentNode.removeChild(this._jsPlumb.div);
  11184. }
  11185. this.detached = true;
  11186. }
  11187. },
  11188. reattach:function(instance, component) {
  11189. if (this._jsPlumb.div != null) {
  11190. instance.getContainer().appendChild(this._jsPlumb.div);
  11191. }
  11192. this.detached = false;
  11193. },
  11194. computeMaxSize: function () {
  11195. var td = _getDimensions(this);
  11196. return Math.max(td[0], td[1]);
  11197. },
  11198. paint: function (p, containerExtents) {
  11199. if (!this._jsPlumb.initialised) {
  11200. this.getElement();
  11201. p.component.appendDisplayElement(this._jsPlumb.div);
  11202. this._jsPlumb.initialised = true;
  11203. if (this.detached) {
  11204. this._jsPlumb.div.parentNode.removeChild(this._jsPlumb.div);
  11205. }
  11206. }
  11207. this._jsPlumb.div.style.left = (p.component.x + p.d.minx) + "px";
  11208. this._jsPlumb.div.style.top = (p.component.y + p.d.miny) + "px";
  11209. }
  11210. });
  11211. /*
  11212. * Class: Overlays.Custom
  11213. * A Custom overlay. You supply a 'create' function which returns some DOM element, and jsPlumb positions it.
  11214. * The 'create' function is passed a Connection or Endpoint.
  11215. */
  11216. /*
  11217. * Function: Constructor
  11218. *
  11219. * Parameters:
  11220. * create - function for jsPlumb to call that returns a DOM element.
  11221. * location - distance (as a decimal from 0 to 1 inclusive) marking where the label should sit on the connector. defaults to 0.5.
  11222. * id - optional id to use for later retrieval of this overlay.
  11223. *
  11224. */
  11225. _jp.Overlays.Custom = function (params) {
  11226. this.type = "Custom";
  11227. AbstractDOMOverlay.apply(this, arguments);
  11228. };
  11229. _ju.extend(_jp.Overlays.Custom, AbstractDOMOverlay);
  11230. _jp.Overlays.GuideLines = function () {
  11231. var self = this;
  11232. self.length = 50;
  11233. self.strokeWidth = 5;
  11234. this.type = "GuideLines";
  11235. AbstractOverlay.apply(this, arguments);
  11236. _jp.jsPlumbUIComponent.apply(this, arguments);
  11237. this.draw = function (connector, currentConnectionPaintStyle) {
  11238. var head = connector.pointAlongPathFrom(self.loc, self.length / 2),
  11239. mid = connector.pointOnPath(self.loc),
  11240. tail = _jg.pointOnLine(head, mid, self.length),
  11241. tailLine = _jg.perpendicularLineTo(head, tail, 40),
  11242. headLine = _jg.perpendicularLineTo(tail, head, 20);
  11243. return {
  11244. connector: connector,
  11245. head: head,
  11246. tail: tail,
  11247. headLine: headLine,
  11248. tailLine: tailLine,
  11249. minX: Math.min(head.x, tail.x, headLine[0].x, headLine[1].x),
  11250. minY: Math.min(head.y, tail.y, headLine[0].y, headLine[1].y),
  11251. maxX: Math.max(head.x, tail.x, headLine[0].x, headLine[1].x),
  11252. maxY: Math.max(head.y, tail.y, headLine[0].y, headLine[1].y)
  11253. };
  11254. };
  11255. // this.cleanup = function() { }; // nothing to clean up for GuideLines
  11256. };
  11257. /*
  11258. * Class: Overlays.Label
  11259. */
  11260. /*
  11261. * Function: Constructor
  11262. *
  11263. * Parameters:
  11264. * cssClass - optional css class string to append to css class. This string is appended "as-is", so you can of course have multiple classes
  11265. * defined. This parameter is preferred to using labelStyle, borderWidth and borderStyle.
  11266. * label - the label to paint. May be a string or a function that returns a string. Nothing will be painted if your label is null or your
  11267. * label function returns null. empty strings _will_ be painted.
  11268. * location - distance (as a decimal from 0 to 1 inclusive) marking where the label should sit on the connector. defaults to 0.5.
  11269. * id - optional id to use for later retrieval of this overlay.
  11270. *
  11271. *
  11272. */
  11273. _jp.Overlays.Label = function (params) {
  11274. this.labelStyle = params.labelStyle;
  11275. var labelWidth = null, labelHeight = null, labelText = null, labelPadding = null;
  11276. this.cssClass = this.labelStyle != null ? this.labelStyle.cssClass : null;
  11277. var p = _jp.extend({
  11278. create: function () {
  11279. return _jp.createElement("div");
  11280. }}, params);
  11281. _jp.Overlays.Custom.call(this, p);
  11282. this.type = "Label";
  11283. this.label = params.label || "";
  11284. this.labelText = null;
  11285. if (this.labelStyle) {
  11286. var el = this.getElement();
  11287. this.labelStyle.font = this.labelStyle.font || "12px sans-serif";
  11288. el.style.font = this.labelStyle.font;
  11289. el.style.color = this.labelStyle.color || "black";
  11290. if (this.labelStyle.fill) {
  11291. el.style.background = this.labelStyle.fill;
  11292. }
  11293. if (this.labelStyle.borderWidth > 0) {
  11294. var dStyle = this.labelStyle.borderStyle ? this.labelStyle.borderStyle : "black";
  11295. el.style.border = this.labelStyle.borderWidth + "px solid " + dStyle;
  11296. }
  11297. if (this.labelStyle.padding) {
  11298. el.style.padding = this.labelStyle.padding;
  11299. }
  11300. }
  11301. };
  11302. _ju.extend(_jp.Overlays.Label, _jp.Overlays.Custom, {
  11303. cleanup: function (force) {
  11304. if (force) {
  11305. this.div = null;
  11306. this.label = null;
  11307. this.labelText = null;
  11308. this.cssClass = null;
  11309. this.labelStyle = null;
  11310. }
  11311. },
  11312. getLabel: function () {
  11313. return this.label;
  11314. },
  11315. /*
  11316. * Function: setLabel
  11317. * sets the label's, um, label. you would think i'd call this function
  11318. * 'setText', but you can pass either a Function or a String to this, so
  11319. * it makes more sense as 'setLabel'. This uses innerHTML on the label div, so keep
  11320. * that in mind if you need escaped HTML.
  11321. */
  11322. setLabel: function (l) {
  11323. this.label = l;
  11324. this.labelText = null;
  11325. this.clearCachedDimensions();
  11326. this.update();
  11327. this.component.repaint();
  11328. },
  11329. getDimensions: function () {
  11330. this.update();
  11331. return AbstractDOMOverlay.prototype.getDimensions.apply(this, arguments);
  11332. },
  11333. update: function () {
  11334. if (typeof this.label === "function") {
  11335. var lt = this.label(this);
  11336. this.getElement().innerHTML = lt.replace(/\r\n/g, "<br/>");
  11337. }
  11338. else {
  11339. if (this.labelText == null) {
  11340. this.labelText = this.label;
  11341. this.getElement().innerHTML = this.labelText.replace(/\r\n/g, "<br/>");
  11342. }
  11343. }
  11344. },
  11345. updateFrom:function(d) {
  11346. if(d.label != null){
  11347. this.setLabel(d.label);
  11348. }
  11349. }
  11350. });
  11351. // ********************************* END OF OVERLAY DEFINITIONS ***********************************************************************
  11352. }).call(typeof window !== 'undefined' ? window : this);
  11353. /*
  11354. * Copyright (c) 2010 - 2020 jsPlumb (hello@jsplumbtoolkit.com)
  11355. *
  11356. * https://jsplumbtoolkit.com
  11357. * https://github.com/jsplumb/jsplumb
  11358. *
  11359. * Dual licensed under the MIT and GPL2 licenses.
  11360. */
  11361. ;(function() {
  11362. "use strict";
  11363. var root = this,
  11364. _ju = root.jsPlumbUtil,
  11365. _jpi = root.jsPlumbInstance;
  11366. var GROUP_COLLAPSED_CLASS = "jtk-group-collapsed";
  11367. var GROUP_EXPANDED_CLASS = "jtk-group-expanded";
  11368. var GROUP_CONTAINER_SELECTOR = "[jtk-group-content]";
  11369. var ELEMENT_DRAGGABLE_EVENT = "elementDraggable";
  11370. var STOP = "stop";
  11371. var REVERT = "revert";
  11372. var GROUP_MANAGER = "_groupManager";
  11373. var GROUP = "_jsPlumbGroup";
  11374. var GROUP_DRAG_SCOPE = "_jsPlumbGroupDrag";
  11375. var EVT_CHILD_ADDED = "group:addMember";
  11376. var EVT_CHILD_REMOVED = "group:removeMember";
  11377. var EVT_GROUP_ADDED = "group:add";
  11378. var EVT_GROUP_REMOVED = "group:remove";
  11379. var EVT_EXPAND = "group:expand";
  11380. var EVT_COLLAPSE = "group:collapse";
  11381. var EVT_GROUP_DRAG_STOP = "groupDragStop";
  11382. var EVT_CONNECTION_MOVED = "connectionMoved";
  11383. var EVT_INTERNAL_CONNECTION_DETACHED = "internal.connectionDetached";
  11384. var CMD_REMOVE_ALL = "removeAll";
  11385. var CMD_ORPHAN_ALL = "orphanAll";
  11386. var CMD_SHOW = "show";
  11387. var CMD_HIDE = "hide";
  11388. var GroupManager = function(_jsPlumb) {
  11389. var _managedGroups = {}, _connectionSourceMap = {}, _connectionTargetMap = {}, self = this;
  11390. // function findGroupFor(el) {
  11391. // var c = _jsPlumb.getContainer();
  11392. // var abort = false, g = null, child = null;
  11393. // while (!abort) {
  11394. // if (el == null || el === c) {
  11395. // abort = true;
  11396. // } else {
  11397. // if (el[GROUP]) {
  11398. // g = el[GROUP];
  11399. // child = el;
  11400. // abort = true;
  11401. // } else {
  11402. // el = el.parentNode;
  11403. // }
  11404. // }
  11405. // }
  11406. // return g;
  11407. // }
  11408. function isDescendant(el, parentEl) {
  11409. var c = _jsPlumb.getContainer();
  11410. var abort = false, g = null, child = null;
  11411. while (!abort) {
  11412. if (el == null || el === c) {
  11413. return false;
  11414. } else {
  11415. if (el === parentEl) {
  11416. return true;
  11417. } else {
  11418. el = el.parentNode;
  11419. }
  11420. }
  11421. }
  11422. }
  11423. _jsPlumb.bind("connection", function(p) {
  11424. var sourceGroup = _jsPlumb.getGroupFor(p.source);
  11425. var targetGroup = _jsPlumb.getGroupFor(p.target);
  11426. if (sourceGroup != null && targetGroup != null && sourceGroup === targetGroup) {
  11427. _connectionSourceMap[p.connection.id] = sourceGroup;
  11428. _connectionTargetMap[p.connection.id] = sourceGroup;
  11429. }
  11430. else {
  11431. if (sourceGroup != null) {
  11432. _ju.suggest(sourceGroup.connections.source, p.connection);
  11433. _connectionSourceMap[p.connection.id] = sourceGroup;
  11434. }
  11435. if (targetGroup != null) {
  11436. _ju.suggest(targetGroup.connections.target, p.connection);
  11437. _connectionTargetMap[p.connection.id] = targetGroup;
  11438. }
  11439. }
  11440. });
  11441. function _cleanupDetachedConnection(conn) {
  11442. delete conn.proxies;
  11443. var group = _connectionSourceMap[conn.id], f;
  11444. if (group != null) {
  11445. f = function(c) { return c.id === conn.id; };
  11446. _ju.removeWithFunction(group.connections.source, f);
  11447. _ju.removeWithFunction(group.connections.target, f);
  11448. delete _connectionSourceMap[conn.id];
  11449. }
  11450. group = _connectionTargetMap[conn.id];
  11451. if (group != null) {
  11452. f = function(c) { return c.id === conn.id; };
  11453. _ju.removeWithFunction(group.connections.source, f);
  11454. _ju.removeWithFunction(group.connections.target, f);
  11455. delete _connectionTargetMap[conn.id];
  11456. }
  11457. }
  11458. _jsPlumb.bind(EVT_INTERNAL_CONNECTION_DETACHED, function(p) {
  11459. _cleanupDetachedConnection(p.connection);
  11460. });
  11461. _jsPlumb.bind(EVT_CONNECTION_MOVED, function(p) {
  11462. var connMap = p.index === 0 ? _connectionSourceMap : _connectionTargetMap;
  11463. var group = connMap[p.connection.id];
  11464. if (group) {
  11465. var list = group.connections[p.index === 0 ? "source" : "target"];
  11466. var idx = list.indexOf(p.connection);
  11467. if (idx !== -1) {
  11468. list.splice(idx, 1);
  11469. }
  11470. }
  11471. });
  11472. this.addGroup = function(group) {
  11473. _jsPlumb.addClass(group.getEl(), GROUP_EXPANDED_CLASS);
  11474. _managedGroups[group.id] = group;
  11475. group.manager = this;
  11476. _updateConnectionsForGroup(group);
  11477. _jsPlumb.fire(EVT_GROUP_ADDED, { group:group });
  11478. };
  11479. this.addToGroup = function(group, el, doNotFireEvent) {
  11480. group = this.getGroup(group);
  11481. if (group) {
  11482. var groupEl = group.getEl();
  11483. if (el._isJsPlumbGroup) {
  11484. return;
  11485. }
  11486. var currentGroup = el._jsPlumbGroup;
  11487. // if already a member of this group, do nothing
  11488. if (currentGroup !== group) {
  11489. _jsPlumb.removeFromDragSelection(el);
  11490. var elpos = _jsPlumb.getOffset(el, true);
  11491. var cpos = group.collapsed ? _jsPlumb.getOffset(groupEl, true) : _jsPlumb.getOffset(group.getDragArea(), true);
  11492. // otherwise, transfer to this group.
  11493. if (currentGroup != null) {
  11494. currentGroup.remove(el, false, doNotFireEvent, false, group);
  11495. self.updateConnectionsForGroup(currentGroup);
  11496. }
  11497. group.add(el, doNotFireEvent/*, currentGroup*/);
  11498. var handleDroppedConnections = function (list, index) {
  11499. var oidx = index === 0 ? 1 : 0;
  11500. list.each(function (c) {
  11501. c.setVisible(false);
  11502. if (c.endpoints[oidx].element._jsPlumbGroup === group) {
  11503. c.endpoints[oidx].setVisible(false);
  11504. _expandConnection(c, oidx, group);
  11505. }
  11506. else {
  11507. c.endpoints[index].setVisible(false);
  11508. _collapseConnection(c, index, group);
  11509. }
  11510. });
  11511. };
  11512. if (group.collapsed) {
  11513. handleDroppedConnections(_jsPlumb.select({source: el}), 0);
  11514. handleDroppedConnections(_jsPlumb.select({target: el}), 1);
  11515. }
  11516. var elId = _jsPlumb.getId(el);
  11517. _jsPlumb.dragManager.setParent(el, elId, groupEl, _jsPlumb.getId(groupEl), elpos);
  11518. var newPosition = { left: elpos.left - cpos.left, top: elpos.top - cpos.top };
  11519. _jsPlumb.setPosition(el, newPosition);
  11520. _jsPlumb.dragManager.revalidateParent(el, elId, elpos);
  11521. self.updateConnectionsForGroup(group);
  11522. _jsPlumb.revalidate(elId);
  11523. if (!doNotFireEvent) {
  11524. var p = {group: group, el: el, pos:newPosition};
  11525. if (currentGroup) {
  11526. p.sourceGroup = currentGroup;
  11527. }
  11528. _jsPlumb.fire(EVT_CHILD_ADDED, p);
  11529. }
  11530. }
  11531. }
  11532. };
  11533. this.removeFromGroup = function(group, el, doNotFireEvent) {
  11534. group = this.getGroup(group);
  11535. if (group) {
  11536. // if this group is currently collapsed then any proxied connections for the given el (or its descendants) need
  11537. // to be put back on their original element, and unproxied
  11538. if (group.collapsed) {
  11539. var _expandSet = function (conns, index) {
  11540. for (var i = 0; i < conns.length; i++) {
  11541. var c = conns[i];
  11542. if (c.proxies) {
  11543. for(var j = 0; j < c.proxies.length; j++) {
  11544. if (c.proxies[j] != null) {
  11545. var proxiedElement = c.proxies[j].originalEp.element;
  11546. if (proxiedElement === el || isDescendant(proxiedElement, el)) {
  11547. _expandConnection(c, index, group);
  11548. }
  11549. }
  11550. }
  11551. }
  11552. }
  11553. };
  11554. // setup proxies for sources and targets
  11555. _expandSet(group.connections.source.slice(), 0);
  11556. _expandSet(group.connections.target.slice(), 1);
  11557. }
  11558. group.remove(el, null, doNotFireEvent);
  11559. }
  11560. };
  11561. this.getGroup = function(groupId) {
  11562. var group = groupId;
  11563. if (_ju.isString(groupId)) {
  11564. group = _managedGroups[groupId];
  11565. if (group == null) {
  11566. throw new TypeError("No such group [" + groupId + "]");
  11567. }
  11568. }
  11569. return group;
  11570. };
  11571. this.getGroups = function() {
  11572. var o = [];
  11573. for (var g in _managedGroups) {
  11574. o.push(_managedGroups[g]);
  11575. }
  11576. return o;
  11577. };
  11578. this.removeGroup = function(group, deleteMembers, manipulateDOM, doNotFireEvent) {
  11579. group = this.getGroup(group);
  11580. this.expandGroup(group, true); // this reinstates any original connections and removes all proxies, but does not fire an event.
  11581. var newPositions = group[deleteMembers ? CMD_REMOVE_ALL : CMD_ORPHAN_ALL](manipulateDOM, doNotFireEvent);
  11582. _jsPlumb.remove(group.getEl());
  11583. delete _managedGroups[group.id];
  11584. delete _jsPlumb._groups[group.id];
  11585. _jsPlumb.fire(EVT_GROUP_REMOVED, { group:group });
  11586. return newPositions; // this will be null in the case or remove, but be a map of {id->[x,y]} in the case of orphan
  11587. };
  11588. this.removeAllGroups = function(deleteMembers, manipulateDOM, doNotFireEvent) {
  11589. for (var g in _managedGroups) {
  11590. this.removeGroup(_managedGroups[g], deleteMembers, manipulateDOM, doNotFireEvent);
  11591. }
  11592. };
  11593. function _setVisible(group, state) {
  11594. // TODO discovering the list of elements would ideally be a pluggable function.
  11595. var m = group.getEl().querySelectorAll(".jtk-managed");
  11596. for (var i = 0; i < m.length; i++) {
  11597. _jsPlumb[state ? CMD_SHOW : CMD_HIDE](m[i], true);
  11598. }
  11599. }
  11600. var _collapseConnection = function(c, index, group) {
  11601. var otherEl = c.endpoints[index === 0 ? 1 : 0].element;
  11602. if (otherEl[GROUP] && (!otherEl[GROUP].shouldProxy() && otherEl[GROUP].collapsed)) {
  11603. return;
  11604. }
  11605. var groupEl = group.getEl(), groupElId = _jsPlumb.getId(groupEl);
  11606. _jsPlumb.proxyConnection(c, index, groupEl, groupElId, function(c, index) { return group.getEndpoint(c, index); }, function(c, index) { return group.getAnchor(c, index); });
  11607. };
  11608. this.collapseGroup = function(group) {
  11609. group = this.getGroup(group);
  11610. if (group == null || group.collapsed) {
  11611. return;
  11612. }
  11613. var groupEl = group.getEl();
  11614. // todo remove old proxy endpoints first, just in case?
  11615. //group.proxies.length = 0;
  11616. // hide all connections
  11617. _setVisible(group, false);
  11618. if (group.shouldProxy()) {
  11619. // collapses all connections in a group.
  11620. var _collapseSet = function (conns, index) {
  11621. for (var i = 0; i < conns.length; i++) {
  11622. var c = conns[i];
  11623. _collapseConnection(c, index, group);
  11624. }
  11625. };
  11626. // setup proxies for sources and targets
  11627. _collapseSet(group.connections.source, 0);
  11628. _collapseSet(group.connections.target, 1);
  11629. }
  11630. group.collapsed = true;
  11631. _jsPlumb.removeClass(groupEl, GROUP_EXPANDED_CLASS);
  11632. _jsPlumb.addClass(groupEl, GROUP_COLLAPSED_CLASS);
  11633. _jsPlumb.revalidate(groupEl);
  11634. _jsPlumb.fire(EVT_COLLAPSE, { group:group });
  11635. };
  11636. var _expandConnection = function(c, index, group) {
  11637. _jsPlumb.unproxyConnection(c, index, _jsPlumb.getId(group.getEl()));
  11638. };
  11639. this.expandGroup = function(group, doNotFireEvent) {
  11640. group = this.getGroup(group);
  11641. if (group == null || !group.collapsed) {
  11642. return;
  11643. }
  11644. var groupEl = group.getEl();
  11645. _setVisible(group, true);
  11646. if (group.shouldProxy()) {
  11647. // expands all connections in a group.
  11648. var _expandSet = function (conns, index) {
  11649. for (var i = 0; i < conns.length; i++) {
  11650. var c = conns[i];
  11651. _expandConnection(c, index, group);
  11652. }
  11653. };
  11654. // setup proxies for sources and targets
  11655. _expandSet(group.connections.source, 0);
  11656. _expandSet(group.connections.target, 1);
  11657. }
  11658. group.collapsed = false;
  11659. _jsPlumb.addClass(groupEl, GROUP_EXPANDED_CLASS);
  11660. _jsPlumb.removeClass(groupEl, GROUP_COLLAPSED_CLASS);
  11661. _jsPlumb.revalidate(groupEl);
  11662. this.repaintGroup(group);
  11663. if (!doNotFireEvent) {
  11664. _jsPlumb.fire(EVT_EXPAND, { group: group});
  11665. }
  11666. };
  11667. this.repaintGroup = function(group) {
  11668. group = this.getGroup(group);
  11669. var m = group.getMembers();
  11670. for (var i = 0; i < m.length; i++) {
  11671. _jsPlumb.revalidate(m[i]);
  11672. }
  11673. };
  11674. // TODO refactor this with the code that responds to `connection` events.
  11675. function _updateConnectionsForGroup(group) {
  11676. var members = group.getMembers().slice();
  11677. var childMembers = [];
  11678. for (var i = 0; i < members.length; i++) {
  11679. Array.prototype.push.apply(childMembers, members[i].querySelectorAll(".jtk-managed"));
  11680. }
  11681. Array.prototype.push.apply(members, childMembers);
  11682. var c1 = _jsPlumb.getConnections({source:members, scope:"*"}, true);
  11683. var c2 = _jsPlumb.getConnections({target:members, scope:"*"}, true);
  11684. var processed = {};
  11685. group.connections.source.length = 0;
  11686. group.connections.target.length = 0;
  11687. var oneSet = function(c) {
  11688. for (var i = 0; i < c.length; i++) {
  11689. if (processed[c[i].id]) {
  11690. continue;
  11691. }
  11692. processed[c[i].id] = true;
  11693. var gs = _jsPlumb.getGroupFor(c[i].source),
  11694. gt = _jsPlumb.getGroupFor(c[i].target);
  11695. if (gs === group) {
  11696. if (gt !== group) {
  11697. group.connections.source.push(c[i]);
  11698. }
  11699. _connectionSourceMap[c[i].id] = group;
  11700. }
  11701. else if (gt === group) {
  11702. group.connections.target.push(c[i]);
  11703. _connectionTargetMap[c[i].id] = group;
  11704. }
  11705. }
  11706. };
  11707. oneSet(c1); oneSet(c2);
  11708. }
  11709. this.updateConnectionsForGroup = _updateConnectionsForGroup;
  11710. this.refreshAllGroups = function() {
  11711. for (var g in _managedGroups) {
  11712. _updateConnectionsForGroup(_managedGroups[g]);
  11713. _jsPlumb.dragManager.updateOffsets(_jsPlumb.getId(_managedGroups[g].getEl()));
  11714. }
  11715. };
  11716. };
  11717. /**
  11718. *
  11719. * @param {jsPlumbInstance} _jsPlumb Associated jsPlumb instance.
  11720. * @param {Object} params
  11721. * @param {Element} params.el The DOM element representing the Group.
  11722. * @param {String} [params.id] Optional ID for the Group. A UUID will be assigned as the Group's ID if you do not provide one.
  11723. * @param {Boolean} [params.constrain=false] If true, child elements will not be able to be dragged outside of the Group container.
  11724. * @param {Boolean} [params.revert=true] By default, child elements revert to the container if dragged outside. You can change this by setting `revert:false`. This behaviour is also overridden if you set `orphan` or `prune`.
  11725. * @param {Boolean} [params.orphan=false] If true, child elements dropped outside of the Group container will be removed from the Group (but not from the DOM).
  11726. * @param {Boolean} [params.prune=false] If true, child elements dropped outside of the Group container will be removed from the Group and also from the DOM.
  11727. * @param {Boolean} [params.dropOverride=false] If true, a child element that has been dropped onto some other Group will not be subject to the controls imposed by `prune`, `revert` or `orphan`.
  11728. * @constructor
  11729. */
  11730. var Group = function(_jsPlumb, params) {
  11731. var self = this;
  11732. var el = params.el;
  11733. this.getEl = function() { return el; };
  11734. this.id = params.id || _ju.uuid();
  11735. el._isJsPlumbGroup = true;
  11736. var getDragArea = this.getDragArea = function() {
  11737. var da = _jsPlumb.getSelector(el, GROUP_CONTAINER_SELECTOR);
  11738. return da && da.length > 0 ? da[0] : el;
  11739. };
  11740. var ghost = params.ghost === true;
  11741. var constrain = ghost || (params.constrain === true);
  11742. var revert = params.revert !== false;
  11743. var orphan = params.orphan === true;
  11744. var prune = params.prune === true;
  11745. var dropOverride = params.dropOverride === true;
  11746. var proxied = params.proxied !== false;
  11747. var elements = [];
  11748. this.connections = { source:[], target:[], internal:[] };
  11749. // this function, and getEndpoint below, are stubs for a future setup in which we can choose endpoint
  11750. // and anchor based upon the connection and the index (source/target) of the endpoint to be proxied.
  11751. this.getAnchor = function(conn, endpointIndex) {
  11752. return params.anchor || "Continuous";
  11753. };
  11754. this.getEndpoint = function(conn, endpointIndex) {
  11755. return params.endpoint || [ "Dot", { radius:10 }];
  11756. };
  11757. this.collapsed = false;
  11758. if (params.draggable !== false) {
  11759. var opts = {
  11760. drag:function() {
  11761. for (var i = 0; i < elements.length; i++) {
  11762. _jsPlumb.draw(elements[i]);
  11763. }
  11764. },
  11765. stop:function(params) {
  11766. _jsPlumb.fire(EVT_GROUP_DRAG_STOP, jsPlumb.extend(params, {group:self}));
  11767. },
  11768. scope:GROUP_DRAG_SCOPE
  11769. };
  11770. if (params.dragOptions) {
  11771. root.jsPlumb.extend(opts, params.dragOptions);
  11772. }
  11773. _jsPlumb.draggable(params.el, opts);
  11774. }
  11775. if (params.droppable !== false) {
  11776. _jsPlumb.droppable(params.el, {
  11777. drop:function(p) {
  11778. var el = p.drag.el;
  11779. if (el._isJsPlumbGroup) {
  11780. return;
  11781. }
  11782. var currentGroup = el._jsPlumbGroup;
  11783. if (currentGroup !== self) {
  11784. if (currentGroup != null) {
  11785. if (currentGroup.overrideDrop(el, self)) {
  11786. return;
  11787. }
  11788. }
  11789. _jsPlumb.getGroupManager().addToGroup(self, el, false);
  11790. }
  11791. }
  11792. });
  11793. }
  11794. var _each = function(_el, fn) {
  11795. var els = _el.nodeType == null ? _el : [ _el ];
  11796. for (var i = 0; i < els.length; i++) {
  11797. fn(els[i]);
  11798. }
  11799. };
  11800. this.overrideDrop = function(_el, targetGroup) {
  11801. return dropOverride && (revert || prune || orphan);
  11802. };
  11803. this.add = function(_el, doNotFireEvent/*, sourceGroup*/) {
  11804. var dragArea = getDragArea();
  11805. _each(_el, function(__el) {
  11806. if (__el._jsPlumbGroup != null) {
  11807. if (__el._jsPlumbGroup === self) {
  11808. return;
  11809. } else {
  11810. __el._jsPlumbGroup.remove(__el, true, doNotFireEvent, false);
  11811. }
  11812. }
  11813. __el._jsPlumbGroup = self;
  11814. elements.push(__el);
  11815. // test if draggable and add handlers if so.
  11816. if (_jsPlumb.isAlreadyDraggable(__el)) {
  11817. _bindDragHandlers(__el);
  11818. }
  11819. if (__el.parentNode !== dragArea) {
  11820. dragArea.appendChild(__el);
  11821. }
  11822. // if (!doNotFireEvent) {
  11823. // var p = {group: self, el: __el};
  11824. // if (sourceGroup) {
  11825. // p.sourceGroup = sourceGroup;
  11826. // }
  11827. // //_jsPlumb.fire(EVT_CHILD_ADDED, p);
  11828. // }
  11829. });
  11830. _jsPlumb.getGroupManager().updateConnectionsForGroup(self);
  11831. };
  11832. this.remove = function(el, manipulateDOM, doNotFireEvent, doNotUpdateConnections, targetGroup) {
  11833. _each(el, function(__el) {
  11834. if (__el._jsPlumbGroup === self) {
  11835. delete __el._jsPlumbGroup;
  11836. _ju.removeWithFunction(elements, function (e) {
  11837. return e === __el;
  11838. });
  11839. if (manipulateDOM) {
  11840. try {
  11841. self.getDragArea().removeChild(__el);
  11842. } catch (e) {
  11843. jsPlumbUtil.log("Could not remove element from Group " + e);
  11844. }
  11845. }
  11846. _unbindDragHandlers(__el);
  11847. if (!doNotFireEvent) {
  11848. var p = {group: self, el: __el};
  11849. if (targetGroup) {
  11850. p.targetGroup = targetGroup;
  11851. }
  11852. _jsPlumb.fire(EVT_CHILD_REMOVED, p);
  11853. }
  11854. }
  11855. });
  11856. if (!doNotUpdateConnections) {
  11857. _jsPlumb.getGroupManager().updateConnectionsForGroup(self);
  11858. }
  11859. };
  11860. this.removeAll = function(manipulateDOM, doNotFireEvent) {
  11861. for (var i = 0, l = elements.length; i < l; i++) {
  11862. var el = elements[0];
  11863. self.remove(el, manipulateDOM, doNotFireEvent, true);
  11864. _jsPlumb.remove(el, true);
  11865. }
  11866. elements.length = 0;
  11867. _jsPlumb.getGroupManager().updateConnectionsForGroup(self);
  11868. };
  11869. this.orphanAll = function() {
  11870. var orphanedPositions = {};
  11871. for (var i = 0; i < elements.length; i++) {
  11872. var newPosition = _orphan(elements[i]);
  11873. orphanedPositions[newPosition[0]] = newPosition[1];
  11874. }
  11875. elements.length = 0;
  11876. return orphanedPositions;
  11877. };
  11878. this.getMembers = function() { return elements; };
  11879. el[GROUP] = this;
  11880. _jsPlumb.bind(ELEMENT_DRAGGABLE_EVENT, function(dragParams) {
  11881. // if its for the current group,
  11882. if (dragParams.el._jsPlumbGroup === this) {
  11883. _bindDragHandlers(dragParams.el);
  11884. }
  11885. }.bind(this));
  11886. function _findParent(_el) {
  11887. return _el.offsetParent;
  11888. }
  11889. function _isInsideParent(_el, pos) {
  11890. var p = _findParent(_el),
  11891. s = _jsPlumb.getSize(p),
  11892. ss = _jsPlumb.getSize(_el),
  11893. leftEdge = pos[0],
  11894. rightEdge = leftEdge + ss[0],
  11895. topEdge = pos[1],
  11896. bottomEdge = topEdge + ss[1];
  11897. return rightEdge > 0 && leftEdge < s[0] && bottomEdge > 0 && topEdge < s[1];
  11898. }
  11899. //
  11900. // orphaning an element means taking it out of the group and adding it to the main jsplumb container.
  11901. // we return the new calculated position from this method and the element's id.
  11902. //
  11903. function _orphan(_el) {
  11904. var id = _jsPlumb.getId(_el);
  11905. var pos = _jsPlumb.getOffset(_el);
  11906. _el.parentNode.removeChild(_el);
  11907. _jsPlumb.getContainer().appendChild(_el);
  11908. _jsPlumb.setPosition(_el, pos);
  11909. _unbindDragHandlers(_el);
  11910. _jsPlumb.dragManager.clearParent(_el, id);
  11911. return [id, pos];
  11912. }
  11913. //
  11914. // remove an element from the group, then either prune it from the jsplumb instance, or just orphan it.
  11915. //
  11916. function _pruneOrOrphan(p) {
  11917. var out = [];
  11918. function _one(el, left, top) {
  11919. var orphanedPosition = null;
  11920. if (!_isInsideParent(el, [left, top])) {
  11921. var group = el._jsPlumbGroup;
  11922. if (prune) {
  11923. _jsPlumb.remove(el);
  11924. } else {
  11925. orphanedPosition = _orphan(el);
  11926. }
  11927. group.remove(el);
  11928. }
  11929. return orphanedPosition;
  11930. }
  11931. for (var i = 0; i < p.selection.length; i++) {
  11932. out.push(_one(p.selection[i][0], p.selection[i][1].left, p.selection[i][1].top));
  11933. }
  11934. return out.length === 1 ? out[0] : out;
  11935. }
  11936. //
  11937. // redraws the element
  11938. //
  11939. function _revalidate(_el) {
  11940. var id = _jsPlumb.getId(_el);
  11941. _jsPlumb.revalidate(_el);
  11942. _jsPlumb.dragManager.revalidateParent(_el, id);
  11943. }
  11944. //
  11945. // unbind the group specific drag/revert handlers.
  11946. //
  11947. function _unbindDragHandlers(_el) {
  11948. if (!_el._katavorioDrag) {
  11949. return;
  11950. }
  11951. if (prune || orphan) {
  11952. _el._katavorioDrag.off(STOP, _pruneOrOrphan);
  11953. }
  11954. if (!prune && !orphan && revert) {
  11955. _el._katavorioDrag.off(REVERT, _revalidate);
  11956. _el._katavorioDrag.setRevert(null);
  11957. }
  11958. }
  11959. function _bindDragHandlers(_el) {
  11960. if (!_el._katavorioDrag) {
  11961. return;
  11962. }
  11963. if (prune || orphan) {
  11964. _el._katavorioDrag.on(STOP, _pruneOrOrphan);
  11965. }
  11966. if (constrain) {
  11967. _el._katavorioDrag.setConstrain(true);
  11968. }
  11969. if (ghost) {
  11970. _el._katavorioDrag.setUseGhostProxy(true);
  11971. }
  11972. if (!prune && !orphan && revert) {
  11973. _el._katavorioDrag.on(REVERT, _revalidate);
  11974. _el._katavorioDrag.setRevert(function(__el, pos) {
  11975. return !_isInsideParent(__el, pos);
  11976. });
  11977. }
  11978. }
  11979. this.shouldProxy = function() {
  11980. return proxied;
  11981. };
  11982. _jsPlumb.getGroupManager().addGroup(this);
  11983. };
  11984. /**
  11985. * Adds a group to the jsPlumb instance.
  11986. * @method addGroup
  11987. * @param {Object} params
  11988. * @return {Group} The newly created Group.
  11989. */
  11990. _jpi.prototype.addGroup = function(params) {
  11991. var j = this;
  11992. j._groups = j._groups || {};
  11993. if (j._groups[params.id] != null) {
  11994. throw new TypeError("cannot create Group [" + params.id + "]; a Group with that ID exists");
  11995. }
  11996. if (params.el[GROUP] != null) {
  11997. throw new TypeError("cannot create Group [" + params.id + "]; the given element is already a Group");
  11998. }
  11999. var group = new Group(j, params);
  12000. j._groups[group.id] = group;
  12001. if (params.collapsed) {
  12002. this.collapseGroup(group);
  12003. }
  12004. return group;
  12005. };
  12006. /**
  12007. * Add an element to a group.
  12008. * @method addToGroup
  12009. * @param {String} group Group, or ID of the group, to add the element to.
  12010. * @param {Element} el Element to add to the group.
  12011. */
  12012. _jpi.prototype.addToGroup = function(group, el, doNotFireEvent) {
  12013. var _one = function(_el) {
  12014. var id = this.getId(_el);
  12015. this.manage(id, _el);
  12016. this.getGroupManager().addToGroup(group, _el, doNotFireEvent);
  12017. }.bind(this);
  12018. if (Array.isArray(el)) {
  12019. for (var i = 0; i < el.length; i++) {
  12020. _one(el[i]);
  12021. }
  12022. } else {
  12023. _one(el);
  12024. }
  12025. };
  12026. /**
  12027. * Remove an element from a group, and sets its DOM element to be a child of the container again. ??
  12028. * @method removeFromGroup
  12029. * @param {String} group Group, or ID of the group, to remove the element from.
  12030. * @param {Element} el Element to add to the group.
  12031. */
  12032. _jpi.prototype.removeFromGroup = function(group, el, doNotFireEvent) {
  12033. this.getGroupManager().removeFromGroup(group, el, doNotFireEvent);
  12034. this.getContainer().appendChild(el);
  12035. };
  12036. /**
  12037. * Remove a group, and optionally remove its members from the jsPlumb instance.
  12038. * @method removeGroup
  12039. * @param {String|Group} group Group to delete, or ID of Group to delete.
  12040. * @param {Boolean} [deleteMembers=false] If true, group members will be removed along with the group. Otherwise they will
  12041. * just be 'orphaned' (returned to the main container).
  12042. * @returns {Map[String, Position}} When deleteMembers is false, this method returns a map of {id->position}
  12043. */
  12044. _jpi.prototype.removeGroup = function(group, deleteMembers, manipulateDOM, doNotFireEvent) {
  12045. return this.getGroupManager().removeGroup(group, deleteMembers, manipulateDOM, doNotFireEvent);
  12046. };
  12047. /**
  12048. * Remove all groups, and optionally remove their members from the jsPlumb instance.
  12049. * @method removeAllGroup
  12050. * @param {Boolean} [deleteMembers=false] If true, group members will be removed along with the groups. Otherwise they will
  12051. * just be 'orphaned' (returned to the main container).
  12052. */
  12053. _jpi.prototype.removeAllGroups = function(deleteMembers, manipulateDOM, doNotFireEvent) {
  12054. this.getGroupManager().removeAllGroups(deleteMembers, manipulateDOM, doNotFireEvent);
  12055. };
  12056. /**
  12057. * Get a Group
  12058. * @method getGroup
  12059. * @param {String} groupId ID of the group to get
  12060. * @return {Group} Group with the given ID, null if not found.
  12061. */
  12062. _jpi.prototype.getGroup = function(groupId) {
  12063. return this.getGroupManager().getGroup(groupId);
  12064. };
  12065. /**
  12066. * Gets all the Groups managed by the jsPlumb instance.
  12067. * @returns {Group[]} List of Groups. Empty if none.
  12068. */
  12069. _jpi.prototype.getGroups = function() {
  12070. return this.getGroupManager().getGroups();
  12071. };
  12072. /**
  12073. * Expands a group element. jsPlumb doesn't do "everything" for you here, because what it means to expand a Group
  12074. * will vary from application to application. jsPlumb does these things:
  12075. *
  12076. * - Hides any connections that are internal to the group (connections between members, and connections from member of
  12077. * the group to the group itself)
  12078. * - Proxies all connections for which the source or target is a member of the group.
  12079. * - Hides the proxied connections.
  12080. * - Adds the jtk-group-expanded class to the group's element
  12081. * - Removes the jtk-group-collapsed class from the group's element.
  12082. *
  12083. * @method expandGroup
  12084. * @param {String|Group} group Group to expand, or ID of Group to expand.
  12085. */
  12086. _jpi.prototype.expandGroup = function(group) {
  12087. this.getGroupManager().expandGroup(group);
  12088. };
  12089. /**
  12090. * Collapses a group element. jsPlumb doesn't do "everything" for you here, because what it means to collapse a Group
  12091. * will vary from application to application. jsPlumb does these things:
  12092. *
  12093. * - Shows any connections that are internal to the group (connections between members, and connections from member of
  12094. * the group to the group itself)
  12095. * - Removes proxies for all connections for which the source or target is a member of the group.
  12096. * - Shows the previously proxied connections.
  12097. * - Adds the jtk-group-collapsed class to the group's element
  12098. * - Removes the jtk-group-expanded class from the group's element.
  12099. *
  12100. * @method expandGroup
  12101. * @param {String|Group} group Group to expand, or ID of Group to expand.
  12102. */
  12103. _jpi.prototype.collapseGroup = function(groupId) {
  12104. this.getGroupManager().collapseGroup(groupId);
  12105. };
  12106. _jpi.prototype.repaintGroup = function(group) {
  12107. this.getGroupManager().repaintGroup(group);
  12108. };
  12109. /**
  12110. * Collapses or expands a group element depending on its current state. See notes in the collapseGroup and expandGroup method.
  12111. *
  12112. * @method toggleGroup
  12113. * @param {String|Group} group Group to expand/collapse, or ID of Group to expand/collapse.
  12114. */
  12115. _jpi.prototype.toggleGroup = function(group) {
  12116. group = this.getGroupManager().getGroup(group);
  12117. if (group != null) {
  12118. this.getGroupManager()[group.collapsed ? "expandGroup" : "collapseGroup"](group);
  12119. }
  12120. };
  12121. //
  12122. // lazy init a group manager for the given jsplumb instance.
  12123. //
  12124. _jpi.prototype.getGroupManager = function() {
  12125. var mgr = this[GROUP_MANAGER];
  12126. if (mgr == null) {
  12127. mgr = this[GROUP_MANAGER] = new GroupManager(this);
  12128. }
  12129. return mgr;
  12130. };
  12131. _jpi.prototype.removeGroupManager = function() {
  12132. delete this[GROUP_MANAGER];
  12133. };
  12134. /**
  12135. * Gets the Group that the given element belongs to, null if none.
  12136. * @method getGroupFor
  12137. * @param {String|Element} el Element, or element ID.
  12138. * @returns {Group} A Group, if found, or null.
  12139. */
  12140. _jpi.prototype.getGroupFor = function(el) {
  12141. el = this.getElement(el);
  12142. if (el) {
  12143. var c = this.getContainer();
  12144. var abort = false, g = null, child = null;
  12145. while (!abort) {
  12146. if (el == null || el === c) {
  12147. abort = true;
  12148. } else {
  12149. if (el[GROUP]) {
  12150. g = el[GROUP];
  12151. child = el;
  12152. abort = true;
  12153. } else {
  12154. el = el.parentNode;
  12155. }
  12156. }
  12157. }
  12158. return g;
  12159. }
  12160. };
  12161. }).call(typeof window !== 'undefined' ? window : this);
  12162. /*
  12163. * This file contains the 'flowchart' connectors, consisting of vertical and horizontal line segments.
  12164. *
  12165. * Copyright (c) 2010 - 2020 jsPlumb (hello@jsplumbtoolkit.com)
  12166. *
  12167. * https://jsplumbtoolkit.com
  12168. * https://github.com/jsplumb/jsplumb
  12169. *
  12170. * Dual licensed under the MIT and GPL2 licenses.
  12171. */
  12172. ;
  12173. (function () {
  12174. "use strict";
  12175. var root = this, _jp = root.jsPlumb, _ju = root.jsPlumbUtil;
  12176. var STRAIGHT = "Straight";
  12177. var ARC = "Arc";
  12178. var Flowchart = function (params) {
  12179. this.type = "Flowchart";
  12180. params = params || {};
  12181. params.stub = params.stub == null ? 30 : params.stub;
  12182. var segments,
  12183. _super = _jp.Connectors.AbstractConnector.apply(this, arguments),
  12184. midpoint = params.midpoint == null || isNaN(params.midpoint) ? 0.5 : params.midpoint,
  12185. alwaysRespectStubs = params.alwaysRespectStubs === true,
  12186. lastx = null, lasty = null, lastOrientation,
  12187. cornerRadius = params.cornerRadius != null ? params.cornerRadius : 0,
  12188. // TODO now common between this and AbstractBezierEditor; refactor into superclass?
  12189. loopbackRadius = params.loopbackRadius || 25,
  12190. isLoopbackCurrently = false,
  12191. sgn = function (n) {
  12192. return n < 0 ? -1 : n === 0 ? 0 : 1;
  12193. },
  12194. segmentDirections = function(segment) {
  12195. return [
  12196. sgn( segment[2] - segment[0] ),
  12197. sgn( segment[3] - segment[1] )
  12198. ];
  12199. },
  12200. /**
  12201. * helper method to add a segment.
  12202. */
  12203. addSegment = function (segments, x, y, paintInfo) {
  12204. if (lastx === x && lasty === y) {
  12205. return;
  12206. }
  12207. var lx = lastx == null ? paintInfo.sx : lastx,
  12208. ly = lasty == null ? paintInfo.sy : lasty,
  12209. o = lx === x ? "v" : "h";
  12210. lastx = x;
  12211. lasty = y;
  12212. segments.push([ lx, ly, x, y, o ]);
  12213. },
  12214. segLength = function (s) {
  12215. return Math.sqrt(Math.pow(s[0] - s[2], 2) + Math.pow(s[1] - s[3], 2));
  12216. },
  12217. _cloneArray = function (a) {
  12218. var _a = [];
  12219. _a.push.apply(_a, a);
  12220. return _a;
  12221. },
  12222. writeSegments = function (conn, segments, paintInfo) {
  12223. var current = null, next, currentDirection, nextDirection;
  12224. for (var i = 0; i < segments.length - 1; i++) {
  12225. current = current || _cloneArray(segments[i]);
  12226. next = _cloneArray(segments[i + 1]);
  12227. currentDirection = segmentDirections(current);
  12228. nextDirection = segmentDirections(next);
  12229. if (cornerRadius > 0 && current[4] !== next[4]) {
  12230. var minSegLength = Math.min(segLength(current), segLength(next));
  12231. var radiusToUse = Math.min(cornerRadius, minSegLength / 2);
  12232. current[2] -= currentDirection[0] * radiusToUse;
  12233. current[3] -= currentDirection[1] * radiusToUse;
  12234. next[0] += nextDirection[0] * radiusToUse;
  12235. next[1] += nextDirection[1] * radiusToUse;
  12236. var ac = (currentDirection[1] === nextDirection[0] && nextDirection[0] === 1) ||
  12237. ((currentDirection[1] === nextDirection[0] && nextDirection[0] === 0) && currentDirection[0] !== nextDirection[1]) ||
  12238. (currentDirection[1] === nextDirection[0] && nextDirection[0] === -1),
  12239. sgny = next[1] > current[3] ? 1 : -1,
  12240. sgnx = next[0] > current[2] ? 1 : -1,
  12241. sgnEqual = sgny === sgnx,
  12242. cx = (sgnEqual && ac || (!sgnEqual && !ac)) ? next[0] : current[2],
  12243. cy = (sgnEqual && ac || (!sgnEqual && !ac)) ? current[3] : next[1];
  12244. _super.addSegment(conn, STRAIGHT, {
  12245. x1: current[0], y1: current[1], x2: current[2], y2: current[3]
  12246. });
  12247. _super.addSegment(conn, ARC, {
  12248. r: radiusToUse,
  12249. x1: current[2],
  12250. y1: current[3],
  12251. x2: next[0],
  12252. y2: next[1],
  12253. cx: cx,
  12254. cy: cy,
  12255. ac: ac
  12256. });
  12257. }
  12258. else {
  12259. // dx + dy are used to adjust for line width.
  12260. var dx = (current[2] === current[0]) ? 0 : (current[2] > current[0]) ? (paintInfo.lw / 2) : -(paintInfo.lw / 2),
  12261. dy = (current[3] === current[1]) ? 0 : (current[3] > current[1]) ? (paintInfo.lw / 2) : -(paintInfo.lw / 2);
  12262. _super.addSegment(conn, STRAIGHT, {
  12263. x1: current[0] - dx, y1: current[1] - dy, x2: current[2] + dx, y2: current[3] + dy
  12264. });
  12265. }
  12266. current = next;
  12267. }
  12268. if (next != null) {
  12269. // last segment
  12270. _super.addSegment(conn, STRAIGHT, {
  12271. x1: next[0], y1: next[1], x2: next[2], y2: next[3]
  12272. });
  12273. }
  12274. };
  12275. this.midpoint = midpoint;
  12276. this._compute = function (paintInfo, params) {
  12277. segments = [];
  12278. lastx = null;
  12279. lasty = null;
  12280. lastOrientation = null;
  12281. var commonStubCalculator = function () {
  12282. return [paintInfo.startStubX, paintInfo.startStubY, paintInfo.endStubX, paintInfo.endStubY];
  12283. },
  12284. stubCalculators = {
  12285. perpendicular: commonStubCalculator,
  12286. orthogonal: commonStubCalculator,
  12287. opposite: function (axis) {
  12288. var pi = paintInfo,
  12289. idx = axis === "x" ? 0 : 1,
  12290. areInProximity = {
  12291. "x": function () {
  12292. return ( (pi.so[idx] === 1 && (
  12293. ( (pi.startStubX > pi.endStubX) && (pi.tx > pi.startStubX) ) ||
  12294. ( (pi.sx > pi.endStubX) && (pi.tx > pi.sx))))) ||
  12295. ( (pi.so[idx] === -1 && (
  12296. ( (pi.startStubX < pi.endStubX) && (pi.tx < pi.startStubX) ) ||
  12297. ( (pi.sx < pi.endStubX) && (pi.tx < pi.sx)))));
  12298. },
  12299. "y": function () {
  12300. return ( (pi.so[idx] === 1 && (
  12301. ( (pi.startStubY > pi.endStubY) && (pi.ty > pi.startStubY) ) ||
  12302. ( (pi.sy > pi.endStubY) && (pi.ty > pi.sy))))) ||
  12303. ( (pi.so[idx] === -1 && (
  12304. ( (pi.startStubY < pi.endStubY) && (pi.ty < pi.startStubY) ) ||
  12305. ( (pi.sy < pi.endStubY) && (pi.ty < pi.sy)))));
  12306. }
  12307. };
  12308. if (!alwaysRespectStubs && areInProximity[axis]()) {
  12309. return {
  12310. "x": [(paintInfo.sx + paintInfo.tx) / 2, paintInfo.startStubY, (paintInfo.sx + paintInfo.tx) / 2, paintInfo.endStubY],
  12311. "y": [paintInfo.startStubX, (paintInfo.sy + paintInfo.ty) / 2, paintInfo.endStubX, (paintInfo.sy + paintInfo.ty) / 2]
  12312. }[axis];
  12313. }
  12314. else {
  12315. return [paintInfo.startStubX, paintInfo.startStubY, paintInfo.endStubX, paintInfo.endStubY];
  12316. }
  12317. }
  12318. };
  12319. // calculate Stubs.
  12320. var stubs = stubCalculators[paintInfo.anchorOrientation](paintInfo.sourceAxis),
  12321. idx = paintInfo.sourceAxis === "x" ? 0 : 1,
  12322. oidx = paintInfo.sourceAxis === "x" ? 1 : 0,
  12323. ss = stubs[idx],
  12324. oss = stubs[oidx],
  12325. es = stubs[idx + 2],
  12326. oes = stubs[oidx + 2];
  12327. // add the start stub segment. use stubs for loopback as it will look better, with the loop spaced
  12328. // away from the element.
  12329. addSegment(segments, stubs[0], stubs[1], paintInfo);
  12330. // if its a loopback and we should treat it differently.
  12331. // if (false && params.sourcePos[0] === params.targetPos[0] && params.sourcePos[1] === params.targetPos[1]) {
  12332. //
  12333. // // we use loopbackRadius here, as statemachine connectors do.
  12334. // // so we go radius to the left from stubs[0], then upwards by 2*radius, to the right by 2*radius,
  12335. // // down by 2*radius, left by radius.
  12336. // addSegment(segments, stubs[0] - loopbackRadius, stubs[1], paintInfo);
  12337. // addSegment(segments, stubs[0] - loopbackRadius, stubs[1] - (2 * loopbackRadius), paintInfo);
  12338. // addSegment(segments, stubs[0] + loopbackRadius, stubs[1] - (2 * loopbackRadius), paintInfo);
  12339. // addSegment(segments, stubs[0] + loopbackRadius, stubs[1], paintInfo);
  12340. // addSegment(segments, stubs[0], stubs[1], paintInfo);
  12341. //
  12342. // }
  12343. // else {
  12344. var midx = paintInfo.startStubX + ((paintInfo.endStubX - paintInfo.startStubX) * midpoint),
  12345. midy = paintInfo.startStubY + ((paintInfo.endStubY - paintInfo.startStubY) * midpoint);
  12346. var orientations = {x: [0, 1], y: [1, 0]},
  12347. lineCalculators = {
  12348. perpendicular: function (axis) {
  12349. var pi = paintInfo,
  12350. sis = {
  12351. x: [
  12352. [[1, 2, 3, 4], null, [2, 1, 4, 3]],
  12353. null,
  12354. [[4, 3, 2, 1], null, [3, 4, 1, 2]]
  12355. ],
  12356. y: [
  12357. [[3, 2, 1, 4], null, [2, 3, 4, 1]],
  12358. null,
  12359. [[4, 1, 2, 3], null, [1, 4, 3, 2]]
  12360. ]
  12361. },
  12362. stubs = {
  12363. x: [[pi.startStubX, pi.endStubX], null, [pi.endStubX, pi.startStubX]],
  12364. y: [[pi.startStubY, pi.endStubY], null, [pi.endStubY, pi.startStubY]]
  12365. },
  12366. midLines = {
  12367. x: [[midx, pi.startStubY], [midx, pi.endStubY]],
  12368. y: [[pi.startStubX, midy], [pi.endStubX, midy]]
  12369. },
  12370. linesToEnd = {
  12371. x: [[pi.endStubX, pi.startStubY]],
  12372. y: [[pi.startStubX, pi.endStubY]]
  12373. },
  12374. startToEnd = {
  12375. x: [[pi.startStubX, pi.endStubY], [pi.endStubX, pi.endStubY]],
  12376. y: [[pi.endStubX, pi.startStubY], [pi.endStubX, pi.endStubY]]
  12377. },
  12378. startToMidToEnd = {
  12379. x: [[pi.startStubX, midy], [pi.endStubX, midy], [pi.endStubX, pi.endStubY]],
  12380. y: [[midx, pi.startStubY], [midx, pi.endStubY], [pi.endStubX, pi.endStubY]]
  12381. },
  12382. otherStubs = {
  12383. x: [pi.startStubY, pi.endStubY],
  12384. y: [pi.startStubX, pi.endStubX]
  12385. },
  12386. soIdx = orientations[axis][0], toIdx = orientations[axis][1],
  12387. _so = pi.so[soIdx] + 1,
  12388. _to = pi.to[toIdx] + 1,
  12389. otherFlipped = (pi.to[toIdx] === -1 && (otherStubs[axis][1] < otherStubs[axis][0])) || (pi.to[toIdx] === 1 && (otherStubs[axis][1] > otherStubs[axis][0])),
  12390. stub1 = stubs[axis][_so][0],
  12391. stub2 = stubs[axis][_so][1],
  12392. segmentIndexes = sis[axis][_so][_to];
  12393. if (pi.segment === segmentIndexes[3] || (pi.segment === segmentIndexes[2] && otherFlipped)) {
  12394. return midLines[axis];
  12395. }
  12396. else if (pi.segment === segmentIndexes[2] && stub2 < stub1) {
  12397. return linesToEnd[axis];
  12398. }
  12399. else if ((pi.segment === segmentIndexes[2] && stub2 >= stub1) || (pi.segment === segmentIndexes[1] && !otherFlipped)) {
  12400. return startToMidToEnd[axis];
  12401. }
  12402. else if (pi.segment === segmentIndexes[0] || (pi.segment === segmentIndexes[1] && otherFlipped)) {
  12403. return startToEnd[axis];
  12404. }
  12405. },
  12406. orthogonal: function (axis, startStub, otherStartStub, endStub, otherEndStub) {
  12407. var pi = paintInfo,
  12408. extent = {
  12409. "x": pi.so[0] === -1 ? Math.min(startStub, endStub) : Math.max(startStub, endStub),
  12410. "y": pi.so[1] === -1 ? Math.min(startStub, endStub) : Math.max(startStub, endStub)
  12411. }[axis];
  12412. return {
  12413. "x": [
  12414. [extent, otherStartStub],
  12415. [extent, otherEndStub],
  12416. [endStub, otherEndStub]
  12417. ],
  12418. "y": [
  12419. [otherStartStub, extent],
  12420. [otherEndStub, extent],
  12421. [otherEndStub, endStub]
  12422. ]
  12423. }[axis];
  12424. },
  12425. opposite: function (axis, ss, oss, es) {
  12426. var pi = paintInfo,
  12427. otherAxis = {"x": "y", "y": "x"}[axis],
  12428. dim = {"x": "height", "y": "width"}[axis],
  12429. comparator = pi["is" + axis.toUpperCase() + "GreaterThanStubTimes2"];
  12430. if (params.sourceEndpoint.elementId === params.targetEndpoint.elementId) {
  12431. var _val = oss + ((1 - params.sourceEndpoint.anchor[otherAxis]) * params.sourceInfo[dim]) + _super.maxStub;
  12432. return {
  12433. "x": [
  12434. [ss, _val],
  12435. [es, _val]
  12436. ],
  12437. "y": [
  12438. [_val, ss],
  12439. [_val, es]
  12440. ]
  12441. }[axis];
  12442. }
  12443. else if (!comparator || (pi.so[idx] === 1 && ss > es) || (pi.so[idx] === -1 && ss < es)) {
  12444. return {
  12445. "x": [
  12446. [ss, midy],
  12447. [es, midy]
  12448. ],
  12449. "y": [
  12450. [midx, ss],
  12451. [midx, es]
  12452. ]
  12453. }[axis];
  12454. }
  12455. else if ((pi.so[idx] === 1 && ss < es) || (pi.so[idx] === -1 && ss > es)) {
  12456. return {
  12457. "x": [
  12458. [midx, pi.sy],
  12459. [midx, pi.ty]
  12460. ],
  12461. "y": [
  12462. [pi.sx, midy],
  12463. [pi.tx, midy]
  12464. ]
  12465. }[axis];
  12466. }
  12467. }
  12468. };
  12469. // compute the rest of the line
  12470. var p = lineCalculators[paintInfo.anchorOrientation](paintInfo.sourceAxis, ss, oss, es, oes);
  12471. if (p) {
  12472. for (var i = 0; i < p.length; i++) {
  12473. addSegment(segments, p[i][0], p[i][1], paintInfo);
  12474. }
  12475. }
  12476. // line to end stub
  12477. addSegment(segments, stubs[2], stubs[3], paintInfo);
  12478. //}
  12479. // end stub to end (common)
  12480. addSegment(segments, paintInfo.tx, paintInfo.ty, paintInfo);
  12481. // write out the segments.
  12482. writeSegments(this, segments, paintInfo);
  12483. };
  12484. };
  12485. _jp.Connectors.Flowchart = Flowchart;
  12486. _ju.extend(_jp.Connectors.Flowchart, _jp.Connectors.AbstractConnector);
  12487. }).call(typeof window !== 'undefined' ? window : this);
  12488. /*
  12489. * This file contains the code for the Bezier connector type.
  12490. *
  12491. * Copyright (c) 2010 - 2020 jsPlumb (hello@jsplumbtoolkit.com)
  12492. *
  12493. * https://jsplumbtoolkit.com
  12494. * https://github.com/jsplumb/jsplumb
  12495. *
  12496. * Dual licensed under the MIT and GPL2 licenses.
  12497. */
  12498. ;
  12499. (function () {
  12500. "use strict";
  12501. var root = this, _jp = root.jsPlumb, _ju = root.jsPlumbUtil;
  12502. _jp.Connectors.AbstractBezierConnector = function(params) {
  12503. params = params || {};
  12504. var showLoopback = params.showLoopback !== false,
  12505. curviness = params.curviness || 10,
  12506. margin = params.margin || 5,
  12507. proximityLimit = params.proximityLimit || 80,
  12508. clockwise = params.orientation && params.orientation === "clockwise",
  12509. loopbackRadius = params.loopbackRadius || 25,
  12510. isLoopbackCurrently = false,
  12511. _super;
  12512. this._compute = function (paintInfo, p) {
  12513. var sp = p.sourcePos,
  12514. tp = p.targetPos,
  12515. _w = Math.abs(sp[0] - tp[0]),
  12516. _h = Math.abs(sp[1] - tp[1]);
  12517. if (!showLoopback || (p.sourceEndpoint.elementId !== p.targetEndpoint.elementId)) {
  12518. isLoopbackCurrently = false;
  12519. this._computeBezier(paintInfo, p, sp, tp, _w, _h);
  12520. } else {
  12521. isLoopbackCurrently = true;
  12522. // a loopback connector. draw an arc from one anchor to the other.
  12523. var x1 = p.sourcePos[0], y1 = p.sourcePos[1] - margin,
  12524. cx = x1, cy = y1 - loopbackRadius,
  12525. // canvas sizing stuff, to ensure the whole painted area is visible.
  12526. _x = cx - loopbackRadius,
  12527. _y = cy - loopbackRadius;
  12528. _w = 2 * loopbackRadius;
  12529. _h = 2 * loopbackRadius;
  12530. paintInfo.points[0] = _x;
  12531. paintInfo.points[1] = _y;
  12532. paintInfo.points[2] = _w;
  12533. paintInfo.points[3] = _h;
  12534. // ADD AN ARC SEGMENT.
  12535. _super.addSegment(this, "Arc", {
  12536. loopback: true,
  12537. x1: (x1 - _x) + 4,
  12538. y1: y1 - _y,
  12539. startAngle: 0,
  12540. endAngle: 2 * Math.PI,
  12541. r: loopbackRadius,
  12542. ac: !clockwise,
  12543. x2: (x1 - _x) - 4,
  12544. y2: y1 - _y,
  12545. cx: cx - _x,
  12546. cy: cy - _y
  12547. });
  12548. }
  12549. };
  12550. _super = _jp.Connectors.AbstractConnector.apply(this, arguments);
  12551. return _super;
  12552. };
  12553. _ju.extend(_jp.Connectors.AbstractBezierConnector, _jp.Connectors.AbstractConnector);
  12554. var Bezier = function (params) {
  12555. params = params || {};
  12556. this.type = "Bezier";
  12557. var _super = _jp.Connectors.AbstractBezierConnector.apply(this, arguments),
  12558. majorAnchor = params.curviness || 150,
  12559. minorAnchor = 10;
  12560. this.getCurviness = function () {
  12561. return majorAnchor;
  12562. };
  12563. this._findControlPoint = function (point, sourceAnchorPosition, targetAnchorPosition, sourceEndpoint, targetEndpoint, soo, too) {
  12564. // determine if the two anchors are perpendicular to each other in their orientation. we swap the control
  12565. // points around if so (code could be tightened up)
  12566. var perpendicular = soo[0] !== too[0] || soo[1] === too[1],
  12567. p = [];
  12568. if (!perpendicular) {
  12569. if (soo[0] === 0) {
  12570. p.push(sourceAnchorPosition[0] < targetAnchorPosition[0] ? point[0] + minorAnchor : point[0] - minorAnchor);
  12571. }
  12572. else {
  12573. p.push(point[0] - (majorAnchor * soo[0]));
  12574. }
  12575. if (soo[1] === 0) {
  12576. p.push(sourceAnchorPosition[1] < targetAnchorPosition[1] ? point[1] + minorAnchor : point[1] - minorAnchor);
  12577. }
  12578. else {
  12579. p.push(point[1] + (majorAnchor * too[1]));
  12580. }
  12581. }
  12582. else {
  12583. if (too[0] === 0) {
  12584. p.push(targetAnchorPosition[0] < sourceAnchorPosition[0] ? point[0] + minorAnchor : point[0] - minorAnchor);
  12585. }
  12586. else {
  12587. p.push(point[0] + (majorAnchor * too[0]));
  12588. }
  12589. if (too[1] === 0) {
  12590. p.push(targetAnchorPosition[1] < sourceAnchorPosition[1] ? point[1] + minorAnchor : point[1] - minorAnchor);
  12591. }
  12592. else {
  12593. p.push(point[1] + (majorAnchor * soo[1]));
  12594. }
  12595. }
  12596. return p;
  12597. };
  12598. this._computeBezier = function (paintInfo, p, sp, tp, _w, _h) {
  12599. var _CP, _CP2,
  12600. _sx = sp[0] < tp[0] ? _w : 0,
  12601. _sy = sp[1] < tp[1] ? _h : 0,
  12602. _tx = sp[0] < tp[0] ? 0 : _w,
  12603. _ty = sp[1] < tp[1] ? 0 : _h;
  12604. _CP = this._findControlPoint([_sx, _sy], sp, tp, p.sourceEndpoint, p.targetEndpoint, paintInfo.so, paintInfo.to);
  12605. _CP2 = this._findControlPoint([_tx, _ty], tp, sp, p.targetEndpoint, p.sourceEndpoint, paintInfo.to, paintInfo.so);
  12606. _super.addSegment(this, "Bezier", {
  12607. x1: _sx, y1: _sy, x2: _tx, y2: _ty,
  12608. cp1x: _CP[0], cp1y: _CP[1], cp2x: _CP2[0], cp2y: _CP2[1]
  12609. });
  12610. };
  12611. };
  12612. _jp.Connectors.Bezier = Bezier;
  12613. _ju.extend(Bezier, _jp.Connectors.AbstractBezierConnector);
  12614. }).call(typeof window !== 'undefined' ? window : this);
  12615. /*
  12616. * This file contains the state machine connectors, which extend AbstractBezierConnector.
  12617. *
  12618. * Copyright (c) 2010 - 2020 jsPlumb (hello@jsplumbtoolkit.com)
  12619. *
  12620. * https://jsplumbtoolkit.com
  12621. * https://github.com/jsplumb/jsplumb
  12622. *
  12623. * Dual licensed under the MIT and GPL2 licenses.
  12624. */
  12625. ;
  12626. (function () {
  12627. "use strict";
  12628. var root = this, _jp = root.jsPlumb, _ju = root.jsPlumbUtil;
  12629. var _segment = function (x1, y1, x2, y2) {
  12630. if (x1 <= x2 && y2 <= y1) {
  12631. return 1;
  12632. }
  12633. else if (x1 <= x2 && y1 <= y2) {
  12634. return 2;
  12635. }
  12636. else if (x2 <= x1 && y2 >= y1) {
  12637. return 3;
  12638. }
  12639. return 4;
  12640. },
  12641. // the control point we will use depends on the faces to which each end of the connection is assigned, specifically whether or not the
  12642. // two faces are parallel or perpendicular. if they are parallel then the control point lies on the midpoint of the axis in which they
  12643. // are parellel and varies only in the other axis; this variation is proportional to the distance that the anchor points lie from the
  12644. // center of that face. if the two faces are perpendicular then the control point is at some distance from both the midpoints; the amount and
  12645. // direction are dependent on the orientation of the two elements. 'seg', passed in to this method, tells you which segment the target element
  12646. // lies in with respect to the source: 1 is top right, 2 is bottom right, 3 is bottom left, 4 is top left.
  12647. //
  12648. // sourcePos and targetPos are arrays of info about where on the source and target each anchor is located. their contents are:
  12649. //
  12650. // 0 - absolute x
  12651. // 1 - absolute y
  12652. // 2 - proportional x in element (0 is left edge, 1 is right edge)
  12653. // 3 - proportional y in element (0 is top edge, 1 is bottom edge)
  12654. //
  12655. _findControlPoint = function (midx, midy, segment, sourceEdge, targetEdge, dx, dy, distance, proximityLimit) {
  12656. // TODO (maybe)
  12657. // - if anchor pos is 0.5, make the control point take into account the relative position of the elements.
  12658. if (distance <= proximityLimit) {
  12659. return [midx, midy];
  12660. }
  12661. if (segment === 1) {
  12662. if (sourceEdge[3] <= 0 && targetEdge[3] >= 1) {
  12663. return [ midx + (sourceEdge[2] < 0.5 ? -1 * dx : dx), midy ];
  12664. }
  12665. else if (sourceEdge[2] >= 1 && targetEdge[2] <= 0) {
  12666. return [ midx, midy + (sourceEdge[3] < 0.5 ? -1 * dy : dy) ];
  12667. }
  12668. else {
  12669. return [ midx + (-1 * dx) , midy + (-1 * dy) ];
  12670. }
  12671. }
  12672. else if (segment === 2) {
  12673. if (sourceEdge[3] >= 1 && targetEdge[3] <= 0) {
  12674. return [ midx + (sourceEdge[2] < 0.5 ? -1 * dx : dx), midy ];
  12675. }
  12676. else if (sourceEdge[2] >= 1 && targetEdge[2] <= 0) {
  12677. return [ midx, midy + (sourceEdge[3] < 0.5 ? -1 * dy : dy) ];
  12678. }
  12679. else {
  12680. return [ midx + dx, midy + (-1 * dy) ];
  12681. }
  12682. }
  12683. else if (segment === 3) {
  12684. if (sourceEdge[3] >= 1 && targetEdge[3] <= 0) {
  12685. return [ midx + (sourceEdge[2] < 0.5 ? -1 * dx : dx), midy ];
  12686. }
  12687. else if (sourceEdge[2] <= 0 && targetEdge[2] >= 1) {
  12688. return [ midx, midy + (sourceEdge[3] < 0.5 ? -1 * dy : dy) ];
  12689. }
  12690. else {
  12691. return [ midx + (-1 * dx) , midy + (-1 * dy) ];
  12692. }
  12693. }
  12694. else if (segment === 4) {
  12695. if (sourceEdge[3] <= 0 && targetEdge[3] >= 1) {
  12696. return [ midx + (sourceEdge[2] < 0.5 ? -1 * dx : dx), midy ];
  12697. }
  12698. else if (sourceEdge[2] <= 0 && targetEdge[2] >= 1) {
  12699. return [ midx, midy + (sourceEdge[3] < 0.5 ? -1 * dy : dy) ];
  12700. }
  12701. else {
  12702. return [ midx + dx , midy + (-1 * dy) ];
  12703. }
  12704. }
  12705. };
  12706. var StateMachine = function (params) {
  12707. params = params || {};
  12708. this.type = "StateMachine";
  12709. var _super = _jp.Connectors.AbstractBezierConnector.apply(this, arguments),
  12710. curviness = params.curviness || 10,
  12711. margin = params.margin || 5,
  12712. proximityLimit = params.proximityLimit || 80,
  12713. clockwise = params.orientation && params.orientation === "clockwise",
  12714. _controlPoint;
  12715. this._computeBezier = function(paintInfo, params, sp, tp, w, h) {
  12716. var _sx = params.sourcePos[0] < params.targetPos[0] ? 0 : w,
  12717. _sy = params.sourcePos[1] < params.targetPos[1] ? 0 : h,
  12718. _tx = params.sourcePos[0] < params.targetPos[0] ? w : 0,
  12719. _ty = params.sourcePos[1] < params.targetPos[1] ? h : 0;
  12720. // now adjust for the margin
  12721. if (params.sourcePos[2] === 0) {
  12722. _sx -= margin;
  12723. }
  12724. if (params.sourcePos[2] === 1) {
  12725. _sx += margin;
  12726. }
  12727. if (params.sourcePos[3] === 0) {
  12728. _sy -= margin;
  12729. }
  12730. if (params.sourcePos[3] === 1) {
  12731. _sy += margin;
  12732. }
  12733. if (params.targetPos[2] === 0) {
  12734. _tx -= margin;
  12735. }
  12736. if (params.targetPos[2] === 1) {
  12737. _tx += margin;
  12738. }
  12739. if (params.targetPos[3] === 0) {
  12740. _ty -= margin;
  12741. }
  12742. if (params.targetPos[3] === 1) {
  12743. _ty += margin;
  12744. }
  12745. //
  12746. // these connectors are quadratic bezier curves, having a single control point. if both anchors
  12747. // are located at 0.5 on their respective faces, the control point is set to the midpoint and you
  12748. // get a straight line. this is also the case if the two anchors are within 'proximityLimit', since
  12749. // it seems to make good aesthetic sense to do that. outside of that, the control point is positioned
  12750. // at 'curviness' pixels away along the normal to the straight line connecting the two anchors.
  12751. //
  12752. // there may be two improvements to this. firstly, we might actually support the notion of avoiding nodes
  12753. // in the UI, or at least making a good effort at doing so. if a connection would pass underneath some node,
  12754. // for example, we might increase the distance the control point is away from the midpoint in a bid to
  12755. // steer it around that node. this will work within limits, but i think those limits would also be the likely
  12756. // limits for, once again, aesthetic good sense in the layout of a chart using these connectors.
  12757. //
  12758. // the second possible change is actually two possible changes: firstly, it is possible we should gradually
  12759. // decrease the 'curviness' as the distance between the anchors decreases; start tailing it off to 0 at some
  12760. // point (which should be configurable). secondly, we might slightly increase the 'curviness' for connectors
  12761. // with respect to how far their anchor is from the center of its respective face. this could either look cool,
  12762. // or stupid, and may indeed work only in a way that is so subtle as to have been a waste of time.
  12763. //
  12764. var _midx = (_sx + _tx) / 2,
  12765. _midy = (_sy + _ty) / 2,
  12766. segment = _segment(_sx, _sy, _tx, _ty),
  12767. distance = Math.sqrt(Math.pow(_tx - _sx, 2) + Math.pow(_ty - _sy, 2)),
  12768. cp1x, cp2x, cp1y, cp2y;
  12769. // calculate the control point. this code will be where we'll put in a rudimentary element avoidance scheme; it
  12770. // will work by extending the control point to force the curve to be, um, curvier.
  12771. _controlPoint = _findControlPoint(_midx,
  12772. _midy,
  12773. segment,
  12774. params.sourcePos,
  12775. params.targetPos,
  12776. curviness, curviness,
  12777. distance,
  12778. proximityLimit);
  12779. cp1x = _controlPoint[0];
  12780. cp2x = _controlPoint[0];
  12781. cp1y = _controlPoint[1];
  12782. cp2y = _controlPoint[1];
  12783. _super.addSegment(this, "Bezier", {
  12784. x1: _tx, y1: _ty, x2: _sx, y2: _sy,
  12785. cp1x: cp1x, cp1y: cp1y,
  12786. cp2x: cp2x, cp2y: cp2y
  12787. });
  12788. };
  12789. };
  12790. _jp.Connectors.StateMachine = StateMachine;
  12791. _ju.extend(StateMachine, _jp.Connectors.AbstractBezierConnector);
  12792. }).call(typeof window !== 'undefined' ? window : this);
  12793. /*
  12794. * This file contains the 'flowchart' connectors, consisting of vertical and horizontal line segments.
  12795. *
  12796. * Copyright (c) 2010 - 2020 jsPlumb (hello@jsplumbtoolkit.com)
  12797. *
  12798. * https://jsplumbtoolkit.com
  12799. * https://github.com/jsplumb/jsplumb
  12800. *
  12801. * Dual licensed under the MIT and GPL2 licenses.
  12802. */
  12803. ;
  12804. (function () {
  12805. "use strict";
  12806. var root = this, _jp = root.jsPlumb, _ju = root.jsPlumbUtil;
  12807. var STRAIGHT = "Straight";
  12808. var Straight = function (params) {
  12809. this.type = STRAIGHT;
  12810. var _super = _jp.Connectors.AbstractConnector.apply(this, arguments);
  12811. this._compute = function (paintInfo, _) {
  12812. _super.addSegment(this, STRAIGHT, {x1: paintInfo.sx, y1: paintInfo.sy, x2: paintInfo.startStubX, y2: paintInfo.startStubY});
  12813. _super.addSegment(this, STRAIGHT, {x1: paintInfo.startStubX, y1: paintInfo.startStubY, x2: paintInfo.endStubX, y2: paintInfo.endStubY});
  12814. _super.addSegment(this, STRAIGHT, {x1: paintInfo.endStubX, y1: paintInfo.endStubY, x2: paintInfo.tx, y2: paintInfo.ty});
  12815. };
  12816. };
  12817. _jp.Connectors.Straight = Straight;
  12818. _ju.extend(Straight, _jp.Connectors.AbstractConnector);
  12819. }).call(typeof window !== 'undefined' ? window : this);
  12820. /*
  12821. * This file contains the SVG renderers.
  12822. *
  12823. * Copyright (c) 2010 - 2020 jsPlumb (hello@jsplumbtoolkit.com)
  12824. *
  12825. * https://jsplumbtoolkit.com
  12826. * https://github.com/jsplumb/jsplumb
  12827. *
  12828. * Dual licensed under the MIT and GPL2 licenses.
  12829. */
  12830. ;
  12831. (function () {
  12832. // ************************** SVG utility methods ********************************************
  12833. "use strict";
  12834. var root = this, _jp = root.jsPlumb, _ju = root.jsPlumbUtil;
  12835. var svgAttributeMap = {
  12836. "stroke-linejoin": "stroke-linejoin",
  12837. "stroke-dashoffset": "stroke-dashoffset",
  12838. "stroke-linecap": "stroke-linecap"
  12839. },
  12840. STROKE_DASHARRAY = "stroke-dasharray",
  12841. DASHSTYLE = "dashstyle",
  12842. LINEAR_GRADIENT = "linearGradient",
  12843. RADIAL_GRADIENT = "radialGradient",
  12844. DEFS = "defs",
  12845. FILL = "fill",
  12846. STOP = "stop",
  12847. STROKE = "stroke",
  12848. STROKE_WIDTH = "stroke-width",
  12849. STYLE = "style",
  12850. NONE = "none",
  12851. JSPLUMB_GRADIENT = "jsplumb_gradient_",
  12852. LINE_WIDTH = "strokeWidth",
  12853. ns = {
  12854. svg: "http://www.w3.org/2000/svg"
  12855. },
  12856. _attr = function (node, attributes) {
  12857. for (var i in attributes) {
  12858. node.setAttribute(i, "" + attributes[i]);
  12859. }
  12860. },
  12861. _node = function (name, attributes) {
  12862. attributes = attributes || {};
  12863. attributes.version = "1.1";
  12864. attributes.xmlns = ns.svg;
  12865. return _jp.createElementNS(ns.svg, name, null, null, attributes);
  12866. },
  12867. _pos = function (d) {
  12868. return "position:absolute;left:" + d[0] + "px;top:" + d[1] + "px";
  12869. },
  12870. _clearGradient = function (parent) {
  12871. var els = parent.querySelectorAll(" defs,linearGradient,radialGradient");
  12872. for (var i = 0; i < els.length; i++) {
  12873. els[i].parentNode.removeChild(els[i]);
  12874. }
  12875. },
  12876. _updateGradient = function (parent, node, style, dimensions, uiComponent) {
  12877. var id = JSPLUMB_GRADIENT + uiComponent._jsPlumb.instance.idstamp();
  12878. // first clear out any existing gradient
  12879. _clearGradient(parent);
  12880. // this checks for an 'offset' property in the gradient, and in the absence of it, assumes
  12881. // we want a linear gradient. if it's there, we create a radial gradient.
  12882. // it is possible that a more explicit means of defining the gradient type would be
  12883. // better. relying on 'offset' means that we can never have a radial gradient that uses
  12884. // some default offset, for instance.
  12885. // issue 244 suggested the 'gradientUnits' attribute; without this, straight/flowchart connectors with gradients would
  12886. // not show gradients when the line was perfectly horizontal or vertical.
  12887. var g;
  12888. if (!style.gradient.offset) {
  12889. g = _node(LINEAR_GRADIENT, {id: id, gradientUnits: "userSpaceOnUse"});
  12890. }
  12891. else {
  12892. g = _node(RADIAL_GRADIENT, { id: id });
  12893. }
  12894. var defs = _node(DEFS);
  12895. parent.appendChild(defs);
  12896. defs.appendChild(g);
  12897. // the svg radial gradient seems to treat stops in the reverse
  12898. // order to how canvas does it. so we want to keep all the maths the same, but
  12899. // iterate the actual style declarations in reverse order, if the x indexes are not in order.
  12900. for (var i = 0; i < style.gradient.stops.length; i++) {
  12901. var styleToUse = uiComponent.segment === 1 || uiComponent.segment === 2 ? i : style.gradient.stops.length - 1 - i,
  12902. stopColor = style.gradient.stops[styleToUse][1],
  12903. s = _node(STOP, {"offset": Math.floor(style.gradient.stops[i][0] * 100) + "%", "stop-color": stopColor});
  12904. g.appendChild(s);
  12905. }
  12906. var applyGradientTo = style.stroke ? STROKE : FILL;
  12907. node.setAttribute(applyGradientTo, "url(#" + id + ")");
  12908. },
  12909. _applyStyles = function (parent, node, style, dimensions, uiComponent) {
  12910. node.setAttribute(FILL, style.fill ? style.fill : NONE);
  12911. node.setAttribute(STROKE, style.stroke ? style.stroke : NONE);
  12912. if (style.gradient) {
  12913. _updateGradient(parent, node, style, dimensions, uiComponent);
  12914. }
  12915. else {
  12916. // make sure we clear any existing gradient
  12917. _clearGradient(parent);
  12918. node.setAttribute(STYLE, "");
  12919. }
  12920. if (style.strokeWidth) {
  12921. node.setAttribute(STROKE_WIDTH, style.strokeWidth);
  12922. }
  12923. // in SVG there is a stroke-dasharray attribute we can set, and its syntax looks like
  12924. // the syntax in VML but is actually kind of nasty: values are given in the pixel
  12925. // coordinate space, whereas in VML they are multiples of the width of the stroked
  12926. // line, which makes a lot more sense. for that reason, jsPlumb is supporting both
  12927. // the native svg 'stroke-dasharray' attribute, and also the 'dashstyle' concept from
  12928. // VML, which will be the preferred method. the code below this converts a dashstyle
  12929. // attribute given in terms of stroke width into a pixel representation, by using the
  12930. // stroke's lineWidth.
  12931. if (style[DASHSTYLE] && style[LINE_WIDTH] && !style[STROKE_DASHARRAY]) {
  12932. var sep = style[DASHSTYLE].indexOf(",") === -1 ? " " : ",",
  12933. parts = style[DASHSTYLE].split(sep),
  12934. styleToUse = "";
  12935. parts.forEach(function (p) {
  12936. styleToUse += (Math.floor(p * style.strokeWidth) + sep);
  12937. });
  12938. node.setAttribute(STROKE_DASHARRAY, styleToUse);
  12939. }
  12940. else if (style[STROKE_DASHARRAY]) {
  12941. node.setAttribute(STROKE_DASHARRAY, style[STROKE_DASHARRAY]);
  12942. }
  12943. // extra attributes such as join type, dash offset.
  12944. for (var i in svgAttributeMap) {
  12945. if (style[i]) {
  12946. node.setAttribute(svgAttributeMap[i], style[i]);
  12947. }
  12948. }
  12949. },
  12950. _appendAtIndex = function (svg, path, idx) {
  12951. if (svg.childNodes.length > idx) {
  12952. svg.insertBefore(path, svg.childNodes[idx]);
  12953. }
  12954. else {
  12955. svg.appendChild(path);
  12956. }
  12957. };
  12958. /**
  12959. utility methods for other objects to use.
  12960. */
  12961. _ju.svg = {
  12962. node: _node,
  12963. attr: _attr,
  12964. pos: _pos
  12965. };
  12966. // ************************** / SVG utility methods ********************************************
  12967. /*
  12968. * Base class for SVG components.
  12969. */
  12970. var SvgComponent = function (params) {
  12971. var pointerEventsSpec = params.pointerEventsSpec || "all", renderer = {};
  12972. _jp.jsPlumbUIComponent.apply(this, params.originalArgs);
  12973. this.canvas = null;
  12974. this.path = null;
  12975. this.svg = null;
  12976. this.bgCanvas = null;
  12977. var clazz = params.cssClass + " " + (params.originalArgs[0].cssClass || ""),
  12978. svgParams = {
  12979. "style": "",
  12980. "width": 0,
  12981. "height": 0,
  12982. "pointer-events": pointerEventsSpec,
  12983. "position": "absolute"
  12984. };
  12985. this.svg = _node("svg", svgParams);
  12986. if (params.useDivWrapper) {
  12987. this.canvas = _jp.createElement("div", { position : "absolute" });
  12988. _ju.sizeElement(this.canvas, 0, 0, 1, 1);
  12989. this.canvas.className = clazz;
  12990. }
  12991. else {
  12992. _attr(this.svg, { "class": clazz });
  12993. this.canvas = this.svg;
  12994. }
  12995. params._jsPlumb.appendElement(this.canvas, params.originalArgs[0].parent);
  12996. if (params.useDivWrapper) {
  12997. this.canvas.appendChild(this.svg);
  12998. }
  12999. var displayElements = [ this.canvas ];
  13000. this.getDisplayElements = function () {
  13001. return displayElements;
  13002. };
  13003. this.appendDisplayElement = function (el) {
  13004. displayElements.push(el);
  13005. };
  13006. this.paint = function (style, anchor, extents) {
  13007. if (style != null) {
  13008. var xy = [ this.x, this.y ], wh = [ this.w, this.h ], p;
  13009. if (extents != null) {
  13010. if (extents.xmin < 0) {
  13011. xy[0] += extents.xmin;
  13012. }
  13013. if (extents.ymin < 0) {
  13014. xy[1] += extents.ymin;
  13015. }
  13016. wh[0] = extents.xmax + ((extents.xmin < 0) ? -extents.xmin : 0);
  13017. wh[1] = extents.ymax + ((extents.ymin < 0) ? -extents.ymin : 0);
  13018. }
  13019. if (params.useDivWrapper) {
  13020. _ju.sizeElement(this.canvas, xy[0], xy[1], wh[0] > 0 ? wh[0] : 1, wh[1] > 0 ? wh[1] : 1);
  13021. xy[0] = 0;
  13022. xy[1] = 0;
  13023. p = _pos([ 0, 0 ]);
  13024. }
  13025. else {
  13026. p = _pos([ xy[0], xy[1] ]);
  13027. }
  13028. renderer.paint.apply(this, arguments);
  13029. _attr(this.svg, {
  13030. "style": p,
  13031. "width": wh[0] || 1,
  13032. "height": wh[1] || 1
  13033. });
  13034. }
  13035. };
  13036. return {
  13037. renderer: renderer
  13038. };
  13039. };
  13040. _ju.extend(SvgComponent, _jp.jsPlumbUIComponent, {
  13041. cleanup: function (force) {
  13042. if (force || this.typeId == null) {
  13043. if (this.canvas) {
  13044. this.canvas._jsPlumb = null;
  13045. }
  13046. if (this.svg) {
  13047. this.svg._jsPlumb = null;
  13048. }
  13049. if (this.bgCanvas) {
  13050. this.bgCanvas._jsPlumb = null;
  13051. }
  13052. if (this.canvas && this.canvas.parentNode) {
  13053. this.canvas.parentNode.removeChild(this.canvas);
  13054. }
  13055. if (this.bgCanvas && this.bgCanvas.parentNode) {
  13056. this.canvas.parentNode.removeChild(this.canvas);
  13057. }
  13058. this.svg = null;
  13059. this.canvas = null;
  13060. this.path = null;
  13061. this.group = null;
  13062. this._jsPlumb = null;
  13063. }
  13064. else {
  13065. // if not a forced cleanup, just detach from DOM for now.
  13066. if (this.canvas && this.canvas.parentNode) {
  13067. this.canvas.parentNode.removeChild(this.canvas);
  13068. }
  13069. if (this.bgCanvas && this.bgCanvas.parentNode) {
  13070. this.bgCanvas.parentNode.removeChild(this.bgCanvas);
  13071. }
  13072. }
  13073. },
  13074. reattach:function(instance) {
  13075. var c = instance.getContainer();
  13076. if (this.canvas && this.canvas.parentNode == null) {
  13077. c.appendChild(this.canvas);
  13078. }
  13079. if (this.bgCanvas && this.bgCanvas.parentNode == null) {
  13080. c.appendChild(this.bgCanvas);
  13081. }
  13082. },
  13083. setVisible: function (v) {
  13084. if (this.canvas) {
  13085. this.canvas.style.display = v ? "block" : "none";
  13086. }
  13087. }
  13088. });
  13089. /*
  13090. * Base class for SVG connectors.
  13091. */
  13092. _jp.ConnectorRenderers.svg = function (params) {
  13093. var self = this,
  13094. _super = SvgComponent.apply(this, [
  13095. {
  13096. cssClass: params._jsPlumb.connectorClass,
  13097. originalArgs: arguments,
  13098. pointerEventsSpec: "none",
  13099. _jsPlumb: params._jsPlumb
  13100. }
  13101. ]);
  13102. _super.renderer.paint = function (style, anchor, extents) {
  13103. var segments = self.getSegments(), p = "", offset = [0, 0];
  13104. if (extents.xmin < 0) {
  13105. offset[0] = -extents.xmin;
  13106. }
  13107. if (extents.ymin < 0) {
  13108. offset[1] = -extents.ymin;
  13109. }
  13110. if (segments.length > 0) {
  13111. p = self.getPathData();
  13112. var a = {
  13113. d: p,
  13114. transform: "translate(" + offset[0] + "," + offset[1] + ")",
  13115. "pointer-events": params["pointer-events"] || "visibleStroke"
  13116. },
  13117. outlineStyle = null,
  13118. d = [self.x, self.y, self.w, self.h];
  13119. // outline style. actually means drawing an svg object underneath the main one.
  13120. if (style.outlineStroke) {
  13121. var outlineWidth = style.outlineWidth || 1,
  13122. outlineStrokeWidth = style.strokeWidth + (2 * outlineWidth);
  13123. outlineStyle = _jp.extend({}, style);
  13124. delete outlineStyle.gradient;
  13125. outlineStyle.stroke = style.outlineStroke;
  13126. outlineStyle.strokeWidth = outlineStrokeWidth;
  13127. if (self.bgPath == null) {
  13128. self.bgPath = _node("path", a);
  13129. _jp.addClass(self.bgPath, _jp.connectorOutlineClass);
  13130. _appendAtIndex(self.svg, self.bgPath, 0);
  13131. }
  13132. else {
  13133. _attr(self.bgPath, a);
  13134. }
  13135. _applyStyles(self.svg, self.bgPath, outlineStyle, d, self);
  13136. }
  13137. if (self.path == null) {
  13138. self.path = _node("path", a);
  13139. _appendAtIndex(self.svg, self.path, style.outlineStroke ? 1 : 0);
  13140. }
  13141. else {
  13142. _attr(self.path, a);
  13143. }
  13144. _applyStyles(self.svg, self.path, style, d, self);
  13145. }
  13146. };
  13147. };
  13148. _ju.extend(_jp.ConnectorRenderers.svg, SvgComponent);
  13149. // ******************************* svg segment renderer *****************************************************
  13150. // ******************************* /svg segments *****************************************************
  13151. /*
  13152. * Base class for SVG endpoints.
  13153. */
  13154. var SvgEndpoint = _jp.SvgEndpoint = function (params) {
  13155. var _super = SvgComponent.apply(this, [
  13156. {
  13157. cssClass: params._jsPlumb.endpointClass,
  13158. originalArgs: arguments,
  13159. pointerEventsSpec: "all",
  13160. useDivWrapper: true,
  13161. _jsPlumb: params._jsPlumb
  13162. }
  13163. ]);
  13164. _super.renderer.paint = function (style) {
  13165. var s = _jp.extend({}, style);
  13166. if (s.outlineStroke) {
  13167. s.stroke = s.outlineStroke;
  13168. }
  13169. if (this.node == null) {
  13170. this.node = this.makeNode(s);
  13171. this.svg.appendChild(this.node);
  13172. }
  13173. else if (this.updateNode != null) {
  13174. this.updateNode(this.node);
  13175. }
  13176. _applyStyles(this.svg, this.node, s, [ this.x, this.y, this.w, this.h ], this);
  13177. _pos(this.node, [ this.x, this.y ]);
  13178. }.bind(this);
  13179. };
  13180. _ju.extend(SvgEndpoint, SvgComponent);
  13181. /*
  13182. * SVG Dot Endpoint
  13183. */
  13184. _jp.Endpoints.svg.Dot = function () {
  13185. _jp.Endpoints.Dot.apply(this, arguments);
  13186. SvgEndpoint.apply(this, arguments);
  13187. this.makeNode = function (style) {
  13188. return _node("circle", {
  13189. "cx": this.w / 2,
  13190. "cy": this.h / 2,
  13191. "r": this.radius
  13192. });
  13193. };
  13194. this.updateNode = function (node) {
  13195. _attr(node, {
  13196. "cx": this.w / 2,
  13197. "cy": this.h / 2,
  13198. "r": this.radius
  13199. });
  13200. };
  13201. };
  13202. _ju.extend(_jp.Endpoints.svg.Dot, [_jp.Endpoints.Dot, SvgEndpoint]);
  13203. /*
  13204. * SVG Rectangle Endpoint
  13205. */
  13206. _jp.Endpoints.svg.Rectangle = function () {
  13207. _jp.Endpoints.Rectangle.apply(this, arguments);
  13208. SvgEndpoint.apply(this, arguments);
  13209. this.makeNode = function (style) {
  13210. return _node("rect", {
  13211. "width": this.w,
  13212. "height": this.h
  13213. });
  13214. };
  13215. this.updateNode = function (node) {
  13216. _attr(node, {
  13217. "width": this.w,
  13218. "height": this.h
  13219. });
  13220. };
  13221. };
  13222. _ju.extend(_jp.Endpoints.svg.Rectangle, [_jp.Endpoints.Rectangle, SvgEndpoint]);
  13223. /*
  13224. * SVG Image Endpoint is the default image endpoint.
  13225. */
  13226. _jp.Endpoints.svg.Image = _jp.Endpoints.Image;
  13227. /*
  13228. * Blank endpoint in svg renderer is the default Blank endpoint.
  13229. */
  13230. _jp.Endpoints.svg.Blank = _jp.Endpoints.Blank;
  13231. /*
  13232. * Label overlay in svg renderer is the default Label overlay.
  13233. */
  13234. _jp.Overlays.svg.Label = _jp.Overlays.Label;
  13235. /*
  13236. * Custom overlay in svg renderer is the default Custom overlay.
  13237. */
  13238. _jp.Overlays.svg.Custom = _jp.Overlays.Custom;
  13239. var AbstractSvgArrowOverlay = function (superclass, originalArgs) {
  13240. superclass.apply(this, originalArgs);
  13241. _jp.jsPlumbUIComponent.apply(this, originalArgs);
  13242. this.isAppendedAtTopLevel = false;
  13243. var self = this;
  13244. this.path = null;
  13245. this.paint = function (params, containerExtents) {
  13246. // only draws on connections, not endpoints.
  13247. if (params.component.svg && containerExtents) {
  13248. if (this.path == null) {
  13249. this.path = _node("path", {
  13250. "pointer-events": "all"
  13251. });
  13252. params.component.svg.appendChild(this.path);
  13253. if (this.elementCreated) {
  13254. this.elementCreated(this.path, params.component);
  13255. }
  13256. this.canvas = params.component.svg; // for the sake of completeness; this behaves the same as other overlays
  13257. }
  13258. var clazz = originalArgs && (originalArgs.length === 1) ? (originalArgs[0].cssClass || "") : "",
  13259. offset = [0, 0];
  13260. if (containerExtents.xmin < 0) {
  13261. offset[0] = -containerExtents.xmin;
  13262. }
  13263. if (containerExtents.ymin < 0) {
  13264. offset[1] = -containerExtents.ymin;
  13265. }
  13266. _attr(this.path, {
  13267. "d": makePath(params.d),
  13268. "class": clazz,
  13269. stroke: params.stroke ? params.stroke : null,
  13270. fill: params.fill ? params.fill : null,
  13271. transform: "translate(" + offset[0] + "," + offset[1] + ")"
  13272. });
  13273. }
  13274. };
  13275. var makePath = function (d) {
  13276. return (isNaN(d.cxy.x) || isNaN(d.cxy.y)) ? "" : "M" + d.hxy.x + "," + d.hxy.y +
  13277. " L" + d.tail[0].x + "," + d.tail[0].y +
  13278. " L" + d.cxy.x + "," + d.cxy.y +
  13279. " L" + d.tail[1].x + "," + d.tail[1].y +
  13280. " L" + d.hxy.x + "," + d.hxy.y;
  13281. };
  13282. this.transfer = function(target) {
  13283. if (target.canvas && this.path && this.path.parentNode) {
  13284. this.path.parentNode.removeChild(this.path);
  13285. target.canvas.appendChild(this.path);
  13286. }
  13287. };
  13288. };
  13289. var svgProtoFunctions = {
  13290. cleanup : function (force) {
  13291. if (this.path != null) {
  13292. if (force) {
  13293. this._jsPlumb.instance.removeElement(this.path);
  13294. }
  13295. else {
  13296. if (this.path.parentNode) {
  13297. this.path.parentNode.removeChild(this.path);
  13298. }
  13299. }
  13300. }
  13301. }, reattach :function(instance, component) {
  13302. if (this.path && component.canvas) {
  13303. component.canvas.appendChild(this.path);
  13304. }
  13305. },
  13306. setVisible : function (v) {
  13307. if (this.path != null) {
  13308. (this.path.style.display = (v ? "block" : "none"));
  13309. }
  13310. }
  13311. };
  13312. _ju.extend(AbstractSvgArrowOverlay, [_jp.jsPlumbUIComponent, _jp.Overlays.AbstractOverlay]);
  13313. _jp.Overlays.svg.Arrow = function () {
  13314. AbstractSvgArrowOverlay.apply(this, [_jp.Overlays.Arrow, arguments]);
  13315. };
  13316. _ju.extend(_jp.Overlays.svg.Arrow, [ _jp.Overlays.Arrow, AbstractSvgArrowOverlay ], svgProtoFunctions);
  13317. _jp.Overlays.svg.PlainArrow = function () {
  13318. AbstractSvgArrowOverlay.apply(this, [_jp.Overlays.PlainArrow, arguments]);
  13319. };
  13320. _ju.extend(_jp.Overlays.svg.PlainArrow, [ _jp.Overlays.PlainArrow, AbstractSvgArrowOverlay ], svgProtoFunctions);
  13321. _jp.Overlays.svg.Diamond = function () {
  13322. AbstractSvgArrowOverlay.apply(this, [_jp.Overlays.Diamond, arguments]);
  13323. };
  13324. _ju.extend(_jp.Overlays.svg.Diamond, [ _jp.Overlays.Diamond, AbstractSvgArrowOverlay ], svgProtoFunctions);
  13325. // a test
  13326. _jp.Overlays.svg.GuideLines = function () {
  13327. var path = null, self = this, p1_1, p1_2;
  13328. _jp.Overlays.GuideLines.apply(this, arguments);
  13329. this.paint = function (params, containerExtents) {
  13330. if (path == null) {
  13331. path = _node("path");
  13332. params.connector.svg.appendChild(path);
  13333. self.attachListeners(path, params.connector);
  13334. self.attachListeners(path, self);
  13335. p1_1 = _node("path");
  13336. params.connector.svg.appendChild(p1_1);
  13337. self.attachListeners(p1_1, params.connector);
  13338. self.attachListeners(p1_1, self);
  13339. p1_2 = _node("path");
  13340. params.connector.svg.appendChild(p1_2);
  13341. self.attachListeners(p1_2, params.connector);
  13342. self.attachListeners(p1_2, self);
  13343. }
  13344. var offset = [0, 0];
  13345. if (containerExtents.xmin < 0) {
  13346. offset[0] = -containerExtents.xmin;
  13347. }
  13348. if (containerExtents.ymin < 0) {
  13349. offset[1] = -containerExtents.ymin;
  13350. }
  13351. _attr(path, {
  13352. "d": makePath(params.head, params.tail),
  13353. stroke: "red",
  13354. fill: null,
  13355. transform: "translate(" + offset[0] + "," + offset[1] + ")"
  13356. });
  13357. _attr(p1_1, {
  13358. "d": makePath(params.tailLine[0], params.tailLine[1]),
  13359. stroke: "blue",
  13360. fill: null,
  13361. transform: "translate(" + offset[0] + "," + offset[1] + ")"
  13362. });
  13363. _attr(p1_2, {
  13364. "d": makePath(params.headLine[0], params.headLine[1]),
  13365. stroke: "green",
  13366. fill: null,
  13367. transform: "translate(" + offset[0] + "," + offset[1] + ")"
  13368. });
  13369. };
  13370. var makePath = function (d1, d2) {
  13371. return "M " + d1.x + "," + d1.y +
  13372. " L" + d2.x + "," + d2.y;
  13373. };
  13374. };
  13375. _ju.extend(_jp.Overlays.svg.GuideLines, _jp.Overlays.GuideLines);
  13376. }).call(typeof window !== 'undefined' ? window : this);
  13377. /*
  13378. * This file contains code used when jsPlumb is being rendered in a DOM.
  13379. *
  13380. * Copyright (c) 2010 - 2020 jsPlumb (hello@jsplumbtoolkit.com)
  13381. *
  13382. * https://jsplumbtoolkit.com
  13383. * https://github.com/jsplumb/jsplumb
  13384. *
  13385. * Dual licensed under the MIT and GPL2 licenses.
  13386. */
  13387. ;
  13388. (function () {
  13389. "use strict";
  13390. var root = this, _jp = root.jsPlumb, _ju = root.jsPlumbUtil,
  13391. _jk = root.Katavorio, _jg = root.Biltong;
  13392. var _getEventManager = function(instance) {
  13393. var e = instance._mottle;
  13394. if (!e) {
  13395. e = instance._mottle = new root.Mottle();
  13396. }
  13397. return e;
  13398. };
  13399. var _getDragManager = function (instance, category) {
  13400. category = category || "main";
  13401. var key = "_katavorio_" + category;
  13402. var k = instance[key],
  13403. e = instance.getEventManager();
  13404. if (!k) {
  13405. k = new _jk({
  13406. bind: e.on,
  13407. unbind: e.off,
  13408. getSize: _jp.getSize,
  13409. getConstrainingRectangle:function(el) {
  13410. return [ el.parentNode.scrollWidth, el.parentNode.scrollHeight ];
  13411. },
  13412. getPosition: function (el, relativeToRoot) {
  13413. // if this is a nested draggable then compute the offset against its own offsetParent, otherwise
  13414. // compute against the Container's origin. see also the getUIPosition method below.
  13415. var o = instance.getOffset(el, relativeToRoot, el._katavorioDrag ? el.offsetParent : null);
  13416. return [o.left, o.top];
  13417. },
  13418. setPosition: function (el, xy) {
  13419. el.style.left = xy[0] + "px";
  13420. el.style.top = xy[1] + "px";
  13421. },
  13422. addClass: _jp.addClass,
  13423. removeClass: _jp.removeClass,
  13424. intersects: _jg.intersects,
  13425. indexOf: function(l, i) { return l.indexOf(i); },
  13426. scope:instance.getDefaultScope(),
  13427. css: {
  13428. noSelect: instance.dragSelectClass,
  13429. droppable: "jtk-droppable",
  13430. draggable: "jtk-draggable",
  13431. drag: "jtk-drag",
  13432. selected: "jtk-drag-selected",
  13433. active: "jtk-drag-active",
  13434. hover: "jtk-drag-hover",
  13435. ghostProxy:"jtk-ghost-proxy"
  13436. }
  13437. });
  13438. k.setZoom(instance.getZoom());
  13439. instance[key] = k;
  13440. instance.bind("zoom", k.setZoom);
  13441. }
  13442. return k;
  13443. };
  13444. var _dragStart=function(params) {
  13445. var options = params.el._jsPlumbDragOptions;
  13446. var cont = true;
  13447. if (options.canDrag) {
  13448. cont = options.canDrag();
  13449. }
  13450. if (cont) {
  13451. this.setHoverSuspended(true);
  13452. this.select({source: params.el}).addClass(this.elementDraggingClass + " " + this.sourceElementDraggingClass, true);
  13453. this.select({target: params.el}).addClass(this.elementDraggingClass + " " + this.targetElementDraggingClass, true);
  13454. this.setConnectionBeingDragged(true);
  13455. }
  13456. return cont;
  13457. };
  13458. var _dragMove=function(params) {
  13459. var ui = this.getUIPosition(arguments, this.getZoom());
  13460. if (ui != null) {
  13461. var o = params.el._jsPlumbDragOptions;
  13462. this.draw(params.el, ui, null, true);
  13463. if (o._dragging) {
  13464. this.addClass(params.el, "jtk-dragged");
  13465. }
  13466. o._dragging = true;
  13467. }
  13468. };
  13469. var _dragStop=function(params) {
  13470. var elements = params.selection, uip;
  13471. var _one = function (_e) {
  13472. var drawResult;
  13473. if (_e[1] != null) {
  13474. // run the reported offset through the code that takes parent containers
  13475. // into account, to adjust if necessary (issue 554)
  13476. uip = this.getUIPosition([{
  13477. el:_e[2].el,
  13478. pos:[_e[1].left, _e[1].top]
  13479. }]);
  13480. drawResult = this.draw(_e[2].el, uip);
  13481. }
  13482. if (_e[0]._jsPlumbDragOptions != null) {
  13483. delete _e[0]._jsPlumbDragOptions._dragging;
  13484. }
  13485. this.removeClass(_e[0], "jtk-dragged");
  13486. this.select({source: _e[2].el}).removeClass(this.elementDraggingClass + " " + this.sourceElementDraggingClass, true);
  13487. this.select({target: _e[2].el}).removeClass(this.elementDraggingClass + " " + this.targetElementDraggingClass, true);
  13488. params.e._drawResult = params.e._drawResult || {c:[],e:[], a:[]};
  13489. Array.prototype.push.apply(params.e._drawResult.c, drawResult.c);
  13490. Array.prototype.push.apply(params.e._drawResult.e, drawResult.e);
  13491. Array.prototype.push.apply(params.e._drawResult.a, drawResult.a);
  13492. this.getDragManager().dragEnded(_e[2].el);
  13493. }.bind(this);
  13494. for (var i = 0; i < elements.length; i++) {
  13495. _one(elements[i]);
  13496. }
  13497. this.setHoverSuspended(false);
  13498. this.setConnectionBeingDragged(false);
  13499. };
  13500. var _animProps = function (o, p) {
  13501. var _one = function (pName) {
  13502. if (p[pName] != null) {
  13503. if (_ju.isString(p[pName])) {
  13504. var m = p[pName].match(/-=/) ? -1 : 1,
  13505. v = p[pName].substring(2);
  13506. return o[pName] + (m * v);
  13507. }
  13508. else {
  13509. return p[pName];
  13510. }
  13511. }
  13512. else {
  13513. return o[pName];
  13514. }
  13515. };
  13516. return [ _one("left"), _one("top") ];
  13517. };
  13518. var _genLoc = function (prefix, e) {
  13519. if (e == null) {
  13520. return [ 0, 0 ];
  13521. }
  13522. var ts = _touches(e), t = _getTouch(ts, 0);
  13523. return [t[prefix + "X"], t[prefix + "Y"]];
  13524. },
  13525. _pageLocation = _genLoc.bind(this, "page"),
  13526. _screenLocation = _genLoc.bind(this, "screen"),
  13527. _clientLocation = _genLoc.bind(this, "client"),
  13528. _getTouch = function (touches, idx) {
  13529. return touches.item ? touches.item(idx) : touches[idx];
  13530. },
  13531. _touches = function (e) {
  13532. return e.touches && e.touches.length > 0 ? e.touches :
  13533. e.changedTouches && e.changedTouches.length > 0 ? e.changedTouches :
  13534. e.targetTouches && e.targetTouches.length > 0 ? e.targetTouches :
  13535. [ e ];
  13536. };
  13537. /**
  13538. Manages dragging for some instance of jsPlumb.
  13539. TODO instead of this being accessed directly, it should subscribe to events on the jsPlumb instance: every method
  13540. in here is called directly by jsPlumb. But what should happen is that we have unpublished events that this listens
  13541. to. The only trick is getting one of these instantiated with every jsPlumb instance: it needs to have a hook somehow.
  13542. Basically the general idea is to pull ALL the drag code out (prototype method registrations plus this) into a
  13543. dedicated drag script), that does not necessarily need to be included.
  13544. */
  13545. var DragManager = function (_currentInstance) {
  13546. var _draggables = {}, _dlist = [], _delements = {}, _elementsWithEndpoints = {},
  13547. // elementids mapped to the draggable to which they belong.
  13548. _draggablesForElements = {};
  13549. /**
  13550. register some element as draggable. right now the drag init stuff is done elsewhere, and it is
  13551. possible that will continue to be the case.
  13552. */
  13553. this.register = function (el) {
  13554. var id = _currentInstance.getId(el),
  13555. parentOffset;
  13556. if (!_draggables[id]) {
  13557. _draggables[id] = el;
  13558. _dlist.push(el);
  13559. _delements[id] = {};
  13560. }
  13561. // look for child elements that have endpoints and register them against this draggable.
  13562. var _oneLevel = function (p) {
  13563. if (p) {
  13564. for (var i = 0; i < p.childNodes.length; i++) {
  13565. if (p.childNodes[i].nodeType !== 3 && p.childNodes[i].nodeType !== 8) {
  13566. var cEl = jsPlumb.getElement(p.childNodes[i]),
  13567. cid = _currentInstance.getId(p.childNodes[i], null, true);
  13568. if (cid && _elementsWithEndpoints[cid] && _elementsWithEndpoints[cid] > 0) {
  13569. if (!parentOffset) {
  13570. parentOffset = _currentInstance.getOffset(el);
  13571. }
  13572. var cOff = _currentInstance.getOffset(cEl);
  13573. _delements[id][cid] = {
  13574. id: cid,
  13575. offset: {
  13576. left: cOff.left - parentOffset.left,
  13577. top: cOff.top - parentOffset.top
  13578. }
  13579. };
  13580. _draggablesForElements[cid] = id;
  13581. }
  13582. _oneLevel(p.childNodes[i]);
  13583. }
  13584. }
  13585. }
  13586. };
  13587. _oneLevel(el);
  13588. };
  13589. // refresh the offsets for child elements of this element.
  13590. this.updateOffsets = function (elId, childOffsetOverrides) {
  13591. if (elId != null) {
  13592. childOffsetOverrides = childOffsetOverrides || {};
  13593. var domEl = jsPlumb.getElement(elId),
  13594. id = _currentInstance.getId(domEl),
  13595. children = _delements[id],
  13596. parentOffset;
  13597. if (children) {
  13598. for (var i in children) {
  13599. if (children.hasOwnProperty(i)) {
  13600. var cel = jsPlumb.getElement(i),
  13601. cOff = childOffsetOverrides[i] || _currentInstance.getOffset(cel);
  13602. // do not update if we have a value already and we'd just be writing 0,0
  13603. if (cel.offsetParent == null && _delements[id][i] != null) {
  13604. continue;
  13605. }
  13606. if (!parentOffset) {
  13607. parentOffset = _currentInstance.getOffset(domEl);
  13608. }
  13609. _delements[id][i] = {
  13610. id: i,
  13611. offset: {
  13612. left: cOff.left - parentOffset.left,
  13613. top: cOff.top - parentOffset.top
  13614. }
  13615. };
  13616. _draggablesForElements[i] = id;
  13617. }
  13618. }
  13619. }
  13620. }
  13621. };
  13622. /**
  13623. notification that an endpoint was added to the given el. we go up from that el's parent
  13624. node, looking for a parent that has been registered as a draggable. if we find one, we add this
  13625. el to that parent's list of elements to update on drag (if it is not there already)
  13626. */
  13627. this.endpointAdded = function (el, id) {
  13628. id = id || _currentInstance.getId(el);
  13629. var b = document.body,
  13630. p = el.parentNode;
  13631. _elementsWithEndpoints[id] = _elementsWithEndpoints[id] ? _elementsWithEndpoints[id] + 1 : 1;
  13632. while (p != null && p !== b) {
  13633. var pid = _currentInstance.getId(p, null, true);
  13634. if (pid && _draggables[pid]) {
  13635. var pLoc = _currentInstance.getOffset(p);
  13636. if (_delements[pid][id] == null) {
  13637. var cLoc = _currentInstance.getOffset(el);
  13638. _delements[pid][id] = {
  13639. id: id,
  13640. offset: {
  13641. left: cLoc.left - pLoc.left,
  13642. top: cLoc.top - pLoc.top
  13643. }
  13644. };
  13645. _draggablesForElements[id] = pid;
  13646. }
  13647. break;
  13648. }
  13649. p = p.parentNode;
  13650. }
  13651. };
  13652. this.endpointDeleted = function (endpoint) {
  13653. if (_elementsWithEndpoints[endpoint.elementId]) {
  13654. _elementsWithEndpoints[endpoint.elementId]--;
  13655. if (_elementsWithEndpoints[endpoint.elementId] <= 0) {
  13656. for (var i in _delements) {
  13657. if (_delements.hasOwnProperty(i) && _delements[i]) {
  13658. delete _delements[i][endpoint.elementId];
  13659. delete _draggablesForElements[endpoint.elementId];
  13660. }
  13661. }
  13662. }
  13663. }
  13664. };
  13665. this.changeId = function (oldId, newId) {
  13666. _delements[newId] = _delements[oldId];
  13667. _delements[oldId] = {};
  13668. _draggablesForElements[newId] = _draggablesForElements[oldId];
  13669. _draggablesForElements[oldId] = null;
  13670. };
  13671. this.getElementsForDraggable = function (id) {
  13672. return _delements[id];
  13673. };
  13674. this.elementRemoved = function (elementId) {
  13675. var elId = _draggablesForElements[elementId];
  13676. if (elId) {
  13677. _delements[elId] && delete _delements[elId][elementId];
  13678. delete _draggablesForElements[elementId];
  13679. }
  13680. };
  13681. this.reset = function () {
  13682. _draggables = {};
  13683. _dlist = [];
  13684. _delements = {};
  13685. _elementsWithEndpoints = {};
  13686. };
  13687. //
  13688. // notification drag ended. We check automatically if need to update some
  13689. // ancestor's offsets.
  13690. //
  13691. this.dragEnded = function (el) {
  13692. if (el.offsetParent != null) {
  13693. var id = _currentInstance.getId(el),
  13694. ancestor = _draggablesForElements[id];
  13695. if (ancestor) {
  13696. this.updateOffsets(ancestor);
  13697. }
  13698. }
  13699. };
  13700. this.setParent = function (el, elId, p, pId, currentChildLocation) {
  13701. var current = _draggablesForElements[elId];
  13702. if (!_delements[pId]) {
  13703. _delements[pId] = {};
  13704. }
  13705. var pLoc = _currentInstance.getOffset(p),
  13706. cLoc = currentChildLocation || _currentInstance.getOffset(el);
  13707. if (current && _delements[current]) {
  13708. delete _delements[current][elId];
  13709. }
  13710. _delements[pId][elId] = {
  13711. id:elId,
  13712. offset : {
  13713. left: cLoc.left - pLoc.left,
  13714. top: cLoc.top - pLoc.top
  13715. }
  13716. };
  13717. _draggablesForElements[elId] = pId;
  13718. };
  13719. this.clearParent = function(el, elId) {
  13720. var current = _draggablesForElements[elId];
  13721. if (current) {
  13722. delete _delements[current][elId];
  13723. delete _draggablesForElements[elId];
  13724. }
  13725. };
  13726. this.revalidateParent = function(el, elId, childOffset) {
  13727. var current = _draggablesForElements[elId];
  13728. if (current) {
  13729. var co = {};
  13730. co[elId] = childOffset;
  13731. this.updateOffsets(current, co);
  13732. _currentInstance.revalidate(current);
  13733. }
  13734. };
  13735. this.getDragAncestor = function (el) {
  13736. var de = jsPlumb.getElement(el),
  13737. id = _currentInstance.getId(de),
  13738. aid = _draggablesForElements[id];
  13739. if (aid) {
  13740. return jsPlumb.getElement(aid);
  13741. }
  13742. else {
  13743. return null;
  13744. }
  13745. };
  13746. };
  13747. var _setClassName = function (el, cn, classList) {
  13748. cn = _ju.fastTrim(cn);
  13749. if (typeof el.className.baseVal !== "undefined") {
  13750. el.className.baseVal = cn;
  13751. }
  13752. else {
  13753. el.className = cn;
  13754. }
  13755. // recent (i currently have 61.0.3163.100) version of chrome do not update classList when you set the base val
  13756. // of an svg element's className. in the long run we'd like to move to just using classList anyway
  13757. try {
  13758. var cl = el.classList;
  13759. if (cl != null) {
  13760. while (cl.length > 0) {
  13761. cl.remove(cl.item(0));
  13762. }
  13763. for (var i = 0; i < classList.length; i++) {
  13764. if (classList[i]) {
  13765. cl.add(classList[i]);
  13766. }
  13767. }
  13768. }
  13769. }
  13770. catch(e) {
  13771. // not fatal
  13772. _ju.log("JSPLUMB: cannot set class list", e);
  13773. }
  13774. },
  13775. _getClassName = function (el) {
  13776. return (typeof el.className.baseVal === "undefined") ? el.className : el.className.baseVal;
  13777. },
  13778. _classManip = function (el, classesToAdd, classesToRemove) {
  13779. classesToAdd = classesToAdd == null ? [] : _ju.isArray(classesToAdd) ? classesToAdd : classesToAdd.split(/\s+/);
  13780. classesToRemove = classesToRemove == null ? [] : _ju.isArray(classesToRemove) ? classesToRemove : classesToRemove.split(/\s+/);
  13781. var className = _getClassName(el),
  13782. curClasses = className.split(/\s+/);
  13783. var _oneSet = function (add, classes) {
  13784. for (var i = 0; i < classes.length; i++) {
  13785. if (add) {
  13786. if (curClasses.indexOf(classes[i]) === -1) {
  13787. curClasses.push(classes[i]);
  13788. }
  13789. }
  13790. else {
  13791. var idx = curClasses.indexOf(classes[i]);
  13792. if (idx !== -1) {
  13793. curClasses.splice(idx, 1);
  13794. }
  13795. }
  13796. }
  13797. };
  13798. _oneSet(true, classesToAdd);
  13799. _oneSet(false, classesToRemove);
  13800. _setClassName(el, curClasses.join(" "), curClasses);
  13801. };
  13802. root.jsPlumb.extend(root.jsPlumbInstance.prototype, {
  13803. headless: false,
  13804. pageLocation: _pageLocation,
  13805. screenLocation: _screenLocation,
  13806. clientLocation: _clientLocation,
  13807. getDragManager:function() {
  13808. if (this.dragManager == null) {
  13809. this.dragManager = new DragManager(this);
  13810. }
  13811. return this.dragManager;
  13812. },
  13813. recalculateOffsets:function(elId) {
  13814. this.getDragManager().updateOffsets(elId);
  13815. },
  13816. createElement:function(tag, style, clazz, atts) {
  13817. return this.createElementNS(null, tag, style, clazz, atts);
  13818. },
  13819. createElementNS:function(ns, tag, style, clazz, atts) {
  13820. var e = ns == null ? document.createElement(tag) : document.createElementNS(ns, tag);
  13821. var i;
  13822. style = style || {};
  13823. for (i in style) {
  13824. e.style[i] = style[i];
  13825. }
  13826. if (clazz) {
  13827. e.className = clazz;
  13828. }
  13829. atts = atts || {};
  13830. for (i in atts) {
  13831. e.setAttribute(i, "" + atts[i]);
  13832. }
  13833. return e;
  13834. },
  13835. getAttribute: function (el, attName) {
  13836. return el.getAttribute != null ? el.getAttribute(attName) : null;
  13837. },
  13838. setAttribute: function (el, a, v) {
  13839. if (el.setAttribute != null) {
  13840. el.setAttribute(a, v);
  13841. }
  13842. },
  13843. setAttributes: function (el, atts) {
  13844. for (var i in atts) {
  13845. if (atts.hasOwnProperty(i)) {
  13846. el.setAttribute(i, atts[i]);
  13847. }
  13848. }
  13849. },
  13850. appendToRoot: function (node) {
  13851. document.body.appendChild(node);
  13852. },
  13853. getRenderModes: function () {
  13854. return [ "svg" ];
  13855. },
  13856. getClass:_getClassName,
  13857. addClass: function (el, clazz) {
  13858. jsPlumb.each(el, function (e) {
  13859. _classManip(e, clazz);
  13860. });
  13861. },
  13862. hasClass: function (el, clazz) {
  13863. el = jsPlumb.getElement(el);
  13864. if (el.classList) {
  13865. return el.classList.contains(clazz);
  13866. }
  13867. else {
  13868. return _getClassName(el).indexOf(clazz) !== -1;
  13869. }
  13870. },
  13871. removeClass: function (el, clazz) {
  13872. jsPlumb.each(el, function (e) {
  13873. _classManip(e, null, clazz);
  13874. });
  13875. },
  13876. toggleClass:function(el, clazz) {
  13877. if (jsPlumb.hasClass(el, clazz)) {
  13878. jsPlumb.removeClass(el, clazz);
  13879. } else {
  13880. jsPlumb.addClass(el, clazz);
  13881. }
  13882. },
  13883. updateClasses: function (el, toAdd, toRemove) {
  13884. jsPlumb.each(el, function (e) {
  13885. _classManip(e, toAdd, toRemove);
  13886. });
  13887. },
  13888. setClass: function (el, clazz) {
  13889. if (clazz != null) {
  13890. jsPlumb.each(el, function (e) {
  13891. _setClassName(e, clazz, clazz.split(/\s+/));
  13892. });
  13893. }
  13894. },
  13895. setPosition: function (el, p) {
  13896. el.style.left = p.left + "px";
  13897. el.style.top = p.top + "px";
  13898. },
  13899. getPosition: function (el) {
  13900. var _one = function (prop) {
  13901. var v = el.style[prop];
  13902. return v ? v.substring(0, v.length - 2) : 0;
  13903. };
  13904. return {
  13905. left: _one("left"),
  13906. top: _one("top")
  13907. };
  13908. },
  13909. getStyle:function(el, prop) {
  13910. if (typeof window.getComputedStyle !== 'undefined') {
  13911. return getComputedStyle(el, null).getPropertyValue(prop);
  13912. } else {
  13913. return el.currentStyle[prop];
  13914. }
  13915. },
  13916. getSelector: function (ctx, spec) {
  13917. var sel = null;
  13918. if (arguments.length === 1) {
  13919. sel = ctx.nodeType != null ? ctx : document.querySelectorAll(ctx);
  13920. }
  13921. else {
  13922. sel = ctx.querySelectorAll(spec);
  13923. }
  13924. return sel;
  13925. },
  13926. getOffset:function(el, relativeToRoot, container) {
  13927. el = jsPlumb.getElement(el);
  13928. container = container || this.getContainer();
  13929. var out = {
  13930. left: el.offsetLeft,
  13931. top: el.offsetTop
  13932. },
  13933. op = (relativeToRoot || (container != null && (el !== container && el.offsetParent !== container))) ? el.offsetParent : null,
  13934. _maybeAdjustScroll = function(offsetParent) {
  13935. if (offsetParent != null && offsetParent !== document.body && (offsetParent.scrollTop > 0 || offsetParent.scrollLeft > 0)) {
  13936. out.left -= offsetParent.scrollLeft;
  13937. out.top -= offsetParent.scrollTop;
  13938. }
  13939. }.bind(this);
  13940. while (op != null) {
  13941. out.left += op.offsetLeft;
  13942. out.top += op.offsetTop;
  13943. _maybeAdjustScroll(op);
  13944. op = relativeToRoot ? op.offsetParent :
  13945. op.offsetParent === container ? null : op.offsetParent;
  13946. }
  13947. // if container is scrolled and the element (or its offset parent) is not absolute or fixed, adjust accordingly.
  13948. if (container != null && !relativeToRoot && (container.scrollTop > 0 || container.scrollLeft > 0)) {
  13949. var pp = el.offsetParent != null ? this.getStyle(el.offsetParent, "position") : "static",
  13950. p = this.getStyle(el, "position");
  13951. if (p !== "absolute" && p !== "fixed" && pp !== "absolute" && pp !== "fixed") {
  13952. out.left -= container.scrollLeft;
  13953. out.top -= container.scrollTop;
  13954. }
  13955. }
  13956. return out;
  13957. },
  13958. //
  13959. // return x+y proportion of the given element's size corresponding to the location of the given event.
  13960. //
  13961. getPositionOnElement: function (evt, el, zoom) {
  13962. var box = typeof el.getBoundingClientRect !== "undefined" ? el.getBoundingClientRect() : { left: 0, top: 0, width: 0, height: 0 },
  13963. body = document.body,
  13964. docElem = document.documentElement,
  13965. scrollTop = window.pageYOffset || docElem.scrollTop || body.scrollTop,
  13966. scrollLeft = window.pageXOffset || docElem.scrollLeft || body.scrollLeft,
  13967. clientTop = docElem.clientTop || body.clientTop || 0,
  13968. clientLeft = docElem.clientLeft || body.clientLeft || 0,
  13969. pst = 0,
  13970. psl = 0,
  13971. top = box.top + scrollTop - clientTop + (pst * zoom),
  13972. left = box.left + scrollLeft - clientLeft + (psl * zoom),
  13973. cl = jsPlumb.pageLocation(evt),
  13974. w = box.width || (el.offsetWidth * zoom),
  13975. h = box.height || (el.offsetHeight * zoom),
  13976. x = (cl[0] - left) / w,
  13977. y = (cl[1] - top) / h;
  13978. return [ x, y ];
  13979. },
  13980. /**
  13981. * Gets the absolute position of some element as read from the left/top properties in its style.
  13982. * @method getAbsolutePosition
  13983. * @param {Element} el The element to retrieve the absolute coordinates from. **Note** this is a DOM element, not a selector from the underlying library.
  13984. * @return {Number[]} [left, top] pixel values.
  13985. */
  13986. getAbsolutePosition: function (el) {
  13987. var _one = function (s) {
  13988. var ss = el.style[s];
  13989. if (ss) {
  13990. return parseFloat(ss.substring(0, ss.length - 2));
  13991. }
  13992. };
  13993. return [ _one("left"), _one("top") ];
  13994. },
  13995. /**
  13996. * Sets the absolute position of some element by setting the left/top properties in its style.
  13997. * @method setAbsolutePosition
  13998. * @param {Element} el The element to set the absolute coordinates on. **Note** this is a DOM element, not a selector from the underlying library.
  13999. * @param {Number[]} xy x and y coordinates
  14000. * @param {Number[]} [animateFrom] Optional previous xy to animate from.
  14001. * @param {Object} [animateOptions] Options for the animation.
  14002. */
  14003. setAbsolutePosition: function (el, xy, animateFrom, animateOptions) {
  14004. if (animateFrom) {
  14005. this.animate(el, {
  14006. left: "+=" + (xy[0] - animateFrom[0]),
  14007. top: "+=" + (xy[1] - animateFrom[1])
  14008. }, animateOptions);
  14009. }
  14010. else {
  14011. el.style.left = xy[0] + "px";
  14012. el.style.top = xy[1] + "px";
  14013. }
  14014. },
  14015. /**
  14016. * gets the size for the element, in an array : [ width, height ].
  14017. */
  14018. getSize: function (el) {
  14019. return [ el.offsetWidth, el.offsetHeight ];
  14020. },
  14021. getWidth: function (el) {
  14022. return el.offsetWidth;
  14023. },
  14024. getHeight: function (el) {
  14025. return el.offsetHeight;
  14026. },
  14027. getRenderMode : function() { return "svg"; },
  14028. draggable : function (el, options) {
  14029. var info;
  14030. el = _ju.isArray(el) || (el.length != null && !_ju.isString(el)) ? el: [ el ];
  14031. Array.prototype.slice.call(el).forEach(function(_el) {
  14032. info = this.info(_el);
  14033. if (info.el) {
  14034. this._initDraggableIfNecessary(info.el, true, options, info.id, true);
  14035. }
  14036. }.bind(this));
  14037. return this;
  14038. },
  14039. snapToGrid : function(el, x, y) {
  14040. var out = [];
  14041. var _oneEl = function(_el) {
  14042. var info = this.info(_el);
  14043. if (info.el != null && info.el._katavorioDrag) {
  14044. var snapped = info.el._katavorioDrag.snap(x, y);
  14045. this.revalidate(info.el);
  14046. out.push([info.el, snapped]);
  14047. }
  14048. }.bind(this);
  14049. // if you call this method with 0 arguments or 2 arguments it is assumed you want to snap all managed elements to
  14050. // a grid. if you supply one argument or 3, then you are assumed to be specifying one element.
  14051. if(arguments.length === 1 || arguments.length === 3) {
  14052. _oneEl(el, x, y);
  14053. } else {
  14054. var _me = this.getManagedElements();
  14055. for (var mel in _me) {
  14056. _oneEl(mel, arguments[0], arguments[1]);
  14057. }
  14058. }
  14059. return out;
  14060. },
  14061. initDraggable: function (el, options, category) {
  14062. _getDragManager(this, category).draggable(el, options);
  14063. el._jsPlumbDragOptions = options;
  14064. },
  14065. destroyDraggable: function (el, category) {
  14066. _getDragManager(this, category).destroyDraggable(el);
  14067. el._jsPlumbDragOptions = null;
  14068. el._jsPlumbRelatedElement = null;
  14069. },
  14070. unbindDraggable: function (el, evt, fn, category) {
  14071. _getDragManager(this, category).destroyDraggable(el, evt, fn);
  14072. },
  14073. setDraggable : function (element, draggable) {
  14074. return jsPlumb.each(element, function (el) {
  14075. if (this.isDragSupported(el)) {
  14076. this._draggableStates[this.getAttribute(el, "id")] = draggable;
  14077. this.setElementDraggable(el, draggable);
  14078. }
  14079. }.bind(this));
  14080. },
  14081. _draggableStates : {},
  14082. /*
  14083. * toggles the draggable state of the given element(s).
  14084. * el is either an id, or an element object, or a list of ids/element objects.
  14085. */
  14086. toggleDraggable : function (el) {
  14087. var state;
  14088. jsPlumb.each(el, function (el) {
  14089. var elId = this.getAttribute(el, "id");
  14090. state = this._draggableStates[elId] == null ? false : this._draggableStates[elId];
  14091. state = !state;
  14092. this._draggableStates[elId] = state;
  14093. this.setDraggable(el, state);
  14094. return state;
  14095. }.bind(this));
  14096. return state;
  14097. },
  14098. _initDraggableIfNecessary : function (element, isDraggable, dragOptions, id, fireEvent) {
  14099. // TODO FIRST: move to DragManager. including as much of the decision to init dragging as possible.
  14100. if (!jsPlumb.headless) {
  14101. var _draggable = isDraggable == null ? false : isDraggable;
  14102. if (_draggable) {
  14103. if (jsPlumb.isDragSupported(element, this)) {
  14104. var options = dragOptions || this.Defaults.DragOptions;
  14105. options = jsPlumb.extend({}, options); // make a copy.
  14106. if (!jsPlumb.isAlreadyDraggable(element, this)) {
  14107. var dragEvent = jsPlumb.dragEvents.drag,
  14108. stopEvent = jsPlumb.dragEvents.stop,
  14109. startEvent = jsPlumb.dragEvents.start;
  14110. this.manage(id, element);
  14111. options[startEvent] = _ju.wrap(options[startEvent], _dragStart.bind(this));
  14112. options[dragEvent] = _ju.wrap(options[dragEvent], _dragMove.bind(this));
  14113. options[stopEvent] = _ju.wrap(options[stopEvent], _dragStop.bind(this));
  14114. var elId = this.getId(element); // need ID
  14115. this._draggableStates[elId] = true;
  14116. var draggable = this._draggableStates[elId];
  14117. options.disabled = draggable == null ? false : !draggable;
  14118. this.initDraggable(element, options);
  14119. this.getDragManager().register(element);
  14120. if (fireEvent) {
  14121. this.fire("elementDraggable", {el:element, options:options});
  14122. }
  14123. }
  14124. else {
  14125. // already draggable. attach any start, drag or stop listeners to the current Drag.
  14126. if (dragOptions.force) {
  14127. this.initDraggable(element, options);
  14128. }
  14129. }
  14130. }
  14131. }
  14132. }
  14133. },
  14134. animationSupported:true,
  14135. getElement: function (el) {
  14136. if (el == null) {
  14137. return null;
  14138. }
  14139. // here we pluck the first entry if el was a list of entries.
  14140. // this is not my favourite thing to do, but previous versions of
  14141. // jsplumb supported jquery selectors, and it is possible a selector
  14142. // will be passed in here.
  14143. el = typeof el === "string" ? el : (el.tagName == null && el.length != null && el.enctype == null) ? el[0] : el;
  14144. return typeof el === "string" ? document.getElementById(el) : el;
  14145. },
  14146. removeElement: function (element) {
  14147. _getDragManager(this).elementRemoved(element);
  14148. this.getEventManager().remove(element);
  14149. },
  14150. //
  14151. // this adapter supports a rudimentary animation function. no easing is supported. only
  14152. // left/top properties are supported. property delta args are expected to be in the form
  14153. //
  14154. // +=x.xxxx
  14155. //
  14156. // or
  14157. //
  14158. // -=x.xxxx
  14159. //
  14160. doAnimate: function (el, properties, options) {
  14161. options = options || {};
  14162. var o = this.getOffset(el),
  14163. ap = _animProps(o, properties),
  14164. ldist = ap[0] - o.left,
  14165. tdist = ap[1] - o.top,
  14166. d = options.duration || 250,
  14167. step = 15, steps = d / step,
  14168. linc = (step / d) * ldist,
  14169. tinc = (step / d) * tdist,
  14170. idx = 0,
  14171. _int = setInterval(function () {
  14172. _jp.setPosition(el, {
  14173. left: o.left + (linc * (idx + 1)),
  14174. top: o.top + (tinc * (idx + 1))
  14175. });
  14176. if (options.step != null) {
  14177. options.step(idx, Math.ceil(steps));
  14178. }
  14179. idx++;
  14180. if (idx >= steps) {
  14181. window.clearInterval(_int);
  14182. if (options.complete != null) {
  14183. options.complete();
  14184. }
  14185. }
  14186. }, step);
  14187. },
  14188. // DRAG/DROP
  14189. destroyDroppable: function (el, category) {
  14190. _getDragManager(this, category).destroyDroppable(el);
  14191. },
  14192. unbindDroppable: function (el, evt, fn, category) {
  14193. _getDragManager(this, category).destroyDroppable(el, evt, fn);
  14194. },
  14195. droppable :function(el, options) {
  14196. el = _ju.isArray(el) || (el.length != null && !_ju.isString(el)) ? el: [ el ];
  14197. var info;
  14198. options = options || {};
  14199. options.allowLoopback = false;
  14200. Array.prototype.slice.call(el).forEach(function(_el) {
  14201. info = this.info(_el);
  14202. if (info.el) {
  14203. this.initDroppable(info.el, options);
  14204. }
  14205. }.bind(this));
  14206. return this;
  14207. },
  14208. initDroppable: function (el, options, category) {
  14209. _getDragManager(this, category).droppable(el, options);
  14210. },
  14211. isAlreadyDraggable: function (el) {
  14212. return el._katavorioDrag != null;
  14213. },
  14214. isDragSupported: function (el, options) {
  14215. return true;
  14216. },
  14217. isDropSupported: function (el, options) {
  14218. return true;
  14219. },
  14220. isElementDraggable: function (el) {
  14221. el = _jp.getElement(el);
  14222. return el._katavorioDrag && el._katavorioDrag.isEnabled();
  14223. },
  14224. getDragObject: function (eventArgs) {
  14225. return eventArgs[0].drag.getDragElement();
  14226. },
  14227. getDragScope: function (el) {
  14228. return el._katavorioDrag && el._katavorioDrag.scopes.join(" ") || "";
  14229. },
  14230. getDropEvent: function (args) {
  14231. return args[0].e;
  14232. },
  14233. getUIPosition: function (eventArgs, zoom) {
  14234. // here the position reported to us by Katavorio is relative to the element's offsetParent. For top
  14235. // level nodes that is fine, but if we have a nested draggable then its offsetParent is actually
  14236. // not going to be the jsplumb container; it's going to be some child of that element. In that case
  14237. // we want to adjust the UI position to account for the offsetParent's position relative to the Container
  14238. // origin.
  14239. var el = eventArgs[0].el;
  14240. if (el.offsetParent == null) {
  14241. return null;
  14242. }
  14243. var finalPos = eventArgs[0].finalPos || eventArgs[0].pos;
  14244. var p = { left:finalPos[0], top:finalPos[1] };
  14245. if (el._katavorioDrag && el.offsetParent !== this.getContainer()) {
  14246. var oc = this.getOffset(el.offsetParent);
  14247. p.left += oc.left;
  14248. p.top += oc.top;
  14249. }
  14250. return p;
  14251. },
  14252. setDragFilter: function (el, filter, _exclude) {
  14253. if (el._katavorioDrag) {
  14254. el._katavorioDrag.setFilter(filter, _exclude);
  14255. }
  14256. },
  14257. setElementDraggable: function (el, draggable) {
  14258. el = _jp.getElement(el);
  14259. if (el._katavorioDrag) {
  14260. el._katavorioDrag.setEnabled(draggable);
  14261. }
  14262. },
  14263. setDragScope: function (el, scope) {
  14264. if (el._katavorioDrag) {
  14265. el._katavorioDrag.k.setDragScope(el, scope);
  14266. }
  14267. },
  14268. setDropScope:function(el, scope) {
  14269. if (el._katavorioDrop && el._katavorioDrop.length > 0) {
  14270. el._katavorioDrop[0].k.setDropScope(el, scope);
  14271. }
  14272. },
  14273. addToPosse:function(el, spec) {
  14274. var specs = Array.prototype.slice.call(arguments, 1);
  14275. var dm = _getDragManager(this);
  14276. _jp.each(el, function(_el) {
  14277. _el = [ _jp.getElement(_el) ];
  14278. _el.push.apply(_el, specs );
  14279. dm.addToPosse.apply(dm, _el);
  14280. });
  14281. },
  14282. setPosse:function(el, spec) {
  14283. var specs = Array.prototype.slice.call(arguments, 1);
  14284. var dm = _getDragManager(this);
  14285. _jp.each(el, function(_el) {
  14286. _el = [ _jp.getElement(_el) ];
  14287. _el.push.apply(_el, specs );
  14288. dm.setPosse.apply(dm, _el);
  14289. });
  14290. },
  14291. removeFromPosse:function(el, posseId) {
  14292. var specs = Array.prototype.slice.call(arguments, 1);
  14293. var dm = _getDragManager(this);
  14294. _jp.each(el, function(_el) {
  14295. _el = [ _jp.getElement(_el) ];
  14296. _el.push.apply(_el, specs );
  14297. dm.removeFromPosse.apply(dm, _el);
  14298. });
  14299. },
  14300. removeFromAllPosses:function(el) {
  14301. var dm = _getDragManager(this);
  14302. _jp.each(el, function(_el) { dm.removeFromAllPosses(_jp.getElement(_el)); });
  14303. },
  14304. setPosseState:function(el, posseId, state) {
  14305. var dm = _getDragManager(this);
  14306. _jp.each(el, function(_el) { dm.setPosseState(_jp.getElement(_el), posseId, state); });
  14307. },
  14308. dragEvents: {
  14309. 'start': 'start', 'stop': 'stop', 'drag': 'drag', 'step': 'step',
  14310. 'over': 'over', 'out': 'out', 'drop': 'drop', 'complete': 'complete',
  14311. 'beforeStart':'beforeStart'
  14312. },
  14313. animEvents: {
  14314. 'step': "step", 'complete': 'complete'
  14315. },
  14316. stopDrag: function (el) {
  14317. if (el._katavorioDrag) {
  14318. el._katavorioDrag.abort();
  14319. }
  14320. },
  14321. addToDragSelection: function (spec) {
  14322. var el = this.getElement(spec);
  14323. if (el != null && (el._isJsPlumbGroup || el._jsPlumbGroup == null)) {
  14324. _getDragManager(this).select(spec);
  14325. }
  14326. },
  14327. removeFromDragSelection: function (spec) {
  14328. _getDragManager(this).deselect(spec);
  14329. },
  14330. getDragSelection:function() {
  14331. return _getDragManager(this).getSelection();
  14332. },
  14333. clearDragSelection: function () {
  14334. _getDragManager(this).deselectAll();
  14335. },
  14336. trigger: function (el, event, originalEvent, payload) {
  14337. this.getEventManager().trigger(el, event, originalEvent, payload);
  14338. },
  14339. doReset:function() {
  14340. // look for katavorio instances and reset each one if found.
  14341. for (var key in this) {
  14342. if (key.indexOf("_katavorio_") === 0) {
  14343. this[key].reset();
  14344. }
  14345. }
  14346. },
  14347. getEventManager:function() {
  14348. return _getEventManager(this);
  14349. },
  14350. on : function(el, event, callback) {
  14351. // TODO: here we would like to map the tap event if we know its
  14352. // an internal bind to a click. we have to know its internal because only
  14353. // then can we be sure that the UP event wont be consumed (tap is a synthesized
  14354. // event from a mousedown followed by a mouseup).
  14355. //event = { "click":"tap", "dblclick":"dbltap"}[event] || event;
  14356. this.getEventManager().on.apply(this, arguments);
  14357. return this;
  14358. },
  14359. off : function(el, event, callback) {
  14360. this.getEventManager().off.apply(this, arguments);
  14361. return this;
  14362. }
  14363. });
  14364. var ready = function (f) {
  14365. var _do = function () {
  14366. if (/complete|loaded|interactive/.test(document.readyState) && typeof(document.body) !== "undefined" && document.body != null) {
  14367. f();
  14368. }
  14369. else {
  14370. setTimeout(_do, 9);
  14371. }
  14372. };
  14373. _do();
  14374. };
  14375. ready(_jp.init);
  14376. }).call(typeof window !== 'undefined' ? window : this);