EditorUi.js 146 KB

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