GraphViewer.js 50 KB

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