jquery.jsPlumb.all.js 413 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561
  1. /*
  2. * jsPlumb
  3. *
  4. * Title:jsPlumb 1.3.16
  5. *
  6. * Provides a way to visually connect elements on an HTML page, using either SVG, Canvas
  7. * elements, or VML.
  8. *
  9. * This file contains the util functions
  10. *
  11. * Copyright (c) 2010 - 2012 Simon Porritt (http://jsplumb.org)
  12. *
  13. * http://jsplumb.org
  14. * http://github.com/sporritt/jsplumb
  15. * http://code.google.com/p/jsplumb
  16. *
  17. * Dual licensed under the MIT and GPL2 licenses.
  18. */
  19. jsPlumbUtil = {
  20. isArray : function(a) {
  21. return Object.prototype.toString.call(a) === "[object Array]";
  22. },
  23. isString : function(s) {
  24. return typeof s === "string";
  25. },
  26. isBoolean: function(s) {
  27. return typeof s === "boolean";
  28. },
  29. isObject : function(o) {
  30. return Object.prototype.toString.call(o) === "[object Object]";
  31. },
  32. isDate : function(o) {
  33. return Object.prototype.toString.call(o) === "[object Date]";
  34. },
  35. isFunction: function(o) {
  36. return Object.prototype.toString.call(o) === "[object Function]";
  37. },
  38. clone : function(a) {
  39. if (this.isString(a)) return new String(a);
  40. else if (this.isBoolean(a)) return new Boolean(a);
  41. else if (this.isDate(a)) return new Date(a.getTime());
  42. else if (this.isFunction(a)) return a;
  43. else if (this.isArray(a)) {
  44. var b = [];
  45. for (var i = 0; i < a.length; i++)
  46. b.push(this.clone(a[i]));
  47. return b;
  48. }
  49. else if (this.isObject(a)) {
  50. var b = {};
  51. for (var i in a)
  52. b[i] = this.clone(a[i]);
  53. return b;
  54. }
  55. else return a;
  56. },
  57. merge : function(a, b) {
  58. var c = this.clone(a);
  59. for (var i in b) {
  60. if (c[i] == null || this.isString(b[i]) || this.isBoolean(b[i]))
  61. c[i] = b[i];
  62. else {
  63. if (this.isArray(b[i]) && this.isArray(c[i])) {
  64. var ar = [];
  65. ar.push.apply(ar, c[i]);
  66. ar.push.apply(ar, b[i]);
  67. c[i] = ar;
  68. }
  69. else if(this.isObject(c[i]) && this.isObject(b[i])) {
  70. for (var j in b[i])
  71. c[i][j] = b[i][j];
  72. }
  73. }
  74. }
  75. return c;
  76. },
  77. convertStyle : function(s, ignoreAlpha) {
  78. // TODO: jsPlumb should support a separate 'opacity' style member.
  79. if ("transparent" === s) return s;
  80. var o = s,
  81. pad = function(n) { return n.length == 1 ? "0" + n : n; },
  82. hex = function(k) { return pad(Number(k).toString(16)); },
  83. pattern = /(rgb[a]?\()(.*)(\))/;
  84. if (s.match(pattern)) {
  85. var parts = s.match(pattern)[2].split(",");
  86. o = "#" + hex(parts[0]) + hex(parts[1]) + hex(parts[2]);
  87. if (!ignoreAlpha && parts.length == 4)
  88. o = o + hex(parts[3]);
  89. }
  90. return o;
  91. },
  92. gradient : function(p1, p2) {
  93. p1 = jsPlumbUtil.isArray(p1) ? p1 : [p1.x, p1.y];
  94. p2 = jsPlumbUtil.isArray(p2) ? p2 : [p2.x, p2.y];
  95. return (p2[1] - p1[1]) / (p2[0] - p1[0]);
  96. },
  97. normal : function(p1, p2) {
  98. return -1 / jsPlumbUtil.gradient(p1,p2);
  99. },
  100. lineLength : function(p1, p2) {
  101. p1 = jsPlumbUtil.isArray(p1) ? p1 : [p1.x, p1.y];
  102. p2 = jsPlumbUtil.isArray(p2) ? p2 : [p2.x, p2.y];
  103. return Math.sqrt(Math.pow(p2[1] - p1[1], 2) + Math.pow(p2[0] - p1[0], 2));
  104. },
  105. segment : function(p1, p2) {
  106. p1 = jsPlumbUtil.isArray(p1) ? p1 : [p1.x, p1.y];
  107. p2 = jsPlumbUtil.isArray(p2) ? p2 : [p2.x, p2.y];
  108. if (p2[0] > p1[0]) {
  109. return (p2[1] > p1[1]) ? 2 : 1;
  110. }
  111. else {
  112. return (p2[1] > p1[1]) ? 3 : 4;
  113. }
  114. },
  115. intersects : function(r1, r2) {
  116. var x1 = r1.x, x2 = r1.x + r1.w, y1 = r1.y, y2 = r1.y + r1.h,
  117. a1 = r2.x, a2 = r2.x + r2.w, b1 = r2.y, b2 = r2.y + r2.h;
  118. return ( (x1 <= a1 && a1 <= x2) && (y1 <= b1 && b1 <= y2) ) ||
  119. ( (x1 <= a2 && a2 <= x2) && (y1 <= b1 && b1 <= y2) ) ||
  120. ( (x1 <= a1 && a1 <= x2) && (y1 <= b2 && b2 <= y2) ) ||
  121. ( (x1 <= a2 && a1 <= x2) && (y1 <= b2 && b2 <= y2) ) ||
  122. ( (a1 <= x1 && x1 <= a2) && (b1 <= y1 && y1 <= b2) ) ||
  123. ( (a1 <= x2 && x2 <= a2) && (b1 <= y1 && y1 <= b2) ) ||
  124. ( (a1 <= x1 && x1 <= a2) && (b1 <= y2 && y2 <= b2) ) ||
  125. ( (a1 <= x2 && x1 <= a2) && (b1 <= y2 && y2 <= b2) );
  126. },
  127. segmentMultipliers : [null, [1, -1], [1, 1], [-1, 1], [-1, -1] ],
  128. inverseSegmentMultipliers : [null, [-1, -1], [-1, 1], [1, 1], [1, -1] ],
  129. pointOnLine : function(fromPoint, toPoint, distance) {
  130. var m = jsPlumbUtil.gradient(fromPoint, toPoint),
  131. s = jsPlumbUtil.segment(fromPoint, toPoint),
  132. segmentMultiplier = distance > 0 ? jsPlumbUtil.segmentMultipliers[s] : jsPlumbUtil.inverseSegmentMultipliers[s],
  133. theta = Math.atan(m),
  134. y = Math.abs(distance * Math.sin(theta)) * segmentMultiplier[1],
  135. x = Math.abs(distance * Math.cos(theta)) * segmentMultiplier[0];
  136. return { x:fromPoint.x + x, y:fromPoint.y + y };
  137. },
  138. /**
  139. * calculates a perpendicular to the line fromPoint->toPoint, that passes through toPoint and is 'length' long.
  140. * @param fromPoint
  141. * @param toPoint
  142. * @param length
  143. */
  144. perpendicularLineTo : function(fromPoint, toPoint, length) {
  145. var m = jsPlumbUtil.gradient(fromPoint, toPoint),
  146. theta2 = Math.atan(-1 / m),
  147. y = length / 2 * Math.sin(theta2),
  148. x = length / 2 * Math.cos(theta2);
  149. return [{x:toPoint.x + x, y:toPoint.y + y}, {x:toPoint.x - x, y:toPoint.y - y}];
  150. },
  151. findWithFunction : function(a, f) {
  152. if (a)
  153. for (var i = 0; i < a.length; i++) if (f(a[i])) return i;
  154. return -1;
  155. },
  156. indexOf : function(l, v) {
  157. return jsPlumbUtil.findWithFunction(l, function(_v) { return _v == v; });
  158. },
  159. removeWithFunction : function(a, f) {
  160. var idx = jsPlumbUtil.findWithFunction(a, f);
  161. if (idx > -1) a.splice(idx, 1);
  162. return idx != -1;
  163. },
  164. remove : function(l, v) {
  165. var idx = jsPlumbUtil.indexOf(l, v);
  166. if (idx > -1) l.splice(idx, 1);
  167. return idx != -1;
  168. },
  169. // TODO support insert index
  170. addWithFunction : function(list, item, hashFunction) {
  171. if (jsPlumbUtil.findWithFunction(list, hashFunction) == -1) list.push(item);
  172. },
  173. addToList : function(map, key, value) {
  174. var l = map[key];
  175. if (l == null) {
  176. l = [], map[key] = l;
  177. }
  178. l.push(value);
  179. return l;
  180. },
  181. /**
  182. * EventGenerator
  183. * Superclass for objects that generate events - jsPlumb extends this, as does jsPlumbUIComponent, which all the UI elements extend.
  184. */
  185. EventGenerator : function() {
  186. var _listeners = {}, self = this;
  187. // this is a list of events that should re-throw any errors that occur during their dispatch. as of 1.3.0 this is private to
  188. // jsPlumb, but it seems feasible that people might want to manipulate this list. the thinking is that we don't want event
  189. // listeners to bring down jsPlumb - or do we. i can't make up my mind about this, but i know i want to hear about it if the "ready"
  190. // event fails, because then my page has most likely not initialised. so i have this halfway-house solution. it will be interesting
  191. // to hear what other people think.
  192. var eventsToDieOn = [ "ready" ];
  193. /*
  194. * Binds a listener to an event.
  195. *
  196. * Parameters:
  197. * event - name of the event to bind to.
  198. * listener - function to execute.
  199. */
  200. this.bind = function(event, listener) {
  201. jsPlumbUtil.addToList(_listeners, event, listener);
  202. return self;
  203. };
  204. /*
  205. * Fires an update for the given event.
  206. *
  207. * Parameters:
  208. * event - event to fire
  209. * value - value to pass to the event listener(s).
  210. * originalEvent - the original event from the browser
  211. */
  212. this.fire = function(event, value, originalEvent) {
  213. if (_listeners[event]) {
  214. for ( var i = 0; i < _listeners[event].length; i++) {
  215. // doing it this way rather than catching and then possibly re-throwing means that an error propagated by this
  216. // method will have the whole call stack available in the debugger.
  217. if (jsPlumbUtil.findWithFunction(eventsToDieOn, function(e) { return e === event}) != -1)
  218. _listeners[event][i](value, originalEvent);
  219. else {
  220. // for events we don't want to die on, catch and log.
  221. try {
  222. _listeners[event][i](value, originalEvent);
  223. } catch (e) {
  224. jsPlumbUtil.log("jsPlumb: fire failed for event " + event + " : " + e);
  225. }
  226. }
  227. }
  228. }
  229. return self;
  230. };
  231. /*
  232. * Clears either all listeners, or listeners for some specific event.
  233. *
  234. * Parameters:
  235. * event - optional. constrains the clear to just listeners for this event.
  236. */
  237. this.unbind = function(event) {
  238. if (event)
  239. delete _listeners[event];
  240. else {
  241. _listeners = {};
  242. }
  243. return self;
  244. };
  245. this.getListener = function(forEvent) {
  246. return _listeners[forEvent];
  247. };
  248. },
  249. logEnabled : true,
  250. log : function() {
  251. if (jsPlumbUtil.logEnabled && typeof console != "undefined") {
  252. try {
  253. var msg = arguments[arguments.length - 1];
  254. console.log(msg);
  255. }
  256. catch (e) {}
  257. }
  258. },
  259. group : function(g) { if (jsPlumbUtil.logEnabled && typeof console != "undefined") console.group(g); },
  260. groupEnd : function(g) { if (jsPlumbUtil.logEnabled && typeof console != "undefined") console.groupEnd(g); },
  261. time : function(t) { if (jsPlumbUtil.logEnabled && typeof console != "undefined") console.time(t); },
  262. timeEnd : function(t) { if (jsPlumbUtil.logEnabled && typeof console != "undefined") console.timeEnd(t); }
  263. };/*
  264. * jsPlumb
  265. *
  266. * Title:jsPlumb 1.3.16
  267. *
  268. * Provides a way to visually connect elements on an HTML page, using either SVG, Canvas
  269. * elements, or VML.
  270. *
  271. * This file contains the base functionality for DOM type adapters.
  272. *
  273. * Copyright (c) 2010 - 2012 Simon Porritt (http://jsplumb.org)
  274. *
  275. * http://jsplumb.org
  276. * http://github.com/sporritt/jsplumb
  277. * http://code.google.com/p/jsplumb
  278. *
  279. * Dual licensed under the MIT and GPL2 licenses.
  280. */
  281. ;(function() {
  282. var canvasAvailable = !!document.createElement('canvas').getContext,
  283. svgAvailable = !!window.SVGAngle || document.implementation.hasFeature("http://www.w3.org/TR/SVG11/feature#BasicStructure", "1.1"),
  284. // http://stackoverflow.com/questions/654112/how-do-you-detect-support-for-vml-or-svg-in-a-browser
  285. vmlAvailable = function() {
  286. if (vmlAvailable.vml == undefined) {
  287. var a = document.body.appendChild(document.createElement('div'));
  288. a.innerHTML = '<v:shape id="vml_flag1" adj="1" />';
  289. var b = a.firstChild;
  290. b.style.behavior = "url(#default#VML)";
  291. vmlAvailable.vml = b ? typeof b.adj == "object": true;
  292. a.parentNode.removeChild(a);
  293. }
  294. return vmlAvailable.vml;
  295. };
  296. /**
  297. Manages dragging for some instance of jsPlumb.
  298. */
  299. var DragManager = function(_currentInstance) {
  300. var _draggables = {}, _dlist = [], _delements = {}, _elementsWithEndpoints = {};
  301. /**
  302. register some element as draggable. right now the drag init stuff is done elsewhere, and it is
  303. possible that will continue to be the case.
  304. */
  305. this.register = function(el) {
  306. var jpcl = jsPlumb.CurrentLibrary;
  307. el = jpcl.getElementObject(el);
  308. var id = _currentInstance.getId(el),
  309. domEl = jpcl.getDOMElement(el),
  310. parentOffset = jpcl.getOffset(el);
  311. if (!_draggables[id]) {
  312. _draggables[id] = el;
  313. _dlist.push(el);
  314. _delements[id] = {};
  315. }
  316. // look for child elements that have endpoints and register them against this draggable.
  317. var _oneLevel = function(p, startOffset) {
  318. if (p) {
  319. for (var i = 0; i < p.childNodes.length; i++) {
  320. if (p.childNodes[i].nodeType != 3) {
  321. var cEl = jpcl.getElementObject(p.childNodes[i]),
  322. cid = _currentInstance.getId(cEl, null, true);
  323. if (cid && _elementsWithEndpoints[cid] && _elementsWithEndpoints[cid] > 0) {
  324. var cOff = jpcl.getOffset(cEl);
  325. _delements[id][cid] = {
  326. id:cid,
  327. offset:{
  328. left:cOff.left - parentOffset.left,
  329. top:cOff.top - parentOffset.top
  330. }
  331. };
  332. }
  333. _oneLevel(p.childNodes[i]);
  334. }
  335. }
  336. }
  337. };
  338. _oneLevel(domEl);
  339. };
  340. // refresh the offsets for child elements of this element.
  341. this.updateOffsets = function(elId) {
  342. var jpcl = jsPlumb.CurrentLibrary,
  343. el = jpcl.getElementObject(elId),
  344. id = _currentInstance.getId(el),
  345. children = _delements[id],
  346. parentOffset = jpcl.getOffset(el);
  347. if (children) {
  348. for (var i in children) {
  349. var cel = jpcl.getElementObject(i),
  350. cOff = jpcl.getOffset(cel);
  351. _delements[id][i] = {
  352. id:i,
  353. offset:{
  354. left:cOff.left - parentOffset.left,
  355. top:cOff.top - parentOffset.top
  356. }
  357. };
  358. }
  359. }
  360. };
  361. /**
  362. notification that an endpoint was added to the given el. we go up from that el's parent
  363. node, looking for a parent that has been registered as a draggable. if we find one, we add this
  364. el to that parent's list of elements to update on drag (if it is not there already)
  365. */
  366. this.endpointAdded = function(el) {
  367. var jpcl = jsPlumb.CurrentLibrary, b = document.body, id = _currentInstance.getId(el), c = jpcl.getDOMElement(el),
  368. p = c.parentNode, done = p == b;
  369. _elementsWithEndpoints[id] = _elementsWithEndpoints[id] ? _elementsWithEndpoints[id] + 1 : 1;
  370. while (p != b) {
  371. var pid = _currentInstance.getId(p, null, true);
  372. if (pid && _draggables[pid]) {
  373. var idx = -1, pEl = jpcl.getElementObject(p), pLoc = jpcl.getOffset(pEl);
  374. if (_delements[pid][id] == null) {
  375. var cLoc = jsPlumb.CurrentLibrary.getOffset(el);
  376. _delements[pid][id] = {
  377. id:id,
  378. offset:{
  379. left:cLoc.left - pLoc.left,
  380. top:cLoc.top - pLoc.top
  381. }
  382. };
  383. }
  384. break;
  385. }
  386. p = p.parentNode;
  387. }
  388. };
  389. this.endpointDeleted = function(endpoint) {
  390. if (_elementsWithEndpoints[endpoint.elementId]) {
  391. _elementsWithEndpoints[endpoint.elementId]--;
  392. if (_elementsWithEndpoints[endpoint.elementId] <= 0) {
  393. for (var i in _delements) {
  394. delete _delements[i][endpoint.elementId];
  395. }
  396. }
  397. }
  398. };
  399. this.getElementsForDraggable = function(id) {
  400. return _delements[id];
  401. };
  402. this.reset = function() {
  403. _draggables = {};
  404. _dlist = [];
  405. _delements = {};
  406. _elementsWithEndpoints = {};
  407. };
  408. };
  409. // for those browsers that dont have it. they still don't have it! but at least they won't crash.
  410. if (!window.console)
  411. window.console = { time:function(){}, timeEnd:function(){}, group:function(){}, groupEnd:function(){}, log:function(){} };
  412. window.jsPlumbAdapter = {
  413. headless:false,
  414. appendToRoot : function(node) {
  415. document.body.appendChild(node);
  416. },
  417. getRenderModes : function() {
  418. return [ "canvas", "svg", "vml" ]
  419. },
  420. isRenderModeAvailable : function(m) {
  421. return {
  422. "canvas":canvasAvailable,
  423. "svg":svgAvailable,
  424. "vml":vmlAvailable()
  425. }[m];
  426. },
  427. getDragManager : function(_jsPlumb) {
  428. return new DragManager(_jsPlumb);
  429. },
  430. setRenderMode : function(mode) {
  431. var renderMode;
  432. if (mode) {
  433. mode = mode.toLowerCase();
  434. var canvasAvailable = this.isRenderModeAvailable("canvas"),
  435. svgAvailable = this.isRenderModeAvailable("svg"),
  436. vmlAvailable = this.isRenderModeAvailable("vml");
  437. //if (mode !== jsPlumb.CANVAS && mode !== jsPlumb.SVG && mode !== jsPlumb.VML) throw new Error("render mode must be one of jsPlumb.CANVAS, jsPlumb.SVG or jsPlumb.VML");
  438. // now test we actually have the capability to do this.
  439. if (mode === "svg") {
  440. if (svgAvailable) renderMode = "svg"
  441. else if (canvasAvailable) renderMode = "canvas"
  442. else if (vmlAvailable) renderMode = "vml"
  443. }
  444. else if (mode === "canvas" && canvasAvailable) renderMode = "canvas";
  445. else if (vmlAvailable) renderMode = "vml";
  446. }
  447. return renderMode;
  448. }
  449. };
  450. })();/*
  451. * jsPlumb
  452. *
  453. * Title:jsPlumb 1.3.16
  454. *
  455. * Provides a way to visually connect elements on an HTML page, using either SVG, Canvas
  456. * elements, or VML.
  457. *
  458. * This file contains the jsPlumb core code.
  459. *
  460. * Copyright (c) 2010 - 2012 Simon Porritt (simon.porritt@gmail.com)
  461. *
  462. * http://jsplumb.org
  463. * http://github.com/sporritt/jsplumb
  464. * http://code.google.com/p/jsplumb
  465. *
  466. * Dual licensed under the MIT and GPL2 licenses.
  467. */
  468. ;(function() {
  469. /**
  470. * Class:jsPlumb
  471. * The jsPlumb engine, registered as a static object in the window. This object contains all of the methods you will use to
  472. * create and maintain Connections and Endpoints.
  473. */
  474. var _findWithFunction = jsPlumbUtil.findWithFunction,
  475. _indexOf = jsPlumbUtil.indexOf,
  476. _removeWithFunction = jsPlumbUtil.removeWithFunction,
  477. _remove = jsPlumbUtil.remove,
  478. // TODO support insert index
  479. _addWithFunction = jsPlumbUtil.addWithFunction,
  480. _addToList = jsPlumbUtil.addToList,
  481. /**
  482. an isArray function that even works across iframes...see here:
  483. http://tobyho.com/2011/01/28/checking-types-in-javascript/
  484. i was originally using "a.constructor == Array" as a test.
  485. */
  486. _isArray = jsPlumbUtil.isArray,
  487. _isString = jsPlumbUtil.isString,
  488. _isObject = jsPlumbUtil.isObject;
  489. var _connectionBeingDragged = null,
  490. _getAttribute = function(el, attName) { return jsPlumb.CurrentLibrary.getAttribute(_getElementObject(el), attName); },
  491. _setAttribute = function(el, attName, attValue) { jsPlumb.CurrentLibrary.setAttribute(_getElementObject(el), attName, attValue); },
  492. _addClass = function(el, clazz) { jsPlumb.CurrentLibrary.addClass(_getElementObject(el), clazz); },
  493. _hasClass = function(el, clazz) { return jsPlumb.CurrentLibrary.hasClass(_getElementObject(el), clazz); },
  494. _removeClass = function(el, clazz) { jsPlumb.CurrentLibrary.removeClass(_getElementObject(el), clazz); },
  495. _getElementObject = function(el) { return jsPlumb.CurrentLibrary.getElementObject(el); },
  496. _getOffset = function(el, _instance) {
  497. var o = jsPlumb.CurrentLibrary.getOffset(_getElementObject(el));
  498. if (_instance != null) {
  499. var z = _instance.getZoom();
  500. return {left:o.left / z, top:o.top / z };
  501. }
  502. else
  503. return o;
  504. },
  505. _getSize = function(el) {
  506. return jsPlumb.CurrentLibrary.getSize(_getElementObject(el));
  507. },
  508. _log = jsPlumbUtil.log,
  509. _group = jsPlumbUtil.group,
  510. _groupEnd = jsPlumbUtil.groupEnd,
  511. _time = jsPlumbUtil.time,
  512. _timeEnd = jsPlumbUtil.timeEnd,
  513. /**
  514. * creates a timestamp, using milliseconds since 1970, but as a string.
  515. */
  516. _timestamp = function() { return "" + (new Date()).getTime(); },
  517. /*
  518. * Class:jsPlumbUIComponent
  519. * Abstract superclass for UI components Endpoint and Connection. Provides the abstraction of paintStyle/hoverPaintStyle,
  520. * and also extends jsPlumbUtil.EventGenerator to provide the bind and fire methods.
  521. */
  522. jsPlumbUIComponent = function(params) {
  523. var self = this,
  524. a = arguments,
  525. _hover = false,
  526. parameters = params.parameters || {},
  527. idPrefix = self.idPrefix,
  528. id = idPrefix + (new Date()).getTime(),
  529. paintStyle = null,
  530. hoverPaintStyle = null;
  531. self._jsPlumb = params["_jsPlumb"];
  532. self.getId = function() { return id; };
  533. self.tooltip = params.tooltip;
  534. self.hoverClass = params.hoverClass || self._jsPlumb.Defaults.HoverClass || jsPlumb.Defaults.HoverClass;
  535. // all components can generate events
  536. jsPlumbUtil.EventGenerator.apply(this);
  537. // all components get this clone function.
  538. // TODO issue 116 showed a problem with this - it seems 'a' that is in
  539. // the clone function's scope is shared by all invocations of it, the classic
  540. // JS closure problem. for now, jsPlumb does a version of this inline where
  541. // it used to call clone. but it would be nice to find some time to look
  542. // further at this.
  543. this.clone = function() {
  544. var o = new Object();
  545. self.constructor.apply(o, a);
  546. return o;
  547. };
  548. this.getParameter = function(name) { return parameters[name]; },
  549. this.getParameters = function() {
  550. return parameters;
  551. },
  552. this.setParameter = function(name, value) { parameters[name] = value; },
  553. this.setParameters = function(p) { parameters = p; },
  554. this.overlayPlacements = [];
  555. // user can supply a beforeDetach callback, which will be executed before a detach
  556. // is performed; returning false prevents the detach.
  557. var beforeDetach = params.beforeDetach;
  558. this.isDetachAllowed = function(connection) {
  559. var r = self._jsPlumb.checkCondition("beforeDetach", connection );
  560. if (beforeDetach) {
  561. try {
  562. r = beforeDetach(connection);
  563. }
  564. catch (e) { _log("jsPlumb: beforeDetach callback failed", e); }
  565. }
  566. return r;
  567. };
  568. // user can supply a beforeDrop callback, which will be executed before a dropped
  569. // connection is confirmed. user can return false to reject connection.
  570. var beforeDrop = params.beforeDrop;
  571. this.isDropAllowed = function(sourceId, targetId, scope, connection, dropEndpoint) {
  572. var r = self._jsPlumb.checkCondition("beforeDrop", {
  573. sourceId:sourceId,
  574. targetId:targetId,
  575. scope:scope,
  576. connection:connection,
  577. dropEndpoint:dropEndpoint
  578. });
  579. if (beforeDrop) {
  580. try {
  581. r = beforeDrop({
  582. sourceId:sourceId,
  583. targetId:targetId,
  584. scope:scope,
  585. connection:connection,
  586. dropEndpoint:dropEndpoint
  587. });
  588. }
  589. catch (e) { _log("jsPlumb: beforeDrop callback failed", e); }
  590. }
  591. return r;
  592. };
  593. // helper method to update the hover style whenever it, or paintStyle, changes.
  594. // we use paintStyle as the foundation and merge hoverPaintStyle over the
  595. // top.
  596. var _updateHoverStyle = function() {
  597. if (paintStyle && hoverPaintStyle) {
  598. var mergedHoverStyle = {};
  599. jsPlumb.extend(mergedHoverStyle, paintStyle);
  600. jsPlumb.extend(mergedHoverStyle, hoverPaintStyle);
  601. delete self["hoverPaintStyle"];
  602. // we want the fillStyle of paintStyle to override a gradient, if possible.
  603. if (mergedHoverStyle.gradient && paintStyle.fillStyle)
  604. delete mergedHoverStyle["gradient"];
  605. hoverPaintStyle = mergedHoverStyle;
  606. }
  607. };
  608. /*
  609. * Sets the paint style and then repaints the element.
  610. *
  611. * Parameters:
  612. * style - Style to use.
  613. */
  614. this.setPaintStyle = function(style, doNotRepaint) {
  615. paintStyle = style;
  616. self.paintStyleInUse = paintStyle;
  617. _updateHoverStyle();
  618. if (!doNotRepaint) self.repaint();
  619. };
  620. /**
  621. * Gets the component's paint style.
  622. *
  623. * Returns:
  624. * the component's paint style. if there is no hoverPaintStyle set then this will be the paint style used all the time, otherwise this is the style used when the mouse is not hovering.
  625. */
  626. this.getPaintStyle = function() {
  627. return paintStyle;
  628. };
  629. /*
  630. * Sets the paint style to use when the mouse is hovering over the element. This is null by default.
  631. * The hover paint style is applied as extensions to the paintStyle; it does not entirely replace
  632. * it. This is because people will most likely want to change just one thing when hovering, say the
  633. * color for example, but leave the rest of the appearance the same.
  634. *
  635. * Parameters:
  636. * style - Style to use when the mouse is hovering.
  637. * doNotRepaint - if true, the component will not be repainted. useful when setting things up initially.
  638. */
  639. this.setHoverPaintStyle = function(style, doNotRepaint) {
  640. hoverPaintStyle = style;
  641. _updateHoverStyle();
  642. if (!doNotRepaint) self.repaint();
  643. };
  644. /**
  645. * Gets the component's hover paint style.
  646. *
  647. * Returns:
  648. * the component's hover paint style. may be null.
  649. */
  650. this.getHoverPaintStyle = function() {
  651. return hoverPaintStyle;
  652. };
  653. /*
  654. * sets/unsets the hover state of this element.
  655. *
  656. * Parameters:
  657. * hover - hover state boolean
  658. * ignoreAttachedElements - if true, does not notify any attached elements of the change in hover state. used mostly to avoid infinite loops.
  659. */
  660. this.setHover = function(hover, ignoreAttachedElements, timestamp) {
  661. // while dragging, we ignore these events. this keeps the UI from flashing and
  662. // swishing and whatevering.
  663. if (!self._jsPlumb.currentlyDragging && !self._jsPlumb.isHoverSuspended()) {
  664. _hover = hover;
  665. if (self.hoverClass != null && self.canvas != null) {
  666. if (hover)
  667. jpcl.addClass(self.canvas, self.hoverClass);
  668. else
  669. jpcl.removeClass(self.canvas, self.hoverClass);
  670. }
  671. if (hoverPaintStyle != null) {
  672. self.paintStyleInUse = hover ? hoverPaintStyle : paintStyle;
  673. timestamp = timestamp || _timestamp();
  674. self.repaint({timestamp:timestamp, recalc:false});
  675. }
  676. // get the list of other affected elements, if supported by this component.
  677. // for a connection, its the endpoints. for an endpoint, its the connections! surprise.
  678. if (self.getAttachedElements && !ignoreAttachedElements)
  679. _updateAttachedElements(hover, _timestamp(), self);
  680. }
  681. };
  682. this.isHover = function() { return _hover; };
  683. var zIndex = null;
  684. this.setZIndex = function(v) { zIndex = v; };
  685. this.getZIndex = function() { return zIndex; };
  686. var jpcl = jsPlumb.CurrentLibrary,
  687. events = [ "click", "dblclick", "mouseenter", "mouseout", "mousemove", "mousedown", "mouseup", "contextmenu" ],
  688. eventFilters = { "mouseout":"mouseexit" },
  689. bindOne = function(o, c, evt) {
  690. var filteredEvent = eventFilters[evt] || evt;
  691. jpcl.bind(o, evt, function(ee) {
  692. c.fire(filteredEvent, c, ee);
  693. });
  694. },
  695. unbindOne = function(o, evt) {
  696. var filteredEvent = eventFilters[evt] || evt;
  697. jpcl.unbind(o, evt);
  698. };
  699. this.attachListeners = function(o, c) {
  700. for (var i = 0; i < events.length; i++) {
  701. bindOne(o, c, events[i]);
  702. }
  703. };
  704. var _updateAttachedElements = function(state, timestamp, sourceElement) {
  705. var affectedElements = self.getAttachedElements(); // implemented in subclasses
  706. if (affectedElements) {
  707. for (var i = 0; i < affectedElements.length; i++) {
  708. if (!sourceElement || sourceElement != affectedElements[i])
  709. affectedElements[i].setHover(state, true, timestamp); // tell the attached elements not to inform their own attached elements.
  710. }
  711. }
  712. };
  713. this.reattachListenersForElement = function(o) {
  714. if (arguments.length > 1) {
  715. for (var i = 0; i < events.length; i++)
  716. unbindOne(o, events[i]);
  717. for (var i = 1; i < arguments.length; i++)
  718. self.attachListeners(o, arguments[i]);
  719. }
  720. };
  721. /*
  722. * TYPES
  723. */
  724. var _types = [],
  725. _splitType = function(t) { return t == null ? null : t.split(" ")},
  726. _applyTypes = function(doNotRepaint) {
  727. if (self.getDefaultType) {
  728. var td = self.getTypeDescriptor();
  729. var o = jsPlumbUtil.merge({}, self.getDefaultType());
  730. for (var i = 0; i < _types.length; i++)
  731. o = jsPlumbUtil.merge(o, self._jsPlumb.getType(_types[i], td));
  732. self.applyType(o);
  733. if (!doNotRepaint) self.repaint();
  734. }
  735. };
  736. self.setType = function(typeId, doNotRepaint) {
  737. _types = _splitType(typeId) || [];
  738. _applyTypes(doNotRepaint);
  739. };
  740. /*
  741. * Function : getType
  742. * Gets the 'types' of this component.
  743. */
  744. self.getType = function() {
  745. return _types;
  746. };
  747. self.hasType = function(typeId) {
  748. return jsPlumbUtil.indexOf(_types, typeId) != -1;
  749. };
  750. self.addType = function(typeId, doNotRepaint) {
  751. var t = _splitType(typeId), _cont = false;
  752. if (t != null) {
  753. for (var i = 0; i < t.length; i++) {
  754. if (!self.hasType(t[i])) {
  755. _types.push(t[i]);
  756. _cont = true;
  757. }
  758. }
  759. if (_cont) _applyTypes(doNotRepaint);
  760. }
  761. };
  762. self.removeType = function(typeId, doNotRepaint) {
  763. var t = _splitType(typeId), _cont = false, _one = function(tt) {
  764. var idx = jsPlumbUtil.indexOf(_types, tt);
  765. if (idx != -1) {
  766. _types.splice(idx, 1);
  767. return true;
  768. }
  769. return false;
  770. };
  771. if (t != null) {
  772. for (var i = 0; i < t.length; i++) {
  773. _cont = _one(t[i]) || _cont;
  774. }
  775. if (_cont) _applyTypes(doNotRepaint);
  776. }
  777. };
  778. self.toggleType = function(typeId, doNotRepaint) {
  779. var t = _splitType(typeId);
  780. if (t != null) {
  781. for (var i = 0; i < t.length; i++) {
  782. var idx = jsPlumbUtil.indexOf(_types, t[i]);
  783. if (idx != -1)
  784. _types.splice(idx, 1);
  785. else
  786. _types.push(t[i]);
  787. }
  788. _applyTypes(doNotRepaint);
  789. }
  790. };
  791. this.applyType = function(t) {
  792. self.setPaintStyle(t.paintStyle);
  793. self.setHoverPaintStyle(t.hoverPaintStyle);
  794. if (t.parameters){
  795. for (var i in t.parameters)
  796. self.setParameter(i, t.parameters[i]);
  797. }
  798. };
  799. },
  800. overlayCapableJsPlumbUIComponent = function(params) {
  801. jsPlumbUIComponent.apply(this, arguments);
  802. var self = this;
  803. this.overlays = [];
  804. var processOverlay = function(o) {
  805. var _newOverlay = null;
  806. if (_isArray(o)) { // this is for the shorthand ["Arrow", { width:50 }] syntax
  807. // there's also a three arg version:
  808. // ["Arrow", { width:50 }, {location:0.7}]
  809. // which merges the 3rd arg into the 2nd.
  810. var type = o[0],
  811. // make a copy of the object so as not to mess up anyone else's reference...
  812. p = jsPlumb.extend({component:self, _jsPlumb:self._jsPlumb}, o[1]);
  813. if (o.length == 3) jsPlumb.extend(p, o[2]);
  814. _newOverlay = new jsPlumb.Overlays[self._jsPlumb.getRenderMode()][type](p);
  815. if (p.events) {
  816. for (var evt in p.events) {
  817. _newOverlay.bind(evt, p.events[evt]);
  818. }
  819. }
  820. } else if (o.constructor == String) {
  821. _newOverlay = new jsPlumb.Overlays[self._jsPlumb.getRenderMode()][o]({component:self, _jsPlumb:self._jsPlumb});
  822. } else {
  823. _newOverlay = o;
  824. }
  825. self.overlays.push(_newOverlay);
  826. },
  827. calculateOverlaysToAdd = function(params) {
  828. var defaultKeys = self.defaultOverlayKeys || [],
  829. o = params.overlays,
  830. checkKey = function(k) {
  831. return self._jsPlumb.Defaults[k] || jsPlumb.Defaults[k] || [];
  832. };
  833. if (!o) o = [];
  834. for (var i = 0; i < defaultKeys.length; i++)
  835. o.unshift.apply(o, checkKey(defaultKeys[i]));
  836. return o;
  837. }
  838. var _overlays = calculateOverlaysToAdd(params);//params.overlays || self._jsPlumb.Defaults.Overlays;
  839. if (_overlays) {
  840. for (var i = 0; i < _overlays.length; i++) {
  841. processOverlay(_overlays[i]);
  842. }
  843. }
  844. // overlay finder helper method
  845. var _getOverlayIndex = function(id) {
  846. var idx = -1;
  847. for (var i = 0; i < self.overlays.length; i++) {
  848. if (id === self.overlays[i].id) {
  849. idx = i;
  850. break;
  851. }
  852. }
  853. return idx;
  854. };
  855. this.addOverlay = function(overlay, doNotRepaint) {
  856. processOverlay(overlay);
  857. if (!doNotRepaint) self.repaint();
  858. };
  859. this.getOverlay = function(id) {
  860. var idx = _getOverlayIndex(id);
  861. return idx >= 0 ? self.overlays[idx] : null;
  862. };
  863. this.getOverlays = function() {
  864. return self.overlays;
  865. };
  866. this.hideOverlay = function(id) {
  867. var o = self.getOverlay(id);
  868. if (o) o.hide();
  869. };
  870. this.hideOverlays = function() {
  871. for (var i = 0; i < self.overlays.length; i++)
  872. self.overlays[i].hide();
  873. };
  874. this.showOverlay = function(id) {
  875. var o = self.getOverlay(id);
  876. if (o) o.show();
  877. };
  878. this.showOverlays = function() {
  879. for (var i = 0; i < self.overlays.length; i++)
  880. self.overlays[i].show();
  881. };
  882. this.removeAllOverlays = function() {
  883. for (var i = 0; i < self.overlays.length; i++) {
  884. if (self.overlays[i].cleanup) self.overlays[i].cleanup();
  885. }
  886. self.overlays.splice(0, self.overlays.length);
  887. self.repaint();
  888. };
  889. this.removeOverlay = function(overlayId) {
  890. var idx = _getOverlayIndex(overlayId);
  891. if (idx != -1) {
  892. var o = self.overlays[idx];
  893. if (o.cleanup) o.cleanup();
  894. self.overlays.splice(idx, 1);
  895. }
  896. };
  897. this.removeOverlays = function() {
  898. for (var i = 0; i < arguments.length; i++)
  899. self.removeOverlay(arguments[i]);
  900. };
  901. // this is a shortcut helper method to let people add a label as
  902. // overlay.
  903. var _internalLabelOverlayId = "__label",
  904. _makeLabelOverlay = function(params) {
  905. var _params = {
  906. cssClass:params.cssClass,
  907. labelStyle : this.labelStyle,
  908. id:_internalLabelOverlayId,
  909. component:self,
  910. _jsPlumb:self._jsPlumb
  911. },
  912. mergedParams = jsPlumb.extend(_params, params);
  913. return new jsPlumb.Overlays[self._jsPlumb.getRenderMode()].Label( mergedParams );
  914. };
  915. if (params.label) {
  916. var loc = params.labelLocation || self.defaultLabelLocation || 0.5,
  917. labelStyle = params.labelStyle || self._jsPlumb.Defaults.LabelStyle || jsPlumb.Defaults.LabelStyle;
  918. this.overlays.push(_makeLabelOverlay({
  919. label:params.label,
  920. location:loc,
  921. labelStyle:labelStyle
  922. }));
  923. }
  924. this.setLabel = function(l) {
  925. var lo = self.getOverlay(_internalLabelOverlayId);
  926. if (!lo) {
  927. var params = l.constructor == String || l.constructor == Function ? { label:l } : l;
  928. lo = _makeLabelOverlay(params);
  929. this.overlays.push(lo);
  930. }
  931. else {
  932. if (l.constructor == String || l.constructor == Function) lo.setLabel(l);
  933. else {
  934. if (l.label) lo.setLabel(l.label);
  935. if (l.location) lo.setLocation(l.location);
  936. }
  937. }
  938. if (!self._jsPlumb.isSuspendDrawing())
  939. self.repaint();
  940. };
  941. this.getLabel = function() {
  942. var lo = self.getOverlay(_internalLabelOverlayId);
  943. return lo != null ? lo.getLabel() : null;
  944. };
  945. this.getLabelOverlay = function() {
  946. return self.getOverlay(_internalLabelOverlayId);
  947. };
  948. var superAt = this.applyType;
  949. this.applyType = function(t) {
  950. superAt(t);
  951. self.removeAllOverlays();
  952. if (t.overlays) {
  953. for (var i = 0; i < t.overlays.length; i++)
  954. self.addOverlay(t.overlays[i], true);
  955. }
  956. };
  957. },
  958. _bindListeners = function(obj, _self, _hoverFunction) {
  959. obj.bind("click", function(ep, e) { _self.fire("click", _self, e); });
  960. obj.bind("dblclick", function(ep, e) { _self.fire("dblclick", _self, e); });
  961. obj.bind("contextmenu", function(ep, e) { _self.fire("contextmenu", _self, e); });
  962. obj.bind("mouseenter", function(ep, e) {
  963. if (!_self.isHover()) {
  964. _hoverFunction(true);
  965. _self.fire("mouseenter", _self, e);
  966. }
  967. });
  968. obj.bind("mouseexit", function(ep, e) {
  969. if (_self.isHover()) {
  970. _hoverFunction(false);
  971. _self.fire("mouseexit", _self, e);
  972. }
  973. });
  974. };
  975. var _jsPlumbInstanceIndex = 0,
  976. getInstanceIndex = function() {
  977. var i = _jsPlumbInstanceIndex + 1;
  978. _jsPlumbInstanceIndex++;
  979. return i;
  980. };
  981. var jsPlumbInstance = function(_defaults) {
  982. /*
  983. * Property: Defaults
  984. *
  985. * These are the default settings for jsPlumb. They are what will be used if you do not supply specific pieces of information
  986. * to the various API calls. A convenient way to implement your own look and feel can be to override these defaults
  987. * by including a script somewhere after the jsPlumb include, but before you make any calls to jsPlumb.
  988. *
  989. * Properties:
  990. * - *Anchor* The default anchor to use for all connections (both source and target). Default is "BottomCenter".
  991. * - *Anchors* The default anchors to use ([source, target]) for all connections. Defaults are ["BottomCenter", "BottomCenter"].
  992. * - *ConnectionsDetachable* Whether or not connections are detachable by default (using the mouse). Defaults to true.
  993. * - *ConnectionOverlays* The default overlay definitions for Connections. Defaults to an empty list.
  994. * - *Connector* The default connector definition to use for all connections. Default is "Bezier".
  995. * - *ConnectorZIndex* Optional value for the z-index of Connections that are not in the hover state. If you set this, jsPlumb will set the z-index of all created Connections to be this value, and the z-index of any Connections in the hover state to be this value plus one. This brings hovered connections up on top of others, which is a nice effect in busy UIs.
  996. * - *Container* Optional selector or element id that instructs jsPlumb to append elements it creates to a specific element.
  997. * - *DragOptions* The default drag options to pass in to connect, makeTarget and addEndpoint calls. Default is empty.
  998. * - *DropOptions* The default drop options to pass in to connect, makeTarget and addEndpoint calls. Default is empty.
  999. * - *Endpoint* The default endpoint definition to use for all connections (both source and target). Default is "Dot".
  1000. * - *EndpointOverlays* The default overlay definitions for Endpoints. Defaults to an empty list.
  1001. * - *Endpoints* The default endpoint definitions ([ source, target ]) to use for all connections. Defaults are ["Dot", "Dot"].
  1002. * - *EndpointStyle* The default style definition to use for all endpoints. Default is fillStyle:"#456".
  1003. * - *EndpointStyles* The default style definitions ([ source, target ]) to use for all endpoints. Defaults are empty.
  1004. * - *EndpointHoverStyle* The default hover style definition to use for all endpoints. Default is null.
  1005. * - *EndpointHoverStyles* The default hover style definitions ([ source, target ]) to use for all endpoints. Defaults are null.
  1006. * - *HoverPaintStyle* The default hover style definition to use for all connections. Defaults are null.
  1007. * - *LabelStyle* The default style to use for label overlays on connections.
  1008. * - *LogEnabled* Whether or not the jsPlumb log is enabled. defaults to false.
  1009. * - *Overlays* The default overlay definitions (for both Connections and Endpoint). Defaults to an empty list.
  1010. * - *MaxConnections* The default maximum number of connections for an Endpoint. Defaults to 1.
  1011. * - *PaintStyle* The default paint style for a connection. Default is line width of 8 pixels, with color "#456".
  1012. * - *ReattachConnections* Whether or not to reattach Connections that a user has detached with the mouse and then dropped. Default is false.
  1013. * - *RenderMode* What mode to use to paint with. If you're on IE<9, you don't really get to choose this. You'll just get VML. Otherwise, the jsPlumb default is to use SVG.
  1014. * - *Scope* The default "scope" to use for connections. Scope lets you assign connections to different categories.
  1015. */
  1016. this.Defaults = {
  1017. Anchor : "BottomCenter",
  1018. Anchors : [ null, null ],
  1019. ConnectionsDetachable : true,
  1020. ConnectionOverlays : [ ],
  1021. Connector : "Bezier",
  1022. ConnectorZIndex : null,
  1023. Container : null,
  1024. DragOptions : { },
  1025. DropOptions : { },
  1026. Endpoint : "Dot",
  1027. EndpointOverlays : [ ],
  1028. Endpoints : [ null, null ],
  1029. EndpointStyle : { fillStyle : "#456" },
  1030. EndpointStyles : [ null, null ],
  1031. EndpointHoverStyle : null,
  1032. EndpointHoverStyles : [ null, null ],
  1033. HoverPaintStyle : null,
  1034. LabelStyle : { color : "black" },
  1035. LogEnabled : false,
  1036. Overlays : [ ],
  1037. MaxConnections : 1,
  1038. PaintStyle : { lineWidth : 8, strokeStyle : "#456" },
  1039. ReattachConnections:false,
  1040. RenderMode : "svg",
  1041. Scope : "jsPlumb_DefaultScope"
  1042. };
  1043. if (_defaults) jsPlumb.extend(this.Defaults, _defaults);
  1044. this.logEnabled = this.Defaults.LogEnabled;
  1045. var _connectionTypes = { }, _endpointTypes = {};
  1046. this.registerConnectionType = function(id, type) {
  1047. _connectionTypes[id] = jsPlumb.extend({}, type);
  1048. };
  1049. this.registerConnectionTypes = function(types) {
  1050. for (var i in types)
  1051. _connectionTypes[i] = jsPlumb.extend({}, types[i]);
  1052. };
  1053. this.registerEndpointType = function(id, type) {
  1054. _endpointTypes[id] = jsPlumb.extend({}, type);
  1055. };
  1056. this.registerEndpointTypes = function(types) {
  1057. for (var i in types)
  1058. _endpointTypes[i] = jsPlumb.extend({}, types[i]);
  1059. };
  1060. this.getType = function(id, typeDescriptor) {
  1061. return typeDescriptor === "connection" ? _connectionTypes[id] : _endpointTypes[id];
  1062. };
  1063. jsPlumbUtil.EventGenerator.apply(this);
  1064. var _currentInstance = this,
  1065. _instanceIndex = getInstanceIndex(),
  1066. _bb = _currentInstance.bind,
  1067. _initialDefaults = {},
  1068. _zoom = 1;
  1069. this.setZoom = function(z, repaintEverything) {
  1070. _zoom = z;
  1071. if (repaintEverything) _currentInstance.repaintEverything();
  1072. };
  1073. this.getZoom = function() { return _zoom; };
  1074. for (var i in this.Defaults)
  1075. _initialDefaults[i] = this.Defaults[i];
  1076. this.bind = function(event, fn) {
  1077. if ("ready" === event && initialized) fn();
  1078. else _bb.apply(_currentInstance,[event, fn]);
  1079. };
  1080. _currentInstance.importDefaults = function(d) {
  1081. for (var i in d) {
  1082. _currentInstance.Defaults[i] = d[i];
  1083. }
  1084. };
  1085. _currentInstance.restoreDefaults = function() {
  1086. _currentInstance.Defaults = jsPlumb.extend({}, _initialDefaults);
  1087. };
  1088. var log = null,
  1089. resizeTimer = null,
  1090. initialized = false,
  1091. connectionsByScope = {},
  1092. /**
  1093. * map of element id -> endpoint lists. an element can have an arbitrary
  1094. * number of endpoints on it, and not all of them have to be connected
  1095. * to anything.
  1096. */
  1097. endpointsByElement = {},
  1098. endpointsByUUID = {},
  1099. offsets = {},
  1100. offsetTimestamps = {},
  1101. floatingConnections = {},
  1102. draggableStates = {},
  1103. canvasList = [],
  1104. sizes = [],
  1105. //listeners = {}, // a map: keys are event types, values are lists of listeners.
  1106. DEFAULT_SCOPE = this.Defaults.Scope,
  1107. renderMode = null, // will be set in init()
  1108. /**
  1109. * helper method to add an item to a list, creating the list if it does
  1110. * not yet exist.
  1111. */
  1112. _addToList = function(map, key, value) {
  1113. var l = map[key];
  1114. if (l == null) {
  1115. l = [];
  1116. map[key] = l;
  1117. }
  1118. l.push(value);
  1119. return l;
  1120. },
  1121. /**
  1122. * appends an element to some other element, which is calculated as follows:
  1123. *
  1124. * 1. if _currentInstance.Defaults.Container exists, use that element.
  1125. * 2. if the 'parent' parameter exists, use that.
  1126. * 3. otherwise just use the root element (for DOM usage, the document body).
  1127. *
  1128. */
  1129. _appendElement = function(el, parent) {
  1130. if (_currentInstance.Defaults.Container)
  1131. jsPlumb.CurrentLibrary.appendElement(el, _currentInstance.Defaults.Container);
  1132. else if (!parent)
  1133. //document.body.appendChild(el);
  1134. jsPlumbAdapter.appendToRoot(el);
  1135. else
  1136. jsPlumb.CurrentLibrary.appendElement(el, parent);
  1137. },
  1138. _curIdStamp = 1,
  1139. _idstamp = function() { return "" + _curIdStamp++; },
  1140. /**
  1141. * YUI, for some reason, put the result of a Y.all call into an object that contains
  1142. * a '_nodes' array, instead of handing back an array-like object like the other
  1143. * libraries do.
  1144. */
  1145. _convertYUICollection = function(c) {
  1146. return c._nodes ? c._nodes : c;
  1147. },
  1148. /**
  1149. * Draws an endpoint and its connections. this is the main entry point into drawing connections as well
  1150. * as endpoints, since jsPlumb is endpoint-centric under the hood.
  1151. *
  1152. * @param element element to draw (of type library specific element object)
  1153. * @param ui UI object from current library's event system. optional.
  1154. * @param timestamp timestamp for this paint cycle. used to speed things up a little by cutting down the amount of offset calculations we do.
  1155. */
  1156. _draw = function(element, ui, timestamp) {
  1157. // TOD is it correct to filter by headless at this top level? how would a headless adapter ever repaint?
  1158. if (!jsPlumbAdapter.headless && !_suspendDrawing) {
  1159. var id = _getAttribute(element, "id"),
  1160. repaintEls = _currentInstance.dragManager.getElementsForDraggable(id);
  1161. if (timestamp == null) timestamp = _timestamp();
  1162. _currentInstance.anchorManager.redraw(id, ui, timestamp);
  1163. if (repaintEls) {
  1164. for (var i in repaintEls) {
  1165. _currentInstance.anchorManager.redraw(repaintEls[i].id, ui, timestamp, repaintEls[i].offset);
  1166. }
  1167. }
  1168. }
  1169. },
  1170. /**
  1171. * executes the given function against the given element if the first
  1172. * argument is an object, or the list of elements, if the first argument
  1173. * is a list. the function passed in takes (element, elementId) as
  1174. * arguments.
  1175. */
  1176. _elementProxy = function(element, fn) {
  1177. var retVal = null;
  1178. if (_isArray(element)) {
  1179. retVal = [];
  1180. for ( var i = 0; i < element.length; i++) {
  1181. var el = _getElementObject(element[i]), id = _getAttribute(el, "id");
  1182. retVal.push(fn(el, id)); // append return values to what we will return
  1183. }
  1184. } else {
  1185. var el = _getElementObject(element), id = _getAttribute(el, "id");
  1186. retVal = fn(el, id);
  1187. }
  1188. return retVal;
  1189. },
  1190. /**
  1191. * gets an Endpoint by uuid.
  1192. */
  1193. _getEndpoint = function(uuid) { return endpointsByUUID[uuid]; },
  1194. /**
  1195. * inits a draggable if it's not already initialised.
  1196. */
  1197. _initDraggableIfNecessary = function(element, isDraggable, dragOptions) {
  1198. // TODO move to DragManager?
  1199. if (!jsPlumbAdapter.headless) {
  1200. var draggable = isDraggable == null ? false : isDraggable,
  1201. jpcl = jsPlumb.CurrentLibrary;
  1202. if (draggable) {
  1203. if (jpcl.isDragSupported(element) && !jpcl.isAlreadyDraggable(element)) {
  1204. var options = dragOptions || _currentInstance.Defaults.DragOptions || jsPlumb.Defaults.DragOptions;
  1205. options = jsPlumb.extend( {}, options); // make a copy.
  1206. var dragEvent = jpcl.dragEvents["drag"],
  1207. stopEvent = jpcl.dragEvents["stop"],
  1208. startEvent = jpcl.dragEvents["start"];
  1209. options[startEvent] = _wrap(options[startEvent], function() {
  1210. _currentInstance.setHoverSuspended(true);
  1211. });
  1212. options[dragEvent] = _wrap(options[dragEvent], function() {
  1213. var ui = jpcl.getUIPosition(arguments, _currentInstance.getZoom());
  1214. _draw(element, ui);
  1215. _addClass(element, "jsPlumb_dragged");
  1216. });
  1217. options[stopEvent] = _wrap(options[stopEvent], function() {
  1218. var ui = jpcl.getUIPosition(arguments, _currentInstance.getZoom());
  1219. _draw(element, ui);
  1220. _removeClass(element, "jsPlumb_dragged");
  1221. _currentInstance.setHoverSuspended(false);
  1222. });
  1223. var elId = _getId(element); // need ID
  1224. draggableStates[elId] = true;
  1225. var draggable = draggableStates[elId];
  1226. options.disabled = draggable == null ? false : !draggable;
  1227. jpcl.initDraggable(element, options, false);
  1228. _currentInstance.dragManager.register(element);
  1229. }
  1230. }
  1231. }
  1232. },
  1233. /*
  1234. * prepares a final params object that can be passed to _newConnection, taking into account defaults, events, etc.
  1235. */
  1236. _prepareConnectionParams = function(params, referenceParams) {
  1237. var _p = jsPlumb.extend( {
  1238. sourceIsNew:true,
  1239. targetIsNew:true
  1240. }, params);
  1241. if (referenceParams) jsPlumb.extend(_p, referenceParams);
  1242. // hotwire endpoints passed as source or target to sourceEndpoint/targetEndpoint, respectively.
  1243. if (_p.source && _p.source.endpoint) _p.sourceEndpoint = _p.source;
  1244. if (_p.source && _p.target.endpoint) _p.targetEndpoint = _p.target;
  1245. // test for endpoint uuids to connect
  1246. if (params.uuids) {
  1247. _p.sourceEndpoint = _getEndpoint(params.uuids[0]);
  1248. _p.targetEndpoint = _getEndpoint(params.uuids[1]);
  1249. }
  1250. // now ensure that if we do have Endpoints already, they're not full.
  1251. // source:
  1252. if (_p.sourceEndpoint && _p.sourceEndpoint.isFull()) {
  1253. _log(_currentInstance, "could not add connection; source endpoint is full");
  1254. return;
  1255. }
  1256. // target:
  1257. if (_p.targetEndpoint && _p.targetEndpoint.isFull()) {
  1258. _log(_currentInstance, "could not add connection; target endpoint is full");
  1259. return;
  1260. }
  1261. // at this point, if we have source or target Endpoints, they were not new and we should mark the
  1262. // flag to reflect that. this is for use later with the deleteEndpointsOnDetach flag.
  1263. if (_p.sourceEndpoint) _p.sourceIsNew = false;
  1264. if (_p.targetEndpoint) _p.targetIsNew = false;
  1265. // if source endpoint mandates connection type and nothing specified in our params, use it.
  1266. if (!_p.type && _p.sourceEndpoint)
  1267. _p.type = _p.sourceEndpoint.connectionType;
  1268. // copy in any connectorOverlays that were specified on the source endpoint.
  1269. // it doesnt copy target endpoint overlays. i'm not sure if we want it to or not.
  1270. if (_p.sourceEndpoint && _p.sourceEndpoint.connectorOverlays) {
  1271. _p.overlays = _p.overlays || [];
  1272. for (var i = 0; i < _p.sourceEndpoint.connectorOverlays.length; i++) {
  1273. _p.overlays.push(_p.sourceEndpoint.connectorOverlays[i]);
  1274. }
  1275. }
  1276. // tooltip. params.tooltip takes precedence, then sourceEndpoint.connectorTooltip.
  1277. _p.tooltip = params.tooltip;
  1278. if (!_p.tooltip && _p.sourceEndpoint && _p.sourceEndpoint.connectorTooltip)
  1279. _p.tooltip = _p.sourceEndpoint.connectorTooltip;
  1280. // if there's a target specified (which of course there should be), and there is no
  1281. // target endpoint specified, and 'newConnection' was not set to true, then we check to
  1282. // see if a prior call to makeTarget has provided us with the specs for the target endpoint, and
  1283. // we use those if so. additionally, if the makeTarget call was specified with 'uniqueEndpoint' set
  1284. // to true, then if that target endpoint has already been created, we re-use it.
  1285. if (_p.target && !_p.target.endpoint && !_p.targetEndpoint && !_p.newConnection) {
  1286. var tid = _getId(_p.target),
  1287. tep =_targetEndpointDefinitions[tid],
  1288. existingUniqueEndpoint = _targetEndpoints[tid];
  1289. if (tep) {
  1290. // if target not enabled, return.
  1291. if (!_targetsEnabled[tid]) return;
  1292. // check for max connections??
  1293. var newEndpoint = existingUniqueEndpoint != null ? existingUniqueEndpoint : _currentInstance.addEndpoint(_p.target, tep);
  1294. if (_targetEndpointsUnique[tid]) _targetEndpoints[tid] = newEndpoint;
  1295. _p.targetEndpoint = newEndpoint;
  1296. newEndpoint._makeTargetCreator = true;
  1297. _p.targetIsNew = true;
  1298. }
  1299. }
  1300. // same thing, but for source.
  1301. if (_p.source && !_p.source.endpoint && !_p.sourceEndpoint && !_p.newConnection) {
  1302. var tid = _getId(_p.source),
  1303. tep = _sourceEndpointDefinitions[tid],
  1304. existingUniqueEndpoint = _sourceEndpoints[tid];
  1305. if (tep) {
  1306. // if source not enabled, return.
  1307. if (!_sourcesEnabled[tid]) return;
  1308. var newEndpoint = existingUniqueEndpoint != null ? existingUniqueEndpoint : _currentInstance.addEndpoint(_p.source, tep);
  1309. if (_sourceEndpointsUnique[tid]) _sourceEndpoints[tid] = newEndpoint;
  1310. _p.sourceEndpoint = newEndpoint;
  1311. _p.sourceIsNew = true;
  1312. }
  1313. }
  1314. return _p;
  1315. },
  1316. _newConnection = function(params) {
  1317. var connectionFunc = _currentInstance.Defaults.ConnectionType || _currentInstance.getDefaultConnectionType(),
  1318. endpointFunc = _currentInstance.Defaults.EndpointType || Endpoint,
  1319. parent = jsPlumb.CurrentLibrary.getParent;
  1320. if (params.container)
  1321. params["parent"] = params.container;
  1322. else {
  1323. if (params.sourceEndpoint)
  1324. params["parent"] = params.sourceEndpoint.parent;
  1325. else if (params.source.constructor == endpointFunc)
  1326. params["parent"] = params.source.parent;
  1327. else params["parent"] = parent(params.source);
  1328. }
  1329. params["_jsPlumb"] = _currentInstance;
  1330. var con = new connectionFunc(params);
  1331. con.id = "con_" + _idstamp();
  1332. _eventFireProxy("click", "click", con);
  1333. _eventFireProxy("dblclick", "dblclick", con);
  1334. _eventFireProxy("contextmenu", "contextmenu", con);
  1335. return con;
  1336. },
  1337. /**
  1338. * adds the connection to the backing model, fires an event if necessary and then redraws
  1339. */
  1340. _finaliseConnection = function(jpc, params, originalEvent) {
  1341. params = params || {};
  1342. // add to list of connections (by scope).
  1343. if (!jpc.suspendedEndpoint)
  1344. _addToList(connectionsByScope, jpc.scope, jpc);
  1345. // fire an event
  1346. if (!params.doNotFireConnectionEvent && params.fireEvent !== false) {
  1347. var eventArgs = {
  1348. connection:jpc,
  1349. source : jpc.source, target : jpc.target,
  1350. sourceId : jpc.sourceId, targetId : jpc.targetId,
  1351. sourceEndpoint : jpc.endpoints[0], targetEndpoint : jpc.endpoints[1]
  1352. };
  1353. _currentInstance.fire("jsPlumbConnection", eventArgs, originalEvent);
  1354. // this is from 1.3.11 onwards. "jsPlumbConnection" always felt so unnecessary, so
  1355. // I've added this alias in 1.3.11, with a view to removing "jsPlumbConnection" completely in a future version. be aware, of course, you should only register listeners for one or the other of these events.
  1356. _currentInstance.fire("connection", eventArgs, originalEvent);
  1357. }
  1358. // always inform the anchor manager
  1359. // except that if jpc has a suspended endpoint it's not true to say the
  1360. // connection is new; it has just (possibly) moved. the question is whether
  1361. // to make that call here or in the anchor manager. i think perhaps here.
  1362. _currentInstance.anchorManager.newConnection(jpc);
  1363. // force a paint
  1364. _draw(jpc.source);
  1365. },
  1366. _eventFireProxy = function(event, proxyEvent, obj) {
  1367. obj.bind(event, function(originalObject, originalEvent) {
  1368. _currentInstance.fire(proxyEvent, obj, originalEvent);
  1369. });
  1370. },
  1371. /**
  1372. * for the given endpoint params, returns an appropriate parent element for the UI elements that will be added.
  1373. * this function is used by _newEndpoint (directly below), and also in the makeSource function in jsPlumb.
  1374. *
  1375. * the logic is to first look for a "container" member of params, and pass that back if found. otherwise we
  1376. * handoff to the 'getParent' function in the current library.
  1377. */
  1378. _getParentFromParams = function(params) {
  1379. if (params.container)
  1380. return params.container;
  1381. else {
  1382. var tag = jsPlumb.CurrentLibrary.getTagName(params.source),
  1383. p = jsPlumb.CurrentLibrary.getParent(params.source);
  1384. if (tag && tag.toLowerCase() === "td")
  1385. return jsPlumb.CurrentLibrary.getParent(p);
  1386. else return p;
  1387. }
  1388. },
  1389. /**
  1390. factory method to prepare a new endpoint. this should always be used instead of creating Endpoints
  1391. manually, since this method attaches event listeners and an id.
  1392. */
  1393. _newEndpoint = function(params) {
  1394. var endpointFunc = _currentInstance.Defaults.EndpointType || Endpoint;
  1395. params.parent = _getParentFromParams(params);
  1396. params["_jsPlumb"] = _currentInstance;
  1397. var ep = new endpointFunc(params);
  1398. ep.id = "ep_" + _idstamp();
  1399. _eventFireProxy("click", "endpointClick", ep);
  1400. _eventFireProxy("dblclick", "endpointDblClick", ep);
  1401. _eventFireProxy("contextmenu", "contextmenu", ep);
  1402. if (!jsPlumbAdapter.headless)
  1403. _currentInstance.dragManager.endpointAdded(params.source);
  1404. return ep;
  1405. },
  1406. /**
  1407. * performs the given function operation on all the connections found
  1408. * for the given element id; this means we find all the endpoints for
  1409. * the given element, and then for each endpoint find the connectors
  1410. * connected to it. then we pass each connection in to the given
  1411. * function.
  1412. */
  1413. _operation = function(elId, func, endpointFunc) {
  1414. var endpoints = endpointsByElement[elId];
  1415. if (endpoints && endpoints.length) {
  1416. for ( var i = 0; i < endpoints.length; i++) {
  1417. for ( var j = 0; j < endpoints[i].connections.length; j++) {
  1418. var retVal = func(endpoints[i].connections[j]);
  1419. // if the function passed in returns true, we exit.
  1420. // most functions return false.
  1421. if (retVal) return;
  1422. }
  1423. if (endpointFunc) endpointFunc(endpoints[i]);
  1424. }
  1425. }
  1426. },
  1427. /**
  1428. * perform an operation on all elements.
  1429. */
  1430. _operationOnAll = function(func) {
  1431. for ( var elId in endpointsByElement) {
  1432. _operation(elId, func);
  1433. }
  1434. },
  1435. /**
  1436. * helper to remove an element from the DOM.
  1437. */
  1438. _removeElement = function(element, parent) {
  1439. if (element != null && element.parentNode != null) {
  1440. element.parentNode.removeChild(element);
  1441. }
  1442. },
  1443. /**
  1444. * helper to remove a list of elements from the DOM.
  1445. */
  1446. _removeElements = function(elements, parent) {
  1447. for ( var i = 0; i < elements.length; i++)
  1448. _removeElement(elements[i], parent);
  1449. },
  1450. /**
  1451. * Sets whether or not the given element(s) should be draggable,
  1452. * regardless of what a particular plumb command may request.
  1453. *
  1454. * @param element
  1455. * May be a string, a element objects, or a list of
  1456. * strings/elements.
  1457. * @param draggable
  1458. * Whether or not the given element(s) should be draggable.
  1459. */
  1460. _setDraggable = function(element, draggable) {
  1461. return _elementProxy(element, function(el, id) {
  1462. draggableStates[id] = draggable;
  1463. if (jsPlumb.CurrentLibrary.isDragSupported(el)) {
  1464. jsPlumb.CurrentLibrary.setDraggable(el, draggable);
  1465. }
  1466. });
  1467. },
  1468. /**
  1469. * private method to do the business of hiding/showing.
  1470. *
  1471. * @param el
  1472. * either Id of the element in question or a library specific
  1473. * object for the element.
  1474. * @param state
  1475. * String specifying a value for the css 'display' property
  1476. * ('block' or 'none').
  1477. */
  1478. _setVisible = function(el, state, alsoChangeEndpoints) {
  1479. state = state === "block";
  1480. var endpointFunc = null;
  1481. if (alsoChangeEndpoints) {
  1482. if (state) endpointFunc = function(ep) {
  1483. ep.setVisible(true, true, true);
  1484. };
  1485. else endpointFunc = function(ep) {
  1486. ep.setVisible(false, true, true);
  1487. };
  1488. }
  1489. var id = _getAttribute(el, "id");
  1490. _operation(id, function(jpc) {
  1491. if (state && alsoChangeEndpoints) {
  1492. // this test is necessary because this functionality is new, and i wanted to maintain backwards compatibility.
  1493. // this block will only set a connection to be visible if the other endpoint in the connection is also visible.
  1494. var oidx = jpc.sourceId === id ? 1 : 0;
  1495. if (jpc.endpoints[oidx].isVisible()) jpc.setVisible(true);
  1496. }
  1497. else // the default behaviour for show, and what always happens for hide, is to just set the visibility without getting clever.
  1498. jpc.setVisible(state);
  1499. }, endpointFunc);
  1500. },
  1501. /**
  1502. * toggles the draggable state of the given element(s).
  1503. *
  1504. * @param el
  1505. * either an id, or an element object, or a list of
  1506. * ids/element objects.
  1507. */
  1508. _toggleDraggable = function(el) {
  1509. return _elementProxy(el, function(el, elId) {
  1510. var state = draggableStates[elId] == null ? false : draggableStates[elId];
  1511. state = !state;
  1512. draggableStates[elId] = state;
  1513. jsPlumb.CurrentLibrary.setDraggable(el, state);
  1514. return state;
  1515. });
  1516. },
  1517. /**
  1518. * private method to do the business of toggling hiding/showing.
  1519. *
  1520. * @param elId
  1521. * Id of the element in question
  1522. */
  1523. _toggleVisible = function(elId, changeEndpoints) {
  1524. var endpointFunc = null;
  1525. if (changeEndpoints) {
  1526. endpointFunc = function(ep) {
  1527. var state = ep.isVisible();
  1528. ep.setVisible(!state);
  1529. };
  1530. }
  1531. _operation(elId, function(jpc) {
  1532. var state = jpc.isVisible();
  1533. jpc.setVisible(!state);
  1534. }, endpointFunc);
  1535. // todo this should call _elementProxy, and pass in the
  1536. // _operation(elId, f) call as a function. cos _toggleDraggable does
  1537. // that.
  1538. },
  1539. /**
  1540. * updates the offset and size for a given element, and stores the
  1541. * values. if 'offset' is not null we use that (it would have been
  1542. * passed in from a drag call) because it's faster; but if it is null,
  1543. * or if 'recalc' is true in order to force a recalculation, we get the current values.
  1544. */
  1545. _updateOffset = function(params) {
  1546. var timestamp = params.timestamp, recalc = params.recalc, offset = params.offset, elId = params.elId;
  1547. if (_suspendDrawing && !timestamp) timestamp = _suspendedAt;
  1548. if (!recalc) {
  1549. if (timestamp && timestamp === offsetTimestamps[elId])
  1550. return offsets[elId];
  1551. }
  1552. if (recalc || !offset) { // if forced repaint or no offset available, we recalculate.
  1553. // get the current size and offset, and store them
  1554. var s = _getElementObject(elId);
  1555. if (s != null) {
  1556. sizes[elId] = _getSize(s);
  1557. offsets[elId] = _getOffset(s, _currentInstance);
  1558. offsetTimestamps[elId] = timestamp;
  1559. }
  1560. } else {
  1561. offsets[elId] = offset;
  1562. if (sizes[elId] == null) {
  1563. var s = _getElementObject(elId);
  1564. if (s != null) sizes[elId] = _getSize(s);
  1565. }
  1566. }
  1567. if(offsets[elId] && !offsets[elId].right) {
  1568. offsets[elId].right = offsets[elId].left + sizes[elId][0];
  1569. offsets[elId].bottom = offsets[elId].top + sizes[elId][1];
  1570. offsets[elId].width = sizes[elId][0];
  1571. offsets[elId].height = sizes[elId][1];
  1572. offsets[elId].centerx = offsets[elId].left + (offsets[elId].width / 2);
  1573. offsets[elId].centery = offsets[elId].top + (offsets[elId].height / 2);
  1574. }
  1575. return offsets[elId];
  1576. },
  1577. // TODO comparison performance
  1578. _getCachedData = function(elId) {
  1579. var o = offsets[elId];
  1580. if (!o) o = _updateOffset({elId:elId});
  1581. return {o:o, s:sizes[elId]};
  1582. },
  1583. /**
  1584. * gets an id for the given element, creating and setting one if
  1585. * necessary. the id is of the form
  1586. *
  1587. * jsPlumb_<instance index>_<index in instance>
  1588. *
  1589. * where "index in instance" is a monotonically increasing integer that starts at 0,
  1590. * for each instance. this method is used not only to assign ids to elements that do not
  1591. * have them but also to connections and endpoints.
  1592. */
  1593. _getId = function(element, uuid, doNotCreateIfNotFound) {
  1594. var ele = _getElementObject(element);
  1595. var id = _getAttribute(ele, "id");
  1596. if (!id || id == "undefined") {
  1597. // check if fixed uuid parameter is given
  1598. if (arguments.length == 2 && arguments[1] != undefined)
  1599. id = uuid;
  1600. else if (arguments.length == 1 || (arguments.length == 3 && !arguments[2]))
  1601. id = "jsPlumb_" + _instanceIndex + "_" + _idstamp();
  1602. if (!doNotCreateIfNotFound) _setAttribute(ele, "id", id);
  1603. }
  1604. return id;
  1605. },
  1606. /**
  1607. * wraps one function with another, creating a placeholder for the
  1608. * wrapped function if it was null. this is used to wrap the various
  1609. * drag/drop event functions - to allow jsPlumb to be notified of
  1610. * important lifecycle events without imposing itself on the user's
  1611. * drag/drop functionality. TODO: determine whether or not we should
  1612. * support an error handler concept, if one of the functions fails.
  1613. *
  1614. * @param wrappedFunction original function to wrap; may be null.
  1615. * @param newFunction function to wrap the original with.
  1616. * @param returnOnThisValue Optional. Indicates that the wrappedFunction should
  1617. * not be executed if the newFunction returns a value matching 'returnOnThisValue'.
  1618. * note that this is a simple comparison and only works for primitives right now.
  1619. */
  1620. _wrap = function(wrappedFunction, newFunction, returnOnThisValue) {
  1621. wrappedFunction = wrappedFunction || function() { };
  1622. newFunction = newFunction || function() { };
  1623. return function() {
  1624. var r = null;
  1625. try {
  1626. r = newFunction.apply(this, arguments);
  1627. } catch (e) {
  1628. _log(_currentInstance, "jsPlumb function failed : " + e);
  1629. }
  1630. if (returnOnThisValue == null || (r !== returnOnThisValue)) {
  1631. try {
  1632. wrappedFunction.apply(this, arguments);
  1633. } catch (e) {
  1634. _log(_currentInstance, "wrapped function failed : " + e);
  1635. }
  1636. }
  1637. return r;
  1638. };
  1639. };
  1640. /*
  1641. * Property: connectorClass
  1642. * The CSS class to set on Connection elements. This value is a String and can have multiple classes; the entire String is appended as-is.
  1643. */
  1644. this.connectorClass = "_jsPlumb_connector";
  1645. /*
  1646. * Property: endpointClass
  1647. * The CSS class to set on Endpoint elements. This value is a String and can have multiple classes; the entire String is appended as-is.
  1648. */
  1649. this.endpointClass = "_jsPlumb_endpoint";
  1650. /*
  1651. * Property: overlayClass
  1652. * The CSS class to set on an Overlay that is an HTML element. This value is a String and can have multiple classes; the entire String is appended as-is.
  1653. */
  1654. this.overlayClass = "_jsPlumb_overlay";
  1655. this.Anchors = {};
  1656. this.Connectors = {
  1657. "canvas":{},
  1658. "svg":{},
  1659. "vml":{}
  1660. };
  1661. this.Endpoints = {
  1662. "canvas":{},
  1663. "svg":{},
  1664. "vml":{}
  1665. };
  1666. this.Overlays = {
  1667. "canvas":{},
  1668. "svg":{},
  1669. "vml":{}
  1670. };
  1671. // ************************ PLACEHOLDER DOC ENTRIES FOR NATURAL DOCS *****************************************
  1672. /*
  1673. * Function: importDefaults
  1674. * Imports all the given defaults into this instance of jsPlumb.
  1675. */
  1676. /*
  1677. * Function: restoreDefaults
  1678. * Restores the default settings to "factory" values.
  1679. */
  1680. /*
  1681. * Function: bind
  1682. * Bind to an event on jsPlumb.
  1683. *
  1684. * Parameters:
  1685. * event - the event to bind. Available events on jsPlumb are:
  1686. * - *jsPlumbConnection* : notification that a new Connection was established. jsPlumb passes the new Connection to the callback.
  1687. * - *jsPlumbConnectionDetached* : notification that a Connection was detached. jsPlumb passes the detached Connection to the callback.
  1688. * - *click* : notification that a Connection was clicked. jsPlumb passes the Connection that was clicked to the callback.
  1689. * - *dblclick* : notification that a Connection was double clicked. jsPlumb passes the Connection that was double clicked to the callback.
  1690. * - *endpointClick* : notification that an Endpoint was clicked. jsPlumb passes the Endpoint that was clicked to the callback.
  1691. * - *endpointDblClick* : notification that an Endpoint was double clicked. jsPlumb passes the Endpoint that was double clicked to the callback.
  1692. * - *beforeDrop* : notification that a Connection is about to be dropped. Returning false from this method cancels the drop. jsPlumb passes { sourceId, targetId, scope, connection, dropEndpoint } to your callback. For more information, refer to the jsPlumb documentation.
  1693. * - *beforeDetach* : notification that a Connection is about to be detached. Returning false from this method cancels the detach. jsPlumb passes the Connection to your callback. For more information, refer to the jsPlumb documentation.
  1694. * - *connectionDrag* : notification that an existing Connection is being dragged. jsPlumb passes the Connection to your callback function.
  1695. * - *connectionDragEnd* : notification that the drag of an existing Connection has ended. jsPlumb passes the Connection to your callback function.
  1696. *
  1697. * callback - function to callback. This function will be passed the Connection/Endpoint that caused the event, and also the original event.
  1698. */
  1699. /*
  1700. * Function: unbind
  1701. * Clears either all listeners, or listeners for some specific event.
  1702. *
  1703. * Parameters:
  1704. * event - optional. constrains the clear to just listeners for this event.
  1705. */
  1706. /*
  1707. * Function: addClass
  1708. * Add class(es) to some element(s).
  1709. *
  1710. * Parameters:
  1711. * el - element id, dom element, or selector representing the element(s) to add class(es) to.
  1712. * clazz - string representing the class(es) to add. may contain multiple classes separated by spaces.
  1713. */
  1714. /*
  1715. * Function: removeClass
  1716. * Remove class from some selement(s).
  1717. *
  1718. * Parameters:
  1719. * el - element id, dom element, or selector representing the element(s) to remove a class from.
  1720. * clazz - string representing the class to remove.
  1721. */
  1722. /*
  1723. * Function: hasClass
  1724. * Checks if an element has some class.
  1725. *
  1726. * Parameters:
  1727. * el - element id, dom element, or selector representing the element to test. If the selector matches multiple elements, we return the test for the first element in the selector only.
  1728. * clazz - string representing the class to test for.
  1729. */
  1730. // *************** END OF PLACEHOLDER DOC ENTRIES FOR NATURAL DOCS ***********************************************************
  1731. // --------------------------- jsPLumbInstance public API ---------------------------------------------------------
  1732. this.addClass = function(el, clazz) { return jsPlumb.CurrentLibrary.addClass(el, clazz); };
  1733. this.removeClass = function(el, clazz) { return jsPlumb.CurrentLibrary.removeClass(el, clazz); };
  1734. this.hasClass = function(el, clazz) { return jsPlumb.CurrentLibrary.hasClass(el, clazz); };
  1735. /*
  1736. Function: addEndpoint
  1737. Adds an <Endpoint> to a given element or elements.
  1738. Parameters:
  1739. el - Element to add the endpoint to. Either an element id, a selector representing some element(s), or an array of either of these.
  1740. params - Object containing Endpoint constructor arguments. For more information, see <Endpoint>.
  1741. referenceParams - Object containing more Endpoint constructor arguments; it will be merged with params by jsPlumb. You would use this if you had some
  1742. shared parameters that you wanted to reuse when you added Endpoints to a number of elements. The allowed values in
  1743. this object are anything that 'params' can contain. See <Endpoint>.
  1744. Returns:
  1745. The newly created <Endpoint>, if el referred to a single element. Otherwise, an array of newly created <Endpoint>s.
  1746. See Also:
  1747. <addEndpoints>
  1748. */
  1749. this.addEndpoint = function(el, params, referenceParams) {
  1750. referenceParams = referenceParams || {};
  1751. var p = jsPlumb.extend({}, referenceParams);
  1752. jsPlumb.extend(p, params);
  1753. p.endpoint = p.endpoint || _currentInstance.Defaults.Endpoint || jsPlumb.Defaults.Endpoint;
  1754. p.paintStyle = p.paintStyle || _currentInstance.Defaults.EndpointStyle || jsPlumb.Defaults.EndpointStyle;
  1755. // YUI wrapper
  1756. el = _convertYUICollection(el);
  1757. var results = [], inputs = el.length && el.constructor != String ? el : [ el ];
  1758. for (var i = 0; i < inputs.length; i++) {
  1759. var _el = _getElementObject(inputs[i]), id = _getId(_el);
  1760. p.source = _el;
  1761. _updateOffset({ elId : id, timestamp:_suspendedAt });
  1762. var e = _newEndpoint(p);
  1763. if (p.parentAnchor) e.parentAnchor = p.parentAnchor;
  1764. _addToList(endpointsByElement, id, e);
  1765. var myOffset = offsets[id], myWH = sizes[id];
  1766. var anchorLoc = e.anchor.compute( { xy : [ myOffset.left, myOffset.top ], wh : myWH, element : e, timestamp:_suspendedAt });
  1767. var endpointPaintParams = { anchorLoc : anchorLoc, timestamp:_suspendedAt };
  1768. if (_suspendDrawing) endpointPaintParams.recalc = false;
  1769. e.paint(endpointPaintParams);
  1770. results.push(e);
  1771. //if (!jsPlumbAdapter.headless)
  1772. //_currentInstance.dragManager.endpointAdded(_el);
  1773. }
  1774. return results.length == 1 ? results[0] : results;
  1775. };
  1776. /*
  1777. Function: addEndpoints
  1778. Adds a list of <Endpoint>s to a given element or elements.
  1779. Parameters:
  1780. target - element to add the Endpoint to. Either an element id, a selector representing some element(s), or an array of either of these.
  1781. endpoints - List of objects containing Endpoint constructor arguments. one Endpoint is created for each entry in this list. See <Endpoint>'s constructor documentation.
  1782. referenceParams - Object containing more Endpoint constructor arguments; it will be merged with params by jsPlumb. You would use this if you had some shared parameters that you wanted to reuse when you added Endpoints to a number of elements.
  1783. Returns:
  1784. List of newly created <Endpoint>s, one for each entry in the 'endpoints' argument.
  1785. See Also:
  1786. <addEndpoint>
  1787. */
  1788. this.addEndpoints = function(el, endpoints, referenceParams) {
  1789. var results = [];
  1790. for ( var i = 0; i < endpoints.length; i++) {
  1791. var e = _currentInstance.addEndpoint(el, endpoints[i], referenceParams);
  1792. if (_isArray(e))
  1793. Array.prototype.push.apply(results, e);
  1794. else results.push(e);
  1795. }
  1796. return results;
  1797. };
  1798. /*
  1799. Function: animate
  1800. This is a wrapper around the supporting library's animate function; it injects a call to jsPlumb in the 'step' function (creating
  1801. the 'step' function if necessary). This only supports the two-arg version of the animate call in jQuery, the one that takes an 'options' object as
  1802. the second arg. MooTools has only one method, a two arg one. Which is handy. YUI has a one-arg method, so jsPlumb merges 'properties' and 'options' together for YUI.
  1803. Parameters:
  1804. el - Element to animate. Either an id, or a selector representing the element.
  1805. properties - The 'properties' argument you want passed to the library's animate call.
  1806. options - The 'options' argument you want passed to the library's animate call.
  1807. Returns:
  1808. void
  1809. */
  1810. this.animate = function(el, properties, options) {
  1811. var ele = _getElementObject(el), id = _getAttribute(el, "id");
  1812. options = options || {};
  1813. var stepFunction = jsPlumb.CurrentLibrary.dragEvents['step'];
  1814. var completeFunction = jsPlumb.CurrentLibrary.dragEvents['complete'];
  1815. options[stepFunction] = _wrap(options[stepFunction], function() {
  1816. _currentInstance.repaint(id);
  1817. });
  1818. // onComplete repaints, just to make sure everything looks good at the end of the animation.
  1819. options[completeFunction] = _wrap(options[completeFunction],
  1820. function() {
  1821. _currentInstance.repaint(id);
  1822. });
  1823. jsPlumb.CurrentLibrary.animate(ele, properties, options);
  1824. };
  1825. /**
  1826. * checks for a listener for the given condition, executing it if found, passing in the given value.
  1827. * condition listeners would have been attached using "bind" (which is, you could argue, now overloaded, since
  1828. * firing click events etc is a bit different to what this does). i thought about adding a "bindCondition"
  1829. * or something, but decided against it, for the sake of simplicity. jsPlumb will never fire one of these
  1830. * condition events anyway.
  1831. */
  1832. this.checkCondition = function(conditionName, value) {
  1833. var l = _currentInstance.getListener(conditionName),
  1834. r = true;
  1835. if (l && l.length > 0) {
  1836. try {
  1837. for (var i = 0 ; i < l.length; i++) {
  1838. r = r && l[i](value);
  1839. }
  1840. }
  1841. catch (e) {
  1842. _log(_currentInstance, "cannot check condition [" + conditionName + "]" + e);
  1843. }
  1844. }
  1845. return r;
  1846. };
  1847. /**
  1848. * checks a condition asynchronously: fires the event handler and passes the handler
  1849. * a 'proceed' function and a 'stop' function. The handler MUST execute one or other
  1850. * of these once it has made up its mind.
  1851. *
  1852. * Note that although this reads the listener list for the given condition, it
  1853. * does not loop through and hit each listener, because that, with asynchronous
  1854. * callbacks, would be messy. so it uses only the first listener registered.
  1855. */
  1856. this.checkASyncCondition = function(conditionName, value, proceed, stop) {
  1857. var l = _currentInstance.getListener(conditionName);
  1858. if (l && l.length > 0) {
  1859. try {
  1860. l[0](value, proceed, stop);
  1861. }
  1862. catch (e) {
  1863. _log(_currentInstance, "cannot asynchronously check condition [" + conditionName + "]" + e);
  1864. }
  1865. }
  1866. };
  1867. /*
  1868. Function: connect
  1869. Establishes a <Connection> between two elements (or <Endpoint>s, which are themselves registered to elements).
  1870. Parameters:
  1871. params - Object containing constructor arguments for the Connection. See <Connection>'s constructor documentation.
  1872. referenceParams - Optional object containing more constructor arguments for the Connection. Typically you would pass in data that a lot of
  1873. Connections are sharing here, such as connector style etc, and then use the main params for data specific to this Connection.
  1874. Returns:
  1875. The newly created <Connection>.
  1876. */
  1877. this.connect = function(params, referenceParams) {
  1878. // prepare a final set of parameters to create connection with
  1879. var _p = _prepareConnectionParams(params, referenceParams), jpc;
  1880. // TODO probably a nicer return value if the connection was not made. _prepareConnectionParams
  1881. // will return null (and log something) if either endpoint was full. what would be nicer is to
  1882. // create a dedicated 'error' object.
  1883. if (_p) {
  1884. // a connect call will delete its created endpoints on detach, unless otherwise specified.
  1885. // this is because the endpoints belong to this connection only, and are no use to
  1886. // anyone else, so they hang around like a bad smell.
  1887. if (_p.deleteEndpointsOnDetach == null)
  1888. _p.deleteEndpointsOnDetach = true;
  1889. // create the connection. it is not yet registered
  1890. jpc = _newConnection(_p);
  1891. // now add it the model, fire an event, and redraw
  1892. _finaliseConnection(jpc, _p);
  1893. }
  1894. return jpc;
  1895. };
  1896. /*
  1897. Function: deleteEndpoint
  1898. Deletes an Endpoint and removes all Connections it has (which removes the Connections from the other Endpoints involved too)
  1899. Parameters:
  1900. object - either an <Endpoint> object (such as from an addEndpoint call), or a String UUID.
  1901. Returns:
  1902. void
  1903. */
  1904. this.deleteEndpoint = function(object) {
  1905. var endpoint = (typeof object == "string") ? endpointsByUUID[object] : object;
  1906. if (endpoint) {
  1907. var uuid = endpoint.getUuid();
  1908. if (uuid) endpointsByUUID[uuid] = null;
  1909. endpoint.detachAll();
  1910. if (endpoint.endpoint.cleanup) endpoint.endpoint.cleanup();
  1911. _removeElements(endpoint.endpoint.getDisplayElements());
  1912. _currentInstance.anchorManager.deleteEndpoint(endpoint);
  1913. for (var e in endpointsByElement) {
  1914. var endpoints = endpointsByElement[e];
  1915. if (endpoints) {
  1916. var newEndpoints = [];
  1917. for (var i = 0; i < endpoints.length; i++)
  1918. if (endpoints[i] != endpoint) newEndpoints.push(endpoints[i]);
  1919. endpointsByElement[e] = newEndpoints;
  1920. }
  1921. }
  1922. if (!jsPlumbAdapter.headless)
  1923. _currentInstance.dragManager.endpointDeleted(endpoint);
  1924. }
  1925. };
  1926. /*
  1927. Function: deleteEveryEndpoint
  1928. Deletes every <Endpoint>, and their associated <Connection>s, in this instance of jsPlumb. Does not unregister any event listeners (this is the only difference
  1929. between this method and jsPlumb.reset).
  1930. Returns:
  1931. void
  1932. */
  1933. this.deleteEveryEndpoint = function() {
  1934. _currentInstance.setSuspendDrawing(true);
  1935. for ( var id in endpointsByElement) {
  1936. var endpoints = endpointsByElement[id];
  1937. if (endpoints && endpoints.length) {
  1938. for ( var i = 0; i < endpoints.length; i++) {
  1939. _currentInstance.deleteEndpoint(endpoints[i]);
  1940. }
  1941. }
  1942. }
  1943. endpointsByElement = {};
  1944. endpointsByUUID = {};
  1945. _currentInstance.setSuspendDrawing(false, true);
  1946. };
  1947. var fireDetachEvent = function(jpc, doFireEvent, originalEvent) {
  1948. // may have been given a connection, or in special cases, an object
  1949. var connType = _currentInstance.Defaults.ConnectionType || _currentInstance.getDefaultConnectionType(),
  1950. argIsConnection = jpc.constructor == connType,
  1951. params = argIsConnection ? {
  1952. connection:jpc,
  1953. source : jpc.source, target : jpc.target,
  1954. sourceId : jpc.sourceId, targetId : jpc.targetId,
  1955. sourceEndpoint : jpc.endpoints[0], targetEndpoint : jpc.endpoints[1]
  1956. } : jpc;
  1957. if (doFireEvent) {
  1958. _currentInstance.fire("jsPlumbConnectionDetached", params, originalEvent);
  1959. // introduced in 1.3.11..an alias because the original event name is unwieldy. in future versions this will be the only event and the other will no longer be fired.
  1960. _currentInstance.fire("connectionDetached", params, originalEvent);
  1961. }
  1962. _currentInstance.anchorManager.connectionDetached(params);
  1963. },
  1964. /**
  1965. fires an event to indicate an existing connection is being dragged.
  1966. */
  1967. fireConnectionDraggingEvent = function(jpc) {
  1968. _currentInstance.fire("connectionDrag", jpc);
  1969. },
  1970. fireConnectionDragStopEvent = function(jpc) {
  1971. _currentInstance.fire("connectionDragStop", jpc);
  1972. };
  1973. /*
  1974. Function: detach
  1975. Detaches and then removes a <Connection>.
  1976. Parameters:
  1977. connection - the <Connection> to detach
  1978. params - optional parameters to the detach call. valid values here are
  1979. fireEvent : defaults to false; indicates you want jsPlumb to fire a connection
  1980. detached event. The thinking behind this is that if you made a programmatic
  1981. call to detach an event, you probably don't need the callback.
  1982. forceDetach : defaults to false. allows you to override any beforeDetach listeners that may be registered.
  1983. Returns:
  1984. true if successful, false if not.
  1985. */
  1986. this.detach = function() {
  1987. if (arguments.length == 0) return;
  1988. var connType = _currentInstance.Defaults.ConnectionType || _currentInstance.getDefaultConnectionType(),
  1989. firstArgIsConnection = arguments[0].constructor == connType,
  1990. params = arguments.length == 2 ? firstArgIsConnection ? (arguments[1] || {}) : arguments[0] : arguments[0],
  1991. fireEvent = (params.fireEvent !== false),
  1992. forceDetach = params.forceDetach,
  1993. connection = firstArgIsConnection ? arguments[0] : params.connection;
  1994. if (connection) {
  1995. if (forceDetach || (connection.isDetachAllowed(connection)
  1996. && connection.endpoints[0].isDetachAllowed(connection)
  1997. && connection.endpoints[1].isDetachAllowed(connection))) {
  1998. if (forceDetach || _currentInstance.checkCondition("beforeDetach", connection))
  1999. connection.endpoints[0].detach(connection, false, true, fireEvent); // TODO check this param iscorrect for endpoint's detach method
  2000. }
  2001. }
  2002. else {
  2003. var _p = jsPlumb.extend( {}, params); // a backwards compatibility hack: source should be thought of as 'params' in this case.
  2004. // test for endpoint uuids to detach
  2005. if (_p.uuids) {
  2006. _getEndpoint(_p.uuids[0]).detachFrom(_getEndpoint(_p.uuids[1]), fireEvent);
  2007. } else if (_p.sourceEndpoint && _p.targetEndpoint) {
  2008. _p.sourceEndpoint.detachFrom(_p.targetEndpoint);
  2009. } else {
  2010. var sourceId = _getId(_p.source),
  2011. targetId = _getId(_p.target);
  2012. _operation(sourceId, function(jpc) {
  2013. if ((jpc.sourceId == sourceId && jpc.targetId == targetId) || (jpc.targetId == sourceId && jpc.sourceId == targetId)) {
  2014. if (_currentInstance.checkCondition("beforeDetach", jpc)) {
  2015. jpc.endpoints[0].detach(jpc, false, true, fireEvent);
  2016. }
  2017. }
  2018. });
  2019. }
  2020. }
  2021. };
  2022. /*
  2023. Function: detachAllConnections
  2024. Removes all an element's Connections.
  2025. Parameters:
  2026. el - either the id of the element, or a selector for the element.
  2027. params - optional parameters. alowed values:
  2028. fireEvent : defaults to true, whether or not to fire the detach event.
  2029. Returns:
  2030. void
  2031. */
  2032. this.detachAllConnections = function(el, params) {
  2033. params = params || {};
  2034. el = _getElementObject(el);
  2035. var id = _getAttribute(el, "id"),
  2036. endpoints = endpointsByElement[id];
  2037. if (endpoints && endpoints.length) {
  2038. for ( var i = 0; i < endpoints.length; i++) {
  2039. endpoints[i].detachAll(params.fireEvent);
  2040. }
  2041. }
  2042. };
  2043. /*
  2044. Function: detachEveryConnection
  2045. Remove all Connections from all elements, but leaves Endpoints in place.
  2046. Parameters:
  2047. params - optional params object containing:
  2048. fireEvent : whether or not to fire detach events. defaults to true.
  2049. Returns:
  2050. void
  2051. See Also:
  2052. <removeEveryEndpoint>
  2053. */
  2054. this.detachEveryConnection = function(params) {
  2055. params = params || {};
  2056. for ( var id in endpointsByElement) {
  2057. var endpoints = endpointsByElement[id];
  2058. if (endpoints && endpoints.length) {
  2059. for ( var i = 0; i < endpoints.length; i++) {
  2060. endpoints[i].detachAll(params.fireEvent);
  2061. }
  2062. }
  2063. }
  2064. connectionsByScope = {};
  2065. };
  2066. /*
  2067. Function: draggable
  2068. Initialises the draggability of some element or elements. You should use this instead of your
  2069. library's draggable method so that jsPlumb can setup the appropriate callbacks. Your
  2070. underlying library's drag method is always called from this method.
  2071. Parameters:
  2072. el - either an element id, a list of element ids, or a selector.
  2073. options - options to pass through to the underlying library
  2074. Returns:
  2075. void
  2076. */
  2077. this.draggable = function(el, options) {
  2078. if (typeof el == 'object' && el.length) {
  2079. for ( var i = 0; i < el.length; i++) {
  2080. var ele = _getElementObject(el[i]);
  2081. if (ele) _initDraggableIfNecessary(ele, true, options);
  2082. }
  2083. }
  2084. else if (el._nodes) { // TODO this is YUI specific; really the logic should be forced
  2085. // into the library adapters (for jquery and mootools aswell)
  2086. for ( var i = 0; i < el._nodes.length; i++) {
  2087. var ele = _getElementObject(el._nodes[i]);
  2088. if (ele) _initDraggableIfNecessary(ele, true, options);
  2089. }
  2090. }
  2091. else {
  2092. var ele = _getElementObject(el);
  2093. if (ele) _initDraggableIfNecessary(ele, true, options);
  2094. }
  2095. };
  2096. /*
  2097. Function: extend
  2098. Wraps the underlying library's extend functionality.
  2099. Parameters:
  2100. o1 - object to extend
  2101. o2 - object to extend o1 with
  2102. Returns:
  2103. o1, extended with all properties from o2.
  2104. */
  2105. this.extend = function(o1, o2) {
  2106. return jsPlumb.CurrentLibrary.extend(o1, o2);
  2107. };
  2108. /*
  2109. * Function: getDefaultEndpointType
  2110. * Returns the default Endpoint type. Used when someone wants to subclass Endpoint and have jsPlumb return instances of their subclass.
  2111. * you would make a call like this in your class's constructor:
  2112. *
  2113. * : jsPlumb.getDefaultEndpointType().apply(this, arguments);
  2114. *
  2115. *
  2116. * Returns:
  2117. * the default Endpoint function used by jsPlumb.
  2118. */
  2119. this.getDefaultEndpointType = function() {
  2120. return Endpoint;
  2121. };
  2122. /*
  2123. * Function: getDefaultConnectionType
  2124. * Returns the default Connection type. Used when someone wants to subclass Connection and have jsPlumb return instances of their subclass.
  2125. * you would make a call like this in your class's constructor:
  2126. *
  2127. * : jsPlumb.getDefaultConnectionType().apply(this, arguments);
  2128. *
  2129. * Returns:
  2130. * the default Connection function used by jsPlumb.
  2131. */
  2132. this.getDefaultConnectionType = function() {
  2133. return Connection;
  2134. };
  2135. // helpers for select/selectEndpoints
  2136. var _setOperation = function(list, func, args, selector) {
  2137. for (var i = 0; i < list.length; i++) {
  2138. list[i][func].apply(list[i], args);
  2139. }
  2140. return selector(list);
  2141. },
  2142. _getOperation = function(list, func, args) {
  2143. var out = [];
  2144. for (var i = 0; i < list.length; i++) {
  2145. out.push([ list[i][func].apply(list[i], args), list[i] ]);
  2146. }
  2147. return out;
  2148. },
  2149. setter = function(list, func, selector) {
  2150. return function() {
  2151. return _setOperation(list, func, arguments, selector);
  2152. };
  2153. },
  2154. getter = function(list, func) {
  2155. return function() {
  2156. return _getOperation(list, func, arguments);
  2157. };
  2158. },
  2159. prepareList = function(input, doNotGetIds) {
  2160. var r = [];
  2161. if (input) {
  2162. if (typeof input == 'string') {
  2163. if (input === "*") return input;
  2164. r.push(input);
  2165. }
  2166. else {
  2167. if (doNotGetIds) r = input;
  2168. else {
  2169. for (var i = 0; i < input.length; i++)
  2170. r.push(_getId(_getElementObject(input[i])));
  2171. }
  2172. }
  2173. }
  2174. return r;
  2175. },
  2176. filterList = function(list, value, missingIsFalse) {
  2177. if (list === "*") return true;
  2178. return list.length > 0 ? _indexOf(list, value) != -1 : !missingIsFalse;
  2179. };
  2180. /*
  2181. * Function: getConnections
  2182. * Gets all or a subset of connections currently managed by this jsPlumb instance. If only one scope is passed in to this method,
  2183. * the result will be a list of connections having that scope (passing in no scope at all will result in jsPlumb assuming you want the
  2184. * default scope).
  2185. *
  2186. * If multiple scopes are passed in, the return value will be a map of
  2187. *
  2188. * : { scope -> [ connection... ] }
  2189. *
  2190. * Parameters:
  2191. * scope - if the only argument to getConnections is a string, jsPlumb will treat that string as a scope filter, and return a list
  2192. * of connections that are in the given scope. use '*' for all scopes.
  2193. * options - if the argument is a JS object, you can specify a finer-grained filter:
  2194. *
  2195. * - *scope* may be a string specifying a single scope, or an array of strings, specifying multiple scopes. Also may have the value '*', indicating any scope.
  2196. * - *source* either a string representing an element id, a selector, or an array of ids. Also may have the value '*', indicating any source. Constrains the result to connections having this/these element(s) as source.
  2197. * - *target* either a string representing an element id, a selector, or an array of ids. Also may have the value '*', indicating any target. Constrains the result to connections having this/these element(s) as target.
  2198. * flat - return results in a flat array (don't return an object whose keys are scopes and whose values are lists per scope).
  2199. *
  2200. */
  2201. this.getConnections = function(options, flat) {
  2202. if (!options) {
  2203. options = {};
  2204. } else if (options.constructor == String) {
  2205. options = { "scope": options };
  2206. }
  2207. var
  2208. scope = options.scope || _currentInstance.getDefaultScope(),
  2209. scopes = prepareList(scope, true),
  2210. sources = prepareList(options.source),
  2211. targets = prepareList(options.target),
  2212. results = (!flat && scopes.length > 1) ? {} : [],
  2213. _addOne = function(scope, obj) {
  2214. if (!flat && scopes.length > 1) {
  2215. var ss = results[scope];
  2216. if (ss == null) {
  2217. ss = []; results[scope] = ss;
  2218. }
  2219. ss.push(obj);
  2220. } else results.push(obj);
  2221. };
  2222. for ( var i in connectionsByScope) {
  2223. if (filterList(scopes, i)) {
  2224. for ( var j = 0; j < connectionsByScope[i].length; j++) {
  2225. var c = connectionsByScope[i][j];
  2226. if (filterList(sources, c.sourceId) && filterList(targets, c.targetId))
  2227. _addOne(i, c);
  2228. }
  2229. }
  2230. }
  2231. return results;
  2232. };
  2233. var _curryEach = function(list, executor) {
  2234. return function(f) {
  2235. for (var i = 0; i < list.length; i++) {
  2236. f(list[i]);
  2237. }
  2238. return executor(list);
  2239. };
  2240. },
  2241. _curryGet = function(list) {
  2242. return function(idx) {
  2243. return list[idx];
  2244. };
  2245. };
  2246. var _makeCommonSelectHandler = function(list, executor) {
  2247. return {
  2248. // setters
  2249. setHover:setter(list, "setHover", executor),
  2250. removeAllOverlays:setter(list, "removeAllOverlays", executor),
  2251. setLabel:setter(list, "setLabel", executor),
  2252. addOverlay:setter(list, "addOverlay", executor),
  2253. removeOverlay:setter(list, "removeOverlay", executor),
  2254. removeOverlays:setter(list, "removeOverlays", executor),
  2255. showOverlay:setter(list, "showOverlay", executor),
  2256. hideOverlay:setter(list, "hideOverlay", executor),
  2257. showOverlays:setter(list, "showOverlays", executor),
  2258. hideOverlays:setter(list, "hideOverlays", executor),
  2259. setPaintStyle:setter(list, "setPaintStyle", executor),
  2260. setHoverPaintStyle:setter(list, "setHoverPaintStyle", executor),
  2261. setParameter:setter(list, "setParameter", executor),
  2262. setParameters:setter(list, "setParameters", executor),
  2263. setVisible:setter(list, "setVisible", executor),
  2264. setZIndex:setter(list, "setZIndex", executor),
  2265. repaint:setter(list, "repaint", executor),
  2266. addType:setter(list, "addType", executor),
  2267. toggleType:setter(list, "toggleType", executor),
  2268. removeType:setter(list, "removeType", executor),
  2269. // getters
  2270. getLabel:getter(list, "getLabel"),
  2271. getOverlay:getter(list, "getOverlay"),
  2272. isHover:getter(list, "isHover"),
  2273. getParameter:getter(list, "getParameter"),
  2274. getParameters:getter(list, "getParameters"),
  2275. getPaintStyle:getter(list, "getPaintStyle"),
  2276. getHoverPaintStyle:getter(list, "getHoverPaintStyle"),
  2277. isVisible:getter(list, "isVisible"),
  2278. getZIndex:getter(list, "getZIndex"),
  2279. hasType:getter(list, "hasType"),
  2280. getType:getter(list, "getType"),
  2281. // util
  2282. length:list.length,
  2283. each:_curryEach(list, executor),
  2284. get:_curryGet(list)
  2285. };
  2286. };
  2287. var _makeConnectionSelectHandler = function(list) {
  2288. var common = _makeCommonSelectHandler(list, _makeConnectionSelectHandler);
  2289. return jsPlumb.CurrentLibrary.extend(common, {
  2290. // setters
  2291. setDetachable:setter(list, "setDetachable", _makeConnectionSelectHandler),
  2292. setReattach:setter(list, "setReattach", _makeConnectionSelectHandler),
  2293. setConnector:setter(list, "setConnector", _makeConnectionSelectHandler),
  2294. detach:function() {
  2295. for (var i = 0; i < list.length; i++)
  2296. _currentInstance.detach(list[i]);
  2297. },
  2298. // getters
  2299. isDetachable:getter(list, "isDetachable"),
  2300. isReattach:getter(list, "isReattach")
  2301. });
  2302. };
  2303. var _makeEndpointSelectHandler = function(list) {
  2304. var common = _makeCommonSelectHandler(list, _makeEndpointSelectHandler);
  2305. return jsPlumb.CurrentLibrary.extend(common, {
  2306. setEnabled:setter(list, "setEnabled", _makeEndpointSelectHandler),
  2307. isEnabled:getter(list, "isEnabled"),
  2308. detachAll:function() {
  2309. for (var i = 0; i < list.length; i++)
  2310. list[i].detachAll();
  2311. },
  2312. "delete":function() {
  2313. for (var i = 0; i < list.length; i++)
  2314. _currentInstance.deleteEndpoint(list[i]);
  2315. }
  2316. });
  2317. };
  2318. /*
  2319. * Function: select
  2320. * Selects a set of Connections, using the filter options from the getConnections method, and returns an object
  2321. * that allows you to perform an operation on all of the Connections at once.
  2322. *
  2323. * The return value from any of these operations is the original list of Connections, allowing operations to be
  2324. * chained (for 'setter' type operations). 'getter' type operations return an array of values, where each entry is
  2325. * of the form:
  2326. *
  2327. * : [ Connection, return value ]
  2328. *
  2329. * Parameters:
  2330. * scope - see getConnections
  2331. * source - see getConnections
  2332. * target - see getConnections
  2333. *
  2334. * Returns:
  2335. * A list of Connections on which operations may be executed. 'Setter' type operations can be chained; 'getter' type operations
  2336. * return an array of [Connection, value] pairs, one entry for each Connection in the list returned. The full list of operations
  2337. * is as follows (where not specified, the operation's effect or return value is the same as the corresponding method on Connection) :
  2338. *
  2339. * - setHover
  2340. * - removeAllOverlays
  2341. * - setLabel
  2342. * - addOverlay
  2343. * - removeOverlay
  2344. * - removeOverlays
  2345. * - showOverlay
  2346. * - hideOverlay
  2347. * - showOverlays
  2348. * - hideOverlays
  2349. * - setPaintStyle
  2350. * - setHoverPaintStyle
  2351. * - setDetachable
  2352. * - setReattach
  2353. * - setConnector
  2354. * - setParameter
  2355. * - setParameters
  2356. * - getLabel
  2357. * - getOverlay
  2358. * - isHover
  2359. * - isDetachable
  2360. * - isReattach
  2361. * - getParameter
  2362. * - getParameters
  2363. * - getPaintStyle
  2364. * - getHoverPaintStyle
  2365. * - detach : detaches all the connections in the list. not chainable and does not return anything.
  2366. * - length : returns the length of the list.
  2367. * - get(index) : returns the Connection at 'index' in the list.
  2368. * - each(function(connection)...) : allows you to specify your own function to execute; this function is chainable.
  2369. */
  2370. this.select = function(params) {
  2371. params = params || {};
  2372. params.scope = params.scope || "*";
  2373. var c = _currentInstance.getConnections(params, true);
  2374. return _makeConnectionSelectHandler(c);
  2375. };
  2376. /*
  2377. * Function: selectEndpoints
  2378. * Selects a set of Endpoints and returns an object that allows you to execute various different methods on them at once. The return
  2379. * value from any of these operations is the original list of Endpoints, allowing operations to be chained (for 'setter' type
  2380. * operations). 'getter' type operations return an array of values, where each entry is of the form:
  2381. *
  2382. * : [ Endpoint, return value ]
  2383. *
  2384. * Parameters:
  2385. * scope - either a string or an array of strings.
  2386. * source - either a string, a dom element, or a selector, or an array of any mix of these. limits returned endpoints to those that are declared as a source endpoint on any elements identified.
  2387. * target - either a string, a dom element, or a selector, or an array of any mix of these. limits returned endpoints to those that are declared as a target endpoint on any elements identified.
  2388. * element - either a string, a dom element, or a selector, or an array of any mix of these. limits returned endpoints to those that are declared as either a source OR a target endpoint on any elements identified.
  2389. *
  2390. * Returns:
  2391. * A list of Endpoints on which operations may be executed. 'Setter' type operations can be chained; 'getter' type operations
  2392. * return an array of [Endpoint, value] pairs, one entry for each Endpoint in the list returned.
  2393. *
  2394. * The full list of operations is as follows (where not specified, the operation's effect or return value is the
  2395. * same as the corresponding method on Endpoint) :
  2396. *
  2397. * - setHover
  2398. * - removeAllOverlays
  2399. * - setLabel
  2400. * - addOverlay
  2401. * - removeOverlay
  2402. * - removeOverlays
  2403. * - showOverlay
  2404. * - hideOverlay
  2405. * - showOverlays
  2406. * - hideOverlays
  2407. * - setPaintStyle
  2408. * - setHoverPaintStyle
  2409. * - setParameter
  2410. * - setParameters
  2411. * - getLabel
  2412. * - getOverlay
  2413. * - isHover
  2414. * - isDetachable
  2415. * - getParameter
  2416. * - getParameters
  2417. * - getPaintStyle
  2418. * - getHoverPaintStyle
  2419. * - detachAll : Detaches all the Connections from every Endpoint in the list. not chainable and does not return anything.
  2420. * - delete : Deletes every Endpoint in the list. not chainable and does not return anything.
  2421. * - length : returns the length of the list.
  2422. * - get(index) : returns the Endpoint at 'index' in the list.
  2423. * - each(function(endpoint)...) : allows you to specify your own function to execute; this function is chainable.
  2424. */
  2425. this.selectEndpoints = function(params) {
  2426. params = params || {};
  2427. params.scope = params.scope || "*";
  2428. var noElementFilters = !params.element && !params.source && !params.target,
  2429. elements = noElementFilters ? "*" : prepareList(params.element),
  2430. sources = noElementFilters ? "*" : prepareList(params.source),
  2431. targets = noElementFilters ? "*" : prepareList(params.target),
  2432. scopes = prepareList(params.scope, true);
  2433. var ep = [];
  2434. for (var el in endpointsByElement) {
  2435. var either = filterList(elements, el, true),
  2436. source = filterList(sources, el, true),
  2437. sourceMatchExact = sources != "*",
  2438. target = filterList(targets, el, true),
  2439. targetMatchExact = targets != "*";
  2440. // 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.
  2441. if ( either || source || target ) {
  2442. inner:
  2443. for (var i = 0; i < endpointsByElement[el].length; i++) {
  2444. var _ep = endpointsByElement[el][i];
  2445. if (filterList(scopes, _ep.scope, true)) {
  2446. var noMatchSource = (sourceMatchExact && sources.length > 0 && !_ep.isSource),
  2447. noMatchTarget = (targetMatchExact && targets.length > 0 && !_ep.isTarget);
  2448. if (noMatchSource || noMatchTarget)
  2449. continue inner;
  2450. ep.push(_ep);
  2451. }
  2452. }
  2453. }
  2454. }
  2455. return _makeEndpointSelectHandler(ep);
  2456. };
  2457. /*
  2458. * Function: getAllConnections
  2459. * Gets all connections, as a map of the form:
  2460. *
  2461. * : { scope -> [ connection... ] }
  2462. */
  2463. this.getAllConnections = function() {
  2464. return connectionsByScope;
  2465. };
  2466. /*
  2467. * Function: getDefaultScope
  2468. * Gets the default scope for connections and endpoints.
  2469. *
  2470. * A scope defines a type of endpoint/connection; supplying a
  2471. * scope to an Endpoint or Connection allows you to support different
  2472. * types of connections in the same UI. but if you're only interested in
  2473. * one type of connection, you don't need to supply a scope. this method
  2474. * will probably be used by very few people; it's good for testing
  2475. * though.
  2476. */
  2477. this.getDefaultScope = function() {
  2478. return DEFAULT_SCOPE;
  2479. };
  2480. /*
  2481. Function: getEndpoint
  2482. Gets an Endpoint by UUID
  2483. Parameters:
  2484. uuid - the UUID for the Endpoint
  2485. Returns:
  2486. Endpoint with the given UUID, null if nothing found.
  2487. */
  2488. this.getEndpoint = _getEndpoint;
  2489. /**
  2490. * Function: getEndpoints
  2491. * Gets the list of Endpoints for a given element.
  2492. *
  2493. * Parameters:
  2494. * el - element id, dom element, or selector.
  2495. *
  2496. * Returns:
  2497. * An array of Endpoints for the specified element.
  2498. */
  2499. this.getEndpoints = function(el) {
  2500. return endpointsByElement[_getId(el)];
  2501. };
  2502. /*
  2503. * Gets an element's id, creating one if necessary. really only exposed
  2504. * for the lib-specific functionality to access; would be better to pass
  2505. * the current instance into the lib-specific code (even though this is
  2506. * a static call. i just don't want to expose it to the public API).
  2507. */
  2508. this.getId = _getId;
  2509. this.getOffset = function(id) {
  2510. var o = offsets[id];
  2511. return _updateOffset({elId:id});
  2512. };
  2513. /*
  2514. * Function: getSelector
  2515. * This method takes the given selector spec and, using the current underlying library, turns it into
  2516. * a selector from that library. This method exists really as a helper function for those applications
  2517. * where you're writing jsPlumb code that will target more than one library (such as in the case of the
  2518. * jsPlumb demo pages).
  2519. *
  2520. * Parameters:
  2521. * spec - a valid selector string.
  2522. */
  2523. this.getSelector = function(spec) {
  2524. return jsPlumb.CurrentLibrary.getSelector(spec);
  2525. };
  2526. // get the size of the element with the given id, perhaps from cache.
  2527. this.getSize = function(id) {
  2528. var s = sizes[id];
  2529. if (!s) _updateOffset({elId:id});
  2530. return sizes[id];
  2531. };
  2532. this.appendElement = _appendElement;
  2533. var _hoverSuspended = false;
  2534. this.isHoverSuspended = function() { return _hoverSuspended; };
  2535. this.setHoverSuspended = function(s) { _hoverSuspended = s; };
  2536. var _isAvailable = function(m) {
  2537. return function() {
  2538. return jsPlumbAdapter.isRenderModeAvailable(m);
  2539. };
  2540. }
  2541. this.isCanvasAvailable = _isAvailable("canvas");
  2542. this.isSVGAvailable = _isAvailable("svg");
  2543. this.isVMLAvailable = _isAvailable("vml");
  2544. /*
  2545. Function: hide
  2546. Sets an element's connections to be hidden.
  2547. Parameters:
  2548. el - either the id of the element, or a selector for the element.
  2549. changeEndpoints - whether not to also hide endpoints on the element. by default this is false.
  2550. Returns:
  2551. void
  2552. */
  2553. this.hide = function(el, changeEndpoints) {
  2554. _setVisible(el, "none", changeEndpoints);
  2555. };
  2556. // exposed for other objects to use to get a unique id.
  2557. this.idstamp = _idstamp;
  2558. /**
  2559. * callback from the current library to tell us to prepare ourselves (attach
  2560. * mouse listeners etc; can't do that until the library has provided a bind method)
  2561. */
  2562. this.init = function() {
  2563. if (!initialized) {
  2564. _currentInstance.setRenderMode(_currentInstance.Defaults.RenderMode); // calling the method forces the capability logic to be run.
  2565. var bindOne = function(event) {
  2566. jsPlumb.CurrentLibrary.bind(document, event, function(e) {
  2567. if (!_currentInstance.currentlyDragging && renderMode == jsPlumb.CANVAS) {
  2568. // try connections first
  2569. for (var scope in connectionsByScope) {
  2570. var c = connectionsByScope[scope];
  2571. for (var i = 0; i < c.length; i++) {
  2572. var t = c[i].connector[event](e);
  2573. if (t) return;
  2574. }
  2575. }
  2576. for (var el in endpointsByElement) {
  2577. var ee = endpointsByElement[el];
  2578. for (var i = 0; i < ee.length; i++) {
  2579. if (ee[i].endpoint[event](e)) return;
  2580. }
  2581. }
  2582. }
  2583. });
  2584. };
  2585. bindOne("click");bindOne("dblclick");bindOne("mousemove");bindOne("mousedown");bindOne("mouseup");bindOne("contextmenu");
  2586. initialized = true;
  2587. _currentInstance.fire("ready");
  2588. }
  2589. };
  2590. this.log = log;
  2591. this.jsPlumbUIComponent = jsPlumbUIComponent;
  2592. /*
  2593. * Creates an anchor with the given params.
  2594. *
  2595. *
  2596. * Returns: The newly created Anchor.
  2597. */
  2598. this.makeAnchor = function() {
  2599. if (arguments.length == 0) return null;
  2600. var specimen = arguments[0], elementId = arguments[1], jsPlumbInstance = arguments[2], newAnchor = null;
  2601. // if it appears to be an anchor already...
  2602. if (specimen.compute && specimen.getOrientation) return specimen; //TODO hazy here about whether it should be added or is already added somehow.
  2603. // is it the name of an anchor type?
  2604. else if (typeof specimen == "string") {
  2605. newAnchor = jsPlumb.Anchors[arguments[0]]({elementId:elementId, jsPlumbInstance:_currentInstance});
  2606. }
  2607. // is it an array? it will be one of:
  2608. // an array of [name, params] - this defines a single anchor
  2609. // an array of arrays - this defines some dynamic anchors
  2610. // an array of numbers - this defines a single anchor.
  2611. else if (_isArray(specimen)) {
  2612. if (_isArray(specimen[0]) || _isString(specimen[0])) {
  2613. if (specimen.length == 2 && _isString(specimen[0]) && _isObject(specimen[1])) {
  2614. var pp = jsPlumb.extend({elementId:elementId, jsPlumbInstance:_currentInstance}, specimen[1]);
  2615. newAnchor = jsPlumb.Anchors[specimen[0]](pp);
  2616. }
  2617. else
  2618. newAnchor = new DynamicAnchor(specimen, null, elementId);
  2619. }
  2620. else {
  2621. var anchorParams = {
  2622. x:specimen[0], y:specimen[1],
  2623. orientation : (specimen.length >= 4) ? [ specimen[2], specimen[3] ] : [0,0],
  2624. offsets : (specimen.length == 6) ? [ specimen[4], specimen[5] ] : [ 0, 0 ],
  2625. elementId:elementId
  2626. };
  2627. newAnchor = new Anchor(anchorParams);
  2628. newAnchor.clone = function() { return new Anchor(anchorParams); };
  2629. }
  2630. }
  2631. if (!newAnchor.id) newAnchor.id = "anchor_" + _idstamp();
  2632. return newAnchor;
  2633. };
  2634. /**
  2635. * makes a list of anchors from the given list of types or coords, eg
  2636. * ["TopCenter", "RightMiddle", "BottomCenter", [0, 1, -1, -1] ]
  2637. */
  2638. this.makeAnchors = function(types, elementId, jsPlumbInstance) {
  2639. var r = [];
  2640. for ( var i = 0; i < types.length; i++) {
  2641. if (typeof types[i] == "string")
  2642. r.push(jsPlumb.Anchors[types[i]]({elementId:elementId, jsPlumbInstance:jsPlumbInstance}));
  2643. else if (_isArray(types[i]))
  2644. r.push(_currentInstance.makeAnchor(types[i], elementId, jsPlumbInstance));
  2645. }
  2646. return r;
  2647. };
  2648. /**
  2649. * Makes a dynamic anchor from the given list of anchors (which may be in shorthand notation as strings or dimension arrays, or Anchor
  2650. * objects themselves) and the given, optional, anchorSelector function (jsPlumb uses a default if this is not provided; most people will
  2651. * not need to provide this - i think).
  2652. */
  2653. this.makeDynamicAnchor = function(anchors, anchorSelector) {
  2654. return new DynamicAnchor(anchors, anchorSelector);
  2655. };
  2656. /**
  2657. * Function: makeTarget
  2658. * Makes some DOM element a Connection target, allowing you to drag connections to it
  2659. * without having to register any Endpoints on it first. When a Connection is established,
  2660. * the endpoint spec that was passed in to this method is used to create a suitable
  2661. * Endpoint (the default will be used if you do not provide one).
  2662. *
  2663. * Parameters:
  2664. * el - string id or element selector for the element to make a target.
  2665. * params - JS object containing parameters:
  2666. * - *endpoint* optional. specification of an Endpoint to create when a Connection is established.
  2667. * - *scope* optional. scope for the drop zone.
  2668. * - *dropOptions* optional. same stuff as you would pass to dropOptions of an Endpoint definition.
  2669. * - *deleteEndpointsOnDetach* optional, defaults to true. whether or not to delete
  2670. * any Endpoints created by a connection to this target if
  2671. * the connection is subsequently detached. this will not
  2672. * remove Endpoints that have had more Connections attached
  2673. * to them after they were created.
  2674. * - *maxConnections* optional. Specifies the maximum number of Connections that can be made to this element as a target. Default is no limit.
  2675. * - *onMaxConnections* optional. Function to call when user attempts to drop a connection but the limit has been reached.
  2676. * The callback is passed two arguments: a JS object with:
  2677. * : { element, connection, maxConnection }
  2678. * ...and the original event.
  2679. */
  2680. var _targetEndpointDefinitions = {},
  2681. _targetEndpoints = {},
  2682. _targetEndpointsUnique = {},
  2683. _targetMaxConnections = {},
  2684. _setEndpointPaintStylesAndAnchor = function(ep, epIndex) {
  2685. ep.paintStyle = ep.paintStyle ||
  2686. _currentInstance.Defaults.EndpointStyles[epIndex] ||
  2687. _currentInstance.Defaults.EndpointStyle ||
  2688. jsPlumb.Defaults.EndpointStyles[epIndex] ||
  2689. jsPlumb.Defaults.EndpointStyle;
  2690. ep.hoverPaintStyle = ep.hoverPaintStyle ||
  2691. _currentInstance.Defaults.EndpointHoverStyles[epIndex] ||
  2692. _currentInstance.Defaults.EndpointHoverStyle ||
  2693. jsPlumb.Defaults.EndpointHoverStyles[epIndex] ||
  2694. jsPlumb.Defaults.EndpointHoverStyle;
  2695. ep.anchor = ep.anchor ||
  2696. _currentInstance.Defaults.Anchors[epIndex] ||
  2697. _currentInstance.Defaults.Anchor ||
  2698. jsPlumb.Defaults.Anchors[epIndex] ||
  2699. jsPlumb.Defaults.Anchor;
  2700. ep.endpoint = ep.endpoint ||
  2701. _currentInstance.Defaults.Endpoints[epIndex] ||
  2702. _currentInstance.Defaults.Endpoint ||
  2703. jsPlumb.Defaults.Endpoints[epIndex] ||
  2704. jsPlumb.Defaults.Endpoint;
  2705. };
  2706. this.makeTarget = function(el, params, referenceParams) {
  2707. var p = jsPlumb.extend({_jsPlumb:_currentInstance}, referenceParams);
  2708. jsPlumb.extend(p, params);
  2709. _setEndpointPaintStylesAndAnchor(p, 1);
  2710. var jpcl = jsPlumb.CurrentLibrary,
  2711. targetScope = p.scope || _currentInstance.Defaults.Scope,
  2712. deleteEndpointsOnDetach = !(p.deleteEndpointsOnDetach === false),
  2713. maxConnections = p.maxConnections || -1,
  2714. onMaxConnections = p.onMaxConnections;
  2715. _doOne = function(_el) {
  2716. // get the element's id and store the endpoint definition for it. jsPlumb.connect calls will look for one of these,
  2717. // and use the endpoint definition if found.
  2718. var elid = _getId(_el);
  2719. _targetEndpointDefinitions[elid] = p;
  2720. _targetEndpointsUnique[elid] = p.uniqueEndpoint,
  2721. _targetMaxConnections[elid] = maxConnections,
  2722. _targetsEnabled[elid] = true,
  2723. proxyComponent = new jsPlumbUIComponent(p);
  2724. var dropOptions = jsPlumb.extend({}, p.dropOptions || {}),
  2725. _drop = function() {
  2726. var originalEvent = jsPlumb.CurrentLibrary.getDropEvent(arguments),
  2727. targetCount = _currentInstance.select({target:elid}).length;
  2728. _currentInstance.currentlyDragging = false;
  2729. var draggable = _getElementObject(jpcl.getDragObject(arguments)),
  2730. id = _getAttribute(draggable, "dragId"),
  2731. // restore the original scope if necessary (issue 57)
  2732. scope = _getAttribute(draggable, "originalScope"),
  2733. jpc = floatingConnections[id],
  2734. source = jpc.endpoints[0],
  2735. _endpoint = p.endpoint ? jsPlumb.extend({}, p.endpoint) : {};
  2736. if (!_targetsEnabled[elid] || _targetMaxConnections[elid] > 0 && targetCount >= _targetMaxConnections[elid]){
  2737. if (onMaxConnections) {
  2738. onMaxConnections({
  2739. element:_el,
  2740. connection:jpc
  2741. }, originalEvent);
  2742. }
  2743. return false;
  2744. }
  2745. // unlock the source anchor to allow it to refresh its position if necessary
  2746. source.anchor.locked = false;
  2747. if (scope) jpcl.setDragScope(draggable, scope);
  2748. // check if drop is allowed here.
  2749. //var _continue = jpc.isDropAllowed(jpc.sourceId, _getId(_el), jpc.scope);
  2750. var _continue = proxyComponent.isDropAllowed(jpc.sourceId, _getId(_el), jpc.scope, jpc, null);
  2751. // regardless of whether the connection is ok, reconfigure the existing connection to
  2752. // point at the current info. we need this to be correct for the detach event that will follow.
  2753. // clear the source endpoint from the list to detach. we will detach this connection at this
  2754. // point, but we want to keep the source endpoint. the target is a floating endpoint and should
  2755. // be removed. TODO need to figure out whether this code can result in endpoints kicking around
  2756. // when they shouldnt be. like is this a full detach of a connection? can it be?
  2757. if (jpc.endpointsToDeleteOnDetach) {
  2758. if (source === jpc.endpointsToDeleteOnDetach[0])
  2759. jpc.endpointsToDeleteOnDetach[0] = null;
  2760. else if (source === jpc.endpointsToDeleteOnDetach[1])
  2761. jpc.endpointsToDeleteOnDetach[1] = null;
  2762. }
  2763. // reinstate any suspended endpoint; this just puts the connection back into
  2764. // a state in which it will report sensible values if someone asks it about
  2765. // its target. we're going to throw this connection away shortly so it doesnt matter
  2766. // if we manipulate it a bit.
  2767. if (jpc.suspendedEndpoint) {
  2768. jpc.targetId = jpc.suspendedEndpoint.elementId;
  2769. jpc.target = jpcl.getElementObject(jpc.suspendedEndpoint.elementId);
  2770. jpc.endpoints[1] = jpc.suspendedEndpoint;
  2771. }
  2772. if (_continue) {
  2773. // detach this connection from the source.
  2774. source.detach(jpc, false, true, false);
  2775. // make a new Endpoint for the target
  2776. var newEndpoint = _targetEndpoints[elid] || _currentInstance.addEndpoint(_el, p);
  2777. if (p.uniqueEndpoint) _targetEndpoints[elid] = newEndpoint; // may of course just store what it just pulled out. that's ok.
  2778. newEndpoint._makeTargetCreator = true;
  2779. // if the anchor has a 'positionFinder' set, then delegate to that function to find
  2780. // out where to locate the anchor.
  2781. if (newEndpoint.anchor.positionFinder != null) {
  2782. var dropPosition = jpcl.getUIPosition(arguments, _currentInstance.getZoom()),
  2783. elPosition = _getOffset(_el, _currentInstance),
  2784. elSize = _getSize(_el),
  2785. ap = newEndpoint.anchor.positionFinder(dropPosition, elPosition, elSize, newEndpoint.anchor.constructorParams);
  2786. newEndpoint.anchor.x = ap[0];
  2787. newEndpoint.anchor.y = ap[1];
  2788. // now figure an orientation for it..kind of hard to know what to do actually. probably the best thing i can do is to
  2789. // support specifying an orientation in the anchor's spec. if one is not supplied then i will make the orientation
  2790. // be what will cause the most natural link to the source: it will be pointing at the source, but it needs to be
  2791. // specified in one axis only, and so how to make that choice? i think i will use whichever axis is the one in which
  2792. // the target is furthest away from the source.
  2793. }
  2794. var c = _currentInstance.connect({
  2795. source:source,
  2796. target:newEndpoint,
  2797. scope:scope,
  2798. previousConnection:jpc,
  2799. container:jpc.parent,
  2800. deleteEndpointsOnDetach:deleteEndpointsOnDetach,
  2801. // 'endpointWillMoveAfterConnection' is set by the makeSource function, and it indicates that the
  2802. // given endpoint will actually transfer from the element it is currently attached to to some other
  2803. // element after a connection has been established. in that case, we do not want to fire the
  2804. // connection event, since it will have the wrong data in it; makeSource will do it for us.
  2805. // this is controlled by the 'parent' parameter on a makeSource call.
  2806. doNotFireConnectionEvent:source.endpointWillMoveAfterConnection
  2807. });
  2808. // delete the original target endpoint. but only want to do this if the endpoint was created
  2809. // automatically and has no other connections.
  2810. if (jpc.endpoints[1]._makeTargetCreator && jpc.endpoints[1].connections.length < 2)
  2811. _currentInstance.deleteEndpoint(jpc.endpoints[1]);
  2812. if (deleteEndpointsOnDetach)
  2813. c.endpointsToDeleteOnDetach = [ source, newEndpoint ];
  2814. c.repaint();
  2815. }
  2816. // if not allowed to drop...
  2817. else {
  2818. // TODO this code is identical (pretty much) to what happens when a connection
  2819. // dragged from a normal endpoint is in this situation. refactor.
  2820. // is this an existing connection, and will we reattach?
  2821. if (jpc.suspendedEndpoint) {
  2822. //if (source.isReattach) {
  2823. if (jpc.isReattach()) {
  2824. jpc.setHover(false);
  2825. jpc.floatingAnchorIndex = null;
  2826. jpc.suspendedEndpoint.addConnection(jpc);
  2827. _currentInstance.repaint(source.elementId);
  2828. }
  2829. else
  2830. source.detach(jpc, false, true, true, originalEvent); // otherwise, detach the connection and tell everyone about it.
  2831. }
  2832. }
  2833. };
  2834. var dropEvent = jpcl.dragEvents['drop'];
  2835. dropOptions["scope"] = dropOptions["scope"] || targetScope;
  2836. dropOptions[dropEvent] = _wrap(dropOptions[dropEvent], _drop);
  2837. jpcl.initDroppable(_el, dropOptions, true);
  2838. };
  2839. el = _convertYUICollection(el);
  2840. var inputs = el.length && el.constructor != String ? el : [ el ];
  2841. for (var i = 0; i < inputs.length; i++) {
  2842. _doOne(_getElementObject(inputs[i]));
  2843. }
  2844. return _currentInstance;
  2845. };
  2846. /*
  2847. * Function: unmakeTarget
  2848. * Sets the given element to no longer be a connection target.
  2849. *
  2850. * Parameters:
  2851. * el - a string id, a dom element, or a selector representing the element.
  2852. *
  2853. * Returns:
  2854. * The current jsPlumb instance.
  2855. */
  2856. this.unmakeTarget = function(el, doNotClearArrays) {
  2857. el = jsPlumb.CurrentLibrary.getElementObject(el);
  2858. var elid = _getId(el);
  2859. // TODO this is not an exhaustive unmake of a target, since it does not remove the droppable stuff from
  2860. // the element. the effect will be to prevent it form behaving as a target, but it's not completely purged.
  2861. if (!doNotClearArrays) {
  2862. delete _targetEndpointDefinitions[elid];
  2863. delete _targetEndpointsUnique[elid];
  2864. delete _targetMaxConnections[elid];
  2865. delete _targetsEnabled[elid];
  2866. }
  2867. return _currentInstance;
  2868. };
  2869. /*
  2870. * Function: makeTargets
  2871. * Makes all elements in some array or a selector connection targets.
  2872. *
  2873. * Parameters:
  2874. * els - either an array of ids or a selector
  2875. * params - parameters to configure each element as a target with
  2876. * referenceParams - extra parameters to configure each element as a taretsource with.
  2877. *
  2878. * Returns:
  2879. * The current jsPlumb instance.
  2880. */
  2881. this.makeTargets = function(els, params, referenceParams) {
  2882. for ( var i = 0; i < els.length; i++) {
  2883. _currentInstance.makeTarget(els[i], params, referenceParams);
  2884. }
  2885. };
  2886. /**
  2887. * Function: makeSource
  2888. * Makes some DOM element a Connection source, allowing you to drag connections from it
  2889. * without having to register any Endpoints on it first. When a Connection is established,
  2890. * the endpoint spec that was passed in to this method is used to create a suitable
  2891. * Endpoint (the default will be used if you do not provide one).
  2892. *
  2893. * Parameters:
  2894. * el - string id or element selector for the element to make a source.
  2895. * params - JS object containing parameters:
  2896. * - *endpoint* optional. specification of an endpoint to create when a connection is created.
  2897. * - *parent* optional. the element to add Endpoints to when a Connection is established. if you omit this,
  2898. * Endpoints will be added to 'el'.
  2899. * - *scope* optional. scope for the connections dragged from this element.
  2900. * - *dragOptions* optional. same stuff as you would pass to dragOptions of an Endpoint definition.
  2901. * - *deleteEndpointsOnDetach* optional, defaults to false. whether or not to delete
  2902. * any Endpoints created by a connection from this source if
  2903. * the connection is subsequently detached. this will not
  2904. * remove Endpoints that have had more Connections attached
  2905. * to them after they were created.
  2906. * - *filter* - optional function to call when the user presses the mouse button to start a drag. This function is passed the original
  2907. * event and the element on which the associated makeSource call was made. If it returns anything other than false,
  2908. * the drag begins as usual. But if it returns false (the boolean false, not just something falsey), the drag is aborted.
  2909. *
  2910. *
  2911. */
  2912. var _sourceEndpointDefinitions = {},
  2913. _sourceEndpoints = {},
  2914. _sourceEndpointsUnique = {},
  2915. _sourcesEnabled = {},
  2916. _sourceTriggers = {},
  2917. _sourceMaxConnections = {},
  2918. _targetsEnabled = {};
  2919. this.makeSource = function(el, params, referenceParams) {
  2920. var p = jsPlumb.extend({}, referenceParams);
  2921. jsPlumb.extend(p, params);
  2922. _setEndpointPaintStylesAndAnchor(p, 0);
  2923. var jpcl = jsPlumb.CurrentLibrary,
  2924. maxConnections = p.maxConnections || -1,
  2925. onMaxConnections = p.onMaxConnections,
  2926. _doOne = function(_el) {
  2927. // get the element's id and store the endpoint definition for it. jsPlumb.connect calls will look for one of these,
  2928. // and use the endpoint definition if found.
  2929. var elid = _getId(_el),
  2930. parent = p.parent,
  2931. idToRegisterAgainst = parent != null ? _currentInstance.getId(jpcl.getElementObject(parent)) : elid;
  2932. _sourceEndpointDefinitions[idToRegisterAgainst] = p;
  2933. _sourceEndpointsUnique[idToRegisterAgainst] = p.uniqueEndpoint;
  2934. _sourcesEnabled[idToRegisterAgainst] = true;
  2935. var stopEvent = jpcl.dragEvents["stop"],
  2936. dragEvent = jpcl.dragEvents["drag"],
  2937. dragOptions = jsPlumb.extend({ }, p.dragOptions || {}),
  2938. existingDrag = dragOptions.drag,
  2939. existingStop = dragOptions.stop,
  2940. ep = null,
  2941. endpointAddedButNoDragYet = false;
  2942. _sourceMaxConnections[idToRegisterAgainst] = maxConnections;
  2943. // set scope if its not set in dragOptions but was passed in in params
  2944. dragOptions["scope"] = dragOptions["scope"] || p.scope;
  2945. dragOptions[dragEvent] = _wrap(dragOptions[dragEvent], function() {
  2946. if (existingDrag) existingDrag.apply(this, arguments);
  2947. endpointAddedButNoDragYet = false;
  2948. });
  2949. dragOptions[stopEvent] = _wrap(dragOptions[stopEvent], function() {
  2950. if (existingStop) existingStop.apply(this, arguments);
  2951. //_currentlyDown = false;
  2952. _currentInstance.currentlyDragging = false;
  2953. if (ep.connections.length == 0)
  2954. _currentInstance.deleteEndpoint(ep);
  2955. else {
  2956. jpcl.unbind(ep.canvas, "mousedown");
  2957. // reset the anchor to the anchor that was initially provided. the one we were using to drag
  2958. // the connection was just a placeholder that was located at the place the user pressed the
  2959. // mouse button to initiate the drag.
  2960. var anchorDef = p.anchor || _currentInstance.Defaults.Anchor,
  2961. oldAnchor = ep.anchor,
  2962. oldConnection = ep.connections[0];
  2963. ep.anchor = _currentInstance.makeAnchor(anchorDef, elid, _currentInstance);
  2964. if (p.parent) {
  2965. var parent = jpcl.getElementObject(p.parent);
  2966. if (parent) {
  2967. var currentId = ep.elementId;
  2968. var potentialParent = p.container || _currentInstance.Defaults.Container || jsPlumb.Defaults.Container;
  2969. ep.setElement(parent, potentialParent);
  2970. ep.endpointWillMoveAfterConnection = false;
  2971. _currentInstance.anchorManager.rehomeEndpoint(currentId, parent);
  2972. oldConnection.previousConnection = null;
  2973. // remove from connectionsByScope
  2974. _removeWithFunction(connectionsByScope[oldConnection.scope], function(c) {
  2975. return c.id === oldConnection.id;
  2976. });
  2977. _currentInstance.anchorManager.connectionDetached({
  2978. sourceId:oldConnection.sourceId,
  2979. targetId:oldConnection.targetId,
  2980. connection:oldConnection
  2981. });
  2982. _finaliseConnection(oldConnection);
  2983. }
  2984. }
  2985. ep.repaint();
  2986. _currentInstance.repaint(ep.elementId);
  2987. _currentInstance.repaint(oldConnection.targetId);
  2988. }
  2989. });
  2990. // when the user presses the mouse, add an Endpoint, if we are enabled.
  2991. var mouseDownListener = function(e) {
  2992. // if disabled, return.
  2993. if (!_sourcesEnabled[idToRegisterAgainst]) return;
  2994. // if maxConnections reached
  2995. var sourceCount = _currentInstance.select({source:idToRegisterAgainst}).length
  2996. if (_sourceMaxConnections[idToRegisterAgainst] >= 0 && sourceCount >= _sourceMaxConnections[idToRegisterAgainst]) {
  2997. if (onMaxConnections) {
  2998. onMaxConnections({
  2999. element:_el,
  3000. maxConnections:maxConnections
  3001. }, e);
  3002. }
  3003. return false;
  3004. }
  3005. // if a filter was given, run it, and return if it says no.
  3006. if (params.filter) {
  3007. // pass the original event to the user:
  3008. var r = params.filter(jpcl.getOriginalEvent(e), _el);
  3009. if (r === false) return;
  3010. }
  3011. // make sure we have the latest offset for this div
  3012. var myOffsetInfo = _updateOffset({elId:elid});
  3013. var x = ((e.pageX || e.page.x) - myOffsetInfo.left) / myOffsetInfo.width,
  3014. y = ((e.pageY || e.page.y) - myOffsetInfo.top) / myOffsetInfo.height,
  3015. parentX = x,
  3016. parentY = y;
  3017. // if there is a parent, the endpoint will actually be added to it now, rather than the div
  3018. // that was the source. in that case, we have to adjust the anchor position so it refers to
  3019. // the parent.
  3020. if (p.parent) {
  3021. var pEl = jpcl.getElementObject(p.parent),
  3022. pId = _getId(pEl);
  3023. myOffsetInfo = _updateOffset({elId:pId});
  3024. parentX = ((e.pageX || e.page.x) - myOffsetInfo.left) / myOffsetInfo.width,
  3025. parentY = ((e.pageY || e.page.y) - myOffsetInfo.top) / myOffsetInfo.height;
  3026. }
  3027. // we need to override the anchor in here, and force 'isSource', but we don't want to mess with
  3028. // the params passed in, because after a connection is established we're going to reset the endpoint
  3029. // to have the anchor we were given.
  3030. var tempEndpointParams = {};
  3031. jsPlumb.extend(tempEndpointParams, p);
  3032. tempEndpointParams.isSource = true;
  3033. tempEndpointParams.anchor = [x,y,0,0];
  3034. tempEndpointParams.parentAnchor = [ parentX, parentY, 0, 0 ];
  3035. tempEndpointParams.dragOptions = dragOptions;
  3036. // if a parent was given we need to turn that into a "container" argument. this is, by default,
  3037. // the parent of the element we will move to, so parent of p.parent in this case. however, if
  3038. // the user has specified a 'container' on the endpoint definition or on
  3039. // the defaults, we should use that.
  3040. if (p.parent) {
  3041. var potentialParent = tempEndpointParams.container || _currentInstance.Defaults.Container || jsPlumb.Defaults.Container;
  3042. if (potentialParent)
  3043. tempEndpointParams.container = potentialParent;
  3044. else
  3045. tempEndpointParams.container = jsPlumb.CurrentLibrary.getParent(p.parent);
  3046. }
  3047. ep = _currentInstance.addEndpoint(elid, tempEndpointParams);
  3048. endpointAddedButNoDragYet = true;
  3049. // we set this to prevent connections from firing attach events before this function has had a chance
  3050. // to move the endpoint.
  3051. ep.endpointWillMoveAfterConnection = p.parent != null;
  3052. ep.endpointWillMoveTo = p.parent ? jpcl.getElementObject(p.parent) : null;
  3053. var _delTempEndpoint = function() {
  3054. // this mouseup event is fired only if no dragging occurred, by jquery and yui, but for mootools
  3055. // it is fired even if dragging has occurred, in which case we would blow away a perfectly
  3056. // legitimate endpoint, were it not for this check. the flag is set after adding an
  3057. // endpoint and cleared in a drag listener we set in the dragOptions above.
  3058. if(endpointAddedButNoDragYet) {
  3059. _currentInstance.deleteEndpoint(ep);
  3060. }
  3061. };
  3062. _currentInstance.registerListener(ep.canvas, "mouseup", _delTempEndpoint);
  3063. _currentInstance.registerListener(_el, "mouseup", _delTempEndpoint);
  3064. // and then trigger its mousedown event, which will kick off a drag, which will start dragging
  3065. // a new connection from this endpoint.
  3066. jpcl.trigger(ep.canvas, "mousedown", e);
  3067. };
  3068. // register this on jsPlumb so that it can be cleared by a reset.
  3069. _currentInstance.registerListener(_el, "mousedown", mouseDownListener);
  3070. _sourceTriggers[elid] = mouseDownListener;
  3071. };
  3072. el = _convertYUICollection(el);
  3073. var inputs = el.length && el.constructor != String ? el : [ el ];
  3074. for (var i = 0; i < inputs.length; i++) {
  3075. _doOne(_getElementObject(inputs[i]));
  3076. }
  3077. return _currentInstance;
  3078. };
  3079. /**
  3080. * Function: unmakeSource
  3081. * Sets the given element to no longer be a connection source.
  3082. *
  3083. * Parameters:
  3084. * el - a string id, a dom element, or a selector representing the element.
  3085. *
  3086. * Returns:
  3087. * The current jsPlumb instance.
  3088. */
  3089. this.unmakeSource = function(el, doNotClearArrays) {
  3090. el = jsPlumb.CurrentLibrary.getElementObject(el);
  3091. var id = _getId(el),
  3092. mouseDownListener = _sourceTriggers[id];
  3093. if (mouseDownListener)
  3094. _currentInstance.unregisterListener(_el, "mousedown", mouseDownListener);
  3095. if (!doNotClearArrays) {
  3096. delete _sourceEndpointDefinitions[id];
  3097. delete _sourceEndpointsUnique[id];
  3098. delete _sourcesEnabled[id];
  3099. delete _sourceTriggers[id];
  3100. delete _sourceMaxConnections[id];
  3101. }
  3102. return _currentInstance;
  3103. };
  3104. /*
  3105. * Function: unmakeEverySource
  3106. * Resets all elements in this instance of jsPlumb so that none of them are connection sources.
  3107. *
  3108. * Returns:
  3109. * The current jsPlumb instance.
  3110. */
  3111. this.unmakeEverySource = function() {
  3112. for (var i in _sourcesEnabled)
  3113. _currentInstance.unmakeSource(i, true);
  3114. _sourceEndpointDefinitions = {};
  3115. _sourceEndpointsUnique = {};
  3116. _sourcesEnabled = {};
  3117. _sourceTriggers = {};
  3118. };
  3119. /*
  3120. * Function: unmakeEveryTarget
  3121. * Resets all elements in this instance of jsPlumb so that none of them are connection targets.
  3122. *
  3123. * Returns:
  3124. * The current jsPlumb instance.
  3125. */
  3126. this.unmakeEveryTarget = function() {
  3127. for (var i in _targetsEnabled)
  3128. _currentInstance.unmakeTarget(i, true);
  3129. _targetEndpointDefinitions = {};
  3130. _targetEndpointsUnique = {};
  3131. _targetMaxConnections = {};
  3132. _targetsEnabled = {};
  3133. return _currentInstance;
  3134. };
  3135. /*
  3136. * Function: makeSources
  3137. * Makes all elements in some array or a selector connection sources.
  3138. *
  3139. * Parameters:
  3140. * els - either an array of ids or a selector
  3141. * params - parameters to configure each element as a source with
  3142. * referenceParams - extra parameters to configure each element as a source with.
  3143. *
  3144. * Returns:
  3145. * The current jsPlumb instance.
  3146. */
  3147. this.makeSources = function(els, params, referenceParams) {
  3148. for ( var i = 0; i < els.length; i++) {
  3149. _currentInstance.makeSource(els[i], params, referenceParams);
  3150. }
  3151. return _currentInstance;
  3152. };
  3153. // does the work of setting a source enabled or disabled.
  3154. var _setEnabled = function(type, el, state, toggle) {
  3155. var a = type == "source" ? _sourcesEnabled : _targetsEnabled;
  3156. if (_isString(el)) a[el] = toggle ? !a[el] : state;
  3157. else if (el.length) {
  3158. el = _convertYUICollection(el);
  3159. for (var i = 0; i < el.length; i++) {
  3160. var id = _el = jsPlumb.CurrentLibrary.getElementObject(el[i]), id = _getId(_el);
  3161. a[id] = toggle ? !a[id] : state;
  3162. }
  3163. }
  3164. return _currentInstance;
  3165. };
  3166. /*
  3167. * Function: setSourceEnabled
  3168. * Sets the enabled state of one or more elements that were previously made a connection source with the makeSource
  3169. * method.
  3170. *
  3171. * Parameters:
  3172. * el - either a string representing some element's id, or an array of ids, or a selector.
  3173. * state - true to enable the element(s), false to disable it.
  3174. *
  3175. * Returns:
  3176. * The current jsPlumb instance.
  3177. */
  3178. this.setSourceEnabled = function(el, state) {
  3179. return _setEnabled("source", el, state);
  3180. };
  3181. /*
  3182. * Function: toggleSourceEnabled
  3183. * Toggles the source enabled state of the given element or elements.
  3184. *
  3185. * Parameters:
  3186. * el - either a string representing some element's id, or an array of ids, or a selector.
  3187. * state - true to enable the element(s), false to disable it.
  3188. *
  3189. * Returns:
  3190. * The current enabled state of the source.
  3191. */
  3192. this.toggleSourceEnabled = function(el) {
  3193. _setEnabled("source", el, null, true);
  3194. return _currentInstance.isSourceEnabled(el);
  3195. };
  3196. /*
  3197. * Function: isSource
  3198. * Returns whether or not the given element is registered as a connection source.
  3199. *
  3200. * Parameters:
  3201. * el - a string id, a dom element, or a selector representing a single element.
  3202. *
  3203. * Returns:
  3204. * True if source, false if not.
  3205. */
  3206. this.isSource = function(el) {
  3207. el = jsPlumb.CurrentLibrary.getElementObject(el);
  3208. return _sourcesEnabled[_getId(el)] != null;
  3209. };
  3210. /*
  3211. * Function: isSourceEnabled
  3212. * Returns whether or not the given connection source is enabled.
  3213. *
  3214. * Parameters:
  3215. * el - a string id, a dom element, or a selector representing a single element.
  3216. *
  3217. * Returns:
  3218. * True if enabled, false if not.
  3219. */
  3220. this.isSourceEnabled = function(el) {
  3221. el = jsPlumb.CurrentLibrary.getElementObject(el);
  3222. return _sourcesEnabled[_getId(el)] === true;
  3223. };
  3224. /*
  3225. * Function: setTargetEnabled
  3226. * Sets the enabled state of one or more elements that were previously made a connection target with the makeTarget method.
  3227. * method.
  3228. *
  3229. * Parameters:
  3230. * el - either a string representing some element's id, or an array of ids, or a selector.
  3231. * state - true to enable the element(s), false to disable it.
  3232. *
  3233. * Returns:
  3234. * The current jsPlumb instance.
  3235. */
  3236. this.setTargetEnabled = function(el, state) {
  3237. return _setEnabled("target", el, state);
  3238. };
  3239. /*
  3240. * Function: toggleTargetEnabled
  3241. * Toggles the target enabled state of the given element or elements.
  3242. *
  3243. * Parameters:
  3244. * el - either a string representing some element's id, or an array of ids, or a selector.
  3245. * state - true to enable the element(s), false to disable it.
  3246. *
  3247. * Returns:
  3248. * The current enabled state of the target.
  3249. */
  3250. this.toggleTargetEnabled = function(el) {
  3251. return _setEnabled("target", el, null, true);
  3252. return _currentInstance.isTargetEnabled(el);
  3253. };
  3254. /*
  3255. Function: isTarget
  3256. Returns whether or not the given element is registered as a connection target.
  3257. Parameters:
  3258. el - a string id, a dom element, or a selector representing a single element.
  3259. Returns:
  3260. True if source, false if not.
  3261. */
  3262. this.isTarget = function(el) {
  3263. el = jsPlumb.CurrentLibrary.getElementObject(el);
  3264. return _targetsEnabled[_getId(el)] != null;
  3265. };
  3266. /*
  3267. Function: isTargetEnabled
  3268. Returns whether or not the given connection target is enabled.
  3269. Parameters:
  3270. el - a string id, a dom element, or a selector representing a single element.
  3271. Returns:
  3272. True if enabled, false if not.
  3273. */
  3274. this.isTargetEnabled = function(el) {
  3275. el = jsPlumb.CurrentLibrary.getElementObject(el);
  3276. return _targetsEnabled[_getId(el)] === true;
  3277. };
  3278. /*
  3279. * Function: ready
  3280. * Helper method to bind a function to jsPlumb's ready event. You should use this method instead of your
  3281. * library's equivalent, to ensure that jsPlumb has loaded properly before you start to use it. This is
  3282. * particularly true in the case of YUI, because of the asynchronous nature of the module loading process.
  3283. */
  3284. this.ready = function(fn) {
  3285. _currentInstance.bind("ready", fn);
  3286. };
  3287. /*
  3288. * Function: repaint
  3289. * Repaints an element and its connections. This method gets new sizes for the elements before painting anything.
  3290. *
  3291. * Parameters:
  3292. * el - id of the element, a dom element, or a selector representing the element.
  3293. *
  3294. * Returns:
  3295. * The current jsPlumb instance.
  3296. *
  3297. * See Also:
  3298. * <repaintEverything>
  3299. */
  3300. this.repaint = function(el, ui, timestamp) {
  3301. // support both lists...
  3302. if (typeof el == 'object')
  3303. for ( var i = 0; i < el.length; i++) {
  3304. _draw(_getElementObject(el[i]), ui, timestamp);
  3305. }
  3306. else // ...and single strings.
  3307. _draw(_getElementObject(el), ui, timestamp);
  3308. return _currentInstance;
  3309. };
  3310. /*
  3311. * Function: repaintEverything
  3312. * Repaints all connections.
  3313. *
  3314. * Returns:
  3315. * The current jsPlumb instance.
  3316. *
  3317. * See Also:
  3318. * <repaint>
  3319. */
  3320. this.repaintEverything = function() {
  3321. for ( var elId in endpointsByElement) {
  3322. _draw(_getElementObject(elId));
  3323. }
  3324. return _currentInstance;
  3325. };
  3326. /*
  3327. * Function: removeAllEndpoints
  3328. * Removes all Endpoints associated with a given element. Also removes all Connections associated with each Endpoint it removes.
  3329. *
  3330. * Parameters:
  3331. * el - either an element id, or a selector for an element.
  3332. *
  3333. * Returns:
  3334. * The current jsPlumb instance.
  3335. *
  3336. * See Also:
  3337. * <removeEndpoint>
  3338. */
  3339. this.removeAllEndpoints = function(el) {
  3340. var elId = _getAttribute(el, "id"),
  3341. ebe = endpointsByElement[elId];
  3342. if (ebe) {
  3343. for ( var i = 0; i < ebe.length; i++)
  3344. _currentInstance.deleteEndpoint(ebe[i]);
  3345. }
  3346. endpointsByElement[elId] = [];
  3347. return _currentInstance;
  3348. };
  3349. var _registeredListeners = {},
  3350. _unbindRegisteredListeners = function() {
  3351. for (var i in _registeredListeners) {
  3352. for (var j = 0; j < _registeredListeners[i].length; j++) {
  3353. var info = _registeredListeners[i][j];
  3354. jsPlumb.CurrentLibrary.unbind(info.el, info.event, info.listener);
  3355. }
  3356. }
  3357. _registeredListeners = {};
  3358. };
  3359. // internal register listener method. gives us a hook to clean things up
  3360. // with if the user calls jsPlumb.reset.
  3361. this.registerListener = function(el, type, listener) {
  3362. jsPlumb.CurrentLibrary.bind(el, type, listener);
  3363. _addToList(_registeredListeners, type, {el:el, event:type, listener:listener});
  3364. };
  3365. this.unregisterListener = function(el, type, listener) {
  3366. jsPlumb.CurrentLibrary.unbind(el, type, listener);
  3367. _removeWithFunction(_registeredListeners, function(rl) {
  3368. return rl.type == type && rl.listener == listener;
  3369. });
  3370. };
  3371. /*
  3372. * Function:reset
  3373. * Removes all endpoints and connections and clears the listener list. To keep listeners call
  3374. * : jsPlumb.deleteEveryEndpoint()
  3375. * instead of this.
  3376. */
  3377. this.reset = function() {
  3378. _currentInstance.deleteEveryEndpoint();
  3379. _currentInstance.unbind();
  3380. _targetEndpointDefinitions = {};
  3381. _targetEndpoints = {};
  3382. _targetEndpointsUnique = {};
  3383. _targetMaxConnections = {};
  3384. _sourceEndpointDefinitions = {};
  3385. _sourceEndpoints = {};
  3386. _sourceEndpointsUnique = {};
  3387. _sourceMaxConnections = {};
  3388. _unbindRegisteredListeners();
  3389. _currentInstance.anchorManager.reset();
  3390. if (!jsPlumbAdapter.headless)
  3391. _currentInstance.dragManager.reset();
  3392. };
  3393. /*
  3394. * Function: setDefaultScope
  3395. * Sets the default scope for Connections and Endpoints. A scope defines a type of Endpoint/Connection; supplying a
  3396. * scope to an Endpoint or Connection allows you to support different
  3397. * types of Connections in the same UI. If you're only interested in
  3398. * one type of Connection, you don't need to supply a scope. This method
  3399. * will probably be used by very few people; it just instructs jsPlumb
  3400. * to use a different key for the default scope.
  3401. *
  3402. * Parameters:
  3403. * scope - scope to set as default.
  3404. *
  3405. * Returns:
  3406. * The current jsPlumb instance.
  3407. */
  3408. this.setDefaultScope = function(scope) {
  3409. DEFAULT_SCOPE = scope;
  3410. return _currentInstance;
  3411. };
  3412. /*
  3413. * Function: setDraggable
  3414. * Sets whether or not a given element is
  3415. * draggable, regardless of what any jsPlumb command may request.
  3416. *
  3417. * Parameters:
  3418. * el - either the id for the element, or a selector representing the element.
  3419. * draggable - whether or not the element should be draggable.
  3420. *
  3421. * Returns:
  3422. * void
  3423. */
  3424. this.setDraggable = _setDraggable;
  3425. /*
  3426. * Function: setId
  3427. * Changes the id of some element, adjusting all connections and endpoints
  3428. *
  3429. * Parameters:
  3430. * el - a selector, a DOM element, or a string.
  3431. * newId - string.
  3432. */
  3433. this.setId = function(el, newId, doNotSetAttribute) {
  3434. var id = el.constructor == String ? el : _currentInstance.getId(el),
  3435. sConns = _currentInstance.getConnections({source:id, scope:'*'}, true),
  3436. tConns = _currentInstance.getConnections({target:id, scope:'*'}, true);
  3437. newId = "" + newId;
  3438. if (!doNotSetAttribute) {
  3439. el = jsPlumb.CurrentLibrary.getElementObject(id);
  3440. jsPlumb.CurrentLibrary.setAttribute(el, "id", newId);
  3441. }
  3442. el = jsPlumb.CurrentLibrary.getElementObject(newId);
  3443. endpointsByElement[newId] = endpointsByElement[id] || [];
  3444. for (var i = 0; i < endpointsByElement[newId].length; i++) {
  3445. endpointsByElement[newId][i].elementId = newId;
  3446. endpointsByElement[newId][i].element = el;
  3447. endpointsByElement[newId][i].anchor.elementId = newId;
  3448. }
  3449. delete endpointsByElement[id];
  3450. _currentInstance.anchorManager.changeId(id, newId);
  3451. var _conns = function(list, epIdx, type) {
  3452. for (var i = 0; i < list.length; i++) {
  3453. list[i].endpoints[epIdx].elementId = newId;
  3454. list[i].endpoints[epIdx].element = el;
  3455. list[i][type + "Id"] = newId;
  3456. list[i][type] = el;
  3457. }
  3458. };
  3459. _conns(sConns, 0, "source");
  3460. _conns(tConns, 1, "target");
  3461. };
  3462. /*
  3463. * Function: setIdChanged
  3464. * Notify jsPlumb that the element with oldId has had its id changed to newId.
  3465. *
  3466. * This method is equivalent to what jsPlumb does itself in the second step of the setId method above.
  3467. *
  3468. * Parameters:
  3469. * oldId - previous element id
  3470. * newId - element's new id
  3471. *
  3472. * See Also:
  3473. * <setId>
  3474. */
  3475. this.setIdChanged = function(oldId, newId) {
  3476. _currentInstance.setId(oldId, newId, true);
  3477. };
  3478. this.setDebugLog = function(debugLog) {
  3479. log = debugLog;
  3480. };
  3481. var _suspendDrawing = false,
  3482. _suspendedAt = null;
  3483. /*
  3484. * Function: setSuspendDrawing
  3485. * Suspends drawing operations. This can (and should!) be used when you have a lot of connections to make or endpoints to register;
  3486. * it will save you a lot of time.
  3487. *
  3488. * Parameters:
  3489. * val - boolean indicating whether to suspend or not
  3490. * repaintAfterwards - optional boolean instructing jsPlumb to do a full repaint after changing the suspension
  3491. * state. defaults to false.
  3492. */
  3493. this.setSuspendDrawing = function(val, repaintAfterwards) {
  3494. _suspendDrawing = val;
  3495. if (val) _suspendedAt = new Date().getTime(); else _suspendedAt = null;
  3496. if (repaintAfterwards) _currentInstance.repaintEverything();
  3497. };
  3498. /*
  3499. * Function: isSuspendDrawing
  3500. * Returns whether or not drawing is currently suspended.
  3501. */
  3502. this.isSuspendDrawing = function() {
  3503. return _suspendDrawing;
  3504. };
  3505. /*
  3506. * Property: CANVAS
  3507. * Constant for use with the setRenderMode method
  3508. */
  3509. this.CANVAS = "canvas";
  3510. /*
  3511. * Property: SVG
  3512. * Constant for use with the setRenderMode method
  3513. */
  3514. this.SVG = "svg";
  3515. /*
  3516. * Property: VML
  3517. * Constant for use with the setRenderMode method
  3518. */
  3519. this.VML = "vml";
  3520. /*
  3521. * Function: setRenderMode
  3522. * Sets render mode: jsPlumb.CANVAS, jsPlumb.SVG or jsPlumb.VML. jsPlumb will fall back to VML if it determines that
  3523. * what you asked for is not supported (and that VML is). If you asked for VML but the browser does
  3524. * not support it, jsPlumb uses SVG.
  3525. *
  3526. * Parameters:
  3527. * mode - a string representing the mode. Use one of the jsPlumb render mode constants as discussed above.
  3528. *
  3529. * Returns:
  3530. * The render mode that jsPlumb set, which of course may be different from that requested.
  3531. */
  3532. this.setRenderMode = function(mode) {
  3533. renderMode = jsPlumbAdapter.setRenderMode(mode);
  3534. return renderMode;
  3535. };
  3536. /*
  3537. * Function: getRenderMode
  3538. *
  3539. * Returns:
  3540. * The current render mode.
  3541. */
  3542. this.getRenderMode = function() { return renderMode; };
  3543. /*
  3544. * Function: show
  3545. * Sets an element's connections to be visible.
  3546. *
  3547. * Parameters:
  3548. * el - either the id of the element, or a selector for the element.
  3549. * changeEndpoints - whether or not to also change the visible state of the endpoints on the element. this also has a bearing on
  3550. * other connections on those endpoints: if their other endpoint is also visible, the connections are made visible.
  3551. *
  3552. * Returns:
  3553. * The current jsPlumb instance.
  3554. */
  3555. this.show = function(el, changeEndpoints) {
  3556. _setVisible(el, "block", changeEndpoints);
  3557. return _currentInstance;
  3558. };
  3559. /*
  3560. * Function: sizeCanvas
  3561. * Helper to size a canvas. You would typically use
  3562. * this when writing your own Connector or Endpoint implementation.
  3563. *
  3564. * Parameters:
  3565. * x - [int] x position for the Canvas origin
  3566. * y - [int] y position for the Canvas origin
  3567. * w - [int] width of the canvas
  3568. * h - [int] height of the canvas
  3569. *
  3570. * Returns:
  3571. * The current jsPlumb instance
  3572. */
  3573. this.sizeCanvas = function(canvas, x, y, w, h) {
  3574. if (canvas) {
  3575. canvas.style.height = h + "px";
  3576. canvas.height = h;
  3577. canvas.style.width = w + "px";
  3578. canvas.width = w;
  3579. canvas.style.left = x + "px";
  3580. canvas.style.top = y + "px";
  3581. }
  3582. return _currentInstance;
  3583. };
  3584. /**
  3585. * gets some test hooks. nothing writable.
  3586. */
  3587. this.getTestHarness = function() {
  3588. return {
  3589. endpointsByElement : endpointsByElement,
  3590. endpointCount : function(elId) {
  3591. var e = endpointsByElement[elId];
  3592. return e ? e.length : 0;
  3593. },
  3594. connectionCount : function(scope) {
  3595. scope = scope || DEFAULT_SCOPE;
  3596. var c = connectionsByScope[scope];
  3597. return c ? c.length : 0;
  3598. },
  3599. //findIndex : _findIndex,
  3600. getId : _getId,
  3601. makeAnchor:self.makeAnchor,
  3602. makeDynamicAnchor:self.makeDynamicAnchor
  3603. };
  3604. };
  3605. /**
  3606. * Toggles visibility of an element's connections. kept for backwards
  3607. * compatibility
  3608. */
  3609. this.toggle = _toggleVisible;
  3610. /*
  3611. * Function: toggleVisible
  3612. * Toggles visibility of an element's Connections.
  3613. *
  3614. * Parameters:
  3615. * el - either the element's id, or a selector representing the element.
  3616. * changeEndpoints - whether or not to also toggle the endpoints on the element.
  3617. *
  3618. * Returns:
  3619. * void, but should be updated to return the current state
  3620. */
  3621. // TODO: update this method to return the current state.
  3622. this.toggleVisible = _toggleVisible;
  3623. /*
  3624. * Function: toggleDraggable
  3625. * Toggles draggability (sic?) of an element's Connections.
  3626. *
  3627. * Parameters:
  3628. * el - either the element's id, or a selector representing the element.
  3629. *
  3630. * Returns:
  3631. * The current draggable state.
  3632. */
  3633. this.toggleDraggable = _toggleDraggable;
  3634. /*
  3635. * Helper method to wrap an existing function with one of
  3636. * your own. This is used by the various implementations to wrap event
  3637. * callbacks for drag/drop etc; it allows jsPlumb to be transparent in
  3638. * its handling of these things. If a user supplies their own event
  3639. * callback, for anything, it will always be called.
  3640. */
  3641. this.wrap = _wrap;
  3642. this.addListener = this.bind;
  3643. var adjustForParentOffsetAndScroll = function(xy, el) {
  3644. var offsetParent = null, result = xy;
  3645. if (el.tagName.toLowerCase() === "svg" && el.parentNode) {
  3646. offsetParent = el.parentNode;
  3647. }
  3648. else if (el.offsetParent) {
  3649. offsetParent = el.offsetParent;
  3650. }
  3651. if (offsetParent != null) {
  3652. var po = offsetParent.tagName.toLowerCase() === "body" ? {left:0,top:0} : _getOffset(offsetParent, _currentInstance),
  3653. so = offsetParent.tagName.toLowerCase() === "body" ? {left:0,top:0} : {left:offsetParent.scrollLeft, top:offsetParent.scrollTop};
  3654. // i thought it might be cool to do this:
  3655. // lastReturnValue[0] = lastReturnValue[0] - offsetParent.offsetLeft + offsetParent.scrollLeft;
  3656. // lastReturnValue[1] = lastReturnValue[1] - offsetParent.offsetTop + offsetParent.scrollTop;
  3657. // but i think it ignores margins. my reasoning was that it's quicker to not hand off to some underlying
  3658. // library.
  3659. result[0] = xy[0] - po.left + so.left;
  3660. result[1] = xy[1] - po.top + so.top;
  3661. }
  3662. return result;
  3663. };
  3664. /**
  3665. * 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
  3666. * 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"),
  3667. * 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
  3668. * creation of Anchors without user intervention.
  3669. */
  3670. var Anchor = function(params) {
  3671. var self = this;
  3672. this.x = params.x || 0;
  3673. this.y = params.y || 0;
  3674. this.elementId = params.elementId;
  3675. var orientation = params.orientation || [ 0, 0 ];
  3676. var lastTimestamp = null, lastReturnValue = null;
  3677. this.offsets = params.offsets || [ 0, 0 ];
  3678. self.timestamp = null;
  3679. this.compute = function(params) {
  3680. var xy = params.xy, wh = params.wh, element = params.element, timestamp = params.timestamp;
  3681. if (timestamp && timestamp === self.timestamp)
  3682. return lastReturnValue;
  3683. lastReturnValue = [ xy[0] + (self.x * wh[0]) + self.offsets[0], xy[1] + (self.y * wh[1]) + self.offsets[1] ];
  3684. // adjust loc if there is an offsetParent
  3685. lastReturnValue = adjustForParentOffsetAndScroll(lastReturnValue, element.canvas);
  3686. self.timestamp = timestamp;
  3687. return lastReturnValue;
  3688. };
  3689. this.getOrientation = function(_endpoint) { return orientation; };
  3690. this.equals = function(anchor) {
  3691. if (!anchor) return false;
  3692. var ao = anchor.getOrientation();
  3693. var o = this.getOrientation();
  3694. return this.x == anchor.x && this.y == anchor.y
  3695. && this.offsets[0] == anchor.offsets[0]
  3696. && this.offsets[1] == anchor.offsets[1]
  3697. && o[0] == ao[0] && o[1] == ao[1];
  3698. };
  3699. this.getCurrentLocation = function() { return lastReturnValue; };
  3700. };
  3701. /**
  3702. * An Anchor that floats. its orientation is computed dynamically from
  3703. * its position relative to the anchor it is floating relative to. It is used when creating
  3704. * a connection through drag and drop.
  3705. *
  3706. * TODO FloatingAnchor could totally be refactored to extend Anchor just slightly.
  3707. */
  3708. var FloatingAnchor = function(params) {
  3709. // this is the anchor that this floating anchor is referenced to for
  3710. // purposes of calculating the orientation.
  3711. var ref = params.reference,
  3712. // the canvas this refers to.
  3713. refCanvas = params.referenceCanvas,
  3714. size = _getSize(_getElementObject(refCanvas)),
  3715. // these are used to store the current relative position of our
  3716. // anchor wrt the reference anchor. they only indicate
  3717. // direction, so have a value of 1 or -1 (or, very rarely, 0). these
  3718. // values are written by the compute method, and read
  3719. // by the getOrientation method.
  3720. xDir = 0, yDir = 0,
  3721. // temporary member used to store an orientation when the floating
  3722. // anchor is hovering over another anchor.
  3723. orientation = null,
  3724. _lastResult = null;
  3725. // set these to 0 each; they are used by certain types of connectors in the loopback case,
  3726. // when the connector is trying to clear the element it is on. but for floating anchor it's not
  3727. // very important.
  3728. this.x = 0; this.y = 0;
  3729. this.isFloating = true;
  3730. this.compute = function(params) {
  3731. var xy = params.xy, element = params.element,
  3732. 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.
  3733. // adjust loc if there is an offsetParent
  3734. result = adjustForParentOffsetAndScroll(result, element.canvas);
  3735. _lastResult = result;
  3736. return result;
  3737. };
  3738. this.getOrientation = function(_endpoint) {
  3739. if (orientation) return orientation;
  3740. else {
  3741. var o = ref.getOrientation(_endpoint);
  3742. // here we take into account the orientation of the other
  3743. // anchor: if it declares zero for some direction, we declare zero too. this might not be the most awesome. perhaps we can come
  3744. // up with a better way. it's just so that the line we draw looks like it makes sense. maybe this wont make sense.
  3745. return [ Math.abs(o[0]) * xDir * -1,
  3746. Math.abs(o[1]) * yDir * -1 ];
  3747. }
  3748. };
  3749. /**
  3750. * notification the endpoint associated with this anchor is hovering
  3751. * over another anchor; we want to assume that anchor's orientation
  3752. * for the duration of the hover.
  3753. */
  3754. this.over = function(anchor) {
  3755. orientation = anchor.getOrientation();
  3756. };
  3757. /**
  3758. * notification the endpoint associated with this anchor is no
  3759. * longer hovering over another anchor; we should resume calculating
  3760. * orientation as we normally do.
  3761. */
  3762. this.out = function() { orientation = null; };
  3763. this.getCurrentLocation = function() { return _lastResult; };
  3764. };
  3765. /*
  3766. * A DynamicAnchor is an Anchor that contains a list of other Anchors, which it cycles
  3767. * through at compute time to find the one that is located closest to
  3768. * the center of the target element, and returns that Anchor's compute
  3769. * method result. this causes endpoints to follow each other with
  3770. * respect to the orientation of their target elements, which is a useful
  3771. * feature for some applications.
  3772. *
  3773. */
  3774. var DynamicAnchor = function(anchors, anchorSelector, elementId) {
  3775. this.isSelective = true;
  3776. this.isDynamic = true;
  3777. var _anchors = [], self = this,
  3778. _convert = function(anchor) {
  3779. return anchor.constructor == Anchor ? anchor: _currentInstance.makeAnchor(anchor, elementId, _currentInstance);
  3780. };
  3781. for (var i = 0; i < anchors.length; i++)
  3782. _anchors[i] = _convert(anchors[i]);
  3783. this.addAnchor = function(anchor) { _anchors.push(_convert(anchor)); };
  3784. this.getAnchors = function() { return _anchors; };
  3785. this.locked = false;
  3786. var _curAnchor = _anchors.length > 0 ? _anchors[0] : null,
  3787. _curIndex = _anchors.length > 0 ? 0 : -1,
  3788. self = this,
  3789. // helper method to calculate the distance between the centers of the two elements.
  3790. _distance = function(anchor, cx, cy, xy, wh) {
  3791. var ax = xy[0] + (anchor.x * wh[0]), ay = xy[1] + (anchor.y * wh[1]),
  3792. acx = xy[0] + (wh[0] / 2), acy = xy[1] + (wh[1] / 2);
  3793. return (Math.sqrt(Math.pow(cx - ax, 2) + Math.pow(cy - ay, 2)) +
  3794. Math.sqrt(Math.pow(acx - ax, 2) + Math.pow(acy - ay, 2)));
  3795. },
  3796. // default method uses distance between element centers. you can provide your own method in the dynamic anchor
  3797. // constructor (and also to jsPlumb.makeDynamicAnchor). the arguments to it are four arrays:
  3798. // xy - xy loc of the anchor's element
  3799. // wh - anchor's element's dimensions
  3800. // txy - xy loc of the element of the other anchor in the connection
  3801. // twh - dimensions of the element of the other anchor in the connection.
  3802. // anchors - the list of selectable anchors
  3803. _anchorSelector = anchorSelector || function(xy, wh, txy, twh, anchors) {
  3804. var cx = txy[0] + (twh[0] / 2), cy = txy[1] + (twh[1] / 2);
  3805. var minIdx = -1, minDist = Infinity;
  3806. for ( var i = 0; i < anchors.length; i++) {
  3807. var d = _distance(anchors[i], cx, cy, xy, wh);
  3808. if (d < minDist) {
  3809. minIdx = i + 0;
  3810. minDist = d;
  3811. }
  3812. }
  3813. return anchors[minIdx];
  3814. };
  3815. this.compute = function(params) {
  3816. var xy = params.xy, wh = params.wh, timestamp = params.timestamp, txy = params.txy, twh = params.twh;
  3817. // if anchor is locked or an opposite element was not given, we
  3818. // maintain our state. anchor will be locked
  3819. // if it is the source of a drag and drop.
  3820. if (self.locked || txy == null || twh == null)
  3821. return _curAnchor.compute(params);
  3822. else
  3823. params.timestamp = null; // otherwise clear this, i think. we want the anchor to compute.
  3824. _curAnchor = _anchorSelector(xy, wh, txy, twh, _anchors);
  3825. self.x = _curAnchor.x;
  3826. self.y = _curAnchor.y;
  3827. return _curAnchor.compute(params);
  3828. };
  3829. this.getCurrentLocation = function() {
  3830. return _curAnchor != null ? _curAnchor.getCurrentLocation() : null;
  3831. };
  3832. this.getOrientation = function(_endpoint) { return _curAnchor != null ? _curAnchor.getOrientation(_endpoint) : [ 0, 0 ]; };
  3833. this.over = function(anchor) { if (_curAnchor != null) _curAnchor.over(anchor); };
  3834. this.out = function() { if (_curAnchor != null) _curAnchor.out(); };
  3835. };
  3836. /*
  3837. manages anchors for all elements.
  3838. */
  3839. // "continuous" anchors: anchors that pick their location based on how many connections the given element has.
  3840. // this requires looking at a lot more elements than normal - anything that has had a Continuous anchor applied has
  3841. // to be recalculated. so this manager is used as a reference point. the first time, with a new timestamp, that
  3842. // a continuous anchor is asked to compute, it calls this guy. or maybe, even, this guy gets called somewhere else
  3843. // and compute only ever returns pre-computed values. either way, this is the central point, and we want it to
  3844. // be called as few times as possible.
  3845. var continuousAnchors = {},
  3846. continuousAnchorLocations = {},
  3847. continuousAnchorOrientations = {},
  3848. Orientation = { HORIZONTAL : "horizontal", VERTICAL : "vertical", DIAGONAL : "diagonal", IDENTITY:"identity" },
  3849. // TODO this functions uses a crude method of determining orientation between two elements.
  3850. // 'diagonal' should be chosen when the angle of the line between the two centers is around
  3851. // one of 45, 135, 225 and 315 degrees. maybe +- 15 degrees.
  3852. calculateOrientation = function(sourceId, targetId, sd, td) {
  3853. if (sourceId === targetId) return {
  3854. orientation:Orientation.IDENTITY,
  3855. a:["top", "top"]
  3856. };
  3857. var theta = Math.atan2((td.centery - sd.centery) , (td.centerx - sd.centerx)),
  3858. theta2 = Math.atan2((sd.centery - td.centery) , (sd.centerx - td.centerx)),
  3859. h = ((sd.left <= td.left && sd.right >= td.left) || (sd.left <= td.right && sd.right >= td.right) ||
  3860. (sd.left <= td.left && sd.right >= td.right) || (td.left <= sd.left && td.right >= sd.right)),
  3861. v = ((sd.top <= td.top && sd.bottom >= td.top) || (sd.top <= td.bottom && sd.bottom >= td.bottom) ||
  3862. (sd.top <= td.top && sd.bottom >= td.bottom) || (td.top <= sd.top && td.bottom >= sd.bottom));
  3863. if (! (h || v)) {
  3864. var a = null, rls = false, rrs = false, sortValue = null;
  3865. if (td.left > sd.left && td.top > sd.top)
  3866. a = ["right", "top"];
  3867. else if (td.left > sd.left && sd.top > td.top)
  3868. a = [ "top", "left"];
  3869. else if (td.left < sd.left && td.top < sd.top)
  3870. a = [ "top", "right"];
  3871. else if (td.left < sd.left && td.top > sd.top)
  3872. a = ["left", "top" ];
  3873. return { orientation:Orientation.DIAGONAL, a:a, theta:theta, theta2:theta2 };
  3874. }
  3875. else if (h) return {
  3876. orientation:Orientation.HORIZONTAL,
  3877. a:sd.top < td.top ? ["bottom", "top"] : ["top", "bottom"],
  3878. theta:theta, theta2:theta2
  3879. }
  3880. else return {
  3881. orientation:Orientation.VERTICAL,
  3882. a:sd.left < td.left ? ["right", "left"] : ["left", "right"],
  3883. theta:theta, theta2:theta2
  3884. }
  3885. },
  3886. placeAnchorsOnLine = function(desc, elementDimensions, elementPosition,
  3887. connections, horizontal, otherMultiplier, reverse) {
  3888. var a = [], step = elementDimensions[horizontal ? 0 : 1] / (connections.length + 1);
  3889. for (var i = 0; i < connections.length; i++) {
  3890. var val = (i + 1) * step, other = otherMultiplier * elementDimensions[horizontal ? 1 : 0];
  3891. if (reverse)
  3892. val = elementDimensions[horizontal ? 0 : 1] - val;
  3893. var dx = (horizontal ? val : other), x = elementPosition[0] + dx, xp = dx / elementDimensions[0],
  3894. dy = (horizontal ? other : val), y = elementPosition[1] + dy, yp = dy / elementDimensions[1];
  3895. a.push([ x, y, xp, yp, connections[i][1], connections[i][2] ]);
  3896. }
  3897. return a;
  3898. },
  3899. standardEdgeSort = function(a, b) { return a[0] > b[0] ? 1 : -1 },
  3900. currySort = function(reverseAngles) {
  3901. return function(a,b) {
  3902. var r = true;
  3903. if (reverseAngles) {
  3904. if (a[0][0] < b[0][0])
  3905. r = true;
  3906. else
  3907. r = a[0][1] > b[0][1];
  3908. }
  3909. else {
  3910. if (a[0][0] > b[0][0])
  3911. r= true;
  3912. else
  3913. r =a[0][1] > b[0][1];
  3914. }
  3915. return r === false ? -1 : 1;
  3916. };
  3917. },
  3918. leftSort = function(a,b) {
  3919. // first get adjusted values
  3920. var p1 = a[0][0] < 0 ? -Math.PI - a[0][0] : Math.PI - a[0][0],
  3921. p2 = b[0][0] < 0 ? -Math.PI - b[0][0] : Math.PI - b[0][0];
  3922. if (p1 > p2) return 1;
  3923. else return a[0][1] > b[0][1] ? 1 : -1;
  3924. },
  3925. edgeSortFunctions = {
  3926. "top":standardEdgeSort,
  3927. "right":currySort(true),
  3928. "bottom":currySort(true),
  3929. "left":leftSort
  3930. },
  3931. _sortHelper = function(_array, _fn) {
  3932. return _array.sort(_fn);
  3933. },
  3934. placeAnchors = function(elementId, _anchorLists) {
  3935. var sS = sizes[elementId], sO = offsets[elementId],
  3936. placeSomeAnchors = function(desc, elementDimensions, elementPosition, unsortedConnections, isHorizontal, otherMultiplier, orientation) {
  3937. if (unsortedConnections.length > 0) {
  3938. var sc = _sortHelper(unsortedConnections, edgeSortFunctions[desc]), // puts them in order based on the target element's pos on screen
  3939. reverse = desc === "right" || desc === "top",
  3940. anchors = placeAnchorsOnLine(desc, elementDimensions,
  3941. elementPosition, sc,
  3942. isHorizontal, otherMultiplier, reverse );
  3943. // takes a computed anchor position and adjusts it for parent offset and scroll, then stores it.
  3944. var _setAnchorLocation = function(endpoint, anchorPos) {
  3945. var a = adjustForParentOffsetAndScroll([anchorPos[0], anchorPos[1]], endpoint.canvas);
  3946. continuousAnchorLocations[endpoint.id] = [ a[0], a[1], anchorPos[2], anchorPos[3] ];
  3947. continuousAnchorOrientations[endpoint.id] = orientation;
  3948. };
  3949. for (var i = 0; i < anchors.length; i++) {
  3950. var c = anchors[i][4], weAreSource = c.endpoints[0].elementId === elementId, weAreTarget = c.endpoints[1].elementId === elementId;
  3951. if (weAreSource)
  3952. _setAnchorLocation(c.endpoints[0], anchors[i]);
  3953. else if (weAreTarget)
  3954. _setAnchorLocation(c.endpoints[1], anchors[i]);
  3955. }
  3956. }
  3957. };
  3958. placeSomeAnchors("bottom", sS, [sO.left,sO.top], _anchorLists.bottom, true, 1, [0,1]);
  3959. placeSomeAnchors("top", sS, [sO.left,sO.top], _anchorLists.top, true, 0, [0,-1]);
  3960. placeSomeAnchors("left", sS, [sO.left,sO.top], _anchorLists.left, false, 0, [-1,0]);
  3961. placeSomeAnchors("right", sS, [sO.left,sO.top], _anchorLists.right, false, 1, [1,0]);
  3962. },
  3963. AnchorManager = function() {
  3964. var _amEndpoints = {},
  3965. connectionsByElementId = {},
  3966. self = this,
  3967. anchorLists = {};
  3968. this.reset = function() {
  3969. _amEndpoints = {};
  3970. connectionsByElementId = {};
  3971. anchorLists = {};
  3972. };
  3973. this.newConnection = function(conn) {
  3974. var sourceId = conn.sourceId, targetId = conn.targetId,
  3975. ep = conn.endpoints,
  3976. doRegisterTarget = true,
  3977. registerConnection = function(otherIndex, otherEndpoint, otherAnchor, elId, c) {
  3978. if ((sourceId == targetId) && otherAnchor.isContinuous){
  3979. // remove the target endpoint's canvas. we dont need it.
  3980. jsPlumb.CurrentLibrary.removeElement(ep[1].canvas);
  3981. doRegisterTarget = false;
  3982. }
  3983. _addToList(connectionsByElementId, elId, [c, otherEndpoint, otherAnchor.constructor == DynamicAnchor]);
  3984. };
  3985. registerConnection(0, ep[0], ep[0].anchor, targetId, conn);
  3986. if (doRegisterTarget)
  3987. registerConnection(1, ep[1], ep[1].anchor, sourceId, conn);
  3988. };
  3989. this.connectionDetached = function(connInfo) {
  3990. var connection = connInfo.connection || connInfo;
  3991. var sourceId = connection.sourceId,
  3992. targetId = connection.targetId,
  3993. ep = connection.endpoints,
  3994. removeConnection = function(otherIndex, otherEndpoint, otherAnchor, elId, c) {
  3995. if (otherAnchor.constructor == FloatingAnchor) {
  3996. // no-op
  3997. }
  3998. else {
  3999. _removeWithFunction(connectionsByElementId[elId], function(_c) {
  4000. return _c[0].id == c.id;
  4001. });
  4002. }
  4003. };
  4004. removeConnection(1, ep[1], ep[1].anchor, sourceId, connection);
  4005. removeConnection(0, ep[0], ep[0].anchor, targetId, connection);
  4006. // remove from anchorLists
  4007. var sEl = connection.sourceId,
  4008. tEl = connection.targetId,
  4009. sE = connection.endpoints[0].id,
  4010. tE = connection.endpoints[1].id,
  4011. _remove = function(list, eId) {
  4012. if (list) { // transient anchors dont get entries in this list.
  4013. var f = function(e) { return e[4] == eId; };
  4014. _removeWithFunction(list["top"], f);
  4015. _removeWithFunction(list["left"], f);
  4016. _removeWithFunction(list["bottom"], f);
  4017. _removeWithFunction(list["right"], f);
  4018. }
  4019. };
  4020. _remove(anchorLists[sEl], sE);
  4021. _remove(anchorLists[tEl], tE);
  4022. self.redraw(sEl);
  4023. self.redraw(tEl);
  4024. };
  4025. this.add = function(endpoint, elementId) {
  4026. _addToList(_amEndpoints, elementId, endpoint);
  4027. };
  4028. this.changeId = function(oldId, newId) {
  4029. connectionsByElementId[newId] = connectionsByElementId[oldId];
  4030. _amEndpoints[newId] = _amEndpoints[oldId];
  4031. delete connectionsByElementId[oldId];
  4032. delete _amEndpoints[oldId];
  4033. };
  4034. this.getConnectionsFor = function(elementId) {
  4035. return connectionsByElementId[elementId] || [];
  4036. };
  4037. this.getEndpointsFor = function(elementId) {
  4038. return _amEndpoints[elementId] || [];
  4039. };
  4040. this.deleteEndpoint = function(endpoint) {
  4041. _removeWithFunction(_amEndpoints[endpoint.elementId], function(e) {
  4042. return e.id == endpoint.id;
  4043. });
  4044. };
  4045. this.clearFor = function(elementId) {
  4046. delete _amEndpoints[elementId];
  4047. _amEndpoints[elementId] = [];
  4048. };
  4049. // updates the given anchor list by either updating an existing anchor's info, or adding it. this function
  4050. // also removes the anchor from its previous list, if the edge it is on has changed.
  4051. // all connections found along the way (those that are connected to one of the faces this function
  4052. // operates on) are added to the connsToPaint list, as are their endpoints. in this way we know to repaint
  4053. // them wthout having to calculate anything else about them.
  4054. var _updateAnchorList = function(lists, theta, order, conn, aBoolean, otherElId, idx, reverse, edgeId, elId, connsToPaint, endpointsToPaint) {
  4055. // first try to find the exact match, but keep track of the first index of a matching element id along the way.s
  4056. var exactIdx = -1,
  4057. firstMatchingElIdx = -1,
  4058. endpoint = conn.endpoints[idx],
  4059. endpointId = endpoint.id,
  4060. oIdx = [1,0][idx],
  4061. values = [ [ theta, order ], conn, aBoolean, otherElId, endpointId ],
  4062. listToAddTo = lists[edgeId],
  4063. listToRemoveFrom = endpoint._continuousAnchorEdge ? lists[endpoint._continuousAnchorEdge] : null;
  4064. if (listToRemoveFrom) {
  4065. var rIdx = _findWithFunction(listToRemoveFrom, function(e) { return e[4] == endpointId });
  4066. if (rIdx != -1) {
  4067. listToRemoveFrom.splice(rIdx, 1);
  4068. // get all connections from this list
  4069. for (var i = 0; i < listToRemoveFrom.length; i++) {
  4070. _addWithFunction(connsToPaint, listToRemoveFrom[i][1], function(c) { return c.id == listToRemoveFrom[i][1].id });
  4071. _addWithFunction(endpointsToPaint, listToRemoveFrom[i][1].endpoints[idx], function(e) { return e.id == listToRemoveFrom[i][1].endpoints[idx].id });
  4072. }
  4073. }
  4074. }
  4075. for (var i = 0; i < listToAddTo.length; i++) {
  4076. if (idx == 1 && listToAddTo[i][3] === otherElId && firstMatchingElIdx == -1)
  4077. firstMatchingElIdx = i;
  4078. _addWithFunction(connsToPaint, listToAddTo[i][1], function(c) { return c.id == listToAddTo[i][1].id });
  4079. _addWithFunction(endpointsToPaint, listToAddTo[i][1].endpoints[idx], function(e) { return e.id == listToAddTo[i][1].endpoints[idx].id });
  4080. }
  4081. if (exactIdx != -1) {
  4082. listToAddTo[exactIdx] = values;
  4083. }
  4084. else {
  4085. var insertIdx = reverse ? firstMatchingElIdx != -1 ? firstMatchingElIdx : 0 : listToAddTo.length; // of course we will get this from having looked through the array shortly.
  4086. listToAddTo.splice(insertIdx, 0, values);
  4087. }
  4088. // store this for next time.
  4089. endpoint._continuousAnchorEdge = edgeId;
  4090. };
  4091. this.redraw = function(elementId, ui, timestamp, offsetToUI) {
  4092. if (!_suspendDrawing) {
  4093. // get all the endpoints for this element
  4094. var ep = _amEndpoints[elementId] || [],
  4095. endpointConnections = connectionsByElementId[elementId] || [],
  4096. connectionsToPaint = [],
  4097. endpointsToPaint = [],
  4098. anchorsToUpdate = [];
  4099. timestamp = timestamp || _timestamp();
  4100. // offsetToUI are values that would have been calculated in the dragManager when registering
  4101. // an endpoint for an element that had a parent (somewhere in the hierarchy) that had been
  4102. // registered as draggable.
  4103. offsetToUI = offsetToUI || {left:0, top:0};
  4104. if (ui) {
  4105. ui = {
  4106. left:ui.left + offsetToUI.left,
  4107. top:ui.top + offsetToUI.top
  4108. }
  4109. }
  4110. _updateOffset( { elId : elementId, offset : ui, recalc : false, timestamp : timestamp });
  4111. // valid for one paint cycle.
  4112. var myOffset = offsets[elementId],
  4113. myWH = sizes[elementId],
  4114. orientationCache = {};
  4115. // actually, first we should compute the orientation of this element to all other elements to which
  4116. // this element is connected with a continuous anchor (whether both ends of the connection have
  4117. // a continuous anchor or just one)
  4118. //for (var i = 0; i < continuousAnchorConnections.length; i++) {
  4119. for (var i = 0; i < endpointConnections.length; i++) {
  4120. var conn = endpointConnections[i][0],
  4121. sourceId = conn.sourceId,
  4122. targetId = conn.targetId,
  4123. sourceContinuous = conn.endpoints[0].anchor.isContinuous,
  4124. targetContinuous = conn.endpoints[1].anchor.isContinuous;
  4125. if (sourceContinuous || targetContinuous) {
  4126. var oKey = sourceId + "_" + targetId,
  4127. oKey2 = targetId + "_" + sourceId,
  4128. o = orientationCache[oKey],
  4129. oIdx = conn.sourceId == elementId ? 1 : 0;
  4130. if (sourceContinuous && !anchorLists[sourceId]) anchorLists[sourceId] = { top:[], right:[], bottom:[], left:[] };
  4131. if (targetContinuous && !anchorLists[targetId]) anchorLists[targetId] = { top:[], right:[], bottom:[], left:[] };
  4132. if (elementId != targetId) _updateOffset( { elId : targetId, timestamp : timestamp });
  4133. if (elementId != sourceId) _updateOffset( { elId : sourceId, timestamp : timestamp });
  4134. var td = _getCachedData(targetId),
  4135. sd = _getCachedData(sourceId);
  4136. if (targetId == sourceId && (sourceContinuous || targetContinuous)) {
  4137. // here we may want to improve this by somehow determining the face we'd like
  4138. // to put the connector on. ideally, when drawing, the face should be calculated
  4139. // by determining which face is closest to the point at which the mouse button
  4140. // was released. for now, we're putting it on the top face.
  4141. _updateAnchorList(anchorLists[sourceId], -Math.PI / 2, 0, conn, false, targetId, 0, false, "top", sourceId, connectionsToPaint, endpointsToPaint)
  4142. }
  4143. else {
  4144. if (!o) {
  4145. o = calculateOrientation(sourceId, targetId, sd.o, td.o);
  4146. orientationCache[oKey] = o;
  4147. // this would be a performance enhancement, but the computed angles need to be clamped to
  4148. //the (-PI/2 -> PI/2) range in order for the sorting to work properly.
  4149. /* orientationCache[oKey2] = {
  4150. orientation:o.orientation,
  4151. a:[o.a[1], o.a[0]],
  4152. theta:o.theta + Math.PI,
  4153. theta2:o.theta2 + Math.PI
  4154. };*/
  4155. }
  4156. if (sourceContinuous) _updateAnchorList(anchorLists[sourceId], o.theta, 0, conn, false, targetId, 0, false, o.a[0], sourceId, connectionsToPaint, endpointsToPaint);
  4157. if (targetContinuous) _updateAnchorList(anchorLists[targetId], o.theta2, -1, conn, true, sourceId, 1, true, o.a[1], targetId, connectionsToPaint, endpointsToPaint);
  4158. }
  4159. if (sourceContinuous) _addWithFunction(anchorsToUpdate, sourceId, function(a) { return a === sourceId; });
  4160. if (targetContinuous) _addWithFunction(anchorsToUpdate, targetId, function(a) { return a === targetId; });
  4161. _addWithFunction(connectionsToPaint, conn, function(c) { return c.id == conn.id; });
  4162. if ((sourceContinuous && oIdx == 0) || (targetContinuous && oIdx == 1))
  4163. _addWithFunction(endpointsToPaint, conn.endpoints[oIdx], function(e) { return e.id == conn.endpoints[oIdx].id; });
  4164. }
  4165. }
  4166. // place Endpoints whose anchors are continuous but have no Connections
  4167. for (var i = 0; i < ep.length; i++) {
  4168. if (ep[i].connections.length == 0 && ep[i].anchor.isContinuous) {
  4169. if (!anchorLists[elementId]) anchorLists[elementId] = { top:[], right:[], bottom:[], left:[] };
  4170. _updateAnchorList(anchorLists[elementId], -Math.PI / 2, 0, {endpoints:[ep[i], ep[i]], paint:function(){}}, false, elementId, 0, false, "top", elementId, connectionsToPaint, endpointsToPaint)
  4171. _addWithFunction(anchorsToUpdate, elementId, function(a) { return a === elementId; })
  4172. }
  4173. }
  4174. // now place all the continuous anchors we need to;
  4175. for (var i = 0; i < anchorsToUpdate.length; i++) {
  4176. placeAnchors(anchorsToUpdate[i], anchorLists[anchorsToUpdate[i]]);
  4177. }
  4178. // now that continuous anchors have been placed, paint all the endpoints for this element
  4179. // TODO performance: add the endpoint ids to a temp array, and then when iterating in the next
  4180. // loop, check that we didn't just paint that endpoint. we can probably shave off a few more milliseconds this way.
  4181. for (var i = 0; i < ep.length; i++) {
  4182. ep[i].paint( { timestamp : timestamp, offset : myOffset, dimensions : myWH });
  4183. }
  4184. // ... and any other endpoints we came across as a result of the continuous anchors.
  4185. for (var i = 0; i < endpointsToPaint.length; i++) {
  4186. endpointsToPaint[i].paint( { timestamp : timestamp, offset : myOffset, dimensions : myWH });
  4187. }
  4188. // paint all the standard and "dynamic connections", which are connections whose other anchor is
  4189. // static and therefore does need to be recomputed; we make sure that happens only one time.
  4190. // TODO we could have compiled a list of these in the first pass through connections; might save some time.
  4191. for (var i = 0; i < endpointConnections.length; i++) {
  4192. var otherEndpoint = endpointConnections[i][1];
  4193. if (otherEndpoint.anchor.constructor == DynamicAnchor) {
  4194. otherEndpoint.paint({ elementWithPrecedence:elementId });
  4195. _addWithFunction(connectionsToPaint, endpointConnections[i][0], function(c) { return c.id == endpointConnections[i][0].id; });
  4196. // all the connections for the other endpoint now need to be repainted
  4197. for (var k = 0; k < otherEndpoint.connections.length; k++) {
  4198. if (otherEndpoint.connections[k] !== endpointConnections[i][0])
  4199. _addWithFunction(connectionsToPaint, otherEndpoint.connections[k], function(c) { return c.id == otherEndpoint.connections[k].id; });
  4200. }
  4201. } else if (otherEndpoint.anchor.constructor == Anchor) {
  4202. _addWithFunction(connectionsToPaint, endpointConnections[i][0], function(c) { return c.id == endpointConnections[i][0].id; });
  4203. }
  4204. }
  4205. // paint current floating connection for this element, if there is one.
  4206. var fc = floatingConnections[elementId];
  4207. if (fc)
  4208. fc.paint({timestamp:timestamp, recalc:false, elId:elementId});
  4209. // paint all the connections
  4210. for (var i = 0; i < connectionsToPaint.length; i++) {
  4211. connectionsToPaint[i].paint({elId:elementId, timestamp:timestamp, recalc:false});
  4212. }
  4213. }
  4214. };
  4215. this.rehomeEndpoint = function(currentId, element) {
  4216. var eps = _amEndpoints[currentId] || [], //,
  4217. elementId = _currentInstance.getId(element);
  4218. if (elementId !== currentId) {
  4219. for (var i = 0; i < eps.length; i++) {
  4220. self.add(eps[i], elementId);
  4221. }
  4222. eps.splice(0, eps.length);
  4223. }
  4224. };
  4225. };
  4226. _currentInstance.anchorManager = new AnchorManager();
  4227. _currentInstance.continuousAnchorFactory = {
  4228. get:function(params) {
  4229. var existing = continuousAnchors[params.elementId];
  4230. if (!existing) {
  4231. existing = {
  4232. type:"Continuous",
  4233. compute : function(params) {
  4234. return continuousAnchorLocations[params.element.id] || [0,0];
  4235. },
  4236. getCurrentLocation : function(endpoint) {
  4237. return continuousAnchorLocations[endpoint.id] || [0,0];
  4238. },
  4239. getOrientation : function(endpoint) {
  4240. return continuousAnchorOrientations[endpoint.id] || [0,0];
  4241. },
  4242. isDynamic : true,
  4243. isContinuous : true
  4244. };
  4245. continuousAnchors[params.elementId] = existing;
  4246. }
  4247. return existing;
  4248. }
  4249. };
  4250. if (!jsPlumbAdapter.headless)
  4251. _currentInstance.dragManager = jsPlumbAdapter.getDragManager(_currentInstance);
  4252. _currentInstance.recalculateOffsets = _currentInstance.dragManager.updateOffsets;
  4253. /*
  4254. * Function: recalculateOffsets
  4255. * Recalculates the offsets of all child elements of some element. If you have Endpoints registered on the
  4256. * descendants of some element and you make changes to that element's markup, it is possible that the location
  4257. * of each Endpooint relative to the origin of the element may have changed. So you call this to tell jsPlumb to
  4258. * recalculate. You need to do this because, for performance reasons, jsplumb won't calculate these offsets on
  4259. * the fly.
  4260. * Parameters:
  4261. * el - either a string id, or a selector.
  4262. */
  4263. /*
  4264. * Class: Connection
  4265. * The connecting line between two Endpoints.
  4266. */
  4267. /*
  4268. * Function: Connection
  4269. * Connection constructor. You should not ever create one of these directly. If you make a call to jsPlumb.connect, all of
  4270. * the parameters that you pass in to that function will be passed to the Connection constructor; if your UI
  4271. * uses the various Endpoint-centric methods like addEndpoint/makeSource/makeTarget, along with drag and drop,
  4272. * then the parameters you set on those functions are translated and passed in to the Connection constructor. So
  4273. * you should check the documentation for each of those methods.
  4274. *
  4275. * Parameters:
  4276. * source - either an element id, a selector for an element, or an Endpoint.
  4277. * target - either an element id, a selector for an element, or an Endpoint
  4278. * scope - scope descriptor for this connection. optional.
  4279. * container - optional id or selector instructing jsPlumb where to attach all the elements it creates for this connection. you should read the documentation for a full discussion of this.
  4280. * detachable - optional, defaults to true. Defines whether or not the connection may be detached using the mouse.
  4281. * reattach - optional, defaults to false. Defines whether not the connection should be retached if it was dragged off an Endpoint and then dropped in whitespace.
  4282. * endpoint - Optional. Endpoint definition to use for both ends of the connection.
  4283. * endpoints - Optional. Array of two Endpoint definitions, one for each end of the Connection. This and 'endpoint' are mutually exclusive parameters.
  4284. * endpointStyle - Optional. Endpoint style definition to use for both ends of the Connection.
  4285. * endpointStyles - Optional. Array of two Endpoint style definitions, one for each end of the Connection. This and 'endpoint' are mutually exclusive parameters.
  4286. * paintStyle - Parameters defining the appearance of the Connection. Optional; jsPlumb will use the defaults if you supply nothing here.
  4287. * hoverPaintStyle - Parameters defining the appearance of the Connection when the mouse is hovering over it. Optional; jsPlumb will use the defaults if you supply nothing here (note that the default hoverPaintStyle is null).
  4288. * cssClass - optional CSS class to set on the display element associated with this Connection.
  4289. * hoverClass - optional CSS class to set on the display element associated with this Connection when it is in hover state.
  4290. * overlays - Optional array of Overlay definitions to appear on this Connection.
  4291. * drawEndpoints - if false, instructs jsPlumb to not draw the endpoints for this Connection. Be careful with this: it only really works when you tell jsPlumb to attach elements to the document body. Read the documentation for a full discussion of this.
  4292. * parameters - Optional JS object containing parameters to set on the Connection. These parameters are then available via the getParameter method.
  4293. */
  4294. var Connection = function(params) {
  4295. var self = this, visible = true, _internalHover, _superClassHover;
  4296. self.idPrefix = "_jsplumb_c_";
  4297. self.defaultLabelLocation = 0.5;
  4298. self.defaultOverlayKeys = ["Overlays", "ConnectionOverlays"];
  4299. this.parent = params.parent;
  4300. overlayCapableJsPlumbUIComponent.apply(this, arguments);
  4301. // ************** get the source and target and register the connection. *******************
  4302. // VISIBILITY
  4303. /*
  4304. * Function: isVisible
  4305. * Returns whether or not the Connection is currently visible.
  4306. */
  4307. this.isVisible = function() { return visible; };
  4308. /*
  4309. * Function: setVisible
  4310. * Sets whether or not the Connection should be visible.
  4311. *
  4312. * Parameters:
  4313. * visible - boolean indicating desired visible state.
  4314. */
  4315. this.setVisible = function(v) {
  4316. visible = v;
  4317. self[v ? "showOverlays" : "hideOverlays"]();
  4318. if (self.connector && self.connector.canvas) self.connector.canvas.style.display = v ? "block" : "none";
  4319. self.repaint();
  4320. };
  4321. // END VISIBILITY
  4322. // TYPE
  4323. this.getTypeDescriptor = function() { return "connection"; };
  4324. this.getDefaultType = function() {
  4325. return {
  4326. parameters:{},
  4327. scope:null,
  4328. detachable:self._jsPlumb.Defaults.ConnectionsDetachable,
  4329. rettach:self._jsPlumb.Defaults.ReattachConnections,
  4330. paintStyle:self._jsPlumb.Defaults.PaintStyle || jsPlumb.Defaults.PaintStyle,
  4331. connector:self._jsPlumb.Defaults.Connector || jsPlumb.Defaults.Connector,
  4332. hoverPaintStyle:self._jsPlumb.Defaults.HoverPaintStyle || jsPlumb.Defaults.HoverPaintStyle,
  4333. overlays:self._jsPlumb.Defaults.ConnectorOverlays || jsPlumb.Defaults.ConnectorOverlays
  4334. };
  4335. };
  4336. var superAt = this.applyType;
  4337. this.applyType = function(t) {
  4338. superAt(t);
  4339. if (t.detachable != null) self.setDetachable(t.detachable);
  4340. if (t.reattach != null) self.setReattach(t.reattach);
  4341. if (t.scope) self.scope = t.scope;
  4342. self.setConnector(t.connector);
  4343. };
  4344. // END TYPE
  4345. // HOVER
  4346. // override setHover to pass it down to the underlying connector
  4347. _superClassHover = self.setHover;
  4348. self.setHover = function(state) {
  4349. var zi = _currentInstance.ConnectorZIndex || jsPlumb.Defaults.ConnectorZIndex;
  4350. if (zi)
  4351. self.connector.setZIndex(zi + (state ? 1 : 0));
  4352. self.connector.setHover.apply(self.connector, arguments);
  4353. _superClassHover.apply(self, arguments);
  4354. };
  4355. _internalHover = function(state) {
  4356. if (_connectionBeingDragged == null) {
  4357. self.setHover(state, false);
  4358. }
  4359. };
  4360. // END HOVER
  4361. /*
  4362. * Function: setConnector
  4363. * Sets the Connection's connector (eg "Bezier", "Flowchart", etc). You pass a Connector definition into this method, the same
  4364. * thing that you would set as the 'connector' property on a jsPlumb.connect call.
  4365. *
  4366. * Parameters:
  4367. * connector - Connector definition
  4368. */
  4369. this.setConnector = function(connector, doNotRepaint) {
  4370. if (self.connector != null) _removeElements(self.connector.getDisplayElements(), self.parent);
  4371. var connectorArgs = { _jsPlumb:self._jsPlumb, parent:params.parent,
  4372. cssClass:params.cssClass, container:params.container, tooltip:self.tooltip
  4373. };
  4374. if (_isString(connector))
  4375. this.connector = new jsPlumb.Connectors[renderMode][connector](connectorArgs); // lets you use a string as shorthand.
  4376. else if (_isArray(connector)) {
  4377. if (connector.length == 1)
  4378. this.connector = new jsPlumb.Connectors[renderMode][connector[0]](connectorArgs);
  4379. else
  4380. this.connector = new jsPlumb.Connectors[renderMode][connector[0]](jsPlumb.extend(connector[1], connectorArgs));
  4381. }
  4382. self.canvas = self.connector.canvas;
  4383. // binds mouse listeners to the current connector.
  4384. _bindListeners(self.connector, self, _internalHover);
  4385. // set z-index if it was set on Defaults.
  4386. var zi = _currentInstance.ConnectorZIndex || jsPlumb.Defaults.ConnectorZIndex;
  4387. if (zi)
  4388. self.connector.setZIndex(zi);
  4389. if (!doNotRepaint) self.repaint();
  4390. };
  4391. // INITIALISATION CODE
  4392. this.source = _getElementObject(params.source);
  4393. this.target = _getElementObject(params.target);
  4394. // sourceEndpoint and targetEndpoint override source/target, if they are present. but
  4395. // source is not overridden if the Endpoint has declared it is not the final target of a connection;
  4396. // instead we use the source that the Endpoint declares will be the final source element.
  4397. if (params.sourceEndpoint) this.source = params.sourceEndpoint.endpointWillMoveTo || params.sourceEndpoint.getElement();
  4398. if (params.targetEndpoint) this.target = params.targetEndpoint.getElement();
  4399. // if a new connection is the result of moving some existing connection, params.previousConnection
  4400. // will have that Connection in it. listeners for the jsPlumbConnection event can look for that
  4401. // member and take action if they need to.
  4402. self.previousConnection = params.previousConnection;
  4403. this.sourceId = _getAttribute(this.source, "id");
  4404. this.targetId = _getAttribute(this.target, "id");
  4405. 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.
  4406. this.endpoints = [];
  4407. this.endpointStyles = [];
  4408. // wrapped the main function to return null if no input given. this lets us cascade defaults properly.
  4409. var _makeAnchor = function(anchorParams, elementId) {
  4410. return (anchorParams) ? _currentInstance.makeAnchor(anchorParams, elementId, _currentInstance) : null;
  4411. },
  4412. prepareEndpoint = function(existing, index, params, element, elementId, connectorPaintStyle, connectorHoverPaintStyle) {
  4413. var e;
  4414. if (existing) {
  4415. self.endpoints[index] = existing;
  4416. existing.addConnection(self);
  4417. } else {
  4418. if (!params.endpoints) params.endpoints = [ null, null ];
  4419. var ep = params.endpoints[index]
  4420. || params.endpoint
  4421. || _currentInstance.Defaults.Endpoints[index]
  4422. || jsPlumb.Defaults.Endpoints[index]
  4423. || _currentInstance.Defaults.Endpoint
  4424. || jsPlumb.Defaults.Endpoint;
  4425. if (!params.endpointStyles) params.endpointStyles = [ null, null ];
  4426. if (!params.endpointHoverStyles) params.endpointHoverStyles = [ null, null ];
  4427. var es = params.endpointStyles[index] || params.endpointStyle || _currentInstance.Defaults.EndpointStyles[index] || jsPlumb.Defaults.EndpointStyles[index] || _currentInstance.Defaults.EndpointStyle || jsPlumb.Defaults.EndpointStyle;
  4428. // Endpoints derive their fillStyle from the connector's strokeStyle, if no fillStyle was specified.
  4429. if (es.fillStyle == null && connectorPaintStyle != null)
  4430. es.fillStyle = connectorPaintStyle.strokeStyle;
  4431. // TODO: decide if the endpoint should derive the connection's outline width and color. currently it does:
  4432. //*
  4433. if (es.outlineColor == null && connectorPaintStyle != null)
  4434. es.outlineColor = connectorPaintStyle.outlineColor;
  4435. if (es.outlineWidth == null && connectorPaintStyle != null)
  4436. es.outlineWidth = connectorPaintStyle.outlineWidth;
  4437. //*/
  4438. var ehs = params.endpointHoverStyles[index] || params.endpointHoverStyle || _currentInstance.Defaults.EndpointHoverStyles[index] || jsPlumb.Defaults.EndpointHoverStyles[index] || _currentInstance.Defaults.EndpointHoverStyle || jsPlumb.Defaults.EndpointHoverStyle;
  4439. // endpoint hover fill style is derived from connector's hover stroke style. TODO: do we want to do this by default? for sure?
  4440. if (connectorHoverPaintStyle != null) {
  4441. if (ehs == null) ehs = {};
  4442. if (ehs.fillStyle == null) {
  4443. ehs.fillStyle = connectorHoverPaintStyle.strokeStyle;
  4444. }
  4445. }
  4446. var a = params.anchors ? params.anchors[index] :
  4447. params.anchor ? params.anchor :
  4448. _makeAnchor(_currentInstance.Defaults.Anchors[index], elementId) ||
  4449. _makeAnchor(jsPlumb.Defaults.Anchors[index], elementId) ||
  4450. _makeAnchor(_currentInstance.Defaults.Anchor, elementId) ||
  4451. _makeAnchor(jsPlumb.Defaults.Anchor, elementId),
  4452. u = params.uuids ? params.uuids[index] : null;
  4453. e = _newEndpoint({
  4454. paintStyle : es, hoverPaintStyle:ehs, endpoint : ep, connections : [ self ],
  4455. uuid : u, anchor : a, source : element, scope : params.scope, container:params.container,
  4456. reattach:params.reattach || _currentInstance.Defaults.ReattachConnections,
  4457. detachable:params.detachable || _currentInstance.Defaults.ConnectionsDetachable
  4458. });
  4459. self.endpoints[index] = e;
  4460. if (params.drawEndpoints === false) e.setVisible(false, true, true);
  4461. }
  4462. return e;
  4463. };
  4464. var eS = prepareEndpoint(params.sourceEndpoint, 0, params, self.source,
  4465. self.sourceId, params.paintStyle, params.hoverPaintStyle);
  4466. if (eS) _addToList(endpointsByElement, this.sourceId, eS);
  4467. var eT = prepareEndpoint(params.targetEndpoint, 1, params, self.target,
  4468. self.targetId, params.paintStyle, params.hoverPaintStyle);
  4469. if (eT) _addToList(endpointsByElement, this.targetId, eT);
  4470. // if scope not set, set it to be the scope for the source endpoint.
  4471. if (!this.scope) this.scope = this.endpoints[0].scope;
  4472. // if delete endpoints on detach, keep a record of just exactly which endpoints they are.
  4473. self.endpointsToDeleteOnDetach = [null, null];
  4474. if (params.deleteEndpointsOnDetach) {
  4475. if (params.sourceIsNew) self.endpointsToDeleteOnDetach[0] = self.endpoints[0];
  4476. if (params.targetIsNew) self.endpointsToDeleteOnDetach[1] = self.endpoints[1];
  4477. }
  4478. self.setConnector(this.endpoints[0].connector ||
  4479. this.endpoints[1].connector ||
  4480. params.connector ||
  4481. _currentInstance.Defaults.Connector ||
  4482. jsPlumb.Defaults.Connector, true);
  4483. this.setPaintStyle(this.endpoints[0].connectorStyle ||
  4484. this.endpoints[1].connectorStyle ||
  4485. params.paintStyle ||
  4486. _currentInstance.Defaults.PaintStyle ||
  4487. jsPlumb.Defaults.PaintStyle, true);
  4488. this.setHoverPaintStyle(this.endpoints[0].connectorHoverStyle ||
  4489. this.endpoints[1].connectorHoverStyle ||
  4490. params.hoverPaintStyle ||
  4491. _currentInstance.Defaults.HoverPaintStyle ||
  4492. jsPlumb.Defaults.HoverPaintStyle, true);
  4493. this.paintStyleInUse = this.getPaintStyle();
  4494. _updateOffset( { elId : this.sourceId, timestamp:_suspendedAt });
  4495. _updateOffset( { elId : this.targetId, timestamp:_suspendedAt });
  4496. // paint the endpoints
  4497. var myOffset = offsets[this.sourceId], myWH = sizes[this.sourceId],
  4498. otherOffset = offsets[this.targetId],
  4499. otherWH = sizes[this.targetId],
  4500. initialTimestamp = _suspendedAt || _timestamp(),
  4501. anchorLoc = this.endpoints[0].anchor.compute( {
  4502. xy : [ myOffset.left, myOffset.top ], wh : myWH, element : this.endpoints[0],
  4503. elementId:this.endpoints[0].elementId,
  4504. txy : [ otherOffset.left, otherOffset.top ], twh : otherWH, tElement : this.endpoints[1],
  4505. timestamp:initialTimestamp
  4506. });
  4507. this.endpoints[0].paint( { anchorLoc : anchorLoc, timestamp:initialTimestamp });
  4508. anchorLoc = this.endpoints[1].anchor.compute( {
  4509. xy : [ otherOffset.left, otherOffset.top ], wh : otherWH, element : this.endpoints[1],
  4510. elementId:this.endpoints[1].elementId,
  4511. txy : [ myOffset.left, myOffset.top ], twh : myWH, tElement : this.endpoints[0],
  4512. timestamp:initialTimestamp
  4513. });
  4514. this.endpoints[1].paint({ anchorLoc : anchorLoc, timestamp:initialTimestamp });
  4515. // END INITIALISATION CODE
  4516. // DETACHABLE
  4517. var _detachable = _currentInstance.Defaults.ConnectionsDetachable;
  4518. if (params.detachable === false) _detachable = false;
  4519. if(self.endpoints[0].connectionsDetachable === false) _detachable = false;
  4520. if(self.endpoints[1].connectionsDetachable === false) _detachable = false;
  4521. /*
  4522. * Function: isDetachable
  4523. * Returns whether or not this connection can be detached from its target/source endpoint. by default this
  4524. * is false; use it in conjunction with the 'reattach' parameter.
  4525. */
  4526. this.isDetachable = function() {
  4527. return _detachable === true;
  4528. };
  4529. /*
  4530. * Function: setDetachable
  4531. * Sets whether or not this connection is detachable.
  4532. *
  4533. * Parameters:
  4534. * detachable - whether or not to set the Connection to be detachable.
  4535. */
  4536. this.setDetachable = function(detachable) {
  4537. _detachable = detachable === true;
  4538. };
  4539. // END DETACHABLE
  4540. // REATTACH
  4541. var _reattach = params.reattach ||
  4542. self.endpoints[0].reattachConnections ||
  4543. self.endpoints[1].reattachConnections ||
  4544. _currentInstance.Defaults.ReattachConnections;
  4545. /*
  4546. * Function: isReattach
  4547. * Returns whether or not this connection will be reattached after having been detached via the mouse and dropped. by default this
  4548. * is false; use it in conjunction with the 'detachable' parameter.
  4549. */
  4550. this.isReattach = function() {
  4551. return _reattach === true;
  4552. };
  4553. /*
  4554. * Function: setReattach
  4555. * Sets whether or not this connection will reattach after having been detached via the mouse and dropped.
  4556. *
  4557. * Parameters:
  4558. * reattach - whether or not to set the Connection to reattach after drop in whitespace.
  4559. */
  4560. this.setReattach = function(reattach) {
  4561. _reattach = reattach === true;
  4562. };
  4563. // END REATTACH
  4564. // COST + DIRECTIONALITY
  4565. // if cost not supplied, try to inherit from source endpoint
  4566. var _cost = params.cost || self.endpoints[0].getConnectionCost();
  4567. self.getCost = function() { return _cost; };
  4568. self.setCost = function(c) { _cost = c; };
  4569. var _bidirectional = !(params.bidirectional === false);
  4570. // inherit bidirectional flag if set no source endpoint
  4571. if (params.bidirectional == null) _bidirectional = self.endpoints[0].areConnectionsBidirectional();
  4572. self.isBidirectional = function() { return _bidirectional; };
  4573. // END COST + DIRECTIONALITY
  4574. // PARAMETERS
  4575. // merge all the parameters objects into the connection. parameters set
  4576. // on the connection take precedence; then target endpoint params, then
  4577. // finally source endpoint params.
  4578. // TODO jsPlumb.extend could be made to take more than two args, and it would
  4579. // apply the second through nth args in order.
  4580. var _p = jsPlumb.extend({}, this.endpoints[0].getParameters());
  4581. jsPlumb.extend(_p, this.endpoints[1].getParameters());
  4582. jsPlumb.extend(_p, self.getParameters());
  4583. self.setParameters(_p);
  4584. // END PARAMETERS
  4585. // MISCELLANEOUS
  4586. /**
  4587. * implementation of abstract method in jsPlumbUtil.EventGenerator
  4588. * @return list of attached elements. in our case, a list of Endpoints.
  4589. */
  4590. this.getAttachedElements = function() {
  4591. return self.endpoints;
  4592. };
  4593. /**
  4594. * changes the parent element of this connection to newParent. not exposed for the public API.
  4595. */
  4596. this.moveParent = function(newParent) {
  4597. var jpcl = jsPlumb.CurrentLibrary, curParent = jpcl.getParent(self.connector.canvas);
  4598. if (self.connector.bgCanvas) {
  4599. jpcl.removeElement(self.connector.bgCanvas, curParent);
  4600. jpcl.appendElement(self.connector.bgCanvas, newParent);
  4601. }
  4602. jpcl.removeElement(self.connector.canvas, curParent);
  4603. jpcl.appendElement(self.connector.canvas, newParent);
  4604. // this only applies for DOMOverlays
  4605. for (var i = 0; i < self.overlays.length; i++) {
  4606. if (self.overlays[i].isAppendedAtTopLevel) {
  4607. jpcl.removeElement(self.overlays[i].canvas, curParent);
  4608. jpcl.appendElement(self.overlays[i].canvas, newParent);
  4609. if (self.overlays[i].reattachListeners)
  4610. self.overlays[i].reattachListeners(self.connector);
  4611. }
  4612. }
  4613. if (self.connector.reattachListeners) // this is for SVG/VML; change an element's parent and you have to reinit its listeners.
  4614. self.connector.reattachListeners(); // the Canvas implementation doesn't have to care about this
  4615. };
  4616. // END MISCELLANEOUS
  4617. // PAINTING
  4618. /*
  4619. * Paints the Connection. Not exposed for public usage.
  4620. *
  4621. * Parameters:
  4622. * elId - Id of the element that is in motion.
  4623. * ui - current library's event system ui object (present if we came from a drag to get here).
  4624. * recalc - whether or not to recalculate all anchors etc before painting.
  4625. * timestamp - timestamp of this paint. If the Connection was last painted with the same timestamp, it does not paint again.
  4626. */
  4627. var lastPaintedAt = null;
  4628. this.paint = function(params) {
  4629. if (visible) {
  4630. params = params || {};
  4631. var elId = params.elId, ui = params.ui, recalc = params.recalc, timestamp = params.timestamp,
  4632. // if the moving object is not the source we must transpose the two references.
  4633. swap = false,
  4634. tId = swap ? this.sourceId : this.targetId, sId = swap ? this.targetId : this.sourceId,
  4635. tIdx = swap ? 0 : 1, sIdx = swap ? 1 : 0;
  4636. if (timestamp == null || timestamp != lastPaintedAt) {
  4637. var sourceInfo = _updateOffset( { elId : elId, offset : ui, recalc : recalc, timestamp : timestamp }),
  4638. targetInfo = _updateOffset( { elId : tId, timestamp : timestamp }); // update the target if this is a forced repaint. otherwise, only the source has been moved.
  4639. var sE = this.endpoints[sIdx], tE = this.endpoints[tIdx],
  4640. sAnchorP = sE.anchor.getCurrentLocation(sE),
  4641. tAnchorP = tE.anchor.getCurrentLocation(tE);
  4642. // find largest overlay; we use it to ensure sufficient padding in the connector canvas.
  4643. var maxSize = 0;
  4644. for ( var i = 0; i < self.overlays.length; i++) {
  4645. var o = self.overlays[i];
  4646. if (o.isVisible()) maxSize = Math.max(maxSize, o.computeMaxSize());
  4647. }
  4648. var dim = this.connector.compute(
  4649. sAnchorP,
  4650. tAnchorP,
  4651. this.endpoints[sIdx],
  4652. this.endpoints[tIdx],
  4653. this.endpoints[sIdx].anchor,
  4654. this.endpoints[tIdx].anchor,
  4655. self.paintStyleInUse.lineWidth,
  4656. maxSize,
  4657. sourceInfo,
  4658. targetInfo );
  4659. self.connector.paint(dim, self.paintStyleInUse);
  4660. // paint overlays
  4661. for ( var i = 0; i < self.overlays.length; i++) {
  4662. var o = self.overlays[i];
  4663. if (o.isVisible) {
  4664. self.overlayPlacements[i] = o.draw(self.connector, self.paintStyleInUse, dim);
  4665. }
  4666. }
  4667. }
  4668. lastPaintedAt = timestamp;
  4669. }
  4670. };
  4671. /*
  4672. * Function: repaint
  4673. * Repaints the Connection. No parameters exposed to public API.
  4674. */
  4675. this.repaint = function(params) {
  4676. params = params || {};
  4677. var recalc = !(params.recalc === false);
  4678. this.paint({ elId : this.sourceId, recalc : recalc, timestamp:params.timestamp });
  4679. };
  4680. // the very last thing we do is check to see if a 'type' was supplied in the params
  4681. var _type = params.type || self.endpoints[0].connectionType || self.endpoints[1].connectionType;
  4682. if (_type)
  4683. self.addType(_type);
  4684. // END PAINTING
  4685. // ***************************** PLACEHOLDERS FOR NATURAL DOCS *************************************************
  4686. /*
  4687. * Function: bind
  4688. * Bind to an event on the Connection.
  4689. *
  4690. * Parameters:
  4691. * event - the event to bind. Available events on a Connection are:
  4692. * - *click* : notification that a Connection was clicked.
  4693. * - *dblclick* : notification that a Connection was double clicked.
  4694. * - *mouseenter* : notification that the mouse is over a Connection.
  4695. * - *mouseexit* : notification that the mouse exited a Connection.
  4696. * - *contextmenu* : notification that the user right-clicked on the Connection.
  4697. *
  4698. * callback - function to callback. This function will be passed the Connection that caused the event, and also the original event.
  4699. */
  4700. /*
  4701. * Function: setPaintStyle
  4702. * Sets the Connection's paint style and then repaints the Connection.
  4703. *
  4704. * Parameters:
  4705. * style - Style to use.
  4706. */
  4707. /*
  4708. * Function: getPaintStyle
  4709. * Gets the Connection's paint style. This is not necessarily the paint style in use at the time;
  4710. * this is the paint style for the connection when the mouse it not hovering over it.
  4711. */
  4712. /*
  4713. * Function: setHoverPaintStyle
  4714. * Sets the paint style to use when the mouse is hovering over the Connection. This is null by default.
  4715. * The hover paint style is applied as extensions to the paintStyle; it does not entirely replace
  4716. * it. This is because people will most likely want to change just one thing when hovering, say the
  4717. * color for example, but leave the rest of the appearance the same.
  4718. *
  4719. * Parameters:
  4720. * style - Style to use when the mouse is hovering.
  4721. * doNotRepaint - if true, the Connection will not be repainted. useful when setting things up initially.
  4722. */
  4723. /*
  4724. * Function: setHover
  4725. * Sets/unsets the hover state of this Connection.
  4726. *
  4727. * Parameters:
  4728. * hover - hover state boolean
  4729. * ignoreAttachedElements - if true, does not notify any attached elements of the change in hover state. used mostly to avoid infinite loops.
  4730. */
  4731. /*
  4732. * Function: getParameter
  4733. * Gets the named parameter; returns null if no parameter with that name is set. Parameter values may have been supplied to a 'connect' or 'addEndpoint' call (connections made with the mouse get a copy of all parameters set on each of their Endpoints), or the parameter value may have been set with setParameter.
  4734. *
  4735. * Parameters:
  4736. * key - Parameter name.
  4737. */
  4738. /*
  4739. * Function: setParameter
  4740. * Sets the named parameter to the given value.
  4741. *
  4742. * Parameters:
  4743. * key - Parameter name.
  4744. * value - Parameter value.
  4745. */
  4746. /*
  4747. * Property: connector
  4748. * The underlying Connector for this Connection (eg. a Bezier connector, straight line connector, flowchart connector etc)
  4749. */
  4750. /*
  4751. * Property: sourceId
  4752. * Id of the source element in the connection.
  4753. */
  4754. /*
  4755. * Property: targetId
  4756. * Id of the target element in the connection.
  4757. */
  4758. /*
  4759. * Property: scope
  4760. * Optional scope descriptor for the connection.
  4761. */
  4762. /*
  4763. * Property: endpoints
  4764. * Array of [source, target] Endpoint objects.
  4765. */
  4766. /*
  4767. * Property: source
  4768. * The source element for this Connection.
  4769. */
  4770. /*
  4771. * Property: target
  4772. * The target element for this Connection.
  4773. */
  4774. /*
  4775. * Property: overlays
  4776. * List of Overlays for this component.
  4777. */
  4778. /*
  4779. * Function: addOverlay
  4780. * Adds an Overlay to the Connection.
  4781. *
  4782. * Parameters:
  4783. * overlay - Overlay to add.
  4784. */
  4785. /*
  4786. * Function: getOverlay
  4787. * Gets an overlay, by ID. Note: by ID. You would pass an 'id' parameter
  4788. * in to the Overlay's constructor arguments, and then use that to retrieve
  4789. * it via this method.
  4790. *
  4791. * Parameters:
  4792. * overlayId - id of the overlay to retrieve.
  4793. */
  4794. /*
  4795. * Function: getOverlays
  4796. * Gets all the overlays for this component.
  4797. */
  4798. /*
  4799. * Function: hideOverlay
  4800. * Hides the overlay specified by the given id.
  4801. *
  4802. * Parameters:
  4803. * overlayId - id of the overlay to hide.
  4804. */
  4805. /*
  4806. * Function: hideOverlays
  4807. * Hides all Overlays
  4808. */
  4809. /*
  4810. * Function: showOverlay
  4811. * Shows the overlay specified by the given id.
  4812. *
  4813. * Parameters:
  4814. * overlayId - id of the overlay to show.
  4815. */
  4816. /*
  4817. * Function: showOverlays
  4818. * Shows all Overlays
  4819. */
  4820. /**
  4821. * Function: removeAllOverlays
  4822. * Removes all overlays from the Connection, and then repaints.
  4823. */
  4824. /**
  4825. * Function: removeOverlay
  4826. * Removes an overlay by ID. Note: by ID. this is a string you set in the overlay spec.
  4827. *
  4828. * Parameters:
  4829. * overlayId - id of the overlay to remove.
  4830. */
  4831. /**
  4832. * Function: removeOverlays
  4833. * Removes a set of overlays by ID. Note: by ID. this is a string you set in the overlay spec.
  4834. *
  4835. * Parameters:
  4836. * overlayIds - this function takes an arbitrary number of arguments, each of which is a single overlay id.
  4837. */
  4838. /*
  4839. * Function: setLabel
  4840. * Sets the Connection's label.
  4841. *
  4842. * Parameters:
  4843. * l - label to set. May be a String, a Function that returns a String, or a params object containing { "label", "labelStyle", "location", "cssClass" }. Note that this uses innerHTML on the label div, so keep
  4844. * that in mind if you need escaped HTML.
  4845. */
  4846. /*
  4847. * Function: getLabel
  4848. * Returns the label text for this Connection (or a function if you are labelling with a function).
  4849. *
  4850. * This does not return the overlay itself; this is a convenience method which is a pair with
  4851. * setLabel; together they allow you to add and access a Label Overlay without having to create the
  4852. * Overlay object itself. For access to the underlying label overlay that jsPlumb has created,
  4853. * use getLabelOverlay.
  4854. *
  4855. * See Also:
  4856. * <getOverlay>
  4857. */
  4858. /*
  4859. * Function: getLabelOverlay
  4860. * Returns the underlying internal label overlay, which will exist if you specified a label on
  4861. * a connect call, or have called setLabel at any stage.
  4862. */
  4863. // ***************************** END OF PLACEHOLDERS FOR NATURAL DOCS *************************************************
  4864. }; // END Connection class
  4865. // ENDPOINT HELPER FUNCTIONS
  4866. var _makeConnectionDragHandler = function(placeholder) {
  4867. var stopped = false;
  4868. return {
  4869. drag : function() {
  4870. if (stopped) {
  4871. stopped = false;
  4872. return true;
  4873. }
  4874. var _ui = jsPlumb.CurrentLibrary.getUIPosition(arguments, _currentInstance.getZoom()),
  4875. el = placeholder.element;
  4876. if (el) {
  4877. jsPlumb.CurrentLibrary.setOffset(el, _ui);
  4878. _draw(_getElementObject(el), _ui);
  4879. }
  4880. },
  4881. stopDrag : function() {
  4882. stopped = true;
  4883. }
  4884. };
  4885. };
  4886. var _makeFloatingEndpoint = function(paintStyle, referenceAnchor, endpoint, referenceCanvas, sourceElement) {
  4887. var floatingAnchor = new FloatingAnchor( { reference : referenceAnchor, referenceCanvas : referenceCanvas });
  4888. //setting the scope here should not be the way to fix that mootools issue. it should be fixed by not
  4889. // adding the floating endpoint as a droppable. that makes more sense anyway!
  4890. return _newEndpoint({ paintStyle : paintStyle, endpoint : endpoint, anchor : floatingAnchor, source : sourceElement, scope:"__floating" });
  4891. };
  4892. /**
  4893. * creates a placeholder div for dragging purposes, adds it to the DOM, and pre-computes its offset.
  4894. */
  4895. var _makeDraggablePlaceholder = function(placeholder, parent) {
  4896. var n = document.createElement("div");
  4897. n.style.position = "absolute";
  4898. var placeholderDragElement = _getElementObject(n);
  4899. _appendElement(n, parent);
  4900. var id = _getId(placeholderDragElement);
  4901. _updateOffset( { elId : id });
  4902. // create and assign an id, and initialize the offset.
  4903. placeholder.id = id;
  4904. placeholder.element = placeholderDragElement;
  4905. };
  4906. /*
  4907. * Class: Endpoint
  4908. *
  4909. * Models an endpoint. Can have 1 to 'maxConnections' Connections emanating from it (set maxConnections to -1
  4910. * to allow unlimited). Typically, if you use 'jsPlumb.connect' to programmatically connect two elements, you won't
  4911. * actually deal with the underlying Endpoint objects. But if you wish to support drag and drop Connections, one of the ways you
  4912. * do so is by creating and registering Endpoints using 'jsPlumb.addEndpoint', and marking these Endpoints as 'source' and/or
  4913. * 'target' Endpoints for Connections.
  4914. *
  4915. *
  4916. */
  4917. /*
  4918. * Function: Endpoint
  4919. *
  4920. * Endpoint constructor.
  4921. *
  4922. * Parameters:
  4923. * anchor - definition of the Anchor for the endpoint. You can include one or more Anchor definitions here; if you include more than one, jsPlumb creates a 'dynamic' Anchor, ie. an Anchor which changes position relative to the other elements in a Connection. Each Anchor definition can be either a string nominating one of the basic Anchors provided by jsPlumb (eg. "TopCenter"), or a four element array that designates the Anchor's location and orientation (eg, and this is equivalent to TopCenter, [ 0.5, 0, 0, -1 ]). To provide more than one Anchor definition just put them all in an array. You can mix string definitions with array definitions.
  4924. * endpoint - optional Endpoint definition. This takes the form of either a string nominating one of the basic Endpoints provided by jsPlumb (eg. "Rectangle"), or an array containing [name,params] for those cases where you don't wish to use the default values, eg. [ "Rectangle", { width:5, height:10 } ].
  4925. * enabled - optional, defaults to true. Indicates whether or not the Endpoint should be enabled for mouse events (drag/drop).
  4926. * paintStyle - endpoint style, a js object. may be null.
  4927. * hoverPaintStyle - style to use when the mouse is hovering over the Endpoint. A js object. may be null; defaults to null.
  4928. * cssClass - optional CSS class to set on the display element associated with this Endpoint.
  4929. * hoverClass - optional CSS class to set on the display element associated with this Endpoint when it is in hover state.
  4930. * source - element the Endpoint is attached to, of type String (an element id) or element selector. Required.
  4931. * canvas - canvas element to use. may be, and most often is, null.
  4932. * container - optional id or selector instructing jsPlumb where to attach the element it creates for this endpoint. you should read the documentation for a full discussion of this.
  4933. * connections - optional list of Connections to configure the Endpoint with.
  4934. * isSource - boolean. indicates the endpoint can act as a source of new connections. Optional; defaults to false.
  4935. * maxConnections - integer; defaults to 1. a value of -1 means no upper limit.
  4936. * dragOptions - if isSource is set to true, you can supply arguments for the underlying library's drag method. Optional; defaults to null.
  4937. * connectorStyle - if isSource is set to true, this is the paint style for Connections from this Endpoint. Optional; defaults to null.
  4938. * connectorHoverStyle - if isSource is set to true, this is the hover paint style for Connections from this Endpoint. Optional; defaults to null.
  4939. * connector - optional Connector type to use. Like 'endpoint', this may be either a single string nominating a known Connector type (eg. "Bezier", "Straight"), or an array containing [name, params], eg. [ "Bezier", { curviness:160 } ].
  4940. * connectorOverlays - optional array of Overlay definitions that will be applied to any Connection from this Endpoint.
  4941. * connectorClass - optional CSS class to set on Connections emanating from this Endpoint.
  4942. * connectorHoverClass - optional CSS class to set on to set on Connections emanating from this Endpoint when they are in hover state.
  4943. * connectionsDetachable - optional, defaults to true. Sets whether connections to/from this Endpoint should be detachable or not.
  4944. * isTarget - boolean. indicates the endpoint can act as a target of new connections. Optional; defaults to false.
  4945. * dropOptions - if isTarget is set to true, you can supply arguments for the underlying library's drop method with this parameter. Optional; defaults to null.
  4946. * reattach - optional boolean that determines whether or not the Connections reattach after they have been dragged off an Endpoint and left floating. defaults to false: Connections dropped in this way will just be deleted.
  4947. * parameters - Optional JS object containing parameters to set on the Endpoint. These parameters are then available via the getParameter method. When a connection is made involving this Endpoint, the parameters from this Endpoint are copied into that Connection. Source Endpoint parameters override target Endpoint parameters if they both have a parameter with the same name.
  4948. */
  4949. var Endpoint = function(params) {
  4950. var self = this;
  4951. self.idPrefix = "_jsplumb_e_";
  4952. self.defaultLabelLocation = [ 0.5, 0.5 ];
  4953. self.defaultOverlayKeys = ["Overlays", "EndpointOverlays"];
  4954. this.parent = params.parent;
  4955. overlayCapableJsPlumbUIComponent.apply(this, arguments);
  4956. params = params || {};
  4957. // TYPE
  4958. this.getTypeDescriptor = function() { return "endpoint"; };
  4959. this.getDefaultType = function() {
  4960. return {
  4961. parameters:{},
  4962. scope:null,
  4963. maxConnections:self._jsPlumb.Defaults.MaxConnections,
  4964. paintStyle:self._jsPlumb.Defaults.EndpointStyle || jsPlumb.Defaults.EndpointStyle,
  4965. endpoint:self._jsPlumb.Defaults.Endpoint || jsPlumb.Defaults.Endpoint,
  4966. hoverPaintStyle:self._jsPlumb.Defaults.EndpointHoverStyle || jsPlumb.Defaults.EndpointHoverStyle,
  4967. overlays:self._jsPlumb.Defaults.EndpointOverlays || jsPlumb.Defaults.EndpointOverlays,
  4968. connectorStyle:params.connectorStyle,
  4969. connectorHoverStyle:params.connectorHoverStyle,
  4970. connectorClass:params.connectorClass,
  4971. connectorHoverClass:params.connectorHoverClass,
  4972. connectorOverlays:params.connectorOverlays,
  4973. connector:params.connector,
  4974. connectorTooltip:params.connectorTooltip
  4975. };
  4976. };
  4977. var superAt = this.applyType;
  4978. this.applyType = function(t) {
  4979. superAt(t);
  4980. if (t.maxConnections != null) _maxConnections = t.maxConnections;
  4981. if (t.scope) self.scope = t.scope;
  4982. self.connectorStyle = t.connectorStyle;
  4983. self.connectorHoverStyle = t.connectorHoverStyle;
  4984. self.connectorOverlays = t.connectorOverlays;
  4985. self.connector = t.connector;
  4986. self.connectorTooltip = t.connectorTooltip;
  4987. self.connectionType = t.connectionType;
  4988. self.connectorClass = t.connectorClass;
  4989. self.connectorHoverClass = t.connectorHoverClass;
  4990. };
  4991. // END TYPE
  4992. var visible = true, __enabled = !(params.enabled === false);
  4993. /*
  4994. Function: isVisible
  4995. Returns whether or not the Endpoint is currently visible.
  4996. */
  4997. this.isVisible = function() { return visible; };
  4998. /*
  4999. Function: setVisible
  5000. Sets whether or not the Endpoint is currently visible.
  5001. Parameters:
  5002. visible - whether or not the Endpoint should be visible.
  5003. doNotChangeConnections - Instructs jsPlumb to not pass the visible state on to any attached Connections. defaults to false.
  5004. doNotNotifyOtherEndpoint - Instructs jsPlumb to not pass the visible state on to Endpoints at the other end of any attached Connections. defaults to false.
  5005. */
  5006. this.setVisible = function(v, doNotChangeConnections, doNotNotifyOtherEndpoint) {
  5007. visible = v;
  5008. if (self.canvas) self.canvas.style.display = v ? "block" : "none";
  5009. self[v ? "showOverlays" : "hideOverlays"]();
  5010. if (!doNotChangeConnections) {
  5011. for (var i = 0; i < self.connections.length; i++) {
  5012. self.connections[i].setVisible(v);
  5013. if (!doNotNotifyOtherEndpoint) {
  5014. var oIdx = self === self.connections[i].endpoints[0] ? 1 : 0;
  5015. // only change the other endpoint if this is its only connection.
  5016. if (self.connections[i].endpoints[oIdx].connections.length == 1) self.connections[i].endpoints[oIdx].setVisible(v, true, true);
  5017. }
  5018. }
  5019. }
  5020. };
  5021. /*
  5022. Function: isEnabled
  5023. Returns whether or not the Endpoint is enabled for drag/drop connections.
  5024. */
  5025. this.isEnabled = function() { return __enabled; };
  5026. /*
  5027. Function: setEnabled
  5028. Sets whether or not the Endpoint is enabled for drag/drop connections.
  5029. Parameters:
  5030. enabled - whether or not the Endpoint is enabled.
  5031. */
  5032. this.setEnabled = function(e) { __enabled = e; };
  5033. var _element = params.source, _uuid = params.uuid, floatingEndpoint = null, inPlaceCopy = null;
  5034. if (_uuid) endpointsByUUID[_uuid] = self;
  5035. var _elementId = _getAttribute(_element, "id");
  5036. this.elementId = _elementId;
  5037. this.element = _element;
  5038. var _connectionCost = params.connectionCost;
  5039. this.getConnectionCost = function() { return _connectionCost; };
  5040. this.setConnectionCost = function(c) {
  5041. _connectionCost = c;
  5042. };
  5043. var _connectionsBidirectional = params.connectionsBidirectional === false ? false : true;
  5044. this.areConnectionsBidirectional = function() { return _connectionsBidirectional; };
  5045. this.setConnectionsBidirectional = function(b) { _connectionsBidirectional = b; };
  5046. self.anchor = params.anchor ? _currentInstance.makeAnchor(params.anchor, _elementId, _currentInstance) : params.anchors ? _currentInstance.makeAnchor(params.anchors, _elementId, _currentInstance) : _currentInstance.makeAnchor(_currentInstance.Defaults.Anchor || "TopCenter", _elementId, _currentInstance);
  5047. // ANCHOR MANAGER
  5048. 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.
  5049. _currentInstance.anchorManager.add(self, _elementId);
  5050. var _endpoint = null, originalEndpoint = null;
  5051. this.setEndpoint = function(ep) {
  5052. var endpointArgs = {
  5053. _jsPlumb:self._jsPlumb,
  5054. parent:params.parent,
  5055. container:params.container,
  5056. tooltip:params.tooltip,
  5057. connectorTooltip:params.connectorTooltip,
  5058. endpoint:self
  5059. };
  5060. if (_isString(ep))
  5061. _endpoint = new jsPlumb.Endpoints[renderMode][ep](endpointArgs);
  5062. else if (_isArray(ep)) {
  5063. endpointArgs = jsPlumb.extend(ep[1], endpointArgs);
  5064. _endpoint = new jsPlumb.Endpoints[renderMode][ep[0]](endpointArgs);
  5065. }
  5066. else {
  5067. _endpoint = ep.clone();
  5068. }
  5069. // assign a clone function using a copy of endpointArgs. this is used when a drag starts: the endpoint that was dragged is cloned,
  5070. // and the clone is left in its place while the original one goes off on a magical journey.
  5071. // the copy is to get around a closure problem, in which endpointArgs ends up getting shared by
  5072. // the whole world.
  5073. var argsForClone = jsPlumb.extend({}, endpointArgs);
  5074. _endpoint.clone = function() {
  5075. var o = new Object();
  5076. _endpoint.constructor.apply(o, [argsForClone]);
  5077. return o;
  5078. };
  5079. self.endpoint = _endpoint;
  5080. self.type = self.endpoint.type;
  5081. };
  5082. this.setEndpoint(params.endpoint || _currentInstance.Defaults.Endpoint || jsPlumb.Defaults.Endpoint || "Dot");
  5083. originalEndpoint = _endpoint;
  5084. // override setHover to pass it down to the underlying endpoint
  5085. var _sh = self.setHover;
  5086. self.setHover = function() {
  5087. self.endpoint.setHover.apply(self.endpoint, arguments);
  5088. _sh.apply(self, arguments);
  5089. };
  5090. // endpoint delegates to first connection for hover, if there is one.
  5091. var internalHover = function(state) {
  5092. if (self.connections.length > 0)
  5093. self.connections[0].setHover(state, false);
  5094. else
  5095. self.setHover(state);
  5096. };
  5097. // bind listeners from endpoint to self, with the internal hover function defined above.
  5098. _bindListeners(self.endpoint, self, internalHover);
  5099. this.setPaintStyle(params.paintStyle ||
  5100. params.style ||
  5101. _currentInstance.Defaults.EndpointStyle ||
  5102. jsPlumb.Defaults.EndpointStyle, true);
  5103. this.setHoverPaintStyle(params.hoverPaintStyle ||
  5104. _currentInstance.Defaults.EndpointHoverStyle ||
  5105. jsPlumb.Defaults.EndpointHoverStyle, true);
  5106. this.paintStyleInUse = this.getPaintStyle();
  5107. var originalPaintStyle = this.getPaintStyle();
  5108. this.connectorStyle = params.connectorStyle;
  5109. this.connectorHoverStyle = params.connectorHoverStyle;
  5110. this.connectorOverlays = params.connectorOverlays;
  5111. this.connector = params.connector;
  5112. this.connectorTooltip = params.connectorTooltip;
  5113. this.connectorClass = params.connectorClass;
  5114. this.connectorHoverClass = params.connectorHoverClass;
  5115. this.isSource = params.isSource || false;
  5116. this.isTarget = params.isTarget || false;
  5117. var _maxConnections = params.maxConnections || _currentInstance.Defaults.MaxConnections; // maximum number of connections this endpoint can be the source of.
  5118. this.getAttachedElements = function() {
  5119. return self.connections;
  5120. };
  5121. this.canvas = this.endpoint.canvas;
  5122. this.connections = params.connections || [];
  5123. this.scope = params.scope || DEFAULT_SCOPE;
  5124. this.connectionType = params.connectionType;
  5125. this.timestamp = null;
  5126. //self.isReattach = params.reattach || false;
  5127. self.reattachConnections = params.reattach || _currentInstance.Defaults.ReattachConnections;
  5128. //self.isReattach = params.reattach || false;
  5129. self.connectionsDetachable = _currentInstance.Defaults.ConnectionsDetachable;
  5130. if (params.connectionsDetachable === false || params.detachable === false)
  5131. self.connectionsDetachable = false;
  5132. var dragAllowedWhenFull = params.dragAllowedWhenFull || true;
  5133. if (params.onMaxConnections)
  5134. self.bind("maxConnections", params.onMaxConnections);
  5135. this.computeAnchor = function(params) {
  5136. return self.anchor.compute(params);
  5137. };
  5138. /*
  5139. * Function: addConnection
  5140. * Adds a Connection to this Endpoint.
  5141. *
  5142. * Parameters:
  5143. * connection - the Connection to add.
  5144. */
  5145. this.addConnection = function(connection) {
  5146. self.connections.push(connection);
  5147. };
  5148. /*
  5149. * Function: detach
  5150. * Detaches the given Connection from this Endpoint.
  5151. *
  5152. * Parameters:
  5153. * connection - the Connection to detach.
  5154. * ignoreTarget - optional; tells the Endpoint to not notify the Connection target that the Connection was detached. The default behaviour is to notify the target.
  5155. */
  5156. this.detach = function(connection, ignoreTarget, forceDetach, fireEvent, originalEvent) {
  5157. var idx = _findWithFunction(self.connections, function(c) { return c.id == connection.id}),
  5158. actuallyDetached = false;
  5159. fireEvent = (fireEvent !== false);
  5160. if (idx >= 0) {
  5161. // 1. does the connection have a before detach (note this also checks jsPlumb's bound
  5162. // detach handlers; but then Endpoint's check will, too, hmm.)
  5163. if (forceDetach || connection._forceDetach || connection.isDetachable() || connection.isDetachAllowed(connection)) {
  5164. // get the target endpoint
  5165. var t = connection.endpoints[0] == self ? connection.endpoints[1] : connection.endpoints[0];
  5166. // it would be nice to check with both endpoints that it is ok to detach. but
  5167. // for this we'll have to get a bit fancier: right now if you use the same beforeDetach
  5168. // interceptor for two endpoints (which is kind of common, because it's part of the
  5169. // endpoint definition), then it gets fired twice. so in fact we need to loop through
  5170. // each beforeDetach and see if it returns false, at which point we exit. but if it
  5171. // returns true, we have to check the next one. however we need to track which ones
  5172. // have already been run, and not run them again.
  5173. if (forceDetach || connection._forceDetach || (self.isDetachAllowed(connection) /*&& t.isDetachAllowed(connection)*/)) {
  5174. self.connections.splice(idx, 1);
  5175. // this avoids a circular loop
  5176. if (!ignoreTarget) {
  5177. t.detach(connection, true, forceDetach);
  5178. // check connection to see if we want to delete the endpoints associated with it.
  5179. // we only detach those that have just this connection; this scenario is most
  5180. // likely if we got to this bit of code because it is set by the methods that
  5181. // create their own endpoints, like .connect or .makeTarget. the user is
  5182. // not likely to have interacted with those endpoints.
  5183. if (connection.endpointsToDeleteOnDetach){
  5184. for (var i = 0; i < connection.endpointsToDeleteOnDetach.length; i++) {
  5185. var cde = connection.endpointsToDeleteOnDetach[i];
  5186. if (cde && cde.connections.length == 0)
  5187. _currentInstance.deleteEndpoint(cde);
  5188. }
  5189. }
  5190. }
  5191. _removeElements(connection.connector.getDisplayElements(), connection.parent);
  5192. _removeWithFunction(connectionsByScope[connection.scope], function(c) {
  5193. return c.id == connection.id;
  5194. });
  5195. actuallyDetached = true;
  5196. var doFireEvent = (!ignoreTarget && fireEvent)
  5197. fireDetachEvent(connection, doFireEvent, originalEvent);
  5198. }
  5199. }
  5200. }
  5201. return actuallyDetached;
  5202. };
  5203. /*
  5204. * Function: detachAll
  5205. * Detaches all Connections this Endpoint has.
  5206. *
  5207. * Parameters:
  5208. * fireEvent - whether or not to fire the detach event. defaults to false.
  5209. */
  5210. this.detachAll = function(fireEvent, originalEvent) {
  5211. while (self.connections.length > 0) {
  5212. self.detach(self.connections[0], false, true, fireEvent, originalEvent);
  5213. }
  5214. };
  5215. /*
  5216. * Function: detachFrom
  5217. * Removes any connections from this Endpoint that are connected to the given target endpoint.
  5218. *
  5219. * Parameters:
  5220. * targetEndpoint - Endpoint from which to detach all Connections from this Endpoint.
  5221. * fireEvent - whether or not to fire the detach event. defaults to false.
  5222. */
  5223. this.detachFrom = function(targetEndpoint, fireEvent, originalEvent) {
  5224. var c = [];
  5225. for ( var i = 0; i < self.connections.length; i++) {
  5226. if (self.connections[i].endpoints[1] == targetEndpoint
  5227. || self.connections[i].endpoints[0] == targetEndpoint) {
  5228. c.push(self.connections[i]);
  5229. }
  5230. }
  5231. for ( var i = 0; i < c.length; i++) {
  5232. if (self.detach(c[i], false, true, fireEvent, originalEvent))
  5233. c[i].setHover(false, false);
  5234. }
  5235. };
  5236. /*
  5237. * Function: detachFromConnection
  5238. * Detach this Endpoint from the Connection, but leave the Connection alive. Used when dragging.
  5239. *
  5240. * Parameters:
  5241. * connection - Connection to detach from.
  5242. */
  5243. this.detachFromConnection = function(connection) {
  5244. var idx = _findWithFunction(self.connections, function(c) { return c.id == connection.id});
  5245. if (idx >= 0) {
  5246. self.connections.splice(idx, 1);
  5247. }
  5248. };
  5249. /*
  5250. * Function: getElement
  5251. *
  5252. * Returns:
  5253. * The DOM element this Endpoint is attached to.
  5254. */
  5255. this.getElement = function() {
  5256. return _element;
  5257. };
  5258. /*
  5259. * Function: setElement
  5260. * Sets the DOM element this Endpoint is attached to.
  5261. *
  5262. * Parameters:
  5263. * el - dom element or selector
  5264. * container - optional, specifies the actual parent element to use as the parent for this Endpoint's visual representation.
  5265. * See the jsPlumb documentation for a discussion about this.
  5266. */
  5267. this.setElement = function(el, container) {
  5268. // TODO possibly have this object take charge of moving the UI components into the appropriate
  5269. // parent. this is used only by makeSource right now, and that function takes care of
  5270. // moving the UI bits and pieces. however it would s
  5271. var parentId = _getId(el);
  5272. // remove the endpoint from the list for the current endpoint's element
  5273. _removeWithFunction(endpointsByElement[self.elementId], function(e) {
  5274. return e.id == self.id;
  5275. });
  5276. _element = _getElementObject(el);
  5277. _elementId = _getId(_element);
  5278. self.elementId = _elementId;
  5279. // need to get the new parent now
  5280. var newParentElement = _getParentFromParams({source:parentId, container:container}),
  5281. curParent = jpcl.getParent(self.canvas);
  5282. jpcl.removeElement(self.canvas, curParent);
  5283. jpcl.appendElement(self.canvas, newParentElement);
  5284. // now move connection(s)...i would expect there to be only one but we will iterate.
  5285. for (var i = 0; i < self.connections.length; i++) {
  5286. self.connections[i].moveParent(newParentElement);
  5287. self.connections[i].sourceId = _elementId;
  5288. self.connections[i].source = _element;
  5289. }
  5290. _addToList(endpointsByElement, parentId, self);
  5291. //_currentInstance.repaint(parentId);
  5292. };
  5293. /*
  5294. * Function: getUuid
  5295. * Returns the UUID for this Endpoint, if there is one. Otherwise returns null.
  5296. */
  5297. this.getUuid = function() {
  5298. return _uuid;
  5299. };
  5300. /**
  5301. * private but must be exposed.
  5302. */
  5303. this.makeInPlaceCopy = function() {
  5304. var loc = self.anchor.getCurrentLocation(self),
  5305. o = self.anchor.getOrientation(self),
  5306. inPlaceAnchor = {
  5307. compute:function() { return [ loc[0], loc[1] ]},
  5308. getCurrentLocation : function() { return [ loc[0], loc[1] ]},
  5309. getOrientation:function() { return o; }
  5310. };
  5311. return _newEndpoint( {
  5312. anchor : inPlaceAnchor,
  5313. source : _element,
  5314. paintStyle : this.getPaintStyle(),
  5315. endpoint : _endpoint,
  5316. _transient:true,
  5317. scope:self.scope
  5318. });
  5319. };
  5320. /*
  5321. * Function: isConnectedTo
  5322. * Returns whether or not this endpoint is connected to the given Endpoint.
  5323. *
  5324. * Parameters:
  5325. * endpoint - Endpoint to test.
  5326. */
  5327. this.isConnectedTo = function(endpoint) {
  5328. var found = false;
  5329. if (endpoint) {
  5330. for ( var i = 0; i < self.connections.length; i++) {
  5331. if (self.connections[i].endpoints[1] == endpoint) {
  5332. found = true;
  5333. break;
  5334. }
  5335. }
  5336. }
  5337. return found;
  5338. };
  5339. /**
  5340. * private but needs to be exposed.
  5341. */
  5342. this.isFloating = function() {
  5343. return floatingEndpoint != null;
  5344. };
  5345. /**
  5346. * returns a connection from the pool; used when dragging starts. just gets the head of the array if it can.
  5347. */
  5348. this.connectorSelector = function() {
  5349. var candidate = self.connections[0];
  5350. if (self.isTarget && candidate) return candidate;
  5351. else {
  5352. return (self.connections.length < _maxConnections) || _maxConnections == -1 ? null : candidate;
  5353. }
  5354. };
  5355. /*
  5356. * Function: isFull
  5357. * Returns whether or not the Endpoint can accept any more Connections.
  5358. */
  5359. this.isFull = function() {
  5360. return !(self.isFloating() || _maxConnections < 1 || self.connections.length < _maxConnections);
  5361. };
  5362. /*
  5363. * Function: setDragAllowedWhenFull
  5364. * Sets whether or not connections can be dragged from this Endpoint once it is full. You would use this in a UI in
  5365. * which you're going to provide some other way of breaking connections, if you need to break them at all. This property
  5366. * is by default true; use it in conjunction with the 'reattach' option on a connect call.
  5367. *
  5368. * Parameters:
  5369. * allowed - whether drag is allowed or not when the Endpoint is full.
  5370. */
  5371. this.setDragAllowedWhenFull = function(allowed) {
  5372. dragAllowedWhenFull = allowed;
  5373. };
  5374. /*
  5375. * Function: setStyle
  5376. * Sets the paint style of the Endpoint. This is a JS object of the same form you supply to a jsPlumb.addEndpoint or jsPlumb.connect call.
  5377. * TODO move setStyle into EventGenerator, remove it from here. is Connection's method currently setPaintStyle ? wire that one up to
  5378. * setStyle and deprecate it if so.
  5379. *
  5380. * Parameters:
  5381. * style - Style object to set, for example {fillStyle:"blue"}.
  5382. *
  5383. * @deprecated use setPaintStyle instead.
  5384. */
  5385. this.setStyle = self.setPaintStyle;
  5386. /**
  5387. * a deep equals check. everything must match, including the anchor,
  5388. * styles, everything. TODO: finish Endpoint.equals
  5389. */
  5390. this.equals = function(endpoint) {
  5391. return this.anchor.equals(endpoint.anchor);
  5392. };
  5393. // a helper function that tries to find a connection to the given element, and returns it if so. if elementWithPrecedence is null,
  5394. // or no connection to it is found, we return the first connection in our list.
  5395. var findConnectionToUseForDynamicAnchor = function(elementWithPrecedence) {
  5396. var idx = 0;
  5397. if (elementWithPrecedence != null) {
  5398. for (var i = 0; i < self.connections.length; i++) {
  5399. if (self.connections[i].sourceId == elementWithPrecedence || self.connections[i].targetId == elementWithPrecedence) {
  5400. idx = i;
  5401. break;
  5402. }
  5403. }
  5404. }
  5405. return self.connections[idx];
  5406. };
  5407. /*
  5408. * Function: paint
  5409. * Paints the Endpoint, recalculating offset and anchor positions if necessary. This does NOT paint
  5410. * any of the Endpoint's connections.
  5411. *
  5412. * Parameters:
  5413. * timestamp - optional timestamp advising the Endpoint of the current paint time; if it has painted already once for this timestamp, it will not paint again.
  5414. * canvas - optional Canvas to paint on. Only used internally by jsPlumb in certain obscure situations.
  5415. * connectorPaintStyle - paint style of the Connector attached to this Endpoint. Used to get a fillStyle if nothing else was supplied.
  5416. */
  5417. this.paint = function(params) {
  5418. params = params || {};
  5419. var timestamp = params.timestamp, recalc = !(params.recalc === false);
  5420. //recalc = params.recalc;
  5421. if (!timestamp || self.timestamp !== timestamp) {
  5422. _updateOffset({ elId:_elementId, timestamp:timestamp, recalc:recalc });
  5423. var xy = params.offset || offsets[_elementId];
  5424. if(xy) {
  5425. var ap = params.anchorPoint,connectorPaintStyle = params.connectorPaintStyle;
  5426. if (ap == null) {
  5427. var wh = params.dimensions || sizes[_elementId];
  5428. if (xy == null || wh == null) {
  5429. _updateOffset( { elId : _elementId, timestamp : timestamp });
  5430. xy = offsets[_elementId];
  5431. wh = sizes[_elementId];
  5432. }
  5433. var anchorParams = { xy : [ xy.left, xy.top ], wh : wh, element : self, timestamp : timestamp };
  5434. if (recalc && self.anchor.isDynamic && self.connections.length > 0) {
  5435. var c = findConnectionToUseForDynamicAnchor(params.elementWithPrecedence),
  5436. oIdx = c.endpoints[0] == self ? 1 : 0,
  5437. oId = oIdx == 0 ? c.sourceId : c.targetId,
  5438. oOffset = offsets[oId], oWH = sizes[oId];
  5439. anchorParams.txy = [ oOffset.left, oOffset.top ];
  5440. anchorParams.twh = oWH;
  5441. anchorParams.tElement = c.endpoints[oIdx];
  5442. }
  5443. ap = self.anchor.compute(anchorParams);
  5444. }
  5445. // switched _endpoint to self here; continuous anchors, for instance, look for the
  5446. // endpoint's id, but here "_endpoint" is the renderer, not the actual endpoint.
  5447. // TODO need to verify this does not break anything.
  5448. //var d = _endpoint.compute(ap, self.anchor.getOrientation(_endpoint), self.paintStyleInUse, connectorPaintStyle || self.paintStyleInUse);
  5449. var d = _endpoint.compute(ap, self.anchor.getOrientation(self), self.paintStyleInUse, connectorPaintStyle || self.paintStyleInUse);
  5450. _endpoint.paint(d, self.paintStyleInUse, self.anchor);
  5451. self.timestamp = timestamp;
  5452. /* paint overlays*/
  5453. for ( var i = 0; i < self.overlays.length; i++) {
  5454. var o = self.overlays[i];
  5455. if (o.isVisible) self.overlayPlacements[i] = o.draw(self.endpoint, self.paintStyleInUse, d);
  5456. }
  5457. }
  5458. }
  5459. };
  5460. this.repaint = this.paint;
  5461. /**
  5462. * @deprecated
  5463. */
  5464. this.removeConnection = this.detach; // backwards compatibility
  5465. // is this a connection source? we make it draggable and have the
  5466. // drag listener maintain a connection with a floating endpoint.
  5467. if (jsPlumb.CurrentLibrary.isDragSupported(_element)) {
  5468. var placeholderInfo = { id:null, element:null },
  5469. jpc = null,
  5470. existingJpc = false,
  5471. existingJpcParams = null,
  5472. _dragHandler = _makeConnectionDragHandler(placeholderInfo);
  5473. var start = function() {
  5474. // drag might have started on an endpoint that is not actually a source, but which has
  5475. // one or more connections.
  5476. jpc = self.connectorSelector();
  5477. var _continue = true;
  5478. // if not enabled, return
  5479. if (!self.isEnabled()) _continue = false;
  5480. // if no connection and we're not a source, return.
  5481. if (jpc == null && !params.isSource) _continue = false;
  5482. // otherwise if we're full and not allowed to drag, also return false.
  5483. if (params.isSource && self.isFull() && !dragAllowedWhenFull) _continue = false;
  5484. // if the connection was setup as not detachable or one of its endpoints
  5485. // was setup as connectionsDetachable = false, or Defaults.ConnectionsDetachable
  5486. // is set to false...
  5487. if (jpc != null && !jpc.isDetachable()) _continue = false;
  5488. if (_continue === false) {
  5489. // this is for mootools and yui. returning false from this causes jquery to stop drag.
  5490. // the events are wrapped in both mootools and yui anyway, but i don't think returning
  5491. // false from the start callback would stop a drag.
  5492. if (jsPlumb.CurrentLibrary.stopDrag) jsPlumb.CurrentLibrary.stopDrag();
  5493. _dragHandler.stopDrag();
  5494. return false;
  5495. }
  5496. // if we're not full but there was a connection, make it null. we'll create a new one.
  5497. if (jpc && !self.isFull() && params.isSource) jpc = null;
  5498. _updateOffset( { elId : _elementId });
  5499. inPlaceCopy = self.makeInPlaceCopy();
  5500. inPlaceCopy.referenceEndpoint = self;
  5501. inPlaceCopy.paint();
  5502. _makeDraggablePlaceholder(placeholderInfo, self.parent);
  5503. // set the offset of this div to be where 'inPlaceCopy' is, to start with.
  5504. // TODO merge this code with the code in both Anchor and FloatingAnchor, because it
  5505. // does the same stuff.
  5506. var ipcoel = _getElementObject(inPlaceCopy.canvas),
  5507. ipco = _getOffset(ipcoel, _currentInstance),
  5508. po = adjustForParentOffsetAndScroll([ipco.left, ipco.top], inPlaceCopy.canvas);
  5509. jsPlumb.CurrentLibrary.setOffset(placeholderInfo.element, {left:po[0], top:po[1]});
  5510. // when using makeSource and a parent, we first draw the source anchor on the source element, then
  5511. // move it to the parent. note that this happens after drawing the placeholder for the
  5512. // first time.
  5513. if (self.parentAnchor) self.anchor = _currentInstance.makeAnchor(self.parentAnchor, self.elementId, _currentInstance);
  5514. // store the id of the dragging div and the source element. the drop function will pick these up.
  5515. _setAttribute(_getElementObject(self.canvas), "dragId", placeholderInfo.id);
  5516. _setAttribute(_getElementObject(self.canvas), "elId", _elementId);
  5517. // create a floating endpoint.
  5518. // here we test to see if a dragProxy was specified in this endpoint's constructor params, and
  5519. // if so, we create that endpoint instead of cloning ourselves.
  5520. if (params.proxy) {
  5521. /* var floatingAnchor = new FloatingAnchor( { reference : self.anchor, referenceCanvas : self.canvas });
  5522. floatingEndpoint = _newEndpoint({
  5523. paintStyle : params.proxy.paintStyle,
  5524. endpoint : params.proxy.endpoint,
  5525. anchor : floatingAnchor,
  5526. source : placeholderInfo.element,
  5527. scope:"__floating"
  5528. });
  5529. //$(self.canvas).hide();
  5530. */
  5531. self.setPaintStyle(params.proxy.paintStyle);
  5532. // if we do this, we have to cleanup the old one. like just remove its display parts
  5533. //self.setEndpoint(params.proxy.endpoint);
  5534. }
  5535. // else {
  5536. floatingEndpoint = _makeFloatingEndpoint(self.getPaintStyle(), self.anchor, _endpoint, self.canvas, placeholderInfo.element);
  5537. // }
  5538. if (jpc == null) {
  5539. self.anchor.locked = true;
  5540. self.setHover(false, false);
  5541. // TODO the hover call above does not reset any target endpoint's hover
  5542. // states.
  5543. // create a connection. one end is this endpoint, the other is a floating endpoint.
  5544. jpc = _newConnection({
  5545. sourceEndpoint : self,
  5546. targetEndpoint : floatingEndpoint,
  5547. source : self.endpointWillMoveTo || _getElementObject(_element), // for makeSource with parent option. ensure source element is represented correctly.
  5548. target : placeholderInfo.element,
  5549. anchors : [ self.anchor, floatingEndpoint.anchor ],
  5550. paintStyle : params.connectorStyle, // this can be null. Connection will use the default.
  5551. hoverPaintStyle:params.connectorHoverStyle,
  5552. connector : params.connector, // this can also be null. Connection will use the default.
  5553. overlays : params.connectorOverlays,
  5554. type:self.connectionType,
  5555. cssClass:self.connectorClass,
  5556. hoverClass:self.connectorHoverClass
  5557. });
  5558. } else {
  5559. existingJpc = true;
  5560. jpc.setHover(false);
  5561. // if existing connection, allow to be dropped back on the source endpoint (issue 51).
  5562. _initDropTarget(_getElementObject(inPlaceCopy.canvas), false, true);
  5563. // new anchor idx
  5564. var anchorIdx = jpc.endpoints[0].id == self.id ? 0 : 1;
  5565. jpc.floatingAnchorIndex = anchorIdx; // save our anchor index as the connection's floating index.
  5566. self.detachFromConnection(jpc); // detach from the connection while dragging is occurring.
  5567. // store the original scope (issue 57)
  5568. var c = _getElementObject(self.canvas),
  5569. dragScope = jsPlumb.CurrentLibrary.getDragScope(c);
  5570. _setAttribute(c, "originalScope", dragScope);
  5571. // now we want to get this endpoint's DROP scope, and set it for now: we can only be dropped on drop zones
  5572. // that have our drop scope (issue 57).
  5573. var dropScope = jsPlumb.CurrentLibrary.getDropScope(c);
  5574. jsPlumb.CurrentLibrary.setDragScope(c, dropScope);
  5575. // now we replace ourselves with the temporary div we created above:
  5576. if (anchorIdx == 0) {
  5577. existingJpcParams = [ jpc.source, jpc.sourceId, i, dragScope ];
  5578. jpc.source = placeholderInfo.element;
  5579. jpc.sourceId = placeholderInfo.id;
  5580. } else {
  5581. existingJpcParams = [ jpc.target, jpc.targetId, i, dragScope ];
  5582. jpc.target = placeholderInfo.element;
  5583. jpc.targetId = placeholderInfo.id;
  5584. }
  5585. // lock the other endpoint; if it is dynamic it will not move while the drag is occurring.
  5586. jpc.endpoints[anchorIdx == 0 ? 1 : 0].anchor.locked = true;
  5587. // store the original endpoint and assign the new floating endpoint for the drag.
  5588. jpc.suspendedEndpoint = jpc.endpoints[anchorIdx];
  5589. jpc.suspendedEndpoint.setHover(false);
  5590. floatingEndpoint.referenceEndpoint = jpc.suspendedEndpoint;
  5591. jpc.endpoints[anchorIdx] = floatingEndpoint;
  5592. // fire an event that informs that a connection is being dragged
  5593. fireConnectionDraggingEvent(jpc);
  5594. }
  5595. // register it and register connection on it.
  5596. floatingConnections[placeholderInfo.id] = jpc;
  5597. floatingEndpoint.addConnection(jpc);
  5598. // only register for the target endpoint; we will not be dragging the source at any time
  5599. // before this connection is either discarded or made into a permanent connection.
  5600. _addToList(endpointsByElement, placeholderInfo.id, floatingEndpoint);
  5601. // tell jsplumb about it
  5602. _currentInstance.currentlyDragging = true;
  5603. };
  5604. var jpcl = jsPlumb.CurrentLibrary,
  5605. dragOptions = params.dragOptions || {},
  5606. defaultOpts = jsPlumb.extend( {}, jpcl.defaultDragOptions),
  5607. startEvent = jpcl.dragEvents["start"],
  5608. stopEvent = jpcl.dragEvents["stop"],
  5609. dragEvent = jpcl.dragEvents["drag"];
  5610. dragOptions = jsPlumb.extend(defaultOpts, dragOptions);
  5611. dragOptions.scope = dragOptions.scope || self.scope;
  5612. dragOptions[startEvent] = _wrap(dragOptions[startEvent], start);
  5613. // extracted drag handler function so can be used by makeSource
  5614. dragOptions[dragEvent] = _wrap(dragOptions[dragEvent], _dragHandler.drag);
  5615. dragOptions[stopEvent] = _wrap(dragOptions[stopEvent],
  5616. function() {
  5617. var originalEvent = jpcl.getDropEvent(arguments);
  5618. _currentInstance.currentlyDragging = false;
  5619. _removeWithFunction(endpointsByElement[placeholderInfo.id], function(e) {
  5620. return e.id == floatingEndpoint.id;
  5621. });
  5622. _removeElements( [ placeholderInfo.element[0], floatingEndpoint.canvas ], _element); // TODO: clean up the connection canvas (if the user aborted)
  5623. _removeElement(inPlaceCopy.canvas, _element);
  5624. _currentInstance.anchorManager.clearFor(placeholderInfo.id);
  5625. var idx = jpc.floatingAnchorIndex == null ? 1 : jpc.floatingAnchorIndex;
  5626. jpc.endpoints[idx == 0 ? 1 : 0].anchor.locked = false;
  5627. self.setPaintStyle(originalPaintStyle); // reset to original; may have been changed by drag proxy.
  5628. if (jpc.endpoints[idx] == floatingEndpoint) {
  5629. // if the connection was an existing one:
  5630. if (existingJpc && jpc.suspendedEndpoint) {
  5631. // fix for issue35, thanks Sylvain Gizard: when firing the detach event make sure the
  5632. // floating endpoint has been replaced.
  5633. if (idx == 0) {
  5634. jpc.source = existingJpcParams[0];
  5635. jpc.sourceId = existingJpcParams[1];
  5636. } else {
  5637. jpc.target = existingJpcParams[0];
  5638. jpc.targetId = existingJpcParams[1];
  5639. }
  5640. // restore the original scope (issue 57)
  5641. jsPlumb.CurrentLibrary.setDragScope(existingJpcParams[2], existingJpcParams[3]);
  5642. jpc.endpoints[idx] = jpc.suspendedEndpoint;
  5643. //if (self.isReattach || jpc._forceDetach || !jpc.endpoints[idx == 0 ? 1 : 0].detach(jpc, false, false, true, originalEvent)) {
  5644. if (jpc.isReattach() || jpc._forceReattach || jpc._forceDetach || !jpc.endpoints[idx == 0 ? 1 : 0].detach(jpc, false, false, true, originalEvent)) {
  5645. jpc.setHover(false);
  5646. jpc.floatingAnchorIndex = null;
  5647. jpc.suspendedEndpoint.addConnection(jpc);
  5648. _currentInstance.repaint(existingJpcParams[1]);
  5649. }
  5650. jpc._forceDetach = null;
  5651. jpc._forceReattach = null;
  5652. } else {
  5653. // TODO this looks suspiciously kind of like an Endpoint.detach call too.
  5654. // i wonder if this one should post an event though. maybe this is good like this.
  5655. _removeElements(jpc.connector.getDisplayElements(), self.parent);
  5656. self.detachFromConnection(jpc);
  5657. }
  5658. }
  5659. self.anchor.locked = false;
  5660. self.paint({recalc:false});
  5661. //jpc.setHover(false, false);
  5662. fireConnectionDragStopEvent(jpc);
  5663. jpc = null;
  5664. inPlaceCopy = null;
  5665. delete endpointsByElement[floatingEndpoint.elementId];
  5666. floatingEndpoint.anchor = null;
  5667. floatingEndpoint = null;
  5668. _currentInstance.currentlyDragging = false;
  5669. });
  5670. var i = _getElementObject(self.canvas);
  5671. jsPlumb.CurrentLibrary.initDraggable(i, dragOptions, true);
  5672. }
  5673. // pulled this out into a function so we can reuse it for the inPlaceCopy canvas; you can now drop detached connections
  5674. // back onto the endpoint you detached it from.
  5675. var _initDropTarget = function(canvas, forceInit, isTransient, endpoint) {
  5676. if ((params.isTarget || forceInit) && jsPlumb.CurrentLibrary.isDropSupported(_element)) {
  5677. var dropOptions = params.dropOptions || _currentInstance.Defaults.DropOptions || jsPlumb.Defaults.DropOptions;
  5678. dropOptions = jsPlumb.extend( {}, dropOptions);
  5679. dropOptions.scope = dropOptions.scope || self.scope;
  5680. var dropEvent = jsPlumb.CurrentLibrary.dragEvents['drop'],
  5681. overEvent = jsPlumb.CurrentLibrary.dragEvents['over'],
  5682. outEvent = jsPlumb.CurrentLibrary.dragEvents['out'],
  5683. drop = function() {
  5684. var originalEvent = jsPlumb.CurrentLibrary.getDropEvent(arguments),
  5685. draggable = _getElementObject(jsPlumb.CurrentLibrary.getDragObject(arguments)),
  5686. id = _getAttribute(draggable, "dragId"),
  5687. elId = _getAttribute(draggable, "elId"),
  5688. scope = _getAttribute(draggable, "originalScope"),
  5689. jpc = floatingConnections[id];
  5690. // if this is a drop back where the connection came from, mark it force rettach and
  5691. // return; the stop handler will reattach. without firing an event.
  5692. var redrop = jpc.suspendedEndpoint && (jpc.suspendedEndpoint.id == self.id ||
  5693. self.referenceEndpoint && jpc.suspendedEndpoint.id == self.referenceEndpoint.id) ;
  5694. if (redrop) {
  5695. jpc._forceReattach = true;
  5696. return;
  5697. }
  5698. if (jpc != null) {
  5699. var idx = jpc.floatingAnchorIndex == null ? 1 : jpc.floatingAnchorIndex, oidx = idx == 0 ? 1 : 0;
  5700. // restore the original scope if necessary (issue 57)
  5701. if (scope) jsPlumb.CurrentLibrary.setDragScope(draggable, scope);
  5702. var endpointEnabled = endpoint != null ? endpoint.isEnabled() : true;
  5703. if (self.isFull()) {
  5704. self.fire("maxConnections", {
  5705. endpoint:self,
  5706. connection:jpc,
  5707. maxConnections:_maxConnections
  5708. }, originalEvent);
  5709. }
  5710. if (!self.isFull() && !(idx == 0 && !self.isSource) && !(idx == 1 && !self.isTarget) && endpointEnabled) {
  5711. var _doContinue = true;
  5712. // the second check here is for the case that the user is dropping it back
  5713. // where it came from.
  5714. if (jpc.suspendedEndpoint && jpc.suspendedEndpoint.id != self.id) {
  5715. if (idx == 0) {
  5716. jpc.source = jpc.suspendedEndpoint.element;
  5717. jpc.sourceId = jpc.suspendedEndpoint.elementId;
  5718. } else {
  5719. jpc.target = jpc.suspendedEndpoint.element;
  5720. jpc.targetId = jpc.suspendedEndpoint.elementId;
  5721. }
  5722. if (!jpc.isDetachAllowed(jpc) || !jpc.endpoints[idx].isDetachAllowed(jpc) || !jpc.suspendedEndpoint.isDetachAllowed(jpc) || !_currentInstance.checkCondition("beforeDetach", jpc))
  5723. _doContinue = false;
  5724. }
  5725. // these have to be set before testing for beforeDrop.
  5726. if (idx == 0) {
  5727. jpc.source = self.element;
  5728. jpc.sourceId = self.elementId;
  5729. } else {
  5730. jpc.target = self.element;
  5731. jpc.targetId = self.elementId;
  5732. }
  5733. // ------------ wrap the execution path in a function so we can support asynchronous beforeDrop
  5734. // we want to execute this regardless.
  5735. var commonFunction = function() {
  5736. jpc.floatingAnchorIndex = null;
  5737. };
  5738. var continueFunction = function() {
  5739. // remove this jpc from the current endpoint
  5740. jpc.endpoints[idx].detachFromConnection(jpc);
  5741. if (jpc.suspendedEndpoint) jpc.suspendedEndpoint.detachFromConnection(jpc);
  5742. jpc.endpoints[idx] = self;
  5743. self.addConnection(jpc);
  5744. // copy our parameters in to the connection:
  5745. var params = self.getParameters();
  5746. for (var aParam in params)
  5747. jpc.setParameter(aParam, params[aParam]);
  5748. if (!jpc.suspendedEndpoint) {
  5749. if (params.draggable)
  5750. jsPlumb.CurrentLibrary.initDraggable(self.element, dragOptions, true);
  5751. }
  5752. else {
  5753. var suspendedElement = jpc.suspendedEndpoint.getElement(), suspendedElementId = jpc.suspendedEndpoint.elementId;
  5754. // fire a detach event
  5755. fireDetachEvent({
  5756. source : idx == 0 ? suspendedElement : jpc.source,
  5757. target : idx == 1 ? suspendedElement : jpc.target,
  5758. sourceId : idx == 0 ? suspendedElementId : jpc.sourceId,
  5759. targetId : idx == 1 ? suspendedElementId : jpc.targetId,
  5760. sourceEndpoint : idx == 0 ? jpc.suspendedEndpoint : jpc.endpoints[0],
  5761. targetEndpoint : idx == 1 ? jpc.suspendedEndpoint : jpc.endpoints[1],
  5762. connection : jpc
  5763. }, true, originalEvent);
  5764. }
  5765. // finalise will inform the anchor manager and also add to
  5766. // connectionsByScope if necessary.
  5767. _finaliseConnection(jpc, null, originalEvent);
  5768. commonFunction();
  5769. };
  5770. var dontContinueFunction = function() {
  5771. // otherwise just put it back on the endpoint it was on before the drag.
  5772. if (jpc.suspendedEndpoint) {
  5773. jpc.endpoints[idx] = jpc.suspendedEndpoint;
  5774. jpc.setHover(false);
  5775. jpc._forceDetach = true;
  5776. if (idx == 0) {
  5777. jpc.source = jpc.suspendedEndpoint.element;
  5778. jpc.sourceId = jpc.suspendedEndpoint.elementId;
  5779. } else {
  5780. jpc.target = jpc.suspendedEndpoint.element;
  5781. jpc.targetId = jpc.suspendedEndpoint.elementId;;
  5782. }
  5783. jpc.suspendedEndpoint.addConnection(jpc);
  5784. jpc.endpoints[0].repaint();
  5785. jpc.repaint();
  5786. _currentInstance.repaint(jpc.source.elementId);
  5787. jpc._forceDetach = false;
  5788. }
  5789. commonFunction();
  5790. };
  5791. // --------------------------------------
  5792. // now check beforeDrop. this will be available only on Endpoints that are setup to
  5793. // have a beforeDrop condition (although, secretly, under the hood all Endpoints and
  5794. // the Connection have them, because they are on jsPlumbUIComponent. shhh!), because
  5795. // it only makes sense to have it on a target endpoint.
  5796. _doContinue = _doContinue && self.isDropAllowed(jpc.sourceId, jpc.targetId, jpc.scope, jpc, self);
  5797. if (_doContinue) {
  5798. continueFunction();
  5799. }
  5800. else {
  5801. dontContinueFunction();
  5802. }
  5803. //commonFunction();
  5804. }
  5805. _currentInstance.currentlyDragging = false;
  5806. delete floatingConnections[id];
  5807. jpc.suspendedEndpoint = null;
  5808. }
  5809. };
  5810. dropOptions[dropEvent] = _wrap(dropOptions[dropEvent], drop);
  5811. dropOptions[overEvent] = _wrap(dropOptions[overEvent], function() {
  5812. var draggable = jsPlumb.CurrentLibrary.getDragObject(arguments),
  5813. id = _getAttribute( _getElementObject(draggable), "dragId"),
  5814. _jpc = floatingConnections[id];
  5815. if (_jpc != null) {
  5816. var idx = _jpc.floatingAnchorIndex == null ? 1 : _jpc.floatingAnchorIndex;
  5817. // here we should fire the 'over' event if we are a target and this is a new connection,
  5818. // or we are the same as the floating endpoint.
  5819. var _cont = (self.isTarget && _jpc.floatingAnchorIndex != 0) || (_jpc.suspendedEndpoint && self.referenceEndpoint && self.referenceEndpoint.id == _jpc.suspendedEndpoint.id);
  5820. if (_cont)
  5821. _jpc.endpoints[idx].anchor.over(self.anchor);
  5822. }
  5823. });
  5824. dropOptions[outEvent] = _wrap(dropOptions[outEvent], function() {
  5825. var draggable = jsPlumb.CurrentLibrary.getDragObject(arguments),
  5826. id = _getAttribute( _getElementObject(draggable), "dragId"),
  5827. _jpc = floatingConnections[id];
  5828. if (_jpc != null) {
  5829. var idx = _jpc.floatingAnchorIndex == null ? 1 : _jpc.floatingAnchorIndex;
  5830. var _cont = (self.isTarget && _jpc.floatingAnchorIndex != 0) || (_jpc.suspendedEndpoint && self.referenceEndpoint && self.referenceEndpoint.id == _jpc.suspendedEndpoint.id);
  5831. if (_cont)
  5832. _jpc.endpoints[idx].anchor.out();
  5833. }
  5834. });
  5835. jsPlumb.CurrentLibrary.initDroppable(canvas, dropOptions, true, isTransient);
  5836. }
  5837. };
  5838. // 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.
  5839. _initDropTarget(_getElementObject(self.canvas), true, !(params._transient || self.anchor.isFloating), self);
  5840. // finally, set type if it was provided
  5841. if (params.type)
  5842. self.addType(params.type);
  5843. // ***************************** PLACEHOLDERS FOR NATURAL DOCS *************************************************
  5844. /*
  5845. * Function: bind
  5846. * Bind to an event on the Endpoint.
  5847. *
  5848. * Parameters:
  5849. * event - the event to bind. Available events on an Endpoint are:
  5850. * - *click* : notification that a Endpoint was clicked.
  5851. * - *dblclick* : notification that a Endpoint was double clicked.
  5852. * - *mouseenter* : notification that the mouse is over a Endpoint.
  5853. * - *mouseexit* : notification that the mouse exited a Endpoint.
  5854. * - *contextmenu* : notification that the user right-clicked on the Endpoint.
  5855. *
  5856. * callback - function to callback. This function will be passed the Endpoint that caused the event, and also the original event.
  5857. */
  5858. /*
  5859. * Function: setPaintStyle
  5860. * Sets the Endpoint's paint style and then repaints the Endpoint.
  5861. *
  5862. * Parameters:
  5863. * style - Style to use.
  5864. */
  5865. /*
  5866. * Function: getPaintStyle
  5867. * Gets the Endpoint's paint style. This is not necessarily the paint style in use at the time;
  5868. * this is the paint style for the Endpoint when the mouse it not hovering over it.
  5869. */
  5870. /*
  5871. * Function: setHoverPaintStyle
  5872. * Sets the paint style to use when the mouse is hovering over the Endpoint. This is null by default.
  5873. * The hover paint style is applied as extensions to the paintStyle; it does not entirely replace
  5874. * it. This is because people will most likely want to change just one thing when hovering, say the
  5875. * color for example, but leave the rest of the appearance the same.
  5876. *
  5877. * Parameters:
  5878. * style - Style to use when the mouse is hovering.
  5879. * doNotRepaint - if true, the Endpoint will not be repainted. useful when setting things up initially.
  5880. */
  5881. /*
  5882. * Function: setHover
  5883. * Sets/unsets the hover state of this Endpoint.
  5884. *
  5885. * Parameters:
  5886. * hover - hover state boolean
  5887. * ignoreAttachedElements - if true, does not notify any attached elements of the change in hover state. used mostly to avoid infinite loops.
  5888. */
  5889. /*
  5890. * Function: getParameter
  5891. * Gets the named parameter; returns null if no parameter with that name is set. Parameters may have been set on the Endpoint in the 'addEndpoint' call, or they may have been set with the setParameter function.
  5892. *
  5893. * Parameters:
  5894. * key - Parameter name.
  5895. */
  5896. /*
  5897. * Function: setParameter
  5898. * Sets the named parameter to the given value.
  5899. *
  5900. * Parameters:
  5901. * key - Parameter name.
  5902. * value - Parameter value.
  5903. */
  5904. /*
  5905. * Property: canvas
  5906. * The Endpoint's drawing area.
  5907. */
  5908. /*
  5909. * Property: connections
  5910. * List of Connections this Endpoint is attached to.
  5911. */
  5912. /*
  5913. * Property: scope
  5914. * Scope descriptor for this Endpoint.
  5915. */
  5916. /*
  5917. * Property: overlays
  5918. * List of Overlays for this Endpoint.
  5919. */
  5920. /*
  5921. * Function: addOverlay
  5922. * Adds an Overlay to the Endpoint.
  5923. *
  5924. * Parameters:
  5925. * overlay - Overlay to add.
  5926. */
  5927. /*
  5928. * Function: getOverlay
  5929. * Gets an overlay, by ID. Note: by ID. You would pass an 'id' parameter
  5930. * in to the Overlay's constructor arguments, and then use that to retrieve
  5931. * it via this method.
  5932. */
  5933. /*
  5934. * Function: getOverlays
  5935. * Gets all the overlays for this component.
  5936. */
  5937. /*
  5938. * Function: hideOverlay
  5939. * Hides the overlay specified by the given id.
  5940. */
  5941. /*
  5942. * Function: hideOverlays
  5943. * Hides all Overlays
  5944. */
  5945. /*
  5946. * Function: showOverlay
  5947. * Shows the overlay specified by the given id.
  5948. */
  5949. /*
  5950. * Function: showOverlays
  5951. * Shows all Overlays
  5952. */
  5953. /**
  5954. * Function: removeAllOverlays
  5955. * Removes all overlays from the Endpoint, and then repaints.
  5956. */
  5957. /**
  5958. * Function: removeOverlay
  5959. * Removes an overlay by ID. Note: by ID. this is a string you set in the overlay spec.
  5960. * Parameters:
  5961. * overlayId - id of the overlay to remove.
  5962. */
  5963. /**
  5964. * Function: removeOverlays
  5965. * Removes a set of overlays by ID. Note: by ID. this is a string you set in the overlay spec.
  5966. * Parameters:
  5967. * overlayIds - this function takes an arbitrary number of arguments, each of which is a single overlay id.
  5968. */
  5969. /*
  5970. * Function: setLabel
  5971. * Sets the Endpoint's label.
  5972. *
  5973. * Parameters:
  5974. * l - label to set. May be a String, a Function that returns a String, or a params object containing { "label", "labelStyle", "location", "cssClass" }. Note that this uses innerHTML on the label div, so keep
  5975. * that in mind if you need escaped HTML.
  5976. */
  5977. /*
  5978. Function: getLabel
  5979. Returns the label text for this Endpoint (or a function if you are labelling with a function).
  5980. This does not return the overlay itself; this is a convenience method which is a pair with
  5981. setLabel; together they allow you to add and access a Label Overlay without having to create the
  5982. Overlay object itself. For access to the underlying label overlay that jsPlumb has created,
  5983. use getLabelOverlay.
  5984. */
  5985. /*
  5986. Function: getLabelOverlay
  5987. Returns the underlying internal label overlay, which will exist if you specified a label on
  5988. an addEndpoint call, or have called setLabel at any stage.
  5989. */
  5990. // ***************************** END OF PLACEHOLDERS FOR NATURAL DOCS *************************************************
  5991. return self;
  5992. };
  5993. };
  5994. // --------------------- static instance + AMD registration -------------------------------------------
  5995. // create static instance and assign to window if window exists.
  5996. var jsPlumb = new jsPlumbInstance();
  5997. if (typeof window != 'undefined') window.jsPlumb = jsPlumb;
  5998. // add 'getInstance' method to static instance
  5999. jsPlumb.getInstance = function(_defaults) {
  6000. var j = new jsPlumbInstance(_defaults);
  6001. j.init();
  6002. return j;
  6003. };
  6004. // maybe register static instance as an AMD module
  6005. if ( typeof define === "function" && define.amd && define.amd.jsPlumb) {
  6006. define( "jsplumb", [], function () { return jsPlumb; } );
  6007. }
  6008. // --------------------- end static instance + AMD registration -------------------------------------------
  6009. // --------------------- anchors (would like to move these out of here -------------------------------------------
  6010. var _curryAnchor = function(x, y, ox, oy, type, fnInit) {
  6011. return function(params) {
  6012. params = params || {};
  6013. //var a = jsPlumb.makeAnchor([ x, y, ox, oy, 0, 0 ], params.elementId, params.jsPlumbInstance);
  6014. var a = params.jsPlumbInstance.makeAnchor([ x, y, ox, oy, 0, 0 ], params.elementId, params.jsPlumbInstance);
  6015. a.type = type;
  6016. if (fnInit) fnInit(a, params);
  6017. return a;
  6018. };
  6019. };
  6020. /*
  6021. * Property: Anchors.TopCenter
  6022. * An Anchor that is located at the top center of the element.
  6023. */
  6024. jsPlumb.Anchors["TopCenter"] = _curryAnchor(0.5, 0, 0,-1, "TopCenter");
  6025. /*
  6026. * Property: Anchors.BottomCenter
  6027. * An Anchor that is located at the bottom center of the element.
  6028. */
  6029. jsPlumb.Anchors["BottomCenter"] = _curryAnchor(0.5, 1, 0, 1, "BottomCenter");
  6030. /*
  6031. * Property: Anchors.LeftMiddle
  6032. * An Anchor that is located at the left middle of the element.
  6033. */
  6034. jsPlumb.Anchors["LeftMiddle"] = _curryAnchor(0, 0.5, -1, 0, "LeftMiddle");
  6035. /*
  6036. * Property: Anchors.RightMiddle
  6037. * An Anchor that is located at the right middle of the element.
  6038. */
  6039. jsPlumb.Anchors["RightMiddle"] = _curryAnchor(1, 0.5, 1, 0, "RightMiddle");
  6040. /*
  6041. * Property: Anchors.Center
  6042. * An Anchor that is located at the center of the element.
  6043. */
  6044. jsPlumb.Anchors["Center"] = _curryAnchor(0.5, 0.5, 0, 0, "Center");
  6045. /*
  6046. * Property: Anchors.TopRight
  6047. * An Anchor that is located at the top right corner of the element.
  6048. */
  6049. jsPlumb.Anchors["TopRight"] = _curryAnchor(1, 0, 0,-1, "TopRight");
  6050. /*
  6051. * Property: Anchors.BottomRight
  6052. * An Anchor that is located at the bottom right corner of the element.
  6053. */
  6054. jsPlumb.Anchors["BottomRight"] = _curryAnchor(1, 1, 0, 1, "BottomRight");
  6055. /*
  6056. * Property: Anchors.TopLeft
  6057. * An Anchor that is located at the top left corner of the element.
  6058. */
  6059. jsPlumb.Anchors["TopLeft"] = _curryAnchor(0, 0, 0, -1, "TopLeft");
  6060. /*
  6061. * Property: Anchors.BottomLeft
  6062. * An Anchor that is located at the bototm left corner of the element.
  6063. */
  6064. jsPlumb.Anchors["BottomLeft"] = _curryAnchor(0, 1, 0, 1, "BottomLeft");
  6065. jsPlumb.Defaults.DynamicAnchors = function(params) {
  6066. return params.jsPlumbInstance.makeAnchors(["TopCenter", "RightMiddle", "BottomCenter", "LeftMiddle"], params.elementId, params.jsPlumbInstance);
  6067. };
  6068. /*
  6069. * Property: Anchors.AutoDefault
  6070. * Default DynamicAnchors - chooses from TopCenter, RightMiddle, BottomCenter, LeftMiddle.
  6071. */
  6072. jsPlumb.Anchors["AutoDefault"] = function(params) {
  6073. var a = params.jsPlumbInstance.makeDynamicAnchor(jsPlumb.Defaults.DynamicAnchors(params));
  6074. a.type = "AutoDefault";
  6075. return a;
  6076. };
  6077. /*
  6078. * Property: Anchors.Assign
  6079. * An Anchor whose location is assigned at connection time, through an AnchorPositionFinder. Used in conjunction
  6080. * with the 'makeTarget' function. jsPlumb has two of these - 'Fixed' and 'Grid', and you can also write your own.
  6081. */
  6082. jsPlumb.Anchors["Assign"] = _curryAnchor(0,0,0,0,"Assign", function(anchor, params) {
  6083. // find what to use as the "position finder". the user may have supplied a String which represents
  6084. // the id of a position finder in jsPlumb.AnchorPositionFinders, or the user may have supplied the
  6085. // position finder as a function. we find out what to use and then set it on the anchor.
  6086. var pf = params.position || "Fixed";
  6087. anchor.positionFinder = pf.constructor == String ? params.jsPlumbInstance.AnchorPositionFinders[pf] : pf;
  6088. // always set the constructor params; the position finder might need them later (the Grid one does,
  6089. // for example)
  6090. anchor.constructorParams = params;
  6091. });
  6092. // Continuous anchor is just curried through to the 'get' method of the continuous anchor
  6093. // factory.
  6094. /*
  6095. * Property: Anchors.Continuous
  6096. * An Anchor that is tracks the other element in the connection, choosing the closest face.
  6097. */
  6098. jsPlumb.Anchors["Continuous"] = function(params) {
  6099. return params.jsPlumbInstance.continuousAnchorFactory.get(params);
  6100. };
  6101. // these are the default anchor positions finders, which are used by the makeTarget function. supply
  6102. // a position finder argument to that function allows you to specify where the resulting anchor will
  6103. // be located
  6104. jsPlumb.AnchorPositionFinders = {
  6105. "Fixed": function(dp, ep, es, params) {
  6106. return [ (dp.left - ep.left) / es[0], (dp.top - ep.top) / es[1] ];
  6107. },
  6108. "Grid":function(dp, ep, es, params) {
  6109. var dx = dp.left - ep.left, dy = dp.top - ep.top,
  6110. gx = es[0] / (params.grid[0]), gy = es[1] / (params.grid[1]),
  6111. mx = Math.floor(dx / gx), my = Math.floor(dy / gy);
  6112. return [ ((mx * gx) + (gx / 2)) / es[0], ((my * gy) + (gy / 2)) / es[1] ];
  6113. }
  6114. };
  6115. /*
  6116. * Property: Anchors.Perimeter
  6117. * An Anchor that tracks the perimeter of some shape, approximating it with a given number of dynamically
  6118. * positioned locations.
  6119. *
  6120. * Parameters:
  6121. *
  6122. * anchorCount - optional number of anchors to use to approximate the perimeter. default is 60.
  6123. * shape - required. the name of the shape. valid values are 'rectangle', 'square', 'ellipse', 'circle', 'triangle' and 'diamond'
  6124. * rotation - optional rotation, in degrees, to apply.
  6125. */
  6126. jsPlumb.Anchors["Perimeter"] = function(params) {
  6127. params = params || {};
  6128. var anchorCount = params.anchorCount || 60,
  6129. shape = params.shape;
  6130. if (!shape) throw new Error("no shape supplied to Perimeter Anchor type");
  6131. var _circle = function() {
  6132. var r = 0.5, step = Math.PI * 2 / anchorCount, current = 0, a = [];
  6133. for (var i = 0; i < anchorCount; i++) {
  6134. var x = r + (r * Math.sin(current)),
  6135. y = r + (r * Math.cos(current));
  6136. a.push( [ x, y, 0, 0 ] );
  6137. current += step;
  6138. }
  6139. return a;
  6140. },
  6141. _path = function(segments) {
  6142. var anchorsPerFace = anchorCount / segments.length, a = [],
  6143. _computeFace = function(x1, y1, x2, y2, fractionalLength) {
  6144. anchorsPerFace = anchorCount * fractionalLength;
  6145. var dx = (x2 - x1) / anchorsPerFace, dy = (y2 - y1) / anchorsPerFace;
  6146. for (var i = 0; i < anchorsPerFace; i++) {
  6147. a.push( [
  6148. x1 + (dx * i),
  6149. y1 + (dy * i),
  6150. 0,
  6151. 0
  6152. ]);
  6153. }
  6154. };
  6155. for (var i = 0; i < segments.length; i++)
  6156. _computeFace.apply(null, segments[i]);
  6157. return a;
  6158. },
  6159. _shape = function(faces) {
  6160. var s = [];
  6161. for (var i = 0; i < faces.length; i++) {
  6162. s.push([faces[i][0], faces[i][1], faces[i][2], faces[i][3], 1 / faces.length]);
  6163. }
  6164. return _path(s);
  6165. },
  6166. _rectangle = function() {
  6167. return _shape([
  6168. [ 0, 0, 1, 0 ], [ 1, 0, 1, 1 ], [ 1, 1, 0, 1 ], [ 0, 1, 0, 0 ]
  6169. ]);
  6170. };
  6171. var _shapes = {
  6172. "circle":_circle,
  6173. "ellipse":_circle,
  6174. "diamond":function() {
  6175. return _shape([
  6176. [ 0.5, 0, 1, 0.5 ], [ 1, 0.5, 0.5, 1 ], [ 0.5, 1, 0, 0.5 ], [ 0, 0.5, 0.5, 0 ]
  6177. ]);
  6178. },
  6179. "rectangle":_rectangle,
  6180. "square":_rectangle,
  6181. "triangle":function() {
  6182. return _shape([
  6183. [ 0.5, 0, 1, 1 ], [ 1, 1, 0, 1 ], [ 0, 1, 0.5, 0]
  6184. ]);
  6185. },
  6186. "path":function(params) {
  6187. var points = params.points;
  6188. var p = [], tl = 0;
  6189. for (var i = 0; i < points.length - 1; i++) {
  6190. var l = Math.sqrt(Math.pow(points[i][2] - points[i][0]) + Math.pow(points[i][3] - points[i][1]));
  6191. tl += l;
  6192. p.push([points[i][0], points[i][1], points[i+1][0], points[i+1][1], l]);
  6193. }
  6194. for (var i = 0; i < p.length; i++) {
  6195. p[i][4] = p[i][4] / tl;
  6196. }
  6197. return _path(p);
  6198. }
  6199. },
  6200. _rotate = function(points, amountInDegrees) {
  6201. var o = [], theta = amountInDegrees / 180 * Math.PI ;
  6202. for (var i = 0; i < points.length; i++) {
  6203. var _x = points[i][0] - 0.5,
  6204. _y = points[i][1] - 0.5;
  6205. o.push([
  6206. 0.5 + ((_x * Math.cos(theta)) - (_y * Math.sin(theta))),
  6207. 0.5 + ((_x * Math.sin(theta)) + (_y * Math.cos(theta))),
  6208. points[i][2],
  6209. points[i][3]
  6210. ]);
  6211. }
  6212. return o;
  6213. };
  6214. if (!_shapes[shape]) throw new Error("Shape [" + shape + "] is unknown by Perimeter Anchor type");
  6215. var da = _shapes[shape](params);
  6216. if (params.rotation) da = _rotate(da, params.rotation);
  6217. var a = params.jsPlumbInstance.makeDynamicAnchor(da);
  6218. a.type = "Perimeter";
  6219. return a;
  6220. };
  6221. })();
  6222. /*
  6223. * jsPlumb
  6224. *
  6225. * Title:jsPlumb 1.3.16
  6226. *
  6227. * Provides a way to visually connect elements on an HTML page, using either SVG, Canvas
  6228. * elements, or VML.
  6229. *
  6230. * This file contains the default Connectors, Endpoint and Overlay definitions.
  6231. *
  6232. * Copyright (c) 2010 - 2012 Simon Porritt (http://jsplumb.org)
  6233. *
  6234. * http://jsplumb.org
  6235. * http://github.com/sporritt/jsplumb
  6236. * http://code.google.com/p/jsplumb
  6237. *
  6238. * Dual licensed under the MIT and GPL2 licenses.
  6239. */
  6240. (function() {
  6241. /**
  6242. *
  6243. * Helper class to consume unused mouse events by components that are DOM elements and
  6244. * are used by all of the different rendering modes.
  6245. *
  6246. */
  6247. jsPlumb.DOMElementComponent = function(params) {
  6248. jsPlumb.jsPlumbUIComponent.apply(this, arguments);
  6249. // when render mode is canvas, these functions may be called by the canvas mouse handler.
  6250. // this component is safe to pipe this stuff to /dev/null.
  6251. this.mousemove =
  6252. this.dblclick =
  6253. this.click =
  6254. this.mousedown =
  6255. this.mouseup = function(e) { };
  6256. };
  6257. /**
  6258. * Class: Connectors.Straight
  6259. * The Straight connector draws a simple straight line between the two anchor points. It does not have any constructor parameters.
  6260. */
  6261. jsPlumb.Connectors.Straight = function() {
  6262. this.type = "Straight";
  6263. var self = this,
  6264. currentPoints = null,
  6265. _m, _m2, _b, _dx, _dy, _theta, _theta2, _sx, _sy, _tx, _ty, _segment, _length;
  6266. /**
  6267. * Computes the new size and position of the canvas.
  6268. */
  6269. this.compute = function(sourcePos, targetPos, sourceEndpoint, targetEndpoint, sourceAnchor, targetAnchor, lineWidth, minWidth) {
  6270. var w = Math.abs(sourcePos[0] - targetPos[0]),
  6271. h = Math.abs(sourcePos[1] - targetPos[1]),
  6272. // these are padding to ensure the whole connector line appears
  6273. xo = 0.45 * w, yo = 0.45 * h;
  6274. // these are padding to ensure the whole connector line appears
  6275. w *= 1.9; h *=1.9;
  6276. var x = Math.min(sourcePos[0], targetPos[0]) - xo;
  6277. var y = Math.min(sourcePos[1], targetPos[1]) - yo;
  6278. // minimum size is 2 * line Width if minWidth was not given.
  6279. var calculatedMinWidth = Math.max(2 * lineWidth, minWidth);
  6280. if (w < calculatedMinWidth) {
  6281. w = calculatedMinWidth;
  6282. x = sourcePos[0] + ((targetPos[0] - sourcePos[0]) / 2) - (calculatedMinWidth / 2);
  6283. xo = (w - Math.abs(sourcePos[0]-targetPos[0])) / 2;
  6284. }
  6285. if (h < calculatedMinWidth) {
  6286. h = calculatedMinWidth;
  6287. y = sourcePos[1] + ((targetPos[1] - sourcePos[1]) / 2) - (calculatedMinWidth / 2);
  6288. yo = (h - Math.abs(sourcePos[1]-targetPos[1])) / 2;
  6289. }
  6290. _sx = sourcePos[0] < targetPos[0] ? xo : w-xo;
  6291. _sy = sourcePos[1] < targetPos[1] ? yo:h-yo;
  6292. _tx = sourcePos[0] < targetPos[0] ? w-xo : xo;
  6293. _ty = sourcePos[1] < targetPos[1] ? h-yo : yo;
  6294. currentPoints = [ x, y, w, h, _sx, _sy, _tx, _ty ];
  6295. _dx = _tx - _sx, _dy = _ty - _sy;
  6296. //_m = _dy / _dx, _m2 = -1 / _m;
  6297. _m = jsPlumbUtil.gradient({x:_sx, y:_sy}, {x:_tx, y:_ty}), _m2 = -1 / _m;
  6298. _b = -1 * ((_m * _sx) - _sy);
  6299. _theta = Math.atan(_m); _theta2 = Math.atan(_m2);
  6300. //_segment = jsPlumbUtil.segment({x:_sx, y:_sy}, {x:_tx, y:_ty});
  6301. _length = Math.sqrt((_dx * _dx) + (_dy * _dy));
  6302. return currentPoints;
  6303. };
  6304. /**
  6305. * returns the point on the connector's path that is 'location' along the length of the path, where 'location' is a decimal from
  6306. * 0 to 1 inclusive. for the straight line connector this is simple maths. for Bezier, not so much.
  6307. */
  6308. this.pointOnPath = function(location, absolute) {
  6309. if (location == 0 && !absolute)
  6310. return { x:_sx, y:_sy };
  6311. else if (location == 1 && !absolute)
  6312. return { x:_tx, y:_ty };
  6313. else {
  6314. var l = absolute ? location > 0 ? location : _length + location : location * _length;
  6315. return jsPlumbUtil.pointOnLine({x:_sx, y:_sy}, {x:_tx, y:_ty}, l);
  6316. }
  6317. };
  6318. /**
  6319. * returns the gradient of the connector at the given point - which for us is constant.
  6320. */
  6321. this.gradientAtPoint = function(location) {
  6322. return _m;
  6323. };
  6324. /**
  6325. * returns the point on the connector's path that is 'distance' along the length of the path from 'location', where
  6326. * 'location' is a decimal from 0 to 1 inclusive, and 'distance' is a number of pixels.
  6327. * this hands off to jsPlumbUtil to do the maths, supplying two points and the distance.
  6328. */
  6329. this.pointAlongPathFrom = function(location, distance, absolute) {
  6330. var p = self.pointOnPath(location, absolute),
  6331. farAwayPoint = location == 1 ? {
  6332. x:_sx + ((_tx - _sx) * 10),
  6333. y:_sy + ((_sy - _ty) * 10)
  6334. } : distance <= 0 ? {x:_sx, y:_sy} : {x:_tx, y:_ty };
  6335. if (distance <= 0 && Math.abs(distance) > 1) distance *= -1;
  6336. return jsPlumbUtil.pointOnLine(p, farAwayPoint, distance);
  6337. };
  6338. };
  6339. /**
  6340. * Class:Connectors.Bezier
  6341. * This Connector draws a Bezier curve with two control points. You can provide a 'curviness' value which gets applied to jsPlumb's
  6342. * internal voodoo machine and ends up generating locations for the two control points. See the constructor documentation below.
  6343. */
  6344. /**
  6345. * Function:Constructor
  6346. *
  6347. * Parameters:
  6348. * curviness - How 'curvy' you want the curve to be! This is a directive for the placement of control points, not endpoints of the curve, so your curve does not
  6349. * actually touch the given point, but it has the tendency to lean towards it. The larger this value, the greater the curve is pulled from a straight line.
  6350. * Optional; defaults to 150.
  6351. * stub - optional value for a distance to travel from the connector's endpoint before beginning the Bezier curve. defaults to 0.
  6352. *
  6353. */
  6354. jsPlumb.Connectors.Bezier = function(params) {
  6355. var self = this;
  6356. params = params || {};
  6357. this.majorAnchor = params.curviness || 150;
  6358. this.minorAnchor = 10;
  6359. var currentPoints = null;
  6360. this.type = "Bezier";
  6361. this._findControlPoint = function(point, sourceAnchorPosition, targetAnchorPosition, sourceEndpoint, targetEndpoint, sourceAnchor, targetAnchor) {
  6362. // determine if the two anchors are perpendicular to each other in their orientation. we swap the control
  6363. // points around if so (code could be tightened up)
  6364. var soo = sourceAnchor.getOrientation(sourceEndpoint),
  6365. too = targetAnchor.getOrientation(targetEndpoint),
  6366. perpendicular = soo[0] != too[0] || soo[1] == too[1],
  6367. p = [],
  6368. ma = self.majorAnchor, mi = self.minorAnchor;
  6369. if (!perpendicular) {
  6370. if (soo[0] == 0) // X
  6371. p.push(sourceAnchorPosition[0] < targetAnchorPosition[0] ? point[0] + mi : point[0] - mi);
  6372. else p.push(point[0] - (ma * soo[0]));
  6373. if (soo[1] == 0) // Y
  6374. p.push(sourceAnchorPosition[1] < targetAnchorPosition[1] ? point[1] + mi : point[1] - mi);
  6375. else p.push(point[1] + (ma * too[1]));
  6376. }
  6377. else {
  6378. if (too[0] == 0) // X
  6379. p.push(targetAnchorPosition[0] < sourceAnchorPosition[0] ? point[0] + mi : point[0] - mi);
  6380. else p.push(point[0] + (ma * too[0]));
  6381. if (too[1] == 0) // Y
  6382. p.push(targetAnchorPosition[1] < sourceAnchorPosition[1] ? point[1] + mi : point[1] - mi);
  6383. else p.push(point[1] + (ma * soo[1]));
  6384. }
  6385. return p;
  6386. };
  6387. var _CP, _CP2, _sx, _tx, _ty, _sx, _sy, _canvasX, _canvasY, _w, _h, _sStubX, _sStubY, _tStubX, _tStubY;
  6388. this.compute = function(sourcePos, targetPos, sourceEndpoint, targetEndpoint, sourceAnchor, targetAnchor, lineWidth, minWidth) {
  6389. // this calculation gives us what the minimum distance between either start or end
  6390. // should be from the edge of the canvas.
  6391. lineWidth = Math.max(minWidth, (lineWidth || 0));
  6392. _w = Math.abs(sourcePos[0] - targetPos[0]) + lineWidth;
  6393. _h = Math.abs(sourcePos[1] - targetPos[1]) + lineWidth;
  6394. _canvasX = Math.min(sourcePos[0], targetPos[0])-(lineWidth/2);
  6395. _canvasY = Math.min(sourcePos[1], targetPos[1])-(lineWidth/2);
  6396. _sx = sourcePos[0] < targetPos[0] ? _w - (lineWidth/2): (lineWidth/2);
  6397. _sy = sourcePos[1] < targetPos[1] ? _h - (lineWidth/2) : (lineWidth/2);
  6398. _tx = sourcePos[0] < targetPos[0] ? (lineWidth/2) : _w - (lineWidth/2);
  6399. _ty = sourcePos[1] < targetPos[1] ? (lineWidth/2) : _h - (lineWidth/2);
  6400. _CP = self._findControlPoint([_sx,_sy], sourcePos, targetPos, sourceEndpoint, targetEndpoint, sourceAnchor, targetAnchor);
  6401. _CP2 = self._findControlPoint([_tx,_ty], targetPos, sourcePos, targetEndpoint, sourceEndpoint, targetAnchor, sourceAnchor);
  6402. var minx1 = Math.min(_sx,_tx), minx2 = Math.min(_CP[0], _CP2[0]), minx = Math.min(minx1,minx2),
  6403. maxx1 = Math.max(_sx,_tx), maxx2 = Math.max(_CP[0], _CP2[0]), maxx = Math.max(maxx1,maxx2);
  6404. if (maxx > _w) _w = maxx;
  6405. if (minx < 0) {
  6406. _canvasX += minx; var ox = Math.abs(minx);
  6407. _w += ox; _CP[0] += ox; _sx += ox; _tx +=ox; _CP2[0] += ox;
  6408. }
  6409. var miny1 = Math.min(_sy,_ty), miny2 = Math.min(_CP[1], _CP2[1]), miny = Math.min(miny1,miny2),
  6410. maxy1 = Math.max(_sy,_ty), maxy2 = Math.max(_CP[1], _CP2[1]), maxy = Math.max(maxy1,maxy2);
  6411. if (maxy > _h) _h = maxy;
  6412. if (miny < 0) {
  6413. _canvasY += miny; var oy = Math.abs(miny);
  6414. _h += oy; _CP[1] += oy; _sy += oy; _ty +=oy; _CP2[1] += oy;
  6415. }
  6416. if (minWidth && _w < minWidth) {
  6417. var posAdjust = (minWidth - _w) / 2;
  6418. _w = minWidth;
  6419. _canvasX -= posAdjust; _sx = _sx + posAdjust ; _tx = _tx + posAdjust; _CP[0] = _CP[0] + posAdjust; _CP2[0] = _CP2[0] + posAdjust;
  6420. }
  6421. if (minWidth && _h < minWidth) {
  6422. var posAdjust = (minWidth - _h) / 2;
  6423. _h = minWidth;
  6424. _canvasY -= posAdjust; _sy = _sy + posAdjust ; _ty = _ty + posAdjust; _CP[1] = _CP[1] + posAdjust; _CP2[1] = _CP2[1] + posAdjust;
  6425. }
  6426. currentPoints = [_canvasX, _canvasY, _w, _h,
  6427. _sx, _sy, _tx, _ty,
  6428. _CP[0], _CP[1], _CP2[0], _CP2[1] ];
  6429. return currentPoints;
  6430. };
  6431. var _makeCurve = function() {
  6432. return [
  6433. { x:_sx, y:_sy },
  6434. { x:_CP[0], y:_CP[1] },
  6435. { x:_CP2[0], y:_CP2[1] },
  6436. { x:_tx, y:_ty }
  6437. ];
  6438. };
  6439. var _translateLocation = function(curve, location, absolute) {
  6440. if (absolute)
  6441. location = jsBezier.locationAlongCurveFrom(curve, location > 0 ? 0 : 1, location);
  6442. return location;
  6443. };
  6444. /**
  6445. * returns the point on the connector's path that is 'location' along the length of the path, where 'location' is a decimal from
  6446. * 0 to 1 inclusive. for the straight line connector this is simple maths. for Bezier, not so much.
  6447. */
  6448. this.pointOnPath = function(location, absolute) {
  6449. var c = _makeCurve();
  6450. location = _translateLocation(c, location, absolute);
  6451. return jsBezier.pointOnCurve(c, location);
  6452. };
  6453. /**
  6454. * returns the gradient of the connector at the given point.
  6455. */
  6456. this.gradientAtPoint = function(location, absolute) {
  6457. var c = _makeCurve();
  6458. location = _translateLocation(c, location, absolute);
  6459. return jsBezier.gradientAtPoint(c, location);
  6460. };
  6461. /**
  6462. * for Bezier curves this method is a little tricky, cos calculating path distance algebraically is notoriously difficult.
  6463. * this method is iterative, jumping forward .05% of the path at a time and summing the distance between this point and the previous
  6464. * one, until the sum reaches 'distance'. the method may turn out to be computationally expensive; we'll see.
  6465. * another drawback of this method is that if the connector gets quite long, .05% of the length of it is not necessarily smaller
  6466. * than the desired distance, in which case the loop returns immediately and the arrow is mis-shapen. so a better strategy might be to
  6467. * calculate the step as a function of distance/distance between endpoints.
  6468. */
  6469. this.pointAlongPathFrom = function(location, distance, absolute) {
  6470. var c = _makeCurve();
  6471. location = _translateLocation(c, location, absolute);
  6472. return jsBezier.pointAlongCurveFrom(c, location, distance);
  6473. };
  6474. };
  6475. /**
  6476. * Class: Connectors.Flowchart
  6477. * Provides 'flowchart' connectors, consisting of vertical and horizontal line segments.
  6478. */
  6479. /**
  6480. * Function: Constructor
  6481. *
  6482. * Parameters:
  6483. * 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,
  6484. * or an array of two integers, giving separate values for each end. The default is an integer with value 30 (pixels).
  6485. * 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. defaults to 0 pixels.
  6486. */
  6487. jsPlumb.Connectors.Flowchart = function(params) {
  6488. this.type = "Flowchart";
  6489. params = params || {};
  6490. var self = this,
  6491. stub = params.stub || params.minStubLength /* bwds compat. */ || 30,
  6492. sourceStub = jsPlumbUtil.isArray(stub) ? stub[0] : stub,
  6493. targetStub = jsPlumbUtil.isArray(stub) ? stub[1] : stub,
  6494. gap = params.gap || 0,
  6495. midpoint = params.midpoint || 0.5,
  6496. segments = [],
  6497. totalLength = 0,
  6498. segmentProportions = [],
  6499. segmentProportionalLengths = [],
  6500. points = [],
  6501. swapX, swapY,
  6502. maxX = -Infinity, maxY = -Infinity,
  6503. minX = Infinity, minY = Infinity,
  6504. grid = params.grid,
  6505. _gridClamp = function(n, g) { var e = n % g, f = Math.floor(n / g), inc = e > (g / 2) ? 1 : 0; return (f + inc) * g; },
  6506. clampToGrid = function(x, y, dontClampX, dontClampY) {
  6507. return [
  6508. dontClampX || grid == null ? x : _gridClamp(x, grid[0]),
  6509. dontClampY || grid == null ? y : _gridClamp(y, grid[1])
  6510. ];
  6511. },
  6512. /**
  6513. * recalculates the points at which the segments begin and end, proportional to the total length travelled
  6514. * by all the segments that constitute the connector. we use this to help with pointOnPath calculations.
  6515. */
  6516. updateSegmentProportions = function(startX, startY, endX, endY) {
  6517. var curLoc = 0;
  6518. for (var i = 0; i < segments.length; i++) {
  6519. segmentProportionalLengths[i] = segments[i][5] / totalLength;
  6520. segmentProportions[i] = [curLoc, (curLoc += (segments[i][5] / totalLength)) ];
  6521. }
  6522. },
  6523. appendSegmentsToPoints = function() {
  6524. points.push(segments.length);
  6525. for (var i = 0; i < segments.length; i++) {
  6526. points.push(segments[i][0]);
  6527. points.push(segments[i][1]);
  6528. }
  6529. },
  6530. /**
  6531. * helper method to add a segment.
  6532. */
  6533. addSegment = function(x, y, sx, sy, tx, ty/*, doGridX, doGridY*/) {
  6534. var lx = segments.length == 0 ? sx : segments[segments.length - 1][0],
  6535. ly = segments.length == 0 ? sy : segments[segments.length - 1][1],
  6536. m = x == lx ? Infinity : 0;
  6537. /*,
  6538. gridded = clampToGrid(x, y),
  6539. doGridX = true,
  6540. doGridY = true;
  6541. // grid experiment. TODO: have two more params that indicate whether or not to lock to a grid in each
  6542. // axis. the reason for this is that anchor points wont always be located on the grid, so until a connector
  6543. // emanating from that anchor has turned a right angle, we can't actually clamp it to a grid for that axis.
  6544. // so if a line came out horizontally heading left, then it will probably not be clamped in the y axis, but
  6545. // we can choose to clamp its first corner in the x axis. the same principle goes for the target anchor.
  6546. //if (segments.length == 0) {
  6547. console.log("this is the first segment...if sx == x then do not do grid in X.")
  6548. doGridX = !(sx == x) && !(tx == x);
  6549. doGridY = !(sy == y) && !(ty == y);
  6550. x = doGridX ? gridded[0] : x;
  6551. y = doGridY ? gridded[1] : y;
  6552. */
  6553. var l = Math.abs(x == lx ? y - ly : x - lx);
  6554. segments.push([x, y, lx, ly, m, l]);
  6555. totalLength += l;
  6556. maxX = Math.max(maxX, x);
  6557. maxY = Math.max(maxY, y);
  6558. minX = Math.min(minX, x);
  6559. minY = Math.min(minY, y);
  6560. },
  6561. /**
  6562. * returns [segment, proportion of travel in segment, segment index] for the segment
  6563. * that contains the point which is 'location' distance along the entire path, where
  6564. * 'location' is a decimal between 0 and 1 inclusive. in this connector type, paths
  6565. * are made up of a list of segments, each of which contributes some fraction to
  6566. * the total length.
  6567. * From 1.3.10 this also supports the 'absolute' property, which lets us specify a location
  6568. * as the absolute distance in pixels, rather than a proportion of the total path.
  6569. */
  6570. findSegmentForLocation = function(location, absolute) {
  6571. if (absolute) {
  6572. location = location > 0 ? location / totalLength : (totalLength + location) / totalLength;
  6573. }
  6574. var idx = segmentProportions.length - 1, inSegmentProportion = 1;
  6575. for (var i = 0; i < segmentProportions.length; i++) {
  6576. if (segmentProportions[i][1] >= location) {
  6577. idx = i;
  6578. inSegmentProportion = (location - segmentProportions[i][0]) / segmentProportionalLengths[i];
  6579. break;
  6580. }
  6581. }
  6582. return { segment:segments[idx], proportion:inSegmentProportion, index:idx };
  6583. };
  6584. this.compute = function(sourcePos, targetPos, sourceEndpoint, targetEndpoint,
  6585. sourceAnchor, targetAnchor, lineWidth, minWidth, sourceInfo, targetInfo) {
  6586. segments = [];
  6587. segmentProportions = [];
  6588. totalLength = 0;
  6589. segmentProportionalLengths = [];
  6590. maxX = maxY = -Infinity;
  6591. minX = minY = Infinity;
  6592. self.lineWidth = lineWidth;
  6593. swapX = targetPos[0] < sourcePos[0];
  6594. swapY = targetPos[1] < sourcePos[1];
  6595. var lw = lineWidth || 1,
  6596. sourceOffx = (lw / 2) + (sourceStub + targetStub),
  6597. targetOffx = (lw / 2) + (targetStub + sourceStub),
  6598. sourceOffy = (lw / 2) + (sourceStub + targetStub),
  6599. targetOffy = (lw / 2) + (targetStub + sourceStub),
  6600. so = sourceAnchor.orientation || sourceAnchor.getOrientation(sourceEndpoint),
  6601. to = targetAnchor.orientation || targetAnchor.getOrientation(targetEndpoint),
  6602. x = swapX ? targetPos[0] : sourcePos[0],
  6603. y = swapY ? targetPos[1] : sourcePos[1],
  6604. w = Math.abs(targetPos[0] - sourcePos[0]) + sourceOffx + targetOffx,
  6605. h = Math.abs(targetPos[1] - sourcePos[1]) + sourceOffy + targetOffy;
  6606. // if either anchor does not have an orientation set, we derive one from their relative
  6607. // positions. we fix the axis to be the one in which the two elements are further apart, and
  6608. // point each anchor at the other element. this is also used when dragging a new connection.
  6609. if (so[0] == 0 && so[1] == 0 || to[0] == 0 && to[1] == 0) {
  6610. var index = w > h ? 0 : 1, oIndex = [1,0][index];
  6611. so = []; to = [];
  6612. so[index] = sourcePos[index] > targetPos[index] ? -1 : 1;
  6613. to[index] = sourcePos[index] > targetPos[index] ? 1 : -1;
  6614. so[oIndex] = 0;
  6615. to[oIndex] = 0;
  6616. }
  6617. /*
  6618. this code is unexplained and causes paint errors with continuous anchors sometimes.
  6619. commenting it out until i can get to the bottom of it.
  6620. if (w < minWidth) {
  6621. sourceOffx += (minWidth - w) / 2;
  6622. w = minWidth;
  6623. }
  6624. if (h < minWidth) {
  6625. sourceOffy += (minWidth - h) / 2;
  6626. h = minWidth;
  6627. }
  6628. */
  6629. var sx = swapX ? (w - targetOffx) +( gap * so[0]) : sourceOffx + (gap * so[0]),
  6630. sy = swapY ? (h - targetOffy) + (gap * so[1]) : sourceOffy + (gap * so[1]),
  6631. tx = swapX ? sourceOffx + (gap * to[0]) : (w - targetOffx) + (gap * to[0]),
  6632. ty = swapY ? sourceOffy + (gap * to[1]) : (h - targetOffy) + (gap * to[1]),
  6633. startStubX = sx + (so[0] * sourceStub),
  6634. startStubY = sy + (so[1] * sourceStub),
  6635. endStubX = tx + (to[0] * targetStub),
  6636. endStubY = ty + (to[1] * targetStub),
  6637. isXGreaterThanStubTimes2 = Math.abs(sx - tx) > (sourceStub + targetStub),
  6638. isYGreaterThanStubTimes2 = Math.abs(sy - ty) > (sourceStub + targetStub),
  6639. midx = startStubX + ((endStubX - startStubX) * midpoint),
  6640. midy = startStubY + ((endStubY - startStubY) * midpoint),
  6641. oProduct = ((so[0] * to[0]) + (so[1] * to[1])),
  6642. opposite = oProduct == -1,
  6643. perpendicular = oProduct == 0,
  6644. orthogonal = oProduct == 1;
  6645. x -= sourceOffx; y -= sourceOffy;
  6646. points = [x, y, w, h, sx, sy, tx, ty];
  6647. var extraPoints = [];
  6648. var sourceAxis = so[0] == 0 ? "y" : "x",
  6649. anchorOrientation = opposite ? "opposite" : orthogonal ? "orthogonal" : "perpendicular",
  6650. segment = jsPlumbUtil.segment([sx, sy], [tx, ty]),
  6651. flipSourceSegments = so[sourceAxis == "x" ? 0 : 1] == -1,
  6652. flipSegments = {
  6653. "x":[null, 4, 3, 2, 1],
  6654. "y":[null, 2, 1, 4, 3]
  6655. }
  6656. if (flipSourceSegments)
  6657. segment = flipSegments[sourceAxis][segment];
  6658. addSegment(startStubX, startStubY, sx, sy, tx, ty);
  6659. var findClearedLine = function(start, mult, anchorPos, dimension) {
  6660. return start + (mult * (( 1 - anchorPos) * dimension) + Math.max(sourceStub, targetStub));
  6661. //mx = so[0] == 0 ? startStubX + ((1 - sourceAnchor.x) * sourceInfo.width) + stub : startStubX,
  6662. },
  6663. lineCalculators = {
  6664. oppositex : function() {
  6665. if (sourceEndpoint.elementId == targetEndpoint.elementId) {
  6666. var _y = startStubY + ((1 - sourceAnchor.y) * sourceInfo.height) + Math.max(sourceStub, targetStub);
  6667. return [ [ startStubX, _y ], [ endStubX, _y ]];
  6668. }
  6669. else if (isXGreaterThanStubTimes2 && (segment == 1 || segment == 2)) {
  6670. return [[ midx, sy ], [ midx, ty ]];
  6671. }
  6672. else {
  6673. return [[ startStubX, midy ], [endStubX, midy ]];
  6674. }
  6675. },
  6676. orthogonalx : function() {
  6677. if (segment == 1 || segment == 2) {
  6678. return [ [ endStubX, startStubY ]];
  6679. }
  6680. else {
  6681. return [ [ startStubX, endStubY ]];
  6682. }
  6683. },
  6684. perpendicularx : function() {
  6685. var my = (ty + sy) / 2;
  6686. if ((segment == 1 && to[1] == 1) || (segment == 2 && to[1] == -1)) {
  6687. if (Math.abs(tx - sx) > Math.max(sourceStub, targetStub))
  6688. return [ [endStubX, startStubY ]];
  6689. else
  6690. return [ [startStubX, startStubY ], [ startStubX, my ], [ endStubX, my ]];
  6691. }
  6692. else if ((segment == 3 && to[1] == -1) || (segment == 4 && to[1] == 1)) {
  6693. return [ [ startStubX, my ], [ endStubX, my ]];
  6694. }
  6695. else if ((segment == 3 && to[1] == 1) || (segment == 4 && to[1] == -1)) {
  6696. return [ [ startStubX, endStubY ]];
  6697. }
  6698. else if ((segment == 1 && to[1] == -1) || (segment == 2 && to[1] == 1)) {
  6699. if (Math.abs(tx - sx) > Math.max(sourceStub, targetStub))
  6700. return [ [ midx, startStubY ], [ midx, endStubY ]];
  6701. else
  6702. return [ [ startStubX, endStubY ]];
  6703. }
  6704. },
  6705. oppositey : function() {
  6706. if (sourceEndpoint.elementId == targetEndpoint.elementId) {
  6707. var _x = startStubX + ((1 - sourceAnchor.x) * sourceInfo.width) + Math.max(sourceStub, targetStub);
  6708. return [ [ _x, startStubY ], [ _x, endStubY ]];
  6709. }
  6710. else if (isYGreaterThanStubTimes2 && (segment == 2 || segment == 3)) {
  6711. return [[ sx, midy ], [ tx, midy ]];
  6712. }
  6713. else {
  6714. return [[ midx, startStubY ], [midx, endStubY ]];
  6715. }
  6716. },
  6717. orthogonaly : function() {
  6718. if (segment == 2 || segment == 3) {
  6719. return [ [ startStubX, endStubY ]];
  6720. }
  6721. else {
  6722. return [ [ endStubX, startStubY ]];
  6723. }
  6724. },
  6725. perpendiculary : function() {
  6726. var mx = (tx + sx) / 2;
  6727. if ((segment == 2 && to[0] == -1) || (segment == 3 && to[0] == 1)) {
  6728. if (Math.abs(tx - sx) > Math.max(sourceStub, targetStub))
  6729. return [ [startStubX, endStubY ]];
  6730. else
  6731. return [ [startStubX, midy ], [ endStubX, midy ]];
  6732. }
  6733. else if ((segment == 1 && to[0] == -1) || (segment == 4 && to[0] == 1)) {
  6734. var mx = (tx + sx) / 2;
  6735. return [ [ mx, startStubY ], [ mx, endStubY ]];
  6736. }
  6737. else if ((segment == 1 && to[0] == 1) || (segment == 4 && to[0] == -1)) {
  6738. return [ [ endStubX, startStubY ]];
  6739. }
  6740. else if ((segment == 2 && to[0] == 1) || (segment == 3 && to[0] == -1)) {
  6741. if (Math.abs(ty - sy) > Math.max(sourceStub, targetStub))
  6742. return [ [ startStubX, midy ], [ endStubX, midy ]];
  6743. else
  6744. return [ [ endStubX, startStubY ]];
  6745. }
  6746. }
  6747. };
  6748. var p = lineCalculators[anchorOrientation + sourceAxis]();
  6749. if (p) {
  6750. for (var i = 0; i < p.length; i++) {
  6751. addSegment(p[i][0], p[i][1], sx, sy, tx, ty);
  6752. }
  6753. }
  6754. addSegment(endStubX, endStubY, sx, sy, tx, ty);
  6755. addSegment(tx, ty, sx, sy, tx, ty);
  6756. appendSegmentsToPoints();
  6757. updateSegmentProportions(sx, sy, tx, ty);
  6758. // adjust the max values of the canvas if we have a value that is larger than what we previously set.
  6759. //
  6760. if (maxY > points[3]) points[3] = maxY + (lineWidth * 2);
  6761. if (maxX > points[2]) points[2] = maxX + (lineWidth * 2);
  6762. return points;
  6763. };
  6764. /**
  6765. * returns the point on the connector's path that is 'location' along the length of the path, where 'location' is a decimal from
  6766. * 0 to 1 inclusive. for this connector we must first figure out which segment the given point lies in, and then compute the x,y position
  6767. * from our knowledge of the segment's start and end points.
  6768. */
  6769. this.pointOnPath = function(location, absolute) {
  6770. return self.pointAlongPathFrom(location, 0, absolute);
  6771. };
  6772. /**
  6773. * returns the gradient of the connector at the given point; the gradient will be either 0 or Infinity, depending on the direction of the
  6774. * segment the point falls in. segment gradients are calculated in the compute method.
  6775. */
  6776. this.gradientAtPoint = function(location, absolute) {
  6777. return segments[findSegmentForLocation(location, absolute)["index"]][4];
  6778. };
  6779. /**
  6780. * returns the point on the connector's path that is 'distance' along the length of the path from 'location', where
  6781. * 'location' is a decimal from 0 to 1 inclusive, and 'distance' is a number of pixels. when you consider this concept from the point of view
  6782. * of this connector, it starts to become clear that there's a problem with the overlay paint code: given that this connector makes several
  6783. * 90 degree turns, it's entirely possible that an arrow overlay could be forced to paint itself around a corner, which would look stupid. this is
  6784. * because jsPlumb uses this method (and pointOnPath) so determine the locations of the various points that go to make up an overlay. a better
  6785. * solution would probably be to just use pointOnPath along with gradientAtPoint, and draw the overlay so that its axis ran along
  6786. * a tangent to the connector. for straight line connectors this would obviously mean the overlay was painted directly on the connector, since a
  6787. * tangent to a straight line is the line itself, which is what we want; for this connector, and for beziers, the results would probably be better. an additional
  6788. * advantage is, of course, that there's less computation involved doing it that way.
  6789. */
  6790. this.pointAlongPathFrom = function(location, distance, absolute) {
  6791. var s = findSegmentForLocation(location, absolute), seg = s.segment, p = s.proportion, sl = segments[s.index][5], m = segments[s.index][4];
  6792. var e = {
  6793. x : m == Infinity ? seg[2] : seg[2] > seg[0] ? seg[0] + ((1 - p) * sl) - distance : seg[2] + (p * sl) + distance,
  6794. y : m == 0 ? seg[3] : seg[3] > seg[1] ? seg[1] + ((1 - p) * sl) - distance : seg[3] + (p * sl) + distance,
  6795. segmentInfo : s
  6796. };
  6797. return e;
  6798. };
  6799. };
  6800. // ********************************* END OF CONNECTOR TYPES *******************************************************************
  6801. // ********************************* ENDPOINT TYPES *******************************************************************
  6802. /**
  6803. * Class: Endpoints.Dot
  6804. * A round endpoint, with default radius 10 pixels.
  6805. */
  6806. /**
  6807. * Function: Constructor
  6808. *
  6809. * Parameters:
  6810. *
  6811. * radius - radius of the endpoint. defaults to 10 pixels.
  6812. */
  6813. jsPlumb.Endpoints.Dot = function(params) {
  6814. this.type = "Dot";
  6815. var self = this;
  6816. params = params || {};
  6817. this.radius = params.radius || 10;
  6818. this.defaultOffset = 0.5 * this.radius;
  6819. this.defaultInnerRadius = this.radius / 3;
  6820. this.compute = function(anchorPoint, orientation, endpointStyle, connectorPaintStyle) {
  6821. var r = endpointStyle.radius || self.radius,
  6822. x = anchorPoint[0] - r,
  6823. y = anchorPoint[1] - r;
  6824. return [ x, y, r * 2, r * 2, r ];
  6825. };
  6826. };
  6827. /**
  6828. * Class: Endpoints.Rectangle
  6829. * A Rectangular Endpoint, with default size 20x20.
  6830. */
  6831. /**
  6832. * Function: Constructor
  6833. *
  6834. * Parameters:
  6835. *
  6836. * width - width of the endpoint. defaults to 20 pixels.
  6837. * height - height of the endpoint. defaults to 20 pixels.
  6838. */
  6839. jsPlumb.Endpoints.Rectangle = function(params) {
  6840. this.type = "Rectangle";
  6841. var self = this;
  6842. params = params || {};
  6843. this.width = params.width || 20;
  6844. this.height = params.height || 20;
  6845. this.compute = function(anchorPoint, orientation, endpointStyle, connectorPaintStyle) {
  6846. var width = endpointStyle.width || self.width,
  6847. height = endpointStyle.height || self.height,
  6848. x = anchorPoint[0] - (width/2),
  6849. y = anchorPoint[1] - (height/2);
  6850. return [ x, y, width, height];
  6851. };
  6852. };
  6853. var DOMElementEndpoint = function(params) {
  6854. jsPlumb.DOMElementComponent.apply(this, arguments);
  6855. var self = this;
  6856. var displayElements = [ ];
  6857. this.getDisplayElements = function() {
  6858. return displayElements;
  6859. };
  6860. this.appendDisplayElement = function(el) {
  6861. displayElements.push(el);
  6862. };
  6863. };
  6864. /**
  6865. * Class: Endpoints.Image
  6866. * Draws an image as the Endpoint.
  6867. */
  6868. /**
  6869. * Function: Constructor
  6870. *
  6871. * Parameters:
  6872. *
  6873. * src - location of the image to use.
  6874. */
  6875. jsPlumb.Endpoints.Image = function(params) {
  6876. this.type = "Image";
  6877. DOMElementEndpoint.apply(this, arguments);
  6878. var self = this,
  6879. initialized = false,
  6880. deleted = false,
  6881. widthToUse = params.width,
  6882. heightToUse = params.height,
  6883. _onload = null,
  6884. _endpoint = params.endpoint;
  6885. this.img = new Image();
  6886. self.ready = false;
  6887. this.img.onload = function() {
  6888. self.ready = true;
  6889. widthToUse = widthToUse || self.img.width;
  6890. heightToUse = heightToUse || self.img.height;
  6891. if (_onload) {
  6892. _onload(self);
  6893. }
  6894. };
  6895. /*
  6896. Function: setImage
  6897. Sets the Image to use in this Endpoint.
  6898. Parameters:
  6899. img - may be a URL or an Image object
  6900. onload - optional; a callback to execute once the image has loaded.
  6901. */
  6902. _endpoint.setImage = function(img, onload) {
  6903. var s = img.constructor == String ? img : img.src;
  6904. _onload = onload;
  6905. self.img.src = img;
  6906. if (self.canvas != null)
  6907. self.canvas.setAttribute("src", img);
  6908. };
  6909. _endpoint.setImage(params.src || params.url, params.onload);
  6910. this.compute = function(anchorPoint, orientation, endpointStyle, connectorPaintStyle) {
  6911. self.anchorPoint = anchorPoint;
  6912. if (self.ready) return [anchorPoint[0] - widthToUse / 2, anchorPoint[1] - heightToUse / 2,
  6913. widthToUse, heightToUse];
  6914. else return [0,0,0,0];
  6915. };
  6916. self.canvas = document.createElement("img"), initialized = false;
  6917. self.canvas.style["margin"] = 0;
  6918. self.canvas.style["padding"] = 0;
  6919. self.canvas.style["outline"] = 0;
  6920. self.canvas.style["position"] = "absolute";
  6921. var clazz = params.cssClass ? " " + params.cssClass : "";
  6922. self.canvas.className = jsPlumb.endpointClass + clazz;
  6923. if (widthToUse) self.canvas.setAttribute("width", widthToUse);
  6924. if (heightToUse) self.canvas.setAttribute("height", heightToUse);
  6925. jsPlumb.appendElement(self.canvas, params.parent);
  6926. self.attachListeners(self.canvas, self);
  6927. self.cleanup = function() {
  6928. deleted = true;
  6929. };
  6930. var actuallyPaint = function(d, style, anchor) {
  6931. if (!deleted) {
  6932. if (!initialized) {
  6933. self.canvas.setAttribute("src", self.img.src);
  6934. self.appendDisplayElement(self.canvas);
  6935. initialized = true;
  6936. }
  6937. var x = self.anchorPoint[0] - (widthToUse / 2),
  6938. y = self.anchorPoint[1] - (heightToUse / 2);
  6939. jsPlumb.sizeCanvas(self.canvas, x, y, widthToUse, heightToUse);
  6940. }
  6941. };
  6942. this.paint = function(d, style, anchor) {
  6943. if (self.ready) {
  6944. actuallyPaint(d, style, anchor);
  6945. }
  6946. else {
  6947. window.setTimeout(function() {
  6948. self.paint(d, style, anchor);
  6949. }, 200);
  6950. }
  6951. };
  6952. };
  6953. /**
  6954. * Class: Endpoints.Blank
  6955. * An Endpoint that paints nothing (visible) on the screen. Supports cssClass and hoverClass parameters like all Endpoints.
  6956. */
  6957. jsPlumb.Endpoints.Blank = function(params) {
  6958. var self = this;
  6959. this.type = "Blank";
  6960. DOMElementEndpoint.apply(this, arguments);
  6961. this.compute = function(anchorPoint, orientation, endpointStyle, connectorPaintStyle) {
  6962. return [anchorPoint[0], anchorPoint[1],10,0];
  6963. };
  6964. self.canvas = document.createElement("div");
  6965. self.canvas.style.display = "block";
  6966. self.canvas.style.width = "1px";
  6967. self.canvas.style.height = "1px";
  6968. self.canvas.style.background = "transparent";
  6969. self.canvas.style.position = "absolute";
  6970. self.canvas.className = self._jsPlumb.endpointClass;
  6971. jsPlumb.appendElement(self.canvas, params.parent);
  6972. this.paint = function(d, style, anchor) {
  6973. jsPlumb.sizeCanvas(self.canvas, d[0], d[1], d[2], d[3]);
  6974. };
  6975. };
  6976. /**
  6977. * Class: Endpoints.Triangle
  6978. * A triangular Endpoint.
  6979. */
  6980. /**
  6981. * Function: Constructor
  6982. *
  6983. * Parameters:
  6984. *
  6985. * width - width of the triangle's base. defaults to 55 pixels.
  6986. * height - height of the triangle from base to apex. defaults to 55 pixels.
  6987. */
  6988. jsPlumb.Endpoints.Triangle = function(params) {
  6989. this.type = "Triangle";
  6990. params = params || { };
  6991. params.width = params.width || 55;
  6992. params.height = params.height || 55;
  6993. this.width = params.width;
  6994. this.height = params.height;
  6995. this.compute = function(anchorPoint, orientation, endpointStyle, connectorPaintStyle) {
  6996. var width = endpointStyle.width || self.width,
  6997. height = endpointStyle.height || self.height,
  6998. x = anchorPoint[0] - (width/2),
  6999. y = anchorPoint[1] - (height/2);
  7000. return [ x, y, width, height ];
  7001. };
  7002. };
  7003. // ********************************* END OF ENDPOINT TYPES *******************************************************************
  7004. // ********************************* OVERLAY DEFINITIONS ***********************************************************************
  7005. var AbstractOverlay = function(params) {
  7006. var visible = true, self = this;
  7007. this.isAppendedAtTopLevel = true;
  7008. this.component = params.component;
  7009. this.loc = params.location == null ? 0.5 : params.location;
  7010. this.endpointLoc = params.endpointLocation == null ? [ 0.5, 0.5] : params.endpointLocation;
  7011. this.setVisible = function(val) {
  7012. visible = val;
  7013. self.component.repaint();
  7014. };
  7015. this.isVisible = function() { return visible; };
  7016. this.hide = function() { self.setVisible(false); };
  7017. this.show = function() { self.setVisible(true); };
  7018. this.incrementLocation = function(amount) {
  7019. self.loc += amount;
  7020. self.component.repaint();
  7021. };
  7022. this.setLocation = function(l) {
  7023. self.loc = l;
  7024. self.component.repaint();
  7025. };
  7026. this.getLocation = function() {
  7027. return self.loc;
  7028. };
  7029. };
  7030. /**
  7031. * Class: Overlays.Arrow
  7032. *
  7033. * 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
  7034. * of the arrow that lines from each tail point converge into. The foldback point is defined using a decimal that indicates some fraction
  7035. * 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
  7036. * across the tail.
  7037. */
  7038. /**
  7039. * Function: Constructor
  7040. *
  7041. * Parameters:
  7042. *
  7043. * length - distance in pixels from head to tail baseline. default 20.
  7044. * width - width in pixels of the tail baseline. default 20.
  7045. * fillStyle - style to use when filling the arrow. defaults to "black".
  7046. * strokeStyle - style to use when stroking the arrow. defaults to null, which means the arrow is not stroked.
  7047. * lineWidth - line width to use when stroking the arrow. defaults to 1, but only used if strokeStyle is not null.
  7048. * 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.
  7049. * location - distance (as a decimal from 0 to 1 inclusive) marking where the arrow should sit on the connector. defaults to 0.5.
  7050. * direction - indicates the direction the arrow points in. valid values are -1 and 1; 1 is default.
  7051. */
  7052. jsPlumb.Overlays.Arrow = function(params) {
  7053. this.type = "Arrow";
  7054. AbstractOverlay.apply(this, arguments);
  7055. this.isAppendedAtTopLevel = false;
  7056. params = params || {};
  7057. var self = this;
  7058. this.length = params.length || 20;
  7059. this.width = params.width || 20;
  7060. this.id = params.id;
  7061. var direction = (params.direction || 1) < 0 ? -1 : 1,
  7062. paintStyle = params.paintStyle || { lineWidth:1 },
  7063. // how far along the arrow the lines folding back in come to. default is 62.3%.
  7064. foldback = params.foldback || 0.623;
  7065. this.computeMaxSize = function() { return self.width * 1.5; };
  7066. this.cleanup = function() { }; // nothing to clean up for Arrows
  7067. this.draw = function(connector, currentConnectionPaintStyle, connectorDimensions) {
  7068. var hxy, mid, txy, tail, cxy;
  7069. if (connector.pointAlongPathFrom) {
  7070. if (jsPlumbUtil.isString(self.loc) || self.loc > 1 || self.loc < 0) {
  7071. var l = parseInt(self.loc);
  7072. hxy = connector.pointAlongPathFrom(l, direction * self.length / 2, true),
  7073. mid = connector.pointOnPath(l, true),
  7074. txy = jsPlumbUtil.pointOnLine(hxy, mid, self.length);
  7075. }
  7076. else if (self.loc == 1) {
  7077. hxy = connector.pointOnPath(self.loc);
  7078. mid = connector.pointAlongPathFrom(self.loc, -1);
  7079. txy = jsPlumbUtil.pointOnLine(hxy, mid, self.length);
  7080. if (direction == -1) {
  7081. var _ = txy;
  7082. txy = hxy;
  7083. hxy = _;
  7084. }
  7085. }
  7086. else if (self.loc == 0) {
  7087. txy = connector.pointOnPath(self.loc);
  7088. mid = connector.pointAlongPathFrom(self.loc, 1);
  7089. hxy = jsPlumbUtil.pointOnLine(txy, mid, self.length);
  7090. if (direction == -1) {
  7091. var _ = txy;
  7092. txy = hxy;
  7093. hxy = _;
  7094. }
  7095. }
  7096. else {
  7097. hxy = connector.pointAlongPathFrom(self.loc, direction * self.length / 2),
  7098. mid = connector.pointOnPath(self.loc),
  7099. txy = jsPlumbUtil.pointOnLine(hxy, mid, self.length);
  7100. }
  7101. tail = jsPlumbUtil.perpendicularLineTo(hxy, txy, self.width);
  7102. cxy = jsPlumbUtil.pointOnLine(hxy, txy, foldback * self.length);
  7103. var minx = Math.min(hxy.x, tail[0].x, tail[1].x),
  7104. maxx = Math.max(hxy.x, tail[0].x, tail[1].x),
  7105. miny = Math.min(hxy.y, tail[0].y, tail[1].y),
  7106. maxy = Math.max(hxy.y, tail[0].y, tail[1].y);
  7107. var d = { hxy:hxy, tail:tail, cxy:cxy },
  7108. strokeStyle = paintStyle.strokeStyle || currentConnectionPaintStyle.strokeStyle,
  7109. fillStyle = paintStyle.fillStyle || currentConnectionPaintStyle.strokeStyle,
  7110. lineWidth = paintStyle.lineWidth || currentConnectionPaintStyle.lineWidth;
  7111. self.paint(connector, d, lineWidth, strokeStyle, fillStyle, connectorDimensions);
  7112. return [ minx, maxx, miny, maxy];
  7113. }
  7114. else return [0,0,0,0];
  7115. };
  7116. };
  7117. /**
  7118. * Class: Overlays.PlainArrow
  7119. *
  7120. * 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
  7121. * point along the length of the arrow: in this case, that foldback point is the full length of the arrow. so it just does
  7122. * a 'call' to Arrow with foldback set appropriately.
  7123. */
  7124. /**
  7125. * Function: Constructor
  7126. * See <Overlays.Arrow> for allowed parameters for this overlay.
  7127. */
  7128. jsPlumb.Overlays.PlainArrow = function(params) {
  7129. params = params || {};
  7130. var p = jsPlumb.extend(params, {foldback:1});
  7131. jsPlumb.Overlays.Arrow.call(this, p);
  7132. this.type = "PlainArrow";
  7133. };
  7134. /**
  7135. * Class: Overlays.Diamond
  7136. *
  7137. * A diamond. Like PlainArrow, this is a concrete case of the more generic case of the tail points converging on some point...it just
  7138. * happens that in this case, that point is greater than the length of the the arrow.
  7139. *
  7140. * this could probably do with some help with positioning...due to the way it reuses the Arrow paint code, what Arrow thinks is the
  7141. * 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
  7142. * stuck when it comes to helping out the Arrow class. possibly we could pass in a 'transpose' parameter or something. the value
  7143. * would be -l/4 in this case - move along one quarter of the total length.
  7144. */
  7145. /**
  7146. * Function: Constructor
  7147. * See <Overlays.Arrow> for allowed parameters for this overlay.
  7148. */
  7149. jsPlumb.Overlays.Diamond = function(params) {
  7150. params = params || {};
  7151. var l = params.length || 40,
  7152. p = jsPlumb.extend(params, {length:l/2, foldback:2});
  7153. jsPlumb.Overlays.Arrow.call(this, p);
  7154. this.type = "Diamond";
  7155. };
  7156. // abstract superclass for overlays that add an element to the DOM.
  7157. var AbstractDOMOverlay = function(params) {
  7158. jsPlumb.DOMElementComponent.apply(this, arguments);
  7159. AbstractOverlay.apply(this, arguments);
  7160. var self = this, initialised = false;
  7161. params = params || {};
  7162. this.id = params.id;
  7163. var div;
  7164. var makeDiv = function() {
  7165. div = params.create(params.component);
  7166. div = jsPlumb.CurrentLibrary.getDOMElement(div);
  7167. div.style["position"] = "absolute";
  7168. var clazz = params["_jsPlumb"].overlayClass + " " +
  7169. (self.cssClass ? self.cssClass :
  7170. params.cssClass ? params.cssClass : "");
  7171. div.className = clazz;
  7172. jsPlumb.appendElement(div, params.component.parent);
  7173. params["_jsPlumb"].getId(div);
  7174. self.attachListeners(div, self);
  7175. self.canvas = div;
  7176. };
  7177. this.getElement = function() {
  7178. if (div == null) {
  7179. makeDiv();
  7180. }
  7181. return div;
  7182. };
  7183. this.getDimensions = function() {
  7184. return jsPlumb.CurrentLibrary.getSize(jsPlumb.CurrentLibrary.getElementObject(self.getElement()));
  7185. };
  7186. var cachedDimensions = null,
  7187. _getDimensions = function(component) {
  7188. if (cachedDimensions == null)
  7189. cachedDimensions = self.getDimensions();
  7190. return cachedDimensions;
  7191. };
  7192. /*
  7193. * Function: clearCachedDimensions
  7194. * Clears the cached dimensions for the label. As a performance enhancement, label dimensions are
  7195. * cached from 1.3.12 onwards. The cache is cleared when you change the label text, of course, but
  7196. * there are other reasons why the text dimensions might change - if you make a change through CSS, for
  7197. * example, you might change the font size. in that case you should explicitly call this method.
  7198. */
  7199. this.clearCachedDimensions = function() {
  7200. cachedDimensions = null;
  7201. };
  7202. this.computeMaxSize = function() {
  7203. var td = _getDimensions();
  7204. return Math.max(td[0], td[1]);
  7205. };
  7206. //override setVisible
  7207. var osv = self.setVisible;
  7208. self.setVisible = function(state) {
  7209. osv(state); // call superclass
  7210. div.style.display = state ? "block" : "none";
  7211. };
  7212. this.cleanup = function() {
  7213. if (div != null) jsPlumb.CurrentLibrary.removeElement(div);
  7214. };
  7215. this.paint = function(component, d, componentDimensions) {
  7216. if (!initialised) {
  7217. self.getElement();
  7218. component.appendDisplayElement(div);
  7219. self.attachListeners(div, component);
  7220. initialised = true;
  7221. }
  7222. div.style.left = (componentDimensions[0] + d.minx) + "px";
  7223. div.style.top = (componentDimensions[1] + d.miny) + "px";
  7224. };
  7225. this.draw = function(component, currentConnectionPaintStyle, componentDimensions) {
  7226. var td = _getDimensions();
  7227. if (td != null && td.length == 2) {
  7228. var cxy = {x:0,y:0};
  7229. if (component.pointOnPath) {
  7230. var loc = self.loc, absolute = false;
  7231. if (jsPlumbUtil.isString(self.loc) || self.loc < 0 || self.loc > 1) {
  7232. loc = parseInt(self.loc);
  7233. absolute = true;
  7234. }
  7235. cxy = component.pointOnPath(loc, absolute); // a connection
  7236. }
  7237. else {
  7238. var locToUse = self.loc.constructor == Array ? self.loc : self.endpointLoc;
  7239. cxy = { x:locToUse[0] * componentDimensions[2],
  7240. y:locToUse[1] * componentDimensions[3] };
  7241. }
  7242. minx = cxy.x - (td[0] / 2),
  7243. miny = cxy.y - (td[1] / 2);
  7244. self.paint(component, { minx:minx, miny:miny, td:td, cxy:cxy }, componentDimensions);
  7245. return [minx, minx + td[0], miny, miny + td[1]];
  7246. }
  7247. else return [0,0,0,0];
  7248. };
  7249. this.reattachListeners = function(connector) {
  7250. if (div) {
  7251. self.reattachListenersForElement(div, self, connector);
  7252. }
  7253. };
  7254. };
  7255. /**
  7256. * Class: Overlays.Custom
  7257. * A Custom overlay. You supply a 'create' function which returns some DOM element, and jsPlumb positions it.
  7258. * The 'create' function is passed a Connection or Endpoint.
  7259. */
  7260. /**
  7261. * Function: Constructor
  7262. *
  7263. * Parameters:
  7264. * create - function for jsPlumb to call that returns a DOM element.
  7265. * location - distance (as a decimal from 0 to 1 inclusive) marking where the label should sit on the connector. defaults to 0.5.
  7266. * id - optional id to use for later retrieval of this overlay.
  7267. *
  7268. */
  7269. jsPlumb.Overlays.Custom = function(params) {
  7270. this.type = "Custom";
  7271. AbstractDOMOverlay.apply(this, arguments);
  7272. };
  7273. /**
  7274. * Class: Overlays.Label
  7275. * A Label overlay. For all different renderer types (SVG/Canvas/VML), jsPlumb draws a Label overlay as a styled DIV. Version 1.3.0 of jsPlumb
  7276. * introduced the ability to set css classes on the label; this is now the preferred way for you to style a label. The 'labelStyle' parameter
  7277. * is still supported in 1.3.0 but its usage is deprecated. Under the hood, jsPlumb just turns that object into a bunch of CSS directive that it
  7278. * puts on the Label's 'style' attribute, so the end result is the same.
  7279. */
  7280. /**
  7281. * Function: Constructor
  7282. *
  7283. * Parameters:
  7284. * cssClass - optional css class string to append to css class. This string is appended "as-is", so you can of course have multiple classes
  7285. * defined. This parameter is preferred to using labelStyle, borderWidth and borderStyle.
  7286. * 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
  7287. * label function returns null. empty strings _will_ be painted.
  7288. * location - distance (as a decimal from 0 to 1 inclusive) marking where the label should sit on the connector. defaults to 0.5.
  7289. * id - optional id to use for later retrieval of this overlay.
  7290. *
  7291. */
  7292. jsPlumb.Overlays.Label = function(params) {
  7293. var self = this;
  7294. this.labelStyle = params.labelStyle || jsPlumb.Defaults.LabelStyle;
  7295. this.cssClass = this.labelStyle != null ? this.labelStyle.cssClass : null;
  7296. params.create = function() {
  7297. return document.createElement("div");
  7298. };
  7299. jsPlumb.Overlays.Custom.apply(this, arguments);
  7300. this.type = "Label";
  7301. var label = params.label || "",
  7302. self = this,
  7303. labelText = null;
  7304. /*
  7305. * Function: setLabel
  7306. * sets the label's, um, label. you would think i'd call this function
  7307. * 'setText', but you can pass either a Function or a String to this, so
  7308. * it makes more sense as 'setLabel'. This uses innerHTML on the label div, so keep
  7309. * that in mind if you need escaped HTML.
  7310. */
  7311. this.setLabel = function(l) {
  7312. label = l;
  7313. labelText = null;
  7314. self.clearCachedDimensions();
  7315. _update();
  7316. self.component.repaint();
  7317. };
  7318. var _update = function() {
  7319. if (typeof label == "function") {
  7320. var lt = label(self);
  7321. self.getElement().innerHTML = lt.replace(/\r\n/g, "<br/>");
  7322. }
  7323. else {
  7324. if (labelText == null) {
  7325. labelText = label;
  7326. self.getElement().innerHTML = labelText.replace(/\r\n/g, "<br/>");
  7327. }
  7328. }
  7329. };
  7330. this.getLabel = function() {
  7331. return label;
  7332. };
  7333. var superGD = this.getDimensions;
  7334. this.getDimensions = function() {
  7335. _update();
  7336. return superGD();
  7337. };
  7338. };
  7339. // ********************************* END OF OVERLAY DEFINITIONS ***********************************************************************
  7340. })();/*
  7341. * jsPlumb
  7342. *
  7343. * Title:jsPlumb 1.3.16
  7344. *
  7345. * Provides a way to visually connect elements on an HTML page, using either SVG, Canvas
  7346. * elements, or VML.
  7347. *
  7348. * This file contains the state machine connectors.
  7349. *
  7350. * Thanks to Brainstorm Mobile Solutions for supporting the development of these.
  7351. *
  7352. * Copyright (c) 2010 - 2012 Simon Porritt (simon.porritt@gmail.com)
  7353. *
  7354. * http://jsplumb.org
  7355. * http://github.com/sporritt/jsplumb
  7356. * http://code.google.com/p/jsplumb
  7357. *
  7358. * Dual licensed under the MIT and GPL2 licenses.
  7359. */
  7360. ;(function() {
  7361. var Line = function(x1, y1, x2, y2) {
  7362. this.m = (y2 - y1) / (x2 - x1);
  7363. this.b = -1 * ((this.m * x1) - y1);
  7364. this.rectIntersect = function(x,y,w,h) {
  7365. var results = [];
  7366. // try top face
  7367. // the equation of the top face is y = (0 * x) + b; y = b.
  7368. var xInt = (y - this.b) / this.m;
  7369. // test that the X value is in the line's range.
  7370. if (xInt >= x && xInt <= (x + w)) results.push([ xInt, (this.m * xInt) + this.b ]);
  7371. // try right face
  7372. var yInt = (this.m * (x + w)) + this.b;
  7373. if (yInt >= y && yInt <= (y + h)) results.push([ (yInt - this.b) / this.m, yInt ]);
  7374. // bottom face
  7375. var xInt = ((y + h) - this.b) / this.m;
  7376. // test that the X value is in the line's range.
  7377. if (xInt >= x && xInt <= (x + w)) results.push([ xInt, (this.m * xInt) + this.b ]);
  7378. // try left face
  7379. var yInt = (this.m * x) + this.b;
  7380. if (yInt >= y && yInt <= (y + h)) results.push([ (yInt - this.b) / this.m, yInt ]);
  7381. if (results.length == 2) {
  7382. var midx = (results[0][0] + results[1][0]) / 2, midy = (results[0][1] + results[1][1]) / 2;
  7383. results.push([ midx,midy ]);
  7384. // now calculate the segment inside the rectangle where the midpoint lies.
  7385. var xseg = midx <= x + (w / 2) ? -1 : 1,
  7386. yseg = midy <= y + (h / 2) ? -1 : 1;
  7387. results.push([xseg, yseg]);
  7388. return results;
  7389. }
  7390. return null;
  7391. };
  7392. },
  7393. _segment = function(x1, y1, x2, y2) {
  7394. if (x1 <= x2 && y2 <= y1) return 1;
  7395. else if (x1 <= x2 && y1 <= y2) return 2;
  7396. else if (x2 <= x1 && y2 >= y1) return 3;
  7397. return 4;
  7398. },
  7399. // the control point we will use depends on the faces to which each end of the connection is assigned, specifically whether or not the
  7400. // two faces are parallel or perpendicular. if they are parallel then the control point lies on the midpoint of the axis in which they
  7401. // are parellel and varies only in the other axis; this variation is proportional to the distance that the anchor points lie from the
  7402. // 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
  7403. // direction are dependent on the orientation of the two elements. 'seg', passed in to this method, tells you which segment the target element
  7404. // lies in with respect to the source: 1 is top right, 2 is bottom right, 3 is bottom left, 4 is top left.
  7405. //
  7406. // sourcePos and targetPos are arrays of info about where on the source and target each anchor is located. their contents are:
  7407. //
  7408. // 0 - absolute x
  7409. // 1 - absolute y
  7410. // 2 - proportional x in element (0 is left edge, 1 is right edge)
  7411. // 3 - proportional y in element (0 is top edge, 1 is bottom edge)
  7412. //
  7413. _findControlPoint = function(midx, midy, segment, sourceEdge, targetEdge, dx, dy, distance, proximityLimit) {
  7414. // TODO (maybe)
  7415. // - if anchor pos is 0.5, make the control point take into account the relative position of the elements.
  7416. if (distance <= proximityLimit) return [midx, midy];
  7417. if (segment == 1) {
  7418. if (sourceEdge[3] <= 0 && targetEdge[3] >= 1) return [ midx + (sourceEdge[2] < 0.5 ? -1 * dx : dx), midy ];
  7419. else if (sourceEdge[2] >= 1 && targetEdge[2] <= 0) return [ midx, midy + (sourceEdge[3] < 0.5 ? -1 * dy : dy) ];
  7420. else return [ midx + (-1 * dx) , midy + (-1 * dy) ];
  7421. }
  7422. else if (segment == 2) {
  7423. if (sourceEdge[3] >= 1 && targetEdge[3] <= 0) return [ midx + (sourceEdge[2] < 0.5 ? -1 * dx : dx), midy ];
  7424. else if (sourceEdge[2] >= 1 && targetEdge[2] <= 0) return [ midx, midy + (sourceEdge[3] < 0.5 ? -1 * dy : dy) ];
  7425. else return [ midx + (1 * dx) , midy + (-1 * dy) ];
  7426. }
  7427. else if (segment == 3) {
  7428. if (sourceEdge[3] >= 1 && targetEdge[3] <= 0) return [ midx + (sourceEdge[2] < 0.5 ? -1 * dx : dx), midy ];
  7429. else if (sourceEdge[2] <= 0 && targetEdge[2] >= 1) return [ midx, midy + (sourceEdge[3] < 0.5 ? -1 * dy : dy) ];
  7430. else return [ midx + (-1 * dx) , midy + (-1 * dy) ];
  7431. }
  7432. else if (segment == 4) {
  7433. if (sourceEdge[3] <= 0 && targetEdge[3] >= 1) return [ midx + (sourceEdge[2] < 0.5 ? -1 * dx : dx), midy ];
  7434. else if (sourceEdge[2] <= 0 && targetEdge[2] >= 1) return [ midx, midy + (sourceEdge[3] < 0.5 ? -1 * dy : dy) ];
  7435. else return [ midx + (1 * dx) , midy + (-1 * dy) ];
  7436. }
  7437. };
  7438. /**
  7439. * Class: Connectors.StateMachine
  7440. * Provides 'state machine' connectors.
  7441. */
  7442. /*
  7443. * Function: Constructor
  7444. *
  7445. * Parameters:
  7446. * curviness - measure of how "curvy" the connectors will be. this is translated as the distance that the
  7447. * Bezier curve's control point is from the midpoint of the straight line connecting the two
  7448. * endpoints, and does not mean that the connector is this wide. The Bezier curve never reaches
  7449. * its control points; they act as gravitational masses. defaults to 10.
  7450. * margin - distance from element to start and end connectors, in pixels. defaults to 5.
  7451. * proximityLimit - sets the distance beneath which the elements are consider too close together to bother
  7452. * with fancy curves. by default this is 80 pixels.
  7453. * loopbackRadius - the radius of a loopback connector. optional; defaults to 25.
  7454. * 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.
  7455. */
  7456. jsPlumb.Connectors.StateMachine = function(params) {
  7457. var self = this,
  7458. currentPoints = null,
  7459. _sx, _sy, _tx, _ty, _controlPoint = [],
  7460. curviness = params.curviness || 10,
  7461. margin = params.margin || 5,
  7462. proximityLimit = params.proximityLimit || 80,
  7463. clockwise = params.orientation && params.orientation == "clockwise",
  7464. loopbackRadius = params.loopbackRadius || 25,
  7465. isLoopback = false,
  7466. showLoopback = params.showLoopback !== false;
  7467. this.type = "StateMachine";
  7468. params = params || {};
  7469. this.compute = function(sourcePos, targetPos, sourceEndpoint, targetEndpoint, sourceAnchor, targetAnchor, lineWidth, minWidth) {
  7470. var w = Math.abs(sourcePos[0] - targetPos[0]),
  7471. h = Math.abs(sourcePos[1] - targetPos[1]),
  7472. // these are padding to ensure the whole connector line appears
  7473. xo = 0.45 * w, yo = 0.45 * h;
  7474. // these are padding to ensure the whole connector line appears
  7475. w *= 1.9; h *= 1.9;
  7476. //ensure at least one pixel width
  7477. lineWidth = lineWidth || 1;
  7478. var x = Math.min(sourcePos[0], targetPos[0]) - xo,
  7479. y = Math.min(sourcePos[1], targetPos[1]) - yo;
  7480. if (!showLoopback || (sourceEndpoint.elementId != targetEndpoint.elementId)) {
  7481. isLoopback = false;
  7482. _sx = sourcePos[0] < targetPos[0] ? xo : w-xo;
  7483. _sy = sourcePos[1] < targetPos[1] ? yo:h-yo;
  7484. _tx = sourcePos[0] < targetPos[0] ? w-xo : xo;
  7485. _ty = sourcePos[1] < targetPos[1] ? h-yo : yo;
  7486. // now adjust for the margin
  7487. if (sourcePos[2] == 0) _sx -= margin;
  7488. if (sourcePos[2] == 1) _sx += margin;
  7489. if (sourcePos[3] == 0) _sy -= margin;
  7490. if (sourcePos[3] == 1) _sy += margin;
  7491. if (targetPos[2] == 0) _tx -= margin;
  7492. if (targetPos[2] == 1) _tx += margin;
  7493. if (targetPos[3] == 0) _ty -= margin;
  7494. if (targetPos[3] == 1) _ty += margin;
  7495. //
  7496. // these connectors are quadratic bezier curves, having a single control point. if both anchors
  7497. // are located at 0.5 on their respective faces, the control point is set to the midpoint and you
  7498. // get a straight line. this is also the case if the two anchors are within 'proximityLimit', since
  7499. // it seems to make good aesthetic sense to do that. outside of that, the control point is positioned
  7500. // at 'curviness' pixels away along the normal to the straight line connecting the two anchors.
  7501. //
  7502. // there may be two improvements to this. firstly, we might actually support the notion of avoiding nodes
  7503. // in the UI, or at least making a good effort at doing so. if a connection would pass underneath some node,
  7504. // for example, we might increase the distance the control point is away from the midpoint in a bid to
  7505. // steer it around that node. this will work within limits, but i think those limits would also be the likely
  7506. // limits for, once again, aesthetic good sense in the layout of a chart using these connectors.
  7507. //
  7508. // the second possible change is actually two possible changes: firstly, it is possible we should gradually
  7509. // decrease the 'curviness' as the distance between the anchors decreases; start tailing it off to 0 at some
  7510. // point (which should be configurable). secondly, we might slightly increase the 'curviness' for connectors
  7511. // with respect to how far their anchor is from the center of its respective face. this could either look cool,
  7512. // or stupid, and may indeed work only in a way that is so subtle as to have been a waste of time.
  7513. //
  7514. var _midx = (_sx + _tx) / 2, _midy = (_sy + _ty) / 2,
  7515. m2 = (-1 * _midx) / _midy, theta2 = Math.atan(m2),
  7516. dy = (m2 == Infinity || m2 == -Infinity) ? 0 : Math.abs(curviness / 2 * Math.sin(theta2)),
  7517. dx = (m2 == Infinity || m2 == -Infinity) ? 0 : Math.abs(curviness / 2 * Math.cos(theta2)),
  7518. segment = _segment(_sx, _sy, _tx, _ty),
  7519. distance = Math.sqrt(Math.pow(_tx - _sx, 2) + Math.pow(_ty - _sy, 2));
  7520. // calculate the control point. this code will be where we'll put in a rudimentary element avoidance scheme; it
  7521. // will work by extending the control point to force the curve to be, um, curvier.
  7522. _controlPoint = _findControlPoint(_midx,
  7523. _midy,
  7524. segment,
  7525. sourcePos,
  7526. targetPos,
  7527. curviness, curviness,
  7528. distance,
  7529. proximityLimit);
  7530. var requiredWidth = Math.max(Math.abs(_controlPoint[0] - _sx) * 3, Math.abs(_controlPoint[0] - _tx) * 3, Math.abs(_tx-_sx), 2 * lineWidth, minWidth),
  7531. requiredHeight = Math.max(Math.abs(_controlPoint[1] - _sy) * 3, Math.abs(_controlPoint[1] - _ty) * 3, Math.abs(_ty-_sy), 2 * lineWidth, minWidth);
  7532. if (w < requiredWidth) {
  7533. var dw = requiredWidth - w;
  7534. x -= (dw / 2);
  7535. _sx += (dw / 2);
  7536. _tx += (dw / 2);
  7537. w = requiredWidth;
  7538. _controlPoint[0] += (dw / 2);
  7539. }
  7540. if (h < requiredHeight) {
  7541. var dh = requiredHeight - h;
  7542. y -= (dh / 2);
  7543. _sy += (dh / 2);
  7544. _ty += (dh / 2);
  7545. h = requiredHeight;
  7546. _controlPoint[1] += (dh / 2);
  7547. }
  7548. currentPoints = [ x, y, w, h, _sx, _sy, _tx, _ty, _controlPoint[0], _controlPoint[1] ];
  7549. }
  7550. else {
  7551. isLoopback = true;
  7552. // a loopback connector. draw an arc from one anchor to the other.
  7553. // i guess we'll do this the same way as the others. just the control point will be a fair distance away.
  7554. var x1 = sourcePos[0], x2 = sourcePos[0], y1 = sourcePos[1] - margin, y2 = sourcePos[1] - margin,
  7555. cx = x1, cy = y1 - loopbackRadius;
  7556. // canvas sizing stuff, to ensure the whole painted area is visible.
  7557. w = ((2 * lineWidth) + (4 * loopbackRadius)), h = ((2 * lineWidth) + (4 * loopbackRadius));
  7558. x = cx - loopbackRadius - lineWidth - loopbackRadius, y = cy - loopbackRadius - lineWidth - loopbackRadius;
  7559. currentPoints = [ x, y, w, h, cx-x, cy-y, loopbackRadius, clockwise, x1-x, y1-y, x2-x, y2-y];
  7560. }
  7561. return currentPoints;
  7562. };
  7563. var _makeCurve = function() {
  7564. return [
  7565. { x:_tx, y:_ty },
  7566. { x:_controlPoint[0], y:_controlPoint[1] },
  7567. { x:_controlPoint[0] + 1, y:_controlPoint[1] + 1},
  7568. { x:_sx, y:_sy }
  7569. ];
  7570. };
  7571. var _translateLocation = function(curve, location, absolute) {
  7572. if (absolute)
  7573. location = jsBezier.locationAlongCurveFrom(curve, location > 0 ? 0 : 1, location);
  7574. return location;
  7575. };
  7576. /**
  7577. * returns the point on the connector's path that is 'location' along the length of the path, where 'location' is a decimal from
  7578. * 0 to 1 inclusive. for the straight line connector this is simple maths. for Bezier, not so much.
  7579. */
  7580. this.pointOnPath = function(location, absolute) {
  7581. if (isLoopback) {
  7582. if (absolute) {
  7583. var circumference = Math.PI * 2 * loopbackRadius;
  7584. location = location / circumference;
  7585. }
  7586. if (location > 0 && location < 1) location = 1 - location;
  7587. // current points are [ x, y, width, height, center x, center y, radius, clockwise, startx, starty, endx, endy ]
  7588. // so the path length is the circumference of the circle
  7589. //var len = 2 * Math.PI * currentPoints[6],
  7590. // map 'location' to an angle. 0 is PI/2 when the connector is on the top face; if we
  7591. // support other faces it will have to be calculated for each one. 1 is also PI/2.
  7592. // 0.5 is -PI/2.
  7593. var startAngle = (location * 2 * Math.PI) + (Math.PI / 2),
  7594. startX = currentPoints[4] + (currentPoints[6] * Math.cos(startAngle)),
  7595. startY = currentPoints[5] + (currentPoints[6] * Math.sin(startAngle));
  7596. return {x:startX, y:startY};
  7597. }
  7598. else {
  7599. var c = _makeCurve();
  7600. location = _translateLocation(c, location, absolute);
  7601. return jsBezier.pointOnCurve(c, location);
  7602. }
  7603. };
  7604. /**
  7605. * returns the gradient of the connector at the given point.
  7606. */
  7607. this.gradientAtPoint = function(location, absolute) {
  7608. if (isLoopback) {
  7609. // todo if absolute, location is a proportion of circumference
  7610. if (absolute) {
  7611. var circumference = Math.PI * 2 * loopbackRadius;
  7612. location = location / circumference;
  7613. }
  7614. return Math.atan(location * 2 * Math.PI);
  7615. }
  7616. else {
  7617. var c = _makeCurve();
  7618. location = _translateLocation(c, location, absolute);
  7619. return jsBezier.gradientAtPoint(c, location);
  7620. }
  7621. };
  7622. /**
  7623. * for Bezier curves this method is a little tricky, cos calculating path distance algebraically is notoriously difficult.
  7624. * this method is iterative, jumping forward .05% of the path at a time and summing the distance between this point and the previous
  7625. * one, until the sum reaches 'distance'. the method may turn out to be computationally expensive; we'll see.
  7626. * another drawback of this method is that if the connector gets quite long, .05% of the length of it is not necessarily smaller
  7627. * than the desired distance, in which case the loop returns immediately and the arrow is mis-shapen. so a better strategy might be to
  7628. * calculate the step as a function of distance/distance between endpoints.
  7629. */
  7630. this.pointAlongPathFrom = function(location, distance, absolute) {
  7631. if (isLoopback) {
  7632. if (absolute) {
  7633. var circumference = Math.PI * 2 * loopbackRadius;
  7634. location = location / circumference;
  7635. }
  7636. if (location > 0 && location < 1) location = 1 - location;
  7637. var circumference = 2 * Math.PI * currentPoints[6],
  7638. arcSpan = distance / circumference * 2 * Math.PI,
  7639. startAngle = (location * 2 * Math.PI) - arcSpan + (Math.PI / 2),
  7640. startX = currentPoints[4] + (currentPoints[6] * Math.cos(startAngle)),
  7641. startY = currentPoints[5] + (currentPoints[6] * Math.sin(startAngle));
  7642. return {x:startX, y:startY};
  7643. }
  7644. else {
  7645. var c = _makeCurve();
  7646. location = _translateLocation(c, location, absolute);
  7647. return jsBezier.pointAlongCurveFrom(c, location, distance);
  7648. }
  7649. };
  7650. };
  7651. /*
  7652. * Canvas state machine renderer.
  7653. */
  7654. jsPlumb.Connectors.canvas.StateMachine = function(params) {
  7655. params = params || {};
  7656. var self = this, drawGuideline = params.drawGuideline || true, avoidSelector = params.avoidSelector;
  7657. jsPlumb.Connectors.StateMachine.apply(this, arguments);
  7658. jsPlumb.CanvasConnector.apply(this, arguments);
  7659. this._paint = function(dimensions) {
  7660. if (dimensions.length == 10) {
  7661. self.ctx.beginPath();
  7662. self.ctx.moveTo(dimensions[4], dimensions[5]);
  7663. self.ctx.bezierCurveTo(dimensions[8], dimensions[9], dimensions[8], dimensions[9], dimensions[6], dimensions[7]);
  7664. self.ctx.stroke();
  7665. }
  7666. else {
  7667. // a loopback connector
  7668. self.ctx.save();
  7669. self.ctx.beginPath();
  7670. var startAngle = 0, // Starting point on circle
  7671. endAngle = 2 * Math.PI, // End point on circle
  7672. clockwise = dimensions[7]; // clockwise or anticlockwise
  7673. self.ctx.arc(dimensions[4],dimensions[5],dimensions[6],0, endAngle, clockwise);
  7674. self.ctx.stroke();
  7675. self.ctx.closePath();
  7676. self.ctx.restore();
  7677. }
  7678. };
  7679. this.createGradient = function(dim, ctx) {
  7680. return ctx.createLinearGradient(dim[4], dim[5], dim[6], dim[7]);
  7681. };
  7682. };
  7683. /*
  7684. * SVG State Machine renderer
  7685. */
  7686. jsPlumb.Connectors.svg.StateMachine = function() {
  7687. var self = this;
  7688. jsPlumb.Connectors.StateMachine.apply(this, arguments);
  7689. jsPlumb.SvgConnector.apply(this, arguments);
  7690. this.getPath = function(d) {
  7691. if (d.length == 10)
  7692. return "M " + d[4] + " " + d[5] + " C " + d[8] + " " + d[9] + " " + d[8] + " " + d[9] + " " + d[6] + " " + d[7];
  7693. else {
  7694. // loopback
  7695. return "M" + (d[8] + 4) + " " + d[9] + " A " + d[6] + " " + d[6] + " 0 1,0 " + (d[8]-4) + " " + d[9];
  7696. }
  7697. };
  7698. };
  7699. /*
  7700. * VML state machine renderer
  7701. */
  7702. jsPlumb.Connectors.vml.StateMachine = function() {
  7703. jsPlumb.Connectors.StateMachine.apply(this, arguments);
  7704. jsPlumb.VmlConnector.apply(this, arguments);
  7705. var _conv = jsPlumb.vml.convertValue;
  7706. this.getPath = function(d) {
  7707. if (d.length == 10) {
  7708. return "m" + _conv(d[4]) + "," + _conv(d[5]) +
  7709. " c" + _conv(d[8]) + "," + _conv(d[9]) + "," + _conv(d[8]) + "," + _conv(d[9]) + "," + _conv(d[6]) + "," + _conv(d[7]) + " e";
  7710. }
  7711. else {
  7712. // loopback
  7713. var left = _conv(d[8] - d[6]),
  7714. top = _conv(d[9] - (2 * d[6])),
  7715. right = left + _conv(2 * d[6]),
  7716. bottom = top + _conv(2 * d[6]),
  7717. posString = left + "," + top + "," + right + "," + bottom;
  7718. var o = "ar " + posString + "," + _conv(d[8]) + ","
  7719. + _conv(d[9]) + "," + _conv(d[8]) + "," + _conv(d[9]) + " e";
  7720. return o;
  7721. }
  7722. };
  7723. };
  7724. })();
  7725. /*
  7726. // now for a rudimentary avoidance scheme. TODO: how to set this in a cross-library way?
  7727. // if (avoidSelector) {
  7728. // var testLine = new Line(sourcePos[0] + _sx,sourcePos[1] + _sy,sourcePos[0] + _tx,sourcePos[1] + _ty);
  7729. // var sel = jsPlumb.getSelector(avoidSelector);
  7730. // for (var i = 0; i < sel.length; i++) {
  7731. // var id = jsPlumb.getId(sel[i]);
  7732. // if (id != sourceEndpoint.elementId && id != targetEndpoint.elementId) {
  7733. // o = jsPlumb.getOffset(id), s = jsPlumb.getSize(id);
  7734. //
  7735. // if (o && s) {
  7736. // var collision = testLine.rectIntersect(o.left,o.top,s[0],s[1]);
  7737. // if (collision) {
  7738. // set the control point to be a certain distance from the midpoint of the two points that
  7739. // the line crosses on the rectangle.
  7740. // TODO where will this 75 number come from?
  7741. // _controlX = collision[2][0] + (75 * collision[3][0]);
  7742. // / _controlY = collision[2][1] + (75 * collision[3][1]);
  7743. // }
  7744. // }
  7745. // }
  7746. // }
  7747. //}
  7748. *//*
  7749. * jsPlumb
  7750. *
  7751. * Title:jsPlumb 1.3.16
  7752. *
  7753. * Provides a way to visually connect elements on an HTML page, using either SVG, Canvas
  7754. * elements, or VML.
  7755. *
  7756. * This file contains the VML renderers.
  7757. *
  7758. * Copyright (c) 2010 - 2012 Simon Porritt (http://jsplumb.org)
  7759. *
  7760. * http://jsplumb.org
  7761. * http://github.com/sporritt/jsplumb
  7762. * http://code.google.com/p/jsplumb
  7763. *
  7764. * Dual licensed under the MIT and GPL2 licenses.
  7765. */
  7766. ;(function() {
  7767. // http://ajaxian.com/archives/the-vml-changes-in-ie-8
  7768. // http://www.nczonline.net/blog/2010/01/19/internet-explorer-8-document-and-browser-modes/
  7769. // http://www.louisremi.com/2009/03/30/changes-in-vml-for-ie8-or-what-feature-can-the-ie-dev-team-break-for-you-today/
  7770. var vmlAttributeMap = {
  7771. "stroke-linejoin":"joinstyle",
  7772. "joinstyle":"joinstyle",
  7773. "endcap":"endcap",
  7774. "miterlimit":"miterlimit"
  7775. },
  7776. jsPlumbStylesheet = null;
  7777. if (document.createStyleSheet && document.namespaces) {
  7778. var ruleClasses = [
  7779. ".jsplumb_vml", "jsplumb\\:textbox", "jsplumb\\:oval", "jsplumb\\:rect",
  7780. "jsplumb\\:stroke", "jsplumb\\:shape", "jsplumb\\:group"
  7781. ],
  7782. rule = "behavior:url(#default#VML);position:absolute;";
  7783. jsPlumbStylesheet = document.createStyleSheet();
  7784. for (var i = 0; i < ruleClasses.length; i++)
  7785. jsPlumbStylesheet.addRule(ruleClasses[i], rule);
  7786. // in this page it is also mentioned that IE requires the extra arg to the namespace
  7787. // http://www.louisremi.com/2009/03/30/changes-in-vml-for-ie8-or-what-feature-can-the-ie-dev-team-break-for-you-today/
  7788. // but someone commented saying they didn't need it, and it seems jsPlumb doesnt need it either.
  7789. // var iev = document.documentMode;
  7790. //if (!iev || iev < 8)
  7791. document.namespaces.add("jsplumb", "urn:schemas-microsoft-com:vml");
  7792. //else
  7793. // document.namespaces.add("jsplumb", "urn:schemas-microsoft-com:vml", "#default#VML");
  7794. }
  7795. jsPlumb.vml = {};
  7796. var scale = 1000,
  7797. _groupMap = {},
  7798. _getGroup = function(container, connectorClass) {
  7799. var id = jsPlumb.getId(container),
  7800. g = _groupMap[id];
  7801. if(!g) {
  7802. g = _node("group", [0,0,scale, scale], {"class":connectorClass});
  7803. //g.style.position=absolute;
  7804. //g["coordsize"] = "1000,1000";
  7805. g.style.backgroundColor="red";
  7806. _groupMap[id] = g;
  7807. jsPlumb.appendElement(g, container); // todo if this gets reinstated, remember to use the current jsplumb instance.
  7808. //document.body.appendChild(g);
  7809. }
  7810. return g;
  7811. },
  7812. _atts = function(o, atts) {
  7813. for (var i in atts) {
  7814. // IE8 fix: setattribute does not work after an element has been added to the dom!
  7815. // http://www.louisremi.com/2009/03/30/changes-in-vml-for-ie8-or-what-feature-can-the-ie-dev-team-break-for-you-today/
  7816. //o.setAttribute(i, atts[i]);
  7817. /*There is an additional problem when accessing VML elements by using get/setAttribute. The simple solution is following:
  7818. if (document.documentMode==8) {
  7819. ele.opacity=1;
  7820. } else {
  7821. ele.setAttribute(‘opacity’,1);
  7822. }
  7823. */
  7824. o[i] = atts[i];
  7825. }
  7826. },
  7827. _node = function(name, d, atts, parent, _jsPlumb, deferToJsPlumbContainer) {
  7828. atts = atts || {};
  7829. var o = document.createElement("jsplumb:" + name);
  7830. if (deferToJsPlumbContainer)
  7831. _jsPlumb.appendElement(o, parent);
  7832. else
  7833. jsPlumb.CurrentLibrary.appendElement(o, parent);
  7834. o.className = (atts["class"] ? atts["class"] + " " : "") + "jsplumb_vml";
  7835. _pos(o, d);
  7836. _atts(o, atts);
  7837. return o;
  7838. },
  7839. _pos = function(o,d, zIndex) {
  7840. o.style.left = d[0] + "px";
  7841. o.style.top = d[1] + "px";
  7842. o.style.width= d[2] + "px";
  7843. o.style.height= d[3] + "px";
  7844. o.style.position = "absolute";
  7845. if (zIndex)
  7846. o.style.zIndex = zIndex;
  7847. },
  7848. _conv = jsPlumb.vml.convertValue = function(v) {
  7849. return Math.floor(v * scale);
  7850. },
  7851. // tests if the given style is "transparent" and then sets the appropriate opacity node to 0 if so,
  7852. // or 1 if not. TODO in the future, support variable opacity.
  7853. _maybeSetOpacity = function(styleToWrite, styleToCheck, type, component) {
  7854. if ("transparent" === styleToCheck)
  7855. component.setOpacity(type, "0.0");
  7856. else
  7857. component.setOpacity(type, "1.0");
  7858. },
  7859. _applyStyles = function(node, style, component, _jsPlumb) {
  7860. var styleToWrite = {};
  7861. if (style.strokeStyle) {
  7862. styleToWrite["stroked"] = "true";
  7863. var strokeColor = jsPlumbUtil.convertStyle(style.strokeStyle, true);
  7864. styleToWrite["strokecolor"] = strokeColor;
  7865. _maybeSetOpacity(styleToWrite, strokeColor, "stroke", component);
  7866. styleToWrite["strokeweight"] = style.lineWidth + "px";
  7867. }
  7868. else styleToWrite["stroked"] = "false";
  7869. if (style.fillStyle) {
  7870. styleToWrite["filled"] = "true";
  7871. var fillColor = jsPlumbUtil.convertStyle(style.fillStyle, true);
  7872. styleToWrite["fillcolor"] = fillColor;
  7873. _maybeSetOpacity(styleToWrite, fillColor, "fill", component);
  7874. }
  7875. else styleToWrite["filled"] = "false";
  7876. if(style["dashstyle"]) {
  7877. if (component.strokeNode == null) {
  7878. component.strokeNode = _node("stroke", [0,0,0,0], { dashstyle:style["dashstyle"] }, node, _jsPlumb);
  7879. }
  7880. else
  7881. component.strokeNode.dashstyle = style["dashstyle"];
  7882. }
  7883. else if (style["stroke-dasharray"] && style["lineWidth"]) {
  7884. var sep = style["stroke-dasharray"].indexOf(",") == -1 ? " " : ",",
  7885. parts = style["stroke-dasharray"].split(sep),
  7886. styleToUse = "";
  7887. for(var i = 0; i < parts.length; i++) {
  7888. styleToUse += (Math.floor(parts[i] / style.lineWidth) + sep);
  7889. }
  7890. if (component.strokeNode == null) {
  7891. component.strokeNode = _node("stroke", [0,0,0,0], { dashstyle:styleToUse }, node, _jsPlumb);
  7892. }
  7893. else
  7894. component.strokeNode.dashstyle = styleToUse;
  7895. }
  7896. _atts(node, styleToWrite);
  7897. },
  7898. /*
  7899. * Base class for Vml endpoints and connectors. Extends jsPlumbUIComponent.
  7900. */
  7901. VmlComponent = function() {
  7902. var self = this;
  7903. jsPlumb.jsPlumbUIComponent.apply(this, arguments);
  7904. this.opacityNodes = {
  7905. "stroke":null,
  7906. "fill":null
  7907. };
  7908. this.initOpacityNodes = function(vml) {
  7909. self.opacityNodes["stroke"] = _node("stroke", [0,0,1,1], {opacity:"0.0"}, vml, self._jsPlumb);
  7910. self.opacityNodes["fill"] = _node("fill", [0,0,1,1], {opacity:"0.0"}, vml, self._jsPlumb);
  7911. };
  7912. this.setOpacity = function(type, value) {
  7913. var node = self.opacityNodes[type];
  7914. if (node) node["opacity"] = "" + value;
  7915. };
  7916. var displayElements = [ ];
  7917. this.getDisplayElements = function() {
  7918. return displayElements;
  7919. };
  7920. this.appendDisplayElement = function(el, doNotAppendToCanvas) {
  7921. if (!doNotAppendToCanvas) self.canvas.parentNode.appendChild(el);
  7922. displayElements.push(el);
  7923. };
  7924. },
  7925. /*
  7926. * Base class for Vml connectors. extends VmlComponent.
  7927. */
  7928. VmlConnector = jsPlumb.VmlConnector = function(params) {
  7929. var self = this;
  7930. self.strokeNode = null;
  7931. self.canvas = null;
  7932. VmlComponent.apply(this, arguments);
  7933. var clazz = self._jsPlumb.connectorClass + (params.cssClass ? (" " + params.cssClass) : "");
  7934. this.paint = function(d, style, anchor) {
  7935. if (style != null) {
  7936. var path = self.getPath(d), p = { "path":path };
  7937. //*
  7938. if (style.outlineColor) {
  7939. var outlineWidth = style.outlineWidth || 1,
  7940. outlineStrokeWidth = style.lineWidth + (2 * outlineWidth),
  7941. outlineStyle = {
  7942. strokeStyle : jsPlumbUtil.convertStyle(style.outlineColor),
  7943. lineWidth : outlineStrokeWidth
  7944. };
  7945. for (var aa in vmlAttributeMap) outlineStyle[aa] = style[aa];
  7946. if (self.bgCanvas == null) {
  7947. p["class"] = clazz;
  7948. p["coordsize"] = (d[2] * scale) + "," + (d[3] * scale);
  7949. self.bgCanvas = _node("shape", d, p, params.parent, self._jsPlumb, true);
  7950. _pos(self.bgCanvas, d, self.getZIndex());
  7951. self.appendDisplayElement(self.bgCanvas, true);
  7952. self.attachListeners(self.bgCanvas, self);
  7953. self.initOpacityNodes(self.bgCanvas, ["stroke"]);
  7954. }
  7955. else {
  7956. p["coordsize"] = (d[2] * scale) + "," + (d[3] * scale);
  7957. _pos(self.bgCanvas, d, self.getZIndex());
  7958. _atts(self.bgCanvas, p);
  7959. }
  7960. _applyStyles(self.bgCanvas, outlineStyle, self);
  7961. }
  7962. //*/
  7963. if (self.canvas == null) {
  7964. p["class"] = clazz;
  7965. p["coordsize"] = (d[2] * scale) + "," + (d[3] * scale);
  7966. if (self.tooltip) p["label"] = self.tooltip;
  7967. self.canvas = _node("shape", d, p, params.parent, self._jsPlumb, true);
  7968. //var group = _getGroup(params.parent); // test of append everything to a group
  7969. //group.appendChild(self.canvas); // sort of works but not exactly;
  7970. //params["_jsPlumb"].appendElement(self.canvas, params.parent); //before introduction of groups
  7971. self.appendDisplayElement(self.canvas, true);
  7972. self.attachListeners(self.canvas, self);
  7973. self.initOpacityNodes(self.canvas, ["stroke"]);
  7974. }
  7975. else {
  7976. p["coordsize"] = (d[2] * scale) + "," + (d[3] * scale);
  7977. _pos(self.canvas, d, self.getZIndex());
  7978. _atts(self.canvas, p);
  7979. }
  7980. _applyStyles(self.canvas, style, self, self._jsPlumb);
  7981. }
  7982. };
  7983. //self.appendDisplayElement(self.canvas);
  7984. this.reattachListeners = function() {
  7985. if (self.canvas) self.reattachListenersForElement(self.canvas, self);
  7986. };
  7987. },
  7988. /*
  7989. *
  7990. * Base class for Vml Endpoints. extends VmlComponent.
  7991. *
  7992. */
  7993. VmlEndpoint = window.VmlEndpoint = function(params) {
  7994. VmlComponent.apply(this, arguments);
  7995. var vml = null, self = this, opacityStrokeNode = null, opacityFillNode = null;
  7996. self.canvas = document.createElement("div");
  7997. self.canvas.style["position"] = "absolute";
  7998. var clazz = self._jsPlumb.endpointClass + (params.cssClass ? (" " + params.cssClass) : "");
  7999. //var group = _getGroup(params.parent);
  8000. //group.appendChild(self.canvas);
  8001. params["_jsPlumb"].appendElement(self.canvas, params.parent);
  8002. if (self.tooltip) self.canvas.setAttribute("label", self.tooltip);
  8003. this.paint = function(d, style, anchor) {
  8004. var p = { };
  8005. jsPlumb.sizeCanvas(self.canvas, d[0], d[1], d[2], d[3]);
  8006. if (vml == null) {
  8007. p["class"] = clazz;
  8008. vml = self.getVml([0,0, d[2], d[3]], p, anchor, self.canvas, self._jsPlumb);
  8009. self.attachListeners(vml, self);
  8010. self.appendDisplayElement(vml, true);
  8011. self.appendDisplayElement(self.canvas, true);
  8012. self.initOpacityNodes(vml, ["fill"]);
  8013. }
  8014. else {
  8015. _pos(vml, [0,0, d[2], d[3]]);
  8016. _atts(vml, p);
  8017. }
  8018. _applyStyles(vml, style, self);
  8019. };
  8020. this.reattachListeners = function() {
  8021. if (vml) self.reattachListenersForElement(vml, self);
  8022. };
  8023. };
  8024. jsPlumb.Connectors.vml.Bezier = function() {
  8025. jsPlumb.Connectors.Bezier.apply(this, arguments);
  8026. VmlConnector.apply(this, arguments);
  8027. this.getPath = function(d) {
  8028. return "m" + _conv(d[4]) + "," + _conv(d[5]) +
  8029. " c" + _conv(d[8]) + "," + _conv(d[9]) + "," + _conv(d[10]) + "," + _conv(d[11]) + "," + _conv(d[6]) + "," + _conv(d[7]) + " e";
  8030. };
  8031. };
  8032. jsPlumb.Connectors.vml.Straight = function() {
  8033. jsPlumb.Connectors.Straight.apply(this, arguments);
  8034. VmlConnector.apply(this, arguments);
  8035. this.getPath = function(d) {
  8036. return "m" + _conv(d[4]) + "," + _conv(d[5]) + " l" + _conv(d[6]) + "," + _conv(d[7]) + " e";
  8037. };
  8038. };
  8039. jsPlumb.Connectors.vml.Flowchart = function() {
  8040. jsPlumb.Connectors.Flowchart.apply(this, arguments);
  8041. VmlConnector.apply(this, arguments);
  8042. this.getPath = function(dimensions) {
  8043. var p = "m " + _conv(dimensions[4]) + "," + _conv(dimensions[5]) + " l";
  8044. // loop through extra points
  8045. for (var i = 0; i < dimensions[8]; i++) {
  8046. p = p + " " + _conv(dimensions[9 + (i*2)]) + "," + _conv(dimensions[10 + (i*2)]);
  8047. }
  8048. // finally draw a line to the end
  8049. p = p + " " + _conv(dimensions[6]) + "," + _conv(dimensions[7]) + " e";
  8050. return p;
  8051. };
  8052. };
  8053. jsPlumb.Endpoints.vml.Dot = function() {
  8054. jsPlumb.Endpoints.Dot.apply(this, arguments);
  8055. VmlEndpoint.apply(this, arguments);
  8056. this.getVml = function(d, atts, anchor, parent, _jsPlumb) { return _node("oval", d, atts, parent, _jsPlumb); };
  8057. };
  8058. jsPlumb.Endpoints.vml.Rectangle = function() {
  8059. jsPlumb.Endpoints.Rectangle.apply(this, arguments);
  8060. VmlEndpoint.apply(this, arguments);
  8061. this.getVml = function(d, atts, anchor, parent, _jsPlumb) { return _node("rect", d, atts, parent, _jsPlumb); };
  8062. };
  8063. /*
  8064. * VML Image Endpoint is the same as the default image endpoint.
  8065. */
  8066. jsPlumb.Endpoints.vml.Image = jsPlumb.Endpoints.Image;
  8067. /**
  8068. * placeholder for Blank endpoint in vml renderer.
  8069. */
  8070. jsPlumb.Endpoints.vml.Blank = jsPlumb.Endpoints.Blank;
  8071. /**
  8072. * VML Label renderer. uses the default label renderer (which adds an element to the DOM)
  8073. */
  8074. jsPlumb.Overlays.vml.Label = jsPlumb.Overlays.Label;
  8075. /**
  8076. * VML Custom renderer. uses the default Custom renderer (which adds an element to the DOM)
  8077. */
  8078. jsPlumb.Overlays.vml.Custom = jsPlumb.Overlays.Custom;
  8079. var AbstractVmlArrowOverlay = function(superclass, originalArgs) {
  8080. superclass.apply(this, originalArgs);
  8081. VmlComponent.apply(this, originalArgs);
  8082. var self = this, path = null;
  8083. self.canvas = null;
  8084. self.isAppendedAtTopLevel = true;
  8085. var getPath = function(d, connectorDimensions) {
  8086. return "m " + _conv(d.hxy.x) + "," + _conv(d.hxy.y) +
  8087. " l " + _conv(d.tail[0].x) + "," + _conv(d.tail[0].y) +
  8088. " " + _conv(d.cxy.x) + "," + _conv(d.cxy.y) +
  8089. " " + _conv(d.tail[1].x) + "," + _conv(d.tail[1].y) +
  8090. " x e";
  8091. };
  8092. this.paint = function(connector, d, lineWidth, strokeStyle, fillStyle, connectorDimensions) {
  8093. var p = {};
  8094. if (strokeStyle) {
  8095. p["stroked"] = "true";
  8096. p["strokecolor"] = jsPlumbUtil.convertStyle(strokeStyle, true);
  8097. }
  8098. if (lineWidth) p["strokeweight"] = lineWidth + "px";
  8099. if (fillStyle) {
  8100. p["filled"] = "true";
  8101. p["fillcolor"] = fillStyle;
  8102. }
  8103. var xmin = Math.min(d.hxy.x, d.tail[0].x, d.tail[1].x, d.cxy.x),
  8104. ymin = Math.min(d.hxy.y, d.tail[0].y, d.tail[1].y, d.cxy.y),
  8105. xmax = Math.max(d.hxy.x, d.tail[0].x, d.tail[1].x, d.cxy.x),
  8106. ymax = Math.max(d.hxy.y, d.tail[0].y, d.tail[1].y, d.cxy.y),
  8107. w = Math.abs(xmax - xmin),
  8108. h = Math.abs(ymax - ymin),
  8109. dim = [xmin, ymin, w, h];
  8110. // for VML, we create overlays using shapes that have the same dimensions and
  8111. // coordsize as their connector - overlays calculate themselves relative to the
  8112. // connector (it's how it's been done since the original canvas implementation, because
  8113. // for canvas that makes sense).
  8114. p["path"] = getPath(d, connectorDimensions);
  8115. p["coordsize"] = (connectorDimensions[2] * scale) + "," + (connectorDimensions[3] * scale);
  8116. dim[0] = connectorDimensions[0];
  8117. dim[1] = connectorDimensions[1];
  8118. dim[2] = connectorDimensions[2];
  8119. dim[3] = connectorDimensions[3];
  8120. if (self.canvas == null) {
  8121. //p["class"] = jsPlumb.overlayClass; // TODO currentInstance?
  8122. self.canvas = _node("shape", dim, p, connector.canvas.parentNode, connector._jsPlumb, true);
  8123. connector.appendDisplayElement(self.canvas, true);
  8124. self.attachListeners(self.canvas, connector);
  8125. self.attachListeners(self.canvas, self);
  8126. }
  8127. else {
  8128. _pos(self.canvas, dim);
  8129. _atts(self.canvas, p);
  8130. }
  8131. };
  8132. this.reattachListeners = function() {
  8133. if (self.canvas) self.reattachListenersForElement(self.canvas, self);
  8134. };
  8135. this.cleanup = function() {
  8136. if (self.canvas != null) jsPlumb.CurrentLibrary.removeElement(self.canvas);
  8137. };
  8138. };
  8139. jsPlumb.Overlays.vml.Arrow = function() {
  8140. AbstractVmlArrowOverlay.apply(this, [jsPlumb.Overlays.Arrow, arguments]);
  8141. };
  8142. jsPlumb.Overlays.vml.PlainArrow = function() {
  8143. AbstractVmlArrowOverlay.apply(this, [jsPlumb.Overlays.PlainArrow, arguments]);
  8144. };
  8145. jsPlumb.Overlays.vml.Diamond = function() {
  8146. AbstractVmlArrowOverlay.apply(this, [jsPlumb.Overlays.Diamond, arguments]);
  8147. };
  8148. })();/*
  8149. * jsPlumb
  8150. *
  8151. * Title:jsPlumb 1.3.16
  8152. *
  8153. * Provides a way to visually connect elements on an HTML page, using either SVG, Canvas
  8154. * elements, or VML.
  8155. *
  8156. * This file contains the SVG renderers.
  8157. *
  8158. * Copyright (c) 2010 - 2012 Simon Porritt (http://jsplumb.org)
  8159. *
  8160. * http://jsplumb.org
  8161. * http://github.com/sporritt/jsplumb
  8162. * http://code.google.com/p/jsplumb
  8163. *
  8164. * Dual licensed under the MIT and GPL2 licenses.
  8165. */
  8166. /**
  8167. * SVG support for jsPlumb.
  8168. *
  8169. * things to investigate:
  8170. *
  8171. * gradients: https://developer.mozilla.org/en/svg_in_html_introduction
  8172. * css:http://tutorials.jenkov.com/svg/svg-and-css.html
  8173. * text on a path: http://www.w3.org/TR/SVG/text.html#TextOnAPath
  8174. * pointer events: https://developer.mozilla.org/en/css/pointer-events
  8175. *
  8176. * IE9 hover jquery: http://forum.jquery.com/topic/1-6-2-broke-svg-hover-events
  8177. *
  8178. */
  8179. ;(function() {
  8180. var svgAttributeMap = {
  8181. "joinstyle":"stroke-linejoin",
  8182. "stroke-linejoin":"stroke-linejoin",
  8183. "stroke-dashoffset":"stroke-dashoffset",
  8184. "stroke-linecap":"stroke-linecap"
  8185. },
  8186. STROKE_DASHARRAY = "stroke-dasharray",
  8187. DASHSTYLE = "dashstyle",
  8188. LINEAR_GRADIENT = "linearGradient",
  8189. RADIAL_GRADIENT = "radialGradient",
  8190. FILL = "fill",
  8191. STOP = "stop",
  8192. STROKE = "stroke",
  8193. STROKE_WIDTH = "stroke-width",
  8194. STYLE = "style",
  8195. NONE = "none",
  8196. JSPLUMB_GRADIENT = "jsplumb_gradient_",
  8197. LINE_WIDTH = "lineWidth",
  8198. ns = {
  8199. svg:"http://www.w3.org/2000/svg",
  8200. xhtml:"http://www.w3.org/1999/xhtml"
  8201. },
  8202. _attr = function(node, attributes) {
  8203. for (var i in attributes)
  8204. node.setAttribute(i, "" + attributes[i]);
  8205. },
  8206. _node = function(name, attributes) {
  8207. var n = document.createElementNS(ns.svg, name);
  8208. attributes = attributes || {};
  8209. attributes["version"] = "1.1";
  8210. attributes["xmlns"] = ns.xhtml;
  8211. _attr(n, attributes);
  8212. return n;
  8213. },
  8214. _pos = function(d) { return "position:absolute;left:" + d[0] + "px;top:" + d[1] + "px"; },
  8215. _clearGradient = function(parent) {
  8216. for (var i = 0; i < parent.childNodes.length; i++) {
  8217. if (parent.childNodes[i].tagName == LINEAR_GRADIENT || parent.childNodes[i].tagName == RADIAL_GRADIENT)
  8218. parent.removeChild(parent.childNodes[i]);
  8219. }
  8220. },
  8221. _updateGradient = function(parent, node, style, dimensions, uiComponent) {
  8222. var id = JSPLUMB_GRADIENT + uiComponent._jsPlumb.idstamp();
  8223. // first clear out any existing gradient
  8224. _clearGradient(parent);
  8225. // this checks for an 'offset' property in the gradient, and in the absence of it, assumes
  8226. // we want a linear gradient. if it's there, we create a radial gradient.
  8227. // it is possible that a more explicit means of defining the gradient type would be
  8228. // better. relying on 'offset' means that we can never have a radial gradient that uses
  8229. // some default offset, for instance.
  8230. // issue 244 suggested the 'gradientUnits' attribute; without this, straight/flowchart connectors with gradients would
  8231. // not show gradients when the line was perfectly horizontal or vertical.
  8232. if (!style.gradient.offset) {
  8233. var g = _node(LINEAR_GRADIENT, {id:id, gradientUnits:"userSpaceOnUse"});
  8234. parent.appendChild(g);
  8235. }
  8236. else {
  8237. var g = _node(RADIAL_GRADIENT, {
  8238. id:id
  8239. });
  8240. parent.appendChild(g);
  8241. }
  8242. // the svg radial gradient seems to treat stops in the reverse
  8243. // order to how canvas does it. so we want to keep all the maths the same, but
  8244. // iterate the actual style declarations in reverse order, if the x indexes are not in order.
  8245. for (var i = 0; i < style.gradient.stops.length; i++) {
  8246. // Straight Connectors and Bezier connectors act slightly differently; this code is a bit of a kludge. but next version of
  8247. // jsplumb will be replacing both Straight and Bezier to be generic instances of 'Connector', which has a list of segments.
  8248. // so, not too concerned about leaving this in for now.
  8249. var styleToUse = i;
  8250. if (dimensions.length == 8)
  8251. styleToUse = dimensions[4] < dimensions[6] ? i: style.gradient.stops.length - 1 - i;
  8252. else
  8253. styleToUse = dimensions[4] < dimensions[6] ? style.gradient.stops.length - 1 - i : i;
  8254. var stopColor = jsPlumbUtil.convertStyle(style.gradient.stops[styleToUse][1], true);
  8255. var s = _node(STOP, {"offset":Math.floor(style.gradient.stops[i][0] * 100) + "%", "stop-color":stopColor});
  8256. g.appendChild(s);
  8257. }
  8258. var applyGradientTo = style.strokeStyle ? STROKE : FILL;
  8259. node.setAttribute(STYLE, applyGradientTo + ":url(#" + id + ")");
  8260. },
  8261. _applyStyles = function(parent, node, style, dimensions, uiComponent) {
  8262. if (style.gradient) {
  8263. _updateGradient(parent, node, style, dimensions, uiComponent);
  8264. }
  8265. else {
  8266. // make sure we clear any existing gradient
  8267. _clearGradient(parent);
  8268. node.setAttribute(STYLE, "");
  8269. }
  8270. node.setAttribute(FILL, style.fillStyle ? jsPlumbUtil.convertStyle(style.fillStyle, true) : NONE);
  8271. node.setAttribute(STROKE, style.strokeStyle ? jsPlumbUtil.convertStyle(style.strokeStyle, true) : NONE);
  8272. if (style.lineWidth) {
  8273. node.setAttribute(STROKE_WIDTH, style.lineWidth);
  8274. }
  8275. // in SVG there is a stroke-dasharray attribute we can set, and its syntax looks like
  8276. // the syntax in VML but is actually kind of nasty: values are given in the pixel
  8277. // coordinate space, whereas in VML they are multiples of the width of the stroked
  8278. // line, which makes a lot more sense. for that reason, jsPlumb is supporting both
  8279. // the native svg 'stroke-dasharray' attribute, and also the 'dashstyle' concept from
  8280. // VML, which will be the preferred method. the code below this converts a dashstyle
  8281. // attribute given in terms of stroke width into a pixel representation, by using the
  8282. // stroke's lineWidth.
  8283. if (style[DASHSTYLE] && style[LINE_WIDTH] && !style[STROKE_DASHARRAY]) {
  8284. var sep = style[DASHSTYLE].indexOf(",") == -1 ? " " : ",",
  8285. parts = style[DASHSTYLE].split(sep),
  8286. styleToUse = "";
  8287. parts.forEach(function(p) {
  8288. styleToUse += (Math.floor(p * style.lineWidth) + sep);
  8289. });
  8290. node.setAttribute(STROKE_DASHARRAY, styleToUse);
  8291. }
  8292. else if(style[STROKE_DASHARRAY]) {
  8293. node.setAttribute(STROKE_DASHARRAY, style[STROKE_DASHARRAY]);
  8294. }
  8295. // extra attributes such as join type, dash offset.
  8296. for (var i in svgAttributeMap) {
  8297. if (style[i]) {
  8298. node.setAttribute(svgAttributeMap[i], style[i]);
  8299. }
  8300. }
  8301. },
  8302. _decodeFont = function(f) {
  8303. var r = /([0-9].)(p[xt])\s(.*)/;
  8304. var bits = f.match(r);
  8305. return {size:bits[1] + bits[2], font:bits[3]};
  8306. },
  8307. _classManip = function(el, add, clazz) {
  8308. var classesToAddOrRemove = clazz.split(" "),
  8309. className = el.className,
  8310. curClasses = className.baseVal.split(" ");
  8311. for (var i = 0; i < classesToAddOrRemove.length; i++) {
  8312. if (add) {
  8313. if (curClasses.indexOf(classesToAddOrRemove[i]) == -1)
  8314. curClasses.push(classesToAddOrRemove[i]);
  8315. }
  8316. else {
  8317. var idx = curClasses.indexOf(classesToAddOrRemove[i]);
  8318. if (idx != -1)
  8319. curClasses.splice(idx, 1);
  8320. }
  8321. }
  8322. el.className.baseVal = curClasses.join(" ");
  8323. },
  8324. _addClass = function(el, clazz) {
  8325. _classManip(el, true, clazz);
  8326. },
  8327. _removeClass = function(el, clazz) {
  8328. _classManip(el, false, clazz);
  8329. };
  8330. /**
  8331. utility methods for other objects to use.
  8332. */
  8333. jsPlumbUtil.svg = {
  8334. addClass:_addClass,
  8335. removeClass:_removeClass,
  8336. node:_node,
  8337. attr:_attr,
  8338. pos:_pos
  8339. };
  8340. /*
  8341. * Base class for SVG components.
  8342. */
  8343. var SvgComponent = function(params) {
  8344. var self = this,
  8345. pointerEventsSpec = params.pointerEventsSpec || "all";
  8346. jsPlumb.jsPlumbUIComponent.apply(this, params.originalArgs);
  8347. self.canvas = null, self.path = null, self.svg = null;
  8348. var clazz = params.cssClass + " " + (params.originalArgs[0].cssClass || ""),
  8349. svgParams = {
  8350. "style":"",
  8351. "width":0,
  8352. "height":0,
  8353. "pointer-events":pointerEventsSpec,
  8354. "position":"absolute"
  8355. };
  8356. if (self.tooltip) svgParams["title"] = self.tooltip;
  8357. self.svg = _node("svg", svgParams);
  8358. if (params.useDivWrapper) {
  8359. self.canvas = document.createElement("div");
  8360. self.canvas.style["position"] = "absolute";
  8361. jsPlumb.sizeCanvas(self.canvas,0,0,1,1);
  8362. self.canvas.className = clazz;
  8363. if (self.tooltip) self.canvas.setAttribute("title", self.tooltip);
  8364. }
  8365. else {
  8366. _attr(self.svg, { "class":clazz });
  8367. self.canvas = self.svg;
  8368. }
  8369. params._jsPlumb.appendElement(self.canvas, params.originalArgs[0]["parent"]);
  8370. if (params.useDivWrapper) self.canvas.appendChild(self.svg);
  8371. // TODO this displayElement stuff is common between all components, across all
  8372. // renderers. would be best moved to jsPlumbUIComponent.
  8373. var displayElements = [ self.canvas ];
  8374. this.getDisplayElements = function() {
  8375. return displayElements;
  8376. };
  8377. this.appendDisplayElement = function(el) {
  8378. displayElements.push(el);
  8379. };
  8380. this.paint = function(d, style, anchor) {
  8381. if (style != null) {
  8382. var x = d[0], y = d[1];
  8383. if (params.useDivWrapper) {
  8384. jsPlumb.sizeCanvas(self.canvas, d[0], d[1], d[2], d[3]);
  8385. x = 0, y = 0;
  8386. }
  8387. var p = _pos([x, y, d[2], d[3]]);
  8388. if (self.getZIndex()) p += ";z-index:" + self.getZIndex() + ";";
  8389. _attr(self.svg, {
  8390. "style":p,
  8391. "width": d[2],
  8392. "height": d[3]
  8393. });
  8394. self._paint.apply(this, arguments);
  8395. }
  8396. };
  8397. };
  8398. /*
  8399. * Base class for SVG connectors.
  8400. */
  8401. var SvgConnector = jsPlumb.SvgConnector = function(params) {
  8402. var self = this;
  8403. SvgComponent.apply(this, [ {
  8404. cssClass:params["_jsPlumb"].connectorClass,
  8405. originalArgs:arguments,
  8406. pointerEventsSpec:"none",
  8407. tooltip:params.tooltip,
  8408. _jsPlumb:params["_jsPlumb"]
  8409. } ]);
  8410. this._paint = function(d, style) {
  8411. var p = self.getPath(d), a = { "d":p }, outlineStyle = null;
  8412. a["pointer-events"] = "all";
  8413. // outline style. actually means drawing an svg object underneath the main one.
  8414. if (style.outlineColor) {
  8415. var outlineWidth = style.outlineWidth || 1,
  8416. outlineStrokeWidth = style.lineWidth + (2 * outlineWidth),
  8417. outlineStyle = jsPlumb.CurrentLibrary.extend({}, style);
  8418. outlineStyle.strokeStyle = jsPlumbUtil.convertStyle(style.outlineColor);
  8419. outlineStyle.lineWidth = outlineStrokeWidth;
  8420. if (self.bgPath == null) {
  8421. self.bgPath = _node("path", a);
  8422. self.svg.appendChild(self.bgPath);
  8423. self.attachListeners(self.bgPath, self);
  8424. }
  8425. else {
  8426. _attr(self.bgPath, a);
  8427. }
  8428. _applyStyles(self.svg, self.bgPath, outlineStyle, d, self);
  8429. }
  8430. // test - see below
  8431. // a["clip-path"]= "url(#testClip)";
  8432. if (self.path == null) {
  8433. self.path = _node("path", a);
  8434. self.svg.appendChild(self.path);
  8435. self.attachListeners(self.path, self);
  8436. /*
  8437. this is a test of a clip path. i'm looking into using one of these to animate a jsplumb connection.
  8438. you could do this by walking along the line, stepping along a little at a time, and setting the clip
  8439. path to extend as far as that point.
  8440. self.clip = _node("clipPath", {id:"testClip", clipPathUnits:"objectBoundingBox"});
  8441. self.svg.appendChild(self.clip);
  8442. self.clip.appendChild(_node("rect", {
  8443. x:"0",y:"0",width:"0.5",height:"1"
  8444. }));
  8445. */
  8446. }
  8447. else {
  8448. _attr(self.path, a);
  8449. }
  8450. _applyStyles(self.svg, self.path, style, d, self);
  8451. };
  8452. this.reattachListeners = function() {
  8453. if (self.bgPath) self.reattachListenersForElement(self.bgPath, self);
  8454. if (self.path) self.reattachListenersForElement(self.path, self);
  8455. };
  8456. };
  8457. /*
  8458. * SVG Bezier Connector
  8459. */
  8460. jsPlumb.Connectors.svg.Bezier = function(params) {
  8461. jsPlumb.Connectors.Bezier.apply(this, arguments);
  8462. SvgConnector.apply(this, arguments);
  8463. this.getPath = function(d) {
  8464. var _p = "M " + d[4] + " " + d[5];
  8465. _p += (" C " + d[8] + " " + d[9] + " " + d[10] + " " + d[11] + " " + d[6] + " " + d[7]);
  8466. return _p;
  8467. };
  8468. };
  8469. /*
  8470. * SVG straight line Connector
  8471. */
  8472. jsPlumb.Connectors.svg.Straight = function(params) {
  8473. jsPlumb.Connectors.Straight.apply(this, arguments);
  8474. SvgConnector.apply(this, arguments);
  8475. this.getPath = function(d) { return "M " + d[4] + " " + d[5] + " L " + d[6] + " " + d[7]; };
  8476. };
  8477. jsPlumb.Connectors.svg.Flowchart = function() {
  8478. var self = this;
  8479. jsPlumb.Connectors.Flowchart.apply(this, arguments);
  8480. SvgConnector.apply(this, arguments);
  8481. this.getPath = function(dimensions) {
  8482. var p = "M " + dimensions[4] + "," + dimensions[5],
  8483. lx = dimensions[4],
  8484. ly = dimensions[5];
  8485. // loop through extra points
  8486. for (var i = 0; i < dimensions[8]; i++) {
  8487. var x1 = dimensions[9 + (i*2)], y1 = dimensions[10 + (i*2)],
  8488. x2 = dimensions[9 + ((i + 1) * 2)], y2 = dimensions[10 + ((i + 1) * 2)],
  8489. horiz = (x1 != lx) && (y1 == ly),
  8490. vert = (x1 == lx) && (y1 != ly),
  8491. multX = horiz ? x1 > x2 ? 1 : -1 : 0,
  8492. multY = vert ? y1 > y2 ? 1 : -1 : 0,
  8493. halfStroke = self.lineWidth / 2;
  8494. // previously:
  8495. //p = p + " L " + x1 + " " + y1;
  8496. //p = p + " M " + x1 + " " + y1;
  8497. // now, with support for painting an extra bit at the end each line:
  8498. p = p + " L " + x1 + " " + y1;
  8499. p = p + " L " + (x1 + (multX * halfStroke)) + " " + (y1 + (multY * halfStroke));
  8500. lx = x1;
  8501. ly = y1;
  8502. p = p + " M " + x1 + " " + y1;
  8503. }
  8504. // finally draw a line to the end
  8505. p = p + " L " + dimensions[6] + "," + dimensions[7];
  8506. return p;
  8507. };
  8508. };
  8509. /*
  8510. * Base class for SVG endpoints.
  8511. */
  8512. var SvgEndpoint = window.SvgEndpoint = function(params) {
  8513. var self = this;
  8514. SvgComponent.apply(this, [ {
  8515. cssClass:params["_jsPlumb"].endpointClass,
  8516. originalArgs:arguments,
  8517. pointerEventsSpec:"all",
  8518. useDivWrapper:true,
  8519. _jsPlumb:params["_jsPlumb"]
  8520. } ]);
  8521. this._paint = function(d, style) {
  8522. var s = jsPlumb.extend({}, style);
  8523. if (s.outlineColor) {
  8524. s.strokeWidth = s.outlineWidth;
  8525. s.strokeStyle = jsPlumbUtil.convertStyle(s.outlineColor, true);
  8526. }
  8527. if (self.node == null) {
  8528. self.node = self.makeNode(d, s);
  8529. self.svg.appendChild(self.node);
  8530. self.attachListeners(self.node, self);
  8531. }
  8532. _applyStyles(self.svg, self.node, s, d, self);
  8533. _pos(self.node, d);
  8534. };
  8535. this.reattachListeners = function() {
  8536. if (self.node) self.reattachListenersForElement(self.node, self);
  8537. };
  8538. };
  8539. /*
  8540. * SVG Dot Endpoint
  8541. */
  8542. jsPlumb.Endpoints.svg.Dot = function() {
  8543. jsPlumb.Endpoints.Dot.apply(this, arguments);
  8544. SvgEndpoint.apply(this, arguments);
  8545. this.makeNode = function(d, style) {
  8546. return _node("circle", {
  8547. "cx" : d[2] / 2,
  8548. "cy" : d[3] / 2,
  8549. "r" : d[2] / 2
  8550. });
  8551. };
  8552. };
  8553. /*
  8554. * SVG Rectangle Endpoint
  8555. */
  8556. jsPlumb.Endpoints.svg.Rectangle = function() {
  8557. jsPlumb.Endpoints.Rectangle.apply(this, arguments);
  8558. SvgEndpoint.apply(this, arguments);
  8559. this.makeNode = function(d, style) {
  8560. return _node("rect", {
  8561. "width":d[2],
  8562. "height":d[3]
  8563. });
  8564. };
  8565. };
  8566. /*
  8567. * SVG Image Endpoint is the default image endpoint.
  8568. */
  8569. jsPlumb.Endpoints.svg.Image = jsPlumb.Endpoints.Image;
  8570. /*
  8571. * Blank endpoint in svg renderer is the default Blank endpoint.
  8572. */
  8573. jsPlumb.Endpoints.svg.Blank = jsPlumb.Endpoints.Blank;
  8574. /*
  8575. * Label overlay in svg renderer is the default Label overlay.
  8576. */
  8577. jsPlumb.Overlays.svg.Label = jsPlumb.Overlays.Label;
  8578. /*
  8579. * Custom overlay in svg renderer is the default Custom overlay.
  8580. */
  8581. jsPlumb.Overlays.svg.Custom = jsPlumb.Overlays.Custom;
  8582. var AbstractSvgArrowOverlay = function(superclass, originalArgs) {
  8583. superclass.apply(this, originalArgs);
  8584. jsPlumb.jsPlumbUIComponent.apply(this, originalArgs);
  8585. this.isAppendedAtTopLevel = false;
  8586. var self = this, path = null;
  8587. this.paint = function(connector, d, lineWidth, strokeStyle, fillStyle) {
  8588. if (path == null) {
  8589. path = _node("path", {
  8590. "pointer-events":"all"
  8591. });
  8592. connector.svg.appendChild(path);
  8593. self.attachListeners(path, connector);
  8594. self.attachListeners(path, self);
  8595. }
  8596. var clazz = originalArgs && (originalArgs.length == 1) ? (originalArgs[0].cssClass || "") : "";
  8597. _attr(path, {
  8598. "d" : makePath(d),
  8599. "class" : clazz,
  8600. stroke : strokeStyle ? strokeStyle : null,
  8601. fill : fillStyle ? fillStyle : null
  8602. });
  8603. };
  8604. var makePath = function(d) {
  8605. return "M" + d.hxy.x + "," + d.hxy.y +
  8606. " L" + d.tail[0].x + "," + d.tail[0].y +
  8607. " L" + d.cxy.x + "," + d.cxy.y +
  8608. " L" + d.tail[1].x + "," + d.tail[1].y +
  8609. " L" + d.hxy.x + "," + d.hxy.y;
  8610. };
  8611. this.reattachListeners = function() {
  8612. if (path) self.reattachListenersForElement(path, self);
  8613. };
  8614. this.cleanup = function() {
  8615. if (path != null) jsPlumb.CurrentLibrary.removeElement(path);
  8616. };
  8617. };
  8618. jsPlumb.Overlays.svg.Arrow = function() {
  8619. AbstractSvgArrowOverlay.apply(this, [jsPlumb.Overlays.Arrow, arguments]);
  8620. };
  8621. jsPlumb.Overlays.svg.PlainArrow = function() {
  8622. AbstractSvgArrowOverlay.apply(this, [jsPlumb.Overlays.PlainArrow, arguments]);
  8623. };
  8624. jsPlumb.Overlays.svg.Diamond = function() {
  8625. AbstractSvgArrowOverlay.apply(this, [jsPlumb.Overlays.Diamond, arguments]);
  8626. };
  8627. // a test
  8628. jsPlumb.Overlays.svg.GuideLines = function() {
  8629. var path = null, self = this, path2 = null, p1_1, p1_2;
  8630. jsPlumb.Overlays.GuideLines.apply(this, arguments);
  8631. this.paint = function(connector, d, lineWidth, strokeStyle, fillStyle) {
  8632. if (path == null) {
  8633. path = _node("path");
  8634. connector.svg.appendChild(path);
  8635. self.attachListeners(path, connector);
  8636. self.attachListeners(path, self);
  8637. p1_1 = _node("path");
  8638. connector.svg.appendChild(p1_1);
  8639. self.attachListeners(p1_1, connector);
  8640. self.attachListeners(p1_1, self);
  8641. p1_2 = _node("path");
  8642. connector.svg.appendChild(p1_2);
  8643. self.attachListeners(p1_2, connector);
  8644. self.attachListeners(p1_2, self);
  8645. }
  8646. _attr(path, {
  8647. "d" : makePath(d[0], d[1]),
  8648. stroke : "red",
  8649. fill : null
  8650. });
  8651. _attr(p1_1, {
  8652. "d" : makePath(d[2][0], d[2][1]),
  8653. stroke : "blue",
  8654. fill : null
  8655. });
  8656. _attr(p1_2, {
  8657. "d" : makePath(d[3][0], d[3][1]),
  8658. stroke : "green",
  8659. fill : null
  8660. });
  8661. };
  8662. var makePath = function(d1, d2) {
  8663. return "M " + d1.x + "," + d1.y +
  8664. " L" + d2.x + "," + d2.y;
  8665. };
  8666. };
  8667. })();/*
  8668. * jsPlumb
  8669. *
  8670. * Title:jsPlumb 1.3.16
  8671. *
  8672. * Provides a way to visually connect elements on an HTML page, using either SVG, Canvas
  8673. * elements, or VML.
  8674. *
  8675. * This file contains the HTML5 canvas renderers.
  8676. *
  8677. * Copyright (c) 2010 - 2012 Simon Porritt (http://jsplumb.org)
  8678. *
  8679. * http://jsplumb.org
  8680. * http://github.com/sporritt/jsplumb
  8681. * http://code.google.com/p/jsplumb
  8682. *
  8683. * Dual licensed under the MIT and GPL2 licenses.
  8684. */
  8685. ;(function() {
  8686. // ********************************* CANVAS RENDERERS FOR CONNECTORS AND ENDPOINTS *******************************************************************
  8687. // TODO refactor to renderer common script. put a ref to jsPlumb.sizeCanvas in there too.
  8688. var _connectionBeingDragged = null,
  8689. _hasClass = function(el, clazz) { return jsPlumb.CurrentLibrary.hasClass(_getElementObject(el), clazz); },
  8690. _getElementObject = function(el) { return jsPlumb.CurrentLibrary.getElementObject(el); },
  8691. _getOffset = function(el) { return jsPlumb.CurrentLibrary.getOffset(_getElementObject(el)); },
  8692. _pageXY = function(el) { return jsPlumb.CurrentLibrary.getPageXY(el); },
  8693. _clientXY = function(el) { return jsPlumb.CurrentLibrary.getClientXY(el); };
  8694. /*
  8695. * Class:CanvasMouseAdapter
  8696. * Provides support for mouse events on canvases.
  8697. */
  8698. var CanvasMouseAdapter = function() {
  8699. var self = this;
  8700. self.overlayPlacements = [];
  8701. jsPlumb.jsPlumbUIComponent.apply(this, arguments);
  8702. jsPlumbUtil.EventGenerator.apply(this, arguments);
  8703. /**
  8704. * returns whether or not the given event is ojver a painted area of the canvas.
  8705. */
  8706. this._over = function(e) {
  8707. var o = _getOffset(_getElementObject(self.canvas)),
  8708. pageXY = _pageXY(e),
  8709. x = pageXY[0] - o.left, y = pageXY[1] - o.top;
  8710. if (x > 0 && y > 0 && x < self.canvas.width && y < self.canvas.height) {
  8711. // first check overlays
  8712. for ( var i = 0; i < self.overlayPlacements.length; i++) {
  8713. var p = self.overlayPlacements[i];
  8714. if (p && (p[0] <= x && p[1] >= x && p[2] <= y && p[3] >= y))
  8715. return true;
  8716. }
  8717. // then the canvas
  8718. var d = self.canvas.getContext("2d").getImageData(parseInt(x), parseInt(y), 1, 1);
  8719. return d.data[0] != 0 || d.data[1] != 0 || d.data[2] != 0 || d.data[3] != 0;
  8720. }
  8721. return false;
  8722. };
  8723. var _mouseover = false, _mouseDown = false, _posWhenMouseDown = null, _mouseWasDown = false,
  8724. _nullSafeHasClass = function(el, clazz) {
  8725. return el != null && _hasClass(el, clazz);
  8726. };
  8727. this.mousemove = function(e) {
  8728. var pageXY = _pageXY(e), clientXY = _clientXY(e),
  8729. ee = document.elementFromPoint(clientXY[0], clientXY[1]),
  8730. eventSourceWasOverlay = _nullSafeHasClass(ee, "_jsPlumb_overlay");
  8731. var _continue = _connectionBeingDragged == null && (_nullSafeHasClass(ee, "_jsPlumb_endpoint") || _nullSafeHasClass(ee, "_jsPlumb_connector"));
  8732. if (!_mouseover && _continue && self._over(e)) {
  8733. _mouseover = true;
  8734. self.fire("mouseenter", self, e);
  8735. return true;
  8736. }
  8737. // TODO here there is a remote chance that the overlay the mouse moved onto
  8738. // is actually not an overlay for the current component. a more thorough check would
  8739. // be to ensure the overlay belonged to the current component.
  8740. else if (_mouseover && (!self._over(e) || !_continue) && !eventSourceWasOverlay) {
  8741. _mouseover = false;
  8742. self.fire("mouseexit", self, e);
  8743. }
  8744. self.fire("mousemove", self, e);
  8745. };
  8746. this.click = function(e) {
  8747. if (_mouseover && self._over(e) && !_mouseWasDown)
  8748. self.fire("click", self, e);
  8749. _mouseWasDown = false;
  8750. };
  8751. this.dblclick = function(e) {
  8752. if (_mouseover && self._over(e) && !_mouseWasDown)
  8753. self.fire("dblclick", self, e);
  8754. _mouseWasDown = false;
  8755. };
  8756. this.mousedown = function(e) {
  8757. if(self._over(e) && !_mouseDown) {
  8758. _mouseDown = true;
  8759. _posWhenMouseDown = _getOffset(_getElementObject(self.canvas));
  8760. self.fire("mousedown", self, e);
  8761. }
  8762. };
  8763. this.mouseup = function(e) {
  8764. _mouseDown = false;
  8765. self.fire("mouseup", self, e);
  8766. };
  8767. this.contextmenu = function(e) {
  8768. if (_mouseover && self._over(e) && !_mouseWasDown)
  8769. self.fire("contextmenu", self, e);
  8770. _mouseWasDown = false;
  8771. };
  8772. };
  8773. var _newCanvas = function(params) {
  8774. var canvas = document.createElement("canvas");
  8775. params["_jsPlumb"].appendElement(canvas, params.parent);
  8776. canvas.style.position = "absolute";
  8777. if (params["class"]) canvas.className = params["class"];
  8778. // set an id. if no id on the element and if uuid was supplied it
  8779. // will be used, otherwise we'll create one.
  8780. params["_jsPlumb"].getId(canvas, params.uuid);
  8781. if (params.tooltip) canvas.setAttribute("title", params.tooltip);
  8782. return canvas;
  8783. };
  8784. var CanvasComponent = function(params) {
  8785. CanvasMouseAdapter.apply(this, arguments);
  8786. var displayElements = [ ];
  8787. this.getDisplayElements = function() { return displayElements; };
  8788. this.appendDisplayElement = function(el) { displayElements.push(el); };
  8789. };
  8790. /**
  8791. * Class:CanvasConnector
  8792. * Superclass for Canvas Connector renderers.
  8793. */
  8794. var CanvasConnector = jsPlumb.CanvasConnector = function(params) {
  8795. CanvasComponent.apply(this, arguments);
  8796. var _paintOneStyle = function(dim, aStyle) {
  8797. self.ctx.save();
  8798. jsPlumb.extend(self.ctx, aStyle);
  8799. if (aStyle.gradient) {
  8800. var g = self.createGradient(dim, self.ctx);
  8801. for ( var i = 0; i < aStyle.gradient.stops.length; i++)
  8802. g.addColorStop(aStyle.gradient.stops[i][0], aStyle.gradient.stops[i][1]);
  8803. self.ctx.strokeStyle = g;
  8804. }
  8805. self._paint(dim, aStyle);
  8806. self.ctx.restore();
  8807. };
  8808. var self = this,
  8809. clazz = self._jsPlumb.connectorClass + " " + (params.cssClass || "");
  8810. self.canvas = _newCanvas({
  8811. "class":clazz,
  8812. _jsPlumb:self._jsPlumb,
  8813. parent:params.parent,
  8814. tooltip:params.tooltip
  8815. });
  8816. self.ctx = self.canvas.getContext("2d");
  8817. self.appendDisplayElement(self.canvas);
  8818. self.paint = function(dim, style) {
  8819. if (style != null) {
  8820. jsPlumb.sizeCanvas(self.canvas, dim[0], dim[1], dim[2], dim[3]);
  8821. if (self.getZIndex())
  8822. self.canvas.style.zIndex = self.getZIndex();
  8823. if (style.outlineColor != null) {
  8824. var outlineWidth = style.outlineWidth || 1,
  8825. outlineStrokeWidth = style.lineWidth + (2 * outlineWidth),
  8826. outlineStyle = {
  8827. strokeStyle:style.outlineColor,
  8828. lineWidth:outlineStrokeWidth
  8829. };
  8830. _paintOneStyle(dim, outlineStyle);
  8831. }
  8832. _paintOneStyle(dim, style);
  8833. }
  8834. };
  8835. };
  8836. /**
  8837. * Class:CanvasEndpoint
  8838. * Superclass for Canvas Endpoint renderers.
  8839. */
  8840. var CanvasEndpoint = function(params) {
  8841. var self = this;
  8842. CanvasComponent.apply(this, arguments);
  8843. var clazz = self._jsPlumb.endpointClass + " " + (params.cssClass || ""),
  8844. canvasParams = {
  8845. "class":clazz,
  8846. _jsPlumb:self._jsPlumb,
  8847. parent:params.parent,
  8848. tooltip:self.tooltip
  8849. };
  8850. self.canvas = _newCanvas(canvasParams);
  8851. self.ctx = self.canvas.getContext("2d");
  8852. self.appendDisplayElement(self.canvas);
  8853. this.paint = function(d, style, anchor) {
  8854. jsPlumb.sizeCanvas(self.canvas, d[0], d[1], d[2], d[3]);
  8855. if (style.outlineColor != null) {
  8856. var outlineWidth = style.outlineWidth || 1,
  8857. outlineStrokeWidth = style.lineWidth + (2 * outlineWidth);
  8858. var outlineStyle = {
  8859. strokeStyle:style.outlineColor,
  8860. lineWidth:outlineStrokeWidth
  8861. };
  8862. }
  8863. self._paint.apply(this, arguments);
  8864. };
  8865. };
  8866. jsPlumb.Endpoints.canvas.Dot = function(params) {
  8867. jsPlumb.Endpoints.Dot.apply(this, arguments);
  8868. CanvasEndpoint.apply(this, arguments);
  8869. var self = this,
  8870. parseValue = function(value) {
  8871. try { return parseInt(value); }
  8872. catch(e) {
  8873. if (value.substring(value.length - 1) == '%')
  8874. return parseInt(value.substring(0, value - 1));
  8875. }
  8876. },
  8877. calculateAdjustments = function(gradient) {
  8878. var offsetAdjustment = self.defaultOffset, innerRadius = self.defaultInnerRadius;
  8879. gradient.offset && (offsetAdjustment = parseValue(gradient.offset));
  8880. gradient.innerRadius && (innerRadius = parseValue(gradient.innerRadius));
  8881. return [offsetAdjustment, innerRadius];
  8882. };
  8883. this._paint = function(d, style, anchor) {
  8884. if (style != null) {
  8885. var ctx = self.canvas.getContext('2d'), orientation = anchor.getOrientation(self);
  8886. jsPlumb.extend(ctx, style);
  8887. if (style.gradient) {
  8888. var adjustments = calculateAdjustments(style.gradient),
  8889. yAdjust = orientation[1] == 1 ? adjustments[0] * -1 : adjustments[0],
  8890. xAdjust = orientation[0] == 1 ? adjustments[0] * -1: adjustments[0],
  8891. g = ctx.createRadialGradient(d[4], d[4], d[4], d[4] + xAdjust, d[4] + yAdjust, adjustments[1]);
  8892. for (var i = 0; i < style.gradient.stops.length; i++)
  8893. g.addColorStop(style.gradient.stops[i][0], style.gradient.stops[i][1]);
  8894. ctx.fillStyle = g;
  8895. }
  8896. ctx.beginPath();
  8897. ctx.arc(d[4], d[4], d[4], 0, Math.PI*2, true);
  8898. ctx.closePath();
  8899. if (style.fillStyle || style.gradient) ctx.fill();
  8900. if (style.strokeStyle) ctx.stroke();
  8901. }
  8902. };
  8903. };
  8904. jsPlumb.Endpoints.canvas.Rectangle = function(params) {
  8905. var self = this;
  8906. jsPlumb.Endpoints.Rectangle.apply(this, arguments);
  8907. CanvasEndpoint.apply(this, arguments);
  8908. this._paint = function(d, style, anchor) {
  8909. var ctx = self.canvas.getContext("2d"), orientation = anchor.getOrientation(self);
  8910. jsPlumb.extend(ctx, style);
  8911. /* canvas gradient */
  8912. if (style.gradient) {
  8913. // first figure out which direction to run the gradient in (it depends on the orientation of the anchors)
  8914. var y1 = orientation[1] == 1 ? d[3] : orientation[1] == 0 ? d[3] / 2 : 0;
  8915. var y2 = orientation[1] == -1 ? d[3] : orientation[1] == 0 ? d[3] / 2 : 0;
  8916. var x1 = orientation[0] == 1 ? d[2] : orientation[0] == 0 ? d[2] / 2 : 0;
  8917. var x2 = orientation[0] == -1 ? d[2] : orientation[0] == 0 ? d[2] / 2 : 0;
  8918. var g = ctx.createLinearGradient(x1,y1,x2,y2);
  8919. for (var i = 0; i < style.gradient.stops.length; i++)
  8920. g.addColorStop(style.gradient.stops[i][0], style.gradient.stops[i][1]);
  8921. ctx.fillStyle = g;
  8922. }
  8923. ctx.beginPath();
  8924. ctx.rect(0, 0, d[2], d[3]);
  8925. ctx.closePath();
  8926. if (style.fillStyle || style.gradient) ctx.fill();
  8927. if (style.strokeStyle) ctx.stroke();
  8928. };
  8929. };
  8930. jsPlumb.Endpoints.canvas.Triangle = function(params) {
  8931. var self = this;
  8932. jsPlumb.Endpoints.Triangle.apply(this, arguments);
  8933. CanvasEndpoint.apply(this, arguments);
  8934. this._paint = function(d, style, anchor)
  8935. {
  8936. var width = d[2], height = d[3], x = d[0], y = d[1],
  8937. ctx = self.canvas.getContext('2d'),
  8938. offsetX = 0, offsetY = 0, angle = 0,
  8939. orientation = anchor.getOrientation(self);
  8940. if( orientation[0] == 1 ) {
  8941. offsetX = width;
  8942. offsetY = height;
  8943. angle = 180;
  8944. }
  8945. if( orientation[1] == -1 ) {
  8946. offsetX = width;
  8947. angle = 90;
  8948. }
  8949. if( orientation[1] == 1 ) {
  8950. offsetY = height;
  8951. angle = -90;
  8952. }
  8953. ctx.fillStyle = style.fillStyle;
  8954. ctx.translate(offsetX, offsetY);
  8955. ctx.rotate(angle * Math.PI/180);
  8956. ctx.beginPath();
  8957. ctx.moveTo(0, 0);
  8958. ctx.lineTo(width/2, height/2);
  8959. ctx.lineTo(0, height);
  8960. ctx.closePath();
  8961. if (style.fillStyle || style.gradient) ctx.fill();
  8962. if (style.strokeStyle) ctx.stroke();
  8963. };
  8964. };
  8965. /*
  8966. * Canvas Image Endpoint: uses the default version, which creates an <img> tag.
  8967. */
  8968. jsPlumb.Endpoints.canvas.Image = jsPlumb.Endpoints.Image;
  8969. /*
  8970. * Blank endpoint in all renderers is just the default Blank endpoint.
  8971. */
  8972. jsPlumb.Endpoints.canvas.Blank = jsPlumb.Endpoints.Blank;
  8973. /*
  8974. * Canvas Bezier Connector. Draws a Bezier curve onto a Canvas element.
  8975. */
  8976. jsPlumb.Connectors.canvas.Bezier = function() {
  8977. var self = this;
  8978. jsPlumb.Connectors.Bezier.apply(this, arguments);
  8979. CanvasConnector.apply(this, arguments);
  8980. this._paint = function(dimensions, style) {
  8981. self.ctx.beginPath();
  8982. self.ctx.moveTo(dimensions[4], dimensions[5]);
  8983. self.ctx.bezierCurveTo(dimensions[8], dimensions[9], dimensions[10], dimensions[11], dimensions[6], dimensions[7]);
  8984. self.ctx.stroke();
  8985. };
  8986. // TODO i doubt this handles the case that source and target are swapped.
  8987. this.createGradient = function(dim, ctx, swap) {
  8988. return /*(swap) ? self.ctx.createLinearGradient(dim[4], dim[5], dim[6], dim[7]) : */self.ctx.createLinearGradient(dim[6], dim[7], dim[4], dim[5]);
  8989. };
  8990. };
  8991. /*
  8992. * Canvas straight line Connector. Draws a straight line onto a Canvas element.
  8993. */
  8994. jsPlumb.Connectors.canvas.Straight = function() {
  8995. var self = this,
  8996. segmentMultipliers = [null, [1, -1], [1, 1], [-1, 1], [-1, -1] ];
  8997. jsPlumb.Connectors.Straight.apply(this, arguments);
  8998. CanvasConnector.apply(this, arguments);
  8999. this._paint = function(dimensions, style) {
  9000. self.ctx.beginPath();
  9001. if (style.dashstyle && style.dashstyle.split(" ").length == 2) {
  9002. // only a very simple dashed style is supported - having two values, which define the stroke length
  9003. // (as a multiple of the stroke width) and then the space length (also as a multiple of stroke width).
  9004. var ds = style.dashstyle.split(" ");
  9005. if (ds.length != 2) ds = [2, 2];
  9006. var dss = [ ds[0] * style.lineWidth, ds[1] * style.lineWidth ],
  9007. m = (dimensions[6] - dimensions[4]) / (dimensions[7] - dimensions[5]),
  9008. s = jsPlumbUtil.segment([dimensions[4], dimensions[5]], [ dimensions[6], dimensions[7] ]),
  9009. sm = segmentMultipliers[s],
  9010. theta = Math.atan(m),
  9011. l = Math.sqrt(Math.pow(dimensions[6] - dimensions[4], 2) + Math.pow(dimensions[7] - dimensions[5], 2)),
  9012. repeats = Math.floor(l / (dss[0] + dss[1])),
  9013. curPos = [dimensions[4], dimensions[5]];
  9014. // TODO: the question here is why could we not support this in all connector types? it's really
  9015. // just a case of going along and asking jsPlumb for the next point on the path a few times, until it
  9016. // reaches the end. every type of connector supports that method, after all. but right now its only the
  9017. // bezier connector that gives you back the new location on the path along with the x,y coordinates, which
  9018. // we would need. we'd start out at loc=0 and ask for the point along the path that is dss[0] pixels away.
  9019. // we then ask for the point that is (dss[0] + dss[1]) pixels away; and from that one we need not just the
  9020. // x,y but the location, cos we're gonna plug that location back in in order to find where that dash ends.
  9021. //
  9022. // it also strikes me that it should be trivial to support arbitrary dash styles (having more or less than two
  9023. // entries). you'd just iterate that array using a step size of 2, and generify the (rss[0] + rss[1])
  9024. // computation to be sum(rss[0]..rss[n]).
  9025. for (var i = 0; i < repeats; i++) {
  9026. self.ctx.moveTo(curPos[0], curPos[1]);
  9027. var nextEndX = curPos[0] + (Math.abs(Math.sin(theta) * dss[0]) * sm[0]),
  9028. nextEndY = curPos[1] + (Math.abs(Math.cos(theta) * dss[0]) * sm[1]),
  9029. nextStartX = curPos[0] + (Math.abs(Math.sin(theta) * (dss[0] + dss[1])) * sm[0]),
  9030. nextStartY = curPos[1] + (Math.abs(Math.cos(theta) * (dss[0] + dss[1])) * sm[1])
  9031. self.ctx.lineTo(nextEndX, nextEndY);
  9032. curPos = [nextStartX, nextStartY];
  9033. }
  9034. // now draw the last bit
  9035. self.ctx.moveTo(curPos[0], curPos[1]);
  9036. self.ctx.lineTo(dimensions[6], dimensions[7]);
  9037. }
  9038. else {
  9039. self.ctx.moveTo(dimensions[4], dimensions[5]);
  9040. self.ctx.lineTo(dimensions[6], dimensions[7]);
  9041. }
  9042. self.ctx.stroke();
  9043. };
  9044. // TODO this does not handle the case that src and target are swapped.
  9045. this.createGradient = function(dim, ctx) {
  9046. return ctx.createLinearGradient(dim[4], dim[5], dim[6], dim[7]);
  9047. };
  9048. };
  9049. jsPlumb.Connectors.canvas.Flowchart = function() {
  9050. var self = this;
  9051. jsPlumb.Connectors.Flowchart.apply(this, arguments);
  9052. CanvasConnector.apply(this, arguments);
  9053. this._paint = function(dimensions, style) {
  9054. self.ctx.beginPath();
  9055. self.ctx.moveTo(dimensions[4], dimensions[5]);
  9056. // loop through extra points
  9057. for (var i = 0; i < dimensions[8]; i++) {
  9058. self.ctx.lineTo(dimensions[9 + (i*2)], dimensions[10 + (i*2)]);
  9059. }
  9060. // finally draw a line to the end
  9061. self.ctx.lineTo(dimensions[6], dimensions[7]);
  9062. self.ctx.stroke();
  9063. };
  9064. this.createGradient = function(dim, ctx) {
  9065. return ctx.createLinearGradient(dim[4], dim[5], dim[6], dim[7]);
  9066. };
  9067. };
  9068. // ********************************* END OF CANVAS RENDERERS *******************************************************************
  9069. jsPlumb.Overlays.canvas.Label = jsPlumb.Overlays.Label;
  9070. jsPlumb.Overlays.canvas.Custom = jsPlumb.Overlays.Custom;
  9071. /**
  9072. * a placeholder right now, really just exists to mirror the fact that there are SVG and VML versions of this.
  9073. */
  9074. var CanvasOverlay = function() {
  9075. jsPlumb.jsPlumbUIComponent.apply(this, arguments);
  9076. };
  9077. var AbstractCanvasArrowOverlay = function(superclass, originalArgs) {
  9078. superclass.apply(this, originalArgs);
  9079. CanvasOverlay.apply(this, originalArgs);
  9080. this.paint = function(connector, d, lineWidth, strokeStyle, fillStyle) {
  9081. var ctx = connector.ctx;
  9082. ctx.lineWidth = lineWidth;
  9083. ctx.beginPath();
  9084. ctx.moveTo(d.hxy.x, d.hxy.y);
  9085. ctx.lineTo(d.tail[0].x, d.tail[0].y);
  9086. ctx.lineTo(d.cxy.x, d.cxy.y);
  9087. ctx.lineTo(d.tail[1].x, d.tail[1].y);
  9088. ctx.lineTo(d.hxy.x, d.hxy.y);
  9089. ctx.closePath();
  9090. if (strokeStyle) {
  9091. ctx.strokeStyle = strokeStyle;
  9092. ctx.stroke();
  9093. }
  9094. if (fillStyle) {
  9095. ctx.fillStyle = fillStyle;
  9096. ctx.fill();
  9097. }
  9098. };
  9099. };
  9100. jsPlumb.Overlays.canvas.Arrow = function() {
  9101. AbstractCanvasArrowOverlay.apply(this, [jsPlumb.Overlays.Arrow, arguments]);
  9102. };
  9103. jsPlumb.Overlays.canvas.PlainArrow = function() {
  9104. AbstractCanvasArrowOverlay.apply(this, [jsPlumb.Overlays.PlainArrow, arguments]);
  9105. };
  9106. jsPlumb.Overlays.canvas.Diamond = function() {
  9107. AbstractCanvasArrowOverlay.apply(this, [jsPlumb.Overlays.Diamond, arguments]);
  9108. };
  9109. })();/*
  9110. * jsPlumb
  9111. *
  9112. * Title:jsPlumb 1.3.16
  9113. *
  9114. * Provides a way to visually connect elements on an HTML page, using either SVG, Canvas
  9115. * elements, or VML.
  9116. *
  9117. * This file contains the jQuery adapter.
  9118. *
  9119. * Copyright (c) 2010 - 2012 Simon Porritt (http://jsplumb.org)
  9120. *
  9121. * http://jsplumb.org
  9122. * http://github.com/sporritt/jsplumb
  9123. * http://code.google.com/p/jsplumb
  9124. *
  9125. * Dual licensed under the MIT and GPL2 licenses.
  9126. */
  9127. /*
  9128. * the library specific functions, such as find offset, get id, get attribute, extend etc.
  9129. * the full list is:
  9130. *
  9131. * addClass adds a class to the given element
  9132. * animate calls the underlying library's animate functionality
  9133. * appendElement appends a child element to a parent element.
  9134. * bind binds some event to an element
  9135. * dragEvents a dictionary of event names
  9136. * extend extend some js object with another. probably not overly necessary; jsPlumb could just do this internally.
  9137. * getAttribute gets some attribute from an element
  9138. * getDragObject gets the object that is being dragged, by extracting it from the arguments passed to a drag callback
  9139. * getDragScope gets the drag scope for a given element.
  9140. * getDropScope gets the drop scope for a given element.
  9141. * getElementObject turns an id or dom element into an element object of the underlying library's type.
  9142. * getOffset gets an element's offset
  9143. * getOriginalEvent gets the original browser event from some wrapper event
  9144. * getPageXY gets the page event's xy location.
  9145. * getParent gets the parent of some element.
  9146. * getScrollLeft gets an element's scroll left. TODO: is this actually used? will it be?
  9147. * getScrollTop gets an element's scroll top. TODO: is this actually used? will it be?
  9148. * getSize gets an element's size.
  9149. * getUIPosition gets the position of some element that is currently being dragged, by extracting it from the arguments passed to a drag callback.
  9150. * hasClass returns whether or not the given element has the given class.
  9151. * initDraggable initializes an element to be draggable
  9152. * initDroppable initializes an element to be droppable
  9153. * isDragSupported returns whether or not drag is supported for some element.
  9154. * isDropSupported returns whether or not drop is supported for some element.
  9155. * removeClass removes a class from a given element.
  9156. * removeElement removes some element completely from the DOM.
  9157. * setAttribute sets an attribute on some element.
  9158. * setDraggable sets whether or not some element should be draggable.
  9159. * setDragScope sets the drag scope for a given element.
  9160. * setOffset sets the offset of some element.
  9161. * trigger triggers some event on an element.
  9162. * unbind unbinds some listener from some element.
  9163. */
  9164. (function($) {
  9165. //var getBoundingClientRectSupported = "getBoundingClientRect" in document.documentElement;
  9166. jsPlumb.CurrentLibrary = {
  9167. /**
  9168. * adds the given class to the element object.
  9169. */
  9170. addClass : function(el, clazz) {
  9171. el = jsPlumb.CurrentLibrary.getElementObject(el);
  9172. try {
  9173. if (el[0].className.constructor == SVGAnimatedString) {
  9174. jsPlumbUtil.svg.addClass(el[0], clazz);
  9175. }
  9176. }
  9177. catch (e) {
  9178. // SVGAnimatedString not supported; no problem.
  9179. }
  9180. el.addClass(clazz);
  9181. },
  9182. /**
  9183. * animates the given element.
  9184. */
  9185. animate : function(el, properties, options) {
  9186. el.animate(properties, options);
  9187. },
  9188. /**
  9189. * appends the given child to the given parent.
  9190. */
  9191. appendElement : function(child, parent) {
  9192. jsPlumb.CurrentLibrary.getElementObject(parent).append(child);
  9193. },
  9194. /**
  9195. * executes an ajax call.
  9196. */
  9197. ajax : function(params) {
  9198. params = params || {};
  9199. params.type = params.type || "get";
  9200. $.ajax(params);
  9201. },
  9202. /**
  9203. * event binding wrapper. it just so happens that jQuery uses 'bind' also. yui3, for example,
  9204. * uses 'on'.
  9205. */
  9206. bind : function(el, event, callback) {
  9207. el = jsPlumb.CurrentLibrary.getElementObject(el);
  9208. el.bind(event, callback);
  9209. },
  9210. /**
  9211. * mapping of drag events for jQuery
  9212. */
  9213. dragEvents : {
  9214. 'start':'start', 'stop':'stop', 'drag':'drag', 'step':'step',
  9215. 'over':'over', 'out':'out', 'drop':'drop', 'complete':'complete'
  9216. },
  9217. /**
  9218. * wrapper around the library's 'extend' functionality (which it hopefully has.
  9219. * otherwise you'll have to do it yourself). perhaps jsPlumb could do this for you
  9220. * instead. it's not like its hard.
  9221. */
  9222. extend : function(o1, o2) {
  9223. return $.extend(o1, o2);
  9224. },
  9225. /**
  9226. * gets the named attribute from the given element object.
  9227. */
  9228. getAttribute : function(el, attName) {
  9229. return el.attr(attName);
  9230. },
  9231. getClientXY : function(eventObject) {
  9232. return [eventObject.clientX, eventObject.clientY];
  9233. },
  9234. /**
  9235. * takes the args passed to an event function and returns you an object representing that which is being dragged.
  9236. */
  9237. getDragObject : function(eventArgs) {
  9238. return eventArgs[1].draggable;
  9239. },
  9240. getDragScope : function(el) {
  9241. return el.draggable("option", "scope");
  9242. },
  9243. getDropEvent : function(args) {
  9244. return args[0];
  9245. },
  9246. getDropScope : function(el) {
  9247. return el.droppable("option", "scope");
  9248. },
  9249. /**
  9250. * gets a DOM element from the given input, which might be a string (in which case we just do document.getElementById),
  9251. * 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
  9252. * two cases). this is the opposite of getElementObject below.
  9253. */
  9254. getDOMElement : function(el) {
  9255. if (typeof(el) == "string") return document.getElementById(el);
  9256. else if (el.context || el.length != null) return el[0];
  9257. else return el;
  9258. },
  9259. /**
  9260. * gets an "element object" from the given input. this means an object that is used by the
  9261. * underlying library on which jsPlumb is running. 'el' may already be one of these objects,
  9262. * in which case it is returned as-is. otherwise, 'el' is a String, the library's lookup
  9263. * function is used to find the element, using the given String as the element's id.
  9264. *
  9265. */
  9266. getElementObject : function(el) {
  9267. return typeof(el) == "string" ? $("#" + el) : $(el);
  9268. },
  9269. /**
  9270. * gets the offset for the element object. this should return a js object like this:
  9271. *
  9272. * { left:xxx, top: xxx }
  9273. */
  9274. getOffset : function(el) {
  9275. return el.offset();
  9276. },
  9277. getOriginalEvent : function(e) {
  9278. return e.originalEvent;
  9279. },
  9280. getPageXY : function(eventObject) {
  9281. return [eventObject.pageX, eventObject.pageY];
  9282. },
  9283. getParent : function(el) {
  9284. return jsPlumb.CurrentLibrary.getElementObject(el).parent();
  9285. },
  9286. getScrollLeft : function(el) {
  9287. return el.scrollLeft();
  9288. },
  9289. getScrollTop : function(el) {
  9290. return el.scrollTop();
  9291. },
  9292. getSelector : function(spec) {
  9293. return $(spec);
  9294. },
  9295. /**
  9296. * gets the size for the element object, in an array : [ width, height ].
  9297. */
  9298. getSize : function(el) {
  9299. return [el.outerWidth(), el.outerHeight()];
  9300. },
  9301. getTagName : function(el) {
  9302. var e = jsPlumb.CurrentLibrary.getElementObject(el);
  9303. return e.length > 0 ? e[0].tagName : null;
  9304. },
  9305. /**
  9306. * takes the args passed to an event function and returns you an object that gives the
  9307. * position of the object being moved, as a js object with the same params as the result of
  9308. * getOffset, ie: { left: xxx, top: xxx }.
  9309. *
  9310. * different libraries have different signatures for their event callbacks.
  9311. * see getDragObject as well
  9312. */
  9313. getUIPosition : function(eventArgs, zoom) {
  9314. zoom = zoom || 1;
  9315. // this code is a workaround for the case that the element being dragged has a margin set on it. jquery UI passes
  9316. // in the wrong offset if the element has a margin (it doesn't take the margin into account). the getBoundingClientRect
  9317. // method, which is in pretty much all browsers now, reports the right numbers. but it introduces a noticeable lag, which
  9318. // i don't like.
  9319. /*if ( getBoundingClientRectSupported ) {
  9320. var r = eventArgs[1].helper[0].getBoundingClientRect();
  9321. return { left : r.left, top: r.top };
  9322. } else {*/
  9323. if (eventArgs.length == 1) {
  9324. ret = { left: eventArgs[0].pageX, top:eventArgs[0].pageY };
  9325. }
  9326. else {
  9327. var ui = eventArgs[1],
  9328. _offset = ui.offset;
  9329. ret = _offset || ui.absolutePosition;
  9330. // adjust ui position to account for zoom, because jquery ui does not do this.
  9331. ui.position.left /= zoom;
  9332. ui.position.top /= zoom;
  9333. }
  9334. return { left:ret.left / zoom, top: ret.top / zoom };
  9335. },
  9336. hasClass : function(el, clazz) {
  9337. return el.hasClass(clazz);
  9338. },
  9339. /**
  9340. * initialises the given element to be draggable.
  9341. */
  9342. initDraggable : function(el, options, isPlumbedComponent) {
  9343. options = options || {};
  9344. // remove helper directive if present.
  9345. options.helper = null;
  9346. if (isPlumbedComponent)
  9347. options['scope'] = options['scope'] || jsPlumb.Defaults.Scope;
  9348. el.draggable(options);
  9349. },
  9350. /**
  9351. * initialises the given element to be droppable.
  9352. */
  9353. initDroppable : function(el, options) {
  9354. options['scope'] = options['scope'] || jsPlumb.Defaults.Scope;
  9355. el.droppable(options);
  9356. },
  9357. isAlreadyDraggable : function(el) {
  9358. el = jsPlumb.CurrentLibrary.getElementObject(el);
  9359. return el.hasClass("ui-draggable");
  9360. },
  9361. /**
  9362. * returns whether or not drag is supported (by the library, not whether or not it is disabled) for the given element.
  9363. */
  9364. isDragSupported : function(el, options) {
  9365. return el.draggable;
  9366. },
  9367. /**
  9368. * returns whether or not drop is supported (by the library, not whether or not it is disabled) for the given element.
  9369. */
  9370. isDropSupported : function(el, options) {
  9371. return el.droppable;
  9372. },
  9373. /**
  9374. * removes the given class from the element object.
  9375. */
  9376. removeClass : function(el, clazz) {
  9377. el = jsPlumb.CurrentLibrary.getElementObject(el);
  9378. try {
  9379. if (el[0].className.constructor == SVGAnimatedString) {
  9380. jsPlumbUtil.svg.removeClass(el[0], clazz);
  9381. }
  9382. }
  9383. catch (e) {
  9384. // SVGAnimatedString not supported; no problem.
  9385. }
  9386. el.removeClass(clazz);
  9387. },
  9388. removeElement : function(element, parent) {
  9389. jsPlumb.CurrentLibrary.getElementObject(element).remove();
  9390. },
  9391. /**
  9392. * sets the named attribute on the given element object.
  9393. */
  9394. setAttribute : function(el, attName, attValue) {
  9395. el.attr(attName, attValue);
  9396. },
  9397. /**
  9398. * sets the draggable state for the given element
  9399. */
  9400. setDraggable : function(el, draggable) {
  9401. el.draggable("option", "disabled", !draggable);
  9402. },
  9403. /**
  9404. * sets the drag scope. probably time for a setDragOption method (roll this and the one above together)
  9405. * @param el
  9406. * @param scope
  9407. */
  9408. setDragScope : function(el, scope) {
  9409. el.draggable("option", "scope", scope);
  9410. },
  9411. setOffset : function(el, o) {
  9412. jsPlumb.CurrentLibrary.getElementObject(el).offset(o);
  9413. },
  9414. /**
  9415. * note that jquery ignores the name of the event you wanted to trigger, and figures it out for itself.
  9416. * the other libraries do not. yui, in fact, cannot even pass an original event. we have to pull out stuff
  9417. * from the originalEvent to put in an options object for YUI.
  9418. * @param el
  9419. * @param event
  9420. * @param originalEvent
  9421. */
  9422. trigger : function(el, event, originalEvent) {
  9423. //originalEvent.stopPropagation();
  9424. //jsPlumb.CurrentLibrary.getElementObject(el).trigger(originalEvent);
  9425. var h = jQuery._data(jsPlumb.CurrentLibrary.getElementObject(el)[0], "handle");
  9426. h(originalEvent);
  9427. //originalEvent.stopPropagation();
  9428. },
  9429. /**
  9430. * event unbinding wrapper. it just so happens that jQuery uses 'unbind' also. yui3, for example,
  9431. * uses..something else.
  9432. */
  9433. unbind : function(el, event, callback) {
  9434. el = jsPlumb.CurrentLibrary.getElementObject(el);
  9435. el.unbind(event, callback);
  9436. }
  9437. };
  9438. $(document).ready(jsPlumb.init);
  9439. })(jQuery);
  9440. (function(){"undefined"==typeof Math.sgn&&(Math.sgn=function(a){return 0==a?0:0<a?1:-1});var m={subtract:function(a,b){return{x:a.x-b.x,y:a.y-b.y}},dotProduct:function(a,b){return a.x*b.x+a.y*b.y},square:function(a){return Math.sqrt(a.x*a.x+a.y*a.y)},scale:function(a,b){return{x:a.x*b,y:a.y*b}}},w=Math.pow(2,-65),u=function(a,b){for(var f=[],d=b.length-1,h=2*d-1,g=[],c=[],k=[],i=[],l=[[1,0.6,0.3,0.1],[0.4,0.6,0.6,0.4],[0.1,0.3,0.6,1]],e=0;e<=d;e++)g[e]=m.subtract(b[e],a);for(e=0;e<=d-1;e++){c[e]=
  9441. m.subtract(b[e+1],b[e]);c[e]=m.scale(c[e],3)}for(e=0;e<=d-1;e++)for(var n=0;n<=d;n++){k[e]||(k[e]=[]);k[e][n]=m.dotProduct(c[e],g[n])}for(e=0;e<=h;e++){i[e]||(i[e]=[]);i[e].y=0;i[e].x=parseFloat(e)/h}h=d-1;for(g=0;g<=d+h;g++){e=Math.max(0,g-h);for(c=Math.min(g,d);e<=c;e++){j=g-e;i[e+j].y=i[e+j].y+k[j][e]*l[j][e]}}d=b.length-1;i=r(i,2*d-1,f,0);h=m.subtract(a,b[0]);k=m.square(h);for(e=l=0;e<i;e++){h=m.subtract(a,s(b,d,f[e],null,null));h=m.square(h);if(h<k){k=h;l=f[e]}}h=m.subtract(a,b[d]);h=m.square(h);
  9442. if(h<k){k=h;l=1}return{location:l,distance:k}},r=function(a,b,f,d){var h=[],g=[],c=[],k=[],i=0,l,e;e=Math.sgn(a[0].y);for(var n=1;n<=b;n++){l=Math.sgn(a[n].y);l!=e&&i++;e=l}switch(i){case 0:return 0;case 1:if(d>=64){f[0]=(a[0].x+a[b].x)/2;return 1}var o,i=a[0].y-a[b].y;e=a[b].x-a[0].x;n=a[0].x*a[b].y-a[b].x*a[0].y;l=max_distance_below=0;for(o=1;o<b;o++){var m=i*a[o].x+e*a[o].y+n;m>l?l=m:m<max_distance_below&&(max_distance_below=m)}o=e;l=(1*(n-l)-o*0)*(1/(0*o-i*1));o=e;e=n-max_distance_below;i=(1*
  9443. e-o*0)*(1/(0*o-i*1));e=Math.min(l,i);if(Math.max(l,i)-e<w){c=a[b].x-a[0].x;k=a[b].y-a[0].y;f[0]=0+1*(c*(a[0].y-0)-k*(a[0].x-0))*(1/(c*0-k*1));return 1}}s(a,b,0.5,h,g);a=r(h,b,c,d+1);b=r(g,b,k,d+1);for(d=0;d<a;d++)f[d]=c[d];for(d=0;d<b;d++)f[d+a]=k[d];return a+b},s=function(a,b,f,d,h){for(var g=[[]],c=0;c<=b;c++)g[0][c]=a[c];for(a=1;a<=b;a++)for(c=0;c<=b-a;c++){g[a]||(g[a]=[]);g[a][c]||(g[a][c]={});g[a][c].x=(1-f)*g[a-1][c].x+f*g[a-1][c+1].x;g[a][c].y=(1-f)*g[a-1][c].y+f*g[a-1][c+1].y}if(d!=null)for(c=
  9444. 0;c<=b;c++)d[c]=g[c][0];if(h!=null)for(c=0;c<=b;c++)h[c]=g[b-c][c];return g[b][0]},v={},x=function(a){var b=v[a];if(!b){var b=[],f=function(a){return function(){return a}},d=function(){return function(a){return a}},h=function(){return function(a){return 1-a}},g=function(a){return function(b){for(var c=1,d=0;d<a.length;d++)c=c*a[d](b);return c}};b.push(new function(){return function(b){return Math.pow(b,a)}});for(var c=1;c<a;c++){for(var k=[new f(a)],i=0;i<a-c;i++)k.push(new d);for(i=0;i<c;i++)k.push(new h);
  9445. b.push(new g(k))}b.push(new function(){return function(b){return Math.pow(1-b,a)}});v[a]=b}return b},p=function(a,b){for(var f=x(a.length-1),d=0,h=0,g=0;g<a.length;g++){d=d+a[g].x*f[g](b);h=h+a[g].y*f[g](b)}return{x:d,y:h}},q=function(a,b,f){for(var d=p(a,b),h=0,g=f>0?1:-1,c=null;h<Math.abs(f);){b=b+0.005*g;c=p(a,b);h=h+Math.sqrt(Math.pow(c.x-d.x,2)+Math.pow(c.y-d.y,2));d=c}return{point:c,location:b}},t=function(a,b){var f=p(a,b),d=p(a.slice(0,a.length-1),b),h=d.y-f.y,f=d.x-f.x;return h==0?Infinity:
  9446. Math.atan(h/f)};window.jsBezier={distanceFromCurve:u,gradientAtPoint:t,gradientAtPointAlongCurveFrom:function(a,b,f){b=q(a,b,f);if(b.location>1)b.location=1;if(b.location<0)b.location=0;return t(a,b.location)},nearestPointOnCurve:function(a,b){var f=u(a,b);return{point:s(b,b.length-1,f.location,null,null),location:f.location}},pointOnCurve:p,pointAlongCurveFrom:function(a,b,f){return q(a,b,f).point},perpendicularToCurveAt:function(a,b,f,d){b=q(a,b,d==null?0:d);a=t(a,b.location);d=Math.atan(-1/a);
  9447. a=f/2*Math.sin(d);f=f/2*Math.cos(d);return[{x:b.point.x+f,y:b.point.y+a},{x:b.point.x-f,y:b.point.y-a}]},locationAlongCurveFrom:function(a,b,f){return q(a,b,f).location}}})();