GraphViewer.js 46 KB

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