GraphViewer.js 44 KB

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