Pages.js 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215
  1. /**
  2. * Copyright (c) 2006-2016, JGraph Ltd
  3. * Copyright (c) 2006-2016, Gaudenz Alder
  4. */
  5. /**
  6. * Constructs a new point for the optional x and y coordinates. If no
  7. * coordinates are given, then the default values for <x> and <y> are used.
  8. * @constructor
  9. * @class Implements a basic 2D point. Known subclassers = {@link mxRectangle}.
  10. * @param {number} x X-coordinate of the point.
  11. * @param {number} y Y-coordinate of the point.
  12. */
  13. /**
  14. * Global types
  15. */
  16. function DiagramPage(node)
  17. {
  18. this.node = node;
  19. }
  20. /**
  21. * Holds the diagram node for the page.
  22. */
  23. DiagramPage.prototype.node = null;
  24. /**
  25. * Holds the root cell for the page.
  26. */
  27. DiagramPage.prototype.root = null;
  28. /**
  29. * Holds the view state for the page.
  30. */
  31. DiagramPage.prototype.viewState = null;
  32. /**
  33. *
  34. */
  35. DiagramPage.prototype.getName = function()
  36. {
  37. return this.node.getAttribute('name');
  38. };
  39. /**
  40. *
  41. */
  42. DiagramPage.prototype.setName = function(value)
  43. {
  44. if (value == null)
  45. {
  46. this.node.removeAttribute('name');
  47. }
  48. else
  49. {
  50. this.node.setAttribute('name', value);
  51. }
  52. };
  53. /**
  54. * Change types
  55. */
  56. function RenamePage(ui, page, name)
  57. {
  58. this.ui = ui;
  59. this.page = page;
  60. this.previous = name;
  61. }
  62. /**
  63. * Implementation of the undoable page rename.
  64. */
  65. RenamePage.prototype.execute = function()
  66. {
  67. var tmp = this.page.getName();
  68. this.page.setName(this.previous);
  69. this.previous = tmp;
  70. // Required to update page name in placeholders
  71. this.ui.editor.graph.updatePlaceholders();
  72. this.ui.editor.fireEvent(new mxEventObject('pageRenamed'));
  73. };
  74. /**
  75. * Undoable change of page title.
  76. */
  77. function MovePage(ui, oldIndex, newIndex)
  78. {
  79. this.ui = ui;
  80. this.oldIndex = oldIndex;
  81. this.newIndex = newIndex;
  82. }
  83. /**
  84. * Implementation of the undoable page rename.
  85. */
  86. MovePage.prototype.execute = function()
  87. {
  88. this.ui.pages.splice(this.newIndex, 0, this.ui.pages.splice(this.oldIndex, 1)[0]);
  89. var tmp = this.oldIndex;
  90. this.oldIndex = this.newIndex;
  91. this.newIndex = tmp;
  92. // Required to update page numbers in placeholders
  93. this.ui.editor.graph.updatePlaceholders();
  94. this.ui.editor.fireEvent(new mxEventObject('pageMoved'));
  95. };
  96. /**
  97. * Class: mxCurrentRootChange
  98. *
  99. * Action to change the current root in a view.
  100. *
  101. * Constructor: mxCurrentRootChange
  102. *
  103. * Constructs a change of the current root in the given view.
  104. */
  105. function SelectPage(ui, page)
  106. {
  107. this.ui = ui;
  108. this.page = page;
  109. this.previousPage = page;
  110. this.neverShown = true;
  111. if (page != null)
  112. {
  113. this.neverShown = page.viewState == null;
  114. this.ui.updatePageRoot(page);
  115. }
  116. };
  117. /**
  118. * Executes selection of a new page.
  119. */
  120. SelectPage.prototype.execute = function()
  121. {
  122. var prevIndex = mxUtils.indexOf(this.ui.pages, this.previousPage);
  123. if (this.page != null && prevIndex >= 0)
  124. {
  125. var page = this.ui.currentPage;
  126. var editor = this.ui.editor;
  127. var graph = editor.graph;
  128. // Stores current diagram state in the page
  129. var data = editor.graph.compress(graph.zapGremlins(mxUtils.getXml(editor.getGraphXml(true))));
  130. mxUtils.setTextContent(page.node, data);
  131. page.viewState = graph.getViewState();
  132. page.root = graph.model.root;
  133. // Transitions for switching pages
  134. // var curIndex = mxUtils.indexOf(this.ui.pages, page);
  135. // mxUtils.setPrefixedStyle(graph.view.canvas.style, 'transition', null);
  136. // mxUtils.setPrefixedStyle(graph.view.canvas.style, 'transform',
  137. // (curIndex > prevIndex) ? 'translate(-50%,0)' : 'translate(50%,0)');
  138. // Removes the previous cells and clears selection
  139. graph.view.clear(page.root, true);
  140. graph.clearSelection();
  141. // Switches the current page
  142. this.ui.currentPage = this.previousPage;
  143. this.previousPage = page;
  144. page = this.ui.currentPage;
  145. // Switches the root cell and sets the view state
  146. graph.model.rootChanged(page.root);
  147. graph.setViewState(page.viewState);
  148. // Fires event to setting view state from realtime
  149. editor.fireEvent(new mxEventObject('setViewState', 'change', this));
  150. // Handles grid state in chromeless mode which is stored in Editor instance
  151. graph.gridEnabled = graph.gridEnabled && (!this.ui.editor.chromeless ||
  152. urlParams['grid'] == '1');
  153. // Updates the display
  154. editor.updateGraphComponents();
  155. graph.view.validate();
  156. graph.sizeDidChange();
  157. // mxUtils.setPrefixedStyle(graph.view.canvas.style, 'transition', 'transform 0.2s');
  158. // mxUtils.setPrefixedStyle(graph.view.canvas.style, 'transform', 'translate(0,0)');
  159. if (this.neverShown)
  160. {
  161. this.neverShown = false;
  162. graph.selectUnlockedLayer();
  163. }
  164. // Fires events
  165. editor.graph.fireEvent(new mxEventObject(mxEvent.ROOT));
  166. editor.fireEvent(new mxEventObject('pageSelected', 'change', this));
  167. }
  168. };
  169. /**
  170. *
  171. */
  172. function ChangePage(ui, page, select, index)
  173. {
  174. SelectPage.call(this, ui, select);
  175. this.relatedPage = page;
  176. this.index = index;
  177. this.previousIndex = null;
  178. };
  179. mxUtils.extend(ChangePage, SelectPage);
  180. /**
  181. * Function: execute
  182. *
  183. * Changes the current root of the view.
  184. */
  185. ChangePage.prototype.execute = function()
  186. {
  187. // Fires event to setting view state from realtime
  188. this.ui.editor.fireEvent(new mxEventObject('beforePageChange', 'change', this));
  189. this.previousIndex = this.index;
  190. if (this.index == null)
  191. {
  192. var tmp = mxUtils.indexOf(this.ui.pages, this.relatedPage);
  193. this.ui.pages.splice(tmp, 1);
  194. this.index = tmp;
  195. }
  196. else
  197. {
  198. this.ui.pages.splice(this.index, 0, this.relatedPage);
  199. this.index = null;
  200. }
  201. SelectPage.prototype.execute.apply(this, arguments);
  202. };
  203. /**
  204. * Returns true if the given string contains an mxfile.
  205. */
  206. EditorUi.prototype.initPages = function()
  207. {
  208. this.actions.addAction('previousPage', mxUtils.bind(this, function()
  209. {
  210. this.selectNextPage(false);
  211. }));
  212. this.actions.addAction('nextPage', mxUtils.bind(this, function()
  213. {
  214. this.selectNextPage(true);
  215. }));
  216. this.keyHandler.bindAction(33, true, 'previousPage', true); // Ctrl+Shift+PageUp
  217. this.keyHandler.bindAction(34, true, 'nextPage', true); // Ctrl+Shift+PageDown
  218. // Updates the tabs after loading the diagram
  219. var graph = this.editor.graph;
  220. var graphViewValidateBackground = graph.view.validateBackground;
  221. graph.view.validateBackground = mxUtils.bind(this, function()
  222. {
  223. if (this.tabContainer != null)
  224. {
  225. var prevHeight = this.tabContainer.style.height;
  226. if (this.fileNode == null || this.pages == null ||
  227. (this.pages.length == 1 && urlParams['pages'] == '0'))
  228. {
  229. this.tabContainer.style.height = '0px';
  230. }
  231. else
  232. {
  233. this.tabContainer.style.height = '30px';
  234. }
  235. if (prevHeight != this.tabContainer.style.height)
  236. {
  237. this.refresh(false);
  238. }
  239. }
  240. graphViewValidateBackground.apply(graph.view, arguments);
  241. });
  242. // Math workaround is only needed for initial rendering
  243. var ignorePendingMath = false;
  244. var lastPage = null;
  245. var updateTabs = mxUtils.bind(this, function()
  246. {
  247. this.updateTabContainer();
  248. // Updates scrollbar positions and backgrounds after validation
  249. var p = this.currentPage;
  250. if (p != null && p != lastPage)
  251. {
  252. if (p.viewState == null || p.viewState.scrollLeft == null)
  253. {
  254. this.resetScrollbars();
  255. if (graph.lightbox)
  256. {
  257. this.lightboxFit();
  258. }
  259. if (this.chromelessResize != null)
  260. {
  261. graph.container.scrollLeft = 0;
  262. graph.container.scrollTop = 0;
  263. this.chromelessResize();
  264. }
  265. }
  266. else
  267. {
  268. graph.container.scrollLeft = graph.view.translate.x * graph.view.scale + p.viewState.scrollLeft;
  269. graph.container.scrollTop = graph.view.translate.y * graph.view.scale + p.viewState.scrollTop;
  270. }
  271. lastPage = p;
  272. }
  273. // Updates layers window
  274. if (this.actions.layersWindow != null)
  275. {
  276. this.actions.layersWindow.refreshLayers();
  277. }
  278. // Workaround for math if tab is switched before typesetting has stopped
  279. if (typeof(MathJax) !== 'undefined' && typeof(MathJax.Hub) !== 'undefined')
  280. {
  281. // Pending math should not be rendered if the graph has no math enabled
  282. if (!ignorePendingMath)
  283. {
  284. if (MathJax.Hub.queue.pending == 1 && !this.editor.graph.mathEnabled)
  285. {
  286. // Since there is no way to stop/undo mathjax or
  287. // clear the queue, we do a refresh after typeset
  288. MathJax.Hub.Queue(mxUtils.bind(this, function()
  289. {
  290. this.editor.graph.refresh();
  291. }));
  292. }
  293. MathJax.Hub.Queue(mxUtils.bind(this, function()
  294. {
  295. ignorePendingMath = true;
  296. }));
  297. }
  298. }
  299. else if (typeof(Editor.MathJaxClear) !== 'undefined' && !this.editor.graph.mathEnabled)
  300. {
  301. // Clears our own queue for async loading
  302. ignorePendingMath = true;
  303. Editor.MathJaxClear();
  304. }
  305. });
  306. // Adds a graph model listener to update the view
  307. this.editor.graph.model.addListener(mxEvent.CHANGE, mxUtils.bind(this, function(sender, evt)
  308. {
  309. var edit = evt.getProperty('edit');
  310. var changes = edit.changes;
  311. for (var i = 0; i < changes.length; i++)
  312. {
  313. if (changes[i] instanceof SelectPage ||
  314. changes[i] instanceof RenamePage ||
  315. changes[i] instanceof MovePage ||
  316. changes[i] instanceof mxRootChange)
  317. {
  318. updateTabs();
  319. break;
  320. }
  321. }
  322. }));
  323. // Updates zoom in toolbar
  324. if (this.toolbar != null)
  325. {
  326. this.editor.addListener('pageSelected', this.toolbar.updateZoom);
  327. }
  328. };
  329. /**
  330. * Overrides setDefaultParent
  331. */
  332. Graph.prototype.createViewState = function(node)
  333. {
  334. var pv = node.getAttribute('page');
  335. var ps = node.getAttribute('pageScale');
  336. var pw = node.getAttribute('pageWidth');
  337. var ph = node.getAttribute('pageHeight');
  338. var bg = node.getAttribute('background');
  339. var temp = node.getAttribute('backgroundImage');
  340. var bgImg = (temp != null && temp.length > 0) ? JSON.parse(temp) : null;
  341. return {
  342. gridEnabled: node.getAttribute('grid') != '0',
  343. //gridColor: node.getAttribute('gridColor') || mxSettings.getGridColor(),
  344. gridSize: parseFloat(node.getAttribute('gridSize')) || mxGraph.prototype.gridSize,
  345. guidesEnabled: node.getAttribute('guides') != '0',
  346. foldingEnabled: node.getAttribute('fold') != '0',
  347. shadowVisible: node.getAttribute('shadow') == '1',
  348. pageVisible: (this.lightbox) ? false : ((pv != null) ? (pv != '0') : this.defaultPageVisible),
  349. background: (bg != null && bg.length > 0) ? bg : this.defaultGraphBackground,
  350. backgroundImage: (bgImg != null) ? new mxImage(bgImg.src, bgImg.width, bgImg.height) : null,
  351. pageScale: (ps != null) ? ps : mxGraph.prototype.pageScale,
  352. pageFormat: (pw != null && ph != null) ? new mxRectangle(0, 0,
  353. parseFloat(pw), parseFloat(ph)) : this.pageFormat,
  354. tooltips: node.getAttribute('tooltips') != '0',
  355. connect: node.getAttribute('connect') != '0',
  356. arrows: node.getAttribute('arrows') != '0',
  357. mathEnabled: node.getAttribute('math') != '0',
  358. selectionCells: null,
  359. defaultParent: null,
  360. scrollbars: this.defaultScrollbars,
  361. scale: 1
  362. };
  363. };
  364. /**
  365. * Overrides setDefaultParent
  366. */
  367. Graph.prototype.getViewState = function()
  368. {
  369. return {
  370. defaultParent: this.defaultParent,
  371. currentRoot: this.view.currentRoot,
  372. gridEnabled: this.gridEnabled,
  373. //gridColor: this.view.gridColor,
  374. gridSize: this.gridSize,
  375. guidesEnabled: this.graphHandler.guidesEnabled,
  376. foldingEnabled: this.foldingEnabled,
  377. shadowVisible: this.shadowVisible,
  378. scrollbars: this.scrollbars,
  379. pageVisible: this.pageVisible,
  380. background: this.background,
  381. backgroundImage: this.backgroundImage,
  382. pageScale: this.pageScale,
  383. pageFormat: this.pageFormat,
  384. tooltips: this.tooltipHandler.isEnabled(),
  385. connect: this.connectionHandler.isEnabled(),
  386. arrows: this.connectionArrowsEnabled,
  387. scale: this.view.scale,
  388. scrollLeft: this.container.scrollLeft - this.view.translate.x * this.view.scale,
  389. scrollTop: this.container.scrollTop - this.view.translate.y * this.view.scale,
  390. translate: this.view.translate.clone(),
  391. lastPasteXml: this.lastPasteXml,
  392. pasteCounter: this.pasteCounter,
  393. mathEnabled: this.mathEnabled
  394. };
  395. };
  396. /**
  397. * Overrides setDefaultParent
  398. */
  399. Graph.prototype.setViewState = function(state)
  400. {
  401. if (state != null)
  402. {
  403. this.lastPasteXml = state.lastPasteXml;
  404. this.pasteCounter = state.pasteCounter || 0;
  405. this.mathEnabled = state.mathEnabled;
  406. this.gridEnabled = state.gridEnabled;
  407. //this.view.gridColor = state.gridColor;
  408. this.gridSize = state.gridSize;
  409. this.graphHandler.guidesEnabled = state.guidesEnabled;
  410. this.foldingEnabled = state.foldingEnabled;
  411. this.setShadowVisible(state.shadowVisible, false);
  412. this.scrollbars = state.scrollbars;
  413. this.pageVisible = state.pageVisible;
  414. this.background = state.background;
  415. this.backgroundImage = state.backgroundImage;
  416. this.pageScale = state.pageScale;
  417. this.pageFormat = state.pageFormat;
  418. this.view.scale = state.scale;
  419. this.view.currentRoot = state.currentRoot;
  420. this.defaultParent = state.defaultParent;
  421. this.connectionArrowsEnabled = state.arrows;
  422. this.setTooltips(state.tooltips);
  423. this.setConnectable(state.connect);
  424. // Checks if current root or default parent have been removed
  425. if (!this.model.contains(this.view.currentRoot))
  426. {
  427. this.view.currentRoot = null;
  428. }
  429. if (!this.model.contains(this.defaultParent))
  430. {
  431. this.setDefaultParent(null);
  432. this.selectUnlockedLayer();
  433. }
  434. if (state.translate != null)
  435. {
  436. this.view.translate = state.translate;
  437. }
  438. }
  439. else
  440. {
  441. this.view.currentRoot = null;
  442. this.view.scale = 1;
  443. this.gridEnabled = true;
  444. this.gridSize = mxGraph.prototype.gridSize;
  445. this.pageScale = mxGraph.prototype.pageScale;
  446. this.pageFormat = mxSettings.getPageFormat();
  447. this.pageVisible = this.defaultPageVisible;
  448. this.background = this.defaultGraphBackground;
  449. this.backgroundImage = null;
  450. this.scrollbars = this.defaultScrollbars;
  451. this.graphHandler.guidesEnabled = true;
  452. this.foldingEnabled = true;
  453. this.defaultParent = null;
  454. this.setTooltips(true);
  455. this.setConnectable(true);
  456. this.lastPasteXml = null;
  457. this.pasteCounter = 0;
  458. this.mathEnabled = false;
  459. this.connectionArrowsEnabled = true;
  460. }
  461. // Implicit settings
  462. this.pageBreaksVisible = this.pageVisible;
  463. this.preferPageSize = this.pageVisible;
  464. };
  465. /**
  466. * Executes selection of a new page.
  467. */
  468. EditorUi.prototype.updatePageRoot = function(page)
  469. {
  470. if (page.root == null)
  471. {
  472. var node = this.editor.extractGraphModel(page.node);
  473. if (node != null)
  474. {
  475. page.graphModelNode = node;
  476. // Converts model XML into page object with root cell
  477. page.viewState = this.editor.graph.createViewState(node);
  478. var codec = new mxCodec(node.ownerDocument);
  479. page.root = codec.decode(node).root;
  480. }
  481. else
  482. {
  483. // Initializes page object with new empty root
  484. page.root = this.editor.graph.model.createRoot();
  485. }
  486. }
  487. return page;
  488. };
  489. /**
  490. * Returns true if the given string contains an mxfile.
  491. */
  492. EditorUi.prototype.selectPage = function(page)
  493. {
  494. this.editor.graph.stopEditing();
  495. var edit = this.editor.graph.model.createUndoableEdit();
  496. // Special flag to bypass autosave for this edit
  497. edit.ignoreEdit = true;
  498. var change = new SelectPage(this, page);
  499. change.execute();
  500. edit.add(change);
  501. edit.notify();
  502. this.editor.graph.model.fireEvent(new mxEventObject(mxEvent.UNDO, 'edit', edit));
  503. };
  504. /**
  505. *
  506. */
  507. EditorUi.prototype.selectNextPage = function(forward)
  508. {
  509. var next = this.currentPage;
  510. if (next != null && this.pages != null)
  511. {
  512. var tmp = mxUtils.indexOf(this.pages, next);
  513. if (forward)
  514. {
  515. this.selectPage(this.pages[mxUtils.mod(tmp + 1, this.pages.length)]);
  516. }
  517. else if (!forward)
  518. {
  519. this.selectPage(this.pages[mxUtils.mod(tmp - 1, this.pages.length)]);
  520. }
  521. }
  522. };
  523. /**
  524. * Returns true if the given string contains an mxfile.
  525. */
  526. EditorUi.prototype.insertPage = function(page, index)
  527. {
  528. if (this.editor.graph.isEnabled())
  529. {
  530. page = (page != null) ? page : this.createPage();
  531. index = (index != null) ? index : this.pages.length;
  532. // Uses model to fire event and trigger autosave
  533. var change = new ChangePage(this, page, page, index);
  534. this.editor.graph.model.execute(change);
  535. }
  536. return page;
  537. };
  538. /**
  539. * Returns true if the given string contains an mxfile.
  540. */
  541. EditorUi.prototype.createPage = function(name)
  542. {
  543. var page = new DiagramPage(this.fileNode.ownerDocument.createElement('diagram'));
  544. page.setName((name != null) ? name : this.createPageName());
  545. return page;
  546. };
  547. /**
  548. * Returns true if the given string contains an mxfile.
  549. */
  550. EditorUi.prototype.createPageName = function()
  551. {
  552. // Creates a lookup with names
  553. var existing = {};
  554. for (var i = 0; i < this.pages.length; i++)
  555. {
  556. var tmp = this.pages[i].getName();
  557. if (tmp != null && tmp.length > 0)
  558. {
  559. existing[tmp] = tmp;
  560. }
  561. }
  562. // Avoids existing names
  563. var nr = this.pages.length;
  564. var name = null;
  565. do
  566. {
  567. name = mxResources.get('pageWithNumber', [++nr]);
  568. }
  569. while (existing[name] != null);
  570. return name;
  571. };
  572. /**
  573. * Returns true if the given string contains an mxfile.
  574. */
  575. EditorUi.prototype.removePage = function(page)
  576. {
  577. var graph = this.editor.graph;
  578. if (graph.isEnabled())
  579. {
  580. graph.model.beginUpdate();
  581. try
  582. {
  583. var next = this.currentPage;
  584. if (next == page)
  585. {
  586. if (this.pages.length > 1)
  587. {
  588. var tmp = mxUtils.indexOf(this.pages, page);
  589. if (tmp == this.pages.length - 1)
  590. {
  591. tmp--;
  592. }
  593. else
  594. {
  595. tmp++;
  596. }
  597. next = this.pages[tmp];
  598. }
  599. else
  600. {
  601. // Removes label with incorrect page number to force
  602. // default page name which is OK for a single page
  603. next = this.insertPage();
  604. graph.model.execute(new RenamePage(this, next, mxResources.get('pageWithNumber', [1])));
  605. }
  606. }
  607. // Uses model to fire event to trigger autosave
  608. graph.model.execute(new ChangePage(this, page, next));
  609. }
  610. finally
  611. {
  612. graph.model.endUpdate();
  613. }
  614. }
  615. return page;
  616. };
  617. /**
  618. * Returns true if the given string contains an mxfile.
  619. */
  620. EditorUi.prototype.duplicatePage = function(page, name)
  621. {
  622. var graph = this.editor.graph;
  623. var newPage = null;
  624. if (graph.isEnabled())
  625. {
  626. if (graph.isEditing())
  627. {
  628. graph.stopEditing();
  629. }
  630. // Clones the current page and takes a snapshot of the graph model and view state
  631. var newPage = new DiagramPage(page.node.cloneNode(false));
  632. newPage.root = graph.cloneCells([graph.model.root])[0];
  633. newPage.viewState = graph.getViewState();
  634. // Resets zoom and scrollbar positions
  635. newPage.viewState.scale = 1;
  636. newPage.viewState.scrollLeft = null;
  637. newPage.viewState.scrollRight = null;
  638. newPage.setName(name);
  639. newPage = this.insertPage(newPage, mxUtils.indexOf(this.pages, page) + 1);
  640. }
  641. return newPage;
  642. };
  643. /**
  644. * Returns true if the given string contains an mxfile.
  645. */
  646. EditorUi.prototype.renamePage = function(page)
  647. {
  648. var graph = this.editor.graph;
  649. if (graph.isEnabled())
  650. {
  651. var dlg = new FilenameDialog(this, page.getName(), mxResources.get('rename'), mxUtils.bind(this, function(name)
  652. {
  653. if (name != null && name.length > 0)
  654. {
  655. this.editor.graph.model.execute(new RenamePage(this, page, name));
  656. }
  657. }), mxResources.get('rename'));
  658. this.showDialog(dlg.container, 300, 80, true, true);
  659. dlg.init();
  660. }
  661. return page;
  662. }
  663. /**
  664. * Returns true if the given string contains an mxfile.
  665. */
  666. EditorUi.prototype.movePage = function(oldIndex, newIndex)
  667. {
  668. this.editor.graph.model.execute(new MovePage(this, oldIndex, newIndex));
  669. }
  670. /**
  671. * Returns true if the given string contains an mxfile.
  672. */
  673. EditorUi.prototype.createTabContainer = function()
  674. {
  675. var div = document.createElement('div');
  676. div.style.backgroundColor = '#dcdcdc';
  677. div.style.position = 'absolute';
  678. div.style.whiteSpace = 'nowrap';
  679. div.style.overflow = 'hidden';
  680. div.style.height = '0px';
  681. return div;
  682. };
  683. /**
  684. * Returns true if the given string contains an mxfile.
  685. */
  686. EditorUi.prototype.updateTabContainer = function()
  687. {
  688. if (this.tabContainer != null && this.pages != null)
  689. {
  690. var graph = this.editor.graph;
  691. var wrapper = document.createElement('div');
  692. wrapper.style.position = 'relative';
  693. wrapper.style.display = (mxClient.IS_QUIRKS) ? 'inline' : 'inline-block';
  694. wrapper.style.verticalAlign = 'top';
  695. wrapper.style.height = this.tabContainer.style.height;
  696. wrapper.style.whiteSpace = 'nowrap';
  697. wrapper.style.overflow = 'hidden';
  698. wrapper.style.fontSize = '12px';
  699. // Allows for negative left margin of first tab
  700. wrapper.style.marginLeft = '30px';
  701. // Automatic tab width to match available width
  702. // TODO: Fix tabWidth in chromeless mode
  703. var btnWidth = (this.editor.chromeless) ? 29 : 59;
  704. var tabWidth = Math.min(140, Math.max(20, (this.tabContainer.clientWidth - btnWidth) / this.pages.length) + 1);
  705. var startIndex = null;
  706. for (var i = 0; i < this.pages.length; i++)
  707. {
  708. // Install drag and drop for page reorder
  709. (mxUtils.bind(this, function(index, tab)
  710. {
  711. if (this.pages[index] == this.currentPage)
  712. {
  713. tab.style.backgroundColor = '#eeeeee';
  714. tab.style.fontWeight = 'bold';
  715. tab.style.borderTopStyle = 'none';
  716. }
  717. tab.setAttribute('draggable', 'true');
  718. mxEvent.addListener(tab, 'dragstart', mxUtils.bind(this, function(evt)
  719. {
  720. if (graph.isEnabled())
  721. {
  722. // Workaround for no DnD on DIV in FF
  723. if (mxClient.IS_FF)
  724. {
  725. // LATER: Check what triggers a parse as XML on this in FF after drop
  726. evt.dataTransfer.setData('Text', '<diagram/>');
  727. }
  728. startIndex = index;
  729. }
  730. else
  731. {
  732. // Blocks event
  733. mxEvent.consume(evt);
  734. }
  735. }));
  736. mxEvent.addListener(tab, 'dragend', mxUtils.bind(this, function(evt)
  737. {
  738. startIndex = null;
  739. evt.stopPropagation();
  740. evt.preventDefault();
  741. }));
  742. mxEvent.addListener(tab, 'dragover', mxUtils.bind(this, function(evt)
  743. {
  744. if (startIndex != null)
  745. {
  746. evt.dataTransfer.dropEffect = 'move';
  747. }
  748. evt.stopPropagation();
  749. evt.preventDefault();
  750. }));
  751. mxEvent.addListener(tab, 'drop', mxUtils.bind(this, function(evt)
  752. {
  753. if (startIndex != null && index != startIndex)
  754. {
  755. // TODO: Shift drag for insert/merge?
  756. this.movePage(startIndex, index);
  757. }
  758. evt.stopPropagation();
  759. evt.preventDefault();
  760. }));
  761. wrapper.appendChild(tab);
  762. }))(i, this.createTabForPage(this.pages[i], tabWidth, this.pages[i] != this.currentPage));
  763. }
  764. this.tabContainer.innerHTML = '';
  765. this.tabContainer.appendChild(wrapper);
  766. // Adds floating menu with all pages and insert option
  767. var menutab = this.createPageMenuTab();
  768. this.tabContainer.appendChild(menutab);
  769. var insertTab = null;
  770. // Not chromeless and not read-only file
  771. if (graph.isEnabled())
  772. {
  773. insertTab = this.createPageInsertTab();
  774. this.tabContainer.appendChild(insertTab);
  775. }
  776. if (wrapper.clientWidth > this.tabContainer.clientWidth - btnWidth)
  777. {
  778. if (insertTab != null)
  779. {
  780. insertTab.style.position = 'absolute';
  781. insertTab.style.right = '0px';
  782. wrapper.style.marginRight = '30px';
  783. }
  784. var temp = this.createControlTab(4, '&nbsp;&#10094;&nbsp;');
  785. temp.style.position = 'absolute';
  786. temp.style.right = (this.editor.chromeless) ? '29px' : '55px';
  787. temp.style.fontSize = '13pt';
  788. this.tabContainer.appendChild(temp);
  789. var temp2 = this.createControlTab(4, '&nbsp;&#10095;');
  790. temp2.style.position = 'absolute';
  791. temp2.style.right = (this.editor.chromeless) ? '0px' : '29px';
  792. temp2.style.fontSize = '13pt';
  793. this.tabContainer.appendChild(temp2);
  794. // TODO: Scroll to current page
  795. var dx = Math.max(0, this.tabContainer.clientWidth - ((this.editor.chromeless) ? 86 : 116));
  796. wrapper.style.width = dx + 'px';
  797. var fade = 50;
  798. mxEvent.addListener(temp, 'click', mxUtils.bind(this, function(evt)
  799. {
  800. wrapper.scrollLeft -= Math.max(20, dx - 20);
  801. mxUtils.setOpacity(temp, (wrapper.scrollLeft > 0) ? 100 : fade);
  802. mxUtils.setOpacity(temp2, (wrapper.scrollLeft < wrapper.scrollWidth - wrapper.clientWidth) ? 100 : fade);
  803. mxEvent.consume(evt);
  804. }));
  805. mxUtils.setOpacity(temp, (wrapper.scrollLeft > 0) ? 100 : fade);
  806. mxUtils.setOpacity(temp2, (wrapper.scrollLeft < wrapper.scrollWidth - wrapper.clientWidth) ? 100 : fade);
  807. mxEvent.addListener(temp2, 'click', mxUtils.bind(this, function(evt)
  808. {
  809. wrapper.scrollLeft += Math.max(20, dx - 20);
  810. mxUtils.setOpacity(temp, (wrapper.scrollLeft > 0) ? 100 : fade);
  811. mxUtils.setOpacity(temp2, (wrapper.scrollLeft < wrapper.scrollWidth - wrapper.clientWidth) ? 100 : fade);
  812. mxEvent.consume(evt);
  813. }));
  814. }
  815. }
  816. };
  817. /**
  818. * Returns true if the given string contains an mxfile.
  819. */
  820. EditorUi.prototype.createTab = function(hoverEnabled)
  821. {
  822. var tab = document.createElement('div');
  823. tab.style.display = (mxClient.IS_QUIRKS) ? 'inline' : 'inline-block';
  824. tab.style.whiteSpace = 'nowrap';
  825. tab.style.boxSizing = 'border-box';
  826. tab.style.position = 'relative';
  827. tab.style.overflow = 'hidden';
  828. tab.style.marginLeft = '-1px';
  829. tab.style.height = this.tabContainer.clientHeight + 'px';
  830. tab.style.padding = '8px 4px 8px 4px';
  831. tab.style.border = '1px solid #c0c0c0';
  832. tab.style.borderBottomStyle = 'solid';
  833. tab.style.backgroundColor = this.tabContainer.style.backgroundColor;
  834. tab.style.cursor = 'default';
  835. tab.style.color = 'gray';
  836. if (hoverEnabled)
  837. {
  838. mxEvent.addListener(tab, 'mouseenter', mxUtils.bind(this, function(evt)
  839. {
  840. if (!this.editor.graph.isMouseDown)
  841. {
  842. tab.style.backgroundColor = '#d3d3d3';
  843. mxEvent.consume(evt);
  844. }
  845. }));
  846. mxEvent.addListener(tab, 'mouseleave', mxUtils.bind(this, function(evt)
  847. {
  848. tab.style.backgroundColor = this.tabContainer.style.backgroundColor;
  849. mxEvent.consume(evt);
  850. }));
  851. }
  852. return tab;
  853. };
  854. /**
  855. * Returns true if the given string contains an mxfile.
  856. */
  857. EditorUi.prototype.createControlTab = function(paddingTop, html)
  858. {
  859. var tab = this.createTab(true);
  860. tab.style.paddingTop = paddingTop + 'px';
  861. tab.style.cursor = 'pointer';
  862. tab.style.width = '30px';
  863. tab.style.lineHeight = '30px';
  864. tab.innerHTML = html;
  865. if (tab.firstChild != null && tab.firstChild.style != null)
  866. {
  867. mxUtils.setOpacity(tab.firstChild, 40);
  868. }
  869. return tab;
  870. };
  871. /**
  872. * Returns true if the given string contains an mxfile.
  873. */
  874. EditorUi.prototype.createPageMenuTab = function()
  875. {
  876. var tab = this.createControlTab(3, '<div class="geSprite geSprite-dots" style="display:inline-block;width:21px;height:21px;"></div>');
  877. tab.setAttribute('title', mxResources.get('pages'));
  878. tab.style.position = 'absolute';
  879. tab.style.left = '1px';
  880. mxEvent.addListener(tab, 'click', mxUtils.bind(this, function(evt)
  881. {
  882. this.editor.graph.popupMenuHandler.hideMenu();
  883. var menu = new mxPopupMenu(mxUtils.bind(this, function(menu, parent)
  884. {
  885. for (var i = 0; i < this.pages.length; i++)
  886. {
  887. (mxUtils.bind(this, function(index)
  888. {
  889. var item = menu.addItem(this.pages[index].getName(), null, mxUtils.bind(this, function()
  890. {
  891. this.selectPage(this.pages[index]);
  892. }), parent);
  893. // Adds checkmark to current page
  894. if (this.pages[index] == this.currentPage)
  895. {
  896. menu.addCheckmark(item, Editor.checkmarkImage);
  897. }
  898. }))(i);
  899. }
  900. if (this.editor.graph.isEnabled())
  901. {
  902. menu.addSeparator(parent);
  903. var item = menu.addItem(mxResources.get('insertPage'), null, mxUtils.bind(this, function()
  904. {
  905. this.insertPage();
  906. }), parent);
  907. }
  908. }));
  909. menu.div.className += ' geMenubarMenu';
  910. menu.smartSeparators = true;
  911. menu.showDisabled = true;
  912. menu.autoExpand = true;
  913. // Disables autoexpand and destroys menu when hidden
  914. menu.hideMenu = mxUtils.bind(this, function()
  915. {
  916. mxPopupMenu.prototype.hideMenu.apply(menu, arguments);
  917. menu.destroy();
  918. });
  919. var x = mxEvent.getClientX(evt);
  920. var y = mxEvent.getClientY(evt);
  921. menu.popup(x, y, null, evt);
  922. // Allows hiding by clicking on document
  923. this.setCurrentMenu(menu);
  924. mxEvent.consume(evt);
  925. }));
  926. return tab;
  927. };
  928. /**
  929. * Returns true if the given string contains an mxfile.
  930. */
  931. EditorUi.prototype.createPageInsertTab = function()
  932. {
  933. var tab = this.createControlTab(4, '<div class="geSprite geSprite-plus" style="display:inline-block;width:21px;height:21px;"></div>');
  934. tab.setAttribute('title', mxResources.get('insertPage'));
  935. var graph = this.editor.graph;
  936. mxEvent.addListener(tab, 'click', mxUtils.bind(this, function(evt)
  937. {
  938. this.insertPage();
  939. mxEvent.consume(evt);
  940. }));
  941. return tab;
  942. };
  943. /**
  944. * Returns true if the given string contains an mxfile.
  945. */
  946. EditorUi.prototype.createTabForPage = function(page, tabWidth, hoverEnabled)
  947. {
  948. var tab = this.createTab(hoverEnabled);
  949. var name = page.getName();
  950. tab.setAttribute('title', name);
  951. mxUtils.write(tab, name);
  952. tab.style.maxWidth = tabWidth + 'px';
  953. tab.style.width = tabWidth + 'px';
  954. this.addTabListeners(page, tab);
  955. if (tabWidth > 42)
  956. {
  957. tab.style.textOverflow = 'ellipsis';
  958. }
  959. return tab;
  960. };
  961. /**
  962. * Translates this point by the given vector.
  963. *
  964. * @param {number} dx X-coordinate of the translation.
  965. * @param {number} dy Y-coordinate of the translation.
  966. */
  967. EditorUi.prototype.addTabListeners = function(page, tab)
  968. {
  969. mxEvent.disableContextMenu(tab);
  970. var graph = this.editor.graph;
  971. var model = graph.model;
  972. mxEvent.addListener(tab, 'dblclick', mxUtils.bind(this, function(evt)
  973. {
  974. this.renamePage(page)
  975. mxEvent.consume(evt);
  976. }));
  977. var menuWasVisible = false;
  978. var pageWasActive = false;
  979. mxEvent.addGestureListeners(tab, mxUtils.bind(this, function(evt)
  980. {
  981. // Do not consume event here to allow for drag and drop of tabs
  982. menuWasVisible = this.currentMenu != null;
  983. pageWasActive = page == this.currentPage;
  984. if (!graph.isMouseDown && !pageWasActive)
  985. {
  986. this.selectPage(page);
  987. }
  988. }), null, mxUtils.bind(this, function(evt)
  989. {
  990. if (graph.isEnabled() && !graph.isMouseDown &&
  991. ((mxEvent.isTouchEvent(evt) && pageWasActive) ||
  992. mxEvent.isPopupTrigger(evt)))
  993. {
  994. graph.popupMenuHandler.hideMenu();
  995. this.hideCurrentMenu();
  996. if (!mxEvent.isTouchEvent(evt) || !menuWasVisible)
  997. {
  998. var menu = new mxPopupMenu(this.createPageMenu(page));
  999. menu.div.className += ' geMenubarMenu';
  1000. menu.smartSeparators = true;
  1001. menu.showDisabled = true;
  1002. menu.autoExpand = true;
  1003. // Disables autoexpand and destroys menu when hidden
  1004. menu.hideMenu = mxUtils.bind(this, function()
  1005. {
  1006. mxPopupMenu.prototype.hideMenu.apply(menu, arguments);
  1007. this.resetCurrentMenu();
  1008. menu.destroy();
  1009. });
  1010. var x = mxEvent.getClientX(evt);
  1011. var y = mxEvent.getClientY(evt);
  1012. menu.popup(x, y, null, evt);
  1013. this.setCurrentMenu(menu, tab);
  1014. }
  1015. mxEvent.consume(evt);
  1016. }
  1017. }));
  1018. };
  1019. /**
  1020. * Returns true if the given string contains an mxfile.
  1021. */
  1022. EditorUi.prototype.createPageMenu = function(page, label)
  1023. {
  1024. return mxUtils.bind(this, function(menu, parent)
  1025. {
  1026. var graph = this.editor.graph;
  1027. var model = graph.model;
  1028. menu.addItem(mxResources.get('insert'), null, mxUtils.bind(this, function()
  1029. {
  1030. this.insertPage(null, mxUtils.indexOf(this.pages, page) + 1);
  1031. }), parent);
  1032. menu.addItem(mxResources.get('delete'), null, mxUtils.bind(this, function()
  1033. {
  1034. this.removePage(page);
  1035. }), parent);
  1036. menu.addItem(mxResources.get('rename'), null, mxUtils.bind(this, function()
  1037. {
  1038. this.renamePage(page, label);
  1039. }), parent);
  1040. menu.addSeparator(parent);
  1041. menu.addItem(mxResources.get('duplicate'), null, mxUtils.bind(this, function()
  1042. {
  1043. this.duplicatePage(page, mxResources.get('copyOf', [page.getName()]));
  1044. }), parent);
  1045. });
  1046. };