Pages.js 31 KB

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