EditorUi.js 153 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862
  1. /**
  2. * Copyright (c) 2006-2012, JGraph Ltd
  3. */
  4. /**
  5. * Constructs a new graph editor
  6. */
  7. EditorUi = function(editor, container, lightbox)
  8. {
  9. mxEventSource.call(this);
  10. this.destroyFunctions = [];
  11. this.editor = editor || new Editor();
  12. this.container = container || document.body;
  13. var graph = this.editor.graph;
  14. graph.lightbox = lightbox;
  15. // Overrides graph bounds to include background images
  16. var graphGetGraphBounds = graph.getGraphBounds;
  17. graph.getGraphBounds = function()
  18. {
  19. var bounds = graphGetGraphBounds.apply(this, arguments);
  20. var img = this.backgroundImage;
  21. if (img != null && img.width != null && img.height != null)
  22. {
  23. var t = this.view.translate;
  24. var s = this.view.scale;
  25. bounds = mxRectangle.fromRectangle(bounds);
  26. bounds.add(new mxRectangle(
  27. (t.x + img.x) * s, (t.y + img.y) * s,
  28. img.width * s, img.height * s));
  29. }
  30. return bounds;
  31. };
  32. // Faster scrollwheel zoom is possible with CSS transforms
  33. if (graph.useCssTransforms)
  34. {
  35. this.lazyZoomDelay = 0;
  36. }
  37. // Pre-fetches submenu image or replaces with embedded image if supported
  38. if (mxClient.IS_SVG)
  39. {
  40. mxPopupMenu.prototype.submenuImage = 'data:image/gif;base64,R0lGODlhCQAJAIAAAP///zMzMyH5BAEAAAAALAAAAAAJAAkAAAIPhI8WebHsHopSOVgb26AAADs=';
  41. }
  42. else
  43. {
  44. new Image().src = mxPopupMenu.prototype.submenuImage;
  45. }
  46. // Pre-fetches connect image
  47. if (!mxClient.IS_SVG && mxConnectionHandler.prototype.connectImage != null)
  48. {
  49. new Image().src = mxConnectionHandler.prototype.connectImage.src;
  50. }
  51. // Installs selection state listener
  52. this.selectionStateListener = mxUtils.bind(this, function(sender, evt)
  53. {
  54. this.clearSelectionState();
  55. });
  56. graph.getSelectionModel().addListener(mxEvent.CHANGE, this.selectionStateListener);
  57. graph.getModel().addListener(mxEvent.CHANGE, this.selectionStateListener);
  58. graph.addListener(mxEvent.EDITING_STARTED, this.selectionStateListener);
  59. graph.addListener(mxEvent.EDITING_STOPPED, this.selectionStateListener);
  60. graph.getView().addListener('unitChanged', this.selectionStateListener);
  61. // Disables graph and forced panning in chromeless mode
  62. if (this.editor.chromeless && !this.editor.editable)
  63. {
  64. this.footerHeight = 0;
  65. graph.isEnabled = function() { return false; };
  66. graph.panningHandler.isForcePanningEvent = function(me)
  67. {
  68. return !mxEvent.isPopupTrigger(me.getEvent());
  69. };
  70. }
  71. // Creates the user interface
  72. this.actions = new Actions(this);
  73. this.menus = this.createMenus();
  74. if (!graph.standalone)
  75. {
  76. // Stores the current style and assigns it to new cells
  77. var styles = ['rounded', 'shadow', 'glass', 'dashed', 'dashPattern', 'labelBackgroundColor',
  78. 'labelBorderColor', 'comic', 'sketch', 'fillWeight', 'hachureGap', 'hachureAngle', 'jiggle',
  79. 'disableMultiStroke', 'disableMultiStrokeFill', 'fillStyle', 'curveFitting',
  80. 'simplification', 'sketchStyle', 'pointerEvents'];
  81. var connectStyles = ['shape', 'edgeStyle', 'curved', 'rounded', 'elbow', 'jumpStyle', 'jumpSize',
  82. 'comic', 'sketch', 'fillWeight', 'hachureGap', 'hachureAngle', 'jiggle',
  83. 'disableMultiStroke', 'disableMultiStrokeFill', 'fillStyle', 'curveFitting',
  84. 'simplification', 'sketchStyle'];
  85. // Styles to be ignored if applyAll is false
  86. var ignoredEdgeStyles = ['curved', 'sourcePerimeterSpacing', 'targetPerimeterSpacing',
  87. 'startArrow', 'startFill', 'startSize', 'endArrow', 'endFill', 'endSize'];
  88. // Note: Everything that is not in styles is ignored (styles is augmented below)
  89. this.setDefaultStyle = function(cell)
  90. {
  91. try
  92. {
  93. var style = graph.getCellStyle(cell, false);
  94. var values = [];
  95. var keys = [];
  96. for (var key in style)
  97. {
  98. values.push(style[key]);
  99. keys.push(key);
  100. }
  101. // Resets current style
  102. if (graph.getModel().isEdge(cell))
  103. {
  104. graph.currentEdgeStyle = {};
  105. }
  106. else
  107. {
  108. graph.currentVertexStyle = {}
  109. }
  110. this.fireEvent(new mxEventObject('styleChanged',
  111. 'keys', keys, 'values', values,
  112. 'cells', [cell]));
  113. }
  114. catch (e)
  115. {
  116. this.handleError(e);
  117. }
  118. };
  119. this.clearDefaultStyle = function()
  120. {
  121. graph.currentEdgeStyle = mxUtils.clone(graph.defaultEdgeStyle);
  122. graph.currentVertexStyle = mxUtils.clone(graph.defaultVertexStyle);
  123. // Updates UI
  124. this.fireEvent(new mxEventObject('styleChanged', 'keys', [], 'values', [], 'cells', []));
  125. };
  126. // Keys that should be ignored if the cell has a value (known: new default for all cells is html=1 so
  127. // for the html key this effecticely only works for edges inserted via the connection handler)
  128. var valueStyles = ['fontFamily', 'fontSource', 'fontSize', 'fontColor'];
  129. for (var i = 0; i < valueStyles.length; i++)
  130. {
  131. if (mxUtils.indexOf(styles, valueStyles[i]) < 0)
  132. {
  133. styles.push(valueStyles[i]);
  134. }
  135. }
  136. // Keys that always update the current edge style regardless of selection
  137. var alwaysEdgeStyles = ['edgeStyle', 'startArrow', 'startFill', 'startSize', 'endArrow',
  138. 'endFill', 'endSize'];
  139. // Keys that are ignored together (if one appears all are ignored)
  140. var keyGroups = [['startArrow', 'startFill', 'endArrow', 'endFill'],
  141. ['startSize', 'endSize'],
  142. ['sourcePerimeterSpacing', 'targetPerimeterSpacing'],
  143. ['strokeColor', 'strokeWidth'],
  144. ['fillColor', 'gradientColor', 'gradientDirection'],
  145. ['opacity'],
  146. ['html']];
  147. // Adds all keys used above to the styles array
  148. for (var i = 0; i < keyGroups.length; i++)
  149. {
  150. for (var j = 0; j < keyGroups[i].length; j++)
  151. {
  152. styles.push(keyGroups[i][j]);
  153. }
  154. }
  155. for (var i = 0; i < connectStyles.length; i++)
  156. {
  157. if (mxUtils.indexOf(styles, connectStyles[i]) < 0)
  158. {
  159. styles.push(connectStyles[i]);
  160. }
  161. }
  162. // Implements a global current style for edges and vertices that is applied to new cells
  163. var insertHandler = function(cells, asText, model, vertexStyle, edgeStyle, applyAll, recurse)
  164. {
  165. vertexStyle = (vertexStyle != null) ? vertexStyle : graph.currentVertexStyle;
  166. edgeStyle = (edgeStyle != null) ? edgeStyle : graph.currentEdgeStyle;
  167. applyAll = (applyAll != null) ? applyAll : true;
  168. model = (model != null) ? model : graph.getModel();
  169. if (recurse)
  170. {
  171. var temp = [];
  172. for (var i = 0; i < cells.length; i++)
  173. {
  174. temp = temp.concat(model.getDescendants(cells[i]));
  175. }
  176. cells = temp;
  177. }
  178. model.beginUpdate();
  179. try
  180. {
  181. for (var i = 0; i < cells.length; i++)
  182. {
  183. var cell = cells[i];
  184. var appliedStyles;
  185. if (asText)
  186. {
  187. // Applies only basic text styles
  188. appliedStyles = ['fontSize', 'fontFamily', 'fontColor'];
  189. }
  190. else
  191. {
  192. // Removes styles defined in the cell style from the styles to be applied
  193. var cellStyle = model.getStyle(cell);
  194. var tokens = (cellStyle != null) ? cellStyle.split(';') : [];
  195. appliedStyles = styles.slice();
  196. for (var j = 0; j < tokens.length; j++)
  197. {
  198. var tmp = tokens[j];
  199. var pos = tmp.indexOf('=');
  200. if (pos >= 0)
  201. {
  202. var key = tmp.substring(0, pos);
  203. var index = mxUtils.indexOf(appliedStyles, key);
  204. if (index >= 0)
  205. {
  206. appliedStyles.splice(index, 1);
  207. }
  208. // Handles special cases where one defined style ignores other styles
  209. for (var k = 0; k < keyGroups.length; k++)
  210. {
  211. var group = keyGroups[k];
  212. if (mxUtils.indexOf(group, key) >= 0)
  213. {
  214. for (var l = 0; l < group.length; l++)
  215. {
  216. var index2 = mxUtils.indexOf(appliedStyles, group[l]);
  217. if (index2 >= 0)
  218. {
  219. appliedStyles.splice(index2, 1);
  220. }
  221. }
  222. }
  223. }
  224. }
  225. }
  226. }
  227. // Applies the current style to the cell
  228. var edge = model.isEdge(cell);
  229. var current = (edge) ? edgeStyle : vertexStyle;
  230. var newStyle = model.getStyle(cell);
  231. for (var j = 0; j < appliedStyles.length; j++)
  232. {
  233. var key = appliedStyles[j];
  234. var styleValue = current[key];
  235. if (styleValue != null && key != 'edgeStyle' && (key != 'shape' || edge))
  236. {
  237. // Special case: Connect styles are not applied here but in the connection handler
  238. if (!edge || applyAll || mxUtils.indexOf(ignoredEdgeStyles, key) < 0)
  239. {
  240. newStyle = mxUtils.setStyle(newStyle, key, styleValue);
  241. }
  242. }
  243. }
  244. if (Editor.simpleLabels)
  245. {
  246. newStyle = mxUtils.setStyle(mxUtils.setStyle(
  247. newStyle, 'html', null), 'whiteSpace', null);
  248. }
  249. model.setStyle(cell, newStyle);
  250. }
  251. }
  252. finally
  253. {
  254. model.endUpdate();
  255. }
  256. return cells;
  257. };
  258. graph.addListener('cellsInserted', function(sender, evt)
  259. {
  260. insertHandler(evt.getProperty('cells'), null, null, null, null, true, true);
  261. });
  262. graph.addListener('textInserted', function(sender, evt)
  263. {
  264. insertHandler(evt.getProperty('cells'), true);
  265. });
  266. this.insertHandler = insertHandler;
  267. this.createDivs();
  268. this.createUi();
  269. this.refresh();
  270. // Disables HTML and text selection
  271. var textEditing = mxUtils.bind(this, function(evt)
  272. {
  273. if (evt == null)
  274. {
  275. evt = window.event;
  276. }
  277. return graph.isEditing() || (evt != null && this.isSelectionAllowed(evt));
  278. });
  279. // Disables text selection while not editing and no dialog visible
  280. if (this.container == document.body)
  281. {
  282. this.menubarContainer.onselectstart = textEditing;
  283. this.menubarContainer.onmousedown = textEditing;
  284. this.toolbarContainer.onselectstart = textEditing;
  285. this.toolbarContainer.onmousedown = textEditing;
  286. this.diagramContainer.onselectstart = textEditing;
  287. this.diagramContainer.onmousedown = textEditing;
  288. this.sidebarContainer.onselectstart = textEditing;
  289. this.sidebarContainer.onmousedown = textEditing;
  290. this.formatContainer.onselectstart = textEditing;
  291. this.formatContainer.onmousedown = textEditing;
  292. this.footerContainer.onselectstart = textEditing;
  293. this.footerContainer.onmousedown = textEditing;
  294. if (this.tabContainer != null)
  295. {
  296. // Mouse down is needed for drag and drop
  297. this.tabContainer.onselectstart = textEditing;
  298. }
  299. }
  300. // And uses built-in context menu while editing
  301. if (!this.editor.chromeless || this.editor.editable)
  302. {
  303. // Allows context menu for links in hints
  304. var linkHandler = function(evt)
  305. {
  306. if (evt != null)
  307. {
  308. var source = mxEvent.getSource(evt);
  309. if (source.nodeName == 'A')
  310. {
  311. while (source != null)
  312. {
  313. if (source.className == 'geHint')
  314. {
  315. return true;
  316. }
  317. source = source.parentNode;
  318. }
  319. }
  320. }
  321. return textEditing(evt);
  322. };
  323. if (mxClient.IS_IE && (typeof(document.documentMode) === 'undefined' || document.documentMode < 9))
  324. {
  325. mxEvent.addListener(this.diagramContainer, 'contextmenu', linkHandler);
  326. }
  327. else
  328. {
  329. // Allows browser context menu outside of diagram and sidebar
  330. this.diagramContainer.oncontextmenu = linkHandler;
  331. }
  332. }
  333. else
  334. {
  335. graph.panningHandler.usePopupTrigger = false;
  336. }
  337. // Contains the main graph instance inside the given panel
  338. graph.init(this.diagramContainer);
  339. // Improves line wrapping for in-place editor
  340. if (mxClient.IS_SVG && graph.view.getDrawPane() != null)
  341. {
  342. var root = graph.view.getDrawPane().ownerSVGElement;
  343. if (root != null)
  344. {
  345. root.style.position = 'absolute';
  346. }
  347. }
  348. // Creates hover icons
  349. this.hoverIcons = this.createHoverIcons();
  350. // Hides hover icons when cells are moved
  351. if (graph.graphHandler != null)
  352. {
  353. var graphHandlerStart = graph.graphHandler.start;
  354. graph.graphHandler.start = function()
  355. {
  356. if (ui.hoverIcons != null)
  357. {
  358. ui.hoverIcons.reset();
  359. }
  360. graphHandlerStart.apply(this, arguments);
  361. };
  362. }
  363. // Adds tooltip when mouse is over scrollbars to show space-drag panning option
  364. mxEvent.addListener(this.diagramContainer, 'mousemove', mxUtils.bind(this, function(evt)
  365. {
  366. var off = mxUtils.getOffset(this.diagramContainer);
  367. if (mxEvent.getClientX(evt) - off.x - this.diagramContainer.clientWidth > 0 ||
  368. mxEvent.getClientY(evt) - off.y - this.diagramContainer.clientHeight > 0)
  369. {
  370. this.diagramContainer.setAttribute('title', mxResources.get('panTooltip'));
  371. }
  372. else
  373. {
  374. this.diagramContainer.removeAttribute('title');
  375. }
  376. }));
  377. // Escape key hides dialogs, adds space+drag panning
  378. var spaceKeyPressed = false;
  379. // Overrides hovericons to disable while space key is pressed
  380. var hoverIconsIsResetEvent = this.hoverIcons.isResetEvent;
  381. this.hoverIcons.isResetEvent = function(evt, allowShift)
  382. {
  383. return spaceKeyPressed || hoverIconsIsResetEvent.apply(this, arguments);
  384. };
  385. this.keydownHandler = mxUtils.bind(this, function(evt)
  386. {
  387. if (evt.which == 32 /* Space */ && !graph.isEditing())
  388. {
  389. spaceKeyPressed = true;
  390. this.hoverIcons.reset();
  391. graph.container.style.cursor = 'move';
  392. // Disables scroll after space keystroke with scrollbars
  393. if (!graph.isEditing() && mxEvent.getSource(evt) == graph.container)
  394. {
  395. mxEvent.consume(evt);
  396. }
  397. }
  398. else if (!mxEvent.isConsumed(evt) && evt.keyCode == 27 /* Escape */)
  399. {
  400. this.hideDialog(null, true);
  401. }
  402. });
  403. mxEvent.addListener(document, 'keydown', this.keydownHandler);
  404. this.keyupHandler = mxUtils.bind(this, function(evt)
  405. {
  406. graph.container.style.cursor = '';
  407. spaceKeyPressed = false;
  408. });
  409. mxEvent.addListener(document, 'keyup', this.keyupHandler);
  410. // Forces panning for middle and right mouse buttons
  411. var panningHandlerIsForcePanningEvent = graph.panningHandler.isForcePanningEvent;
  412. graph.panningHandler.isForcePanningEvent = function(me)
  413. {
  414. // Ctrl+left button is reported as right button in FF on Mac
  415. return panningHandlerIsForcePanningEvent.apply(this, arguments) ||
  416. spaceKeyPressed || (mxEvent.isMouseEvent(me.getEvent()) &&
  417. (this.usePopupTrigger || !mxEvent.isPopupTrigger(me.getEvent())) &&
  418. ((!mxEvent.isControlDown(me.getEvent()) &&
  419. mxEvent.isRightMouseButton(me.getEvent())) ||
  420. mxEvent.isMiddleMouseButton(me.getEvent())));
  421. };
  422. // Ctrl/Cmd+Enter applies editing value except in Safari where Ctrl+Enter creates
  423. // a new line (while Enter creates a new paragraph and Shift+Enter stops)
  424. var cellEditorIsStopEditingEvent = graph.cellEditor.isStopEditingEvent;
  425. graph.cellEditor.isStopEditingEvent = function(evt)
  426. {
  427. return cellEditorIsStopEditingEvent.apply(this, arguments) ||
  428. (evt.keyCode == 13 && ((!mxClient.IS_SF && mxEvent.isControlDown(evt)) ||
  429. (mxClient.IS_MAC && mxEvent.isMetaDown(evt)) ||
  430. (mxClient.IS_SF && mxEvent.isShiftDown(evt))));
  431. };
  432. // Adds space+wheel for zoom
  433. var graphIsZoomWheelEvent = graph.isZoomWheelEvent;
  434. graph.isZoomWheelEvent = function()
  435. {
  436. return spaceKeyPressed || graphIsZoomWheelEvent.apply(this, arguments);
  437. };
  438. // Switches toolbar for text editing
  439. var textMode = false;
  440. var fontMenu = null;
  441. var sizeMenu = null;
  442. var nodes = null;
  443. var updateToolbar = mxUtils.bind(this, function()
  444. {
  445. if (this.toolbar != null && textMode != graph.cellEditor.isContentEditing())
  446. {
  447. var node = this.toolbar.container.firstChild;
  448. var newNodes = [];
  449. while (node != null)
  450. {
  451. var tmp = node.nextSibling;
  452. if (mxUtils.indexOf(this.toolbar.staticElements, node) < 0)
  453. {
  454. node.parentNode.removeChild(node);
  455. newNodes.push(node);
  456. }
  457. node = tmp;
  458. }
  459. // Saves references to special items
  460. var tmp1 = this.toolbar.fontMenu;
  461. var tmp2 = this.toolbar.sizeMenu;
  462. if (nodes == null)
  463. {
  464. this.toolbar.createTextToolbar();
  465. }
  466. else
  467. {
  468. for (var i = 0; i < nodes.length; i++)
  469. {
  470. this.toolbar.container.appendChild(nodes[i]);
  471. }
  472. // Restores references to special items
  473. this.toolbar.fontMenu = fontMenu;
  474. this.toolbar.sizeMenu = sizeMenu;
  475. }
  476. textMode = graph.cellEditor.isContentEditing();
  477. fontMenu = tmp1;
  478. sizeMenu = tmp2;
  479. nodes = newNodes;
  480. }
  481. });
  482. var ui = this;
  483. // Overrides cell editor to update toolbar
  484. var cellEditorStartEditing = graph.cellEditor.startEditing;
  485. graph.cellEditor.startEditing = function()
  486. {
  487. cellEditorStartEditing.apply(this, arguments);
  488. updateToolbar();
  489. if (graph.cellEditor.isContentEditing())
  490. {
  491. var updating = false;
  492. var updateCssHandler = function()
  493. {
  494. if (!updating)
  495. {
  496. updating = true;
  497. window.setTimeout(function()
  498. {
  499. var node = graph.getSelectedEditingElement();
  500. if (node != null)
  501. {
  502. var css = mxUtils.getCurrentStyle(node);
  503. if (css != null && ui.toolbar != null)
  504. {
  505. ui.toolbar.setFontName(Graph.stripQuotes(css.fontFamily));
  506. ui.toolbar.setFontSize(parseInt(css.fontSize));
  507. }
  508. }
  509. updating = false;
  510. }, 0);
  511. }
  512. };
  513. mxEvent.addListener(graph.cellEditor.textarea, 'input', updateCssHandler)
  514. mxEvent.addListener(graph.cellEditor.textarea, 'touchend', updateCssHandler);
  515. mxEvent.addListener(graph.cellEditor.textarea, 'mouseup', updateCssHandler);
  516. mxEvent.addListener(graph.cellEditor.textarea, 'keyup', updateCssHandler);
  517. updateCssHandler();
  518. }
  519. };
  520. // Updates toolbar and handles possible errors
  521. var cellEditorStopEditing = graph.cellEditor.stopEditing;
  522. graph.cellEditor.stopEditing = function(cell, trigger)
  523. {
  524. try
  525. {
  526. cellEditorStopEditing.apply(this, arguments);
  527. updateToolbar();
  528. }
  529. catch (e)
  530. {
  531. ui.handleError(e);
  532. }
  533. };
  534. // Enables scrollbars and sets cursor style for the container
  535. graph.container.setAttribute('tabindex', '0');
  536. graph.container.style.cursor = 'default';
  537. // Workaround for page scroll if embedded via iframe
  538. if (window.self === window.top && graph.container.parentNode != null)
  539. {
  540. try
  541. {
  542. graph.container.focus();
  543. }
  544. catch (e)
  545. {
  546. // ignores error in old versions of IE
  547. }
  548. }
  549. // Keeps graph container focused on mouse down
  550. var graphFireMouseEvent = graph.fireMouseEvent;
  551. graph.fireMouseEvent = function(evtName, me, sender)
  552. {
  553. if (evtName == mxEvent.MOUSE_DOWN)
  554. {
  555. this.container.focus();
  556. }
  557. graphFireMouseEvent.apply(this, arguments);
  558. };
  559. // Configures automatic expand on mouseover
  560. graph.popupMenuHandler.autoExpand = true;
  561. // Installs context menu
  562. if (this.menus != null)
  563. {
  564. graph.popupMenuHandler.factoryMethod = mxUtils.bind(this, function(menu, cell, evt)
  565. {
  566. this.menus.createPopupMenu(menu, cell, evt);
  567. });
  568. }
  569. // Hides context menu
  570. mxEvent.addGestureListeners(document, mxUtils.bind(this, function(evt)
  571. {
  572. graph.popupMenuHandler.hideMenu();
  573. }));
  574. // Create handler for key events
  575. this.keyHandler = this.createKeyHandler(editor);
  576. // Getter for key handler
  577. this.getKeyHandler = function()
  578. {
  579. return keyHandler;
  580. };
  581. graph.connectionHandler.addListener(mxEvent.CONNECT, function(sender, evt)
  582. {
  583. var cells = [evt.getProperty('cell')];
  584. if (evt.getProperty('terminalInserted'))
  585. {
  586. cells.push(evt.getProperty('terminal'));
  587. window.setTimeout(function()
  588. {
  589. if (ui.hoverIcons != null)
  590. {
  591. ui.hoverIcons.update(graph.view.getState(cells[cells.length - 1]));
  592. }
  593. }, 0);
  594. }
  595. insertHandler(cells);
  596. });
  597. this.addListener('styleChanged', mxUtils.bind(this, function(sender, evt)
  598. {
  599. // Checks if edges and/or vertices were modified
  600. var cells = evt.getProperty('cells');
  601. var vertex = false;
  602. var edge = false;
  603. if (cells.length > 0)
  604. {
  605. for (var i = 0; i < cells.length; i++)
  606. {
  607. vertex = graph.getModel().isVertex(cells[i]) || vertex;
  608. edge = graph.getModel().isEdge(cells[i]) || edge;
  609. if (edge && vertex)
  610. {
  611. break;
  612. }
  613. }
  614. }
  615. else
  616. {
  617. vertex = true;
  618. edge = true;
  619. }
  620. var keys = evt.getProperty('keys');
  621. var values = evt.getProperty('values');
  622. for (var i = 0; i < keys.length; i++)
  623. {
  624. var common = mxUtils.indexOf(valueStyles, keys[i]) >= 0;
  625. // Ignores transparent stroke colors
  626. if (keys[i] != 'strokeColor' || values[i] != null && values[i] != 'none')
  627. {
  628. // Special case: Edge style and shape
  629. if (mxUtils.indexOf(connectStyles, keys[i]) >= 0)
  630. {
  631. if (edge || mxUtils.indexOf(alwaysEdgeStyles, keys[i]) >= 0)
  632. {
  633. if (values[i] == null)
  634. {
  635. delete graph.currentEdgeStyle[keys[i]];
  636. }
  637. else
  638. {
  639. graph.currentEdgeStyle[keys[i]] = values[i];
  640. }
  641. }
  642. // Uses style for vertex if defined in styles
  643. else if (vertex && mxUtils.indexOf(styles, keys[i]) >= 0)
  644. {
  645. if (values[i] == null)
  646. {
  647. delete graph.currentVertexStyle[keys[i]];
  648. }
  649. else
  650. {
  651. graph.currentVertexStyle[keys[i]] = values[i];
  652. }
  653. }
  654. }
  655. else if (mxUtils.indexOf(styles, keys[i]) >= 0)
  656. {
  657. if (vertex || common)
  658. {
  659. if (values[i] == null)
  660. {
  661. delete graph.currentVertexStyle[keys[i]];
  662. }
  663. else
  664. {
  665. graph.currentVertexStyle[keys[i]] = values[i];
  666. }
  667. }
  668. if (edge || common || mxUtils.indexOf(alwaysEdgeStyles, keys[i]) >= 0)
  669. {
  670. if (values[i] == null)
  671. {
  672. delete graph.currentEdgeStyle[keys[i]];
  673. }
  674. else
  675. {
  676. graph.currentEdgeStyle[keys[i]] = values[i];
  677. }
  678. }
  679. }
  680. }
  681. }
  682. if (this.toolbar != null)
  683. {
  684. this.toolbar.setFontName(graph.currentVertexStyle['fontFamily'] || Menus.prototype.defaultFont);
  685. this.toolbar.setFontSize(graph.currentVertexStyle['fontSize'] || Menus.prototype.defaultFontSize);
  686. if (this.toolbar.edgeStyleMenu != null)
  687. {
  688. // Updates toolbar icon for edge style
  689. var edgeStyleDiv = this.toolbar.edgeStyleMenu.getElementsByTagName('div')[0];
  690. if (graph.currentEdgeStyle['edgeStyle'] == 'orthogonalEdgeStyle' && graph.currentEdgeStyle['curved'] == '1')
  691. {
  692. edgeStyleDiv.className = 'geSprite geSprite-curved';
  693. }
  694. else if (graph.currentEdgeStyle['edgeStyle'] == 'straight' || graph.currentEdgeStyle['edgeStyle'] == 'none' ||
  695. graph.currentEdgeStyle['edgeStyle'] == null)
  696. {
  697. edgeStyleDiv.className = 'geSprite geSprite-straight';
  698. }
  699. else if (graph.currentEdgeStyle['edgeStyle'] == 'entityRelationEdgeStyle')
  700. {
  701. edgeStyleDiv.className = 'geSprite geSprite-entity';
  702. }
  703. else if (graph.currentEdgeStyle['edgeStyle'] == 'elbowEdgeStyle')
  704. {
  705. edgeStyleDiv.className = 'geSprite geSprite-' + ((graph.currentEdgeStyle['elbow'] == 'vertical') ?
  706. 'verticalelbow' : 'horizontalelbow');
  707. }
  708. else if (graph.currentEdgeStyle['edgeStyle'] == 'isometricEdgeStyle')
  709. {
  710. edgeStyleDiv.className = 'geSprite geSprite-' + ((graph.currentEdgeStyle['elbow'] == 'vertical') ?
  711. 'verticalisometric' : 'horizontalisometric');
  712. }
  713. else
  714. {
  715. edgeStyleDiv.className = 'geSprite geSprite-orthogonal';
  716. }
  717. }
  718. if (this.toolbar.edgeShapeMenu != null)
  719. {
  720. // Updates icon for edge shape
  721. var edgeShapeDiv = this.toolbar.edgeShapeMenu.getElementsByTagName('div')[0];
  722. if (graph.currentEdgeStyle['shape'] == 'link')
  723. {
  724. edgeShapeDiv.className = 'geSprite geSprite-linkedge';
  725. }
  726. else if (graph.currentEdgeStyle['shape'] == 'flexArrow')
  727. {
  728. edgeShapeDiv.className = 'geSprite geSprite-arrow';
  729. }
  730. else if (graph.currentEdgeStyle['shape'] == 'arrow')
  731. {
  732. edgeShapeDiv.className = 'geSprite geSprite-simplearrow';
  733. }
  734. else
  735. {
  736. edgeShapeDiv.className = 'geSprite geSprite-connection';
  737. }
  738. }
  739. // Updates icon for optinal line start shape
  740. if (this.toolbar.lineStartMenu != null)
  741. {
  742. var lineStartDiv = this.toolbar.lineStartMenu.getElementsByTagName('div')[0];
  743. lineStartDiv.className = this.getCssClassForMarker('start',
  744. graph.currentEdgeStyle['shape'], graph.currentEdgeStyle[mxConstants.STYLE_STARTARROW],
  745. mxUtils.getValue(graph.currentEdgeStyle, 'startFill', '1'));
  746. }
  747. // Updates icon for optinal line end shape
  748. if (this.toolbar.lineEndMenu != null)
  749. {
  750. var lineEndDiv = this.toolbar.lineEndMenu.getElementsByTagName('div')[0];
  751. lineEndDiv.className = this.getCssClassForMarker('end',
  752. graph.currentEdgeStyle['shape'], graph.currentEdgeStyle[mxConstants.STYLE_ENDARROW],
  753. mxUtils.getValue(graph.currentEdgeStyle, 'endFill', '1'));
  754. }
  755. }
  756. }));
  757. // Update font size and font family labels
  758. if (this.toolbar != null)
  759. {
  760. var update = mxUtils.bind(this, function()
  761. {
  762. var ff = graph.currentVertexStyle['fontFamily'] || 'Helvetica';
  763. var fs = String(graph.currentVertexStyle['fontSize'] || '12');
  764. var state = graph.getView().getState(graph.getSelectionCell());
  765. if (state != null)
  766. {
  767. ff = state.style[mxConstants.STYLE_FONTFAMILY] || ff;
  768. fs = state.style[mxConstants.STYLE_FONTSIZE] || fs;
  769. if (ff.length > 10)
  770. {
  771. ff = ff.substring(0, 8) + '...';
  772. }
  773. }
  774. this.toolbar.setFontName(ff);
  775. this.toolbar.setFontSize(fs);
  776. });
  777. graph.getSelectionModel().addListener(mxEvent.CHANGE, update);
  778. graph.getModel().addListener(mxEvent.CHANGE, update);
  779. }
  780. // Makes sure the current layer is visible when cells are added
  781. graph.addListener(mxEvent.CELLS_ADDED, function(sender, evt)
  782. {
  783. var cells = evt.getProperty('cells');
  784. var parent = evt.getProperty('parent');
  785. if (parent != null && graph.getModel().isLayer(parent) &&
  786. !graph.isCellVisible(parent) && cells != null &&
  787. cells.length > 0)
  788. {
  789. graph.getModel().setVisible(parent, true);
  790. }
  791. });
  792. // Global handler to hide the current menu
  793. this.gestureHandler = mxUtils.bind(this, function(evt)
  794. {
  795. if (this.currentMenu != null && mxEvent.getSource(evt) != this.currentMenu.div)
  796. {
  797. this.hideCurrentMenu();
  798. }
  799. });
  800. mxEvent.addGestureListeners(document, this.gestureHandler);
  801. // Updates the editor UI after the window has been resized or the orientation changes
  802. // Timeout is workaround for old IE versions which have a delay for DOM client sizes.
  803. // Should not use delay > 0 to avoid handle multiple repaints during window resize
  804. this.resizeHandler = mxUtils.bind(this, function()
  805. {
  806. window.setTimeout(mxUtils.bind(this, function()
  807. {
  808. if (this.editor.graph != null)
  809. {
  810. this.refresh();
  811. }
  812. }), 0);
  813. });
  814. mxEvent.addListener(window, 'resize', this.resizeHandler);
  815. this.orientationChangeHandler = mxUtils.bind(this, function()
  816. {
  817. this.refresh();
  818. });
  819. mxEvent.addListener(window, 'orientationchange', this.orientationChangeHandler);
  820. // Workaround for bug on iOS see
  821. // http://stackoverflow.com/questions/19012135/ios-7-ipad-safari-landscape-innerheight-outerheight-layout-issue
  822. if (mxClient.IS_IOS && !window.navigator.standalone && typeof Menus !== 'undefined')
  823. {
  824. this.scrollHandler = mxUtils.bind(this, function()
  825. {
  826. window.scrollTo(0, 0);
  827. });
  828. mxEvent.addListener(window, 'scroll', this.scrollHandler);
  829. }
  830. /**
  831. * Sets the initial scrollbar locations after a file was loaded.
  832. */
  833. this.editor.addListener('resetGraphView', mxUtils.bind(this, function()
  834. {
  835. this.resetScrollbars();
  836. }));
  837. /**
  838. * Repaints the grid.
  839. */
  840. this.addListener('gridEnabledChanged', mxUtils.bind(this, function()
  841. {
  842. graph.view.validateBackground();
  843. }));
  844. this.addListener('backgroundColorChanged', mxUtils.bind(this, function()
  845. {
  846. graph.view.validateBackground();
  847. }));
  848. /**
  849. * Repaints the grid.
  850. */
  851. graph.addListener('gridSizeChanged', mxUtils.bind(this, function()
  852. {
  853. if (graph.isGridEnabled())
  854. {
  855. graph.view.validateBackground();
  856. }
  857. }));
  858. // Resets UI, updates action and menu states
  859. this.editor.resetGraph();
  860. }
  861. this.init();
  862. if (!graph.standalone)
  863. {
  864. this.open();
  865. }
  866. };
  867. /**
  868. * Global config that specifies if the compact UI elements should be used.
  869. */
  870. EditorUi.compactUi = true;
  871. /**
  872. * Static method for pasing PNG files.
  873. */
  874. EditorUi.parsePng = function(f, fn, error)
  875. {
  876. var pos = 0;
  877. function fread(d, count)
  878. {
  879. var start = pos;
  880. pos += count;
  881. return d.substring(start, pos);
  882. };
  883. // Reads unsigned long 32 bit big endian
  884. function _freadint(d)
  885. {
  886. var bytes = fread(d, 4);
  887. return bytes.charCodeAt(3) + (bytes.charCodeAt(2) << 8) +
  888. (bytes.charCodeAt(1) << 16) + (bytes.charCodeAt(0) << 24);
  889. };
  890. // Checks signature
  891. if (fread(f,8) != String.fromCharCode(137) + 'PNG' + String.fromCharCode(13, 10, 26, 10))
  892. {
  893. if (error != null)
  894. {
  895. error();
  896. }
  897. return;
  898. }
  899. // Reads header chunk
  900. fread(f,4);
  901. if (fread(f,4) != 'IHDR')
  902. {
  903. if (error != null)
  904. {
  905. error();
  906. }
  907. return;
  908. }
  909. fread(f, 17);
  910. do
  911. {
  912. var n = _freadint(f);
  913. var type = fread(f,4);
  914. if (fn != null)
  915. {
  916. if (fn(pos - 8, type, n))
  917. {
  918. break;
  919. }
  920. }
  921. value = fread(f,n);
  922. fread(f,4);
  923. if (type == 'IEND')
  924. {
  925. break;
  926. }
  927. }
  928. while (n);
  929. };
  930. // Extends mxEventSource
  931. mxUtils.extend(EditorUi, mxEventSource);
  932. /**
  933. * Specifies the size of the split bar.
  934. */
  935. EditorUi.prototype.splitSize = (mxClient.IS_TOUCH || mxClient.IS_POINTER) ? 12 : 8;
  936. /**
  937. * Specifies the height of the menubar. Default is 30.
  938. */
  939. EditorUi.prototype.menubarHeight = 30;
  940. /**
  941. * Specifies the width of the format panel should be enabled. Default is true.
  942. */
  943. EditorUi.prototype.formatEnabled = true;
  944. /**
  945. * Specifies the width of the format panel. Default is 240.
  946. */
  947. EditorUi.prototype.formatWidth = 240;
  948. /**
  949. * Specifies the height of the toolbar. Default is 38.
  950. */
  951. EditorUi.prototype.toolbarHeight = 38;
  952. /**
  953. * Specifies the height of the footer. Default is 28.
  954. */
  955. EditorUi.prototype.footerHeight = 28;
  956. /**
  957. * Specifies the height of the optional sidebarFooterContainer. Default is 34.
  958. */
  959. EditorUi.prototype.sidebarFooterHeight = 34;
  960. /**
  961. * Specifies the position of the horizontal split bar. Default is 240 or 118 for
  962. * screen widths <= 640px.
  963. */
  964. EditorUi.prototype.hsplitPosition = (screen.width <= 640) ? 118 : ((urlParams['sidebar-entries'] != 'large') ? 212 : 240);
  965. /**
  966. * Specifies if animations are allowed in <executeLayout>. Default is true.
  967. */
  968. EditorUi.prototype.allowAnimation = true;
  969. /**
  970. * Default is 2.
  971. */
  972. EditorUi.prototype.lightboxMaxFitScale = 2;
  973. /**
  974. * Default is 4.
  975. */
  976. EditorUi.prototype.lightboxVerticalDivider = 4;
  977. /**
  978. * Specifies if single click on horizontal split should collapse sidebar. Default is false.
  979. */
  980. EditorUi.prototype.hsplitClickEnabled = false;
  981. /**
  982. * Installs the listeners to update the action states.
  983. */
  984. EditorUi.prototype.init = function()
  985. {
  986. var graph = this.editor.graph;
  987. if (!graph.standalone)
  988. {
  989. if (urlParams['shape-picker'] != '0')
  990. {
  991. this.installShapePicker();
  992. }
  993. // Hides tooltips and connection points when scrolling
  994. mxEvent.addListener(graph.container, 'scroll', mxUtils.bind(this, function()
  995. {
  996. graph.tooltipHandler.hide();
  997. if (graph.connectionHandler != null && graph.connectionHandler.constraintHandler != null)
  998. {
  999. graph.connectionHandler.constraintHandler.reset();
  1000. }
  1001. }));
  1002. // Hides tooltip on escape
  1003. graph.addListener(mxEvent.ESCAPE, mxUtils.bind(this, function()
  1004. {
  1005. graph.tooltipHandler.hide();
  1006. var rb = graph.getRubberband();
  1007. if (rb != null)
  1008. {
  1009. rb.cancel();
  1010. }
  1011. }));
  1012. mxEvent.addListener(graph.container, 'keydown', mxUtils.bind(this, function(evt)
  1013. {
  1014. this.onKeyDown(evt);
  1015. }));
  1016. mxEvent.addListener(graph.container, 'keypress', mxUtils.bind(this, function(evt)
  1017. {
  1018. this.onKeyPress(evt);
  1019. }));
  1020. // Updates action states
  1021. this.addUndoListener();
  1022. this.addBeforeUnloadListener();
  1023. graph.getSelectionModel().addListener(mxEvent.CHANGE, mxUtils.bind(this, function()
  1024. {
  1025. this.updateActionStates();
  1026. }));
  1027. graph.getModel().addListener(mxEvent.CHANGE, mxUtils.bind(this, function()
  1028. {
  1029. this.updateActionStates();
  1030. }));
  1031. // Changes action states after change of default parent
  1032. var graphSetDefaultParent = graph.setDefaultParent;
  1033. var ui = this;
  1034. this.editor.graph.setDefaultParent = function()
  1035. {
  1036. graphSetDefaultParent.apply(this, arguments);
  1037. ui.updateActionStates();
  1038. };
  1039. // Hack to make editLink available in vertex handler
  1040. graph.editLink = ui.actions.get('editLink').funct;
  1041. this.updateActionStates();
  1042. this.initClipboard();
  1043. this.initCanvas();
  1044. if (this.format != null)
  1045. {
  1046. this.format.init();
  1047. }
  1048. }
  1049. };
  1050. /**
  1051. * Returns information about the current selection.
  1052. */
  1053. EditorUi.prototype.clearSelectionState = function()
  1054. {
  1055. this.selectionState = null;
  1056. };
  1057. /**
  1058. * Returns information about the current selection.
  1059. */
  1060. EditorUi.prototype.getSelectionState = function()
  1061. {
  1062. if (this.selectionState == null)
  1063. {
  1064. this.selectionState = this.createSelectionState();
  1065. }
  1066. return this.selectionState;
  1067. };
  1068. /**
  1069. * Returns information about the current selection.
  1070. */
  1071. EditorUi.prototype.createSelectionState = function()
  1072. {
  1073. var graph = this.editor.graph;
  1074. var cells = graph.getSelectionCells();
  1075. var result = this.initSelectionState();
  1076. var initial = true;
  1077. for (var i = 0; i < cells.length; i++)
  1078. {
  1079. var style = graph.getCurrentCellStyle(cells[i]);
  1080. if (mxUtils.getValue(style, mxConstants.STYLE_EDITABLE, '1') != '0')
  1081. {
  1082. this.updateSelectionStateForCell(result, cells[i], cells, initial);
  1083. initial = false;
  1084. }
  1085. }
  1086. this.updateSelectionStateForTableCells(result);
  1087. return result;
  1088. };
  1089. /**
  1090. * Returns information about the current selection.
  1091. */
  1092. EditorUi.prototype.initSelectionState = function()
  1093. {
  1094. return {vertices: [], edges: [], cells: [], x: null, y: null, width: null, height: null,
  1095. style: {}, containsImage: false, containsLabel: false, fill: true, glass: true,
  1096. rounded: true, autoSize: false, image: true, shadow: true, lineJumps: true, resizable: true,
  1097. table: false, cell: false, row: false, movable: true, rotatable: true, stroke: true,
  1098. swimlane: false, unlocked: this.editor.graph.isEnabled(), connections: false};
  1099. };
  1100. /**
  1101. * Adds information about current selected table cells range.
  1102. */
  1103. EditorUi.prototype.updateSelectionStateForTableCells = function(result)
  1104. {
  1105. if (result.cells.length > 1 && result.cell)
  1106. {
  1107. var cells = mxUtils.sortCells(result.cells);
  1108. var model = this.editor.graph.model;
  1109. var parent = model.getParent(cells[0]);
  1110. var table = model.getParent(parent);
  1111. var col = parent.getIndex(cells[0]);
  1112. var row = table.getIndex(parent);
  1113. var lastspan = null;
  1114. var colspan = 1;
  1115. var rowspan = 1;
  1116. var index = 0;
  1117. var nextRowCell = (row < table.getChildCount() - 1) ?
  1118. model.getChildAt(model.getChildAt(
  1119. table, row + 1), col) : null;
  1120. while (index < cells.length - 1)
  1121. {
  1122. var next = cells[++index];
  1123. if (nextRowCell != null && nextRowCell == next &&
  1124. (lastspan == null || colspan == lastspan))
  1125. {
  1126. lastspan = colspan;
  1127. colspan = 0;
  1128. rowspan++;
  1129. parent = model.getParent(nextRowCell);
  1130. nextRowCell = (row + rowspan < table.getChildCount()) ?
  1131. model.getChildAt(model.getChildAt(
  1132. table, row + rowspan), col) : null;
  1133. }
  1134. var state = this.editor.graph.view.getState(next);
  1135. if (next == model.getChildAt(parent, col + colspan) && state != null &&
  1136. mxUtils.getValue(state.style, 'colspan', 1) == 1 &&
  1137. mxUtils.getValue(state.style, 'rowspan', 1) == 1)
  1138. {
  1139. colspan++;
  1140. }
  1141. else
  1142. {
  1143. break;
  1144. }
  1145. }
  1146. if (index == rowspan * colspan - 1)
  1147. {
  1148. result.mergeCell = cells[0];
  1149. result.colspan = colspan;
  1150. result.rowspan = rowspan;
  1151. }
  1152. }
  1153. };
  1154. /**
  1155. * Returns information about the current selection.
  1156. */
  1157. EditorUi.prototype.updateSelectionStateForCell = function(result, cell, cells, initial)
  1158. {
  1159. var graph = this.editor.graph;
  1160. result.cells.push(cell);
  1161. if (graph.getModel().isVertex(cell))
  1162. {
  1163. result.connections = graph.model.getEdgeCount(cell) > 0;
  1164. result.unlocked = result.unlocked && !graph.isCellLocked(cell);
  1165. result.resizable = result.resizable && graph.isCellResizable(cell);
  1166. result.rotatable = result.rotatable && graph.isCellRotatable(cell);
  1167. result.movable = result.movable && graph.isCellMovable(cell) &&
  1168. !graph.isTableRow(cell) && !graph.isTableCell(cell);
  1169. result.swimlane = result.swimlane || graph.isSwimlane(cell);
  1170. result.table = result.table || graph.isTable(cell);
  1171. result.cell = result.cell || graph.isTableCell(cell);
  1172. result.row = result.row || graph.isTableRow(cell);
  1173. result.vertices.push(cell);
  1174. var geo = graph.getCellGeometry(cell);
  1175. if (geo != null)
  1176. {
  1177. if (geo.width > 0)
  1178. {
  1179. if (result.width == null)
  1180. {
  1181. result.width = geo.width;
  1182. }
  1183. else if (result.width != geo.width)
  1184. {
  1185. result.width = '';
  1186. }
  1187. }
  1188. else
  1189. {
  1190. result.containsLabel = true;
  1191. }
  1192. if (geo.height > 0)
  1193. {
  1194. if (result.height == null)
  1195. {
  1196. result.height = geo.height;
  1197. }
  1198. else if (result.height != geo.height)
  1199. {
  1200. result.height = '';
  1201. }
  1202. }
  1203. else
  1204. {
  1205. result.containsLabel = true;
  1206. }
  1207. if (!geo.relative || geo.offset != null)
  1208. {
  1209. var x = (geo.relative) ? geo.offset.x : geo.x;
  1210. var y = (geo.relative) ? geo.offset.y : geo.y;
  1211. if (result.x == null)
  1212. {
  1213. result.x = x;
  1214. }
  1215. else if (result.x != x)
  1216. {
  1217. result.x = '';
  1218. }
  1219. if (result.y == null)
  1220. {
  1221. result.y = y;
  1222. }
  1223. else if (result.y != y)
  1224. {
  1225. result.y = '';
  1226. }
  1227. }
  1228. }
  1229. }
  1230. else if (graph.getModel().isEdge(cell))
  1231. {
  1232. result.edges.push(cell);
  1233. result.connections = true;
  1234. result.resizable = false;
  1235. result.rotatable = false;
  1236. result.movable = false;
  1237. }
  1238. var state = graph.view.getState(cell);
  1239. if (state != null)
  1240. {
  1241. result.autoSize = result.autoSize || graph.isAutoSizeState(state);
  1242. result.glass = result.glass && graph.isGlassState(state);
  1243. result.rounded = result.rounded && graph.isRoundedState(state);
  1244. result.lineJumps = result.lineJumps && graph.isLineJumpState(state);
  1245. result.image = result.image && graph.isImageState(state);
  1246. result.shadow = result.shadow && graph.isShadowState(state);
  1247. result.fill = result.fill && graph.isFillState(state);
  1248. result.stroke = result.stroke && graph.isStrokeState(state);
  1249. var shape = mxUtils.getValue(state.style, mxConstants.STYLE_SHAPE, null);
  1250. result.containsImage = result.containsImage || shape == 'image';
  1251. graph.mergeStyle(state.style, result.style, initial);
  1252. }
  1253. };
  1254. /**
  1255. * Returns true if the given event should start editing. This implementation returns true.
  1256. */
  1257. EditorUi.prototype.installShapePicker = function()
  1258. {
  1259. var graph = this.editor.graph;
  1260. var ui = this;
  1261. // Uses this event to process mouseDown to check the selection state before it is changed
  1262. graph.addListener(mxEvent.FIRE_MOUSE_EVENT, mxUtils.bind(this, function(sender, evt)
  1263. {
  1264. if (evt.getProperty('eventName') == 'mouseDown')
  1265. {
  1266. ui.hideShapePicker();
  1267. }
  1268. }));
  1269. var hidePicker = mxUtils.bind(this, function()
  1270. {
  1271. ui.hideShapePicker(true);
  1272. });
  1273. graph.addListener('wheel', hidePicker);
  1274. graph.addListener(mxEvent.ESCAPE, hidePicker);
  1275. graph.view.addListener(mxEvent.SCALE, hidePicker);
  1276. graph.view.addListener(mxEvent.SCALE_AND_TRANSLATE, hidePicker);
  1277. graph.getSelectionModel().addListener(mxEvent.CHANGE, hidePicker);
  1278. // Counts as popup menu
  1279. var popupMenuHandlerIsMenuShowing = graph.popupMenuHandler.isMenuShowing;
  1280. graph.popupMenuHandler.isMenuShowing = function()
  1281. {
  1282. return popupMenuHandlerIsMenuShowing.apply(this, arguments) || ui.shapePicker != null;
  1283. };
  1284. // Adds dbl click dialog for inserting shapes
  1285. var graphDblClick = graph.dblClick;
  1286. graph.dblClick = function(evt, cell)
  1287. {
  1288. if (this.isEnabled())
  1289. {
  1290. if (cell == null && ui.sidebar != null && !mxEvent.isShiftDown(evt) &&
  1291. !graph.isCellLocked(graph.getDefaultParent()))
  1292. {
  1293. var pt = mxUtils.convertPoint(this.container, mxEvent.getClientX(evt), mxEvent.getClientY(evt));
  1294. mxEvent.consume(evt);
  1295. // Asynchronous to avoid direct insert after double tap
  1296. window.setTimeout(mxUtils.bind(this, function()
  1297. {
  1298. ui.showShapePicker(pt.x, pt.y);
  1299. }), 30);
  1300. }
  1301. else
  1302. {
  1303. graphDblClick.apply(this, arguments);
  1304. }
  1305. }
  1306. };
  1307. if (this.hoverIcons != null)
  1308. {
  1309. this.hoverIcons.addListener('reset', hidePicker);
  1310. var hoverIconsDrag = this.hoverIcons.drag;
  1311. this.hoverIcons.drag = function()
  1312. {
  1313. ui.hideShapePicker();
  1314. hoverIconsDrag.apply(this, arguments);
  1315. };
  1316. var hoverIconsExecute = this.hoverIcons.execute;
  1317. this.hoverIcons.execute = function(state, dir, me)
  1318. {
  1319. var evt = me.getEvent();
  1320. if (!this.graph.isCloneEvent(evt) && !mxEvent.isShiftDown(evt))
  1321. {
  1322. this.graph.connectVertex(state.cell, dir, this.graph.defaultEdgeLength, evt, null, null, mxUtils.bind(this, function(x, y, execute)
  1323. {
  1324. var temp = graph.getCompositeParent(state.cell);
  1325. var geo = graph.getCellGeometry(temp);
  1326. me.consume();
  1327. while (temp != null && graph.model.isVertex(temp) && geo != null && geo.relative)
  1328. {
  1329. cell = temp;
  1330. temp = graph.model.getParent(cell)
  1331. geo = graph.getCellGeometry(temp);
  1332. }
  1333. // Asynchronous to avoid direct insert after double tap
  1334. window.setTimeout(mxUtils.bind(this, function()
  1335. {
  1336. ui.showShapePicker(me.getGraphX(), me.getGraphY(), temp, mxUtils.bind(this, function(cell)
  1337. {
  1338. execute(cell);
  1339. if (ui.hoverIcons != null)
  1340. {
  1341. ui.hoverIcons.update(graph.view.getState(cell));
  1342. }
  1343. }), dir);
  1344. }), 30);
  1345. }), mxUtils.bind(this, function(result)
  1346. {
  1347. this.graph.selectCellsForConnectVertex(result, evt, this);
  1348. }));
  1349. }
  1350. else
  1351. {
  1352. hoverIconsExecute.apply(this, arguments);
  1353. }
  1354. };
  1355. var thread = null;
  1356. this.hoverIcons.addListener('focus', mxUtils.bind(this, function(sender, evt)
  1357. {
  1358. if (thread != null)
  1359. {
  1360. window.clearTimeout(thread);
  1361. }
  1362. thread = window.setTimeout(mxUtils.bind(this, function()
  1363. {
  1364. var arrow = evt.getProperty('arrow');
  1365. var dir = evt.getProperty('direction');
  1366. var mouseEvent = evt.getProperty('event');
  1367. var rect = arrow.getBoundingClientRect();
  1368. var offset = mxUtils.getOffset(graph.container);
  1369. var x = graph.container.scrollLeft + rect.x - offset.x;
  1370. var y = graph.container.scrollTop + rect.y - offset.y;
  1371. var temp = graph.getCompositeParent((this.hoverIcons.currentState != null) ?
  1372. this.hoverIcons.currentState.cell : null);
  1373. var div = ui.showShapePicker(x, y, temp, mxUtils.bind(this, function(cell)
  1374. {
  1375. if (cell != null)
  1376. {
  1377. graph.connectVertex(temp, dir, graph.defaultEdgeLength, mouseEvent, true, true, function(x, y, execute)
  1378. {
  1379. execute(cell);
  1380. if (ui.hoverIcons != null)
  1381. {
  1382. ui.hoverIcons.update(graph.view.getState(cell));
  1383. }
  1384. }, function(cells)
  1385. {
  1386. graph.selectCellsForConnectVertex(cells);
  1387. }, mouseEvent, this.hoverIcons);
  1388. }
  1389. }), dir, true);
  1390. this.centerShapePicker(div, rect, x, y, dir);
  1391. mxUtils.setOpacity(div, 30);
  1392. mxEvent.addListener(div, 'mouseenter', function()
  1393. {
  1394. mxUtils.setOpacity(div, 100);
  1395. });
  1396. mxEvent.addListener(div, 'mouseleave', function()
  1397. {
  1398. ui.hideShapePicker();
  1399. });
  1400. }), Editor.shapePickerHoverDelay);
  1401. }));
  1402. this.hoverIcons.addListener('blur', mxUtils.bind(this, function(sender, evt)
  1403. {
  1404. if (thread != null)
  1405. {
  1406. window.clearTimeout(thread);
  1407. }
  1408. }));
  1409. }
  1410. };
  1411. /**
  1412. * Creates a temporary graph instance for rendering off-screen content.
  1413. */
  1414. EditorUi.prototype.centerShapePicker = function(div, rect, x, y, dir)
  1415. {
  1416. if (dir == mxConstants.DIRECTION_EAST || dir == mxConstants.DIRECTION_WEST)
  1417. {
  1418. div.style.width = '40px';
  1419. }
  1420. var r2 = div.getBoundingClientRect();
  1421. if (dir == mxConstants.DIRECTION_NORTH)
  1422. {
  1423. x -= r2.width / 2 - 10;
  1424. y -= r2.height + 6;
  1425. }
  1426. else if (dir == mxConstants.DIRECTION_SOUTH)
  1427. {
  1428. x -= r2.width / 2 - 10;
  1429. y += rect.height + 6;
  1430. }
  1431. else if (dir == mxConstants.DIRECTION_WEST)
  1432. {
  1433. x -= r2.width + 6;
  1434. y -= r2.height / 2 - 10;
  1435. }
  1436. else if (dir == mxConstants.DIRECTION_EAST)
  1437. {
  1438. x += rect.width + 6;
  1439. y -= r2.height / 2 - 10;
  1440. }
  1441. div.style.left = x + 'px';
  1442. div.style.top = y + 'px';
  1443. };
  1444. /**
  1445. * Creates a temporary graph instance for rendering off-screen content.
  1446. */
  1447. EditorUi.prototype.showShapePicker = function(x, y, source, callback, direction, hovering)
  1448. {
  1449. var div = this.createShapePicker(x, y, source, callback, direction, mxUtils.bind(this, function()
  1450. {
  1451. this.hideShapePicker();
  1452. }), this.getCellsForShapePicker(source, hovering), hovering);
  1453. if (div != null)
  1454. {
  1455. if (this.hoverIcons != null && !hovering)
  1456. {
  1457. this.hoverIcons.reset();
  1458. }
  1459. var graph = this.editor.graph;
  1460. graph.popupMenuHandler.hideMenu();
  1461. graph.tooltipHandler.hideTooltip();
  1462. this.hideCurrentMenu();
  1463. this.hideShapePicker();
  1464. this.shapePickerCallback = callback;
  1465. this.shapePicker = div;
  1466. }
  1467. return div;
  1468. };
  1469. /**
  1470. * Creates a temporary graph instance for rendering off-screen content.
  1471. */
  1472. EditorUi.prototype.createShapePicker = function(x, y, source, callback, direction, afterClick, cells, hovering)
  1473. {
  1474. var div = null;
  1475. if (cells != null && cells.length > 0)
  1476. {
  1477. var ui = this;
  1478. var graph = this.editor.graph;
  1479. div = document.createElement('div');
  1480. var sourceState = graph.view.getState(source);
  1481. var style = (source != null && (sourceState == null ||
  1482. !graph.isTransparentState(sourceState))) ?
  1483. graph.copyStyle(source) : null;
  1484. // Do not place entry under pointer for touch devices
  1485. var w = (cells.length < 6) ? cells.length * 35 : 140;
  1486. div.className = 'geToolbarContainer geSidebarContainer';
  1487. div.style.cssText = 'position:absolute;left:' + x + 'px;top:' + y +
  1488. 'px;width:' + w + 'px;border-radius:10px;padding:4px;text-align:center;' +
  1489. 'box-shadow:0px 0px 3px 1px #d1d1d1;padding: 6px 0 8px 0;' +
  1490. 'z-index: ' + mxPopupMenu.prototype.zIndex + 1 + ';';
  1491. if (!hovering)
  1492. {
  1493. mxUtils.setPrefixedStyle(div.style, 'transform', 'translate(-22px,-22px)');
  1494. }
  1495. if (graph.background != null && graph.background != mxConstants.NONE)
  1496. {
  1497. div.style.backgroundColor = graph.background;
  1498. }
  1499. graph.container.appendChild(div);
  1500. var addCell = mxUtils.bind(this, function(cell)
  1501. {
  1502. // Wrapper needed to catch events
  1503. var node = document.createElement('a');
  1504. node.className = 'geItem';
  1505. node.style.cssText = 'position:relative;display:inline-block;position:relative;' +
  1506. 'width:30px;height:30px;cursor:pointer;overflow:hidden;padding:3px 0 0 3px;';
  1507. div.appendChild(node);
  1508. if (style != null && urlParams['sketch'] != '1')
  1509. {
  1510. this.sidebar.graph.pasteStyle(style, [cell]);
  1511. }
  1512. else
  1513. {
  1514. ui.insertHandler([cell], cell.value != '' && urlParams['sketch'] != '1', this.sidebar.graph.model);
  1515. }
  1516. this.sidebar.createThumb([cell], 25, 25, node, null, true, false, cell.geometry.width, cell.geometry.height);
  1517. mxEvent.addListener(node, 'click', function()
  1518. {
  1519. var clone = graph.cloneCell(cell);
  1520. if (callback != null)
  1521. {
  1522. callback(clone);
  1523. }
  1524. else
  1525. {
  1526. clone.geometry.x = graph.snap(Math.round(x / graph.view.scale) -
  1527. graph.view.translate.x - cell.geometry.width / 2);
  1528. clone.geometry.y = graph.snap(Math.round(y / graph.view.scale) -
  1529. graph.view.translate.y - cell.geometry.height / 2);
  1530. graph.model.beginUpdate();
  1531. try
  1532. {
  1533. graph.addCell(clone);
  1534. }
  1535. finally
  1536. {
  1537. graph.model.endUpdate();
  1538. }
  1539. graph.setSelectionCell(clone);
  1540. graph.scrollCellToVisible(clone);
  1541. graph.startEditingAtCell(clone);
  1542. if (ui.hoverIcons != null)
  1543. {
  1544. ui.hoverIcons.update(graph.view.getState(clone));
  1545. }
  1546. }
  1547. if (afterClick != null)
  1548. {
  1549. afterClick();
  1550. }
  1551. });
  1552. });
  1553. for (var i = 0; i < (hovering ? Math.min(cells.length, 4) : cells.length); i++)
  1554. {
  1555. addCell(cells[i]);
  1556. }
  1557. var b = graph.container.scrollTop + graph.container.offsetHeight;
  1558. var dy = div.offsetTop + div.clientHeight - b;
  1559. if (dy > 0)
  1560. {
  1561. div.style.top = Math.max(graph.container.scrollTop + 22, y - dy) + 'px';
  1562. }
  1563. var r = graph.container.scrollLeft + graph.container.offsetWidth;
  1564. var dx = div.offsetLeft + div.clientWidth - r;
  1565. if (dx > 0)
  1566. {
  1567. div.style.left = Math.max(graph.container.scrollLeft + 22, x - dx) + 'px';
  1568. }
  1569. }
  1570. return div;
  1571. };
  1572. /**
  1573. * Creates a temporary graph instance for rendering off-screen content.
  1574. */
  1575. EditorUi.prototype.getCellsForShapePicker = function(cell, hovering)
  1576. {
  1577. var createVertex = mxUtils.bind(this, function(style, w, h, value)
  1578. {
  1579. return this.editor.graph.createVertex(null, null, value || '', 0, 0, w || 120, h || 60, style, false);
  1580. });
  1581. return [(cell != null) ? this.editor.graph.cloneCell(cell) :
  1582. createVertex('text;html=1;align=center;verticalAlign=middle;resizable=0;points=[];autosize=1;strokeColor=none;fillColor=none;', 40, 20, 'Text'),
  1583. createVertex('whiteSpace=wrap;html=1;'),
  1584. createVertex('ellipse;whiteSpace=wrap;html=1;'),
  1585. createVertex('rhombus;whiteSpace=wrap;html=1;', 80, 80),
  1586. createVertex('rounded=1;whiteSpace=wrap;html=1;'),
  1587. createVertex('shape=parallelogram;perimeter=parallelogramPerimeter;whiteSpace=wrap;html=1;fixedSize=1;'),
  1588. createVertex('shape=trapezoid;perimeter=trapezoidPerimeter;whiteSpace=wrap;html=1;fixedSize=1;', 120, 60),
  1589. createVertex('shape=hexagon;perimeter=hexagonPerimeter2;whiteSpace=wrap;html=1;fixedSize=1;', 120, 80),
  1590. createVertex('shape=step;perimeter=stepPerimeter;whiteSpace=wrap;html=1;fixedSize=1;', 120, 80),
  1591. createVertex('shape=process;whiteSpace=wrap;html=1;backgroundOutline=1;'),
  1592. createVertex('triangle;whiteSpace=wrap;html=1;', 60, 80),
  1593. createVertex('shape=document;whiteSpace=wrap;html=1;boundedLbl=1;', 120, 80),
  1594. createVertex('shape=tape;whiteSpace=wrap;html=1;', 120, 100),
  1595. createVertex('ellipse;shape=cloud;whiteSpace=wrap;html=1;', 120, 80),
  1596. createVertex('shape=singleArrow;whiteSpace=wrap;html=1;arrowWidth=0.4;arrowSize=0.4;', 80, 60),
  1597. createVertex('shape=waypoint;sketch=0;size=6;pointerEvents=1;points=[];fillColor=none;resizable=0;rotatable=0;perimeter=centerPerimeter;snapToPoint=1;', 40, 40)];
  1598. };
  1599. /**
  1600. * Creates a temporary graph instance for rendering off-screen content.
  1601. */
  1602. EditorUi.prototype.hideShapePicker = function(cancel)
  1603. {
  1604. if (this.shapePicker != null)
  1605. {
  1606. this.shapePicker.parentNode.removeChild(this.shapePicker);
  1607. this.shapePicker = null;
  1608. if (!cancel && this.shapePickerCallback != null)
  1609. {
  1610. this.shapePickerCallback();
  1611. }
  1612. this.shapePickerCallback = null;
  1613. }
  1614. };
  1615. /**
  1616. * Returns true if the given event should start editing. This implementation returns true.
  1617. */
  1618. EditorUi.prototype.onKeyDown = function(evt)
  1619. {
  1620. var graph = this.editor.graph;
  1621. // Alt+tab for task switcher in Windows, ctrl+tab for tab control in Chrome
  1622. if (evt.which == 9 && graph.isEnabled() && !mxEvent.isControlDown(evt))
  1623. {
  1624. if (graph.isEditing())
  1625. {
  1626. if (mxEvent.isAltDown(evt))
  1627. {
  1628. graph.stopEditing(false);
  1629. }
  1630. else
  1631. {
  1632. try
  1633. {
  1634. var nesting = graph.cellEditor.isContentEditing() && graph.cellEditor.isTextSelected();
  1635. if (window.getSelection && graph.cellEditor.isContentEditing() &&
  1636. !nesting && !mxClient.IS_IE && !mxClient.IS_IE11)
  1637. {
  1638. var selection = window.getSelection();
  1639. var container = (selection.rangeCount > 0) ? selection.getRangeAt(0).commonAncestorContainer : null;
  1640. nesting = container != null && (container.nodeName == 'LI' || (container.parentNode != null &&
  1641. container.parentNode.nodeName == 'LI'));
  1642. }
  1643. if (nesting)
  1644. {
  1645. // (Shift+)tab indents/outdents with text selection or inside list elements
  1646. document.execCommand(mxEvent.isShiftDown(evt) ? 'outdent' : 'indent', false, null);
  1647. }
  1648. // Shift+tab applies value with cursor
  1649. else if (mxEvent.isShiftDown(evt))
  1650. {
  1651. graph.stopEditing(false);
  1652. }
  1653. else
  1654. {
  1655. // Inserts tab character
  1656. graph.cellEditor.insertTab(!graph.cellEditor.isContentEditing() ? 4 : null);
  1657. }
  1658. }
  1659. catch (e)
  1660. {
  1661. // ignore
  1662. }
  1663. }
  1664. }
  1665. else if (mxEvent.isAltDown(evt))
  1666. {
  1667. graph.selectParentCell();
  1668. }
  1669. else
  1670. {
  1671. graph.selectCell(!mxEvent.isShiftDown(evt));
  1672. }
  1673. mxEvent.consume(evt);
  1674. }
  1675. };
  1676. /**
  1677. * Returns true if the given event should start editing. This implementation returns true.
  1678. */
  1679. EditorUi.prototype.onKeyPress = function(evt)
  1680. {
  1681. var graph = this.editor.graph;
  1682. // KNOWN: Focus does not work if label is empty in quirks mode
  1683. if (this.isImmediateEditingEvent(evt) && !graph.isEditing() && !graph.isSelectionEmpty() && evt.which !== 0 &&
  1684. evt.which !== 27 && !mxEvent.isAltDown(evt) && !mxEvent.isControlDown(evt) && !mxEvent.isMetaDown(evt))
  1685. {
  1686. graph.escape();
  1687. graph.startEditing();
  1688. // Workaround for FF where char is lost if cursor is placed before char
  1689. if (mxClient.IS_FF)
  1690. {
  1691. var ce = graph.cellEditor;
  1692. if (ce.textarea != null)
  1693. {
  1694. ce.textarea.innerHTML = String.fromCharCode(evt.which);
  1695. // Moves cursor to end of textarea
  1696. var range = document.createRange();
  1697. range.selectNodeContents(ce.textarea);
  1698. range.collapse(false);
  1699. var sel = window.getSelection();
  1700. sel.removeAllRanges();
  1701. sel.addRange(range);
  1702. }
  1703. }
  1704. }
  1705. };
  1706. /**
  1707. * Returns true if the given event should start editing. This implementation returns true.
  1708. */
  1709. EditorUi.prototype.isImmediateEditingEvent = function(evt)
  1710. {
  1711. return true;
  1712. };
  1713. /**
  1714. * Private helper method.
  1715. */
  1716. EditorUi.prototype.getCssClassForMarker = function(prefix, shape, marker, fill)
  1717. {
  1718. var result = '';
  1719. if (shape == 'flexArrow')
  1720. {
  1721. result = (marker != null && marker != mxConstants.NONE) ?
  1722. 'geSprite geSprite-' + prefix + 'blocktrans' : 'geSprite geSprite-noarrow';
  1723. }
  1724. else
  1725. {
  1726. // SVG marker sprites
  1727. if (marker == 'box' || marker == 'halfCircle')
  1728. {
  1729. result = 'geSprite geSvgSprite geSprite-' + marker + ((prefix == 'end') ? ' geFlipSprite' : '');
  1730. }
  1731. else if (marker == mxConstants.ARROW_CLASSIC)
  1732. {
  1733. result = (fill == '1') ? 'geSprite geSprite-' + prefix + 'classic' : 'geSprite geSprite-' + prefix + 'classictrans';
  1734. }
  1735. else if (marker == mxConstants.ARROW_CLASSIC_THIN)
  1736. {
  1737. result = (fill == '1') ? 'geSprite geSprite-' + prefix + 'classicthin' : 'geSprite geSprite-' + prefix + 'classicthintrans';
  1738. }
  1739. else if (marker == mxConstants.ARROW_OPEN)
  1740. {
  1741. result = 'geSprite geSprite-' + prefix + 'open';
  1742. }
  1743. else if (marker == mxConstants.ARROW_OPEN_THIN)
  1744. {
  1745. result = 'geSprite geSprite-' + prefix + 'openthin';
  1746. }
  1747. else if (marker == mxConstants.ARROW_BLOCK)
  1748. {
  1749. result = (fill == '1') ? 'geSprite geSprite-' + prefix + 'block' : 'geSprite geSprite-' + prefix + 'blocktrans';
  1750. }
  1751. else if (marker == mxConstants.ARROW_BLOCK_THIN)
  1752. {
  1753. result = (fill == '1') ? 'geSprite geSprite-' + prefix + 'blockthin' : 'geSprite geSprite-' + prefix + 'blockthintrans';
  1754. }
  1755. else if (marker == mxConstants.ARROW_OVAL)
  1756. {
  1757. result = (fill == '1') ? 'geSprite geSprite-' + prefix + 'oval' : 'geSprite geSprite-' + prefix + 'ovaltrans';
  1758. }
  1759. else if (marker == mxConstants.ARROW_DIAMOND)
  1760. {
  1761. result = (fill == '1') ? 'geSprite geSprite-' + prefix + 'diamond' : 'geSprite geSprite-' + prefix + 'diamondtrans';
  1762. }
  1763. else if (marker == mxConstants.ARROW_DIAMOND_THIN)
  1764. {
  1765. result = (fill == '1') ? 'geSprite geSprite-' + prefix + 'thindiamond' : 'geSprite geSprite-' + prefix + 'thindiamondtrans';
  1766. }
  1767. else if (marker == 'openAsync')
  1768. {
  1769. result = 'geSprite geSprite-' + prefix + 'openasync';
  1770. }
  1771. else if (marker == 'dash')
  1772. {
  1773. result = 'geSprite geSprite-' + prefix + 'dash';
  1774. }
  1775. else if (marker == 'cross')
  1776. {
  1777. result = 'geSprite geSprite-' + prefix + 'cross';
  1778. }
  1779. else if (marker == 'async')
  1780. {
  1781. result = (fill == '1') ? 'geSprite geSprite-' + prefix + 'async' : 'geSprite geSprite-' + prefix + 'asynctrans';
  1782. }
  1783. else if (marker == 'circle' || marker == 'circlePlus')
  1784. {
  1785. result = (fill == '1' || marker == 'circle') ? 'geSprite geSprite-' + prefix + 'circle' : 'geSprite geSprite-' + prefix + 'circleplus';
  1786. }
  1787. else if (marker == 'ERone')
  1788. {
  1789. result = 'geSprite geSprite-' + prefix + 'erone';
  1790. }
  1791. else if (marker == 'ERmandOne')
  1792. {
  1793. result = 'geSprite geSprite-' + prefix + 'eronetoone';
  1794. }
  1795. else if (marker == 'ERmany')
  1796. {
  1797. result = 'geSprite geSprite-' + prefix + 'ermany';
  1798. }
  1799. else if (marker == 'ERoneToMany')
  1800. {
  1801. result = 'geSprite geSprite-' + prefix + 'eronetomany';
  1802. }
  1803. else if (marker == 'ERzeroToOne')
  1804. {
  1805. result = 'geSprite geSprite-' + prefix + 'eroneopt';
  1806. }
  1807. else if (marker == 'ERzeroToMany')
  1808. {
  1809. result = 'geSprite geSprite-' + prefix + 'ermanyopt';
  1810. }
  1811. else
  1812. {
  1813. result = 'geSprite geSprite-noarrow';
  1814. }
  1815. }
  1816. return result;
  1817. };
  1818. /**
  1819. * Overridden in Menus.js
  1820. */
  1821. EditorUi.prototype.createMenus = function()
  1822. {
  1823. return null;
  1824. };
  1825. /**
  1826. * Hook for allowing selection and context menu for certain events.
  1827. */
  1828. EditorUi.prototype.updatePasteActionStates = function()
  1829. {
  1830. var graph = this.editor.graph;
  1831. var paste = this.actions.get('paste');
  1832. var pasteHere = this.actions.get('pasteHere');
  1833. paste.setEnabled(this.editor.graph.cellEditor.isContentEditing() ||
  1834. (((!mxClient.IS_FF && navigator.clipboard != null) || !mxClipboard.isEmpty()) &&
  1835. graph.isEnabled() && !graph.isCellLocked(graph.getDefaultParent())));
  1836. pasteHere.setEnabled(paste.isEnabled());
  1837. };
  1838. /**
  1839. * Hook for allowing selection and context menu for certain events.
  1840. */
  1841. EditorUi.prototype.initClipboard = function()
  1842. {
  1843. var ui = this;
  1844. var mxClipboardCut = mxClipboard.cut;
  1845. mxClipboard.cut = function(graph)
  1846. {
  1847. if (graph.cellEditor.isContentEditing())
  1848. {
  1849. document.execCommand('cut', false, null);
  1850. }
  1851. else
  1852. {
  1853. mxClipboardCut.apply(this, arguments);
  1854. }
  1855. ui.updatePasteActionStates();
  1856. };
  1857. var mxClipboardCopy = mxClipboard.copy;
  1858. mxClipboard.copy = function(graph)
  1859. {
  1860. var result = null;
  1861. if (graph.cellEditor.isContentEditing())
  1862. {
  1863. document.execCommand('copy', false, null);
  1864. }
  1865. else
  1866. {
  1867. result = result || graph.getSelectionCells();
  1868. result = graph.getExportableCells(graph.model.getTopmostCells(result));
  1869. var cloneMap = new Object();
  1870. var lookup = graph.createCellLookup(result);
  1871. var clones = graph.cloneCells(result, null, cloneMap);
  1872. // Uses temporary model to force new IDs to be assigned
  1873. // to avoid having to carry over the mapping from object
  1874. // ID to cell ID to the paste operation
  1875. var model = new mxGraphModel();
  1876. var parent = model.getChildAt(model.getRoot(), 0);
  1877. for (var i = 0; i < clones.length; i++)
  1878. {
  1879. model.add(parent, clones[i]);
  1880. // Checks for orphaned relative children and makes absolute
  1881. var state = graph.view.getState(result[i]);
  1882. if (state != null)
  1883. {
  1884. var geo = graph.getCellGeometry(clones[i]);
  1885. if (geo != null && geo.relative && !model.isEdge(result[i]) &&
  1886. lookup[mxObjectIdentity.get(model.getParent(result[i]))] == null)
  1887. {
  1888. geo.offset = null;
  1889. geo.relative = false;
  1890. geo.x = state.x / state.view.scale - state.view.translate.x;
  1891. geo.y = state.y / state.view.scale - state.view.translate.y;
  1892. }
  1893. }
  1894. }
  1895. graph.updateCustomLinks(graph.createCellMapping(cloneMap, lookup), clones);
  1896. mxClipboard.insertCount = 1;
  1897. mxClipboard.setCells(clones);
  1898. }
  1899. ui.updatePasteActionStates();
  1900. return result;
  1901. };
  1902. var mxClipboardPaste = mxClipboard.paste;
  1903. mxClipboard.paste = function(graph)
  1904. {
  1905. var result = null;
  1906. if (graph.cellEditor.isContentEditing())
  1907. {
  1908. document.execCommand('paste', false, null);
  1909. }
  1910. else
  1911. {
  1912. result = mxClipboardPaste.apply(this, arguments);
  1913. }
  1914. ui.updatePasteActionStates();
  1915. return result;
  1916. };
  1917. // Overrides cell editor to update paste action state
  1918. var cellEditorStartEditing = this.editor.graph.cellEditor.startEditing;
  1919. this.editor.graph.cellEditor.startEditing = function()
  1920. {
  1921. cellEditorStartEditing.apply(this, arguments);
  1922. ui.updatePasteActionStates();
  1923. };
  1924. var cellEditorStopEditing = this.editor.graph.cellEditor.stopEditing;
  1925. this.editor.graph.cellEditor.stopEditing = function(cell, trigger)
  1926. {
  1927. cellEditorStopEditing.apply(this, arguments);
  1928. ui.updatePasteActionStates();
  1929. };
  1930. this.updatePasteActionStates();
  1931. };
  1932. /**
  1933. * Delay between zoom steps when not using preview.
  1934. */
  1935. EditorUi.prototype.lazyZoomDelay = 20;
  1936. /**
  1937. * Delay before update of DOM when using preview.
  1938. */
  1939. EditorUi.prototype.wheelZoomDelay = 400;
  1940. /**
  1941. * Delay before update of DOM when using preview.
  1942. */
  1943. EditorUi.prototype.buttonZoomDelay = 600;
  1944. /**
  1945. * Initializes the infinite canvas.
  1946. */
  1947. EditorUi.prototype.initCanvas = function()
  1948. {
  1949. // Initial page layout view, scrollBuffer and timer-based scrolling
  1950. var graph = this.editor.graph;
  1951. graph.timerAutoScroll = true;
  1952. /**
  1953. * Returns the padding for pages in page view with scrollbars.
  1954. */
  1955. graph.getPagePadding = function()
  1956. {
  1957. return new mxPoint(Math.max(0, Math.round((graph.container.offsetWidth - 34) / graph.view.scale)),
  1958. Math.max(0, Math.round((graph.container.offsetHeight - 34) / graph.view.scale)));
  1959. };
  1960. // Fits the number of background pages to the graph
  1961. graph.view.getBackgroundPageBounds = function()
  1962. {
  1963. var layout = this.graph.getPageLayout();
  1964. var page = this.graph.getPageSize();
  1965. return new mxRectangle(this.scale * (this.translate.x + layout.x * page.width),
  1966. this.scale * (this.translate.y + layout.y * page.height),
  1967. this.scale * layout.width * page.width,
  1968. this.scale * layout.height * page.height);
  1969. };
  1970. graph.getPreferredPageSize = function(bounds, width, height)
  1971. {
  1972. var pages = this.getPageLayout();
  1973. var size = this.getPageSize();
  1974. return new mxRectangle(0, 0, pages.width * size.width, pages.height * size.height);
  1975. };
  1976. // Scales pages/graph to fit available size
  1977. var resize = null;
  1978. var ui = this;
  1979. if (this.editor.isChromelessView())
  1980. {
  1981. resize = mxUtils.bind(this, function(autoscale, maxScale, cx, cy)
  1982. {
  1983. if (graph.container != null && !graph.isViewer())
  1984. {
  1985. cx = (cx != null) ? cx : 0;
  1986. cy = (cy != null) ? cy : 0;
  1987. var bds = (graph.pageVisible) ? graph.view.getBackgroundPageBounds() : graph.getGraphBounds();
  1988. var scroll = mxUtils.hasScrollbars(graph.container);
  1989. var tr = graph.view.translate;
  1990. var s = graph.view.scale;
  1991. // Normalizes the bounds
  1992. var b = mxRectangle.fromRectangle(bds);
  1993. b.x = b.x / s - tr.x;
  1994. b.y = b.y / s - tr.y;
  1995. b.width /= s;
  1996. b.height /= s;
  1997. var st = graph.container.scrollTop;
  1998. var sl = graph.container.scrollLeft;
  1999. var sb = (document.documentMode >= 8) ? 20 : 14;
  2000. if (document.documentMode == 8 || document.documentMode == 9)
  2001. {
  2002. sb += 3;
  2003. }
  2004. var cw = graph.container.offsetWidth - sb;
  2005. var ch = graph.container.offsetHeight - sb;
  2006. var ns = (autoscale) ? Math.max(0.3, Math.min(maxScale || 1, cw / b.width)) : s;
  2007. var dx = ((cw - ns * b.width) / 2) / ns;
  2008. var dy = (this.lightboxVerticalDivider == 0) ? 0 : ((ch - ns * b.height) / this.lightboxVerticalDivider) / ns;
  2009. if (scroll)
  2010. {
  2011. dx = Math.max(dx, 0);
  2012. dy = Math.max(dy, 0);
  2013. }
  2014. if (scroll || bds.width < cw || bds.height < ch)
  2015. {
  2016. graph.view.scaleAndTranslate(ns, Math.floor(dx - b.x), Math.floor(dy - b.y));
  2017. graph.container.scrollTop = st * ns / s;
  2018. graph.container.scrollLeft = sl * ns / s;
  2019. }
  2020. else if (cx != 0 || cy != 0)
  2021. {
  2022. var t = graph.view.translate;
  2023. graph.view.setTranslate(Math.floor(t.x + cx / s), Math.floor(t.y + cy / s));
  2024. }
  2025. }
  2026. });
  2027. // Hack to make function available to subclassers
  2028. this.chromelessResize = resize;
  2029. // Hook for subclassers for override
  2030. this.chromelessWindowResize = mxUtils.bind(this, function()
  2031. {
  2032. this.chromelessResize(false);
  2033. });
  2034. // Removable resize listener
  2035. var autoscaleResize = mxUtils.bind(this, function()
  2036. {
  2037. this.chromelessWindowResize(false);
  2038. });
  2039. mxEvent.addListener(window, 'resize', autoscaleResize);
  2040. this.destroyFunctions.push(function()
  2041. {
  2042. mxEvent.removeListener(window, 'resize', autoscaleResize);
  2043. });
  2044. this.editor.addListener('resetGraphView', mxUtils.bind(this, function()
  2045. {
  2046. this.chromelessResize(true);
  2047. }));
  2048. this.actions.get('zoomIn').funct = mxUtils.bind(this, function(evt)
  2049. {
  2050. graph.zoomIn();
  2051. this.chromelessResize(false);
  2052. });
  2053. this.actions.get('zoomOut').funct = mxUtils.bind(this, function(evt)
  2054. {
  2055. graph.zoomOut();
  2056. this.chromelessResize(false);
  2057. });
  2058. // Creates toolbar for viewer - do not use CSS here
  2059. // as this may be used in a viewer that has no CSS
  2060. if (urlParams['toolbar'] != '0')
  2061. {
  2062. var toolbarConfig = JSON.parse(decodeURIComponent(urlParams['toolbar-config'] || '{}'));
  2063. this.chromelessToolbar = document.createElement('div');
  2064. this.chromelessToolbar.style.position = 'fixed';
  2065. this.chromelessToolbar.style.overflow = 'hidden';
  2066. this.chromelessToolbar.style.boxSizing = 'border-box';
  2067. this.chromelessToolbar.style.whiteSpace = 'nowrap';
  2068. this.chromelessToolbar.style.padding = '10px 10px 8px 10px';
  2069. this.chromelessToolbar.style.left = (graph.isViewer()) ? '0' : '50%';
  2070. if (!mxClient.IS_IE && !mxClient.IS_IE11)
  2071. {
  2072. this.chromelessToolbar.style.backgroundColor = '#000000';
  2073. }
  2074. else
  2075. {
  2076. this.chromelessToolbar.style.backgroundColor = '#ffffff';
  2077. this.chromelessToolbar.style.border = '3px solid black';
  2078. }
  2079. mxUtils.setPrefixedStyle(this.chromelessToolbar.style, 'borderRadius', '16px');
  2080. mxUtils.setPrefixedStyle(this.chromelessToolbar.style, 'transition', 'opacity 600ms ease-in-out');
  2081. var updateChromelessToolbarPosition = mxUtils.bind(this, function()
  2082. {
  2083. var css = mxUtils.getCurrentStyle(graph.container);
  2084. if (graph.isViewer())
  2085. {
  2086. this.chromelessToolbar.style.top = '0';
  2087. }
  2088. else
  2089. {
  2090. this.chromelessToolbar.style.bottom = ((css != null) ? parseInt(css['margin-bottom'] || 0) : 0) +
  2091. ((this.tabContainer != null) ? (20 + parseInt(this.tabContainer.style.height)) : 20) + 'px';
  2092. }
  2093. });
  2094. this.editor.addListener('resetGraphView', updateChromelessToolbarPosition);
  2095. updateChromelessToolbarPosition();
  2096. var btnCount = 0;
  2097. var addButton = mxUtils.bind(this, function(fn, imgSrc, tip)
  2098. {
  2099. btnCount++;
  2100. var a = document.createElement('span');
  2101. a.style.paddingLeft = '8px';
  2102. a.style.paddingRight = '8px';
  2103. a.style.cursor = 'pointer';
  2104. mxEvent.addListener(a, 'click', fn);
  2105. if (tip != null)
  2106. {
  2107. a.setAttribute('title', tip);
  2108. }
  2109. var img = document.createElement('img');
  2110. img.setAttribute('border', '0');
  2111. img.setAttribute('src', imgSrc);
  2112. img.style.width = '36px';
  2113. img.style.filter = 'invert(100%)';
  2114. a.appendChild(img);
  2115. this.chromelessToolbar.appendChild(a);
  2116. return a;
  2117. });
  2118. if (toolbarConfig.backBtn != null)
  2119. {
  2120. addButton(mxUtils.bind(this, function(evt)
  2121. {
  2122. window.location.href = toolbarConfig.backBtn.url;
  2123. mxEvent.consume(evt);
  2124. }), Editor.backImage, mxResources.get('back', null, 'Back'));
  2125. }
  2126. if (this.isPagesEnabled())
  2127. {
  2128. var prevButton = addButton(mxUtils.bind(this, function(evt)
  2129. {
  2130. this.actions.get('previousPage').funct();
  2131. mxEvent.consume(evt);
  2132. }), Editor.previousImage, mxResources.get('previousPage'));
  2133. var pageInfo = document.createElement('div');
  2134. pageInfo.style.fontFamily = Editor.defaultHtmlFont;
  2135. pageInfo.style.display = 'inline-block';
  2136. pageInfo.style.verticalAlign = 'top';
  2137. pageInfo.style.fontWeight = 'bold';
  2138. pageInfo.style.marginTop = '8px';
  2139. pageInfo.style.fontSize = '14px';
  2140. if (!mxClient.IS_IE && !mxClient.IS_IE11)
  2141. {
  2142. pageInfo.style.color = '#ffffff';
  2143. }
  2144. else
  2145. {
  2146. pageInfo.style.color = '#000000';
  2147. }
  2148. this.chromelessToolbar.appendChild(pageInfo);
  2149. var nextButton = addButton(mxUtils.bind(this, function(evt)
  2150. {
  2151. this.actions.get('nextPage').funct();
  2152. mxEvent.consume(evt);
  2153. }), Editor.nextImage, mxResources.get('nextPage'));
  2154. var updatePageInfo = mxUtils.bind(this, function()
  2155. {
  2156. if (this.pages != null && this.pages.length > 1 && this.currentPage != null)
  2157. {
  2158. pageInfo.innerHTML = '';
  2159. mxUtils.write(pageInfo, (mxUtils.indexOf(this.pages, this.currentPage) + 1) + ' / ' + this.pages.length);
  2160. }
  2161. });
  2162. prevButton.style.paddingLeft = '0px';
  2163. prevButton.style.paddingRight = '4px';
  2164. nextButton.style.paddingLeft = '4px';
  2165. nextButton.style.paddingRight = '0px';
  2166. var updatePageButtons = mxUtils.bind(this, function()
  2167. {
  2168. if (this.pages != null && this.pages.length > 1 && this.currentPage != null)
  2169. {
  2170. nextButton.style.display = '';
  2171. prevButton.style.display = '';
  2172. pageInfo.style.display = 'inline-block';
  2173. }
  2174. else
  2175. {
  2176. nextButton.style.display = 'none';
  2177. prevButton.style.display = 'none';
  2178. pageInfo.style.display = 'none';
  2179. }
  2180. updatePageInfo();
  2181. });
  2182. this.editor.addListener('resetGraphView', updatePageButtons);
  2183. this.editor.addListener('pageSelected', updatePageInfo);
  2184. }
  2185. addButton(mxUtils.bind(this, function(evt)
  2186. {
  2187. this.actions.get('zoomOut').funct();
  2188. mxEvent.consume(evt);
  2189. }), Editor.zoomOutImage, mxResources.get('zoomOut') + ' (Alt+Mousewheel)');
  2190. addButton(mxUtils.bind(this, function(evt)
  2191. {
  2192. this.actions.get('zoomIn').funct();
  2193. mxEvent.consume(evt);
  2194. }), Editor.zoomInImage, mxResources.get('zoomIn') + ' (Alt+Mousewheel)');
  2195. addButton(mxUtils.bind(this, function(evt)
  2196. {
  2197. if (graph.isLightboxView())
  2198. {
  2199. if (graph.view.scale == 1)
  2200. {
  2201. this.lightboxFit();
  2202. }
  2203. else
  2204. {
  2205. graph.zoomTo(1);
  2206. }
  2207. this.chromelessResize(false);
  2208. }
  2209. else
  2210. {
  2211. this.chromelessResize(true);
  2212. }
  2213. mxEvent.consume(evt);
  2214. }), Editor.zoomFitImage, mxResources.get('fit'));
  2215. // Changes toolbar opacity on hover
  2216. var fadeThread = null;
  2217. var fadeThread2 = null;
  2218. var fadeOut = mxUtils.bind(this, function(delay)
  2219. {
  2220. if (fadeThread != null)
  2221. {
  2222. window.clearTimeout(fadeThread);
  2223. fadeThread = null;
  2224. }
  2225. if (fadeThread2 != null)
  2226. {
  2227. window.clearTimeout(fadeThread2);
  2228. fadeThread2 = null;
  2229. }
  2230. fadeThread = window.setTimeout(mxUtils.bind(this, function()
  2231. {
  2232. mxUtils.setOpacity(this.chromelessToolbar, 0);
  2233. fadeThread = null;
  2234. fadeThread2 = window.setTimeout(mxUtils.bind(this, function()
  2235. {
  2236. this.chromelessToolbar.style.display = 'none';
  2237. fadeThread2 = null;
  2238. }), 600);
  2239. }), delay || 200);
  2240. });
  2241. var fadeIn = mxUtils.bind(this, function(opacity)
  2242. {
  2243. if (fadeThread != null)
  2244. {
  2245. window.clearTimeout(fadeThread);
  2246. fadeThread = null;
  2247. }
  2248. if (fadeThread2 != null)
  2249. {
  2250. window.clearTimeout(fadeThread2);
  2251. fadeThread2 = null;
  2252. }
  2253. this.chromelessToolbar.style.display = '';
  2254. mxUtils.setOpacity(this.chromelessToolbar, opacity || 30);
  2255. });
  2256. if (urlParams['layers'] == '1')
  2257. {
  2258. this.layersDialog = null;
  2259. var layersButton = addButton(mxUtils.bind(this, function(evt)
  2260. {
  2261. if (this.layersDialog != null)
  2262. {
  2263. this.layersDialog.parentNode.removeChild(this.layersDialog);
  2264. this.layersDialog = null;
  2265. }
  2266. else
  2267. {
  2268. this.layersDialog = graph.createLayersDialog(null, true);
  2269. mxEvent.addListener(this.layersDialog, 'mouseleave', mxUtils.bind(this, function()
  2270. {
  2271. this.layersDialog.parentNode.removeChild(this.layersDialog);
  2272. this.layersDialog = null;
  2273. }));
  2274. var r = layersButton.getBoundingClientRect();
  2275. mxUtils.setPrefixedStyle(this.layersDialog.style, 'borderRadius', '5px');
  2276. this.layersDialog.style.position = 'fixed';
  2277. this.layersDialog.style.fontFamily = Editor.defaultHtmlFont;
  2278. this.layersDialog.style.width = '160px';
  2279. this.layersDialog.style.padding = '4px 2px 4px 2px';
  2280. this.layersDialog.style.left = r.left + 'px';
  2281. this.layersDialog.style.bottom = parseInt(this.chromelessToolbar.style.bottom) +
  2282. this.chromelessToolbar.offsetHeight + 4 + 'px';
  2283. if (!mxClient.IS_IE && !mxClient.IS_IE11)
  2284. {
  2285. this.layersDialog.style.backgroundColor = '#000000';
  2286. this.layersDialog.style.color = '#ffffff';
  2287. mxUtils.setOpacity(this.layersDialog, 80);
  2288. }
  2289. else
  2290. {
  2291. this.layersDialog.style.backgroundColor = '#ffffff';
  2292. this.layersDialog.style.border = '2px solid black';
  2293. this.layersDialog.style.color = '#000000';
  2294. }
  2295. // Puts the dialog on top of the container z-index
  2296. var style = mxUtils.getCurrentStyle(this.editor.graph.container);
  2297. this.layersDialog.style.zIndex = style.zIndex;
  2298. document.body.appendChild(this.layersDialog);
  2299. this.editor.fireEvent(new mxEventObject('layersDialogShown'));
  2300. }
  2301. mxEvent.consume(evt);
  2302. }), Editor.layersImage, mxResources.get('layers'));
  2303. // Shows/hides layers button depending on content
  2304. var model = graph.getModel();
  2305. model.addListener(mxEvent.CHANGE, function()
  2306. {
  2307. layersButton.style.display = (model.getChildCount(model.root) > 1) ? '' : 'none';
  2308. });
  2309. }
  2310. if (urlParams['openInSameWin'] != '1' || navigator.standalone)
  2311. {
  2312. this.addChromelessToolbarItems(addButton);
  2313. }
  2314. if (this.editor.editButtonLink != null || this.editor.editButtonFunc != null)
  2315. {
  2316. addButton(mxUtils.bind(this, function(evt)
  2317. {
  2318. if (this.editor.editButtonFunc != null)
  2319. {
  2320. this.editor.editButtonFunc();
  2321. }
  2322. else if (this.editor.editButtonLink == '_blank')
  2323. {
  2324. this.editor.editAsNew(this.getEditBlankXml());
  2325. }
  2326. else
  2327. {
  2328. graph.openLink(this.editor.editButtonLink, 'editWindow');
  2329. }
  2330. mxEvent.consume(evt);
  2331. }), Editor.editImage, mxResources.get('edit'));
  2332. }
  2333. if (this.lightboxToolbarActions != null)
  2334. {
  2335. for (var i = 0; i < this.lightboxToolbarActions.length; i++)
  2336. {
  2337. var lbAction = this.lightboxToolbarActions[i];
  2338. lbAction.elem = addButton(lbAction.fn, lbAction.icon, lbAction.tooltip);
  2339. }
  2340. }
  2341. if (toolbarConfig.refreshBtn != null)
  2342. {
  2343. addButton(mxUtils.bind(this, function(evt)
  2344. {
  2345. if (toolbarConfig.refreshBtn.url)
  2346. {
  2347. window.location.href = toolbarConfig.refreshBtn.url;
  2348. }
  2349. else
  2350. {
  2351. window.location.reload();
  2352. }
  2353. mxEvent.consume(evt);
  2354. }), Editor.refreshImage, mxResources.get('refresh', null, 'Refresh'));
  2355. }
  2356. if (toolbarConfig.fullscreenBtn != null && window.self !== window.top)
  2357. {
  2358. addButton(mxUtils.bind(this, function(evt)
  2359. {
  2360. if (toolbarConfig.fullscreenBtn.url)
  2361. {
  2362. graph.openLink(toolbarConfig.fullscreenBtn.url);
  2363. }
  2364. else
  2365. {
  2366. graph.openLink(window.location.href);
  2367. }
  2368. mxEvent.consume(evt);
  2369. }), Editor.fullscreenImage, mxResources.get('openInNewWindow', null, 'Open in New Window'));
  2370. }
  2371. if ((toolbarConfig.closeBtn && window.self === window.top) ||
  2372. (graph.lightbox && (urlParams['close'] == '1' || this.container != document.body)))
  2373. {
  2374. addButton(mxUtils.bind(this, function(evt)
  2375. {
  2376. if (urlParams['close'] == '1' || toolbarConfig.closeBtn)
  2377. {
  2378. window.close();
  2379. }
  2380. else
  2381. {
  2382. this.destroy();
  2383. mxEvent.consume(evt);
  2384. }
  2385. }), Editor.closeImage, mxResources.get('close') + ' (Escape)');
  2386. }
  2387. // Initial state invisible
  2388. this.chromelessToolbar.style.display = 'none';
  2389. if (!graph.isViewer())
  2390. {
  2391. mxUtils.setPrefixedStyle(this.chromelessToolbar.style, 'transform', 'translate(-50%,0)');
  2392. }
  2393. graph.container.appendChild(this.chromelessToolbar);
  2394. mxEvent.addListener(graph.container, (mxClient.IS_POINTER) ? 'pointermove' : 'mousemove', mxUtils.bind(this, function(evt)
  2395. {
  2396. if (!mxEvent.isTouchEvent(evt))
  2397. {
  2398. if (!mxEvent.isShiftDown(evt))
  2399. {
  2400. fadeIn(30);
  2401. }
  2402. fadeOut();
  2403. }
  2404. }));
  2405. mxEvent.addListener(this.chromelessToolbar, (mxClient.IS_POINTER) ? 'pointermove' : 'mousemove', function(evt)
  2406. {
  2407. mxEvent.consume(evt);
  2408. });
  2409. mxEvent.addListener(this.chromelessToolbar, 'mouseenter', mxUtils.bind(this, function(evt)
  2410. {
  2411. graph.tooltipHandler.resetTimer();
  2412. graph.tooltipHandler.hideTooltip();
  2413. if (!mxEvent.isShiftDown(evt))
  2414. {
  2415. fadeIn(100);
  2416. }
  2417. else
  2418. {
  2419. fadeOut();
  2420. }
  2421. }));
  2422. mxEvent.addListener(this.chromelessToolbar, 'mousemove', mxUtils.bind(this, function(evt)
  2423. {
  2424. if (!mxEvent.isShiftDown(evt))
  2425. {
  2426. fadeIn(100);
  2427. }
  2428. else
  2429. {
  2430. fadeOut();
  2431. }
  2432. mxEvent.consume(evt);
  2433. }));
  2434. mxEvent.addListener(this.chromelessToolbar, 'mouseleave', mxUtils.bind(this, function(evt)
  2435. {
  2436. if (!mxEvent.isTouchEvent(evt))
  2437. {
  2438. fadeIn(30);
  2439. }
  2440. }));
  2441. // Shows/hides toolbar for touch devices
  2442. var tol = graph.getTolerance();
  2443. graph.addMouseListener(
  2444. {
  2445. startX: 0,
  2446. startY: 0,
  2447. scrollLeft: 0,
  2448. scrollTop: 0,
  2449. mouseDown: function(sender, me)
  2450. {
  2451. this.startX = me.getGraphX();
  2452. this.startY = me.getGraphY();
  2453. this.scrollLeft = graph.container.scrollLeft;
  2454. this.scrollTop = graph.container.scrollTop;
  2455. },
  2456. mouseMove: function(sender, me) {},
  2457. mouseUp: function(sender, me)
  2458. {
  2459. if (mxEvent.isTouchEvent(me.getEvent()))
  2460. {
  2461. if ((Math.abs(this.scrollLeft - graph.container.scrollLeft) < tol &&
  2462. Math.abs(this.scrollTop - graph.container.scrollTop) < tol) &&
  2463. (Math.abs(this.startX - me.getGraphX()) < tol &&
  2464. Math.abs(this.startY - me.getGraphY()) < tol))
  2465. {
  2466. if (parseFloat(ui.chromelessToolbar.style.opacity || 0) > 0)
  2467. {
  2468. fadeOut();
  2469. }
  2470. else
  2471. {
  2472. fadeIn(30);
  2473. }
  2474. }
  2475. }
  2476. }
  2477. });
  2478. } // end if toolbar
  2479. // Installs handling of highlight and handling links to relative links and anchors
  2480. if (!this.editor.editable)
  2481. {
  2482. this.addChromelessClickHandler();
  2483. }
  2484. }
  2485. else if (this.editor.extendCanvas)
  2486. {
  2487. /**
  2488. * Guesses autoTranslate to avoid another repaint (see below).
  2489. * Works if only the scale of the graph changes or if pages
  2490. * are visible and the visible pages do not change.
  2491. */
  2492. var graphViewValidate = graph.view.validate;
  2493. graph.view.validate = function()
  2494. {
  2495. if (this.graph.container != null && mxUtils.hasScrollbars(this.graph.container))
  2496. {
  2497. var pad = this.graph.getPagePadding();
  2498. var size = this.graph.getPageSize();
  2499. // Updating scrollbars here causes flickering in quirks and is not needed
  2500. // if zoom method is always used to set the current scale on the graph.
  2501. var tx = this.translate.x;
  2502. var ty = this.translate.y;
  2503. this.translate.x = pad.x - (this.x0 || 0) * size.width;
  2504. this.translate.y = pad.y - (this.y0 || 0) * size.height;
  2505. }
  2506. graphViewValidate.apply(this, arguments);
  2507. };
  2508. if (!graph.isViewer())
  2509. {
  2510. var graphSizeDidChange = graph.sizeDidChange;
  2511. graph.sizeDidChange = function()
  2512. {
  2513. if (this.container != null && mxUtils.hasScrollbars(this.container))
  2514. {
  2515. var pages = this.getPageLayout();
  2516. var pad = this.getPagePadding();
  2517. var size = this.getPageSize();
  2518. // Updates the minimum graph size
  2519. var minw = Math.ceil(2 * pad.x + pages.width * size.width);
  2520. var minh = Math.ceil(2 * pad.y + pages.height * size.height);
  2521. var min = graph.minimumGraphSize;
  2522. // LATER: Fix flicker of scrollbar size in IE quirks mode
  2523. // after delayed call in window.resize event handler
  2524. if (min == null || min.width != minw || min.height != minh)
  2525. {
  2526. graph.minimumGraphSize = new mxRectangle(0, 0, minw, minh);
  2527. }
  2528. // Updates auto-translate to include padding and graph size
  2529. var dx = pad.x - pages.x * size.width;
  2530. var dy = pad.y - pages.y * size.height;
  2531. if (!this.autoTranslate && (this.view.translate.x != dx || this.view.translate.y != dy))
  2532. {
  2533. this.autoTranslate = true;
  2534. this.view.x0 = pages.x;
  2535. this.view.y0 = pages.y;
  2536. // NOTE: THIS INVOKES THIS METHOD AGAIN. UNFORTUNATELY THERE IS NO WAY AROUND THIS SINCE THE
  2537. // BOUNDS ARE KNOWN AFTER THE VALIDATION AND SETTING THE TRANSLATE TRIGGERS A REVALIDATION.
  2538. // SHOULD MOVE TRANSLATE/SCALE TO VIEW.
  2539. var tx = graph.view.translate.x;
  2540. var ty = graph.view.translate.y;
  2541. graph.view.setTranslate(dx, dy);
  2542. // LATER: Fix rounding errors for small zoom
  2543. graph.container.scrollLeft += Math.round((dx - tx) * graph.view.scale);
  2544. graph.container.scrollTop += Math.round((dy - ty) * graph.view.scale);
  2545. this.autoTranslate = false;
  2546. return;
  2547. }
  2548. graphSizeDidChange.apply(this, arguments);
  2549. }
  2550. else
  2551. {
  2552. // Fires event but does not invoke superclass
  2553. this.fireEvent(new mxEventObject(mxEvent.SIZE, 'bounds', this.getGraphBounds()));
  2554. }
  2555. };
  2556. }
  2557. }
  2558. // Accumulates the zoom factor while the rendering is taking place
  2559. // so that not the complete sequence of zoom steps must be painted
  2560. var bgGroup = graph.view.getBackgroundPane();
  2561. var mainGroup = graph.view.getDrawPane();
  2562. graph.cumulativeZoomFactor = 1;
  2563. var updateZoomTimeout = null;
  2564. var cursorPosition = null;
  2565. var scrollPosition = null;
  2566. var forcedZoom = null;
  2567. var filter = null;
  2568. var scheduleZoom = function(delay)
  2569. {
  2570. if (updateZoomTimeout != null)
  2571. {
  2572. window.clearTimeout(updateZoomTimeout);
  2573. }
  2574. if (delay >= 0)
  2575. {
  2576. window.setTimeout(function()
  2577. {
  2578. if (!graph.isMouseDown || forcedZoom)
  2579. {
  2580. updateZoomTimeout = window.setTimeout(mxUtils.bind(this, function()
  2581. {
  2582. if (graph.isFastZoomEnabled())
  2583. {
  2584. // Transforms background page
  2585. if (graph.view.backgroundPageShape != null && graph.view.backgroundPageShape.node != null)
  2586. {
  2587. mxUtils.setPrefixedStyle(graph.view.backgroundPageShape.node.style, 'transform-origin', null);
  2588. mxUtils.setPrefixedStyle(graph.view.backgroundPageShape.node.style, 'transform', null);
  2589. }
  2590. // Transforms graph and background image
  2591. mainGroup.style.transformOrigin = '';
  2592. bgGroup.style.transformOrigin = '';
  2593. // Workaround for no reset of transform in Safari
  2594. if (mxClient.IS_SF)
  2595. {
  2596. mainGroup.style.transform = 'scale(1)';
  2597. bgGroup.style.transform = 'scale(1)';
  2598. window.setTimeout(function()
  2599. {
  2600. mainGroup.style.transform = '';
  2601. bgGroup.style.transform = '';
  2602. }, 0)
  2603. }
  2604. else
  2605. {
  2606. mainGroup.style.transform = '';
  2607. bgGroup.style.transform = '';
  2608. }
  2609. // Shows interactive elements
  2610. graph.view.getDecoratorPane().style.opacity = '';
  2611. graph.view.getOverlayPane().style.opacity = '';
  2612. }
  2613. var sp = new mxPoint(graph.container.scrollLeft, graph.container.scrollTop);
  2614. var offset = mxUtils.getOffset(graph.container);
  2615. var prev = graph.view.scale;
  2616. var dx = 0;
  2617. var dy = 0;
  2618. if (cursorPosition != null)
  2619. {
  2620. dx = graph.container.offsetWidth / 2 - cursorPosition.x + offset.x;
  2621. dy = graph.container.offsetHeight / 2 - cursorPosition.y + offset.y;
  2622. }
  2623. graph.zoom(graph.cumulativeZoomFactor, null,
  2624. graph.isFastZoomEnabled() ? 20 : null);
  2625. var s = graph.view.scale;
  2626. if (s != prev)
  2627. {
  2628. if (scrollPosition != null)
  2629. {
  2630. dx += sp.x - scrollPosition.x;
  2631. dy += sp.y - scrollPosition.y;
  2632. }
  2633. if (resize != null)
  2634. {
  2635. ui.chromelessResize(false, null, dx * (graph.cumulativeZoomFactor - 1),
  2636. dy * (graph.cumulativeZoomFactor - 1));
  2637. }
  2638. if (mxUtils.hasScrollbars(graph.container) && (dx != 0 || dy != 0))
  2639. {
  2640. graph.container.scrollLeft -= dx * (graph.cumulativeZoomFactor - 1);
  2641. graph.container.scrollTop -= dy * (graph.cumulativeZoomFactor - 1);
  2642. }
  2643. }
  2644. if (filter != null)
  2645. {
  2646. mainGroup.setAttribute('filter', filter);
  2647. }
  2648. graph.cumulativeZoomFactor = 1;
  2649. updateZoomTimeout = null;
  2650. scrollPosition = null;
  2651. cursorPosition = null;
  2652. forcedZoom = null;
  2653. filter = null;
  2654. }), (delay != null) ? delay : ((graph.isFastZoomEnabled()) ? ui.wheelZoomDelay : ui.lazyZoomDelay));
  2655. }
  2656. }, 0);
  2657. }
  2658. };
  2659. graph.lazyZoom = function(zoomIn, ignoreCursorPosition, delay, factor)
  2660. {
  2661. factor = (factor != null) ? factor : this.zoomFactor;
  2662. // TODO: Fix ignored cursor position if scrollbars are disabled
  2663. ignoreCursorPosition = ignoreCursorPosition || !graph.scrollbars;
  2664. if (ignoreCursorPosition)
  2665. {
  2666. cursorPosition = new mxPoint(
  2667. graph.container.offsetLeft + graph.container.clientWidth / 2,
  2668. graph.container.offsetTop + graph.container.clientHeight / 2);
  2669. }
  2670. // Switches to 5% zoom steps below 15%
  2671. if (zoomIn)
  2672. {
  2673. if (this.view.scale * this.cumulativeZoomFactor <= 0.15)
  2674. {
  2675. this.cumulativeZoomFactor *= (this.view.scale + 0.05) / this.view.scale;
  2676. }
  2677. else
  2678. {
  2679. this.cumulativeZoomFactor *= factor;
  2680. this.cumulativeZoomFactor = Math.round(this.view.scale * this.cumulativeZoomFactor * 100) / 100 / this.view.scale;
  2681. }
  2682. }
  2683. else
  2684. {
  2685. if (this.view.scale * this.cumulativeZoomFactor <= 0.15)
  2686. {
  2687. this.cumulativeZoomFactor *= (this.view.scale - 0.05) / this.view.scale;
  2688. }
  2689. else
  2690. {
  2691. this.cumulativeZoomFactor /= factor;
  2692. this.cumulativeZoomFactor = Math.round(this.view.scale * this.cumulativeZoomFactor * 100) / 100 / this.view.scale;
  2693. }
  2694. }
  2695. this.cumulativeZoomFactor = Math.max(0.05, Math.min(this.view.scale * this.cumulativeZoomFactor, 160)) / this.view.scale;
  2696. if (graph.isFastZoomEnabled())
  2697. {
  2698. if (filter == null && mainGroup.getAttribute('filter') != '')
  2699. {
  2700. filter = mainGroup.getAttribute('filter');
  2701. mainGroup.removeAttribute('filter');
  2702. }
  2703. scrollPosition = new mxPoint(graph.container.scrollLeft, graph.container.scrollTop);
  2704. var cx = (ignoreCursorPosition || cursorPosition == null) ?
  2705. graph.container.scrollLeft + graph.container.clientWidth / 2 :
  2706. cursorPosition.x + graph.container.scrollLeft - graph.container.offsetLeft;
  2707. var cy = (ignoreCursorPosition || cursorPosition == null) ?
  2708. graph.container.scrollTop + graph.container.clientHeight / 2 :
  2709. cursorPosition.y + graph.container.scrollTop - graph.container.offsetTop;
  2710. mainGroup.style.transformOrigin = cx + 'px ' + cy + 'px';
  2711. mainGroup.style.transform = 'scale(' + this.cumulativeZoomFactor + ')';
  2712. bgGroup.style.transformOrigin = cx + 'px ' + cy + 'px';
  2713. bgGroup.style.transform = 'scale(' + this.cumulativeZoomFactor + ')';
  2714. if (graph.view.backgroundPageShape != null && graph.view.backgroundPageShape.node != null)
  2715. {
  2716. var page = graph.view.backgroundPageShape.node;
  2717. mxUtils.setPrefixedStyle(page.style, 'transform-origin',
  2718. ((ignoreCursorPosition || cursorPosition == null) ?
  2719. ((graph.container.clientWidth / 2 + graph.container.scrollLeft -
  2720. page.offsetLeft) + 'px') : ((cursorPosition.x + graph.container.scrollLeft -
  2721. page.offsetLeft - graph.container.offsetLeft) + 'px')) + ' ' +
  2722. ((ignoreCursorPosition || cursorPosition == null) ?
  2723. ((graph.container.clientHeight / 2 + graph.container.scrollTop -
  2724. page.offsetTop) + 'px') : ((cursorPosition.y + graph.container.scrollTop -
  2725. page.offsetTop - graph.container.offsetTop) + 'px')));
  2726. mxUtils.setPrefixedStyle(page.style, 'transform',
  2727. 'scale(' + this.cumulativeZoomFactor + ')');
  2728. }
  2729. graph.view.getDecoratorPane().style.opacity = '0';
  2730. graph.view.getOverlayPane().style.opacity = '0';
  2731. if (ui.hoverIcons != null)
  2732. {
  2733. ui.hoverIcons.reset();
  2734. }
  2735. }
  2736. scheduleZoom(graph.isFastZoomEnabled() ? delay : 0);
  2737. };
  2738. // Holds back repaint until after mouse gestures
  2739. mxEvent.addGestureListeners(graph.container, function(evt)
  2740. {
  2741. if (updateZoomTimeout != null)
  2742. {
  2743. window.clearTimeout(updateZoomTimeout);
  2744. }
  2745. }, null, function(evt)
  2746. {
  2747. if (graph.cumulativeZoomFactor != 1)
  2748. {
  2749. scheduleZoom(0);
  2750. }
  2751. });
  2752. // Holds back repaint until scroll ends
  2753. mxEvent.addListener(graph.container, 'scroll', function(evt)
  2754. {
  2755. if (updateZoomTimeout != null && !graph.isMouseDown && graph.cumulativeZoomFactor != 1)
  2756. {
  2757. scheduleZoom(0);
  2758. }
  2759. });
  2760. mxEvent.addMouseWheelListener(mxUtils.bind(this, function(evt, up, force, cx, cy)
  2761. {
  2762. graph.fireEvent(new mxEventObject('wheel'));
  2763. if (this.dialogs == null || this.dialogs.length == 0)
  2764. {
  2765. // Scrolls with scrollbars turned off
  2766. if (!graph.scrollbars && !force && graph.isScrollWheelEvent(evt))
  2767. {
  2768. var t = graph.view.getTranslate();
  2769. var step = 40 / graph.view.scale;
  2770. if (!mxEvent.isShiftDown(evt))
  2771. {
  2772. graph.view.setTranslate(t.x, t.y + ((up) ? step : -step));
  2773. }
  2774. else
  2775. {
  2776. graph.view.setTranslate(t.x + ((up) ? -step : step), t.y);
  2777. }
  2778. }
  2779. else if (force || graph.isZoomWheelEvent(evt))
  2780. {
  2781. var source = mxEvent.getSource(evt);
  2782. while (source != null)
  2783. {
  2784. if (source == graph.container)
  2785. {
  2786. graph.tooltipHandler.hideTooltip();
  2787. cursorPosition = (cx != null && cy!= null) ? new mxPoint(cx, cy) :
  2788. new mxPoint(mxEvent.getClientX(evt), mxEvent.getClientY(evt));
  2789. forcedZoom = force;
  2790. var factor = graph.zoomFactor;
  2791. var delay = null;
  2792. // Slower zoom for pinch gesture on trackpad with max delta to
  2793. // filter out mouse wheel events in Brave browser for Windows
  2794. if (evt.ctrlKey && evt.deltaY != null && Math.abs(evt.deltaY) < 40 &&
  2795. Math.round(evt.deltaY) != evt.deltaY)
  2796. {
  2797. factor = 1 + (Math.abs(evt.deltaY) / 20) * (factor - 1);
  2798. }
  2799. // Slower zoom for pinch gesture on touch screens
  2800. else if (evt.movementY != null && evt.type == 'pointermove')
  2801. {
  2802. factor = 1 + (Math.max(1, Math.abs(evt.movementY)) / 20) * (factor - 1);
  2803. delay = -1;
  2804. }
  2805. graph.lazyZoom(up, null, delay, factor);
  2806. mxEvent.consume(evt);
  2807. return false;
  2808. }
  2809. source = source.parentNode;
  2810. }
  2811. }
  2812. }
  2813. }), graph.container);
  2814. // Uses fast zoom for pinch gestures on iOS
  2815. graph.panningHandler.zoomGraph = function(evt)
  2816. {
  2817. graph.cumulativeZoomFactor = evt.scale;
  2818. graph.lazyZoom(evt.scale > 0, true);
  2819. mxEvent.consume(evt);
  2820. };
  2821. };
  2822. /**
  2823. * Creates a temporary graph instance for rendering off-screen content.
  2824. */
  2825. EditorUi.prototype.addChromelessToolbarItems = function(addButton)
  2826. {
  2827. addButton(mxUtils.bind(this, function(evt)
  2828. {
  2829. this.actions.get('print').funct();
  2830. mxEvent.consume(evt);
  2831. }), Editor.printImage, mxResources.get('print'));
  2832. };
  2833. /**
  2834. * Creates a temporary graph instance for rendering off-screen content.
  2835. */
  2836. EditorUi.prototype.isPagesEnabled = function()
  2837. {
  2838. return this.editor.editable || urlParams['hide-pages'] != '1';
  2839. };
  2840. /**
  2841. * Creates a temporary graph instance for rendering off-screen content.
  2842. */
  2843. EditorUi.prototype.createTemporaryGraph = function(stylesheet)
  2844. {
  2845. return Graph.createOffscreenGraph(stylesheet);
  2846. };
  2847. /**
  2848. *
  2849. */
  2850. EditorUi.prototype.addChromelessClickHandler = function()
  2851. {
  2852. var hl = urlParams['highlight'];
  2853. // Adds leading # for highlight color code
  2854. if (hl != null && hl.length > 0)
  2855. {
  2856. hl = '#' + hl;
  2857. }
  2858. this.editor.graph.addClickHandler(hl);
  2859. };
  2860. /**
  2861. *
  2862. */
  2863. EditorUi.prototype.toggleFormatPanel = function(visible)
  2864. {
  2865. visible = (visible != null) ? visible : this.formatWidth == 0;
  2866. if (this.format != null)
  2867. {
  2868. this.formatWidth = (visible) ? 240 : 0;
  2869. this.formatContainer.style.display = (visible) ? '' : 'none';
  2870. this.refresh();
  2871. this.format.refresh();
  2872. this.fireEvent(new mxEventObject('formatWidthChanged'));
  2873. }
  2874. };
  2875. /**
  2876. * Adds support for placeholders in labels.
  2877. */
  2878. EditorUi.prototype.lightboxFit = function(maxHeight)
  2879. {
  2880. if (this.isDiagramEmpty())
  2881. {
  2882. this.editor.graph.view.setScale(1);
  2883. }
  2884. else
  2885. {
  2886. var p = urlParams['border'];
  2887. var border = 60;
  2888. if (p != null)
  2889. {
  2890. border = parseInt(p);
  2891. }
  2892. // LATER: Use initial graph bounds to avoid rounding errors
  2893. this.editor.graph.maxFitScale = this.lightboxMaxFitScale;
  2894. this.editor.graph.fit(border, null, null, null, null, null, maxHeight);
  2895. this.editor.graph.maxFitScale = null;
  2896. }
  2897. };
  2898. /**
  2899. * Translates this point by the given vector.
  2900. *
  2901. * @param {number} dx X-coordinate of the translation.
  2902. * @param {number} dy Y-coordinate of the translation.
  2903. */
  2904. EditorUi.prototype.isDiagramEmpty = function()
  2905. {
  2906. var model = this.editor.graph.getModel();
  2907. return model.getChildCount(model.root) == 1 && model.getChildCount(model.getChildAt(model.root, 0)) == 0;
  2908. };
  2909. /**
  2910. * Hook for allowing selection and context menu for certain events.
  2911. */
  2912. EditorUi.prototype.isSelectionAllowed = function(evt)
  2913. {
  2914. return mxEvent.getSource(evt).nodeName == 'SELECT' || (mxEvent.getSource(evt).nodeName == 'INPUT' &&
  2915. mxUtils.isAncestorNode(this.formatContainer, mxEvent.getSource(evt)));
  2916. };
  2917. /**
  2918. * Installs dialog if browser window is closed without saving
  2919. * This must be disabled during save and image export.
  2920. */
  2921. EditorUi.prototype.addBeforeUnloadListener = function()
  2922. {
  2923. // Installs dialog if browser window is closed without saving
  2924. // This must be disabled during save and image export
  2925. window.onbeforeunload = mxUtils.bind(this, function()
  2926. {
  2927. if (!this.editor.isChromelessView())
  2928. {
  2929. return this.onBeforeUnload();
  2930. }
  2931. });
  2932. };
  2933. /**
  2934. * Sets the onbeforeunload for the application
  2935. */
  2936. EditorUi.prototype.onBeforeUnload = function()
  2937. {
  2938. if (this.editor.modified)
  2939. {
  2940. return mxResources.get('allChangesLost');
  2941. }
  2942. };
  2943. /**
  2944. * Opens the current diagram via the window.opener if one exists.
  2945. */
  2946. EditorUi.prototype.open = function()
  2947. {
  2948. // Cross-domain window access is not allowed in FF, so if we
  2949. // were opened from another domain then this will fail.
  2950. try
  2951. {
  2952. if (window.opener != null && window.opener.openFile != null)
  2953. {
  2954. window.opener.openFile.setConsumer(mxUtils.bind(this, function(xml, filename)
  2955. {
  2956. try
  2957. {
  2958. var doc = mxUtils.parseXml(xml);
  2959. this.editor.setGraphXml(doc.documentElement);
  2960. this.editor.setModified(false);
  2961. this.editor.undoManager.clear();
  2962. if (filename != null)
  2963. {
  2964. this.editor.setFilename(filename);
  2965. this.updateDocumentTitle();
  2966. }
  2967. return;
  2968. }
  2969. catch (e)
  2970. {
  2971. mxUtils.alert(mxResources.get('invalidOrMissingFile') + ': ' + e.message);
  2972. }
  2973. }));
  2974. }
  2975. }
  2976. catch(e)
  2977. {
  2978. // ignore
  2979. }
  2980. // Fires as the last step if no file was loaded
  2981. this.editor.graph.view.validate();
  2982. // Required only in special cases where an initial file is opened
  2983. // and the minimumGraphSize changes and CSS must be updated.
  2984. this.editor.graph.sizeDidChange();
  2985. this.editor.fireEvent(new mxEventObject('resetGraphView'));
  2986. };
  2987. /**
  2988. * Shows the given popup menu.
  2989. */
  2990. EditorUi.prototype.showPopupMenu = function(fn, x, y, evt)
  2991. {
  2992. this.editor.graph.popupMenuHandler.hideMenu();
  2993. var menu = new mxPopupMenu(fn);
  2994. menu.div.className += ' geMenubarMenu';
  2995. menu.smartSeparators = true;
  2996. menu.showDisabled = true;
  2997. menu.autoExpand = true;
  2998. // Disables autoexpand and destroys menu when hidden
  2999. menu.hideMenu = mxUtils.bind(this, function()
  3000. {
  3001. mxPopupMenu.prototype.hideMenu.apply(menu, arguments);
  3002. menu.destroy();
  3003. });
  3004. menu.popup(x, y, null, evt);
  3005. // Allows hiding by clicking on document
  3006. this.setCurrentMenu(menu);
  3007. };
  3008. /**
  3009. * Sets the current menu and element.
  3010. */
  3011. EditorUi.prototype.setCurrentMenu = function(menu, elt)
  3012. {
  3013. this.currentMenuElt = elt;
  3014. this.currentMenu = menu;
  3015. };
  3016. /**
  3017. * Resets the current menu and element.
  3018. */
  3019. EditorUi.prototype.resetCurrentMenu = function()
  3020. {
  3021. this.currentMenuElt = null;
  3022. this.currentMenu = null;
  3023. };
  3024. /**
  3025. * Hides and destroys the current menu.
  3026. */
  3027. EditorUi.prototype.hideCurrentMenu = function()
  3028. {
  3029. if (this.currentMenu != null)
  3030. {
  3031. this.currentMenu.hideMenu();
  3032. this.resetCurrentMenu();
  3033. }
  3034. };
  3035. /**
  3036. * Updates the document title.
  3037. */
  3038. EditorUi.prototype.updateDocumentTitle = function()
  3039. {
  3040. var title = this.editor.getOrCreateFilename();
  3041. if (this.editor.appName != null)
  3042. {
  3043. title += ' - ' + this.editor.appName;
  3044. }
  3045. document.title = title;
  3046. };
  3047. /**
  3048. * Updates the document title.
  3049. */
  3050. EditorUi.prototype.createHoverIcons = function()
  3051. {
  3052. return new HoverIcons(this.editor.graph);
  3053. };
  3054. /**
  3055. * Returns the URL for a copy of this editor with no state.
  3056. */
  3057. EditorUi.prototype.redo = function()
  3058. {
  3059. try
  3060. {
  3061. var graph = this.editor.graph;
  3062. if (graph.isEditing())
  3063. {
  3064. document.execCommand('redo', false, null);
  3065. }
  3066. else
  3067. {
  3068. this.editor.undoManager.redo();
  3069. }
  3070. }
  3071. catch (e)
  3072. {
  3073. // ignore all errors
  3074. }
  3075. };
  3076. /**
  3077. * Returns the URL for a copy of this editor with no state.
  3078. */
  3079. EditorUi.prototype.undo = function()
  3080. {
  3081. try
  3082. {
  3083. var graph = this.editor.graph;
  3084. if (graph.isEditing())
  3085. {
  3086. // Stops editing and executes undo on graph if native undo
  3087. // does not affect current editing value
  3088. var value = graph.cellEditor.textarea.innerHTML;
  3089. document.execCommand('undo', false, null);
  3090. if (value == graph.cellEditor.textarea.innerHTML)
  3091. {
  3092. graph.stopEditing(true);
  3093. this.editor.undoManager.undo();
  3094. }
  3095. }
  3096. else
  3097. {
  3098. this.editor.undoManager.undo();
  3099. }
  3100. }
  3101. catch (e)
  3102. {
  3103. // ignore all errors
  3104. }
  3105. };
  3106. /**
  3107. * Returns the URL for a copy of this editor with no state.
  3108. */
  3109. EditorUi.prototype.canRedo = function()
  3110. {
  3111. return this.editor.graph.isEditing() || this.editor.undoManager.canRedo();
  3112. };
  3113. /**
  3114. * Returns the URL for a copy of this editor with no state.
  3115. */
  3116. EditorUi.prototype.canUndo = function()
  3117. {
  3118. return this.editor.graph.isEditing() || this.editor.undoManager.canUndo();
  3119. };
  3120. /**
  3121. *
  3122. */
  3123. EditorUi.prototype.getEditBlankXml = function()
  3124. {
  3125. return mxUtils.getXml(this.editor.getGraphXml());
  3126. };
  3127. /**
  3128. * Returns the URL for a copy of this editor with no state.
  3129. */
  3130. EditorUi.prototype.getUrl = function(pathname)
  3131. {
  3132. var href = (pathname != null) ? pathname : window.location.pathname;
  3133. var parms = (href.indexOf('?') > 0) ? 1 : 0;
  3134. // Removes template URL parameter for new blank diagram
  3135. for (var key in urlParams)
  3136. {
  3137. if (parms == 0)
  3138. {
  3139. href += '?';
  3140. }
  3141. else
  3142. {
  3143. href += '&';
  3144. }
  3145. href += key + '=' + urlParams[key];
  3146. parms++;
  3147. }
  3148. return href;
  3149. };
  3150. /**
  3151. * Specifies if the graph has scrollbars.
  3152. */
  3153. EditorUi.prototype.setScrollbars = function(value)
  3154. {
  3155. var graph = this.editor.graph;
  3156. var prev = graph.container.style.overflow;
  3157. graph.scrollbars = value;
  3158. this.editor.updateGraphComponents();
  3159. if (prev != graph.container.style.overflow)
  3160. {
  3161. graph.container.scrollTop = 0;
  3162. graph.container.scrollLeft = 0;
  3163. graph.view.scaleAndTranslate(1, 0, 0);
  3164. this.resetScrollbars();
  3165. }
  3166. this.fireEvent(new mxEventObject('scrollbarsChanged'));
  3167. };
  3168. /**
  3169. * Returns true if the graph has scrollbars.
  3170. */
  3171. EditorUi.prototype.hasScrollbars = function()
  3172. {
  3173. return this.editor.graph.scrollbars;
  3174. };
  3175. /**
  3176. * Resets the state of the scrollbars.
  3177. */
  3178. EditorUi.prototype.resetScrollbars = function()
  3179. {
  3180. var graph = this.editor.graph;
  3181. if (!this.editor.extendCanvas)
  3182. {
  3183. graph.container.scrollTop = 0;
  3184. graph.container.scrollLeft = 0;
  3185. if (!mxUtils.hasScrollbars(graph.container))
  3186. {
  3187. graph.view.setTranslate(0, 0);
  3188. }
  3189. }
  3190. else if (!this.editor.isChromelessView())
  3191. {
  3192. if (mxUtils.hasScrollbars(graph.container))
  3193. {
  3194. if (graph.pageVisible)
  3195. {
  3196. var pad = graph.getPagePadding();
  3197. graph.container.scrollTop = Math.floor(pad.y - this.editor.initialTopSpacing) - 1;
  3198. graph.container.scrollLeft = Math.floor(Math.min(pad.x,
  3199. (graph.container.scrollWidth - graph.container.clientWidth) / 2)) - 1;
  3200. // Scrolls graph to visible area
  3201. var bounds = graph.getGraphBounds();
  3202. if (bounds.width > 0 && bounds.height > 0)
  3203. {
  3204. if (bounds.x > graph.container.scrollLeft + graph.container.clientWidth * 0.9)
  3205. {
  3206. graph.container.scrollLeft = Math.min(bounds.x + bounds.width - graph.container.clientWidth, bounds.x - 10);
  3207. }
  3208. if (bounds.y > graph.container.scrollTop + graph.container.clientHeight * 0.9)
  3209. {
  3210. graph.container.scrollTop = Math.min(bounds.y + bounds.height - graph.container.clientHeight, bounds.y - 10);
  3211. }
  3212. }
  3213. }
  3214. else
  3215. {
  3216. var bounds = graph.getGraphBounds();
  3217. var width = Math.max(bounds.width, graph.scrollTileSize.width * graph.view.scale);
  3218. var height = Math.max(bounds.height, graph.scrollTileSize.height * graph.view.scale);
  3219. graph.container.scrollTop = Math.floor(Math.max(0, bounds.y - Math.max(20, (graph.container.clientHeight - height) / 4)));
  3220. graph.container.scrollLeft = Math.floor(Math.max(0, bounds.x - Math.max(0, (graph.container.clientWidth - width) / 2)));
  3221. }
  3222. }
  3223. else
  3224. {
  3225. var b = mxRectangle.fromRectangle((graph.pageVisible) ? graph.view.getBackgroundPageBounds() : graph.getGraphBounds())
  3226. var tr = graph.view.translate;
  3227. var s = graph.view.scale;
  3228. b.x = b.x / s - tr.x;
  3229. b.y = b.y / s - tr.y;
  3230. b.width /= s;
  3231. b.height /= s;
  3232. var dy = (graph.pageVisible) ? 0 : Math.max(0, (graph.container.clientHeight - b.height) / 4);
  3233. graph.view.setTranslate(Math.floor(Math.max(0,
  3234. (graph.container.clientWidth - b.width) / 2) - b.x + 2),
  3235. Math.floor(dy - b.y + 1));
  3236. }
  3237. }
  3238. };
  3239. /**
  3240. * Loads the stylesheet for this graph.
  3241. */
  3242. EditorUi.prototype.setPageVisible = function(value)
  3243. {
  3244. var graph = this.editor.graph;
  3245. var hasScrollbars = mxUtils.hasScrollbars(graph.container);
  3246. var tx = 0;
  3247. var ty = 0;
  3248. if (hasScrollbars)
  3249. {
  3250. tx = graph.view.translate.x * graph.view.scale - graph.container.scrollLeft;
  3251. ty = graph.view.translate.y * graph.view.scale - graph.container.scrollTop;
  3252. }
  3253. graph.pageVisible = value;
  3254. graph.pageBreaksVisible = value;
  3255. graph.preferPageSize = value;
  3256. graph.view.validateBackground();
  3257. // Workaround for possible handle offset
  3258. if (hasScrollbars)
  3259. {
  3260. var cells = graph.getSelectionCells();
  3261. graph.clearSelection();
  3262. graph.setSelectionCells(cells);
  3263. }
  3264. // Calls updatePageBreaks
  3265. graph.sizeDidChange();
  3266. if (hasScrollbars)
  3267. {
  3268. graph.container.scrollLeft = graph.view.translate.x * graph.view.scale - tx;
  3269. graph.container.scrollTop = graph.view.translate.y * graph.view.scale - ty;
  3270. }
  3271. graph.defaultPageVisible = value;
  3272. this.fireEvent(new mxEventObject('pageViewChanged'));
  3273. };
  3274. /**
  3275. * Class: ChangeGridColor
  3276. *
  3277. * Undoable change to grid color.
  3278. */
  3279. function ChangeGridColor(ui, color)
  3280. {
  3281. this.ui = ui;
  3282. this.color = color;
  3283. };
  3284. /**
  3285. * Executes selection of a new page.
  3286. */
  3287. ChangeGridColor.prototype.execute = function()
  3288. {
  3289. var temp = this.ui.editor.graph.view.gridColor;
  3290. this.ui.setGridColor(this.color);
  3291. this.color = temp;
  3292. };
  3293. // Registers codec for ChangePageSetup
  3294. (function()
  3295. {
  3296. var codec = new mxObjectCodec(new ChangeGridColor(), ['ui']);
  3297. mxCodecRegistry.register(codec);
  3298. })();
  3299. /**
  3300. * Change types
  3301. */
  3302. function ChangePageSetup(ui, color, image, format, pageScale)
  3303. {
  3304. this.ui = ui;
  3305. this.color = color;
  3306. this.previousColor = color;
  3307. this.image = image;
  3308. this.previousImage = image;
  3309. this.format = format;
  3310. this.previousFormat = format;
  3311. this.pageScale = pageScale;
  3312. this.previousPageScale = pageScale;
  3313. // Needed since null are valid values for color and image
  3314. this.ignoreColor = false;
  3315. this.ignoreImage = false;
  3316. }
  3317. /**
  3318. * Implementation of the undoable page rename.
  3319. */
  3320. ChangePageSetup.prototype.execute = function()
  3321. {
  3322. var graph = this.ui.editor.graph;
  3323. if (!this.ignoreColor)
  3324. {
  3325. this.color = this.previousColor;
  3326. var tmp = graph.background;
  3327. this.ui.setBackgroundColor(this.previousColor);
  3328. this.previousColor = tmp;
  3329. }
  3330. if (!this.ignoreImage)
  3331. {
  3332. this.image = this.previousImage;
  3333. var tmp = graph.backgroundImage;
  3334. var img = this.previousImage;
  3335. if (img != null && img.src != null && img.src.substring(0, 13) == 'data:page/id,')
  3336. {
  3337. img = this.ui.createImageForPageLink(img.src, this.ui.currentPage);
  3338. }
  3339. this.ui.setBackgroundImage(img);
  3340. this.previousImage = tmp;
  3341. }
  3342. if (this.previousFormat != null)
  3343. {
  3344. this.format = this.previousFormat;
  3345. var tmp = graph.pageFormat;
  3346. if (this.previousFormat.width != tmp.width ||
  3347. this.previousFormat.height != tmp.height)
  3348. {
  3349. this.ui.setPageFormat(this.previousFormat);
  3350. this.previousFormat = tmp;
  3351. }
  3352. }
  3353. if (this.foldingEnabled != null && this.foldingEnabled != this.ui.editor.graph.foldingEnabled)
  3354. {
  3355. this.ui.setFoldingEnabled(this.foldingEnabled);
  3356. this.foldingEnabled = !this.foldingEnabled;
  3357. }
  3358. if (this.previousPageScale != null)
  3359. {
  3360. var currentPageScale = this.ui.editor.graph.pageScale;
  3361. if (this.previousPageScale != currentPageScale)
  3362. {
  3363. this.ui.setPageScale(this.previousPageScale);
  3364. this.previousPageScale = currentPageScale;
  3365. }
  3366. }
  3367. };
  3368. // Registers codec for ChangePageSetup
  3369. (function()
  3370. {
  3371. var codec = new mxObjectCodec(new ChangePageSetup(), ['ui', 'previousColor', 'previousImage', 'previousFormat', 'previousPageScale']);
  3372. codec.afterDecode = function(dec, node, obj)
  3373. {
  3374. obj.previousColor = obj.color;
  3375. obj.previousImage = obj.image;
  3376. obj.previousFormat = obj.format;
  3377. obj.previousPageScale = obj.pageScale;
  3378. if (obj.foldingEnabled != null)
  3379. {
  3380. obj.foldingEnabled = !obj.foldingEnabled;
  3381. }
  3382. return obj;
  3383. };
  3384. mxCodecRegistry.register(codec);
  3385. })();
  3386. /**
  3387. * Loads the stylesheet for this graph.
  3388. */
  3389. EditorUi.prototype.setBackgroundColor = function(value)
  3390. {
  3391. this.editor.graph.background = value;
  3392. this.editor.graph.view.validateBackground();
  3393. this.fireEvent(new mxEventObject('backgroundColorChanged'));
  3394. };
  3395. /**
  3396. * Loads the stylesheet for this graph.
  3397. */
  3398. EditorUi.prototype.setFoldingEnabled = function(value)
  3399. {
  3400. this.editor.graph.foldingEnabled = value;
  3401. this.editor.graph.view.revalidate();
  3402. this.fireEvent(new mxEventObject('foldingEnabledChanged'));
  3403. };
  3404. /**
  3405. * Loads the stylesheet for this graph.
  3406. */
  3407. EditorUi.prototype.setPageFormat = function(value, ignorePageVisible)
  3408. {
  3409. ignorePageVisible = (ignorePageVisible != null) ? ignorePageVisible : urlParams['sketch'] == '1';
  3410. this.editor.graph.pageFormat = value;
  3411. if (!ignorePageVisible)
  3412. {
  3413. if (!this.editor.graph.pageVisible)
  3414. {
  3415. this.actions.get('pageView').funct();
  3416. }
  3417. else
  3418. {
  3419. this.editor.graph.view.validateBackground();
  3420. this.editor.graph.sizeDidChange();
  3421. }
  3422. }
  3423. this.fireEvent(new mxEventObject('pageFormatChanged'));
  3424. };
  3425. /**
  3426. * Loads the stylesheet for this graph.
  3427. */
  3428. EditorUi.prototype.setPageScale = function(value)
  3429. {
  3430. this.editor.graph.pageScale = value;
  3431. if (!this.editor.graph.pageVisible)
  3432. {
  3433. this.actions.get('pageView').funct();
  3434. }
  3435. else
  3436. {
  3437. this.editor.graph.view.validateBackground();
  3438. this.editor.graph.sizeDidChange();
  3439. }
  3440. this.fireEvent(new mxEventObject('pageScaleChanged'));
  3441. };
  3442. /**
  3443. * Loads the stylesheet for this graph.
  3444. */
  3445. EditorUi.prototype.setGridColor = function(value)
  3446. {
  3447. this.editor.graph.view.gridColor = value;
  3448. this.editor.graph.view.validateBackground();
  3449. this.fireEvent(new mxEventObject('gridColorChanged'));
  3450. };
  3451. /**
  3452. * Updates the states of the given undo/redo items.
  3453. */
  3454. EditorUi.prototype.addUndoListener = function()
  3455. {
  3456. var undo = this.actions.get('undo');
  3457. var redo = this.actions.get('redo');
  3458. var undoMgr = this.editor.undoManager;
  3459. var undoListener = mxUtils.bind(this, function()
  3460. {
  3461. undo.setEnabled(this.canUndo());
  3462. redo.setEnabled(this.canRedo());
  3463. });
  3464. undoMgr.addListener(mxEvent.ADD, undoListener);
  3465. undoMgr.addListener(mxEvent.UNDO, undoListener);
  3466. undoMgr.addListener(mxEvent.REDO, undoListener);
  3467. undoMgr.addListener(mxEvent.CLEAR, undoListener);
  3468. // Overrides cell editor to update action states
  3469. var cellEditorStartEditing = this.editor.graph.cellEditor.startEditing;
  3470. this.editor.graph.cellEditor.startEditing = function()
  3471. {
  3472. cellEditorStartEditing.apply(this, arguments);
  3473. undoListener();
  3474. };
  3475. var cellEditorStopEditing = this.editor.graph.cellEditor.stopEditing;
  3476. this.editor.graph.cellEditor.stopEditing = function(cell, trigger)
  3477. {
  3478. cellEditorStopEditing.apply(this, arguments);
  3479. undoListener();
  3480. };
  3481. // Updates the button states once
  3482. undoListener();
  3483. };
  3484. /**
  3485. * Updates the states of the given toolbar items based on the selection.
  3486. */
  3487. EditorUi.prototype.updateActionStates = function()
  3488. {
  3489. var graph = this.editor.graph;
  3490. var ss = this.getSelectionState();
  3491. var unlocked = graph.isEnabled() && !graph.isCellLocked(graph.getDefaultParent());
  3492. // Updates action states
  3493. var actions = ['cut', 'copy', 'bold', 'italic', 'underline', 'delete', 'duplicate',
  3494. 'editStyle', 'editTooltip', 'editLink', 'backgroundColor', 'borderColor',
  3495. 'edit', 'toFront', 'toBack', 'solid', 'dashed', 'pasteSize',
  3496. 'dotted', 'fillColor', 'gradientColor', 'shadow', 'fontColor',
  3497. 'formattedText', 'rounded', 'toggleRounded', 'strokeColor',
  3498. 'sharp', 'snapToGrid'];
  3499. for (var i = 0; i < actions.length; i++)
  3500. {
  3501. this.actions.get(actions[i]).setEnabled(ss.cells.length > 0);
  3502. }
  3503. this.actions.get('grid').setEnabled(!this.editor.chromeless || this.editor.editable);
  3504. this.actions.get('pasteSize').setEnabled(this.copiedSize != null && ss.vertices.length > 0);
  3505. this.actions.get('pasteData').setEnabled(this.copiedValue != null && ss.cells.length > 0);
  3506. this.actions.get('setAsDefaultStyle').setEnabled(graph.getSelectionCount() == 1);
  3507. this.actions.get('lockUnlock').setEnabled(!graph.isSelectionEmpty());
  3508. this.actions.get('bringForward').setEnabled(ss.cells.length == 1);
  3509. this.actions.get('sendBackward').setEnabled(ss.cells.length == 1);
  3510. this.actions.get('rotation').setEnabled(ss.vertices.length == 1);
  3511. this.actions.get('wordWrap').setEnabled(ss.vertices.length == 1);
  3512. this.actions.get('autosize').setEnabled(ss.vertices.length == 1);
  3513. this.actions.get('copySize').setEnabled(ss.vertices.length == 1);
  3514. this.actions.get('clearWaypoints').setEnabled(ss.connections);
  3515. this.actions.get('curved').setEnabled(ss.edges.length > 0);
  3516. this.actions.get('turn').setEnabled(ss.cells.length > 0);
  3517. this.actions.get('group').setEnabled(!ss.row && !ss.cell &&
  3518. (ss.cells.length > 1 || (ss.vertices.length == 1 &&
  3519. graph.model.getChildCount(ss.cells[0]) == 0 &&
  3520. !graph.isContainer(ss.vertices[0]))));
  3521. this.actions.get('ungroup').setEnabled(!ss.row && !ss.cell && !ss.table &&
  3522. ss.vertices.length > 0 && (graph.isContainer(ss.vertices[0]) ||
  3523. graph.getModel().getChildCount(ss.vertices[0]) > 0));
  3524. this.actions.get('removeFromGroup').setEnabled(ss.cells.length == 1 &&
  3525. graph.getModel().isVertex(graph.getModel().getParent(ss.cells[0])));
  3526. this.actions.get('collapsible').setEnabled(ss.vertices.length == 1 &&
  3527. (graph.model.getChildCount(ss.vertices[0]) > 0 ||
  3528. graph.isContainer(ss.vertices[0])));
  3529. this.actions.get('exitGroup').setEnabled(graph.view.currentRoot != null);
  3530. this.actions.get('home').setEnabled(graph.view.currentRoot != null);
  3531. this.actions.get('enterGroup').setEnabled(ss.cells.length == 1 &&
  3532. graph.isValidRoot(ss.cells[0]));
  3533. this.actions.get('editLink').setEnabled(ss.cells.length == 1);
  3534. this.actions.get('openLink').setEnabled(ss.cells.length == 1 &&
  3535. graph.getLinkForCell(ss.cells[0]) != null);
  3536. this.actions.get('guides').setEnabled(graph.isEnabled());
  3537. this.actions.get('selectVertices').setEnabled(unlocked);
  3538. this.actions.get('selectEdges').setEnabled(unlocked);
  3539. this.actions.get('selectAll').setEnabled(unlocked);
  3540. this.actions.get('selectNone').setEnabled(unlocked);
  3541. var foldable = ss.vertices.length == 1 &&
  3542. graph.isCellFoldable(ss.vertices[0]);
  3543. this.actions.get('expand').setEnabled(foldable);
  3544. this.actions.get('collapse').setEnabled(foldable);
  3545. // Updates menu states
  3546. this.menus.get('navigation').setEnabled(ss.cells.length > 0 ||
  3547. graph.view.currentRoot != null);
  3548. this.menus.get('layout').setEnabled(unlocked);
  3549. this.menus.get('insert').setEnabled(unlocked);
  3550. this.menus.get('direction').setEnabled(ss.unlocked &&
  3551. ss.vertices.length == 1);
  3552. this.menus.get('distribute').setEnabled(ss.unlocked &&
  3553. ss.vertices.length > 1);
  3554. this.menus.get('align').setEnabled(ss.unlocked &&
  3555. ss.cells.length > 0);
  3556. this.updatePasteActionStates();
  3557. };
  3558. EditorUi.prototype.zeroOffset = new mxPoint(0, 0);
  3559. EditorUi.prototype.getDiagramContainerOffset = function()
  3560. {
  3561. return this.zeroOffset;
  3562. };
  3563. /**
  3564. * Refreshes the viewport.
  3565. */
  3566. EditorUi.prototype.refresh = function(sizeDidChange)
  3567. {
  3568. sizeDidChange = (sizeDidChange != null) ? sizeDidChange : true;
  3569. var w = this.container.clientWidth;
  3570. var h = this.container.clientHeight;
  3571. if (this.container == document.body)
  3572. {
  3573. w = document.body.clientWidth || document.documentElement.clientWidth;
  3574. h = document.documentElement.clientHeight;
  3575. }
  3576. // Workaround for bug on iOS see
  3577. // http://stackoverflow.com/questions/19012135/ios-7-ipad-safari-landscape-innerheight-outerheight-layout-issue
  3578. // FIXME: Fix if footer visible
  3579. var off = 0;
  3580. if (mxClient.IS_IOS && !window.navigator.standalone && typeof Menus !== 'undefined')
  3581. {
  3582. if (window.innerHeight != document.documentElement.clientHeight)
  3583. {
  3584. off = document.documentElement.clientHeight - window.innerHeight;
  3585. window.scrollTo(0, 0);
  3586. }
  3587. }
  3588. var effHsplitPosition = Math.max(0, Math.min(this.hsplitPosition, w - this.splitSize - 20));
  3589. var tmp = 0;
  3590. if (this.menubar != null)
  3591. {
  3592. this.menubarContainer.style.height = this.menubarHeight + 'px';
  3593. tmp += this.menubarHeight;
  3594. }
  3595. if (this.toolbar != null)
  3596. {
  3597. this.toolbarContainer.style.top = this.menubarHeight + 'px';
  3598. this.toolbarContainer.style.height = this.toolbarHeight + 'px';
  3599. tmp += this.toolbarHeight;
  3600. }
  3601. if (tmp > 0)
  3602. {
  3603. tmp += 1;
  3604. }
  3605. var sidebarFooterHeight = 0;
  3606. if (this.sidebarFooterContainer != null)
  3607. {
  3608. var bottom = this.footerHeight + off;
  3609. sidebarFooterHeight = Math.max(0, Math.min(h - tmp - bottom, this.sidebarFooterHeight));
  3610. this.sidebarFooterContainer.style.width = effHsplitPosition + 'px';
  3611. this.sidebarFooterContainer.style.height = sidebarFooterHeight + 'px';
  3612. this.sidebarFooterContainer.style.bottom = bottom + 'px';
  3613. }
  3614. var fw = (this.format != null) ? this.formatWidth : 0;
  3615. this.sidebarContainer.style.top = tmp + 'px';
  3616. this.sidebarContainer.style.width = effHsplitPosition + 'px';
  3617. this.formatContainer.style.top = tmp + 'px';
  3618. this.formatContainer.style.width = fw + 'px';
  3619. this.formatContainer.style.display = (this.format != null) ? '' : 'none';
  3620. var diagContOffset = this.getDiagramContainerOffset();
  3621. var contLeft = (this.hsplit.parentNode != null) ? (effHsplitPosition + this.splitSize) : 0;
  3622. this.footerContainer.style.height = this.footerHeight + 'px';
  3623. this.hsplit.style.top = this.sidebarContainer.style.top;
  3624. this.hsplit.style.bottom = (this.footerHeight + off) + 'px';
  3625. this.hsplit.style.left = effHsplitPosition + 'px';
  3626. this.footerContainer.style.display = (this.footerHeight == 0) ? 'none' : '';
  3627. if (this.tabContainer != null)
  3628. {
  3629. this.tabContainer.style.left = contLeft + 'px';
  3630. }
  3631. if (this.footerHeight > 0)
  3632. {
  3633. this.footerContainer.style.bottom = off + 'px';
  3634. }
  3635. var th = 0;
  3636. if (this.tabContainer != null)
  3637. {
  3638. this.tabContainer.style.bottom = (this.footerHeight + off) + 'px';
  3639. this.tabContainer.style.right = this.diagramContainer.style.right;
  3640. th = this.tabContainer.clientHeight;
  3641. }
  3642. this.sidebarContainer.style.bottom = (this.footerHeight + sidebarFooterHeight + off) + 'px';
  3643. this.formatContainer.style.bottom = (this.footerHeight + off) + 'px';
  3644. if (urlParams['embedInline'] != '1')
  3645. {
  3646. this.diagramContainer.style.left = (contLeft + diagContOffset.x) + 'px';
  3647. this.diagramContainer.style.top = (tmp + diagContOffset.y) + 'px';
  3648. this.diagramContainer.style.right = fw + 'px';
  3649. this.diagramContainer.style.bottom = (this.footerHeight + off + th) + 'px';
  3650. }
  3651. if (sizeDidChange)
  3652. {
  3653. this.editor.graph.sizeDidChange();
  3654. }
  3655. };
  3656. /**
  3657. * Creates the required containers.
  3658. */
  3659. EditorUi.prototype.createTabContainer = function()
  3660. {
  3661. return null;
  3662. };
  3663. /**
  3664. * Creates the required containers.
  3665. */
  3666. EditorUi.prototype.createDivs = function()
  3667. {
  3668. this.menubarContainer = this.createDiv('geMenubarContainer');
  3669. this.toolbarContainer = this.createDiv('geToolbarContainer');
  3670. this.sidebarContainer = this.createDiv('geSidebarContainer');
  3671. this.formatContainer = this.createDiv('geSidebarContainer geFormatContainer');
  3672. this.diagramContainer = this.createDiv('geDiagramContainer');
  3673. this.footerContainer = this.createDiv('geFooterContainer');
  3674. this.hsplit = this.createDiv('geHsplit');
  3675. this.hsplit.setAttribute('title', mxResources.get('collapseExpand'));
  3676. // Sets static style for containers
  3677. this.menubarContainer.style.top = '0px';
  3678. this.menubarContainer.style.left = '0px';
  3679. this.menubarContainer.style.right = '0px';
  3680. this.toolbarContainer.style.left = '0px';
  3681. this.toolbarContainer.style.right = '0px';
  3682. this.sidebarContainer.style.left = '0px';
  3683. this.formatContainer.style.right = '0px';
  3684. this.formatContainer.style.zIndex = '1';
  3685. this.diagramContainer.style.right = ((this.format != null) ? this.formatWidth : 0) + 'px';
  3686. this.footerContainer.style.left = '0px';
  3687. this.footerContainer.style.right = '0px';
  3688. this.footerContainer.style.bottom = '0px';
  3689. this.footerContainer.style.zIndex = mxPopupMenu.prototype.zIndex - 3;
  3690. this.hsplit.style.width = this.splitSize + 'px';
  3691. this.sidebarFooterContainer = this.createSidebarFooterContainer();
  3692. if (this.sidebarFooterContainer)
  3693. {
  3694. this.sidebarFooterContainer.style.left = '0px';
  3695. }
  3696. if (!this.editor.chromeless)
  3697. {
  3698. this.tabContainer = this.createTabContainer();
  3699. }
  3700. else
  3701. {
  3702. this.diagramContainer.style.border = 'none';
  3703. }
  3704. };
  3705. /**
  3706. * Hook for sidebar footer container. This implementation returns null.
  3707. */
  3708. EditorUi.prototype.createSidebarFooterContainer = function()
  3709. {
  3710. return null;
  3711. };
  3712. /**
  3713. * Creates the required containers.
  3714. */
  3715. EditorUi.prototype.createUi = function()
  3716. {
  3717. // Creates menubar
  3718. this.menubar = (this.editor.chromeless) ? null : this.menus.createMenubar(this.createDiv('geMenubar'));
  3719. if (this.menubar != null)
  3720. {
  3721. this.menubarContainer.appendChild(this.menubar.container);
  3722. }
  3723. // Adds status bar in menubar
  3724. if (this.menubar != null)
  3725. {
  3726. this.statusContainer = this.createStatusContainer();
  3727. // Connects the status bar to the editor status
  3728. this.editor.addListener('statusChanged', mxUtils.bind(this, function()
  3729. {
  3730. this.setStatusText(this.editor.getStatus());
  3731. }));
  3732. this.setStatusText(this.editor.getStatus());
  3733. this.menubar.container.appendChild(this.statusContainer);
  3734. // Inserts into DOM
  3735. this.container.appendChild(this.menubarContainer);
  3736. }
  3737. // Creates the sidebar
  3738. this.sidebar = (this.editor.chromeless) ? null : this.createSidebar(this.sidebarContainer);
  3739. if (this.sidebar != null)
  3740. {
  3741. this.container.appendChild(this.sidebarContainer);
  3742. }
  3743. // Creates the format sidebar
  3744. this.format = (this.editor.chromeless || !this.formatEnabled) ? null : this.createFormat(this.formatContainer);
  3745. if (this.format != null)
  3746. {
  3747. this.container.appendChild(this.formatContainer);
  3748. }
  3749. // Creates the footer
  3750. var footer = (this.editor.chromeless) ? null : this.createFooter();
  3751. if (footer != null)
  3752. {
  3753. this.footerContainer.appendChild(footer);
  3754. this.container.appendChild(this.footerContainer);
  3755. }
  3756. if (this.sidebar != null && this.sidebarFooterContainer)
  3757. {
  3758. this.container.appendChild(this.sidebarFooterContainer);
  3759. }
  3760. this.container.appendChild(this.diagramContainer);
  3761. if (this.container != null && this.tabContainer != null)
  3762. {
  3763. this.container.appendChild(this.tabContainer);
  3764. }
  3765. // Creates toolbar
  3766. this.toolbar = (this.editor.chromeless) ? null : this.createToolbar(this.createDiv('geToolbar'));
  3767. if (this.toolbar != null)
  3768. {
  3769. this.toolbarContainer.appendChild(this.toolbar.container);
  3770. this.container.appendChild(this.toolbarContainer);
  3771. }
  3772. // HSplit
  3773. if (this.sidebar != null)
  3774. {
  3775. this.container.appendChild(this.hsplit);
  3776. this.addSplitHandler(this.hsplit, true, 0, mxUtils.bind(this, function(value)
  3777. {
  3778. this.hsplitPosition = value;
  3779. this.refresh();
  3780. }));
  3781. }
  3782. };
  3783. /**
  3784. * Creates a new toolbar for the given container.
  3785. */
  3786. EditorUi.prototype.createStatusContainer = function()
  3787. {
  3788. var container = document.createElement('a');
  3789. container.className = 'geItem geStatus';
  3790. return container;
  3791. };
  3792. /**
  3793. * Creates a new toolbar for the given container.
  3794. */
  3795. EditorUi.prototype.setStatusText = function(value)
  3796. {
  3797. this.statusContainer.innerHTML = value;
  3798. // Wraps simple status messages in a div for styling
  3799. if (this.statusContainer.getElementsByTagName('div').length == 0)
  3800. {
  3801. this.statusContainer.innerHTML = '';
  3802. var div = this.createStatusDiv(value);
  3803. this.statusContainer.appendChild(div);
  3804. }
  3805. };
  3806. /**
  3807. * Creates a new toolbar for the given container.
  3808. */
  3809. EditorUi.prototype.createStatusDiv = function(value)
  3810. {
  3811. var div = document.createElement('div');
  3812. div.setAttribute('title', value);
  3813. div.innerHTML = value;
  3814. return div;
  3815. };
  3816. /**
  3817. * Creates a new toolbar for the given container.
  3818. */
  3819. EditorUi.prototype.createToolbar = function(container)
  3820. {
  3821. return new Toolbar(this, container);
  3822. };
  3823. /**
  3824. * Creates a new sidebar for the given container.
  3825. */
  3826. EditorUi.prototype.createSidebar = function(container)
  3827. {
  3828. return new Sidebar(this, container);
  3829. };
  3830. /**
  3831. * Creates a new sidebar for the given container.
  3832. */
  3833. EditorUi.prototype.createFormat = function(container)
  3834. {
  3835. return new Format(this, container);
  3836. };
  3837. /**
  3838. * Creates and returns a new footer.
  3839. */
  3840. EditorUi.prototype.createFooter = function()
  3841. {
  3842. return this.createDiv('geFooter');
  3843. };
  3844. /**
  3845. * Creates the actual toolbar for the toolbar container.
  3846. */
  3847. EditorUi.prototype.createDiv = function(classname)
  3848. {
  3849. var elt = document.createElement('div');
  3850. elt.className = classname;
  3851. return elt;
  3852. };
  3853. /**
  3854. * Updates the states of the given undo/redo items.
  3855. */
  3856. EditorUi.prototype.addSplitHandler = function(elt, horizontal, dx, onChange)
  3857. {
  3858. var start = null;
  3859. var initial = null;
  3860. var ignoreClick = true;
  3861. var last = null;
  3862. // Disables built-in pan and zoom in IE10 and later
  3863. if (mxClient.IS_POINTER)
  3864. {
  3865. elt.style.touchAction = 'none';
  3866. }
  3867. var getValue = mxUtils.bind(this, function()
  3868. {
  3869. var result = parseInt(((horizontal) ? elt.style.left : elt.style.bottom));
  3870. // Takes into account hidden footer
  3871. if (!horizontal)
  3872. {
  3873. result = result + dx - this.footerHeight;
  3874. }
  3875. return result;
  3876. });
  3877. function moveHandler(evt)
  3878. {
  3879. if (start != null)
  3880. {
  3881. var pt = new mxPoint(mxEvent.getClientX(evt), mxEvent.getClientY(evt));
  3882. onChange(Math.max(0, initial + ((horizontal) ? (pt.x - start.x) : (start.y - pt.y)) - dx));
  3883. mxEvent.consume(evt);
  3884. if (initial != getValue())
  3885. {
  3886. ignoreClick = true;
  3887. last = null;
  3888. }
  3889. }
  3890. };
  3891. function dropHandler(evt)
  3892. {
  3893. moveHandler(evt);
  3894. initial = null;
  3895. start = null;
  3896. };
  3897. mxEvent.addGestureListeners(elt, function(evt)
  3898. {
  3899. start = new mxPoint(mxEvent.getClientX(evt), mxEvent.getClientY(evt));
  3900. initial = getValue();
  3901. ignoreClick = false;
  3902. mxEvent.consume(evt);
  3903. });
  3904. mxEvent.addListener(elt, 'click', mxUtils.bind(this, function(evt)
  3905. {
  3906. if (!ignoreClick && this.hsplitClickEnabled)
  3907. {
  3908. var next = (last != null) ? last - dx : 0;
  3909. last = getValue();
  3910. onChange(next);
  3911. mxEvent.consume(evt);
  3912. }
  3913. }));
  3914. mxEvent.addGestureListeners(document, null, moveHandler, dropHandler);
  3915. this.destroyFunctions.push(function()
  3916. {
  3917. mxEvent.removeGestureListeners(document, null, moveHandler, dropHandler);
  3918. });
  3919. };
  3920. /**
  3921. * Translates this point by the given vector.
  3922. *
  3923. * @param {number} dx X-coordinate of the translation.
  3924. * @param {number} dy Y-coordinate of the translation.
  3925. */
  3926. EditorUi.prototype.handleError = function(resp, title, fn, invokeFnOnClose, notFoundMessage)
  3927. {
  3928. var e = (resp != null && resp.error != null) ? resp.error : resp;
  3929. if (e != null || title != null)
  3930. {
  3931. var msg = mxUtils.htmlEntities(mxResources.get('unknownError'));
  3932. var btn = mxResources.get('ok');
  3933. title = (title != null) ? title : mxResources.get('error');
  3934. if (e != null && e.message != null)
  3935. {
  3936. msg = mxUtils.htmlEntities(e.message);
  3937. }
  3938. this.showError(title, msg, btn, fn, null, null, null, null, null,
  3939. null, null, null, (invokeFnOnClose) ? fn : null);
  3940. }
  3941. else if (fn != null)
  3942. {
  3943. fn();
  3944. }
  3945. };
  3946. /**
  3947. * Translates this point by the given vector.
  3948. *
  3949. * @param {number} dx X-coordinate of the translation.
  3950. * @param {number} dy Y-coordinate of the translation.
  3951. */
  3952. EditorUi.prototype.showError = function(title, msg, btn, fn, retry, btn2, fn2, btn3, fn3, w, h, hide, onClose)
  3953. {
  3954. var dlg = new ErrorDialog(this, title, msg, btn || mxResources.get('ok'),
  3955. fn, retry, btn2, fn2, hide, btn3, fn3);
  3956. var lines = Math.ceil((msg != null) ? msg.length / 50 : 1);
  3957. this.showDialog(dlg.container, w || 340, h || (100 + lines * 20), true, false, onClose);
  3958. dlg.init();
  3959. };
  3960. /**
  3961. * Displays a print dialog.
  3962. */
  3963. EditorUi.prototype.showDialog = function(elt, w, h, modal, closable, onClose, noScroll, transparent, onResize, ignoreBgClick)
  3964. {
  3965. this.editor.graph.tooltipHandler.resetTimer();
  3966. this.editor.graph.tooltipHandler.hideTooltip();
  3967. if (this.dialogs == null)
  3968. {
  3969. this.dialogs = [];
  3970. }
  3971. this.dialog = new Dialog(this, elt, w, h, modal, closable, onClose, noScroll, transparent, onResize, ignoreBgClick);
  3972. this.dialogs.push(this.dialog);
  3973. };
  3974. /**
  3975. * Displays a print dialog.
  3976. */
  3977. EditorUi.prototype.hideDialog = function(cancel, isEsc, matchContainer)
  3978. {
  3979. if (this.dialogs != null && this.dialogs.length > 0)
  3980. {
  3981. if (matchContainer != null && matchContainer != this.dialog.container.firstChild)
  3982. {
  3983. return;
  3984. }
  3985. var dlg = this.dialogs.pop();
  3986. if (dlg.close(cancel, isEsc) == false)
  3987. {
  3988. //add the dialog back if dialog closing is cancelled
  3989. this.dialogs.push(dlg);
  3990. return;
  3991. }
  3992. this.dialog = (this.dialogs.length > 0) ? this.dialogs[this.dialogs.length - 1] : null;
  3993. this.editor.fireEvent(new mxEventObject('hideDialog'));
  3994. if (this.dialog == null && this.editor.graph.container.style.visibility != 'hidden')
  3995. {
  3996. window.setTimeout(mxUtils.bind(this, function()
  3997. {
  3998. if (this.editor.graph.isEditing() && this.editor.graph.cellEditor.textarea != null)
  3999. {
  4000. this.editor.graph.cellEditor.textarea.focus();
  4001. }
  4002. else
  4003. {
  4004. mxUtils.clearSelection();
  4005. this.editor.graph.container.focus();
  4006. }
  4007. }), 0);
  4008. }
  4009. }
  4010. };
  4011. /**
  4012. * Handles ctrl+enter keystroke to clone cells.
  4013. */
  4014. EditorUi.prototype.ctrlEnter = function()
  4015. {
  4016. var graph = this.editor.graph;
  4017. if (graph.isEnabled())
  4018. {
  4019. try
  4020. {
  4021. var cells = graph.getSelectionCells();
  4022. var lookup = new mxDictionary();
  4023. var newCells = [];
  4024. for (var i = 0; i < cells.length; i++)
  4025. {
  4026. // Clones table rows instead of cells
  4027. var cell = (graph.isTableCell(cells[i])) ? graph.model.getParent(cells[i]) : cells[i];
  4028. if (cell != null && !lookup.get(cell))
  4029. {
  4030. lookup.put(cell, true);
  4031. newCells.push(cell);
  4032. }
  4033. }
  4034. graph.setSelectionCells(graph.duplicateCells(newCells, false));
  4035. }
  4036. catch (e)
  4037. {
  4038. this.handleError(e);
  4039. }
  4040. }
  4041. };
  4042. /**
  4043. * Display a color dialog.
  4044. */
  4045. EditorUi.prototype.pickColor = function(color, apply)
  4046. {
  4047. var graph = this.editor.graph;
  4048. var selState = graph.cellEditor.saveSelection();
  4049. var h = 230 + ((Math.ceil(ColorDialog.prototype.presetColors.length / 12) +
  4050. Math.ceil(ColorDialog.prototype.defaultColors.length / 12)) * 17);
  4051. var dlg = new ColorDialog(this, mxUtils.rgba2hex(color) || 'none', function(color)
  4052. {
  4053. graph.cellEditor.restoreSelection(selState);
  4054. apply(color);
  4055. }, function()
  4056. {
  4057. graph.cellEditor.restoreSelection(selState);
  4058. });
  4059. this.showDialog(dlg.container, 230, h, true, false);
  4060. dlg.init();
  4061. };
  4062. /**
  4063. * Adds the label menu items to the given menu and parent.
  4064. */
  4065. EditorUi.prototype.openFile = function()
  4066. {
  4067. // Closes dialog after open
  4068. window.openFile = new OpenFile(mxUtils.bind(this, function(cancel)
  4069. {
  4070. this.hideDialog(cancel);
  4071. }));
  4072. // Removes openFile if dialog is closed
  4073. this.showDialog(new OpenDialog(this).container, (Editor.useLocalStorage) ? 640 : 320,
  4074. (Editor.useLocalStorage) ? 480 : 220, true, true, function()
  4075. {
  4076. window.openFile = null;
  4077. });
  4078. };
  4079. /**
  4080. * Extracs the graph model from the given HTML data from a data transfer event.
  4081. */
  4082. EditorUi.prototype.extractGraphModelFromHtml = function(data)
  4083. {
  4084. var result = null;
  4085. try
  4086. {
  4087. var idx = data.indexOf('&lt;mxGraphModel ');
  4088. if (idx >= 0)
  4089. {
  4090. var idx2 = data.lastIndexOf('&lt;/mxGraphModel&gt;');
  4091. if (idx2 > idx)
  4092. {
  4093. result = data.substring(idx, idx2 + 21).replace(/&gt;/g, '>').
  4094. replace(/&lt;/g, '<').replace(/\\&quot;/g, '"').replace(/\n/g, '');
  4095. }
  4096. }
  4097. }
  4098. catch (e)
  4099. {
  4100. // ignore
  4101. }
  4102. return result;
  4103. };
  4104. /**
  4105. * Opens the given files in the editor.
  4106. */
  4107. EditorUi.prototype.readGraphModelFromClipboard = function(fn)
  4108. {
  4109. this.readGraphModelFromClipboardWithType(mxUtils.bind(this, function(xml)
  4110. {
  4111. if (xml != null)
  4112. {
  4113. fn(xml);
  4114. }
  4115. else
  4116. {
  4117. this.readGraphModelFromClipboardWithType(mxUtils.bind(this, function(xml)
  4118. {
  4119. if (xml != null)
  4120. {
  4121. var tmp = decodeURIComponent(xml);
  4122. if (this.isCompatibleString(tmp))
  4123. {
  4124. xml = tmp;
  4125. }
  4126. }
  4127. fn(xml);
  4128. }), 'text');
  4129. }
  4130. }), 'html');
  4131. };
  4132. /**
  4133. * Opens the given files in the editor.
  4134. */
  4135. EditorUi.prototype.readGraphModelFromClipboardWithType = function(fn, type)
  4136. {
  4137. navigator.clipboard.read().then(mxUtils.bind(this, function(data)
  4138. {
  4139. if (data != null && data.length > 0 && type == 'html' &&
  4140. mxUtils.indexOf(data[0].types, 'text/html') >= 0)
  4141. {
  4142. data[0].getType('text/html').then(mxUtils.bind(this, function(blob)
  4143. {
  4144. blob.text().then(mxUtils.bind(this, function(value)
  4145. {
  4146. try
  4147. {
  4148. var elt = this.parseHtmlData(value);
  4149. var asHtml = elt.getAttribute('data-type') != 'text/plain';
  4150. // KNOWN: Paste from IE11 to other browsers on Windows
  4151. // seems to paste the contents of index.html
  4152. var xml = (asHtml) ? elt.innerHTML :
  4153. mxUtils.trim((elt.innerText == null) ?
  4154. mxUtils.getTextContent(elt) : elt.innerText);
  4155. // Workaround for junk after XML in VM
  4156. try
  4157. {
  4158. var idx = xml.lastIndexOf('%3E');
  4159. if (idx >= 0 && idx < xml.length - 3)
  4160. {
  4161. xml = xml.substring(0, idx + 3);
  4162. }
  4163. }
  4164. catch (e)
  4165. {
  4166. // ignore
  4167. }
  4168. // Checks for embedded XML content
  4169. try
  4170. {
  4171. var spans = elt.getElementsByTagName('span');
  4172. var tmp = (spans != null && spans.length > 0) ?
  4173. mxUtils.trim(decodeURIComponent(spans[0].textContent)) :
  4174. decodeURIComponent(xml);
  4175. if (this.isCompatibleString(tmp))
  4176. {
  4177. xml = tmp;
  4178. }
  4179. }
  4180. catch (e)
  4181. {
  4182. // ignore
  4183. }
  4184. }
  4185. catch (e)
  4186. {
  4187. // ignore
  4188. }
  4189. fn(this.isCompatibleString(xml) ? xml : null);
  4190. }))['catch'](function(data)
  4191. {
  4192. fn(null);
  4193. });
  4194. }))['catch'](function(data)
  4195. {
  4196. fn(null);
  4197. });
  4198. }
  4199. else if (data != null && data.length > 0 && type == 'text' &&
  4200. mxUtils.indexOf(data[0].types, 'text/plain') >= 0)
  4201. {
  4202. data[0].getType('text/plain').then(function(blob)
  4203. {
  4204. blob.text().then(function(value)
  4205. {
  4206. fn(value);
  4207. })['catch'](function()
  4208. {
  4209. fn(null);
  4210. });
  4211. })['catch'](function()
  4212. {
  4213. fn(null);
  4214. });
  4215. }
  4216. else
  4217. {
  4218. fn(null);
  4219. }
  4220. }))['catch'](function(data)
  4221. {
  4222. fn(null);
  4223. });
  4224. };
  4225. /**
  4226. * Parses the given HTML data and returns a DIV.
  4227. */
  4228. EditorUi.prototype.parseHtmlData = function(data)
  4229. {
  4230. var elt = null;
  4231. if (data != null && data.length > 0)
  4232. {
  4233. var hasMeta = data.substring(0, 6) == '<meta ';
  4234. elt = document.createElement('div');
  4235. elt.innerHTML = ((hasMeta) ? '<meta charset="utf-8">' : '') +
  4236. this.editor.graph.sanitizeHtml(data);
  4237. asHtml = true;
  4238. // Workaround for innerText not ignoring style elements in Chrome
  4239. var styles = elt.getElementsByTagName('style');
  4240. if (styles != null)
  4241. {
  4242. while (styles.length > 0)
  4243. {
  4244. styles[0].parentNode.removeChild(styles[0]);
  4245. }
  4246. }
  4247. // Special case of link pasting from Chrome
  4248. if (elt.firstChild != null && elt.firstChild.nodeType == mxConstants.NODETYPE_ELEMENT &&
  4249. elt.firstChild.nextSibling != null && elt.firstChild.nextSibling.nodeType == mxConstants.NODETYPE_ELEMENT &&
  4250. elt.firstChild.nodeName == 'META' && elt.firstChild.nextSibling.nodeName == 'A' &&
  4251. elt.firstChild.nextSibling.nextSibling == null)
  4252. {
  4253. var temp = (elt.firstChild.nextSibling.innerText == null) ?
  4254. mxUtils.getTextContent(elt.firstChild.nextSibling) :
  4255. elt.firstChild.nextSibling.innerText;
  4256. if (temp == elt.firstChild.nextSibling.getAttribute('href'))
  4257. {
  4258. mxUtils.setTextContent(elt, temp);
  4259. asHtml = false;
  4260. }
  4261. }
  4262. // Extracts single image source address with meta tag in markup
  4263. var img = (hasMeta && elt.firstChild != null) ? elt.firstChild.nextSibling : elt.firstChild;
  4264. if (img != null && img.nextSibling == null &&
  4265. img.nodeType == mxConstants.NODETYPE_ELEMENT &&
  4266. img.nodeName == 'IMG')
  4267. {
  4268. var temp = img.getAttribute('src');
  4269. if (temp != null)
  4270. {
  4271. if (Editor.isPngDataUrl(temp))
  4272. {
  4273. var xml = Editor.extractGraphModelFromPng(temp);
  4274. if (xml != null && xml.length > 0)
  4275. {
  4276. temp = xml;
  4277. }
  4278. }
  4279. mxUtils.setTextContent(elt, temp);
  4280. asHtml = false;
  4281. }
  4282. }
  4283. else
  4284. {
  4285. // Extracts embedded XML or image source address from single PNG image
  4286. var images = elt.getElementsByTagName('img');
  4287. if (images.length == 1)
  4288. {
  4289. var img = images[0];
  4290. var temp = img.getAttribute('src');
  4291. if (temp != null && img.parentNode == elt && elt.children.length == 1)
  4292. {
  4293. if (Editor.isPngDataUrl(temp))
  4294. {
  4295. var xml = Editor.extractGraphModelFromPng(temp);
  4296. if (xml != null && xml.length > 0)
  4297. {
  4298. temp = xml;
  4299. }
  4300. }
  4301. mxUtils.setTextContent(elt, temp);
  4302. asHtml = false;
  4303. }
  4304. }
  4305. }
  4306. if (asHtml)
  4307. {
  4308. Graph.removePasteFormatting(elt);
  4309. }
  4310. }
  4311. if (!asHtml)
  4312. {
  4313. elt.setAttribute('data-type', 'text/plain');
  4314. }
  4315. return elt;
  4316. };
  4317. /**
  4318. * Opens the given files in the editor.
  4319. */
  4320. EditorUi.prototype.extractGraphModelFromEvent = function(evt)
  4321. {
  4322. var result = null;
  4323. var data = null;
  4324. if (evt != null)
  4325. {
  4326. var provider = (evt.dataTransfer != null) ? evt.dataTransfer : evt.clipboardData;
  4327. if (provider != null)
  4328. {
  4329. if (document.documentMode == 10 || document.documentMode == 11)
  4330. {
  4331. data = provider.getData('Text');
  4332. }
  4333. else
  4334. {
  4335. data = (mxUtils.indexOf(provider.types, 'text/html') >= 0) ? provider.getData('text/html') : null;
  4336. if (mxUtils.indexOf(provider.types, 'text/plain' && (data == null || data.length == 0)))
  4337. {
  4338. data = provider.getData('text/plain');
  4339. }
  4340. }
  4341. if (data != null)
  4342. {
  4343. data = Graph.zapGremlins(mxUtils.trim(data));
  4344. // Tries parsing as HTML document with embedded XML
  4345. var xml = this.extractGraphModelFromHtml(data);
  4346. if (xml != null)
  4347. {
  4348. data = xml;
  4349. }
  4350. }
  4351. }
  4352. }
  4353. if (data != null && this.isCompatibleString(data))
  4354. {
  4355. result = data;
  4356. }
  4357. return result;
  4358. };
  4359. /**
  4360. * Hook for subclassers to return true if event data is a supported format.
  4361. * This implementation always returns false.
  4362. */
  4363. EditorUi.prototype.isCompatibleString = function(data)
  4364. {
  4365. return false;
  4366. };
  4367. /**
  4368. * Adds the label menu items to the given menu and parent.
  4369. */
  4370. EditorUi.prototype.saveFile = function(forceDialog)
  4371. {
  4372. if (!forceDialog && this.editor.filename != null)
  4373. {
  4374. this.save(this.editor.getOrCreateFilename());
  4375. }
  4376. else
  4377. {
  4378. var dlg = new FilenameDialog(this, this.editor.getOrCreateFilename(), mxResources.get('save'), mxUtils.bind(this, function(name)
  4379. {
  4380. this.save(name);
  4381. }), null, mxUtils.bind(this, function(name)
  4382. {
  4383. if (name != null && name.length > 0)
  4384. {
  4385. return true;
  4386. }
  4387. mxUtils.confirm(mxResources.get('invalidName'));
  4388. return false;
  4389. }));
  4390. this.showDialog(dlg.container, 300, 100, true, true);
  4391. dlg.init();
  4392. }
  4393. };
  4394. /**
  4395. * Saves the current graph under the given filename.
  4396. */
  4397. EditorUi.prototype.save = function(name)
  4398. {
  4399. if (name != null)
  4400. {
  4401. if (this.editor.graph.isEditing())
  4402. {
  4403. this.editor.graph.stopEditing();
  4404. }
  4405. var xml = mxUtils.getXml(this.editor.getGraphXml());
  4406. try
  4407. {
  4408. if (Editor.useLocalStorage)
  4409. {
  4410. if (localStorage.getItem(name) != null &&
  4411. !mxUtils.confirm(mxResources.get('replaceIt', [name])))
  4412. {
  4413. return;
  4414. }
  4415. localStorage.setItem(name, xml);
  4416. this.editor.setStatus(mxUtils.htmlEntities(mxResources.get('saved')) + ' ' + new Date());
  4417. }
  4418. else
  4419. {
  4420. if (xml.length < MAX_REQUEST_SIZE)
  4421. {
  4422. new mxXmlRequest(SAVE_URL, 'filename=' + encodeURIComponent(name) +
  4423. '&xml=' + encodeURIComponent(xml)).simulate(document, '_blank');
  4424. }
  4425. else
  4426. {
  4427. mxUtils.alert(mxResources.get('drawingTooLarge'));
  4428. mxUtils.popup(xml);
  4429. return;
  4430. }
  4431. }
  4432. this.editor.setModified(false);
  4433. this.editor.setFilename(name);
  4434. this.updateDocumentTitle();
  4435. }
  4436. catch (e)
  4437. {
  4438. this.editor.setStatus(mxUtils.htmlEntities(mxResources.get('errorSavingFile')));
  4439. }
  4440. }
  4441. };
  4442. /**
  4443. * Executes the given layout.
  4444. */
  4445. EditorUi.prototype.executeLayout = function(exec, animate, post)
  4446. {
  4447. var graph = this.editor.graph;
  4448. if (graph.isEnabled())
  4449. {
  4450. graph.getModel().beginUpdate();
  4451. try
  4452. {
  4453. exec();
  4454. }
  4455. catch (e)
  4456. {
  4457. throw e;
  4458. }
  4459. finally
  4460. {
  4461. // Animates the changes in the graph model except
  4462. // for Camino, where animation is too slow
  4463. if (this.allowAnimation && animate && (navigator.userAgent == null ||
  4464. navigator.userAgent.indexOf('Camino') < 0))
  4465. {
  4466. // New API for animating graph layout results asynchronously
  4467. var morph = new mxMorphing(graph);
  4468. morph.addListener(mxEvent.DONE, mxUtils.bind(this, function()
  4469. {
  4470. graph.getModel().endUpdate();
  4471. if (post != null)
  4472. {
  4473. post();
  4474. }
  4475. }));
  4476. morph.startAnimation();
  4477. }
  4478. else
  4479. {
  4480. graph.getModel().endUpdate();
  4481. if (post != null)
  4482. {
  4483. post();
  4484. }
  4485. }
  4486. }
  4487. }
  4488. };
  4489. /**
  4490. * Hides the current menu.
  4491. */
  4492. EditorUi.prototype.showImageDialog = function(title, value, fn, ignoreExisting)
  4493. {
  4494. var cellEditor = this.editor.graph.cellEditor;
  4495. var selState = cellEditor.saveSelection();
  4496. var newValue = mxUtils.prompt(title, value);
  4497. cellEditor.restoreSelection(selState);
  4498. if (newValue != null && newValue.length > 0)
  4499. {
  4500. var img = new Image();
  4501. img.onload = function()
  4502. {
  4503. fn(newValue, img.width, img.height);
  4504. };
  4505. img.onerror = function()
  4506. {
  4507. fn(null);
  4508. mxUtils.alert(mxResources.get('fileNotFound'));
  4509. };
  4510. img.src = newValue;
  4511. }
  4512. else
  4513. {
  4514. fn(null);
  4515. }
  4516. };
  4517. /**
  4518. * Hides the current menu.
  4519. */
  4520. EditorUi.prototype.showLinkDialog = function(value, btnLabel, fn)
  4521. {
  4522. var dlg = new LinkDialog(this, value, btnLabel, fn);
  4523. this.showDialog(dlg.container, 420, 90, true, true);
  4524. dlg.init();
  4525. };
  4526. /**
  4527. * Hides the current menu.
  4528. */
  4529. EditorUi.prototype.showDataDialog = function(cell)
  4530. {
  4531. if (cell != null)
  4532. {
  4533. var dlg = new EditDataDialog(this, cell);
  4534. this.showDialog(dlg.container, 480, 420, true, false, null, false);
  4535. dlg.init();
  4536. }
  4537. };
  4538. /**
  4539. * Hides the current menu.
  4540. */
  4541. EditorUi.prototype.showBackgroundImageDialog = function(apply, img)
  4542. {
  4543. apply = (apply != null) ? apply : mxUtils.bind(this, function(image)
  4544. {
  4545. var change = new ChangePageSetup(this, null, image);
  4546. change.ignoreColor = true;
  4547. this.editor.graph.model.execute(change);
  4548. });
  4549. var newValue = mxUtils.prompt(mxResources.get('backgroundImage'), (img != null) ? img.src : '');
  4550. if (newValue != null && newValue.length > 0)
  4551. {
  4552. var img = new Image();
  4553. img.onload = function()
  4554. {
  4555. apply(new mxImage(newValue, img.width, img.height), false);
  4556. };
  4557. img.onerror = function()
  4558. {
  4559. apply(null, true);
  4560. mxUtils.alert(mxResources.get('fileNotFound'));
  4561. };
  4562. img.src = newValue;
  4563. }
  4564. else
  4565. {
  4566. apply(null);
  4567. }
  4568. };
  4569. /**
  4570. * Loads the stylesheet for this graph.
  4571. */
  4572. EditorUi.prototype.setBackgroundImage = function(image)
  4573. {
  4574. this.editor.graph.setBackgroundImage(image);
  4575. this.editor.graph.view.validateBackgroundImage();
  4576. this.fireEvent(new mxEventObject('backgroundImageChanged'));
  4577. };
  4578. /**
  4579. * Creates the keyboard event handler for the current graph and history.
  4580. */
  4581. EditorUi.prototype.confirm = function(msg, okFn, cancelFn)
  4582. {
  4583. if (mxUtils.confirm(msg))
  4584. {
  4585. if (okFn != null)
  4586. {
  4587. okFn();
  4588. }
  4589. }
  4590. else if (cancelFn != null)
  4591. {
  4592. cancelFn();
  4593. }
  4594. };
  4595. /**
  4596. * Creates the keyboard event handler for the current graph and history.
  4597. */
  4598. EditorUi.prototype.createOutline = function(wnd)
  4599. {
  4600. var outline = new mxOutline(this.editor.graph);
  4601. mxEvent.addListener(window, 'resize', function()
  4602. {
  4603. outline.update(false);
  4604. });
  4605. return outline;
  4606. };
  4607. // Alt+Shift+Keycode mapping to action
  4608. EditorUi.prototype.altShiftActions = {67: 'clearWaypoints', // Alt+Shift+C
  4609. 65: 'connectionArrows', // Alt+Shift+A
  4610. 76: 'editLink', // Alt+Shift+L
  4611. 80: 'connectionPoints', // Alt+Shift+P
  4612. 84: 'editTooltip', // Alt+Shift+T
  4613. 86: 'pasteSize', // Alt+Shift+V
  4614. 88: 'copySize', // Alt+Shift+X
  4615. 66: 'copyData', // Alt+Shift+B
  4616. 69: 'pasteData' // Alt+Shift+E
  4617. };
  4618. /**
  4619. * Creates the keyboard event handler for the current graph and history.
  4620. */
  4621. EditorUi.prototype.createKeyHandler = function(editor)
  4622. {
  4623. var editorUi = this;
  4624. var graph = this.editor.graph;
  4625. var keyHandler = new mxKeyHandler(graph);
  4626. var isEventIgnored = keyHandler.isEventIgnored;
  4627. keyHandler.isEventIgnored = function(evt)
  4628. {
  4629. // Handles undo/redo/ctrl+./,/u via action and allows ctrl+b/i
  4630. // only if editing value is HTML (except for FF and Safari)
  4631. return !(mxEvent.isShiftDown(evt) && evt.keyCode == 9) &&
  4632. ((!this.isControlDown(evt) || mxEvent.isShiftDown(evt) ||
  4633. (evt.keyCode != 90 && evt.keyCode != 89 && evt.keyCode != 188 &&
  4634. evt.keyCode != 190 && evt.keyCode != 85)) && ((evt.keyCode != 66 && evt.keyCode != 73) ||
  4635. !this.isControlDown(evt) || (this.graph.cellEditor.isContentEditing() &&
  4636. !mxClient.IS_FF && !mxClient.IS_SF)) && isEventIgnored.apply(this, arguments));
  4637. };
  4638. // Ignores graph enabled state but not chromeless state
  4639. keyHandler.isEnabledForEvent = function(evt)
  4640. {
  4641. return (!mxEvent.isConsumed(evt) && this.isGraphEvent(evt) && this.isEnabled() &&
  4642. (editorUi.dialogs == null || editorUi.dialogs.length == 0));
  4643. };
  4644. // Routes command-key to control-key on Mac
  4645. keyHandler.isControlDown = function(evt)
  4646. {
  4647. return mxEvent.isControlDown(evt) || (mxClient.IS_MAC && evt.metaKey);
  4648. };
  4649. var thread = null;
  4650. // Helper function to move cells with the cursor keys
  4651. function nudge(keyCode, stepSize, resize)
  4652. {
  4653. if (!graph.isSelectionEmpty() && graph.isEnabled())
  4654. {
  4655. stepSize = (stepSize != null) ? stepSize : 1;
  4656. var cells = graph.getCompositeParents(graph.getSelectionCells());
  4657. var cell = (cells.length > 0) ? cells[0] : null;
  4658. if (cell != null)
  4659. {
  4660. if (resize)
  4661. {
  4662. // Resizes all selected vertices
  4663. graph.getModel().beginUpdate();
  4664. try
  4665. {
  4666. for (var i = 0; i < cells.length; i++)
  4667. {
  4668. if (graph.getModel().isVertex(cells[i]) && graph.isCellResizable(cells[i]))
  4669. {
  4670. var geo = graph.getCellGeometry(cells[i]);
  4671. if (geo != null)
  4672. {
  4673. geo = geo.clone();
  4674. if (keyCode == 37)
  4675. {
  4676. geo.width = Math.max(0, geo.width - stepSize);
  4677. }
  4678. else if (keyCode == 38)
  4679. {
  4680. geo.height = Math.max(0, geo.height - stepSize);
  4681. }
  4682. else if (keyCode == 39)
  4683. {
  4684. geo.width += stepSize;
  4685. }
  4686. else if (keyCode == 40)
  4687. {
  4688. geo.height += stepSize;
  4689. }
  4690. graph.getModel().setGeometry(cells[i], geo);
  4691. }
  4692. }
  4693. }
  4694. }
  4695. finally
  4696. {
  4697. graph.getModel().endUpdate();
  4698. }
  4699. }
  4700. else
  4701. {
  4702. // Moves vertices up/down in a stack layout
  4703. var parent = graph.model.getParent(cell);
  4704. var scale = graph.getView().scale;
  4705. var layout = null;
  4706. if (graph.getSelectionCount() == 1 && graph.model.isVertex(cell) &&
  4707. graph.layoutManager != null && !graph.isCellLocked(cell))
  4708. {
  4709. layout = graph.layoutManager.getLayout(parent);
  4710. }
  4711. if (layout != null && layout.constructor == mxStackLayout)
  4712. {
  4713. var index = parent.getIndex(cell);
  4714. if (keyCode == 37 || keyCode == 38)
  4715. {
  4716. graph.model.add(parent, cell, Math.max(0, index - 1));
  4717. }
  4718. else if (keyCode == 39 ||keyCode == 40)
  4719. {
  4720. graph.model.add(parent, cell, Math.min(graph.model.getChildCount(parent), index + 1));
  4721. }
  4722. }
  4723. else
  4724. {
  4725. var handler = graph.graphHandler;
  4726. if (handler != null)
  4727. {
  4728. if (handler.first == null)
  4729. {
  4730. handler.start(cell, 0, 0, cells);
  4731. }
  4732. if (handler.first != null)
  4733. {
  4734. var dx = 0;
  4735. var dy = 0;
  4736. if (keyCode == 37)
  4737. {
  4738. dx = -stepSize;
  4739. }
  4740. else if (keyCode == 38)
  4741. {
  4742. dy = -stepSize;
  4743. }
  4744. else if (keyCode == 39)
  4745. {
  4746. dx = stepSize;
  4747. }
  4748. else if (keyCode == 40)
  4749. {
  4750. dy = stepSize;
  4751. }
  4752. handler.currentDx += dx * scale;
  4753. handler.currentDy += dy * scale;
  4754. handler.checkPreview();
  4755. handler.updatePreview();
  4756. }
  4757. // Groups move steps in undoable change
  4758. if (thread != null)
  4759. {
  4760. window.clearTimeout(thread);
  4761. }
  4762. thread = window.setTimeout(function()
  4763. {
  4764. if (handler.first != null)
  4765. {
  4766. var dx = handler.roundLength(handler.currentDx / scale);
  4767. var dy = handler.roundLength(handler.currentDy / scale);
  4768. handler.moveCells(handler.cells, dx, dy);
  4769. handler.reset();
  4770. }
  4771. }, 400);
  4772. }
  4773. }
  4774. }
  4775. }
  4776. }
  4777. };
  4778. // Overridden to handle special alt+shift+cursor keyboard shortcuts
  4779. var directions = {37: mxConstants.DIRECTION_WEST, 38: mxConstants.DIRECTION_NORTH,
  4780. 39: mxConstants.DIRECTION_EAST, 40: mxConstants.DIRECTION_SOUTH};
  4781. var keyHandlerGetFunction = keyHandler.getFunction;
  4782. mxKeyHandler.prototype.getFunction = function(evt)
  4783. {
  4784. if (graph.isEnabled())
  4785. {
  4786. // TODO: Add alt modified state in core API, here are some specific cases
  4787. if (mxEvent.isShiftDown(evt) && mxEvent.isAltDown(evt))
  4788. {
  4789. var action = editorUi.actions.get(editorUi.altShiftActions[evt.keyCode]);
  4790. if (action != null)
  4791. {
  4792. return action.funct;
  4793. }
  4794. }
  4795. if (directions[evt.keyCode] != null && !graph.isSelectionEmpty())
  4796. {
  4797. // On macOS, Control+Cursor is used by Expose so allow for Alt+Control to resize
  4798. if (!this.isControlDown(evt) && mxEvent.isShiftDown(evt) && mxEvent.isAltDown(evt))
  4799. {
  4800. if (graph.model.isVertex(graph.getSelectionCell()))
  4801. {
  4802. return function()
  4803. {
  4804. var cells = graph.connectVertex(graph.getSelectionCell(), directions[evt.keyCode],
  4805. graph.defaultEdgeLength, evt, true);
  4806. if (cells != null && cells.length > 0)
  4807. {
  4808. if (cells.length == 1 && graph.model.isEdge(cells[0]))
  4809. {
  4810. graph.setSelectionCell(graph.model.getTerminal(cells[0], false));
  4811. }
  4812. else
  4813. {
  4814. graph.setSelectionCell(cells[cells.length - 1]);
  4815. }
  4816. graph.scrollCellToVisible(graph.getSelectionCell());
  4817. if (editorUi.hoverIcons != null)
  4818. {
  4819. editorUi.hoverIcons.update(graph.view.getState(graph.getSelectionCell()));
  4820. }
  4821. }
  4822. };
  4823. }
  4824. }
  4825. else
  4826. {
  4827. // Avoids consuming event if no vertex is selected by returning null below
  4828. // Cursor keys move and resize (ctrl) cells
  4829. if (this.isControlDown(evt))
  4830. {
  4831. return function()
  4832. {
  4833. nudge(evt.keyCode, (mxEvent.isShiftDown(evt)) ? graph.gridSize : null, true);
  4834. };
  4835. }
  4836. else
  4837. {
  4838. return function()
  4839. {
  4840. nudge(evt.keyCode, (mxEvent.isShiftDown(evt)) ? graph.gridSize : null);
  4841. };
  4842. }
  4843. }
  4844. }
  4845. }
  4846. return keyHandlerGetFunction.apply(this, arguments);
  4847. };
  4848. // Binds keystrokes to actions
  4849. keyHandler.bindAction = mxUtils.bind(this, function(code, control, key, shift)
  4850. {
  4851. var action = this.actions.get(key);
  4852. if (action != null)
  4853. {
  4854. var f = function()
  4855. {
  4856. if (action.isEnabled())
  4857. {
  4858. action.funct();
  4859. }
  4860. };
  4861. if (control)
  4862. {
  4863. if (shift)
  4864. {
  4865. keyHandler.bindControlShiftKey(code, f);
  4866. }
  4867. else
  4868. {
  4869. keyHandler.bindControlKey(code, f);
  4870. }
  4871. }
  4872. else
  4873. {
  4874. if (shift)
  4875. {
  4876. keyHandler.bindShiftKey(code, f);
  4877. }
  4878. else
  4879. {
  4880. keyHandler.bindKey(code, f);
  4881. }
  4882. }
  4883. }
  4884. });
  4885. var ui = this;
  4886. var keyHandlerEscape = keyHandler.escape;
  4887. keyHandler.escape = function(evt)
  4888. {
  4889. keyHandlerEscape.apply(this, arguments);
  4890. };
  4891. // Ignores enter keystroke. Remove this line if you want the
  4892. // enter keystroke to stop editing. N, W, T are reserved.
  4893. keyHandler.enter = function() {};
  4894. keyHandler.bindControlShiftKey(36, function() { graph.exitGroup(); }); // Ctrl+Shift+Home
  4895. keyHandler.bindControlShiftKey(35, function() { graph.enterGroup(); }); // Ctrl+Shift+End
  4896. keyHandler.bindShiftKey(36, function() { graph.home(); }); // Ctrl+Shift+Home
  4897. keyHandler.bindKey(35, function() { graph.refresh(); }); // End
  4898. keyHandler.bindAction(107, true, 'zoomIn'); // Ctrl+Plus
  4899. keyHandler.bindAction(109, true, 'zoomOut'); // Ctrl+Minus
  4900. keyHandler.bindAction(80, true, 'print'); // Ctrl+P
  4901. keyHandler.bindAction(79, true, 'outline', true); // Ctrl+Shift+O
  4902. if (!this.editor.chromeless || this.editor.editable)
  4903. {
  4904. keyHandler.bindControlKey(36, function() { if (graph.isEnabled()) { graph.foldCells(true); }}); // Ctrl+Home
  4905. keyHandler.bindControlKey(35, function() { if (graph.isEnabled()) { graph.foldCells(false); }}); // Ctrl+End
  4906. keyHandler.bindControlKey(13, function() { ui.ctrlEnter(); }); // Ctrl+Enter
  4907. keyHandler.bindAction(8, false, 'delete'); // Backspace
  4908. keyHandler.bindAction(8, true, 'deleteAll'); // Ctrl+Backspace
  4909. keyHandler.bindAction(8, false, 'deleteLabels', true); // Shift+Backspace
  4910. keyHandler.bindAction(46, false, 'delete'); // Delete
  4911. keyHandler.bindAction(46, true, 'deleteAll'); // Ctrl+Delete
  4912. keyHandler.bindAction(46, false, 'deleteLabels', true); // Shift+Delete
  4913. keyHandler.bindAction(36, false, 'resetView'); // Home
  4914. keyHandler.bindAction(72, true, 'fitWindow', true); // Ctrl+Shift+H
  4915. keyHandler.bindAction(74, true, 'fitPage'); // Ctrl+J
  4916. keyHandler.bindAction(74, true, 'fitTwoPages', true); // Ctrl+Shift+J
  4917. keyHandler.bindAction(48, true, 'customZoom'); // Ctrl+0
  4918. keyHandler.bindAction(82, true, 'turn'); // Ctrl+R
  4919. keyHandler.bindAction(82, true, 'clearDefaultStyle', true); // Ctrl+Shift+R
  4920. keyHandler.bindAction(83, true, 'save'); // Ctrl+S
  4921. keyHandler.bindAction(83, true, 'saveAs', true); // Ctrl+Shift+S
  4922. keyHandler.bindAction(65, true, 'selectAll'); // Ctrl+A
  4923. keyHandler.bindAction(65, true, 'selectNone', true); // Ctrl+A
  4924. keyHandler.bindAction(73, true, 'selectVertices', true); // Ctrl+Shift+I
  4925. keyHandler.bindAction(69, true, 'selectEdges', true); // Ctrl+Shift+E
  4926. keyHandler.bindAction(69, true, 'editStyle'); // Ctrl+E
  4927. keyHandler.bindAction(66, true, 'bold'); // Ctrl+B
  4928. keyHandler.bindAction(66, true, 'toBack', true); // Ctrl+Shift+B
  4929. keyHandler.bindAction(70, true, 'toFront', true); // Ctrl+Shift+F
  4930. keyHandler.bindAction(68, true, 'duplicate'); // Ctrl+D
  4931. keyHandler.bindAction(68, true, 'setAsDefaultStyle', true); // Ctrl+Shift+D
  4932. keyHandler.bindAction(90, true, 'undo'); // Ctrl+Z
  4933. keyHandler.bindAction(89, true, 'autosize', true); // Ctrl+Shift+Y
  4934. keyHandler.bindAction(88, true, 'cut'); // Ctrl+X
  4935. keyHandler.bindAction(67, true, 'copy'); // Ctrl+C
  4936. keyHandler.bindAction(86, true, 'paste'); // Ctrl+V
  4937. keyHandler.bindAction(71, true, 'group'); // Ctrl+G
  4938. keyHandler.bindAction(77, true, 'editData'); // Ctrl+M
  4939. keyHandler.bindAction(71, true, 'grid', true); // Ctrl+Shift+G
  4940. keyHandler.bindAction(73, true, 'italic'); // Ctrl+I
  4941. keyHandler.bindAction(76, true, 'lockUnlock'); // Ctrl+L
  4942. keyHandler.bindAction(76, true, 'layers', true); // Ctrl+Shift+L
  4943. keyHandler.bindAction(80, true, 'formatPanel', true); // Ctrl+Shift+P
  4944. keyHandler.bindAction(85, true, 'underline'); // Ctrl+U
  4945. keyHandler.bindAction(85, true, 'ungroup', true); // Ctrl+Shift+U
  4946. keyHandler.bindAction(190, true, 'superscript'); // Ctrl+.
  4947. keyHandler.bindAction(188, true, 'subscript'); // Ctrl+,
  4948. keyHandler.bindAction(13, false, 'keyPressEnter'); // Enter
  4949. keyHandler.bindKey(113, function() { if (graph.isEnabled()) { graph.startEditingAtCell(); }}); // F2
  4950. }
  4951. if (!mxClient.IS_WIN)
  4952. {
  4953. keyHandler.bindAction(90, true, 'redo', true); // Ctrl+Shift+Z
  4954. }
  4955. else
  4956. {
  4957. keyHandler.bindAction(89, true, 'redo'); // Ctrl+Y
  4958. }
  4959. return keyHandler;
  4960. };
  4961. /**
  4962. * Creates the keyboard event handler for the current graph and history.
  4963. */
  4964. EditorUi.prototype.destroy = function()
  4965. {
  4966. var graph = this.editor.graph;
  4967. if (graph != null && this.selectionStateListener != null)
  4968. {
  4969. graph.getSelectionModel().removeListener(mxEvent.CHANGE, this.selectionStateListener);
  4970. graph.getModel().removeListener(mxEvent.CHANGE, this.selectionStateListener);
  4971. graph.removeListener(mxEvent.EDITING_STARTED, this.selectionStateListener);
  4972. graph.removeListener(mxEvent.EDITING_STOPPED, this.selectionStateListener);
  4973. graph.getView().removeListener('unitChanged', this.selectionStateListener);
  4974. this.selectionStateListener = null;
  4975. }
  4976. if (this.editor != null)
  4977. {
  4978. this.editor.destroy();
  4979. this.editor = null;
  4980. }
  4981. if (this.menubar != null)
  4982. {
  4983. this.menubar.destroy();
  4984. this.menubar = null;
  4985. }
  4986. if (this.toolbar != null)
  4987. {
  4988. this.toolbar.destroy();
  4989. this.toolbar = null;
  4990. }
  4991. if (this.sidebar != null)
  4992. {
  4993. this.sidebar.destroy();
  4994. this.sidebar = null;
  4995. }
  4996. if (this.keyHandler != null)
  4997. {
  4998. this.keyHandler.destroy();
  4999. this.keyHandler = null;
  5000. }
  5001. if (this.keydownHandler != null)
  5002. {
  5003. mxEvent.removeListener(document, 'keydown', this.keydownHandler);
  5004. this.keydownHandler = null;
  5005. }
  5006. if (this.keyupHandler != null)
  5007. {
  5008. mxEvent.removeListener(document, 'keyup', this.keyupHandler);
  5009. this.keyupHandler = null;
  5010. }
  5011. if (this.resizeHandler != null)
  5012. {
  5013. mxEvent.removeListener(window, 'resize', this.resizeHandler);
  5014. this.resizeHandler = null;
  5015. }
  5016. if (this.gestureHandler != null)
  5017. {
  5018. mxEvent.removeGestureListeners(document, this.gestureHandler);
  5019. this.gestureHandler = null;
  5020. }
  5021. if (this.orientationChangeHandler != null)
  5022. {
  5023. mxEvent.removeListener(window, 'orientationchange', this.orientationChangeHandler);
  5024. this.orientationChangeHandler = null;
  5025. }
  5026. if (this.scrollHandler != null)
  5027. {
  5028. mxEvent.removeListener(window, 'scroll', this.scrollHandler);
  5029. this.scrollHandler = null;
  5030. }
  5031. if (this.destroyFunctions != null)
  5032. {
  5033. for (var i = 0; i < this.destroyFunctions.length; i++)
  5034. {
  5035. this.destroyFunctions[i]();
  5036. }
  5037. this.destroyFunctions = null;
  5038. }
  5039. var c = [this.menubarContainer, this.toolbarContainer, this.sidebarContainer,
  5040. this.formatContainer, this.diagramContainer, this.footerContainer,
  5041. this.chromelessToolbar, this.hsplit, this.sidebarFooterContainer,
  5042. this.layersDialog];
  5043. for (var i = 0; i < c.length; i++)
  5044. {
  5045. if (c[i] != null && c[i].parentNode != null)
  5046. {
  5047. c[i].parentNode.removeChild(c[i]);
  5048. }
  5049. }
  5050. };