Pages.js 40 KB

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