GraphViewer.js 45 KB

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