Pages.js 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669
  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, id)
  17. {
  18. this.node = node;
  19. if (id != null)
  20. {
  21. this.node.setAttribute('id', id);
  22. }
  23. else if (this.getId() == null)
  24. {
  25. this.node.setAttribute('id', Editor.guid());
  26. }
  27. };
  28. /**
  29. * Holds the diagram node for the page.
  30. */
  31. DiagramPage.prototype.node = null;
  32. /**
  33. * Holds the root cell for the page.
  34. */
  35. DiagramPage.prototype.root = null;
  36. /**
  37. * Holds the view state for the page.
  38. */
  39. DiagramPage.prototype.viewState = null;
  40. /**
  41. *
  42. */
  43. DiagramPage.prototype.getId = function()
  44. {
  45. return this.node.getAttribute('id');
  46. };
  47. /**
  48. *
  49. */
  50. DiagramPage.prototype.getName = function()
  51. {
  52. return this.node.getAttribute('name');
  53. };
  54. /**
  55. *
  56. */
  57. DiagramPage.prototype.setName = function(value)
  58. {
  59. if (value == null)
  60. {
  61. this.node.removeAttribute('name');
  62. }
  63. else
  64. {
  65. this.node.setAttribute('name', value);
  66. }
  67. };
  68. /**
  69. * Change types
  70. */
  71. function RenamePage(ui, page, name)
  72. {
  73. this.ui = ui;
  74. this.page = page;
  75. this.name = name;
  76. this.previous = name;
  77. }
  78. /**
  79. * Implementation of the undoable page rename.
  80. */
  81. RenamePage.prototype.execute = function()
  82. {
  83. var tmp = this.page.getName();
  84. this.page.setName(this.previous);
  85. this.name = this.previous;
  86. this.previous = tmp;
  87. // Required to update page name in placeholders
  88. this.ui.editor.graph.updatePlaceholders();
  89. this.ui.editor.fireEvent(new mxEventObject('pageRenamed'));
  90. };
  91. /**
  92. * Undoable change of page title.
  93. */
  94. function MovePage(ui, oldIndex, newIndex)
  95. {
  96. this.ui = ui;
  97. this.oldIndex = oldIndex;
  98. this.newIndex = newIndex;
  99. }
  100. /**
  101. * Implementation of the undoable page rename.
  102. */
  103. MovePage.prototype.execute = function()
  104. {
  105. this.ui.pages.splice(this.newIndex, 0, this.ui.pages.splice(this.oldIndex, 1)[0]);
  106. var tmp = this.oldIndex;
  107. this.oldIndex = this.newIndex;
  108. this.newIndex = tmp;
  109. // Required to update page numbers in placeholders
  110. this.ui.editor.graph.updatePlaceholders();
  111. this.ui.editor.fireEvent(new mxEventObject('pageMoved'));
  112. };
  113. /**
  114. * Class: mxCurrentRootChange
  115. *
  116. * Action to change the current root in a view.
  117. *
  118. * Constructor: mxCurrentRootChange
  119. *
  120. * Constructs a change of the current root in the given view.
  121. */
  122. function SelectPage(ui, page, viewState)
  123. {
  124. this.ui = ui;
  125. this.page = page;
  126. this.previousPage = page;
  127. this.neverShown = true;
  128. if (page != null)
  129. {
  130. this.neverShown = page.viewState == null;
  131. this.ui.updatePageRoot(page);
  132. if (viewState != null)
  133. {
  134. page.viewState = viewState;
  135. this.neverShown = false;
  136. }
  137. }
  138. };
  139. /**
  140. * Executes selection of a new page.
  141. */
  142. SelectPage.prototype.execute = function()
  143. {
  144. var prevIndex = mxUtils.indexOf(this.ui.pages, this.previousPage);
  145. if (this.page != null && prevIndex >= 0)
  146. {
  147. var page = this.ui.currentPage;
  148. var editor = this.ui.editor;
  149. var graph = editor.graph;
  150. // Stores current diagram state in the page
  151. var data = Graph.compressNode(editor.getGraphXml(true));
  152. mxUtils.setTextContent(page.node, data);
  153. page.viewState = graph.getViewState();
  154. page.root = graph.model.root;
  155. if (page.model != null)
  156. {
  157. // Updates internal structures of offpage model
  158. page.model.rootChanged(page.root);
  159. }
  160. // Transitions for switching pages
  161. // var curIndex = mxUtils.indexOf(this.ui.pages, page);
  162. // mxUtils.setPrefixedStyle(graph.view.canvas.style, 'transition', null);
  163. // mxUtils.setPrefixedStyle(graph.view.canvas.style, 'transform',
  164. // (curIndex > prevIndex) ? 'translate(-50%,0)' : 'translate(50%,0)');
  165. // Removes the previous cells and clears selection
  166. graph.view.clear(page.root, true);
  167. graph.clearSelection();
  168. // Switches the current page
  169. this.ui.currentPage = this.previousPage;
  170. this.previousPage = page;
  171. page = this.ui.currentPage;
  172. // Switches the root cell and sets the view state
  173. graph.model.prefix = Editor.guid() + '-';
  174. graph.model.rootChanged(page.root);
  175. graph.setViewState(page.viewState);
  176. // Handles grid state in chromeless mode which is stored in Editor instance
  177. graph.gridEnabled = graph.gridEnabled && (!this.ui.editor.isChromelessView() ||
  178. urlParams['grid'] == '1');
  179. // Updates the display
  180. editor.updateGraphComponents();
  181. graph.view.validate();
  182. graph.blockMathRender = true;
  183. graph.sizeDidChange();
  184. graph.blockMathRender = false;
  185. // mxUtils.setPrefixedStyle(graph.view.canvas.style, 'transition', 'transform 0.2s');
  186. // mxUtils.setPrefixedStyle(graph.view.canvas.style, 'transform', 'translate(0,0)');
  187. if (this.neverShown)
  188. {
  189. this.neverShown = false;
  190. graph.selectUnlockedLayer();
  191. }
  192. // Fires events
  193. editor.graph.fireEvent(new mxEventObject(mxEvent.ROOT));
  194. editor.fireEvent(new mxEventObject('pageSelected', 'change', this));
  195. }
  196. };
  197. /**
  198. *
  199. */
  200. function ChangePage(ui, page, select, index, noSelect)
  201. {
  202. SelectPage.call(this, ui, select);
  203. this.relatedPage = page;
  204. this.index = index;
  205. this.previousIndex = null;
  206. this.noSelect = noSelect;
  207. };
  208. mxUtils.extend(ChangePage, SelectPage);
  209. /**
  210. * Function: execute
  211. *
  212. * Changes the current root of the view.
  213. */
  214. ChangePage.prototype.execute = function()
  215. {
  216. // Fires event to setting view state from realtime
  217. this.ui.editor.fireEvent(new mxEventObject('beforePageChange', 'change', this));
  218. this.previousIndex = this.index;
  219. if (this.index == null)
  220. {
  221. var tmp = mxUtils.indexOf(this.ui.pages, this.relatedPage);
  222. this.ui.pages.splice(tmp, 1);
  223. this.index = tmp;
  224. }
  225. else
  226. {
  227. this.ui.pages.splice(this.index, 0, this.relatedPage);
  228. this.index = null;
  229. }
  230. if (!this.noSelect)
  231. {
  232. SelectPage.prototype.execute.apply(this, arguments);
  233. }
  234. };
  235. /**
  236. * Specifies the height of the tab container. Default is 38.
  237. */
  238. EditorUi.prototype.tabContainerHeight = 38;
  239. /**
  240. * Returns the index of the selected page.
  241. */
  242. EditorUi.prototype.getSelectedPageIndex = function()
  243. {
  244. var result = null;
  245. if (this.pages != null && this.currentPage != null)
  246. {
  247. for (var i = 0; i < this.pages.length; i++)
  248. {
  249. if (this.pages[i] == this.currentPage)
  250. {
  251. result = i;
  252. break;
  253. }
  254. }
  255. }
  256. return result;
  257. };
  258. /**
  259. * Returns true if the given string contains an mxfile.
  260. */
  261. EditorUi.prototype.getPageById = function(id)
  262. {
  263. if (this.pages != null)
  264. {
  265. for (var i = 0; i < this.pages.length; i++)
  266. {
  267. if (this.pages[i].getId() == id)
  268. {
  269. return this.pages[i];
  270. }
  271. }
  272. }
  273. return null;
  274. };
  275. /**
  276. * Returns true if the given string contains an mxfile.
  277. */
  278. EditorUi.prototype.initPages = function()
  279. {
  280. if (!this.editor.graph.standalone)
  281. {
  282. this.actions.addAction('previousPage', mxUtils.bind(this, function()
  283. {
  284. this.selectNextPage(false);
  285. }));
  286. this.actions.addAction('nextPage', mxUtils.bind(this, function()
  287. {
  288. this.selectNextPage(true);
  289. }));
  290. this.keyHandler.bindAction(33, true, 'previousPage', true); // Ctrl+Shift+PageUp
  291. this.keyHandler.bindAction(34, true, 'nextPage', true); // Ctrl+Shift+PageDown
  292. // Updates the tabs after loading the diagram
  293. var graph = this.editor.graph;
  294. var graphViewValidateBackground = graph.view.validateBackground;
  295. graph.view.validateBackground = mxUtils.bind(this, function()
  296. {
  297. if (this.tabContainer != null)
  298. {
  299. var prevHeight = this.tabContainer.style.height;
  300. if (this.fileNode == null || this.pages == null ||
  301. (this.pages.length == 1 && urlParams['pages'] == '0'))
  302. {
  303. this.tabContainer.style.height = '0px';
  304. }
  305. else
  306. {
  307. this.tabContainer.style.height = this.tabContainerHeight + 'px';
  308. }
  309. if (prevHeight != this.tabContainer.style.height)
  310. {
  311. this.refresh(false);
  312. }
  313. }
  314. graphViewValidateBackground.apply(graph.view, arguments);
  315. });
  316. var lastPage = null;
  317. var updateTabs = mxUtils.bind(this, function()
  318. {
  319. this.updateTabContainer();
  320. // Updates scrollbar positions and backgrounds after validation
  321. var p = this.currentPage;
  322. if (p != null && p != lastPage)
  323. {
  324. if (p.viewState == null || p.viewState.scrollLeft == null)
  325. {
  326. this.resetScrollbars();
  327. if (graph.isLightboxView())
  328. {
  329. this.lightboxFit();
  330. }
  331. if (this.chromelessResize != null)
  332. {
  333. graph.container.scrollLeft = 0;
  334. graph.container.scrollTop = 0;
  335. this.chromelessResize();
  336. }
  337. }
  338. else
  339. {
  340. graph.container.scrollLeft = graph.view.translate.x * graph.view.scale + p.viewState.scrollLeft;
  341. graph.container.scrollTop = graph.view.translate.y * graph.view.scale + p.viewState.scrollTop;
  342. }
  343. lastPage = p;
  344. }
  345. // Updates layers window
  346. if (this.actions.layersWindow != null)
  347. {
  348. this.actions.layersWindow.refreshLayers();
  349. }
  350. // Workaround for math if tab is switched before typesetting has stopped
  351. if (typeof(MathJax) !== 'undefined' && typeof(MathJax.Hub) !== 'undefined')
  352. {
  353. // Pending math should not be rendered if the graph has no math enabled
  354. if (MathJax.Hub.queue.pending == 1 && this.editor != null && !this.editor.graph.mathEnabled)
  355. {
  356. // Since there is no way to stop/undo mathjax or
  357. // clear the queue we have to refresh after typeset
  358. MathJax.Hub.Queue(mxUtils.bind(this, function()
  359. {
  360. if (this.editor != null)
  361. {
  362. this.editor.graph.refresh();
  363. }
  364. }));
  365. }
  366. }
  367. else if (typeof(Editor.MathJaxClear) !== 'undefined' && (this.editor == null || !this.editor.graph.mathEnabled))
  368. {
  369. // Clears our own queue for async loading
  370. Editor.MathJaxClear();
  371. }
  372. });
  373. // Adds a graph model listener to update the view
  374. this.editor.graph.model.addListener(mxEvent.CHANGE, mxUtils.bind(this, function(sender, evt)
  375. {
  376. var edit = evt.getProperty('edit');
  377. var changes = edit.changes;
  378. for (var i = 0; i < changes.length; i++)
  379. {
  380. if (changes[i] instanceof SelectPage ||
  381. changes[i] instanceof RenamePage ||
  382. changes[i] instanceof MovePage ||
  383. changes[i] instanceof mxRootChange)
  384. {
  385. updateTabs();
  386. break;
  387. }
  388. }
  389. }));
  390. // Updates zoom in toolbar
  391. if (this.toolbar != null)
  392. {
  393. this.editor.addListener('pageSelected', this.toolbar.updateZoom);
  394. }
  395. }
  396. };
  397. /**
  398. * Adds the listener for automatically saving the diagram for local changes.
  399. */
  400. EditorUi.prototype.restoreViewState = function(page, viewState, selection)
  401. {
  402. var newPage = (page != null) ? this.getPageById(page.getId()) : null;
  403. var graph = this.editor.graph;
  404. if (newPage != null && this.currentPage != null && this.pages != null)
  405. {
  406. if (newPage != this.currentPage)
  407. {
  408. this.selectPage(newPage, true, viewState);
  409. }
  410. else
  411. {
  412. // TODO: Pass viewState to setGraphXml
  413. graph.setViewState(viewState);
  414. this.editor.updateGraphComponents();
  415. graph.view.revalidate();
  416. graph.sizeDidChange();
  417. }
  418. graph.container.scrollLeft = graph.view.translate.x * graph.view.scale + viewState.scrollLeft;
  419. graph.container.scrollTop = graph.view.translate.y * graph.view.scale + viewState.scrollTop;
  420. graph.restoreSelection(selection);
  421. }
  422. };
  423. /**
  424. * Overrides setDefaultParent
  425. */
  426. Graph.prototype.createViewState = function(node)
  427. {
  428. var pv = node.getAttribute('page');
  429. var ps = parseFloat(node.getAttribute('pageScale'));
  430. var pw = parseFloat(node.getAttribute('pageWidth'));
  431. var ph = parseFloat(node.getAttribute('pageHeight'));
  432. var bg = node.getAttribute('background');
  433. var temp = node.getAttribute('backgroundImage');
  434. var bgImg = (temp != null && temp.length > 0) ? JSON.parse(temp) : null;
  435. return {
  436. gridEnabled: node.getAttribute('grid') != '0',
  437. //gridColor: node.getAttribute('gridColor') || mxSettings.getGridColor(),
  438. gridSize: parseFloat(node.getAttribute('gridSize')) || mxGraph.prototype.gridSize,
  439. guidesEnabled: node.getAttribute('guides') != '0',
  440. foldingEnabled: node.getAttribute('fold') != '0',
  441. shadowVisible: node.getAttribute('shadow') == '1',
  442. pageVisible: (this.isLightboxView()) ? false : ((pv != null) ? (pv != '0') : this.defaultPageVisible),
  443. background: (bg != null && bg.length > 0) ? bg : null,
  444. backgroundImage: (bgImg != null) ? new mxImage(bgImg.src, bgImg.width, bgImg.height) : null,
  445. pageScale: (!isNaN(ps)) ? ps : mxGraph.prototype.pageScale,
  446. pageFormat: (!isNaN(pw) && !isNaN(ph)) ? new mxRectangle(0, 0, pw, ph) : mxSettings.getPageFormat(),
  447. tooltips: node.getAttribute('tooltips') != '0',
  448. connect: node.getAttribute('connect') != '0',
  449. arrows: node.getAttribute('arrows') != '0',
  450. mathEnabled: node.getAttribute('math') == '1',
  451. selectionCells: null,
  452. defaultParent: null,
  453. scrollbars: this.defaultScrollbars,
  454. scale: 1
  455. };
  456. };
  457. /**
  458. * Writes the graph properties from the realtime model to the given mxGraphModel node.
  459. */
  460. Graph.prototype.saveViewState = function(vs, node, ignoreTransient)
  461. {
  462. if (!ignoreTransient)
  463. {
  464. node.setAttribute('grid', (vs == null || vs.gridEnabled) ? '1' : '0');
  465. node.setAttribute('gridSize', (vs != null) ? vs.gridSize : mxGraph.prototype.gridSize);
  466. node.setAttribute('guides', (vs == null || vs.guidesEnabled) ? '1' : '0');
  467. node.setAttribute('tooltips', (vs == null || vs.tooltips) ? '1' : '0');
  468. node.setAttribute('connect', (vs == null || vs.connect) ? '1' : '0');
  469. node.setAttribute('arrows', (vs == null || vs.arrows) ? '1' : '0');
  470. node.setAttribute('page', ((vs == null && this.defaultPageVisible ) ||
  471. (vs != null && vs.pageVisible)) ? '1' : '0');
  472. // Ignores fold to avoid checksum errors for lightbox mode
  473. node.setAttribute('fold', (vs == null || vs.foldingEnabled) ? '1' : '0');
  474. }
  475. node.setAttribute('pageScale', (vs != null && vs.pageScale != null) ? vs.pageScale : mxGraph.prototype.pageScale);
  476. var pf = (vs != null) ? vs.pageFormat : mxSettings.getPageFormat();
  477. if (pf != null)
  478. {
  479. node.setAttribute('pageWidth', pf.width);
  480. node.setAttribute('pageHeight', pf.height);
  481. }
  482. if (vs != null && vs.background != null)
  483. {
  484. node.setAttribute('background', vs.background);
  485. }
  486. if (vs != null && vs.backgroundImage != null)
  487. {
  488. node.setAttribute('backgroundImage', JSON.stringify(vs.backgroundImage));
  489. }
  490. node.setAttribute('math', (vs != null && vs.mathEnabled) ? '1' : '0');
  491. node.setAttribute('shadow', (vs != null && vs.shadowVisible) ? '1' : '0');
  492. };
  493. /**
  494. * Overrides setDefaultParent
  495. */
  496. Graph.prototype.getViewState = function()
  497. {
  498. return {
  499. defaultParent: this.defaultParent,
  500. currentRoot: this.view.currentRoot,
  501. gridEnabled: this.gridEnabled,
  502. //gridColor: this.view.gridColor,
  503. gridSize: this.gridSize,
  504. guidesEnabled: this.graphHandler.guidesEnabled,
  505. foldingEnabled: this.foldingEnabled,
  506. shadowVisible: this.shadowVisible,
  507. scrollbars: this.scrollbars,
  508. pageVisible: this.pageVisible,
  509. background: this.background,
  510. backgroundImage: this.backgroundImage,
  511. pageScale: this.pageScale,
  512. pageFormat: this.pageFormat,
  513. tooltips: this.tooltipHandler.isEnabled(),
  514. connect: this.connectionHandler.isEnabled(),
  515. arrows: this.connectionArrowsEnabled,
  516. scale: this.view.scale,
  517. scrollLeft: this.container.scrollLeft - this.view.translate.x * this.view.scale,
  518. scrollTop: this.container.scrollTop - this.view.translate.y * this.view.scale,
  519. translate: this.view.translate.clone(),
  520. lastPasteXml: this.lastPasteXml,
  521. pasteCounter: this.pasteCounter,
  522. mathEnabled: this.mathEnabled
  523. };
  524. };
  525. /**
  526. * Overrides setDefaultParent
  527. */
  528. Graph.prototype.setViewState = function(state)
  529. {
  530. if (state != null)
  531. {
  532. this.lastPasteXml = state.lastPasteXml;
  533. this.pasteCounter = state.pasteCounter || 0;
  534. this.mathEnabled = state.mathEnabled;
  535. this.gridEnabled = state.gridEnabled;
  536. //this.view.gridColor = state.gridColor;
  537. this.gridSize = state.gridSize;
  538. this.graphHandler.guidesEnabled = state.guidesEnabled;
  539. this.foldingEnabled = state.foldingEnabled;
  540. this.setShadowVisible(state.shadowVisible, false);
  541. this.scrollbars = state.scrollbars;
  542. this.pageVisible = !this.isViewer() && state.pageVisible;
  543. this.background = state.background;
  544. this.backgroundImage = state.backgroundImage;
  545. this.pageScale = state.pageScale;
  546. this.pageFormat = state.pageFormat;
  547. this.view.currentRoot = state.currentRoot;
  548. this.defaultParent = state.defaultParent;
  549. this.connectionArrowsEnabled = state.arrows;
  550. this.setTooltips(state.tooltips);
  551. this.setConnectable(state.connect);
  552. if (state.scale != null)
  553. {
  554. this.view.scale = state.scale;
  555. }
  556. else
  557. {
  558. this.view.scale = 1;
  559. }
  560. // Checks if current root or default parent have been removed
  561. if (this.view.currentRoot != null &&
  562. !this.model.contains(this.view.currentRoot))
  563. {
  564. this.view.currentRoot = null;
  565. }
  566. if (this.defaultParent != null &&
  567. !this.model.contains(this.defaultParent))
  568. {
  569. this.setDefaultParent(null);
  570. this.selectUnlockedLayer();
  571. }
  572. if (state.translate != null)
  573. {
  574. this.view.translate = state.translate;
  575. }
  576. }
  577. else
  578. {
  579. this.view.currentRoot = null;
  580. this.view.scale = 1;
  581. this.gridEnabled = true;
  582. this.gridSize = mxGraph.prototype.gridSize;
  583. this.pageScale = mxGraph.prototype.pageScale;
  584. this.pageFormat = mxSettings.getPageFormat();
  585. this.pageVisible = this.defaultPageVisible;
  586. this.background = null;
  587. this.backgroundImage = null;
  588. this.scrollbars = this.defaultScrollbars;
  589. this.graphHandler.guidesEnabled = true;
  590. this.foldingEnabled = true;
  591. this.setShadowVisible(false, false);
  592. this.defaultParent = null;
  593. this.setTooltips(true);
  594. this.setConnectable(true);
  595. this.lastPasteXml = null;
  596. this.pasteCounter = 0;
  597. this.mathEnabled = false;
  598. this.connectionArrowsEnabled = true;
  599. }
  600. // Implicit settings
  601. this.pageBreaksVisible = this.pageVisible;
  602. this.preferPageSize = this.pageVisible;
  603. this.fireEvent(new mxEventObject('viewStateChanged', 'state', state));
  604. };
  605. /**
  606. * Executes selection of a new page.
  607. */
  608. EditorUi.prototype.updatePageRoot = function(page)
  609. {
  610. if (page.root == null)
  611. {
  612. var node = this.editor.extractGraphModel(page.node);
  613. if (node != null)
  614. {
  615. page.graphModelNode = node;
  616. // Converts model XML into page object with root cell
  617. page.viewState = this.editor.graph.createViewState(node);
  618. var codec = new mxCodec(node.ownerDocument);
  619. page.root = codec.decode(node).root;
  620. }
  621. else
  622. {
  623. // Initializes page object with new empty root
  624. page.root = this.editor.graph.model.createRoot();
  625. }
  626. }
  627. else if (page.viewState == null)
  628. {
  629. if (page.graphModelNode == null)
  630. {
  631. var node = this.editor.extractGraphModel(page.node);
  632. if (node != null)
  633. {
  634. page.graphModelNode = node;
  635. }
  636. }
  637. if (page.graphModelNode != null)
  638. {
  639. page.viewState = this.editor.graph.createViewState(page.graphModelNode);
  640. }
  641. }
  642. return page;
  643. };
  644. /**
  645. * Returns true if the given string contains an mxfile.
  646. */
  647. EditorUi.prototype.selectPage = function(page, quiet, viewState)
  648. {
  649. try
  650. {
  651. if (page != this.currentPage)
  652. {
  653. if (this.editor.graph.isEditing())
  654. {
  655. this.editor.graph.stopEditing(false);
  656. }
  657. quiet = (quiet != null) ? quiet : false;
  658. this.editor.graph.isMouseDown = false;
  659. this.editor.graph.reset();
  660. var edit = this.editor.graph.model.createUndoableEdit();
  661. // Special flag to bypass autosave for this edit
  662. edit.ignoreEdit = true;
  663. var change = new SelectPage(this, page, viewState);
  664. change.execute();
  665. edit.add(change);
  666. edit.notify();
  667. this.editor.graph.tooltipHandler.hide();
  668. if (!quiet)
  669. {
  670. this.editor.graph.model.fireEvent(new mxEventObject(mxEvent.UNDO, 'edit', edit));
  671. }
  672. }
  673. }
  674. catch (e)
  675. {
  676. this.handleError(e);
  677. }
  678. };
  679. /**
  680. *
  681. */
  682. EditorUi.prototype.selectNextPage = function(forward)
  683. {
  684. var next = this.currentPage;
  685. if (next != null && this.pages != null)
  686. {
  687. var tmp = mxUtils.indexOf(this.pages, next);
  688. if (forward)
  689. {
  690. this.selectPage(this.pages[mxUtils.mod(tmp + 1, this.pages.length)]);
  691. }
  692. else if (!forward)
  693. {
  694. this.selectPage(this.pages[mxUtils.mod(tmp - 1, this.pages.length)]);
  695. }
  696. }
  697. };
  698. /**
  699. * Returns true if the given string contains an mxfile.
  700. */
  701. EditorUi.prototype.insertPage = function(page, index)
  702. {
  703. if (this.editor.graph.isEnabled())
  704. {
  705. if (this.editor.graph.isEditing())
  706. {
  707. this.editor.graph.stopEditing(false);
  708. }
  709. page = (page != null) ? page : this.createPage(null, this.createPageId());
  710. index = (index != null) ? index : this.pages.length;
  711. // Uses model to fire event and trigger autosave
  712. var change = new ChangePage(this, page, page, index);
  713. this.editor.graph.model.execute(change);
  714. }
  715. return page;
  716. };
  717. /**
  718. * Returns a unique page ID.
  719. */
  720. EditorUi.prototype.createPageId = function()
  721. {
  722. var id = null;
  723. do
  724. {
  725. id = Editor.guid();
  726. } while (this.getPageById(id) != null)
  727. return id;
  728. };
  729. /**
  730. * Returns a new DiagramPage instance.
  731. */
  732. EditorUi.prototype.createPage = function(name, id)
  733. {
  734. var page = new DiagramPage(this.fileNode.ownerDocument.createElement('diagram'), id);
  735. page.setName((name != null) ? name : this.createPageName());
  736. return page;
  737. };
  738. /**
  739. * Returns a page name.
  740. */
  741. EditorUi.prototype.createPageName = function()
  742. {
  743. // Creates a lookup with names
  744. var existing = {};
  745. for (var i = 0; i < this.pages.length; i++)
  746. {
  747. var tmp = this.pages[i].getName();
  748. if (tmp != null && tmp.length > 0)
  749. {
  750. existing[tmp] = tmp;
  751. }
  752. }
  753. // Avoids existing names
  754. var nr = this.pages.length;
  755. var name = null;
  756. do
  757. {
  758. name = mxResources.get('pageWithNumber', [++nr]);
  759. }
  760. while (existing[name] != null);
  761. return name;
  762. };
  763. /**
  764. * Removes the given page.
  765. */
  766. EditorUi.prototype.removePage = function(page)
  767. {
  768. try
  769. {
  770. var graph = this.editor.graph;
  771. var tmp = mxUtils.indexOf(this.pages, page);
  772. if (graph.isEnabled() && tmp >= 0)
  773. {
  774. if (this.editor.graph.isEditing())
  775. {
  776. this.editor.graph.stopEditing(false);
  777. }
  778. graph.model.beginUpdate();
  779. try
  780. {
  781. var next = this.currentPage;
  782. if (next == page && this.pages.length > 1)
  783. {
  784. if (tmp == this.pages.length - 1)
  785. {
  786. tmp--;
  787. }
  788. else
  789. {
  790. tmp++;
  791. }
  792. next = this.pages[tmp];
  793. }
  794. else if (this.pages.length <= 1)
  795. {
  796. // Removes label with incorrect page number to force
  797. // default page name which is OK for a single page
  798. next = this.insertPage();
  799. graph.model.execute(new RenamePage(this, next,
  800. mxResources.get('pageWithNumber', [1])));
  801. }
  802. // Uses model to fire event to trigger autosave
  803. graph.model.execute(new ChangePage(this, page, next));
  804. }
  805. finally
  806. {
  807. graph.model.endUpdate();
  808. }
  809. }
  810. }
  811. catch (e)
  812. {
  813. this.handleError(e);
  814. }
  815. return page;
  816. };
  817. /**
  818. * Duplicates the given page.
  819. */
  820. EditorUi.prototype.duplicatePage = function(page, name)
  821. {
  822. var newPage = null;
  823. try
  824. {
  825. var graph = this.editor.graph;
  826. if (graph.isEnabled())
  827. {
  828. if (graph.isEditing())
  829. {
  830. graph.stopEditing();
  831. }
  832. // Clones the current page and takes a snapshot of the graph model and view state
  833. var node = page.node.cloneNode(false);
  834. node.removeAttribute('id');
  835. var newPage = new DiagramPage(node);
  836. newPage.root = graph.cloneCell(graph.model.root);
  837. newPage.viewState = graph.getViewState();
  838. // Resets zoom and scrollbar positions
  839. newPage.viewState.scale = 1;
  840. newPage.viewState.scrollLeft = null;
  841. newPage.viewState.scrollTop = null;
  842. newPage.viewState.currentRoot = null;
  843. newPage.viewState.defaultParent = null;
  844. newPage.setName(name);
  845. newPage = this.insertPage(newPage, mxUtils.indexOf(this.pages, page) + 1);
  846. }
  847. }
  848. catch (e)
  849. {
  850. this.handleError(e);
  851. }
  852. return newPage;
  853. };
  854. /**
  855. * Renames the given page using a dialog.
  856. */
  857. EditorUi.prototype.renamePage = function(page)
  858. {
  859. var graph = this.editor.graph;
  860. if (graph.isEnabled())
  861. {
  862. var dlg = new FilenameDialog(this, page.getName(), mxResources.get('rename'), mxUtils.bind(this, function(name)
  863. {
  864. if (name != null && name.length > 0)
  865. {
  866. this.editor.graph.model.execute(new RenamePage(this, page, name));
  867. }
  868. }), mxResources.get('rename'));
  869. this.showDialog(dlg.container, 300, 80, true, true);
  870. dlg.init();
  871. }
  872. return page;
  873. }
  874. /**
  875. * Returns true if the given string contains an mxfile.
  876. */
  877. EditorUi.prototype.movePage = function(oldIndex, newIndex)
  878. {
  879. this.editor.graph.model.execute(new MovePage(this, oldIndex, newIndex));
  880. }
  881. /**
  882. * Returns true if the given string contains an mxfile.
  883. */
  884. EditorUi.prototype.createTabContainer = function()
  885. {
  886. var div = document.createElement('div');
  887. div.className = 'geTabContainer';
  888. div.style.position = 'absolute';
  889. div.style.whiteSpace = 'nowrap';
  890. div.style.overflow = 'hidden';
  891. div.style.height = '0px';
  892. return div;
  893. };
  894. /**
  895. * Returns true if the given string contains an mxfile.
  896. */
  897. EditorUi.prototype.updateTabContainer = function()
  898. {
  899. if (this.tabContainer != null && this.pages != null)
  900. {
  901. var graph = this.editor.graph;
  902. var wrapper = document.createElement('div');
  903. wrapper.style.position = 'relative';
  904. wrapper.style.display = (mxClient.IS_QUIRKS) ? 'inline' : 'inline-block';
  905. wrapper.style.verticalAlign = 'top';
  906. wrapper.style.height = this.tabContainer.style.height;
  907. wrapper.style.whiteSpace = 'nowrap';
  908. wrapper.style.overflow = 'hidden';
  909. wrapper.style.fontSize = '13px';
  910. // Allows for negative left margin of first tab
  911. wrapper.style.marginLeft = '30px';
  912. // Automatic tab width to match available width
  913. // TODO: Fix tabWidth in chromeless mode
  914. var btnWidth = (this.editor.isChromelessView()) ? 29 : 59;
  915. var tabWidth = Math.min(140, Math.max(20, (this.tabContainer.clientWidth - btnWidth) / this.pages.length) + 1);
  916. var startIndex = null;
  917. for (var i = 0; i < this.pages.length; i++)
  918. {
  919. // Install drag and drop for page reorder
  920. (mxUtils.bind(this, function(index, tab)
  921. {
  922. if (this.pages[index] == this.currentPage)
  923. {
  924. tab.className = 'geActivePage';
  925. tab.style.backgroundColor = (uiTheme == 'dark') ? '#2a2a2a' : '#fff';
  926. }
  927. else
  928. {
  929. tab.className = 'geInactivePage';
  930. }
  931. tab.setAttribute('draggable', 'true');
  932. mxEvent.addListener(tab, 'dragstart', mxUtils.bind(this, function(evt)
  933. {
  934. if (graph.isEnabled())
  935. {
  936. // Workaround for no DnD on DIV in FF
  937. if (mxClient.IS_FF)
  938. {
  939. // LATER: Check what triggers a parse as XML on this in FF after drop
  940. evt.dataTransfer.setData('Text', '<diagram/>');
  941. }
  942. startIndex = index;
  943. }
  944. else
  945. {
  946. // Blocks event
  947. mxEvent.consume(evt);
  948. }
  949. }));
  950. mxEvent.addListener(tab, 'dragend', mxUtils.bind(this, function(evt)
  951. {
  952. startIndex = null;
  953. evt.stopPropagation();
  954. evt.preventDefault();
  955. }));
  956. mxEvent.addListener(tab, 'dragover', mxUtils.bind(this, function(evt)
  957. {
  958. if (startIndex != null)
  959. {
  960. evt.dataTransfer.dropEffect = 'move';
  961. }
  962. evt.stopPropagation();
  963. evt.preventDefault();
  964. }));
  965. mxEvent.addListener(tab, 'drop', mxUtils.bind(this, function(evt)
  966. {
  967. if (startIndex != null && index != startIndex)
  968. {
  969. // TODO: Shift drag for insert/merge?
  970. this.movePage(startIndex, index);
  971. }
  972. evt.stopPropagation();
  973. evt.preventDefault();
  974. }));
  975. wrapper.appendChild(tab);
  976. }))(i, this.createTabForPage(this.pages[i], tabWidth, this.pages[i] != this.currentPage, i + 1));
  977. }
  978. this.tabContainer.innerHTML = '';
  979. this.tabContainer.appendChild(wrapper);
  980. // Adds floating menu with all pages and insert option
  981. var menutab = this.createPageMenuTab();
  982. this.tabContainer.appendChild(menutab);
  983. var insertTab = null;
  984. // Not chromeless and not read-only file
  985. if (this.isPageInsertTabVisible())
  986. {
  987. insertTab = this.createPageInsertTab();
  988. this.tabContainer.appendChild(insertTab);
  989. }
  990. if (wrapper.clientWidth > this.tabContainer.clientWidth - btnWidth)
  991. {
  992. if (insertTab != null)
  993. {
  994. insertTab.style.position = 'absolute';
  995. insertTab.style.right = '0px';
  996. wrapper.style.marginRight = '30px';
  997. }
  998. var temp = this.createControlTab(4, '&nbsp;&#10094;&nbsp;');
  999. temp.style.position = 'absolute';
  1000. temp.style.right = (this.editor.chromeless) ? '29px' : '55px';
  1001. temp.style.fontSize = '13pt';
  1002. this.tabContainer.appendChild(temp);
  1003. var temp2 = this.createControlTab(4, '&nbsp;&#10095;');
  1004. temp2.style.position = 'absolute';
  1005. temp2.style.right = (this.editor.chromeless) ? '0px' : '29px';
  1006. temp2.style.fontSize = '13pt';
  1007. this.tabContainer.appendChild(temp2);
  1008. // TODO: Scroll to current page
  1009. var dx = Math.max(0, this.tabContainer.clientWidth - ((this.editor.chromeless) ? 86 : 116));
  1010. wrapper.style.width = dx + 'px';
  1011. var fade = 50;
  1012. mxEvent.addListener(temp, 'click', mxUtils.bind(this, function(evt)
  1013. {
  1014. wrapper.scrollLeft -= Math.max(20, dx - 20);
  1015. mxUtils.setOpacity(temp, (wrapper.scrollLeft > 0) ? 100 : fade);
  1016. mxUtils.setOpacity(temp2, (wrapper.scrollLeft < wrapper.scrollWidth - wrapper.clientWidth) ? 100 : fade);
  1017. mxEvent.consume(evt);
  1018. }));
  1019. mxUtils.setOpacity(temp, (wrapper.scrollLeft > 0) ? 100 : fade);
  1020. mxUtils.setOpacity(temp2, (wrapper.scrollLeft < wrapper.scrollWidth - wrapper.clientWidth) ? 100 : fade);
  1021. mxEvent.addListener(temp2, 'click', mxUtils.bind(this, function(evt)
  1022. {
  1023. wrapper.scrollLeft += Math.max(20, dx - 20);
  1024. mxUtils.setOpacity(temp, (wrapper.scrollLeft > 0) ? 100 : fade);
  1025. mxUtils.setOpacity(temp2, (wrapper.scrollLeft < wrapper.scrollWidth - wrapper.clientWidth) ? 100 : fade);
  1026. mxEvent.consume(evt);
  1027. }));
  1028. }
  1029. }
  1030. };
  1031. /**
  1032. * Returns true if the given string contains an mxfile.
  1033. */
  1034. EditorUi.prototype.isPageInsertTabVisible = function()
  1035. {
  1036. return urlParams['embed'] == 1 || (this.getCurrentFile() != null &&
  1037. this.getCurrentFile().isEditable());
  1038. };
  1039. /**
  1040. * Returns true if the given string contains an mxfile.
  1041. */
  1042. EditorUi.prototype.createTab = function(hoverEnabled)
  1043. {
  1044. var tab = document.createElement('div');
  1045. tab.style.display = (mxClient.IS_QUIRKS) ? 'inline' : 'inline-block';
  1046. tab.style.whiteSpace = 'nowrap';
  1047. tab.style.boxSizing = 'border-box';
  1048. tab.style.position = 'relative';
  1049. tab.style.overflow = 'hidden';
  1050. tab.style.textAlign = 'center';
  1051. tab.style.marginLeft = '-1px';
  1052. tab.style.height = this.tabContainer.clientHeight + 'px';
  1053. tab.style.padding = '12px 4px 8px 4px';
  1054. tab.style.border = (uiTheme == 'dark') ? '1px solid #505759' : '1px solid #e8eaed';
  1055. tab.style.borderTopStyle = 'none';
  1056. tab.style.borderBottomStyle = 'none';
  1057. tab.style.backgroundColor = this.tabContainer.style.backgroundColor;
  1058. tab.style.cursor = 'move';
  1059. tab.style.color = 'gray';
  1060. if (hoverEnabled)
  1061. {
  1062. mxEvent.addListener(tab, 'mouseenter', mxUtils.bind(this, function(evt)
  1063. {
  1064. if (!this.editor.graph.isMouseDown)
  1065. {
  1066. tab.style.backgroundColor = (uiTheme == 'dark') ? 'black' : '#e8eaed';
  1067. mxEvent.consume(evt);
  1068. }
  1069. }));
  1070. mxEvent.addListener(tab, 'mouseleave', mxUtils.bind(this, function(evt)
  1071. {
  1072. tab.style.backgroundColor = this.tabContainer.style.backgroundColor;
  1073. mxEvent.consume(evt);
  1074. }));
  1075. }
  1076. return tab;
  1077. };
  1078. /**
  1079. * Returns true if the given string contains an mxfile.
  1080. */
  1081. EditorUi.prototype.createControlTab = function(paddingTop, html)
  1082. {
  1083. var tab = this.createTab(true);
  1084. tab.style.lineHeight = this.tabContainerHeight + 'px';
  1085. tab.style.paddingTop = paddingTop + 'px';
  1086. tab.style.cursor = 'pointer';
  1087. tab.style.width = '30px';
  1088. tab.innerHTML = html;
  1089. if (tab.firstChild != null && tab.firstChild.style != null)
  1090. {
  1091. mxUtils.setOpacity(tab.firstChild, 40);
  1092. }
  1093. return tab;
  1094. };
  1095. /**
  1096. * Returns true if the given string contains an mxfile.
  1097. */
  1098. EditorUi.prototype.createPageMenuTab = function()
  1099. {
  1100. var tab = this.createControlTab(3, '<div class="geSprite geSprite-dots" style="display:inline-block;margin-top:5px;width:21px;height:21px;"></div>');
  1101. tab.setAttribute('title', mxResources.get('pages'));
  1102. tab.style.position = 'absolute';
  1103. tab.style.marginLeft = '0px';
  1104. tab.style.top = '0px';
  1105. tab.style.left = '1px';
  1106. mxEvent.addListener(tab, 'click', mxUtils.bind(this, function(evt)
  1107. {
  1108. this.editor.graph.popupMenuHandler.hideMenu();
  1109. var menu = new mxPopupMenu(mxUtils.bind(this, function(menu, parent)
  1110. {
  1111. for (var i = 0; i < this.pages.length; i++)
  1112. {
  1113. (mxUtils.bind(this, function(index)
  1114. {
  1115. var item = menu.addItem(this.pages[index].getName(), null, mxUtils.bind(this, function()
  1116. {
  1117. this.selectPage(this.pages[index]);
  1118. }), parent);
  1119. // Adds checkmark to current page
  1120. if (this.pages[index] == this.currentPage)
  1121. {
  1122. menu.addCheckmark(item, Editor.checkmarkImage);
  1123. }
  1124. }))(i);
  1125. }
  1126. if (this.editor.graph.isEnabled())
  1127. {
  1128. menu.addSeparator(parent);
  1129. var item = menu.addItem(mxResources.get('insertPage'), null, mxUtils.bind(this, function()
  1130. {
  1131. this.insertPage();
  1132. }), parent);
  1133. var page = this.currentPage;
  1134. if (page != null)
  1135. {
  1136. menu.addSeparator(parent);
  1137. menu.addItem(mxResources.get('delete'), null, mxUtils.bind(this, function()
  1138. {
  1139. this.removePage(page);
  1140. }), parent);
  1141. menu.addItem(mxResources.get('rename'), null, mxUtils.bind(this, function()
  1142. {
  1143. this.renamePage(page, page.getName());
  1144. }), parent);
  1145. menu.addSeparator(parent);
  1146. menu.addItem(mxResources.get('duplicate'), null, mxUtils.bind(this, function()
  1147. {
  1148. this.duplicatePage(page, mxResources.get('copyOf', [page.getName()]));
  1149. }), parent);
  1150. }
  1151. }
  1152. }));
  1153. menu.div.className += ' geMenubarMenu';
  1154. menu.smartSeparators = true;
  1155. menu.showDisabled = true;
  1156. menu.autoExpand = true;
  1157. // Disables autoexpand and destroys menu when hidden
  1158. menu.hideMenu = mxUtils.bind(this, function()
  1159. {
  1160. mxPopupMenu.prototype.hideMenu.apply(menu, arguments);
  1161. menu.destroy();
  1162. });
  1163. var x = mxEvent.getClientX(evt);
  1164. var y = mxEvent.getClientY(evt);
  1165. menu.popup(x, y, null, evt);
  1166. // Allows hiding by clicking on document
  1167. this.setCurrentMenu(menu);
  1168. mxEvent.consume(evt);
  1169. }));
  1170. return tab;
  1171. };
  1172. /**
  1173. * Returns true if the given string contains an mxfile.
  1174. */
  1175. EditorUi.prototype.createPageInsertTab = function()
  1176. {
  1177. var tab = this.createControlTab(4, '<div class="geSprite geSprite-plus" style="display:inline-block;width:21px;height:21px;"></div>');
  1178. tab.setAttribute('title', mxResources.get('insertPage'));
  1179. var graph = this.editor.graph;
  1180. mxEvent.addListener(tab, 'click', mxUtils.bind(this, function(evt)
  1181. {
  1182. this.insertPage();
  1183. mxEvent.consume(evt);
  1184. }));
  1185. return tab;
  1186. };
  1187. /**
  1188. * Returns true if the given string contains an mxfile.
  1189. */
  1190. EditorUi.prototype.createTabForPage = function(page, tabWidth, hoverEnabled, pageNumber)
  1191. {
  1192. var tab = this.createTab(hoverEnabled);
  1193. var name = page.getName() || mxResources.get('untitled');
  1194. var id = page.getId();
  1195. tab.setAttribute('title', name + ((id != null) ? ' (' + id + ')' : '') + ' [' + pageNumber + ']');
  1196. mxUtils.write(tab, name);
  1197. tab.style.maxWidth = tabWidth + 'px';
  1198. tab.style.width = tabWidth + 'px';
  1199. this.addTabListeners(page, tab);
  1200. if (tabWidth > 42)
  1201. {
  1202. tab.style.textOverflow = 'ellipsis';
  1203. }
  1204. return tab;
  1205. };
  1206. /**
  1207. * Translates this point by the given vector.
  1208. *
  1209. * @param {number} dx X-coordinate of the translation.
  1210. * @param {number} dy Y-coordinate of the translation.
  1211. */
  1212. EditorUi.prototype.addTabListeners = function(page, tab)
  1213. {
  1214. mxEvent.disableContextMenu(tab);
  1215. var graph = this.editor.graph;
  1216. var model = graph.model;
  1217. mxEvent.addListener(tab, 'dblclick', mxUtils.bind(this, function(evt)
  1218. {
  1219. this.renamePage(page)
  1220. mxEvent.consume(evt);
  1221. }));
  1222. var menuWasVisible = false;
  1223. var pageWasActive = false;
  1224. mxEvent.addGestureListeners(tab, mxUtils.bind(this, function(evt)
  1225. {
  1226. // Do not consume event here to allow for drag and drop of tabs
  1227. menuWasVisible = this.currentMenu != null;
  1228. pageWasActive = page == this.currentPage;
  1229. if (!graph.isMouseDown && !pageWasActive)
  1230. {
  1231. this.selectPage(page);
  1232. }
  1233. }), null, mxUtils.bind(this, function(evt)
  1234. {
  1235. if (graph.isEnabled() && !graph.isMouseDown &&
  1236. ((mxEvent.isTouchEvent(evt) && pageWasActive) ||
  1237. mxEvent.isPopupTrigger(evt)))
  1238. {
  1239. graph.popupMenuHandler.hideMenu();
  1240. this.hideCurrentMenu();
  1241. if (!mxEvent.isTouchEvent(evt) || !menuWasVisible)
  1242. {
  1243. var menu = new mxPopupMenu(this.createPageMenu(page));
  1244. menu.div.className += ' geMenubarMenu';
  1245. menu.smartSeparators = true;
  1246. menu.showDisabled = true;
  1247. menu.autoExpand = true;
  1248. // Disables autoexpand and destroys menu when hidden
  1249. menu.hideMenu = mxUtils.bind(this, function()
  1250. {
  1251. mxPopupMenu.prototype.hideMenu.apply(menu, arguments);
  1252. this.resetCurrentMenu();
  1253. menu.destroy();
  1254. });
  1255. var x = mxEvent.getClientX(evt);
  1256. var y = mxEvent.getClientY(evt);
  1257. menu.popup(x, y, null, evt);
  1258. this.setCurrentMenu(menu, tab);
  1259. }
  1260. mxEvent.consume(evt);
  1261. }
  1262. }));
  1263. };
  1264. /**
  1265. * Returns true if the given string contains an mxfile.
  1266. */
  1267. EditorUi.prototype.createPageMenu = function(page, label)
  1268. {
  1269. return mxUtils.bind(this, function(menu, parent)
  1270. {
  1271. var graph = this.editor.graph;
  1272. var model = graph.model;
  1273. menu.addItem(mxResources.get('insert'), null, mxUtils.bind(this, function()
  1274. {
  1275. this.insertPage(null, mxUtils.indexOf(this.pages, page) + 1);
  1276. }), parent);
  1277. menu.addItem(mxResources.get('delete'), null, mxUtils.bind(this, function()
  1278. {
  1279. this.removePage(page);
  1280. }), parent);
  1281. menu.addItem(mxResources.get('rename'), null, mxUtils.bind(this, function()
  1282. {
  1283. this.renamePage(page, label);
  1284. }), parent);
  1285. menu.addSeparator(parent);
  1286. menu.addItem(mxResources.get('duplicate'), null, mxUtils.bind(this, function()
  1287. {
  1288. this.duplicatePage(page, mxResources.get('copyOf', [page.getName()]));
  1289. }), parent);
  1290. });
  1291. };
  1292. // Overrides refresh to repaint tab container
  1293. (function()
  1294. {
  1295. var editorUiRefresh = EditorUi.prototype.refresh;
  1296. EditorUi.prototype.refresh = function(sizeDidChange)
  1297. {
  1298. editorUiRefresh.apply(this, arguments);
  1299. this.updateTabContainer();
  1300. }
  1301. })();
  1302. //Overrides ChangePageSetup codec to exclude page
  1303. (function()
  1304. {
  1305. var codec = mxCodecRegistry.getCodec(ChangePageSetup);
  1306. codec.exclude.push('page');
  1307. })();
  1308. //Registers codec for MovePage
  1309. (function()
  1310. {
  1311. var codec = new mxObjectCodec(new MovePage(), ['ui']);
  1312. codec.beforeDecode = function(dec, node, obj)
  1313. {
  1314. obj.ui = dec.ui;
  1315. return node;
  1316. };
  1317. codec.afterDecode = function(dec, node, obj)
  1318. {
  1319. var tmp = obj.oldIndex;
  1320. obj.oldIndex = obj.newIndex;
  1321. obj.newIndex = tmp;
  1322. return obj;
  1323. };
  1324. mxCodecRegistry.register(codec);
  1325. })();
  1326. //Registers codec for RenamePage
  1327. (function()
  1328. {
  1329. var codec = new mxObjectCodec(new RenamePage(), ['ui', 'page']);
  1330. codec.beforeDecode = function(dec, node, obj)
  1331. {
  1332. obj.ui = dec.ui;
  1333. return node;
  1334. };
  1335. codec.afterDecode = function(dec, node, obj)
  1336. {
  1337. var tmp = obj.previous;
  1338. obj.previous = obj.name;
  1339. obj.name = tmp;
  1340. return obj;
  1341. };
  1342. mxCodecRegistry.register(codec);
  1343. })();
  1344. //Registers codec for ChangePage
  1345. (function()
  1346. {
  1347. var codec = new mxObjectCodec(new ChangePage(), ['ui', 'relatedPage',
  1348. 'index', 'neverShown', 'page', 'previousPage']);
  1349. var viewStateIgnored = ['defaultParent', 'currentRoot', 'scrollLeft',
  1350. 'scrollTop', 'scale', 'translate', 'lastPasteXml', 'pasteCounter'];
  1351. codec.afterEncode = function(enc, obj, node)
  1352. {
  1353. node.setAttribute('relatedPage', obj.relatedPage.getId())
  1354. if (obj.index == null)
  1355. {
  1356. node.setAttribute('name', obj.relatedPage.getName());
  1357. if (obj.relatedPage.viewState != null)
  1358. {
  1359. node.setAttribute('viewState', JSON.stringify(
  1360. obj.relatedPage.viewState, function(key, value)
  1361. {
  1362. return (mxUtils.indexOf(viewStateIgnored, key) < 0) ? value : undefined;
  1363. }));
  1364. }
  1365. if (obj.relatedPage.root != null)
  1366. {
  1367. enc.encodeCell(obj.relatedPage.root, node);
  1368. }
  1369. }
  1370. return node;
  1371. };
  1372. codec.beforeDecode = function(dec, node, obj)
  1373. {
  1374. obj.ui = dec.ui;
  1375. obj.relatedPage = obj.ui.getPageById(node.getAttribute('relatedPage'));
  1376. if (obj.relatedPage == null)
  1377. {
  1378. var temp = node.ownerDocument.createElement('diagram');
  1379. temp.setAttribute('id', node.getAttribute('relatedPage'));
  1380. temp.setAttribute('name', node.getAttribute('name'));
  1381. obj.relatedPage = new DiagramPage(temp);
  1382. var vs = node.getAttribute('viewState');
  1383. if (vs != null)
  1384. {
  1385. obj.relatedPage.viewState = JSON.parse(vs);
  1386. node.removeAttribute('viewState');
  1387. }
  1388. // Makes sure the original node isn't modified
  1389. node = node.cloneNode(true);
  1390. var tmp = node.firstChild;
  1391. if (tmp != null)
  1392. {
  1393. obj.relatedPage.root = dec.decodeCell(tmp, false);
  1394. var tmp2 = tmp.nextSibling;
  1395. tmp.parentNode.removeChild(tmp);
  1396. tmp = tmp2;
  1397. while (tmp != null)
  1398. {
  1399. tmp2 = tmp.nextSibling;
  1400. if (tmp.nodeType == mxConstants.NODETYPE_ELEMENT)
  1401. {
  1402. // Ignores all existing cells because those do not need to
  1403. // be re-inserted into the model. Since the encoded version
  1404. // of these cells contains the new parent, this would leave
  1405. // to an inconsistent state on the model (ie. a parent
  1406. // change without a call to parentForCellChanged).
  1407. var id = tmp.getAttribute('id');
  1408. if (dec.lookup(id) == null)
  1409. {
  1410. dec.decodeCell(tmp);
  1411. }
  1412. }
  1413. tmp.parentNode.removeChild(tmp);
  1414. tmp = tmp2;
  1415. }
  1416. }
  1417. }
  1418. return node;
  1419. };
  1420. codec.afterDecode = function(dec, node, obj)
  1421. {
  1422. obj.index = obj.previousIndex;
  1423. return obj;
  1424. };
  1425. mxCodecRegistry.register(codec);
  1426. })();