jquery.jsPlumb-1.7.2.js 456 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214
  1. /**
  2. * jsBezier-0.6
  3. *
  4. * Copyright (c) 2010 - 2013 Simon Porritt (simon.porritt@gmail.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. if(typeof Math.sgn == "undefined") {
  67. Math.sgn = function(x) { return x == 0 ? 0 : x > 0 ? 1 :-1; };
  68. }
  69. var Vectors = {
  70. subtract : function(v1, v2) { return {x:v1.x - v2.x, y:v1.y - v2.y }; },
  71. dotProduct : function(v1, v2) { return (v1.x * v2.x) + (v1.y * v2.y); },
  72. square : function(v) { return Math.sqrt((v.x * v.x) + (v.y * v.y)); },
  73. scale : function(v, s) { return {x:v.x * s, y:v.y * s }; }
  74. },
  75. maxRecursion = 64,
  76. flatnessTolerance = Math.pow(2.0,-maxRecursion-1);
  77. /**
  78. * Calculates the distance that the point lies from the curve.
  79. *
  80. * @param point a point in the form {x:567, y:3342}
  81. * @param curve a Bezier curve in the form [{x:..., y:...}, {x:..., y:...}, {x:..., y:...}, {x:..., y:...}]. note that this is currently
  82. * hardcoded to assume cubiz beziers, but would be better off supporting any degree.
  83. * @return a JS object literal containing location and distance, for example: {location:0.35, distance:10}. Location is analogous to the location
  84. * argument you pass to the pointOnPath function: it is a ratio of distance travelled along the curve. Distance is the distance in pixels from
  85. * the point to the curve.
  86. */
  87. var _distanceFromCurve = function(point, curve) {
  88. var candidates = [],
  89. w = _convertToBezier(point, curve),
  90. degree = curve.length - 1, higherDegree = (2 * degree) - 1,
  91. numSolutions = _findRoots(w, higherDegree, candidates, 0),
  92. v = Vectors.subtract(point, curve[0]), dist = Vectors.square(v), t = 0.0;
  93. for (var i = 0; i < numSolutions; i++) {
  94. v = Vectors.subtract(point, _bezier(curve, degree, candidates[i], null, null));
  95. var newDist = Vectors.square(v);
  96. if (newDist < dist) {
  97. dist = newDist;
  98. t = candidates[i];
  99. }
  100. }
  101. v = Vectors.subtract(point, curve[degree]);
  102. newDist = Vectors.square(v);
  103. if (newDist < dist) {
  104. dist = newDist;
  105. t = 1.0;
  106. }
  107. return {location:t, distance:dist};
  108. };
  109. /**
  110. * finds the nearest point on the curve to the given point.
  111. */
  112. var _nearestPointOnCurve = function(point, curve) {
  113. var td = _distanceFromCurve(point, curve);
  114. return {point:_bezier(curve, curve.length - 1, td.location, null, null), location:td.location};
  115. };
  116. var _convertToBezier = function(point, curve) {
  117. var degree = curve.length - 1, higherDegree = (2 * degree) - 1,
  118. c = [], d = [], cdTable = [], w = [],
  119. 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] ];
  120. for (var i = 0; i <= degree; i++) c[i] = Vectors.subtract(curve[i], point);
  121. for (var i = 0; i <= degree - 1; i++) {
  122. d[i] = Vectors.subtract(curve[i+1], curve[i]);
  123. d[i] = Vectors.scale(d[i], 3.0);
  124. }
  125. for (var row = 0; row <= degree - 1; row++) {
  126. for (var column = 0; column <= degree; column++) {
  127. if (!cdTable[row]) cdTable[row] = [];
  128. cdTable[row][column] = Vectors.dotProduct(d[row], c[column]);
  129. }
  130. }
  131. for (i = 0; i <= higherDegree; i++) {
  132. if (!w[i]) w[i] = [];
  133. w[i].y = 0.0;
  134. w[i].x = parseFloat(i) / higherDegree;
  135. }
  136. var n = degree, m = degree-1;
  137. for (var k = 0; k <= n + m; k++) {
  138. var lb = Math.max(0, k - m),
  139. ub = Math.min(k, n);
  140. for (i = lb; i <= ub; i++) {
  141. j = k - i;
  142. w[i+j].y += cdTable[j][i] * z[j][i];
  143. }
  144. }
  145. return w;
  146. };
  147. /**
  148. * counts how many roots there are.
  149. */
  150. var _findRoots = function(w, degree, t, depth) {
  151. var left = [], right = [],
  152. left_count, right_count,
  153. left_t = [], right_t = [];
  154. switch (_getCrossingCount(w, degree)) {
  155. case 0 : {
  156. return 0;
  157. }
  158. case 1 : {
  159. if (depth >= maxRecursion) {
  160. t[0] = (w[0].x + w[degree].x) / 2.0;
  161. return 1;
  162. }
  163. if (_isFlatEnough(w, degree)) {
  164. t[0] = _computeXIntercept(w, degree);
  165. return 1;
  166. }
  167. break;
  168. }
  169. }
  170. _bezier(w, degree, 0.5, left, right);
  171. left_count = _findRoots(left, degree, left_t, depth+1);
  172. right_count = _findRoots(right, degree, right_t, depth+1);
  173. for (var i = 0; i < left_count; i++) t[i] = left_t[i];
  174. for (var i = 0; i < right_count; i++) t[i+left_count] = right_t[i];
  175. return (left_count+right_count);
  176. };
  177. var _getCrossingCount = function(curve, degree) {
  178. var n_crossings = 0, sign, old_sign;
  179. sign = old_sign = Math.sgn(curve[0].y);
  180. for (var i = 1; i <= degree; i++) {
  181. sign = Math.sgn(curve[i].y);
  182. if (sign != old_sign) n_crossings++;
  183. old_sign = sign;
  184. }
  185. return n_crossings;
  186. };
  187. var _isFlatEnough = function(curve, degree) {
  188. var error,
  189. intercept_1, intercept_2, left_intercept, right_intercept,
  190. a, b, c, det, dInv, a1, b1, c1, a2, b2, c2;
  191. a = curve[0].y - curve[degree].y;
  192. b = curve[degree].x - curve[0].x;
  193. c = curve[0].x * curve[degree].y - curve[degree].x * curve[0].y;
  194. var max_distance_above = max_distance_below = 0.0;
  195. for (var i = 1; i < degree; i++) {
  196. var value = a * curve[i].x + b * curve[i].y + c;
  197. if (value > max_distance_above)
  198. max_distance_above = value;
  199. else if (value < max_distance_below)
  200. max_distance_below = value;
  201. }
  202. a1 = 0.0; b1 = 1.0; c1 = 0.0; a2 = a; b2 = b;
  203. c2 = c - max_distance_above;
  204. det = a1 * b2 - a2 * b1;
  205. dInv = 1.0/det;
  206. intercept_1 = (b1 * c2 - b2 * c1) * dInv;
  207. a2 = a; b2 = b; c2 = c - max_distance_below;
  208. det = a1 * b2 - a2 * b1;
  209. dInv = 1.0/det;
  210. intercept_2 = (b1 * c2 - b2 * c1) * dInv;
  211. left_intercept = Math.min(intercept_1, intercept_2);
  212. right_intercept = Math.max(intercept_1, intercept_2);
  213. error = right_intercept - left_intercept;
  214. return (error < flatnessTolerance)? 1 : 0;
  215. };
  216. var _computeXIntercept = function(curve, degree) {
  217. var XLK = 1.0, YLK = 0.0,
  218. XNM = curve[degree].x - curve[0].x, YNM = curve[degree].y - curve[0].y,
  219. XMK = curve[0].x - 0.0, YMK = curve[0].y - 0.0,
  220. det = XNM*YLK - YNM*XLK, detInv = 1.0/det,
  221. S = (XNM*YMK - YNM*XMK) * detInv;
  222. return 0.0 + XLK * S;
  223. };
  224. var _bezier = function(curve, degree, t, left, right) {
  225. var temp = [[]];
  226. for (var j =0; j <= degree; j++) temp[0][j] = curve[j];
  227. for (var i = 1; i <= degree; i++) {
  228. for (var j =0 ; j <= degree - i; j++) {
  229. if (!temp[i]) temp[i] = [];
  230. if (!temp[i][j]) temp[i][j] = {};
  231. temp[i][j].x = (1.0 - t) * temp[i-1][j].x + t * temp[i-1][j+1].x;
  232. temp[i][j].y = (1.0 - t) * temp[i-1][j].y + t * temp[i-1][j+1].y;
  233. }
  234. }
  235. if (left != null)
  236. for (j = 0; j <= degree; j++) left[j] = temp[j][0];
  237. if (right != null)
  238. for (j = 0; j <= degree; j++) right[j] = temp[degree-j][j];
  239. return (temp[degree][0]);
  240. };
  241. var _curveFunctionCache = {};
  242. var _getCurveFunctions = function(order) {
  243. var fns = _curveFunctionCache[order];
  244. if (!fns) {
  245. fns = [];
  246. var f_term = function() { return function(t) { return Math.pow(t, order); }; },
  247. l_term = function() { return function(t) { return Math.pow((1-t), order); }; },
  248. c_term = function(c) { return function(t) { return c; }; },
  249. t_term = function() { return function(t) { return t; }; },
  250. one_minus_t_term = function() { return function(t) { return 1-t; }; },
  251. _termFunc = function(terms) {
  252. return function(t) {
  253. var p = 1;
  254. for (var i = 0; i < terms.length; i++) p = p * terms[i](t);
  255. return p;
  256. };
  257. };
  258. fns.push(new f_term()); // first is t to the power of the curve order
  259. for (var i = 1; i < order; i++) {
  260. var terms = [new c_term(order)];
  261. for (var j = 0 ; j < (order - i); j++) terms.push(new t_term());
  262. for (var j = 0 ; j < i; j++) terms.push(new one_minus_t_term());
  263. fns.push(new _termFunc(terms));
  264. }
  265. fns.push(new l_term()); // last is (1-t) to the power of the curve order
  266. _curveFunctionCache[order] = fns;
  267. }
  268. return fns;
  269. };
  270. /**
  271. * calculates a point on the curve, for a Bezier of arbitrary order.
  272. * @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.
  273. * @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.
  274. */
  275. var _pointOnPath = function(curve, location) {
  276. var cc = _getCurveFunctions(curve.length - 1),
  277. _x = 0, _y = 0;
  278. for (var i = 0; i < curve.length ; i++) {
  279. _x = _x + (curve[i].x * cc[i](location));
  280. _y = _y + (curve[i].y * cc[i](location));
  281. }
  282. return {x:_x, y:_y};
  283. };
  284. var _dist = function(p1,p2) {
  285. return Math.sqrt(Math.pow(p1.x - p2.x, 2) + Math.pow(p1.y - p2.y, 2));
  286. };
  287. var _isPoint = function(curve) {
  288. return curve[0].x == curve[1].x && curve[0].y == curve[1].y;
  289. };
  290. /**
  291. * finds the point that is 'distance' along the path from 'location'. this method returns both the x,y location of the point and also
  292. * its 'location' (proportion of travel along the path); the method below - _pointAlongPathFrom - calls this method and just returns the
  293. * point.
  294. */
  295. var _pointAlongPath = function(curve, location, distance) {
  296. if (_isPoint(curve)) {
  297. return {
  298. point:curve[0],
  299. location:location
  300. };
  301. }
  302. var prev = _pointOnPath(curve, location),
  303. tally = 0,
  304. curLoc = location,
  305. direction = distance > 0 ? 1 : -1,
  306. cur = null;
  307. while (tally < Math.abs(distance)) {
  308. curLoc += (0.005 * direction);
  309. cur = _pointOnPath(curve, curLoc);
  310. tally += _dist(cur, prev);
  311. prev = cur;
  312. }
  313. return {point:cur, location:curLoc};
  314. };
  315. var _length = function(curve) {
  316. if (_isPoint(curve)) return 0;
  317. var prev = _pointOnPath(curve, 0),
  318. tally = 0,
  319. curLoc = 0,
  320. direction = 1,
  321. cur = null;
  322. while (curLoc < 1) {
  323. curLoc += (0.005 * direction);
  324. cur = _pointOnPath(curve, curLoc);
  325. tally += _dist(cur, prev);
  326. prev = cur;
  327. }
  328. return tally;
  329. };
  330. /**
  331. * finds the point that is 'distance' along the path from 'location'.
  332. */
  333. var _pointAlongPathFrom = function(curve, location, distance) {
  334. return _pointAlongPath(curve, location, distance).point;
  335. };
  336. /**
  337. * finds the location that is 'distance' along the path from 'location'.
  338. */
  339. var _locationAlongPathFrom = function(curve, location, distance) {
  340. return _pointAlongPath(curve, location, distance).location;
  341. };
  342. /**
  343. * returns the gradient of the curve at the given location, which is a decimal between 0 and 1 inclusive.
  344. *
  345. * thanks // http://bimixual.org/AnimationLibrary/beziertangents.html
  346. */
  347. var _gradientAtPoint = function(curve, location) {
  348. var p1 = _pointOnPath(curve, location),
  349. p2 = _pointOnPath(curve.slice(0, curve.length - 1), location),
  350. dy = p2.y - p1.y, dx = p2.x - p1.x;
  351. return dy == 0 ? Infinity : Math.atan(dy / dx);
  352. };
  353. /**
  354. returns the gradient of the curve at the point which is 'distance' from the given location.
  355. if this point is greater than location 1, the gradient at location 1 is returned.
  356. if this point is less than location 0, the gradient at location 0 is returned.
  357. */
  358. var _gradientAtPointAlongPathFrom = function(curve, location, distance) {
  359. var p = _pointAlongPath(curve, location, distance);
  360. if (p.location > 1) p.location = 1;
  361. if (p.location < 0) p.location = 0;
  362. return _gradientAtPoint(curve, p.location);
  363. };
  364. /**
  365. * calculates a line that is 'length' pixels long, perpendicular to, and centered on, the path at 'distance' pixels from the given location.
  366. * if distance is not supplied, the perpendicular for the given location is computed (ie. we set distance to zero).
  367. */
  368. var _perpendicularToPathAt = function(curve, location, length, distance) {
  369. distance = distance == null ? 0 : distance;
  370. var p = _pointAlongPath(curve, location, distance),
  371. m = _gradientAtPoint(curve, p.location),
  372. _theta2 = Math.atan(-1 / m),
  373. y = length / 2 * Math.sin(_theta2),
  374. x = length / 2 * Math.cos(_theta2);
  375. return [{x:p.point.x + x, y:p.point.y + y}, {x:p.point.x - x, y:p.point.y - y}];
  376. };
  377. var jsBezier = window.jsBezier = {
  378. distanceFromCurve : _distanceFromCurve,
  379. gradientAtPoint : _gradientAtPoint,
  380. gradientAtPointAlongCurveFrom : _gradientAtPointAlongPathFrom,
  381. nearestPointOnCurve : _nearestPointOnCurve,
  382. pointOnCurve : _pointOnPath,
  383. pointAlongCurveFrom : _pointAlongPathFrom,
  384. perpendicularToCurveAt : _perpendicularToPathAt,
  385. locationAlongCurveFrom:_locationAlongPathFrom,
  386. getLength:_length
  387. };
  388. })();
  389. /**
  390. * Biltong v0.2
  391. *
  392. * Various geometry functions written as part of jsPlumb and perhaps useful for others.
  393. *
  394. * Copyright (c) 2014 Simon Porritt
  395. *
  396. * Permission is hereby granted, free of charge, to any person
  397. * obtaining a copy of this software and associated documentation
  398. * files (the "Software"), to deal in the Software without
  399. * restriction, including without limitation the rights to use,
  400. * copy, modify, merge, publish, distribute, sublicense, and/or sell
  401. * copies of the Software, and to permit persons to whom the
  402. * Software is furnished to do so, subject to the following
  403. * conditions:
  404. *
  405. * The above copyright notice and this permission notice shall be
  406. * included in all copies or substantial portions of the Software.
  407. *
  408. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  409. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  410. * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  411. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  412. * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  413. * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  414. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  415. * OTHER DEALINGS IN THE SOFTWARE.
  416. */
  417. ;(function() {
  418. "use strict";
  419. var Biltong = this.Biltong = {};
  420. var _isa = function(a) { return Object.prototype.toString.call(a) === "[object Array]"; },
  421. _pointHelper = function(p1, p2, fn) {
  422. p1 = _isa(p1) ? p1 : [p1.x, p1.y];
  423. p2 = _isa(p2) ? p2 : [p2.x, p2.y];
  424. return fn(p1, p2);
  425. },
  426. /**
  427. * @name Biltong.gradient
  428. * @function
  429. * @desc Calculates the gradient of a line between the two points.
  430. * @param {Point} p1 First point, either as a 2 entry array or object with `left` and `top` properties.
  431. * @param {Point} p2 Second point, either as a 2 entry array or object with `left` and `top` properties.
  432. * @return {Float} The gradient of a line between the two points.
  433. */
  434. _gradient = Biltong.gradient = function(p1, p2) {
  435. return _pointHelper(p1, p2, function(_p1, _p2) {
  436. if (_p2[0] == _p1[0])
  437. return _p2[1] > _p1[1] ? Infinity : -Infinity;
  438. else if (_p2[1] == _p1[1])
  439. return _p2[0] > _p1[0] ? 0 : -0;
  440. else
  441. return (_p2[1] - _p1[1]) / (_p2[0] - _p1[0]);
  442. });
  443. },
  444. /**
  445. * @name Biltong.normal
  446. * @function
  447. * @desc Calculates the gradient of a normal to a line between the two points.
  448. * @param {Point} p1 First point, either as a 2 entry array or object with `left` and `top` properties.
  449. * @param {Point} p2 Second point, either as a 2 entry array or object with `left` and `top` properties.
  450. * @return {Float} The gradient of a normal to a line between the two points.
  451. */
  452. _normal = Biltong.normal = function(p1, p2) {
  453. return -1 / _gradient(p1, p2);
  454. },
  455. /**
  456. * @name Biltong.lineLength
  457. * @function
  458. * @desc Calculates the length of a line between the two points.
  459. * @param {Point} p1 First point, either as a 2 entry array or object with `left` and `top` properties.
  460. * @param {Point} p2 Second point, either as a 2 entry array or object with `left` and `top` properties.
  461. * @return {Float} The length of a line between the two points.
  462. */
  463. _lineLength = Biltong.lineLength = function(p1, p2) {
  464. return _pointHelper(p1, p2, function(_p1, _p2) {
  465. return Math.sqrt(Math.pow(_p2[1] - _p1[1], 2) + Math.pow(_p2[0] - _p1[0], 2));
  466. });
  467. },
  468. /**
  469. * @name Biltong.quadrant
  470. * @function
  471. * @desc Calculates the quadrant in which the angle between the two points lies.
  472. * @param {Point} p1 First point, either as a 2 entry array or object with `left` and `top` properties.
  473. * @param {Point} p2 Second point, either as a 2 entry array or object with `left` and `top` properties.
  474. * @return {Integer} The quadrant - 1 for upper right, 2 for lower right, 3 for lower left, 4 for upper left.
  475. */
  476. _quadrant = Biltong.quadrant = function(p1, p2) {
  477. return _pointHelper(p1, p2, function(_p1, _p2) {
  478. if (_p2[0] > _p1[0]) {
  479. return (_p2[1] > _p1[1]) ? 2 : 1;
  480. }
  481. else if (_p2[0] == _p1[0]) {
  482. return _p2[1] > _p1[1] ? 2 : 1;
  483. }
  484. else {
  485. return (_p2[1] > _p1[1]) ? 3 : 4;
  486. }
  487. });
  488. },
  489. /**
  490. * @name Biltong.theta
  491. * @function
  492. * @desc Calculates the angle between the two points.
  493. * @param {Point} p1 First point, either as a 2 entry array or object with `left` and `top` properties.
  494. * @param {Point} p2 Second point, either as a 2 entry array or object with `left` and `top` properties.
  495. * @return {Float} The angle between the two points.
  496. */
  497. _theta = Biltong.theta = function(p1, p2) {
  498. return _pointHelper(p1, p2, function(_p1, _p2) {
  499. var m = _gradient(_p1, _p2),
  500. t = Math.atan(m),
  501. s = _quadrant(_p1, _p2);
  502. if ((s == 4 || s== 3)) t += Math.PI;
  503. if (t < 0) t += (2 * Math.PI);
  504. return t;
  505. });
  506. },
  507. /**
  508. * @name Biltong.intersects
  509. * @function
  510. * @desc Calculates whether or not the two rectangles intersect.
  511. * @param {Rectangle} r1 First rectangle, as a js object in the form `{x:.., y:.., w:.., h:..}`
  512. * @param {Rectangle} r2 Second rectangle, as a js object in the form `{x:.., y:.., w:.., h:..}`
  513. * @return {Boolean} True if the rectangles intersect, false otherwise.
  514. */
  515. _intersects = Biltong.intersects = function(r1, r2) {
  516. var x1 = r1.x, x2 = r1.x + r1.w, y1 = r1.y, y2 = r1.y + r1.h,
  517. a1 = r2.x, a2 = r2.x + r2.w, b1 = r2.y, b2 = r2.y + r2.h;
  518. return ( (x1 <= a1 && a1 <= x2) && (y1 <= b1 && b1 <= y2) ) ||
  519. ( (x1 <= a2 && a2 <= x2) && (y1 <= b1 && b1 <= y2) ) ||
  520. ( (x1 <= a1 && a1 <= x2) && (y1 <= b2 && b2 <= y2) ) ||
  521. ( (x1 <= a2 && a1 <= x2) && (y1 <= b2 && b2 <= y2) ) ||
  522. ( (a1 <= x1 && x1 <= a2) && (b1 <= y1 && y1 <= b2) ) ||
  523. ( (a1 <= x2 && x2 <= a2) && (b1 <= y1 && y1 <= b2) ) ||
  524. ( (a1 <= x1 && x1 <= a2) && (b1 <= y2 && y2 <= b2) ) ||
  525. ( (a1 <= x2 && x1 <= a2) && (b1 <= y2 && y2 <= b2) );
  526. },
  527. /**
  528. * @name Biltong.encloses
  529. * @function
  530. * @desc Calculates whether or not r2 is completely enclosed by r1.
  531. * @param {Rectangle} r1 First rectangle, as a js object in the form `{x:.., y:.., w:.., h:..}`
  532. * @param {Rectangle} r2 Second rectangle, as a js object in the form `{x:.., y:.., w:.., h:..}`
  533. * @param {Boolean} [allowSharedEdges=false] If true, the concept of enclosure allows for one or more edges to be shared by the two rectangles.
  534. * @return {Boolean} True if r1 encloses r2, false otherwise.
  535. */
  536. _encloses = Biltong.encloses = function(r1, r2, allowSharedEdges) {
  537. var x1 = r1.x, x2 = r1.x + r1.w, y1 = r1.y, y2 = r1.y + r1.h,
  538. a1 = r2.x, a2 = r2.x + r2.w, b1 = r2.y, b2 = r2.y + r2.h,
  539. c = function(v1, v2, v3, v4) { return allowSharedEdges ? v1 <= v2 && v3>= v4 : v1 < v2 && v3 > v4; };
  540. return c(x1,a1,x2,a2) && c(y1,b1,y2,b2);
  541. },
  542. _segmentMultipliers = [null, [1, -1], [1, 1], [-1, 1], [-1, -1] ],
  543. _inverseSegmentMultipliers = [null, [-1, -1], [-1, 1], [1, 1], [1, -1] ],
  544. /**
  545. * @name Biltong.pointOnLine
  546. * @function
  547. * @desc Calculates a point on the line from `fromPoint` to `toPoint` that is `distance` units along the length of the line.
  548. * @param {Point} p1 First point, either as a 2 entry array or object with `left` and `top` properties.
  549. * @param {Point} p2 Second point, either as a 2 entry array or object with `left` and `top` properties.
  550. * @return {Point} Point on the line, in the form `{ x:..., y:... }`.
  551. */
  552. _pointOnLine = Biltong.pointOnLine = function(fromPoint, toPoint, distance) {
  553. var m = _gradient(fromPoint, toPoint),
  554. s = _quadrant(fromPoint, toPoint),
  555. segmentMultiplier = distance > 0 ? _segmentMultipliers[s] : _inverseSegmentMultipliers[s],
  556. theta = Math.atan(m),
  557. y = Math.abs(distance * Math.sin(theta)) * segmentMultiplier[1],
  558. x = Math.abs(distance * Math.cos(theta)) * segmentMultiplier[0];
  559. return { x:fromPoint.x + x, y:fromPoint.y + y };
  560. },
  561. /**
  562. * @name Biltong.perpendicularLineTo
  563. * @function
  564. * @desc Calculates a line of length `length` that is perpendicular to the line from `fromPoint` to `toPoint` and passes through `toPoint`.
  565. * @param {Point} p1 First point, either as a 2 entry array or object with `left` and `top` properties.
  566. * @param {Point} p2 Second point, either as a 2 entry array or object with `left` and `top` properties.
  567. * @return {Line} Perpendicular line, in the form `[ { x:..., y:... }, { x:..., y:... } ]`.
  568. */
  569. _perpendicularLineTo = Biltong.perpendicularLineTo = function(fromPoint, toPoint, length) {
  570. var m = _gradient(fromPoint, toPoint),
  571. theta2 = Math.atan(-1 / m),
  572. y = length / 2 * Math.sin(theta2),
  573. x = length / 2 * Math.cos(theta2);
  574. return [{x:toPoint.x + x, y:toPoint.y + y}, {x:toPoint.x - x, y:toPoint.y - y}];
  575. };
  576. }).call(this);
  577. ;(function() {
  578. "use strict";
  579. var Sniff = {
  580. android:navigator.userAgent.toLowerCase().indexOf("android") > -1
  581. };
  582. var matchesSelector = function(el, selector, ctx) {
  583. ctx = ctx || el.parentNode;
  584. var possibles = ctx.querySelectorAll(selector);
  585. for (var i = 0; i < possibles.length; i++) {
  586. if (possibles[i] === el) {
  587. return true;
  588. }
  589. }
  590. return false;
  591. },
  592. _gel = function(el) { return typeof el == "string" ? document.getElementById(el) : el; },
  593. _t = function(e) { return e.srcElement || e.target; },
  594. _d = function(l, fn) {
  595. for (var i = 0, j = l.length; i < j; i++) {
  596. if (l[i] == fn) break;
  597. }
  598. if (i < l.length) l.splice(i, 1);
  599. },
  600. guid = 1,
  601. //
  602. // this function generates a guid for every handler, sets it on the handler, then adds
  603. // it to the associated object's map of handlers for the given event. this is what enables us
  604. // to unbind all events of some type, or all events (the second of which can be requested by the user,
  605. // but it also used by Mottle when an element is removed.)
  606. _store = function(obj, event, fn) {
  607. var g = guid++;
  608. obj.__ta = obj.__ta || {};
  609. obj.__ta[event] = obj.__ta[event] || {};
  610. // store each handler with a unique guid.
  611. obj.__ta[event][g] = fn;
  612. // set the guid on the handler.
  613. fn.__tauid = g;
  614. return g;
  615. },
  616. _unstore = function(obj, event, fn) {
  617. obj.__ta && obj.__ta[event] && delete obj.__ta[event][fn.__tauid];
  618. // a handler might have attached extra functions, so we unbind those too.
  619. if (fn.__taExtra) {
  620. for (var i = 0; i < fn.__taExtra.length; i++) {
  621. _unbind(obj, fn.__taExtra[i][0], fn.__taExtra[i][1]);
  622. }
  623. fn.__taExtra.length = 0;
  624. }
  625. // a handler might have attached an unstore callback
  626. fn.__taUnstore && fn.__taUnstore();
  627. },
  628. _curryChildFilter = function(children, obj, fn, evt) {
  629. if (children == null) return fn;
  630. else {
  631. var c = children.split(","),
  632. _fn = function(e) {
  633. _fn.__tauid = fn.__tauid;
  634. var t = _t(e);
  635. for (var i = 0; i < c.length; i++) {
  636. if (matchesSelector(t, c[i], obj)) {
  637. fn.apply(t, arguments);
  638. }
  639. }
  640. };
  641. registerExtraFunction(fn, evt, _fn);
  642. return _fn;
  643. }
  644. },
  645. //
  646. // registers an 'extra' function on some event listener function we were given - a function that we
  647. // created and bound to the element as part of our housekeeping, and which we want to unbind and remove
  648. // whenever the given function is unbound.
  649. registerExtraFunction = function(fn, evt, newFn) {
  650. fn.__taExtra = fn.__taExtra || [];
  651. fn.__taExtra.push([evt, newFn]);
  652. },
  653. DefaultHandler = function(obj, evt, fn, children) {
  654. // TODO: this was here originally because i wanted to handle devices that are both touch AND mouse. however this can cause certain of the helper
  655. // functions to be bound twice, as - for example - on a nexus 4, both a mouse event and a touch event are fired. the use case i had in mind
  656. // was a device such as an Asus touch pad thing, which has a touch pad but can also be controlled with a mouse.
  657. //if (isMouseDevice)
  658. // _bind(obj, evt, _curryChildFilter(children, obj, fn, evt), fn);
  659. if (isTouchDevice && touchMap[evt]) {
  660. _bind(obj, touchMap[evt], _curryChildFilter(children, obj, fn, touchMap[evt]), fn);
  661. }
  662. else
  663. _bind(obj, evt, _curryChildFilter(children, obj, fn, evt), fn);
  664. },
  665. SmartClickHandler = function(obj, evt, fn, children) {
  666. if (obj.__taSmartClicks == null) {
  667. var down = function(e) { obj.__tad = _pageLocation(e); },
  668. up = function(e) { obj.__tau = _pageLocation(e); },
  669. click = function(e) {
  670. if (obj.__tad && obj.__tau && obj.__tad[0] === obj.__tau[0] && obj.__tad[1] === obj.__tau[1]) {
  671. for (var i = 0; i < obj.__taSmartClicks.length; i++)
  672. obj.__taSmartClicks[i].apply(_t(e), [ e ]);
  673. }
  674. };
  675. DefaultHandler(obj, "mousedown", down, children);
  676. DefaultHandler(obj, "mouseup", up, children);
  677. DefaultHandler(obj, "click", click, children);
  678. obj.__taSmartClicks = [];
  679. }
  680. // store in the list of callbacks
  681. obj.__taSmartClicks.push(fn);
  682. // the unstore function removes this function from the object's listener list for this type.
  683. fn.__taUnstore = function() {
  684. _d(obj.__taSmartClicks, fn);
  685. };
  686. },
  687. _tapProfiles = {
  688. "tap":{touches:1, taps:1},
  689. "dbltap":{touches:1, taps:2},
  690. "contextmenu":{touches:2, taps:1}
  691. },
  692. TapHandler = function(clickThreshold, dblClickThreshold) {
  693. return function(obj, evt, fn, children) {
  694. // if event is contextmenu, for devices which are mouse only, we want to
  695. // use the default bind.
  696. if (evt == "contextmenu" && isMouseDevice)
  697. DefaultHandler(obj, evt, fn, children);
  698. else {
  699. // the issue here is that this down handler gets registered only for the
  700. // child nodes in the first registration. in fact it should be registered with
  701. // no child selector and then on down we should cycle through the regustered
  702. // functions to see if one of them matches. on mouseup we should execute ALL of
  703. // the functions whose children are either null or match the element.
  704. if (obj.__taTapHandler == null) {
  705. var tt = obj.__taTapHandler = {
  706. tap:[],
  707. dbltap:[],
  708. contextmenu:[],
  709. down:false,
  710. taps:0,
  711. downSelectors:[]
  712. };
  713. var down = function(e) {
  714. var target = e.srcElement || e.target;
  715. for (var i = 0; i < tt.downSelectors.length; i++) {
  716. if (tt.downSelectors[i] == null || matchesSelector(target, tt.downSelectors[i], obj)) {
  717. tt.down = true;
  718. setTimeout(clearSingle, clickThreshold);
  719. setTimeout(clearDouble, dblClickThreshold);
  720. break; // we only need one match on mousedown
  721. }
  722. }
  723. },
  724. up = function(e) {
  725. if (tt.down) {
  726. var target = e.srcElement || e.target;
  727. tt.taps++;
  728. var tc = _touchCount(e);
  729. for (var eventId in _tapProfiles) {
  730. var p = _tapProfiles[eventId];
  731. if (p.touches === tc && (p.taps === 1 || p.taps === tt.taps)) {
  732. for (var i = 0; i < tt[eventId].length; i++) {
  733. if (tt[eventId][i][1] == null || matchesSelector(target, tt[eventId][i][1], obj))
  734. tt[eventId][i][0].apply(_t(e), [ e ]);
  735. }
  736. }
  737. }
  738. }
  739. },
  740. clearSingle = function() {
  741. tt.down = false;
  742. },
  743. clearDouble = function() {
  744. tt.taps = 0;
  745. };
  746. DefaultHandler(obj, "mousedown", down/*, children*/);
  747. DefaultHandler(obj, "mouseup", up/*, children*/);
  748. }
  749. // add this child selector (it can be null, that's fine).
  750. obj.__taTapHandler.downSelectors.push(children);
  751. obj.__taTapHandler[evt].push([fn, children]);
  752. // the unstore function removes this function from the object's listener list for this type.
  753. fn.__taUnstore = function() {
  754. _d(obj.__taTapHandler[evt], fn);
  755. };
  756. }
  757. };
  758. },
  759. meeHelper = function(type, evt, obj, target) {
  760. for (var i in obj.__tamee[type]) {
  761. obj.__tamee[type][i].apply(target, [ evt ]);
  762. }
  763. },
  764. MouseEnterExitHandler = function() {
  765. var activeElements = [];
  766. return function(obj, evt, fn, children) {
  767. if (!obj.__tamee) {
  768. // __tamee holds a flag saying whether the mouse is currently "in" the element, and a list of
  769. // both mouseenter and mouseexit functions.
  770. obj.__tamee = { over:false, mouseenter:[], mouseexit:[] };
  771. // register over and out functions
  772. var over = function(e) {
  773. var t = _t(e);
  774. if ( (children== null && (t == obj && !obj.__tamee.over)) || (matchesSelector(t, children, obj) && (t.__tamee == null || !t.__tamee.over)) ) {
  775. meeHelper("mouseenter", e, obj, t);
  776. t.__tamee = t.__tamee || {};
  777. t.__tamee.over = true;
  778. activeElements.push(t);
  779. }
  780. },
  781. out = function(e) {
  782. var t = _t(e);
  783. // is the current target one of the activeElements? and is the
  784. // related target NOT a descendant of it?
  785. for (var i = 0; i < activeElements.length; i++) {
  786. if (t == activeElements[i] && !matchesSelector((e.relatedTarget || e.toElement), "*", t)) {
  787. t.__tamee.over = false;
  788. activeElements.splice(i, 1);
  789. meeHelper("mouseexit", e, obj, t);
  790. }
  791. }
  792. };
  793. _bind(obj, "mouseover", _curryChildFilter(children, obj, over, "mouseover"), over);
  794. _bind(obj, "mouseout", _curryChildFilter(children, obj, out, "mouseout"), out);
  795. }
  796. fn.__taUnstore = function() {
  797. delete obj.__tamee[evt][fn.__tauid];
  798. };
  799. _store(obj, evt, fn);
  800. obj.__tamee[evt][fn.__tauid] = fn;
  801. };
  802. },
  803. isTouchDevice = "ontouchstart" in document.documentElement,
  804. isMouseDevice = "onmousedown" in document.documentElement,
  805. touchMap = { "mousedown":"touchstart", "mouseup":"touchend", "mousemove":"touchmove" },
  806. touchstart="touchstart",touchend="touchend",touchmove="touchmove",
  807. ta_down = "__MottleDown", ta_up = "__MottleUp",
  808. ta_context_down = "__MottleContextDown", ta_context_up = "__MottleContextUp",
  809. iev = (function() {
  810. var rv = -1;
  811. if (navigator.appName == 'Microsoft Internet Explorer') {
  812. var ua = navigator.userAgent,
  813. re = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
  814. if (re.exec(ua) != null)
  815. rv = parseFloat(RegExp.$1);
  816. }
  817. return rv;
  818. })(),
  819. isIELT9 = iev > -1 && iev < 9,
  820. _genLoc = function(e, prefix) {
  821. if (e == null) return [ 0, 0 ];
  822. var ts = _touches(e), t = _getTouch(ts, 0);
  823. return [t[prefix + "X"], t[prefix + "Y"]];
  824. },
  825. _pageLocation = function(e) {
  826. if (e == null) return [ 0, 0 ];
  827. if (isIELT9) {
  828. return [ e.clientX + document.documentElement.scrollLeft, e.clientY + document.documentElement.scrollTop ];
  829. }
  830. else {
  831. return _genLoc(e, "page");
  832. }
  833. },
  834. _screenLocation = function(e) {
  835. return _genLoc(e, "screen");
  836. },
  837. _clientLocation = function(e) {
  838. return _genLoc(e, "client");
  839. },
  840. _getTouch = function(touches, idx) { return touches.item ? touches.item(idx) : touches[idx]; },
  841. _touches = function(e) {
  842. return e.touches && e.touches.length > 0 ? e.touches :
  843. e.changedTouches && e.changedTouches.length > 0 ? e.changedTouches :
  844. e.targetTouches && e.targetTouches.length > 0 ? e.targetTouches :
  845. [ e ];
  846. },
  847. _touchCount = function(e) { return _touches(e).length; },
  848. //http://www.quirksmode.org/blog/archives/2005/10/_and_the_winner_1.html
  849. _bind = function( obj, type, fn, originalFn) {
  850. _store(obj, type, fn);
  851. originalFn.__tauid = fn.__tauid;
  852. if (obj.addEventListener)
  853. obj.addEventListener( type, fn, false );
  854. else if (obj.attachEvent) {
  855. var key = type + fn.__tauid;
  856. obj["e" + key] = fn;
  857. // TODO look at replacing with .call(..)
  858. obj[key] = function() {
  859. obj["e"+key] && obj["e"+key]( window.event );
  860. };
  861. obj.attachEvent( "on"+type, obj[key] );
  862. }
  863. },
  864. _unbind = function( obj, type, fn) {
  865. if (fn == null) return;
  866. _each(obj, function() {
  867. var _el = _gel(this);
  868. _unstore(_el, type, fn);
  869. // it has been bound if there is a tauid. otherwise it was not bound and we can ignore it.
  870. if (fn.__tauid != null) {
  871. if (_el.removeEventListener)
  872. _el.removeEventListener( type, fn, false );
  873. else if (this.detachEvent) {
  874. var key = type + fn.__tauid;
  875. _el[key] && _el.detachEvent( "on"+type, _el[key] );
  876. _el[key] = null;
  877. _el["e"+key] = null;
  878. }
  879. }
  880. });
  881. },
  882. _devNull = function() {},
  883. _each = function(obj, fn) {
  884. if (obj == null) return;
  885. // if a list (or list-like), use it. if a string, get a list
  886. // by running the string through querySelectorAll. else, assume
  887. // it's an Element.
  888. obj = (typeof obj !== "string") && (obj.tagName == null && obj.length != null) ? obj : typeof obj === "string" ? document.querySelectorAll(obj) : [ obj ];
  889. for (var i = 0; i < obj.length; i++)
  890. fn.apply(obj[i]);
  891. };
  892. /**
  893. * Event handler. Offers support for abstracting out the differences
  894. * between touch and mouse devices, plus "smart click" functionality
  895. * (don't fire click if the mouse has moved betweeb mousedown and mouseup),
  896. * and synthesized click/tap events.
  897. * @class Mottle
  898. * @constructor
  899. * @param {Object} params Constructor params
  900. * @param {Integer} [params.clickThreshold=150] Threshold, in milliseconds beyond which a touchstart followed by a touchend is not considered to be a click.
  901. * @param {Integer} [params.dblClickThreshold=350] Threshold, in milliseconds beyond which two successive tap events are not considered to be a click.
  902. * @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
  903. * requires that Mottle consume the mousedown event, and so may not be viable in all use cases.
  904. */
  905. this.Mottle = function(params) {
  906. params = params || {};
  907. var self = this,
  908. clickThreshold = params.clickThreshold || 150,
  909. dblClickThreshold = params.dblClickThreshold || 350,
  910. mouseEnterExitHandler = new MouseEnterExitHandler(),
  911. tapHandler = new TapHandler(clickThreshold, dblClickThreshold),
  912. _smartClicks = params.smartClicks,
  913. _doBind = function(obj, evt, fn, children) {
  914. if (fn == null) return;
  915. _each(obj, function() {
  916. var _el = _gel(this);
  917. if (_smartClicks && evt === "click")
  918. SmartClickHandler(_el, evt, fn, children);
  919. else if (evt === "tap" || evt === "dbltap" || evt === "contextmenu") {
  920. tapHandler(_el, evt, fn, children);
  921. }
  922. else if (evt === "mouseenter" || evt == "mouseexit")
  923. mouseEnterExitHandler(_el, evt, fn, children);
  924. else
  925. DefaultHandler(_el, evt, fn, children);
  926. });
  927. };
  928. /**
  929. * Removes an element from the DOM, and unregisters all event handlers for it. You should use this
  930. * to ensure you don't leak memory.
  931. * @method remove
  932. * @param {String|Element} el Element, or id of the element, to remove.
  933. * @return {Mottle} The current Mottle instance; you can chain this method.
  934. */
  935. this.remove = function(el) {
  936. _each(el, function() {
  937. var _el = _gel(this);
  938. if (_el.__ta) {
  939. for (var evt in _el.__ta) {
  940. for (var h in _el.__ta[evt]) {
  941. _unbind(_el, evt, _el.__ta[evt][h]);
  942. }
  943. }
  944. }
  945. _el.parentNode && _el.parentNode.removeChild(_el);
  946. });
  947. return this;
  948. };
  949. /**
  950. * Register an event handler, optionally as a delegate for some set of descendant elements. Note
  951. * that this method takes either 3 or 4 arguments - if you supply 3 arguments it is assumed you have
  952. * omitted the `children` parameter, and that the event handler should be bound directly to the given element.
  953. * @method on
  954. * @param {Element[]|Element|String} el Either an Element, or a CSS spec for a list of elements, or an array of Elements.
  955. * @param {String} [children] Comma-delimited list of selectors identifying allowed children.
  956. * @param {String} event Event ID.
  957. * @param {Function} fn Event handler function.
  958. * @return {Mottle} The current Mottle instance; you can chain this method.
  959. */
  960. this.on = function(el, event, children, fn) {
  961. var _el = arguments[0],
  962. _c = arguments.length == 4 ? arguments[2] : null,
  963. _e = arguments[1],
  964. _f = arguments[arguments.length - 1];
  965. _doBind(_el, _e, _f, _c);
  966. return this;
  967. };
  968. /**
  969. * Cancel delegate event handling for the given function. Note that unlike with 'on' you do not supply
  970. * a list of child selectors here: it removes event delegation from all of the child selectors for which the
  971. * given function was registered (if any).
  972. * @method off
  973. * @param {Element[]|Element|String} el Element - or ID of element - from which to remove event listener.
  974. * @param {String} event Event ID.
  975. * @param {Function} fn Event handler function.
  976. * @return {Mottle} The current Mottle instance; you can chain this method.
  977. */
  978. this.off = function(el, evt, fn) {
  979. _unbind(el, evt, fn);
  980. return this;
  981. };
  982. /**
  983. * Triggers some event for a given element.
  984. * @method trigger
  985. * @param {Element} el Element for which to trigger the event.
  986. * @param {String} event Event ID.
  987. * @param {Event} originalEvent The original event. Should be optional of course, but currently is not, due
  988. * to the jsPlumb use case that caused this method to be added.
  989. * @param {Object} [payload] Optional object to set as `payload` on the generated event; useful for message passing.
  990. * @return {Mottle} The current Mottle instance; you can chain this method.
  991. */
  992. this.trigger = function(el, event, originalEvent, payload) {
  993. var eventToBind = (isTouchDevice && touchMap[event]) ? touchMap[event] : event;
  994. var pl = _pageLocation(originalEvent), sl = _screenLocation(originalEvent), cl = _clientLocation(originalEvent);
  995. _each(el, function() {
  996. var _el = _gel(this), evt;
  997. originalEvent = originalEvent || {
  998. screenX:sl[0],
  999. screenY:sl[1],
  1000. clientX:cl[0],
  1001. clientY:cl[1]
  1002. };
  1003. var _decorate = function(_evt) {
  1004. if (payload) _evt.payload = payload;
  1005. };
  1006. var eventGenerators = {
  1007. "TouchEvent":function(evt) {
  1008. var t = document.createTouch(window, _el, 0, pl[0], pl[1],
  1009. sl[0], sl[1],
  1010. cl[0], cl[1],
  1011. 0,0,0,0);
  1012. evt.initTouchEvent(eventToBind, true, true, window, 0,
  1013. sl[0], sl[1],
  1014. cl[0], cl[1],
  1015. false, false, false, false, document.createTouchList(t));
  1016. },
  1017. "MouseEvents":function(evt) {
  1018. evt.initMouseEvent(eventToBind, true, true, window, 0,
  1019. sl[0], sl[1],
  1020. cl[0], cl[1],
  1021. false, false, false, false, 1, _el);
  1022. if (Sniff.android) {
  1023. // Android's touch events are not standard.
  1024. var t = document.createTouch(window, _el, 0, pl[0], pl[1],
  1025. sl[0], sl[1],
  1026. cl[0], cl[1],
  1027. 0,0,0,0);
  1028. evt.touches = evt.targetTouches = evt.changedTouches = document.createTouchList(t);
  1029. }
  1030. }
  1031. };
  1032. if (document.createEvent) {
  1033. var ite = (isTouchDevice && touchMap[event] && !Sniff.android), evtName = ite ? "TouchEvent" : "MouseEvents";
  1034. evt = document.createEvent(evtName);
  1035. eventGenerators[evtName](evt);
  1036. _decorate(evt);
  1037. _el.dispatchEvent(evt);
  1038. }
  1039. else if (document.createEventObject) {
  1040. evt = document.createEventObject();
  1041. evt.eventType = evt.eventName = eventToBind;
  1042. evt.screenX = sl[0];
  1043. evt.screenY = sl[1];
  1044. evt.clientX = cl[0];
  1045. evt.clientY = cl[1];
  1046. _decorate(evt);
  1047. _el.fireEvent('on' + eventToBind, evt);
  1048. }
  1049. });
  1050. return this;
  1051. }
  1052. };
  1053. /**
  1054. * Static method to assist in 'consuming' an element: uses `stopPropagation` where available, or sets `e.returnValue=false` where it is not.
  1055. * @method Mottle.consume
  1056. * @param {Event} e Event to consume
  1057. * @param {Boolean} [doNotPreventDefault=false] If true, does not call `preventDefault()` on the event.
  1058. */
  1059. Mottle.consume = function(e, doNotPreventDefault) {
  1060. if (e.stopPropagation)
  1061. e.stopPropagation();
  1062. else
  1063. e.returnValue = false;
  1064. if (!doNotPreventDefault && e.preventDefault)
  1065. e.preventDefault();
  1066. };
  1067. /**
  1068. * Gets the page location corresponding to the given event. For touch events this means get the page location of the first touch.
  1069. * @method Mottle.pageLocation
  1070. * @param {Event} e Event to get page location for.
  1071. * @return {Integer[]} [left, top] for the given event.
  1072. */
  1073. Mottle.pageLocation = _pageLocation;
  1074. }).call(this);
  1075. /*
  1076. * jsPlumb
  1077. *
  1078. * Title:jsPlumb 1.7.2
  1079. *
  1080. * Provides a way to visually connect elements on an HTML page, using SVG or VML.
  1081. *
  1082. * This file contains utility functions that run in both browsers and headless.
  1083. *
  1084. * Copyright (c) 2010 - 2014 Simon Porritt (simon@jsplumbtoolkit.com)
  1085. *
  1086. * http://jsplumbtoolkit.com
  1087. * http://github.com/sporritt/jsplumb
  1088. *
  1089. * Dual licensed under the MIT and GPL2 licenses.
  1090. */
  1091. ;(function() {
  1092. var _isa = function(a) { return Object.prototype.toString.call(a) === "[object Array]"; },
  1093. _isnum = function(n) { return Object.prototype.toString.call(n) === "[object Number]"; },
  1094. _iss = function(s) { return typeof s === "string"; },
  1095. _isb = function(s) { return typeof s === "boolean"; },
  1096. _isnull = function(s) { return s == null; },
  1097. _iso = function(o) { return o == null ? false : Object.prototype.toString.call(o) === "[object Object]"; },
  1098. _isd = function(o) { return Object.prototype.toString.call(o) === "[object Date]"; },
  1099. _isf = function(o) { return Object.prototype.toString.call(o) === "[object Function]"; },
  1100. _ise = function(o) {
  1101. for (var i in o) { if (o.hasOwnProperty(i)) return false; }
  1102. return true;
  1103. },
  1104. pointHelper = function(p1, p2, fn) {
  1105. p1 = _isa(p1) ? p1 : [p1.x, p1.y];
  1106. p2 = _isa(p2) ? p2 : [p2.x, p2.y];
  1107. return fn(p1, p2);
  1108. };
  1109. var root = this;
  1110. var exports = root.jsPlumbUtil = {
  1111. isArray : _isa,
  1112. isString : _iss,
  1113. isBoolean: _isb,
  1114. isNull : _isnull,
  1115. isObject : _iso,
  1116. isDate : _isd,
  1117. isFunction: _isf,
  1118. isEmpty:_ise,
  1119. isNumber:_isnum,
  1120. clone : function(a) {
  1121. if (_iss(a)) return "" + a;
  1122. else if (_isb(a)) return !!a;
  1123. else if (_isd(a)) return new Date(a.getTime());
  1124. else if (_isf(a)) return a;
  1125. else if (_isa(a)) {
  1126. var b = [];
  1127. for (var i = 0; i < a.length; i++)
  1128. b.push(this.clone(a[i]));
  1129. return b;
  1130. }
  1131. else if (_iso(a)) {
  1132. var c = {};
  1133. for (var j in a)
  1134. c[j] = this.clone(a[j]);
  1135. return c;
  1136. }
  1137. else return a;
  1138. },
  1139. merge : function(a, b, collations) {
  1140. // first change the collations array - if present - into a lookup table, because its faster.
  1141. var cMap = {}, ar, i;
  1142. collations = collations || [];
  1143. for (i = 0; i < collations.length; i++)
  1144. cMap[collations[i]] = true;
  1145. var c = this.clone(a);
  1146. for (i in b) {
  1147. if (c[i] == null)
  1148. c[i] = b[i];
  1149. else if (_iss(b[i]) || _isb(b[i])) {
  1150. if (!cMap[i]) c[i] = b[i]; // if we dont want to collate, just copy it in.
  1151. else {
  1152. ar = [];
  1153. // if c's object is also an array we can keep its values.
  1154. ar.push.apply(ar, _isa(c[i]) ? c[i] : [ c[i] ] );
  1155. ar.push.apply(ar, _isa(b[i]) ? b[i] : [ b[i] ] );
  1156. c[i] = ar;
  1157. }
  1158. }
  1159. else {
  1160. if (_isa(b[i])) {
  1161. ar = [];
  1162. // if c's object is also an array we can keep its values.
  1163. if (_isa(c[i])) ar.push.apply(ar, c[i]);
  1164. ar.push.apply(ar, b[i]);
  1165. c[i] = ar;
  1166. }
  1167. else if(_iso(b[i])) {
  1168. // overwite c's value with an object if it is not already one.
  1169. if (!_iso(c[i]))
  1170. c[i] = {};
  1171. for (var j in b[i])
  1172. c[i][j] = b[i][j];
  1173. }
  1174. }
  1175. }
  1176. return c;
  1177. },
  1178. replace:function(inObj, path, value) {
  1179. if (inObj == null) return;
  1180. var q = inObj, t = q;
  1181. path.replace(/([^\.])+/g, function(term, lc, pos, str) {
  1182. var array = term.match(/([^\[0-9]+){1}(\[)([0-9+])/),
  1183. last = pos + term.length >= str.length,
  1184. _getArray = function() {
  1185. return t[array[1]] || (function() { t[array[1]] = []; return t[array[1]]; })();
  1186. };
  1187. if (last) {
  1188. // set term = value on current t, creating term as array if necessary.
  1189. if (array)
  1190. _getArray()[array[3]] = value;
  1191. else
  1192. t[term] = value;
  1193. }
  1194. else {
  1195. // set to current t[term], creating t[term] if necessary.
  1196. if (array) {
  1197. var a = _getArray();
  1198. t = a[array[3]] || (function() { a[array[3]] = {}; return a[array[3]]; })();
  1199. }
  1200. else
  1201. t = t[term] || (function() { t[term] = {}; return t[term]; })();
  1202. }
  1203. });
  1204. return inObj;
  1205. },
  1206. //
  1207. // chain a list of functions, supplied by [ object, method name, args ], and return on the first
  1208. // one that returns the failValue. if none return the failValue, return the successValue.
  1209. //
  1210. functionChain : function(successValue, failValue, fns) {
  1211. for (var i = 0; i < fns.length; i++) {
  1212. var o = fns[i][0][fns[i][1]].apply(fns[i][0], fns[i][2]);
  1213. if (o === failValue) {
  1214. return o;
  1215. }
  1216. }
  1217. return successValue;
  1218. },
  1219. // take the given model and expand out any parameters.
  1220. populate : function(model, values) {
  1221. // for a string, see if it has parameter matches, and if so, try to make the substitutions.
  1222. var getValue = function(fromString) {
  1223. var matches = fromString.match(/(\${.*?})/g);
  1224. if (matches != null) {
  1225. for (var i = 0; i < matches.length; i++) {
  1226. var val = values[matches[i].substring(2, matches[i].length - 1)] || "";
  1227. if (val != null) {
  1228. fromString = fromString.replace(matches[i], val);
  1229. }
  1230. }
  1231. }
  1232. return fromString;
  1233. },
  1234. // process one entry.
  1235. _one = function(d) {
  1236. if (d != null) {
  1237. if (_iss(d)) {
  1238. return getValue(d);
  1239. }
  1240. else if (_isa(d)) {
  1241. var r = [];
  1242. for (var i = 0; i < d.length; i++)
  1243. r.push(_one(d[i]));
  1244. return r;
  1245. }
  1246. else if (_iso(d)) {
  1247. var s = {};
  1248. for (var j in d) {
  1249. s[j] = _one(d[j]);
  1250. }
  1251. return s;
  1252. }
  1253. else {
  1254. return d;
  1255. }
  1256. }
  1257. };
  1258. return _one(model);
  1259. },
  1260. convertStyle : function(s, ignoreAlpha) {
  1261. // TODO: jsPlumb should support a separate 'opacity' style member.
  1262. if ("transparent" === s) return s;
  1263. var o = s,
  1264. pad = function(n) { return n.length == 1 ? "0" + n : n; },
  1265. hex = function(k) { return pad(Number(k).toString(16)); },
  1266. pattern = /(rgb[a]?\()(.*)(\))/;
  1267. if (s.match(pattern)) {
  1268. var parts = s.match(pattern)[2].split(",");
  1269. o = "#" + hex(parts[0]) + hex(parts[1]) + hex(parts[2]);
  1270. if (!ignoreAlpha && parts.length == 4)
  1271. o = o + hex(parts[3]);
  1272. }
  1273. return o;
  1274. },
  1275. findWithFunction : function(a, f) {
  1276. if (a)
  1277. for (var i = 0; i < a.length; i++) if (f(a[i])) return i;
  1278. return -1;
  1279. },
  1280. indexOf : function(l, v) {
  1281. return l.indexOf ? l.indexOf(v) : exports.findWithFunction(l, function(_v) { return _v == v; });
  1282. },
  1283. removeWithFunction : function(a, f) {
  1284. var idx = exports.findWithFunction(a, f);
  1285. if (idx > -1) a.splice(idx, 1);
  1286. return idx != -1;
  1287. },
  1288. remove : function(l, v) {
  1289. var idx = exports.indexOf(l, v);
  1290. if (idx > -1) l.splice(idx, 1);
  1291. return idx != -1;
  1292. },
  1293. // TODO support insert index
  1294. addWithFunction : function(list, item, hashFunction) {
  1295. if (exports.findWithFunction(list, hashFunction) == -1) list.push(item);
  1296. },
  1297. addToList : function(map, key, value, insertAtStart) {
  1298. var l = map[key];
  1299. if (l == null) {
  1300. l = [];
  1301. map[key] = l;
  1302. }
  1303. l[insertAtStart ? "unshift" : "push"](value);
  1304. return l;
  1305. },
  1306. //
  1307. // extends the given obj (which can be an array) with the given constructor function, prototype functions, and
  1308. // class members, any of which may be null.
  1309. //
  1310. extend : function(child, parent, _protoFn) {
  1311. var i;
  1312. parent = _isa(parent) ? parent : [ parent ];
  1313. for (i = 0; i < parent.length; i++) {
  1314. for (var j in parent[i].prototype) {
  1315. if(parent[i].prototype.hasOwnProperty(j)) {
  1316. child.prototype[j] = parent[i].prototype[j];
  1317. }
  1318. }
  1319. }
  1320. var _makeFn = function(name, protoFn) {
  1321. return function() {
  1322. for (i = 0; i < parent.length; i++) {
  1323. if (parent[i].prototype[name])
  1324. parent[i].prototype[name].apply(this, arguments);
  1325. }
  1326. return protoFn.apply(this, arguments);
  1327. };
  1328. };
  1329. var _oneSet = function(fns) {
  1330. for (var k in fns) {
  1331. child.prototype[k] = _makeFn(k, fns[k]);
  1332. }
  1333. };
  1334. if (arguments.length > 2) {
  1335. for (i = 2; i < arguments.length; i++)
  1336. _oneSet(arguments[i]);
  1337. }
  1338. return child;
  1339. },
  1340. uuid : function() {
  1341. return ('xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
  1342. var r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8);
  1343. return v.toString(16);
  1344. }));
  1345. },
  1346. logEnabled : true,
  1347. log : function() {
  1348. if (exports.logEnabled && typeof console != "undefined") {
  1349. try {
  1350. var msg = arguments[arguments.length - 1];
  1351. console.log(msg);
  1352. }
  1353. catch (e) {}
  1354. }
  1355. },
  1356. /**
  1357. * Wraps one function with another, creating a placeholder for the
  1358. * wrapped function if it was null. this is used to wrap the various
  1359. * drag/drop event functions - to allow jsPlumb to be notified of
  1360. * important lifecycle events without imposing itself on the user's
  1361. * drag/drop functionality.
  1362. * @method jsPlumbUtil.wrap
  1363. * @param {Function} wrappedFunction original function to wrap; may be null.
  1364. * @param {Function} newFunction function to wrap the original with.
  1365. * @param {Object} [returnOnThisValue] Optional. Indicates that the wrappedFunction should
  1366. * not be executed if the newFunction returns a value matching 'returnOnThisValue'.
  1367. * note that this is a simple comparison and only works for primitives right now.
  1368. */
  1369. wrap : function(wrappedFunction, newFunction, returnOnThisValue) {
  1370. wrappedFunction = wrappedFunction || function() { };
  1371. newFunction = newFunction || function() { };
  1372. return function() {
  1373. var r = null;
  1374. try {
  1375. r = newFunction.apply(this, arguments);
  1376. } catch (e) {
  1377. exports.log("jsPlumb function failed : " + e);
  1378. }
  1379. if (returnOnThisValue == null || (r !== returnOnThisValue)) {
  1380. try {
  1381. r = wrappedFunction.apply(this, arguments);
  1382. } catch (e) {
  1383. exports.log("wrapped function failed : " + e);
  1384. }
  1385. }
  1386. return r;
  1387. };
  1388. }
  1389. };
  1390. exports.EventGenerator = function() {
  1391. var _listeners = {},
  1392. eventsSuspended = false,
  1393. // this is a list of events that should re-throw any errors that occur during their dispatch. it is current private.
  1394. eventsToDieOn = { "ready":true };
  1395. this.bind = function(event, listener, insertAtStart) {
  1396. exports.addToList(_listeners, event, listener, insertAtStart);
  1397. return this;
  1398. };
  1399. this.fire = function(event, value, originalEvent) {
  1400. if (!eventsSuspended && _listeners[event]) {
  1401. var l = _listeners[event].length, i = 0, _gone = false, ret = null;
  1402. if (!this.shouldFireEvent || this.shouldFireEvent(event, value, originalEvent)) {
  1403. while (!_gone && i < l && ret !== false) {
  1404. // doing it this way rather than catching and then possibly re-throwing means that an error propagated by this
  1405. // method will have the whole call stack available in the debugger.
  1406. if (eventsToDieOn[event])
  1407. _listeners[event][i].apply(this, [ value, originalEvent]);
  1408. else {
  1409. try {
  1410. ret = _listeners[event][i].apply(this, [ value, originalEvent ]);
  1411. } catch (e) {
  1412. exports.log("jsPlumb: fire failed for event " + event + " : " + e);
  1413. }
  1414. }
  1415. i++;
  1416. if (_listeners == null || _listeners[event] == null)
  1417. _gone = true;
  1418. }
  1419. }
  1420. }
  1421. return this;
  1422. };
  1423. this.unbind = function(event) {
  1424. if (event)
  1425. delete _listeners[event];
  1426. else {
  1427. _listeners = {};
  1428. }
  1429. return this;
  1430. };
  1431. this.getListener = function(forEvent) { return _listeners[forEvent]; };
  1432. this.setSuspendEvents = function(val) { eventsSuspended = val; };
  1433. this.isSuspendEvents = function() { return eventsSuspended; };
  1434. this.cleanupListeners = function() {
  1435. for (var i in _listeners) {
  1436. _listeners[i] = null;
  1437. }
  1438. };
  1439. };
  1440. exports.EventGenerator.prototype = {
  1441. cleanup:function() {
  1442. this.cleanupListeners();
  1443. }
  1444. };
  1445. // thanks MDC
  1446. // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind?redirectlocale=en-US&redirectslug=JavaScript%2FReference%2FGlobal_Objects%2FFunction%2Fbind
  1447. if (!Function.prototype.bind) {
  1448. Function.prototype.bind = function (oThis) {
  1449. if (typeof this !== "function") {
  1450. // closest thing possible to the ECMAScript 5 internal IsCallable function
  1451. throw new TypeError("Function.prototype.bind - what is trying to be bound is not callable");
  1452. }
  1453. var aArgs = Array.prototype.slice.call(arguments, 1),
  1454. fToBind = this,
  1455. fNOP = function () {},
  1456. fBound = function () {
  1457. return fToBind.apply(this instanceof fNOP && oThis ? this : oThis,
  1458. aArgs.concat(Array.prototype.slice.call(arguments)));
  1459. };
  1460. fNOP.prototype = this.prototype;
  1461. fBound.prototype = new fNOP();
  1462. return fBound;
  1463. };
  1464. }
  1465. }).call(this);
  1466. /*
  1467. * jsPlumb
  1468. *
  1469. * Title:jsPlumb 1.7.2
  1470. *
  1471. * Provides a way to visually connect elements on an HTML page, using SVG or VML.
  1472. *
  1473. * This file contains utility functions that run browsers only.
  1474. *
  1475. * Copyright (c) 2010 - 2014 Simon Porritt (simon@jsplumbtoolkit.com)
  1476. *
  1477. * http://jsplumbtoolkit.com
  1478. * http://github.com/sporritt/jsplumb
  1479. *
  1480. * Dual licensed under the MIT and GPL2 licenses.
  1481. */
  1482. ;(function() {
  1483. "use strict";
  1484. var root = this;
  1485. var exports = root.jsPlumbUtil;
  1486. exports.ieVersion = /MSIE\s([\d.]+)/.test(navigator.userAgent) ? (new Number(RegExp.$1)) : -1;
  1487. exports.oldIE = exports.ieVersion > -1 && exports.ieVersion < 9;
  1488. exports.matchesSelector = function(el, selector, ctx) {
  1489. ctx = ctx || el.parentNode;
  1490. var possibles = ctx.querySelectorAll(selector);
  1491. for (var i = 0; i < possibles.length; i++) {
  1492. if (possibles[i] === el)
  1493. return true;
  1494. }
  1495. return false;
  1496. };
  1497. exports.consume = function(e, doNotPreventDefault) {
  1498. if (e.stopPropagation)
  1499. e.stopPropagation();
  1500. else
  1501. e.returnValue = false;
  1502. if (!doNotPreventDefault && e.preventDefault)
  1503. e.preventDefault();
  1504. };
  1505. /*
  1506. * Function: sizeElement
  1507. * Helper to size and position an element. You would typically use
  1508. * this when writing your own Connector or Endpoint implementation.
  1509. *
  1510. * Parameters:
  1511. * x - [int] x position for the element origin
  1512. * y - [int] y position for the element origin
  1513. * w - [int] width of the element
  1514. * h - [int] height of the element
  1515. *
  1516. */
  1517. exports.sizeElement = function(el, x, y, w, h) {
  1518. if (el) {
  1519. el.style.height = h + "px";
  1520. el.height = h;
  1521. el.style.width = w + "px";
  1522. el.width = w;
  1523. el.style.left = x + "px";
  1524. el.style.top = y + "px";
  1525. }
  1526. };
  1527. }).call(this);
  1528. /*
  1529. * jsPlumb
  1530. *
  1531. * Title:jsPlumb 1.7.2
  1532. *
  1533. * Provides a way to visually connect elements on an HTML page, using SVG or VML.
  1534. *
  1535. * This file contains the base functionality for DOM type adapters.
  1536. *
  1537. * Copyright (c) 2010 - 2014 Simon Porritt (simon@jsplumbtoolkit.com)
  1538. *
  1539. * http://jsplumbtoolkit.com
  1540. * http://github.com/sporritt/jsplumb
  1541. *
  1542. * Dual licensed under the MIT and GPL2 licenses.
  1543. */
  1544. ;(function() {
  1545. var root = this;
  1546. var svgAvailable = !!window.SVGAngle || document.implementation.hasFeature("http://www.w3.org/TR/SVG11/feature#BasicStructure", "1.1"),
  1547. vmlAvailable = function() {
  1548. if (vmlAvailable.vml === undefined) {
  1549. var a = document.body.appendChild(document.createElement('div'));
  1550. a.innerHTML = '<v:shape id="vml_flag1" adj="1" />';
  1551. var b = a.firstChild;
  1552. if (b != null && b.style != null) {
  1553. b.style.behavior = "url(#default#VML)";
  1554. vmlAvailable.vml = b ? typeof b.adj == "object": true;
  1555. }
  1556. else
  1557. vmlAvailable.vml = false;
  1558. a.parentNode.removeChild(a);
  1559. }
  1560. return vmlAvailable.vml;
  1561. },
  1562. // TODO: remove this once we remove all library adapter versions and have only vanilla jsplumb: this functionality
  1563. // comes from Mottle.
  1564. iev = (function() {
  1565. var rv = -1;
  1566. if (navigator.appName == 'Microsoft Internet Explorer') {
  1567. var ua = navigator.userAgent,
  1568. re = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
  1569. if (re.exec(ua) != null)
  1570. rv = parseFloat(RegExp.$1);
  1571. }
  1572. return rv;
  1573. })(),
  1574. isIELT9 = iev > -1 && iev < 9,
  1575. _genLoc = function(e, prefix) {
  1576. if (e == null) return [ 0, 0 ];
  1577. var ts = _touches(e), t = _getTouch(ts, 0);
  1578. return [t[prefix + "X"], t[prefix + "Y"]];
  1579. },
  1580. _pageLocation = function(e) {
  1581. if (e == null) return [ 0, 0 ];
  1582. if (isIELT9) {
  1583. return [ e.clientX + document.documentElement.scrollLeft, e.clientY + document.documentElement.scrollTop ];
  1584. }
  1585. else {
  1586. return _genLoc(e, "page");
  1587. }
  1588. },
  1589. _screenLocation = function(e) {
  1590. return _genLoc(e, "screen");
  1591. },
  1592. _clientLocation = function(e) {
  1593. return _genLoc(e, "client");
  1594. },
  1595. _getTouch = function(touches, idx) { return touches.item ? touches.item(idx) : touches[idx]; },
  1596. _touches = function(e) {
  1597. return e.touches && e.touches.length > 0 ? e.touches :
  1598. e.changedTouches && e.changedTouches.length > 0 ? e.changedTouches :
  1599. e.targetTouches && e.targetTouches.length > 0 ? e.targetTouches :
  1600. [ e ];
  1601. };
  1602. /**
  1603. Manages dragging for some instance of jsPlumb.
  1604. */
  1605. var DragManager = function(_currentInstance) {
  1606. var _draggables = {}, _dlist = [], _delements = {}, _elementsWithEndpoints = {},
  1607. // elementids mapped to the draggable to which they belong.
  1608. _draggablesForElements = {};
  1609. /**
  1610. register some element as draggable. right now the drag init stuff is done elsewhere, and it is
  1611. possible that will continue to be the case.
  1612. */
  1613. this.register = function(el) {
  1614. var id = _currentInstance.getId(el),
  1615. parentOffset = jsPlumbAdapter.getOffset(el, _currentInstance);
  1616. if (!_draggables[id]) {
  1617. _draggables[id] = el;
  1618. _dlist.push(el);
  1619. _delements[id] = {};
  1620. }
  1621. // look for child elements that have endpoints and register them against this draggable.
  1622. var _oneLevel = function(p, startOffset) {
  1623. if (p) {
  1624. for (var i = 0; i < p.childNodes.length; i++) {
  1625. if (p.childNodes[i].nodeType != 3 && p.childNodes[i].nodeType != 8) {
  1626. var cEl = jsPlumb.getElementObject(p.childNodes[i]),
  1627. cid = _currentInstance.getId(p.childNodes[i], null, true);
  1628. if (cid && _elementsWithEndpoints[cid] && _elementsWithEndpoints[cid] > 0) {
  1629. var cOff = jsPlumbAdapter.getOffset(cEl, _currentInstance);
  1630. _delements[id][cid] = {
  1631. id:cid,
  1632. offset:{
  1633. left:cOff.left - parentOffset.left,
  1634. top:cOff.top - parentOffset.top
  1635. }
  1636. };
  1637. _draggablesForElements[cid] = id;
  1638. }
  1639. _oneLevel(p.childNodes[i]);
  1640. }
  1641. }
  1642. }
  1643. };
  1644. _oneLevel(el);
  1645. };
  1646. // refresh the offsets for child elements of this element.
  1647. this.updateOffsets = function(elId) {
  1648. if (elId != null) {
  1649. var domEl = jsPlumb.getDOMElement(elId),
  1650. id = _currentInstance.getId(domEl),
  1651. children = _delements[id],
  1652. parentOffset = jsPlumbAdapter.getOffset(domEl, _currentInstance);
  1653. if (children) {
  1654. for (var i in children) {
  1655. var cel = jsPlumb.getElementObject(i),
  1656. cOff = jsPlumbAdapter.getOffset(cel, _currentInstance);
  1657. _delements[id][i] = {
  1658. id:i,
  1659. offset:{
  1660. left:cOff.left - parentOffset.left,
  1661. top:cOff.top - parentOffset.top
  1662. }
  1663. };
  1664. _draggablesForElements[i] = id;
  1665. }
  1666. }
  1667. }
  1668. };
  1669. /**
  1670. notification that an endpoint was added to the given el. we go up from that el's parent
  1671. node, looking for a parent that has been registered as a draggable. if we find one, we add this
  1672. el to that parent's list of elements to update on drag (if it is not there already)
  1673. */
  1674. this.endpointAdded = function(el, id) {
  1675. id = id || _currentInstance.getId(el);
  1676. var b = document.body,
  1677. p = el.parentNode;
  1678. _elementsWithEndpoints[id] = _elementsWithEndpoints[id] ? _elementsWithEndpoints[id] + 1 : 1;
  1679. while (p != null && p != b) {
  1680. var pid = _currentInstance.getId(p, null, true);
  1681. if (pid && _draggables[pid]) {
  1682. var pLoc = jsPlumbAdapter.getOffset(p, _currentInstance);
  1683. if (_delements[pid][id] == null) {
  1684. var cLoc = jsPlumbAdapter.getOffset(el, _currentInstance);
  1685. _delements[pid][id] = {
  1686. id:id,
  1687. offset:{
  1688. left:cLoc.left - pLoc.left,
  1689. top:cLoc.top - pLoc.top
  1690. }
  1691. };
  1692. _draggablesForElements[id] = pid;
  1693. }
  1694. break;
  1695. }
  1696. p = p.parentNode;
  1697. }
  1698. };
  1699. this.endpointDeleted = function(endpoint) {
  1700. if (_elementsWithEndpoints[endpoint.elementId]) {
  1701. _elementsWithEndpoints[endpoint.elementId]--;
  1702. if (_elementsWithEndpoints[endpoint.elementId] <= 0) {
  1703. for (var i in _delements) {
  1704. if (_delements[i]) {
  1705. delete _delements[i][endpoint.elementId];
  1706. delete _draggablesForElements[endpoint.elementId];
  1707. }
  1708. }
  1709. }
  1710. }
  1711. };
  1712. this.changeId = function(oldId, newId) {
  1713. _delements[newId] = _delements[oldId];
  1714. _delements[oldId] = {};
  1715. _draggablesForElements[newId] = _draggablesForElements[oldId];
  1716. _draggablesForElements[oldId] = null;
  1717. };
  1718. this.getElementsForDraggable = function(id) {
  1719. return _delements[id];
  1720. };
  1721. this.elementRemoved = function(elementId) {
  1722. var elId = _draggablesForElements[elementId];
  1723. if (elId) {
  1724. delete _delements[elId][elementId];
  1725. delete _draggablesForElements[elementId];
  1726. }
  1727. };
  1728. this.reset = function() {
  1729. _draggables = {};
  1730. _dlist = [];
  1731. _delements = {};
  1732. _elementsWithEndpoints = {};
  1733. };
  1734. //
  1735. // notification drag ended. We check automatically if need to update some
  1736. // ancestor's offsets.
  1737. //
  1738. this.dragEnded = function(el) {
  1739. var id = _currentInstance.getId(el),
  1740. ancestor = _draggablesForElements[id];
  1741. if (ancestor) this.updateOffsets(ancestor);
  1742. };
  1743. this.setParent = function(el, elId, p, pId) {
  1744. var current = _draggablesForElements[elId];
  1745. if (current) {
  1746. if (!_delements[pId])
  1747. _delements[pId] = {};
  1748. _delements[pId][elId] = _delements[current][elId];
  1749. delete _delements[current][elId];
  1750. var pLoc = jsPlumbAdapter.getOffset(p, _currentInstance),
  1751. cLoc = jsPlumbAdapter.getOffset(el, _currentInstance);
  1752. _delements[pId][elId].offset = {
  1753. left:cLoc.left - pLoc.left,
  1754. top:cLoc.top - pLoc.top
  1755. };
  1756. _draggablesForElements[elId] = pId;
  1757. }
  1758. };
  1759. this.getDragAncestor = function(el) {
  1760. var de = jsPlumb.getDOMElement(el),
  1761. id = _currentInstance.getId(de),
  1762. aid = _draggablesForElements[id];
  1763. if (aid)
  1764. return jsPlumb.getDOMElement(aid);
  1765. else
  1766. return null;
  1767. };
  1768. };
  1769. // for those browsers that dont have it. they still don't have it! but at least they won't crash.
  1770. if (!window.console)
  1771. window.console = { time:function(){}, timeEnd:function(){}, group:function(){}, groupEnd:function(){}, log:function(){} };
  1772. // TODO: katavorio default helper uses this stuff. should i extract to a support lib?
  1773. var trim = function(str) {
  1774. return str == null ? null : (str.replace(/^\s\s*/, '').replace(/\s\s*$/, ''));
  1775. },
  1776. _setClassName = function(el, cn) {
  1777. cn = trim(cn);
  1778. if (typeof el.className.baseVal != "undefined") // SVG
  1779. el.className.baseVal = cn;
  1780. else
  1781. el.className = cn;
  1782. },
  1783. _getClassName = function(el) {
  1784. return (typeof el.className.baseVal == "undefined") ? el.className : el.className.baseVal;
  1785. },
  1786. _classManip = function(el, add, clazz) {
  1787. // TODO if classList exists, use it.
  1788. var classesToAddOrRemove = jsPlumbUtil.isArray(clazz) ? clazz : clazz.split(/\s+/),
  1789. className = _getClassName(el),
  1790. curClasses = className.split(/\s+/);
  1791. for (var i = 0; i < classesToAddOrRemove.length; i++) {
  1792. if (add) {
  1793. if (jsPlumbUtil.indexOf(curClasses, classesToAddOrRemove[i]) == -1)
  1794. curClasses.push(classesToAddOrRemove[i]);
  1795. }
  1796. else {
  1797. var idx = jsPlumbUtil.indexOf(curClasses, classesToAddOrRemove[i]);
  1798. if (idx != -1)
  1799. curClasses.splice(idx, 1);
  1800. }
  1801. }
  1802. _setClassName(el, curClasses.join(" "));
  1803. },
  1804. _classManipNew = function(el, classesToAdd, classesToRemove) {
  1805. // TODO if classList exists, use it.
  1806. classesToAdd = classesToAdd == null ? [] : jsPlumbUtil.isArray(classesToAdd) ? classesToAdd : classesToAdd.split(/\s+/);
  1807. classesToRemove= classesToRemove== null ? [] : jsPlumbUtil.isArray(classesToRemove) ? classesToRemove: classesToRemove.split(/\s+/);
  1808. var className = _getClassName(el),
  1809. curClasses = className.split(/\s+/);
  1810. var _oneSet = function(add, classes) {
  1811. for (var i = 0; i < classes.length; i++) {
  1812. if (add) {
  1813. if (jsPlumbUtil.indexOf(curClasses, classes[i]) == -1)
  1814. curClasses.push(classes[i]);
  1815. }
  1816. else {
  1817. var idx = jsPlumbUtil.indexOf(curClasses, classes[i]);
  1818. if (idx != -1)
  1819. curClasses.splice(idx, 1);
  1820. }
  1821. }
  1822. };
  1823. _oneSet(true, classesToAdd);
  1824. _oneSet(false, classesToRemove);
  1825. _setClassName(el, curClasses.join(" "));
  1826. },
  1827. _each = function(spec, fn) {
  1828. if (spec == null) return;
  1829. if (typeof spec === "string")
  1830. fn(jsPlumb.getDOMElement(spec));
  1831. else if (spec.length != null) {
  1832. for (var i = 0; i < spec.length; i++)
  1833. fn(jsPlumb.getDOMElement(spec[i]));
  1834. }
  1835. else
  1836. fn(spec); // assume it's an element.
  1837. };
  1838. window.jsPlumbAdapter = {
  1839. headless:false,
  1840. pageLocation:_pageLocation,
  1841. screenLocation:_screenLocation,
  1842. clientLocation:_clientLocation,
  1843. getAttribute:function(el, attName) {
  1844. return el.getAttribute(attName);
  1845. },
  1846. setAttribute:function(el, a, v) {
  1847. el.setAttribute(a, v);
  1848. },
  1849. appendToRoot : function(node) {
  1850. document.body.appendChild(node);
  1851. },
  1852. getRenderModes : function() {
  1853. return [ "svg", "vml" ];
  1854. },
  1855. isRenderModeAvailable : function(m) {
  1856. return {
  1857. "svg":svgAvailable,
  1858. "vml":vmlAvailable()
  1859. }[m];
  1860. },
  1861. getDragManager : function(_jsPlumb) {
  1862. return new DragManager(_jsPlumb);
  1863. },
  1864. setRenderMode : function(mode) {
  1865. var renderMode;
  1866. if (mode) {
  1867. mode = mode.toLowerCase();
  1868. var svgAvailable = this.isRenderModeAvailable("svg"),
  1869. vmlAvailable = this.isRenderModeAvailable("vml");
  1870. // now test we actually have the capability to do this.
  1871. if (mode === "svg") {
  1872. if (svgAvailable) renderMode = "svg";
  1873. else if (vmlAvailable) renderMode = "vml";
  1874. }
  1875. else if (vmlAvailable) renderMode = "vml";
  1876. }
  1877. return renderMode;
  1878. },
  1879. addClass:function(el, clazz) {
  1880. _each(el, function(e) {
  1881. _classManipNew(e, clazz);
  1882. });
  1883. },
  1884. hasClass:function(el, clazz) {
  1885. el = jsPlumb.getDOMElement(el);
  1886. if (el.classList) return el.classList.contains(clazz);
  1887. else {
  1888. return _getClassName(el).indexOf(clazz) != -1;
  1889. }
  1890. },
  1891. removeClass:function(el, clazz) {
  1892. _each(el, function(e) {
  1893. _classManipNew(e, null, clazz);
  1894. });
  1895. },
  1896. updateClasses:function(el, toAdd, toRemove) {
  1897. _each(el, function(e) {
  1898. _classManipNew(e, toAdd, toRemove);
  1899. });
  1900. },
  1901. setClass:function(el, clazz) {
  1902. _each(el, function(e) {
  1903. _setClassName(e, clazz);
  1904. });
  1905. },
  1906. setPosition:function(el, p) {
  1907. el.style.left = p.left + "px";
  1908. el.style.top = p.top + "px";
  1909. },
  1910. getPosition:function(el) {
  1911. var _one = function(prop) {
  1912. var v = el.style[prop];
  1913. return v ? v.substring(0, v.length - 2) : 0;
  1914. };
  1915. return {
  1916. left:_one("left"),
  1917. top:_one("top")
  1918. };
  1919. },
  1920. getOffset:function(el, _instance, relativeToRoot) {
  1921. el = jsPlumb.getDOMElement(el);
  1922. var container = _instance.getContainer();
  1923. var l = el.offsetLeft, t = el.offsetTop, op = (relativeToRoot || (container != null && el.offsetParent != container)) ? el.offsetParent : null;
  1924. while (op != null) {
  1925. l += op.offsetLeft;
  1926. t += op.offsetTop;
  1927. op = relativeToRoot ? op.offsetParent :
  1928. op.offsetParent == container ? null : op.offsetParent;
  1929. }
  1930. return {
  1931. left:l, top:t
  1932. };
  1933. },
  1934. //
  1935. // return x+y proportion of the given element's size corresponding to the location of the given event.
  1936. //
  1937. getPositionOnElement:function(evt, el, zoom) {
  1938. var box = typeof el.getBoundingClientRect !== "undefined" ? el.getBoundingClientRect() : { left:0, top:0, width:0, height:0 },
  1939. body = document.body,
  1940. docElem = document.documentElement,
  1941. offPar = el.offsetParent,
  1942. scrollTop = window.pageYOffset || docElem.scrollTop || body.scrollTop,
  1943. scrollLeft = window.pageXOffset || docElem.scrollLeft || body.scrollLeft,
  1944. clientTop = docElem.clientTop || body.clientTop || 0,
  1945. clientLeft = docElem.clientLeft || body.clientLeft || 0,
  1946. pst = 0,//offPar ? offPar.scrollTop : 0,
  1947. psl = 0,//offPar ? offPar.scrollLeft : 0,
  1948. top = box.top + scrollTop - clientTop + (pst * zoom),
  1949. left = box.left + scrollLeft - clientLeft + (psl * zoom),
  1950. cl = jsPlumbAdapter.pageLocation(evt),
  1951. w = box.width || (el.offsetWidth * zoom),
  1952. h = box.height || (el.offsetHeight * zoom),
  1953. x = (cl[0] - left) / w,
  1954. y = (cl[1] - top) / h;
  1955. return [ x, y ];
  1956. },
  1957. /**
  1958. * Gets the absolute position of some element as read from the left/top properties in its style.
  1959. * @method getAbsolutePosition
  1960. * @param {Element} el The element to retrieve the absolute coordinates from. **Note** this is a DOM element, not a selector from the underlying library.
  1961. * @return [Float, Float] [left, top] pixel values.
  1962. */
  1963. getAbsolutePosition : function(el) {
  1964. var _one = function(s) {
  1965. var ss = el.style[s];
  1966. if (ss) return parseFloat(ss.substring(0, ss.length - 2));
  1967. };
  1968. return [ _one("left"), _one("top") ];
  1969. },
  1970. /**
  1971. * Sets the absolute position of some element by setting the left/top properties in its style.
  1972. * @method setAbsolutePosition
  1973. * @param {Element} el The element to set the absolute coordinates on. **Note** this is a DOM element, not a selector from the underlying library.
  1974. * @param {Float[]} xy x and y coordinates
  1975. * @param {Float[]} [animateFrom] Optional previous xy to animate from.
  1976. */
  1977. setAbsolutePosition : function(el, xy, animateFrom, animateOptions) {
  1978. if (animateFrom) {
  1979. root.jsPlumb.animate(el, {
  1980. left: "+=" + (xy[0] - animateFrom[0]),
  1981. top: "+=" + (xy[1] - animateFrom[1])
  1982. }, animateOptions);
  1983. }
  1984. else {
  1985. el.style.left = xy[0] + "px";
  1986. el.style.top = xy[1] + "px";
  1987. }
  1988. }
  1989. };
  1990. }).call(this);
  1991. /*
  1992. * jsPlumb
  1993. *
  1994. * Title:jsPlumb 1.7.2
  1995. *
  1996. * Provides a way to visually connect elements on an HTML page, using SVG or VML.
  1997. *
  1998. * This file contains the core code.
  1999. *
  2000. * Copyright (c) 2010 - 2014 Simon Porritt (simon@jsplumbtoolkit.com)
  2001. *
  2002. * http://jsplumbtoolkit.com
  2003. * http://github.com/sporritt/jsplumb
  2004. *
  2005. * Dual licensed under the MIT and GPL2 licenses.
  2006. */
  2007. ;(function() {
  2008. "use strict";
  2009. var _ju = jsPlumbUtil,
  2010. _getOffset = function(el, _instance, relativeToRoot) {
  2011. return jsPlumbAdapter.getOffset(el, _instance, relativeToRoot);
  2012. },
  2013. /**
  2014. * creates a timestamp, using milliseconds since 1970, but as a string.
  2015. */
  2016. _timestamp = function() { return "" + (new Date()).getTime(); },
  2017. // helper method to update the hover style whenever it, or paintStyle, changes.
  2018. // we use paintStyle as the foundation and merge hoverPaintStyle over the
  2019. // top.
  2020. _updateHoverStyle = function(component) {
  2021. if (component._jsPlumb.paintStyle && component._jsPlumb.hoverPaintStyle) {
  2022. var mergedHoverStyle = {};
  2023. jsPlumb.extend(mergedHoverStyle, component._jsPlumb.paintStyle);
  2024. jsPlumb.extend(mergedHoverStyle, component._jsPlumb.hoverPaintStyle);
  2025. delete component._jsPlumb.hoverPaintStyle;
  2026. // we want the fillStyle of paintStyle to override a gradient, if possible.
  2027. if (mergedHoverStyle.gradient && component._jsPlumb.paintStyle.fillStyle)
  2028. delete mergedHoverStyle.gradient;
  2029. component._jsPlumb.hoverPaintStyle = mergedHoverStyle;
  2030. }
  2031. },
  2032. //events = [ "click", "dblclick", "mouseenter", "mouseout", "mousemove", "mousedown", "mouseup", "contextmenu" ],
  2033. events = [ "click", "mouseover", "mouseout", "mousemove", "mousedown", "mouseup", "contextmenu", "touchmove" ],
  2034. eventFilters = { "mouseout":"mouseleave", "mouseexit":"mouseleave" },
  2035. _updateAttachedElements = function(component, state, timestamp, sourceElement) {
  2036. var affectedElements = component.getAttachedElements();
  2037. if (affectedElements) {
  2038. for (var i = 0, j = affectedElements.length; i < j; i++) {
  2039. if (!sourceElement || sourceElement != affectedElements[i])
  2040. affectedElements[i].setHover(state, true, timestamp); // tell the attached elements not to inform their own attached elements.
  2041. }
  2042. }
  2043. },
  2044. _splitType = function(t) { return t == null ? null : t.split(" "); },
  2045. _applyTypes = function(component, params, doNotRepaint) {
  2046. if (component.getDefaultType) {
  2047. var td = component.getTypeDescriptor();
  2048. var o = _ju.merge({}, component.getDefaultType());
  2049. for (var i = 0, j = component._jsPlumb.types.length; i < j; i++)
  2050. o = _ju.merge(o, component._jsPlumb.instance.getType(component._jsPlumb.types[i], td), [ "cssClass" ]);
  2051. if (params) {
  2052. o = _ju.populate(o, params);
  2053. }
  2054. component.applyType(o, doNotRepaint);
  2055. if (!doNotRepaint) component.repaint();
  2056. }
  2057. },
  2058. // ------------------------------ BEGIN jsPlumbUIComponent --------------------------------------------
  2059. jsPlumbUIComponent = window.jsPlumbUIComponent = function(params) {
  2060. jsPlumbUtil.EventGenerator.apply(this, arguments);
  2061. var self = this,
  2062. a = arguments,
  2063. idPrefix = self.idPrefix,
  2064. id = idPrefix + (new Date()).getTime();
  2065. this._jsPlumb = {
  2066. instance: params._jsPlumb,
  2067. parameters:params.parameters || {},
  2068. paintStyle:null,
  2069. hoverPaintStyle:null,
  2070. paintStyleInUse:null,
  2071. hover:false,
  2072. beforeDetach:params.beforeDetach,
  2073. beforeDrop:params.beforeDrop,
  2074. overlayPlacements : [],
  2075. hoverClass: params.hoverClass || params._jsPlumb.Defaults.HoverClass,
  2076. types:[]
  2077. };
  2078. this.getId = function() { return id; };
  2079. // all components can generate events
  2080. if (params.events) {
  2081. for (var i in params.events)
  2082. self.bind(i, params.events[i]);
  2083. }
  2084. // all components get this clone function.
  2085. // TODO issue 116 showed a problem with this - it seems 'a' that is in
  2086. // the clone function's scope is shared by all invocations of it, the classic
  2087. // JS closure problem. for now, jsPlumb does a version of this inline where
  2088. // it used to call clone. but it would be nice to find some time to look
  2089. // further at this.
  2090. this.clone = function() {
  2091. var o = {};//new Object();
  2092. this.constructor.apply(o, a);
  2093. return o;
  2094. }.bind(this);
  2095. // user can supply a beforeDetach callback, which will be executed before a detach
  2096. // is performed; returning false prevents the detach.
  2097. this.isDetachAllowed = function(connection) {
  2098. var r = true;
  2099. if (this._jsPlumb.beforeDetach) {
  2100. try {
  2101. r = this._jsPlumb.beforeDetach(connection);
  2102. }
  2103. catch (e) { _ju.log("jsPlumb: beforeDetach callback failed", e); }
  2104. }
  2105. return r;
  2106. };
  2107. // user can supply a beforeDrop callback, which will be executed before a dropped
  2108. // connection is confirmed. user can return false to reject connection.
  2109. this.isDropAllowed = function(sourceId, targetId, scope, connection, dropEndpoint, source, target) {
  2110. var r = this._jsPlumb.instance.checkCondition("beforeDrop", {
  2111. sourceId:sourceId,
  2112. targetId:targetId,
  2113. scope:scope,
  2114. connection:connection,
  2115. dropEndpoint:dropEndpoint,
  2116. source:source, target:target
  2117. });
  2118. if (this._jsPlumb.beforeDrop) {
  2119. try {
  2120. r = this._jsPlumb.beforeDrop({
  2121. sourceId:sourceId,
  2122. targetId:targetId,
  2123. scope:scope,
  2124. connection:connection,
  2125. dropEndpoint:dropEndpoint,
  2126. source:source, target:target
  2127. });
  2128. }
  2129. catch (e) { _ju.log("jsPlumb: beforeDrop callback failed", e); }
  2130. }
  2131. return r;
  2132. };
  2133. var boundListeners = [],
  2134. bindAListener = function(obj, type, fn) {
  2135. boundListeners.push([obj, type, fn]);
  2136. obj.bind(type, fn);
  2137. },
  2138. domListeners = [];
  2139. // sets the component associated with listener events. for instance, an overlay delegates
  2140. // its events back to a connector. but if the connector is swapped on the underlying connection,
  2141. // then this component must be changed. This is called by setConnector in the Connection class.
  2142. this.setListenerComponent = function(c) {
  2143. for (var i = 0; i < domListeners.length; i++)
  2144. domListeners[i][3] = c;
  2145. };
  2146. };
  2147. var _removeTypeCssHelper = function(component, typeIndex) {
  2148. var typeId = component._jsPlumb.types[typeIndex],
  2149. type = component._jsPlumb.instance.getType(typeId, component.getTypeDescriptor());
  2150. if (type != null) {
  2151. if (type.cssClass && component.canvas)
  2152. component._jsPlumb.instance.removeClass(component.canvas, type.cssClass);
  2153. }
  2154. };
  2155. jsPlumbUtil.extend(jsPlumbUIComponent, jsPlumbUtil.EventGenerator, {
  2156. getParameter : function(name) {
  2157. return this._jsPlumb.parameters[name];
  2158. },
  2159. setParameter : function(name, value) {
  2160. this._jsPlumb.parameters[name] = value;
  2161. },
  2162. getParameters : function() {
  2163. return this._jsPlumb.parameters;
  2164. },
  2165. setParameters : function(p) {
  2166. this._jsPlumb.parameters = p;
  2167. },
  2168. addClass : function(clazz) {
  2169. jsPlumbAdapter.addClass(this.canvas, clazz);
  2170. },
  2171. removeClass : function(clazz) {
  2172. jsPlumbAdapter.removeClass(this.canvas, clazz);
  2173. },
  2174. updateClasses : function(classesToAdd, classesToRemove) {
  2175. jsPlumbAdapter.updateClasses(this.canvas, classesToAdd, classesToRemove);
  2176. },
  2177. setType : function(typeId, params, doNotRepaint) {
  2178. this.clearTypes();
  2179. this._jsPlumb.types = _splitType(typeId) || [];
  2180. _applyTypes(this, params, doNotRepaint);
  2181. },
  2182. getType : function() {
  2183. return this._jsPlumb.types;
  2184. },
  2185. reapplyTypes : function(params, doNotRepaint) {
  2186. _applyTypes(this, params, doNotRepaint);
  2187. },
  2188. hasType : function(typeId) {
  2189. return jsPlumbUtil.indexOf(this._jsPlumb.types, typeId) != -1;
  2190. },
  2191. addType : function(typeId, params, doNotRepaint) {
  2192. var t = _splitType(typeId), _cont = false;
  2193. if (t != null) {
  2194. for (var i = 0, j = t.length; i < j; i++) {
  2195. if (!this.hasType(t[i])) {
  2196. this._jsPlumb.types.push(t[i]);
  2197. _cont = true;
  2198. }
  2199. }
  2200. if (_cont) _applyTypes(this, params, doNotRepaint);
  2201. }
  2202. },
  2203. removeType : function(typeId, doNotRepaint) {
  2204. var t = _splitType(typeId), _cont = false, _one = function(tt) {
  2205. var idx = _ju.indexOf(this._jsPlumb.types, tt);
  2206. if (idx != -1) {
  2207. // remove css class if necessary
  2208. _removeTypeCssHelper(this, idx);
  2209. this._jsPlumb.types.splice(idx, 1);
  2210. return true;
  2211. }
  2212. return false;
  2213. }.bind(this);
  2214. if (t != null) {
  2215. for (var i = 0,j = t.length; i < j; i++) {
  2216. _cont = _one(t[i]) || _cont;
  2217. }
  2218. if (_cont) _applyTypes(this, null, doNotRepaint);
  2219. }
  2220. },
  2221. clearTypes : function(doNotRepaint) {
  2222. var i = this._jsPlumb.types.length;
  2223. for (var j = 0; j < i; j++) {
  2224. _removeTypeCssHelper(this, 0);
  2225. this._jsPlumb.types.splice(0, 1);
  2226. }
  2227. _applyTypes(this, {}, doNotRepaint);
  2228. },
  2229. toggleType : function(typeId, params, doNotRepaint) {
  2230. var t = _splitType(typeId);
  2231. if (t != null) {
  2232. for (var i = 0, j = t.length; i < j; i++) {
  2233. var idx = jsPlumbUtil.indexOf(this._jsPlumb.types, t[i]);
  2234. if (idx != -1) {
  2235. _removeTypeCssHelper(this, idx);
  2236. this._jsPlumb.types.splice(idx, 1);
  2237. }
  2238. else
  2239. this._jsPlumb.types.push(t[i]);
  2240. }
  2241. _applyTypes(this, params, doNotRepaint);
  2242. }
  2243. },
  2244. applyType : function(t, doNotRepaint) {
  2245. this.setPaintStyle(t.paintStyle, doNotRepaint);
  2246. this.setHoverPaintStyle(t.hoverPaintStyle, doNotRepaint);
  2247. if (t.parameters){
  2248. for (var i in t.parameters)
  2249. this.setParameter(i, t.parameters[i]);
  2250. }
  2251. },
  2252. setPaintStyle : function(style, doNotRepaint) {
  2253. // this._jsPlumb.paintStyle = jsPlumb.extend({}, style);
  2254. // TODO figure out if we want components to clone paintStyle so as not to share it.
  2255. this._jsPlumb.paintStyle = style;
  2256. this._jsPlumb.paintStyleInUse = this._jsPlumb.paintStyle;
  2257. _updateHoverStyle(this);
  2258. if (!doNotRepaint) this.repaint();
  2259. },
  2260. getPaintStyle : function() {
  2261. return this._jsPlumb.paintStyle;
  2262. },
  2263. setHoverPaintStyle : function(style, doNotRepaint) {
  2264. //this._jsPlumb.hoverPaintStyle = jsPlumb.extend({}, style);
  2265. // TODO figure out if we want components to clone paintStyle so as not to share it.
  2266. this._jsPlumb.hoverPaintStyle = style;
  2267. _updateHoverStyle(this);
  2268. if (!doNotRepaint) this.repaint();
  2269. },
  2270. getHoverPaintStyle : function() {
  2271. return this._jsPlumb.hoverPaintStyle;
  2272. },
  2273. destroy:function() {
  2274. this.cleanupListeners(); // this is on EventGenerator
  2275. this.clone = null;
  2276. this._jsPlumb = null;
  2277. },
  2278. isHover : function() { return this._jsPlumb.hover; },
  2279. setHover : function(hover, ignoreAttachedElements, timestamp) {
  2280. // while dragging, we ignore these events. this keeps the UI from flashing and
  2281. // swishing and whatevering.
  2282. if (this._jsPlumb && !this._jsPlumb.instance.currentlyDragging && !this._jsPlumb.instance.isHoverSuspended()) {
  2283. this._jsPlumb.hover = hover;
  2284. if (this.canvas != null) {
  2285. if (this._jsPlumb.instance.hoverClass != null) {
  2286. var method = hover ? "addClass" : "removeClass";
  2287. this._jsPlumb.instance[method](this.canvas, this._jsPlumb.instance.hoverClass);
  2288. }
  2289. if (this._jsPlumb.hoverClass != null) {
  2290. this._jsPlumb.instance[method](this.canvas, this._jsPlumb.hoverClass);
  2291. }
  2292. }
  2293. if (this._jsPlumb.hoverPaintStyle != null) {
  2294. this._jsPlumb.paintStyleInUse = hover ? this._jsPlumb.hoverPaintStyle : this._jsPlumb.paintStyle;
  2295. if (!this._jsPlumb.instance.isSuspendDrawing()) {
  2296. timestamp = timestamp || _timestamp();
  2297. this.repaint({timestamp:timestamp, recalc:false});
  2298. }
  2299. }
  2300. // get the list of other affected elements, if supported by this component.
  2301. // for a connection, its the endpoints. for an endpoint, its the connections! surprise.
  2302. if (this.getAttachedElements && !ignoreAttachedElements)
  2303. _updateAttachedElements(this, hover, _timestamp(), this);
  2304. }
  2305. }
  2306. });
  2307. // ------------------------------ END jsPlumbUIComponent --------------------------------------------
  2308. // ------------------------------ BEGIN OverlayCapablejsPlumbUIComponent --------------------------------------------
  2309. var _internalLabelOverlayId = "__label",
  2310. // helper to get the index of some overlay
  2311. _getOverlayIndex = function(component, id) {
  2312. var idx = -1;
  2313. for (var i = 0, j = component._jsPlumb.overlays.length; i < j; i++) {
  2314. if (id === component._jsPlumb.overlays[i].id) {
  2315. idx = i;
  2316. break;
  2317. }
  2318. }
  2319. return idx;
  2320. },
  2321. // this is a shortcut helper method to let people add a label as
  2322. // overlay.
  2323. _makeLabelOverlay = function(component, params) {
  2324. var _params = {
  2325. cssClass:params.cssClass,
  2326. labelStyle : component.labelStyle,
  2327. id:_internalLabelOverlayId,
  2328. component:component,
  2329. _jsPlumb:component._jsPlumb.instance // TODO not necessary, since the instance can be accessed through the component.
  2330. },
  2331. mergedParams = jsPlumb.extend(_params, params);
  2332. return new jsPlumb.Overlays[component._jsPlumb.instance.getRenderMode()].Label( mergedParams );
  2333. },
  2334. _processOverlay = function(component, o) {
  2335. var _newOverlay = null;
  2336. if (_ju.isArray(o)) { // this is for the shorthand ["Arrow", { width:50 }] syntax
  2337. // there's also a three arg version:
  2338. // ["Arrow", { width:50 }, {location:0.7}]
  2339. // which merges the 3rd arg into the 2nd.
  2340. var type = o[0],
  2341. // make a copy of the object so as not to mess up anyone else's reference...
  2342. p = jsPlumb.extend({component:component, _jsPlumb:component._jsPlumb.instance}, o[1]);
  2343. if (o.length == 3) jsPlumb.extend(p, o[2]);
  2344. _newOverlay = new jsPlumb.Overlays[component._jsPlumb.instance.getRenderMode()][type](p);
  2345. } else if (o.constructor == String) {
  2346. _newOverlay = new jsPlumb.Overlays[component._jsPlumb.instance.getRenderMode()][o]({component:component, _jsPlumb:component._jsPlumb.instance});
  2347. } else {
  2348. _newOverlay = o;
  2349. }
  2350. component._jsPlumb.overlays.push(_newOverlay);
  2351. },
  2352. _calculateOverlaysToAdd = function(component, params) {
  2353. var defaultKeys = component.defaultOverlayKeys || [], o = params.overlays,
  2354. checkKey = function(k) {
  2355. return component._jsPlumb.instance.Defaults[k] || jsPlumb.Defaults[k] || [];
  2356. };
  2357. if (!o) o = [];
  2358. for (var i = 0, j = defaultKeys.length; i < j; i++)
  2359. o.unshift.apply(o, checkKey(defaultKeys[i]));
  2360. return o;
  2361. },
  2362. OverlayCapableJsPlumbUIComponent = window.OverlayCapableJsPlumbUIComponent = function(params) {
  2363. jsPlumbUIComponent.apply(this, arguments);
  2364. this._jsPlumb.overlays = [];
  2365. var _overlays = _calculateOverlaysToAdd(this, params);
  2366. if (_overlays) {
  2367. for (var i = 0, j = _overlays.length; i < j; i++) {
  2368. _processOverlay(this, _overlays[i]);
  2369. }
  2370. }
  2371. if (params.label) {
  2372. var loc = params.labelLocation || this.defaultLabelLocation || 0.5,
  2373. labelStyle = params.labelStyle || this._jsPlumb.instance.Defaults.LabelStyle;
  2374. this._jsPlumb.overlays.push(_makeLabelOverlay(this, {
  2375. label:params.label,
  2376. location:loc,
  2377. labelStyle:labelStyle
  2378. }));
  2379. }
  2380. this.setListenerComponent = function(c) {
  2381. if (this._jsPlumb) {
  2382. for (var i = 0; i < this._jsPlumb.overlays.length; i++)
  2383. this._jsPlumb.overlays[i].setListenerComponent(c);
  2384. }
  2385. };
  2386. };
  2387. jsPlumbUtil.extend(OverlayCapableJsPlumbUIComponent, jsPlumbUIComponent, {
  2388. applyType : function(t, doNotRepaint) {
  2389. this.removeAllOverlays(doNotRepaint);
  2390. if (t.overlays) {
  2391. for (var i = 0, j = t.overlays.length; i < j; i++)
  2392. this.addOverlay(t.overlays[i], true);
  2393. }
  2394. },
  2395. setHover : function(hover, ignoreAttachedElements) {
  2396. if (this._jsPlumb && !this._jsPlumb.instance.isConnectionBeingDragged()) {
  2397. for (var i = 0, j = this._jsPlumb.overlays.length; i < j; i++) {
  2398. this._jsPlumb.overlays[i][hover ? "addClass":"removeClass"](this._jsPlumb.instance.hoverClass);
  2399. }
  2400. }
  2401. },
  2402. addOverlay : function(overlay, doNotRepaint) {
  2403. _processOverlay(this, overlay);
  2404. if (!doNotRepaint) this.repaint();
  2405. },
  2406. getOverlay : function(id) {
  2407. var idx = _getOverlayIndex(this, id);
  2408. return idx >= 0 ? this._jsPlumb.overlays[idx] : null;
  2409. },
  2410. getOverlays : function() {
  2411. return this._jsPlumb.overlays;
  2412. },
  2413. hideOverlay : function(id) {
  2414. var o = this.getOverlay(id);
  2415. if (o) o.hide();
  2416. },
  2417. hideOverlays : function() {
  2418. for (var i = 0, j = this._jsPlumb.overlays.length; i < j; i++)
  2419. this._jsPlumb.overlays[i].hide();
  2420. },
  2421. showOverlay : function(id) {
  2422. var o = this.getOverlay(id);
  2423. if (o) o.show();
  2424. },
  2425. showOverlays : function() {
  2426. for (var i = 0, j = this._jsPlumb.overlays.length; i < j; i++)
  2427. this._jsPlumb.overlays[i].show();
  2428. },
  2429. removeAllOverlays : function(doNotRepaint) {
  2430. for (var i = 0, j = this._jsPlumb.overlays.length; i < j; i++) {
  2431. if (this._jsPlumb.overlays[i].cleanup) this._jsPlumb.overlays[i].cleanup();
  2432. }
  2433. this._jsPlumb.overlays.splice(0, this._jsPlumb.overlays.length);
  2434. this._jsPlumb.overlayPositions = null;
  2435. if (!doNotRepaint)
  2436. this.repaint();
  2437. },
  2438. removeOverlay : function(overlayId) {
  2439. var idx = _getOverlayIndex(this, overlayId);
  2440. if (idx != -1) {
  2441. var o = this._jsPlumb.overlays[idx];
  2442. if (o.cleanup) o.cleanup();
  2443. this._jsPlumb.overlays.splice(idx, 1);
  2444. if (this._jsPlumb.overlayPositions)
  2445. delete this._jsPlumb.overlayPositions[overlayId];
  2446. }
  2447. },
  2448. removeOverlays : function() {
  2449. for (var i = 0, j = arguments.length; i < j; i++)
  2450. this.removeOverlay(arguments[i]);
  2451. },
  2452. moveParent:function(newParent) {
  2453. if (this.bgCanvas) {
  2454. this.bgCanvas.parentNode.removeChild(this.bgCanvas);
  2455. newParent.appendChild(this.bgCanvas);
  2456. }
  2457. this.canvas.parentNode.removeChild(this.canvas);
  2458. newParent.appendChild(this.canvas);
  2459. for (var i = 0; i < this._jsPlumb.overlays.length; i++) {
  2460. if (this._jsPlumb.overlays[i].isAppendedAtTopLevel) {
  2461. this._jsPlumb.overlays[i].canvas.parentNode.removeChild(this._jsPlumb.overlays[i].canvas);
  2462. newParent.appendChild(this._jsPlumb.overlays[i].canvas);
  2463. }
  2464. }
  2465. },
  2466. getLabel : function() {
  2467. var lo = this.getOverlay(_internalLabelOverlayId);
  2468. return lo != null ? lo.getLabel() : null;
  2469. },
  2470. getLabelOverlay : function() {
  2471. return this.getOverlay(_internalLabelOverlayId);
  2472. },
  2473. setLabel : function(l) {
  2474. var lo = this.getOverlay(_internalLabelOverlayId);
  2475. if (!lo) {
  2476. var params = l.constructor == String || l.constructor == Function ? { label:l } : l;
  2477. lo = _makeLabelOverlay(this, params);
  2478. this._jsPlumb.overlays.push(lo);
  2479. }
  2480. else {
  2481. if (l.constructor == String || l.constructor == Function) lo.setLabel(l);
  2482. else {
  2483. if (l.label) lo.setLabel(l.label);
  2484. if (l.location) lo.setLocation(l.location);
  2485. }
  2486. }
  2487. if (!this._jsPlumb.instance.isSuspendDrawing())
  2488. this.repaint();
  2489. },
  2490. cleanup:function() {
  2491. for (var i = 0; i < this._jsPlumb.overlays.length; i++) {
  2492. this._jsPlumb.overlays[i].cleanup();
  2493. this._jsPlumb.overlays[i].destroy();
  2494. }
  2495. this._jsPlumb.overlays.length = 0;
  2496. this._jsPlumb.overlayPositions = null;
  2497. },
  2498. setVisible:function(v) {
  2499. this[v ? "showOverlays" : "hideOverlays"]();
  2500. },
  2501. setAbsoluteOverlayPosition:function(overlay, xy) {
  2502. this._jsPlumb.overlayPositions = this._jsPlumb.overlayPositions || {};
  2503. this._jsPlumb.overlayPositions[overlay.id] = xy;
  2504. },
  2505. getAbsoluteOverlayPosition:function(overlay) {
  2506. return this._jsPlumb.overlayPositions ? this._jsPlumb.overlayPositions[overlay.id] : null;
  2507. }
  2508. });
  2509. // ------------------------------ END OverlayCapablejsPlumbUIComponent --------------------------------------------
  2510. var _jsPlumbInstanceIndex = 0,
  2511. getInstanceIndex = function() {
  2512. var i = _jsPlumbInstanceIndex + 1;
  2513. _jsPlumbInstanceIndex++;
  2514. return i;
  2515. };
  2516. var jsPlumbInstance = window.jsPlumbInstance = function(_defaults) {
  2517. this.Defaults = {
  2518. Anchor : "Bottom",
  2519. Anchors : [ null, null ],
  2520. ConnectionsDetachable : true,
  2521. ConnectionOverlays : [ ],
  2522. Connector : "Bezier",
  2523. Container : null,
  2524. DoNotThrowErrors:false,
  2525. DragOptions : { },
  2526. DropOptions : { },
  2527. Endpoint : "Dot",
  2528. EndpointOverlays : [ ],
  2529. Endpoints : [ null, null ],
  2530. EndpointStyle : { fillStyle : "#456" },
  2531. EndpointStyles : [ null, null ],
  2532. EndpointHoverStyle : null,
  2533. EndpointHoverStyles : [ null, null ],
  2534. HoverPaintStyle : null,
  2535. LabelStyle : { color : "black" },
  2536. LogEnabled : false,
  2537. Overlays : [ ],
  2538. MaxConnections : 1,
  2539. PaintStyle : { lineWidth : 4, strokeStyle : "#456" },
  2540. ReattachConnections:false,
  2541. RenderMode : "svg",
  2542. Scope : "jsPlumb_DefaultScope"
  2543. };
  2544. if (_defaults) jsPlumb.extend(this.Defaults, _defaults);
  2545. this.logEnabled = this.Defaults.LogEnabled;
  2546. this._connectionTypes = {};
  2547. this._endpointTypes = {};
  2548. jsPlumbUtil.EventGenerator.apply(this);
  2549. var _currentInstance = this,
  2550. _instanceIndex = getInstanceIndex(),
  2551. _bb = _currentInstance.bind,
  2552. _initialDefaults = {},
  2553. _zoom = 1,
  2554. _info = function(el) {
  2555. var _el = _currentInstance.getDOMElement(el);
  2556. return { el:_el, id:(jsPlumbUtil.isString(el) && _el == null) ? el : _getId(_el) };
  2557. };
  2558. this.getInstanceIndex = function() { return _instanceIndex; };
  2559. this.setZoom = function(z, repaintEverything) {
  2560. if (!jsPlumbUtil.oldIE) {
  2561. _zoom = z;
  2562. _currentInstance.fire("zoom", _zoom);
  2563. if (repaintEverything) _currentInstance.repaintEverything();
  2564. }
  2565. return !jsPlumbUtil.oldIE;
  2566. };
  2567. this.getZoom = function() { return _zoom; };
  2568. for (var i in this.Defaults)
  2569. _initialDefaults[i] = this.Defaults[i];
  2570. var _container;
  2571. this.setContainer = function(c) {
  2572. // TODO if a _container already exists, unbind delegations from it
  2573. c = this.getDOMElement(c);
  2574. this.select().each(function(conn) {
  2575. conn.moveParent(c);
  2576. });
  2577. this.selectEndpoints().each(function(ep) {
  2578. ep.moveParent(c);
  2579. });
  2580. _container = c;
  2581. var _oneDelegateHandler = function(id, e) {
  2582. var t = e.srcElement || e.target,
  2583. jp = (t && t.parentNode ? t.parentNode._jsPlumb : null) || (t ? t._jsPlumb : null) || (t && t.parentNode && t.parentNode.parentNode ? t.parentNode.parentNode._jsPlumb : null);
  2584. if (jp) {
  2585. jp.fire(id, jp, e);
  2586. // jsplumb also fires every event coming from components
  2587. _currentInstance.fire(id, jp, e);
  2588. }
  2589. };
  2590. // delegate one event on the container to jsplumb elements. it might be possible to
  2591. // abstract this out: each of endpoint, connection and overlay could register themselves with
  2592. // jsplumb as "component types" or whatever, and provide a suitable selector. this would be
  2593. // done by the renderer (although admittedly from 2.0 onwards we're not supporting vml anymore)
  2594. var _oneDelegate = function(id) {
  2595. // connections.
  2596. _currentInstance.on(_container, id, "._jsPlumb_connector, ._jsPlumb_connector > *", function(e) { _oneDelegateHandler(id, e); });
  2597. // endpoints. note they can have an enclosing div, or not.
  2598. _currentInstance.on(_container, id, "._jsPlumb_endpoint, ._jsPlumb_endpoint > *, ._jsPlumb_endpoint svg *", function(e) { _oneDelegateHandler(id, e); });
  2599. // overlays
  2600. _currentInstance.on(_container, id, "._jsPlumb_overlay, ._jsPlumb_overlay *", function(e) { _oneDelegateHandler(id, e); });
  2601. };
  2602. for (var i = 0; i < events.length; i++)
  2603. _oneDelegate(events[i]);
  2604. };
  2605. this.getContainer = function() {
  2606. return _container;
  2607. };
  2608. this.bind = function(event, fn) {
  2609. if ("ready" === event && initialized) fn();
  2610. else _bb.apply(_currentInstance,[event, fn]);
  2611. };
  2612. _currentInstance.importDefaults = function(d) {
  2613. for (var i in d) {
  2614. _currentInstance.Defaults[i] = d[i];
  2615. }
  2616. if (d.Container)
  2617. _currentInstance.setContainer(d.Container);
  2618. return _currentInstance;
  2619. };
  2620. _currentInstance.restoreDefaults = function() {
  2621. _currentInstance.Defaults = jsPlumb.extend({}, _initialDefaults);
  2622. return _currentInstance;
  2623. };
  2624. //_currentInstance.floatingConnections = {};ctions = {};
  2625. var log = null,
  2626. initialized = false,
  2627. // TODO remove from window scope
  2628. connections = [],
  2629. // map of element id -> endpoint lists. an element can have an arbitrary
  2630. // number of endpoints on it, and not all of them have to be connected
  2631. // to anything.
  2632. endpointsByElement = {},
  2633. endpointsByUUID = {},
  2634. // SP new
  2635. managedElements = {},
  2636. offsets = {},
  2637. offsetTimestamps = {},
  2638. draggableStates = {},
  2639. connectionBeingDragged = false,
  2640. sizes = [],
  2641. _suspendDrawing = false,
  2642. _suspendedAt = null,
  2643. DEFAULT_SCOPE = this.Defaults.Scope,
  2644. renderMode = null, // will be set in init()
  2645. _curIdStamp = 1,
  2646. _idstamp = function() { return "" + _curIdStamp++; },
  2647. //
  2648. // appends an element to some other element, which is calculated as follows:
  2649. //
  2650. // 1. if Container exists, use that element.
  2651. // 2. if the 'parent' parameter exists, use that.
  2652. // 3. otherwise just use the root element (for DOM usage, the document body).
  2653. //
  2654. //
  2655. _appendElement = function(el, parent) {
  2656. if (_container)
  2657. _container.appendChild(el);
  2658. else if (!parent)
  2659. this.appendToRoot(el);
  2660. else
  2661. this.getDOMElement(parent).appendChild(el);
  2662. }.bind(this),
  2663. //
  2664. // Draws an endpoint and its connections. this is the main entry point into drawing connections as well
  2665. // as endpoints, since jsPlumb is endpoint-centric under the hood.
  2666. //
  2667. // @param element element to draw (of type library specific element object)
  2668. // @param ui UI object from current library's event system. optional.
  2669. // @param timestamp timestamp for this paint cycle. used to speed things up a little by cutting down the amount of offset calculations we do.
  2670. // @param clearEdits defaults to false; indicates that mouse edits for connectors should be cleared
  2671. ///
  2672. _draw = function(element, ui, timestamp, clearEdits) {
  2673. // TODO is it correct to filter by headless at this top level? how would a headless adapter ever repaint?
  2674. if (!jsPlumbAdapter.headless && !_suspendDrawing) {
  2675. var id = _getId(element),
  2676. repaintEls = _currentInstance.dragManager.getElementsForDraggable(id);
  2677. if (timestamp == null) timestamp = _timestamp();
  2678. // update the offset of everything _before_ we try to draw anything.
  2679. var o = _updateOffset( { elId : id, offset : ui, recalc : false, timestamp : timestamp });
  2680. if (repaintEls) {
  2681. for (var i in repaintEls) {
  2682. // TODO this seems to cause a lag, but we provide the offset, so in theory it
  2683. // should not. is the timestamp failing?
  2684. _updateOffset( {
  2685. elId : repaintEls[i].id,
  2686. offset : {
  2687. left:o.o.left + repaintEls[i].offset.left,
  2688. top:o.o.top + repaintEls[i].offset.top
  2689. },
  2690. recalc : false,
  2691. timestamp : timestamp
  2692. });
  2693. }
  2694. }
  2695. _currentInstance.anchorManager.redraw(id, ui, timestamp, null, clearEdits);
  2696. if (repaintEls) {
  2697. for (var j in repaintEls) {
  2698. _currentInstance.anchorManager.redraw(repaintEls[j].id, ui, timestamp, repaintEls[j].offset, clearEdits, true);
  2699. }
  2700. }
  2701. }
  2702. },
  2703. //
  2704. // executes the given function against the given element if the first
  2705. // argument is an object, or the list of elements, if the first argument
  2706. // is a list. the function passed in takes (element, elementId) as
  2707. // arguments.
  2708. //
  2709. _elementProxy = function(element, fn) {
  2710. var retVal = null, el, id, del;
  2711. if (_ju.isArray(element)) {
  2712. retVal = [];
  2713. for ( var i = 0, j = element.length; i < j; i++) {
  2714. el = _currentInstance.getElementObject(element[i]);
  2715. del = _currentInstance.getDOMElement(el);
  2716. id = _currentInstance.getAttribute(del, "id");
  2717. retVal.push(fn.apply(_currentInstance, [del, id])); // append return values to what we will return
  2718. }
  2719. } else {
  2720. el = _currentInstance.getDOMElement(element);
  2721. id = _currentInstance.getId(el);
  2722. retVal = fn.apply(_currentInstance, [el, id]);
  2723. }
  2724. return retVal;
  2725. },
  2726. //
  2727. // gets an Endpoint by uuid.
  2728. //
  2729. _getEndpoint = function(uuid) { return endpointsByUUID[uuid]; },
  2730. /**
  2731. * inits a draggable if it's not already initialised.
  2732. * TODO: somehow abstract this to the adapter, because the concept of "draggable" has no
  2733. * place on the server.
  2734. */
  2735. _initDraggableIfNecessary = function(element, isDraggable, dragOptions, id) {
  2736. // TODO move to DragManager?
  2737. if (!jsPlumbAdapter.headless) {
  2738. var _draggable = isDraggable == null ? false : isDraggable;
  2739. if (_draggable) {
  2740. if (jsPlumb.isDragSupported(element, _currentInstance) && !jsPlumb.isAlreadyDraggable(element, _currentInstance)) {
  2741. var options = dragOptions || _currentInstance.Defaults.DragOptions;
  2742. options = jsPlumb.extend( {}, options); // make a copy.
  2743. var dragEvent = jsPlumb.dragEvents.drag,
  2744. stopEvent = jsPlumb.dragEvents.stop,
  2745. startEvent = jsPlumb.dragEvents.start,
  2746. _del = _currentInstance.getDOMElement(element),
  2747. _ancestor = _currentInstance.dragManager.getDragAncestor(_del),
  2748. _noOffset = {left:0, top:0},
  2749. _ancestorOffset = _noOffset,
  2750. _started = false;
  2751. _manage(id, element);
  2752. options[startEvent] = _ju.wrap(options[startEvent], function() {
  2753. _ancestorOffset = _ancestor != null ? jsPlumbAdapter.getOffset(_ancestor, _currentInstance) : _noOffset;
  2754. _currentInstance.setHoverSuspended(true);
  2755. _currentInstance.select({source:element}).addClass(_currentInstance.elementDraggingClass + " " + _currentInstance.sourceElementDraggingClass, true);
  2756. _currentInstance.select({target:element}).addClass(_currentInstance.elementDraggingClass + " " + _currentInstance.targetElementDraggingClass, true);
  2757. _currentInstance.setConnectionBeingDragged(true);
  2758. if (options.canDrag) return dragOptions.canDrag();
  2759. }, false);
  2760. options[dragEvent] = _ju.wrap(options[dragEvent], function() {
  2761. // TODO: here we could actually use getDragObject, and then compute it ourselves,
  2762. // since every adapter does the same thing. but i'm not sure why YUI's getDragObject
  2763. // differs from getUIPosition so much
  2764. var ui = _currentInstance.getUIPosition(arguments, _currentInstance.getZoom());
  2765. // adjust by ancestor offset if there is one: this is for the case that a draggable
  2766. // is contained inside some other element that is not the Container.
  2767. ui.left += _ancestorOffset.left;
  2768. ui.top += _ancestorOffset.top;
  2769. _draw(element, ui, null, true);
  2770. if (_started) _currentInstance.addClass(element, "jsPlumb_dragged");
  2771. _started = true;
  2772. });
  2773. options[stopEvent] = _ju.wrap(options[stopEvent], function() {
  2774. var ui = _currentInstance.getUIPosition(arguments, _currentInstance.getZoom(), true);
  2775. _draw(element, ui);
  2776. _started = false;
  2777. _currentInstance.removeClass(element, "jsPlumb_dragged");
  2778. _currentInstance.setHoverSuspended(false);
  2779. _currentInstance.select({source:element}).removeClass(_currentInstance.elementDraggingClass + " " + _currentInstance.sourceElementDraggingClass, true);
  2780. _currentInstance.select({target:element}).removeClass(_currentInstance.elementDraggingClass + " " + _currentInstance.targetElementDraggingClass, true);
  2781. _currentInstance.setConnectionBeingDragged(false);
  2782. _currentInstance.dragManager.dragEnded(element);
  2783. });
  2784. var elId = _getId(element); // need ID
  2785. draggableStates[elId] = true;
  2786. var draggable = draggableStates[elId];
  2787. options.disabled = draggable == null ? false : !draggable;
  2788. _currentInstance.initDraggable(element, options);
  2789. _currentInstance.dragManager.register(element);
  2790. }
  2791. }
  2792. }
  2793. },
  2794. _scopeMatch = function(e1, e2) {
  2795. var s1 = e1.scope.split(/\s/), s2 = e2.scope.split(/\s/);
  2796. for (var i = 0; i < s1.length; i++)
  2797. for (var j = 0; j < s2.length; j++)
  2798. if (s2[j] == s1[i]) return true;
  2799. return false;
  2800. },
  2801. /*
  2802. * prepares a final params object that can be passed to _newConnection, taking into account defaults, events, etc.
  2803. */
  2804. _prepareConnectionParams = function(params, referenceParams) {
  2805. var _p = jsPlumb.extend( { }, params);
  2806. if (referenceParams) jsPlumb.extend(_p, referenceParams);
  2807. // hotwire endpoints passed as source or target to sourceEndpoint/targetEndpoint, respectively.
  2808. if (_p.source) {
  2809. if (_p.source.endpoint)
  2810. _p.sourceEndpoint = _p.source;
  2811. else
  2812. _p.source = _currentInstance.getDOMElement(_p.source);
  2813. }
  2814. if (_p.target) {
  2815. if (_p.target.endpoint)
  2816. _p.targetEndpoint = _p.target;
  2817. else
  2818. _p.target = _currentInstance.getDOMElement(_p.target);
  2819. }
  2820. // test for endpoint uuids to connect
  2821. if (params.uuids) {
  2822. _p.sourceEndpoint = _getEndpoint(params.uuids[0]);
  2823. _p.targetEndpoint = _getEndpoint(params.uuids[1]);
  2824. }
  2825. // now ensure that if we do have Endpoints already, they're not full.
  2826. // source:
  2827. if (_p.sourceEndpoint && _p.sourceEndpoint.isFull()) {
  2828. _ju.log(_currentInstance, "could not add connection; source endpoint is full");
  2829. return;
  2830. }
  2831. // target:
  2832. if (_p.targetEndpoint && _p.targetEndpoint.isFull()) {
  2833. _ju.log(_currentInstance, "could not add connection; target endpoint is full");
  2834. return;
  2835. }
  2836. // if source endpoint mandates connection type and nothing specified in our params, use it.
  2837. if (!_p.type && _p.sourceEndpoint)
  2838. _p.type = _p.sourceEndpoint.connectionType;
  2839. // copy in any connectorOverlays that were specified on the source endpoint.
  2840. // it doesnt copy target endpoint overlays. i'm not sure if we want it to or not.
  2841. if (_p.sourceEndpoint && _p.sourceEndpoint.connectorOverlays) {
  2842. _p.overlays = _p.overlays || [];
  2843. for (var i = 0, j = _p.sourceEndpoint.connectorOverlays.length; i < j; i++) {
  2844. _p.overlays.push(_p.sourceEndpoint.connectorOverlays[i]);
  2845. }
  2846. }
  2847. // pointer events
  2848. if (!_p["pointer-events"] && _p.sourceEndpoint && _p.sourceEndpoint.connectorPointerEvents)
  2849. _p["pointer-events"] = _p.sourceEndpoint.connectorPointerEvents;
  2850. var _mergeOverrides = function(def, values) {
  2851. var m = jsPlumb.extend({}, def);
  2852. for (var i in values) {
  2853. if (values[i]) m[i] = values[i];
  2854. }
  2855. return m;
  2856. };
  2857. var _addEndpoint = function(el, def, idx) {
  2858. return _currentInstance.addEndpoint(el, _mergeOverrides(def, {
  2859. anchor:_p.anchors ? _p.anchors[idx] : _p.anchor,
  2860. endpoint:_p.endpoints ? _p.endpoints[idx] : _p.endpoint,
  2861. paintStyle:_p.endpointStyles ? _p.endpointStyles[idx] : _p.endpointStyle,
  2862. hoverPaintStyle:_p.endpointHoverStyles ? _p.endpointHoverStyles[idx] : _p.endpointHoverStyle
  2863. }));
  2864. };
  2865. // check for makeSource/makeTarget specs.
  2866. var _oneElementDef = function(type, idx, defs) {
  2867. if (_p[type] && !_p[type].endpoint && !_p[type + "Endpoint"] && !_p.newConnection) {
  2868. var tid = _getId(_p[type]), tep = defs[tid];
  2869. if (tep) {
  2870. // if not enabled, return.
  2871. if (!tep.enabled) return false;
  2872. var newEndpoint = tep.endpoint != null && tep.endpoint._jsPlumb ? tep.endpoint : _addEndpoint(_p[type], tep.def, idx);
  2873. if (tep.uniqueEndpoint) tep.endpoint = newEndpoint;
  2874. if (newEndpoint.isFull()) return false;
  2875. _p[type + "Endpoint"] = newEndpoint;
  2876. newEndpoint._doNotDeleteOnDetach = false; // reset.
  2877. newEndpoint._deleteOnDetach = true;
  2878. }
  2879. }
  2880. };
  2881. if (_oneElementDef("source", 0, this.sourceEndpointDefinitions) === false) return;
  2882. if (_oneElementDef("target", 1, this.targetEndpointDefinitions) === false) return;
  2883. // last, ensure scopes match
  2884. if (_p.sourceEndpoint && _p.targetEndpoint)
  2885. if (!_scopeMatch(_p.sourceEndpoint, _p.targetEndpoint)) _p = null;
  2886. return _p;
  2887. }.bind(_currentInstance),
  2888. _newConnection = function(params) {
  2889. var connectionFunc = _currentInstance.Defaults.ConnectionType || _currentInstance.getDefaultConnectionType();
  2890. params._jsPlumb = _currentInstance;
  2891. params.newConnection = _newConnection;
  2892. params.newEndpoint = _newEndpoint;
  2893. params.endpointsByUUID = endpointsByUUID;
  2894. params.endpointsByElement = endpointsByElement;
  2895. params.finaliseConnection = _finaliseConnection;
  2896. var con = new connectionFunc(params);
  2897. con.id = "con_" + _idstamp();
  2898. // if the connection is draggable, then maybe we need to tell the target endpoint to init the
  2899. // dragging code. it won't run again if it already configured to be draggable.
  2900. if (con.isDetachable()) {
  2901. con.endpoints[0].initDraggable();
  2902. con.endpoints[1].initDraggable();
  2903. }
  2904. return con;
  2905. },
  2906. //
  2907. // adds the connection to the backing model, fires an event if necessary and then redraws
  2908. //
  2909. _finaliseConnection = _currentInstance.finaliseConnection = function(jpc, params, originalEvent, doInformAnchorManager) {
  2910. params = params || {};
  2911. // add to list of connections (by scope).
  2912. if (!jpc.suspendedEndpoint)
  2913. connections.push(jpc);
  2914. // turn off isTemporarySource on the source endpoint (only viable on first draw)
  2915. jpc.endpoints[0].isTemporarySource = false;
  2916. // always inform the anchor manager
  2917. // except that if jpc has a suspended endpoint it's not true to say the
  2918. // connection is new; it has just (possibly) moved. the question is whether
  2919. // to make that call here or in the anchor manager. i think perhaps here.
  2920. if (jpc.suspendedEndpoint == null || doInformAnchorManager)
  2921. _currentInstance.anchorManager.newConnection(jpc);
  2922. // force a paint
  2923. _draw(jpc.source);
  2924. // fire an event
  2925. if (!params.doNotFireConnectionEvent && params.fireEvent !== false) {
  2926. var eventArgs = {
  2927. connection:jpc,
  2928. source : jpc.source, target : jpc.target,
  2929. sourceId : jpc.sourceId, targetId : jpc.targetId,
  2930. sourceEndpoint : jpc.endpoints[0], targetEndpoint : jpc.endpoints[1]
  2931. };
  2932. _currentInstance.fire("connection", eventArgs, originalEvent);
  2933. }
  2934. },
  2935. /*
  2936. factory method to prepare a new endpoint. this should always be used instead of creating Endpoints
  2937. manually, since this method attaches event listeners and an id.
  2938. */
  2939. _newEndpoint = function(params, id) {
  2940. var endpointFunc = _currentInstance.Defaults.EndpointType || jsPlumb.Endpoint;
  2941. var _p = jsPlumb.extend({}, params);
  2942. _p._jsPlumb = _currentInstance;
  2943. _p.newConnection = _newConnection;
  2944. _p.newEndpoint = _newEndpoint;
  2945. _p.endpointsByUUID = endpointsByUUID;
  2946. _p.endpointsByElement = endpointsByElement;
  2947. _p.fireDetachEvent = fireDetachEvent;
  2948. _p.elementId = id || _getId(_p.source);
  2949. var ep = new endpointFunc(_p);
  2950. ep.id = "ep_" + _idstamp();
  2951. _manage(_p.elementId, _p.source);
  2952. if (!jsPlumbAdapter.headless)
  2953. _currentInstance.dragManager.endpointAdded(_p.source, id);
  2954. return ep;
  2955. },
  2956. /*
  2957. * performs the given function operation on all the connections found
  2958. * for the given element id; this means we find all the endpoints for
  2959. * the given element, and then for each endpoint find the connectors
  2960. * connected to it. then we pass each connection in to the given
  2961. * function.
  2962. */
  2963. _operation = function(elId, func, endpointFunc) {
  2964. var endpoints = endpointsByElement[elId];
  2965. if (endpoints && endpoints.length) {
  2966. for ( var i = 0, ii = endpoints.length; i < ii; i++) {
  2967. for ( var j = 0, jj = endpoints[i].connections.length; j < jj; j++) {
  2968. var retVal = func(endpoints[i].connections[j]);
  2969. // if the function passed in returns true, we exit.
  2970. // most functions return false.
  2971. if (retVal) return;
  2972. }
  2973. if (endpointFunc) endpointFunc(endpoints[i]);
  2974. }
  2975. }
  2976. },
  2977. _setDraggable = function(element, draggable) {
  2978. return _elementProxy(element, function(el, id) {
  2979. draggableStates[id] = draggable;
  2980. if (this.isDragSupported(el)) {
  2981. this.setElementDraggable(el, draggable);
  2982. }
  2983. });
  2984. },
  2985. /*
  2986. * private method to do the business of hiding/showing.
  2987. *
  2988. * @param el
  2989. * either Id of the element in question or a library specific
  2990. * object for the element.
  2991. * @param state
  2992. * String specifying a value for the css 'display' property
  2993. * ('block' or 'none').
  2994. */
  2995. _setVisible = function(el, state, alsoChangeEndpoints) {
  2996. state = state === "block";
  2997. var endpointFunc = null;
  2998. if (alsoChangeEndpoints) {
  2999. if (state) endpointFunc = function(ep) {
  3000. ep.setVisible(true, true, true);
  3001. };
  3002. else endpointFunc = function(ep) {
  3003. ep.setVisible(false, true, true);
  3004. };
  3005. }
  3006. var info = _info(el);
  3007. _operation(info.id, function(jpc) {
  3008. if (state && alsoChangeEndpoints) {
  3009. // this test is necessary because this functionality is new, and i wanted to maintain backwards compatibility.
  3010. // this block will only set a connection to be visible if the other endpoint in the connection is also visible.
  3011. var oidx = jpc.sourceId === info.id ? 1 : 0;
  3012. if (jpc.endpoints[oidx].isVisible()) jpc.setVisible(true);
  3013. }
  3014. else // the default behaviour for show, and what always happens for hide, is to just set the visibility without getting clever.
  3015. jpc.setVisible(state);
  3016. }, endpointFunc);
  3017. },
  3018. /*
  3019. * toggles the draggable state of the given element(s).
  3020. * el is either an id, or an element object, or a list of ids/element objects.
  3021. */
  3022. _toggleDraggable = function(el) {
  3023. return _elementProxy(el, function(el, elId) {
  3024. var state = draggableStates[elId] == null ? false : draggableStates[elId];
  3025. state = !state;
  3026. draggableStates[elId] = state;
  3027. this.setDraggable(el, state);
  3028. return state;
  3029. }.bind(this));
  3030. },
  3031. /**
  3032. * private method to do the business of toggling hiding/showing.
  3033. */
  3034. _toggleVisible = function(elId, changeEndpoints) {
  3035. var endpointFunc = null;
  3036. if (changeEndpoints) {
  3037. endpointFunc = function(ep) {
  3038. var state = ep.isVisible();
  3039. ep.setVisible(!state);
  3040. };
  3041. }
  3042. _operation(elId, function(jpc) {
  3043. var state = jpc.isVisible();
  3044. jpc.setVisible(!state);
  3045. }, endpointFunc);
  3046. // todo this should call _elementProxy, and pass in the
  3047. // _operation(elId, f) call as a function. cos _toggleDraggable does
  3048. // that.
  3049. },
  3050. // TODO comparison performance
  3051. _getCachedData = function(elId) {
  3052. var o = offsets[elId];
  3053. if (!o)
  3054. return _updateOffset({elId:elId});
  3055. else
  3056. return {o:o, s:sizes[elId]};
  3057. },
  3058. /**
  3059. * gets an id for the given element, creating and setting one if
  3060. * necessary. the id is of the form
  3061. *
  3062. * jsPlumb_<instance index>_<index in instance>
  3063. *
  3064. * where "index in instance" is a monotonically increasing integer that starts at 0,
  3065. * for each instance. this method is used not only to assign ids to elements that do not
  3066. * have them but also to connections and endpoints.
  3067. */
  3068. _getId = function(element, uuid, doNotCreateIfNotFound) {
  3069. if (jsPlumbUtil.isString(element)) return element;
  3070. if (element == null) return null;
  3071. var id = _currentInstance.getAttribute(element, "id");
  3072. if (!id || id === "undefined") {
  3073. // check if fixed uuid parameter is given
  3074. if (arguments.length == 2 && arguments[1] !== undefined)
  3075. id = uuid;
  3076. else if (arguments.length == 1 || (arguments.length == 3 && !arguments[2]))
  3077. id = "jsPlumb_" + _instanceIndex + "_" + _idstamp();
  3078. if (!doNotCreateIfNotFound) _currentInstance.setAttribute(element, "id", id);
  3079. }
  3080. return id;
  3081. };
  3082. this.setConnectionBeingDragged = function(v) {
  3083. connectionBeingDragged = v;
  3084. };
  3085. this.isConnectionBeingDragged = function() {
  3086. return connectionBeingDragged;
  3087. };
  3088. this.connectorClass = "_jsPlumb_connector";
  3089. this.hoverClass = "_jsPlumb_hover";
  3090. this.endpointClass = "_jsPlumb_endpoint";
  3091. this.endpointConnectedClass = "_jsPlumb_endpoint_connected";
  3092. this.endpointFullClass = "_jsPlumb_endpoint_full";
  3093. this.endpointDropAllowedClass = "_jsPlumb_endpoint_drop_allowed";
  3094. this.endpointDropForbiddenClass = "_jsPlumb_endpoint_drop_forbidden";
  3095. this.overlayClass = "_jsPlumb_overlay";
  3096. this.draggingClass = "_jsPlumb_dragging";
  3097. this.elementDraggingClass = "_jsPlumb_element_dragging";
  3098. this.sourceElementDraggingClass = "_jsPlumb_source_element_dragging";
  3099. this.targetElementDraggingClass = "_jsPlumb_target_element_dragging";
  3100. this.endpointAnchorClassPrefix = "_jsPlumb_endpoint_anchor";
  3101. this.hoverSourceClass = "_jsPlumb_source_hover";
  3102. this.hoverTargetClass = "_jsPlumb_target_hover";
  3103. this.dragSelectClass = "_jsPlumb_drag_select";
  3104. this.Anchors = {};
  3105. this.Connectors = { "svg":{}, "vml":{} };
  3106. this.Endpoints = { "svg":{}, "vml":{} };
  3107. this.Overlays = { "svg":{}, "vml":{}};
  3108. this.ConnectorRenderers = {};
  3109. this.SVG = "svg";
  3110. this.VML = "vml";
  3111. // --------------------------- jsPlumbInstance public API ---------------------------------------------------------
  3112. this.addEndpoint = function(el, params, referenceParams) {
  3113. referenceParams = referenceParams || {};
  3114. var p = jsPlumb.extend({}, referenceParams);
  3115. jsPlumb.extend(p, params);
  3116. p.endpoint = p.endpoint || _currentInstance.Defaults.Endpoint;
  3117. p.paintStyle = p.paintStyle || _currentInstance.Defaults.EndpointStyle;
  3118. var results = [],
  3119. inputs = (_ju.isArray(el) || (el.length != null && !_ju.isString(el))) ? el : [ el ];
  3120. for (var i = 0, j = inputs.length; i < j; i++) {
  3121. p.source = _currentInstance.getDOMElement(inputs[i]);
  3122. _ensureContainer(p.source);
  3123. var id = _getId(p.source), e = _newEndpoint(p, id);
  3124. // SP new. here we have introduced a class-wide element manager, which is responsible
  3125. // for getting object dimensions and width/height, and for updating these values only
  3126. // when necessary (after a drag, or on a forced refresh call).
  3127. var myOffset = _manage(id, p.source).info.o;
  3128. _ju.addToList(endpointsByElement, id, e);
  3129. if (!_suspendDrawing) {
  3130. e.paint({
  3131. anchorLoc: e.anchor.compute({ xy: [ myOffset.left, myOffset.top ], wh: sizes[id], element: e, timestamp: _suspendedAt }),
  3132. timestamp: _suspendedAt
  3133. });
  3134. }
  3135. results.push(e);
  3136. e._doNotDeleteOnDetach = true; // mark this as being added via addEndpoint.
  3137. }
  3138. return results.length == 1 ? results[0] : results;
  3139. };
  3140. this.addEndpoints = function(el, endpoints, referenceParams) {
  3141. var results = [];
  3142. for ( var i = 0, j = endpoints.length; i < j; i++) {
  3143. var e = _currentInstance.addEndpoint(el, endpoints[i], referenceParams);
  3144. if (_ju.isArray(e))
  3145. Array.prototype.push.apply(results, e);
  3146. else results.push(e);
  3147. }
  3148. return results;
  3149. };
  3150. this.animate = function(el, properties, options) {
  3151. options = options || {};
  3152. var ele = _currentInstance.getElementObject(el),
  3153. del = _currentInstance.getDOMElement(el),
  3154. id = _getId(del),
  3155. stepFunction = jsPlumb.animEvents.step,
  3156. completeFunction = jsPlumb.animEvents.complete;
  3157. options[stepFunction] = _ju.wrap(options[stepFunction], function() {
  3158. _currentInstance.revalidate(id);
  3159. });
  3160. // onComplete repaints, just to make sure everything looks good at the end of the animation.
  3161. options[completeFunction] = _ju.wrap(options[completeFunction], function() {
  3162. _currentInstance.revalidate(id);
  3163. });
  3164. _currentInstance.doAnimate(ele, properties, options);
  3165. };
  3166. /**
  3167. * checks for a listener for the given condition, executing it if found, passing in the given value.
  3168. * condition listeners would have been attached using "bind" (which is, you could argue, now overloaded, since
  3169. * firing click events etc is a bit different to what this does). i thought about adding a "bindCondition"
  3170. * or something, but decided against it, for the sake of simplicity. jsPlumb will never fire one of these
  3171. * condition events anyway.
  3172. */
  3173. this.checkCondition = function(conditionName, value) {
  3174. var l = _currentInstance.getListener(conditionName),
  3175. r = true;
  3176. if (l && l.length > 0) {
  3177. try {
  3178. for (var i = 0, j = l.length; i < j; i++) {
  3179. r = r && l[i](value);
  3180. }
  3181. }
  3182. catch (e) {
  3183. _ju.log(_currentInstance, "cannot check condition [" + conditionName + "]" + e);
  3184. }
  3185. }
  3186. return r;
  3187. };
  3188. this.connect = function(params, referenceParams) {
  3189. // prepare a final set of parameters to create connection with
  3190. var _p = _prepareConnectionParams(params, referenceParams), jpc;
  3191. // TODO probably a nicer return value if the connection was not made. _prepareConnectionParams
  3192. // will return null (and log something) if either endpoint was full. what would be nicer is to
  3193. // create a dedicated 'error' object.
  3194. if (_p) {
  3195. if (_p.source == null && _p.sourceEndpoint == null) {
  3196. jsPlumbUtil.log("Cannot establish connection - source does not exist");
  3197. return;
  3198. }
  3199. if (_p.target == null && _p.targetEndpoint == null) {
  3200. jsPlumbUtil.log("Cannot establish connection - target does not exist");
  3201. return;
  3202. }
  3203. _ensureContainer(_p.source);
  3204. // create the connection. it is not yet registered
  3205. jpc = _newConnection(_p);
  3206. // now add it the model, fire an event, and redraw
  3207. _finaliseConnection(jpc, _p);
  3208. }
  3209. return jpc;
  3210. };
  3211. var stTypes = [
  3212. { el:"source", elId:"sourceId", epDefs:"sourceEndpointDefinitions" },
  3213. { el:"target", elId:"targetId", epDefs:"targetEndpointDefinitions" }
  3214. ];
  3215. var _set = function(c, el, idx, doNotRepaint) {
  3216. var ep, _st = stTypes[idx], cId = c[_st.elId], cEl = c[_st.el], sid, sep,
  3217. oldEndpoint = c.endpoints[idx];
  3218. var evtParams = {
  3219. index:idx,
  3220. originalSourceId:idx === 0 ? cId : c.sourceId,
  3221. newSourceId:c.sourceId,
  3222. originalTargetId:idx == 1 ? cId : c.targetId,
  3223. newTargetId:c.targetId,
  3224. connection:c
  3225. };
  3226. if (el.constructor == jsPlumb.Endpoint) { // TODO here match the current endpoint class; users can change it {
  3227. ep = el;
  3228. ep.addConnection(c);
  3229. }
  3230. else {
  3231. sid = _getId(el);
  3232. sep = this[_st.epDefs][sid];
  3233. if (sid === c[_st.elId])
  3234. ep = null; // dont change source/target if the element is already the one given.
  3235. else if (sep) {
  3236. if (!sep.enabled) return;
  3237. ep = sep.endpoint != null && sep.endpoint._jsPlumb ? sep.endpoint : this.addEndpoint(el, sep.def);
  3238. if (sep.uniqueEndpoint) sep.endpoint = ep;
  3239. ep._doNotDeleteOnDetach = false;
  3240. ep._deleteOnDetach = true;
  3241. ep.addConnection(c);
  3242. }
  3243. else {
  3244. ep = c.makeEndpoint(idx === 0, el, sid);
  3245. ep._doNotDeleteOnDetach = false;
  3246. ep._deleteOnDetach = true;
  3247. }
  3248. }
  3249. if (ep != null) {
  3250. oldEndpoint.detachFromConnection(c);
  3251. c.endpoints[idx] = ep;
  3252. c[_st.el] = ep.element;
  3253. c[_st.elId] = ep.elementId;
  3254. evtParams[idx === 0 ? "newSourceId" : "newTargetId"] = ep.elementId;
  3255. fireMoveEvent(evtParams);
  3256. if (!doNotRepaint)
  3257. c.repaint();
  3258. }
  3259. return evtParams;
  3260. }.bind(this);
  3261. this.setSource = function(connection, el, doNotRepaint) {
  3262. var p = _set(connection, el, 0, doNotRepaint);
  3263. this.anchorManager.sourceChanged(p.originalSourceId, p.newSourceId, connection);
  3264. };
  3265. this.setTarget = function(connection, el, doNotRepaint) {
  3266. var p = _set(connection, el, 1, doNotRepaint);
  3267. this.anchorManager.updateOtherEndpoint(p.originalSourceId, p.originalTargetId, p.newTargetId, connection);
  3268. };
  3269. this.deleteEndpoint = function(object, dontUpdateHover) {
  3270. var endpoint = (typeof object === "string") ? endpointsByUUID[object] : object;
  3271. if (endpoint) {
  3272. _currentInstance.deleteObject({ endpoint:endpoint, dontUpdateHover:dontUpdateHover });
  3273. }
  3274. return _currentInstance;
  3275. };
  3276. this.deleteEveryEndpoint = function() {
  3277. var _is = _currentInstance.setSuspendDrawing(true);
  3278. for ( var id in endpointsByElement) {
  3279. var endpoints = endpointsByElement[id];
  3280. if (endpoints && endpoints.length) {
  3281. for ( var i = 0, j = endpoints.length; i < j; i++) {
  3282. _currentInstance.deleteEndpoint(endpoints[i], true);
  3283. }
  3284. }
  3285. }
  3286. endpointsByElement = {};
  3287. // SP new
  3288. managedElements = {};
  3289. endpointsByUUID = {};
  3290. _currentInstance.anchorManager.reset();
  3291. _currentInstance.dragManager.reset();
  3292. if(!_is) _currentInstance.setSuspendDrawing(false);
  3293. return _currentInstance;
  3294. };
  3295. var fireDetachEvent = function(jpc, doFireEvent, originalEvent) {
  3296. // may have been given a connection, or in special cases, an object
  3297. var connType = _currentInstance.Defaults.ConnectionType || _currentInstance.getDefaultConnectionType(),
  3298. argIsConnection = jpc.constructor == connType,
  3299. params = argIsConnection ? {
  3300. connection:jpc,
  3301. source : jpc.source, target : jpc.target,
  3302. sourceId : jpc.sourceId, targetId : jpc.targetId,
  3303. sourceEndpoint : jpc.endpoints[0], targetEndpoint : jpc.endpoints[1]
  3304. } : jpc;
  3305. if (doFireEvent)
  3306. _currentInstance.fire("connectionDetached", params, originalEvent);
  3307. _currentInstance.anchorManager.connectionDetached(params);
  3308. };
  3309. var fireMoveEvent = _currentInstance.fireMoveEvent = function(params, evt) {
  3310. _currentInstance.fire("connectionMoved", params, evt);
  3311. };
  3312. this.unregisterEndpoint = function(endpoint) {
  3313. //if (endpoint._jsPlumb == null) return;
  3314. if (endpoint._jsPlumb.uuid) endpointsByUUID[endpoint._jsPlumb.uuid] = null;
  3315. _currentInstance.anchorManager.deleteEndpoint(endpoint);
  3316. // TODO at least replace this with a removeWithFunction call.
  3317. for (var e in endpointsByElement) {
  3318. var endpoints = endpointsByElement[e];
  3319. if (endpoints) {
  3320. var newEndpoints = [];
  3321. for (var i = 0, j = endpoints.length; i < j; i++)
  3322. if (endpoints[i] != endpoint) newEndpoints.push(endpoints[i]);
  3323. endpointsByElement[e] = newEndpoints;
  3324. }
  3325. if(endpointsByElement[e].length <1){
  3326. delete endpointsByElement[e];
  3327. }
  3328. }
  3329. };
  3330. this.detach = function() {
  3331. if (arguments.length === 0) return;
  3332. var connType = _currentInstance.Defaults.ConnectionType || _currentInstance.getDefaultConnectionType(),
  3333. firstArgIsConnection = arguments[0].constructor == connType,
  3334. params = arguments.length == 2 ? firstArgIsConnection ? (arguments[1] || {}) : arguments[0] : arguments[0],
  3335. fireEvent = (params.fireEvent !== false),
  3336. forceDetach = params.forceDetach,
  3337. conn = firstArgIsConnection ? arguments[0] : params.connection;
  3338. if (conn) {
  3339. if (forceDetach || jsPlumbUtil.functionChain(true, false, [
  3340. [ conn.endpoints[0], "isDetachAllowed", [ conn ] ],
  3341. [ conn.endpoints[1], "isDetachAllowed", [ conn ] ],
  3342. [ conn, "isDetachAllowed", [ conn ] ],
  3343. [ _currentInstance, "checkCondition", [ "beforeDetach", conn ] ] ])) {
  3344. conn.endpoints[0].detach(conn, false, true, fireEvent);
  3345. }
  3346. }
  3347. else {
  3348. var _p = jsPlumb.extend( {}, params); // a backwards compatibility hack: source should be thought of as 'params' in this case.
  3349. // test for endpoint uuids to detach
  3350. if (_p.uuids) {
  3351. _getEndpoint(_p.uuids[0]).detachFrom(_getEndpoint(_p.uuids[1]), fireEvent);
  3352. } else if (_p.sourceEndpoint && _p.targetEndpoint) {
  3353. _p.sourceEndpoint.detachFrom(_p.targetEndpoint);
  3354. } else {
  3355. var sourceId = _getId(_currentInstance.getDOMElement(_p.source)),
  3356. targetId = _getId(_currentInstance.getDOMElement(_p.target));
  3357. _operation(sourceId, function(jpc) {
  3358. if ((jpc.sourceId == sourceId && jpc.targetId == targetId) || (jpc.targetId == sourceId && jpc.sourceId == targetId)) {
  3359. if (_currentInstance.checkCondition("beforeDetach", jpc)) {
  3360. jpc.endpoints[0].detach(jpc, false, true, fireEvent);
  3361. }
  3362. }
  3363. });
  3364. }
  3365. }
  3366. };
  3367. this.detachAllConnections = function(el, params) {
  3368. params = params || {};
  3369. el = _currentInstance.getDOMElement(el);
  3370. var id = _getId(el),
  3371. endpoints = endpointsByElement[id];
  3372. if (endpoints && endpoints.length) {
  3373. for ( var i = 0, j = endpoints.length; i < j; i++) {
  3374. endpoints[i].detachAll(params.fireEvent !== false);
  3375. }
  3376. }
  3377. return _currentInstance;
  3378. };
  3379. this.detachEveryConnection = function(params) {
  3380. params = params || {};
  3381. _currentInstance.doWhileSuspended(function() {
  3382. for ( var id in endpointsByElement) {
  3383. var endpoints = endpointsByElement[id];
  3384. if (endpoints && endpoints.length) {
  3385. for ( var i = 0, j = endpoints.length; i < j; i++) {
  3386. endpoints[i].detachAll(params.fireEvent !== false);
  3387. }
  3388. }
  3389. }
  3390. connections.length = 0;
  3391. });
  3392. return _currentInstance;
  3393. };
  3394. /// not public. but of course its exposed. how to change this.
  3395. this.deleteObject = function(params) {
  3396. var result = {
  3397. endpoints : {},
  3398. connections : {},
  3399. endpointCount:0,
  3400. connectionCount:0
  3401. },
  3402. fireEvent = params.fireEvent !== false,
  3403. deleteAttachedObjects = params.deleteAttachedObjects !== false;
  3404. var unravelConnection = function(connection) {
  3405. if(connection != null && result.connections[connection.id] == null) {
  3406. if (!params.dontUpdateHover && connection._jsPlumb != null) connection.setHover(false);
  3407. result.connections[connection.id] = connection;
  3408. result.connectionCount++;
  3409. if (deleteAttachedObjects) {
  3410. for (var j = 0; j < connection.endpoints.length; j++) {
  3411. if (connection.endpoints[j]._deleteOnDetach)
  3412. unravelEndpoint(connection.endpoints[j]);
  3413. }
  3414. }
  3415. }
  3416. };
  3417. var unravelEndpoint = function(endpoint) {
  3418. if(endpoint != null && result.endpoints[endpoint.id] == null) {
  3419. if (!params.dontUpdateHover && endpoint._jsPlumb != null) endpoint.setHover(false);
  3420. result.endpoints[endpoint.id] = endpoint;
  3421. result.endpointCount++;
  3422. if (deleteAttachedObjects) {
  3423. for (var i = 0; i < endpoint.connections.length; i++) {
  3424. var c = endpoint.connections[i];
  3425. unravelConnection(c);
  3426. }
  3427. }
  3428. }
  3429. };
  3430. if (params.connection)
  3431. unravelConnection(params.connection);
  3432. else unravelEndpoint(params.endpoint);
  3433. // loop through connections
  3434. for (var i in result.connections) {
  3435. var c = result.connections[i];
  3436. if (c._jsPlumb) {
  3437. jsPlumbUtil.removeWithFunction(connections, function(_c) {
  3438. return c.id == _c.id;
  3439. });
  3440. fireDetachEvent(c, fireEvent, params.originalEvent);
  3441. c.endpoints[0].detachFromConnection(c);
  3442. c.endpoints[1].detachFromConnection(c);
  3443. // sp was ere
  3444. c.cleanup();
  3445. c.destroy();
  3446. }
  3447. }
  3448. // loop through endpoints
  3449. for (var j in result.endpoints) {
  3450. var e = result.endpoints[j];
  3451. if (e._jsPlumb) {
  3452. _currentInstance.unregisterEndpoint(e);
  3453. // FIRE some endpoint deleted event?
  3454. e.cleanup();
  3455. e.destroy();
  3456. }
  3457. }
  3458. return result;
  3459. };
  3460. this.draggable = function(el, options) {
  3461. var i,j,info;
  3462. // allows for array or jquery selector
  3463. if (typeof el == 'object' && el.length) {
  3464. for (i = 0, j = el.length; i < j; i++) {
  3465. info = _info(el[i]);
  3466. if (info.el) _initDraggableIfNecessary(info.el, true, options, info.id);
  3467. }
  3468. }
  3469. else {
  3470. //ele = _currentInstance.getDOMElement(el);
  3471. info = _info(el);
  3472. if (info.el) _initDraggableIfNecessary(info.el, true, options, info.id);
  3473. }
  3474. return _currentInstance;
  3475. };
  3476. // helpers for select/selectEndpoints
  3477. var _setOperation = function(list, func, args, selector) {
  3478. for (var i = 0, j = list.length; i < j; i++) {
  3479. list[i][func].apply(list[i], args);
  3480. }
  3481. return selector(list);
  3482. },
  3483. _getOperation = function(list, func, args) {
  3484. var out = [];
  3485. for (var i = 0, j = list.length; i < j; i++) {
  3486. out.push([ list[i][func].apply(list[i], args), list[i] ]);
  3487. }
  3488. return out;
  3489. },
  3490. setter = function(list, func, selector) {
  3491. return function() {
  3492. return _setOperation(list, func, arguments, selector);
  3493. };
  3494. },
  3495. getter = function(list, func) {
  3496. return function() {
  3497. return _getOperation(list, func, arguments);
  3498. };
  3499. },
  3500. prepareList = function(input, doNotGetIds) {
  3501. var r = [];
  3502. if (input) {
  3503. if (typeof input == 'string') {
  3504. if (input === "*") return input;
  3505. r.push(input);
  3506. }
  3507. else {
  3508. if (doNotGetIds) r = input;
  3509. else {
  3510. if (input.length) {
  3511. for (var i = 0, j = input.length; i < j; i++)
  3512. r.push(_info(input[i]).id);
  3513. }
  3514. else
  3515. r.push(_info(input).id);
  3516. }
  3517. }
  3518. }
  3519. return r;
  3520. },
  3521. filterList = function(list, value, missingIsFalse) {
  3522. if (list === "*") return true;
  3523. return list.length > 0 ? jsPlumbUtil.indexOf(list, value) != -1 : !missingIsFalse;
  3524. };
  3525. // get some connections, specifying source/target/scope
  3526. this.getConnections = function(options, flat) {
  3527. if (!options) {
  3528. options = {};
  3529. } else if (options.constructor == String) {
  3530. options = { "scope": options };
  3531. }
  3532. var scope = options.scope || _currentInstance.getDefaultScope(),
  3533. scopes = prepareList(scope, true),
  3534. sources = prepareList(options.source),
  3535. targets = prepareList(options.target),
  3536. results = (!flat && scopes.length > 1) ? {} : [],
  3537. _addOne = function(scope, obj) {
  3538. if (!flat && scopes.length > 1) {
  3539. var ss = results[scope];
  3540. if (ss == null) {
  3541. ss = results[scope] = [];
  3542. }
  3543. ss.push(obj);
  3544. } else results.push(obj);
  3545. };
  3546. for ( var j = 0, jj = connections.length; j < jj; j++) {
  3547. var c = connections[j];
  3548. if (filterList(scopes, c.scope) && filterList(sources, c.sourceId) && filterList(targets, c.targetId))
  3549. _addOne(c.scope, c);
  3550. }
  3551. return results;
  3552. };
  3553. var _curryEach = function(list, executor) {
  3554. return function(f) {
  3555. for (var i = 0, ii = list.length; i < ii; i++) {
  3556. f(list[i]);
  3557. }
  3558. return executor(list);
  3559. };
  3560. },
  3561. _curryGet = function(list) {
  3562. return function(idx) {
  3563. return list[idx];
  3564. };
  3565. };
  3566. var _makeCommonSelectHandler = function(list, executor) {
  3567. var out = {
  3568. length:list.length,
  3569. each:_curryEach(list, executor),
  3570. get:_curryGet(list)
  3571. },
  3572. setters = ["setHover", "removeAllOverlays", "setLabel", "addClass", "addOverlay", "removeOverlay",
  3573. "removeOverlays", "showOverlay", "hideOverlay", "showOverlays", "hideOverlays", "setPaintStyle",
  3574. "setHoverPaintStyle", "setSuspendEvents", "setParameter", "setParameters", "setVisible",
  3575. "repaint", "addType", "toggleType", "removeType", "removeClass", "setType", "bind", "unbind" ],
  3576. getters = ["getLabel", "getOverlay", "isHover", "getParameter", "getParameters", "getPaintStyle",
  3577. "getHoverPaintStyle", "isVisible", "hasType", "getType", "isSuspendEvents" ],
  3578. i, ii;
  3579. for (i = 0, ii = setters.length; i < ii; i++)
  3580. out[setters[i]] = setter(list, setters[i], executor);
  3581. for (i = 0, ii = getters.length; i < ii; i++)
  3582. out[getters[i]] = getter(list, getters[i]);
  3583. return out;
  3584. };
  3585. var _makeConnectionSelectHandler = function(list) {
  3586. var common = _makeCommonSelectHandler(list, _makeConnectionSelectHandler);
  3587. return jsPlumb.extend(common, {
  3588. // setters
  3589. setDetachable:setter(list, "setDetachable", _makeConnectionSelectHandler),
  3590. setReattach:setter(list, "setReattach", _makeConnectionSelectHandler),
  3591. setConnector:setter(list, "setConnector", _makeConnectionSelectHandler),
  3592. detach:function() {
  3593. for (var i = 0, ii = list.length; i < ii; i++)
  3594. _currentInstance.detach(list[i]);
  3595. },
  3596. // getters
  3597. isDetachable:getter(list, "isDetachable"),
  3598. isReattach:getter(list, "isReattach")
  3599. });
  3600. };
  3601. var _makeEndpointSelectHandler = function(list) {
  3602. var common = _makeCommonSelectHandler(list, _makeEndpointSelectHandler);
  3603. return jsPlumb.extend(common, {
  3604. setEnabled:setter(list, "setEnabled", _makeEndpointSelectHandler),
  3605. setAnchor:setter(list, "setAnchor", _makeEndpointSelectHandler),
  3606. isEnabled:getter(list, "isEnabled"),
  3607. detachAll:function() {
  3608. for (var i = 0, ii = list.length; i < ii; i++)
  3609. list[i].detachAll();
  3610. },
  3611. "remove":function() {
  3612. for (var i = 0, ii = list.length; i < ii; i++)
  3613. _currentInstance.deleteObject({endpoint:list[i]});
  3614. }
  3615. });
  3616. };
  3617. this.select = function(params) {
  3618. params = params || {};
  3619. params.scope = params.scope || "*";
  3620. return _makeConnectionSelectHandler(params.connections || _currentInstance.getConnections(params, true));
  3621. };
  3622. this.selectEndpoints = function(params) {
  3623. params = params || {};
  3624. params.scope = params.scope || "*";
  3625. var noElementFilters = !params.element && !params.source && !params.target,
  3626. elements = noElementFilters ? "*" : prepareList(params.element),
  3627. sources = noElementFilters ? "*" : prepareList(params.source),
  3628. targets = noElementFilters ? "*" : prepareList(params.target),
  3629. scopes = prepareList(params.scope, true);
  3630. var ep = [];
  3631. for (var el in endpointsByElement) {
  3632. var either = filterList(elements, el, true),
  3633. source = filterList(sources, el, true),
  3634. sourceMatchExact = sources != "*",
  3635. target = filterList(targets, el, true),
  3636. targetMatchExact = targets != "*";
  3637. // 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.
  3638. if ( either || source || target ) {
  3639. inner:
  3640. for (var i = 0, ii = endpointsByElement[el].length; i < ii; i++) {
  3641. var _ep = endpointsByElement[el][i];
  3642. if (filterList(scopes, _ep.scope, true)) {
  3643. var noMatchSource = (sourceMatchExact && sources.length > 0 && !_ep.isSource),
  3644. noMatchTarget = (targetMatchExact && targets.length > 0 && !_ep.isTarget);
  3645. if (noMatchSource || noMatchTarget)
  3646. continue inner;
  3647. ep.push(_ep);
  3648. }
  3649. }
  3650. }
  3651. }
  3652. return _makeEndpointSelectHandler(ep);
  3653. };
  3654. // get all connections managed by the instance of jsplumb.
  3655. this.getAllConnections = function() { return connections; };
  3656. this.getDefaultScope = function() { return DEFAULT_SCOPE; };
  3657. // get an endpoint by uuid.
  3658. this.getEndpoint = _getEndpoint;
  3659. // get endpoints for some element.
  3660. this.getEndpoints = function(el) { return endpointsByElement[_info(el).id]; };
  3661. // gets the default endpoint type. used when subclassing. see wiki.
  3662. this.getDefaultEndpointType = function() { return jsPlumb.Endpoint; };
  3663. // gets the default connection type. used when subclassing. see wiki.
  3664. this.getDefaultConnectionType = function() { return jsPlumb.Connection; };
  3665. /*
  3666. * Gets an element's id, creating one if necessary. really only exposed
  3667. * for the lib-specific functionality to access; would be better to pass
  3668. * the current instance into the lib-specific code (even though this is
  3669. * a static call. i just don't want to expose it to the public API).
  3670. */
  3671. this.getId = _getId;
  3672. this.getOffset = function(id) {
  3673. return _updateOffset({elId:id}).o;
  3674. };
  3675. this.appendElement = _appendElement;
  3676. var _hoverSuspended = false;
  3677. this.isHoverSuspended = function() { return _hoverSuspended; };
  3678. this.setHoverSuspended = function(s) { _hoverSuspended = s; };
  3679. var _isAvailable = function(m) {
  3680. return function() {
  3681. return jsPlumbAdapter.isRenderModeAvailable(m);
  3682. };
  3683. };
  3684. this.isSVGAvailable = _isAvailable("svg");
  3685. this.isVMLAvailable = _isAvailable("vml");
  3686. // set an element's connections to be hidden
  3687. this.hide = function(el, changeEndpoints) {
  3688. _setVisible(el, "none", changeEndpoints);
  3689. return _currentInstance;
  3690. };
  3691. // exposed for other objects to use to get a unique id.
  3692. this.idstamp = _idstamp;
  3693. this.connectorsInitialized = false;
  3694. var connectorTypes = [], rendererTypes = ["svg", "vml"];
  3695. this.registerConnectorType = function(connector, name) {
  3696. connectorTypes.push([connector, name]);
  3697. };
  3698. // ensure that, if the current container exists, it is a DOM element and not a selector.
  3699. // if it does not exist and `candidate` is supplied, the offset parent of that element will be set as the Container.
  3700. // this is used to do a better default behaviour for the case that the user has not set a container:
  3701. // addEndpoint, makeSource, makeTarget and connect all call this method with the offsetParent of the
  3702. // element in question (for connect it is the source element). So if no container is set, it is inferred
  3703. // to be the offsetParent of the first element the user tries to connect.
  3704. var _ensureContainer = function(candidate) {
  3705. if (!_container && candidate) {
  3706. var can = _currentInstance.getDOMElement(candidate);
  3707. if (can.offsetParent) _currentInstance.setContainer(can.offsetParent);
  3708. }
  3709. };
  3710. var _getContainerFromDefaults = function() {
  3711. if (_currentInstance.Defaults.Container)
  3712. _currentInstance.setContainer(_currentInstance.Defaults.Container);
  3713. };
  3714. // check if a given element is managed or not. if not, add to our map. if drawing is not suspended then
  3715. // we'll also stash its dimensions; otherwise we'll do this in a lazy way through updateOffset.
  3716. // TODO make sure we add a test that this tracks a setId call.
  3717. var _manage = _currentInstance.manage = function(id, element) {
  3718. if (!managedElements[id]) {
  3719. managedElements[id] = {
  3720. el:element,
  3721. endpoints:[],
  3722. connections:[]
  3723. };
  3724. managedElements[id].info = _updateOffset({ elId: id, timestamp: _suspendedAt });
  3725. /* if (!_suspendDrawing) {
  3726. managedElements[id].info = _updateOffset({ elId: id, timestamp: _suspendedAt });
  3727. }*/
  3728. }
  3729. return managedElements[id];
  3730. };
  3731. var _unmanage = function(id) {
  3732. delete managedElements[id];
  3733. };
  3734. /**
  3735. * updates the offset and size for a given element, and stores the
  3736. * values. if 'offset' is not null we use that (it would have been
  3737. * passed in from a drag call) because it's faster; but if it is null,
  3738. * or if 'recalc' is true in order to force a recalculation, we get the current values.
  3739. */
  3740. var _updateOffset = this.updateOffset = function(params) {
  3741. var timestamp = params.timestamp, recalc = params.recalc, offset = params.offset, elId = params.elId, s;
  3742. if (_suspendDrawing && !timestamp) timestamp = _suspendedAt;
  3743. if (!recalc) {
  3744. if (timestamp && timestamp === offsetTimestamps[elId]) {
  3745. return {o:params.offset || offsets[elId], s:sizes[elId]};
  3746. }
  3747. }
  3748. if (recalc || (!offset && offsets[elId] == null)) { // if forced repaint or no offset available, we recalculate.
  3749. // get the current size and offset, and store them
  3750. s = managedElements[elId] ? managedElements[elId].el : null;
  3751. if (s != null) {
  3752. sizes[elId] = _currentInstance.getSize(s);
  3753. offsets[elId] = _getOffset(s, _currentInstance);
  3754. offsetTimestamps[elId] = timestamp;
  3755. }
  3756. } else {
  3757. offsets[elId] = offset || offsets[elId];
  3758. if (sizes[elId] == null) {
  3759. //s = document.getElementById(elId);
  3760. s = managedElements[elId].el;
  3761. if (s != null) sizes[elId] = _currentInstance.getSize(s);
  3762. }
  3763. offsetTimestamps[elId] = timestamp;
  3764. }
  3765. if(offsets[elId] && !offsets[elId].right) {
  3766. offsets[elId].right = offsets[elId].left + sizes[elId][0];
  3767. offsets[elId].bottom = offsets[elId].top + sizes[elId][1];
  3768. offsets[elId].width = sizes[elId][0];
  3769. offsets[elId].height = sizes[elId][1];
  3770. offsets[elId].centerx = offsets[elId].left + (offsets[elId].width / 2);
  3771. offsets[elId].centery = offsets[elId].top + (offsets[elId].height / 2);
  3772. }
  3773. return {o:offsets[elId], s:sizes[elId]};
  3774. };
  3775. /**
  3776. * callback from the current library to tell us to prepare ourselves (attach
  3777. * mouse listeners etc; can't do that until the library has provided a bind method)
  3778. */
  3779. this.init = function() {
  3780. var _oneType = function(renderer, name, fn) {
  3781. jsPlumb.Connectors[renderer][name] = function() {
  3782. fn.apply(this, arguments);
  3783. jsPlumb.ConnectorRenderers[renderer].apply(this, arguments);
  3784. };
  3785. jsPlumbUtil.extend(jsPlumb.Connectors[renderer][name], [ fn, jsPlumb.ConnectorRenderers[renderer]]);
  3786. };
  3787. if (!jsPlumb.connectorsInitialized) {
  3788. for (var i = 0; i < connectorTypes.length; i++) {
  3789. for (var j = 0; j < rendererTypes.length; j++) {
  3790. _oneType(rendererTypes[j], connectorTypes[i][1], connectorTypes[i][0]);
  3791. }
  3792. }
  3793. jsPlumb.connectorsInitialized = true;
  3794. }
  3795. if (!initialized) {
  3796. _getContainerFromDefaults();
  3797. _currentInstance.anchorManager = new jsPlumb.AnchorManager({jsPlumbInstance:_currentInstance});
  3798. _currentInstance.setRenderMode(_currentInstance.Defaults.RenderMode); // calling the method forces the capability logic to be run.
  3799. initialized = true;
  3800. _currentInstance.fire("ready", _currentInstance);
  3801. }
  3802. }.bind(this);
  3803. this.log = log;
  3804. this.jsPlumbUIComponent = jsPlumbUIComponent;
  3805. /*
  3806. * Creates an anchor with the given params.
  3807. *
  3808. *
  3809. * Returns: The newly created Anchor.
  3810. * Throws: an error if a named anchor was not found.
  3811. */
  3812. this.makeAnchor = function() {
  3813. var pp, _a = function(t, p) {
  3814. if (jsPlumb.Anchors[t]) return new jsPlumb.Anchors[t](p);
  3815. if (!_currentInstance.Defaults.DoNotThrowErrors)
  3816. throw { msg:"jsPlumb: unknown anchor type '" + t + "'" };
  3817. };
  3818. if (arguments.length === 0) return null;
  3819. var specimen = arguments[0], elementId = arguments[1], jsPlumbInstance = arguments[2], newAnchor = null;
  3820. // if it appears to be an anchor already...
  3821. if (specimen.compute && specimen.getOrientation) return specimen; //TODO hazy here about whether it should be added or is already added somehow.
  3822. // is it the name of an anchor type?
  3823. else if (typeof specimen == "string") {
  3824. newAnchor = _a(arguments[0], {elementId:elementId, jsPlumbInstance:_currentInstance});
  3825. }
  3826. // is it an array? it will be one of:
  3827. // an array of [spec, params] - this defines a single anchor, which may be dynamic, but has parameters.
  3828. // an array of arrays - this defines some dynamic anchors
  3829. // an array of numbers - this defines a single anchor.
  3830. else if (_ju.isArray(specimen)) {
  3831. if (_ju.isArray(specimen[0]) || _ju.isString(specimen[0])) {
  3832. // if [spec, params] format
  3833. if (specimen.length == 2 && _ju.isObject(specimen[1])) {
  3834. // if first arg is a string, its a named anchor with params
  3835. if (_ju.isString(specimen[0])) {
  3836. pp = jsPlumb.extend({elementId:elementId, jsPlumbInstance:_currentInstance}, specimen[1]);
  3837. newAnchor = _a(specimen[0], pp);
  3838. }
  3839. // otherwise first arg is array, second is params. we treat as a dynamic anchor, which is fine
  3840. // even if the first arg has only one entry. you could argue all anchors should be implicitly dynamic in fact.
  3841. else {
  3842. pp = jsPlumb.extend({elementId:elementId, jsPlumbInstance:_currentInstance, anchors:specimen[0]}, specimen[1]);
  3843. newAnchor = new jsPlumb.DynamicAnchor(pp);
  3844. }
  3845. }
  3846. else
  3847. newAnchor = new jsPlumb.DynamicAnchor({anchors:specimen, selector:null, elementId:elementId, jsPlumbInstance:_currentInstance});
  3848. }
  3849. else {
  3850. var anchorParams = {
  3851. x:specimen[0], y:specimen[1],
  3852. orientation : (specimen.length >= 4) ? [ specimen[2], specimen[3] ] : [0,0],
  3853. offsets : (specimen.length >= 6) ? [ specimen[4], specimen[5] ] : [ 0, 0 ],
  3854. elementId:elementId,
  3855. jsPlumbInstance:_currentInstance,
  3856. cssClass:specimen.length == 7 ? specimen[6] : null
  3857. };
  3858. newAnchor = new jsPlumb.Anchor(anchorParams);
  3859. newAnchor.clone = function() { return new jsPlumb.Anchor(anchorParams); };
  3860. }
  3861. }
  3862. if (!newAnchor.id) newAnchor.id = "anchor_" + _idstamp();
  3863. return newAnchor;
  3864. };
  3865. /**
  3866. * makes a list of anchors from the given list of types or coords, eg
  3867. * ["TopCenter", "RightMiddle", "BottomCenter", [0, 1, -1, -1] ]
  3868. */
  3869. this.makeAnchors = function(types, elementId, jsPlumbInstance) {
  3870. var r = [];
  3871. for ( var i = 0, ii = types.length; i < ii; i++) {
  3872. if (typeof types[i] == "string")
  3873. r.push(jsPlumb.Anchors[types[i]]({elementId:elementId, jsPlumbInstance:jsPlumbInstance}));
  3874. else if (_ju.isArray(types[i]))
  3875. r.push(_currentInstance.makeAnchor(types[i], elementId, jsPlumbInstance));
  3876. }
  3877. return r;
  3878. };
  3879. /**
  3880. * Makes a dynamic anchor from the given list of anchors (which may be in shorthand notation as strings or dimension arrays, or Anchor
  3881. * objects themselves) and the given, optional, anchorSelector function (jsPlumb uses a default if this is not provided; most people will
  3882. * not need to provide this - i think).
  3883. */
  3884. this.makeDynamicAnchor = function(anchors, anchorSelector) {
  3885. return new jsPlumb.DynamicAnchor({anchors:anchors, selector:anchorSelector, elementId:null, jsPlumbInstance:_currentInstance});
  3886. };
  3887. // --------------------- makeSource/makeTarget ----------------------------------------------
  3888. this.targetEndpointDefinitions = {};
  3889. var _setEndpointPaintStylesAndAnchor = function(ep, epIndex, _instance) {
  3890. ep.paintStyle = ep.paintStyle ||
  3891. _instance.Defaults.EndpointStyles[epIndex] ||
  3892. _instance.Defaults.EndpointStyle;
  3893. ep.hoverPaintStyle = ep.hoverPaintStyle ||
  3894. _instance.Defaults.EndpointHoverStyles[epIndex] ||
  3895. _instance.Defaults.EndpointHoverStyle;
  3896. ep.anchor = ep.anchor ||
  3897. _instance.Defaults.Anchors[epIndex] ||
  3898. _instance.Defaults.Anchor;
  3899. ep.endpoint = ep.endpoint ||
  3900. _instance.Defaults.Endpoints[epIndex] ||
  3901. _instance.Defaults.Endpoint;
  3902. };
  3903. // TODO put all the source stuff inside one parent, keyed by id.
  3904. this.sourceEndpointDefinitions = {};
  3905. var selectorFilter = function(evt, _el, selector, _instance, negate) {
  3906. var t = evt.target || evt.srcElement, ok = false,
  3907. sel = _instance.getSelector(_el, selector);
  3908. for (var j = 0; j < sel.length; j++) {
  3909. if (sel[j] == t) {
  3910. ok = true;
  3911. break;
  3912. }
  3913. }
  3914. return negate ? !ok : ok;
  3915. };
  3916. //this.
  3917. // see API docs
  3918. this.makeTarget = function(el, params, referenceParams) {
  3919. // put jsplumb ref into params without altering the params passed in
  3920. var p = jsPlumb.extend({_jsPlumb:this}, referenceParams);
  3921. jsPlumb.extend(p, params);
  3922. // calculate appropriate paint styles and anchor from the params given
  3923. _setEndpointPaintStylesAndAnchor(p, 1, this);
  3924. var targetScope = p.scope || _currentInstance.Defaults.Scope,
  3925. deleteEndpointsOnDetach = !(p.deleteEndpointsOnDetach === false),
  3926. maxConnections = p.maxConnections || -1,
  3927. onMaxConnections = p.onMaxConnections,
  3928. _doOne = function(el) {
  3929. // get the element's id and store the endpoint definition for it. jsPlumb.connect calls will look for one of these,
  3930. // and use the endpoint definition if found.
  3931. // decode the info for this element (id and element)
  3932. var elInfo = _info(el),
  3933. elid = elInfo.id,
  3934. proxyComponent = new jsPlumbUIComponent(p),
  3935. dropOptions = jsPlumb.extend({}, p.dropOptions || {});
  3936. _ensureContainer(elid);
  3937. // store the definitions keyed against the element id.
  3938. // TODO why not just store inside the element itself?
  3939. var _def = {
  3940. def:p,
  3941. uniqueEndpoint:p.uniqueEndpoint,
  3942. maxConnections:maxConnections,
  3943. enabled:true
  3944. };
  3945. elInfo.el._jsPlumbTarget = _def;
  3946. this.targetEndpointDefinitions[elid] = _def;
  3947. var _drop = p._jsPlumb.EndpointDropHandler({
  3948. //endpoint:_ep,
  3949. jsPlumb: _currentInstance,
  3950. enabled:function() {
  3951. return elInfo.el._jsPlumbTarget.enabled;
  3952. },
  3953. isFull:function(originalEvent) {
  3954. var targetCount = _currentInstance.select({target:elid}).length;
  3955. var def = elInfo.el._jsPlumbTarget;
  3956. var full = def.maxConnections > 0 && targetCount >= def.maxConnections;
  3957. if (full && onMaxConnections) {
  3958. // TODO here we still have the id of the floating element, not the
  3959. // actual target.
  3960. onMaxConnections({
  3961. element:elInfo.el,
  3962. connection:jpc
  3963. }, originalEvent);
  3964. }
  3965. return full;
  3966. },
  3967. element:elInfo.el,
  3968. elementId:elid,
  3969. isSource:false,
  3970. isTarget:true,
  3971. addClass:function(clazz) {
  3972. //_ep.addClass(clazz)
  3973. _currentInstance.addClass(elInfo.el, clazz);
  3974. },
  3975. removeClass:function(clazz) {
  3976. //_ep.removeClass(clazz)
  3977. _currentInstance.removeClass(elInfo.el, clazz);
  3978. },
  3979. onDrop:function(jpc) {
  3980. var source = jpc.endpoints[0];
  3981. source.anchor.locked = false;
  3982. },
  3983. isDropAllowed:function() {
  3984. return proxyComponent.isDropAllowed.apply(proxyComponent, arguments);
  3985. },
  3986. getEndpoint:function(jpc) {
  3987. // make a new Endpoint for the target, or get it from the cache if uniqueEndpoint
  3988. // is set.
  3989. var _el = _currentInstance.getElementObject(elInfo.el),
  3990. def = elInfo.el._jsPlumbTarget,
  3991. newEndpoint = def.endpoint;
  3992. // if no cached endpoint, or there was one but it has been cleaned up
  3993. // (ie. detached), then create a new one.
  3994. if (newEndpoint == null || newEndpoint._jsPlumb == null)
  3995. newEndpoint = _currentInstance.addEndpoint(_el, p);
  3996. if (p.uniqueEndpoint) def.endpoint = newEndpoint; // may of course just store what it just pulled out. that's ok.
  3997. // TODO test options to makeTarget to see if we should do this?
  3998. newEndpoint._doNotDeleteOnDetach = false; // reset.
  3999. newEndpoint._deleteOnDetach = true;
  4000. // if connection is detachable, init the new endpoint to be draggable, to support that happening.
  4001. if (jpc.isDetachable())
  4002. newEndpoint.initDraggable();
  4003. // if the anchor has a 'positionFinder' set, then delegate to that function to find
  4004. // out where to locate the anchor.
  4005. if (newEndpoint.anchor.positionFinder != null) {
  4006. var dropPosition = _currentInstance.getUIPosition(arguments, this.getZoom()),
  4007. elPosition = _getOffset(_el, this),
  4008. elSize = _currentInstance.getSize(_el),
  4009. ap = newEndpoint.anchor.positionFinder(dropPosition, elPosition, elSize, newEndpoint.anchor.constructorParams);
  4010. newEndpoint.anchor.x = ap[0];
  4011. newEndpoint.anchor.y = ap[1];
  4012. // now figure an orientation for it..kind of hard to know what to do actually. probably the best thing i can do is to
  4013. // support specifying an orientation in the anchor's spec. if one is not supplied then i will make the orientation
  4014. // be what will cause the most natural link to the source: it will be pointing at the source, but it needs to be
  4015. // specified in one axis only, and so how to make that choice? i think i will use whichever axis is the one in which
  4016. // the target is furthest away from the source.
  4017. }
  4018. return newEndpoint;
  4019. }
  4020. });
  4021. // wrap drop events as needed and initialise droppable
  4022. var dropEvent = jsPlumb.dragEvents.drop;
  4023. dropOptions.scope = dropOptions.scope || targetScope;
  4024. dropOptions[dropEvent] = _ju.wrap(dropOptions[dropEvent], _drop);
  4025. // vanilla jsplumb only
  4026. if (p.allowLoopback === false) {
  4027. dropOptions.canDrop = function(_drag) {
  4028. var de = _drag.getDragElement()._jsPlumbRelatedElement;
  4029. return de != elInfo.el;
  4030. };
  4031. }
  4032. this.initDroppable(this.getElementObject(elInfo.el), dropOptions, "internal");
  4033. }.bind(this);
  4034. // make an array if only given one element
  4035. var inputs = el.length && el.constructor != String ? el : [ el ];
  4036. // register each one in the list.
  4037. for (var i = 0, ii = inputs.length; i < ii; i++) {
  4038. _doOne(inputs[i]);
  4039. }
  4040. return this;
  4041. };
  4042. // see api docs
  4043. this.unmakeTarget = function(el, doNotClearArrays) {
  4044. var info = _info(el);
  4045. jsPlumb.destroyDroppable(info.el);
  4046. // TODO this is not an exhaustive unmake of a target, since it does not remove the droppable stuff from
  4047. // the element. the effect will be to prevent it from behaving as a target, but it's not completely purged.
  4048. if (!doNotClearArrays) {
  4049. delete this.targetEndpointDefinitions[info.id];
  4050. }
  4051. return this;
  4052. };
  4053. // see api docs
  4054. this.makeSource = function(el, params, referenceParams) {
  4055. var p = jsPlumb.extend({}, referenceParams);
  4056. jsPlumb.extend(p, params);
  4057. _setEndpointPaintStylesAndAnchor(p, 0, this);
  4058. var maxConnections = p.maxConnections || 1,
  4059. onMaxConnections = p.onMaxConnections,
  4060. _doOne = function(elInfo) {
  4061. // get the element's id and store the endpoint definition for it. jsPlumb.connect calls will look for one of these,
  4062. // and use the endpoint definition if found.
  4063. var elid = elInfo.id,
  4064. _el = this.getElementObject(elInfo.el),
  4065. _del = this.getDOMElement(_el),
  4066. parentElement = function() {
  4067. return p.parent == null ? null : p.parent === "parent" ? elInfo.el.parentNode : _currentInstance.getDOMElement(p.parent);
  4068. },
  4069. idToRegisterAgainst = p.parent != null ? this.getId(parentElement()) : elid;
  4070. _ensureContainer(idToRegisterAgainst);
  4071. this.sourceEndpointDefinitions[idToRegisterAgainst] = {
  4072. def:p,
  4073. uniqueEndpoint:p.uniqueEndpoint,
  4074. maxConnections:maxConnections,
  4075. enabled:true
  4076. };
  4077. var stopEvent = jsPlumb.dragEvents.stop,
  4078. dragEvent = jsPlumb.dragEvents.drag,
  4079. dragOptions = jsPlumb.extend({ }, p.dragOptions || {}),
  4080. existingDrag = dragOptions.drag,
  4081. existingStop = dragOptions.stop,
  4082. ep = null,
  4083. endpointAddedButNoDragYet = false;
  4084. // set scope if its not set in dragOptions but was passed in in params
  4085. dragOptions.scope = dragOptions.scope || p.scope;
  4086. dragOptions[dragEvent] = _ju.wrap(dragOptions[dragEvent], function() {
  4087. if (existingDrag) existingDrag.apply(this, arguments);
  4088. endpointAddedButNoDragYet = false;
  4089. });
  4090. dragOptions[stopEvent] = _ju.wrap(dragOptions[stopEvent], function() {
  4091. if (existingStop) existingStop.apply(this, arguments);
  4092. this.currentlyDragging = false;
  4093. if (ep._jsPlumb != null) { // if not cleaned up...
  4094. // reset the anchor to the anchor that was initially provided. the one we were using to drag
  4095. // the connection was just a placeholder that was located at the place the user pressed the
  4096. // mouse button to initiate the drag.
  4097. var anchorDef = p.anchor || this.Defaults.Anchor,
  4098. oldAnchor = ep.anchor,
  4099. oldConnection = ep.connections[0],
  4100. newAnchor = this.makeAnchor(anchorDef, elid, this),
  4101. _el = ep.element;
  4102. // if the anchor has a 'positionFinder' set, then delegate to that function to find
  4103. // out where to locate the anchor. issue 117.
  4104. if (newAnchor.positionFinder != null) {
  4105. var elPosition = _getOffset(_el, this),
  4106. elSize = this.getSize(_el),
  4107. dropPosition = { left:elPosition.left + (oldAnchor.x * elSize[0]), top:elPosition.top + (oldAnchor.y * elSize[1]) },
  4108. ap = newAnchor.positionFinder(dropPosition, elPosition, elSize, newAnchor.constructorParams);
  4109. newAnchor.x = ap[0];
  4110. newAnchor.y = ap[1];
  4111. }
  4112. ep.setAnchor(newAnchor, true);
  4113. if (p.parent) {
  4114. var parent = parentElement();
  4115. if (parent) {
  4116. var potentialParent = p.container || _container;
  4117. ep.setElement(parent, potentialParent);
  4118. }
  4119. }
  4120. ep.repaint();
  4121. this.repaint(ep.elementId);
  4122. this.repaint(oldConnection.targetId);
  4123. }
  4124. }.bind(this));
  4125. // when the user presses the mouse, add an Endpoint, if we are enabled.
  4126. var mouseDownListener = function(e) {
  4127. var evt = this.getOriginalEvent(e);
  4128. // on right mouse button, abort.
  4129. if (e.which === 3 || e.button === 2) return;
  4130. var def = this.sourceEndpointDefinitions[idToRegisterAgainst];
  4131. elid = this.getId(this.getDOMElement(_el)); // elid might have changed since this method was called to configure the element.
  4132. // if disabled, return.
  4133. if (!def.enabled) return;
  4134. // if a filter was given, run it, and return if it says no.
  4135. if (p.filter) {
  4136. var r = jsPlumbUtil.isString(p.filter) ? selectorFilter(evt, _el, p.filter, this, p.filterExclude) : p.filter(evt, _el);
  4137. if (r === false) return;
  4138. }
  4139. // if maxConnections reached
  4140. var sourceCount = this.select({source:idToRegisterAgainst}).length;
  4141. if (def.maxConnections >= 0 && (def.uniqueEndpoint && sourceCount >= def.maxConnections)) {
  4142. if (onMaxConnections) {
  4143. onMaxConnections({
  4144. element:_el,
  4145. maxConnections:maxConnections
  4146. }, e);
  4147. }
  4148. return false;
  4149. }
  4150. // find the position on the element at which the mouse was pressed; this is where the endpoint
  4151. // will be located.
  4152. var elxy = jsPlumbAdapter.getPositionOnElement(evt, _del, _zoom), pelxy = elxy;
  4153. // we need to override the anchor in here, and force 'isSource', but we don't want to mess with
  4154. // the params passed in, because after a connection is established we're going to reset the endpoint
  4155. // to have the anchor we were given.
  4156. var tempEndpointParams = {};
  4157. jsPlumb.extend(tempEndpointParams, p);
  4158. tempEndpointParams.isTemporarySource = true;
  4159. tempEndpointParams.anchor = [ elxy[0], elxy[1] , 0,0];
  4160. tempEndpointParams.dragOptions = dragOptions;
  4161. ep = this.addEndpoint(elid, tempEndpointParams);
  4162. endpointAddedButNoDragYet = true;
  4163. ep.endpointWillMoveTo = p.parent ? parentElement() : null;
  4164. // if unique endpoint and it's already been created, push it onto the endpoint we create. at the end
  4165. // of a successful connection we'll switch to that endpoint.
  4166. //if (def.uniqueEndpoint && def.endpoint) ep.finalEndpoint = def.endpoint;
  4167. if (def.uniqueEndpoint) {
  4168. if (!def.endpoint)
  4169. def.endpoint = ep;
  4170. else
  4171. ep.finalEndpoint = def.endpoint;
  4172. }
  4173. // TODO test options to makeSource to see if we should do this?
  4174. ep._doNotDeleteOnDetach = false; // reset.
  4175. ep._deleteOnDetach = true;
  4176. var _delTempEndpoint = function() {
  4177. // this mouseup event is fired only if no dragging occurred, by jquery and yui, but for mootools
  4178. // it is fired even if dragging has occurred, in which case we would blow away a perfectly
  4179. // legitimate endpoint, were it not for this check. the flag is set after adding an
  4180. // endpoint and cleared in a drag listener we set in the dragOptions above.
  4181. _currentInstance.off(ep.canvas, "mouseup", _delTempEndpoint);
  4182. _currentInstance.off(_el, "mouseup", _delTempEndpoint);
  4183. if(endpointAddedButNoDragYet) {
  4184. endpointAddedButNoDragYet = false;
  4185. _currentInstance.deleteEndpoint(ep);
  4186. }
  4187. };
  4188. _currentInstance.on(ep.canvas, "mouseup", _delTempEndpoint);
  4189. _currentInstance.on(_el, "mouseup", _delTempEndpoint);
  4190. // and then trigger its mousedown event, which will kick off a drag, which will start dragging
  4191. // a new connection from this endpoint.
  4192. _currentInstance.trigger(ep.canvas, "mousedown", e);
  4193. jsPlumbUtil.consume(e);
  4194. }.bind(this);
  4195. this.on(_el, "mousedown", mouseDownListener);
  4196. this.sourceEndpointDefinitions[idToRegisterAgainst].trigger = mouseDownListener;
  4197. // lastly, if a filter was provided, set it as a dragFilter on the element,
  4198. // to prevent the element drag function from kicking in when we want to
  4199. // drag a new connection
  4200. if (p.filter && jsPlumbUtil.isString(p.filter)) {
  4201. _currentInstance.setDragFilter(_el, p.filter);
  4202. }
  4203. }.bind(this);
  4204. var inputs = el.length && el.constructor != String ? el : [ el ];
  4205. for (var i = 0, ii = inputs.length; i < ii; i++) {
  4206. _doOne(_info(inputs[i]));
  4207. }
  4208. return this;
  4209. };
  4210. // see api docs
  4211. this.unmakeSource = function(el, doNotClearArrays) {
  4212. var info = _info(el),
  4213. mouseDownListener = this.sourceEndpointDefinitions[info.id].trigger;
  4214. if (mouseDownListener)
  4215. _currentInstance.off(info.el, "mousedown", mouseDownListener);
  4216. if (!doNotClearArrays) {
  4217. delete this.sourceEndpointDefinitions[info.id];
  4218. }
  4219. return this;
  4220. };
  4221. // see api docs
  4222. this.unmakeEverySource = function() {
  4223. for (var i in this.sourceEndpointDefinitions)
  4224. _currentInstance.unmakeSource(i, true);
  4225. this.sourceEndpointDefinitions = {};
  4226. return this;
  4227. };
  4228. var _getScope = function(el, types) {
  4229. types = jsPlumbUtil.isArray(types) ? types : [ types ];
  4230. var id = _getId(el);
  4231. for (var i = 0; i < types.length; i++) {
  4232. var def = this[types[i]][id];
  4233. if (def) return def.def.scope || this.Defaults.Scope;
  4234. }
  4235. }.bind(this);
  4236. var _setScope = function(el, scope, types) {
  4237. types = jsPlumbUtil.isArray(types) ? types : [ types ];
  4238. var id = _getId(el);
  4239. for (var i = 0; i < types.length; i++) {
  4240. var def = this[types[i]][id];
  4241. if (def) {
  4242. def.def.scope = scope;
  4243. if (this.scopeChange != null) this.scopeChange(el, id, endpointsByElement[id], scope, types[i]);
  4244. }
  4245. }
  4246. }.bind(this);
  4247. this.getScope = function(el, scope) { return _getScope(el, [ "sourceEndpointDefinitions", "targetEndpointDefinitions" ]); };
  4248. this.getSourceScope = function(el) { return _getScope(el, "sourceEndpointDefinitions"); };
  4249. this.getTargetScope = function(el) { return _getScope(el, "targetEndpointDefinitions"); };
  4250. this.setScope = function(el, scope) { _setScope(el, scope, [ "sourceEndpointDefinitions", "targetEndpointDefinitions" ]); };
  4251. this.setSourceScope = function(el, scope) { _setScope(el, scope, "sourceEndpointDefinitions"); };
  4252. this.setTargetScope = function(el, scope) { _setScope(el, scope, "targetEndpointDefinitions"); };
  4253. // see api docs
  4254. this.unmakeEveryTarget = function() {
  4255. for (var i in this.targetEndpointDefinitions)
  4256. _currentInstance.unmakeTarget(i, true);
  4257. this.targetEndpointDefinitions = {};
  4258. return this;
  4259. };
  4260. // does the work of setting a source enabled or disabled.
  4261. var _setEnabled = function(type, el, state, toggle) {
  4262. var a = type == "source" ? this.sourceEndpointDefinitions : this.targetEndpointDefinitions;
  4263. if (_ju.isString(el)) a[el].enabled = toggle ? !a[el].enabled : state;
  4264. else if (el.length) {
  4265. for (var i = 0, ii = el.length; i < ii; i++) {
  4266. var info = _info(el[i]);
  4267. if (a[info.id])
  4268. a[info.id].enabled = toggle ? !a[info.id].enabled : state;
  4269. }
  4270. }
  4271. // otherwise a DOM element
  4272. else {
  4273. var id = _info(el).id;
  4274. a[id].enabled = toggle ? !a[id].enabled : state;
  4275. }
  4276. return this;
  4277. }.bind(this);
  4278. var _first = function(el, fn) {
  4279. if (_ju.isString(el) || !el.length)
  4280. return fn.apply(this, [ el ]);
  4281. else if (el.length)
  4282. return fn.apply(this, [ el[0] ]);
  4283. }.bind(this);
  4284. this.toggleSourceEnabled = function(el) {
  4285. _setEnabled("source", el, null, true);
  4286. return this.isSourceEnabled(el);
  4287. };
  4288. this.setSourceEnabled = function(el, state) { return _setEnabled("source", el, state); };
  4289. this.isSource = function(el) {
  4290. return _first(el, function(_el) {
  4291. return this.sourceEndpointDefinitions[_info(_el).id] != null;
  4292. }.bind(this));
  4293. };
  4294. this.isSourceEnabled = function(el) {
  4295. return _first(el, function(_el) {
  4296. var sep = this.sourceEndpointDefinitions[_info(_el).id];
  4297. return sep && sep.enabled === true;
  4298. }.bind(this));
  4299. };
  4300. this.toggleTargetEnabled = function(el) {
  4301. _setEnabled("target", el, null, true);
  4302. return this.isTargetEnabled(el);
  4303. };
  4304. this.isTarget = function(el) {
  4305. return _first(el, function(_el) {
  4306. return this.targetEndpointDefinitions[_info(_el).id] != null;
  4307. }.bind(this));
  4308. };
  4309. this.isTargetEnabled = function(el) {
  4310. return _first(el, function(_el) {
  4311. var tep = this.targetEndpointDefinitions[_info(_el).id];
  4312. return tep && tep.enabled === true;
  4313. }.bind(this));
  4314. };
  4315. this.setTargetEnabled = function(el, state) { return _setEnabled("target", el, state); };
  4316. // --------------------- end makeSource/makeTarget ----------------------------------------------
  4317. this.ready = function(fn) {
  4318. _currentInstance.bind("ready", fn);
  4319. };
  4320. // repaint some element's endpoints and connections
  4321. this.repaint = function(el, ui, timestamp) {
  4322. // support both lists...
  4323. if (typeof el == 'object' && el.length)
  4324. for ( var i = 0, ii = el.length; i < ii; i++) {
  4325. _draw(el[i], ui, timestamp);
  4326. }
  4327. else // ...and single strings.
  4328. _draw(el, ui, timestamp);
  4329. return _currentInstance;
  4330. };
  4331. this.revalidate = function(el) {
  4332. var elId = _currentInstance.getId(el);
  4333. _currentInstance.updateOffset( { elId : elId, recalc : true } );
  4334. return _currentInstance.repaint(el);
  4335. };
  4336. // repaint every endpoint and connection.
  4337. this.repaintEverything = function(clearEdits) {
  4338. // TODO this timestamp causes continuous anchors to not repaint properly.
  4339. // fix this. do not just take out the timestamp. it runs a lot faster with
  4340. // the timestamp included.
  4341. //var timestamp = null;
  4342. var timestamp = _timestamp(), elId;
  4343. for (elId in endpointsByElement) {
  4344. _currentInstance.updateOffset( { elId : elId, recalc : true, timestamp:timestamp } );
  4345. }
  4346. for (elId in endpointsByElement) {
  4347. _draw(elId, null, timestamp, clearEdits);
  4348. }
  4349. return this;
  4350. };
  4351. this.removeAllEndpoints = function(el, recurse) {
  4352. var _one = function(_el) {
  4353. var info = _info(_el),
  4354. ebe = endpointsByElement[info.id],
  4355. i, ii;
  4356. if (ebe) {
  4357. for ( i = 0, ii = ebe.length; i < ii; i++)
  4358. _currentInstance.deleteEndpoint(ebe[i]);
  4359. }
  4360. delete endpointsByElement[info.id];
  4361. if (recurse) {
  4362. if (info.el && info.el.nodeType != 3 && info.el.nodeType != 8 ) {
  4363. for ( i = 0, ii = info.el.childNodes.length; i < ii; i++) {
  4364. _one(info.el.childNodes[i]);
  4365. }
  4366. }
  4367. }
  4368. };
  4369. _one(el);
  4370. return this;
  4371. };
  4372. /**
  4373. * Remove the given element, including cleaning up all endpoints registered for it.
  4374. * This is exposed in the public API but also used internally by jsPlumb when removing the
  4375. * element associated with a connection drag.
  4376. */
  4377. this.remove = function(el, doNotRepaint) {
  4378. var info = _info(el);
  4379. _currentInstance.doWhileSuspended(function() {
  4380. _currentInstance.removeAllEndpoints(info.id, true);
  4381. _currentInstance.dragManager.elementRemoved(info.id);
  4382. delete _currentInstance.floatingConnections[info.id];
  4383. _currentInstance.anchorManager.clearFor(info.id);
  4384. _currentInstance.anchorManager.removeFloatingConnection(info.id);
  4385. }, doNotRepaint === false);
  4386. _unmanage(info.id);
  4387. if (info.el){
  4388. _currentInstance.removeElement(info.el);
  4389. info.el._jsPlumb = null;
  4390. }
  4391. return _currentInstance;
  4392. };
  4393. this.reset = function() {
  4394. _currentInstance.setSuspendEvents(true);
  4395. _currentInstance.deleteEveryEndpoint();
  4396. _currentInstance.unbind();
  4397. this.targetEndpointDefinitions = {};
  4398. this.sourceEndpointDefinitions = {};
  4399. connections.length = 0;
  4400. _currentInstance.setSuspendEvents(false);
  4401. };
  4402. var _clearObject = function(obj) {
  4403. if(obj.canvas && obj.canvas.parentNode)
  4404. obj.canvas.parentNode.removeChild(obj.canvas);
  4405. obj.cleanup();
  4406. obj.destroy();
  4407. };
  4408. var _clearOverlayObject = function(obj) {
  4409. _clearObject(obj);
  4410. };
  4411. this.clear = function() {
  4412. _currentInstance.select().each(_clearOverlayObject);
  4413. _currentInstance.selectEndpoints().each(_clearOverlayObject);
  4414. endpointsByElement = {};
  4415. endpointsByUUID = {};
  4416. };
  4417. this.setDefaultScope = function(scope) {
  4418. DEFAULT_SCOPE = scope;
  4419. return _currentInstance;
  4420. };
  4421. // sets whether or not some element should be currently draggable.
  4422. this.setDraggable = _setDraggable;
  4423. // sets the id of some element, changing whatever we need to to keep track.
  4424. this.setId = function(el, newId, doNotSetAttribute) {
  4425. //
  4426. var id;
  4427. if (jsPlumbUtil.isString(el)) {
  4428. id = el;
  4429. }
  4430. else {
  4431. el = this.getDOMElement(el);
  4432. id = this.getId(el);
  4433. }
  4434. var sConns = this.getConnections({source:id, scope:'*'}, true),
  4435. tConns = this.getConnections({target:id, scope:'*'}, true);
  4436. newId = "" + newId;
  4437. if (!doNotSetAttribute) {
  4438. el = this.getDOMElement(id);
  4439. this.setAttribute(el, "id", newId);
  4440. }
  4441. else
  4442. el = this.getDOMElement(newId);
  4443. endpointsByElement[newId] = endpointsByElement[id] || [];
  4444. for (var i = 0, ii = endpointsByElement[newId].length; i < ii; i++) {
  4445. endpointsByElement[newId][i].setElementId(newId);
  4446. endpointsByElement[newId][i].setReferenceElement(el);
  4447. }
  4448. delete endpointsByElement[id];
  4449. this.anchorManager.changeId(id, newId);
  4450. if (this.dragManager) this.dragManager.changeId(id, newId);
  4451. managedElements[newId] = managedElements[id];
  4452. delete managedElements[id];
  4453. var _conns = function(list, epIdx, type) {
  4454. for (var i = 0, ii = list.length; i < ii; i++) {
  4455. list[i].endpoints[epIdx].setElementId(newId);
  4456. list[i].endpoints[epIdx].setReferenceElement(el);
  4457. list[i][type + "Id"] = newId;
  4458. list[i][type] = el;
  4459. }
  4460. };
  4461. _conns(sConns, 0, "source");
  4462. _conns(tConns, 1, "target");
  4463. this.repaint(newId);
  4464. };
  4465. this.setDebugLog = function(debugLog) {
  4466. log = debugLog;
  4467. };
  4468. this.setSuspendDrawing = function(val, repaintAfterwards) {
  4469. var curVal = _suspendDrawing;
  4470. _suspendDrawing = val;
  4471. if (val) _suspendedAt = new Date().getTime(); else _suspendedAt = null;
  4472. if (repaintAfterwards) this.repaintEverything();
  4473. return curVal;
  4474. };
  4475. // returns whether or not drawing is currently suspended.
  4476. this.isSuspendDrawing = function() {
  4477. return _suspendDrawing;
  4478. };
  4479. // return timestamp for when drawing was suspended.
  4480. this.getSuspendedAt = function() { return _suspendedAt; };
  4481. this.doWhileSuspended = function(fn, doNotRepaintAfterwards) {
  4482. var _wasSuspended = this.isSuspendDrawing();
  4483. if (!_wasSuspended)
  4484. this.setSuspendDrawing(true);
  4485. try {
  4486. fn();
  4487. }
  4488. catch (e) {
  4489. _ju.log("Function run while suspended failed", e);
  4490. }
  4491. if (!_wasSuspended)
  4492. this.setSuspendDrawing(false, !doNotRepaintAfterwards);
  4493. };
  4494. this.getOffset = function(elId) { return offsets[elId]; };
  4495. this.getCachedData = _getCachedData;
  4496. this.timestamp = _timestamp;
  4497. this.setRenderMode = function(mode) {
  4498. if (mode !== jsPlumb.SVG && mode !== jsPlumb.VML) throw new TypeError("Render mode [" + mode + "] not supported");
  4499. renderMode = jsPlumbAdapter.setRenderMode(mode);
  4500. return renderMode;
  4501. };
  4502. this.getRenderMode = function() { return renderMode; };
  4503. this.show = function(el, changeEndpoints) {
  4504. _setVisible(el, "block", changeEndpoints);
  4505. return _currentInstance;
  4506. };
  4507. // TODO: update this method to return the current state.
  4508. this.toggleVisible = _toggleVisible;
  4509. this.toggleDraggable = _toggleDraggable;
  4510. this.addListener = this.bind;
  4511. if (!jsPlumbAdapter.headless) {
  4512. _currentInstance.dragManager = jsPlumbAdapter.getDragManager(_currentInstance);
  4513. _currentInstance.recalculateOffsets = _currentInstance.dragManager.updateOffsets;
  4514. }
  4515. };
  4516. jsPlumbUtil.extend(jsPlumbInstance, jsPlumbUtil.EventGenerator, {
  4517. setAttribute : function(el, a, v) {
  4518. this.setAttribute(el, a, v);
  4519. },
  4520. getAttribute : function(el, a) {
  4521. return this.getAttribute(jsPlumb.getDOMElement(el), a);
  4522. },
  4523. registerConnectionType : function(id, type) {
  4524. this._connectionTypes[id] = jsPlumb.extend({}, type);
  4525. },
  4526. registerConnectionTypes : function(types) {
  4527. for (var i in types)
  4528. this._connectionTypes[i] = jsPlumb.extend({}, types[i]);
  4529. },
  4530. registerEndpointType : function(id, type) {
  4531. this._endpointTypes[id] = jsPlumb.extend({}, type);
  4532. },
  4533. registerEndpointTypes : function(types) {
  4534. for (var i in types)
  4535. this._endpointTypes[i] = jsPlumb.extend({}, types[i]);
  4536. },
  4537. getType : function(id, typeDescriptor) {
  4538. return typeDescriptor === "connection" ? this._connectionTypes[id] : this._endpointTypes[id];
  4539. },
  4540. setIdChanged : function(oldId, newId) {
  4541. this.setId(oldId, newId, true);
  4542. },
  4543. // set parent: change the parent for some node and update all the registrations we need to.
  4544. setParent : function(el, newParent) {
  4545. var _el = this.getElementObject(el),
  4546. _dom = this.getDOMElement(_el),
  4547. _id = this.getId(_dom),
  4548. _pel = this.getElementObject(newParent),
  4549. _pdom = this.getDOMElement(_pel),
  4550. _pid = this.getId(_pdom);
  4551. _dom.parentNode.removeChild(_dom);
  4552. _pdom.appendChild(_dom);
  4553. this.dragManager.setParent(_el, _id, _pel, _pid);
  4554. },
  4555. /**
  4556. * gets the size for the element, in an array : [ width, height ].
  4557. */
  4558. getSize : function(el) {
  4559. return [ el.offsetWidth, el.offsetHeight ];
  4560. },
  4561. getWidth : function(el) {
  4562. return el.offsetWidth;
  4563. },
  4564. getHeight : function(el) {
  4565. return el.offsetHeight;
  4566. },
  4567. extend : function(o1, o2, names) {
  4568. var i;
  4569. if (names) {
  4570. for (i = 0; i < names.length; i++)
  4571. o1[names[i]] = o2[names[i]];
  4572. }
  4573. else
  4574. for (i in o2) o1[i] = o2[i];
  4575. return o1;
  4576. },
  4577. floatingConnections:{},
  4578. getFloatingAnchorIndex : function(jpc) {
  4579. return jpc.endpoints[0].isFloating() ? 0 : 1;
  4580. }
  4581. }, jsPlumbAdapter);
  4582. // --------------------- static instance + AMD registration -------------------------------------------
  4583. // create static instance and assign to window if window exists.
  4584. var jsPlumb = new jsPlumbInstance();
  4585. // register on window if defined (lets us run on server)
  4586. if (typeof window != 'undefined') window.jsPlumb = jsPlumb;
  4587. // add 'getInstance' method to static instance
  4588. jsPlumb.getInstance = function(_defaults) {
  4589. var j = new jsPlumbInstance(_defaults);
  4590. j.init();
  4591. return j;
  4592. };
  4593. // maybe register static instance as an AMD module, and getInstance method too.
  4594. if ( typeof define === "function") {
  4595. define( "jsplumb", [], function () { return jsPlumb; } );
  4596. define( "jsplumbinstance", [], function () { return jsPlumb.getInstance(); } );
  4597. }
  4598. // CommonJS
  4599. if (typeof exports !== 'undefined') {
  4600. exports.jsPlumb = jsPlumb;
  4601. }
  4602. // --------------------- end static instance + AMD registration -------------------------------------------
  4603. })();
  4604. /*
  4605. * jsPlumb
  4606. *
  4607. * Title:jsPlumb 1.7.2
  4608. *
  4609. * Provides a way to visually connect elements on an HTML page, using SVG or VML.
  4610. *
  4611. * This file contains the code for Endpoints.
  4612. *
  4613. * Copyright (c) 2010 - 2014 Simon Porritt (simon@jsplumbtoolkit.com)
  4614. *
  4615. * http://jsplumbtoolkit.com
  4616. * http://github.com/sporritt/jsplumb
  4617. *
  4618. * Dual licensed under the MIT and GPL2 licenses.
  4619. */
  4620. ;(function() {
  4621. "use strict";
  4622. // create the drag handler for a connection
  4623. var _makeConnectionDragHandler = function(placeholder, _jsPlumb) {
  4624. var stopped = false;
  4625. return {
  4626. drag : function() {
  4627. if (stopped) {
  4628. stopped = false;
  4629. return true;
  4630. }
  4631. var _ui = jsPlumb.getUIPosition(arguments, _jsPlumb.getZoom());
  4632. if (placeholder.element) {
  4633. jsPlumbAdapter.setPosition(placeholder.element, _ui);
  4634. _jsPlumb.repaint(placeholder.element, _ui);
  4635. }
  4636. },
  4637. stopDrag : function() {
  4638. stopped = true;
  4639. }
  4640. };
  4641. };
  4642. // creates a placeholder div for dragging purposes, adds it to the DOM, and pre-computes its offset.
  4643. var _makeDraggablePlaceholder = function(placeholder, _jsPlumb) {
  4644. var n = document.createElement("div");
  4645. n.style.position = "absolute";
  4646. var parent = _jsPlumb.getContainer() || document.body;
  4647. parent.appendChild(n);
  4648. var id = _jsPlumb.getId(n);
  4649. //_jsPlumb.updateOffset( { elId : id });
  4650. _jsPlumb.manage( id, n );
  4651. // create and assign an id, and initialize the offset.
  4652. placeholder.id = id;
  4653. placeholder.element = n;
  4654. };
  4655. // create a floating endpoint (for drag connections)
  4656. var _makeFloatingEndpoint = function(paintStyle, referenceAnchor, endpoint, referenceCanvas, sourceElement, _jsPlumb, _newEndpoint, scope) {
  4657. var floatingAnchor = new jsPlumb.FloatingAnchor( { reference : referenceAnchor, referenceCanvas : referenceCanvas, jsPlumbInstance:_jsPlumb });
  4658. //setting the scope here should not be the way to fix that mootools issue. it should be fixed by not
  4659. // adding the floating endpoint as a droppable. that makes more sense anyway!
  4660. return _newEndpoint({ paintStyle : paintStyle, endpoint : endpoint, anchor : floatingAnchor, source : sourceElement, scope:scope });
  4661. };
  4662. var typeParameters = [ "connectorStyle", "connectorHoverStyle", "connectorOverlays",
  4663. "connector", "connectionType", "connectorClass", "connectorHoverClass" ];
  4664. // a helper function that tries to find a connection to the given element, and returns it if so. if elementWithPrecedence is null,
  4665. // or no connection to it is found, we return the first connection in our list.
  4666. var findConnectionToUseForDynamicAnchor = function(ep, elementWithPrecedence) {
  4667. var idx = 0;
  4668. if (elementWithPrecedence != null) {
  4669. for (var i = 0; i < ep.connections.length; i++) {
  4670. if (ep.connections[i].sourceId == elementWithPrecedence || ep.connections[i].targetId == elementWithPrecedence) {
  4671. idx = i;
  4672. break;
  4673. }
  4674. }
  4675. }
  4676. return ep.connections[idx];
  4677. };
  4678. var findConnectionIndex = function(conn, ep) {
  4679. return jsPlumbUtil.findWithFunction(ep.connections, function(c) { return c.id == conn.id; });
  4680. };
  4681. jsPlumb.Endpoint = function(params) {
  4682. var _jsPlumb = params._jsPlumb,
  4683. _gel = jsPlumb.getElementObject,
  4684. _ju = jsPlumbUtil,
  4685. _newConnection = params.newConnection,
  4686. _newEndpoint = params.newEndpoint,
  4687. _finaliseConnection = params.finaliseConnection,
  4688. _fireMoveEvent = params.fireMoveEvent;
  4689. this.idPrefix = "_jsplumb_e_";
  4690. this.defaultLabelLocation = [ 0.5, 0.5 ];
  4691. this.defaultOverlayKeys = ["Overlays", "EndpointOverlays"];
  4692. OverlayCapableJsPlumbUIComponent.apply(this, arguments);
  4693. // TYPE
  4694. this.getDefaultType = function() {
  4695. return {
  4696. parameters:{},
  4697. scope:null,
  4698. maxConnections:this._jsPlumb.instance.Defaults.MaxConnections,
  4699. paintStyle:this._jsPlumb.instance.Defaults.EndpointStyle || jsPlumb.Defaults.EndpointStyle,
  4700. endpoint:this._jsPlumb.instance.Defaults.Endpoint || jsPlumb.Defaults.Endpoint,
  4701. hoverPaintStyle:this._jsPlumb.instance.Defaults.EndpointHoverStyle || jsPlumb.Defaults.EndpointHoverStyle,
  4702. overlays:this._jsPlumb.instance.Defaults.EndpointOverlays || jsPlumb.Defaults.EndpointOverlays,
  4703. connectorStyle:params.connectorStyle,
  4704. connectorHoverStyle:params.connectorHoverStyle,
  4705. connectorClass:params.connectorClass,
  4706. connectorHoverClass:params.connectorHoverClass,
  4707. connectorOverlays:params.connectorOverlays,
  4708. connector:params.connector,
  4709. connectorTooltip:params.connectorTooltip
  4710. };
  4711. };
  4712. // END TYPE
  4713. this._jsPlumb.enabled = !(params.enabled === false);
  4714. this._jsPlumb.visible = true;
  4715. this.element = jsPlumb.getDOMElement(params.source);
  4716. this._jsPlumb.uuid = params.uuid;
  4717. this._jsPlumb.floatingEndpoint = null;
  4718. var inPlaceCopy = null;
  4719. if (this._jsPlumb.uuid) params.endpointsByUUID[this._jsPlumb.uuid] = this;
  4720. this.elementId = params.elementId;
  4721. this._jsPlumb.connectionCost = params.connectionCost;
  4722. this._jsPlumb.connectionsDirected = params.connectionsDirected;
  4723. this._jsPlumb.currentAnchorClass = "";
  4724. this._jsPlumb.events = {};
  4725. var _updateAnchorClass = function() {
  4726. // stash old, get new
  4727. var oldAnchorClass = this._jsPlumb.currentAnchorClass;
  4728. this._jsPlumb.currentAnchorClass = this.anchor.getCssClass();
  4729. // add and remove at the same time to reduce the number of reflows.
  4730. jsPlumbAdapter.updateClasses(this.element, _jsPlumb.endpointAnchorClassPrefix + "_" + this._jsPlumb.currentAnchorClass, _jsPlumb.endpointAnchorClassPrefix + "_" + oldAnchorClass);
  4731. this.updateClasses(_jsPlumb.endpointAnchorClassPrefix + "_" + this._jsPlumb.currentAnchorClass, _jsPlumb.endpointAnchorClassPrefix + "_" + oldAnchorClass);
  4732. }.bind(this);
  4733. this.setAnchor = function(anchorParams, doNotRepaint) {
  4734. this._jsPlumb.instance.continuousAnchorFactory.clear(this.elementId);
  4735. this.anchor = this._jsPlumb.instance.makeAnchor(anchorParams, this.elementId, _jsPlumb);
  4736. _updateAnchorClass();
  4737. this.anchor.bind("anchorChanged", function(currentAnchor) {
  4738. this.fire("anchorChanged", {endpoint:this, anchor:currentAnchor});
  4739. _updateAnchorClass();
  4740. }.bind(this));
  4741. if (!doNotRepaint)
  4742. this._jsPlumb.instance.repaint(this.elementId);
  4743. return this;
  4744. };
  4745. var anchorParamsToUse = params.anchor ? params.anchor : params.anchors ? params.anchors : (_jsPlumb.Defaults.Anchor || "Top");
  4746. this.setAnchor(anchorParamsToUse, true);
  4747. var internalHover = function(state) {
  4748. if (this.connections.length > 0) {
  4749. for (var i = 0; i < this.connections.length; i++)
  4750. this.connections[i].setHover(state, false);
  4751. }
  4752. else
  4753. this.setHover(state);
  4754. }.bind(this);
  4755. this.bind("mouseover", function() { internalHover(true); });
  4756. this.bind("mouseout", function() { internalHover(false); });
  4757. // ANCHOR MANAGER
  4758. 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.
  4759. this._jsPlumb.instance.anchorManager.add(this, this.elementId);
  4760. this.setEndpoint = function(ep) {
  4761. if (this.endpoint != null) {
  4762. this.endpoint.cleanup();
  4763. this.endpoint.destroy();
  4764. }
  4765. var _e = function(t, p) {
  4766. var rm = _jsPlumb.getRenderMode();
  4767. if (jsPlumb.Endpoints[rm][t]) return new jsPlumb.Endpoints[rm][t](p);
  4768. if (!_jsPlumb.Defaults.DoNotThrowErrors)
  4769. throw { msg:"jsPlumb: unknown endpoint type '" + t + "'" };
  4770. };
  4771. var endpointArgs = {
  4772. _jsPlumb:this._jsPlumb.instance,
  4773. cssClass:params.cssClass,
  4774. container:params.container,
  4775. tooltip:params.tooltip,
  4776. connectorTooltip:params.connectorTooltip,
  4777. endpoint:this
  4778. };
  4779. if (_ju.isString(ep))
  4780. this.endpoint = _e(ep, endpointArgs);
  4781. else if (_ju.isArray(ep)) {
  4782. endpointArgs = _ju.merge(ep[1], endpointArgs);
  4783. this.endpoint = _e(ep[0], endpointArgs);
  4784. }
  4785. else {
  4786. this.endpoint = ep.clone();
  4787. }
  4788. // assign a clone function using a copy of endpointArgs. this is used when a drag starts: the endpoint that was dragged is cloned,
  4789. // and the clone is left in its place while the original one goes off on a magical journey.
  4790. // the copy is to get around a closure problem, in which endpointArgs ends up getting shared by
  4791. // the whole world.
  4792. //var argsForClone = jsPlumb.extend({}, endpointArgs);
  4793. this.endpoint.clone = function() {
  4794. // TODO this, and the code above, can be refactored to be more dry.
  4795. if (_ju.isString(ep))
  4796. return _e(ep, endpointArgs);
  4797. else if (_ju.isArray(ep)) {
  4798. endpointArgs = _ju.merge(ep[1], endpointArgs);
  4799. return _e(ep[0], endpointArgs);
  4800. }
  4801. }.bind(this);
  4802. this.type = this.endpoint.type;
  4803. };
  4804. this.setEndpoint(params.endpoint || _jsPlumb.Defaults.Endpoint || jsPlumb.Defaults.Endpoint || "Dot");
  4805. this.setPaintStyle(params.endpointStyle || params.paintStyle || params.style || _jsPlumb.Defaults.EndpointStyle || jsPlumb.Defaults.EndpointStyle, true);
  4806. this.setHoverPaintStyle(params.endpointHoverStyle || params.hoverPaintStyle || _jsPlumb.Defaults.EndpointHoverStyle || jsPlumb.Defaults.EndpointHoverStyle, true);
  4807. this._jsPlumb.paintStyleInUse = this.getPaintStyle();
  4808. jsPlumb.extend(this, params, typeParameters);
  4809. this.isSource = params.isSource || false;
  4810. this.isTemporarySource = params.isTemporarySource || false;
  4811. this.isTarget = params.isTarget || false;
  4812. this._jsPlumb.maxConnections = params.maxConnections || _jsPlumb.Defaults.MaxConnections; // maximum number of connections this endpoint can be the source of.
  4813. this.canvas = this.endpoint.canvas;
  4814. this.canvas._jsPlumb = this;
  4815. // add anchor class (need to do this on construction because we set anchor first)
  4816. this.addClass(_jsPlumb.endpointAnchorClassPrefix + "_" + this._jsPlumb.currentAnchorClass);
  4817. jsPlumbAdapter.addClass(this.element, _jsPlumb.endpointAnchorClassPrefix + "_" + this._jsPlumb.currentAnchorClass);
  4818. this.connections = params.connections || [];
  4819. this.connectorPointerEvents = params["connector-pointer-events"];
  4820. this.scope = params.scope || _jsPlumb.getDefaultScope();
  4821. this.timestamp = null;
  4822. this.reattachConnections = params.reattach || _jsPlumb.Defaults.ReattachConnections;
  4823. this.connectionsDetachable = _jsPlumb.Defaults.ConnectionsDetachable;
  4824. if (params.connectionsDetachable === false || params.detachable === false)
  4825. this.connectionsDetachable = false;
  4826. this.dragAllowedWhenFull = params.dragAllowedWhenFull !== false;
  4827. if (params.onMaxConnections)
  4828. this.bind("maxConnections", params.onMaxConnections);
  4829. //
  4830. // add a connection. not part of public API.
  4831. //
  4832. this.addConnection = function(connection) {
  4833. this.connections.push(connection);
  4834. this[(this.connections.length > 0 ? "add" : "remove") + "Class"](_jsPlumb.endpointConnectedClass);
  4835. this[(this.isFull() ? "add" : "remove") + "Class"](_jsPlumb.endpointFullClass);
  4836. };
  4837. this.detachFromConnection = function(connection, idx, doNotCleanup) {
  4838. idx = idx == null ? findConnectionIndex(connection, this) : idx;
  4839. if (idx >= 0) {
  4840. this.connections.splice(idx, 1);
  4841. this[(this.connections.length > 0 ? "add" : "remove") + "Class"](_jsPlumb.endpointConnectedClass);
  4842. this[(this.isFull() ? "add" : "remove") + "Class"](_jsPlumb.endpointFullClass);
  4843. }
  4844. if (!doNotCleanup && this._deleteOnDetach && this.connections.length === 0) {
  4845. _jsPlumb.deleteObject({
  4846. endpoint:this,
  4847. fireEvent:false,
  4848. deleteAttachedObjects:false
  4849. });
  4850. }
  4851. };
  4852. this.detach = function(connection, ignoreTarget, forceDetach, fireEvent, originalEvent, endpointBeingDeleted, connectionIndex) {
  4853. var idx = connectionIndex == null ? findConnectionIndex(connection, this) : connectionIndex,
  4854. actuallyDetached = false;
  4855. fireEvent = (fireEvent !== false);
  4856. if (idx >= 0) {
  4857. if (forceDetach || connection._forceDetach || (connection.isDetachable() && connection.isDetachAllowed(connection) && this.isDetachAllowed(connection) && _jsPlumb.checkCondition("beforeDetach", connection) )) {
  4858. _jsPlumb.deleteObject({
  4859. connection:connection,
  4860. fireEvent:(!ignoreTarget && fireEvent),
  4861. originalEvent:originalEvent,
  4862. deleteAttachedObjects:false
  4863. });
  4864. actuallyDetached = true;
  4865. }
  4866. }
  4867. return actuallyDetached;
  4868. };
  4869. this.detachAll = function(fireEvent, originalEvent) {
  4870. while (this.connections.length > 0) {
  4871. // TODO this could pass the index in to the detach method to save some time (index will always be zero in this while loop)
  4872. this.detach(this.connections[0], false, true, fireEvent !== false, originalEvent, this, 0);
  4873. }
  4874. return this;
  4875. };
  4876. this.detachFrom = function(targetEndpoint, fireEvent, originalEvent) {
  4877. var c = [];
  4878. for ( var i = 0; i < this.connections.length; i++) {
  4879. if (this.connections[i].endpoints[1] == targetEndpoint || this.connections[i].endpoints[0] == targetEndpoint) {
  4880. c.push(this.connections[i]);
  4881. }
  4882. }
  4883. for ( var j = 0; j < c.length; j++) {
  4884. this.detach(c[j], false, true, fireEvent, originalEvent);
  4885. }
  4886. return this;
  4887. };
  4888. this.getElement = function() {
  4889. return this.element;
  4890. };
  4891. this.setElement = function(el) {
  4892. var parentId = this._jsPlumb.instance.getId(el),
  4893. curId = this.elementId;
  4894. // remove the endpoint from the list for the current endpoint's element
  4895. _ju.removeWithFunction(params.endpointsByElement[this.elementId], function(e) {
  4896. return e.id == this.id;
  4897. }.bind(this));
  4898. this.element = jsPlumb.getDOMElement(el);
  4899. this.elementId = _jsPlumb.getId(this.element);
  4900. _jsPlumb.anchorManager.rehomeEndpoint(this, curId, this.element);
  4901. _jsPlumb.dragManager.endpointAdded(this.element);
  4902. _ju.addToList(params.endpointsByElement, parentId, this);
  4903. return this;
  4904. };
  4905. /**
  4906. * private but must be exposed.
  4907. */
  4908. this.makeInPlaceCopy = function() {
  4909. var loc = this.anchor.getCurrentLocation({element:this}),
  4910. o = this.anchor.getOrientation(this),
  4911. acc = this.anchor.getCssClass(),
  4912. inPlaceAnchor = {
  4913. bind:function() { },
  4914. compute:function() { return [ loc[0], loc[1] ]; },
  4915. getCurrentLocation : function() { return [ loc[0], loc[1] ]; },
  4916. getOrientation:function() { return o; },
  4917. getCssClass:function() { return acc; }
  4918. };
  4919. return _newEndpoint( {
  4920. dropOptions:params.dropOptions,
  4921. anchor : inPlaceAnchor,
  4922. source : this.element,
  4923. paintStyle : this.getPaintStyle(),
  4924. endpoint : params.hideOnDrag ? "Blank" : this.endpoint,
  4925. _transient:true,
  4926. scope:this.scope
  4927. });
  4928. };
  4929. /**
  4930. * returns a connection from the pool; used when dragging starts. just gets the head of the array if it can.
  4931. */
  4932. this.connectorSelector = function() {
  4933. var candidate = this.connections[0];
  4934. if (this.isTarget && candidate) return candidate;
  4935. else {
  4936. return (this.connections.length < this._jsPlumb.maxConnections) || this._jsPlumb.maxConnections == -1 ? null : candidate;
  4937. }
  4938. };
  4939. this.setStyle = this.setPaintStyle;
  4940. this.paint = function(params) {
  4941. params = params || {};
  4942. var timestamp = params.timestamp, recalc = !(params.recalc === false);
  4943. if (!timestamp || this.timestamp !== timestamp) {
  4944. var info = _jsPlumb.updateOffset({ elId:this.elementId, timestamp:timestamp });
  4945. var xy = params.offset ? params.offset.o : info.o;
  4946. if(xy != null) {
  4947. var ap = params.anchorPoint,connectorPaintStyle = params.connectorPaintStyle;
  4948. if (ap == null) {
  4949. var wh = params.dimensions || info.s,
  4950. anchorParams = { xy : [ xy.left, xy.top ], wh : wh, element : this, timestamp : timestamp };
  4951. if (recalc && this.anchor.isDynamic && this.connections.length > 0) {
  4952. var c = findConnectionToUseForDynamicAnchor(this, params.elementWithPrecedence),
  4953. oIdx = c.endpoints[0] == this ? 1 : 0,
  4954. oId = oIdx === 0 ? c.sourceId : c.targetId,
  4955. oInfo = _jsPlumb.getCachedData(oId),
  4956. oOffset = oInfo.o, oWH = oInfo.s;
  4957. anchorParams.txy = [ oOffset.left, oOffset.top ];
  4958. anchorParams.twh = oWH;
  4959. anchorParams.tElement = c.endpoints[oIdx];
  4960. }
  4961. ap = this.anchor.compute(anchorParams);
  4962. }
  4963. this.endpoint.compute(ap, this.anchor.getOrientation(this), this._jsPlumb.paintStyleInUse, connectorPaintStyle || this.paintStyleInUse);
  4964. this.endpoint.paint(this._jsPlumb.paintStyleInUse, this.anchor);
  4965. this.timestamp = timestamp;
  4966. // paint overlays
  4967. for ( var i = 0; i < this._jsPlumb.overlays.length; i++) {
  4968. var o = this._jsPlumb.overlays[i];
  4969. if (o.isVisible()) {
  4970. this._jsPlumb.overlayPlacements[i] = o.draw(this.endpoint, this._jsPlumb.paintStyleInUse);
  4971. o.paint(this._jsPlumb.overlayPlacements[i]);
  4972. }
  4973. }
  4974. }
  4975. }
  4976. };
  4977. this.repaint = this.paint;
  4978. var draggingInitialised = false;
  4979. this.initDraggable = function() {
  4980. // is this a connection source? we make it draggable and have the
  4981. // drag listener maintain a connection with a floating endpoint.
  4982. if (!draggingInitialised && jsPlumb.isDragSupported(this.element)) {
  4983. var placeholderInfo = { id:null, element:null },
  4984. jpc = null,
  4985. existingJpc = false,
  4986. existingJpcParams = null,
  4987. _dragHandler = _makeConnectionDragHandler(placeholderInfo, _jsPlumb),
  4988. dragOptions = params.dragOptions || {},
  4989. defaultOpts = {},
  4990. startEvent = jsPlumb.dragEvents.start,
  4991. stopEvent = jsPlumb.dragEvents.stop,
  4992. dragEvent = jsPlumb.dragEvents.drag;
  4993. var start = function() {
  4994. // drag might have started on an endpoint that is not actually a source, but which has
  4995. // one or more connections.
  4996. jpc = this.connectorSelector();
  4997. var _continue = true;
  4998. // if not enabled, return
  4999. if (!this.isEnabled()) _continue = false;
  5000. // if no connection and we're not a source - or temporarily a source, as is the case with makeSource - return.
  5001. if (jpc == null && !this.isSource && !this.isTemporarySource) _continue = false;
  5002. // otherwise if we're full and not allowed to drag, also return false.
  5003. if (this.isSource && this.isFull() && !this.dragAllowedWhenFull) _continue = false;
  5004. // if the connection was setup as not detachable or one of its endpoints
  5005. // was setup as connectionsDetachable = false, or Defaults.ConnectionsDetachable
  5006. // is set to false...
  5007. if (jpc != null && !jpc.isDetachable()) _continue = false;
  5008. if (_continue === false) {
  5009. // this is for mootools and yui. returning false from this causes jquery to stop drag.
  5010. // the events are wrapped in both mootools and yui anyway, but i don't think returning
  5011. // false from the start callback would stop a drag.
  5012. if (_jsPlumb.stopDrag) _jsPlumb.stopDrag(this.canvas);
  5013. _dragHandler.stopDrag();
  5014. return false;
  5015. }
  5016. // clear hover for all connections for this endpoint before continuing.
  5017. for (var i = 0; i < this.connections.length; i++)
  5018. this.connections[i].setHover(false);
  5019. this.addClass("endpointDrag");
  5020. _jsPlumb.setConnectionBeingDragged(true);
  5021. // if we're not full but there was a connection, make it null. we'll create a new one.
  5022. if (jpc && !this.isFull() && this.isSource) jpc = null;
  5023. _jsPlumb.updateOffset( { elId : this.elementId });
  5024. inPlaceCopy = this.makeInPlaceCopy();
  5025. inPlaceCopy.referenceEndpoint = this;
  5026. inPlaceCopy.paint();
  5027. _makeDraggablePlaceholder(placeholderInfo, _jsPlumb);
  5028. // set the offset of this div to be where 'inPlaceCopy' is, to start with.
  5029. // TODO merge this code with the code in both Anchor and FloatingAnchor, because it
  5030. // does the same stuff.
  5031. var ipcoel = _gel(inPlaceCopy.canvas),
  5032. ipco = jsPlumbAdapter.getOffset(ipcoel, this._jsPlumb.instance),
  5033. canvasElement = _gel(this.canvas);
  5034. jsPlumbAdapter.setPosition(placeholderInfo.element, ipco);
  5035. // when using makeSource and a parent, we first draw the source anchor on the source element, then
  5036. // move it to the parent. note that this happens after drawing the placeholder for the
  5037. // first time.
  5038. if (this.parentAnchor) this.anchor = _jsPlumb.makeAnchor(this.parentAnchor, this.elementId, _jsPlumb);
  5039. // store the id of the dragging div and the source element. the drop function will pick these up.
  5040. _jsPlumb.setAttribute(this.canvas, "dragId", placeholderInfo.id);
  5041. _jsPlumb.setAttribute(this.canvas, "elId", this.elementId);
  5042. this._jsPlumb.floatingEndpoint = _makeFloatingEndpoint(this.getPaintStyle(), this.anchor, this.endpoint, this.canvas, placeholderInfo.element, _jsPlumb, _newEndpoint, this.scope);
  5043. // TODO we should not know about DOM here. make the library adapter do this (or the
  5044. // dom adapter)
  5045. this.canvas.style.visibility = "hidden";
  5046. if (jpc == null) {
  5047. this.anchor.locked = true;
  5048. this.setHover(false, false);
  5049. // create a connection. one end is this endpoint, the other is a floating endpoint.
  5050. jpc = _newConnection({
  5051. sourceEndpoint : this,
  5052. targetEndpoint : this._jsPlumb.floatingEndpoint,
  5053. source : this.endpointWillMoveTo || this.element, // for makeSource with parent option. ensure source element is represented correctly.
  5054. target : placeholderInfo.element,
  5055. anchors : [ this.anchor, this._jsPlumb.floatingEndpoint.anchor ],
  5056. paintStyle : params.connectorStyle, // this can be null. Connection will use the default.
  5057. hoverPaintStyle:params.connectorHoverStyle,
  5058. connector : params.connector, // this can also be null. Connection will use the default.
  5059. overlays : params.connectorOverlays,
  5060. type:this.connectionType,
  5061. cssClass:this.connectorClass,
  5062. hoverClass:this.connectorHoverClass
  5063. });
  5064. //jpc.pending = true; // mark this connection as not having been established.
  5065. jpc.addClass(_jsPlumb.draggingClass);
  5066. this._jsPlumb.floatingEndpoint.addClass(_jsPlumb.draggingClass);
  5067. // fire an event that informs that a connection is being dragged
  5068. _jsPlumb.fire("connectionDrag", jpc);
  5069. } else {
  5070. existingJpc = true;
  5071. jpc.setHover(false);
  5072. // new anchor idx
  5073. var anchorIdx = jpc.endpoints[0].id == this.id ? 0 : 1;
  5074. this.detachFromConnection(jpc, null, true); // detach from the connection while dragging is occurring. but dont cleanup automatically.
  5075. // store the original scope (issue 57)
  5076. var dragScope = _jsPlumb.getDragScope(canvasElement);
  5077. _jsPlumb.setAttribute(this.canvas, "originalScope", dragScope);
  5078. // now we want to get this endpoint's DROP scope, and set it for now: we can only be dropped on drop zones
  5079. // that have our drop scope (issue 57).
  5080. var dropScope = _jsPlumb.getDropScope(canvasElement);
  5081. _jsPlumb.setDragScope(canvasElement, dropScope);
  5082. //*/
  5083. // fire an event that informs that a connection is being dragged. we do this before
  5084. // replacing the original target with the floating element info.
  5085. _jsPlumb.fire("connectionDrag", jpc);
  5086. // now we replace ourselves with the temporary div we created above:
  5087. if (anchorIdx === 0) {
  5088. existingJpcParams = [ jpc.source, jpc.sourceId, canvasElement, dragScope ];
  5089. jpc.source = placeholderInfo.element;
  5090. jpc.sourceId = placeholderInfo.id;
  5091. } else {
  5092. existingJpcParams = [ jpc.target, jpc.targetId, canvasElement, dragScope ];
  5093. jpc.target = placeholderInfo.element;
  5094. jpc.targetId = placeholderInfo.id;
  5095. }
  5096. // lock the other endpoint; if it is dynamic it will not move while the drag is occurring.
  5097. jpc.endpoints[anchorIdx === 0 ? 1 : 0].anchor.locked = true;
  5098. // store the original endpoint and assign the new floating endpoint for the drag.
  5099. jpc.suspendedEndpoint = jpc.endpoints[anchorIdx];
  5100. // PROVIDE THE SUSPENDED ELEMENT, BE IT A SOURCE OR TARGET (ISSUE 39)
  5101. jpc.suspendedElement = jpc.endpoints[anchorIdx].getElement();
  5102. jpc.suspendedElementId = jpc.endpoints[anchorIdx].elementId;
  5103. jpc.suspendedElementType = anchorIdx === 0 ? "source" : "target";
  5104. jpc.suspendedEndpoint.setHover(false);
  5105. this._jsPlumb.floatingEndpoint.referenceEndpoint = jpc.suspendedEndpoint;
  5106. jpc.endpoints[anchorIdx] = this._jsPlumb.floatingEndpoint;
  5107. jpc.addClass(_jsPlumb.draggingClass);
  5108. this._jsPlumb.floatingEndpoint.addClass(_jsPlumb.draggingClass);
  5109. }
  5110. // register it and register connection on it.
  5111. _jsPlumb.floatingConnections[placeholderInfo.id] = jpc;
  5112. _jsPlumb.anchorManager.addFloatingConnection(placeholderInfo.id, jpc);
  5113. // only register for the target endpoint; we will not be dragging the source at any time
  5114. // before this connection is either discarded or made into a permanent connection.
  5115. _ju.addToList(params.endpointsByElement, placeholderInfo.id, this._jsPlumb.floatingEndpoint);
  5116. // tell jsplumb about it
  5117. _jsPlumb.currentlyDragging = true;
  5118. }.bind(this);
  5119. var stop = function() {
  5120. _jsPlumb.setConnectionBeingDragged(false);
  5121. // if no endpoints, jpc already cleaned up.
  5122. if (jpc && jpc.endpoints != null) {
  5123. // get the actual drop event (decode from library args to stop function)
  5124. var originalEvent = _jsPlumb.getDropEvent(arguments);
  5125. // unlock the other endpoint (if it is dynamic, it would have been locked at drag start)
  5126. var idx = _jsPlumb.getFloatingAnchorIndex(jpc);
  5127. jpc.endpoints[idx === 0 ? 1 : 0].anchor.locked = false;
  5128. // TODO: Dont want to know about css classes inside jsplumb, ideally.
  5129. jpc.removeClass(_jsPlumb.draggingClass);
  5130. // if we have the floating endpoint then the connection has not been dropped
  5131. // on another endpoint. If it is a new connection we throw it away. If it is an
  5132. // existing connection we check to see if we should reattach it, throwing it away
  5133. // if not.
  5134. if (this._jsPlumb && (jpc.deleteConnectionNow || jpc.endpoints[idx] == this._jsPlumb.floatingEndpoint)) {
  5135. // 6a. if the connection was an existing one...
  5136. if (existingJpc && jpc.suspendedEndpoint) {
  5137. // fix for issue35, thanks Sylvain Gizard: when firing the detach event make sure the
  5138. // floating endpoint has been replaced.
  5139. if (idx === 0) {
  5140. jpc.source = existingJpcParams[0];
  5141. jpc.sourceId = existingJpcParams[1];
  5142. } else {
  5143. jpc.target = existingJpcParams[0];
  5144. jpc.targetId = existingJpcParams[1];
  5145. }
  5146. var fe = this._jsPlumb.floatingEndpoint; // store for later removal.
  5147. // restore the original scope (issue 57)
  5148. _jsPlumb.setDragScope(existingJpcParams[2], existingJpcParams[3]);
  5149. jpc.endpoints[idx] = jpc.suspendedEndpoint;
  5150. // IF the connection should be reattached, or the other endpoint refuses detach, then
  5151. // reset the connection to its original state
  5152. if (jpc.isReattach() || jpc._forceReattach || jpc._forceDetach || !jpc.endpoints[idx === 0 ? 1 : 0].detach(jpc, false, false, true, originalEvent)) {
  5153. jpc.setHover(false);
  5154. jpc._forceDetach = null;
  5155. jpc._forceReattach = null;
  5156. this._jsPlumb.floatingEndpoint.detachFromConnection(jpc);
  5157. jpc.suspendedEndpoint.addConnection(jpc);
  5158. _jsPlumb.repaint(existingJpcParams[1]);
  5159. }
  5160. else
  5161. _jsPlumb.deleteObject({endpoint:fe});
  5162. }
  5163. }
  5164. // remove the element associated with the floating endpoint
  5165. // (and its associated floating endpoint and visual artefacts)
  5166. _jsPlumb.remove(placeholderInfo.element, false);
  5167. // remove the inplace copy
  5168. _jsPlumb.deleteObject({endpoint:inPlaceCopy});
  5169. // makeTargets sets this flag, to tell us we have been replaced and should delete ourself.
  5170. if (this.deleteAfterDragStop) {
  5171. _jsPlumb.deleteObject({endpoint:this});
  5172. }
  5173. else {
  5174. if (this._jsPlumb) {
  5175. this._jsPlumb.floatingEndpoint = null;
  5176. // repaint this endpoint.
  5177. // make our canvas visible (TODO: hand off to library; we should not know about DOM)
  5178. this.canvas.style.visibility = "visible";
  5179. // unlock our anchor
  5180. this.anchor.locked = false;
  5181. this.paint({recalc:false});
  5182. }
  5183. }
  5184. // although the connection is no longer valid, there are use cases where this is useful.
  5185. _jsPlumb.fire("connectionDragStop", jpc, originalEvent);
  5186. // tell jsplumb that dragging is finished.
  5187. _jsPlumb.currentlyDragging = false;
  5188. jpc = null;
  5189. }
  5190. }.bind(this);
  5191. dragOptions = jsPlumb.extend(defaultOpts, dragOptions);
  5192. dragOptions.scope = this.scope || dragOptions.scope;
  5193. dragOptions[startEvent] = _ju.wrap(dragOptions[startEvent], start, false);
  5194. // extracted drag handler function so can be used by makeSource
  5195. dragOptions[dragEvent] = _ju.wrap(dragOptions[dragEvent], _dragHandler.drag);
  5196. dragOptions[stopEvent] = _ju.wrap(dragOptions[stopEvent], stop);
  5197. dragOptions.canDrag = function() {
  5198. return this.isSource || this.isTemporarySource || (this.isTarget && this.connections.length > 0);
  5199. }.bind(this);
  5200. _jsPlumb.initDraggable(this.canvas, dragOptions, "internal");
  5201. this.canvas._jsPlumbRelatedElement = this.element;
  5202. draggingInitialised = true;
  5203. }
  5204. };
  5205. // if marked as source or target at create time, init the dragging.
  5206. if (this.isSource || this.isTarget || this.isTemporarySource)
  5207. this.initDraggable();
  5208. // pulled this out into a function so we can reuse it for the inPlaceCopy canvas; you can now drop detached connections
  5209. // back onto the endpoint you detached it from.
  5210. var _initDropTarget = function(canvas, forceInit, isTransient, endpoint) {
  5211. if ((this.isTarget || forceInit) && jsPlumb.isDropSupported(this.element)) {
  5212. var dropOptions = params.dropOptions || _jsPlumb.Defaults.DropOptions || jsPlumb.Defaults.DropOptions;
  5213. dropOptions = jsPlumb.extend( {}, dropOptions);
  5214. dropOptions.scope = dropOptions.scope || this.scope;
  5215. var dropEvent = jsPlumb.dragEvents.drop,
  5216. overEvent = jsPlumb.dragEvents.over,
  5217. outEvent = jsPlumb.dragEvents.out,
  5218. _ep = this,
  5219. drop = _jsPlumb.EndpointDropHandler({
  5220. getEndpoint:function() { return _ep; },
  5221. jsPlumb:_jsPlumb,
  5222. enabled:function() {
  5223. return endpoint != null ? endpoint.isEnabled() : true;
  5224. },
  5225. isFull:function() {
  5226. return endpoint.isFull();
  5227. },
  5228. element:this.element,
  5229. elementId:this.elementId,
  5230. isSource:this.isSource,
  5231. isTarget:this.isTarget,
  5232. addClass:function(clazz) {
  5233. _ep.addClass(clazz);
  5234. },
  5235. removeClass:function(clazz) {
  5236. _ep.removeClass(clazz);
  5237. },
  5238. isDropAllowed:function() {
  5239. return _ep.isDropAllowed.apply(_ep, arguments);
  5240. }
  5241. });
  5242. dropOptions[dropEvent] = _ju.wrap(dropOptions[dropEvent], drop);
  5243. dropOptions[overEvent] = _ju.wrap(dropOptions[overEvent], function() {
  5244. var draggable = jsPlumb.getDragObject(arguments),
  5245. id = _jsPlumb.getAttribute(jsPlumb.getDOMElement(draggable), "dragId"),
  5246. _jpc = _jsPlumb.floatingConnections[id];
  5247. if (_jpc != null) {
  5248. var idx = _jsPlumb.getFloatingAnchorIndex(_jpc);
  5249. // here we should fire the 'over' event if we are a target and this is a new connection,
  5250. // or we are the same as the floating endpoint.
  5251. var _cont = (this.isTarget && idx !== 0) || (_jpc.suspendedEndpoint && this.referenceEndpoint && this.referenceEndpoint.id == _jpc.suspendedEndpoint.id);
  5252. if (_cont) {
  5253. var bb = _jsPlumb.checkCondition("checkDropAllowed", {
  5254. sourceEndpoint:_jpc.endpoints[idx],
  5255. targetEndpoint:this,
  5256. connection:_jpc
  5257. });
  5258. this[(bb ? "add" : "remove") + "Class"](_jsPlumb.endpointDropAllowedClass);
  5259. this[(bb ? "remove" : "add") + "Class"](_jsPlumb.endpointDropForbiddenClass);
  5260. _jpc.endpoints[idx].anchor.over(this.anchor, this);
  5261. }
  5262. }
  5263. }.bind(this));
  5264. dropOptions[outEvent] = _ju.wrap(dropOptions[outEvent], function() {
  5265. var draggable = jsPlumb.getDragObject(arguments),
  5266. id = draggable == null ? null : _jsPlumb.getAttribute( jsPlumb.getDOMElement(draggable), "dragId"),
  5267. _jpc = id? _jsPlumb.floatingConnections[id] : null;
  5268. if (_jpc != null) {
  5269. var idx = _jsPlumb.getFloatingAnchorIndex(_jpc);
  5270. var _cont = (this.isTarget && idx !== 0) || (_jpc.suspendedEndpoint && this.referenceEndpoint && this.referenceEndpoint.id == _jpc.suspendedEndpoint.id);
  5271. if (_cont) {
  5272. this.removeClass(_jsPlumb.endpointDropAllowedClass);
  5273. this.removeClass(_jsPlumb.endpointDropForbiddenClass);
  5274. _jpc.endpoints[idx].anchor.out();
  5275. }
  5276. }
  5277. }.bind(this));
  5278. _jsPlumb.initDroppable(canvas, dropOptions, "internal", isTransient);
  5279. }
  5280. }.bind(this);
  5281. // initialise the endpoint's canvas as a drop target. this will be ignored if the endpoint is not a target or drag is not supported.
  5282. if (!this.anchor.isFloating)
  5283. _initDropTarget(_gel(this.canvas), true, !(params._transient || this.anchor.isFloating), this);
  5284. // finally, set type if it was provided
  5285. if (params.type)
  5286. this.addType(params.type, params.data, _jsPlumb.isSuspendDrawing());
  5287. return this;
  5288. };
  5289. jsPlumbUtil.extend(jsPlumb.Endpoint, OverlayCapableJsPlumbUIComponent, {
  5290. getTypeDescriptor : function() { return "endpoint"; },
  5291. isVisible : function() { return this._jsPlumb.visible; },
  5292. setVisible : function(v, doNotChangeConnections, doNotNotifyOtherEndpoint) {
  5293. this._jsPlumb.visible = v;
  5294. if (this.canvas) this.canvas.style.display = v ? "block" : "none";
  5295. this[v ? "showOverlays" : "hideOverlays"]();
  5296. if (!doNotChangeConnections) {
  5297. for (var i = 0; i < this.connections.length; i++) {
  5298. this.connections[i].setVisible(v);
  5299. if (!doNotNotifyOtherEndpoint) {
  5300. var oIdx = this === this.connections[i].endpoints[0] ? 1 : 0;
  5301. // only change the other endpoint if this is its only connection.
  5302. if (this.connections[i].endpoints[oIdx].connections.length == 1) this.connections[i].endpoints[oIdx].setVisible(v, true, true);
  5303. }
  5304. }
  5305. }
  5306. },
  5307. getAttachedElements : function() {
  5308. return this.connections;
  5309. },
  5310. applyType : function(t) {
  5311. this.setPaintStyle(t.endpointStyle || t.paintStyle);
  5312. this.setHoverPaintStyle(t.endpointHoverStyle || t.hoverPaintStyle);
  5313. if (t.maxConnections != null) this._jsPlumb.maxConnections = t.maxConnections;
  5314. if (t.scope) this.scope = t.scope;
  5315. jsPlumb.extend(this, t, typeParameters);
  5316. if (t.anchor) {
  5317. this.anchor = this._jsPlumb.instance.makeAnchor(t.anchor);
  5318. }
  5319. if (t.cssClass != null && this.canvas) this._jsPlumb.instance.addClass(this.canvas, t.cssClass);
  5320. },
  5321. isEnabled : function() { return this._jsPlumb.enabled; },
  5322. setEnabled : function(e) { this._jsPlumb.enabled = e; },
  5323. cleanup : function() {
  5324. jsPlumbAdapter.removeClass(this.element, this._jsPlumb.instance.endpointAnchorClassPrefix + "_" + this._jsPlumb.currentAnchorClass);
  5325. this.anchor = null;
  5326. this.endpoint.cleanup();
  5327. this.endpoint.destroy();
  5328. this.endpoint = null;
  5329. // drag/drop
  5330. var i = jsPlumb.getElementObject(this.canvas);
  5331. this._jsPlumb.instance.destroyDraggable(i, "internal");
  5332. this._jsPlumb.instance.destroyDroppable(i, "internal");
  5333. },
  5334. setHover : function(h) {
  5335. if (this.endpoint && this._jsPlumb && !this._jsPlumb.instance.isConnectionBeingDragged())
  5336. this.endpoint.setHover(h);
  5337. },
  5338. isFull : function() {
  5339. return !(this.isFloating() || this._jsPlumb.maxConnections < 1 || this.connections.length < this._jsPlumb.maxConnections);
  5340. },
  5341. /**
  5342. * private but needs to be exposed.
  5343. */
  5344. isFloating : function() {
  5345. return this.anchor != null && this.anchor.isFloating;
  5346. },
  5347. isConnectedTo : function(endpoint) {
  5348. var found = false;
  5349. if (endpoint) {
  5350. for ( var i = 0; i < this.connections.length; i++) {
  5351. if (this.connections[i].endpoints[1] == endpoint || this.connections[i].endpoints[0] == endpoint) {
  5352. found = true;
  5353. break;
  5354. }
  5355. }
  5356. }
  5357. return found;
  5358. },
  5359. getConnectionCost : function() { return this._jsPlumb.connectionCost; },
  5360. setConnectionCost : function(c) {
  5361. this._jsPlumb.connectionCost = c;
  5362. },
  5363. areConnectionsDirected : function() { return this._jsPlumb.connectionsDirected; },
  5364. setConnectionsDirected : function(b) { this._jsPlumb.connectionsDirected = b; },
  5365. setElementId : function(_elId) {
  5366. this.elementId = _elId;
  5367. this.anchor.elementId = _elId;
  5368. },
  5369. setReferenceElement : function(_el) {
  5370. this.element = jsPlumb.getDOMElement(_el);
  5371. },
  5372. setDragAllowedWhenFull : function(allowed) {
  5373. this.dragAllowedWhenFull = allowed;
  5374. },
  5375. equals : function(endpoint) {
  5376. return this.anchor.equals(endpoint.anchor);
  5377. },
  5378. getUuid : function() {
  5379. return this._jsPlumb.uuid;
  5380. },
  5381. computeAnchor : function(params) {
  5382. return this.anchor.compute(params);
  5383. }
  5384. });
  5385. jsPlumbInstance.prototype.EndpointDropHandler = function(dhParams) {
  5386. return function(e) {
  5387. var _jsPlumb = dhParams.jsPlumb;
  5388. // remove the classes that are added dynamically. drop is neither forbidden nor allowed now that
  5389. // the drop is finishing.
  5390. // makeTarget:probably keep these. 'this' would refer to the DOM element though
  5391. dhParams.removeClass(_jsPlumb.endpointDropAllowedClass);
  5392. dhParams.removeClass(_jsPlumb.endpointDropForbiddenClass);
  5393. var originalEvent = _jsPlumb.getDropEvent(arguments),
  5394. draggable = _jsPlumb.getDOMElement(_jsPlumb.getDragObject(arguments)),
  5395. id = _jsPlumb.getAttribute(draggable, "dragId"),
  5396. elId = _jsPlumb.getAttribute(draggable, "elId"),
  5397. scope = _jsPlumb.getAttribute(draggable, "originalScope"),
  5398. jpc = _jsPlumb.floatingConnections[id];
  5399. if (jpc == null) return;
  5400. var _ep = dhParams.getEndpoint(jpc);
  5401. if (dhParams.onDrop) dhParams.onDrop(jpc);
  5402. // if this is a drop back where the connection came from, mark it force rettach and
  5403. // return; the stop handler will reattach. without firing an event.
  5404. var redrop = jpc.suspendedEndpoint && (jpc.suspendedEndpoint.id == _ep.id ||
  5405. _ep.referenceEndpoint && jpc.suspendedEndpoint.id == _ep.referenceEndpoint.id) ;
  5406. if (redrop) {
  5407. jpc._forceReattach = true;
  5408. jpc.setHover(false);
  5409. return;
  5410. }
  5411. var idx = _jsPlumb.getFloatingAnchorIndex(jpc);
  5412. // restore the original scope if necessary (issue 57)
  5413. if (scope) _jsPlumb.setDragScope(draggable, scope);
  5414. // if the target of the drop is full, fire an event (we abort below)
  5415. // makeTarget: keep.
  5416. if (dhParams.isFull(e)) {
  5417. _ep.fire("maxConnections", {
  5418. endpoint:this,
  5419. connection:jpc,
  5420. maxConnections:_ep._jsPlumb.maxConnections
  5421. }, originalEvent);
  5422. }
  5423. if (!dhParams.isFull() && !(idx === 0 && !dhParams.isSource) && !(idx == 1 && !dhParams.isTarget) && dhParams.enabled()) {
  5424. var _doContinue = true;
  5425. // if this is an existing connection and detach is not allowed we won't continue. The connection's
  5426. // endpoints have been reinstated; everything is back to how it was.
  5427. if (jpc.suspendedEndpoint && jpc.suspendedEndpoint.id != _ep.id) {
  5428. if (!jpc.isDetachAllowed(jpc) || !jpc.endpoints[idx].isDetachAllowed(jpc) || !jpc.suspendedEndpoint.isDetachAllowed(jpc) || !_jsPlumb.checkCondition("beforeDetach", jpc))
  5429. _doContinue = false;
  5430. }
  5431. // these have to be set before testing for beforeDrop.
  5432. if (idx === 0) {
  5433. jpc.source = dhParams.element;
  5434. jpc.sourceId = dhParams.elementId;
  5435. } else {
  5436. jpc.target = dhParams.element;
  5437. jpc.targetId = dhParams.elementId;
  5438. }
  5439. // ------------ wrap the execution path in a function so we can support asynchronous beforeDrop
  5440. var continueFunction = function() {
  5441. // remove this jpc from the current endpoint, which is a floating endpoint that we will
  5442. // subsequently discard.
  5443. jpc.endpoints[idx].detachFromConnection(jpc);
  5444. // if there's a suspended endpoint, detach it from the connection.
  5445. if (jpc.suspendedEndpoint) jpc.suspendedEndpoint.detachFromConnection(jpc);
  5446. // TODO why?
  5447. jpc.endpoints[idx] = _ep;
  5448. _ep.addConnection(jpc);
  5449. // copy our parameters in to the connection:
  5450. var params = _ep.getParameters();
  5451. for (var aParam in params)
  5452. jpc.setParameter(aParam, params[aParam]);
  5453. if (!jpc.suspendedEndpoint) {
  5454. // if not an existing connection and
  5455. if (params.draggable)
  5456. _jsPlumb.initDraggable(this.element, dragOptions, "internal", _jsPlumb);
  5457. }
  5458. else {
  5459. var suspendedElementId = jpc.suspendedEndpoint.elementId;
  5460. _jsPlumb.fireMoveEvent({
  5461. index:idx,
  5462. originalSourceId:idx === 0 ? suspendedElementId : jpc.sourceId,
  5463. newSourceId:idx === 0 ? _ep.elementId : jpc.sourceId,
  5464. originalTargetId:idx == 1 ? suspendedElementId : jpc.targetId,
  5465. newTargetId:idx == 1 ? _ep.elementId : jpc.targetId,
  5466. originalSourceEndpoint:idx === 0 ? jpc.suspendedEndpoint : jpc.endpoints[0],
  5467. newSourceEndpoint:idx === 0 ? _ep : jpc.endpoints[0],
  5468. originalTargetEndpoint:idx == 1 ? jpc.suspendedEndpoint : jpc.endpoints[1],
  5469. newTargetEndpoint:idx == 1 ? _ep : jpc.endpoints[1],
  5470. connection:jpc
  5471. }, originalEvent);
  5472. }
  5473. if (idx == 1)
  5474. _jsPlumb.anchorManager.updateOtherEndpoint(jpc.sourceId, jpc.suspendedElementId, jpc.targetId, jpc);
  5475. else
  5476. _jsPlumb.anchorManager.sourceChanged(jpc.suspendedEndpoint.elementId, jpc.sourceId, jpc);
  5477. // when makeSource has uniqueEndpoint:true, we want to create connections with new endpoints
  5478. // that are subsequently deleted. So makeSource sets `finalEndpoint`, which is the Endpoint to
  5479. // which the connection should be attached. The `detachFromConnection` call below results in the
  5480. // temporary endpoint being cleaned up.
  5481. if (jpc.endpoints[0].finalEndpoint) {
  5482. var _toDelete = jpc.endpoints[0];
  5483. _toDelete.detachFromConnection(jpc);
  5484. jpc.endpoints[0] = jpc.endpoints[0].finalEndpoint;
  5485. jpc.endpoints[0].addConnection(jpc);
  5486. }
  5487. // finalise will inform the anchor manager and also add to
  5488. // connectionsByScope if necessary.
  5489. // TODO if this is not set to true, then dragging a connection's target to a new
  5490. // target causes the connection to be forgotten. however if it IS set to true, then
  5491. // the opposite happens: dragging by source causes the connection to get forgotten
  5492. // about and then if you delete it jsplumb breaks.
  5493. _jsPlumb.finaliseConnection(jpc, null, originalEvent/*, true*/);
  5494. jpc.setHover(false);
  5495. }.bind(this);
  5496. var dontContinueFunction = function() {
  5497. // otherwise just put it back on the endpoint it was on before the drag.
  5498. if (jpc.suspendedEndpoint) {
  5499. jpc.endpoints[idx] = jpc.suspendedEndpoint;
  5500. jpc.setHover(false);
  5501. jpc._forceDetach = true;
  5502. if (idx === 0) {
  5503. jpc.source = jpc.suspendedEndpoint.element;
  5504. jpc.sourceId = jpc.suspendedEndpoint.elementId;
  5505. } else {
  5506. jpc.target = jpc.suspendedEndpoint.element;
  5507. jpc.targetId = jpc.suspendedEndpoint.elementId;
  5508. }
  5509. jpc.suspendedEndpoint.addConnection(jpc);
  5510. _jsPlumb.repaint(jpc.sourceId);
  5511. jpc._forceDetach = false;
  5512. }
  5513. };
  5514. // --------------------------------------
  5515. // now check beforeDrop. this will be available only on Endpoints that are setup to
  5516. // have a beforeDrop condition (although, secretly, under the hood all Endpoints and
  5517. // the Connection have them, because they are on jsPlumbUIComponent. shhh!), because
  5518. // it only makes sense to have it on a target endpoint.
  5519. _doContinue = _doContinue && dhParams.isDropAllowed(jpc.sourceId, jpc.targetId, jpc.scope, jpc, _ep);// && jpc.pending;
  5520. if (_doContinue) {
  5521. continueFunction();
  5522. }
  5523. else {
  5524. dontContinueFunction();
  5525. }
  5526. }
  5527. _jsPlumb.currentlyDragging = false;
  5528. };
  5529. };
  5530. })();
  5531. /*
  5532. * jsPlumb
  5533. *
  5534. * Title:jsPlumb 1.7.2
  5535. *
  5536. * Provides a way to visually connect elements on an HTML page, using SVG or VML.
  5537. *
  5538. * This file contains the code for Connections.
  5539. *
  5540. * Copyright (c) 2010 - 2014 jsPlumb (hello@jsplumbtoolkit.com)
  5541. *
  5542. * http://jsplumbtoolkit.com
  5543. * http://jsplumb.org
  5544. * http://github.com/sporritt/jsplumb
  5545. *
  5546. * Dual licensed under the MIT and GPL2 licenses.
  5547. */
  5548. ;(function() {
  5549. "use strict";
  5550. var makeConnector = function(_jsPlumb, renderMode, connectorName, connectorArgs, forComponent) {
  5551. if (!_jsPlumb.Defaults.DoNotThrowErrors && jsPlumb.Connectors[renderMode][connectorName] == null)
  5552. throw { msg:"jsPlumb: unknown connector type '" + connectorName + "'" };
  5553. return new jsPlumb.Connectors[renderMode][connectorName](connectorArgs, forComponent);
  5554. },
  5555. _makeAnchor = function(anchorParams, elementId, _jsPlumb) {
  5556. return (anchorParams) ? _jsPlumb.makeAnchor(anchorParams, elementId, _jsPlumb) : null;
  5557. };
  5558. jsPlumb.Connection = function(params) {
  5559. var _newEndpoint = params.newEndpoint, _ju = jsPlumbUtil;
  5560. this.connector = null;
  5561. this.idPrefix = "_jsplumb_c_";
  5562. this.defaultLabelLocation = 0.5;
  5563. this.defaultOverlayKeys = ["Overlays", "ConnectionOverlays"];
  5564. // if a new connection is the result of moving some existing connection, params.previousConnection
  5565. // will have that Connection in it. listeners for the jsPlumbConnection event can look for that
  5566. // member and take action if they need to.
  5567. this.previousConnection = params.previousConnection;
  5568. this.source = jsPlumb.getDOMElement(params.source);
  5569. this.target = jsPlumb.getDOMElement(params.target);
  5570. // sourceEndpoint and targetEndpoint override source/target, if they are present. but
  5571. // source is not overridden if the Endpoint has declared it is not the final target of a connection;
  5572. // instead we use the source that the Endpoint declares will be the final source element.
  5573. if (params.sourceEndpoint) this.source = params.sourceEndpoint.endpointWillMoveTo || params.sourceEndpoint.getElement();
  5574. if (params.targetEndpoint) this.target = params.targetEndpoint.getElement();
  5575. OverlayCapableJsPlumbUIComponent.apply(this, arguments);
  5576. this.sourceId = this._jsPlumb.instance.getId(this.source);
  5577. this.targetId = this._jsPlumb.instance.getId(this.target);
  5578. 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.
  5579. this.endpoints = [];
  5580. this.endpointStyles = [];
  5581. var _jsPlumb = this._jsPlumb.instance;
  5582. _jsPlumb.manage(this.sourceId, this.source);
  5583. _jsPlumb.manage(this.targetId, this.target);
  5584. this._jsPlumb.visible = true;
  5585. this._jsPlumb.editable = params.editable === true;
  5586. this._jsPlumb.params = {
  5587. cssClass:params.cssClass,
  5588. container:params.container,
  5589. "pointer-events":params["pointer-events"],
  5590. editorParams:params.editorParams
  5591. };
  5592. this._jsPlumb.lastPaintedAt = null;
  5593. this.getDefaultType = function() {
  5594. return {
  5595. parameters:{},
  5596. scope:null,
  5597. detachable:this._jsPlumb.instance.Defaults.ConnectionsDetachable,
  5598. rettach:this._jsPlumb.instance.Defaults.ReattachConnections,
  5599. paintStyle:this._jsPlumb.instance.Defaults.PaintStyle || jsPlumb.Defaults.PaintStyle,
  5600. connector:this._jsPlumb.instance.Defaults.Connector || jsPlumb.Defaults.Connector,
  5601. hoverPaintStyle:this._jsPlumb.instance.Defaults.HoverPaintStyle || jsPlumb.Defaults.HoverPaintStyle,
  5602. overlays:this._jsPlumb.instance.Defaults.ConnectorOverlays || jsPlumb.Defaults.ConnectorOverlays
  5603. };
  5604. };
  5605. // listen to mouseover and mouseout events passed from the container delegate.
  5606. this.bind("mouseover", function() { this.setHover(true);}.bind(this));
  5607. this.bind("mouseout", function() { this.setHover(false);}.bind(this));
  5608. // INITIALISATION CODE
  5609. // wrapped the main function to return null if no input given. this lets us cascade defaults properly.
  5610. this.makeEndpoint = function(isSource, el, elId, ep) {
  5611. elId = elId || this._jsPlumb.instance.getId(el);
  5612. return this.prepareEndpoint(_jsPlumb, _newEndpoint, this, ep, isSource ? 0 : 1, params, el, elId);
  5613. };
  5614. var eS = this.makeEndpoint(true, this.source, this.sourceId, params.sourceEndpoint),
  5615. eT = this.makeEndpoint(false, this.target, this.targetId, params.targetEndpoint);
  5616. if (eS) _ju.addToList(params.endpointsByElement, this.sourceId, eS);
  5617. if (eT) _ju.addToList(params.endpointsByElement, this.targetId, eT);
  5618. // if scope not set, set it to be the scope for the source endpoint.
  5619. if (!this.scope) this.scope = this.endpoints[0].scope;
  5620. // if explicitly told to (or not to) delete endpoints on detach, override endpoint's preferences
  5621. if (params.deleteEndpointsOnDetach != null) {
  5622. this.endpoints[0]._deleteOnDetach = params.deleteEndpointsOnDetach;
  5623. this.endpoints[1]._deleteOnDetach = params.deleteEndpointsOnDetach;
  5624. }
  5625. else {
  5626. // otherwise, unless the endpoints say otherwise, mark them for deletion.
  5627. if (!this.endpoints[0]._doNotDeleteOnDetach) this.endpoints[0]._deleteOnDetach = true;
  5628. if (!this.endpoints[1]._doNotDeleteOnDetach) this.endpoints[1]._deleteOnDetach = true;
  5629. }
  5630. // TODO these could surely be refactored into some method that tries them one at a time until something exists
  5631. this.setConnector(this.endpoints[0].connector ||
  5632. this.endpoints[1].connector ||
  5633. params.connector ||
  5634. _jsPlumb.Defaults.Connector ||
  5635. jsPlumb.Defaults.Connector, true, true);
  5636. if (params.path)
  5637. this.connector.setPath(params.path);
  5638. this.setPaintStyle(this.endpoints[0].connectorStyle ||
  5639. this.endpoints[1].connectorStyle ||
  5640. params.paintStyle ||
  5641. _jsPlumb.Defaults.PaintStyle ||
  5642. jsPlumb.Defaults.PaintStyle, true);
  5643. this.setHoverPaintStyle(this.endpoints[0].connectorHoverStyle ||
  5644. this.endpoints[1].connectorHoverStyle ||
  5645. params.hoverPaintStyle ||
  5646. _jsPlumb.Defaults.HoverPaintStyle ||
  5647. jsPlumb.Defaults.HoverPaintStyle, true);
  5648. this._jsPlumb.paintStyleInUse = this.getPaintStyle();
  5649. var _suspendedAt = _jsPlumb.getSuspendedAt();
  5650. //*
  5651. if(!_jsPlumb.isSuspendDrawing()) {
  5652. // paint the endpoints
  5653. var myInfo = _jsPlumb.getCachedData(this.sourceId),
  5654. myOffset = myInfo.o, myWH = myInfo.s,
  5655. otherInfo = _jsPlumb.getCachedData(this.targetId),
  5656. otherOffset = otherInfo.o,
  5657. otherWH = otherInfo.s,
  5658. initialTimestamp = _suspendedAt || _jsPlumb.timestamp(),
  5659. anchorLoc = this.endpoints[0].anchor.compute( {
  5660. xy : [ myOffset.left, myOffset.top ], wh : myWH, element : this.endpoints[0],
  5661. elementId:this.endpoints[0].elementId,
  5662. txy : [ otherOffset.left, otherOffset.top ], twh : otherWH, tElement : this.endpoints[1],
  5663. timestamp:initialTimestamp
  5664. });
  5665. this.endpoints[0].paint( { anchorLoc : anchorLoc, timestamp:initialTimestamp });
  5666. anchorLoc = this.endpoints[1].anchor.compute( {
  5667. xy : [ otherOffset.left, otherOffset.top ], wh : otherWH, element : this.endpoints[1],
  5668. elementId:this.endpoints[1].elementId,
  5669. txy : [ myOffset.left, myOffset.top ], twh : myWH, tElement : this.endpoints[0],
  5670. timestamp:initialTimestamp
  5671. });
  5672. this.endpoints[1].paint({ anchorLoc : anchorLoc, timestamp:initialTimestamp });
  5673. }
  5674. //*/
  5675. // END INITIALISATION CODE
  5676. // DETACHABLE
  5677. this._jsPlumb.detachable = _jsPlumb.Defaults.ConnectionsDetachable;
  5678. if (params.detachable === false) this._jsPlumb.detachable = false;
  5679. if(this.endpoints[0].connectionsDetachable === false) this._jsPlumb.detachable = false;
  5680. if(this.endpoints[1].connectionsDetachable === false) this._jsPlumb.detachable = false;
  5681. // REATTACH
  5682. this._jsPlumb.reattach = params.reattach || this.endpoints[0].reattachConnections || this.endpoints[1].reattachConnections || _jsPlumb.Defaults.ReattachConnections;
  5683. // COST + DIRECTIONALITY
  5684. // if cost not supplied, try to inherit from source endpoint
  5685. this._jsPlumb.cost = params.cost || this.endpoints[0].getConnectionCost();
  5686. this._jsPlumb.directed = params.directed;
  5687. // inherit directed flag if set no source endpoint
  5688. if (params.directed == null) this._jsPlumb.directed = this.endpoints[0].areConnectionsDirected();
  5689. // END COST + DIRECTIONALITY
  5690. // PARAMETERS
  5691. // merge all the parameters objects into the connection. parameters set
  5692. // on the connection take precedence; then source endpoint params, then
  5693. // finally target endpoint params.
  5694. // TODO jsPlumb.extend could be made to take more than two args, and it would
  5695. // apply the second through nth args in order.
  5696. var _p = jsPlumb.extend({}, this.endpoints[1].getParameters());
  5697. jsPlumb.extend(_p, this.endpoints[0].getParameters());
  5698. jsPlumb.extend(_p, this.getParameters());
  5699. this.setParameters(_p);
  5700. // END PARAMETERS
  5701. // PAINTING
  5702. // the very last thing we do is apply types, if there are any.
  5703. var _types = [params.type, this.endpoints[0].connectionType, this.endpoints[1].connectionType ].join(" ");
  5704. if (/[^\s]/.test(_types))
  5705. this.addType(_types, params.data, true);
  5706. // END PAINTING
  5707. };
  5708. jsPlumbUtil.extend(jsPlumb.Connection, OverlayCapableJsPlumbUIComponent, {
  5709. applyType : function(t, doNotRepaint) {
  5710. if (t.detachable != null) this.setDetachable(t.detachable);
  5711. if (t.reattach != null) this.setReattach(t.reattach);
  5712. if (t.scope) this.scope = t.scope;
  5713. this.setConnector(t.connector, doNotRepaint);
  5714. if (t.cssClass != null && this.canvas) this._jsPlumb.instance.addClass(this.canvas, t.cssClass);
  5715. if (t.anchor) {
  5716. this.endpoints[0].anchor = this._jsPlumb.instance.makeAnchor(t.anchor);
  5717. this.endpoints[1].anchor = this._jsPlumb.instance.makeAnchor(t.anchor);
  5718. }
  5719. else if (t.anchors) {
  5720. this.endpoints[0].anchor = this._jsPlumb.instance.makeAnchor(t.anchors[0]);
  5721. this.endpoints[1].anchor = this._jsPlumb.instance.makeAnchor(t.anchors[1]);
  5722. }
  5723. },
  5724. getTypeDescriptor : function() { return "connection"; },
  5725. getAttachedElements : function() {
  5726. return this.endpoints;
  5727. },
  5728. addClass : function(c, informEndpoints) {
  5729. if (informEndpoints) {
  5730. this.endpoints[0].addClass(c);
  5731. this.endpoints[1].addClass(c);
  5732. if (this.suspendedEndpoint) this.suspendedEndpoint.addClass(c);
  5733. }
  5734. if (this.connector) {
  5735. this.connector.addClass(c);
  5736. }
  5737. },
  5738. removeClass : function(c, informEndpoints) {
  5739. if (informEndpoints) {
  5740. this.endpoints[0].removeClass(c);
  5741. this.endpoints[1].removeClass(c);
  5742. if (this.suspendedEndpoint) this.suspendedEndpoint.removeClass(c);
  5743. }
  5744. if (this.connector) {
  5745. this.connector.removeClass(c);
  5746. }
  5747. },
  5748. isVisible : function() { return this._jsPlumb.visible; },
  5749. setVisible : function(v) {
  5750. this._jsPlumb.visible = v;
  5751. if (this.connector)
  5752. this.connector.setVisible(v);
  5753. this.repaint();
  5754. },
  5755. cleanup:function() {
  5756. this.endpoints = null;
  5757. this.source = null;
  5758. this.target = null;
  5759. if (this.connector != null) {
  5760. this.connector.cleanup();
  5761. this.connector.destroy();
  5762. }
  5763. this.connector = null;
  5764. },
  5765. isDetachable : function() {
  5766. return this._jsPlumb.detachable === true;
  5767. },
  5768. setDetachable : function(detachable) {
  5769. this._jsPlumb.detachable = detachable === true;
  5770. },
  5771. isReattach : function() {
  5772. return this._jsPlumb.reattach === true;
  5773. },
  5774. setReattach : function(reattach) {
  5775. this._jsPlumb.reattach = reattach === true;
  5776. },
  5777. setHover : function(state) {
  5778. if (this.connector && this._jsPlumb && !this._jsPlumb.instance.isConnectionBeingDragged()) {
  5779. this.connector.setHover(state);
  5780. jsPlumbAdapter[state ? "addClass" : "removeClass"](this.source, this._jsPlumb.instance.hoverSourceClass);
  5781. jsPlumbAdapter[state ? "addClass" : "removeClass"](this.target, this._jsPlumb.instance.hoverTargetClass);
  5782. }
  5783. },
  5784. getCost : function() { return this._jsPlumb.cost; },
  5785. setCost : function(c) { this._jsPlumb.cost = c; },
  5786. isDirected : function() { return this._jsPlumb.directed === true; },
  5787. getConnector : function() { return this.connector; },
  5788. setConnector : function(connectorSpec, doNotRepaint, doNotChangeListenerComponent) {
  5789. var _ju = jsPlumbUtil;
  5790. if (this.connector != null) {
  5791. this.connector.cleanup();
  5792. this.connector.destroy();
  5793. }
  5794. var connectorArgs = {
  5795. _jsPlumb:this._jsPlumb.instance,
  5796. cssClass:this._jsPlumb.params.cssClass,
  5797. container:this._jsPlumb.params.container,
  5798. "pointer-events":this._jsPlumb.params["pointer-events"]
  5799. },
  5800. renderMode = this._jsPlumb.instance.getRenderMode();
  5801. if (_ju.isString(connectorSpec))
  5802. this.connector = makeConnector(this._jsPlumb.instance, renderMode, connectorSpec, connectorArgs, this); // lets you use a string as shorthand.
  5803. else if (_ju.isArray(connectorSpec)) {
  5804. if (connectorSpec.length == 1)
  5805. this.connector = makeConnector(this._jsPlumb.instance, renderMode, connectorSpec[0], connectorArgs, this);
  5806. else
  5807. this.connector = makeConnector(this._jsPlumb.instance, renderMode, connectorSpec[0], _ju.merge(connectorSpec[1], connectorArgs), this);
  5808. }
  5809. this.canvas = this.connector.canvas;
  5810. this.bgCanvas = this.connector.bgCanvas;
  5811. // new: instead of binding listeners per connector, we now just have one delegate on the container.
  5812. // so for that handler we set the connection as the '_jsPlumb' member of the canvas element, and
  5813. // bgCanvas, if it exists, which it does right now in the VML renderer, so it won't from v 2.0.0 onwards.
  5814. if(this.canvas) this.canvas._jsPlumb = this;
  5815. if(this.bgCanvas) this.bgCanvas._jsPlumb = this;
  5816. if (!doNotChangeListenerComponent) this.setListenerComponent(this.connector);
  5817. if (!doNotRepaint) this.repaint();
  5818. },
  5819. paint : function(params) {
  5820. if (!this._jsPlumb.instance.isSuspendDrawing() && this._jsPlumb.visible) {
  5821. params = params || {};
  5822. var timestamp = params.timestamp,
  5823. // if the moving object is not the source we must transpose the two references.
  5824. swap = false,
  5825. tId = swap ? this.sourceId : this.targetId, sId = swap ? this.targetId : this.sourceId,
  5826. tIdx = swap ? 0 : 1, sIdx = swap ? 1 : 0;
  5827. if (timestamp == null || timestamp != this._jsPlumb.lastPaintedAt) {
  5828. var sourceInfo = this._jsPlumb.instance.getOffset(sId),
  5829. targetInfo = this._jsPlumb.instance.getOffset(tId),
  5830. sE = this.endpoints[sIdx], tE = this.endpoints[tIdx];
  5831. var sAnchorP = sE.anchor.getCurrentLocation({xy:[sourceInfo.left,sourceInfo.top], wh:[sourceInfo.width, sourceInfo.height], element:sE, timestamp:timestamp}),
  5832. tAnchorP = tE.anchor.getCurrentLocation({xy:[targetInfo.left,targetInfo.top], wh:[targetInfo.width, targetInfo.height], element:tE, timestamp:timestamp});
  5833. this.connector.resetBounds();
  5834. this.connector.compute({
  5835. sourcePos:sAnchorP,
  5836. targetPos:tAnchorP,
  5837. sourceEndpoint:this.endpoints[sIdx],
  5838. targetEndpoint:this.endpoints[tIdx],
  5839. lineWidth:this._jsPlumb.paintStyleInUse.lineWidth,
  5840. sourceInfo:sourceInfo,
  5841. targetInfo:targetInfo
  5842. });
  5843. var overlayExtents = { minX:Infinity, minY:Infinity, maxX:-Infinity, maxY:-Infinity };
  5844. // compute overlays. we do this first so we can get their placements, and adjust the
  5845. // container if needs be (if an overlay would be clipped)
  5846. for ( var i = 0; i < this._jsPlumb.overlays.length; i++) {
  5847. var o = this._jsPlumb.overlays[i];
  5848. if (o.isVisible()) {
  5849. this._jsPlumb.overlayPlacements[i] = o.draw(this.connector, this._jsPlumb.paintStyleInUse, this.getAbsoluteOverlayPosition(o));
  5850. overlayExtents.minX = Math.min(overlayExtents.minX, this._jsPlumb.overlayPlacements[i].minX);
  5851. overlayExtents.maxX = Math.max(overlayExtents.maxX, this._jsPlumb.overlayPlacements[i].maxX);
  5852. overlayExtents.minY = Math.min(overlayExtents.minY, this._jsPlumb.overlayPlacements[i].minY);
  5853. overlayExtents.maxY = Math.max(overlayExtents.maxY, this._jsPlumb.overlayPlacements[i].maxY);
  5854. }
  5855. }
  5856. var lineWidth = parseFloat(this._jsPlumb.paintStyleInUse.lineWidth || 1) / 2,
  5857. outlineWidth = parseFloat(this._jsPlumb.paintStyleInUse.lineWidth || 0),
  5858. extents = {
  5859. xmin : Math.min(this.connector.bounds.minX - (lineWidth + outlineWidth), overlayExtents.minX),
  5860. ymin : Math.min(this.connector.bounds.minY - (lineWidth + outlineWidth), overlayExtents.minY),
  5861. xmax : Math.max(this.connector.bounds.maxX + (lineWidth + outlineWidth), overlayExtents.maxX),
  5862. ymax : Math.max(this.connector.bounds.maxY + (lineWidth + outlineWidth), overlayExtents.maxY)
  5863. };
  5864. // paint the connector.
  5865. this.connector.paint(this._jsPlumb.paintStyleInUse, null, extents);
  5866. // and then the overlays
  5867. for ( var j = 0; j < this._jsPlumb.overlays.length; j++) {
  5868. var p = this._jsPlumb.overlays[j];
  5869. if (p.isVisible()) {
  5870. p.paint(this._jsPlumb.overlayPlacements[j], extents);
  5871. }
  5872. }
  5873. }
  5874. this._jsPlumb.lastPaintedAt = timestamp;
  5875. }
  5876. },
  5877. repaint : function(params) {
  5878. params = params || {};
  5879. this.paint({ elId : this.sourceId, recalc : !(params.recalc === false), timestamp:params.timestamp});
  5880. },
  5881. prepareEndpoint : function(_jsPlumb, _newEndpoint, conn, existing, index, params, element, elementId) {
  5882. var e;
  5883. if (existing) {
  5884. conn.endpoints[index] = existing;
  5885. existing.addConnection(conn);
  5886. } else {
  5887. if (!params.endpoints) params.endpoints = [ null, null ];
  5888. var ep = params.endpoints[index] || params.endpoint || _jsPlumb.Defaults.Endpoints[index] || jsPlumb.Defaults.Endpoints[index] || _jsPlumb.Defaults.Endpoint || jsPlumb.Defaults.Endpoint;
  5889. if (!params.endpointStyles) params.endpointStyles = [ null, null ];
  5890. if (!params.endpointHoverStyles) params.endpointHoverStyles = [ null, null ];
  5891. var es = params.endpointStyles[index] || params.endpointStyle || _jsPlumb.Defaults.EndpointStyles[index] || jsPlumb.Defaults.EndpointStyles[index] || _jsPlumb.Defaults.EndpointStyle || jsPlumb.Defaults.EndpointStyle;
  5892. // Endpoints derive their fillStyle from the connector's strokeStyle, if no fillStyle was specified.
  5893. if (es.fillStyle == null && params.paintStyle != null)
  5894. es.fillStyle = params.paintStyle.strokeStyle;
  5895. // TODO: decide if the endpoint should derive the connection's outline width and color. currently it does:
  5896. //*
  5897. if (es.outlineColor == null && params.paintStyle != null)
  5898. es.outlineColor = params.paintStyle.outlineColor;
  5899. if (es.outlineWidth == null && params.paintStyle != null)
  5900. es.outlineWidth = params.paintStyle.outlineWidth;
  5901. //*/
  5902. var ehs = params.endpointHoverStyles[index] || params.endpointHoverStyle || _jsPlumb.Defaults.EndpointHoverStyles[index] || jsPlumb.Defaults.EndpointHoverStyles[index] || _jsPlumb.Defaults.EndpointHoverStyle || jsPlumb.Defaults.EndpointHoverStyle;
  5903. // endpoint hover fill style is derived from connector's hover stroke style. TODO: do we want to do this by default? for sure?
  5904. if (params.hoverPaintStyle != null) {
  5905. if (ehs == null) ehs = {};
  5906. if (ehs.fillStyle == null) {
  5907. ehs.fillStyle = params.hoverPaintStyle.strokeStyle;
  5908. }
  5909. }
  5910. var a = params.anchors ? params.anchors[index] :
  5911. params.anchor ? params.anchor :
  5912. _makeAnchor(_jsPlumb.Defaults.Anchors[index], elementId, _jsPlumb) ||
  5913. _makeAnchor(jsPlumb.Defaults.Anchors[index], elementId,_jsPlumb) ||
  5914. _makeAnchor(_jsPlumb.Defaults.Anchor, elementId,_jsPlumb) ||
  5915. _makeAnchor(jsPlumb.Defaults.Anchor, elementId, _jsPlumb),
  5916. u = params.uuids ? params.uuids[index] : null;
  5917. e = _newEndpoint({
  5918. paintStyle : es, hoverPaintStyle:ehs, endpoint : ep, connections : [ conn ],
  5919. uuid : u, anchor : a, source : element, scope : params.scope,
  5920. reattach:params.reattach || _jsPlumb.Defaults.ReattachConnections,
  5921. detachable:params.detachable || _jsPlumb.Defaults.ConnectionsDetachable
  5922. });
  5923. conn.endpoints[index] = e;
  5924. if (params.drawEndpoints === false) e.setVisible(false, true, true);
  5925. }
  5926. return e;
  5927. }
  5928. }); // END Connection class
  5929. })();
  5930. /*
  5931. * jsPlumb
  5932. *
  5933. * Title:jsPlumb 1.7.2
  5934. *
  5935. * Provides a way to visually connect elements on an HTML page, using SVG or VML.
  5936. *
  5937. * This file contains the code for creating and manipulating anchors.
  5938. *
  5939. * Copyright (c) 2010 - 2014 Simon Porritt (simon@jsplumbtoolkit.com)
  5940. *
  5941. * http://jsplumbtoolkit.com
  5942. * http://github.com/sporritt/jsplumb
  5943. *
  5944. * Dual licensed under the MIT and GPL2 licenses.
  5945. */
  5946. ;(function() {
  5947. "use strict";
  5948. //
  5949. // manages anchors for all elements.
  5950. //
  5951. jsPlumb.AnchorManager = function(params) {
  5952. var _amEndpoints = {},
  5953. continuousAnchors = {},
  5954. continuousAnchorLocations = {},
  5955. userDefinedContinuousAnchorLocations = {},
  5956. continuousAnchorOrientations = {},
  5957. Orientation = { HORIZONTAL : "horizontal", VERTICAL : "vertical", DIAGONAL : "diagonal", IDENTITY:"identity" },
  5958. axes = ["left", "top", "right", "bottom"],
  5959. connectionsByElementId = {},
  5960. self = this,
  5961. anchorLists = {},
  5962. jsPlumbInstance = params.jsPlumbInstance,
  5963. floatingConnections = {},
  5964. calculateOrientation = function(sourceId, targetId, sd, td, sourceAnchor, targetAnchor) {
  5965. if (sourceId === targetId) return {
  5966. orientation:Orientation.IDENTITY,
  5967. a:["top", "top"]
  5968. };
  5969. var theta = Math.atan2((td.centery - sd.centery) , (td.centerx - sd.centerx)),
  5970. theta2 = Math.atan2((sd.centery - td.centery) , (sd.centerx - td.centerx));
  5971. // --------------------------------------------------------------------------------------
  5972. // improved face calculation. get midpoints of each face for source and target, then put in an array with all combinations of
  5973. // source/target faces. sort this array by distance between midpoints. the entry at index 0 is our preferred option. we can
  5974. // go through the array one by one until we find an entry in which each requested face is supported.
  5975. var candidates = [], midpoints = { };
  5976. (function(types, dim) {
  5977. for (var i = 0; i < types.length; i++) {
  5978. midpoints[types[i]] = {
  5979. "left":[ dim[i].left, dim[i].centery ],
  5980. "right":[ dim[i].right, dim[i].centery ],
  5981. "top":[ dim[i].centerx, dim[i].top ],
  5982. "bottom":[ dim[i].centerx , dim[i].bottom]
  5983. };
  5984. }
  5985. })([ "source", "target" ], [ sd, td ]);
  5986. for (var sf = 0; sf < axes.length; sf++) {
  5987. for (var tf = 0; tf < axes.length; tf++) {
  5988. if (sf != tf) {
  5989. candidates.push({
  5990. source:axes[sf],
  5991. target:axes[tf],
  5992. dist:Biltong.lineLength(midpoints.source[axes[sf]], midpoints.target[axes[tf]])
  5993. });
  5994. }
  5995. }
  5996. }
  5997. candidates.sort(function(a, b) {
  5998. return a.dist < b.dist ? -1 : a.dist > b.dist ? 1 : 0;
  5999. });
  6000. // now go through this list and try to get an entry that satisfies both (there will be one, unless one of the anchors
  6001. // declares no available faces)
  6002. var sourceEdge = candidates[0].source, targetEdge = candidates[0].target;
  6003. for (var i = 0; i < candidates.length; i++) {
  6004. if (!sourceAnchor.isContinuous || sourceAnchor.isEdgeSupported(candidates[i].source))
  6005. sourceEdge = candidates[i].source;
  6006. else
  6007. sourceEdge = null;
  6008. if (!targetAnchor.isContinuous || targetAnchor.isEdgeSupported(candidates[i].target))
  6009. targetEdge = candidates[i].target;
  6010. else {
  6011. targetEdge = null;
  6012. }
  6013. if (sourceEdge != null && targetEdge != null) break;
  6014. }
  6015. // --------------------------------------------------------------------------------------
  6016. return {
  6017. a : [ sourceEdge, targetEdge ],
  6018. theta:theta,
  6019. theta2:theta2
  6020. };
  6021. },
  6022. // used by placeAnchors function
  6023. placeAnchorsOnLine = function(desc, elementDimensions, elementPosition,
  6024. connections, horizontal, otherMultiplier, reverse) {
  6025. var a = [], step = elementDimensions[horizontal ? 0 : 1] / (connections.length + 1);
  6026. for (var i = 0; i < connections.length; i++) {
  6027. var val = (i + 1) * step, other = otherMultiplier * elementDimensions[horizontal ? 1 : 0];
  6028. if (reverse)
  6029. val = elementDimensions[horizontal ? 0 : 1] - val;
  6030. var dx = (horizontal ? val : other), x = elementPosition[0] + dx, xp = dx / elementDimensions[0],
  6031. dy = (horizontal ? other : val), y = elementPosition[1] + dy, yp = dy / elementDimensions[1];
  6032. a.push([ x, y, xp, yp, connections[i][1], connections[i][2] ]);
  6033. }
  6034. return a;
  6035. },
  6036. // used by edgeSortFunctions
  6037. currySort = function(reverseAngles) {
  6038. return function(a,b) {
  6039. var r = true;
  6040. if (reverseAngles) {
  6041. r = a[0][0] < b[0][0];
  6042. }
  6043. else {
  6044. r = a[0][0] > b[0][0];
  6045. }
  6046. return r === false ? -1 : 1;
  6047. };
  6048. },
  6049. // used by edgeSortFunctions
  6050. leftSort = function(a,b) {
  6051. // first get adjusted values
  6052. var p1 = a[0][0] < 0 ? -Math.PI - a[0][0] : Math.PI - a[0][0],
  6053. p2 = b[0][0] < 0 ? -Math.PI - b[0][0] : Math.PI - b[0][0];
  6054. if (p1 > p2) return 1;
  6055. else return a[0][1] > b[0][1] ? 1 : -1;
  6056. },
  6057. // used by placeAnchors
  6058. edgeSortFunctions = {
  6059. "top":function(a, b) { return a[0] > b[0] ? 1 : -1; },
  6060. "right":currySort(true),
  6061. "bottom":currySort(true),
  6062. "left":leftSort
  6063. },
  6064. // used by placeAnchors
  6065. _sortHelper = function(_array, _fn) { return _array.sort(_fn); },
  6066. // used by AnchorManager.redraw
  6067. placeAnchors = function(elementId, _anchorLists) {
  6068. var cd = jsPlumbInstance.getCachedData(elementId), sS = cd.s, sO = cd.o,
  6069. placeSomeAnchors = function(desc, elementDimensions, elementPosition, unsortedConnections, isHorizontal, otherMultiplier, orientation) {
  6070. if (unsortedConnections.length > 0) {
  6071. var sc = _sortHelper(unsortedConnections, edgeSortFunctions[desc]), // puts them in order based on the target element's pos on screen
  6072. reverse = desc === "right" || desc === "top",
  6073. anchors = placeAnchorsOnLine(desc, elementDimensions,
  6074. elementPosition, sc,
  6075. isHorizontal, otherMultiplier, reverse );
  6076. // takes a computed anchor position and adjusts it for parent offset and scroll, then stores it.
  6077. var _setAnchorLocation = function(endpoint, anchorPos) {
  6078. continuousAnchorLocations[endpoint.id] = [ anchorPos[0], anchorPos[1], anchorPos[2], anchorPos[3] ];
  6079. continuousAnchorOrientations[endpoint.id] = orientation;
  6080. };
  6081. for (var i = 0; i < anchors.length; i++) {
  6082. var c = anchors[i][4], weAreSource = c.endpoints[0].elementId === elementId, weAreTarget = c.endpoints[1].elementId === elementId;
  6083. if (weAreSource)
  6084. _setAnchorLocation(c.endpoints[0], anchors[i]);
  6085. else if (weAreTarget)
  6086. _setAnchorLocation(c.endpoints[1], anchors[i]);
  6087. }
  6088. }
  6089. };
  6090. placeSomeAnchors("bottom", sS, [sO.left,sO.top], _anchorLists.bottom, true, 1, [0,1]);
  6091. placeSomeAnchors("top", sS, [sO.left,sO.top], _anchorLists.top, true, 0, [0,-1]);
  6092. placeSomeAnchors("left", sS, [sO.left,sO.top], _anchorLists.left, false, 0, [-1,0]);
  6093. placeSomeAnchors("right", sS, [sO.left,sO.top], _anchorLists.right, false, 1, [1,0]);
  6094. };
  6095. this.reset = function() {
  6096. _amEndpoints = {};
  6097. connectionsByElementId = {};
  6098. anchorLists = {};
  6099. };
  6100. this.addFloatingConnection = function(key, conn) {
  6101. floatingConnections[key] = conn;
  6102. };
  6103. this.removeFloatingConnection = function(key) {
  6104. delete floatingConnections[key];
  6105. };
  6106. this.newConnection = function(conn) {
  6107. var sourceId = conn.sourceId, targetId = conn.targetId,
  6108. ep = conn.endpoints,
  6109. doRegisterTarget = true,
  6110. registerConnection = function(otherIndex, otherEndpoint, otherAnchor, elId, c) {
  6111. if ((sourceId == targetId) && otherAnchor.isContinuous){
  6112. // remove the target endpoint's canvas. we dont need it.
  6113. conn._jsPlumb.instance.removeElement(ep[1].canvas);
  6114. doRegisterTarget = false;
  6115. }
  6116. jsPlumbUtil.addToList(connectionsByElementId, elId, [c, otherEndpoint, otherAnchor.constructor == jsPlumb.DynamicAnchor]);
  6117. };
  6118. registerConnection(0, ep[0], ep[0].anchor, targetId, conn);
  6119. if (doRegisterTarget)
  6120. registerConnection(1, ep[1], ep[1].anchor, sourceId, conn);
  6121. };
  6122. var removeEndpointFromAnchorLists = function(endpoint) {
  6123. (function(list, eId) {
  6124. if (list) { // transient anchors dont get entries in this list.
  6125. var f = function(e) { return e[4] == eId; };
  6126. jsPlumbUtil.removeWithFunction(list.top, f);
  6127. jsPlumbUtil.removeWithFunction(list.left, f);
  6128. jsPlumbUtil.removeWithFunction(list.bottom, f);
  6129. jsPlumbUtil.removeWithFunction(list.right, f);
  6130. }
  6131. })(anchorLists[endpoint.elementId], endpoint.id);
  6132. };
  6133. this.connectionDetached = function(connInfo) {
  6134. var connection = connInfo.connection || connInfo,
  6135. sourceId = connInfo.sourceId,
  6136. targetId = connInfo.targetId,
  6137. ep = connection.endpoints,
  6138. removeConnection = function(otherIndex, otherEndpoint, otherAnchor, elId, c) {
  6139. if (otherAnchor != null && otherAnchor.constructor == jsPlumb.FloatingAnchor) {
  6140. // no-op
  6141. }
  6142. else {
  6143. jsPlumbUtil.removeWithFunction(connectionsByElementId[elId], function(_c) {
  6144. return _c[0].id == c.id;
  6145. });
  6146. }
  6147. };
  6148. removeConnection(1, ep[1], ep[1].anchor, sourceId, connection);
  6149. removeConnection(0, ep[0], ep[0].anchor, targetId, connection);
  6150. // remove from anchorLists
  6151. removeEndpointFromAnchorLists(connection.endpoints[0]);
  6152. removeEndpointFromAnchorLists(connection.endpoints[1]);
  6153. self.redraw(connection.sourceId);
  6154. self.redraw(connection.targetId);
  6155. };
  6156. this.add = function(endpoint, elementId) {
  6157. jsPlumbUtil.addToList(_amEndpoints, elementId, endpoint);
  6158. };
  6159. this.changeId = function(oldId, newId) {
  6160. connectionsByElementId[newId] = connectionsByElementId[oldId];
  6161. _amEndpoints[newId] = _amEndpoints[oldId];
  6162. delete connectionsByElementId[oldId];
  6163. delete _amEndpoints[oldId];
  6164. };
  6165. this.getConnectionsFor = function(elementId) {
  6166. return connectionsByElementId[elementId] || [];
  6167. };
  6168. this.getEndpointsFor = function(elementId) {
  6169. return _amEndpoints[elementId] || [];
  6170. };
  6171. this.deleteEndpoint = function(endpoint) {
  6172. jsPlumbUtil.removeWithFunction(_amEndpoints[endpoint.elementId], function(e) {
  6173. return e.id == endpoint.id;
  6174. });
  6175. removeEndpointFromAnchorLists(endpoint);
  6176. };
  6177. this.clearFor = function(elementId) {
  6178. delete _amEndpoints[elementId];
  6179. _amEndpoints[elementId] = [];
  6180. };
  6181. // updates the given anchor list by either updating an existing anchor's info, or adding it. this function
  6182. // also removes the anchor from its previous list, if the edge it is on has changed.
  6183. // all connections found along the way (those that are connected to one of the faces this function
  6184. // operates on) are added to the connsToPaint list, as are their endpoints. in this way we know to repaint
  6185. // them wthout having to calculate anything else about them.
  6186. var _updateAnchorList = function(lists, theta, order, conn, aBoolean, otherElId, idx, reverse, edgeId, elId, connsToPaint, endpointsToPaint) {
  6187. // first try to find the exact match, but keep track of the first index of a matching element id along the way.s
  6188. var exactIdx = -1,
  6189. firstMatchingElIdx = -1,
  6190. endpoint = conn.endpoints[idx],
  6191. endpointId = endpoint.id,
  6192. oIdx = [1,0][idx],
  6193. values = [ [ theta, order ], conn, aBoolean, otherElId, endpointId ],
  6194. listToAddTo = lists[edgeId],
  6195. listToRemoveFrom = endpoint._continuousAnchorEdge ? lists[endpoint._continuousAnchorEdge] : null,
  6196. i;
  6197. if (listToRemoveFrom) {
  6198. var rIdx = jsPlumbUtil.findWithFunction(listToRemoveFrom, function(e) { return e[4] == endpointId; });
  6199. if (rIdx != -1) {
  6200. listToRemoveFrom.splice(rIdx, 1);
  6201. // get all connections from this list
  6202. for (i = 0; i < listToRemoveFrom.length; i++) {
  6203. jsPlumbUtil.addWithFunction(connsToPaint, listToRemoveFrom[i][1], function(c) { return c.id == listToRemoveFrom[i][1].id; });
  6204. jsPlumbUtil.addWithFunction(endpointsToPaint, listToRemoveFrom[i][1].endpoints[idx], function(e) { return e.id == listToRemoveFrom[i][1].endpoints[idx].id; });
  6205. jsPlumbUtil.addWithFunction(endpointsToPaint, listToRemoveFrom[i][1].endpoints[oIdx], function(e) { return e.id == listToRemoveFrom[i][1].endpoints[oIdx].id; });
  6206. }
  6207. }
  6208. }
  6209. for (i = 0; i < listToAddTo.length; i++) {
  6210. if (params.idx == 1 && listToAddTo[i][3] === otherElId && firstMatchingElIdx == -1)
  6211. firstMatchingElIdx = i;
  6212. jsPlumbUtil.addWithFunction(connsToPaint, listToAddTo[i][1], function(c) { return c.id == listToAddTo[i][1].id; });
  6213. jsPlumbUtil.addWithFunction(endpointsToPaint, listToAddTo[i][1].endpoints[idx], function(e) { return e.id == listToAddTo[i][1].endpoints[idx].id; });
  6214. jsPlumbUtil.addWithFunction(endpointsToPaint, listToAddTo[i][1].endpoints[oIdx], function(e) { return e.id == listToAddTo[i][1].endpoints[oIdx].id; });
  6215. }
  6216. if (exactIdx != -1) {
  6217. listToAddTo[exactIdx] = values;
  6218. }
  6219. else {
  6220. var insertIdx = reverse ? firstMatchingElIdx != -1 ? firstMatchingElIdx : 0 : listToAddTo.length; // of course we will get this from having looked through the array shortly.
  6221. listToAddTo.splice(insertIdx, 0, values);
  6222. }
  6223. // store this for next time.
  6224. endpoint._continuousAnchorEdge = edgeId;
  6225. };
  6226. //
  6227. // find the entry in an endpoint's list for this connection and update its target endpoint
  6228. // with the current target in the connection.
  6229. //
  6230. //
  6231. this.updateOtherEndpoint = function(elId, oldTargetId, newTargetId, connection) {
  6232. var sIndex = jsPlumbUtil.findWithFunction(connectionsByElementId[elId], function(i) {
  6233. return i[0].id === connection.id;
  6234. }),
  6235. tIndex = jsPlumbUtil.findWithFunction(connectionsByElementId[oldTargetId], function(i) {
  6236. return i[0].id === connection.id;
  6237. });
  6238. // update or add data for source
  6239. if (sIndex != -1) {
  6240. connectionsByElementId[elId][sIndex][0] = connection;
  6241. connectionsByElementId[elId][sIndex][1] = connection.endpoints[1];
  6242. connectionsByElementId[elId][sIndex][2] = connection.endpoints[1].anchor.constructor == jsPlumb.DynamicAnchor;
  6243. }
  6244. // remove entry for previous target (if there)
  6245. if (tIndex > -1) {
  6246. connectionsByElementId[oldTargetId].splice(tIndex, 1);
  6247. // add entry for new target
  6248. jsPlumbUtil.addToList(connectionsByElementId, newTargetId, [connection, connection.endpoints[0], connection.endpoints[0].anchor.constructor == jsPlumb.DynamicAnchor]);
  6249. }
  6250. };
  6251. //
  6252. // notification that the connection given has changed source from the originalId to the newId.
  6253. // This involves:
  6254. // 1. removing the connection from the list of connections stored for the originalId
  6255. // 2. updating the source information for the target of the connection
  6256. // 3. re-registering the connection in connectionsByElementId with the newId
  6257. //
  6258. this.sourceChanged = function(originalId, newId, connection) {
  6259. if (originalId !== newId) {
  6260. // remove the entry that points from the old source to the target
  6261. jsPlumbUtil.removeWithFunction(connectionsByElementId[originalId], function(info) {
  6262. return info[0].id === connection.id;
  6263. });
  6264. // find entry for target and update it
  6265. var tIdx = jsPlumbUtil.findWithFunction(connectionsByElementId[connection.targetId], function(i) {
  6266. return i[0].id === connection.id;
  6267. });
  6268. if (tIdx > -1) {
  6269. connectionsByElementId[connection.targetId][tIdx][0] = connection;
  6270. connectionsByElementId[connection.targetId][tIdx][1] = connection.endpoints[0];
  6271. connectionsByElementId[connection.targetId][tIdx][2] = connection.endpoints[0].anchor.constructor == jsPlumb.DynamicAnchor;
  6272. }
  6273. // add entry for new source
  6274. jsPlumbUtil.addToList(connectionsByElementId, newId, [connection, connection.endpoints[1], connection.endpoints[1].anchor.constructor == jsPlumb.DynamicAnchor]);
  6275. }
  6276. };
  6277. //
  6278. // moves the given endpoint from `currentId` to `element`.
  6279. // This involves:
  6280. //
  6281. // 1. changing the key in _amEndpoints under which the endpoint is stored
  6282. // 2. changing the source or target values in all of the endpoint's connections
  6283. // 3. changing the array in connectionsByElementId in which the endpoint's connections
  6284. // are stored (done by either sourceChanged or updateOtherEndpoint)
  6285. //
  6286. this.rehomeEndpoint = function(ep, currentId, element) {
  6287. var eps = _amEndpoints[currentId] || [],
  6288. elementId = jsPlumbInstance.getId(element);
  6289. if (elementId !== currentId) {
  6290. var idx = jsPlumbUtil.indexOf(eps, ep);
  6291. if (idx > -1) {
  6292. var _ep = eps.splice(idx, 1)[0];
  6293. self.add(_ep, elementId);
  6294. }
  6295. }
  6296. for (var i = 0; i < ep.connections.length; i++) {
  6297. if (ep.connections[i].sourceId == currentId) {
  6298. ep.connections[i].sourceId = ep.elementId;
  6299. ep.connections[i].source = ep.element;
  6300. self.sourceChanged(currentId, ep.elementId, ep.connections[i]);
  6301. }
  6302. else if(ep.connections[i].targetId == currentId) {
  6303. ep.connections[i].targetId = ep.elementId;
  6304. ep.connections[i].target = ep.element;
  6305. self.updateOtherEndpoint(ep.connections[i].sourceId, currentId, ep.elementId, ep.connections[i]);
  6306. }
  6307. }
  6308. };
  6309. this.redraw = function(elementId, ui, timestamp, offsetToUI, clearEdits, doNotRecalcEndpoint) {
  6310. if (!jsPlumbInstance.isSuspendDrawing()) {
  6311. // get all the endpoints for this element
  6312. var ep = _amEndpoints[elementId] || [],
  6313. endpointConnections = connectionsByElementId[elementId] || [],
  6314. connectionsToPaint = [],
  6315. endpointsToPaint = [],
  6316. anchorsToUpdate = [];
  6317. timestamp = timestamp || jsPlumbInstance.timestamp();
  6318. // offsetToUI are values that would have been calculated in the dragManager when registering
  6319. // an endpoint for an element that had a parent (somewhere in the hierarchy) that had been
  6320. // registered as draggable.
  6321. offsetToUI = offsetToUI || {left:0, top:0};
  6322. if (ui) {
  6323. ui = {
  6324. left:ui.left + offsetToUI.left,
  6325. top:ui.top + offsetToUI.top
  6326. };
  6327. }
  6328. // valid for one paint cycle.
  6329. var myOffset = jsPlumbInstance.updateOffset( { elId : elementId, offset : ui, recalc : false, timestamp : timestamp }),
  6330. orientationCache = {};
  6331. // actually, first we should compute the orientation of this element to all other elements to which
  6332. // this element is connected with a continuous anchor (whether both ends of the connection have
  6333. // a continuous anchor or just one)
  6334. for (var i = 0; i < endpointConnections.length; i++) {
  6335. var conn = endpointConnections[i][0],
  6336. sourceId = conn.sourceId,
  6337. targetId = conn.targetId,
  6338. sourceContinuous = conn.endpoints[0].anchor.isContinuous,
  6339. targetContinuous = conn.endpoints[1].anchor.isContinuous;
  6340. if (sourceContinuous || targetContinuous) {
  6341. var oKey = sourceId + "_" + targetId,
  6342. oKey2 = targetId + "_" + sourceId,
  6343. o = orientationCache[oKey],
  6344. oIdx = conn.sourceId == elementId ? 1 : 0;
  6345. if (sourceContinuous && !anchorLists[sourceId]) anchorLists[sourceId] = { top:[], right:[], bottom:[], left:[] };
  6346. if (targetContinuous && !anchorLists[targetId]) anchorLists[targetId] = { top:[], right:[], bottom:[], left:[] };
  6347. if (elementId != targetId) jsPlumbInstance.updateOffset( { elId : targetId, timestamp : timestamp });
  6348. if (elementId != sourceId) jsPlumbInstance.updateOffset( { elId : sourceId, timestamp : timestamp });
  6349. var td = jsPlumbInstance.getCachedData(targetId),
  6350. sd = jsPlumbInstance.getCachedData(sourceId);
  6351. if (targetId == sourceId && (sourceContinuous || targetContinuous)) {
  6352. // here we may want to improve this by somehow determining the face we'd like
  6353. // to put the connector on. ideally, when drawing, the face should be calculated
  6354. // by determining which face is closest to the point at which the mouse button
  6355. // was released. for now, we're putting it on the top face.
  6356. _updateAnchorList(
  6357. anchorLists[sourceId],
  6358. -Math.PI / 2,
  6359. 0,
  6360. conn,
  6361. false,
  6362. targetId,
  6363. 0, false, "top", sourceId, connectionsToPaint, endpointsToPaint);
  6364. }
  6365. else {
  6366. if (!o) {
  6367. o = calculateOrientation(sourceId, targetId, sd.o, td.o, conn.endpoints[0].anchor, conn.endpoints[1].anchor);
  6368. orientationCache[oKey] = o;
  6369. // this would be a performance enhancement, but the computed angles need to be clamped to
  6370. //the (-PI/2 -> PI/2) range in order for the sorting to work properly.
  6371. /* orientationCache[oKey2] = {
  6372. orientation:o.orientation,
  6373. a:[o.a[1], o.a[0]],
  6374. theta:o.theta + Math.PI,
  6375. theta2:o.theta2 + Math.PI
  6376. };*/
  6377. }
  6378. if (sourceContinuous) _updateAnchorList(anchorLists[sourceId], o.theta, 0, conn, false, targetId, 0, false, o.a[0], sourceId, connectionsToPaint, endpointsToPaint);
  6379. if (targetContinuous) _updateAnchorList(anchorLists[targetId], o.theta2, -1, conn, true, sourceId, 1, true, o.a[1], targetId, connectionsToPaint, endpointsToPaint);
  6380. }
  6381. if (sourceContinuous) jsPlumbUtil.addWithFunction(anchorsToUpdate, sourceId, function(a) { return a === sourceId; });
  6382. if (targetContinuous) jsPlumbUtil.addWithFunction(anchorsToUpdate, targetId, function(a) { return a === targetId; });
  6383. jsPlumbUtil.addWithFunction(connectionsToPaint, conn, function(c) { return c.id == conn.id; });
  6384. if ((sourceContinuous && oIdx === 0) || (targetContinuous && oIdx === 1))
  6385. jsPlumbUtil.addWithFunction(endpointsToPaint, conn.endpoints[oIdx], function(e) { return e.id == conn.endpoints[oIdx].id; });
  6386. }
  6387. }
  6388. // place Endpoints whose anchors are continuous but have no Connections
  6389. for (i = 0; i < ep.length; i++) {
  6390. if (ep[i].connections.length === 0 && ep[i].anchor.isContinuous) {
  6391. if (!anchorLists[elementId]) anchorLists[elementId] = { top:[], right:[], bottom:[], left:[] };
  6392. _updateAnchorList(anchorLists[elementId], -Math.PI / 2, 0, {endpoints:[ep[i], ep[i]], paint:function(){}}, false, elementId, 0, false, ep[i].anchor.getDefaultFace(), elementId, connectionsToPaint, endpointsToPaint);
  6393. jsPlumbUtil.addWithFunction(anchorsToUpdate, elementId, function(a) { return a === elementId; });
  6394. }
  6395. }
  6396. // now place all the continuous anchors we need to;
  6397. for (i = 0; i < anchorsToUpdate.length; i++) {
  6398. placeAnchors(anchorsToUpdate[i], anchorLists[anchorsToUpdate[i]]);
  6399. }
  6400. // now that continuous anchors have been placed, paint all the endpoints for this element
  6401. // TODO performance: add the endpoint ids to a temp array, and then when iterating in the next
  6402. // loop, check that we didn't just paint that endpoint. we can probably shave off a few more milliseconds this way.
  6403. for (i = 0; i < ep.length; i++) {
  6404. ep[i].paint( { timestamp : timestamp, offset : myOffset, dimensions : myOffset.s, recalc:doNotRecalcEndpoint !== true });
  6405. }
  6406. // ... and any other endpoints we came across as a result of the continuous anchors.
  6407. for (i = 0; i < endpointsToPaint.length; i++) {
  6408. var cd = jsPlumbInstance.getCachedData(endpointsToPaint[i].elementId);
  6409. endpointsToPaint[i].paint( { timestamp : timestamp, offset : cd, dimensions : cd.s });
  6410. }
  6411. // paint all the standard and "dynamic connections", which are connections whose other anchor is
  6412. // static and therefore does need to be recomputed; we make sure that happens only one time.
  6413. // TODO we could have compiled a list of these in the first pass through connections; might save some time.
  6414. for (i = 0; i < endpointConnections.length; i++) {
  6415. var otherEndpoint = endpointConnections[i][1];
  6416. if (otherEndpoint.anchor.constructor == jsPlumb.DynamicAnchor) {
  6417. otherEndpoint.paint({ elementWithPrecedence:elementId, timestamp:timestamp });
  6418. jsPlumbUtil.addWithFunction(connectionsToPaint, endpointConnections[i][0], function(c) { return c.id == endpointConnections[i][0].id; });
  6419. // all the connections for the other endpoint now need to be repainted
  6420. for (var k = 0; k < otherEndpoint.connections.length; k++) {
  6421. if (otherEndpoint.connections[k] !== endpointConnections[i][0])
  6422. jsPlumbUtil.addWithFunction(connectionsToPaint, otherEndpoint.connections[k], function(c) { return c.id == otherEndpoint.connections[k].id; });
  6423. }
  6424. } else if (otherEndpoint.anchor.constructor == jsPlumb.Anchor) {
  6425. jsPlumbUtil.addWithFunction(connectionsToPaint, endpointConnections[i][0], function(c) { return c.id == endpointConnections[i][0].id; });
  6426. }
  6427. }
  6428. // paint current floating connection for this element, if there is one.
  6429. var fc = floatingConnections[elementId];
  6430. if (fc)
  6431. fc.paint({timestamp:timestamp, recalc:false, elId:elementId});
  6432. // paint all the connections
  6433. for (i = 0; i < connectionsToPaint.length; i++) {
  6434. connectionsToPaint[i].paint({elId:elementId, timestamp:timestamp, recalc:false, clearEdits:clearEdits});
  6435. }
  6436. }
  6437. };
  6438. var ContinuousAnchor = function(anchorParams) {
  6439. jsPlumbUtil.EventGenerator.apply(this);
  6440. this.type = "Continuous";
  6441. this.isDynamic = true;
  6442. this.isContinuous = true;
  6443. var faces = anchorParams.faces || ["top", "right", "bottom", "left"],
  6444. clockwise = !(anchorParams.clockwise === false),
  6445. availableFaces = { },
  6446. opposites = { "top":"bottom", "right":"left","left":"right","bottom":"top" },
  6447. clockwiseOptions = { "top":"right", "right":"bottom","left":"top","bottom":"left" },
  6448. antiClockwiseOptions = { "top":"left", "right":"top","left":"bottom","bottom":"right" },
  6449. secondBest = clockwise ? clockwiseOptions : antiClockwiseOptions,
  6450. lastChoice = clockwise ? antiClockwiseOptions : clockwiseOptions,
  6451. cssClass = anchorParams.cssClass || "";
  6452. for (var i = 0; i < faces.length; i++) { availableFaces[faces[i]] = true; }
  6453. this.getDefaultFace = function() {
  6454. return faces.length === 0 ? "top" : faces[0];
  6455. };
  6456. // if the given edge is supported, returns it. otherwise looks for a substitute that _is_
  6457. // supported. if none supported we also return the request edge.
  6458. this.verifyEdge = function(edge) {
  6459. if (availableFaces[edge]) return edge;
  6460. else if (availableFaces[opposites[edge]]) return opposites[edge];
  6461. else if (availableFaces[secondBest[edge]]) return secondBest[edge];
  6462. else if (availableFaces[lastChoice[edge]]) return lastChoice[edge];
  6463. return edge; // we have to give them something.
  6464. };
  6465. this.isEdgeSupported = function(edge) {
  6466. return availableFaces[edge] === true;
  6467. };
  6468. this.compute = function(params) {
  6469. return userDefinedContinuousAnchorLocations[params.element.id] || continuousAnchorLocations[params.element.id] || [0,0];
  6470. };
  6471. this.getCurrentLocation = function(params) {
  6472. return userDefinedContinuousAnchorLocations[params.element.id] || continuousAnchorLocations[params.element.id] || [0,0];
  6473. };
  6474. this.getOrientation = function(endpoint) {
  6475. return continuousAnchorOrientations[endpoint.id] || [0,0];
  6476. };
  6477. this.clearUserDefinedLocation = function() {
  6478. delete userDefinedContinuousAnchorLocations[anchorParams.elementId];
  6479. };
  6480. this.setUserDefinedLocation = function(loc) {
  6481. userDefinedContinuousAnchorLocations[anchorParams.elementId] = loc;
  6482. };
  6483. this.getCssClass = function() { return cssClass; };
  6484. this.setCssClass = function(c) { cssClass = c; };
  6485. };
  6486. // continuous anchors
  6487. jsPlumbInstance.continuousAnchorFactory = {
  6488. get:function(params) {
  6489. return new ContinuousAnchor(params);
  6490. },
  6491. clear:function(elementId) {
  6492. delete userDefinedContinuousAnchorLocations[elementId];
  6493. delete continuousAnchorLocations[elementId];
  6494. }
  6495. };
  6496. };
  6497. /**
  6498. * 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
  6499. * 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"),
  6500. * 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
  6501. * creation of Anchors without user intervention.
  6502. */
  6503. jsPlumb.Anchor = function(params) {
  6504. this.x = params.x || 0;
  6505. this.y = params.y || 0;
  6506. this.elementId = params.elementId;
  6507. this.cssClass = params.cssClass || "";
  6508. this.userDefinedLocation = null;
  6509. this.orientation = params.orientation || [ 0, 0 ];
  6510. jsPlumbUtil.EventGenerator.apply(this);
  6511. var jsPlumbInstance = params.jsPlumbInstance;//,
  6512. //lastTimestamp = null;//, lastReturnValue = null;
  6513. this.lastReturnValue = null;
  6514. this.offsets = params.offsets || [ 0, 0 ];
  6515. this.timestamp = null;
  6516. this.compute = function(params) {
  6517. var xy = params.xy, wh = params.wh, element = params.element, timestamp = params.timestamp;
  6518. if(params.clearUserDefinedLocation)
  6519. this.userDefinedLocation = null;
  6520. if (timestamp && timestamp === self.timestamp)
  6521. return this.lastReturnValue;
  6522. if (this.userDefinedLocation != null) {
  6523. this.lastReturnValue = this.userDefinedLocation;
  6524. }
  6525. else {
  6526. this.lastReturnValue = [ xy[0] + (this.x * wh[0]) + this.offsets[0], xy[1] + (this.y * wh[1]) + this.offsets[1] ];
  6527. }
  6528. this.timestamp = timestamp;
  6529. return this.lastReturnValue;
  6530. };
  6531. this.getCurrentLocation = function(params) {
  6532. return (this.lastReturnValue == null || (params.timestamp != null && this.timestamp != params.timestamp)) ? this.compute(params) : this.lastReturnValue;
  6533. };
  6534. };
  6535. jsPlumbUtil.extend(jsPlumb.Anchor, jsPlumbUtil.EventGenerator, {
  6536. equals : function(anchor) {
  6537. if (!anchor) return false;
  6538. var ao = anchor.getOrientation(),
  6539. o = this.getOrientation();
  6540. 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];
  6541. },
  6542. getUserDefinedLocation : function() {
  6543. return this.userDefinedLocation;
  6544. },
  6545. setUserDefinedLocation : function(l) {
  6546. this.userDefinedLocation = l;
  6547. },
  6548. clearUserDefinedLocation : function() {
  6549. this.userDefinedLocation = null;
  6550. },
  6551. getOrientation : function(_endpoint) { return this.orientation; },
  6552. getCssClass : function() { return this.cssClass; }
  6553. });
  6554. /**
  6555. * An Anchor that floats. its orientation is computed dynamically from
  6556. * its position relative to the anchor it is floating relative to. It is used when creating
  6557. * a connection through drag and drop.
  6558. *
  6559. * TODO FloatingAnchor could totally be refactored to extend Anchor just slightly.
  6560. */
  6561. jsPlumb.FloatingAnchor = function(params) {
  6562. jsPlumb.Anchor.apply(this, arguments);
  6563. // this is the anchor that this floating anchor is referenced to for
  6564. // purposes of calculating the orientation.
  6565. var ref = params.reference,
  6566. jsPlumbInstance = params.jsPlumbInstance,
  6567. // the canvas this refers to.
  6568. refCanvas = params.referenceCanvas,
  6569. size = jsPlumb.getSize(refCanvas),
  6570. // these are used to store the current relative position of our
  6571. // anchor wrt the reference anchor. they only indicate
  6572. // direction, so have a value of 1 or -1 (or, very rarely, 0). these
  6573. // values are written by the compute method, and read
  6574. // by the getOrientation method.
  6575. xDir = 0, yDir = 0,
  6576. // temporary member used to store an orientation when the floating
  6577. // anchor is hovering over another anchor.
  6578. orientation = null,
  6579. _lastResult = null;
  6580. // clear from parent. we want floating anchor orientation to always be computed.
  6581. this.orientation = null;
  6582. // set these to 0 each; they are used by certain types of connectors in the loopback case,
  6583. // when the connector is trying to clear the element it is on. but for floating anchor it's not
  6584. // very important.
  6585. this.x = 0; this.y = 0;
  6586. this.isFloating = true;
  6587. this.compute = function(params) {
  6588. var xy = params.xy, element = params.element,
  6589. 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.
  6590. _lastResult = result;
  6591. return result;
  6592. };
  6593. this.getOrientation = function(_endpoint) {
  6594. if (orientation) return orientation;
  6595. else {
  6596. var o = ref.getOrientation(_endpoint);
  6597. // here we take into account the orientation of the other
  6598. // anchor: if it declares zero for some direction, we declare zero too. this might not be the most awesome. perhaps we can come
  6599. // up with a better way. it's just so that the line we draw looks like it makes sense. maybe this wont make sense.
  6600. return [ Math.abs(o[0]) * xDir * -1,
  6601. Math.abs(o[1]) * yDir * -1 ];
  6602. }
  6603. };
  6604. /**
  6605. * notification the endpoint associated with this anchor is hovering
  6606. * over another anchor; we want to assume that anchor's orientation
  6607. * for the duration of the hover.
  6608. */
  6609. this.over = function(anchor, endpoint) {
  6610. orientation = anchor.getOrientation(endpoint);
  6611. };
  6612. /**
  6613. * notification the endpoint associated with this anchor is no
  6614. * longer hovering over another anchor; we should resume calculating
  6615. * orientation as we normally do.
  6616. */
  6617. this.out = function() { orientation = null; };
  6618. this.getCurrentLocation = function(params) { return _lastResult == null ? this.compute(params) : _lastResult; };
  6619. };
  6620. jsPlumbUtil.extend(jsPlumb.FloatingAnchor, jsPlumb.Anchor);
  6621. var _convertAnchor = function(anchor, jsPlumbInstance, elementId) {
  6622. return anchor.constructor == jsPlumb.Anchor ? anchor: jsPlumbInstance.makeAnchor(anchor, elementId, jsPlumbInstance);
  6623. };
  6624. /*
  6625. * A DynamicAnchor is an Anchor that contains a list of other Anchors, which it cycles
  6626. * through at compute time to find the one that is located closest to
  6627. * the center of the target element, and returns that Anchor's compute
  6628. * method result. this causes endpoints to follow each other with
  6629. * respect to the orientation of their target elements, which is a useful
  6630. * feature for some applications.
  6631. *
  6632. */
  6633. jsPlumb.DynamicAnchor = function(params) {
  6634. jsPlumb.Anchor.apply(this, arguments);
  6635. this.isSelective = true;
  6636. this.isDynamic = true;
  6637. this.anchors = [];
  6638. this.elementId = params.elementId;
  6639. this.jsPlumbInstance = params.jsPlumbInstance;
  6640. for (var i = 0; i < params.anchors.length; i++)
  6641. this.anchors[i] = _convertAnchor(params.anchors[i], this.jsPlumbInstance, this.elementId);
  6642. this.addAnchor = function(anchor) { this.anchors.push(_convertAnchor(anchor, this.jsPlumbInstance, this.elementId)); };
  6643. this.getAnchors = function() { return this.anchors; };
  6644. this.locked = false;
  6645. var _curAnchor = this.anchors.length > 0 ? this.anchors[0] : null,
  6646. _curIndex = this.anchors.length > 0 ? 0 : -1,
  6647. _lastAnchor = _curAnchor,
  6648. self = this,
  6649. // helper method to calculate the distance between the centers of the two elements.
  6650. _distance = function(anchor, cx, cy, xy, wh) {
  6651. var ax = xy[0] + (anchor.x * wh[0]), ay = xy[1] + (anchor.y * wh[1]),
  6652. acx = xy[0] + (wh[0] / 2), acy = xy[1] + (wh[1] / 2);
  6653. return (Math.sqrt(Math.pow(cx - ax, 2) + Math.pow(cy - ay, 2)) +
  6654. Math.sqrt(Math.pow(acx - ax, 2) + Math.pow(acy - ay, 2)));
  6655. },
  6656. // default method uses distance between element centers. you can provide your own method in the dynamic anchor
  6657. // constructor (and also to jsPlumb.makeDynamicAnchor). the arguments to it are four arrays:
  6658. // xy - xy loc of the anchor's element
  6659. // wh - anchor's element's dimensions
  6660. // txy - xy loc of the element of the other anchor in the connection
  6661. // twh - dimensions of the element of the other anchor in the connection.
  6662. // anchors - the list of selectable anchors
  6663. _anchorSelector = params.selector || function(xy, wh, txy, twh, anchors) {
  6664. var cx = txy[0] + (twh[0] / 2), cy = txy[1] + (twh[1] / 2);
  6665. var minIdx = -1, minDist = Infinity;
  6666. for ( var i = 0; i < anchors.length; i++) {
  6667. var d = _distance(anchors[i], cx, cy, xy, wh);
  6668. if (d < minDist) {
  6669. minIdx = i + 0;
  6670. minDist = d;
  6671. }
  6672. }
  6673. return anchors[minIdx];
  6674. };
  6675. this.compute = function(params) {
  6676. var xy = params.xy, wh = params.wh, timestamp = params.timestamp, txy = params.txy, twh = params.twh;
  6677. this.timestamp = timestamp;
  6678. var udl = self.getUserDefinedLocation();
  6679. if (udl != null) {
  6680. return udl;
  6681. }
  6682. // if anchor is locked or an opposite element was not given, we
  6683. // maintain our state. anchor will be locked
  6684. // if it is the source of a drag and drop.
  6685. if (this.locked || txy == null || twh == null)
  6686. return _curAnchor.compute(params);
  6687. else
  6688. params.timestamp = null; // otherwise clear this, i think. we want the anchor to compute.
  6689. _curAnchor = _anchorSelector(xy, wh, txy, twh, this.anchors);
  6690. this.x = _curAnchor.x;
  6691. this.y = _curAnchor.y;
  6692. if (_curAnchor != _lastAnchor)
  6693. this.fire("anchorChanged", _curAnchor);
  6694. _lastAnchor = _curAnchor;
  6695. return _curAnchor.compute(params);
  6696. };
  6697. this.getCurrentLocation = function(params) {
  6698. return this.getUserDefinedLocation() || (_curAnchor != null ? _curAnchor.getCurrentLocation(params) : null);
  6699. };
  6700. this.getOrientation = function(_endpoint) { return _curAnchor != null ? _curAnchor.getOrientation(_endpoint) : [ 0, 0 ]; };
  6701. this.over = function(anchor, endpoint) { if (_curAnchor != null) _curAnchor.over(anchor, endpoint); };
  6702. this.out = function() { if (_curAnchor != null) _curAnchor.out(); };
  6703. this.getCssClass = function() { return (_curAnchor && _curAnchor.getCssClass()) || ""; };
  6704. };
  6705. jsPlumbUtil.extend(jsPlumb.DynamicAnchor, jsPlumb.Anchor);
  6706. // -------- basic anchors ------------------
  6707. var _curryAnchor = function(x, y, ox, oy, type, fnInit) {
  6708. jsPlumb.Anchors[type] = function(params) {
  6709. var a = params.jsPlumbInstance.makeAnchor([ x, y, ox, oy, 0, 0 ], params.elementId, params.jsPlumbInstance);
  6710. a.type = type;
  6711. if (fnInit) fnInit(a, params);
  6712. return a;
  6713. };
  6714. };
  6715. _curryAnchor(0.5, 0, 0,-1, "TopCenter");
  6716. _curryAnchor(0.5, 1, 0, 1, "BottomCenter");
  6717. _curryAnchor(0, 0.5, -1, 0, "LeftMiddle");
  6718. _curryAnchor(1, 0.5, 1, 0, "RightMiddle");
  6719. // from 1.4.2: Top, Right, Bottom, Left
  6720. _curryAnchor(0.5, 0, 0,-1, "Top");
  6721. _curryAnchor(0.5, 1, 0, 1, "Bottom");
  6722. _curryAnchor(0, 0.5, -1, 0, "Left");
  6723. _curryAnchor(1, 0.5, 1, 0, "Right");
  6724. _curryAnchor(0.5, 0.5, 0, 0, "Center");
  6725. _curryAnchor(1, 0, 0,-1, "TopRight");
  6726. _curryAnchor(1, 1, 0, 1, "BottomRight");
  6727. _curryAnchor(0, 0, 0, -1, "TopLeft");
  6728. _curryAnchor(0, 1, 0, 1, "BottomLeft");
  6729. // ------- dynamic anchors -------------------
  6730. // default dynamic anchors chooses from Top, Right, Bottom, Left
  6731. jsPlumb.Defaults.DynamicAnchors = function(params) {
  6732. return params.jsPlumbInstance.makeAnchors(["TopCenter", "RightMiddle", "BottomCenter", "LeftMiddle"], params.elementId, params.jsPlumbInstance);
  6733. };
  6734. // default dynamic anchors bound to name 'AutoDefault'
  6735. jsPlumb.Anchors.AutoDefault = function(params) {
  6736. var a = params.jsPlumbInstance.makeDynamicAnchor(jsPlumb.Defaults.DynamicAnchors(params));
  6737. a.type = "AutoDefault";
  6738. return a;
  6739. };
  6740. // ------- continuous anchors -------------------
  6741. var _curryContinuousAnchor = function(type, faces) {
  6742. jsPlumb.Anchors[type] = function(params) {
  6743. var a = params.jsPlumbInstance.makeAnchor(["Continuous", { faces:faces }], params.elementId, params.jsPlumbInstance);
  6744. a.type = type;
  6745. return a;
  6746. };
  6747. };
  6748. jsPlumb.Anchors.Continuous = function(params) {
  6749. return params.jsPlumbInstance.continuousAnchorFactory.get(params);
  6750. };
  6751. _curryContinuousAnchor("ContinuousLeft", ["left"]);
  6752. _curryContinuousAnchor("ContinuousTop", ["top"]);
  6753. _curryContinuousAnchor("ContinuousBottom", ["bottom"]);
  6754. _curryContinuousAnchor("ContinuousRight", ["right"]);
  6755. // ------- position assign anchors -------------------
  6756. // this anchor type lets you assign the position at connection time.
  6757. _curryAnchor(0, 0, 0, 0, "Assign", function(anchor, params) {
  6758. // find what to use as the "position finder". the user may have supplied a String which represents
  6759. // the id of a position finder in jsPlumb.AnchorPositionFinders, or the user may have supplied the
  6760. // position finder as a function. we find out what to use and then set it on the anchor.
  6761. var pf = params.position || "Fixed";
  6762. anchor.positionFinder = pf.constructor == String ? params.jsPlumbInstance.AnchorPositionFinders[pf] : pf;
  6763. // always set the constructor params; the position finder might need them later (the Grid one does,
  6764. // for example)
  6765. anchor.constructorParams = params;
  6766. });
  6767. // these are the default anchor positions finders, which are used by the makeTarget function. supplying
  6768. // a position finder argument to that function allows you to specify where the resulting anchor will
  6769. // be located
  6770. jsPlumbInstance.prototype.AnchorPositionFinders = {
  6771. "Fixed": function(dp, ep, es, params) {
  6772. return [ (dp.left - ep.left) / es[0], (dp.top - ep.top) / es[1] ];
  6773. },
  6774. "Grid":function(dp, ep, es, params) {
  6775. var dx = dp.left - ep.left, dy = dp.top - ep.top,
  6776. gx = es[0] / (params.grid[0]), gy = es[1] / (params.grid[1]),
  6777. mx = Math.floor(dx / gx), my = Math.floor(dy / gy);
  6778. return [ ((mx * gx) + (gx / 2)) / es[0], ((my * gy) + (gy / 2)) / es[1] ];
  6779. }
  6780. };
  6781. // ------- perimeter anchors -------------------
  6782. jsPlumb.Anchors.Perimeter = function(params) {
  6783. params = params || {};
  6784. var anchorCount = params.anchorCount || 60,
  6785. shape = params.shape;
  6786. if (!shape) throw new Error("no shape supplied to Perimeter Anchor type");
  6787. var _circle = function() {
  6788. var r = 0.5, step = Math.PI * 2 / anchorCount, current = 0, a = [];
  6789. for (var i = 0; i < anchorCount; i++) {
  6790. var x = r + (r * Math.sin(current)),
  6791. y = r + (r * Math.cos(current));
  6792. a.push( [ x, y, 0, 0 ] );
  6793. current += step;
  6794. }
  6795. return a;
  6796. },
  6797. _path = function(segments) {
  6798. var anchorsPerFace = anchorCount / segments.length, a = [],
  6799. _computeFace = function(x1, y1, x2, y2, fractionalLength) {
  6800. anchorsPerFace = anchorCount * fractionalLength;
  6801. var dx = (x2 - x1) / anchorsPerFace, dy = (y2 - y1) / anchorsPerFace;
  6802. for (var i = 0; i < anchorsPerFace; i++) {
  6803. a.push( [
  6804. x1 + (dx * i),
  6805. y1 + (dy * i),
  6806. 0,
  6807. 0
  6808. ]);
  6809. }
  6810. };
  6811. for (var i = 0; i < segments.length; i++)
  6812. _computeFace.apply(null, segments[i]);
  6813. return a;
  6814. },
  6815. _shape = function(faces) {
  6816. var s = [];
  6817. for (var i = 0; i < faces.length; i++) {
  6818. s.push([faces[i][0], faces[i][1], faces[i][2], faces[i][3], 1 / faces.length]);
  6819. }
  6820. return _path(s);
  6821. },
  6822. _rectangle = function() {
  6823. return _shape([
  6824. [ 0, 0, 1, 0 ], [ 1, 0, 1, 1 ], [ 1, 1, 0, 1 ], [ 0, 1, 0, 0 ]
  6825. ]);
  6826. };
  6827. var _shapes = {
  6828. "Circle":_circle,
  6829. "Ellipse":_circle,
  6830. "Diamond":function() {
  6831. return _shape([
  6832. [ 0.5, 0, 1, 0.5 ], [ 1, 0.5, 0.5, 1 ], [ 0.5, 1, 0, 0.5 ], [ 0, 0.5, 0.5, 0 ]
  6833. ]);
  6834. },
  6835. "Rectangle":_rectangle,
  6836. "Square":_rectangle,
  6837. "Triangle":function() {
  6838. return _shape([
  6839. [ 0.5, 0, 1, 1 ], [ 1, 1, 0, 1 ], [ 0, 1, 0.5, 0]
  6840. ]);
  6841. },
  6842. "Path":function(params) {
  6843. var points = params.points, p = [], tl = 0;
  6844. for (var i = 0; i < points.length - 1; i++) {
  6845. var l = Math.sqrt(Math.pow(points[i][2] - points[i][0]) + Math.pow(points[i][3] - points[i][1]));
  6846. tl += l;
  6847. p.push([points[i][0], points[i][1], points[i+1][0], points[i+1][1], l]);
  6848. }
  6849. for (var j = 0; j < p.length; j++) {
  6850. p[j][4] = p[j][4] / tl;
  6851. }
  6852. return _path(p);
  6853. }
  6854. },
  6855. _rotate = function(points, amountInDegrees) {
  6856. var o = [], theta = amountInDegrees / 180 * Math.PI ;
  6857. for (var i = 0; i < points.length; i++) {
  6858. var _x = points[i][0] - 0.5,
  6859. _y = points[i][1] - 0.5;
  6860. o.push([
  6861. 0.5 + ((_x * Math.cos(theta)) - (_y * Math.sin(theta))),
  6862. 0.5 + ((_x * Math.sin(theta)) + (_y * Math.cos(theta))),
  6863. points[i][2],
  6864. points[i][3]
  6865. ]);
  6866. }
  6867. return o;
  6868. };
  6869. if (!_shapes[shape]) throw new Error("Shape [" + shape + "] is unknown by Perimeter Anchor type");
  6870. var da = _shapes[shape](params);
  6871. if (params.rotation) da = _rotate(da, params.rotation);
  6872. var a = params.jsPlumbInstance.makeDynamicAnchor(da);
  6873. a.type = "Perimeter";
  6874. return a;
  6875. };
  6876. })();
  6877. /*
  6878. * jsPlumb
  6879. *
  6880. * Title:jsPlumb 1.7.2
  6881. *
  6882. * Provides a way to visually connect elements on an HTML page, using SVG or VML.
  6883. *
  6884. * This file contains the default Connectors, Endpoint and Overlay definitions.
  6885. *
  6886. * Copyright (c) 2010 - 2014 Simon Porritt (simon@jsplumbtoolkit.com)
  6887. *
  6888. * http://jsplumbtoolkit.com
  6889. * http://github.com/sporritt/jsplumb
  6890. *
  6891. * Dual licensed under the MIT and GPL2 licenses.
  6892. */
  6893. ;(function() {
  6894. "use strict";
  6895. jsPlumb.Segments = {
  6896. /*
  6897. * Class: AbstractSegment
  6898. * A Connector is made up of 1..N Segments, each of which has a Type, such as 'Straight', 'Arc',
  6899. * 'Bezier'. This is new from 1.4.2, and gives us a lot more flexibility when drawing connections: things such
  6900. * as rounded corners for flowchart connectors, for example, or a straight line stub for Bezier connections, are
  6901. * much easier to do now.
  6902. *
  6903. * A Segment is responsible for providing coordinates for painting it, and also must be able to report its length.
  6904. *
  6905. */
  6906. AbstractSegment : function(params) {
  6907. this.params = params;
  6908. /**
  6909. * Function: findClosestPointOnPath
  6910. * Finds the closest point on this segment to the given [x, y],
  6911. * returning both the x and y of the point plus its distance from
  6912. * the supplied point, and its location along the length of the
  6913. * path inscribed by the segment. This implementation returns
  6914. * Infinity for distance and null values for everything else;
  6915. * subclasses are expected to override.
  6916. */
  6917. this.findClosestPointOnPath = function(x, y) {
  6918. return {
  6919. d:Infinity,
  6920. x:null,
  6921. y:null,
  6922. l:null
  6923. };
  6924. };
  6925. this.getBounds = function() {
  6926. return {
  6927. minX:Math.min(params.x1, params.x2),
  6928. minY:Math.min(params.y1, params.y2),
  6929. maxX:Math.max(params.x1, params.x2),
  6930. maxY:Math.max(params.y1, params.y2)
  6931. };
  6932. };
  6933. },
  6934. Straight : function(params) {
  6935. var _super = jsPlumb.Segments.AbstractSegment.apply(this, arguments),
  6936. length, m, m2, x1, x2, y1, y2,
  6937. _recalc = function() {
  6938. length = Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2));
  6939. m = Biltong.gradient({x:x1, y:y1}, {x:x2, y:y2});
  6940. m2 = -1 / m;
  6941. };
  6942. this.type = "Straight";
  6943. this.getLength = function() { return length; };
  6944. this.getGradient = function() { return m; };
  6945. this.getCoordinates = function() {
  6946. return { x1:x1,y1:y1,x2:x2,y2:y2 };
  6947. };
  6948. this.setCoordinates = function(coords) {
  6949. x1 = coords.x1; y1 = coords.y1; x2 = coords.x2; y2 = coords.y2;
  6950. _recalc();
  6951. };
  6952. this.setCoordinates({x1:params.x1, y1:params.y1, x2:params.x2, y2:params.y2});
  6953. this.getBounds = function() {
  6954. return {
  6955. minX:Math.min(x1, x2),
  6956. minY:Math.min(y1, y2),
  6957. maxX:Math.max(x1, x2),
  6958. maxY:Math.max(y1, y2)
  6959. };
  6960. };
  6961. /**
  6962. * returns the point on the segment's path that is 'location' along the length of the path, where 'location' is a decimal from
  6963. * 0 to 1 inclusive. for the straight line segment this is simple maths.
  6964. */
  6965. this.pointOnPath = function(location, absolute) {
  6966. if (location === 0 && !absolute)
  6967. return { x:x1, y:y1 };
  6968. else if (location == 1 && !absolute)
  6969. return { x:x2, y:y2 };
  6970. else {
  6971. var l = absolute ? location > 0 ? location : length + location : location * length;
  6972. return Biltong.pointOnLine({x:x1, y:y1}, {x:x2, y:y2}, l);
  6973. }
  6974. };
  6975. /**
  6976. * returns the gradient of the segment at the given point - which for us is constant.
  6977. */
  6978. this.gradientAtPoint = function(_) {
  6979. return m;
  6980. };
  6981. /**
  6982. * returns the point on the segment's path that is 'distance' along the length of the path from 'location', where
  6983. * 'location' is a decimal from 0 to 1 inclusive, and 'distance' is a number of pixels.
  6984. * this hands off to jsPlumbUtil to do the maths, supplying two points and the distance.
  6985. */
  6986. this.pointAlongPathFrom = function(location, distance, absolute) {
  6987. var p = this.pointOnPath(location, absolute),
  6988. farAwayPoint = distance <= 0 ? {x:x1, y:y1} : {x:x2, y:y2 };
  6989. /*
  6990. location == 1 ? {
  6991. x:x1 + ((x2 - x1) * 10),
  6992. y:y1 + ((y1 - y2) * 10)
  6993. } :
  6994. */
  6995. if (distance <= 0 && Math.abs(distance) > 1) distance *= -1;
  6996. return Biltong.pointOnLine(p, farAwayPoint, distance);
  6997. };
  6998. // is c between a and b?
  6999. var within = function(a,b,c) {
  7000. return c >= Math.min(a,b) && c <= Math.max(a,b);
  7001. };
  7002. // find which of a and b is closest to c
  7003. var closest = function(a,b,c) {
  7004. return Math.abs(c - a) < Math.abs(c - b) ? a : b;
  7005. };
  7006. /**
  7007. Function: findClosestPointOnPath
  7008. Finds the closest point on this segment to [x,y]. See
  7009. notes on this method in AbstractSegment.
  7010. */
  7011. this.findClosestPointOnPath = function(x, y) {
  7012. var out = {
  7013. d:Infinity,
  7014. x:null,
  7015. y:null,
  7016. l:null,
  7017. x1:x1,
  7018. x2:x2,
  7019. y1:y1,
  7020. y2:y2
  7021. };
  7022. if (m === 0) {
  7023. out.y = y1;
  7024. out.x = within(x1, x2, x) ? x : closest(x1, x2, x);
  7025. }
  7026. else if (m == Infinity || m == -Infinity) {
  7027. out.x = x1;
  7028. out.y = within(y1, y2, y) ? y : closest(y1, y2, y);
  7029. }
  7030. else {
  7031. // closest point lies on normal from given point to this line.
  7032. var b = y1 - (m * x1),
  7033. b2 = y - (m2 * x),
  7034. // y1 = m.x1 + b and y1 = m2.x1 + b2
  7035. // so m.x1 + b = m2.x1 + b2
  7036. // x1(m - m2) = b2 - b
  7037. // x1 = (b2 - b) / (m - m2)
  7038. _x1 = (b2 -b) / (m - m2),
  7039. _y1 = (m * _x1) + b;
  7040. out.x = within(x1,x2,_x1) ? _x1 : closest(x1,x2,_x1);//_x1;
  7041. out.y = within(y1,y2,_y1) ? _y1 : closest(y1,y2,_y1);//_y1;
  7042. }
  7043. var fractionInSegment = Biltong.lineLength([ out.x, out.y ], [ x1, y1 ]);
  7044. out.d = Biltong.lineLength([x,y], [out.x, out.y]);
  7045. out.l = fractionInSegment / length;
  7046. return out;
  7047. };
  7048. },
  7049. /*
  7050. Arc Segment. You need to supply:
  7051. r - radius
  7052. cx - center x for the arc
  7053. cy - center y for the arc
  7054. ac - whether the arc is anticlockwise or not. default is clockwise.
  7055. and then either:
  7056. startAngle - startAngle for the arc.
  7057. endAngle - endAngle for the arc.
  7058. or:
  7059. x1 - x for start point
  7060. y1 - y for start point
  7061. x2 - x for end point
  7062. y2 - y for end point
  7063. */
  7064. Arc : function(params) {
  7065. var _super = jsPlumb.Segments.AbstractSegment.apply(this, arguments),
  7066. _calcAngle = function(_x, _y) {
  7067. return Biltong.theta([params.cx, params.cy], [_x, _y]);
  7068. },
  7069. _calcAngleForLocation = function(segment, location) {
  7070. if (segment.anticlockwise) {
  7071. var sa = segment.startAngle < segment.endAngle ? segment.startAngle + TWO_PI : segment.startAngle,
  7072. s = Math.abs(sa - segment.endAngle);
  7073. return sa - (s * location);
  7074. }
  7075. else {
  7076. var ea = segment.endAngle < segment.startAngle ? segment.endAngle + TWO_PI : segment.endAngle,
  7077. ss = Math.abs (ea - segment.startAngle);
  7078. return segment.startAngle + (ss * location);
  7079. }
  7080. },
  7081. TWO_PI = 2 * Math.PI;
  7082. this.radius = params.r;
  7083. this.anticlockwise = params.ac;
  7084. this.type = "Arc";
  7085. if (params.startAngle && params.endAngle) {
  7086. this.startAngle = params.startAngle;
  7087. this.endAngle = params.endAngle;
  7088. this.x1 = params.cx + (this.radius * Math.cos(params.startAngle));
  7089. this.y1 = params.cy + (this.radius * Math.sin(params.startAngle));
  7090. this.x2 = params.cx + (this.radius * Math.cos(params.endAngle));
  7091. this.y2 = params.cy + (this.radius * Math.sin(params.endAngle));
  7092. }
  7093. else {
  7094. this.startAngle = _calcAngle(params.x1, params.y1);
  7095. this.endAngle = _calcAngle(params.x2, params.y2);
  7096. this.x1 = params.x1;
  7097. this.y1 = params.y1;
  7098. this.x2 = params.x2;
  7099. this.y2 = params.y2;
  7100. }
  7101. if (this.endAngle < 0) this.endAngle += TWO_PI;
  7102. if (this.startAngle < 0) this.startAngle += TWO_PI;
  7103. // segment is used by vml
  7104. this.segment = Biltong.quadrant([this.x1, this.y1], [this.x2, this.y2]);
  7105. // we now have startAngle and endAngle as positive numbers, meaning the
  7106. // absolute difference (|d|) between them is the sweep (s) of this arc, unless the
  7107. // arc is 'anticlockwise' in which case 's' is given by 2PI - |d|.
  7108. var ea = this.endAngle < this.startAngle ? this.endAngle + TWO_PI : this.endAngle;
  7109. this.sweep = Math.abs (ea - this.startAngle);
  7110. if (this.anticlockwise) this.sweep = TWO_PI - this.sweep;
  7111. var circumference = 2 * Math.PI * this.radius,
  7112. frac = this.sweep / TWO_PI,
  7113. length = circumference * frac;
  7114. this.getLength = function() {
  7115. return length;
  7116. };
  7117. this.getBounds = function() {
  7118. return {
  7119. minX:params.cx - params.r,
  7120. maxX:params.cx + params.r,
  7121. minY:params.cy - params.r,
  7122. maxY:params.cy + params.r
  7123. };
  7124. };
  7125. var VERY_SMALL_VALUE = 0.0000000001,
  7126. gentleRound = function(n) {
  7127. var f = Math.floor(n), r = Math.ceil(n);
  7128. if (n - f < VERY_SMALL_VALUE)
  7129. return f;
  7130. else if (r - n < VERY_SMALL_VALUE)
  7131. return r;
  7132. return n;
  7133. };
  7134. /**
  7135. * returns the point on the segment's path that is 'location' along the length of the path, where 'location' is a decimal from
  7136. * 0 to 1 inclusive.
  7137. */
  7138. this.pointOnPath = function(location, absolute) {
  7139. if (location === 0) {
  7140. return { x:this.x1, y:this.y1, theta:this.startAngle };
  7141. }
  7142. else if (location == 1) {
  7143. return { x:this.x2, y:this.y2, theta:this.endAngle };
  7144. }
  7145. if (absolute) {
  7146. location = location / length;
  7147. }
  7148. var angle = _calcAngleForLocation(this, location),
  7149. _x = params.cx + (params.r * Math.cos(angle)),
  7150. _y = params.cy + (params.r * Math.sin(angle));
  7151. return { x:gentleRound(_x), y:gentleRound(_y), theta:angle };
  7152. };
  7153. /**
  7154. * returns the gradient of the segment at the given point.
  7155. */
  7156. this.gradientAtPoint = function(location, absolute) {
  7157. var p = this.pointOnPath(location, absolute);
  7158. var m = Biltong.normal( [ params.cx, params.cy ], [p.x, p.y ] );
  7159. if (!this.anticlockwise && (m == Infinity || m == -Infinity)) m *= -1;
  7160. return m;
  7161. };
  7162. this.pointAlongPathFrom = function(location, distance, absolute) {
  7163. var p = this.pointOnPath(location, absolute),
  7164. arcSpan = distance / circumference * 2 * Math.PI,
  7165. dir = this.anticlockwise ? -1 : 1,
  7166. startAngle = p.theta + (dir * arcSpan),
  7167. startX = params.cx + (this.radius * Math.cos(startAngle)),
  7168. startY = params.cy + (this.radius * Math.sin(startAngle));
  7169. return {x:startX, y:startY};
  7170. };
  7171. },
  7172. Bezier : function(params) {
  7173. this.curve = [
  7174. { x:params.x1, y:params.y1},
  7175. { x:params.cp1x, y:params.cp1y },
  7176. { x:params.cp2x, y:params.cp2y },
  7177. { x:params.x2, y:params.y2 }
  7178. ];
  7179. var _super = jsPlumb.Segments.AbstractSegment.apply(this, arguments);
  7180. // although this is not a strictly rigorous determination of bounds
  7181. // of a bezier curve, it works for the types of curves that this segment
  7182. // type produces.
  7183. this.bounds = {
  7184. minX:Math.min(params.x1, params.x2, params.cp1x, params.cp2x),
  7185. minY:Math.min(params.y1, params.y2, params.cp1y, params.cp2y),
  7186. maxX:Math.max(params.x1, params.x2, params.cp1x, params.cp2x),
  7187. maxY:Math.max(params.y1, params.y2, params.cp1y, params.cp2y)
  7188. };
  7189. this.type = "Bezier";
  7190. var _translateLocation = function(_curve, location, absolute) {
  7191. if (absolute)
  7192. location = jsBezier.locationAlongCurveFrom(_curve, location > 0 ? 0 : 1, location);
  7193. return location;
  7194. };
  7195. /**
  7196. * returns the point on the segment's path that is 'location' along the length of the path, where 'location' is a decimal from
  7197. * 0 to 1 inclusive.
  7198. */
  7199. this.pointOnPath = function(location, absolute) {
  7200. location = _translateLocation(this.curve, location, absolute);
  7201. return jsBezier.pointOnCurve(this.curve, location);
  7202. };
  7203. /**
  7204. * returns the gradient of the segment at the given point.
  7205. */
  7206. this.gradientAtPoint = function(location, absolute) {
  7207. location = _translateLocation(this.curve, location, absolute);
  7208. return jsBezier.gradientAtPoint(this.curve, location);
  7209. };
  7210. this.pointAlongPathFrom = function(location, distance, absolute) {
  7211. location = _translateLocation(this.curve, location, absolute);
  7212. return jsBezier.pointAlongCurveFrom(this.curve, location, distance);
  7213. };
  7214. this.getLength = function() {
  7215. return jsBezier.getLength(this.curve);
  7216. };
  7217. this.getBounds = function() {
  7218. return this.bounds;
  7219. };
  7220. }
  7221. };
  7222. /*
  7223. Class: AbstractComponent
  7224. Superclass for AbstractConnector and AbstractEndpoint.
  7225. */
  7226. var AbstractComponent = function() {
  7227. this.resetBounds = function() {
  7228. this.bounds = { minX:Infinity, minY:Infinity, maxX:-Infinity, maxY:-Infinity };
  7229. };
  7230. this.resetBounds();
  7231. };
  7232. /*
  7233. * Class: AbstractConnector
  7234. * Superclass for all Connectors; here is where Segments are managed. This is exposed on jsPlumb just so it
  7235. * can be accessed from other files. You should not try to instantiate one of these directly.
  7236. *
  7237. * When this class is asked for a pointOnPath, or gradient etc, it must first figure out which segment to dispatch
  7238. * that request to. This is done by keeping track of the total connector length as segments are added, and also
  7239. * their cumulative ratios to the total length. Then when the right segment is found it is a simple case of dispatching
  7240. * the request to it (and adjusting 'location' so that it is relative to the beginning of that segment.)
  7241. */
  7242. jsPlumb.Connectors.AbstractConnector = function(params) {
  7243. AbstractComponent.apply(this, arguments);
  7244. var segments = [],
  7245. totalLength = 0,
  7246. segmentProportions = [],
  7247. segmentProportionalLengths = [],
  7248. stub = params.stub || 0,
  7249. sourceStub = jsPlumbUtil.isArray(stub) ? stub[0] : stub,
  7250. targetStub = jsPlumbUtil.isArray(stub) ? stub[1] : stub,
  7251. gap = params.gap || 0,
  7252. sourceGap = jsPlumbUtil.isArray(gap) ? gap[0] : gap,
  7253. targetGap = jsPlumbUtil.isArray(gap) ? gap[1] : gap,
  7254. userProvidedSegments = null,
  7255. edited = false,
  7256. paintInfo = null;
  7257. // to be overridden by subclasses.
  7258. this.getPath = function() { };
  7259. this.setPath = function(path) { };
  7260. /**
  7261. * Function: findSegmentForPoint
  7262. * Returns the segment that is closest to the given [x,y],
  7263. * null if nothing found. This function returns a JS
  7264. * object with:
  7265. *
  7266. * d - distance from segment
  7267. * l - proportional location in segment
  7268. * x - x point on the segment
  7269. * y - y point on the segment
  7270. * s - the segment itself.
  7271. */
  7272. this.findSegmentForPoint = function(x, y) {
  7273. var out = { d:Infinity, s:null, x:null, y:null, l:null };
  7274. for (var i = 0; i < segments.length; i++) {
  7275. var _s = segments[i].findClosestPointOnPath(x, y);
  7276. if (_s.d < out.d) {
  7277. out.d = _s.d;
  7278. out.l = _s.l;
  7279. out.x = _s.x;
  7280. out.y = _s.y;
  7281. out.s = segments[i];
  7282. out.x1 = _s.x1;
  7283. out.x2 = _s.x2;
  7284. out.y1 = _s.y1;
  7285. out.y2 = _s.y2;
  7286. out.index = i;
  7287. }
  7288. }
  7289. return out;
  7290. };
  7291. var _updateSegmentProportions = function() {
  7292. var curLoc = 0;
  7293. for (var i = 0; i < segments.length; i++) {
  7294. var sl = segments[i].getLength();
  7295. segmentProportionalLengths[i] = sl / totalLength;
  7296. segmentProportions[i] = [curLoc, (curLoc += (sl / totalLength)) ];
  7297. }
  7298. },
  7299. /**
  7300. * returns [segment, proportion of travel in segment, segment index] for the segment
  7301. * that contains the point which is 'location' distance along the entire path, where
  7302. * 'location' is a decimal between 0 and 1 inclusive. in this connector type, paths
  7303. * are made up of a list of segments, each of which contributes some fraction to
  7304. * the total length.
  7305. * From 1.3.10 this also supports the 'absolute' property, which lets us specify a location
  7306. * as the absolute distance in pixels, rather than a proportion of the total path.
  7307. */
  7308. _findSegmentForLocation = function(location, absolute) {
  7309. if (absolute) {
  7310. location = location > 0 ? location / totalLength : (totalLength + location) / totalLength;
  7311. }
  7312. var idx = segmentProportions.length - 1, inSegmentProportion = 1;
  7313. for (var i = 0; i < segmentProportions.length; i++) {
  7314. if (segmentProportions[i][1] >= location) {
  7315. idx = i;
  7316. // todo is this correct for all connector path types?
  7317. inSegmentProportion = location == 1 ? 1 : location === 0 ? 0 : (location - segmentProportions[i][0]) / segmentProportionalLengths[i];
  7318. break;
  7319. }
  7320. }
  7321. return { segment:segments[idx], proportion:inSegmentProportion, index:idx };
  7322. },
  7323. _addSegment = function(conn, type, params) {
  7324. if (params.x1 == params.x2 && params.y1 == params.y2) return;
  7325. var s = new jsPlumb.Segments[type](params);
  7326. segments.push(s);
  7327. totalLength += s.getLength();
  7328. conn.updateBounds(s);
  7329. },
  7330. _clearSegments = function() {
  7331. totalLength = segments.length = segmentProportions.length = segmentProportionalLengths.length = 0;
  7332. };
  7333. this.setSegments = function(_segs) {
  7334. userProvidedSegments = [];
  7335. totalLength = 0;
  7336. for (var i = 0; i < _segs.length; i++) {
  7337. userProvidedSegments.push(_segs[i]);
  7338. totalLength += _segs[i].getLength();
  7339. }
  7340. };
  7341. var _prepareCompute = function(params) {
  7342. this.lineWidth = params.lineWidth;
  7343. var segment = Biltong.quadrant(params.sourcePos, params.targetPos),
  7344. swapX = params.targetPos[0] < params.sourcePos[0],
  7345. swapY = params.targetPos[1] < params.sourcePos[1],
  7346. lw = params.lineWidth || 1,
  7347. so = params.sourceEndpoint.anchor.getOrientation(params.sourceEndpoint),
  7348. to = params.targetEndpoint.anchor.getOrientation(params.targetEndpoint),
  7349. x = swapX ? params.targetPos[0] : params.sourcePos[0],
  7350. y = swapY ? params.targetPos[1] : params.sourcePos[1],
  7351. w = Math.abs(params.targetPos[0] - params.sourcePos[0]),
  7352. h = Math.abs(params.targetPos[1] - params.sourcePos[1]);
  7353. // if either anchor does not have an orientation set, we derive one from their relative
  7354. // positions. we fix the axis to be the one in which the two elements are further apart, and
  7355. // point each anchor at the other element. this is also used when dragging a new connection.
  7356. if (so[0] === 0 && so[1] === 0 || to[0] === 0 && to[1] === 0) {
  7357. var index = w > h ? 0 : 1, oIndex = [1,0][index];
  7358. so = []; to = [];
  7359. so[index] = params.sourcePos[index] > params.targetPos[index] ? -1 : 1;
  7360. to[index] = params.sourcePos[index] > params.targetPos[index] ? 1 : -1;
  7361. so[oIndex] = 0; to[oIndex] = 0;
  7362. }
  7363. var sx = swapX ? w + (sourceGap * so[0]) : sourceGap * so[0],
  7364. sy = swapY ? h + (sourceGap * so[1]) : sourceGap * so[1],
  7365. tx = swapX ? targetGap * to[0] : w + (targetGap * to[0]),
  7366. ty = swapY ? targetGap * to[1] : h + (targetGap * to[1]),
  7367. oProduct = ((so[0] * to[0]) + (so[1] * to[1]));
  7368. var result = {
  7369. sx:sx, sy:sy, tx:tx, ty:ty, lw:lw,
  7370. xSpan:Math.abs(tx - sx),
  7371. ySpan:Math.abs(ty - sy),
  7372. mx:(sx + tx) / 2,
  7373. my:(sy + ty) / 2,
  7374. so:so, to:to, x:x, y:y, w:w, h:h,
  7375. segment : segment,
  7376. startStubX : sx + (so[0] * sourceStub),
  7377. startStubY : sy + (so[1] * sourceStub),
  7378. endStubX : tx + (to[0] * targetStub),
  7379. endStubY : ty + (to[1] * targetStub),
  7380. isXGreaterThanStubTimes2 : Math.abs(sx - tx) > (sourceStub + targetStub),
  7381. isYGreaterThanStubTimes2 : Math.abs(sy - ty) > (sourceStub + targetStub),
  7382. opposite:oProduct == -1,
  7383. perpendicular:oProduct === 0,
  7384. orthogonal:oProduct == 1,
  7385. sourceAxis : so[0] === 0 ? "y" : "x",
  7386. points:[x, y, w, h, sx, sy, tx, ty ]
  7387. };
  7388. result.anchorOrientation = result.opposite ? "opposite" : result.orthogonal ? "orthogonal" : "perpendicular";
  7389. return result;
  7390. };
  7391. this.getSegments = function() { return segments; };
  7392. this.updateBounds = function(segment) {
  7393. var segBounds = segment.getBounds();
  7394. this.bounds.minX = Math.min(this.bounds.minX, segBounds.minX);
  7395. this.bounds.maxX = Math.max(this.bounds.maxX, segBounds.maxX);
  7396. this.bounds.minY = Math.min(this.bounds.minY, segBounds.minY);
  7397. this.bounds.maxY = Math.max(this.bounds.maxY, segBounds.maxY);
  7398. };
  7399. var dumpSegmentsToConsole = function() {
  7400. console.log("SEGMENTS:");
  7401. for (var i = 0; i < segments.length; i++) {
  7402. console.log(segments[i].type, segments[i].getLength(), segmentProportions[i]);
  7403. }
  7404. };
  7405. this.pointOnPath = function(location, absolute) {
  7406. var seg = _findSegmentForLocation(location, absolute);
  7407. return seg.segment && seg.segment.pointOnPath(seg.proportion, false) || [0,0];
  7408. };
  7409. this.gradientAtPoint = function(location, absolute) {
  7410. var seg = _findSegmentForLocation(location, absolute);
  7411. return seg.segment && seg.segment.gradientAtPoint(seg.proportion, false) || 0;
  7412. };
  7413. this.pointAlongPathFrom = function(location, distance, absolute) {
  7414. var seg = _findSegmentForLocation(location, absolute);
  7415. // TODO what happens if this crosses to the next segment?
  7416. return seg.segment && seg.segment.pointAlongPathFrom(seg.proportion, distance, false) || [0,0];
  7417. };
  7418. this.compute = function(params) {
  7419. if (!edited)
  7420. paintInfo = _prepareCompute.call(this, params);
  7421. _clearSegments();
  7422. this._compute(paintInfo, params);
  7423. this.x = paintInfo.points[0];
  7424. this.y = paintInfo.points[1];
  7425. this.w = paintInfo.points[2];
  7426. this.h = paintInfo.points[3];
  7427. this.segment = paintInfo.segment;
  7428. _updateSegmentProportions();
  7429. };
  7430. return {
  7431. addSegment:_addSegment,
  7432. prepareCompute:_prepareCompute,
  7433. sourceStub:sourceStub,
  7434. targetStub:targetStub,
  7435. maxStub:Math.max(sourceStub, targetStub),
  7436. sourceGap:sourceGap,
  7437. targetGap:targetGap,
  7438. maxGap:Math.max(sourceGap, targetGap)
  7439. };
  7440. };
  7441. jsPlumbUtil.extend(jsPlumb.Connectors.AbstractConnector, AbstractComponent);
  7442. /**
  7443. * Class: Connectors.Straight
  7444. * The Straight connector draws a simple straight line between the two anchor points. It does not have any constructor parameters.
  7445. */
  7446. var Straight = jsPlumb.Connectors.Straight = function() {
  7447. this.type = "Straight";
  7448. var _super = jsPlumb.Connectors.AbstractConnector.apply(this, arguments);
  7449. this._compute = function(paintInfo, _) {
  7450. _super.addSegment(this, "Straight", {x1:paintInfo.sx, y1:paintInfo.sy, x2:paintInfo.startStubX, y2:paintInfo.startStubY});
  7451. _super.addSegment(this, "Straight", {x1:paintInfo.startStubX, y1:paintInfo.startStubY, x2:paintInfo.endStubX, y2:paintInfo.endStubY});
  7452. _super.addSegment(this, "Straight", {x1:paintInfo.endStubX, y1:paintInfo.endStubY, x2:paintInfo.tx, y2:paintInfo.ty});
  7453. };
  7454. };
  7455. jsPlumbUtil.extend(jsPlumb.Connectors.Straight, jsPlumb.Connectors.AbstractConnector);
  7456. jsPlumb.registerConnectorType(Straight, "Straight");
  7457. // ********************************* END OF CONNECTOR TYPES *******************************************************************
  7458. // ********************************* ENDPOINT TYPES *******************************************************************
  7459. jsPlumb.Endpoints.AbstractEndpoint = function(params) {
  7460. AbstractComponent.apply(this, arguments);
  7461. var compute = this.compute = function(anchorPoint, orientation, endpointStyle, connectorPaintStyle) {
  7462. var out = this._compute.apply(this, arguments);
  7463. this.x = out[0];
  7464. this.y = out[1];
  7465. this.w = out[2];
  7466. this.h = out[3];
  7467. this.bounds.minX = this.x;
  7468. this.bounds.minY = this.y;
  7469. this.bounds.maxX = this.x + this.w;
  7470. this.bounds.maxY = this.y + this.h;
  7471. return out;
  7472. };
  7473. return {
  7474. compute:compute,
  7475. cssClass:params.cssClass
  7476. };
  7477. };
  7478. jsPlumbUtil.extend(jsPlumb.Endpoints.AbstractEndpoint, AbstractComponent);
  7479. /**
  7480. * Class: Endpoints.Dot
  7481. * A round endpoint, with default radius 10 pixels.
  7482. */
  7483. /**
  7484. * Function: Constructor
  7485. *
  7486. * Parameters:
  7487. *
  7488. * radius - radius of the endpoint. defaults to 10 pixels.
  7489. */
  7490. jsPlumb.Endpoints.Dot = function(params) {
  7491. this.type = "Dot";
  7492. var _super = jsPlumb.Endpoints.AbstractEndpoint.apply(this, arguments);
  7493. params = params || {};
  7494. this.radius = params.radius || 10;
  7495. this.defaultOffset = 0.5 * this.radius;
  7496. this.defaultInnerRadius = this.radius / 3;
  7497. this._compute = function(anchorPoint, orientation, endpointStyle, connectorPaintStyle) {
  7498. this.radius = endpointStyle.radius || this.radius;
  7499. var x = anchorPoint[0] - this.radius,
  7500. y = anchorPoint[1] - this.radius,
  7501. w = this.radius * 2,
  7502. h = this.radius * 2;
  7503. if (endpointStyle.strokeStyle) {
  7504. var lw = endpointStyle.lineWidth || 1;
  7505. x -= lw;
  7506. y -= lw;
  7507. w += (lw * 2);
  7508. h += (lw * 2);
  7509. }
  7510. return [ x, y, w, h, this.radius ];
  7511. };
  7512. };
  7513. jsPlumbUtil.extend(jsPlumb.Endpoints.Dot, jsPlumb.Endpoints.AbstractEndpoint);
  7514. jsPlumb.Endpoints.Rectangle = function(params) {
  7515. this.type = "Rectangle";
  7516. var _super = jsPlumb.Endpoints.AbstractEndpoint.apply(this, arguments);
  7517. params = params || {};
  7518. this.width = params.width || 20;
  7519. this.height = params.height || 20;
  7520. this._compute = function(anchorPoint, orientation, endpointStyle, connectorPaintStyle) {
  7521. var width = endpointStyle.width || this.width,
  7522. height = endpointStyle.height || this.height,
  7523. x = anchorPoint[0] - (width/2),
  7524. y = anchorPoint[1] - (height/2);
  7525. return [ x, y, width, height];
  7526. };
  7527. };
  7528. jsPlumbUtil.extend(jsPlumb.Endpoints.Rectangle, jsPlumb.Endpoints.AbstractEndpoint);
  7529. var DOMElementEndpoint = function(params) {
  7530. jsPlumb.jsPlumbUIComponent.apply(this, arguments);
  7531. this._jsPlumb.displayElements = [];
  7532. };
  7533. jsPlumbUtil.extend(DOMElementEndpoint, jsPlumb.jsPlumbUIComponent, {
  7534. getDisplayElements : function() {
  7535. return this._jsPlumb.displayElements;
  7536. },
  7537. appendDisplayElement : function(el) {
  7538. this._jsPlumb.displayElements.push(el);
  7539. }
  7540. });
  7541. /**
  7542. * Class: Endpoints.Image
  7543. * Draws an image as the Endpoint.
  7544. */
  7545. /**
  7546. * Function: Constructor
  7547. *
  7548. * Parameters:
  7549. *
  7550. * src - location of the image to use.
  7551. TODO: multiple references to self. not sure quite how to get rid of them entirely. perhaps self = null in the cleanup
  7552. function will suffice
  7553. TODO this class still leaks memory.
  7554. */
  7555. jsPlumb.Endpoints.Image = function(params) {
  7556. this.type = "Image";
  7557. DOMElementEndpoint.apply(this, arguments);
  7558. jsPlumb.Endpoints.AbstractEndpoint.apply(this, arguments);
  7559. var _onload = params.onload,
  7560. src = params.src || params.url,
  7561. clazz = params.cssClass ? " " + params.cssClass : "";
  7562. this._jsPlumb.img = new Image();
  7563. this._jsPlumb.ready = false;
  7564. this._jsPlumb.initialized = false;
  7565. this._jsPlumb.deleted = false;
  7566. this._jsPlumb.widthToUse = params.width;
  7567. this._jsPlumb.heightToUse = params.height;
  7568. this._jsPlumb.endpoint = params.endpoint;
  7569. this._jsPlumb.img.onload = function() {
  7570. if (this._jsPlumb != null) {
  7571. this._jsPlumb.ready = true;
  7572. this._jsPlumb.widthToUse = this._jsPlumb.widthToUse || this._jsPlumb.img.width;
  7573. this._jsPlumb.heightToUse = this._jsPlumb.heightToUse || this._jsPlumb.img.height;
  7574. if (_onload) {
  7575. _onload(this);
  7576. }
  7577. }
  7578. }.bind(this);
  7579. /*
  7580. Function: setImage
  7581. Sets the Image to use in this Endpoint.
  7582. Parameters:
  7583. img - may be a URL or an Image object
  7584. onload - optional; a callback to execute once the image has loaded.
  7585. */
  7586. this._jsPlumb.endpoint.setImage = function(_img, onload) {
  7587. var s = _img.constructor == String ? _img : _img.src;
  7588. _onload = onload;
  7589. this._jsPlumb.img.src = s;
  7590. if (this.canvas != null)
  7591. this.canvas.setAttribute("src", this._jsPlumb.img.src);
  7592. }.bind(this);
  7593. this._jsPlumb.endpoint.setImage(src, _onload);
  7594. this._compute = function(anchorPoint, orientation, endpointStyle, connectorPaintStyle) {
  7595. this.anchorPoint = anchorPoint;
  7596. if (this._jsPlumb.ready) return [anchorPoint[0] - this._jsPlumb.widthToUse / 2, anchorPoint[1] - this._jsPlumb.heightToUse / 2,
  7597. this._jsPlumb.widthToUse, this._jsPlumb.heightToUse];
  7598. else return [0,0,0,0];
  7599. };
  7600. this.canvas = document.createElement("img");
  7601. this.canvas.style.margin = 0;
  7602. this.canvas.style.padding = 0;
  7603. this.canvas.style.outline = 0;
  7604. this.canvas.style.position = "absolute";
  7605. this.canvas.className = this._jsPlumb.instance.endpointClass + clazz;
  7606. if (this._jsPlumb.widthToUse) this.canvas.setAttribute("width", this._jsPlumb.widthToUse);
  7607. if (this._jsPlumb.heightToUse) this.canvas.setAttribute("height", this._jsPlumb.heightToUse);
  7608. this._jsPlumb.instance.appendElement(this.canvas);
  7609. this.actuallyPaint = function(d, style, anchor) {
  7610. if (!this._jsPlumb.deleted) {
  7611. if (!this._jsPlumb.initialized) {
  7612. this.canvas.setAttribute("src", this._jsPlumb.img.src);
  7613. this.appendDisplayElement(this.canvas);
  7614. this._jsPlumb.initialized = true;
  7615. }
  7616. var x = this.anchorPoint[0] - (this._jsPlumb.widthToUse / 2),
  7617. y = this.anchorPoint[1] - (this._jsPlumb.heightToUse / 2);
  7618. jsPlumbUtil.sizeElement(this.canvas, x, y, this._jsPlumb.widthToUse, this._jsPlumb.heightToUse);
  7619. }
  7620. };
  7621. this.paint = function(style, anchor) {
  7622. if (this._jsPlumb != null) { // may have been deleted
  7623. if (this._jsPlumb.ready) {
  7624. this.actuallyPaint(style, anchor);
  7625. }
  7626. else {
  7627. window.setTimeout(function() {
  7628. this.paint(style, anchor);
  7629. }.bind(this), 200);
  7630. }
  7631. }
  7632. };
  7633. };
  7634. jsPlumbUtil.extend(jsPlumb.Endpoints.Image, [ DOMElementEndpoint, jsPlumb.Endpoints.AbstractEndpoint ], {
  7635. cleanup : function() {
  7636. this._jsPlumb.deleted = true;
  7637. if (this.canvas) this.canvas.parentNode.removeChild(this.canvas);
  7638. this.canvas = null;
  7639. }
  7640. });
  7641. /*
  7642. * Class: Endpoints.Blank
  7643. * An Endpoint that paints nothing (visible) on the screen. Supports cssClass and hoverClass parameters like all Endpoints.
  7644. */
  7645. jsPlumb.Endpoints.Blank = function(params) {
  7646. var _super = jsPlumb.Endpoints.AbstractEndpoint.apply(this, arguments);
  7647. this.type = "Blank";
  7648. DOMElementEndpoint.apply(this, arguments);
  7649. this._compute = function(anchorPoint, orientation, endpointStyle, connectorPaintStyle) {
  7650. return [anchorPoint[0], anchorPoint[1],10,0];
  7651. };
  7652. var clazz = params.cssClass ? " " + params.cssClass : "";
  7653. this.canvas = document.createElement("div");
  7654. this.canvas.style.display = "block";
  7655. this.canvas.style.width = "1px";
  7656. this.canvas.style.height = "1px";
  7657. this.canvas.style.background = "transparent";
  7658. this.canvas.style.position = "absolute";
  7659. this.canvas.className = this._jsPlumb.instance.endpointClass + clazz;
  7660. this._jsPlumb.instance.appendElement(this.canvas);
  7661. this.paint = function(style, anchor) {
  7662. jsPlumbUtil.sizeElement(this.canvas, this.x, this.y, this.w, this.h);
  7663. };
  7664. };
  7665. jsPlumbUtil.extend(jsPlumb.Endpoints.Blank, [jsPlumb.Endpoints.AbstractEndpoint, DOMElementEndpoint], {
  7666. cleanup:function() {
  7667. if (this.canvas && this.canvas.parentNode) {
  7668. this.canvas.parentNode.removeChild(this.canvas);
  7669. }
  7670. }
  7671. });
  7672. /*
  7673. * Class: Endpoints.Triangle
  7674. * A triangular Endpoint.
  7675. */
  7676. /*
  7677. * Function: Constructor
  7678. *
  7679. * Parameters:
  7680. *
  7681. * width - width of the triangle's base. defaults to 55 pixels.
  7682. * height - height of the triangle from base to apex. defaults to 55 pixels.
  7683. */
  7684. jsPlumb.Endpoints.Triangle = function(params) {
  7685. this.type = "Triangle";
  7686. jsPlumb.Endpoints.AbstractEndpoint.apply(this, arguments);
  7687. params = params || { };
  7688. params.width = params.width || 55;
  7689. params.height = params.height || 55;
  7690. this.width = params.width;
  7691. this.height = params.height;
  7692. this._compute = function(anchorPoint, orientation, endpointStyle, connectorPaintStyle) {
  7693. var width = endpointStyle.width || self.width,
  7694. height = endpointStyle.height || self.height,
  7695. x = anchorPoint[0] - (width/2),
  7696. y = anchorPoint[1] - (height/2);
  7697. return [ x, y, width, height ];
  7698. };
  7699. };
  7700. // ********************************* END OF ENDPOINT TYPES *******************************************************************
  7701. // ********************************* OVERLAY DEFINITIONS ***********************************************************************
  7702. var AbstractOverlay = jsPlumb.Overlays.AbstractOverlay = function(params) {
  7703. this.visible = true;
  7704. this.isAppendedAtTopLevel = true;
  7705. this.component = params.component;
  7706. this.loc = params.location == null ? 0.5 : params.location;
  7707. this.endpointLoc = params.endpointLocation == null ? [ 0.5, 0.5] : params.endpointLocation;
  7708. };
  7709. AbstractOverlay.prototype = {
  7710. cleanup:function() {
  7711. this.component = null;
  7712. this.canvas = null;
  7713. this.endpointLoc = null;
  7714. },
  7715. setVisible : function(val) {
  7716. this.visible = val;
  7717. this.component.repaint();
  7718. },
  7719. isVisible : function() { return this.visible; },
  7720. hide : function() { this.setVisible(false); },
  7721. show : function() { this.setVisible(true); },
  7722. incrementLocation : function(amount) {
  7723. this.loc += amount;
  7724. this.component.repaint();
  7725. },
  7726. setLocation : function(l) {
  7727. this.loc = l;
  7728. this.component.repaint();
  7729. },
  7730. getLocation : function() {
  7731. return this.loc;
  7732. }
  7733. };
  7734. /*
  7735. * Class: Overlays.Arrow
  7736. *
  7737. * 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
  7738. * of the arrow that lines from each tail point converge into. The foldback point is defined using a decimal that indicates some fraction
  7739. * 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
  7740. * across the tail.
  7741. */
  7742. /*
  7743. * Function: Constructor
  7744. *
  7745. * Parameters:
  7746. *
  7747. * length - distance in pixels from head to tail baseline. default 20.
  7748. * width - width in pixels of the tail baseline. default 20.
  7749. * fillStyle - style to use when filling the arrow. defaults to "black".
  7750. * strokeStyle - style to use when stroking the arrow. defaults to null, which means the arrow is not stroked.
  7751. * lineWidth - line width to use when stroking the arrow. defaults to 1, but only used if strokeStyle is not null.
  7752. * 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.
  7753. * location - distance (as a decimal from 0 to 1 inclusive) marking where the arrow should sit on the connector. defaults to 0.5.
  7754. * direction - indicates the direction the arrow points in. valid values are -1 and 1; 1 is default.
  7755. */
  7756. jsPlumb.Overlays.Arrow = function(params) {
  7757. this.type = "Arrow";
  7758. AbstractOverlay.apply(this, arguments);
  7759. this.isAppendedAtTopLevel = false;
  7760. params = params || {};
  7761. var _ju = jsPlumbUtil, _jg = Biltong;
  7762. this.length = params.length || 20;
  7763. this.width = params.width || 20;
  7764. this.id = params.id;
  7765. var direction = (params.direction || 1) < 0 ? -1 : 1,
  7766. paintStyle = params.paintStyle || { lineWidth:1 },
  7767. // how far along the arrow the lines folding back in come to. default is 62.3%.
  7768. foldback = params.foldback || 0.623;
  7769. this.computeMaxSize = function() { return self.width * 1.5; };
  7770. this.draw = function(component, currentConnectionPaintStyle) {
  7771. var hxy, mid, txy, tail, cxy;
  7772. if (component.pointAlongPathFrom) {
  7773. if (_ju.isString(this.loc) || this.loc > 1 || this.loc < 0) {
  7774. var l = parseInt(this.loc, 10),
  7775. fromLoc = this.loc < 0 ? 1 : 0;
  7776. hxy = component.pointAlongPathFrom(fromLoc, l, false);
  7777. mid = component.pointAlongPathFrom(fromLoc, l - (direction * this.length / 2), false);
  7778. txy = _jg.pointOnLine(hxy, mid, this.length);
  7779. }
  7780. else if (this.loc == 1) {
  7781. hxy = component.pointOnPath(this.loc);
  7782. mid = component.pointAlongPathFrom(this.loc, -(this.length));
  7783. txy = _jg.pointOnLine(hxy, mid, this.length);
  7784. if (direction == -1) {
  7785. var _ = txy;
  7786. txy = hxy;
  7787. hxy = _;
  7788. }
  7789. }
  7790. else if (this.loc === 0) {
  7791. txy = component.pointOnPath(this.loc);
  7792. mid = component.pointAlongPathFrom(this.loc, this.length);
  7793. hxy = _jg.pointOnLine(txy, mid, this.length);
  7794. if (direction == -1) {
  7795. var __ = txy;
  7796. txy = hxy;
  7797. hxy = __;
  7798. }
  7799. }
  7800. else {
  7801. hxy = component.pointAlongPathFrom(this.loc, direction * this.length / 2);
  7802. mid = component.pointOnPath(this.loc);
  7803. txy = _jg.pointOnLine(hxy, mid, this.length);
  7804. }
  7805. tail = _jg.perpendicularLineTo(hxy, txy, this.width);
  7806. cxy = _jg.pointOnLine(hxy, txy, foldback * this.length);
  7807. var d = { hxy:hxy, tail:tail, cxy:cxy },
  7808. strokeStyle = paintStyle.strokeStyle || currentConnectionPaintStyle.strokeStyle,
  7809. fillStyle = paintStyle.fillStyle || currentConnectionPaintStyle.strokeStyle,
  7810. lineWidth = paintStyle.lineWidth || currentConnectionPaintStyle.lineWidth;
  7811. return {
  7812. component:component,
  7813. d:d,
  7814. lineWidth:lineWidth,
  7815. strokeStyle:strokeStyle,
  7816. fillStyle:fillStyle,
  7817. minX:Math.min(hxy.x, tail[0].x, tail[1].x),
  7818. maxX:Math.max(hxy.x, tail[0].x, tail[1].x),
  7819. minY:Math.min(hxy.y, tail[0].y, tail[1].y),
  7820. maxY:Math.max(hxy.y, tail[0].y, tail[1].y)
  7821. };
  7822. }
  7823. else return {component:component, minX:0,maxX:0,minY:0,maxY:0};
  7824. };
  7825. };
  7826. jsPlumbUtil.extend(jsPlumb.Overlays.Arrow, AbstractOverlay);
  7827. /*
  7828. * Class: Overlays.PlainArrow
  7829. *
  7830. * 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
  7831. * point along the length of the arrow: in this case, that foldback point is the full length of the arrow. so it just does
  7832. * a 'call' to Arrow with foldback set appropriately.
  7833. */
  7834. /*
  7835. * Function: Constructor
  7836. * See <Overlays.Arrow> for allowed parameters for this overlay.
  7837. */
  7838. jsPlumb.Overlays.PlainArrow = function(params) {
  7839. params = params || {};
  7840. var p = jsPlumb.extend(params, {foldback:1});
  7841. jsPlumb.Overlays.Arrow.call(this, p);
  7842. this.type = "PlainArrow";
  7843. };
  7844. jsPlumbUtil.extend(jsPlumb.Overlays.PlainArrow, jsPlumb.Overlays.Arrow);
  7845. /*
  7846. * Class: Overlays.Diamond
  7847. *
  7848. * A diamond. Like PlainArrow, this is a concrete case of the more generic case of the tail points converging on some point...it just
  7849. * happens that in this case, that point is greater than the length of the the arrow.
  7850. *
  7851. * this could probably do with some help with positioning...due to the way it reuses the Arrow paint code, what Arrow thinks is the
  7852. * 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
  7853. * stuck when it comes to helping out the Arrow class. possibly we could pass in a 'transpose' parameter or something. the value
  7854. * would be -l/4 in this case - move along one quarter of the total length.
  7855. */
  7856. /*
  7857. * Function: Constructor
  7858. * See <Overlays.Arrow> for allowed parameters for this overlay.
  7859. */
  7860. jsPlumb.Overlays.Diamond = function(params) {
  7861. params = params || {};
  7862. var l = params.length || 40,
  7863. p = jsPlumb.extend(params, {length:l/2, foldback:2});
  7864. jsPlumb.Overlays.Arrow.call(this, p);
  7865. this.type = "Diamond";
  7866. };
  7867. jsPlumbUtil.extend(jsPlumb.Overlays.Diamond, jsPlumb.Overlays.Arrow);
  7868. var _getDimensions = function(component, forceRefresh) {
  7869. if (component._jsPlumb.cachedDimensions == null || forceRefresh)
  7870. component._jsPlumb.cachedDimensions = component.getDimensions();
  7871. return component._jsPlumb.cachedDimensions;
  7872. };
  7873. // abstract superclass for overlays that add an element to the DOM.
  7874. var AbstractDOMOverlay = function(params) {
  7875. jsPlumb.jsPlumbUIComponent.apply(this, arguments);
  7876. AbstractOverlay.apply(this, arguments);
  7877. // hand off fired events to associated component.
  7878. var _f = this.fire;
  7879. this.fire = function() {
  7880. _f.apply(this, arguments);
  7881. if (this.component) this.component.fire.apply(this.component, arguments);
  7882. };
  7883. this.id = params.id;
  7884. this._jsPlumb.div = null;
  7885. this._jsPlumb.initialised = false;
  7886. this._jsPlumb.component = params.component;
  7887. this._jsPlumb.cachedDimensions = null;
  7888. this._jsPlumb.create = params.create;
  7889. this._jsPlumb.initiallyInvisible = params.visible === false;
  7890. this.getElement = function() {
  7891. if (this._jsPlumb.div == null) {
  7892. var div = this._jsPlumb.div = jsPlumb.getDOMElement(this._jsPlumb.create(this._jsPlumb.component));
  7893. div.style.position = "absolute";
  7894. div.className = this._jsPlumb.instance.overlayClass + " " +
  7895. (this.cssClass ? this.cssClass :
  7896. params.cssClass ? params.cssClass : "");
  7897. this._jsPlumb.instance.appendElement(div);
  7898. this._jsPlumb.instance.getId(div);
  7899. this.canvas = div;
  7900. // in IE the top left corner is what it placed at the desired location. This will not
  7901. // be fixed. IE8 is not going to be supported for much longer.
  7902. var ts = "translate(-50%, -50%)";
  7903. div.style.webkitTransform = ts;
  7904. div.style.mozTransform = ts;
  7905. div.style.msTransform = ts;
  7906. div.style.oTransform = ts;
  7907. div.style.transform = ts;
  7908. // write the related component into the created element
  7909. div._jsPlumb = this;
  7910. if (params.visible === false)
  7911. div.style.display = "none";
  7912. }
  7913. return this._jsPlumb.div;
  7914. };
  7915. this.draw = function(component, currentConnectionPaintStyle, absolutePosition) {
  7916. var td = _getDimensions(this);
  7917. if (td != null && td.length == 2) {
  7918. var cxy = { x:0,y:0 };
  7919. // absolutePosition would have been set by a call to connection.setAbsoluteOverlayPosition.
  7920. if (absolutePosition) {
  7921. cxy = { x:absolutePosition[0], y:absolutePosition[1] };
  7922. }
  7923. else if (component.pointOnPath) {
  7924. var loc = this.loc, absolute = false;
  7925. if (jsPlumbUtil.isString(this.loc) || this.loc < 0 || this.loc > 1) {
  7926. loc = parseInt(this.loc, 10);
  7927. absolute = true;
  7928. }
  7929. cxy = component.pointOnPath(loc, absolute); // a connection
  7930. }
  7931. else {
  7932. var locToUse = this.loc.constructor == Array ? this.loc : this.endpointLoc;
  7933. cxy = { x:locToUse[0] * component.w,
  7934. y:locToUse[1] * component.h };
  7935. }
  7936. var minx = cxy.x - (td[0] / 2),
  7937. miny = cxy.y - (td[1] / 2);
  7938. return {
  7939. component:component,
  7940. d:{ minx:minx, miny:miny, td:td, cxy:cxy },
  7941. minX:minx,
  7942. maxX:minx + td[0],
  7943. minY:miny,
  7944. maxY:miny + td[1]
  7945. };
  7946. }
  7947. else return {minX:0,maxX:0,minY:0,maxY:0};
  7948. };
  7949. };
  7950. jsPlumbUtil.extend(AbstractDOMOverlay, [jsPlumb.jsPlumbUIComponent, AbstractOverlay], {
  7951. getDimensions : function() {
  7952. // still support the old way, for now, for IE8. But from 2.0.0 this whole method will be gone.
  7953. return jsPlumbUtil.oldIE ? jsPlumb.getSize(this.getElement()) : [1,1];
  7954. },
  7955. setVisible : function(state) {
  7956. this._jsPlumb.div.style.display = state ? "block" : "none";
  7957. // if initially invisible, dimensions are 0,0 and never get updated
  7958. if (state && this._jsPlumb.initiallyInvisible) {
  7959. _getDimensions(this, true);
  7960. this.component.repaint();
  7961. this._jsPlumb.initiallyInvisible = false;
  7962. }
  7963. },
  7964. /*
  7965. * Function: clearCachedDimensions
  7966. * Clears the cached dimensions for the label. As a performance enhancement, label dimensions are
  7967. * cached from 1.3.12 onwards. The cache is cleared when you change the label text, of course, but
  7968. * there are other reasons why the text dimensions might change - if you make a change through CSS, for
  7969. * example, you might change the font size. in that case you should explicitly call this method.
  7970. */
  7971. clearCachedDimensions : function() {
  7972. this._jsPlumb.cachedDimensions = null;
  7973. },
  7974. cleanup : function() {
  7975. if (this._jsPlumb.div != null) {
  7976. this._jsPlumb.div._jsPlumb = null;
  7977. this._jsPlumb.instance.removeElement(this._jsPlumb.div);
  7978. }
  7979. },
  7980. computeMaxSize : function() {
  7981. var td = _getDimensions(this);
  7982. return Math.max(td[0], td[1]);
  7983. },
  7984. paint : function(p, containerExtents) {
  7985. if (!this._jsPlumb.initialised) {
  7986. this.getElement();
  7987. p.component.appendDisplayElement(this._jsPlumb.div);
  7988. this._jsPlumb.initialised = true;
  7989. }
  7990. this._jsPlumb.div.style.left = (p.component.x + p.d.minx) + "px";
  7991. this._jsPlumb.div.style.top = (p.component.y + p.d.miny) + "px";
  7992. }
  7993. });
  7994. /*
  7995. * Class: Overlays.Custom
  7996. * A Custom overlay. You supply a 'create' function which returns some DOM element, and jsPlumb positions it.
  7997. * The 'create' function is passed a Connection or Endpoint.
  7998. */
  7999. /*
  8000. * Function: Constructor
  8001. *
  8002. * Parameters:
  8003. * create - function for jsPlumb to call that returns a DOM element.
  8004. * location - distance (as a decimal from 0 to 1 inclusive) marking where the label should sit on the connector. defaults to 0.5.
  8005. * id - optional id to use for later retrieval of this overlay.
  8006. *
  8007. */
  8008. jsPlumb.Overlays.Custom = function(params) {
  8009. this.type = "Custom";
  8010. AbstractDOMOverlay.apply(this, arguments);
  8011. };
  8012. jsPlumbUtil.extend(jsPlumb.Overlays.Custom, AbstractDOMOverlay);
  8013. jsPlumb.Overlays.GuideLines = function() {
  8014. var self = this;
  8015. self.length = 50;
  8016. self.lineWidth = 5;
  8017. this.type = "GuideLines";
  8018. AbstractOverlay.apply(this, arguments);
  8019. jsPlumb.jsPlumbUIComponent.apply(this, arguments);
  8020. this.draw = function(connector, currentConnectionPaintStyle) {
  8021. var head = connector.pointAlongPathFrom(self.loc, self.length / 2),
  8022. mid = connector.pointOnPath(self.loc),
  8023. tail = Biltong.pointOnLine(head, mid, self.length),
  8024. tailLine = Biltong.perpendicularLineTo(head, tail, 40),
  8025. headLine = Biltong.perpendicularLineTo(tail, head, 20);
  8026. return {
  8027. connector:connector,
  8028. head:head,
  8029. tail:tail,
  8030. headLine:headLine,
  8031. tailLine:tailLine,
  8032. minX:Math.min(head.x, tail.x, headLine[0].x, headLine[1].x),
  8033. minY:Math.min(head.y, tail.y, headLine[0].y, headLine[1].y),
  8034. maxX:Math.max(head.x, tail.x, headLine[0].x, headLine[1].x),
  8035. maxY:Math.max(head.y, tail.y, headLine[0].y, headLine[1].y)
  8036. };
  8037. };
  8038. // this.cleanup = function() { }; // nothing to clean up for GuideLines
  8039. };
  8040. /*
  8041. * Class: Overlays.Label
  8042. */
  8043. /*
  8044. * Function: Constructor
  8045. *
  8046. * Parameters:
  8047. * cssClass - optional css class string to append to css class. This string is appended "as-is", so you can of course have multiple classes
  8048. * defined. This parameter is preferred to using labelStyle, borderWidth and borderStyle.
  8049. * 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
  8050. * label function returns null. empty strings _will_ be painted.
  8051. * location - distance (as a decimal from 0 to 1 inclusive) marking where the label should sit on the connector. defaults to 0.5.
  8052. * id - optional id to use for later retrieval of this overlay.
  8053. *
  8054. *
  8055. */
  8056. jsPlumb.Overlays.Label = function(params) {
  8057. this.labelStyle = params.labelStyle;
  8058. var labelWidth = null, labelHeight = null, labelText = null, labelPadding = null;
  8059. this.cssClass = this.labelStyle != null ? this.labelStyle.cssClass : null;
  8060. var p = jsPlumb.extend({
  8061. create : function() {
  8062. return document.createElement("div");
  8063. }}, params);
  8064. jsPlumb.Overlays.Custom.call(this, p);
  8065. this.type = "Label";
  8066. this.label = params.label || "";
  8067. this.labelText = null;
  8068. if (this.labelStyle) {
  8069. var el = this.getElement();
  8070. this.labelStyle.font = this.labelStyle.font || "12px sans-serif";
  8071. el.style.font = this.labelStyle.font;
  8072. el.style.color = this.labelStyle.color || "black";
  8073. if (this.labelStyle.fillStyle) el.style.background = this.labelStyle.fillStyle;
  8074. if (this.labelStyle.borderWidth > 0) {
  8075. var dStyle = this.labelStyle.borderStyle ? this.labelStyle.borderStyle : "black";
  8076. el.style.border = this.labelStyle.borderWidth + "px solid " + dStyle;
  8077. }
  8078. if (this.labelStyle.padding) el.style.padding = this.labelStyle.padding;
  8079. }
  8080. };
  8081. jsPlumbUtil.extend(jsPlumb.Overlays.Label, jsPlumb.Overlays.Custom, {
  8082. cleanup:function() {
  8083. this.div = null;
  8084. this.label = null;
  8085. this.labelText = null;
  8086. this.cssClass = null;
  8087. this.labelStyle = null;
  8088. },
  8089. getLabel : function() {
  8090. return this.label;
  8091. },
  8092. /*
  8093. * Function: setLabel
  8094. * sets the label's, um, label. you would think i'd call this function
  8095. * 'setText', but you can pass either a Function or a String to this, so
  8096. * it makes more sense as 'setLabel'. This uses innerHTML on the label div, so keep
  8097. * that in mind if you need escaped HTML.
  8098. */
  8099. setLabel : function(l) {
  8100. this.label = l;
  8101. this.labelText = null;
  8102. this.clearCachedDimensions();
  8103. this.update();
  8104. this.component.repaint();
  8105. },
  8106. getDimensions : function() {
  8107. this.update();
  8108. return AbstractDOMOverlay.prototype.getDimensions.apply(this, arguments);
  8109. },
  8110. update : function() {
  8111. if (typeof this.label == "function") {
  8112. var lt = this.label(this);
  8113. this.getElement().innerHTML = lt.replace(/\r\n/g, "<br/>");
  8114. }
  8115. else {
  8116. if (this.labelText == null) {
  8117. this.labelText = this.label;
  8118. this.getElement().innerHTML = this.labelText.replace(/\r\n/g, "<br/>");
  8119. }
  8120. }
  8121. }
  8122. });
  8123. // ********************************* END OF OVERLAY DEFINITIONS ***********************************************************************
  8124. })();
  8125. /*
  8126. * jsPlumb
  8127. *
  8128. * Title:jsPlumb 1.7.2
  8129. *
  8130. * Provides a way to visually connect elements on an HTML page, using SVG or VML.
  8131. *
  8132. * This file contains the base class for library adapters. From 1.7.2 onwards all event management internal to jsPlumb is handled
  8133. * through Mottle, regardless of the underlying library. Dragging - and the events associated with it - is still handled
  8134. * by the library.
  8135. *
  8136. * Copyright (c) 2010 - 2014 Simon Porritt (simon@jsplumbtoolkit.com)
  8137. *
  8138. * http://jsplumbtoolkit.com
  8139. * http://github.com/sporritt/jsplumb
  8140. *
  8141. * Dual licensed under the MIT and GPL2 licenses.
  8142. */
  8143. ;(function() {
  8144. "use strict";
  8145. var _getEventManager = function(instance) {
  8146. var e = instance._mottle;
  8147. if (!e) {
  8148. e = instance._mottle = new Mottle();
  8149. }
  8150. return e;
  8151. };
  8152. jsPlumb.extend(jsPlumbInstance.prototype, {
  8153. getEventManager:function() {
  8154. return _getEventManager(this);
  8155. },
  8156. // EVENTS
  8157. // e.originalEvent is for jQuery; in Vanilla jsPlumb we get the native event.
  8158. on : function(el, event, callback) {
  8159. // TODO: here we would like to map the tap event if we know its
  8160. // an internal bind to a click. we have to know its internal because only
  8161. // then can we be sure that the UP event wont be consumed (tap is a synthesized
  8162. // event from a mousedown followed by a mouseup).
  8163. //event = { "click":"tap", "dblclick":"dbltap"}[event] || event;
  8164. this.getEventManager().on.apply(this, arguments);
  8165. },
  8166. off : function(el, event, callback) {
  8167. this.getEventManager().off.apply(this, arguments);
  8168. }
  8169. });
  8170. }).call(this);
  8171. /*
  8172. * jsPlumb
  8173. *
  8174. * Title:jsPlumb 1.7.2
  8175. *
  8176. * Provides a way to visually connect elements on an HTML page, using SVG or VML.
  8177. *
  8178. * This file contains the 'flowchart' connectors, consisting of vertical and horizontal line segments.
  8179. *
  8180. * Copyright (c) 2010 - 2014 Simon Porritt (simon@jsplumbtoolkit.com)
  8181. *
  8182. * http://jsplumbtoolkit.com
  8183. * http://github.com/sporritt/jsplumb
  8184. *
  8185. * Dual licensed under the MIT and GPL2 licenses.
  8186. */
  8187. ;(function() {
  8188. "use strict";
  8189. /**
  8190. * Function: Constructor
  8191. *
  8192. * Parameters:
  8193. * stub - minimum length for the stub at each end of the connector. This can be an integer, giving a value for both ends of the connections,
  8194. * or an array of two integers, giving separate values for each end. The default is an integer with value 30 (pixels).
  8195. * gap - gap to leave between the end of the connector and the element on which the endpoint resides. if you make this larger than stub then you will see some odd looking behaviour.
  8196. Like stub, this can be an array or a single value. defaults to 0 pixels for each end.
  8197. * cornerRadius - optional, defines the radius of corners between segments. defaults to 0 (hard edged corners).
  8198. * alwaysRespectStubs - defaults to false. whether or not the connectors should always draw the stub, or, if the two elements
  8199. are in close proximity to each other (closer than the sum of the two stubs), to adjust the stubs.
  8200. */
  8201. var Flowchart = function(params) {
  8202. this.type = "Flowchart";
  8203. params = params || {};
  8204. params.stub = params.stub == null ? 30 : params.stub;
  8205. var self = this,
  8206. _super = jsPlumb.Connectors.AbstractConnector.apply(this, arguments),
  8207. midpoint = params.midpoint == null ? 0.5 : params.midpoint,
  8208. points = [], segments = [],
  8209. grid = params.grid,
  8210. alwaysRespectStubs = params.alwaysRespectStubs,
  8211. userSuppliedSegments = null,
  8212. lastx = null, lasty = null, lastOrientation,
  8213. cornerRadius = params.cornerRadius != null ? params.cornerRadius : 0,
  8214. sgn = function(n) { return n < 0 ? -1 : n === 0 ? 0 : 1; },
  8215. /**
  8216. * helper method to add a segment.
  8217. */
  8218. addSegment = function(segments, x, y, paintInfo) {
  8219. if (lastx == x && lasty == y) return;
  8220. var lx = lastx == null ? paintInfo.sx : lastx,
  8221. ly = lasty == null ? paintInfo.sy : lasty,
  8222. o = lx == x ? "v" : "h",
  8223. sgnx = sgn(x - lx),
  8224. sgny = sgn(y - ly);
  8225. lastx = x;
  8226. lasty = y;
  8227. segments.push([lx, ly, x, y, o, sgnx, sgny]);
  8228. },
  8229. segLength = function(s) {
  8230. return Math.sqrt(Math.pow(s[0] - s[2], 2) + Math.pow(s[1] - s[3], 2));
  8231. },
  8232. _cloneArray = function(a) { var _a = []; _a.push.apply(_a, a); return _a;},
  8233. updateMinMax = function(a1) {
  8234. self.bounds.minX = Math.min(self.bounds.minX, a1[2]);
  8235. self.bounds.maxX = Math.max(self.bounds.maxX, a1[2]);
  8236. self.bounds.minY = Math.min(self.bounds.minY, a1[3]);
  8237. self.bounds.maxY = Math.max(self.bounds.maxY, a1[3]);
  8238. },
  8239. writeSegments = function(conn, segments, paintInfo) {
  8240. var current, next;
  8241. for (var i = 0; i < segments.length - 1; i++) {
  8242. current = current || _cloneArray(segments[i]);
  8243. next = _cloneArray(segments[i + 1]);
  8244. if (cornerRadius > 0 && current[4] != next[4]) {
  8245. var radiusToUse = Math.min(cornerRadius, segLength(current), segLength(next));
  8246. // right angle. adjust current segment's end point, and next segment's start point.
  8247. current[2] -= current[5] * radiusToUse;
  8248. current[3] -= current[6] * radiusToUse;
  8249. next[0] += next[5] * radiusToUse;
  8250. next[1] += next[6] * radiusToUse;
  8251. var ac = (current[6] == next[5] && next[5] == 1) ||
  8252. ((current[6] == next[5] && next[5] === 0) && current[5] != next[6]) ||
  8253. (current[6] == next[5] && next[5] == -1),
  8254. sgny = next[1] > current[3] ? 1 : -1,
  8255. sgnx = next[0] > current[2] ? 1 : -1,
  8256. sgnEqual = sgny == sgnx,
  8257. cx = (sgnEqual && ac || (!sgnEqual && !ac)) ? next[0] : current[2],
  8258. cy = (sgnEqual && ac || (!sgnEqual && !ac)) ? current[3] : next[1];
  8259. _super.addSegment(conn, "Straight", {
  8260. x1:current[0], y1:current[1], x2:current[2], y2:current[3]
  8261. });
  8262. _super.addSegment(conn, "Arc", {
  8263. r:radiusToUse,
  8264. x1:current[2],
  8265. y1:current[3],
  8266. x2:next[0],
  8267. y2:next[1],
  8268. cx:cx,
  8269. cy:cy,
  8270. ac:ac
  8271. });
  8272. }
  8273. else {
  8274. // dx + dy are used to adjust for line width.
  8275. var dx = (current[2] == current[0]) ? 0 : (current[2] > current[0]) ? (paintInfo.lw / 2) : -(paintInfo.lw / 2),
  8276. dy = (current[3] == current[1]) ? 0 : (current[3] > current[1]) ? (paintInfo.lw / 2) : -(paintInfo.lw / 2);
  8277. _super.addSegment(conn, "Straight", {
  8278. x1:current[0]- dx, y1:current[1]-dy, x2:current[2] + dx, y2:current[3] + dy
  8279. });
  8280. }
  8281. current = next;
  8282. }
  8283. if (next != null) {
  8284. // last segment
  8285. _super.addSegment(conn, "Straight", {
  8286. x1:next[0], y1:next[1], x2:next[2], y2:next[3]
  8287. });
  8288. }
  8289. };
  8290. this.setSegments = function(s) {
  8291. userSuppliedSegments = s;
  8292. };
  8293. this.isEditable = function() { return true; };
  8294. /*
  8295. Function: getOriginalSegments
  8296. Gets the segments before the addition of rounded corners. This is used by the flowchart
  8297. connector editor, since it only wants to concern itself with the original segments.
  8298. */
  8299. this.getOriginalSegments = function() {
  8300. return userSuppliedSegments || segments;
  8301. };
  8302. this._compute = function(paintInfo, params) {
  8303. if (params.clearEdits)
  8304. userSuppliedSegments = null;
  8305. if (userSuppliedSegments != null) {
  8306. writeSegments(this, userSuppliedSegments, paintInfo);
  8307. return;
  8308. }
  8309. segments = [];
  8310. lastx = null; lasty = null;
  8311. lastOrientation = null;
  8312. var midx = paintInfo.startStubX + ((paintInfo.endStubX - paintInfo.startStubX) * midpoint),
  8313. midy = paintInfo.startStubY + ((paintInfo.endStubY - paintInfo.startStubY) * midpoint);
  8314. var findClearedLine = function(start, mult, anchorPos, dimension) {
  8315. return start + (mult * (( 1 - anchorPos) * dimension) + _super.maxStub);
  8316. },
  8317. orientations = { x:[ 0, 1 ], y:[ 1, 0 ] },
  8318. commonStubCalculator = function(axis) {
  8319. return [ paintInfo.startStubX, paintInfo.startStubY, paintInfo.endStubX, paintInfo.endStubY ];
  8320. },
  8321. stubCalculators = {
  8322. perpendicular:commonStubCalculator,
  8323. orthogonal:commonStubCalculator,
  8324. opposite:function(axis) {
  8325. var pi = paintInfo,
  8326. idx = axis == "x" ? 0 : 1,
  8327. areInProximity = {
  8328. "x":function() {
  8329. return ( (pi.so[idx] == 1 && (
  8330. ( (pi.startStubX > pi.endStubX) && (pi.tx > pi.startStubX) ) ||
  8331. ( (pi.sx > pi.endStubX) && (pi.tx > pi.sx))))) ||
  8332. ( (pi.so[idx] == -1 && (
  8333. ( (pi.startStubX < pi.endStubX) && (pi.tx < pi.startStubX) ) ||
  8334. ( (pi.sx < pi.endStubX) && (pi.tx < pi.sx)))));
  8335. },
  8336. "y":function() {
  8337. return ( (pi.so[idx] == 1 && (
  8338. ( (pi.startStubY > pi.endStubY) && (pi.ty > pi.startStubY) ) ||
  8339. ( (pi.sy > pi.endStubY) && (pi.ty > pi.sy))))) ||
  8340. ( (pi.so[idx] == -1 && (
  8341. ( (pi.startStubY < pi.endStubY) && (pi.ty < pi.startStubY) ) ||
  8342. ( (pi.sy < pi.endStubY) && (pi.ty < pi.sy)))));
  8343. }
  8344. };
  8345. if (!alwaysRespectStubs && areInProximity[axis]()) {
  8346. return {
  8347. "x":[(paintInfo.sx + paintInfo.tx) / 2, paintInfo.startStubY, (paintInfo.sx + paintInfo.tx) / 2, paintInfo.endStubY],
  8348. "y":[paintInfo.startStubX, (paintInfo.sy + paintInfo.ty) / 2, paintInfo.endStubX, (paintInfo.sy + paintInfo.ty) / 2]
  8349. }[axis];
  8350. }
  8351. else {
  8352. return [ paintInfo.startStubX, paintInfo.startStubY, paintInfo.endStubX, paintInfo.endStubY ];
  8353. }
  8354. }
  8355. },
  8356. lineCalculators = {
  8357. perpendicular : function(axis, ss, oss, es, oes) {
  8358. var pi = paintInfo,
  8359. sis = {
  8360. x:[ [ [ 1,2,3,4 ], null, [ 2,1,4,3 ] ], null, [ [ 4,3,2,1 ], null, [ 3,4,1,2 ] ] ],
  8361. y:[ [ [ 3,2,1,4 ], null, [ 2,3,4,1 ] ], null, [ [ 4,1,2,3 ], null, [ 1,4,3,2 ] ] ]
  8362. },
  8363. stubs = {
  8364. x:[ [ pi.startStubX, pi.endStubX ] , null, [ pi.endStubX, pi.startStubX ] ],
  8365. y:[ [ pi.startStubY, pi.endStubY ] , null, [ pi.endStubY, pi.startStubY ] ]
  8366. },
  8367. midLines = {
  8368. x:[ [ midx, pi.startStubY ], [ midx, pi.endStubY ] ],
  8369. y:[ [ pi.startStubX, midy ], [ pi.endStubX, midy ] ]
  8370. },
  8371. linesToEnd = {
  8372. x:[ [ pi.endStubX, pi.startStubY ] ],
  8373. y:[ [ pi.startStubX, pi.endStubY ] ]
  8374. },
  8375. startToEnd = {
  8376. x:[ [ pi.startStubX, pi.endStubY ], [ pi.endStubX, pi.endStubY ] ],
  8377. y:[ [ pi.endStubX, pi.startStubY ], [ pi.endStubX, pi.endStubY ] ]
  8378. },
  8379. startToMidToEnd = {
  8380. x:[ [ pi.startStubX, midy ], [ pi.endStubX, midy ], [ pi.endStubX, pi.endStubY ] ],
  8381. y:[ [ midx, pi.startStubY ], [ midx, pi.endStubY ], [ pi.endStubX, pi.endStubY ] ]
  8382. },
  8383. otherStubs = {
  8384. x:[ pi.startStubY, pi.endStubY ],
  8385. y:[ pi.startStubX, pi.endStubX ]
  8386. },
  8387. soIdx = orientations[axis][0], toIdx = orientations[axis][1],
  8388. _so = pi.so[soIdx] + 1,
  8389. _to = pi.to[toIdx] + 1,
  8390. otherFlipped = (pi.to[toIdx] == -1 && (otherStubs[axis][1] < otherStubs[axis][0])) || (pi.to[toIdx] == 1 && (otherStubs[axis][1] > otherStubs[axis][0])),
  8391. stub1 = stubs[axis][_so][0],
  8392. stub2 = stubs[axis][_so][1],
  8393. segmentIndexes = sis[axis][_so][_to];
  8394. if (pi.segment == segmentIndexes[3] || (pi.segment == segmentIndexes[2] && otherFlipped)) {
  8395. return midLines[axis];
  8396. }
  8397. else if (pi.segment == segmentIndexes[2] && stub2 < stub1) {
  8398. return linesToEnd[axis];
  8399. }
  8400. else if ((pi.segment == segmentIndexes[2] && stub2 >= stub1) || (pi.segment == segmentIndexes[1] && !otherFlipped)) {
  8401. return startToMidToEnd[axis];
  8402. }
  8403. else if (pi.segment == segmentIndexes[0] || (pi.segment == segmentIndexes[1] && otherFlipped)) {
  8404. return startToEnd[axis];
  8405. }
  8406. },
  8407. orthogonal : function(axis, startStub, otherStartStub, endStub, otherEndStub) {
  8408. var pi = paintInfo,
  8409. extent = {
  8410. "x":pi.so[0] == -1 ? Math.min(startStub, endStub) : Math.max(startStub, endStub),
  8411. "y":pi.so[1] == -1 ? Math.min(startStub, endStub) : Math.max(startStub, endStub)
  8412. }[axis];
  8413. return {
  8414. "x":[ [ extent, otherStartStub ],[ extent, otherEndStub ], [ endStub, otherEndStub ] ],
  8415. "y":[ [ otherStartStub, extent ], [ otherEndStub, extent ], [ otherEndStub, endStub ] ]
  8416. }[axis];
  8417. },
  8418. opposite : function(axis, ss, oss, es, oes) {
  8419. var pi = paintInfo,
  8420. otherAxis = {"x":"y","y":"x"}[axis],
  8421. dim = {"x":"height","y":"width"}[axis],
  8422. comparator = pi["is" + axis.toUpperCase() + "GreaterThanStubTimes2"];
  8423. if (params.sourceEndpoint.elementId == params.targetEndpoint.elementId) {
  8424. var _val = oss + ((1 - params.sourceEndpoint.anchor[otherAxis]) * params.sourceInfo[dim]) + _super.maxStub;
  8425. return {
  8426. "x":[ [ ss, _val ], [ es, _val ] ],
  8427. "y":[ [ _val, ss ], [ _val, es ] ]
  8428. }[axis];
  8429. }
  8430. else if (!comparator || (pi.so[idx] == 1 && ss > es) || (pi.so[idx] == -1 && ss < es)) {
  8431. return {
  8432. "x":[[ ss, midy ], [ es, midy ]],
  8433. "y":[[ midx, ss ], [ midx, es ]]
  8434. }[axis];
  8435. }
  8436. else if ((pi.so[idx] == 1 && ss < es) || (pi.so[idx] == -1 && ss > es)) {
  8437. return {
  8438. "x":[[ midx, pi.sy ], [ midx, pi.ty ]],
  8439. "y":[[ pi.sx, midy ], [ pi.tx, midy ]]
  8440. }[axis];
  8441. }
  8442. }
  8443. };
  8444. var stubs = stubCalculators[paintInfo.anchorOrientation](paintInfo.sourceAxis),
  8445. idx = paintInfo.sourceAxis == "x" ? 0 : 1,
  8446. oidx = paintInfo.sourceAxis == "x" ? 1 : 0,
  8447. ss = stubs[idx],
  8448. oss = stubs[oidx],
  8449. es = stubs[idx + 2],
  8450. oes = stubs[oidx + 2];
  8451. // add the start stub segment.
  8452. addSegment(segments, stubs[0], stubs[1], paintInfo);
  8453. // compute the rest of the line
  8454. var p = lineCalculators[paintInfo.anchorOrientation](paintInfo.sourceAxis, ss, oss, es, oes);
  8455. if (p) {
  8456. for (var i = 0; i < p.length; i++) {
  8457. addSegment(segments, p[i][0], p[i][1], paintInfo);
  8458. }
  8459. }
  8460. // line to end stub
  8461. addSegment(segments, stubs[2], stubs[3], paintInfo);
  8462. // end stub to end
  8463. addSegment(segments, paintInfo.tx, paintInfo.ty, paintInfo);
  8464. writeSegments(this, segments, paintInfo);
  8465. };
  8466. this.getPath = function() {
  8467. var _last = null, _lastAxis = null, s = [], segs = userSuppliedSegments || segments;
  8468. for (var i = 0; i < segs.length; i++) {
  8469. var seg = segs[i], axis = seg[4], axisIndex = (axis == "v" ? 3 : 2);
  8470. if (_last != null && _lastAxis === axis) {
  8471. _last[axisIndex] = seg[axisIndex];
  8472. }
  8473. else {
  8474. if (seg[0] != seg[2] || seg[1] != seg[3]) {
  8475. s.push({
  8476. start:[ seg[0], seg[1] ],
  8477. end:[ seg[2], seg[3] ]
  8478. });
  8479. _last = seg;
  8480. _lastAxis = seg[4];
  8481. }
  8482. }
  8483. }
  8484. return s;
  8485. };
  8486. this.setPath = function(path) {
  8487. userSuppliedSegments = [];
  8488. for (var i = 0; i < path.length; i++) {
  8489. var lx = path[i].start[0],
  8490. ly = path[i].start[1],
  8491. x = path[i].end[0],
  8492. y = path[i].end[1],
  8493. o = lx == x ? "v" : "h",
  8494. sgnx = sgn(x - lx),
  8495. sgny = sgn(y - ly);
  8496. userSuppliedSegments.push([lx, ly, x, y, o, sgnx, sgny]);
  8497. }
  8498. };
  8499. };
  8500. jsPlumbUtil.extend(Flowchart, jsPlumb.Connectors.AbstractConnector);
  8501. jsPlumb.registerConnectorType(Flowchart, "Flowchart");
  8502. })();
  8503. /*
  8504. * jsPlumb
  8505. *
  8506. * Title:jsPlumb 1.7.2
  8507. *
  8508. * Provides a way to visually connect elements on an HTML page, using SVG or VML.
  8509. *
  8510. * This file contains the state machine connectors.
  8511. *
  8512. * Copyright (c) 2010 - 2014 Simon Porritt (simon@jsplumbtoolkit.com)
  8513. *
  8514. * http://jsplumbtoolkit.com
  8515. * http://github.com/sporritt/jsplumb
  8516. *
  8517. * Dual licensed under the MIT and GPL2 licenses.
  8518. */
  8519. ;(function() {
  8520. "use strict";
  8521. var Line = function(x1, y1, x2, y2) {
  8522. this.m = (y2 - y1) / (x2 - x1);
  8523. this.b = -1 * ((this.m * x1) - y1);
  8524. this.rectIntersect = function(x,y,w,h) {
  8525. var results = [], xInt, yInt;
  8526. // try top face
  8527. // the equation of the top face is y = (0 * x) + b; y = b.
  8528. xInt = (y - this.b) / this.m;
  8529. // test that the X value is in the line's range.
  8530. if (xInt >= x && xInt <= (x + w)) results.push([ xInt, (this.m * xInt) + this.b ]);
  8531. // try right face
  8532. yInt = (this.m * (x + w)) + this.b;
  8533. if (yInt >= y && yInt <= (y + h)) results.push([ (yInt - this.b) / this.m, yInt ]);
  8534. // bottom face
  8535. xInt = ((y + h) - this.b) / this.m;
  8536. // test that the X value is in the line's range.
  8537. if (xInt >= x && xInt <= (x + w)) results.push([ xInt, (this.m * xInt) + this.b ]);
  8538. // try left face
  8539. yInt = (this.m * x) + this.b;
  8540. if (yInt >= y && yInt <= (y + h)) results.push([ (yInt - this.b) / this.m, yInt ]);
  8541. if (results.length == 2) {
  8542. var midx = (results[0][0] + results[1][0]) / 2, midy = (results[0][1] + results[1][1]) / 2;
  8543. results.push([ midx,midy ]);
  8544. // now calculate the segment inside the rectangle where the midpoint lies.
  8545. var xseg = midx <= x + (w / 2) ? -1 : 1,
  8546. yseg = midy <= y + (h / 2) ? -1 : 1;
  8547. results.push([xseg, yseg]);
  8548. return results;
  8549. }
  8550. return null;
  8551. };
  8552. },
  8553. _segment = function(x1, y1, x2, y2) {
  8554. if (x1 <= x2 && y2 <= y1) return 1;
  8555. else if (x1 <= x2 && y1 <= y2) return 2;
  8556. else if (x2 <= x1 && y2 >= y1) return 3;
  8557. return 4;
  8558. },
  8559. // the control point we will use depends on the faces to which each end of the connection is assigned, specifically whether or not the
  8560. // two faces are parallel or perpendicular. if they are parallel then the control point lies on the midpoint of the axis in which they
  8561. // are parellel and varies only in the other axis; this variation is proportional to the distance that the anchor points lie from the
  8562. // 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
  8563. // direction are dependent on the orientation of the two elements. 'seg', passed in to this method, tells you which segment the target element
  8564. // lies in with respect to the source: 1 is top right, 2 is bottom right, 3 is bottom left, 4 is top left.
  8565. //
  8566. // sourcePos and targetPos are arrays of info about where on the source and target each anchor is located. their contents are:
  8567. //
  8568. // 0 - absolute x
  8569. // 1 - absolute y
  8570. // 2 - proportional x in element (0 is left edge, 1 is right edge)
  8571. // 3 - proportional y in element (0 is top edge, 1 is bottom edge)
  8572. //
  8573. _findControlPoint = function(midx, midy, segment, sourceEdge, targetEdge, dx, dy, distance, proximityLimit) {
  8574. // TODO (maybe)
  8575. // - if anchor pos is 0.5, make the control point take into account the relative position of the elements.
  8576. if (distance <= proximityLimit) return [midx, midy];
  8577. if (segment === 1) {
  8578. if (sourceEdge[3] <= 0 && targetEdge[3] >= 1) return [ midx + (sourceEdge[2] < 0.5 ? -1 * dx : dx), midy ];
  8579. else if (sourceEdge[2] >= 1 && targetEdge[2] <= 0) return [ midx, midy + (sourceEdge[3] < 0.5 ? -1 * dy : dy) ];
  8580. else return [ midx + (-1 * dx) , midy + (-1 * dy) ];
  8581. }
  8582. else if (segment === 2) {
  8583. if (sourceEdge[3] >= 1 && targetEdge[3] <= 0) return [ midx + (sourceEdge[2] < 0.5 ? -1 * dx : dx), midy ];
  8584. else if (sourceEdge[2] >= 1 && targetEdge[2] <= 0) return [ midx, midy + (sourceEdge[3] < 0.5 ? -1 * dy : dy) ];
  8585. else return [ midx + (1 * dx) , midy + (-1 * dy) ];
  8586. }
  8587. else if (segment === 3) {
  8588. if (sourceEdge[3] >= 1 && targetEdge[3] <= 0) return [ midx + (sourceEdge[2] < 0.5 ? -1 * dx : dx), midy ];
  8589. else if (sourceEdge[2] <= 0 && targetEdge[2] >= 1) return [ midx, midy + (sourceEdge[3] < 0.5 ? -1 * dy : dy) ];
  8590. else return [ midx + (-1 * dx) , midy + (-1 * dy) ];
  8591. }
  8592. else if (segment === 4) {
  8593. if (sourceEdge[3] <= 0 && targetEdge[3] >= 1) return [ midx + (sourceEdge[2] < 0.5 ? -1 * dx : dx), midy ];
  8594. else if (sourceEdge[2] <= 0 && targetEdge[2] >= 1) return [ midx, midy + (sourceEdge[3] < 0.5 ? -1 * dy : dy) ];
  8595. else return [ midx + (1 * dx) , midy + (-1 * dy) ];
  8596. }
  8597. };
  8598. /**
  8599. * Class: Connectors.StateMachine
  8600. * Provides 'state machine' connectors.
  8601. */
  8602. /*
  8603. * Function: Constructor
  8604. *
  8605. * Parameters:
  8606. * curviness - measure of how "curvy" the connectors will be. this is translated as the distance that the
  8607. * Bezier curve's control point is from the midpoint of the straight line connecting the two
  8608. * endpoints, and does not mean that the connector is this wide. The Bezier curve never reaches
  8609. * its control points; they act as gravitational masses. defaults to 10.
  8610. * margin - distance from element to start and end connectors, in pixels. defaults to 5.
  8611. * proximityLimit - sets the distance beneath which the elements are consider too close together to bother
  8612. * with fancy curves. by default this is 80 pixels.
  8613. * loopbackRadius - the radius of a loopback connector. optional; defaults to 25.
  8614. * showLoopback - If set to false this tells the connector that it is ok to paint connections whose source and target is the same element with a connector running through the element. The default value for this is true; the connector always makes a loopback connection loop around the element rather than passing through it.
  8615. */
  8616. var StateMachine = function(params) {
  8617. params = params || {};
  8618. this.type = "StateMachine";
  8619. var self = this,
  8620. _super = jsPlumb.Connectors.AbstractConnector.apply(this, arguments),
  8621. curviness = params.curviness || 10,
  8622. margin = params.margin || 5,
  8623. proximityLimit = params.proximityLimit || 80,
  8624. clockwise = params.orientation && params.orientation === "clockwise",
  8625. loopbackRadius = params.loopbackRadius || 25,
  8626. showLoopback = params.showLoopback !== false;
  8627. this._compute = function(paintInfo, params) {
  8628. var w = Math.abs(params.sourcePos[0] - params.targetPos[0]),
  8629. h = Math.abs(params.sourcePos[1] - params.targetPos[1]),
  8630. x = Math.min(params.sourcePos[0], params.targetPos[0]),
  8631. y = Math.min(params.sourcePos[1], params.targetPos[1]);
  8632. if (!showLoopback || (params.sourceEndpoint.elementId !== params.targetEndpoint.elementId)) {
  8633. var _sx = params.sourcePos[0] < params.targetPos[0] ? 0 : w,
  8634. _sy = params.sourcePos[1] < params.targetPos[1] ? 0:h,
  8635. _tx = params.sourcePos[0] < params.targetPos[0] ? w : 0,
  8636. _ty = params.sourcePos[1] < params.targetPos[1] ? h : 0;
  8637. // now adjust for the margin
  8638. if (params.sourcePos[2] === 0) _sx -= margin;
  8639. if (params.sourcePos[2] === 1) _sx += margin;
  8640. if (params.sourcePos[3] === 0) _sy -= margin;
  8641. if (params.sourcePos[3] === 1) _sy += margin;
  8642. if (params.targetPos[2] === 0) _tx -= margin;
  8643. if (params.targetPos[2] === 1) _tx += margin;
  8644. if (params.targetPos[3] === 0) _ty -= margin;
  8645. if (params.targetPos[3] === 1) _ty += margin;
  8646. //
  8647. // these connectors are quadratic bezier curves, having a single control point. if both anchors
  8648. // are located at 0.5 on their respective faces, the control point is set to the midpoint and you
  8649. // get a straight line. this is also the case if the two anchors are within 'proximityLimit', since
  8650. // it seems to make good aesthetic sense to do that. outside of that, the control point is positioned
  8651. // at 'curviness' pixels away along the normal to the straight line connecting the two anchors.
  8652. //
  8653. // there may be two improvements to this. firstly, we might actually support the notion of avoiding nodes
  8654. // in the UI, or at least making a good effort at doing so. if a connection would pass underneath some node,
  8655. // for example, we might increase the distance the control point is away from the midpoint in a bid to
  8656. // steer it around that node. this will work within limits, but i think those limits would also be the likely
  8657. // limits for, once again, aesthetic good sense in the layout of a chart using these connectors.
  8658. //
  8659. // the second possible change is actually two possible changes: firstly, it is possible we should gradually
  8660. // decrease the 'curviness' as the distance between the anchors decreases; start tailing it off to 0 at some
  8661. // point (which should be configurable). secondly, we might slightly increase the 'curviness' for connectors
  8662. // with respect to how far their anchor is from the center of its respective face. this could either look cool,
  8663. // or stupid, and may indeed work only in a way that is so subtle as to have been a waste of time.
  8664. //
  8665. var _midx = (_sx + _tx) / 2, _midy = (_sy + _ty) / 2,
  8666. m2 = (-1 * _midx) / _midy, theta2 = Math.atan(m2),
  8667. dy = (m2 == Infinity || m2 == -Infinity) ? 0 : Math.abs(curviness / 2 * Math.sin(theta2)),
  8668. dx = (m2 == Infinity || m2 == -Infinity) ? 0 : Math.abs(curviness / 2 * Math.cos(theta2)),
  8669. segment = _segment(_sx, _sy, _tx, _ty),
  8670. distance = Math.sqrt(Math.pow(_tx - _sx, 2) + Math.pow(_ty - _sy, 2)),
  8671. // calculate the control point. this code will be where we'll put in a rudimentary element avoidance scheme; it
  8672. // will work by extending the control point to force the curve to be, um, curvier.
  8673. _controlPoint = _findControlPoint(_midx,
  8674. _midy,
  8675. segment,
  8676. params.sourcePos,
  8677. params.targetPos,
  8678. curviness, curviness,
  8679. distance,
  8680. proximityLimit);
  8681. _super.addSegment(this, "Bezier", {
  8682. x1:_tx, y1:_ty, x2:_sx, y2:_sy,
  8683. cp1x:_controlPoint[0], cp1y:_controlPoint[1],
  8684. cp2x:_controlPoint[0], cp2y:_controlPoint[1]
  8685. });
  8686. }
  8687. else {
  8688. // a loopback connector. draw an arc from one anchor to the other.
  8689. var x1 = params.sourcePos[0], x2 = params.sourcePos[0], y1 = params.sourcePos[1] - margin, y2 = params.sourcePos[1] - margin,
  8690. cx = x1, cy = y1 - loopbackRadius,
  8691. // canvas sizing stuff, to ensure the whole painted area is visible.
  8692. _w = 2 * loopbackRadius,
  8693. _h = 2 * loopbackRadius,
  8694. _x = cx - loopbackRadius,
  8695. _y = cy - loopbackRadius;
  8696. paintInfo.points[0] = _x;
  8697. paintInfo.points[1] = _y;
  8698. paintInfo.points[2] = _w;
  8699. paintInfo.points[3] = _h;
  8700. // ADD AN ARC SEGMENT.
  8701. _super.addSegment(this, "Arc", {
  8702. loopback:true,
  8703. x1:(x1 - _x) + 4,
  8704. y1:y1 - _y,
  8705. startAngle:0,
  8706. endAngle: 2 * Math.PI,
  8707. r:loopbackRadius,
  8708. ac:!clockwise,
  8709. x2:(x1 - _x) - 4,
  8710. y2:y1 - _y,
  8711. cx:cx - _x,
  8712. cy:cy - _y
  8713. });
  8714. }
  8715. };
  8716. };
  8717. jsPlumb.registerConnectorType(StateMachine, "StateMachine");
  8718. })();
  8719. /*
  8720. // a possible rudimentary avoidance scheme, old now, perhaps not useful.
  8721. // if (avoidSelector) {
  8722. // var testLine = new Line(sourcePos[0] + _sx,sourcePos[1] + _sy,sourcePos[0] + _tx,sourcePos[1] + _ty);
  8723. // var sel = jsPlumb.getSelector(avoidSelector);
  8724. // for (var i = 0; i < sel.length; i++) {
  8725. // var id = jsPlumb.getId(sel[i]);
  8726. // if (id != sourceEndpoint.elementId && id != targetEndpoint.elementId) {
  8727. // o = jsPlumb.getOffset(id), s = jsPlumb.getSize(id);
  8728. //
  8729. // if (o && s) {
  8730. // var collision = testLine.rectIntersect(o.left,o.top,s[0],s[1]);
  8731. // if (collision) {
  8732. // set the control point to be a certain distance from the midpoint of the two points that
  8733. // the line crosses on the rectangle.
  8734. // TODO where will this 75 number come from?
  8735. // _controlX = collision[2][0] + (75 * collision[3][0]);
  8736. // / _controlY = collision[2][1] + (75 * collision[3][1]);
  8737. // }
  8738. // }
  8739. // }
  8740. // }
  8741. //}
  8742. */
  8743. /*
  8744. * jsPlumb
  8745. *
  8746. * Title:jsPlumb 1.7.2
  8747. *
  8748. * Provides a way to visually connect elements on an HTML page, using SVG or VML.
  8749. *
  8750. * This file contains the code for the Bezier connector type.
  8751. *
  8752. * Copyright (c) 2010 - 2014 Simon Porritt (simon@jsplumbtoolkit.com)
  8753. *
  8754. * http://jsplumbtoolkit.com
  8755. * http://github.com/sporritt/jsplumb
  8756. *
  8757. * Dual licensed under the MIT and GPL2 licenses.
  8758. */
  8759. ;(function() {
  8760. var Bezier = function(params) {
  8761. params = params || {};
  8762. var _super = jsPlumb.Connectors.AbstractConnector.apply(this, arguments),
  8763. stub = params.stub || 50,
  8764. majorAnchor = params.curviness || 150,
  8765. minorAnchor = 10;
  8766. this.type = "Bezier";
  8767. this.getCurviness = function() { return majorAnchor; };
  8768. this._findControlPoint = function(point, sourceAnchorPosition, targetAnchorPosition, sourceEndpoint, targetEndpoint) {
  8769. // determine if the two anchors are perpendicular to each other in their orientation. we swap the control
  8770. // points around if so (code could be tightened up)
  8771. var soo = sourceEndpoint.anchor.getOrientation(sourceEndpoint),
  8772. too = targetEndpoint.anchor.getOrientation(targetEndpoint),
  8773. perpendicular = soo[0] != too[0] || soo[1] == too[1],
  8774. p = [];
  8775. if (!perpendicular) {
  8776. if (soo[0] === 0) // X
  8777. p.push(sourceAnchorPosition[0] < targetAnchorPosition[0] ? point[0] + minorAnchor : point[0] - minorAnchor);
  8778. else p.push(point[0] - (majorAnchor * soo[0]));
  8779. if (soo[1] === 0) // Y
  8780. p.push(sourceAnchorPosition[1] < targetAnchorPosition[1] ? point[1] + minorAnchor : point[1] - minorAnchor);
  8781. else p.push(point[1] + (majorAnchor * too[1]));
  8782. }
  8783. else {
  8784. if (too[0] === 0) // X
  8785. p.push(targetAnchorPosition[0] < sourceAnchorPosition[0] ? point[0] + minorAnchor : point[0] - minorAnchor);
  8786. else p.push(point[0] + (majorAnchor * too[0]));
  8787. if (too[1] === 0) // Y
  8788. p.push(targetAnchorPosition[1] < sourceAnchorPosition[1] ? point[1] + minorAnchor : point[1] - minorAnchor);
  8789. else p.push(point[1] + (majorAnchor * soo[1]));
  8790. }
  8791. return p;
  8792. };
  8793. this._compute = function(paintInfo, p) {
  8794. var sp = p.sourcePos,
  8795. tp = p.targetPos,
  8796. _w = Math.abs(sp[0] - tp[0]),
  8797. _h = Math.abs(sp[1] - tp[1]),
  8798. _sx = sp[0] < tp[0] ? _w : 0,
  8799. _sy = sp[1] < tp[1] ? _h : 0,
  8800. _tx = sp[0] < tp[0] ? 0 : _w,
  8801. _ty = sp[1] < tp[1] ? 0 : _h,
  8802. _CP = this._findControlPoint([_sx, _sy], sp, tp, p.sourceEndpoint, p.targetEndpoint, paintInfo.so, paintInfo.to),
  8803. _CP2 = this._findControlPoint([_tx, _ty], tp, sp, p.targetEndpoint, p.sourceEndpoint, paintInfo.so, paintInfo.to);
  8804. _super.addSegment(this, "Bezier", {
  8805. x1:_sx, y1:_sy, x2:_tx, y2:_ty,
  8806. cp1x:_CP[0], cp1y:_CP[1], cp2x:_CP2[0], cp2y:_CP2[1]
  8807. });
  8808. };
  8809. };
  8810. jsPlumbUtil.extend(Bezier, jsPlumb.Connectors.AbstractConnector);
  8811. jsPlumb.registerConnectorType(Bezier, "Bezier");
  8812. })();
  8813. /*
  8814. * jsPlumb
  8815. *
  8816. * Title:jsPlumb 1.7.2
  8817. *
  8818. * Provides a way to visually connect elements on an HTML page, using SVG or VML.
  8819. *
  8820. * This file contains the SVG renderers.
  8821. *
  8822. * Copyright (c) 2010 - 2014 Simon Porritt (simon@jsplumbtoolkit.com)
  8823. *
  8824. * http://jsplumbtoolkit.com
  8825. * http://github.com/sporritt/jsplumb
  8826. *
  8827. * Dual licensed under the MIT and GPL2 licenses.
  8828. */
  8829. ;(function() {
  8830. // ************************** SVG utility methods ********************************************
  8831. "use strict";
  8832. var svgAttributeMap = {
  8833. "joinstyle":"stroke-linejoin",
  8834. "stroke-linejoin":"stroke-linejoin",
  8835. "stroke-dashoffset":"stroke-dashoffset",
  8836. "stroke-linecap":"stroke-linecap"
  8837. },
  8838. STROKE_DASHARRAY = "stroke-dasharray",
  8839. DASHSTYLE = "dashstyle",
  8840. LINEAR_GRADIENT = "linearGradient",
  8841. RADIAL_GRADIENT = "radialGradient",
  8842. DEFS = "defs",
  8843. FILL = "fill",
  8844. STOP = "stop",
  8845. STROKE = "stroke",
  8846. STROKE_WIDTH = "stroke-width",
  8847. STYLE = "style",
  8848. NONE = "none",
  8849. JSPLUMB_GRADIENT = "jsplumb_gradient_",
  8850. LINE_WIDTH = "lineWidth",
  8851. ns = {
  8852. svg:"http://www.w3.org/2000/svg",
  8853. xhtml:"http://www.w3.org/1999/xhtml"
  8854. },
  8855. _attr = function(node, attributes) {
  8856. for (var i in attributes)
  8857. node.setAttribute(i, "" + attributes[i]);
  8858. },
  8859. _node = function(name, attributes) {
  8860. var n = document.createElementNS(ns.svg, name);
  8861. attributes = attributes || {};
  8862. attributes.version = "1.1";
  8863. attributes.xmlns = ns.xhtml;
  8864. _attr(n, attributes);
  8865. return n;
  8866. },
  8867. _pos = function(d) { return "position:absolute;left:" + d[0] + "px;top:" + d[1] + "px"; },
  8868. _clearGradient = function(parent) {
  8869. // TODO use querySelectorAll here instead?
  8870. for (var i = 0; i < parent.childNodes.length; i++) {
  8871. if (parent.childNodes[i].tagName == DEFS || parent.childNodes[i].tagName == LINEAR_GRADIENT || parent.childNodes[i].tagName == RADIAL_GRADIENT)
  8872. parent.removeChild(parent.childNodes[i]);
  8873. }
  8874. },
  8875. _updateGradient = function(parent, node, style, dimensions, uiComponent) {
  8876. var id = JSPLUMB_GRADIENT + uiComponent._jsPlumb.instance.idstamp();
  8877. // first clear out any existing gradient
  8878. _clearGradient(parent);
  8879. // this checks for an 'offset' property in the gradient, and in the absence of it, assumes
  8880. // we want a linear gradient. if it's there, we create a radial gradient.
  8881. // it is possible that a more explicit means of defining the gradient type would be
  8882. // better. relying on 'offset' means that we can never have a radial gradient that uses
  8883. // some default offset, for instance.
  8884. // issue 244 suggested the 'gradientUnits' attribute; without this, straight/flowchart connectors with gradients would
  8885. // not show gradients when the line was perfectly horizontal or vertical.
  8886. var g;
  8887. if (!style.gradient.offset)
  8888. g = _node(LINEAR_GRADIENT, {id:id, gradientUnits:"userSpaceOnUse"});
  8889. else
  8890. g = _node(RADIAL_GRADIENT, { id:id });
  8891. var defs = _node(DEFS);
  8892. parent.appendChild(defs);
  8893. defs.appendChild(g);
  8894. //parent.appendChild(g);
  8895. // the svg radial gradient seems to treat stops in the reverse
  8896. // order to how canvas does it. so we want to keep all the maths the same, but
  8897. // iterate the actual style declarations in reverse order, if the x indexes are not in order.
  8898. for (var i = 0; i < style.gradient.stops.length; i++) {
  8899. var styleToUse = uiComponent.segment == 1 || uiComponent.segment == 2 ? i: style.gradient.stops.length - 1 - i,
  8900. stopColor = jsPlumbUtil.convertStyle(style.gradient.stops[styleToUse][1], true),
  8901. s = _node(STOP, {"offset":Math.floor(style.gradient.stops[i][0] * 100) + "%", "stop-color":stopColor});
  8902. g.appendChild(s);
  8903. }
  8904. var applyGradientTo = style.strokeStyle ? STROKE : FILL;
  8905. //node.setAttribute(STYLE, applyGradientTo + ":url(" + /[^#]+/.exec(document.location.toString()) + "#" + id + ")");
  8906. //node.setAttribute(STYLE, applyGradientTo + ":url(#" + id + ")");
  8907. //node.setAttribute(applyGradientTo, "url(" + /[^#]+/.exec(document.location.toString()) + "#" + id + ")");
  8908. node.setAttribute(applyGradientTo, "url(#" + id + ")");
  8909. },
  8910. _applyStyles = function(parent, node, style, dimensions, uiComponent) {
  8911. node.setAttribute(FILL, style.fillStyle ? jsPlumbUtil.convertStyle(style.fillStyle, true) : NONE);
  8912. node.setAttribute(STROKE, style.strokeStyle ? jsPlumbUtil.convertStyle(style.strokeStyle, true) : NONE);
  8913. if (style.gradient) {
  8914. _updateGradient(parent, node, style, dimensions, uiComponent);
  8915. }
  8916. else {
  8917. // make sure we clear any existing gradient
  8918. _clearGradient(parent);
  8919. node.setAttribute(STYLE, "");
  8920. }
  8921. if (style.lineWidth) {
  8922. node.setAttribute(STROKE_WIDTH, style.lineWidth);
  8923. }
  8924. // in SVG there is a stroke-dasharray attribute we can set, and its syntax looks like
  8925. // the syntax in VML but is actually kind of nasty: values are given in the pixel
  8926. // coordinate space, whereas in VML they are multiples of the width of the stroked
  8927. // line, which makes a lot more sense. for that reason, jsPlumb is supporting both
  8928. // the native svg 'stroke-dasharray' attribute, and also the 'dashstyle' concept from
  8929. // VML, which will be the preferred method. the code below this converts a dashstyle
  8930. // attribute given in terms of stroke width into a pixel representation, by using the
  8931. // stroke's lineWidth.
  8932. if (style[DASHSTYLE] && style[LINE_WIDTH] && !style[STROKE_DASHARRAY]) {
  8933. var sep = style[DASHSTYLE].indexOf(",") == -1 ? " " : ",",
  8934. parts = style[DASHSTYLE].split(sep),
  8935. styleToUse = "";
  8936. parts.forEach(function(p) {
  8937. styleToUse += (Math.floor(p * style.lineWidth) + sep);
  8938. });
  8939. node.setAttribute(STROKE_DASHARRAY, styleToUse);
  8940. }
  8941. else if(style[STROKE_DASHARRAY]) {
  8942. node.setAttribute(STROKE_DASHARRAY, style[STROKE_DASHARRAY]);
  8943. }
  8944. // extra attributes such as join type, dash offset.
  8945. for (var i in svgAttributeMap) {
  8946. if (style[i]) {
  8947. node.setAttribute(svgAttributeMap[i], style[i]);
  8948. }
  8949. }
  8950. },
  8951. _decodeFont = function(f) {
  8952. var r = /([0-9].)(p[xt])\s(.*)/,
  8953. bits = f.match(r);
  8954. return {size:bits[1] + bits[2], font:bits[3]};
  8955. },
  8956. _appendAtIndex = function(svg, path, idx) {
  8957. if (svg.childNodes.length > idx) {
  8958. svg.insertBefore(path, svg.childNodes[idx]);
  8959. }
  8960. else svg.appendChild(path);
  8961. };
  8962. /**
  8963. utility methods for other objects to use.
  8964. */
  8965. jsPlumbUtil.svg = {
  8966. node:_node,
  8967. attr:_attr,
  8968. pos:_pos
  8969. };
  8970. // ************************** / SVG utility methods ********************************************
  8971. /*
  8972. * Base class for SVG components.
  8973. */
  8974. var SvgComponent = function(params) {
  8975. var pointerEventsSpec = params.pointerEventsSpec || "all", renderer = {};
  8976. jsPlumb.jsPlumbUIComponent.apply(this, params.originalArgs);
  8977. this.canvas = null;this.path = null;this.svg = null; this.bgCanvas = null;
  8978. var clazz = params.cssClass + " " + (params.originalArgs[0].cssClass || ""),
  8979. svgParams = {
  8980. "style":"",
  8981. "width":0,
  8982. "height":0,
  8983. "pointer-events":pointerEventsSpec,
  8984. "position":"absolute"
  8985. };
  8986. this.svg = _node("svg", svgParams);
  8987. if (params.useDivWrapper) {
  8988. this.canvas = document.createElement("div");
  8989. this.canvas.style.position = "absolute";
  8990. jsPlumbUtil.sizeElement(this.canvas,0,0,1,1);
  8991. this.canvas.className = clazz;
  8992. }
  8993. else {
  8994. _attr(this.svg, { "class":clazz });
  8995. this.canvas = this.svg;
  8996. }
  8997. params._jsPlumb.appendElement(this.canvas, params.originalArgs[0].parent);
  8998. if (params.useDivWrapper) this.canvas.appendChild(this.svg);
  8999. // TODO this displayElement stuff is common between all components, across all
  9000. // renderers. would be best moved to jsPlumbUIComponent.
  9001. var displayElements = [ this.canvas ];
  9002. this.getDisplayElements = function() {
  9003. return displayElements;
  9004. };
  9005. this.appendDisplayElement = function(el) {
  9006. displayElements.push(el);
  9007. };
  9008. this.paint = function(style, anchor, extents) {
  9009. if (style != null) {
  9010. var xy = [ this.x, this.y ], wh = [ this.w, this.h ], p;
  9011. if (extents != null) {
  9012. if (extents.xmin < 0) xy[0] += extents.xmin;
  9013. if (extents.ymin < 0) xy[1] += extents.ymin;
  9014. wh[0] = extents.xmax + ((extents.xmin < 0) ? -extents.xmin : 0);
  9015. wh[1] = extents.ymax + ((extents.ymin < 0) ? -extents.ymin : 0);
  9016. }
  9017. if (params.useDivWrapper) {
  9018. jsPlumbUtil.sizeElement(this.canvas, xy[0], xy[1], wh[0], wh[1]);
  9019. xy[0] = 0; xy[1] = 0;
  9020. p = _pos([ 0, 0 ]);
  9021. }
  9022. else
  9023. p = _pos([ xy[0], xy[1] ]);
  9024. renderer.paint.apply(this, arguments);
  9025. _attr(this.svg, {
  9026. "style":p,
  9027. "width": wh[0],
  9028. "height": wh[1]
  9029. });
  9030. }
  9031. };
  9032. return {
  9033. renderer:renderer
  9034. };
  9035. };
  9036. jsPlumbUtil.extend(SvgComponent, jsPlumb.jsPlumbUIComponent, {
  9037. cleanup:function() {
  9038. if (this.canvas) this.canvas._jsPlumb = null;
  9039. if (this.svg) this.svg._jsPlumb = null;
  9040. if (this.bgCanvas) this.bgCanvas._jsPlumb = null;
  9041. if (this.canvas && this.canvas.parentNode) this.canvas.parentNode.removeChild(this.canvas);
  9042. if (this.bgCanvas && this. bgCanvas.parentNode) this.canvas.parentNode.removeChild(this.canvas);
  9043. this.svg = null;
  9044. this.canvas = null;
  9045. this.path = null;
  9046. this.group = null;
  9047. },
  9048. setVisible:function(v) {
  9049. if (this.canvas) {
  9050. this.canvas.style.display = v ? "block" : "none";
  9051. }
  9052. }
  9053. });
  9054. /*
  9055. * Base class for SVG connectors.
  9056. */
  9057. jsPlumb.ConnectorRenderers.svg = function(params) {
  9058. var self = this,
  9059. _super = SvgComponent.apply(this, [ {
  9060. cssClass:params._jsPlumb.connectorClass,
  9061. originalArgs:arguments,
  9062. pointerEventsSpec:"none",
  9063. _jsPlumb:params._jsPlumb
  9064. } ]);
  9065. /*this.pointOnPath = function(location, absolute) {
  9066. if (!self.path) return [0,0];
  9067. var p = absolute ? location : location * self.path.getTotalLength();
  9068. return self.path.getPointAtLength(p);
  9069. };*/
  9070. _super.renderer.paint = function(style, anchor, extents) {
  9071. var segments = self.getSegments(), p = "", offset = [0,0];
  9072. if (extents.xmin < 0) offset[0] = -extents.xmin;
  9073. if (extents.ymin < 0) offset[1] = -extents.ymin;
  9074. if (segments.length > 0) {
  9075. // create path from segments.
  9076. for (var i = 0; i < segments.length; i++) {
  9077. p += jsPlumb.Segments.svg.SegmentRenderer.getPath(segments[i]);
  9078. p += " ";
  9079. }
  9080. var a = {
  9081. d:p,
  9082. transform:"translate(" + offset[0] + "," + offset[1] + ")",
  9083. "pointer-events":params["pointer-events"] || "visibleStroke"
  9084. },
  9085. outlineStyle = null,
  9086. d = [self.x,self.y,self.w,self.h];
  9087. // outline style. actually means drawing an svg object underneath the main one.
  9088. if (style.outlineColor) {
  9089. var outlineWidth = style.outlineWidth || 1,
  9090. outlineStrokeWidth = style.lineWidth + (2 * outlineWidth);
  9091. outlineStyle = jsPlumb.extend({}, style);
  9092. delete outlineStyle.gradient;
  9093. outlineStyle.strokeStyle = jsPlumbUtil.convertStyle(style.outlineColor);
  9094. outlineStyle.lineWidth = outlineStrokeWidth;
  9095. if (self.bgPath == null) {
  9096. self.bgPath = _node("path", a);
  9097. _appendAtIndex(self.svg, self.bgPath, 0);
  9098. }
  9099. else {
  9100. _attr(self.bgPath, a);
  9101. }
  9102. _applyStyles(self.svg, self.bgPath, outlineStyle, d, self);
  9103. }
  9104. if (self.path == null) {
  9105. self.path = _node("path", a);
  9106. _appendAtIndex(self.svg, self.path, style.outlineColor ? 1 : 0);
  9107. }
  9108. else {
  9109. _attr(self.path, a);
  9110. }
  9111. _applyStyles(self.svg, self.path, style, d, self);
  9112. }
  9113. };
  9114. };
  9115. jsPlumbUtil.extend(jsPlumb.ConnectorRenderers.svg, SvgComponent);
  9116. // ******************************* svg segment renderer *****************************************************
  9117. jsPlumb.Segments.svg = {
  9118. SegmentRenderer : {
  9119. getPath : function(segment) {
  9120. return ({
  9121. "Straight":function() {
  9122. var d = segment.getCoordinates();
  9123. return "M " + d.x1 + " " + d.y1 + " L " + d.x2 + " " + d.y2;
  9124. },
  9125. "Bezier":function() {
  9126. var d = segment.params;
  9127. return "M " + d.x1 + " " + d.y1 +
  9128. " C " + d.cp1x + " " + d.cp1y + " " + d.cp2x + " " + d.cp2y + " " + d.x2 + " " + d.y2;
  9129. },
  9130. "Arc":function() {
  9131. var d = segment.params,
  9132. laf = segment.sweep > Math.PI ? 1 : 0,
  9133. sf = segment.anticlockwise ? 0 : 1;
  9134. return "M" + segment.x1 + " " + segment.y1 + " A " + segment.radius + " " + d.r + " 0 " + laf + "," + sf + " " + segment.x2 + " " + segment.y2;
  9135. }
  9136. })[segment.type]();
  9137. }
  9138. }
  9139. };
  9140. // ******************************* /svg segments *****************************************************
  9141. /*
  9142. * Base class for SVG endpoints.
  9143. */
  9144. var SvgEndpoint = window.SvgEndpoint = function(params) {
  9145. var _super = SvgComponent.apply(this, [ {
  9146. cssClass:params._jsPlumb.endpointClass,
  9147. originalArgs:arguments,
  9148. pointerEventsSpec:"all",
  9149. useDivWrapper:true,
  9150. _jsPlumb:params._jsPlumb
  9151. } ]);
  9152. _super.renderer.paint = function(style) {
  9153. var s = jsPlumb.extend({}, style);
  9154. if (s.outlineColor) {
  9155. s.strokeWidth = s.outlineWidth;
  9156. s.strokeStyle = jsPlumbUtil.convertStyle(s.outlineColor, true);
  9157. }
  9158. if (this.node == null) {
  9159. this.node = this.makeNode(s);
  9160. this.svg.appendChild(this.node);
  9161. }
  9162. else if (this.updateNode != null) {
  9163. this.updateNode(this.node);
  9164. }
  9165. _applyStyles(this.svg, this.node, s, [ this.x, this.y, this.w, this.h ], this);
  9166. _pos(this.node, [ this.x, this.y ]);
  9167. }.bind(this);
  9168. };
  9169. jsPlumbUtil.extend(SvgEndpoint, SvgComponent);
  9170. /*
  9171. * SVG Dot Endpoint
  9172. */
  9173. jsPlumb.Endpoints.svg.Dot = function() {
  9174. jsPlumb.Endpoints.Dot.apply(this, arguments);
  9175. SvgEndpoint.apply(this, arguments);
  9176. this.makeNode = function(style) {
  9177. return _node("circle", {
  9178. "cx" : this.w / 2,
  9179. "cy" : this.h / 2,
  9180. "r" : this.radius
  9181. });
  9182. };
  9183. this.updateNode = function(node) {
  9184. _attr(node, {
  9185. "cx":this.w / 2,
  9186. "cy":this.h / 2,
  9187. "r":this.radius
  9188. });
  9189. };
  9190. };
  9191. jsPlumbUtil.extend(jsPlumb.Endpoints.svg.Dot, [jsPlumb.Endpoints.Dot, SvgEndpoint]);
  9192. /*
  9193. * SVG Rectangle Endpoint
  9194. */
  9195. jsPlumb.Endpoints.svg.Rectangle = function() {
  9196. jsPlumb.Endpoints.Rectangle.apply(this, arguments);
  9197. SvgEndpoint.apply(this, arguments);
  9198. this.makeNode = function(style) {
  9199. return _node("rect", {
  9200. "width" : this.w,
  9201. "height" : this.h
  9202. });
  9203. };
  9204. this.updateNode = function(node) {
  9205. _attr(node, {
  9206. "width":this.w,
  9207. "height":this.h
  9208. });
  9209. };
  9210. };
  9211. jsPlumbUtil.extend(jsPlumb.Endpoints.svg.Rectangle, [jsPlumb.Endpoints.Rectangle, SvgEndpoint]);
  9212. /*
  9213. * SVG Image Endpoint is the default image endpoint.
  9214. */
  9215. jsPlumb.Endpoints.svg.Image = jsPlumb.Endpoints.Image;
  9216. /*
  9217. * Blank endpoint in svg renderer is the default Blank endpoint.
  9218. */
  9219. jsPlumb.Endpoints.svg.Blank = jsPlumb.Endpoints.Blank;
  9220. /*
  9221. * Label overlay in svg renderer is the default Label overlay.
  9222. */
  9223. jsPlumb.Overlays.svg.Label = jsPlumb.Overlays.Label;
  9224. /*
  9225. * Custom overlay in svg renderer is the default Custom overlay.
  9226. */
  9227. jsPlumb.Overlays.svg.Custom = jsPlumb.Overlays.Custom;
  9228. var AbstractSvgArrowOverlay = function(superclass, originalArgs) {
  9229. superclass.apply(this, originalArgs);
  9230. jsPlumb.jsPlumbUIComponent.apply(this, originalArgs);
  9231. this.isAppendedAtTopLevel = false;
  9232. var self = this;
  9233. this.path = null;
  9234. this.paint = function(params, containerExtents) {
  9235. // only draws on connections, not endpoints.
  9236. if (params.component.svg && containerExtents) {
  9237. if (this.path == null) {
  9238. this.path = _node("path", {
  9239. "pointer-events":"all"
  9240. });
  9241. params.component.svg.appendChild(this.path);
  9242. this.canvas = params.component.svg; // for the sake of completeness; this behaves the same as other overlays
  9243. }
  9244. var clazz = originalArgs && (originalArgs.length == 1) ? (originalArgs[0].cssClass || "") : "",
  9245. offset = [0,0];
  9246. if (containerExtents.xmin < 0) offset[0] = -containerExtents.xmin;
  9247. if (containerExtents.ymin < 0) offset[1] = -containerExtents.ymin;
  9248. _attr(this.path, {
  9249. "d" : makePath(params.d),
  9250. "class" : clazz,
  9251. stroke : params.strokeStyle ? params.strokeStyle : null,
  9252. fill : params.fillStyle ? params.fillStyle : null,
  9253. transform : "translate(" + offset[0] + "," + offset[1] + ")"
  9254. });
  9255. }
  9256. };
  9257. var makePath = function(d) {
  9258. return "M" + d.hxy.x + "," + d.hxy.y +
  9259. " L" + d.tail[0].x + "," + d.tail[0].y +
  9260. " L" + d.cxy.x + "," + d.cxy.y +
  9261. " L" + d.tail[1].x + "," + d.tail[1].y +
  9262. " L" + d.hxy.x + "," + d.hxy.y;
  9263. };
  9264. };
  9265. jsPlumbUtil.extend(AbstractSvgArrowOverlay, [jsPlumb.jsPlumbUIComponent, jsPlumb.Overlays.AbstractOverlay], {
  9266. cleanup : function() {
  9267. if (this.path != null) this._jsPlumb.instance.removeElement(this.path);
  9268. },
  9269. setVisible:function(v) {
  9270. if(this.path != null) (this.path.style.display = (v ? "block" : "none"));
  9271. }
  9272. });
  9273. jsPlumb.Overlays.svg.Arrow = function() {
  9274. AbstractSvgArrowOverlay.apply(this, [jsPlumb.Overlays.Arrow, arguments]);
  9275. };
  9276. jsPlumbUtil.extend(jsPlumb.Overlays.svg.Arrow, [ jsPlumb.Overlays.Arrow, AbstractSvgArrowOverlay ]);
  9277. jsPlumb.Overlays.svg.PlainArrow = function() {
  9278. AbstractSvgArrowOverlay.apply(this, [jsPlumb.Overlays.PlainArrow, arguments]);
  9279. };
  9280. jsPlumbUtil.extend(jsPlumb.Overlays.svg.PlainArrow, [ jsPlumb.Overlays.PlainArrow, AbstractSvgArrowOverlay ]);
  9281. jsPlumb.Overlays.svg.Diamond = function() {
  9282. AbstractSvgArrowOverlay.apply(this, [jsPlumb.Overlays.Diamond, arguments]);
  9283. };
  9284. jsPlumbUtil.extend(jsPlumb.Overlays.svg.Diamond, [ jsPlumb.Overlays.Diamond, AbstractSvgArrowOverlay ]);
  9285. // a test
  9286. jsPlumb.Overlays.svg.GuideLines = function() {
  9287. var path = null, self = this, p1_1, p1_2;
  9288. jsPlumb.Overlays.GuideLines.apply(this, arguments);
  9289. this.paint = function(params, containerExtents) {
  9290. if (path == null) {
  9291. path = _node("path");
  9292. params.connector.svg.appendChild(path);
  9293. self.attachListeners(path, params.connector);
  9294. self.attachListeners(path, self);
  9295. p1_1 = _node("path");
  9296. params.connector.svg.appendChild(p1_1);
  9297. self.attachListeners(p1_1, params.connector);
  9298. self.attachListeners(p1_1, self);
  9299. p1_2 = _node("path");
  9300. params.connector.svg.appendChild(p1_2);
  9301. self.attachListeners(p1_2, params.connector);
  9302. self.attachListeners(p1_2, self);
  9303. }
  9304. var offset =[0,0];
  9305. if (containerExtents.xmin < 0) offset[0] = -containerExtents.xmin;
  9306. if (containerExtents.ymin < 0) offset[1] = -containerExtents.ymin;
  9307. _attr(path, {
  9308. "d" : makePath(params.head, params.tail),
  9309. stroke : "red",
  9310. fill : null,
  9311. transform:"translate(" + offset[0] + "," + offset[1] + ")"
  9312. });
  9313. _attr(p1_1, {
  9314. "d" : makePath(params.tailLine[0], params.tailLine[1]),
  9315. stroke : "blue",
  9316. fill : null,
  9317. transform:"translate(" + offset[0] + "," + offset[1] + ")"
  9318. });
  9319. _attr(p1_2, {
  9320. "d" : makePath(params.headLine[0], params.headLine[1]),
  9321. stroke : "green",
  9322. fill : null,
  9323. transform:"translate(" + offset[0] + "," + offset[1] + ")"
  9324. });
  9325. };
  9326. var makePath = function(d1, d2) {
  9327. return "M " + d1.x + "," + d1.y +
  9328. " L" + d2.x + "," + d2.y;
  9329. };
  9330. };
  9331. jsPlumbUtil.extend(jsPlumb.Overlays.svg.GuideLines, jsPlumb.Overlays.GuideLines);
  9332. })();
  9333. /*
  9334. * jsPlumb
  9335. *
  9336. * Title:jsPlumb 1.7.2
  9337. *
  9338. * Provides a way to visually connect elements on an HTML page, using SVG or VML.
  9339. *
  9340. * This file contains the VML renderers.
  9341. *
  9342. * Copyright (c) 2010 - 2014 Simon Porritt (simon@jsplumbtoolkit.com)
  9343. *
  9344. * http://jsplumbtoolkit.com
  9345. * http://github.com/sporritt/jsplumb
  9346. *
  9347. * Dual licensed under the MIT and GPL2 licenses.
  9348. */
  9349. ;(function() {
  9350. "use strict";
  9351. // http://ajaxian.com/archives/the-vml-changes-in-ie-8
  9352. // http://www.nczonline.net/blog/2010/01/19/internet-explorer-8-document-and-browser-modes/
  9353. // http://www.louisremi.com/2009/03/30/changes-in-vml-for-ie8-or-what-feature-can-the-ie-dev-team-break-for-you-today/
  9354. var vmlAttributeMap = {
  9355. "stroke-linejoin":"joinstyle",
  9356. "joinstyle":"joinstyle",
  9357. "endcap":"endcap",
  9358. "miterlimit":"miterlimit"
  9359. },
  9360. jsPlumbStylesheet = null;
  9361. if (document.createStyleSheet && document.namespaces) {
  9362. var ruleClasses = [
  9363. ".jsplumb_vml", "jsplumb\\:textbox", "jsplumb\\:oval", "jsplumb\\:rect",
  9364. "jsplumb\\:stroke", "jsplumb\\:shape", "jsplumb\\:group"
  9365. ],
  9366. rule = "behavior:url(#default#VML);position:absolute;";
  9367. jsPlumbStylesheet = document.createStyleSheet();
  9368. for (var i = 0; i < ruleClasses.length; i++)
  9369. jsPlumbStylesheet.addRule(ruleClasses[i], rule);
  9370. // in this page it is also mentioned that IE requires the extra arg to the namespace
  9371. // http://www.louisremi.com/2009/03/30/changes-in-vml-for-ie8-or-what-feature-can-the-ie-dev-team-break-for-you-today/
  9372. // but someone commented saying they didn't need it, and it seems jsPlumb doesnt need it either.
  9373. // var iev = document.documentMode;
  9374. //if (!iev || iev < 8)
  9375. document.namespaces.add("jsplumb", "urn:schemas-microsoft-com:vml");
  9376. //else
  9377. // document.namespaces.add("jsplumb", "urn:schemas-microsoft-com:vml", "#default#VML");
  9378. }
  9379. jsPlumb.vml = {};
  9380. var scale = 1000,
  9381. _atts = function(o, atts) {
  9382. for (var i in atts) {
  9383. // IE8 fix: setattribute does not work after an element has been added to the dom!
  9384. // http://www.louisremi.com/2009/03/30/changes-in-vml-for-ie8-or-what-feature-can-the-ie-dev-team-break-for-you-today/
  9385. //o.setAttribute(i, atts[i]);
  9386. /*There is an additional problem when accessing VML elements by using get/setAttribute. The simple solution is following:
  9387. if (document.documentMode==8) {
  9388. ele.opacity=1;
  9389. } else {
  9390. ele.setAttribute(‘opacity’,1);
  9391. }
  9392. */
  9393. o[i] = atts[i];
  9394. }
  9395. },
  9396. _node = function(name, d, atts, parent, _jsPlumb, deferToJsPlumbContainer) {
  9397. atts = atts || {};
  9398. var o = document.createElement("jsplumb:" + name);
  9399. if (deferToJsPlumbContainer)
  9400. _jsPlumb.appendElement(o, parent);
  9401. else
  9402. // TODO is this failing? that would be because parent is not a plain DOM element.
  9403. // IF SO, uncomment the line below this one and remove this one.
  9404. parent.appendChild(o);
  9405. //jsPlumb.getDOMElement(parent).appendChild(o);
  9406. o.className = (atts["class"] ? atts["class"] + " " : "") + "jsplumb_vml";
  9407. _pos(o, d);
  9408. _atts(o, atts);
  9409. return o;
  9410. },
  9411. _pos = function(o,d, zIndex) {
  9412. o.style.left = d[0] + "px";
  9413. o.style.top = d[1] + "px";
  9414. o.style.width= d[2] + "px";
  9415. o.style.height= d[3] + "px";
  9416. o.style.position = "absolute";
  9417. if (zIndex)
  9418. o.style.zIndex = zIndex;
  9419. },
  9420. _conv = jsPlumb.vml.convertValue = function(v) {
  9421. return Math.floor(v * scale);
  9422. },
  9423. // tests if the given style is "transparent" and then sets the appropriate opacity node to 0 if so,
  9424. // or 1 if not. TODO in the future, support variable opacity.
  9425. _maybeSetOpacity = function(styleToWrite, styleToCheck, type, component) {
  9426. if ("transparent" === styleToCheck)
  9427. component.setOpacity(type, "0.0");
  9428. else
  9429. component.setOpacity(type, "1.0");
  9430. },
  9431. _applyStyles = function(node, style, component, _jsPlumb) {
  9432. var styleToWrite = {};
  9433. if (style.strokeStyle) {
  9434. styleToWrite.stroked = "true";
  9435. var strokeColor = jsPlumbUtil.convertStyle(style.strokeStyle, true);
  9436. styleToWrite.strokecolor = strokeColor;
  9437. _maybeSetOpacity(styleToWrite, strokeColor, "stroke", component);
  9438. styleToWrite.strokeweight = style.lineWidth + "px";
  9439. }
  9440. else styleToWrite.stroked = "false";
  9441. if (style.fillStyle) {
  9442. styleToWrite.filled = "true";
  9443. var fillColor = jsPlumbUtil.convertStyle(style.fillStyle, true);
  9444. styleToWrite.fillcolor = fillColor;
  9445. _maybeSetOpacity(styleToWrite, fillColor, "fill", component);
  9446. }
  9447. else styleToWrite.filled = "false";
  9448. if(style.dashstyle) {
  9449. if (component.strokeNode == null) {
  9450. component.strokeNode = _node("stroke", [0,0,0,0], { dashstyle:style.dashstyle }, node, _jsPlumb);
  9451. }
  9452. else
  9453. component.strokeNode.dashstyle = style.dashstyle;
  9454. }
  9455. else if (style["stroke-dasharray"] && style.lineWidth) {
  9456. var sep = style["stroke-dasharray"].indexOf(",") == -1 ? " " : ",",
  9457. parts = style["stroke-dasharray"].split(sep),
  9458. styleToUse = "";
  9459. for(var i = 0; i < parts.length; i++) {
  9460. styleToUse += (Math.floor(parts[i] / style.lineWidth) + sep);
  9461. }
  9462. if (component.strokeNode == null) {
  9463. component.strokeNode = _node("stroke", [0,0,0,0], { dashstyle:styleToUse }, node, _jsPlumb);
  9464. }
  9465. else
  9466. component.strokeNode.dashstyle = styleToUse;
  9467. }
  9468. _atts(node, styleToWrite);
  9469. },
  9470. /*
  9471. * Base class for Vml endpoints and connectors. Extends jsPlumbUIComponent.
  9472. */
  9473. VmlComponent = function() {
  9474. var self = this, renderer = {};
  9475. jsPlumb.jsPlumbUIComponent.apply(this, arguments);
  9476. this.opacityNodes = {
  9477. "stroke":null,
  9478. "fill":null
  9479. };
  9480. this.initOpacityNodes = function(vml) {
  9481. self.opacityNodes.stroke = _node("stroke", [0,0,1,1], {opacity:"0.0"}, vml, self._jsPlumb.instance);
  9482. self.opacityNodes.fill = _node("fill", [0,0,1,1], {opacity:"0.0"}, vml, self._jsPlumb.instance);
  9483. };
  9484. this.setOpacity = function(type, value) {
  9485. var node = self.opacityNodes[type];
  9486. if (node) node.opacity = "" + value;
  9487. };
  9488. var displayElements = [ ];
  9489. this.getDisplayElements = function() {
  9490. return displayElements;
  9491. };
  9492. this.appendDisplayElement = function(el, doNotAppendToCanvas) {
  9493. if (!doNotAppendToCanvas) self.canvas.parentNode.appendChild(el);
  9494. displayElements.push(el);
  9495. };
  9496. };
  9497. jsPlumbUtil.extend(VmlComponent, jsPlumb.jsPlumbUIComponent, {
  9498. cleanup:function() {
  9499. if (this.bgCanvas) this.bgCanvas.parentNode.removeChild(this.bgCanvas);
  9500. if (this.canvas) this.canvas.parentNode.removeChild(this.canvas);
  9501. }
  9502. });
  9503. /*
  9504. * Base class for Vml connectors. extends VmlComponent.
  9505. */
  9506. var VmlConnector = jsPlumb.ConnectorRenderers.vml = function(params, component) {
  9507. this.strokeNode = null;
  9508. this.canvas = null;
  9509. VmlComponent.apply(this, arguments);
  9510. var clazz = this._jsPlumb.instance.connectorClass + (params.cssClass ? (" " + params.cssClass) : "");
  9511. this.paint = function(style) {
  9512. if (style !== null) {
  9513. // we need to be at least 1 pixel in each direction, because otherwise coordsize gets set to
  9514. // 0 and overlays cannot paint.
  9515. this.w = Math.max(this.w, 1);
  9516. this.h = Math.max(this.h, 1);
  9517. var segments = this.getSegments(), p = { "path":"" },
  9518. d = [this.x, this.y, this.w, this.h];
  9519. // create path from segments.
  9520. for (var i = 0; i < segments.length; i++) {
  9521. p.path += jsPlumb.Segments.vml.SegmentRenderer.getPath(segments[i]);
  9522. p.path += " ";
  9523. }
  9524. //*
  9525. if (style.outlineColor) {
  9526. var outlineWidth = style.outlineWidth || 1,
  9527. outlineStrokeWidth = style.lineWidth + (2 * outlineWidth),
  9528. outlineStyle = {
  9529. strokeStyle : jsPlumbUtil.convertStyle(style.outlineColor),
  9530. lineWidth : outlineStrokeWidth
  9531. };
  9532. for (var aa in vmlAttributeMap) outlineStyle[aa] = style[aa];
  9533. if (this.bgCanvas == null) {
  9534. p["class"] = clazz;
  9535. p.coordsize = (d[2] * scale) + "," + (d[3] * scale);
  9536. this.bgCanvas = _node("shape", d, p, params.parent, this._jsPlumb.instance, true);
  9537. _pos(this.bgCanvas, d);
  9538. this.appendDisplayElement(this.bgCanvas, true);
  9539. this.initOpacityNodes(this.bgCanvas, ["stroke"]);
  9540. this.bgCanvas._jsPlumb = component;
  9541. }
  9542. else {
  9543. p.coordsize = (d[2] * scale) + "," + (d[3] * scale);
  9544. _pos(this.bgCanvas, d);
  9545. _atts(this.bgCanvas, p);
  9546. }
  9547. _applyStyles(this.bgCanvas, outlineStyle, this);
  9548. }
  9549. //*/
  9550. if (this.canvas == null) {
  9551. p["class"] = clazz;
  9552. p.coordsize = (d[2] * scale) + "," + (d[3] * scale);
  9553. this.canvas = _node("shape", d, p, params.parent, this._jsPlumb.instance, true);
  9554. this.appendDisplayElement(this.canvas, true);
  9555. this.initOpacityNodes(this.canvas, ["stroke"]);
  9556. this.canvas._jsPlumb = component;
  9557. }
  9558. else {
  9559. p.coordsize = (d[2] * scale) + "," + (d[3] * scale);
  9560. _pos(this.canvas, d);
  9561. _atts(this.canvas, p);
  9562. }
  9563. _applyStyles(this.canvas, style, this, this._jsPlumb.instance);
  9564. }
  9565. };
  9566. };
  9567. jsPlumbUtil.extend(VmlConnector, VmlComponent, {
  9568. setVisible:function(v) {
  9569. if (this.canvas) {
  9570. this.canvas.style.display = v ? "block" : "none";
  9571. }
  9572. if (this.bgCanvas) {
  9573. this.bgCanvas.style.display = v ? "block" : "none";
  9574. }
  9575. }
  9576. });
  9577. /*
  9578. *
  9579. * Base class for Vml Endpoints. extends VmlComponent.
  9580. *
  9581. */
  9582. var VmlEndpoint = window.VmlEndpoint = function(params) {
  9583. VmlComponent.apply(this, arguments);
  9584. this._jsPlumb.vml = null;//, opacityStrokeNode = null, opacityFillNode = null;
  9585. this.canvas = document.createElement("div");
  9586. this.canvas.style.position = "absolute";
  9587. this._jsPlumb.clazz = this._jsPlumb.instance.endpointClass + (params.cssClass ? (" " + params.cssClass) : "");
  9588. // TODO vml endpoint adds class to VML at constructor time. but the addClass method adds VML
  9589. // to the enclosing DIV. what to do? seems like it would be better to just target the div.
  9590. // HOWEVER...vml connection has no containing div. why not? it feels like it should.
  9591. params._jsPlumb.appendElement(this.canvas, params.parent);
  9592. this.paint = function(style, anchor) {
  9593. var p = { }, vml = this._jsPlumb.vml;
  9594. jsPlumbUtil.sizeElement(this.canvas, this.x, this.y, this.w, this.h);
  9595. if (this._jsPlumb.vml == null) {
  9596. p["class"] = this._jsPlumb.clazz;
  9597. vml = this._jsPlumb.vml = this.getVml([0,0, this.w, this.h], p, anchor, this.canvas, this._jsPlumb.instance);
  9598. this.appendDisplayElement(vml, true);
  9599. this.appendDisplayElement(this.canvas, true);
  9600. this.initOpacityNodes(vml, ["fill"]);
  9601. }
  9602. else {
  9603. _pos(vml, [0,0, this.w, this.h]);
  9604. _atts(vml, p);
  9605. }
  9606. _applyStyles(vml, style, this);
  9607. };
  9608. };
  9609. jsPlumbUtil.extend(VmlEndpoint, VmlComponent);
  9610. // ******************************* vml segments *****************************************************
  9611. jsPlumb.Segments.vml = {
  9612. SegmentRenderer : {
  9613. getPath : function(segment) {
  9614. return ({
  9615. "Straight":function(segment) {
  9616. var d = segment.params;
  9617. return "m" + _conv(d.x1) + "," + _conv(d.y1) + " l" + _conv(d.x2) + "," + _conv(d.y2) + " e";
  9618. },
  9619. "Bezier":function(segment) {
  9620. var d = segment.params;
  9621. return "m" + _conv(d.x1) + "," + _conv(d.y1) +
  9622. " c" + _conv(d.cp1x) + "," + _conv(d.cp1y) + "," + _conv(d.cp2x) + "," + _conv(d.cp2y) + "," + _conv(d.x2) + "," + _conv(d.y2) + " e";
  9623. },
  9624. "Arc":function(segment) {
  9625. var d = segment.params,
  9626. xmin = Math.min(d.x1, d.x2),
  9627. xmax = Math.max(d.x1, d.x2),
  9628. ymin = Math.min(d.y1, d.y2),
  9629. ymax = Math.max(d.y1, d.y2),
  9630. sf = segment.anticlockwise ? 1 : 0,
  9631. pathType = (segment.anticlockwise ? "at " : "wa "),
  9632. makePosString = function() {
  9633. if (d.loopback)
  9634. return "0,0," + _conv(2*d.r) + "," + _conv(2 * d.r);
  9635. var xy = [
  9636. null,
  9637. [ function() { return [xmin, ymin ];}, function() { return [xmin - d.r, ymin - d.r ];}],
  9638. [ function() { return [xmin - d.r, ymin ];}, function() { return [xmin, ymin - d.r ];}],
  9639. [ function() { return [xmin - d.r, ymin - d.r ];}, function() { return [xmin, ymin ];}],
  9640. [ function() { return [xmin, ymin - d.r ];}, function() { return [xmin - d.r, ymin ];}]
  9641. ][segment.segment][sf]();
  9642. return _conv(xy[0]) + "," + _conv(xy[1]) + "," + _conv(xy[0] + (2*d.r)) + "," + _conv(xy[1] + (2*d.r));
  9643. };
  9644. return pathType + " " + makePosString() + "," + _conv(d.x1) + "," + _conv(d.y1) + "," + _conv(d.x2) + "," + _conv(d.y2) + " e";
  9645. }
  9646. })[segment.type](segment);
  9647. }
  9648. }
  9649. };
  9650. // ******************************* /vml segments *****************************************************
  9651. // ******************************* vml endpoints *****************************************************
  9652. jsPlumb.Endpoints.vml.Dot = function() {
  9653. jsPlumb.Endpoints.Dot.apply(this, arguments);
  9654. VmlEndpoint.apply(this, arguments);
  9655. this.getVml = function(d, atts, anchor, parent, _jsPlumb) { return _node("oval", d, atts, parent, _jsPlumb); };
  9656. };
  9657. jsPlumbUtil.extend(jsPlumb.Endpoints.vml.Dot, VmlEndpoint);
  9658. jsPlumb.Endpoints.vml.Rectangle = function() {
  9659. jsPlumb.Endpoints.Rectangle.apply(this, arguments);
  9660. VmlEndpoint.apply(this, arguments);
  9661. this.getVml = function(d, atts, anchor, parent, _jsPlumb) { return _node("rect", d, atts, parent, _jsPlumb); };
  9662. };
  9663. jsPlumbUtil.extend(jsPlumb.Endpoints.vml.Rectangle, VmlEndpoint);
  9664. /*
  9665. * VML Image Endpoint is the same as the default image endpoint.
  9666. */
  9667. jsPlumb.Endpoints.vml.Image = jsPlumb.Endpoints.Image;
  9668. /**
  9669. * placeholder for Blank endpoint in vml renderer.
  9670. */
  9671. jsPlumb.Endpoints.vml.Blank = jsPlumb.Endpoints.Blank;
  9672. // ******************************* /vml endpoints *****************************************************
  9673. // ******************************* vml overlays *****************************************************
  9674. /**
  9675. * VML Label renderer. uses the default label renderer (which adds an element to the DOM)
  9676. */
  9677. jsPlumb.Overlays.vml.Label = jsPlumb.Overlays.Label;
  9678. /**
  9679. * VML Custom renderer. uses the default Custom renderer (which adds an element to the DOM)
  9680. */
  9681. jsPlumb.Overlays.vml.Custom = jsPlumb.Overlays.Custom;
  9682. /**
  9683. * Abstract VML arrow superclass
  9684. */
  9685. var AbstractVmlArrowOverlay = function(superclass, originalArgs) {
  9686. superclass.apply(this, originalArgs);
  9687. VmlComponent.apply(this, originalArgs);
  9688. var self = this, path = null;
  9689. this.canvas = null;
  9690. this.isAppendedAtTopLevel = true;
  9691. var getPath = function(d) {
  9692. return "m " + _conv(d.hxy.x) + "," + _conv(d.hxy.y) +
  9693. " l " + _conv(d.tail[0].x) + "," + _conv(d.tail[0].y) +
  9694. " " + _conv(d.cxy.x) + "," + _conv(d.cxy.y) +
  9695. " " + _conv(d.tail[1].x) + "," + _conv(d.tail[1].y) +
  9696. " x e";
  9697. };
  9698. this.paint = function(params, containerExtents) {
  9699. // only draws for connectors, not endpoints.
  9700. if (params.component.canvas && containerExtents) {
  9701. var p = {}, d = params.d, connector = params.component;
  9702. if (params.strokeStyle) {
  9703. p.stroked = "true";
  9704. p.strokecolor = jsPlumbUtil.convertStyle(params.strokeStyle, true);
  9705. }
  9706. if (params.lineWidth) p.strokeweight = params.lineWidth + "px";
  9707. if (params.fillStyle) {
  9708. p.filled = "true";
  9709. p.fillcolor = params.fillStyle;
  9710. }
  9711. var xmin = Math.min(d.hxy.x, d.tail[0].x, d.tail[1].x, d.cxy.x),
  9712. ymin = Math.min(d.hxy.y, d.tail[0].y, d.tail[1].y, d.cxy.y),
  9713. xmax = Math.max(d.hxy.x, d.tail[0].x, d.tail[1].x, d.cxy.x),
  9714. ymax = Math.max(d.hxy.y, d.tail[0].y, d.tail[1].y, d.cxy.y),
  9715. w = Math.abs(xmax - xmin),
  9716. h = Math.abs(ymax - ymin),
  9717. dim = [xmin, ymin, w, h];
  9718. // for VML, we create overlays using shapes that have the same dimensions and
  9719. // coordsize as their connector - overlays calculate themselves relative to the
  9720. // connector (it's how it's been done since the original canvas implementation, because
  9721. // for canvas that makes sense).
  9722. p.path = getPath(d);
  9723. p.coordsize = (connector.w * scale) + "," + (connector.h * scale);
  9724. dim[0] = connector.x;
  9725. dim[1] = connector.y;
  9726. dim[2] = connector.w;
  9727. dim[3] = connector.h;
  9728. if (self.canvas == null) {
  9729. var overlayClass = connector._jsPlumb.overlayClass || "";
  9730. var clazz = originalArgs && (originalArgs.length == 1) ? (originalArgs[0].cssClass || "") : "";
  9731. p["class"] = clazz + " " + overlayClass;
  9732. self.canvas = _node("shape", dim, p, connector.canvas.parentNode, connector._jsPlumb.instance, true);
  9733. connector.appendDisplayElement(self.canvas, true);
  9734. }
  9735. else {
  9736. _pos(self.canvas, dim);
  9737. _atts(self.canvas, p);
  9738. }
  9739. }
  9740. };
  9741. this.cleanup = function() {
  9742. if (this.canvas != null) this._jsPlumb.instance.removeElement(this.canvas);
  9743. };
  9744. };
  9745. jsPlumbUtil.extend(AbstractVmlArrowOverlay, [VmlComponent, jsPlumb.Overlays.AbstractOverlay], {
  9746. setVisible : function(state) {
  9747. this.canvas.style.display = state ? "block" : "none";
  9748. }
  9749. });
  9750. jsPlumb.Overlays.vml.Arrow = function() {
  9751. AbstractVmlArrowOverlay.apply(this, [jsPlumb.Overlays.Arrow, arguments]);
  9752. };
  9753. jsPlumbUtil.extend(jsPlumb.Overlays.vml.Arrow, [ jsPlumb.Overlays.Arrow, AbstractVmlArrowOverlay ]);
  9754. jsPlumb.Overlays.vml.PlainArrow = function() {
  9755. AbstractVmlArrowOverlay.apply(this, [jsPlumb.Overlays.PlainArrow, arguments]);
  9756. };
  9757. jsPlumbUtil.extend(jsPlumb.Overlays.vml.PlainArrow, [ jsPlumb.Overlays.PlainArrow, AbstractVmlArrowOverlay ]);
  9758. jsPlumb.Overlays.vml.Diamond = function() {
  9759. AbstractVmlArrowOverlay.apply(this, [jsPlumb.Overlays.Diamond, arguments]);
  9760. };
  9761. jsPlumbUtil.extend(jsPlumb.Overlays.vml.Diamond, [ jsPlumb.Overlays.Diamond, AbstractVmlArrowOverlay ]);
  9762. // ******************************* /vml overlays *****************************************************
  9763. })();
  9764. /*
  9765. * jsPlumb
  9766. *
  9767. * Title:jsPlumb 1.7.2
  9768. *
  9769. * Provides a way to visually connect elements on an HTML page, using SVG or VML.
  9770. *
  9771. * This file contains the jQuery adapter.
  9772. *
  9773. * Copyright (c) 2010 - 2014 Simon Porritt (simon@jsplumbtoolkit.com)
  9774. *
  9775. * http://jsplumbtoolkit.com
  9776. * http://github.com/sporritt/jsplumb
  9777. *
  9778. * Dual licensed under the MIT and GPL2 licenses.
  9779. */
  9780. ;(function($) {
  9781. "use strict";
  9782. var _getElementObject = function(el) {
  9783. return typeof(el) == "string" ? $("#" + el) : $(el);
  9784. };
  9785. $.extend(jsPlumbInstance.prototype, {
  9786. // ---------------------------- DOM MANIPULATION ---------------------------------------
  9787. /**
  9788. * gets a DOM element from the given input, which might be a string (in which case we just do document.getElementById),
  9789. * a selector (in which case we return el[0]), or a DOM element already (we assume this if it's not either of the other
  9790. * two cases). this is the opposite of getElementObject below.
  9791. */
  9792. getDOMElement : function(el) {
  9793. if (el == null) return null;
  9794. if (typeof(el) == "string") return document.getElementById(el);
  9795. else if (el.context || el.length != null) return el[0];
  9796. else return el;
  9797. },
  9798. /**
  9799. * gets an "element object" from the given input. this means an object that is used by the
  9800. * underlying library on which jsPlumb is running. 'el' may already be one of these objects,
  9801. * in which case it is returned as-is. otherwise, 'el' is a String, the library's lookup
  9802. * function is used to find the element, using the given String as the element's id.
  9803. *
  9804. */
  9805. getElementObject : _getElementObject,
  9806. /**
  9807. * removes an element from the DOM. doing it via the library is
  9808. * safer from a memory perspective, as it ix expected that the library's
  9809. * remove method will unbind any event listeners before removing the element from the DOM.
  9810. */
  9811. removeElement:function(element) {
  9812. _getElementObject(element).remove();
  9813. },
  9814. // ---------------------------- END DOM MANIPULATION ---------------------------------------
  9815. // ---------------------------- MISCELLANEOUS ---------------------------------------
  9816. /**
  9817. * animates the given element.
  9818. */
  9819. doAnimate : function(el, properties, options) {
  9820. el.animate(properties, options);
  9821. },
  9822. getSelector : function(context, spec) {
  9823. if (arguments.length == 2)
  9824. return _getElementObject(context).find(spec);
  9825. else
  9826. return $(context);
  9827. },
  9828. // ---------------------------- END MISCELLANEOUS ---------------------------------------
  9829. // -------------------------------------- DRAG/DROP ---------------------------------
  9830. destroyDraggable : function(el) {
  9831. if ($(el).data("draggable"))
  9832. $(el).draggable("destroy");
  9833. },
  9834. destroyDroppable : function(el) {
  9835. if ($(el).data("droppable"))
  9836. $(el).droppable("destroy");
  9837. },
  9838. /**
  9839. * initialises the given element to be draggable.
  9840. */
  9841. initDraggable : function(el, options, isPlumbedComponent) {
  9842. options = options || {};
  9843. el = $(el);
  9844. options.start = jsPlumbUtil.wrap(options.start, function() {
  9845. $("body").addClass(this.dragSelectClass);
  9846. }, false);
  9847. options.stop = jsPlumbUtil.wrap(options.stop, function() {
  9848. $("body").removeClass(this.dragSelectClass);
  9849. });
  9850. // remove helper directive if present and no override
  9851. if (!options.doNotRemoveHelper)
  9852. options.helper = null;
  9853. if (isPlumbedComponent == "internal")
  9854. options.scope = options.scope || jsPlumb.Defaults.Scope;
  9855. el.draggable(options);
  9856. },
  9857. /**
  9858. * initialises the given element to be droppable.
  9859. */
  9860. initDroppable : function(el, options) {
  9861. options.scope = options.scope || jsPlumb.Defaults.Scope;
  9862. $(el).droppable(options);
  9863. },
  9864. isAlreadyDraggable : function(el) {
  9865. return $(el).hasClass("ui-draggable");
  9866. },
  9867. /**
  9868. * returns whether or not drag is supported (by the library, not whether or not it is disabled) for the given element.
  9869. */
  9870. isDragSupported : function(el, options) {
  9871. return $(el).draggable;
  9872. },
  9873. /**
  9874. * returns whether or not drop is supported (by the library, not whether or not it is disabled) for the given element.
  9875. */
  9876. isDropSupported : function(el, options) {
  9877. return $(el).droppable;
  9878. },
  9879. /**
  9880. * takes the args passed to an event function and returns you an object representing that which is being dragged.
  9881. */
  9882. getDragObject : function(eventArgs) {
  9883. //return eventArgs[1].draggable || eventArgs[1].helper;
  9884. return eventArgs[1].helper || eventArgs[1].draggable;
  9885. },
  9886. getDragScope : function(el) {
  9887. return $(el).draggable("option", "scope");
  9888. },
  9889. getDropEvent : function(args) {
  9890. return args[0];
  9891. },
  9892. getDropScope : function(el) {
  9893. return $(el).droppable("option", "scope");
  9894. },
  9895. /**
  9896. * takes the args passed to an event function and returns you an object that gives the
  9897. * position of the object being moved, as a js object with the same params as the result of
  9898. * getOffset, ie: { left: xxx, top: xxx }.
  9899. *
  9900. * different libraries have different signatures for their event callbacks.
  9901. * see getDragObject as well
  9902. */
  9903. getUIPosition : function(eventArgs, zoom, dontAdjustHelper) {
  9904. var ret;
  9905. zoom = zoom || 1;
  9906. if (eventArgs.length == 1) {
  9907. ret = { left: eventArgs[0].pageX, top:eventArgs[0].pageY };
  9908. }
  9909. else {
  9910. var ui = eventArgs[1],
  9911. _offset = ui.position;//ui.offset;
  9912. ret = _offset || ui.absolutePosition;
  9913. // adjust ui position to account for zoom, because jquery ui does not do this.
  9914. if (!dontAdjustHelper) {
  9915. ui.position.left /= zoom;
  9916. ui.position.top /= zoom;
  9917. }
  9918. }
  9919. return { left:ret.left, top: ret.top };
  9920. },
  9921. isDragFilterSupported:function() { return true; },
  9922. setDragFilter : function(el, filter) {
  9923. if (jsPlumb.isAlreadyDraggable(el))
  9924. $(el).draggable("option", "cancel", filter);
  9925. },
  9926. setElementDraggable : function(el, draggable) {
  9927. $(el).draggable("option", "disabled", !draggable);
  9928. },
  9929. setDragScope : function(el, scope) {
  9930. $(el).draggable("option", "scope", scope);
  9931. },
  9932. /**
  9933. * mapping of drag events for jQuery
  9934. */
  9935. dragEvents : {
  9936. 'start':'start', 'stop':'stop', 'drag':'drag', 'step':'step',
  9937. 'over':'over', 'out':'out', 'drop':'drop', 'complete':'complete'
  9938. },
  9939. animEvents:{
  9940. 'step':"step", 'complete':'complete'
  9941. },
  9942. getOriginalEvent : function(e) { return e.originalEvent || e; },
  9943. /**
  9944. * note that jquery ignores the name of the event you wanted to trigger, and figures it out for itself.
  9945. * the other libraries do not. yui, in fact, cannot even pass an original event. we have to pull out stuff
  9946. * from the originalEvent to put in an options object for YUI.
  9947. * @param el
  9948. * @param event
  9949. * @param originalEvent
  9950. */
  9951. trigger : function(el, event, originalEvent) {
  9952. var h = jQuery._data(_getElementObject(el)[0], "handle");
  9953. h(originalEvent);
  9954. }
  9955. // -------------------------------------- END DRAG/DROP ---------------------------------
  9956. });
  9957. $(document).ready(jsPlumb.init);
  9958. })(jQuery);