Pages.js 28 KB

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