EditorUi.js 123 KB

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