EditorUi.js 116 KB

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