Pages.js 30 KB

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