Pages.js 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172
  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. }
  436. // Implicit settings
  437. this.pageBreaksVisible = this.pageVisible;
  438. this.preferPageSize = this.pageVisible;
  439. };
  440. /**
  441. * Executes selection of a new page.
  442. */
  443. EditorUi.prototype.updatePageRoot = function(page)
  444. {
  445. if (page.root == null)
  446. {
  447. var node = this.editor.extractGraphModel(page.node);
  448. if (node != null)
  449. {
  450. page.graphModelNode = node;
  451. // Converts model XML into page object with root cell
  452. page.viewState = this.editor.graph.createViewState(node);
  453. var codec = new mxCodec(node.ownerDocument);
  454. page.root = codec.decode(node).root;
  455. }
  456. else
  457. {
  458. // Initializes page object with new empty root
  459. page.root = this.editor.graph.model.createRoot();
  460. }
  461. }
  462. return page;
  463. };
  464. /**
  465. * Returns true if the given string contains an mxfile.
  466. */
  467. EditorUi.prototype.selectPage = function(page)
  468. {
  469. this.editor.graph.stopEditing();
  470. var edit = this.editor.graph.model.createUndoableEdit();
  471. // Special flag to bypass autosave for this edit
  472. edit.ignoreEdit = true;
  473. var change = new SelectPage(this, page);
  474. change.execute();
  475. edit.add(change);
  476. edit.notify();
  477. this.editor.graph.model.fireEvent(new mxEventObject(mxEvent.UNDO, 'edit', edit));
  478. };
  479. /**
  480. *
  481. */
  482. EditorUi.prototype.selectNextPage = function(forward)
  483. {
  484. var next = this.currentPage;
  485. if (next != null && this.pages != null)
  486. {
  487. var tmp = mxUtils.indexOf(this.pages, next);
  488. if (forward)
  489. {
  490. this.selectPage(this.pages[mxUtils.mod(tmp + 1, this.pages.length)]);
  491. }
  492. else if (!forward)
  493. {
  494. this.selectPage(this.pages[mxUtils.mod(tmp - 1, this.pages.length)]);
  495. }
  496. }
  497. };
  498. /**
  499. * Returns true if the given string contains an mxfile.
  500. */
  501. EditorUi.prototype.insertPage = function(page, index)
  502. {
  503. if (this.editor.graph.isEnabled())
  504. {
  505. page = (page != null) ? page : this.createPage();
  506. index = (index != null) ? index : this.pages.length;
  507. // Uses model to fire event and trigger autosave
  508. var change = new ChangePage(this, page, page, index);
  509. this.editor.graph.model.execute(change);
  510. }
  511. return page;
  512. };
  513. /**
  514. * Returns true if the given string contains an mxfile.
  515. */
  516. EditorUi.prototype.createPage = function(name)
  517. {
  518. var page = new DiagramPage(this.fileNode.ownerDocument.createElement('diagram'));
  519. page.setName((name != null) ? name : this.createPageName());
  520. return page;
  521. };
  522. /**
  523. * Returns true if the given string contains an mxfile.
  524. */
  525. EditorUi.prototype.createPageName = function()
  526. {
  527. // Creates a lookup with names
  528. var existing = {};
  529. for (var i = 0; i < this.pages.length; i++)
  530. {
  531. var tmp = this.pages[i].getName();
  532. if (tmp != null && tmp.length > 0)
  533. {
  534. existing[tmp] = tmp;
  535. }
  536. }
  537. // Avoids existing names
  538. var nr = this.pages.length;
  539. var name = null;
  540. do
  541. {
  542. name = mxResources.get('pageWithNumber', [++nr]);
  543. }
  544. while (existing[name] != null);
  545. return name;
  546. };
  547. /**
  548. * Returns true if the given string contains an mxfile.
  549. */
  550. EditorUi.prototype.removePage = function(page)
  551. {
  552. var graph = this.editor.graph;
  553. if (graph.isEnabled())
  554. {
  555. graph.model.beginUpdate();
  556. try
  557. {
  558. var next = this.currentPage;
  559. if (next == page)
  560. {
  561. if (this.pages.length > 1)
  562. {
  563. var tmp = mxUtils.indexOf(this.pages, page);
  564. if (tmp == this.pages.length - 1)
  565. {
  566. tmp--;
  567. }
  568. else
  569. {
  570. tmp++;
  571. }
  572. next = this.pages[tmp];
  573. }
  574. else
  575. {
  576. // Removes label with incorrect page number to force
  577. // default page name which is OK for a single page
  578. next = this.insertPage();
  579. graph.model.execute(new RenamePage(this, next, mxResources.get('pageWithNumber', [1])));
  580. }
  581. }
  582. // Uses model to fire event to trigger autosave
  583. graph.model.execute(new ChangePage(this, page, next));
  584. }
  585. finally
  586. {
  587. graph.model.endUpdate();
  588. }
  589. }
  590. return page;
  591. };
  592. /**
  593. * Returns true if the given string contains an mxfile.
  594. */
  595. EditorUi.prototype.duplicatePage = function(page, name)
  596. {
  597. var graph = this.editor.graph;
  598. var newPage = null;
  599. if (graph.isEnabled())
  600. {
  601. // Clones the current page and takes a snapshot of the graph model and view state
  602. var newPage = new DiagramPage(page.node.cloneNode(false));
  603. newPage.root = graph.cloneCells([graph.model.root])[0];
  604. newPage.viewState = graph.getViewState();
  605. // Resets zoom and scrollbar positions
  606. newPage.viewState.scale = 1;
  607. newPage.viewState.scrollLeft = null;
  608. newPage.viewState.scrollRight = null;
  609. newPage.setName(name);
  610. newPage = this.insertPage(newPage, mxUtils.indexOf(this.pages, page) + 1);
  611. }
  612. return newPage;
  613. };
  614. /**
  615. * Returns true if the given string contains an mxfile.
  616. */
  617. EditorUi.prototype.renamePage = function(page)
  618. {
  619. var graph = this.editor.graph;
  620. if (graph.isEnabled())
  621. {
  622. var dlg = new FilenameDialog(this, page.getName(), mxResources.get('rename'), mxUtils.bind(this, function(name)
  623. {
  624. if (name != null && name.length > 0)
  625. {
  626. this.editor.graph.model.execute(new RenamePage(this, page, name));
  627. }
  628. }), mxResources.get('rename'));
  629. this.showDialog(dlg.container, 300, 80, true, true);
  630. dlg.init();
  631. }
  632. return page;
  633. }
  634. /**
  635. * Returns true if the given string contains an mxfile.
  636. */
  637. EditorUi.prototype.movePage = function(oldIndex, newIndex)
  638. {
  639. this.editor.graph.model.execute(new MovePage(this, oldIndex, newIndex));
  640. }
  641. /**
  642. * Returns true if the given string contains an mxfile.
  643. */
  644. EditorUi.prototype.createTabContainer = function()
  645. {
  646. var div = document.createElement('div');
  647. div.style.backgroundColor = '#dcdcdc';
  648. div.style.position = 'absolute';
  649. div.style.whiteSpace = 'nowrap';
  650. div.style.overflow = 'hidden';
  651. div.style.height = '0px';
  652. return div;
  653. };
  654. /**
  655. * Returns true if the given string contains an mxfile.
  656. */
  657. EditorUi.prototype.updateTabContainer = function()
  658. {
  659. if (this.tabContainer != null && this.pages != null)
  660. {
  661. var graph = this.editor.graph;
  662. var wrapper = document.createElement('div');
  663. wrapper.style.position = 'relative';
  664. wrapper.style.display = (mxClient.IS_QUIRKS) ? 'inline' : 'inline-block';
  665. wrapper.style.verticalAlign = 'top';
  666. wrapper.style.height = this.tabContainer.style.height;
  667. wrapper.style.whiteSpace = 'nowrap';
  668. wrapper.style.overflow = 'hidden';
  669. wrapper.style.fontSize = '12px';
  670. // Allows for negative left margin of first tab
  671. wrapper.style.marginLeft = '30px';
  672. // Automatic tab width to match available width
  673. // TODO: Fix tabWidth in chromeless mode
  674. var btnWidth = (this.editor.chromeless) ? 29 : 59;
  675. var tabWidth = Math.min(140, Math.max(20, (this.tabContainer.clientWidth - btnWidth) / this.pages.length) + 1);
  676. var startIndex = null;
  677. for (var i = 0; i < this.pages.length; i++)
  678. {
  679. // Install drag and drop for page reorder
  680. (mxUtils.bind(this, function(index, tab)
  681. {
  682. if (this.pages[index] == this.currentPage)
  683. {
  684. tab.style.backgroundColor = '#eeeeee';
  685. tab.style.fontWeight = 'bold';
  686. tab.style.borderTopStyle = 'none';
  687. }
  688. tab.setAttribute('draggable', 'true');
  689. mxEvent.addListener(tab, 'dragstart', mxUtils.bind(this, function(evt)
  690. {
  691. if (graph.isEnabled())
  692. {
  693. startIndex = index;
  694. }
  695. else
  696. {
  697. // Blocks event
  698. mxEvent.consume(evt);
  699. }
  700. }));
  701. mxEvent.addListener(tab, 'dragend', mxUtils.bind(this, function(evt)
  702. {
  703. startIndex = null;
  704. evt.stopPropagation();
  705. evt.preventDefault();
  706. }));
  707. mxEvent.addListener(tab, 'dragover', mxUtils.bind(this, function(evt)
  708. {
  709. if (startIndex != null)
  710. {
  711. evt.dataTransfer.dropEffect = 'move';
  712. }
  713. evt.stopPropagation();
  714. evt.preventDefault();
  715. }));
  716. mxEvent.addListener(tab, 'drop', mxUtils.bind(this, function(evt)
  717. {
  718. if (startIndex != null && index != startIndex)
  719. {
  720. // TODO: Shift drag for insert/merge?
  721. this.movePage(startIndex, index);
  722. }
  723. evt.stopPropagation();
  724. evt.preventDefault();
  725. }));
  726. wrapper.appendChild(tab);
  727. }))(i, this.createTabForPage(this.pages[i], tabWidth, this.pages[i] != this.currentPage));
  728. }
  729. this.tabContainer.innerHTML = '';
  730. this.tabContainer.appendChild(wrapper);
  731. // Adds floating menu with all pages and insert option
  732. var menutab = this.createPageMenuTab();
  733. this.tabContainer.appendChild(menutab);
  734. var insertTab = null;
  735. // Not chromeless and not read-only file
  736. if (graph.isEnabled())
  737. {
  738. insertTab = this.createPageInsertTab();
  739. this.tabContainer.appendChild(insertTab);
  740. }
  741. if (wrapper.clientWidth > this.tabContainer.clientWidth - btnWidth)
  742. {
  743. if (insertTab != null)
  744. {
  745. insertTab.style.position = 'absolute';
  746. insertTab.style.right = '0px';
  747. wrapper.style.marginRight = '30px';
  748. }
  749. var temp = this.createControlTab(4, '&nbsp;&#10094;&nbsp;');
  750. temp.style.position = 'absolute';
  751. temp.style.right = (this.editor.chromeless) ? '29px' : '55px';
  752. temp.style.fontSize = '13pt';
  753. this.tabContainer.appendChild(temp);
  754. var temp2 = this.createControlTab(4, '&nbsp;&#10095;');
  755. temp2.style.position = 'absolute';
  756. temp2.style.right = (this.editor.chromeless) ? '0px' : '29px';
  757. temp2.style.fontSize = '13pt';
  758. this.tabContainer.appendChild(temp2);
  759. // TODO: Scroll to current page
  760. var dx = Math.max(0, this.tabContainer.clientWidth - ((this.editor.chromeless) ? 86 : 116));
  761. wrapper.style.width = dx + 'px';
  762. var fade = 50;
  763. mxEvent.addListener(temp, 'click', mxUtils.bind(this, function(evt)
  764. {
  765. wrapper.scrollLeft -= Math.max(20, dx - 20);
  766. mxUtils.setOpacity(temp, (wrapper.scrollLeft > 0) ? 100 : fade);
  767. mxUtils.setOpacity(temp2, (wrapper.scrollLeft < wrapper.scrollWidth - wrapper.clientWidth) ? 100 : fade);
  768. mxEvent.consume(evt);
  769. }));
  770. mxUtils.setOpacity(temp, (wrapper.scrollLeft > 0) ? 100 : fade);
  771. mxUtils.setOpacity(temp2, (wrapper.scrollLeft < wrapper.scrollWidth - wrapper.clientWidth) ? 100 : fade);
  772. mxEvent.addListener(temp2, 'click', mxUtils.bind(this, function(evt)
  773. {
  774. wrapper.scrollLeft += Math.max(20, dx - 20);
  775. mxUtils.setOpacity(temp, (wrapper.scrollLeft > 0) ? 100 : fade);
  776. mxUtils.setOpacity(temp2, (wrapper.scrollLeft < wrapper.scrollWidth - wrapper.clientWidth) ? 100 : fade);
  777. mxEvent.consume(evt);
  778. }));
  779. }
  780. }
  781. };
  782. /**
  783. * Returns true if the given string contains an mxfile.
  784. */
  785. EditorUi.prototype.createTab = function(hoverEnabled)
  786. {
  787. var tab = document.createElement('div');
  788. tab.style.display = (mxClient.IS_QUIRKS) ? 'inline' : 'inline-block';
  789. tab.style.whiteSpace = 'nowrap';
  790. tab.style.boxSizing = 'border-box';
  791. tab.style.position = 'relative';
  792. tab.style.overflow = 'hidden';
  793. tab.style.marginLeft = '-1px';
  794. tab.style.height = this.tabContainer.clientHeight + 'px';
  795. tab.style.padding = '8px 4px 8px 4px';
  796. tab.style.border = '1px solid #c0c0c0';
  797. tab.style.borderBottomStyle = 'solid';
  798. tab.style.backgroundColor = this.tabContainer.style.backgroundColor;
  799. tab.style.cursor = 'default';
  800. tab.style.color = 'gray';
  801. if (hoverEnabled)
  802. {
  803. mxEvent.addListener(tab, 'mouseenter', mxUtils.bind(this, function(evt)
  804. {
  805. if (!this.editor.graph.isMouseDown)
  806. {
  807. tab.style.backgroundColor = '#d3d3d3';
  808. mxEvent.consume(evt);
  809. }
  810. }));
  811. mxEvent.addListener(tab, 'mouseleave', mxUtils.bind(this, function(evt)
  812. {
  813. tab.style.backgroundColor = this.tabContainer.style.backgroundColor;
  814. mxEvent.consume(evt);
  815. }));
  816. }
  817. return tab;
  818. };
  819. /**
  820. * Returns true if the given string contains an mxfile.
  821. */
  822. EditorUi.prototype.createControlTab = function(paddingTop, html)
  823. {
  824. var tab = this.createTab(true);
  825. tab.style.paddingTop = paddingTop + 'px';
  826. tab.style.cursor = 'pointer';
  827. tab.style.width = '30px';
  828. tab.style.lineHeight = '30px';
  829. tab.innerHTML = html;
  830. if (tab.firstChild != null && tab.firstChild.style != null)
  831. {
  832. mxUtils.setOpacity(tab.firstChild, 40);
  833. }
  834. return tab;
  835. };
  836. /**
  837. * Returns true if the given string contains an mxfile.
  838. */
  839. EditorUi.prototype.createPageMenuTab = function()
  840. {
  841. var tab = this.createControlTab(3, '<div class="geSprite geSprite-dots" style="display:inline-block;width:21px;height:21px;"></div>');
  842. tab.setAttribute('title', mxResources.get('pages'));
  843. tab.style.position = 'absolute';
  844. tab.style.left = '1px';
  845. mxEvent.addListener(tab, 'click', mxUtils.bind(this, function(evt)
  846. {
  847. this.editor.graph.popupMenuHandler.hideMenu();
  848. var menu = new mxPopupMenu(mxUtils.bind(this, function(menu, parent)
  849. {
  850. for (var i = 0; i < this.pages.length; i++)
  851. {
  852. (mxUtils.bind(this, function(index)
  853. {
  854. var item = menu.addItem(this.pages[index].getName(), null, mxUtils.bind(this, function()
  855. {
  856. this.selectPage(this.pages[index]);
  857. }), parent);
  858. // Adds checkmark to current page
  859. if (this.pages[index] == this.currentPage)
  860. {
  861. menu.addCheckmark(item, Editor.checkmarkImage);
  862. }
  863. }))(i);
  864. }
  865. if (this.editor.graph.isEnabled())
  866. {
  867. menu.addSeparator(parent);
  868. var item = menu.addItem(mxResources.get('insertPage'), null, mxUtils.bind(this, function()
  869. {
  870. this.insertPage();
  871. }), parent);
  872. }
  873. }));
  874. menu.div.className += ' geMenubarMenu';
  875. menu.smartSeparators = true;
  876. menu.showDisabled = true;
  877. menu.autoExpand = true;
  878. // Disables autoexpand and destroys menu when hidden
  879. menu.hideMenu = mxUtils.bind(this, function()
  880. {
  881. mxPopupMenu.prototype.hideMenu.apply(menu, arguments);
  882. menu.destroy();
  883. });
  884. var x = mxEvent.getClientX(evt);
  885. var y = mxEvent.getClientY(evt);
  886. menu.popup(x, y, null, evt);
  887. // Allows hiding by clicking on document
  888. this.setCurrentMenu(menu);
  889. mxEvent.consume(evt);
  890. }));
  891. return tab;
  892. };
  893. /**
  894. * Returns true if the given string contains an mxfile.
  895. */
  896. EditorUi.prototype.createPageInsertTab = function()
  897. {
  898. var tab = this.createControlTab(4, '<div class="geSprite geSprite-plus" style="display:inline-block;width:21px;height:21px;"></div>');
  899. tab.setAttribute('title', mxResources.get('insertPage'));
  900. var graph = this.editor.graph;
  901. mxEvent.addListener(tab, 'click', mxUtils.bind(this, function(evt)
  902. {
  903. this.insertPage();
  904. mxEvent.consume(evt);
  905. }));
  906. return tab;
  907. };
  908. /**
  909. * Returns true if the given string contains an mxfile.
  910. */
  911. EditorUi.prototype.createTabForPage = function(page, tabWidth, hoverEnabled)
  912. {
  913. var tab = this.createTab(hoverEnabled);
  914. var name = page.getName();
  915. tab.setAttribute('title', name);
  916. mxUtils.write(tab, name);
  917. tab.style.maxWidth = tabWidth + 'px';
  918. tab.style.width = tabWidth + 'px';
  919. this.addTabListeners(page, tab);
  920. if (tabWidth > 42)
  921. {
  922. tab.style.textOverflow = 'ellipsis';
  923. }
  924. return tab;
  925. };
  926. /**
  927. * Translates this point by the given vector.
  928. *
  929. * @param {number} dx X-coordinate of the translation.
  930. * @param {number} dy Y-coordinate of the translation.
  931. */
  932. EditorUi.prototype.addTabListeners = function(page, tab)
  933. {
  934. mxEvent.disableContextMenu(tab);
  935. var graph = this.editor.graph;
  936. var model = graph.model;
  937. mxEvent.addListener(tab, 'dblclick', mxUtils.bind(this, function(evt)
  938. {
  939. this.renamePage(page)
  940. mxEvent.consume(evt);
  941. }));
  942. var menuWasVisible = false;
  943. var pageWasActive = false;
  944. mxEvent.addGestureListeners(tab, mxUtils.bind(this, function(evt)
  945. {
  946. // Do not consume event here to allow for drag and drop of tabs
  947. menuWasVisible = this.currentMenu != null;
  948. pageWasActive = page == this.currentPage;
  949. if (!graph.isMouseDown && !pageWasActive)
  950. {
  951. this.selectPage(page);
  952. }
  953. }), null, mxUtils.bind(this, function(evt)
  954. {
  955. if (graph.isEnabled() && !graph.isMouseDown &&
  956. ((mxEvent.isTouchEvent(evt) && pageWasActive) ||
  957. mxEvent.isPopupTrigger(evt)))
  958. {
  959. graph.popupMenuHandler.hideMenu();
  960. this.hideCurrentMenu();
  961. if (!mxEvent.isTouchEvent(evt) || !menuWasVisible)
  962. {
  963. var menu = new mxPopupMenu(this.createPageMenu(page));
  964. menu.div.className += ' geMenubarMenu';
  965. menu.smartSeparators = true;
  966. menu.showDisabled = true;
  967. menu.autoExpand = true;
  968. // Disables autoexpand and destroys menu when hidden
  969. menu.hideMenu = mxUtils.bind(this, function()
  970. {
  971. mxPopupMenu.prototype.hideMenu.apply(menu, arguments);
  972. this.resetCurrentMenu();
  973. menu.destroy();
  974. });
  975. var x = mxEvent.getClientX(evt);
  976. var y = mxEvent.getClientY(evt);
  977. menu.popup(x, y, null, evt);
  978. this.setCurrentMenu(menu, tab);
  979. }
  980. mxEvent.consume(evt);
  981. }
  982. }));
  983. };
  984. /**
  985. * Returns true if the given string contains an mxfile.
  986. */
  987. EditorUi.prototype.createPageMenu = function(page, label)
  988. {
  989. return mxUtils.bind(this, function(menu, parent)
  990. {
  991. var graph = this.editor.graph;
  992. var model = graph.model;
  993. menu.addItem(mxResources.get('insert'), null, mxUtils.bind(this, function()
  994. {
  995. this.insertPage(null, mxUtils.indexOf(this.pages, page) + 1);
  996. }), parent);
  997. menu.addItem(mxResources.get('delete'), null, mxUtils.bind(this, function()
  998. {
  999. this.removePage(page);
  1000. }), parent);
  1001. menu.addItem(mxResources.get('rename'), null, mxUtils.bind(this, function()
  1002. {
  1003. this.renamePage(page, label);
  1004. }), parent);
  1005. menu.addSeparator(parent);
  1006. menu.addItem(mxResources.get('duplicate'), null, mxUtils.bind(this, function()
  1007. {
  1008. this.duplicatePage(page, mxResources.get('copyOf', [page.getName()]));
  1009. }), parent);
  1010. });
  1011. };