EditorUi.js 135 KB

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