Pages.js 32 KB

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