Actions.js 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961
  1. /**
  2. * Copyright (c) 2006-2020, JGraph Ltd
  3. * Copyright (c) 2006-2020, draw.io AG
  4. *
  5. * Constructs the actions object for the given UI.
  6. */
  7. function Actions(editorUi)
  8. {
  9. this.editorUi = editorUi;
  10. this.actions = new Object();
  11. this.init();
  12. };
  13. /**
  14. * Adds the default actions.
  15. */
  16. Actions.prototype.init = function()
  17. {
  18. var ui = this.editorUi;
  19. var editor = ui.editor;
  20. var graph = editor.graph;
  21. var isGraphEnabled = function()
  22. {
  23. return Action.prototype.isEnabled.apply(this, arguments) && graph.isEnabled();
  24. };
  25. // File actions
  26. this.addAction('new...', function() { graph.openLink(ui.getUrl()); });
  27. this.addAction('open...', function()
  28. {
  29. window.openNew = true;
  30. window.openKey = 'open';
  31. ui.openFile();
  32. });
  33. this.put('smartFit', new Action(mxResources.get('fitWindow') + ' / ' + mxResources.get('resetView'), function()
  34. {
  35. graph.popupMenuHandler.hideMenu();
  36. var scale = graph.view.scale;
  37. var sx = graph.container.scrollLeft;
  38. var sy = graph.container.scrollTop;
  39. var tx = graph.view.translate.x;
  40. var ty = graph.view.translate.y;
  41. var thresh = 5;
  42. ui.actions.get('resetView').funct();
  43. // Toggle scale if nothing has changed
  44. if (Math.abs(scale - graph.view.scale) < 0.00001 &&
  45. Math.abs(sx - graph.container.scrollLeft) < thresh &&
  46. Math.abs(sy - graph.container.scrollTop) < thresh &&
  47. tx == graph.view.translate.x &&
  48. ty == graph.view.translate.y)
  49. {
  50. ui.actions.get('fitWindow').funct();
  51. }
  52. }, null, null, 'Enter'));
  53. this.addAction('keyPressEnter', function()
  54. {
  55. if (graph.isEnabled())
  56. {
  57. if (graph.isSelectionEmpty())
  58. {
  59. ui.actions.get('smartFit').funct();
  60. }
  61. else
  62. {
  63. graph.startEditingAtCell();
  64. }
  65. }
  66. });
  67. this.addAction('import...', function()
  68. {
  69. window.openNew = false;
  70. window.openKey = 'import';
  71. // Closes dialog after open
  72. window.openFile = new OpenFile(mxUtils.bind(this, function()
  73. {
  74. ui.hideDialog();
  75. }));
  76. window.openFile.setConsumer(mxUtils.bind(this, function(xml, filename)
  77. {
  78. try
  79. {
  80. var doc = mxUtils.parseXml(xml);
  81. editor.graph.setSelectionCells(editor.graph.importGraphModel(doc.documentElement));
  82. }
  83. catch (e)
  84. {
  85. mxUtils.alert(mxResources.get('invalidOrMissingFile') + ': ' + e.message);
  86. }
  87. }));
  88. // Removes openFile if dialog is closed
  89. ui.showDialog(new OpenDialog(this).container, 320, 220, true, true, function()
  90. {
  91. window.openFile = null;
  92. });
  93. }).isEnabled = isGraphEnabled;
  94. this.addAction('save', function() { ui.saveFile(false); }, null, null, Editor.ctrlKey + '+S').isEnabled = isGraphEnabled;
  95. this.addAction('saveAs...', function() { ui.saveFile(true); }, null, null, Editor.ctrlKey + '+Shift+S').isEnabled = isGraphEnabled;
  96. this.addAction('export...', function() { ui.showDialog(new ExportDialog(ui).container, 300, 340, true, true); });
  97. this.addAction('editDiagram...', function()
  98. {
  99. var dlg = new EditDiagramDialog(ui);
  100. ui.showDialog(dlg.container, 620, 420, true, false);
  101. dlg.init();
  102. });
  103. this.addAction('pageSetup...', function() { ui.showDialog(new PageSetupDialog(ui).container, 320, 240, true, true); }).isEnabled = isGraphEnabled;
  104. this.addAction('print...', function() { ui.showDialog(new PrintDialog(ui).container, 300, 180, true, true); }, null, 'sprite-print', Editor.ctrlKey + '+P');
  105. this.addAction('preview', function() { mxUtils.show(graph, null, 10, 10); });
  106. // Edit actions
  107. this.addAction('undo', function() { ui.undo(); }, null, 'sprite-undo', Editor.ctrlKey + '+Z');
  108. this.addAction('redo', function() { ui.redo(); }, null, 'sprite-redo', (!mxClient.IS_WIN) ? Editor.ctrlKey + '+Shift+Z' : Editor.ctrlKey + '+Y');
  109. this.addAction('cut', function()
  110. {
  111. var cells = null;
  112. try
  113. {
  114. cells = ui.copyXml();
  115. if (cells != null)
  116. {
  117. graph.removeCells(cells, false);
  118. }
  119. }
  120. catch (e)
  121. {
  122. // ignore
  123. }
  124. try
  125. {
  126. if (cells == null)
  127. {
  128. mxClipboard.cut(graph);
  129. }
  130. }
  131. catch (e)
  132. {
  133. ui.handleError(e);
  134. }
  135. }, null, 'sprite-cut', Editor.ctrlKey + '+X');
  136. this.addAction('copy', function()
  137. {
  138. try
  139. {
  140. ui.copyXml();
  141. }
  142. catch (e)
  143. {
  144. // ignore
  145. }
  146. try
  147. {
  148. mxClipboard.copy(graph);
  149. }
  150. catch (e)
  151. {
  152. ui.handleError(e);
  153. }
  154. }, null, 'sprite-copy', Editor.ctrlKey + '+C');
  155. this.addAction('paste', function()
  156. {
  157. if (graph.isEnabled() && !graph.isCellLocked(graph.getDefaultParent()))
  158. {
  159. var done = false;
  160. try
  161. {
  162. if (Editor.enableNativeCipboard)
  163. {
  164. ui.readGraphModelFromClipboard(function(xml)
  165. {
  166. if (xml != null)
  167. {
  168. graph.getModel().beginUpdate();
  169. try
  170. {
  171. ui.pasteXml(xml, true);
  172. }
  173. finally
  174. {
  175. graph.getModel().endUpdate();
  176. }
  177. }
  178. else
  179. {
  180. mxClipboard.paste(graph);
  181. }
  182. })
  183. done = true;
  184. }
  185. }
  186. catch (e)
  187. {
  188. // ignore
  189. }
  190. if (!done)
  191. {
  192. mxClipboard.paste(graph);
  193. }
  194. }
  195. }, false, 'sprite-paste', Editor.ctrlKey + '+V');
  196. this.addAction('pasteHere', function(evt)
  197. {
  198. function pasteCellsHere(cells)
  199. {
  200. if (cells != null)
  201. {
  202. var includeEdges = true;
  203. for (var i = 0; i < cells.length && includeEdges; i++)
  204. {
  205. includeEdges = includeEdges && graph.model.isEdge(cells[i]);
  206. }
  207. var t = graph.view.translate;
  208. var s = graph.view.scale;
  209. var dx = t.x;
  210. var dy = t.y;
  211. var bb = null;
  212. if (cells.length == 1 && includeEdges)
  213. {
  214. var geo = graph.getCellGeometry(cells[0]);
  215. if (geo != null)
  216. {
  217. bb = geo.getTerminalPoint(true);
  218. }
  219. }
  220. bb = (bb != null) ? bb : graph.getBoundingBoxFromGeometry(cells, includeEdges);
  221. if (bb != null)
  222. {
  223. var x = Math.round(graph.snap(graph.popupMenuHandler.triggerX / s - dx));
  224. var y = Math.round(graph.snap(graph.popupMenuHandler.triggerY / s - dy));
  225. graph.cellsMoved(cells, x - bb.x, y - bb.y);
  226. }
  227. }
  228. };
  229. function fallback()
  230. {
  231. graph.getModel().beginUpdate();
  232. try
  233. {
  234. pasteCellsHere(mxClipboard.paste(graph));
  235. }
  236. finally
  237. {
  238. graph.getModel().endUpdate();
  239. }
  240. };
  241. if (graph.isEnabled() && !graph.isCellLocked(graph.getDefaultParent()))
  242. {
  243. var done = false;
  244. try
  245. {
  246. if (Editor.enableNativeCipboard)
  247. {
  248. ui.readGraphModelFromClipboard(function(xml)
  249. {
  250. if (xml != null)
  251. {
  252. graph.getModel().beginUpdate();
  253. try
  254. {
  255. pasteCellsHere(ui.pasteXml(xml, true));
  256. }
  257. finally
  258. {
  259. graph.getModel().endUpdate();
  260. }
  261. }
  262. else
  263. {
  264. fallback();
  265. }
  266. })
  267. done = true;
  268. }
  269. }
  270. catch (e)
  271. {
  272. // ignore
  273. }
  274. if (!done)
  275. {
  276. fallback();
  277. }
  278. }
  279. });
  280. this.addAction('copySize', function()
  281. {
  282. var cell = graph.getSelectionCell();
  283. if (graph.isEnabled() && cell != null && graph.getModel().isVertex(cell))
  284. {
  285. var geo = graph.getCellGeometry(cell);
  286. if (geo != null)
  287. {
  288. ui.copiedSize = new mxRectangle(geo.x, geo.y, geo.width, geo.height);
  289. }
  290. }
  291. }, null, null, 'Alt+Shift+F');
  292. this.addAction('pasteSize', function()
  293. {
  294. if (graph.isEnabled() && !graph.isSelectionEmpty() && ui.copiedSize != null)
  295. {
  296. graph.getModel().beginUpdate();
  297. try
  298. {
  299. var cells = graph.getResizableCells(graph.getSelectionCells());
  300. for (var i = 0; i < cells.length; i++)
  301. {
  302. if (graph.getModel().isVertex(cells[i]))
  303. {
  304. var geo = graph.getCellGeometry(cells[i]);
  305. if (geo != null)
  306. {
  307. geo = geo.clone();
  308. geo.width = ui.copiedSize.width;
  309. geo.height = ui.copiedSize.height;
  310. graph.getModel().setGeometry(cells[i], geo);
  311. }
  312. }
  313. }
  314. }
  315. finally
  316. {
  317. graph.getModel().endUpdate();
  318. }
  319. }
  320. }, null, null, 'Alt+Shift+V');
  321. this.addAction('copyData', function()
  322. {
  323. var cell = graph.getSelectionCell() || graph.getModel().getRoot();
  324. if (graph.isEnabled() && cell != null)
  325. {
  326. var value = cell.cloneValue();
  327. if (value != null && !isNaN(value.nodeType))
  328. {
  329. ui.copiedValue = value;
  330. }
  331. }
  332. }, null, null, 'Alt+Shift+B');
  333. this.addAction('pasteData', function(evt, trigger)
  334. {
  335. // Context menu click uses trigger, toolbar menu click uses evt
  336. var evt = (trigger != null) ? trigger : evt;
  337. var model = graph.getModel();
  338. function applyValue(cell, value)
  339. {
  340. var old = model.getValue(cell);
  341. value = cell.cloneValue(value);
  342. value.removeAttribute('placeholders');
  343. // Carries over placeholders and label properties
  344. if (old != null && !isNaN(old.nodeType))
  345. {
  346. value.setAttribute('placeholders', old.getAttribute('placeholders'));
  347. }
  348. if (evt == null || !mxEvent.isShiftDown(evt))
  349. {
  350. value.setAttribute('label', graph.convertValueToString(cell));
  351. }
  352. model.setValue(cell, value);
  353. };
  354. if (graph.isEnabled() && !graph.isSelectionEmpty() && ui.copiedValue != null)
  355. {
  356. model.beginUpdate();
  357. try
  358. {
  359. var cells = graph.getEditableCells(graph.getSelectionCells());
  360. if (cells.length == 0)
  361. {
  362. applyValue(model.getRoot(), ui.copiedValue);
  363. }
  364. else
  365. {
  366. for (var i = 0; i < cells.length; i++)
  367. {
  368. applyValue(cells[i], ui.copiedValue);
  369. }
  370. }
  371. }
  372. finally
  373. {
  374. model.endUpdate();
  375. }
  376. }
  377. }, null, null, 'Alt+Shift+E');
  378. function deleteCells(includeEdges)
  379. {
  380. // Cancels interactive operations
  381. graph.escape();
  382. var select = graph.deleteCells(graph.getDeletableCells(graph.getSelectionCells()), includeEdges);
  383. if (select != null)
  384. {
  385. graph.setSelectionCells(select);
  386. }
  387. };
  388. function deleteLabels()
  389. {
  390. if (!graph.isSelectionEmpty())
  391. {
  392. graph.getModel().beginUpdate();
  393. try
  394. {
  395. var cells = graph.getSelectionCells();
  396. for (var i = 0; i < cells.length; i++)
  397. {
  398. graph.cellLabelChanged(cells[i], '');
  399. }
  400. }
  401. finally
  402. {
  403. graph.getModel().endUpdate();
  404. }
  405. }
  406. };
  407. this.addAction('delete', function(evt, trigger)
  408. {
  409. // Context menu click uses trigger, toolbar menu click uses evt
  410. var evt = (trigger != null) ? trigger : evt;
  411. if (evt != null && mxEvent.isShiftDown(evt))
  412. {
  413. deleteLabels();
  414. }
  415. else
  416. {
  417. deleteCells(evt != null && (mxEvent.isControlDown(evt) ||
  418. mxEvent.isMetaDown(evt) || mxEvent.isAltDown(evt)));
  419. }
  420. }, null, null, 'Delete');
  421. this.addAction('deleteAll', function()
  422. {
  423. deleteCells(true);
  424. });
  425. this.addAction('deleteLabels', function()
  426. {
  427. deleteLabels();
  428. }, null, null, Editor.ctrlKey + '+Delete');
  429. this.addAction('duplicate', function()
  430. {
  431. try
  432. {
  433. graph.setSelectionCells(graph.duplicateCells());
  434. graph.scrollCellToVisible(graph.getSelectionCell());
  435. }
  436. catch (e)
  437. {
  438. ui.handleError(e);
  439. }
  440. }, null, null, Editor.ctrlKey + '+D');
  441. this.put('mergeCells', new Action(mxResources.get('merge'), function()
  442. {
  443. var ss = ui.getSelectionState();
  444. if (ss.mergeCell != null)
  445. {
  446. graph.getModel().beginUpdate();
  447. try
  448. {
  449. graph.setCellStyles('rowspan', ss.rowspan, [ss.mergeCell]);
  450. graph.setCellStyles('colspan', ss.colspan, [ss.mergeCell]);
  451. }
  452. finally
  453. {
  454. graph.getModel().endUpdate();
  455. }
  456. }
  457. }));
  458. this.put('unmergeCells', new Action(mxResources.get('unmerge'), function()
  459. {
  460. var ss = ui.getSelectionState();
  461. if (ss.cells.length > 0)
  462. {
  463. graph.getModel().beginUpdate();
  464. try
  465. {
  466. graph.setCellStyles('rowspan', null, ss.cells);
  467. graph.setCellStyles('colspan', null, ss.cells);
  468. }
  469. finally
  470. {
  471. graph.getModel().endUpdate();
  472. }
  473. }
  474. }));
  475. this.put('turn', new Action(mxResources.get('turn') + ' / ' + mxResources.get('reverse'), function(evt, trigger)
  476. {
  477. // Context menu click uses trigger, toolbar menu click uses evt
  478. var evt = (trigger != null) ? trigger : evt;
  479. graph.turnShapes(graph.getResizableCells(graph.getSelectionCells()),
  480. (evt != null) ? mxEvent.isShiftDown(evt) : false);
  481. }, null, null, (mxClient.IS_SF) ? null : Editor.ctrlKey + '+R'));
  482. this.put('selectConnections', new Action(mxResources.get('selectEdges'), function(evt)
  483. {
  484. var cell = graph.getSelectionCell();
  485. if (graph.isEnabled() && cell != null)
  486. {
  487. graph.addSelectionCells(graph.getEdges(cell));
  488. }
  489. }));
  490. this.addAction('selectVertices', function() { graph.selectVertices(null, true); }, null, null, Editor.ctrlKey + '+Shift+I');
  491. this.addAction('selectEdges', function() { graph.selectEdges(); }, null, null, Editor.ctrlKey + '+Shift+E');
  492. this.addAction('selectAll', function() { graph.selectAll(null, true); }, null, null, Editor.ctrlKey + '+A');
  493. this.addAction('selectNone', function() { graph.clearSelection(); }, null, null, Editor.ctrlKey + '+Shift+A');
  494. this.addAction('lockUnlock', function()
  495. {
  496. if (!graph.isSelectionEmpty())
  497. {
  498. graph.getModel().beginUpdate();
  499. try
  500. {
  501. var cells = graph.getSelectionCells();
  502. var style = graph.getCurrentCellStyle(graph.getSelectionCell());
  503. var value = (mxUtils.getValue(style, mxConstants.STYLE_EDITABLE, 1)) == 1 ? 0 : 1;
  504. graph.setCellStyles(mxConstants.STYLE_MOVABLE, value, cells);
  505. graph.setCellStyles(mxConstants.STYLE_RESIZABLE, value, cells);
  506. graph.setCellStyles(mxConstants.STYLE_ROTATABLE, value, cells);
  507. graph.setCellStyles(mxConstants.STYLE_DELETABLE, value, cells);
  508. graph.setCellStyles(mxConstants.STYLE_EDITABLE, value, cells);
  509. graph.setCellStyles('locked', (value == 1) ? 0 : 1, cells);
  510. graph.setCellStyles('connectable', value, cells);
  511. }
  512. finally
  513. {
  514. graph.getModel().endUpdate();
  515. }
  516. }
  517. }, null, null, Editor.ctrlKey + '+L');
  518. // Navigation actions
  519. this.addAction('home', function() { graph.home(); }, null, null, 'Shift+Home');
  520. this.addAction('exitGroup', function() { graph.exitGroup(); }, null, null, Editor.ctrlKey + '+Shift+Home');
  521. this.addAction('enterGroup', function() { graph.enterGroup(); }, null, null, Editor.ctrlKey + '+Shift+End');
  522. this.addAction('collapse', function() { graph.foldCells(true); }, null, null, Editor.ctrlKey + '+Home');
  523. this.addAction('expand', function() { graph.foldCells(false); }, null, null, Editor.ctrlKey + '+End');
  524. // Arrange actions
  525. this.addAction('toFront', function()
  526. {
  527. graph.orderCells(false);
  528. }, null, null, Editor.ctrlKey + '+Shift+F');
  529. this.addAction('toBack', function()
  530. {
  531. graph.orderCells(true);
  532. }, null, null, Editor.ctrlKey + '+Shift+B');
  533. this.addAction('bringForward', function(evt)
  534. {
  535. graph.orderCells(false, null, true);
  536. });
  537. this.addAction('sendBackward', function(evt)
  538. {
  539. graph.orderCells(true, null, true);
  540. });
  541. this.addAction('group', function()
  542. {
  543. if (graph.isEnabled())
  544. {
  545. var cells = mxUtils.sortCells(graph.getSelectionCells(), true);
  546. if (cells.length == 1 && !graph.isTable(cells[0]) && !graph.isTableRow(cells[0]))
  547. {
  548. graph.setCellStyles('container', '1');
  549. }
  550. else
  551. {
  552. cells = graph.getCellsForGroup(cells);
  553. if (cells.length > 1)
  554. {
  555. graph.setSelectionCell(graph.groupCells(null, 0, cells));
  556. }
  557. }
  558. }
  559. }, null, null, Editor.ctrlKey + '+G');
  560. this.addAction('ungroup', function()
  561. {
  562. if (graph.isEnabled())
  563. {
  564. var cells = graph.getEditableCells(graph.getSelectionCells());
  565. graph.model.beginUpdate();
  566. try
  567. {
  568. var temp = graph.ungroupCells();
  569. // Clears container flag for remaining cells
  570. if (cells != null)
  571. {
  572. for (var i = 0; i < cells.length; i++)
  573. {
  574. if (graph.model.contains(cells[i]))
  575. {
  576. if (graph.model.getChildCount(cells[i]) == 0 &&
  577. graph.model.isVertex(cells[i]))
  578. {
  579. graph.setCellStyles('container', '0', [cells[i]]);
  580. }
  581. temp.push(cells[i]);
  582. }
  583. }
  584. }
  585. }
  586. finally
  587. {
  588. graph.model.endUpdate();
  589. }
  590. if (temp.length > 0)
  591. {
  592. graph.setSelectionCells(temp);
  593. }
  594. }
  595. }, null, null, Editor.ctrlKey + '+Shift+U');
  596. this.addAction('removeFromGroup', function()
  597. {
  598. if (graph.isEnabled())
  599. {
  600. var cells = graph.getSelectionCells();
  601. // Removes table rows and cells
  602. if (cells != null)
  603. {
  604. var temp = [];
  605. for (var i = 0; i < cells.length; i++)
  606. {
  607. if (!graph.isTableRow(cells[i]) &&
  608. !graph.isTableCell(cells[i]))
  609. {
  610. temp.push(cells[i]);
  611. }
  612. }
  613. graph.removeCellsFromParent(temp);
  614. }
  615. }
  616. });
  617. // Adds action
  618. this.addAction('edit', function()
  619. {
  620. if (graph.isEnabled())
  621. {
  622. graph.startEditingAtCell();
  623. }
  624. }, null, null, 'F2/Enter');
  625. this.addAction('editData...', function()
  626. {
  627. var cell = graph.getSelectionCell() || graph.getModel().getRoot();
  628. ui.showDataDialog(cell);
  629. }, null, null, Editor.ctrlKey + '+M');
  630. this.addAction('editTooltip...', function()
  631. {
  632. var cell = graph.getSelectionCell();
  633. if (graph.isEnabled() && cell != null && graph.isCellEditable(cell))
  634. {
  635. var tooltip = '';
  636. if (mxUtils.isNode(cell.value))
  637. {
  638. var tmp = null;
  639. if (Graph.translateDiagram && Graph.diagramLanguage != null &&
  640. cell.value.hasAttribute('tooltip_' + Graph.diagramLanguage))
  641. {
  642. tmp = cell.value.getAttribute('tooltip_' + Graph.diagramLanguage);
  643. }
  644. if (tmp == null)
  645. {
  646. tmp = cell.value.getAttribute('tooltip');
  647. }
  648. if (tmp != null)
  649. {
  650. tooltip = tmp;
  651. }
  652. }
  653. var dlg = new TextareaDialog(ui, mxResources.get('editTooltip') + ':', tooltip, function(newValue)
  654. {
  655. graph.setTooltipForCell(cell, newValue);
  656. });
  657. ui.showDialog(dlg.container, 320, 200, true, true);
  658. dlg.init();
  659. }
  660. }, null, null, 'Alt+Shift+T');
  661. this.addAction('openLink', function()
  662. {
  663. var link = graph.getLinkForCell(graph.getSelectionCell());
  664. if (link != null)
  665. {
  666. graph.openLink(link);
  667. }
  668. });
  669. this.addAction('editLink...', function()
  670. {
  671. var cell = graph.getSelectionCell();
  672. if (graph.isEnabled() && cell != null && graph.isCellEditable(cell))
  673. {
  674. var value = graph.getLinkForCell(cell) || '';
  675. ui.showLinkDialog(value, mxResources.get('apply'), function(link, docs, linkTarget)
  676. {
  677. link = mxUtils.trim(link);
  678. graph.setLinkForCell(cell, (link.length > 0) ? link : null);
  679. graph.setAttributeForCell(cell, 'linkTarget', linkTarget);
  680. }, true, graph.getLinkTargetForCell(cell));
  681. }
  682. }, null, null, 'Alt+Shift+L');
  683. this.put('insertImage', new Action(mxResources.get('image') + '...', function()
  684. {
  685. if (graph.isEnabled() && !graph.isCellLocked(graph.getDefaultParent()))
  686. {
  687. graph.clearSelection();
  688. ui.actions.get('image').funct();
  689. }
  690. })).isEnabled = isGraphEnabled;
  691. this.addAction('editImage...', function()
  692. {
  693. ui.actions.get('image').funct();
  694. });
  695. this.put('insertLink', new Action(mxResources.get('link') + '...', function()
  696. {
  697. if (graph.isEnabled() && !graph.isCellLocked(graph.getDefaultParent()))
  698. {
  699. ui.showLinkDialog('', mxResources.get('insert'), function(link, docs, linkTarget)
  700. {
  701. link = mxUtils.trim(link);
  702. if (link.length > 0)
  703. {
  704. var icon = null;
  705. var title = graph.getLinkTitle(link);
  706. if (docs != null && docs.length > 0)
  707. {
  708. icon = docs[0].iconUrl;
  709. title = docs[0].name || docs[0].type;
  710. title = title.charAt(0).toUpperCase() + title.substring(1);
  711. if (title.length > 30)
  712. {
  713. title = title.substring(0, 30) + '...';
  714. }
  715. }
  716. var linkCell = new mxCell(title, new mxGeometry(0, 0, 100, 40),
  717. 'fontColor=#0000EE;fontStyle=4;rounded=1;overflow=hidden;' + ((icon != null) ?
  718. 'shape=label;imageWidth=16;imageHeight=16;spacingLeft=26;align=left;image=' + icon :
  719. 'spacing=10;'));
  720. linkCell.vertex = true;
  721. var pt = graph.getCenterInsertPoint(graph.getBoundingBoxFromGeometry([linkCell], true));
  722. linkCell.geometry.x = pt.x;
  723. linkCell.geometry.y = pt.y;
  724. graph.setAttributeForCell(linkCell, 'linkTarget', linkTarget);
  725. graph.setLinkForCell(linkCell, link);
  726. graph.cellSizeUpdated(linkCell, true);
  727. graph.getModel().beginUpdate();
  728. try
  729. {
  730. linkCell = graph.addCell(linkCell);
  731. graph.fireEvent(new mxEventObject('cellsInserted', 'cells', [linkCell]));
  732. }
  733. finally
  734. {
  735. graph.getModel().endUpdate();
  736. }
  737. graph.setSelectionCell(linkCell);
  738. graph.scrollCellToVisible(graph.getSelectionCell());
  739. }
  740. }, true);
  741. }
  742. }, null, null, 'L')).isEnabled = isGraphEnabled;
  743. this.addAction('link...', mxUtils.bind(this, function()
  744. {
  745. if (graph.isEnabled())
  746. {
  747. if (graph.cellEditor.isContentEditing())
  748. {
  749. var elt = graph.getSelectedElement();
  750. var link = graph.getParentByName(elt, 'A', graph.cellEditor.textarea);
  751. var oldValue = '';
  752. // Workaround for FF returning the outermost selected element after double
  753. // click on a DOM hierarchy with a link inside (but not as topmost element)
  754. if (link == null && elt != null && elt.getElementsByTagName != null)
  755. {
  756. // Finds all links in the selected DOM and uses the link
  757. // where the selection text matches its text content
  758. var links = elt.getElementsByTagName('a');
  759. for (var i = 0; i < links.length && link == null; i++)
  760. {
  761. if (links[i].textContent == elt.textContent)
  762. {
  763. link = links[i];
  764. }
  765. }
  766. }
  767. if (link != null && link.nodeName == 'A')
  768. {
  769. oldValue = link.getAttribute('href') || '';
  770. graph.selectNode(link);
  771. }
  772. var selState = graph.cellEditor.saveSelection();
  773. ui.showLinkDialog(oldValue, mxResources.get('apply'), mxUtils.bind(this, function(value)
  774. {
  775. graph.cellEditor.restoreSelection(selState);
  776. if (value != null)
  777. {
  778. graph.insertLink(value);
  779. }
  780. }));
  781. }
  782. else if (graph.isSelectionEmpty())
  783. {
  784. this.get('insertLink').funct();
  785. }
  786. else
  787. {
  788. this.get('editLink').funct();
  789. }
  790. }
  791. })).isEnabled = isGraphEnabled;
  792. this.addAction('autosize', function()
  793. {
  794. var cells = graph.getSelectionCells();
  795. if (cells != null)
  796. {
  797. graph.getModel().beginUpdate();
  798. try
  799. {
  800. for (var i = 0; i < cells.length; i++)
  801. {
  802. var cell = cells[i];
  803. if (graph.getModel().isVertex(cell))
  804. {
  805. if (graph.getModel().getChildCount(cell) > 0)
  806. {
  807. graph.updateGroupBounds([cell], 0, true);
  808. }
  809. else
  810. {
  811. graph.updateCellSize(cell);
  812. }
  813. }
  814. }
  815. }
  816. finally
  817. {
  818. graph.getModel().endUpdate();
  819. }
  820. }
  821. }, null, null, Editor.ctrlKey + '+Shift+Y');
  822. this.addAction('snapToGrid', function()
  823. {
  824. graph.snapCellsToGrid(graph.getSelectionCells(), graph.gridSize);
  825. });
  826. this.addAction('formattedText', function()
  827. {
  828. graph.stopEditing();
  829. var style = graph.getCommonStyle(graph.getSelectionCells());
  830. var value = (mxUtils.getValue(style, 'html', '0') == '1') ? null : '1';
  831. graph.getModel().beginUpdate();
  832. try
  833. {
  834. var cells = graph.getEditableCells(graph.getSelectionCells());
  835. for (var i = 0; i < cells.length; i++)
  836. {
  837. state = graph.getView().getState(cells[i]);
  838. if (state != null)
  839. {
  840. var html = mxUtils.getValue(state.style, 'html', '0');
  841. if (html == '1' && value == null)
  842. {
  843. graph.removeTextStyleForCell(state.cell);
  844. graph.setCellStyles('html', value, [cells[i]]);
  845. }
  846. else if (html == '0' && value == '1')
  847. {
  848. // Converts HTML tags to text
  849. var label = mxUtils.htmlEntities(graph.convertValueToString(state.cell), false);
  850. if (mxUtils.getValue(state.style, 'nl2Br', '1') != '0')
  851. {
  852. // Converts newlines in plain text to breaks in HTML
  853. // to match the plain text output
  854. label = label.replace(/\n/g, '<br/>');
  855. }
  856. graph.cellLabelChanged(state.cell, Graph.sanitizeHtml(label));
  857. graph.setCellStyles('html', value, [cells[i]]);
  858. }
  859. }
  860. }
  861. ui.fireEvent(new mxEventObject('styleChanged', 'keys', ['html'],
  862. 'values', [(value != null) ? value : '0'], 'cells', cells));
  863. }
  864. finally
  865. {
  866. graph.getModel().endUpdate();
  867. }
  868. });
  869. this.addAction('wordWrap', function()
  870. {
  871. var state = graph.getView().getState(graph.getSelectionCell());
  872. var value = 'wrap';
  873. graph.stopEditing();
  874. if (state != null && state.style[mxConstants.STYLE_WHITE_SPACE] == 'wrap')
  875. {
  876. value = null;
  877. }
  878. graph.setCellStyles(mxConstants.STYLE_WHITE_SPACE, value);
  879. });
  880. this.addAction('rotation', function()
  881. {
  882. var value = '0';
  883. var state = graph.getView().getState(graph.getSelectionCell());
  884. if (state != null)
  885. {
  886. value = state.style[mxConstants.STYLE_ROTATION] || value;
  887. }
  888. var dlg = new FilenameDialog(ui, value, mxResources.get('apply'), function(newValue)
  889. {
  890. if (newValue != null && newValue.length > 0)
  891. {
  892. graph.setCellStyles(mxConstants.STYLE_ROTATION, newValue);
  893. }
  894. }, mxResources.get('enterValue') + ' (' + mxResources.get('rotation') + ' 0-360)');
  895. ui.showDialog(dlg.container, 375, 80, true, true);
  896. dlg.init();
  897. });
  898. // View actions
  899. this.addAction('resetView', function()
  900. {
  901. graph.zoomTo(1);
  902. ui.resetScrollbars();
  903. }, null, null, 'Enter/Home');
  904. this.addAction('zoomIn', function(evt)
  905. {
  906. if (graph.isFastZoomEnabled())
  907. {
  908. graph.lazyZoom(true, true, ui.buttonZoomDelay);
  909. }
  910. else
  911. {
  912. graph.zoomIn();
  913. }
  914. }, null, null, Editor.ctrlKey + ' + (Numpad) / Alt+Mousewheel');
  915. this.addAction('zoomOut', function(evt)
  916. {
  917. if (graph.isFastZoomEnabled())
  918. {
  919. graph.lazyZoom(false, true, ui.buttonZoomDelay);
  920. }
  921. else
  922. {
  923. graph.zoomOut();
  924. }
  925. }, null, null, Editor.ctrlKey + ' - (Numpad) / Alt+Mousewheel');
  926. this.addAction('fitWindow', function()
  927. {
  928. if (graph.pageVisible && graph.isSelectionEmpty())
  929. {
  930. graph.fitPages();
  931. }
  932. else
  933. {
  934. ui.fitDiagramToWindow();
  935. }
  936. }, null, null, Editor.ctrlKey + '+Shift+H');
  937. this.addAction('fitPage', mxUtils.bind(this, function()
  938. {
  939. if (graph.pageVisible)
  940. {
  941. graph.fitPages(1);
  942. }
  943. else
  944. {
  945. this.get('pageView').funct();
  946. }
  947. }), null, null, Editor.ctrlKey + '+J');
  948. this.addAction('fitTwoPages', mxUtils.bind(this, function()
  949. {
  950. if (graph.pageVisible)
  951. {
  952. graph.fitPages(2);
  953. }
  954. else
  955. {
  956. this.get('pageView').funct();
  957. }
  958. }), null, null, Editor.ctrlKey + '+Shift+J');
  959. this.addAction('fitPageWidth', mxUtils.bind(this, function()
  960. {
  961. if (graph.pageVisible)
  962. {
  963. graph.fitPages(1, true);
  964. }
  965. else
  966. {
  967. this.get('pageView').funct();
  968. }
  969. }));
  970. this.put('customZoom', new Action(mxResources.get('custom') + '...', mxUtils.bind(this, function()
  971. {
  972. var dlg = new FilenameDialog(this.editorUi, parseInt(graph.getView().getScale() * 100), mxResources.get('apply'), mxUtils.bind(this, function(newValue)
  973. {
  974. var val = parseInt(newValue);
  975. if (!isNaN(val) && val > 0)
  976. {
  977. graph.zoomTo(val / 100);
  978. }
  979. }), mxResources.get('zoom') + ' (%)');
  980. this.editorUi.showDialog(dlg.container, 300, 80, true, true);
  981. dlg.init();
  982. }), null, null, Editor.ctrlKey + '+0'));
  983. this.addAction('pageScale...', mxUtils.bind(this, function()
  984. {
  985. var dlg = new FilenameDialog(this.editorUi, parseInt(graph.pageScale * 100), mxResources.get('apply'), mxUtils.bind(this, function(newValue)
  986. {
  987. var val = parseInt(newValue);
  988. if (!isNaN(val) && val > 0)
  989. {
  990. var change = new ChangePageSetup(ui, null, null, null, val / 100);
  991. change.ignoreColor = true;
  992. change.ignoreImage = true;
  993. graph.model.execute(change);
  994. }
  995. }), mxResources.get('pageScale') + ' (%)');
  996. this.editorUi.showDialog(dlg.container, 300, 80, true, true);
  997. dlg.init();
  998. }));
  999. // Option actions
  1000. var action = null;
  1001. action = this.addAction('grid', function()
  1002. {
  1003. graph.setGridEnabled(!graph.isGridEnabled());
  1004. graph.defaultGridEnabled = graph.isGridEnabled();
  1005. ui.fireEvent(new mxEventObject('gridEnabledChanged'));
  1006. }, null, null, Editor.ctrlKey + '+Shift+G');
  1007. action.setToggleAction(true);
  1008. action.setSelectedCallback(function() { return graph.isGridEnabled(); });
  1009. action.setEnabled(false);
  1010. action = this.addAction('guides', function()
  1011. {
  1012. graph.graphHandler.guidesEnabled = !graph.graphHandler.guidesEnabled;
  1013. ui.fireEvent(new mxEventObject('guidesEnabledChanged'));
  1014. });
  1015. action.setToggleAction(true);
  1016. action.setSelectedCallback(function() { return graph.graphHandler.guidesEnabled; });
  1017. action.setEnabled(false);
  1018. action = this.addAction('tooltips', function()
  1019. {
  1020. graph.tooltipHandler.setEnabled(!graph.tooltipHandler.isEnabled());
  1021. ui.fireEvent(new mxEventObject('tooltipsEnabledChanged'));
  1022. });
  1023. action.setToggleAction(true);
  1024. action.setSelectedCallback(function() { return graph.tooltipHandler.isEnabled(); });
  1025. action = this.addAction('collapseExpand', function()
  1026. {
  1027. var change = new ChangePageSetup(ui);
  1028. change.ignoreColor = true;
  1029. change.ignoreImage = true;
  1030. change.foldingEnabled = !graph.foldingEnabled;
  1031. graph.model.execute(change);
  1032. });
  1033. action.setToggleAction(true);
  1034. action.setSelectedCallback(function() { return graph.foldingEnabled; });
  1035. action.isEnabled = isGraphEnabled;
  1036. action = this.addAction('pageView', mxUtils.bind(this, function()
  1037. {
  1038. ui.setPageVisible(!graph.pageVisible);
  1039. }));
  1040. action.setToggleAction(true);
  1041. action.setSelectedCallback(function() { return graph.pageVisible; });
  1042. action = this.addAction('connectionArrows', function()
  1043. {
  1044. graph.connectionArrowsEnabled = !graph.connectionArrowsEnabled;
  1045. ui.fireEvent(new mxEventObject('connectionArrowsChanged'));
  1046. }, null, null, 'Alt+Shift+A');
  1047. action.setToggleAction(true);
  1048. action.setSelectedCallback(function() { return graph.connectionArrowsEnabled; });
  1049. action = this.addAction('connectionPoints', function()
  1050. {
  1051. graph.setConnectable(!graph.connectionHandler.isEnabled());
  1052. ui.fireEvent(new mxEventObject('connectionPointsChanged'));
  1053. }, null, null, 'Alt+Shift+O');
  1054. action.setToggleAction(true);
  1055. action.setSelectedCallback(function() { return graph.connectionHandler.isEnabled(); });
  1056. action = this.addAction('copyConnect', function()
  1057. {
  1058. graph.connectionHandler.setCreateTarget(!graph.connectionHandler.isCreateTarget());
  1059. ui.fireEvent(new mxEventObject('copyConnectChanged'));
  1060. });
  1061. action.setToggleAction(true);
  1062. action.setSelectedCallback(function() { return graph.connectionHandler.isCreateTarget(); });
  1063. action.isEnabled = isGraphEnabled;
  1064. action = this.addAction('autosave', function()
  1065. {
  1066. ui.editor.setAutosave(!ui.editor.autosave);
  1067. });
  1068. action.setToggleAction(true);
  1069. action.setSelectedCallback(function() { return ui.editor.autosave; });
  1070. action.isEnabled = isGraphEnabled;
  1071. action.visible = false;
  1072. // Help actions
  1073. this.addAction('help', function()
  1074. {
  1075. var ext = '';
  1076. if (mxResources.isLanguageSupported(mxClient.language))
  1077. {
  1078. ext = '_' + mxClient.language;
  1079. }
  1080. graph.openLink(RESOURCES_PATH + '/help' + ext + '.html');
  1081. });
  1082. var showingAbout = false;
  1083. this.put('about', new Action(mxResources.get('about') + ' Graph Editor...', function()
  1084. {
  1085. if (!showingAbout)
  1086. {
  1087. ui.showDialog(new AboutDialog(ui).container, 320, 280, true, true, function()
  1088. {
  1089. showingAbout = false;
  1090. });
  1091. showingAbout = true;
  1092. }
  1093. }));
  1094. // Font style actions
  1095. var toggleFontStyle = mxUtils.bind(this, function(key, style, fn, shortcut)
  1096. {
  1097. return this.addAction(key, function()
  1098. {
  1099. if (fn != null && graph.cellEditor.isContentEditing())
  1100. {
  1101. fn();
  1102. }
  1103. else
  1104. {
  1105. graph.stopEditing(false);
  1106. graph.getModel().beginUpdate();
  1107. try
  1108. {
  1109. var cells = graph.getEditableCells(graph.getSelectionCells());
  1110. graph.toggleCellStyleFlags(mxConstants.STYLE_FONTSTYLE, style, cells);
  1111. // Removes bold and italic tags and CSS styles inside labels
  1112. if ((style & mxConstants.FONT_BOLD) == mxConstants.FONT_BOLD)
  1113. {
  1114. graph.updateLabelElements(cells, function(elt)
  1115. {
  1116. elt.style.fontWeight = null;
  1117. if (elt.nodeName == 'B')
  1118. {
  1119. graph.replaceElement(elt);
  1120. }
  1121. });
  1122. }
  1123. else if ((style & mxConstants.FONT_ITALIC) == mxConstants.FONT_ITALIC)
  1124. {
  1125. graph.updateLabelElements(cells, function(elt)
  1126. {
  1127. elt.style.fontStyle = null;
  1128. if (elt.nodeName == 'I')
  1129. {
  1130. graph.replaceElement(elt);
  1131. }
  1132. });
  1133. }
  1134. else if ((style & mxConstants.FONT_UNDERLINE) == mxConstants.FONT_UNDERLINE)
  1135. {
  1136. graph.updateLabelElements(cells, function(elt)
  1137. {
  1138. elt.style.textDecoration = null;
  1139. if (elt.nodeName == 'U')
  1140. {
  1141. graph.replaceElement(elt);
  1142. }
  1143. });
  1144. }
  1145. for (var i = 0; i < cells.length; i++)
  1146. {
  1147. if (graph.model.getChildCount(cells[i]) == 0)
  1148. {
  1149. graph.autoSizeCell(cells[i], false);
  1150. }
  1151. }
  1152. }
  1153. finally
  1154. {
  1155. graph.getModel().endUpdate();
  1156. }
  1157. }
  1158. }, null, null, shortcut);
  1159. });
  1160. toggleFontStyle('bold', mxConstants.FONT_BOLD, function() { document.execCommand('bold', false, null); }, Editor.ctrlKey + '+B');
  1161. toggleFontStyle('italic', mxConstants.FONT_ITALIC, function() { document.execCommand('italic', false, null); }, Editor.ctrlKey + '+I');
  1162. toggleFontStyle('underline', mxConstants.FONT_UNDERLINE, function() { document.execCommand('underline', false, null); }, Editor.ctrlKey + '+U');
  1163. // Color actions
  1164. this.addAction('fontColor...', function() { ui.menus.pickColor(mxConstants.STYLE_FONTCOLOR, 'forecolor', '000000'); });
  1165. this.addAction('strokeColor...', function() { ui.menus.pickColor(mxConstants.STYLE_STROKECOLOR); });
  1166. this.addAction('fillColor...', function() { ui.menus.pickColor(mxConstants.STYLE_FILLCOLOR); });
  1167. this.addAction('gradientColor...', function() { ui.menus.pickColor(mxConstants.STYLE_GRADIENTCOLOR); });
  1168. this.addAction('backgroundColor...', function() { ui.menus.pickColor(mxConstants.STYLE_LABEL_BACKGROUNDCOLOR, 'backcolor'); });
  1169. this.addAction('borderColor...', function() { ui.menus.pickColor(mxConstants.STYLE_LABEL_BORDERCOLOR); });
  1170. // Format actions
  1171. this.addAction('removeFormat', function()
  1172. {
  1173. if (graph.isEnabled() && !graph.isSelectionEmpty() && !graph.isEditing())
  1174. {
  1175. graph.getModel().beginUpdate();
  1176. try
  1177. {
  1178. var cells = graph.getSelectionCells();
  1179. for (var i = 0; i < cells.length; i++)
  1180. {
  1181. graph.removeTextStyleForCell(cells[i], true);
  1182. }
  1183. }
  1184. finally
  1185. {
  1186. graph.getModel().endUpdate();
  1187. }
  1188. }
  1189. });
  1190. this.addAction('vertical', function() { ui.menus.toggleStyle(mxConstants.STYLE_HORIZONTAL, true); });
  1191. this.addAction('shadow', function() { ui.menus.toggleStyle(mxConstants.STYLE_SHADOW); });
  1192. this.addAction('solid', function()
  1193. {
  1194. graph.getModel().beginUpdate();
  1195. try
  1196. {
  1197. graph.setCellStyles(mxConstants.STYLE_DASHED, null);
  1198. graph.setCellStyles(mxConstants.STYLE_DASH_PATTERN, null);
  1199. ui.fireEvent(new mxEventObject('styleChanged', 'keys', [mxConstants.STYLE_DASHED, mxConstants.STYLE_DASH_PATTERN],
  1200. 'values', [null, null], 'cells', graph.getSelectionCells()));
  1201. }
  1202. finally
  1203. {
  1204. graph.getModel().endUpdate();
  1205. }
  1206. });
  1207. this.addAction('dashed', function()
  1208. {
  1209. graph.getModel().beginUpdate();
  1210. try
  1211. {
  1212. graph.setCellStyles(mxConstants.STYLE_DASHED, '1');
  1213. graph.setCellStyles(mxConstants.STYLE_DASH_PATTERN, null);
  1214. ui.fireEvent(new mxEventObject('styleChanged', 'keys', [mxConstants.STYLE_DASHED, mxConstants.STYLE_DASH_PATTERN],
  1215. 'values', ['1', null], 'cells', graph.getSelectionCells()));
  1216. }
  1217. finally
  1218. {
  1219. graph.getModel().endUpdate();
  1220. }
  1221. });
  1222. this.addAction('dotted', function()
  1223. {
  1224. graph.getModel().beginUpdate();
  1225. try
  1226. {
  1227. graph.setCellStyles(mxConstants.STYLE_DASHED, '1');
  1228. graph.setCellStyles(mxConstants.STYLE_DASH_PATTERN, '1 4');
  1229. ui.fireEvent(new mxEventObject('styleChanged', 'keys', [mxConstants.STYLE_DASHED, mxConstants.STYLE_DASH_PATTERN],
  1230. 'values', ['1', '1 4'], 'cells', graph.getSelectionCells()));
  1231. }
  1232. finally
  1233. {
  1234. graph.getModel().endUpdate();
  1235. }
  1236. });
  1237. this.addAction('sharp', function()
  1238. {
  1239. graph.getModel().beginUpdate();
  1240. try
  1241. {
  1242. graph.setCellStyles(mxConstants.STYLE_ROUNDED, '0');
  1243. graph.setCellStyles(mxConstants.STYLE_CURVED, '0');
  1244. ui.fireEvent(new mxEventObject('styleChanged', 'keys', [mxConstants.STYLE_ROUNDED, mxConstants.STYLE_CURVED],
  1245. 'values', ['0', '0'], 'cells', graph.getSelectionCells()));
  1246. }
  1247. finally
  1248. {
  1249. graph.getModel().endUpdate();
  1250. }
  1251. });
  1252. this.addAction('rounded', function()
  1253. {
  1254. graph.getModel().beginUpdate();
  1255. try
  1256. {
  1257. graph.setCellStyles(mxConstants.STYLE_ROUNDED, '1');
  1258. graph.setCellStyles(mxConstants.STYLE_CURVED, '0');
  1259. ui.fireEvent(new mxEventObject('styleChanged', 'keys', [mxConstants.STYLE_ROUNDED, mxConstants.STYLE_CURVED],
  1260. 'values', ['1', '0'], 'cells', graph.getSelectionCells()));
  1261. }
  1262. finally
  1263. {
  1264. graph.getModel().endUpdate();
  1265. }
  1266. });
  1267. this.addAction('toggleRounded', function()
  1268. {
  1269. if (!graph.isSelectionEmpty() && graph.isEnabled())
  1270. {
  1271. graph.getModel().beginUpdate();
  1272. try
  1273. {
  1274. var cells = graph.getSelectionCells();
  1275. var style = graph.getCurrentCellStyle(cells[0]);
  1276. var value = (mxUtils.getValue(style, mxConstants.STYLE_ROUNDED, '0') == '1') ? '0' : '1';
  1277. graph.setCellStyles(mxConstants.STYLE_ROUNDED, value);
  1278. graph.setCellStyles(mxConstants.STYLE_CURVED, null);
  1279. ui.fireEvent(new mxEventObject('styleChanged', 'keys', [mxConstants.STYLE_ROUNDED, mxConstants.STYLE_CURVED],
  1280. 'values', [value, '0'], 'cells', graph.getSelectionCells()));
  1281. }
  1282. finally
  1283. {
  1284. graph.getModel().endUpdate();
  1285. }
  1286. }
  1287. });
  1288. this.addAction('curved', function()
  1289. {
  1290. graph.getModel().beginUpdate();
  1291. try
  1292. {
  1293. graph.setCellStyles(mxConstants.STYLE_ROUNDED, '0');
  1294. graph.setCellStyles(mxConstants.STYLE_CURVED, '1');
  1295. ui.fireEvent(new mxEventObject('styleChanged', 'keys', [mxConstants.STYLE_ROUNDED, mxConstants.STYLE_CURVED],
  1296. 'values', ['0', '1'], 'cells', graph.getSelectionCells()));
  1297. }
  1298. finally
  1299. {
  1300. graph.getModel().endUpdate();
  1301. }
  1302. });
  1303. this.addAction('collapsible', function()
  1304. {
  1305. var state = graph.view.getState(graph.getSelectionCell());
  1306. var value = '1';
  1307. if (state != null && graph.getFoldingImage(state) != null)
  1308. {
  1309. value = '0';
  1310. }
  1311. graph.setCellStyles('collapsible', value);
  1312. ui.fireEvent(new mxEventObject('styleChanged', 'keys', ['collapsible'],
  1313. 'values', [value], 'cells', graph.getSelectionCells()));
  1314. });
  1315. this.addAction('editStyle...', mxUtils.bind(this, function()
  1316. {
  1317. var cells = graph.getEditableCells(graph.getSelectionCells());
  1318. if (cells != null && cells.length > 0)
  1319. {
  1320. var model = graph.getModel();
  1321. var dlg = new TextareaDialog(this.editorUi, mxResources.get('editStyle') + ':',
  1322. model.getStyle(cells[0]) || '', function(newValue)
  1323. {
  1324. if (newValue != null)
  1325. {
  1326. graph.setCellStyle(mxUtils.trim(newValue), cells);
  1327. }
  1328. }, null, null, 400, 220);
  1329. this.editorUi.showDialog(dlg.container, 420, 300, true, true);
  1330. dlg.init();
  1331. }
  1332. }), null, null, Editor.ctrlKey + '+E');
  1333. this.addAction('setAsDefaultStyle', function()
  1334. {
  1335. if (graph.isEnabled() && !graph.isSelectionEmpty())
  1336. {
  1337. ui.setDefaultStyle(graph.getSelectionCell());
  1338. }
  1339. }, null, null, Editor.ctrlKey + '+Shift+D');
  1340. this.addAction('clearDefaultStyle', function()
  1341. {
  1342. if (graph.isEnabled())
  1343. {
  1344. ui.clearDefaultStyle();
  1345. }
  1346. }, null, null, Editor.ctrlKey + '+Shift+R');
  1347. this.addAction('addWaypoint', function()
  1348. {
  1349. var cell = graph.getSelectionCell();
  1350. if (cell != null && graph.getModel().isEdge(cell))
  1351. {
  1352. var handler = editor.graph.selectionCellsHandler.getHandler(cell);
  1353. if (handler instanceof mxEdgeHandler)
  1354. {
  1355. var t = graph.view.translate;
  1356. var s = graph.view.scale;
  1357. var dx = t.x;
  1358. var dy = t.y;
  1359. var parent = graph.getModel().getParent(cell);
  1360. var pgeo = graph.getCellGeometry(parent);
  1361. while (graph.getModel().isVertex(parent) && pgeo != null)
  1362. {
  1363. dx += pgeo.x;
  1364. dy += pgeo.y;
  1365. parent = graph.getModel().getParent(parent);
  1366. pgeo = graph.getCellGeometry(parent);
  1367. }
  1368. var x = Math.round(graph.snap(graph.popupMenuHandler.triggerX / s - dx));
  1369. var y = Math.round(graph.snap(graph.popupMenuHandler.triggerY / s - dy));
  1370. handler.addPointAt(handler.state, x, y);
  1371. }
  1372. }
  1373. });
  1374. this.addAction('removeWaypoint', function()
  1375. {
  1376. // TODO: Action should run with "this" set to action
  1377. var rmWaypointAction = ui.actions.get('removeWaypoint');
  1378. if (rmWaypointAction.handler != null)
  1379. {
  1380. // NOTE: Popupevent handled and action updated in Menus.createPopupMenu
  1381. rmWaypointAction.handler.removePoint(rmWaypointAction.handler.state, rmWaypointAction.index);
  1382. }
  1383. });
  1384. this.addAction('clearWaypoints', function(evt, trigger)
  1385. {
  1386. // Context menu click uses trigger, toolbar menu click uses evt
  1387. var evt = (trigger != null) ? trigger : evt;
  1388. var cells = graph.getSelectionCells();
  1389. if (cells != null)
  1390. {
  1391. cells = graph.getEditableCells(graph.addAllEdges(cells));
  1392. graph.getModel().beginUpdate();
  1393. try
  1394. {
  1395. for (var i = 0; i < cells.length; i++)
  1396. {
  1397. var cell = cells[i];
  1398. if (graph.getModel().isEdge(cell))
  1399. {
  1400. var geo = graph.getCellGeometry(cell);
  1401. // Resets fixed connection point
  1402. if (trigger != null && mxEvent.isShiftDown(evt))
  1403. {
  1404. graph.setCellStyles(mxConstants.STYLE_EXIT_X, null, [cell]);
  1405. graph.setCellStyles(mxConstants.STYLE_EXIT_Y, null, [cell]);
  1406. graph.setCellStyles(mxConstants.STYLE_ENTRY_X, null, [cell]);
  1407. graph.setCellStyles(mxConstants.STYLE_ENTRY_Y, null, [cell]);
  1408. }
  1409. else if (geo != null)
  1410. {
  1411. geo = geo.clone();
  1412. geo.points = null;
  1413. geo.x = 0;
  1414. geo.y = 0;
  1415. geo.offset = null;
  1416. graph.getModel().setGeometry(cell, geo);
  1417. }
  1418. }
  1419. }
  1420. }
  1421. finally
  1422. {
  1423. graph.getModel().endUpdate();
  1424. }
  1425. }
  1426. }, null, null, 'Alt+Shift+R');
  1427. action = this.addAction('subscript', mxUtils.bind(this, function()
  1428. {
  1429. if (graph.cellEditor.isContentEditing())
  1430. {
  1431. document.execCommand('subscript', false, null);
  1432. }
  1433. }), null, null, Editor.ctrlKey + '+,');
  1434. action = this.addAction('superscript', mxUtils.bind(this, function()
  1435. {
  1436. if (graph.cellEditor.isContentEditing())
  1437. {
  1438. document.execCommand('superscript', false, null);
  1439. }
  1440. }), null, null, Editor.ctrlKey + '+.');
  1441. action = this.addAction('decreaseFontSize', mxUtils.bind(this, function()
  1442. {
  1443. if (!graph.isSelectionEmpty())
  1444. {
  1445. var style = graph.getCurrentCellStyle(graph.getSelectionCell());
  1446. var size = mxUtils.getValue(style, mxConstants.STYLE_FONTSIZE, mxConstants.DEFAULT_FONTSIZE);
  1447. graph.setCellStyles(mxConstants.STYLE_FONTSIZE, Math.max(1, size - 1),
  1448. graph.getSelectionCells());
  1449. }
  1450. }), null, null, Editor.ctrlKey + '+Shift + (Numpad)');
  1451. action = this.addAction('increaseFontSize', mxUtils.bind(this, function()
  1452. {
  1453. if (!graph.isSelectionEmpty())
  1454. {
  1455. var style = graph.getCurrentCellStyle(graph.getSelectionCell());
  1456. var size = mxUtils.getValue(style, mxConstants.STYLE_FONTSIZE, mxConstants.DEFAULT_FONTSIZE);
  1457. graph.setCellStyles(mxConstants.STYLE_FONTSIZE, Math.min(100, size + 1),
  1458. graph.getSelectionCells());
  1459. }
  1460. }), null, null, Editor.ctrlKey + '+Shift - (Numpad)');
  1461. function applyClipPath(cell, clipPath, width, height, graph)
  1462. {
  1463. graph.getModel().beginUpdate();
  1464. try
  1465. {
  1466. var geo = graph.getCellGeometry(cell);
  1467. if (geo != null && width && height) //Comparing the ratio mostly will fail since it's float
  1468. {
  1469. var scale = width / height;
  1470. geo = geo.clone();
  1471. if (scale > 1)
  1472. {
  1473. geo.height = geo.width / scale;
  1474. }
  1475. else
  1476. {
  1477. geo.width = geo.height * scale;
  1478. }
  1479. graph.getModel().setGeometry(cell, geo);
  1480. }
  1481. graph.setCellStyles(mxConstants.STYLE_CLIP_PATH, clipPath, [cell]); //Set/unset clipPath
  1482. graph.setCellStyles(mxConstants.STYLE_ASPECT, 'fixed', [cell]);
  1483. }
  1484. finally
  1485. {
  1486. graph.getModel().endUpdate();
  1487. }
  1488. };
  1489. this.addAction('image...', function()
  1490. {
  1491. if (graph.isEnabled() && !graph.isCellLocked(graph.getDefaultParent()))
  1492. {
  1493. var title = mxResources.get('image') + ' (' + mxResources.get('url') + '):';
  1494. var state = graph.getView().getState(graph.getSelectionCell());
  1495. var value = '', clipPath = null;
  1496. if (state != null)
  1497. {
  1498. value = state.style[mxConstants.STYLE_IMAGE] || value;
  1499. clipPath = state.style[mxConstants.STYLE_CLIP_PATH] || clipPath;
  1500. }
  1501. var selectionState = graph.cellEditor.saveSelection();
  1502. ui.showImageDialog(title, value, function(newValue, w, h, clipPath, cW, cH)
  1503. {
  1504. // Inserts image into HTML text
  1505. if (graph.cellEditor.isContentEditing())
  1506. {
  1507. graph.cellEditor.restoreSelection(selectionState);
  1508. graph.insertImage(newValue, w, h);
  1509. }
  1510. else
  1511. {
  1512. var cells = graph.getSelectionCells();
  1513. if (newValue != null && (newValue.length > 0 || cells.length > 0))
  1514. {
  1515. var select = null;
  1516. graph.getModel().beginUpdate();
  1517. try
  1518. {
  1519. // Inserts new cell if no cell is selected
  1520. if (cells.length == 0)
  1521. {
  1522. cells = [graph.insertVertex(graph.getDefaultParent(), null, '', 0, 0, w, h,
  1523. 'shape=image;imageAspect=0;aspect=fixed;verticalLabelPosition=bottom;verticalAlign=top;')];
  1524. var pt = graph.getCenterInsertPoint(graph.getBoundingBoxFromGeometry(cells, true));
  1525. cells[0].geometry.x = pt.x;
  1526. cells[0].geometry.y = pt.y;
  1527. if (clipPath != null)
  1528. {
  1529. applyClipPath(cells[0], clipPath, cW, cH, graph);
  1530. }
  1531. select = cells;
  1532. graph.fireEvent(new mxEventObject('cellsInserted', 'cells', select));
  1533. }
  1534. graph.setCellStyles(mxConstants.STYLE_IMAGE, (newValue.length > 0) ? newValue : null, cells);
  1535. // Sets shape only if not already shape with image (label or image)
  1536. var style = graph.getCurrentCellStyle(cells[0]);
  1537. if (style[mxConstants.STYLE_SHAPE] != 'image' && style[mxConstants.STYLE_SHAPE] != 'label')
  1538. {
  1539. graph.setCellStyles(mxConstants.STYLE_SHAPE, 'image', cells);
  1540. }
  1541. else if (newValue.length == 0)
  1542. {
  1543. graph.setCellStyles(mxConstants.STYLE_SHAPE, null, cells);
  1544. }
  1545. if (clipPath == null)
  1546. {
  1547. graph.setCellStyles(mxConstants.STYLE_CLIP_PATH, null, cells); //Reset clip path
  1548. }
  1549. if (w != null && h != null)
  1550. {
  1551. for (var i = 0; i < cells.length; i++)
  1552. {
  1553. var cell = cells[i];
  1554. if (graph.getCurrentCellStyle(cell)['expand'] != '0')
  1555. {
  1556. var geo = graph.getModel().getGeometry(cell);
  1557. if (geo != null)
  1558. {
  1559. geo = geo.clone();
  1560. geo.width = w;
  1561. geo.height = h;
  1562. graph.getModel().setGeometry(cell, geo);
  1563. }
  1564. }
  1565. if (clipPath != null)
  1566. {
  1567. applyClipPath(cell, clipPath, cW, cH, graph);
  1568. }
  1569. }
  1570. }
  1571. }
  1572. finally
  1573. {
  1574. graph.getModel().endUpdate();
  1575. }
  1576. if (select != null)
  1577. {
  1578. graph.setSelectionCells(select);
  1579. graph.scrollCellToVisible(select[0]);
  1580. }
  1581. }
  1582. }
  1583. }, graph.cellEditor.isContentEditing(), !graph.cellEditor.isContentEditing(), true, clipPath);
  1584. }
  1585. }).isEnabled = isGraphEnabled;
  1586. this.addAction('crop...', function()
  1587. {
  1588. var cell = graph.getSelectionCell();
  1589. if (graph.isEnabled() && !graph.isCellLocked(graph.getDefaultParent()) && cell != null)
  1590. {
  1591. var style = graph.getCurrentCellStyle(cell);
  1592. var value = style[mxConstants.STYLE_IMAGE], shape = style[mxConstants.STYLE_SHAPE];
  1593. if (!value || shape != 'image')
  1594. {
  1595. return; //Can only process an existing image
  1596. }
  1597. var dlg = new CropImageDialog(ui, value, style[mxConstants.STYLE_CLIP_PATH], function(clipPath, width, height)
  1598. {
  1599. applyClipPath(cell, clipPath, width, height, graph);
  1600. });
  1601. ui.showDialog(dlg.container, 300, 390, true, true);
  1602. }
  1603. }).isEnabled = isGraphEnabled;
  1604. action = this.addAction('layers', mxUtils.bind(this, function()
  1605. {
  1606. if (this.layersWindow == null)
  1607. {
  1608. // LATER: Check outline window for initial placement
  1609. this.layersWindow = new LayersWindow(ui, document.body.offsetWidth - 280, 120, 212, 200);
  1610. this.layersWindow.window.addListener('show', mxUtils.bind(this, function()
  1611. {
  1612. ui.fireEvent(new mxEventObject('layers'));
  1613. }));
  1614. this.layersWindow.window.addListener('hide', function()
  1615. {
  1616. ui.fireEvent(new mxEventObject('layers'));
  1617. });
  1618. this.layersWindow.window.setVisible(true);
  1619. ui.fireEvent(new mxEventObject('layers'));
  1620. this.layersWindow.init();
  1621. }
  1622. else
  1623. {
  1624. this.layersWindow.window.setVisible(!this.layersWindow.window.isVisible());
  1625. }
  1626. }), null, null, Editor.ctrlKey + '+Shift+L');
  1627. action.setToggleAction(true);
  1628. action.setSelectedCallback(mxUtils.bind(this, function() { return this.layersWindow != null && this.layersWindow.window.isVisible(); }));
  1629. action = this.addAction('format', mxUtils.bind(this, function()
  1630. {
  1631. ui.toggleFormatPanel();
  1632. }), null, null, Editor.ctrlKey + '+Shift+P');
  1633. action.setToggleAction(true);
  1634. action.setSelectedCallback(mxUtils.bind(this, function() { return ui.isFormatPanelVisible(); }));
  1635. action = this.addAction('outline', mxUtils.bind(this, function()
  1636. {
  1637. if (this.outlineWindow == null)
  1638. {
  1639. // LATER: Check layers window for initial placement
  1640. this.outlineWindow = new OutlineWindow(ui, document.body.offsetWidth - 260, 100, 180, 180);
  1641. this.outlineWindow.window.addListener('show', mxUtils.bind(this, function()
  1642. {
  1643. ui.fireEvent(new mxEventObject('outline'));
  1644. }));
  1645. this.outlineWindow.window.addListener('hide', function()
  1646. {
  1647. ui.fireEvent(new mxEventObject('outline'));
  1648. });
  1649. this.outlineWindow.window.setVisible(true);
  1650. ui.fireEvent(new mxEventObject('outline'));
  1651. }
  1652. else
  1653. {
  1654. this.outlineWindow.window.setVisible(!this.outlineWindow.window.isVisible());
  1655. }
  1656. }), null, null, Editor.ctrlKey + '+Shift+O');
  1657. action.setToggleAction(true);
  1658. action.setSelectedCallback(mxUtils.bind(this, function() { return this.outlineWindow != null && this.outlineWindow.window.isVisible(); }));
  1659. this.addAction('editConnectionPoints...', function()
  1660. {
  1661. var cell = graph.getSelectionCell();
  1662. if (graph.isEnabled() && !graph.isCellLocked(graph.getDefaultParent()) && cell != null)
  1663. {
  1664. var dlg = new ConnectionPointsDialog(ui, cell);
  1665. ui.showDialog(dlg.container, 350, 450, true, false, function()
  1666. {
  1667. dlg.destroy();
  1668. });
  1669. dlg.init();
  1670. }
  1671. }, null, null, 'Alt+Shift+Q').isEnabled = isGraphEnabled;
  1672. };
  1673. /**
  1674. * Registers the given action under the given name.
  1675. */
  1676. Actions.prototype.addAction = function(key, funct, enabled, iconCls, shortcut)
  1677. {
  1678. var title;
  1679. if (key.substring(key.length - 3) == '...')
  1680. {
  1681. key = key.substring(0, key.length - 3);
  1682. title = mxResources.get(key) + '...';
  1683. }
  1684. else
  1685. {
  1686. title = mxResources.get(key);
  1687. }
  1688. return this.put(key, new Action(title, funct, enabled, iconCls, shortcut));
  1689. };
  1690. /**
  1691. * Registers the given action under the given name.
  1692. */
  1693. Actions.prototype.put = function(name, action)
  1694. {
  1695. this.actions[name] = action;
  1696. return action;
  1697. };
  1698. /**
  1699. * Returns the action for the given name or null if no such action exists.
  1700. */
  1701. Actions.prototype.get = function(name)
  1702. {
  1703. return this.actions[name];
  1704. };
  1705. /**
  1706. * Constructs a new action for the given parameters.
  1707. */
  1708. function Action(label, funct, enabled, iconCls, shortcut)
  1709. {
  1710. mxEventSource.call(this);
  1711. this.label = label;
  1712. this.funct = this.createFunction(funct);
  1713. this.enabled = (enabled != null) ? enabled : true;
  1714. this.iconCls = iconCls;
  1715. this.shortcut = shortcut;
  1716. this.visible = true;
  1717. };
  1718. // Action inherits from mxEventSource
  1719. mxUtils.extend(Action, mxEventSource);
  1720. /**
  1721. * Sets the enabled state of the action and fires a stateChanged event.
  1722. */
  1723. Action.prototype.createFunction = function(funct)
  1724. {
  1725. return funct;
  1726. };
  1727. /**
  1728. * Sets the enabled state of the action and fires a stateChanged event.
  1729. */
  1730. Action.prototype.setEnabled = function(value)
  1731. {
  1732. if (this.enabled != value)
  1733. {
  1734. this.enabled = value;
  1735. this.fireEvent(new mxEventObject('stateChanged'));
  1736. }
  1737. };
  1738. /**
  1739. * Sets the enabled state of the action and fires a stateChanged event.
  1740. */
  1741. Action.prototype.isEnabled = function()
  1742. {
  1743. return this.enabled;
  1744. };
  1745. /**
  1746. * Sets the enabled state of the action and fires a stateChanged event.
  1747. */
  1748. Action.prototype.setToggleAction = function(value)
  1749. {
  1750. this.toggleAction = value;
  1751. };
  1752. /**
  1753. * Sets the enabled state of the action and fires a stateChanged event.
  1754. */
  1755. Action.prototype.setSelectedCallback = function(funct)
  1756. {
  1757. this.selectedCallback = funct;
  1758. };
  1759. /**
  1760. * Sets the enabled state of the action and fires a stateChanged event.
  1761. */
  1762. Action.prototype.isSelected = function()
  1763. {
  1764. return this.selectedCallback();
  1765. };