EditorUi.js 104 KB

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