EditorUi.js 109 KB

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