Menus.js 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415
  1. /**
  2. * Copyright (c) 2006-2012, JGraph Ltd
  3. */
  4. /**
  5. * Constructs a new graph editor
  6. */
  7. Menus = function(editorUi)
  8. {
  9. this.editorUi = editorUi;
  10. this.menus = new Object();
  11. this.init();
  12. // Pre-fetches checkmark image
  13. if (!mxClient.IS_SVG)
  14. {
  15. new Image().src = this.checkmarkImage;
  16. }
  17. };
  18. /**
  19. * Sets the default font family.
  20. */
  21. Menus.prototype.defaultFont = 'Helvetica';
  22. /**
  23. * Sets the default font size.
  24. */
  25. Menus.prototype.defaultFontSize = '12';
  26. /**
  27. * Sets the default font size.
  28. */
  29. Menus.prototype.defaultMenuItems = ['file', 'edit', 'view', 'arrange', 'extras', 'help'];
  30. /**
  31. * Adds the label menu items to the given menu and parent.
  32. */
  33. Menus.prototype.defaultFonts = ['Helvetica', 'Verdana', 'Times New Roman', 'Garamond', 'Comic Sans MS',
  34. 'Courier New', 'Georgia', 'Lucida Console', 'Tahoma'];
  35. /**
  36. * Adds the label menu items to the given menu and parent.
  37. */
  38. Menus.prototype.init = function()
  39. {
  40. var graph = this.editorUi.editor.graph;
  41. var isGraphEnabled = mxUtils.bind(graph, graph.isEnabled);
  42. this.customFonts = [];
  43. this.customFontSizes = [];
  44. this.put('fontFamily', new Menu(mxUtils.bind(this, function(menu, parent)
  45. {
  46. var addItem = mxUtils.bind(this, function(fontname)
  47. {
  48. var tr = this.styleChange(menu, fontname, [mxConstants.STYLE_FONTFAMILY], [fontname], null, parent, function()
  49. {
  50. document.execCommand('fontname', false, fontname);
  51. }, function()
  52. {
  53. graph.updateLabelElements(graph.getSelectionCells(), function(elt)
  54. {
  55. elt.removeAttribute('face');
  56. elt.style.fontFamily = null;
  57. if (elt.nodeName == 'PRE')
  58. {
  59. graph.replaceElement(elt, 'div');
  60. }
  61. });
  62. });
  63. tr.firstChild.nextSibling.style.fontFamily = fontname;
  64. });
  65. for (var i = 0; i < this.defaultFonts.length; i++)
  66. {
  67. addItem(this.defaultFonts[i]);
  68. }
  69. menu.addSeparator(parent);
  70. if (this.customFonts.length > 0)
  71. {
  72. for (var i = 0; i < this.customFonts.length; i++)
  73. {
  74. addItem(this.customFonts[i]);
  75. }
  76. menu.addSeparator(parent);
  77. menu.addItem(mxResources.get('reset'), null, mxUtils.bind(this, function()
  78. {
  79. this.customFonts = [];
  80. this.editorUi.fireEvent(new mxEventObject('customFontsChanged'));
  81. }), parent);
  82. menu.addSeparator(parent);
  83. }
  84. this.promptChange(menu, mxResources.get('custom') + '...', '', mxConstants.DEFAULT_FONTFAMILY, mxConstants.STYLE_FONTFAMILY, parent, true, mxUtils.bind(this, function(newValue)
  85. {
  86. if (mxUtils.indexOf(this.customFonts, newValue) < 0)
  87. {
  88. this.customFonts.push(newValue);
  89. this.editorUi.fireEvent(new mxEventObject('customFontsChanged'));
  90. }
  91. }));
  92. })));
  93. this.put('formatBlock', new Menu(mxUtils.bind(this, function(menu, parent)
  94. {
  95. function addItem(label, tag)
  96. {
  97. return menu.addItem(label, null, mxUtils.bind(this, function()
  98. {
  99. // TODO: Check if visible
  100. if (graph.cellEditor.textarea != null)
  101. {
  102. graph.cellEditor.textarea.focus();
  103. document.execCommand('formatBlock', false, '<' + tag + '>');
  104. }
  105. }), parent);
  106. };
  107. addItem(mxResources.get('normal'), 'p');
  108. addItem('', 'h1').firstChild.nextSibling.innerHTML = '<h1 style="margin:0px;">' + mxResources.get('heading') + ' 1</h1>';
  109. addItem('', 'h2').firstChild.nextSibling.innerHTML = '<h2 style="margin:0px;">' + mxResources.get('heading') + ' 2</h2>';
  110. addItem('', 'h3').firstChild.nextSibling.innerHTML = '<h3 style="margin:0px;">' + mxResources.get('heading') + ' 3</h3>';
  111. addItem('', 'h4').firstChild.nextSibling.innerHTML = '<h4 style="margin:0px;">' + mxResources.get('heading') + ' 4</h4>';
  112. addItem('', 'h5').firstChild.nextSibling.innerHTML = '<h5 style="margin:0px;">' + mxResources.get('heading') + ' 5</h5>';
  113. addItem('', 'h6').firstChild.nextSibling.innerHTML = '<h6 style="margin:0px;">' + mxResources.get('heading') + ' 6</h6>';
  114. addItem('', 'pre').firstChild.nextSibling.innerHTML = '<pre style="margin:0px;">' + mxResources.get('formatted') + '</pre>';
  115. addItem('', 'blockquote').firstChild.nextSibling.innerHTML = '<blockquote style="margin-top:0px;margin-bottom:0px;">' + mxResources.get('blockquote') + '</blockquote>';
  116. })));
  117. this.put('fontSize', new Menu(mxUtils.bind(this, function(menu, parent)
  118. {
  119. var sizes = [6, 8, 9, 10, 11, 12, 14, 18, 24, 36, 48, 72];
  120. var addItem = mxUtils.bind(this, function(fontsize)
  121. {
  122. this.styleChange(menu, fontsize, [mxConstants.STYLE_FONTSIZE], [fontsize], null, parent, function()
  123. {
  124. if (graph.cellEditor.textarea != null)
  125. {
  126. // Creates an element with arbitrary size 3
  127. document.execCommand('fontSize', false, '3');
  128. // Changes the css font size of the first font element inside the in-place editor with size 3
  129. // hopefully the above element that we've just created. LATER: Check for new element using
  130. // previous result of getElementsByTagName (see other actions)
  131. var elts = graph.cellEditor.textarea.getElementsByTagName('font');
  132. for (var i = 0; i < elts.length; i++)
  133. {
  134. if (elts[i].getAttribute('size') == '3')
  135. {
  136. elts[i].removeAttribute('size');
  137. elts[i].style.fontSize = fontsize + 'px';
  138. break;
  139. }
  140. }
  141. }
  142. });
  143. });
  144. for (var i = 0; i < sizes.length; i++)
  145. {
  146. addItem(sizes[i]);
  147. }
  148. menu.addSeparator(parent);
  149. if (this.customFontSizes.length > 0)
  150. {
  151. for (var i = 0; i < this.customFontSizes.length; i++)
  152. {
  153. addItem(this.customFontSizes[i]);
  154. }
  155. menu.addSeparator(parent);
  156. menu.addItem(mxResources.get('reset'), null, mxUtils.bind(this, function()
  157. {
  158. this.customFontSizes = [];
  159. }), parent);
  160. menu.addSeparator(parent);
  161. }
  162. this.promptChange(menu, mxResources.get('custom') + '...', '(pt)', '12', mxConstants.STYLE_FONTSIZE, parent, true, mxUtils.bind(this, function(newValue)
  163. {
  164. this.customFontSizes.push(newValue);
  165. }));
  166. })));
  167. this.put('direction', new Menu(mxUtils.bind(this, function(menu, parent)
  168. {
  169. menu.addItem(mxResources.get('flipH'), null, function() { graph.toggleCellStyles(mxConstants.STYLE_FLIPH, false); }, parent);
  170. menu.addItem(mxResources.get('flipV'), null, function() { graph.toggleCellStyles(mxConstants.STYLE_FLIPV, false); }, parent);
  171. this.addMenuItems(menu, ['-', 'rotation'], parent);
  172. })));
  173. this.put('align', new Menu(mxUtils.bind(this, function(menu, parent)
  174. {
  175. menu.addItem(mxResources.get('leftAlign'), null, function() { graph.alignCells(mxConstants.ALIGN_LEFT); }, parent);
  176. menu.addItem(mxResources.get('center'), null, function() { graph.alignCells(mxConstants.ALIGN_CENTER); }, parent);
  177. menu.addItem(mxResources.get('rightAlign'), null, function() { graph.alignCells(mxConstants.ALIGN_RIGHT); }, parent);
  178. menu.addSeparator(parent);
  179. menu.addItem(mxResources.get('topAlign'), null, function() { graph.alignCells(mxConstants.ALIGN_TOP); }, parent);
  180. menu.addItem(mxResources.get('middle'), null, function() { graph.alignCells(mxConstants.ALIGN_MIDDLE); }, parent);
  181. menu.addItem(mxResources.get('bottomAlign'), null, function() { graph.alignCells(mxConstants.ALIGN_BOTTOM); }, parent);
  182. })));
  183. this.put('distribute', new Menu(mxUtils.bind(this, function(menu, parent)
  184. {
  185. menu.addItem(mxResources.get('horizontal'), null, function() { graph.distributeCells(true); }, parent);
  186. menu.addItem(mxResources.get('vertical'), null, function() { graph.distributeCells(false); }, parent);
  187. })));
  188. this.put('layout', new Menu(mxUtils.bind(this, function(menu, parent)
  189. {
  190. var promptSpacing = mxUtils.bind(this, function(defaultValue, fn)
  191. {
  192. var dlg = new FilenameDialog(this.editorUi, defaultValue, mxResources.get('apply'), function(newValue)
  193. {
  194. fn(parseFloat(newValue));
  195. }, mxResources.get('spacing'));
  196. this.editorUi.showDialog(dlg.container, 300, 80, true, true);
  197. dlg.init();
  198. });
  199. menu.addItem(mxResources.get('horizontalFlow'), null, mxUtils.bind(this, function()
  200. {
  201. var layout = new mxHierarchicalLayout(graph, mxConstants.DIRECTION_WEST);
  202. this.editorUi.executeLayout(function()
  203. {
  204. var selectionCells = graph.getSelectionCells();
  205. layout.execute(graph.getDefaultParent(), selectionCells.length == 0 ? null : selectionCells);
  206. }, true);
  207. }), parent);
  208. menu.addItem(mxResources.get('verticalFlow'), null, mxUtils.bind(this, function()
  209. {
  210. var layout = new mxHierarchicalLayout(graph, mxConstants.DIRECTION_NORTH);
  211. this.editorUi.executeLayout(function()
  212. {
  213. var selectionCells = graph.getSelectionCells();
  214. layout.execute(graph.getDefaultParent(), selectionCells.length == 0 ? null : selectionCells);
  215. }, true);
  216. }), parent);
  217. menu.addSeparator(parent);
  218. menu.addItem(mxResources.get('horizontalTree'), null, mxUtils.bind(this, function()
  219. {
  220. var tmp = graph.getSelectionCell();
  221. var roots = null;
  222. if (tmp == null || graph.getModel().getChildCount(tmp) == 0)
  223. {
  224. if (graph.getModel().getEdgeCount(tmp) == 0)
  225. {
  226. roots = graph.findTreeRoots(graph.getDefaultParent());
  227. }
  228. }
  229. else
  230. {
  231. roots = graph.findTreeRoots(tmp);
  232. }
  233. if (roots != null && roots.length > 0)
  234. {
  235. tmp = roots[0];
  236. }
  237. if (tmp != null)
  238. {
  239. var layout = new mxCompactTreeLayout(graph, true);
  240. layout.edgeRouting = false;
  241. layout.levelDistance = 30;
  242. promptSpacing(layout.levelDistance, mxUtils.bind(this, function(newValue)
  243. {
  244. layout.levelDistance = newValue;
  245. this.editorUi.executeLayout(function()
  246. {
  247. layout.execute(graph.getDefaultParent(), tmp);
  248. }, true);
  249. }));
  250. }
  251. }), parent);
  252. menu.addItem(mxResources.get('verticalTree'), null, mxUtils.bind(this, function()
  253. {
  254. var tmp = graph.getSelectionCell();
  255. var roots = null;
  256. if (tmp == null || graph.getModel().getChildCount(tmp) == 0)
  257. {
  258. if (graph.getModel().getEdgeCount(tmp) == 0)
  259. {
  260. roots = graph.findTreeRoots(graph.getDefaultParent());
  261. }
  262. }
  263. else
  264. {
  265. roots = graph.findTreeRoots(tmp);
  266. }
  267. if (roots != null && roots.length > 0)
  268. {
  269. tmp = roots[0];
  270. }
  271. if (tmp != null)
  272. {
  273. var layout = new mxCompactTreeLayout(graph, false);
  274. layout.edgeRouting = false;
  275. layout.levelDistance = 30;
  276. promptSpacing(layout.levelDistance, mxUtils.bind(this, function(newValue)
  277. {
  278. layout.levelDistance = newValue;
  279. this.editorUi.executeLayout(function()
  280. {
  281. layout.execute(graph.getDefaultParent(), tmp);
  282. }, true);
  283. }));
  284. }
  285. }), parent);
  286. menu.addItem(mxResources.get('radialTree'), null, mxUtils.bind(this, function()
  287. {
  288. var tmp = graph.getSelectionCell();
  289. var roots = null;
  290. if (tmp == null || graph.getModel().getChildCount(tmp) == 0)
  291. {
  292. if (graph.getModel().getEdgeCount(tmp) == 0)
  293. {
  294. roots = graph.findTreeRoots(graph.getDefaultParent());
  295. }
  296. }
  297. else
  298. {
  299. roots = graph.findTreeRoots(tmp);
  300. }
  301. if (roots != null && roots.length > 0)
  302. {
  303. tmp = roots[0];
  304. }
  305. if (tmp != null)
  306. {
  307. var layout = new mxRadialTreeLayout(graph, false);
  308. layout.levelDistance = 80;
  309. layout.autoRadius = true;
  310. promptSpacing(layout.levelDistance, mxUtils.bind(this, function(newValue)
  311. {
  312. layout.levelDistance = newValue;
  313. this.editorUi.executeLayout(function()
  314. {
  315. layout.execute(graph.getDefaultParent(), tmp);
  316. if (!graph.isSelectionEmpty())
  317. {
  318. tmp = graph.getModel().getParent(tmp);
  319. if (graph.getModel().isVertex(tmp))
  320. {
  321. graph.updateGroupBounds([tmp], graph.gridSize * 2, true);
  322. }
  323. }
  324. }, true);
  325. }));
  326. }
  327. }), parent);
  328. menu.addSeparator(parent);
  329. menu.addItem(mxResources.get('organic'), null, mxUtils.bind(this, function()
  330. {
  331. var layout = new mxFastOrganicLayout(graph);
  332. promptSpacing(layout.forceConstant, mxUtils.bind(this, function(newValue)
  333. {
  334. layout.forceConstant = newValue;
  335. this.editorUi.executeLayout(function()
  336. {
  337. var tmp = graph.getSelectionCell();
  338. if (tmp == null || graph.getModel().getChildCount(tmp) == 0)
  339. {
  340. tmp = graph.getDefaultParent();
  341. }
  342. layout.execute(tmp);
  343. if (graph.getModel().isVertex(tmp))
  344. {
  345. graph.updateGroupBounds([tmp], graph.gridSize * 2, true);
  346. }
  347. }, true);
  348. }));
  349. }), parent);
  350. menu.addItem(mxResources.get('circle'), null, mxUtils.bind(this, function()
  351. {
  352. var layout = new mxCircleLayout(graph);
  353. this.editorUi.executeLayout(function()
  354. {
  355. var tmp = graph.getSelectionCell();
  356. if (tmp == null || graph.getModel().getChildCount(tmp) == 0)
  357. {
  358. tmp = graph.getDefaultParent();
  359. }
  360. layout.execute(tmp);
  361. if (graph.getModel().isVertex(tmp))
  362. {
  363. graph.updateGroupBounds([tmp], graph.gridSize * 2, true);
  364. }
  365. }, true);
  366. }), parent);
  367. })));
  368. this.put('navigation', new Menu(mxUtils.bind(this, function(menu, parent)
  369. {
  370. this.addMenuItems(menu, ['home', '-', 'exitGroup', 'enterGroup', '-', 'expand', 'collapse', '-', 'collapsible'], parent);
  371. })));
  372. this.put('arrange', new Menu(mxUtils.bind(this, function(menu, parent)
  373. {
  374. this.addMenuItems(menu, ['toFront', 'toBack', '-'], parent);
  375. this.addSubmenu('direction', menu, parent);
  376. this.addMenuItems(menu, ['turn', '-'], parent);
  377. this.addSubmenu('align', menu, parent);
  378. this.addSubmenu('distribute', menu, parent);
  379. menu.addSeparator(parent);
  380. this.addSubmenu('navigation', menu, parent);
  381. this.addSubmenu('insert', menu, parent);
  382. this.addSubmenu('layout', menu, parent);
  383. this.addMenuItems(menu, ['-', 'group', 'ungroup', 'removeFromGroup', '-', 'clearWaypoints', 'autosize'], parent);
  384. }))).isEnabled = isGraphEnabled;
  385. this.put('insert', new Menu(mxUtils.bind(this, function(menu, parent)
  386. {
  387. this.addMenuItems(menu, ['insertLink', 'insertImage'], parent);
  388. })));
  389. this.put('view', new Menu(mxUtils.bind(this, function(menu, parent)
  390. {
  391. this.addMenuItems(menu, ((this.editorUi.format != null) ? ['formatPanel'] : []).
  392. concat(['outline', 'layers', '-', 'pageView', 'pageScale', '-', 'scrollbars', 'tooltips', '-',
  393. 'grid', 'guides', '-', 'connectionArrows', 'connectionPoints', '-',
  394. 'resetView', 'zoomIn', 'zoomOut'], parent));
  395. })));
  396. // Two special dropdowns that are only used in the toolbar
  397. this.put('viewPanels', new Menu(mxUtils.bind(this, function(menu, parent)
  398. {
  399. if (this.editorUi.format != null)
  400. {
  401. this.addMenuItems(menu, ['formatPanel'], parent);
  402. }
  403. this.addMenuItems(menu, ['outline', 'layers'], parent);
  404. })));
  405. this.put('viewZoom', new Menu(mxUtils.bind(this, function(menu, parent)
  406. {
  407. this.addMenuItems(menu, ['resetView', '-'], parent);
  408. var scales = [0.25, 0.5, 0.75, 1, 1.25, 1.5, 2, 3, 4];
  409. for (var i = 0; i < scales.length; i++)
  410. {
  411. (function(scale)
  412. {
  413. menu.addItem((scale * 100) + '%', null, function()
  414. {
  415. graph.zoomTo(scale);
  416. }, parent);
  417. })(scales[i]);
  418. }
  419. this.addMenuItems(menu, ['-', 'fitWindow', 'fitPageWidth', 'fitPage', 'fitTwoPages', '-', 'customZoom'], parent);
  420. })));
  421. this.put('file', new Menu(mxUtils.bind(this, function(menu, parent)
  422. {
  423. this.addMenuItems(menu, ['new', 'open', '-', 'save', 'saveAs', '-', 'import', 'export', '-', 'pageSetup', 'print'], parent);
  424. })));
  425. this.put('edit', new Menu(mxUtils.bind(this, function(menu, parent)
  426. {
  427. this.addMenuItems(menu, ['undo', 'redo', '-', 'cut', 'copy', 'paste', 'delete', '-', 'duplicate', '-',
  428. 'editData', 'editTooltip', 'editStyle', '-', 'edit', '-', 'editLink', 'openLink', '-',
  429. 'selectVertices', 'selectEdges', 'selectAll', 'selectNone', '-', 'lockUnlock']);
  430. })));
  431. this.put('extras', new Menu(mxUtils.bind(this, function(menu, parent)
  432. {
  433. this.addMenuItems(menu, ['copyConnect', 'collapseExpand', '-', 'editDiagram']);
  434. })));
  435. this.put('help', new Menu(mxUtils.bind(this, function(menu, parent)
  436. {
  437. this.addMenuItems(menu, ['help', '-', 'about']);
  438. })));
  439. };
  440. /**
  441. * Adds the label menu items to the given menu and parent.
  442. */
  443. Menus.prototype.put = function(name, menu)
  444. {
  445. this.menus[name] = menu;
  446. return menu;
  447. };
  448. /**
  449. * Adds the label menu items to the given menu and parent.
  450. */
  451. Menus.prototype.get = function(name)
  452. {
  453. return this.menus[name];
  454. };
  455. /**
  456. * Adds the given submenu.
  457. */
  458. Menus.prototype.addSubmenu = function(name, menu, parent, label)
  459. {
  460. var entry = this.get(name);
  461. if (entry != null)
  462. {
  463. var enabled = entry.isEnabled();
  464. if (menu.showDisabled || enabled)
  465. {
  466. var submenu = menu.addItem(label || mxResources.get(name), null, null, parent, null, enabled);
  467. this.addMenu(name, menu, submenu);
  468. }
  469. }
  470. };
  471. /**
  472. * Adds the label menu items to the given menu and parent.
  473. */
  474. Menus.prototype.addMenu = function(name, popupMenu, parent)
  475. {
  476. var menu = this.get(name);
  477. if (menu != null && (popupMenu.showDisabled || menu.isEnabled()))
  478. {
  479. this.get(name).execute(popupMenu, parent);
  480. }
  481. };
  482. /**
  483. * Adds a menu item to insert a table.
  484. */
  485. Menus.prototype.addInsertTableItem = function(menu, insertFn)
  486. {
  487. insertFn = (insertFn != null) ? insertFn : mxUtils.bind(this, function(evt, rows, cols)
  488. {
  489. var graph = this.editorUi.editor.graph;
  490. var td = graph.getParentByName(mxEvent.getSource(evt), 'TD');
  491. if (td != null && graph.cellEditor.textarea != null)
  492. {
  493. var row2 = graph.getParentByName(td, 'TR');
  494. // To find the new link, we create a list of all existing links first
  495. // LATER: Refactor for reuse with code for finding inserted image below
  496. var tmp = graph.cellEditor.textarea.getElementsByTagName('table');
  497. var oldTables = [];
  498. for (var i = 0; i < tmp.length; i++)
  499. {
  500. oldTables.push(tmp[i]);
  501. }
  502. // Finding the new table will work with insertHTML, but IE does not support that
  503. graph.container.focus();
  504. graph.pasteHtmlAtCaret(createTable(rows, cols));
  505. // Moves cursor to first table cell
  506. var newTables = graph.cellEditor.textarea.getElementsByTagName('table');
  507. if (newTables.length == oldTables.length + 1)
  508. {
  509. // Inverse order in favor of appended tables
  510. for (var i = newTables.length - 1; i >= 0; i--)
  511. {
  512. if (i == 0 || newTables[i] != oldTables[i - 1])
  513. {
  514. graph.selectNode(newTables[i].rows[0].cells[0]);
  515. break;
  516. }
  517. }
  518. }
  519. }
  520. });
  521. // KNOWN: Does not work in IE8 standards and quirks
  522. var graph = this.editorUi.editor.graph;
  523. var row2 = null;
  524. var td = null;
  525. function createTable(rows, cols)
  526. {
  527. var html = ['<table>'];
  528. for (var i = 0; i < rows; i++)
  529. {
  530. html.push('<tr>');
  531. for (var j = 0; j < cols; j++)
  532. {
  533. html.push('<td><br></td>');
  534. }
  535. html.push('</tr>');
  536. }
  537. html.push('</table>');
  538. return html.join('');
  539. };
  540. // Show table size dialog
  541. var elt2 = menu.addItem('', null, mxUtils.bind(this, function(evt)
  542. {
  543. if (td != null && row2 != null)
  544. {
  545. insertFn(evt, row2.sectionRowIndex + 1, td.cellIndex + 1);
  546. }
  547. }));
  548. // Quirks mode does not add cell padding if cell is empty, needs good old spacer solution
  549. var quirksCellHtml = '<img src="' + mxClient.imageBasePath + '/transparent.gif' + '" width="16" height="16"/>';
  550. function createPicker(rows, cols)
  551. {
  552. var table2 = document.createElement('table');
  553. table2.setAttribute('border', '1');
  554. table2.style.borderCollapse = 'collapse';
  555. if (!mxClient.IS_QUIRKS)
  556. {
  557. table2.setAttribute('cellPadding', '8');
  558. }
  559. for (var i = 0; i < rows; i++)
  560. {
  561. var row = table2.insertRow(i);
  562. for (var j = 0; j < cols; j++)
  563. {
  564. var cell = row.insertCell(-1);
  565. if (mxClient.IS_QUIRKS)
  566. {
  567. cell.innerHTML = quirksCellHtml;
  568. }
  569. }
  570. }
  571. return table2;
  572. };
  573. function extendPicker(picker, rows, cols)
  574. {
  575. for (var i = picker.rows.length; i < rows; i++)
  576. {
  577. var row = picker.insertRow(i);
  578. for (var j = 0; j < picker.rows[0].cells.length; j++)
  579. {
  580. var cell = row.insertCell(-1);
  581. if (mxClient.IS_QUIRKS)
  582. {
  583. cell.innerHTML = quirksCellHtml;
  584. }
  585. }
  586. }
  587. for (var i = 0; i < picker.rows.length; i++)
  588. {
  589. var row = picker.rows[i];
  590. for (var j = row.cells.length; j < cols; j++)
  591. {
  592. var cell = row.insertCell(-1);
  593. if (mxClient.IS_QUIRKS)
  594. {
  595. cell.innerHTML = quirksCellHtml;
  596. }
  597. }
  598. }
  599. };
  600. elt2.firstChild.innerHTML = '';
  601. var picker = createPicker(5, 5);
  602. elt2.firstChild.appendChild(picker);
  603. var label = document.createElement('div');
  604. label.style.padding = '4px';
  605. label.style.fontSize = Menus.prototype.defaultFontSize + 'px';
  606. label.innerHTML = '1x1';
  607. elt2.firstChild.appendChild(label);
  608. mxEvent.addListener(picker, 'mouseover', function(e)
  609. {
  610. td = graph.getParentByName(mxEvent.getSource(e), 'TD');
  611. if (td != null)
  612. {
  613. row2 = graph.getParentByName(td, 'TR');
  614. extendPicker(picker, Math.min(20, row2.sectionRowIndex + 2), Math.min(20, td.cellIndex + 2));
  615. label.innerHTML = (td.cellIndex + 1) + 'x' + (row2.sectionRowIndex + 1);
  616. for (var i = 0; i < picker.rows.length; i++)
  617. {
  618. var r = picker.rows[i];
  619. for (var j = 0; j < r.cells.length; j++)
  620. {
  621. var cell = r.cells[j];
  622. if (i <= row2.sectionRowIndex && j <= td.cellIndex)
  623. {
  624. cell.style.backgroundColor = 'blue';
  625. }
  626. else
  627. {
  628. cell.style.backgroundColor = 'white';
  629. }
  630. }
  631. }
  632. mxEvent.consume(e);
  633. }
  634. });
  635. };
  636. /**
  637. * Adds a style change item to the given menu.
  638. */
  639. Menus.prototype.edgeStyleChange = function(menu, label, keys, values, sprite, parent, reset)
  640. {
  641. return menu.addItem(label, null, mxUtils.bind(this, function()
  642. {
  643. var graph = this.editorUi.editor.graph;
  644. graph.stopEditing(false);
  645. graph.getModel().beginUpdate();
  646. try
  647. {
  648. var cells = graph.getSelectionCells();
  649. var edges = [];
  650. for (var i = 0; i < cells.length; i++)
  651. {
  652. var cell = cells[i];
  653. if (graph.getModel().isEdge(cell))
  654. {
  655. if (reset)
  656. {
  657. var geo = graph.getCellGeometry(cell);
  658. // Resets all edge points
  659. if (geo != null)
  660. {
  661. geo = geo.clone();
  662. geo.points = null;
  663. graph.getModel().setGeometry(cell, geo);
  664. }
  665. }
  666. for (var j = 0; j < keys.length; j++)
  667. {
  668. graph.setCellStyles(keys[j], values[j], [cell]);
  669. }
  670. edges.push(cell);
  671. }
  672. }
  673. this.editorUi.fireEvent(new mxEventObject('styleChanged', 'keys', keys,
  674. 'values', values, 'cells', edges));
  675. }
  676. finally
  677. {
  678. graph.getModel().endUpdate();
  679. }
  680. }), parent, sprite);
  681. };
  682. /**
  683. * Adds a style change item to the given menu.
  684. */
  685. Menus.prototype.styleChange = function(menu, label, keys, values, sprite, parent, fn, post)
  686. {
  687. var apply = this.createStyleChangeFunction(keys, values);
  688. return menu.addItem(label, null, mxUtils.bind(this, function()
  689. {
  690. var graph = this.editorUi.editor.graph;
  691. if (fn != null && graph.cellEditor.isContentEditing())
  692. {
  693. fn();
  694. }
  695. else
  696. {
  697. apply(post);
  698. }
  699. }), parent, sprite);
  700. };
  701. /**
  702. *
  703. */
  704. Menus.prototype.createStyleChangeFunction = function(keys, values)
  705. {
  706. return mxUtils.bind(this, function(post)
  707. {
  708. var graph = this.editorUi.editor.graph;
  709. graph.stopEditing(false);
  710. graph.getModel().beginUpdate();
  711. try
  712. {
  713. var cells = graph.getSelectionCells();
  714. for (var i = 0; i < keys.length; i++)
  715. {
  716. graph.setCellStyles(keys[i], values[i], cells);
  717. // Removes CSS alignment to produce consistent output
  718. if (keys[i] == mxConstants.STYLE_ALIGN)
  719. {
  720. graph.updateLabelElements(cells, function(elt)
  721. {
  722. elt.removeAttribute('align');
  723. elt.style.textAlign = null;
  724. });
  725. }
  726. // Updates autosize after font changes
  727. if (keys[i] == mxConstants.STYLE_FONTFAMILY)
  728. {
  729. for (var i = 0; i < cells.length; i++)
  730. {
  731. if (graph.model.getChildCount(cells[i]) == 0)
  732. {
  733. graph.autoSizeCell(cells[i], false);
  734. }
  735. }
  736. }
  737. }
  738. if (post != null)
  739. {
  740. post();
  741. }
  742. this.editorUi.fireEvent(new mxEventObject('styleChanged',
  743. 'keys', keys, 'values', values, 'cells', cells));
  744. }
  745. finally
  746. {
  747. graph.getModel().endUpdate();
  748. }
  749. });
  750. };
  751. /**
  752. * Adds a style change item with a prompt to the given menu.
  753. */
  754. Menus.prototype.promptChange = function(menu, label, hint, defaultValue, key, parent, enabled, fn, sprite)
  755. {
  756. return menu.addItem(label, null, mxUtils.bind(this, function()
  757. {
  758. var graph = this.editorUi.editor.graph;
  759. var value = defaultValue;
  760. var state = graph.getView().getState(graph.getSelectionCell());
  761. if (state != null)
  762. {
  763. value = state.style[key] || value;
  764. }
  765. var dlg = new FilenameDialog(this.editorUi, value, mxResources.get('apply'), mxUtils.bind(this, function(newValue)
  766. {
  767. if (newValue != null && newValue.length > 0)
  768. {
  769. graph.getModel().beginUpdate();
  770. try
  771. {
  772. graph.stopEditing(false);
  773. graph.setCellStyles(key, newValue);
  774. }
  775. finally
  776. {
  777. graph.getModel().endUpdate();
  778. }
  779. if (fn != null)
  780. {
  781. fn(newValue);
  782. }
  783. }
  784. }), mxResources.get('enterValue') + ((hint.length > 0) ? (' ' + hint) : ''));
  785. this.editorUi.showDialog(dlg.container, 300, 80, true, true);
  786. dlg.init();
  787. }), parent, sprite, enabled);
  788. };
  789. /**
  790. * Adds a handler for showing a menu in the given element.
  791. */
  792. Menus.prototype.pickColor = function(key, cmd, defaultValue)
  793. {
  794. var graph = this.editorUi.editor.graph;
  795. var h = 226 + ((Math.ceil(ColorDialog.prototype.presetColors.length / 12) +
  796. Math.ceil(ColorDialog.prototype.defaultColors.length / 12)) * 17);
  797. if (cmd != null && graph.cellEditor.isContentEditing())
  798. {
  799. // Saves and restores text selection for in-place editor
  800. var selState = graph.cellEditor.saveSelection();
  801. var dlg = new ColorDialog(this.editorUi, defaultValue || '000000', mxUtils.bind(this, function(color)
  802. {
  803. graph.cellEditor.restoreSelection(selState);
  804. document.execCommand(cmd, false, (color != mxConstants.NONE) ? color : 'transparent');
  805. }), function()
  806. {
  807. graph.cellEditor.restoreSelection(selState);
  808. });
  809. this.editorUi.showDialog(dlg.container, 230, h, true, true);
  810. dlg.init();
  811. }
  812. else
  813. {
  814. if (this.colorDialog == null)
  815. {
  816. this.colorDialog = new ColorDialog(this.editorUi);
  817. }
  818. this.colorDialog.currentColorKey = key;
  819. var state = graph.getView().getState(graph.getSelectionCell());
  820. var color = 'none';
  821. if (state != null)
  822. {
  823. color = state.style[key] || color;
  824. }
  825. if (color == 'none')
  826. {
  827. color = 'ffffff';
  828. this.colorDialog.picker.fromString('ffffff');
  829. this.colorDialog.colorInput.value = 'none';
  830. }
  831. else
  832. {
  833. this.colorDialog.picker.fromString(color);
  834. }
  835. this.editorUi.showDialog(this.colorDialog.container, 230, h, true, true);
  836. this.colorDialog.init();
  837. }
  838. };
  839. /**
  840. * Adds a handler for showing a menu in the given element.
  841. */
  842. Menus.prototype.toggleStyle = function(key, defaultValue)
  843. {
  844. var graph = this.editorUi.editor.graph;
  845. var value = graph.toggleCellStyles(key, defaultValue);
  846. this.editorUi.fireEvent(new mxEventObject('styleChanged', 'keys', [key], 'values', [value],
  847. 'cells', graph.getSelectionCells()));
  848. };
  849. /**
  850. * Creates the keyboard event handler for the current graph and history.
  851. */
  852. Menus.prototype.addMenuItem = function(menu, key, parent, trigger, sprite, label)
  853. {
  854. var action = this.editorUi.actions.get(key);
  855. if (action != null && (menu.showDisabled || action.isEnabled()) && action.visible)
  856. {
  857. var item = menu.addItem(label || action.label, null, function()
  858. {
  859. action.funct(trigger);
  860. }, parent, sprite, action.isEnabled());
  861. // Adds checkmark image
  862. if (action.toggleAction && action.isSelected())
  863. {
  864. menu.addCheckmark(item, Editor.checkmarkImage);
  865. }
  866. this.addShortcut(item, action);
  867. return item;
  868. }
  869. return null;
  870. };
  871. /**
  872. * Adds a checkmark to the given menuitem.
  873. */
  874. Menus.prototype.addShortcut = function(item, action)
  875. {
  876. if (action.shortcut != null)
  877. {
  878. var td = item.firstChild.nextSibling.nextSibling;
  879. var span = document.createElement('span');
  880. span.style.color = 'gray';
  881. mxUtils.write(span, action.shortcut);
  882. td.appendChild(span);
  883. }
  884. };
  885. /**
  886. * Creates the keyboard event handler for the current graph and history.
  887. */
  888. Menus.prototype.addMenuItems = function(menu, keys, parent, trigger, sprites)
  889. {
  890. for (var i = 0; i < keys.length; i++)
  891. {
  892. if (keys[i] == '-')
  893. {
  894. menu.addSeparator(parent);
  895. }
  896. else
  897. {
  898. this.addMenuItem(menu, keys[i], parent, trigger, (sprites != null) ? sprites[i] : null);
  899. }
  900. }
  901. };
  902. /**
  903. * Creates the keyboard event handler for the current graph and history.
  904. */
  905. Menus.prototype.createPopupMenu = function(menu, cell, evt)
  906. {
  907. menu.smartSeparators = true;
  908. this.addPopupMenuHistoryItems(menu, cell, evt);
  909. this.addPopupMenuEditItems(menu, cell, evt);
  910. this.addPopupMenuStyleItems(menu, cell, evt);
  911. this.addPopupMenuArrangeItems(menu, cell, evt);
  912. this.addPopupMenuCellItems(menu, cell, evt);
  913. this.addPopupMenuSelectionItems(menu, cell, evt);
  914. };
  915. /**
  916. * Creates the keyboard event handler for the current graph and history.
  917. */
  918. Menus.prototype.addPopupMenuHistoryItems = function(menu, cell, evt)
  919. {
  920. if (this.editorUi.editor.graph.isSelectionEmpty())
  921. {
  922. this.addMenuItems(menu, ['undo', 'redo'], null, evt);
  923. }
  924. };
  925. /**
  926. * Creates the keyboard event handler for the current graph and history.
  927. */
  928. Menus.prototype.addPopupMenuEditItems = function(menu, cell, evt)
  929. {
  930. if (this.editorUi.editor.graph.isSelectionEmpty())
  931. {
  932. this.addMenuItems(menu, ['pasteHere'], null, evt);
  933. }
  934. else
  935. {
  936. this.addMenuItems(menu, ['delete', '-', 'cut', 'copy', '-', 'duplicate'], null, evt);
  937. }
  938. };
  939. /**
  940. * Creates the keyboard event handler for the current graph and history.
  941. */
  942. Menus.prototype.addPopupMenuStyleItems = function(menu, cell, evt)
  943. {
  944. if (this.editorUi.editor.graph.getSelectionCount() == 1)
  945. {
  946. this.addMenuItems(menu, ['-', 'setAsDefaultStyle'], null, evt);
  947. }
  948. else if (this.editorUi.editor.graph.isSelectionEmpty())
  949. {
  950. this.addMenuItems(menu, ['-', 'clearDefaultStyle'], null, evt);
  951. }
  952. };
  953. /**
  954. * Creates the keyboard event handler for the current graph and history.
  955. */
  956. Menus.prototype.addPopupMenuArrangeItems = function(menu, cell, evt)
  957. {
  958. var graph = this.editorUi.editor.graph;
  959. if (!graph.isSelectionEmpty())
  960. {
  961. this.addMenuItems(menu, ['-', 'toFront', 'toBack'], null, evt);
  962. }
  963. if (graph.getSelectionCount() > 1)
  964. {
  965. this.addMenuItems(menu, ['-', 'group'], null, evt);
  966. }
  967. else if (graph.getSelectionCount() == 1 && !graph.getModel().isEdge(cell) &&
  968. !graph.isSwimlane(cell) && graph.getModel().getChildCount(cell) > 0)
  969. {
  970. this.addMenuItems(menu, ['-', 'ungroup'], null, evt);
  971. }
  972. };
  973. /**
  974. * Creates the keyboard event handler for the current graph and history.
  975. */
  976. Menus.prototype.addPopupMenuCellItems = function(menu, cell, evt)
  977. {
  978. var graph = this.editorUi.editor.graph;
  979. cell = graph.getSelectionCell();
  980. var state = graph.view.getState(cell);
  981. menu.addSeparator();
  982. if (state != null)
  983. {
  984. var hasWaypoints = false;
  985. if (graph.getModel().isEdge(cell) && mxUtils.getValue(state.style, mxConstants.STYLE_EDGE, null) != 'entityRelationEdgeStyle' &&
  986. mxUtils.getValue(state.style, mxConstants.STYLE_SHAPE, null) != 'arrow')
  987. {
  988. var handler = graph.selectionCellsHandler.getHandler(cell);
  989. var isWaypoint = false;
  990. if (handler instanceof mxEdgeHandler && handler.bends != null && handler.bends.length > 2)
  991. {
  992. var index = handler.getHandleForEvent(graph.updateMouseEvent(new mxMouseEvent(evt)));
  993. // Configures removeWaypoint action before execution
  994. // Using trigger parameter is cleaner but have to find waypoint here anyway.
  995. var rmWaypointAction = this.editorUi.actions.get('removeWaypoint');
  996. rmWaypointAction.handler = handler;
  997. rmWaypointAction.index = index;
  998. isWaypoint = index > 0 && index < handler.bends.length - 1;
  999. }
  1000. menu.addSeparator();
  1001. this.addMenuItem(menu, 'turn', null, evt, null, mxResources.get('reverse'));
  1002. this.addMenuItems(menu, [(isWaypoint) ? 'removeWaypoint' : 'addWaypoint'], null, evt);
  1003. // Adds reset waypoints option if waypoints exist
  1004. var geo = graph.getModel().getGeometry(cell);
  1005. hasWaypoints = geo != null && geo.points != null && geo.points.length > 0;
  1006. }
  1007. if (graph.getSelectionCount() == 1 && (hasWaypoints || (graph.getModel().isVertex(cell) &&
  1008. graph.getModel().getEdgeCount(cell) > 0)))
  1009. {
  1010. this.addMenuItems(menu, ['-', 'clearWaypoints'], null, evt);
  1011. }
  1012. }
  1013. if (graph.getSelectionCount() == 1)
  1014. {
  1015. this.addMenuItems(menu, ['-', 'editData', 'editLink'], null, evt);
  1016. // Shows edit image action if there is an image in the style
  1017. if (graph.getModel().isVertex(cell) && mxUtils.getValue(state.style, mxConstants.STYLE_IMAGE, null) != null)
  1018. {
  1019. menu.addSeparator();
  1020. this.addMenuItem(menu, 'image', null, evt).firstChild.nextSibling.innerHTML = mxResources.get('editImage') + '...';
  1021. }
  1022. }
  1023. };
  1024. /**
  1025. * Creates the keyboard event handler for the current graph and history.
  1026. */
  1027. Menus.prototype.addPopupMenuSelectionItems = function(menu, cell, evt)
  1028. {
  1029. if (this.editorUi.editor.graph.isSelectionEmpty())
  1030. {
  1031. this.addMenuItems(menu, ['-', 'selectVertices', 'selectEdges', 'selectAll'], null, evt);
  1032. }
  1033. };
  1034. /**
  1035. * Creates the keyboard event handler for the current graph and history.
  1036. */
  1037. Menus.prototype.createMenubar = function(container)
  1038. {
  1039. var menubar = new Menubar(this.editorUi, container);
  1040. var menus = this.defaultMenuItems;
  1041. for (var i = 0; i < menus.length; i++)
  1042. {
  1043. (mxUtils.bind(this, function(menu)
  1044. {
  1045. var elt = menubar.addMenu(mxResources.get(menus[i]), mxUtils.bind(this, function()
  1046. {
  1047. // Allows extensions of menu.funct
  1048. menu.funct.apply(this, arguments);
  1049. }));
  1050. this.menuCreated(menu, elt);
  1051. }))(this.get(menus[i]));
  1052. }
  1053. return menubar;
  1054. };
  1055. /**
  1056. * Creates the keyboard event handler for the current graph and history.
  1057. */
  1058. Menus.prototype.menuCreated = function(menu, elt, className)
  1059. {
  1060. if (elt != null)
  1061. {
  1062. className = (className != null) ? className : 'geItem';
  1063. menu.addListener('stateChanged', function()
  1064. {
  1065. elt.enabled = menu.enabled;
  1066. if (!menu.enabled)
  1067. {
  1068. elt.className = className + ' mxDisabled';
  1069. if (document.documentMode == 8)
  1070. {
  1071. elt.style.color = '#c3c3c3';
  1072. }
  1073. }
  1074. else
  1075. {
  1076. elt.className = className;
  1077. if (document.documentMode == 8)
  1078. {
  1079. elt.style.color = '';
  1080. }
  1081. }
  1082. });
  1083. }
  1084. };
  1085. /**
  1086. * Construcs a new menubar for the given editor.
  1087. */
  1088. function Menubar(editorUi, container)
  1089. {
  1090. this.editorUi = editorUi;
  1091. this.container = container;
  1092. };
  1093. /**
  1094. * Adds the menubar elements.
  1095. */
  1096. Menubar.prototype.hideMenu = function()
  1097. {
  1098. this.editorUi.hideCurrentMenu();
  1099. };
  1100. /**
  1101. * Adds a submenu to this menubar.
  1102. */
  1103. Menubar.prototype.addMenu = function(label, funct, before)
  1104. {
  1105. var elt = document.createElement('a');
  1106. elt.className = 'geItem';
  1107. mxUtils.write(elt, label);
  1108. this.addMenuHandler(elt, funct);
  1109. if (before != null)
  1110. {
  1111. this.container.insertBefore(elt, before);
  1112. }
  1113. else
  1114. {
  1115. this.container.appendChild(elt);
  1116. }
  1117. return elt;
  1118. };
  1119. /**
  1120. * Adds a handler for showing a menu in the given element.
  1121. */
  1122. Menubar.prototype.addMenuHandler = function(elt, funct)
  1123. {
  1124. if (funct != null)
  1125. {
  1126. var show = true;
  1127. var clickHandler = mxUtils.bind(this, function(evt)
  1128. {
  1129. if (show && elt.enabled == null || elt.enabled)
  1130. {
  1131. this.editorUi.editor.graph.popupMenuHandler.hideMenu();
  1132. var menu = new mxPopupMenu(funct);
  1133. menu.div.className += ' geMenubarMenu';
  1134. menu.smartSeparators = true;
  1135. menu.showDisabled = true;
  1136. menu.autoExpand = true;
  1137. // Disables autoexpand and destroys menu when hidden
  1138. menu.hideMenu = mxUtils.bind(this, function()
  1139. {
  1140. mxPopupMenu.prototype.hideMenu.apply(menu, arguments);
  1141. this.editorUi.resetCurrentMenu();
  1142. menu.destroy();
  1143. });
  1144. var offset = mxUtils.getOffset(elt);
  1145. menu.popup(offset.x, offset.y + elt.offsetHeight, null, evt);
  1146. this.editorUi.setCurrentMenu(menu, elt);
  1147. }
  1148. mxEvent.consume(evt);
  1149. });
  1150. // Shows menu automatically while in expanded state
  1151. mxEvent.addListener(elt, 'mousemove', mxUtils.bind(this, function(evt)
  1152. {
  1153. if (this.editorUi.currentMenu != null && this.editorUi.currentMenuElt != elt)
  1154. {
  1155. this.editorUi.hideCurrentMenu();
  1156. clickHandler(evt);
  1157. }
  1158. }));
  1159. // Hides menu if already showing and prevents focus
  1160. mxEvent.addListener(elt, (mxClient.IS_POINTER) ? 'pointerdown' : 'mousedown',
  1161. mxUtils.bind(this, function(evt)
  1162. {
  1163. show = this.currentElt != elt;
  1164. evt.preventDefault();
  1165. }));
  1166. mxEvent.addListener(elt, 'click', mxUtils.bind(this, function(evt)
  1167. {
  1168. clickHandler(evt);
  1169. show = true;
  1170. }));
  1171. }
  1172. };
  1173. /**
  1174. * Creates the keyboard event handler for the current graph and history.
  1175. */
  1176. Menubar.prototype.destroy = function()
  1177. {
  1178. // do nothing
  1179. };
  1180. /**
  1181. * Constructs a new action for the given parameters.
  1182. */
  1183. function Menu(funct, enabled)
  1184. {
  1185. mxEventSource.call(this);
  1186. this.funct = funct;
  1187. this.enabled = (enabled != null) ? enabled : true;
  1188. };
  1189. // Menu inherits from mxEventSource
  1190. mxUtils.extend(Menu, mxEventSource);
  1191. /**
  1192. * Sets the enabled state of the action and fires a stateChanged event.
  1193. */
  1194. Menu.prototype.isEnabled = function()
  1195. {
  1196. return this.enabled;
  1197. };
  1198. /**
  1199. * Sets the enabled state of the action and fires a stateChanged event.
  1200. */
  1201. Menu.prototype.setEnabled = function(value)
  1202. {
  1203. if (this.enabled != value)
  1204. {
  1205. this.enabled = value;
  1206. this.fireEvent(new mxEventObject('stateChanged'));
  1207. }
  1208. };
  1209. /**
  1210. * Sets the enabled state of the action and fires a stateChanged event.
  1211. */
  1212. Menu.prototype.execute = function(menu, parent)
  1213. {
  1214. this.funct(menu, parent);
  1215. };
  1216. /**
  1217. * "Installs" menus in EditorUi.
  1218. */
  1219. EditorUi.prototype.createMenus = function()
  1220. {
  1221. return new Menus(this);
  1222. };