GraphViewer.js 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793
  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. // Hides lightbox if any links are clicked
  947. if (ui != null)
  948. {
  949. ui.destroy();
  950. }
  951. }, mxUtils.bind(this, function(evt)
  952. {
  953. if (ui == null && this.lightboxClickEnabled &&
  954. (!mxEvent.isTouchEvent(evt) ||
  955. this.toolbarItems.length == 0))
  956. {
  957. this.showLightbox();
  958. }
  959. }));
  960. };
  961. /**
  962. * Adds the given array of stencils to avoid dynamic loading of shapes.
  963. */
  964. GraphViewer.prototype.showLightbox = function()
  965. {
  966. if (this.graphConfig.lightbox == 'open' || window.self !== window.top)
  967. {
  968. var p = (this.layersEnabled) ? '&layers=1' : '';
  969. if (typeof window.postMessage !== 'undefined' && (document.documentMode == null || document.documentMode >= 10))
  970. {
  971. var wnd = null;
  972. var receive = mxUtils.bind(this, function(evt)
  973. {
  974. if (evt.data == 'ready' && evt.source == wnd)
  975. {
  976. wnd.postMessage(this.xml, '*');
  977. mxEvent.removeListener(window, 'message', receive);
  978. }
  979. });
  980. mxEvent.addListener(window, 'message', receive);
  981. wnd = window.open('https://www.draw.io/?client=1&chrome=0&lightbox=1&close=1&edit=_blank' + p);
  982. }
  983. else
  984. {
  985. // Data is pulled from global variable after tab loads
  986. window.drawdata = this.xml;
  987. window.open('https://www.draw.io/?client=1&chrome=0&lightbox=1&edit=_blank' + p);
  988. }
  989. }
  990. else
  991. {
  992. this.showLocalLightbox();
  993. }
  994. };
  995. /**
  996. * Adds the given array of stencils to avoid dynamic loading of shapes.
  997. */
  998. GraphViewer.prototype.showLocalLightbox = function()
  999. {
  1000. var origin = mxUtils.getDocumentScrollOrigin(document);
  1001. var backdrop = document.createElement('div');
  1002. var tabContainer = null;
  1003. if (mxClient.IS_QUIRKS)
  1004. {
  1005. backdrop.style.position = 'absolute';
  1006. backdrop.style.left = origin.x + 'px';
  1007. backdrop.style.top = origin.y + 'px';
  1008. backdrop.style.width = document.body.offsetWidth + 'px';
  1009. backdrop.style.height = document.body.offsetHeight + 'px';
  1010. }
  1011. else
  1012. {
  1013. backdrop.style.cssText = 'position:fixed;top:0;left:0;bottom:0;right:0;';
  1014. }
  1015. backdrop.style.zIndex = this.lightboxZIndex;
  1016. backdrop.style.backgroundColor = '#000000';
  1017. mxUtils.setOpacity(backdrop, 70);
  1018. document.body.appendChild(backdrop);
  1019. var closeImg = document.createElement('img');
  1020. closeImg.setAttribute('border', '0');
  1021. closeImg.setAttribute('src', Editor.closeImage);
  1022. if (mxClient.IS_QUIRKS)
  1023. {
  1024. closeImg.style.position = 'absolute';
  1025. closeImg.style.right = 32 + 'px';
  1026. closeImg.style.top = origin.y + 32 + 'px';
  1027. }
  1028. else
  1029. {
  1030. closeImg.style.cssText = 'position:fixed;top:32px;right:32px;';
  1031. }
  1032. closeImg.style.cursor = 'pointer';
  1033. mxEvent.addListener(closeImg, 'click', function()
  1034. {
  1035. ui.destroy();
  1036. });
  1037. // LATER: Make possible to assign after instance was created
  1038. urlParams['pages'] = '1';
  1039. urlParams['page'] = this.currentPage;
  1040. urlParams['nav'] = (this.graphConfig.nav != false) ? '1' : '0';
  1041. urlParams['layers'] = (this.layersEnabled) ? '1' : '0';
  1042. // PostMessage not working and Permission denied for opened access in IE9-
  1043. if (document.documentMode == null || document.documentMode >= 10)
  1044. {
  1045. Editor.prototype.editButtonLink = this.graphConfig.edit;
  1046. }
  1047. EditorUi.prototype.updateActionStates = function() {};
  1048. EditorUi.prototype.addBeforeUnloadListener = function() {};
  1049. EditorUi.prototype.addChromelessClickHandler = function() {};
  1050. // Workaround for lost reference with same ID (cannot override after instance is created)
  1051. Graph.prototype.shadowId = 'lightboxDropShadow';
  1052. var ui = new EditorUi(new Editor(true), document.createElement('div'), true);
  1053. // Workaround for lost reference with same ID
  1054. Graph.prototype.shadowId = 'dropShadow';
  1055. // Disables refresh
  1056. ui.refresh = function() {};
  1057. // Click on backdrop closes lightbox
  1058. mxEvent.addListener(backdrop, 'click', function()
  1059. {
  1060. ui.destroy();
  1061. });
  1062. // Passes current page and local URLs to open in new window action
  1063. ui.editor.editBlankUrl = this.editBlankUrl;
  1064. ui.editor.editBlankFallbackUrl = this.editBlankFallbackUrl;
  1065. var editorGetEditBlankUrl = ui.editor.getEditBlankUrl;
  1066. ui.editor.getEditBlankUrl = function(params, fallback)
  1067. {
  1068. var param = '';
  1069. if (ui.pages != null && ui.currentPage != null)
  1070. {
  1071. var pageIndex = mxUtils.indexOf(ui.pages, ui.currentPage);
  1072. if (pageIndex > 0)
  1073. {
  1074. param = '&page=' + pageIndex;
  1075. }
  1076. }
  1077. return editorGetEditBlankUrl.apply(this, arguments) + param;
  1078. }
  1079. // Handles escape keystroke
  1080. var keydownHandler = mxUtils.bind(this, function(evt)
  1081. {
  1082. if (evt.keyCode == 27 /* Escape */)
  1083. {
  1084. ui.destroy();
  1085. }
  1086. });
  1087. var destroy = ui.destroy;
  1088. ui.destroy = function()
  1089. {
  1090. mxEvent.removeListener(document.documentElement, 'keydown', keydownHandler);
  1091. document.body.removeChild(backdrop);
  1092. document.body.removeChild(closeImg);
  1093. document.body.style.overflow = 'auto';
  1094. GraphViewer.resizeSensorEnabled = true;
  1095. destroy.apply(this, arguments);
  1096. };
  1097. var graph = ui.editor.graph;
  1098. var lightbox = graph.container;
  1099. lightbox.style.overflow = 'hidden';
  1100. if (this.lightboxChrome)
  1101. {
  1102. lightbox.style.border = '1px solid #c0c0c0';
  1103. lightbox.style.margin = '40px';
  1104. // Installs the keystroke listener in the target
  1105. mxEvent.addListener(document.documentElement, 'keydown', keydownHandler);
  1106. }
  1107. else
  1108. {
  1109. backdrop.style.display = 'none';
  1110. closeImg.style.display = 'none';
  1111. }
  1112. // Handles relative images
  1113. var self = this;
  1114. graph.getImageFromBundles = function(key)
  1115. {
  1116. return self.getImageUrl(key);
  1117. };
  1118. if (this.graphConfig.move)
  1119. {
  1120. graph.isMoveCellsEvent = function(evt)
  1121. {
  1122. return true;
  1123. };
  1124. }
  1125. if (!mxClient.IS_QUIRKS)
  1126. {
  1127. mxUtils.setPrefixedStyle(lightbox.style, 'border-radius', '4px');
  1128. lightbox.style.position = 'fixed';
  1129. }
  1130. GraphViewer.resizeSensorEnabled = false;
  1131. document.body.style.overflow = 'hidden';
  1132. // Workaround for possible rendering issues in Safari
  1133. if (!mxClient.IS_SF)
  1134. {
  1135. mxUtils.setPrefixedStyle(lightbox.style, 'transform', 'rotateY(90deg)');
  1136. mxUtils.setPrefixedStyle(lightbox.style, 'transition', 'all .25s ease-in-out');
  1137. }
  1138. this.addClickHandler(graph, ui);
  1139. window.setTimeout(mxUtils.bind(this, function()
  1140. {
  1141. // Disables focus border in Chrome
  1142. lightbox.style.outline = 'none';
  1143. lightbox.style.zIndex = this.lightboxZIndex;
  1144. closeImg.style.zIndex = this.lightboxZIndex;
  1145. document.body.appendChild(lightbox);
  1146. document.body.appendChild(closeImg);
  1147. ui.setFileData(this.xml);
  1148. mxUtils.setPrefixedStyle(lightbox.style, 'transform', 'rotateY(0deg)');
  1149. ui.chromelessToolbar.style.bottom = 60 + 'px';
  1150. ui.chromelessToolbar.style.zIndex = this.lightboxZIndex;
  1151. // Workaround for clipping in IE11-
  1152. document.body.appendChild(ui.chromelessToolbar);
  1153. ui.getEditBlankXml = mxUtils.bind(this, function()
  1154. {
  1155. return this.xml;
  1156. });
  1157. if (mxClient.IS_QUIRKS)
  1158. {
  1159. lightbox.style.position = 'absolute';
  1160. lightbox.style.display = 'block';
  1161. lightbox.style.left = origin.x + 'px';
  1162. lightbox.style.top = origin.y + 'px';
  1163. lightbox.style.width = document.body.clientWidth - 80 + 'px';
  1164. lightbox.style.height = document.body.clientHeight - 80 + 'px';
  1165. lightbox.style.backgroundColor = 'white';
  1166. ui.chromelessToolbar.style.display = 'block';
  1167. ui.chromelessToolbar.style.position = 'absolute';
  1168. ui.chromelessToolbar.style.bottom = '';
  1169. ui.chromelessToolbar.style.top = origin.y +
  1170. document.body.clientHeight - 100 + 'px';
  1171. }
  1172. ui.lightboxFit();
  1173. ui.chromelessResize();
  1174. this.showLayers(graph, this.graph);
  1175. }), 0);
  1176. return ui;
  1177. };
  1178. /**
  1179. *
  1180. */
  1181. GraphViewer.processElements = function(classname)
  1182. {
  1183. mxUtils.forEach(GraphViewer.getElementsByClassName(classname || 'mxgraph'), function(div)
  1184. {
  1185. try
  1186. {
  1187. div.innerHTML = '';
  1188. GraphViewer.createViewerForElement(div);
  1189. }
  1190. catch (e)
  1191. {
  1192. div.innerHTML = e.message;
  1193. throw e;
  1194. }
  1195. });
  1196. };
  1197. /**
  1198. * Adds the given array of stencils to avoid dynamic loading of shapes.
  1199. */
  1200. GraphViewer.getElementsByClassName = function(classname)
  1201. {
  1202. if (document.getElementsByClassName)
  1203. {
  1204. var divs = document.getElementsByClassName(classname);
  1205. // Workaround for changing divs while processing
  1206. var result = [];
  1207. for (var i = 0; i < divs.length; i++)
  1208. {
  1209. result.push(divs[i]);
  1210. }
  1211. return result;
  1212. }
  1213. else
  1214. {
  1215. var tmp = document.getElementsByTagName('*');
  1216. var divs = [];
  1217. for (var i = 0; i < tmp.length; i++)
  1218. {
  1219. var cls = tmp[i].className;
  1220. if (cls != null && cls.length > 0)
  1221. {
  1222. var tokens = cls.split(' ');
  1223. if (mxUtils.indexOf(tokens, classname) >= 0)
  1224. {
  1225. divs.push(tmp[i]);
  1226. }
  1227. }
  1228. }
  1229. return divs;
  1230. }
  1231. };
  1232. /**
  1233. * Adds the given array of stencils to avoid dynamic loading of shapes.
  1234. */
  1235. GraphViewer.createViewerForElement = function(element, callback)
  1236. {
  1237. var data = element.getAttribute('data-mxgraph');
  1238. if (data != null)
  1239. {
  1240. var config = JSON.parse(data);
  1241. var createViewer = function(xml)
  1242. {
  1243. var xmlDoc = mxUtils.parseXml(xml);
  1244. var viewer = new GraphViewer(element, xmlDoc.documentElement, config);
  1245. if (callback != null)
  1246. {
  1247. callback(viewer);
  1248. }
  1249. };
  1250. if (config.url != null)
  1251. {
  1252. GraphViewer.getUrl(config.url, function(xml)
  1253. {
  1254. createViewer(xml);
  1255. });
  1256. }
  1257. else
  1258. {
  1259. createViewer(config.xml);
  1260. }
  1261. }
  1262. };
  1263. /**
  1264. * Adds event if grid size is changed.
  1265. */
  1266. GraphViewer.initCss = function()
  1267. {
  1268. try
  1269. {
  1270. var style = document.createElement('style')
  1271. style.type = 'text/css'
  1272. style.innerHTML = ['div.mxTooltip {',
  1273. '-webkit-box-shadow: 3px 3px 12px #C0C0C0;',
  1274. '-moz-box-shadow: 3px 3px 12px #C0C0C0;',
  1275. 'box-shadow: 3px 3px 12px #C0C0C0;',
  1276. 'background: #FFFFCC;',
  1277. 'border-style: solid;',
  1278. 'border-width: 1px;',
  1279. 'border-color: black;',
  1280. 'font-family: Arial;',
  1281. 'font-size: 8pt;',
  1282. 'position: absolute;',
  1283. 'cursor: default;',
  1284. 'padding: 4px;',
  1285. 'color: black;}',
  1286. 'td.mxPopupMenuIcon div {',
  1287. 'width:16px;',
  1288. 'height:16px;}',
  1289. 'html div.mxPopupMenu {',
  1290. '-webkit-box-shadow:2px 2px 3px #d5d5d5;',
  1291. '-moz-box-shadow:2px 2px 3px #d5d5d5;',
  1292. 'box-shadow:2px 2px 3px #d5d5d5;',
  1293. '_filter:progid:DXImageTransform.Microsoft.DropShadow(OffX=2, OffY=2, Color=\'#d0d0d0\',Positive=\'true\');',
  1294. 'background:white;',
  1295. 'position:absolute;',
  1296. 'border:3px solid #e7e7e7;',
  1297. 'padding:3px;}',
  1298. 'html table.mxPopupMenu {',
  1299. 'border-collapse:collapse;',
  1300. 'margin:0px;}',
  1301. 'html td.mxPopupMenuItem {',
  1302. 'padding:7px 30px 7px 30px;',
  1303. 'font-family:Helvetica Neue,Helvetica,Arial Unicode MS,Arial;',
  1304. 'font-size:10pt;}',
  1305. 'html td.mxPopupMenuIcon {',
  1306. 'background-color:white;',
  1307. 'padding:0px;}',
  1308. 'td.mxPopupMenuIcon .geIcon {',
  1309. 'padding:2px;',
  1310. 'padding-bottom:4px;',
  1311. 'margin:2px;',
  1312. 'border:1px solid transparent;',
  1313. 'opacity:0.5;',
  1314. '_width:26px;',
  1315. '_height:26px;}',
  1316. 'td.mxPopupMenuIcon .geIcon:hover {',
  1317. 'border:1px solid gray;',
  1318. 'border-radius:2px;',
  1319. 'opacity:1;}',
  1320. 'html tr.mxPopupMenuItemHover {',
  1321. 'background-color: #eeeeee;',
  1322. 'color: black;}',
  1323. 'table.mxPopupMenu hr {',
  1324. 'color:#cccccc;',
  1325. 'background-color:#cccccc;',
  1326. 'border:none;',
  1327. 'height:1px;}',
  1328. 'table.mxPopupMenu tr { font-size:4pt;}'].join('\n');
  1329. document.getElementsByTagName('head')[0].appendChild(style)
  1330. }
  1331. catch (e)
  1332. {
  1333. // ignore
  1334. }
  1335. };
  1336. /**
  1337. * Lookup for URLs.
  1338. */
  1339. GraphViewer.cachedUrls = {};
  1340. /**
  1341. * Workaround for unsupported CORS in IE9 XHR
  1342. */
  1343. GraphViewer.getUrl = function(url, onload, onerror)
  1344. {
  1345. if (GraphViewer.cachedUrls[url] != null)
  1346. {
  1347. onload(GraphViewer.cachedUrls[url]);
  1348. }
  1349. else
  1350. {
  1351. var xhr = (navigator.userAgent.indexOf('MSIE 9') > 0) ? new XDomainRequest() : new XMLHttpRequest();
  1352. xhr.open('GET', url);
  1353. xhr.onload = function()
  1354. {
  1355. onload((xhr.getText != null) ? xhr.getText() : xhr.responseText);
  1356. };
  1357. xhr.onerror = onerror;
  1358. xhr.send();
  1359. }
  1360. };
  1361. /**
  1362. * Redirects editing to absolue URLs.
  1363. */
  1364. GraphViewer.resizeSensorEnabled = true;
  1365. /**
  1366. * Copyright Marc J. Schmidt. See the LICENSE file at the top-level
  1367. * directory of this distribution and at
  1368. * https://github.com/marcj/css-element-queries/blob/master/LICENSE.
  1369. */
  1370. (function() {
  1371. // Only used for the dirty checking, so the event callback count is limted to max 1 call per fps per sensor.
  1372. // In combination with the event based resize sensor this saves cpu time, because the sensor is too fast and
  1373. // would generate too many unnecessary events.
  1374. var requestAnimationFrame = window.requestAnimationFrame ||
  1375. window.mozRequestAnimationFrame ||
  1376. window.webkitRequestAnimationFrame ||
  1377. function (fn) {
  1378. return window.setTimeout(fn, 20);
  1379. };
  1380. /**
  1381. * Class for dimension change detection.
  1382. *
  1383. * @param {Element|Element[]|Elements|jQuery} element
  1384. * @param {Function} callback
  1385. *
  1386. * @constructor
  1387. */
  1388. var ResizeSensor = function(element, fn) {
  1389. var callback = function()
  1390. {
  1391. if (GraphViewer.resizeSensorEnabled)
  1392. {
  1393. fn();
  1394. }
  1395. };
  1396. /**
  1397. *
  1398. * @constructor
  1399. */
  1400. function EventQueue() {
  1401. this.q = [];
  1402. this.add = function(ev) {
  1403. this.q.push(ev);
  1404. };
  1405. var i, j;
  1406. this.call = function() {
  1407. for (i = 0, j = this.q.length; i < j; i++) {
  1408. this.q[i].call();
  1409. }
  1410. };
  1411. }
  1412. /**
  1413. * @param {HTMLElement} element
  1414. * @param {String} prop
  1415. * @returns {String|Number}
  1416. */
  1417. function getComputedStyle(element, prop) {
  1418. if (element.currentStyle) {
  1419. return element.currentStyle[prop];
  1420. } else if (window.getComputedStyle) {
  1421. return window.getComputedStyle(element, null).getPropertyValue(prop);
  1422. } else {
  1423. return element.style[prop];
  1424. }
  1425. }
  1426. /**
  1427. *
  1428. * @param {HTMLElement} element
  1429. * @param {Function} resized
  1430. */
  1431. function attachResizeEvent(element, resized) {
  1432. if (!element.resizedAttached) {
  1433. element.resizedAttached = new EventQueue();
  1434. element.resizedAttached.add(resized);
  1435. } else if (element.resizedAttached) {
  1436. element.resizedAttached.add(resized);
  1437. return;
  1438. }
  1439. element.resizeSensor = document.createElement('div');
  1440. element.resizeSensor.className = 'resize-sensor';
  1441. var style = 'position: absolute; left: 0; top: 0; right: 0; bottom: 0; overflow: hidden; z-index: -1; visibility: hidden;';
  1442. var styleChild = 'position: absolute; left: 0; top: 0; transition: 0s;';
  1443. element.resizeSensor.style.cssText = style;
  1444. element.resizeSensor.innerHTML =
  1445. '<div class="resize-sensor-expand" style="' + style + '">' +
  1446. '<div style="' + styleChild + '"></div>' +
  1447. '</div>' +
  1448. '<div class="resize-sensor-shrink" style="' + style + '">' +
  1449. '<div style="' + styleChild + ' width: 200%; height: 200%"></div>' +
  1450. '</div>';
  1451. element.appendChild(element.resizeSensor);
  1452. // FIXME: Should not change element style
  1453. if (getComputedStyle(element, 'position') == 'static') {
  1454. element.style.position = 'relative';
  1455. }
  1456. var expand = element.resizeSensor.childNodes[0];
  1457. var expandChild = expand.childNodes[0];
  1458. var shrink = element.resizeSensor.childNodes[1];
  1459. var reset = function() {
  1460. expandChild.style.width = 100000 + 'px';
  1461. expandChild.style.height = 100000 + 'px';
  1462. expand.scrollLeft = 100000;
  1463. expand.scrollTop = 100000;
  1464. shrink.scrollLeft = 100000;
  1465. shrink.scrollTop = 100000;
  1466. };
  1467. reset();
  1468. var dirty = false;
  1469. var dirtyChecking = function() {
  1470. if (!element.resizedAttached) return;
  1471. if (dirty) {
  1472. element.resizedAttached.call();
  1473. dirty = false;
  1474. }
  1475. requestAnimationFrame(dirtyChecking);
  1476. };
  1477. requestAnimationFrame(dirtyChecking);
  1478. var lastWidth, lastHeight;
  1479. var cachedWidth, cachedHeight; //useful to not query offsetWidth twice
  1480. var onScroll = function() {
  1481. if ((cachedWidth = element.offsetWidth) != lastWidth || (cachedHeight = element.offsetHeight) != lastHeight) {
  1482. dirty = true;
  1483. lastWidth = cachedWidth;
  1484. lastHeight = cachedHeight;
  1485. }
  1486. reset();
  1487. };
  1488. var addEvent = function(el, name, cb) {
  1489. if (el.attachEvent) {
  1490. el.attachEvent('on' + name, cb);
  1491. } else {
  1492. el.addEventListener(name, cb);
  1493. }
  1494. };
  1495. addEvent(expand, 'scroll', onScroll);
  1496. addEvent(shrink, 'scroll', onScroll);
  1497. }
  1498. var elementType = Object.prototype.toString.call(element);
  1499. var isCollectionTyped = ('[object Array]' === elementType
  1500. || ('[object NodeList]' === elementType)
  1501. || ('[object HTMLCollection]' === elementType)
  1502. || ('undefined' !== typeof jQuery && element instanceof jQuery) //jquery
  1503. || ('undefined' !== typeof Elements && element instanceof Elements) //mootools
  1504. );
  1505. if (isCollectionTyped) {
  1506. var i = 0, j = element.length;
  1507. for (; i < j; i++) {
  1508. attachResizeEvent(element[i], callback);
  1509. }
  1510. } else {
  1511. attachResizeEvent(element, callback);
  1512. }
  1513. this.detach = function() {
  1514. if (isCollectionTyped) {
  1515. var i = 0, j = element.length;
  1516. for (; i < j; i++) {
  1517. ResizeSensor.detach(element[i]);
  1518. }
  1519. } else {
  1520. ResizeSensor.detach(element);
  1521. }
  1522. };
  1523. };
  1524. ResizeSensor.detach = function(element) {
  1525. if (element.resizeSensor) {
  1526. element.removeChild(element.resizeSensor);
  1527. delete element.resizeSensor;
  1528. delete element.resizedAttached;
  1529. }
  1530. };
  1531. window.ResizeSensor = ResizeSensor;
  1532. })();