Pages.js 30 KB

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