EditorUi.js 100 KB

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