EditorUi.js 110 KB

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