Actions.js 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311
  1. /**
  2. * Copyright (c) 2006-2012, JGraph Ltd
  3. */
  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() { window.open(ui.getUrl()); });
  27. this.addAction('open...', function()
  28. {
  29. window.openNew = true;
  30. window.openKey = 'open';
  31. ui.openFile();
  32. });
  33. this.addAction('import...', function()
  34. {
  35. window.openNew = false;
  36. window.openKey = 'import';
  37. // Closes dialog after open
  38. window.openFile = new OpenFile(mxUtils.bind(this, function()
  39. {
  40. ui.hideDialog();
  41. }));
  42. window.openFile.setConsumer(mxUtils.bind(this, function(xml, filename)
  43. {
  44. try
  45. {
  46. var doc = mxUtils.parseXml(xml);
  47. var model = new mxGraphModel();
  48. var codec = new mxCodec(doc);
  49. codec.decode(doc.documentElement, model);
  50. var children = model.getChildren(model.getChildAt(model.getRoot(), 0));
  51. editor.graph.setSelectionCells(editor.graph.importCells(children));
  52. }
  53. catch (e)
  54. {
  55. mxUtils.alert(mxResources.get('invalidOrMissingFile') + ': ' + e.message);
  56. }
  57. }));
  58. // Removes openFile if dialog is closed
  59. ui.showDialog(new OpenDialog(this).container, 320, 220, true, true, function()
  60. {
  61. window.openFile = null;
  62. });
  63. }).isEnabled = isGraphEnabled;
  64. this.addAction('save', function() { ui.saveFile(false); }, null, null, 'Ctrl+S').isEnabled = isGraphEnabled;
  65. this.addAction('saveAs...', function() { ui.saveFile(true); }, null, null, 'Ctrl+Shift+S').isEnabled = isGraphEnabled;
  66. this.addAction('export...', function() { ui.showDialog(new ExportDialog(ui).container, 300, 210, true, true); });
  67. this.addAction('editDiagram...', function()
  68. {
  69. var dlg = new EditDiagramDialog(ui);
  70. ui.showDialog(dlg.container, 620, 420, true, true);
  71. dlg.init();
  72. });
  73. this.addAction('pageSetup...', function() { ui.showDialog(new PageSetupDialog(ui).container, 320, 220, true, true); }).isEnabled = isGraphEnabled;
  74. this.addAction('print...', function() { ui.showDialog(new PrintDialog(ui).container, 300, 180, true, true); }, null, 'sprite-print', 'Ctrl+P');
  75. this.addAction('preview', function() { mxUtils.show(graph, null, 10, 10); });
  76. // Edit actions
  77. this.addAction('undo', function() { ui.undo(); }, null, 'sprite-undo', 'Ctrl+Z');
  78. this.addAction('redo', function() { ui.redo(); }, null, 'sprite-redo', (!mxClient.IS_WIN) ? 'Ctrl+Shift+Z' : 'Ctrl+Y');
  79. this.addAction('cut', function() { mxClipboard.cut(graph); }, null, 'sprite-cut', 'Ctrl+X');
  80. this.addAction('copy', function() { mxClipboard.copy(graph); }, null, 'sprite-copy', 'Ctrl+C');
  81. this.addAction('paste', function()
  82. {
  83. if (graph.isEnabled() && !graph.isCellLocked(graph.getDefaultParent()))
  84. {
  85. mxClipboard.paste(graph);
  86. }
  87. }, false, 'sprite-paste', 'Ctrl+V');
  88. this.addAction('pasteHere', function(evt)
  89. {
  90. if (graph.isEnabled() && !graph.isCellLocked(graph.getDefaultParent()))
  91. {
  92. graph.getModel().beginUpdate();
  93. try
  94. {
  95. var cells = mxClipboard.paste(graph);
  96. if (cells != null)
  97. {
  98. var bb = graph.getBoundingBoxFromGeometry(cells);
  99. if (bb != null)
  100. {
  101. var t = graph.view.translate;
  102. var s = graph.view.scale;
  103. var dx = t.x;
  104. var dy = t.y;
  105. var x = Math.round(graph.snap(graph.popupMenuHandler.triggerX / s - dx));
  106. var y = Math.round(graph.snap(graph.popupMenuHandler.triggerY / s - dy));
  107. graph.cellsMoved(cells, x - bb.x, y - bb.y);
  108. }
  109. }
  110. }
  111. finally
  112. {
  113. graph.getModel().endUpdate();
  114. }
  115. }
  116. });
  117. function deleteCells(includeEdges)
  118. {
  119. // Cancels interactive operations
  120. graph.escape();
  121. var cells = graph.getDeletableCells(graph.getSelectionCells());
  122. if (cells != null && cells.length > 0)
  123. {
  124. var parents = graph.model.getParents(cells);
  125. graph.removeCells(cells, includeEdges);
  126. // Selects parents for easier editing of groups
  127. if (parents != null)
  128. {
  129. var select = [];
  130. for (var i = 0; i < parents.length; i++)
  131. {
  132. if (graph.model.isVertex(parents[i]) || graph.model.isEdge(parents[i]))
  133. {
  134. select.push(parents[i]);
  135. }
  136. }
  137. graph.setSelectionCells(select);
  138. }
  139. }
  140. };
  141. this.addAction('delete', function()
  142. {
  143. deleteCells(false);
  144. }, null, null, 'Delete');
  145. this.addAction('deleteAll', function()
  146. {
  147. deleteCells(true);
  148. }, null, null, 'Ctrl+Delete');
  149. this.addAction('duplicate', function()
  150. {
  151. graph.setSelectionCells(graph.duplicateCells());
  152. }, null, null, 'Ctrl+D');
  153. this.addAction('turn', function()
  154. {
  155. graph.setSelectionCells(graph.turnShapes(graph.getSelectionCells()));
  156. }, null, null, 'Ctrl+R');
  157. this.addAction('selectVertices', function() { graph.selectVertices(); }, null, null, 'Ctrl+Shift+I');
  158. this.addAction('selectEdges', function() { graph.selectEdges(); }, null, null, 'Ctrl+Shift+E');
  159. this.addAction('selectAll', function() { graph.selectAll(null, true); }, null, null, 'Ctrl+A');
  160. this.addAction('selectNone', function() { graph.clearSelection(); }, null, null, 'Ctrl+Shift+A');
  161. this.addAction('lockUnlock', function()
  162. {
  163. if (!graph.isSelectionEmpty())
  164. {
  165. graph.getModel().beginUpdate();
  166. try
  167. {
  168. var defaultValue = graph.isCellMovable(graph.getSelectionCell()) ? 1 : 0;
  169. graph.toggleCellStyles(mxConstants.STYLE_MOVABLE, defaultValue);
  170. graph.toggleCellStyles(mxConstants.STYLE_RESIZABLE, defaultValue);
  171. graph.toggleCellStyles(mxConstants.STYLE_ROTATABLE, defaultValue);
  172. graph.toggleCellStyles(mxConstants.STYLE_DELETABLE, defaultValue);
  173. graph.toggleCellStyles(mxConstants.STYLE_EDITABLE, defaultValue);
  174. graph.toggleCellStyles('connectable', defaultValue);
  175. }
  176. finally
  177. {
  178. graph.getModel().endUpdate();
  179. }
  180. }
  181. }, null, null, 'Ctrl+L');
  182. // Navigation actions
  183. this.addAction('home', function() { graph.home(); }, null, null, 'Home');
  184. this.addAction('exitGroup', function() { graph.exitGroup(); }, null, null, 'Ctrl+Shift+Page Up');
  185. this.addAction('enterGroup', function() { graph.enterGroup(); }, null, null, 'Ctrl+Shift+Page Down');
  186. this.addAction('expand', function() { graph.foldCells(false); }, null, null, 'Ctrl+Page Down');
  187. this.addAction('collapse', function() { graph.foldCells(true); }, null, null, 'Ctrl+Page Up');
  188. // Arrange actions
  189. this.addAction('toFront', function() { graph.orderCells(false); }, null, null, 'Ctrl+Shift+F');
  190. this.addAction('toBack', function() { graph.orderCells(true); }, null, null, 'Ctrl+Shift+B');
  191. this.addAction('group', function()
  192. {
  193. if (graph.getSelectionCount() == 1)
  194. {
  195. graph.setCellStyles('container', '1');
  196. }
  197. else
  198. {
  199. graph.setSelectionCell(graph.groupCells(null, 0));
  200. }
  201. }, null, null, 'Ctrl+G');
  202. this.addAction('ungroup', function()
  203. {
  204. if (graph.getSelectionCount() == 1 && graph.getModel().getChildCount(graph.getSelectionCell()) == 0)
  205. {
  206. graph.setCellStyles('container', '0');
  207. }
  208. else
  209. {
  210. graph.setSelectionCells(graph.ungroupCells());
  211. }
  212. }, null, null, 'Ctrl+Shift+U');
  213. this.addAction('removeFromGroup', function() { graph.removeCellsFromParent(); });
  214. // Adds action
  215. this.addAction('editData...', function()
  216. {
  217. var cell = graph.getSelectionCell() || graph.getModel().getRoot();
  218. if (cell != null)
  219. {
  220. var dlg = new EditDataDialog(ui, cell);
  221. ui.showDialog(dlg.container, 320, 320, true, false);
  222. dlg.init();
  223. }
  224. }, null, null, 'Ctrl+M');
  225. this.addAction('editTooltip...', function()
  226. {
  227. var graph = ui.editor.graph;
  228. if (graph.isEnabled() && !graph.isSelectionEmpty())
  229. {
  230. var cell = graph.getSelectionCell();
  231. var tooltip = '';
  232. if (mxUtils.isNode(cell.value))
  233. {
  234. var tmp = cell.value.getAttribute('tooltip');
  235. if (tmp != null)
  236. {
  237. tooltip = tmp;
  238. }
  239. }
  240. var dlg = new TextareaDialog(ui, mxResources.get('editTooltip') + ':', tooltip, function(newValue)
  241. {
  242. graph.setTooltipForCell(cell, newValue);
  243. });
  244. ui.showDialog(dlg.container, 320, 200, true, true);
  245. dlg.init();
  246. }
  247. });
  248. this.addAction('openLink', function()
  249. {
  250. var link = graph.getLinkForCell(graph.getSelectionCell());
  251. if (link != null)
  252. {
  253. window.open(link);
  254. }
  255. });
  256. this.addAction('editLink...', function()
  257. {
  258. var graph = ui.editor.graph;
  259. if (graph.isEnabled() && !graph.isSelectionEmpty())
  260. {
  261. var cell = graph.getSelectionCell();
  262. var value = graph.getLinkForCell(cell) || '';
  263. ui.showLinkDialog(value, mxResources.get('apply'), function(link)
  264. {
  265. link = mxUtils.trim(link);
  266. graph.setLinkForCell(cell, (link.length > 0) ? link : null);
  267. });
  268. }
  269. });
  270. this.addAction('insertLink', function()
  271. {
  272. if (graph.isEnabled() && !graph.isCellLocked(graph.getDefaultParent()))
  273. {
  274. var dlg = new LinkDialog(ui, '', mxResources.get('insert'), function(link, docs)
  275. {
  276. link = mxUtils.trim(link);
  277. if (link.length > 0)
  278. {
  279. var title = link.substring(link.lastIndexOf('/') + 1);
  280. var icon = null;
  281. if (docs != null && docs.length > 0)
  282. {
  283. icon = docs[0].iconUrl;
  284. title = docs[0].name || docs[0].type;
  285. title = title.charAt(0).toUpperCase() + title.substring(1);
  286. if (title.length > 30)
  287. {
  288. title = title.substring(0, 30) + '...';
  289. }
  290. }
  291. var pt = graph.getInsertPoint();
  292. var linkCell = new mxCell(title, new mxGeometry(pt.x, pt.y, 100, 40),
  293. 'fontColor=#0000EE;fontStyle=4;rounded=1;overflow=hidden;' + ((icon != null) ?
  294. 'shape=label;imageWidth=16;imageHeight=16;spacingLeft=26;align=left;image=' + icon :
  295. 'spacing=10;'));
  296. linkCell.vertex = true;
  297. graph.setLinkForCell(linkCell, link);
  298. graph.cellSizeUpdated(linkCell, true);
  299. graph.setSelectionCell(graph.addCell(linkCell));
  300. }
  301. });
  302. ui.showDialog(dlg.container, 420, 90, true, true);
  303. dlg.init();
  304. }
  305. }).isEnabled = isGraphEnabled;
  306. this.addAction('link...', mxUtils.bind(this, function()
  307. {
  308. var graph = ui.editor.graph;
  309. if (graph.isEnabled())
  310. {
  311. if (graph.cellEditor.isContentEditing())
  312. {
  313. var link = graph.getParentByName(graph.getSelectedElement(), 'A', graph.cellEditor.textarea);
  314. var oldValue = '';
  315. if (link != null)
  316. {
  317. oldValue = link.getAttribute('href') || '';
  318. }
  319. var selState = graph.cellEditor.saveSelection();
  320. ui.showLinkDialog(oldValue, mxResources.get('apply'), mxUtils.bind(this, function(value)
  321. {
  322. graph.cellEditor.restoreSelection(selState);
  323. if (value != null)
  324. {
  325. if (value.length == 0)
  326. {
  327. document.execCommand('unlink', false);
  328. }
  329. else
  330. {
  331. // To find the new link, we create a list of all existing links first
  332. var tmp = graph.cellEditor.textarea.getElementsByTagName('a');
  333. var oldLinks = [];
  334. for (var i = 0; i < tmp.length; i++)
  335. {
  336. oldLinks.push(tmp[i]);
  337. }
  338. // LATER: Fix inserting link/image in IE8/quirks after focus lost
  339. document.execCommand('createlink', false, mxUtils.trim(value));
  340. // Adds target="_blank" for the new link
  341. var newLinks = graph.cellEditor.textarea.getElementsByTagName('a');
  342. if (newLinks.length == oldLinks.length + 1)
  343. {
  344. // Inverse order in favor of appended links
  345. for (var i = newLinks.length - 1; i >= 0; i--)
  346. {
  347. if (i == 0 || newLinks[i] != oldLinks[i - 1])
  348. {
  349. newLinks[i].setAttribute('target', '_blank');
  350. break;
  351. }
  352. }
  353. }
  354. }
  355. }
  356. }));
  357. }
  358. else if (graph.isSelectionEmpty())
  359. {
  360. this.get('insertLink').funct();
  361. }
  362. else
  363. {
  364. this.get('editLink').funct();
  365. }
  366. }
  367. })).isEnabled = isGraphEnabled;
  368. this.addAction('autosize', function()
  369. {
  370. var cells = graph.getSelectionCells();
  371. if (cells != null)
  372. {
  373. graph.getModel().beginUpdate();
  374. try
  375. {
  376. for (var i = 0; i < cells.length; i++)
  377. {
  378. var cell = cells[i];
  379. if (graph.getModel().getChildCount(cell))
  380. {
  381. graph.updateGroupBounds([cell], 20);
  382. }
  383. else
  384. {
  385. var state = graph.view.getState(cell);
  386. var geo = graph.getCellGeometry(cell);
  387. if (graph.getModel().isVertex(cell) && state != null && state.text != null &&
  388. geo != null && graph.isWrapping(cell))
  389. {
  390. geo = geo.clone();
  391. geo.height = state.text.boundingBox.height / graph.view.scale;
  392. graph.getModel().setGeometry(cell, geo);
  393. }
  394. else
  395. {
  396. graph.updateCellSize(cell);
  397. }
  398. }
  399. }
  400. }
  401. finally
  402. {
  403. graph.getModel().endUpdate();
  404. }
  405. }
  406. }, null, null, 'Ctrl+Shift+Y');
  407. this.addAction('formattedText', function()
  408. {
  409. var state = graph.getView().getState(graph.getSelectionCell());
  410. if (state != null)
  411. {
  412. var value = '1';
  413. graph.stopEditing();
  414. graph.getModel().beginUpdate();
  415. try
  416. {
  417. if (state.style['html'] == '1')
  418. {
  419. value = null;
  420. // Removes newlines from HTML and converts breaks to newlines
  421. // to match the HTML output in plain text
  422. if (mxUtils.getValue(state.style, 'nl2Br', '1') != '0')
  423. {
  424. graph.cellLabelChanged(state.cell, graph.convertValueToString(state.cell).
  425. replace(/\n/g, '').replace(/<br\s*.?>/g, '\n'));
  426. }
  427. }
  428. else
  429. {
  430. // FIXME: HTML entities are converted in plain text labels if word wrap is on
  431. // TODO: Convert HTML entities? (Check for userobject!)
  432. // Converts newlines in plain text to breaks in HTML
  433. // to match the plain text output
  434. var label = graph.convertValueToString(state.cell);
  435. if (mxUtils.getValue(state.style, 'nl2Br', '1') != '0')
  436. {
  437. label = label.replace(/\n/g, '<br/>');
  438. }
  439. graph.cellLabelChanged(state.cell, graph.sanitizeHtml(label));
  440. }
  441. graph.setCellStyles('html', value);
  442. ui.fireEvent(new mxEventObject('styleChanged', 'keys', ['html'],
  443. 'values', [(value != null) ? value : '0'], 'cells',
  444. graph.getSelectionCells()));
  445. }
  446. finally
  447. {
  448. graph.getModel().endUpdate();
  449. }
  450. }
  451. });
  452. this.addAction('wordWrap', function()
  453. {
  454. var state = graph.getView().getState(graph.getSelectionCell());
  455. var value = 'wrap';
  456. graph.stopEditing();
  457. if (state != null && state.style[mxConstants.STYLE_WHITE_SPACE] == 'wrap')
  458. {
  459. value = null;
  460. }
  461. graph.setCellStyles(mxConstants.STYLE_WHITE_SPACE, value);
  462. });
  463. this.addAction('rotation', function()
  464. {
  465. var value = '0';
  466. var state = graph.getView().getState(graph.getSelectionCell());
  467. if (state != null)
  468. {
  469. value = state.style[mxConstants.STYLE_ROTATION] || value;
  470. }
  471. var dlg = new FilenameDialog(ui, value, mxResources.get('apply'), function(newValue)
  472. {
  473. if (newValue != null && newValue.length > 0)
  474. {
  475. graph.setCellStyles(mxConstants.STYLE_ROTATION, newValue);
  476. }
  477. }, mxResources.get('enterValue') + ' (' + mxResources.get('rotation') + ' 0-360)');
  478. ui.showDialog(dlg.container, 300, 80, true, true);
  479. dlg.init();
  480. });
  481. // View actions
  482. this.addAction('resetView', function()
  483. {
  484. graph.zoomTo(1);
  485. ui.resetScrollbars();
  486. }, null, null, 'Ctrl+H');
  487. this.addAction('zoomIn', function(evt) { graph.zoomIn(); }, null, null, 'Ctrl + / Alt+Mousewheel');
  488. this.addAction('zoomOut', function(evt) { graph.zoomOut(); }, null, null, 'Ctrl - / Alt+Mousewheel');
  489. this.addAction('fitWindow', function() { graph.fit(); }, null, null, 'Ctrl+Shift+H');
  490. this.addAction('fitPage', mxUtils.bind(this, function()
  491. {
  492. if (!graph.pageVisible)
  493. {
  494. this.get('pageView').funct();
  495. }
  496. var fmt = graph.pageFormat;
  497. var ps = graph.pageScale;
  498. var cw = graph.container.clientWidth - 10;
  499. var ch = graph.container.clientHeight - 10;
  500. var scale = Math.floor(20 * Math.min(cw / fmt.width / ps, ch / fmt.height / ps)) / 20;
  501. graph.zoomTo(scale);
  502. if (mxUtils.hasScrollbars(graph.container))
  503. {
  504. var pad = graph.getPagePadding();
  505. graph.container.scrollTop = pad.y * graph.view.scale;
  506. graph.container.scrollLeft = Math.min(pad.x * graph.view.scale, (graph.container.scrollWidth - graph.container.clientWidth) / 2);
  507. }
  508. }), null, null, 'Ctrl+J');
  509. this.addAction('fitTwoPages', mxUtils.bind(this, function()
  510. {
  511. if (!graph.pageVisible)
  512. {
  513. this.get('pageView').funct();
  514. }
  515. var fmt = graph.pageFormat;
  516. var ps = graph.pageScale;
  517. var cw = graph.container.clientWidth - 10;
  518. var ch = graph.container.clientHeight - 10;
  519. var scale = Math.floor(20 * Math.min(cw / (2 * fmt.width) / ps, ch / fmt.height / ps)) / 20;
  520. graph.zoomTo(scale);
  521. if (mxUtils.hasScrollbars(graph.container))
  522. {
  523. var pad = graph.getPagePadding();
  524. graph.container.scrollTop = Math.min(pad.y, (graph.container.scrollHeight - graph.container.clientHeight) / 2);
  525. graph.container.scrollLeft = Math.min(pad.x, (graph.container.scrollWidth - graph.container.clientWidth) / 2);
  526. }
  527. }), null, null, 'Ctrl+Shift+J');
  528. this.addAction('fitPageWidth', mxUtils.bind(this, function()
  529. {
  530. if (!graph.pageVisible)
  531. {
  532. this.get('pageView').funct();
  533. }
  534. var fmt = graph.pageFormat;
  535. var ps = graph.pageScale;
  536. var cw = graph.container.clientWidth - 10;
  537. var scale = Math.floor(20 * cw / fmt.width / ps) / 20;
  538. graph.zoomTo(scale);
  539. if (mxUtils.hasScrollbars(graph.container))
  540. {
  541. var pad = graph.getPagePadding();
  542. graph.container.scrollLeft = Math.min(pad.x * graph.view.scale,
  543. (graph.container.scrollWidth - graph.container.clientWidth) / 2);
  544. }
  545. }));
  546. this.put('customZoom', new Action(mxResources.get('custom') + '...', mxUtils.bind(this, function()
  547. {
  548. var dlg = new FilenameDialog(this.editorUi, parseInt(graph.getView().getScale() * 100), mxResources.get('apply'), mxUtils.bind(this, function(newValue)
  549. {
  550. var val = parseInt(newValue);
  551. if (!isNaN(val) && val > 0)
  552. {
  553. graph.zoomTo(val / 100);
  554. }
  555. }), mxResources.get('zoom') + ' (%)');
  556. this.editorUi.showDialog(dlg.container, 300, 80, true, true);
  557. dlg.init();
  558. }), null, null, 'Ctrl+0'));
  559. this.addAction('pageScale', mxUtils.bind(this, function()
  560. {
  561. var dlg = new FilenameDialog(this.editorUi, parseInt(graph.pageScale * 100), mxResources.get('apply'), mxUtils.bind(this, function(newValue)
  562. {
  563. var val = parseInt(newValue);
  564. if (!isNaN(val) && val > 0)
  565. {
  566. ui.setPageScale(val / 100);
  567. }
  568. }), mxResources.get('pageScale') + ' (%)');
  569. this.editorUi.showDialog(dlg.container, 300, 80, true, true);
  570. dlg.init();
  571. }));
  572. // Option actions
  573. var action = null;
  574. action = this.addAction('grid', function()
  575. {
  576. graph.setGridEnabled(!graph.isGridEnabled());
  577. ui.fireEvent(new mxEventObject('gridEnabledChanged'));
  578. }, null, null, 'Ctrl+Shift+G');
  579. action.setToggleAction(true);
  580. action.setSelectedCallback(function() { return graph.isGridEnabled(); });
  581. action.setEnabled(false);
  582. action = this.addAction('guides', function()
  583. {
  584. graph.graphHandler.guidesEnabled = !graph.graphHandler.guidesEnabled;
  585. ui.fireEvent(new mxEventObject('guidesEnabledChanged'));
  586. });
  587. action.setToggleAction(true);
  588. action.setSelectedCallback(function() { return graph.graphHandler.guidesEnabled; });
  589. action.setEnabled(false);
  590. action = this.addAction('tooltips', function()
  591. {
  592. graph.tooltipHandler.setEnabled(!graph.tooltipHandler.isEnabled());
  593. });
  594. action.setToggleAction(true);
  595. action.setSelectedCallback(function() { return graph.tooltipHandler.isEnabled(); });
  596. action = this.addAction('collapseExpand', function()
  597. {
  598. ui.setFoldingEnabled(!graph.foldingEnabled);
  599. });
  600. action.setToggleAction(true);
  601. action.setSelectedCallback(function() { return graph.foldingEnabled; });
  602. action.isEnabled = isGraphEnabled;
  603. action = this.addAction('scrollbars', function()
  604. {
  605. ui.setScrollbars(!ui.hasScrollbars());
  606. });
  607. action.setToggleAction(true);
  608. action.setSelectedCallback(function() { return graph.scrollbars; });
  609. action = this.addAction('pageView', mxUtils.bind(this, function()
  610. {
  611. var hasScrollbars = mxUtils.hasScrollbars(graph.container);
  612. var tx = 0;
  613. var ty = 0;
  614. if (hasScrollbars)
  615. {
  616. tx = graph.view.translate.x * graph.view.scale - graph.container.scrollLeft;
  617. ty = graph.view.translate.y * graph.view.scale - graph.container.scrollTop;
  618. }
  619. graph.pageVisible = !graph.pageVisible;
  620. graph.pageBreaksVisible = graph.pageVisible;
  621. graph.preferPageSize = graph.pageBreaksVisible;
  622. graph.view.validateBackground();
  623. // Workaround for possible handle offset
  624. if (hasScrollbars)
  625. {
  626. var cells = graph.getSelectionCells();
  627. graph.clearSelection();
  628. graph.setSelectionCells(cells);
  629. }
  630. // Calls updatePageBreaks
  631. graph.sizeDidChange();
  632. if (hasScrollbars)
  633. {
  634. graph.container.scrollLeft = graph.view.translate.x * graph.view.scale - tx;
  635. graph.container.scrollTop = graph.view.translate.y * graph.view.scale - ty;
  636. }
  637. ui.fireEvent(new mxEventObject('pageViewChanged'));
  638. }));
  639. action.setToggleAction(true);
  640. action.setSelectedCallback(function() { return graph.pageVisible; });
  641. this.put('pageBackgroundColor', new Action(mxResources.get('backgroundColor') + '...', function()
  642. {
  643. ui.pickColor(graph.background || 'none', function(color)
  644. {
  645. ui.setBackgroundColor(color);
  646. });
  647. }));
  648. action = this.addAction('connectionArrows', function()
  649. {
  650. graph.connectionArrowsEnabled = !graph.connectionArrowsEnabled;
  651. ui.fireEvent(new mxEventObject('connectionArrowsChanged'));
  652. }, null, null, 'Ctrl+Q');
  653. action.setToggleAction(true);
  654. action.setSelectedCallback(function() { return graph.connectionArrowsEnabled; });
  655. action = this.addAction('connectionPoints', function()
  656. {
  657. graph.setConnectable(!graph.connectionHandler.isEnabled());
  658. ui.fireEvent(new mxEventObject('connectionPointsChanged'));
  659. }, null, null, 'Ctrl+Shift+Q');
  660. action.setToggleAction(true);
  661. action.setSelectedCallback(function() { return graph.connectionHandler.isEnabled(); });
  662. action = this.addAction('copyConnect', function()
  663. {
  664. graph.connectionHandler.setCreateTarget(!graph.connectionHandler.isCreateTarget());
  665. ui.fireEvent(new mxEventObject('copyConnectChanged'));
  666. });
  667. action.setToggleAction(true);
  668. action.setSelectedCallback(function() { return graph.connectionHandler.isCreateTarget(); });
  669. action.isEnabled = isGraphEnabled;
  670. action = this.addAction('autosave', function()
  671. {
  672. ui.editor.setAutosave(!ui.editor.autosave);
  673. });
  674. action.setToggleAction(true);
  675. action.setSelectedCallback(function() { return ui.editor.autosave; });
  676. action.isEnabled = isGraphEnabled;
  677. action.visible = false;
  678. // Help actions
  679. this.addAction('help', function()
  680. {
  681. var ext = '';
  682. if (mxResources.isLanguageSupported(mxClient.language))
  683. {
  684. ext = '_' + mxClient.language;
  685. }
  686. window.open(RESOURCES_PATH + '/help' + ext + '.html');
  687. });
  688. this.put('about', new Action(mxResources.get('about') + ' Graph Editor...', function()
  689. {
  690. ui.showDialog(new AboutDialog(ui).container, 320, 280, true, true);
  691. }, null, null, 'F1'));
  692. // Font style actions
  693. var toggleFontStyle = mxUtils.bind(this, function(key, style, fn, shortcut)
  694. {
  695. return this.addAction(key, function()
  696. {
  697. if (fn != null && graph.cellEditor.isContentEditing())
  698. {
  699. fn();
  700. }
  701. else
  702. {
  703. graph.stopEditing(false);
  704. graph.toggleCellStyleFlags(mxConstants.STYLE_FONTSTYLE, style);
  705. }
  706. }, null, null, shortcut);
  707. });
  708. toggleFontStyle('bold', mxConstants.FONT_BOLD, function() { document.execCommand('bold', false, null); }, 'Ctrl+B');
  709. toggleFontStyle('italic', mxConstants.FONT_ITALIC, function() { document.execCommand('italic', false, null); }, 'Ctrl+I');
  710. toggleFontStyle('underline', mxConstants.FONT_UNDERLINE, function() { document.execCommand('underline', false, null); }, 'Ctrl+U');
  711. // Color actions
  712. this.addAction('fontColor...', function() { ui.menus.pickColor(mxConstants.STYLE_FONTCOLOR, 'forecolor', '000000'); });
  713. this.addAction('strokeColor...', function() { ui.menus.pickColor(mxConstants.STYLE_STROKECOLOR); });
  714. this.addAction('fillColor...', function() { ui.menus.pickColor(mxConstants.STYLE_FILLCOLOR); });
  715. this.addAction('gradientColor...', function() { ui.menus.pickColor(mxConstants.STYLE_GRADIENTCOLOR); });
  716. this.addAction('backgroundColor...', function() { ui.menus.pickColor(mxConstants.STYLE_LABEL_BACKGROUNDCOLOR, 'backcolor'); });
  717. this.addAction('borderColor...', function() { ui.menus.pickColor(mxConstants.STYLE_LABEL_BORDERCOLOR); });
  718. // Format actions
  719. this.addAction('vertical', function() { ui.menus.toggleStyle(mxConstants.STYLE_HORIZONTAL, true); });
  720. this.addAction('shadow', function() { ui.menus.toggleStyle(mxConstants.STYLE_SHADOW); });
  721. this.addAction('solid', function()
  722. {
  723. graph.getModel().beginUpdate();
  724. try
  725. {
  726. graph.setCellStyles(mxConstants.STYLE_DASHED, null);
  727. graph.setCellStyles(mxConstants.STYLE_DASH_PATTERN, null);
  728. ui.fireEvent(new mxEventObject('styleChanged', 'keys', [mxConstants.STYLE_DASHED, mxConstants.STYLE_DASH_PATTERN],
  729. 'values', [null, null], 'cells', graph.getSelectionCells()));
  730. }
  731. finally
  732. {
  733. graph.getModel().endUpdate();
  734. }
  735. });
  736. this.addAction('dashed', function()
  737. {
  738. graph.getModel().beginUpdate();
  739. try
  740. {
  741. graph.setCellStyles(mxConstants.STYLE_DASHED, '1');
  742. graph.setCellStyles(mxConstants.STYLE_DASH_PATTERN, null);
  743. ui.fireEvent(new mxEventObject('styleChanged', 'keys', [mxConstants.STYLE_DASHED, mxConstants.STYLE_DASH_PATTERN],
  744. 'values', ['1', null], 'cells', graph.getSelectionCells()));
  745. }
  746. finally
  747. {
  748. graph.getModel().endUpdate();
  749. }
  750. });
  751. this.addAction('dotted', function()
  752. {
  753. graph.getModel().beginUpdate();
  754. try
  755. {
  756. graph.setCellStyles(mxConstants.STYLE_DASHED, '1');
  757. graph.setCellStyles(mxConstants.STYLE_DASH_PATTERN, '1 4');
  758. ui.fireEvent(new mxEventObject('styleChanged', 'keys', [mxConstants.STYLE_DASHED, mxConstants.STYLE_DASH_PATTERN],
  759. 'values', ['1', '1 4'], 'cells', graph.getSelectionCells()));
  760. }
  761. finally
  762. {
  763. graph.getModel().endUpdate();
  764. }
  765. });
  766. this.addAction('sharp', function()
  767. {
  768. graph.getModel().beginUpdate();
  769. try
  770. {
  771. graph.setCellStyles(mxConstants.STYLE_ROUNDED, '0');
  772. graph.setCellStyles(mxConstants.STYLE_CURVED, '0');
  773. ui.fireEvent(new mxEventObject('styleChanged', 'keys', [mxConstants.STYLE_ROUNDED, mxConstants.STYLE_CURVED],
  774. 'values', ['0', '0'], 'cells', graph.getSelectionCells()));
  775. }
  776. finally
  777. {
  778. graph.getModel().endUpdate();
  779. }
  780. });
  781. this.addAction('rounded', function()
  782. {
  783. graph.getModel().beginUpdate();
  784. try
  785. {
  786. graph.setCellStyles(mxConstants.STYLE_ROUNDED, '1');
  787. graph.setCellStyles(mxConstants.STYLE_CURVED, '0');
  788. ui.fireEvent(new mxEventObject('styleChanged', 'keys', [mxConstants.STYLE_ROUNDED, mxConstants.STYLE_CURVED],
  789. 'values', ['1', '0'], 'cells', graph.getSelectionCells()));
  790. }
  791. finally
  792. {
  793. graph.getModel().endUpdate();
  794. }
  795. });
  796. this.addAction('toggleRounded', function()
  797. {
  798. if (!graph.isSelectionEmpty() && graph.isEnabled())
  799. {
  800. graph.getModel().beginUpdate();
  801. try
  802. {
  803. var cells = graph.getSelectionCells();
  804. var state = graph.view.getState(cells[0]);
  805. var style = (state != null) ? state.style : graph.getCellStyle(cells[0]);
  806. var value = (mxUtils.getValue(style, mxConstants.STYLE_ROUNDED, '0') == '1') ? '0' : '1';
  807. graph.setCellStyles(mxConstants.STYLE_ROUNDED, value);
  808. graph.setCellStyles(mxConstants.STYLE_CURVED, null);
  809. ui.fireEvent(new mxEventObject('styleChanged', 'keys', [mxConstants.STYLE_ROUNDED, mxConstants.STYLE_CURVED],
  810. 'values', [value, '0'], 'cells', graph.getSelectionCells()));
  811. }
  812. finally
  813. {
  814. graph.getModel().endUpdate();
  815. }
  816. }
  817. });
  818. this.addAction('curved', function()
  819. {
  820. graph.getModel().beginUpdate();
  821. try
  822. {
  823. graph.setCellStyles(mxConstants.STYLE_ROUNDED, '0');
  824. graph.setCellStyles(mxConstants.STYLE_CURVED, '1');
  825. ui.fireEvent(new mxEventObject('styleChanged', 'keys', [mxConstants.STYLE_ROUNDED, mxConstants.STYLE_CURVED],
  826. 'values', ['0', '1'], 'cells', graph.getSelectionCells()));
  827. }
  828. finally
  829. {
  830. graph.getModel().endUpdate();
  831. }
  832. });
  833. this.addAction('collapsible', function()
  834. {
  835. var state = graph.view.getState(graph.getSelectionCell());
  836. var value = '1';
  837. if (state != null && graph.getFoldingImage(state) != null)
  838. {
  839. value = '0';
  840. }
  841. graph.setCellStyles('collapsible', value);
  842. ui.fireEvent(new mxEventObject('styleChanged', 'keys', ['collapsible'],
  843. 'values', [value], 'cells', graph.getSelectionCells()));
  844. });
  845. this.addAction('editStyle...', mxUtils.bind(this, function()
  846. {
  847. var cells = graph.getSelectionCells();
  848. if (cells != null && cells.length > 0)
  849. {
  850. var model = graph.getModel();
  851. var dlg = new TextareaDialog(this.editorUi, mxResources.get('editStyle') + ':',
  852. model.getStyle(cells[0]) || '', function(newValue)
  853. {
  854. if (newValue != null)
  855. {
  856. graph.setCellStyle(mxUtils.trim(newValue), cells);
  857. }
  858. }, null, null, 400, 220);
  859. this.editorUi.showDialog(dlg.container, 420, 300, true, true);
  860. dlg.init();
  861. }
  862. }), null, null, 'Ctrl+E');
  863. this.addAction('setAsDefaultStyle', function()
  864. {
  865. if (graph.isEnabled() && !graph.isSelectionEmpty())
  866. {
  867. ui.setDefaultStyle(graph.getSelectionCell());
  868. }
  869. }, null, null, 'Ctrl+Shift+D');
  870. this.addAction('clearDefaultStyle', function()
  871. {
  872. if (graph.isEnabled())
  873. {
  874. ui.clearDefaultStyle();
  875. }
  876. }, null, null, 'Ctrl+Shift+R');
  877. this.addAction('addWaypoint', function()
  878. {
  879. var cell = graph.getSelectionCell();
  880. if (cell != null && graph.getModel().isEdge(cell))
  881. {
  882. var handler = editor.graph.selectionCellsHandler.getHandler(cell);
  883. if (handler instanceof mxEdgeHandler)
  884. {
  885. var t = graph.view.translate;
  886. var s = graph.view.scale;
  887. var dx = t.x;
  888. var dy = t.y;
  889. var parent = graph.getModel().getParent(cell);
  890. var pgeo = graph.getCellGeometry(parent);
  891. while (graph.getModel().isVertex(parent) && pgeo != null)
  892. {
  893. dx += pgeo.x;
  894. dy += pgeo.y;
  895. parent = graph.getModel().getParent(parent);
  896. pgeo = graph.getCellGeometry(parent);
  897. }
  898. var x = Math.round(graph.snap(graph.popupMenuHandler.triggerX / s - dx));
  899. var y = Math.round(graph.snap(graph.popupMenuHandler.triggerY / s - dy));
  900. handler.addPointAt(handler.state, x, y);
  901. }
  902. }
  903. });
  904. this.addAction('removeWaypoint', function()
  905. {
  906. // TODO: Action should run with "this" set to action
  907. var rmWaypointAction = ui.actions.get('removeWaypoint');
  908. if (rmWaypointAction.handler != null)
  909. {
  910. // NOTE: Popupevent handled and action updated in Menus.createPopupMenu
  911. rmWaypointAction.handler.removePoint(rmWaypointAction.handler.state, rmWaypointAction.index);
  912. }
  913. });
  914. this.addAction('clearWaypoints', function()
  915. {
  916. var cells = graph.getSelectionCells();
  917. if (cells != null)
  918. {
  919. graph.getModel().beginUpdate();
  920. try
  921. {
  922. for (var i = 0; i < cells.length; i++)
  923. {
  924. var cell = cells[i];
  925. if (graph.getModel().isEdge(cell))
  926. {
  927. var geo = graph.getCellGeometry(cell);
  928. if (geo != null)
  929. {
  930. geo = geo.clone();
  931. geo.points = null;
  932. graph.getModel().setGeometry(cell, geo);
  933. }
  934. }
  935. }
  936. }
  937. finally
  938. {
  939. graph.getModel().endUpdate();
  940. }
  941. }
  942. });
  943. action = this.addAction('subscript', mxUtils.bind(this, function()
  944. {
  945. if (graph.cellEditor.isContentEditing())
  946. {
  947. document.execCommand('subscript', false, null);
  948. }
  949. }), null, null, 'Ctrl+,');
  950. action = this.addAction('superscript', mxUtils.bind(this, function()
  951. {
  952. if (graph.cellEditor.isContentEditing())
  953. {
  954. document.execCommand('superscript', false, null);
  955. }
  956. }), null, null, 'Ctrl+.');
  957. this.addAction('image...', function()
  958. {
  959. if (graph.isEnabled() && !graph.isCellLocked(graph.getDefaultParent()))
  960. {
  961. var title = mxResources.get('image') + ' (' + mxResources.get('url') + '):';
  962. var state = graph.getView().getState(graph.getSelectionCell());
  963. var value = '';
  964. if (state != null)
  965. {
  966. value = state.style[mxConstants.STYLE_IMAGE] || value;
  967. }
  968. var selectionState = graph.cellEditor.saveSelection();
  969. ui.showImageDialog(title, value, function(newValue, w, h)
  970. {
  971. // Inserts image into HTML text
  972. if (graph.cellEditor.isContentEditing())
  973. {
  974. graph.cellEditor.restoreSelection(selectionState);
  975. graph.insertImage(newValue, w, h);
  976. }
  977. else
  978. {
  979. var cells = graph.getSelectionCells();
  980. if (newValue != null)
  981. {
  982. var select = null;
  983. graph.getModel().beginUpdate();
  984. try
  985. {
  986. // Inserts new cell if no cell is selected
  987. if (cells.length == 0)
  988. {
  989. var pt = graph.getInsertPoint();
  990. cells = [graph.insertVertex(graph.getDefaultParent(), null, '', pt.x, pt.y, w, h,
  991. 'shape=image;verticalLabelPosition=bottom;verticalAlign=top;')];
  992. select = cells;
  993. }
  994. graph.setCellStyles(mxConstants.STYLE_IMAGE, newValue, cells);
  995. // Sets shape only if not already shape with image (label or image)
  996. var state = graph.view.getState(cells[0]);
  997. var style = (state != null) ? state.style : graph.getCellStyle(cells[0]);
  998. if (style[mxConstants.STYLE_SHAPE] != 'image' && style[mxConstants.STYLE_SHAPE] != 'label')
  999. {
  1000. graph.setCellStyles(mxConstants.STYLE_SHAPE, 'image', cells);
  1001. }
  1002. if (graph.getSelectionCount() == 1)
  1003. {
  1004. if (w != null && h != null)
  1005. {
  1006. var cell = cells[0];
  1007. var geo = graph.getModel().getGeometry(cell);
  1008. if (geo != null)
  1009. {
  1010. geo = geo.clone();
  1011. geo.width = w;
  1012. geo.height = h;
  1013. graph.getModel().setGeometry(cell, geo);
  1014. }
  1015. }
  1016. }
  1017. }
  1018. finally
  1019. {
  1020. graph.getModel().endUpdate();
  1021. }
  1022. if (select != null)
  1023. {
  1024. graph.setSelectionCells(select);
  1025. graph.scrollCellToVisible(select[0]);
  1026. }
  1027. }
  1028. }
  1029. }, graph.cellEditor.isContentEditing(), !graph.cellEditor.isContentEditing());
  1030. }
  1031. }).isEnabled = isGraphEnabled;
  1032. this.addAction('insertImage...', function()
  1033. {
  1034. if (graph.isEnabled() && !graph.isCellLocked(graph.getDefaultParent()))
  1035. {
  1036. graph.clearSelection();
  1037. ui.actions.get('image').funct();
  1038. }
  1039. }).isEnabled = isGraphEnabled;
  1040. action = this.addAction('layers', mxUtils.bind(this, function()
  1041. {
  1042. if (this.layersWindow == null)
  1043. {
  1044. // LATER: Check outline window for initial placement
  1045. this.layersWindow = new LayersWindow(ui, document.body.offsetWidth - 280, 120, 220, 180);
  1046. this.layersWindow.window.addListener('show', function()
  1047. {
  1048. ui.fireEvent(new mxEventObject('layers'));
  1049. });
  1050. this.layersWindow.window.addListener('hide', function()
  1051. {
  1052. ui.fireEvent(new mxEventObject('layers'));
  1053. });
  1054. this.layersWindow.window.setVisible(true);
  1055. ui.fireEvent(new mxEventObject('layers'));
  1056. }
  1057. else
  1058. {
  1059. this.layersWindow.window.setVisible(!this.layersWindow.window.isVisible());
  1060. }
  1061. //ui.fireEvent(new mxEventObject('layers'));
  1062. }), null, null, 'Ctrl+Shift+L');
  1063. action.setToggleAction(true);
  1064. action.setSelectedCallback(mxUtils.bind(this, function() { return this.layersWindow != null && this.layersWindow.window.isVisible(); }));
  1065. action = this.addAction('formatPanel', mxUtils.bind(this, function()
  1066. {
  1067. ui.toggleFormatPanel();
  1068. }), null, null, 'Ctrl+Shift+P');
  1069. action.setToggleAction(true);
  1070. action.setSelectedCallback(mxUtils.bind(this, function() { return ui.formatWidth > 0; }));
  1071. action = this.addAction('outline', mxUtils.bind(this, function()
  1072. {
  1073. if (this.outlineWindow == null)
  1074. {
  1075. // LATER: Check layers window for initial placement
  1076. this.outlineWindow = new OutlineWindow(ui, document.body.offsetWidth - 260, 100, 180, 180);
  1077. this.outlineWindow.window.addListener('show', function()
  1078. {
  1079. ui.fireEvent(new mxEventObject('outline'));
  1080. });
  1081. this.outlineWindow.window.addListener('hide', function()
  1082. {
  1083. ui.fireEvent(new mxEventObject('outline'));
  1084. });
  1085. this.outlineWindow.window.setVisible(true);
  1086. ui.fireEvent(new mxEventObject('outline'));
  1087. }
  1088. else
  1089. {
  1090. this.outlineWindow.window.setVisible(!this.outlineWindow.window.isVisible());
  1091. }
  1092. ui.fireEvent(new mxEventObject('outline'));
  1093. }), null, null, 'Ctrl+Shift+O');
  1094. action.setToggleAction(true);
  1095. action.setSelectedCallback(mxUtils.bind(this, function() { return this.outlineWindow != null && this.outlineWindow.window.isVisible(); }));
  1096. };
  1097. /**
  1098. * Registers the given action under the given name.
  1099. */
  1100. Actions.prototype.addAction = function(key, funct, enabled, iconCls, shortcut)
  1101. {
  1102. var title;
  1103. if (key.substring(key.length - 3) == '...')
  1104. {
  1105. key = key.substring(0, key.length - 3);
  1106. title = mxResources.get(key) + '...';
  1107. }
  1108. else
  1109. {
  1110. title = mxResources.get(key);
  1111. }
  1112. return this.put(key, new Action(title, funct, enabled, iconCls, shortcut));
  1113. };
  1114. /**
  1115. * Registers the given action under the given name.
  1116. */
  1117. Actions.prototype.put = function(name, action)
  1118. {
  1119. this.actions[name] = action;
  1120. return action;
  1121. };
  1122. /**
  1123. * Returns the action for the given name or null if no such action exists.
  1124. */
  1125. Actions.prototype.get = function(name)
  1126. {
  1127. return this.actions[name];
  1128. };
  1129. /**
  1130. * Constructs a new action for the given parameters.
  1131. */
  1132. function Action(label, funct, enabled, iconCls, shortcut)
  1133. {
  1134. mxEventSource.call(this);
  1135. this.label = label;
  1136. this.funct = funct;
  1137. this.enabled = (enabled != null) ? enabled : true;
  1138. this.iconCls = iconCls;
  1139. this.shortcut = shortcut;
  1140. this.visible = true;
  1141. };
  1142. // Action inherits from mxEventSource
  1143. mxUtils.extend(Action, mxEventSource);
  1144. /**
  1145. * Sets the enabled state of the action and fires a stateChanged event.
  1146. */
  1147. Action.prototype.setEnabled = function(value)
  1148. {
  1149. if (this.enabled != value)
  1150. {
  1151. this.enabled = value;
  1152. this.fireEvent(new mxEventObject('stateChanged'));
  1153. }
  1154. };
  1155. /**
  1156. * Sets the enabled state of the action and fires a stateChanged event.
  1157. */
  1158. Action.prototype.isEnabled = function()
  1159. {
  1160. return this.enabled;
  1161. };
  1162. /**
  1163. * Sets the enabled state of the action and fires a stateChanged event.
  1164. */
  1165. Action.prototype.setToggleAction = function(value)
  1166. {
  1167. this.toggleAction = value;
  1168. };
  1169. /**
  1170. * Sets the enabled state of the action and fires a stateChanged event.
  1171. */
  1172. Action.prototype.setSelectedCallback = function(funct)
  1173. {
  1174. this.selectedCallback = funct;
  1175. };
  1176. /**
  1177. * Sets the enabled state of the action and fires a stateChanged event.
  1178. */
  1179. Action.prototype.isSelected = function()
  1180. {
  1181. return this.selectedCallback();
  1182. };