Pages.js 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680
  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. var cause = Editor.extractParserError(node);
  614. if (cause)
  615. {
  616. throw new Error(cause);
  617. }
  618. else if (node != null)
  619. {
  620. page.graphModelNode = node;
  621. // Converts model XML into page object with root cell
  622. page.viewState = this.editor.graph.createViewState(node);
  623. var codec = new mxCodec(node.ownerDocument);
  624. page.root = codec.decode(node).root;
  625. }
  626. else
  627. {
  628. // Initializes page object with new empty root
  629. page.root = this.editor.graph.model.createRoot();
  630. }
  631. }
  632. else if (page.viewState == null)
  633. {
  634. if (page.graphModelNode == null)
  635. {
  636. var node = this.editor.extractGraphModel(page.node);
  637. var cause = Editor.extractParserError(node);
  638. if (cause)
  639. {
  640. throw new Error(cause);
  641. }
  642. else if (node != null)
  643. {
  644. page.graphModelNode = node;
  645. }
  646. }
  647. if (page.graphModelNode != null)
  648. {
  649. page.viewState = this.editor.graph.createViewState(page.graphModelNode);
  650. }
  651. }
  652. return page;
  653. };
  654. /**
  655. * Returns true if the given string contains an mxfile.
  656. */
  657. EditorUi.prototype.selectPage = function(page, quiet, viewState)
  658. {
  659. try
  660. {
  661. if (page != this.currentPage)
  662. {
  663. if (this.editor.graph.isEditing())
  664. {
  665. this.editor.graph.stopEditing(false);
  666. }
  667. quiet = (quiet != null) ? quiet : false;
  668. this.editor.graph.isMouseDown = false;
  669. this.editor.graph.reset();
  670. var edit = this.editor.graph.model.createUndoableEdit();
  671. // Special flag to bypass autosave for this edit
  672. edit.ignoreEdit = true;
  673. var change = new SelectPage(this, page, viewState);
  674. change.execute();
  675. edit.add(change);
  676. edit.notify();
  677. this.editor.graph.tooltipHandler.hide();
  678. if (!quiet)
  679. {
  680. this.editor.graph.model.fireEvent(new mxEventObject(mxEvent.UNDO, 'edit', edit));
  681. }
  682. }
  683. }
  684. catch (e)
  685. {
  686. this.handleError(e);
  687. }
  688. };
  689. /**
  690. *
  691. */
  692. EditorUi.prototype.selectNextPage = function(forward)
  693. {
  694. var next = this.currentPage;
  695. if (next != null && this.pages != null)
  696. {
  697. var tmp = mxUtils.indexOf(this.pages, next);
  698. if (forward)
  699. {
  700. this.selectPage(this.pages[mxUtils.mod(tmp + 1, this.pages.length)]);
  701. }
  702. else if (!forward)
  703. {
  704. this.selectPage(this.pages[mxUtils.mod(tmp - 1, this.pages.length)]);
  705. }
  706. }
  707. };
  708. /**
  709. * Returns true if the given string contains an mxfile.
  710. */
  711. EditorUi.prototype.insertPage = function(page, index)
  712. {
  713. if (this.editor.graph.isEnabled())
  714. {
  715. if (this.editor.graph.isEditing())
  716. {
  717. this.editor.graph.stopEditing(false);
  718. }
  719. page = (page != null) ? page : this.createPage(null, this.createPageId());
  720. index = (index != null) ? index : this.pages.length;
  721. // Uses model to fire event and trigger autosave
  722. var change = new ChangePage(this, page, page, index);
  723. this.editor.graph.model.execute(change);
  724. }
  725. return page;
  726. };
  727. /**
  728. * Returns a unique page ID.
  729. */
  730. EditorUi.prototype.createPageId = function()
  731. {
  732. var id = null;
  733. do
  734. {
  735. id = Editor.guid();
  736. } while (this.getPageById(id) != null)
  737. return id;
  738. };
  739. /**
  740. * Returns a new DiagramPage instance.
  741. */
  742. EditorUi.prototype.createPage = function(name, id)
  743. {
  744. var page = new DiagramPage(this.fileNode.ownerDocument.createElement('diagram'), id);
  745. page.setName((name != null) ? name : this.createPageName());
  746. return page;
  747. };
  748. /**
  749. * Returns a page name.
  750. */
  751. EditorUi.prototype.createPageName = function()
  752. {
  753. // Creates a lookup with names
  754. var existing = {};
  755. for (var i = 0; i < this.pages.length; i++)
  756. {
  757. var tmp = this.pages[i].getName();
  758. if (tmp != null && tmp.length > 0)
  759. {
  760. existing[tmp] = tmp;
  761. }
  762. }
  763. // Avoids existing names
  764. var nr = this.pages.length;
  765. var name = null;
  766. do
  767. {
  768. name = mxResources.get('pageWithNumber', [++nr]);
  769. }
  770. while (existing[name] != null);
  771. return name;
  772. };
  773. /**
  774. * Removes the given page.
  775. */
  776. EditorUi.prototype.removePage = function(page)
  777. {
  778. try
  779. {
  780. var graph = this.editor.graph;
  781. var tmp = mxUtils.indexOf(this.pages, page);
  782. if (graph.isEnabled() && tmp >= 0)
  783. {
  784. if (this.editor.graph.isEditing())
  785. {
  786. this.editor.graph.stopEditing(false);
  787. }
  788. graph.model.beginUpdate();
  789. try
  790. {
  791. var next = this.currentPage;
  792. if (next == page && this.pages.length > 1)
  793. {
  794. if (tmp == this.pages.length - 1)
  795. {
  796. tmp--;
  797. }
  798. else
  799. {
  800. tmp++;
  801. }
  802. next = this.pages[tmp];
  803. }
  804. else if (this.pages.length <= 1)
  805. {
  806. // Removes label with incorrect page number to force
  807. // default page name which is OK for a single page
  808. next = this.insertPage();
  809. graph.model.execute(new RenamePage(this, next,
  810. mxResources.get('pageWithNumber', [1])));
  811. }
  812. // Uses model to fire event to trigger autosave
  813. graph.model.execute(new ChangePage(this, page, next));
  814. }
  815. finally
  816. {
  817. graph.model.endUpdate();
  818. }
  819. }
  820. }
  821. catch (e)
  822. {
  823. this.handleError(e);
  824. }
  825. return page;
  826. };
  827. /**
  828. * Duplicates the given page.
  829. */
  830. EditorUi.prototype.duplicatePage = function(page, name)
  831. {
  832. var newPage = null;
  833. try
  834. {
  835. var graph = this.editor.graph;
  836. if (graph.isEnabled())
  837. {
  838. if (graph.isEditing())
  839. {
  840. graph.stopEditing();
  841. }
  842. // Clones the current page and takes a snapshot of the graph model and view state
  843. var node = page.node.cloneNode(false);
  844. node.removeAttribute('id');
  845. var newPage = new DiagramPage(node);
  846. newPage.root = graph.cloneCell(graph.model.root);
  847. newPage.viewState = graph.getViewState();
  848. // Resets zoom and scrollbar positions
  849. newPage.viewState.scale = 1;
  850. newPage.viewState.scrollLeft = null;
  851. newPage.viewState.scrollTop = null;
  852. newPage.viewState.currentRoot = null;
  853. newPage.viewState.defaultParent = null;
  854. newPage.setName(name);
  855. newPage = this.insertPage(newPage, mxUtils.indexOf(this.pages, page) + 1);
  856. }
  857. }
  858. catch (e)
  859. {
  860. this.handleError(e);
  861. }
  862. return newPage;
  863. };
  864. /**
  865. * Renames the given page using a dialog.
  866. */
  867. EditorUi.prototype.renamePage = function(page)
  868. {
  869. var graph = this.editor.graph;
  870. if (graph.isEnabled())
  871. {
  872. var dlg = new FilenameDialog(this, page.getName(), mxResources.get('rename'), mxUtils.bind(this, function(name)
  873. {
  874. if (name != null && name.length > 0)
  875. {
  876. this.editor.graph.model.execute(new RenamePage(this, page, name));
  877. }
  878. }), mxResources.get('rename'));
  879. this.showDialog(dlg.container, 300, 80, true, true);
  880. dlg.init();
  881. }
  882. return page;
  883. }
  884. /**
  885. * Returns true if the given string contains an mxfile.
  886. */
  887. EditorUi.prototype.movePage = function(oldIndex, newIndex)
  888. {
  889. this.editor.graph.model.execute(new MovePage(this, oldIndex, newIndex));
  890. }
  891. /**
  892. * Returns true if the given string contains an mxfile.
  893. */
  894. EditorUi.prototype.createTabContainer = function()
  895. {
  896. var div = document.createElement('div');
  897. div.className = 'geTabContainer';
  898. div.style.position = 'absolute';
  899. div.style.whiteSpace = 'nowrap';
  900. div.style.overflow = 'hidden';
  901. div.style.height = '0px';
  902. return div;
  903. };
  904. /**
  905. * Returns true if the given string contains an mxfile.
  906. */
  907. EditorUi.prototype.updateTabContainer = function()
  908. {
  909. if (this.tabContainer != null && this.pages != null)
  910. {
  911. var graph = this.editor.graph;
  912. var wrapper = document.createElement('div');
  913. wrapper.style.position = 'relative';
  914. wrapper.style.display = (mxClient.IS_QUIRKS) ? 'inline' : 'inline-block';
  915. wrapper.style.verticalAlign = 'top';
  916. wrapper.style.height = this.tabContainer.style.height;
  917. wrapper.style.whiteSpace = 'nowrap';
  918. wrapper.style.overflow = 'hidden';
  919. wrapper.style.fontSize = '13px';
  920. // Allows for negative left margin of first tab
  921. wrapper.style.marginLeft = '30px';
  922. // Automatic tab width to match available width
  923. // TODO: Fix tabWidth in chromeless mode
  924. var btnWidth = (this.editor.isChromelessView()) ? 29 : 59;
  925. var tabWidth = Math.min(140, Math.max(20, (this.tabContainer.clientWidth - btnWidth) / this.pages.length) + 1);
  926. var startIndex = null;
  927. for (var i = 0; i < this.pages.length; i++)
  928. {
  929. // Install drag and drop for page reorder
  930. (mxUtils.bind(this, function(index, tab)
  931. {
  932. if (this.pages[index] == this.currentPage)
  933. {
  934. tab.className = 'geActivePage';
  935. tab.style.backgroundColor = (uiTheme == 'dark') ? '#2a2a2a' : '#fff';
  936. }
  937. else
  938. {
  939. tab.className = 'geInactivePage';
  940. }
  941. tab.setAttribute('draggable', 'true');
  942. mxEvent.addListener(tab, 'dragstart', mxUtils.bind(this, function(evt)
  943. {
  944. if (graph.isEnabled())
  945. {
  946. // Workaround for no DnD on DIV in FF
  947. if (mxClient.IS_FF)
  948. {
  949. // LATER: Check what triggers a parse as XML on this in FF after drop
  950. evt.dataTransfer.setData('Text', '<diagram/>');
  951. }
  952. startIndex = index;
  953. }
  954. else
  955. {
  956. // Blocks event
  957. mxEvent.consume(evt);
  958. }
  959. }));
  960. mxEvent.addListener(tab, 'dragend', mxUtils.bind(this, function(evt)
  961. {
  962. startIndex = null;
  963. evt.stopPropagation();
  964. evt.preventDefault();
  965. }));
  966. mxEvent.addListener(tab, 'dragover', mxUtils.bind(this, function(evt)
  967. {
  968. if (startIndex != null)
  969. {
  970. evt.dataTransfer.dropEffect = 'move';
  971. }
  972. evt.stopPropagation();
  973. evt.preventDefault();
  974. }));
  975. mxEvent.addListener(tab, 'drop', mxUtils.bind(this, function(evt)
  976. {
  977. if (startIndex != null && index != startIndex)
  978. {
  979. // TODO: Shift drag for insert/merge?
  980. this.movePage(startIndex, index);
  981. }
  982. evt.stopPropagation();
  983. evt.preventDefault();
  984. }));
  985. wrapper.appendChild(tab);
  986. }))(i, this.createTabForPage(this.pages[i], tabWidth, this.pages[i] != this.currentPage, i + 1));
  987. }
  988. this.tabContainer.innerHTML = '';
  989. this.tabContainer.appendChild(wrapper);
  990. // Adds floating menu with all pages and insert option
  991. var menutab = this.createPageMenuTab();
  992. this.tabContainer.appendChild(menutab);
  993. var insertTab = null;
  994. // Not chromeless and not read-only file
  995. if (this.isPageInsertTabVisible())
  996. {
  997. insertTab = this.createPageInsertTab();
  998. this.tabContainer.appendChild(insertTab);
  999. }
  1000. if (wrapper.clientWidth > this.tabContainer.clientWidth - btnWidth)
  1001. {
  1002. if (insertTab != null)
  1003. {
  1004. insertTab.style.position = 'absolute';
  1005. insertTab.style.right = '0px';
  1006. wrapper.style.marginRight = '30px';
  1007. }
  1008. var temp = this.createControlTab(4, '&nbsp;&#10094;&nbsp;');
  1009. temp.style.position = 'absolute';
  1010. temp.style.right = (this.editor.chromeless) ? '29px' : '55px';
  1011. temp.style.fontSize = '13pt';
  1012. this.tabContainer.appendChild(temp);
  1013. var temp2 = this.createControlTab(4, '&nbsp;&#10095;');
  1014. temp2.style.position = 'absolute';
  1015. temp2.style.right = (this.editor.chromeless) ? '0px' : '29px';
  1016. temp2.style.fontSize = '13pt';
  1017. this.tabContainer.appendChild(temp2);
  1018. // TODO: Scroll to current page
  1019. var dx = Math.max(0, this.tabContainer.clientWidth - ((this.editor.chromeless) ? 86 : 116));
  1020. wrapper.style.width = dx + 'px';
  1021. var fade = 50;
  1022. mxEvent.addListener(temp, 'click', mxUtils.bind(this, function(evt)
  1023. {
  1024. wrapper.scrollLeft -= Math.max(20, dx - 20);
  1025. mxUtils.setOpacity(temp, (wrapper.scrollLeft > 0) ? 100 : fade);
  1026. mxUtils.setOpacity(temp2, (wrapper.scrollLeft < wrapper.scrollWidth - wrapper.clientWidth) ? 100 : fade);
  1027. mxEvent.consume(evt);
  1028. }));
  1029. mxUtils.setOpacity(temp, (wrapper.scrollLeft > 0) ? 100 : fade);
  1030. mxUtils.setOpacity(temp2, (wrapper.scrollLeft < wrapper.scrollWidth - wrapper.clientWidth) ? 100 : fade);
  1031. mxEvent.addListener(temp2, 'click', mxUtils.bind(this, function(evt)
  1032. {
  1033. wrapper.scrollLeft += Math.max(20, dx - 20);
  1034. mxUtils.setOpacity(temp, (wrapper.scrollLeft > 0) ? 100 : fade);
  1035. mxUtils.setOpacity(temp2, (wrapper.scrollLeft < wrapper.scrollWidth - wrapper.clientWidth) ? 100 : fade);
  1036. mxEvent.consume(evt);
  1037. }));
  1038. }
  1039. }
  1040. };
  1041. /**
  1042. * Returns true if the given string contains an mxfile.
  1043. */
  1044. EditorUi.prototype.isPageInsertTabVisible = function()
  1045. {
  1046. return urlParams['embed'] == 1 || (this.getCurrentFile() != null &&
  1047. this.getCurrentFile().isEditable());
  1048. };
  1049. /**
  1050. * Returns true if the given string contains an mxfile.
  1051. */
  1052. EditorUi.prototype.createTab = function(hoverEnabled)
  1053. {
  1054. var tab = document.createElement('div');
  1055. tab.style.display = (mxClient.IS_QUIRKS) ? 'inline' : 'inline-block';
  1056. tab.style.whiteSpace = 'nowrap';
  1057. tab.style.boxSizing = 'border-box';
  1058. tab.style.position = 'relative';
  1059. tab.style.overflow = 'hidden';
  1060. tab.style.textAlign = 'center';
  1061. tab.style.marginLeft = '-1px';
  1062. tab.style.height = this.tabContainer.clientHeight + 'px';
  1063. tab.style.padding = '12px 4px 8px 4px';
  1064. tab.style.border = (uiTheme == 'dark') ? '1px solid #505759' : '1px solid #e8eaed';
  1065. tab.style.borderTopStyle = 'none';
  1066. tab.style.borderBottomStyle = 'none';
  1067. tab.style.backgroundColor = this.tabContainer.style.backgroundColor;
  1068. tab.style.cursor = 'move';
  1069. tab.style.color = 'gray';
  1070. if (hoverEnabled)
  1071. {
  1072. mxEvent.addListener(tab, 'mouseenter', mxUtils.bind(this, function(evt)
  1073. {
  1074. if (!this.editor.graph.isMouseDown)
  1075. {
  1076. tab.style.backgroundColor = (uiTheme == 'dark') ? 'black' : '#e8eaed';
  1077. mxEvent.consume(evt);
  1078. }
  1079. }));
  1080. mxEvent.addListener(tab, 'mouseleave', mxUtils.bind(this, function(evt)
  1081. {
  1082. tab.style.backgroundColor = this.tabContainer.style.backgroundColor;
  1083. mxEvent.consume(evt);
  1084. }));
  1085. }
  1086. return tab;
  1087. };
  1088. /**
  1089. * Returns true if the given string contains an mxfile.
  1090. */
  1091. EditorUi.prototype.createControlTab = function(paddingTop, html)
  1092. {
  1093. var tab = this.createTab(true);
  1094. tab.style.lineHeight = this.tabContainerHeight + 'px';
  1095. tab.style.paddingTop = paddingTop + 'px';
  1096. tab.style.cursor = 'pointer';
  1097. tab.style.width = '30px';
  1098. tab.innerHTML = html;
  1099. if (tab.firstChild != null && tab.firstChild.style != null)
  1100. {
  1101. mxUtils.setOpacity(tab.firstChild, 40);
  1102. }
  1103. return tab;
  1104. };
  1105. /**
  1106. * Returns true if the given string contains an mxfile.
  1107. */
  1108. EditorUi.prototype.createPageMenuTab = function()
  1109. {
  1110. var tab = this.createControlTab(3, '<div class="geSprite geSprite-dots" style="display:inline-block;margin-top:5px;width:21px;height:21px;"></div>');
  1111. tab.setAttribute('title', mxResources.get('pages'));
  1112. tab.style.position = 'absolute';
  1113. tab.style.marginLeft = '0px';
  1114. tab.style.top = '0px';
  1115. tab.style.left = '1px';
  1116. mxEvent.addListener(tab, 'click', mxUtils.bind(this, function(evt)
  1117. {
  1118. this.editor.graph.popupMenuHandler.hideMenu();
  1119. var menu = new mxPopupMenu(mxUtils.bind(this, function(menu, parent)
  1120. {
  1121. for (var i = 0; i < this.pages.length; i++)
  1122. {
  1123. (mxUtils.bind(this, function(index)
  1124. {
  1125. var item = menu.addItem(this.pages[index].getName(), null, mxUtils.bind(this, function()
  1126. {
  1127. this.selectPage(this.pages[index]);
  1128. }), parent);
  1129. // Adds checkmark to current page
  1130. if (this.pages[index] == this.currentPage)
  1131. {
  1132. menu.addCheckmark(item, Editor.checkmarkImage);
  1133. }
  1134. }))(i);
  1135. }
  1136. if (this.editor.graph.isEnabled())
  1137. {
  1138. menu.addSeparator(parent);
  1139. var item = menu.addItem(mxResources.get('insertPage'), null, mxUtils.bind(this, function()
  1140. {
  1141. this.insertPage();
  1142. }), parent);
  1143. var page = this.currentPage;
  1144. if (page != null)
  1145. {
  1146. menu.addSeparator(parent);
  1147. menu.addItem(mxResources.get('delete'), null, mxUtils.bind(this, function()
  1148. {
  1149. this.removePage(page);
  1150. }), parent);
  1151. menu.addItem(mxResources.get('rename'), null, mxUtils.bind(this, function()
  1152. {
  1153. this.renamePage(page, page.getName());
  1154. }), parent);
  1155. menu.addSeparator(parent);
  1156. menu.addItem(mxResources.get('duplicate'), null, mxUtils.bind(this, function()
  1157. {
  1158. this.duplicatePage(page, mxResources.get('copyOf', [page.getName()]));
  1159. }), parent);
  1160. }
  1161. }
  1162. }));
  1163. menu.div.className += ' geMenubarMenu';
  1164. menu.smartSeparators = true;
  1165. menu.showDisabled = true;
  1166. menu.autoExpand = true;
  1167. // Disables autoexpand and destroys menu when hidden
  1168. menu.hideMenu = mxUtils.bind(this, function()
  1169. {
  1170. mxPopupMenu.prototype.hideMenu.apply(menu, arguments);
  1171. menu.destroy();
  1172. });
  1173. var x = mxEvent.getClientX(evt);
  1174. var y = mxEvent.getClientY(evt);
  1175. menu.popup(x, y, null, evt);
  1176. // Allows hiding by clicking on document
  1177. this.setCurrentMenu(menu);
  1178. mxEvent.consume(evt);
  1179. }));
  1180. return tab;
  1181. };
  1182. /**
  1183. * Returns true if the given string contains an mxfile.
  1184. */
  1185. EditorUi.prototype.createPageInsertTab = function()
  1186. {
  1187. var tab = this.createControlTab(4, '<div class="geSprite geSprite-plus" style="display:inline-block;width:21px;height:21px;"></div>');
  1188. tab.setAttribute('title', mxResources.get('insertPage'));
  1189. var graph = this.editor.graph;
  1190. mxEvent.addListener(tab, 'click', mxUtils.bind(this, function(evt)
  1191. {
  1192. this.insertPage();
  1193. mxEvent.consume(evt);
  1194. }));
  1195. return tab;
  1196. };
  1197. /**
  1198. * Returns true if the given string contains an mxfile.
  1199. */
  1200. EditorUi.prototype.createTabForPage = function(page, tabWidth, hoverEnabled, pageNumber)
  1201. {
  1202. var tab = this.createTab(hoverEnabled);
  1203. var name = page.getName() || mxResources.get('untitled');
  1204. var id = page.getId();
  1205. tab.setAttribute('title', name + ((id != null) ? ' (' + id + ')' : '') + ' [' + pageNumber + ']');
  1206. mxUtils.write(tab, name);
  1207. tab.style.maxWidth = tabWidth + 'px';
  1208. tab.style.width = tabWidth + 'px';
  1209. this.addTabListeners(page, tab);
  1210. if (tabWidth > 42)
  1211. {
  1212. tab.style.textOverflow = 'ellipsis';
  1213. }
  1214. return tab;
  1215. };
  1216. /**
  1217. * Translates this point by the given vector.
  1218. *
  1219. * @param {number} dx X-coordinate of the translation.
  1220. * @param {number} dy Y-coordinate of the translation.
  1221. */
  1222. EditorUi.prototype.addTabListeners = function(page, tab)
  1223. {
  1224. mxEvent.disableContextMenu(tab);
  1225. var graph = this.editor.graph;
  1226. var model = graph.model;
  1227. mxEvent.addListener(tab, 'dblclick', mxUtils.bind(this, function(evt)
  1228. {
  1229. this.renamePage(page)
  1230. mxEvent.consume(evt);
  1231. }));
  1232. var menuWasVisible = false;
  1233. var pageWasActive = false;
  1234. mxEvent.addGestureListeners(tab, mxUtils.bind(this, function(evt)
  1235. {
  1236. // Do not consume event here to allow for drag and drop of tabs
  1237. menuWasVisible = this.currentMenu != null;
  1238. pageWasActive = page == this.currentPage;
  1239. if (!graph.isMouseDown && !pageWasActive)
  1240. {
  1241. this.selectPage(page);
  1242. }
  1243. }), null, mxUtils.bind(this, function(evt)
  1244. {
  1245. if (graph.isEnabled() && !graph.isMouseDown &&
  1246. ((mxEvent.isTouchEvent(evt) && pageWasActive) ||
  1247. mxEvent.isPopupTrigger(evt)))
  1248. {
  1249. graph.popupMenuHandler.hideMenu();
  1250. this.hideCurrentMenu();
  1251. if (!mxEvent.isTouchEvent(evt) || !menuWasVisible)
  1252. {
  1253. var menu = new mxPopupMenu(this.createPageMenu(page));
  1254. menu.div.className += ' geMenubarMenu';
  1255. menu.smartSeparators = true;
  1256. menu.showDisabled = true;
  1257. menu.autoExpand = true;
  1258. // Disables autoexpand and destroys menu when hidden
  1259. menu.hideMenu = mxUtils.bind(this, function()
  1260. {
  1261. mxPopupMenu.prototype.hideMenu.apply(menu, arguments);
  1262. this.resetCurrentMenu();
  1263. menu.destroy();
  1264. });
  1265. var x = mxEvent.getClientX(evt);
  1266. var y = mxEvent.getClientY(evt);
  1267. menu.popup(x, y, null, evt);
  1268. this.setCurrentMenu(menu, tab);
  1269. }
  1270. mxEvent.consume(evt);
  1271. }
  1272. }));
  1273. };
  1274. /**
  1275. * Returns true if the given string contains an mxfile.
  1276. */
  1277. EditorUi.prototype.createPageMenu = function(page, label)
  1278. {
  1279. return mxUtils.bind(this, function(menu, parent)
  1280. {
  1281. var graph = this.editor.graph;
  1282. var model = graph.model;
  1283. menu.addItem(mxResources.get('insert'), null, mxUtils.bind(this, function()
  1284. {
  1285. this.insertPage(null, mxUtils.indexOf(this.pages, page) + 1);
  1286. }), parent);
  1287. menu.addItem(mxResources.get('delete'), null, mxUtils.bind(this, function()
  1288. {
  1289. this.removePage(page);
  1290. }), parent);
  1291. menu.addItem(mxResources.get('rename'), null, mxUtils.bind(this, function()
  1292. {
  1293. this.renamePage(page, label);
  1294. }), parent);
  1295. menu.addSeparator(parent);
  1296. menu.addItem(mxResources.get('duplicate'), null, mxUtils.bind(this, function()
  1297. {
  1298. this.duplicatePage(page, mxResources.get('copyOf', [page.getName()]));
  1299. }), parent);
  1300. });
  1301. };
  1302. // Overrides refresh to repaint tab container
  1303. (function()
  1304. {
  1305. var editorUiRefresh = EditorUi.prototype.refresh;
  1306. EditorUi.prototype.refresh = function(sizeDidChange)
  1307. {
  1308. editorUiRefresh.apply(this, arguments);
  1309. this.updateTabContainer();
  1310. }
  1311. })();
  1312. //Overrides ChangePageSetup codec to exclude page
  1313. (function()
  1314. {
  1315. var codec = mxCodecRegistry.getCodec(ChangePageSetup);
  1316. codec.exclude.push('page');
  1317. })();
  1318. //Registers codec for MovePage
  1319. (function()
  1320. {
  1321. var codec = new mxObjectCodec(new MovePage(), ['ui']);
  1322. codec.beforeDecode = function(dec, node, obj)
  1323. {
  1324. obj.ui = dec.ui;
  1325. return node;
  1326. };
  1327. codec.afterDecode = function(dec, node, obj)
  1328. {
  1329. var tmp = obj.oldIndex;
  1330. obj.oldIndex = obj.newIndex;
  1331. obj.newIndex = tmp;
  1332. return obj;
  1333. };
  1334. mxCodecRegistry.register(codec);
  1335. })();
  1336. //Registers codec for RenamePage
  1337. (function()
  1338. {
  1339. var codec = new mxObjectCodec(new RenamePage(), ['ui', 'page']);
  1340. codec.beforeDecode = function(dec, node, obj)
  1341. {
  1342. obj.ui = dec.ui;
  1343. return node;
  1344. };
  1345. codec.afterDecode = function(dec, node, obj)
  1346. {
  1347. var tmp = obj.previous;
  1348. obj.previous = obj.name;
  1349. obj.name = tmp;
  1350. return obj;
  1351. };
  1352. mxCodecRegistry.register(codec);
  1353. })();
  1354. //Registers codec for ChangePage
  1355. (function()
  1356. {
  1357. var codec = new mxObjectCodec(new ChangePage(), ['ui', 'relatedPage',
  1358. 'index', 'neverShown', 'page', 'previousPage']);
  1359. var viewStateIgnored = ['defaultParent', 'currentRoot', 'scrollLeft',
  1360. 'scrollTop', 'scale', 'translate', 'lastPasteXml', 'pasteCounter'];
  1361. codec.afterEncode = function(enc, obj, node)
  1362. {
  1363. node.setAttribute('relatedPage', obj.relatedPage.getId())
  1364. if (obj.index == null)
  1365. {
  1366. node.setAttribute('name', obj.relatedPage.getName());
  1367. if (obj.relatedPage.viewState != null)
  1368. {
  1369. node.setAttribute('viewState', JSON.stringify(
  1370. obj.relatedPage.viewState, function(key, value)
  1371. {
  1372. return (mxUtils.indexOf(viewStateIgnored, key) < 0) ? value : undefined;
  1373. }));
  1374. }
  1375. if (obj.relatedPage.root != null)
  1376. {
  1377. enc.encodeCell(obj.relatedPage.root, node);
  1378. }
  1379. }
  1380. return node;
  1381. };
  1382. codec.beforeDecode = function(dec, node, obj)
  1383. {
  1384. obj.ui = dec.ui;
  1385. obj.relatedPage = obj.ui.getPageById(node.getAttribute('relatedPage'));
  1386. if (obj.relatedPage == null)
  1387. {
  1388. var temp = node.ownerDocument.createElement('diagram');
  1389. temp.setAttribute('id', node.getAttribute('relatedPage'));
  1390. temp.setAttribute('name', node.getAttribute('name'));
  1391. obj.relatedPage = new DiagramPage(temp);
  1392. var vs = node.getAttribute('viewState');
  1393. if (vs != null)
  1394. {
  1395. obj.relatedPage.viewState = JSON.parse(vs);
  1396. node.removeAttribute('viewState');
  1397. }
  1398. // Makes sure the original node isn't modified
  1399. node = node.cloneNode(true);
  1400. var tmp = node.firstChild;
  1401. if (tmp != null)
  1402. {
  1403. obj.relatedPage.root = dec.decodeCell(tmp, false);
  1404. var tmp2 = tmp.nextSibling;
  1405. tmp.parentNode.removeChild(tmp);
  1406. tmp = tmp2;
  1407. while (tmp != null)
  1408. {
  1409. tmp2 = tmp.nextSibling;
  1410. if (tmp.nodeType == mxConstants.NODETYPE_ELEMENT)
  1411. {
  1412. // Ignores all existing cells because those do not need to
  1413. // be re-inserted into the model. Since the encoded version
  1414. // of these cells contains the new parent, this would leave
  1415. // to an inconsistent state on the model (ie. a parent
  1416. // change without a call to parentForCellChanged).
  1417. var id = tmp.getAttribute('id');
  1418. if (dec.lookup(id) == null)
  1419. {
  1420. dec.decodeCell(tmp);
  1421. }
  1422. }
  1423. tmp.parentNode.removeChild(tmp);
  1424. tmp = tmp2;
  1425. }
  1426. }
  1427. }
  1428. return node;
  1429. };
  1430. codec.afterDecode = function(dec, node, obj)
  1431. {
  1432. obj.index = obj.previousIndex;
  1433. return obj;
  1434. };
  1435. mxCodecRegistry.register(codec);
  1436. })();