Pages.js 32 KB

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