Pages.js 31 KB

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