GraphViewer.js 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879
  1. /**
  2. * Copyright (c) 2006-2016, JGraph Ltd
  3. */
  4. /**
  5. * No CSS and resources available in embed mode. Parameters and docs:
  6. * https://desk.draw.io/solution/articles/16000042542-how-to-embed-html-
  7. */
  8. GraphViewer = function(container, xmlNode, graphConfig)
  9. {
  10. this.init(container, xmlNode, graphConfig);
  11. };
  12. // Editor inherits from mxEventSource
  13. mxUtils.extend(GraphViewer, mxEventSource);
  14. /**
  15. * Redirects editing to absolue URLs.
  16. */
  17. GraphViewer.prototype.editBlankUrl = 'https://www.draw.io/?client=1';
  18. /**
  19. * Redirects editing to absolue URLs.
  20. */
  21. GraphViewer.prototype.editBlankFallbackUrl = 'https://www.draw.io/?create=drawdata&splash=0';
  22. /**
  23. * Base URL for relative images.
  24. */
  25. GraphViewer.prototype.imageBaseUrl = 'https://www.draw.io/';
  26. /**
  27. * Redirects editing to absolue URLs.
  28. */
  29. GraphViewer.prototype.toolbarHeight = (document.compatMode == 'BackCompat') ? 28 : 30;
  30. /**
  31. * Redirects editing to absolue URLs.
  32. */
  33. GraphViewer.prototype.lightboxChrome = true;
  34. /**
  35. * Redirects editing to absolue URLs.
  36. */
  37. GraphViewer.prototype.lightboxZIndex = 999;
  38. /**
  39. * Redirects editing to absolue URLs.
  40. */
  41. GraphViewer.prototype.toolbarZIndex = 999;
  42. /**
  43. * Base URL for relative images.
  44. */
  45. GraphViewer.prototype.imageBaseUrl = 'https://www.draw.io/';
  46. /**
  47. * If automatic fit should be enabled if zoom is disabled. Default is true.
  48. */
  49. GraphViewer.prototype.autoFit = true;
  50. /**
  51. * Specifies if zooming in for auto fit is allowed. Default is false.
  52. */
  53. GraphViewer.prototype.allowZoomIn = false;
  54. /**
  55. * Whether the title should be shown as a tooltip if the toolbar is disabled.
  56. * Default is false.
  57. */
  58. GraphViewer.prototype.showTitleAsTooltip = false;
  59. /**
  60. * Specifies if the constructur should delay the rendering if the container
  61. * is not visible by default.
  62. */
  63. GraphViewer.prototype.checkVisibleState = true;
  64. /**
  65. * Initializes the viewer.
  66. */
  67. GraphViewer.prototype.init = function(container, xmlNode, graphConfig)
  68. {
  69. this.graphConfig = (graphConfig != null) ? graphConfig : {};
  70. this.autoFit = (this.graphConfig['auto-fit'] != null) ?
  71. this.graphConfig['auto-fit'] : this.autoFit;
  72. this.allowZoomIn = (this.graphConfig['allow-zoom-in'] != null) ?
  73. this.graphConfig['allow-zoom-in'] : this.allowZoomIn;
  74. this.checkVisibleState = (this.graphConfig['check-visible-state'] != null) ?
  75. this.graphConfig['check-visible-state'] : this.checkVisibleState;
  76. this.toolbarItems = (this.graphConfig.toolbar != null) ?
  77. this.graphConfig.toolbar.split(' ') : [];
  78. this.zoomEnabled = mxUtils.indexOf(this.toolbarItems, 'zoom') >= 0;
  79. this.layersEnabled = mxUtils.indexOf(this.toolbarItems, 'layers') >= 0;
  80. this.lightboxEnabled = mxUtils.indexOf(this.toolbarItems, 'lightbox') >= 0;
  81. this.lightboxClickEnabled = this.graphConfig.lightbox != false;
  82. this.initialWidth = (container != null) ? container.style.width : null;
  83. this.widthIsEmpty = (this.initialWidth != null) ? this.initialWidth == '' : true;
  84. this.currentPage = parseInt(this.graphConfig.page) || 0;
  85. this.editor = null;
  86. if (xmlNode != null)
  87. {
  88. this.xmlDocument = xmlNode.ownerDocument;
  89. this.xmlNode = xmlNode;
  90. this.xml = mxUtils.getXml(xmlNode);
  91. if (container != null)
  92. {
  93. this.graph = new Graph(container);
  94. this.graph.transparentBackground = false;
  95. if (this.graphConfig.move)
  96. {
  97. this.graph.isMoveCellsEvent = function(evt)
  98. {
  99. return true;
  100. };
  101. }
  102. // Adds lightbox and link handling for shapes
  103. if (this.lightboxClickEnabled)
  104. {
  105. container.style.cursor = 'pointer';
  106. }
  107. // Hack for using EditorUi methods on the graph instance
  108. this.editor = new Editor(true, null, null, this.graph);
  109. this.editor.editBlankUrl = this.editBlankUrl;
  110. this.editor.editBlankFallbackUrl = this.editBlankFallbackUrl;
  111. this.graph.lightbox = true;
  112. this.graph.centerZoom = false;
  113. this.graph.autoExtend = false;
  114. this.graph.autoScroll = false;
  115. this.graph.setEnabled(false);
  116. // Handles relative images
  117. var self = this;
  118. this.graph.getImageFromBundles = function(key)
  119. {
  120. return self.getImageUrl(key);
  121. };
  122. if (mxClient.IS_SVG)
  123. {
  124. // LATER: Add shadow for labels in graph.container (eg. math, NO_FO), scaling
  125. this.editor.graph.addSvgShadow(this.graph.view.canvas.ownerSVGElement, null, true);
  126. }
  127. // Adds page placeholders
  128. if (xmlNode.nodeName == 'mxfile')
  129. {
  130. var diagrams = xmlNode.getElementsByTagName('diagram');
  131. if (diagrams.length > 0)
  132. {
  133. var graphGetGlobalVariable = this.graph.getGlobalVariable;
  134. var self = this;
  135. this.graph.getGlobalVariable = function(name)
  136. {
  137. var diagram = diagrams[self.currentPage];
  138. if (name == 'page')
  139. {
  140. return diagram.getAttribute('name') || 'Page-' + (self.currentPage + 1);
  141. }
  142. else if (name == 'pagenumber')
  143. {
  144. return self.currentPage + 1;
  145. }
  146. return graphGetGlobalVariable.apply(this, arguments);
  147. };
  148. }
  149. }
  150. this.diagrams = [];
  151. var lastXmlNode = null;
  152. this.selectPage = function(number)
  153. {
  154. this.currentPage = mxUtils.mod(number, this.diagrams.length);
  155. this.updateGraphXml(mxUtils.parseXml(this.graph.decompress(mxUtils.getTextContent(
  156. this.diagrams[this.currentPage]))).documentElement);
  157. };
  158. this.selectPageById = function(id)
  159. {
  160. for (var i = 0; i < this.diagrams.length; i++)
  161. {
  162. if (this.diagrams[i].getAttribute('id') == id)
  163. {
  164. this.selectPage(i);
  165. break;
  166. }
  167. }
  168. };
  169. var update = mxUtils.bind(this, function()
  170. {
  171. if (this.xmlNode == null || this.xmlNode.nodeName != 'mxfile')
  172. {
  173. this.diagrams = [];
  174. }
  175. if (this.xmlNode != lastXmlNode)
  176. {
  177. this.diagrams = this.xmlNode.getElementsByTagName('diagram');
  178. lastXmlNode = this.xmlNode;
  179. }
  180. });
  181. // LATER: Add event for setGraphXml
  182. this.addListener('xmlNodeChanged', update);
  183. update();
  184. // Passes current page via urlParams global variable
  185. // to let the parser know which page we're using
  186. urlParams['page'] = self.currentPage;
  187. var render = mxUtils.bind(this, function()
  188. {
  189. this.graph.getModel().beginUpdate();
  190. try
  191. {
  192. // Required for correct parsing of fold parameter
  193. urlParams['nav'] = (this.graphConfig.nav != false) ? '1' : '0';
  194. this.editor.setGraphXml(this.xmlNode);
  195. this.graph.border = (this.graphConfig.border != null) ? this.graphConfig.border : 8;
  196. this.graph.view.scale = this.graphConfig.zoom || 1;
  197. }
  198. finally
  199. {
  200. this.graph.getModel().endUpdate();
  201. }
  202. // Adds left-button panning only if scrollbars are visible
  203. this.graph.panningHandler.useLeftButtonForPanning = true;
  204. this.graph.panningHandler.isForcePanningEvent = function(me)
  205. {
  206. return !mxEvent.isPopupTrigger(me.getEvent()) &&
  207. this.graph.container.style.overflow == 'auto';
  208. };
  209. this.graph.panningHandler.usePopupTrigger = false;
  210. this.graph.panningHandler.pinchEnabled = false;
  211. this.graph.panningHandler.ignoreCell = true;
  212. this.graph.setPanning(false);
  213. this.addSizeHandler();
  214. this.showLayers(this.graph);
  215. this.addClickHandler(this.graph);
  216. this.graph.setTooltips(this.graphConfig.tooltips != false);
  217. this.graph.initialViewState = {
  218. translate: this.graph.view.translate.clone(),
  219. scale: this.graph.view.scale
  220. };
  221. if (this.graphConfig.toolbar != null)
  222. {
  223. this.addToolbar();
  224. }
  225. else if (this.graphConfig.title != null && this.showTitleAsTooltip)
  226. {
  227. container.setAttribute('title', this.graphConfig.title);
  228. }
  229. });
  230. var MutObs = window.MutationObserver ||
  231. window.WebKitMutationObserver ||
  232. window.MozMutationObserver;
  233. if (this.checkVisibleState && container.offsetWidth == 0 && typeof MutObs !== 'undefined')
  234. {
  235. // Delayed rendering if inside hidden container and event available
  236. var par = this.getObservableParent(container);
  237. var observer = new MutObs(mxUtils.bind(this, function(mutation)
  238. {
  239. if (container.offsetWidth > 0)
  240. {
  241. observer.disconnect();
  242. render();
  243. }
  244. }));
  245. observer.observe(par, {attributes: true});
  246. }
  247. else
  248. {
  249. // Immediate rendering in all other cases
  250. render();
  251. }
  252. }
  253. }
  254. };
  255. /**
  256. *
  257. */
  258. GraphViewer.prototype.getObservableParent = function(container)
  259. {
  260. var node = container.parentNode;
  261. while (node != document.body && node.parentNode != null &&
  262. mxUtils.getCurrentStyle(node).display !== 'none')
  263. {
  264. node = node.parentNode;
  265. }
  266. return node;
  267. };
  268. /**
  269. *
  270. */
  271. GraphViewer.prototype.getImageUrl = function(url)
  272. {
  273. if (url != null && url.substring(0, 7) != 'http://' &&
  274. url.substring(0, 8) != 'https://' && url.substring(0, 10) != 'data:image')
  275. {
  276. if (url.charAt(0) == '/')
  277. {
  278. url = url.substring(1, url.length);
  279. }
  280. url = this.imageBaseUrl + url;
  281. }
  282. return url;
  283. };
  284. /**
  285. *
  286. */
  287. GraphViewer.prototype.setXmlNode = function(xmlNode)
  288. {
  289. this.xmlDocument = xmlNode.ownerDocument;
  290. this.xml = mxUtils.getXml(xmlNode);
  291. this.xmlNode = xmlNode;
  292. this.updateGraphXml(xmlNode);
  293. this.fireEvent(new mxEventObject('xmlNodeChanged'));
  294. };
  295. /**
  296. *
  297. */
  298. GraphViewer.prototype.setFileNode = function(xmlNode)
  299. {
  300. if (this.xmlNode == null)
  301. {
  302. this.xmlDocument = xmlNode.ownerDocument;
  303. this.xml = mxUtils.getXml(xmlNode);
  304. this.xmlNode = xmlNode;
  305. }
  306. this.setGraphXml(xmlNode);
  307. };
  308. /**
  309. *
  310. */
  311. GraphViewer.prototype.updateGraphXml = function(xmlNode)
  312. {
  313. this.setGraphXml(xmlNode);
  314. this.fireEvent(new mxEventObject('graphChanged'));
  315. };
  316. /**
  317. *
  318. */
  319. GraphViewer.prototype.setGraphXml = function(xmlNode)
  320. {
  321. if (this.graph != null)
  322. {
  323. this.graph.view.translate = new mxPoint();
  324. this.graph.view.scale = 1;
  325. this.graph.getModel().clear();
  326. this.editor.setGraphXml(xmlNode);
  327. // Restores initial CSS state
  328. if (this.widthIsEmpty)
  329. {
  330. this.graph.container.style.width = '';
  331. this.graph.container.style.height = '';
  332. }
  333. else
  334. {
  335. this.graph.container.style.width = this.initialWidth;
  336. }
  337. this.positionGraph();
  338. this.graph.initialViewState = {
  339. translate: this.graph.view.translate.clone(),
  340. scale: this.graph.view.scale
  341. };
  342. }
  343. };
  344. /**
  345. *
  346. */
  347. GraphViewer.prototype.addSizeHandler = function()
  348. {
  349. var container = this.graph.container;
  350. var bounds = this.graph.getGraphBounds();
  351. var updatingOverflow = false;
  352. container.style.overflow = 'hidden';
  353. var updateOverflow = mxUtils.bind(this, function()
  354. {
  355. if (!updatingOverflow)
  356. {
  357. updatingOverflow = true;
  358. var tmp = this.graph.getGraphBounds();
  359. if (container.offsetWidth < tmp.width + this.graph.border)
  360. {
  361. container.style.overflow = 'auto';
  362. }
  363. else
  364. {
  365. container.style.overflow = 'hidden';
  366. }
  367. if (this.toolbar != null)
  368. {
  369. var r = container.getBoundingClientRect();
  370. // Workaround for position:relative set in ResizeSensor
  371. var origin = mxUtils.getScrollOrigin(document.body)
  372. var b = (document.body.style.position === 'relative') ? document.body.getBoundingClientRect() :
  373. {left: -origin.x, top: -origin.y};
  374. r = {left: r.left - b.left, top: r.top - b.top, bottom: r.bottom - b.top, right: r.right - b.left};
  375. this.toolbar.style.left = r.left + 'px';
  376. if (this.graphConfig['toolbar-position'] == 'bottom')
  377. {
  378. this.toolbar.style.top = r.bottom - 1 + 'px';
  379. }
  380. else
  381. {
  382. if (this.graphConfig['toolbar-position'] != 'inline')
  383. {
  384. this.toolbar.style.width = Math.max(this.minToolbarWidth, container.offsetWidth) + 'px';
  385. this.toolbar.style.top = r.top + 1 + 'px';
  386. }
  387. else
  388. {
  389. this.toolbar.style.top = r.top + 'px';
  390. }
  391. }
  392. }
  393. updatingOverflow = false;
  394. }
  395. });
  396. var lastOffsetWidth = null;
  397. var cachedOffsetWidth = null;
  398. var handlingResize = false;
  399. // Installs function on instance
  400. this.fitGraph = function(maxScale)
  401. {
  402. var cachedOffsetWidth = container.offsetWidth;
  403. if (cachedOffsetWidth != lastOffsetWidth)
  404. {
  405. if (!handlingResize)
  406. {
  407. handlingResize = true;
  408. this.graph.maxFitScale = (maxScale != null) ? maxScale : (this.graphConfig.zoom ||
  409. ((this.allowZoomIn) ? null : 1));
  410. this.graph.fit(null, null, null, null, false, true);
  411. this.graph.maxFitScale = null;
  412. var tmp = this.graph.getGraphBounds();
  413. this.updateContainerHeight(container, tmp.height + 2 * this.graph.border + 1);
  414. this.graph.initialViewState = {
  415. translate: this.graph.view.translate.clone(),
  416. scale: this.graph.view.scale
  417. };
  418. lastOffsetWidth = cachedOffsetWidth;
  419. // Workaround for fit triggering scrollbars triggering doResize (infinite loop)
  420. window.setTimeout(function()
  421. {
  422. handlingResize = false;
  423. }, 0);
  424. }
  425. }
  426. };
  427. // Fallback for older browsers
  428. if (mxClient.IS_QUIRKS || document.documentMode <= 9)
  429. {
  430. mxEvent.addListener(window, 'resize', updateOverflow);
  431. this.graph.addListener('size', updateOverflow);
  432. }
  433. else
  434. {
  435. new ResizeSensor(this.graph.container, updateOverflow);
  436. }
  437. if (this.graphConfig.resize || ((this.zoomEnabled || !this.autoFit) && this.graphConfig.resize != false))
  438. {
  439. this.graph.minimumContainerSize = new mxRectangle(0, 0, 100, this.toolbarHeight);
  440. this.graph.resizeContainer = true;
  441. }
  442. else
  443. {
  444. // Sets initial size for responsive diagram to stop at actual size
  445. if (this.widthIsEmpty)
  446. {
  447. this.updateContainerWidth(container, bounds.width + 2 * this.graph.border);
  448. }
  449. this.updateContainerHeight(container, bounds.height + 2 * this.graph.border + 1);
  450. if (!this.zoomEnabled && this.autoFit)
  451. {
  452. var lastOffsetWidth = null;
  453. var scheduledResize = null;
  454. var cachedOffsetWidth = null;
  455. var doResize = mxUtils.bind(this, function()
  456. {
  457. window.clearTimeout(scheduledResize);
  458. if (!handlingResize)
  459. {
  460. scheduledResize = window.setTimeout(mxUtils.bind(this, this.fitGraph), 100);
  461. }
  462. });
  463. // Fallback for older browsers
  464. if (mxClient.IS_QUIRKS || document.documentMode <= 9)
  465. {
  466. mxEvent.addListener(window, 'resize', doResize);
  467. }
  468. else
  469. {
  470. new ResizeSensor(this.graph.container, doResize);
  471. }
  472. }
  473. else if (!(mxClient.IS_QUIRKS || document.documentMode <= 9))
  474. {
  475. this.graph.addListener('size', updateOverflow);
  476. }
  477. }
  478. var positionGraph = mxUtils.bind(this, function()
  479. {
  480. // Allocates maximum width while setting initial view state
  481. var prev = container.style.minWidth;
  482. if (this.widthIsEmpty)
  483. {
  484. container.style.minWidth = '100%';
  485. }
  486. if (container.offsetWidth > 0 && (this.allowZoomIn ||
  487. (bounds.width + 2 * this.graph.border > container.offsetWidth ||
  488. bounds.height + 2 * this.graph.border > this.graphConfig['max-height'])))
  489. {
  490. var maxScale = null;
  491. if (this.graphConfig['max-height'] != null)
  492. {
  493. maxScale = this.graphConfig['max-height'] / (bounds.height + 2 * this.graph.border);
  494. }
  495. this.fitGraph(maxScale);
  496. }
  497. else
  498. {
  499. this.graph.view.setTranslate(Math.floor((this.graph.border - bounds.x) / this.graph.view.scale),
  500. Math.floor((this.graph.border - bounds.y) / this.graph.view.scale));
  501. lastOffsetWidth = container.offsetWidth;
  502. }
  503. container.style.minWidth = prev
  504. });
  505. if (mxClient.IS_QUIRKS || document.documentMode == 8)
  506. {
  507. window.setTimeout(positionGraph, 0);
  508. }
  509. else
  510. {
  511. positionGraph();
  512. }
  513. // Installs function on instance
  514. this.positionGraph = function()
  515. {
  516. bounds = this.graph.getGraphBounds();
  517. lastOffsetWidth = null;
  518. positionGraph();
  519. };
  520. };
  521. /**
  522. *
  523. */
  524. GraphViewer.prototype.updateContainerWidth = function(container, width)
  525. {
  526. container.style.width = width + 'px';
  527. };
  528. /**
  529. *
  530. */
  531. GraphViewer.prototype.updateContainerHeight = function(container, height)
  532. {
  533. if (this.zoomEnabled || !this.autoFit || document.compatMode == 'BackCompat' ||
  534. mxClient.IS_QUIRKS || document.documentMode == 8)
  535. {
  536. container.style.height = height + 'px';
  537. }
  538. };
  539. /**
  540. * Shows the
  541. */
  542. GraphViewer.prototype.showLayers = function(graph, sourceGraph)
  543. {
  544. var layers = this.graphConfig.layers;
  545. if (layers != null || sourceGraph != null)
  546. {
  547. var idx = (layers != null) ? layers.split(' ') : null;
  548. if (sourceGraph != null || idx.length > 0)
  549. {
  550. var source = (sourceGraph != null) ? sourceGraph.getModel() : null;
  551. var model = graph.getModel();
  552. model.beginUpdate();
  553. try
  554. {
  555. var childCount = model.getChildCount(model.root);
  556. // Hides all layers
  557. for (var i = 0; i < childCount; i++)
  558. {
  559. model.setVisible(model.getChildAt(model.root, i),
  560. (sourceGraph != null) ? source.isVisible(source.getChildAt(source.root, i)) : false);
  561. }
  562. // Shows specified layers (eg. 0 1 3)
  563. if (source == null)
  564. {
  565. for (var i = 0; i < idx.length; i++)
  566. {
  567. model.setVisible(model.getChildAt(model.root, parseInt(idx[i])), true);
  568. }
  569. }
  570. }
  571. finally
  572. {
  573. model.endUpdate();
  574. }
  575. }
  576. }
  577. };
  578. /**
  579. *
  580. */
  581. GraphViewer.prototype.addToolbar = function()
  582. {
  583. var container = this.graph.container;
  584. var initialCursor = this.graph.container.style.cursor;
  585. if (this.graphConfig['toolbar-position'] == 'bottom')
  586. {
  587. container.style.marginBottom = this.toolbarHeight + 'px';
  588. }
  589. else if (this.graphConfig['toolbar-position'] != 'inline')
  590. {
  591. container.style.marginTop = this.toolbarHeight + 'px';
  592. }
  593. // Creates toolbar for viewer
  594. var toolbar = container.ownerDocument.createElement('div');
  595. toolbar.style.position = 'absolute';
  596. toolbar.style.overflow = 'hidden';
  597. toolbar.style.boxSizing = 'border-box';
  598. toolbar.style.whiteSpace = 'nowrap';
  599. toolbar.style.zIndex = this.toolbarZIndex;
  600. toolbar.style.backgroundColor = '#eee';
  601. toolbar.style.height = this.toolbarHeight + 'px';
  602. this.toolbar = toolbar;
  603. if (this.graphConfig['toolbar-position'] == 'inline')
  604. {
  605. mxUtils.setPrefixedStyle(toolbar.style, 'transition', 'opacity 100ms ease-in-out');
  606. mxUtils.setOpacity(toolbar, 30);
  607. // Changes toolbar opacity on hover
  608. var fadeThread = null;
  609. var fadeThread2 = null;
  610. var fadeOut = mxUtils.bind(this, function(delay)
  611. {
  612. if (fadeThread != null)
  613. {
  614. window.clearTimeout(fadeThread);
  615. fadeThead = null;
  616. }
  617. if (fadeThread2 != null)
  618. {
  619. window.clearTimeout(fadeThread2);
  620. fadeThead2 = null;
  621. }
  622. fadeThread = window.setTimeout(mxUtils.bind(this, function()
  623. {
  624. mxUtils.setOpacity(toolbar, 0);
  625. fadeThread = null;
  626. fadeThread2 = window.setTimeout(mxUtils.bind(this, function()
  627. {
  628. toolbar.style.display = 'none';
  629. fadeThread2 = null;
  630. }), 100);
  631. }), delay || 200);
  632. });
  633. var fadeIn = mxUtils.bind(this, function(opacity)
  634. {
  635. if (fadeThread != null)
  636. {
  637. window.clearTimeout(fadeThread);
  638. fadeThead = null;
  639. }
  640. if (fadeThread2 != null)
  641. {
  642. window.clearTimeout(fadeThread2);
  643. fadeThead2 = null;
  644. }
  645. toolbar.style.display = '';
  646. mxUtils.setOpacity(toolbar, opacity || 30);
  647. });
  648. mxEvent.addListener(this.graph.container, (mxClient.IS_POINTER) ? 'pointermove' : 'mousemove', mxUtils.bind(this, function(evt)
  649. {
  650. if (!mxEvent.isTouchEvent(evt))
  651. {
  652. fadeIn(30);
  653. fadeOut();
  654. }
  655. }));
  656. mxEvent.addListener(toolbar, (mxClient.IS_POINTER) ? 'pointermove' : 'mousemove', function(evt)
  657. {
  658. mxEvent.consume(evt);
  659. });
  660. mxEvent.addListener(toolbar, 'mouseenter', mxUtils.bind(this, function(evt)
  661. {
  662. fadeIn(100);
  663. }));
  664. mxEvent.addListener(toolbar, 'mousemove', mxUtils.bind(this, function(evt)
  665. {
  666. fadeIn(100);
  667. mxEvent.consume(evt);
  668. }));
  669. mxEvent.addListener(toolbar, 'mouseleave', mxUtils.bind(this, function(evt)
  670. {
  671. if (!mxEvent.isTouchEvent(evt))
  672. {
  673. fadeIn(30);
  674. }
  675. }));
  676. // Shows/hides toolbar for touch devices
  677. var graph = this.graph;
  678. var tol = graph.getTolerance();
  679. graph.addMouseListener(
  680. {
  681. startX: 0,
  682. startY: 0,
  683. scrollLeft: 0,
  684. scrollTop: 0,
  685. mouseDown: function(sender, me)
  686. {
  687. this.startX = me.getGraphX();
  688. this.startY = me.getGraphY();
  689. this.scrollLeft = graph.container.scrollLeft;
  690. this.scrollTop = graph.container.scrollTop;
  691. },
  692. mouseMove: function(sender, me) {},
  693. mouseUp: function(sender, me)
  694. {
  695. if (mxEvent.isTouchEvent(me.getEvent()))
  696. {
  697. if ((Math.abs(this.scrollLeft - graph.container.scrollLeft) < tol &&
  698. Math.abs(this.scrollTop - graph.container.scrollTop) < tol) &&
  699. (Math.abs(this.startX - me.getGraphX()) < tol &&
  700. Math.abs(this.startY - me.getGraphY()) < tol))
  701. {
  702. if (parseFloat(toolbar.style.opacity || 0) > 0)
  703. {
  704. fadeOut();
  705. }
  706. else
  707. {
  708. fadeIn(30);
  709. }
  710. }
  711. }
  712. }
  713. });
  714. }
  715. var tokens = this.toolbarItems;
  716. var buttonCount = 0;
  717. function addButton(fn, imgSrc, tip, enabled)
  718. {
  719. var a = document.createElement('div');
  720. a.style.borderRight = '1px solid #d0d0d0';
  721. a.style.padding = '3px 6px 3px 6px';
  722. mxEvent.addListener(a, 'click', fn);
  723. if (tip != null)
  724. {
  725. a.setAttribute('title', tip);
  726. }
  727. if (mxClient.IS_QUIRKS)
  728. {
  729. a.style.display = 'inline';
  730. }
  731. else
  732. {
  733. a.style.display = 'inline-block';
  734. }
  735. var img = document.createElement('img');
  736. img.setAttribute('border', '0');
  737. img.setAttribute('src', imgSrc);
  738. if (enabled == null || enabled)
  739. {
  740. mxEvent.addListener(a, 'mouseenter', function()
  741. {
  742. a.style.backgroundColor = '#ddd';
  743. });
  744. mxEvent.addListener(a, 'mouseleave', function()
  745. {
  746. a.style.backgroundColor = '#eee';
  747. });
  748. mxUtils.setOpacity(img, 60);
  749. a.style.cursor = 'pointer';
  750. }
  751. else
  752. {
  753. mxUtils.setOpacity(a, 30);
  754. }
  755. a.appendChild(img);
  756. toolbar.appendChild(a);
  757. buttonCount++;
  758. return a;
  759. };
  760. var layersDialog = null;
  761. var layersDialogEntered = false;
  762. var pageInfo = null;
  763. for (var i = 0; i < tokens.length; i++)
  764. {
  765. var token = tokens[i];
  766. if (token == 'pages')
  767. {
  768. pageInfo = container.ownerDocument.createElement('div');
  769. pageInfo.style.cssText = 'display:inline-block;position:relative;padding:3px 4px 0 4px;' +
  770. 'vertical-align:top;font-family:Helvetica,Arial;font-size:12px;top:4px;cursor:default;'
  771. mxUtils.setOpacity(pageInfo, 70);
  772. var prevButton = addButton(mxUtils.bind(this, function()
  773. {
  774. this.selectPage(this.currentPage - 1);
  775. }), Editor.previousImage, mxResources.get('previousPage') || 'Previous Page');
  776. prevButton.style.borderRightStyle = 'none';
  777. prevButton.style.paddingLeft = '0px';
  778. prevButton.style.paddingRight = '0px';
  779. toolbar.appendChild(pageInfo);
  780. var nextButton = addButton(mxUtils.bind(this, function()
  781. {
  782. this.selectPage(this.currentPage + 1);
  783. }), Editor.nextImage, mxResources.get('nextPage') || 'Next Page');
  784. nextButton.style.paddingLeft = '0px';
  785. nextButton.style.paddingRight = '0px';
  786. var lastXmlNode = null;
  787. var update = mxUtils.bind(this, function()
  788. {
  789. pageInfo.innerHTML = '';
  790. mxUtils.write(pageInfo, (this.currentPage + 1) + ' / ' + this.diagrams.length);
  791. pageInfo.style.display = (this.diagrams.length > 1) ? 'inline-block' : 'none';
  792. prevButton.style.display = pageInfo.style.display;
  793. nextButton.style.display = pageInfo.style.display;
  794. });
  795. // LATER: Add event for setGraphXml
  796. this.addListener('graphChanged', update);
  797. update();
  798. }
  799. else if (token == 'zoom')
  800. {
  801. if (this.zoomEnabled)
  802. {
  803. addButton(mxUtils.bind(this, function()
  804. {
  805. this.graph.zoomOut();
  806. }), Editor.zoomOutImage, mxResources.get('zoomOut') || 'Zoom Out');
  807. addButton(mxUtils.bind(this, function()
  808. {
  809. this.graph.zoomIn();
  810. }), Editor.zoomInImage, mxResources.get('zoomIn') || 'Zoom In');
  811. addButton(mxUtils.bind(this, function()
  812. {
  813. this.graph.view.scaleAndTranslate(this.graph.initialViewState.scale,
  814. this.graph.initialViewState.translate.x,
  815. this.graph.initialViewState.translate.y);
  816. }), Editor.zoomFitImage, mxResources.get('fit') || 'Fit');
  817. }
  818. }
  819. else if (token == 'layers')
  820. {
  821. if (this.layersEnabled)
  822. {
  823. var model = this.graph.getModel();
  824. var layersButton = addButton(mxUtils.bind(this, function(evt)
  825. {
  826. if (layersDialog != null)
  827. {
  828. layersDialog.parentNode.removeChild(layersDialog);
  829. layersDialog = null;
  830. }
  831. else
  832. {
  833. layersDialog = this.graph.createLayersDialog();
  834. mxEvent.addListener(layersDialog, 'mouseleave', function()
  835. {
  836. layersDialog.parentNode.removeChild(layersDialog);
  837. layersDialog = null;
  838. });
  839. var r = layersButton.getBoundingClientRect();
  840. layersDialog.style.width = '140px';
  841. layersDialog.style.padding = '2px 0px 2px 0px';
  842. layersDialog.style.border = '1px solid #d0d0d0';
  843. layersDialog.style.backgroundColor = '#eee';
  844. layersDialog.style.fontFamily = 'Helvetica Neue,Helvetica,Arial Unicode MS,Arial';
  845. layersDialog.style.fontSize = '11px';
  846. layersDialog.style.zIndex = this.toolbarZIndex + 1;
  847. mxUtils.setOpacity(layersDialog, 80);
  848. var origin = mxUtils.getDocumentScrollOrigin(document);
  849. layersDialog.style.left = origin.x + r.left + 'px';
  850. layersDialog.style.top = origin.y + r.bottom + 'px';
  851. document.body.appendChild(layersDialog);
  852. }
  853. }), Editor.layersImage, mxResources.get('layers') || 'Layers');
  854. model.addListener(mxEvent.CHANGE, function()
  855. {
  856. layersButton.style.display = (model.getChildCount(model.root) > 1) ? 'inline-block' : 'none';
  857. });
  858. layersButton.style.display = (model.getChildCount(model.root) > 1) ? 'inline-block' : 'none';
  859. }
  860. }
  861. else if (token == 'lightbox')
  862. {
  863. if (this.lightboxEnabled)
  864. {
  865. addButton(mxUtils.bind(this, function()
  866. {
  867. this.showLightbox();
  868. }), Editor.maximizeImage, (mxResources.get('show') || 'Show'));
  869. }
  870. }
  871. else if (this.graphConfig['toolbar-buttons'] != null)
  872. {
  873. var def = this.graphConfig['toolbar-buttons'][token];
  874. if (def != null)
  875. {
  876. addButton((def.enabled == null || def.enabled) ? def.handler : function() {},
  877. def.image, def.title, def.enabled);
  878. }
  879. }
  880. }
  881. if (this.graph.minimumContainerSize != null)
  882. {
  883. this.graph.minimumContainerSize.width = buttonCount * 34;
  884. }
  885. if (this.graphConfig.title != null)
  886. {
  887. var filename = container.ownerDocument.createElement('div');
  888. filename.style.cssText = 'display:inline-block;position:relative;padding:3px 6px 0 6px;' +
  889. 'vertical-align:top;font-family:Helvetica,Arial;font-size:12px;top:4px;cursor:default;'
  890. filename.setAttribute('title', this.graphConfig.title);
  891. mxUtils.write(filename, this.graphConfig.title);
  892. mxUtils.setOpacity(filename, 70);
  893. toolbar.appendChild(filename);
  894. }
  895. this.minToolbarWidth = buttonCount * 34;
  896. var prevBorder = container.style.border;
  897. var enter = mxUtils.bind(this, function()
  898. {
  899. var r = container.getBoundingClientRect();
  900. // Workaround for position:relative set in ResizeSensor
  901. var origin = mxUtils.getScrollOrigin(document.body)
  902. var b = (document.body.style.position === 'relative') ? document.body.getBoundingClientRect() :
  903. {left: -origin.x, top: -origin.y};
  904. r = {left: r.left - b.left, top: r.top - b.top, bottom: r.bottom - b.top, right: r.right - b.left};
  905. toolbar.style.left = r.left + 'px';
  906. toolbar.style.width = (this.graphConfig['toolbar-position'] == 'inline') ? 'auto' :
  907. Math.max(this.minToolbarWidth, container.offsetWidth) + 'px';
  908. toolbar.style.border = '1px solid #d0d0d0';
  909. if (this.graphConfig['toolbar-position'] == 'bottom')
  910. {
  911. toolbar.style.top = r.bottom - 1 + 'px';
  912. }
  913. else
  914. {
  915. if (this.graphConfig['toolbar-position'] != 'inline')
  916. {
  917. toolbar.style.marginTop = -this.toolbarHeight + 'px';
  918. toolbar.style.top = r.top + 1 + 'px';
  919. }
  920. else
  921. {
  922. toolbar.style.top = r.top + 'px';
  923. }
  924. }
  925. if (prevBorder == '1px solid transparent')
  926. {
  927. container.style.border = '1px solid #d0d0d0';
  928. }
  929. document.body.appendChild(toolbar);
  930. var hideToolbar = mxUtils.bind(this, function()
  931. {
  932. if (this.graphConfig['toolbar-nohide'] != true)
  933. {
  934. if (toolbar.parentNode != null)
  935. {
  936. toolbar.parentNode.removeChild(toolbar);
  937. }
  938. if (layersDialog != null)
  939. {
  940. layersDialog.parentNode.removeChild(layersDialog);
  941. layersDialog = null;
  942. }
  943. container.style.border = prevBorder;
  944. }
  945. });
  946. mxEvent.addListener(document, 'mousemove', function(evt)
  947. {
  948. var source = mxEvent.getSource(evt);
  949. while (source != null)
  950. {
  951. if (source == container || source == toolbar || source == layersDialog)
  952. {
  953. return;
  954. }
  955. source = source.parentNode;
  956. }
  957. hideToolbar();
  958. });
  959. mxEvent.addListener(document, 'mouseleave', function(evt)
  960. {
  961. hideToolbar();
  962. });
  963. });
  964. mxEvent.addListener(container, 'mouseenter', enter);
  965. };
  966. /**
  967. * Adds event handler for links and lightbox.
  968. */
  969. GraphViewer.prototype.addClickHandler = function(graph, ui)
  970. {
  971. graph.linkPolicy = this.graphConfig.target || graph.linkPolicy;
  972. graph.addClickHandler(this.graphConfig.highlight, mxUtils.bind(this, function(evt, href)
  973. {
  974. if (href == null)
  975. {
  976. var source = mxEvent.getSource(evt);
  977. if (source.nodeName.toLowerCase() == 'a')
  978. {
  979. href = source.getAttribute('href');
  980. }
  981. }
  982. if (ui != null)
  983. {
  984. if (href != null && !(graph.isExternalProtocol(href) || graph.isBlankLink(href)))
  985. {
  986. // Hides lightbox if any links are clicked
  987. // Async handling needed for anchors to work
  988. window.setTimeout(function()
  989. {
  990. ui.destroy();
  991. }, 0);
  992. }
  993. }
  994. else
  995. {
  996. if (href != null && graph.isPageLink(href))
  997. {
  998. var comma = href.indexOf(',');
  999. if (comma > 0)
  1000. {
  1001. var id = href.substring(comma + 1);
  1002. this.selectPageById(id);
  1003. mxEvent.consume(evt);
  1004. }
  1005. }
  1006. }
  1007. }), mxUtils.bind(this, function(evt)
  1008. {
  1009. if (ui == null && this.lightboxClickEnabled &&
  1010. (!mxEvent.isTouchEvent(evt) ||
  1011. this.toolbarItems.length == 0))
  1012. {
  1013. this.showLightbox();
  1014. }
  1015. }));
  1016. };
  1017. /**
  1018. * Adds the given array of stencils to avoid dynamic loading of shapes.
  1019. */
  1020. GraphViewer.prototype.showLightbox = function()
  1021. {
  1022. if (this.graphConfig.lightbox == 'open' || window.self !== window.top)
  1023. {
  1024. var url = 'https://www.draw.io/?client=1&lightbox=1&close=1&edit=_blank&target=blank';
  1025. url += (this.layersEnabled) ? '&layers=1' : '';
  1026. if (typeof window.postMessage !== 'undefined' && (document.documentMode == null || document.documentMode >= 10))
  1027. {
  1028. var wnd = null;
  1029. var receive = mxUtils.bind(this, function(evt)
  1030. {
  1031. if (evt.data == 'ready' && evt.source == wnd)
  1032. {
  1033. wnd.postMessage(this.xml, '*');
  1034. mxEvent.removeListener(window, 'message', receive);
  1035. }
  1036. });
  1037. mxEvent.addListener(window, 'message', receive);
  1038. wnd = window.open(url);
  1039. }
  1040. else
  1041. {
  1042. // Data is pulled from global variable after tab loads
  1043. window.drawdata = this.xml;
  1044. window.open(url);
  1045. }
  1046. }
  1047. else
  1048. {
  1049. this.showLocalLightbox();
  1050. }
  1051. };
  1052. /**
  1053. * Adds the given array of stencils to avoid dynamic loading of shapes.
  1054. */
  1055. GraphViewer.prototype.showLocalLightbox = function()
  1056. {
  1057. var origin = mxUtils.getDocumentScrollOrigin(document);
  1058. var backdrop = document.createElement('div');
  1059. if (mxClient.IS_QUIRKS)
  1060. {
  1061. backdrop.style.position = 'absolute';
  1062. backdrop.style.left = origin.x + 'px';
  1063. backdrop.style.top = origin.y + 'px';
  1064. backdrop.style.width = document.body.offsetWidth + 'px';
  1065. backdrop.style.height = document.body.offsetHeight + 'px';
  1066. }
  1067. else
  1068. {
  1069. backdrop.style.cssText = 'position:fixed;top:0;left:0;bottom:0;right:0;';
  1070. }
  1071. backdrop.style.zIndex = this.lightboxZIndex;
  1072. backdrop.style.backgroundColor = '#000000';
  1073. mxUtils.setOpacity(backdrop, 70);
  1074. document.body.appendChild(backdrop);
  1075. var closeImg = document.createElement('img');
  1076. closeImg.setAttribute('border', '0');
  1077. closeImg.setAttribute('src', Editor.closeImage);
  1078. if (mxClient.IS_QUIRKS)
  1079. {
  1080. closeImg.style.position = 'absolute';
  1081. closeImg.style.right = 32 + 'px';
  1082. closeImg.style.top = origin.y + 32 + 'px';
  1083. }
  1084. else
  1085. {
  1086. closeImg.style.cssText = 'position:fixed;top:32px;right:32px;';
  1087. }
  1088. closeImg.style.cursor = 'pointer';
  1089. mxEvent.addListener(closeImg, 'click', function()
  1090. {
  1091. ui.destroy();
  1092. });
  1093. // LATER: Make possible to assign after instance was created
  1094. urlParams['pages'] = '1';
  1095. urlParams['page'] = this.currentPage;
  1096. urlParams['nav'] = (this.graphConfig.nav != false) ? '1' : '0';
  1097. urlParams['layers'] = (this.layersEnabled) ? '1' : '0';
  1098. // PostMessage not working and Permission denied for opened access in IE9-
  1099. if (document.documentMode == null || document.documentMode >= 10)
  1100. {
  1101. Editor.prototype.editButtonLink = this.graphConfig.edit;
  1102. }
  1103. EditorUi.prototype.updateActionStates = function() {};
  1104. EditorUi.prototype.addBeforeUnloadListener = function() {};
  1105. EditorUi.prototype.addChromelessClickHandler = function() {};
  1106. // Workaround for lost reference with same ID (cannot override after instance is created)
  1107. Graph.prototype.shadowId = 'lightboxDropShadow';
  1108. var ui = new EditorUi(new Editor(true), document.createElement('div'), true);
  1109. // Workaround for lost reference with same ID
  1110. Graph.prototype.shadowId = 'dropShadow';
  1111. // Disables refresh
  1112. ui.refresh = function() {};
  1113. // Click on backdrop closes lightbox
  1114. mxEvent.addListener(backdrop, 'click', function()
  1115. {
  1116. ui.destroy();
  1117. });
  1118. // Passes current page and local URLs to open in new window action
  1119. ui.editor.editBlankUrl = this.editBlankUrl;
  1120. ui.editor.editBlankFallbackUrl = this.editBlankFallbackUrl;
  1121. var editorGetEditBlankUrl = ui.editor.getEditBlankUrl;
  1122. ui.editor.getEditBlankUrl = function(params, fallback)
  1123. {
  1124. var param = '';
  1125. if (ui.pages != null && ui.currentPage != null)
  1126. {
  1127. var pageIndex = mxUtils.indexOf(ui.pages, ui.currentPage);
  1128. if (pageIndex > 0)
  1129. {
  1130. param = '&page=' + pageIndex;
  1131. }
  1132. }
  1133. return editorGetEditBlankUrl.apply(this, arguments) + param;
  1134. }
  1135. // Handles escape keystroke
  1136. var keydownHandler = mxUtils.bind(this, function(evt)
  1137. {
  1138. if (evt.keyCode == 27 /* Escape */)
  1139. {
  1140. ui.destroy();
  1141. }
  1142. });
  1143. var destroy = ui.destroy;
  1144. ui.destroy = function()
  1145. {
  1146. mxEvent.removeListener(document.documentElement, 'keydown', keydownHandler);
  1147. document.body.removeChild(backdrop);
  1148. document.body.removeChild(closeImg);
  1149. document.body.style.overflow = 'auto';
  1150. GraphViewer.resizeSensorEnabled = true;
  1151. destroy.apply(this, arguments);
  1152. };
  1153. var graph = ui.editor.graph;
  1154. var lightbox = graph.container;
  1155. lightbox.style.overflow = 'hidden';
  1156. if (this.lightboxChrome)
  1157. {
  1158. lightbox.style.border = '1px solid #c0c0c0';
  1159. lightbox.style.margin = '40px';
  1160. // Installs the keystroke listener in the target
  1161. mxEvent.addListener(document.documentElement, 'keydown', keydownHandler);
  1162. }
  1163. else
  1164. {
  1165. backdrop.style.display = 'none';
  1166. closeImg.style.display = 'none';
  1167. }
  1168. // Handles relative images
  1169. var self = this;
  1170. graph.getImageFromBundles = function(key)
  1171. {
  1172. return self.getImageUrl(key);
  1173. };
  1174. // Handles relative images in print output and temporary graphs
  1175. var uiCreateTemporaryGraph = ui.createTemporaryGraph;
  1176. ui.createTemporaryGraph = function()
  1177. {
  1178. var graph = uiCreateTemporaryGraph.apply(this, arguments);
  1179. graph.getImageFromBundles = function(key)
  1180. {
  1181. return self.getImageUrl(key);
  1182. };
  1183. return graph;
  1184. };
  1185. if (this.graphConfig.move)
  1186. {
  1187. graph.isMoveCellsEvent = function(evt)
  1188. {
  1189. return true;
  1190. };
  1191. }
  1192. if (!mxClient.IS_QUIRKS)
  1193. {
  1194. mxUtils.setPrefixedStyle(lightbox.style, 'border-radius', '4px');
  1195. lightbox.style.position = 'fixed';
  1196. }
  1197. GraphViewer.resizeSensorEnabled = false;
  1198. document.body.style.overflow = 'hidden';
  1199. // Workaround for possible rendering issues in Safari
  1200. if (!mxClient.IS_SF)
  1201. {
  1202. mxUtils.setPrefixedStyle(lightbox.style, 'transform', 'rotateY(90deg)');
  1203. mxUtils.setPrefixedStyle(lightbox.style, 'transition', 'all .25s ease-in-out');
  1204. }
  1205. this.addClickHandler(graph, ui);
  1206. window.setTimeout(mxUtils.bind(this, function()
  1207. {
  1208. // Disables focus border in Chrome
  1209. lightbox.style.outline = 'none';
  1210. lightbox.style.zIndex = this.lightboxZIndex;
  1211. closeImg.style.zIndex = this.lightboxZIndex;
  1212. document.body.appendChild(lightbox);
  1213. document.body.appendChild(closeImg);
  1214. ui.setFileData(this.xml);
  1215. mxUtils.setPrefixedStyle(lightbox.style, 'transform', 'rotateY(0deg)');
  1216. ui.chromelessToolbar.style.bottom = 60 + 'px';
  1217. ui.chromelessToolbar.style.zIndex = this.lightboxZIndex;
  1218. // Workaround for clipping in IE11-
  1219. document.body.appendChild(ui.chromelessToolbar);
  1220. ui.getEditBlankXml = mxUtils.bind(this, function()
  1221. {
  1222. return this.xml;
  1223. });
  1224. if (mxClient.IS_QUIRKS)
  1225. {
  1226. lightbox.style.position = 'absolute';
  1227. lightbox.style.display = 'block';
  1228. lightbox.style.left = origin.x + 'px';
  1229. lightbox.style.top = origin.y + 'px';
  1230. lightbox.style.width = document.body.clientWidth - 80 + 'px';
  1231. lightbox.style.height = document.body.clientHeight - 80 + 'px';
  1232. lightbox.style.backgroundColor = 'white';
  1233. ui.chromelessToolbar.style.display = 'block';
  1234. ui.chromelessToolbar.style.position = 'absolute';
  1235. ui.chromelessToolbar.style.bottom = '';
  1236. ui.chromelessToolbar.style.top = origin.y +
  1237. document.body.clientHeight - 100 + 'px';
  1238. }
  1239. ui.lightboxFit();
  1240. ui.chromelessResize();
  1241. this.showLayers(graph, this.graph);
  1242. }), 0);
  1243. return ui;
  1244. };
  1245. /**
  1246. *
  1247. */
  1248. GraphViewer.processElements = function(classname)
  1249. {
  1250. mxUtils.forEach(GraphViewer.getElementsByClassName(classname || 'mxgraph'), function(div)
  1251. {
  1252. try
  1253. {
  1254. div.innerHTML = '';
  1255. GraphViewer.createViewerForElement(div);
  1256. }
  1257. catch (e)
  1258. {
  1259. div.innerHTML = e.message;
  1260. throw e;
  1261. }
  1262. });
  1263. };
  1264. /**
  1265. * Adds the given array of stencils to avoid dynamic loading of shapes.
  1266. */
  1267. GraphViewer.getElementsByClassName = function(classname)
  1268. {
  1269. if (document.getElementsByClassName)
  1270. {
  1271. var divs = document.getElementsByClassName(classname);
  1272. // Workaround for changing divs while processing
  1273. var result = [];
  1274. for (var i = 0; i < divs.length; i++)
  1275. {
  1276. result.push(divs[i]);
  1277. }
  1278. return result;
  1279. }
  1280. else
  1281. {
  1282. var tmp = document.getElementsByTagName('*');
  1283. var divs = [];
  1284. for (var i = 0; i < tmp.length; i++)
  1285. {
  1286. var cls = tmp[i].className;
  1287. if (cls != null && cls.length > 0)
  1288. {
  1289. var tokens = cls.split(' ');
  1290. if (mxUtils.indexOf(tokens, classname) >= 0)
  1291. {
  1292. divs.push(tmp[i]);
  1293. }
  1294. }
  1295. }
  1296. return divs;
  1297. }
  1298. };
  1299. /**
  1300. * Adds the given array of stencils to avoid dynamic loading of shapes.
  1301. */
  1302. GraphViewer.createViewerForElement = function(element, callback)
  1303. {
  1304. var data = element.getAttribute('data-mxgraph');
  1305. if (data != null)
  1306. {
  1307. var config = JSON.parse(data);
  1308. var createViewer = function(xml)
  1309. {
  1310. var xmlDoc = mxUtils.parseXml(xml);
  1311. var viewer = new GraphViewer(element, xmlDoc.documentElement, config);
  1312. if (callback != null)
  1313. {
  1314. callback(viewer);
  1315. }
  1316. };
  1317. if (config.url != null)
  1318. {
  1319. GraphViewer.getUrl(config.url, function(xml)
  1320. {
  1321. createViewer(xml);
  1322. });
  1323. }
  1324. else
  1325. {
  1326. createViewer(config.xml);
  1327. }
  1328. }
  1329. };
  1330. /**
  1331. * Adds event if grid size is changed.
  1332. */
  1333. GraphViewer.initCss = function()
  1334. {
  1335. try
  1336. {
  1337. var style = document.createElement('style')
  1338. style.type = 'text/css'
  1339. style.innerHTML = ['div.mxTooltip {',
  1340. '-webkit-box-shadow: 3px 3px 12px #C0C0C0;',
  1341. '-moz-box-shadow: 3px 3px 12px #C0C0C0;',
  1342. 'box-shadow: 3px 3px 12px #C0C0C0;',
  1343. 'background: #FFFFCC;',
  1344. 'border-style: solid;',
  1345. 'border-width: 1px;',
  1346. 'border-color: black;',
  1347. 'font-family: Arial;',
  1348. 'font-size: 8pt;',
  1349. 'position: absolute;',
  1350. 'cursor: default;',
  1351. 'padding: 4px;',
  1352. 'color: black;}',
  1353. 'td.mxPopupMenuIcon div {',
  1354. 'width:16px;',
  1355. 'height:16px;}',
  1356. 'html div.mxPopupMenu {',
  1357. '-webkit-box-shadow:2px 2px 3px #d5d5d5;',
  1358. '-moz-box-shadow:2px 2px 3px #d5d5d5;',
  1359. 'box-shadow:2px 2px 3px #d5d5d5;',
  1360. '_filter:progid:DXImageTransform.Microsoft.DropShadow(OffX=2, OffY=2, Color=\'#d0d0d0\',Positive=\'true\');',
  1361. 'background:white;',
  1362. 'position:absolute;',
  1363. 'border:3px solid #e7e7e7;',
  1364. 'padding:3px;}',
  1365. 'html table.mxPopupMenu {',
  1366. 'border-collapse:collapse;',
  1367. 'margin:0px;}',
  1368. 'html td.mxPopupMenuItem {',
  1369. 'padding:7px 30px 7px 30px;',
  1370. 'font-family:Helvetica Neue,Helvetica,Arial Unicode MS,Arial;',
  1371. 'font-size:10pt;}',
  1372. 'html td.mxPopupMenuIcon {',
  1373. 'background-color:white;',
  1374. 'padding:0px;}',
  1375. 'td.mxPopupMenuIcon .geIcon {',
  1376. 'padding:2px;',
  1377. 'padding-bottom:4px;',
  1378. 'margin:2px;',
  1379. 'border:1px solid transparent;',
  1380. 'opacity:0.5;',
  1381. '_width:26px;',
  1382. '_height:26px;}',
  1383. 'td.mxPopupMenuIcon .geIcon:hover {',
  1384. 'border:1px solid gray;',
  1385. 'border-radius:2px;',
  1386. 'opacity:1;}',
  1387. 'html tr.mxPopupMenuItemHover {',
  1388. 'background-color: #eeeeee;',
  1389. 'color: black;}',
  1390. 'table.mxPopupMenu hr {',
  1391. 'color:#cccccc;',
  1392. 'background-color:#cccccc;',
  1393. 'border:none;',
  1394. 'height:1px;}',
  1395. 'table.mxPopupMenu tr { font-size:4pt;}',
  1396. // Modified to only apply to the print dialog
  1397. '.geDialog { font-family:Helvetica Neue,Helvetica,Arial Unicode MS,Arial;',
  1398. 'font-size:10pt;',
  1399. 'border:none;',
  1400. 'margin:0px;}',
  1401. // These are required for the print dialog
  1402. '.geDialog { position:absolute; background:white; overflow:hidden; padding:30px; border:1px solid #acacac; -webkit-box-shadow:0px 0px 2px 2px #d5d5d5; -moz-box-shadow:0px 0px 2px 2px #d5d5d5; box-shadow:0px 0px 2px 2px #d5d5d5; _filter:progid:DXImageTransform.Microsoft.DropShadow(OffX=2, OffY=2, Color=\'#d5d5d5\', Positive=\'true\'); z-index: 2;}.geDialogClose { position:absolute; width:9px; height:9px; opacity:0.5; cursor:pointer; _filter:alpha(opacity=50);}.geDialogClose:hover { opacity:1;}.geDialogTitle { box-sizing:border-box; white-space:nowrap; background:rgb(229, 229, 229); border-bottom:1px solid rgb(192, 192, 192); font-size:15px; font-weight:bold; text-align:center; color:rgb(35, 86, 149);}.geDialogFooter { background:whiteSmoke; white-space:nowrap; text-align:right; box-sizing:border-box; border-top:1px solid #e5e5e5; color:darkGray;}',
  1403. '.geBtn { background-color: #f5f5f5; border-radius: 2px; border: 1px solid #d8d8d8; color: #333; cursor: default; font-size: 11px; font-weight: bold; height: 29px; line-height: 27px; margin: 0 0 0 8px; min-width: 72px; outline: 0; padding: 0 8px; cursor: pointer;}.geBtn:hover, .geBtn:focus { -webkit-box-shadow: 0px 1px 1px rgba(0,0,0,0.1); -moz-box-shadow: 0px 1px 1px rgba(0,0,0,0.1); box-shadow: 0px 1px 1px rgba(0,0,0,0.1); border: 1px solid #c6c6c6; background-color: #f8f8f8; background-image: linear-gradient(#f8f8f8 0px,#f1f1f1 100%); color: #111;}.geBtn:disabled { opacity: .5;}.gePrimaryBtn { background-color: #4d90fe; background-image: linear-gradient(#4d90fe 0px,#4787ed 100%); border: 1px solid #3079ed; color: #fff;}.gePrimaryBtn:hover, .gePrimaryBtn:focus { background-color: #357ae8; background-image: linear-gradient(#4d90fe 0px,#357ae8 100%); border: 1px solid #2f5bb7; color: #fff;}.gePrimaryBtn:disabled { opacity: .5;}'].join('\n');
  1404. document.getElementsByTagName('head')[0].appendChild(style)
  1405. }
  1406. catch (e)
  1407. {
  1408. // ignore
  1409. }
  1410. };
  1411. /**
  1412. * Lookup for URLs.
  1413. */
  1414. GraphViewer.cachedUrls = {};
  1415. /**
  1416. * Workaround for unsupported CORS in IE9 XHR
  1417. */
  1418. GraphViewer.getUrl = function(url, onload, onerror)
  1419. {
  1420. if (GraphViewer.cachedUrls[url] != null)
  1421. {
  1422. onload(GraphViewer.cachedUrls[url]);
  1423. }
  1424. else
  1425. {
  1426. var xhr = (navigator.userAgent.indexOf('MSIE 9') > 0) ? new XDomainRequest() : new XMLHttpRequest();
  1427. xhr.open('GET', url);
  1428. xhr.onload = function()
  1429. {
  1430. onload((xhr.getText != null) ? xhr.getText() : xhr.responseText);
  1431. };
  1432. xhr.onerror = onerror;
  1433. xhr.send();
  1434. }
  1435. };
  1436. /**
  1437. * Redirects editing to absolue URLs.
  1438. */
  1439. GraphViewer.resizeSensorEnabled = true;
  1440. /**
  1441. * Copyright Marc J. Schmidt. See the LICENSE file at the top-level
  1442. * directory of this distribution and at
  1443. * https://github.com/marcj/css-element-queries/blob/master/LICENSE.
  1444. */
  1445. (function() {
  1446. // Only used for the dirty checking, so the event callback count is limted to max 1 call per fps per sensor.
  1447. // In combination with the event based resize sensor this saves cpu time, because the sensor is too fast and
  1448. // would generate too many unnecessary events.
  1449. var requestAnimationFrame = window.requestAnimationFrame ||
  1450. window.mozRequestAnimationFrame ||
  1451. window.webkitRequestAnimationFrame ||
  1452. function (fn) {
  1453. return window.setTimeout(fn, 20);
  1454. };
  1455. /**
  1456. * Class for dimension change detection.
  1457. *
  1458. * @param {Element|Element[]|Elements|jQuery} element
  1459. * @param {Function} callback
  1460. *
  1461. * @constructor
  1462. */
  1463. var ResizeSensor = function(element, fn) {
  1464. var callback = function()
  1465. {
  1466. if (GraphViewer.resizeSensorEnabled)
  1467. {
  1468. fn();
  1469. }
  1470. };
  1471. /**
  1472. *
  1473. * @constructor
  1474. */
  1475. function EventQueue() {
  1476. this.q = [];
  1477. this.add = function(ev) {
  1478. this.q.push(ev);
  1479. };
  1480. var i, j;
  1481. this.call = function() {
  1482. for (i = 0, j = this.q.length; i < j; i++) {
  1483. this.q[i].call();
  1484. }
  1485. };
  1486. }
  1487. /**
  1488. * @param {HTMLElement} element
  1489. * @param {String} prop
  1490. * @returns {String|Number}
  1491. */
  1492. function getComputedStyle(element, prop) {
  1493. if (element.currentStyle) {
  1494. return element.currentStyle[prop];
  1495. } else if (window.getComputedStyle) {
  1496. return window.getComputedStyle(element, null).getPropertyValue(prop);
  1497. } else {
  1498. return element.style[prop];
  1499. }
  1500. }
  1501. /**
  1502. *
  1503. * @param {HTMLElement} element
  1504. * @param {Function} resized
  1505. */
  1506. function attachResizeEvent(element, resized) {
  1507. if (!element.resizedAttached) {
  1508. element.resizedAttached = new EventQueue();
  1509. element.resizedAttached.add(resized);
  1510. } else if (element.resizedAttached) {
  1511. element.resizedAttached.add(resized);
  1512. return;
  1513. }
  1514. element.resizeSensor = document.createElement('div');
  1515. element.resizeSensor.className = 'resize-sensor';
  1516. var style = 'position: absolute; left: 0; top: 0; right: 0; bottom: 0; overflow: hidden; z-index: -1; visibility: hidden;';
  1517. var styleChild = 'position: absolute; left: 0; top: 0; transition: 0s;';
  1518. element.resizeSensor.style.cssText = style;
  1519. element.resizeSensor.innerHTML =
  1520. '<div class="resize-sensor-expand" style="' + style + '">' +
  1521. '<div style="' + styleChild + '"></div>' +
  1522. '</div>' +
  1523. '<div class="resize-sensor-shrink" style="' + style + '">' +
  1524. '<div style="' + styleChild + ' width: 200%; height: 200%"></div>' +
  1525. '</div>';
  1526. element.appendChild(element.resizeSensor);
  1527. // FIXME: Should not change element style
  1528. if (getComputedStyle(element, 'position') == 'static') {
  1529. element.style.position = 'relative';
  1530. }
  1531. var expand = element.resizeSensor.childNodes[0];
  1532. var expandChild = expand.childNodes[0];
  1533. var shrink = element.resizeSensor.childNodes[1];
  1534. var reset = function() {
  1535. expandChild.style.width = 100000 + 'px';
  1536. expandChild.style.height = 100000 + 'px';
  1537. expand.scrollLeft = 100000;
  1538. expand.scrollTop = 100000;
  1539. shrink.scrollLeft = 100000;
  1540. shrink.scrollTop = 100000;
  1541. };
  1542. reset();
  1543. var dirty = false;
  1544. var dirtyChecking = function() {
  1545. if (!element.resizedAttached) return;
  1546. if (dirty) {
  1547. element.resizedAttached.call();
  1548. dirty = false;
  1549. }
  1550. requestAnimationFrame(dirtyChecking);
  1551. };
  1552. requestAnimationFrame(dirtyChecking);
  1553. var lastWidth, lastHeight;
  1554. var cachedWidth, cachedHeight; //useful to not query offsetWidth twice
  1555. var onScroll = function() {
  1556. if ((cachedWidth = element.offsetWidth) != lastWidth || (cachedHeight = element.offsetHeight) != lastHeight) {
  1557. dirty = true;
  1558. lastWidth = cachedWidth;
  1559. lastHeight = cachedHeight;
  1560. }
  1561. reset();
  1562. };
  1563. var addEvent = function(el, name, cb) {
  1564. if (el.attachEvent) {
  1565. el.attachEvent('on' + name, cb);
  1566. } else {
  1567. el.addEventListener(name, cb);
  1568. }
  1569. };
  1570. addEvent(expand, 'scroll', onScroll);
  1571. addEvent(shrink, 'scroll', onScroll);
  1572. }
  1573. var elementType = Object.prototype.toString.call(element);
  1574. var isCollectionTyped = ('[object Array]' === elementType
  1575. || ('[object NodeList]' === elementType)
  1576. || ('[object HTMLCollection]' === elementType)
  1577. || ('undefined' !== typeof jQuery && element instanceof jQuery) //jquery
  1578. || ('undefined' !== typeof Elements && element instanceof Elements) //mootools
  1579. );
  1580. if (isCollectionTyped) {
  1581. var i = 0, j = element.length;
  1582. for (; i < j; i++) {
  1583. attachResizeEvent(element[i], callback);
  1584. }
  1585. } else {
  1586. attachResizeEvent(element, callback);
  1587. }
  1588. this.detach = function() {
  1589. if (isCollectionTyped) {
  1590. var i = 0, j = element.length;
  1591. for (; i < j; i++) {
  1592. ResizeSensor.detach(element[i]);
  1593. }
  1594. } else {
  1595. ResizeSensor.detach(element);
  1596. }
  1597. };
  1598. };
  1599. ResizeSensor.detach = function(element) {
  1600. if (element.resizeSensor) {
  1601. element.removeChild(element.resizeSensor);
  1602. delete element.resizeSensor;
  1603. delete element.resizedAttached;
  1604. }
  1605. };
  1606. window.ResizeSensor = ResizeSensor;
  1607. })();